OLD | NEW |
(Empty) | |
| 1 //===-- ExpandUtils.cpp - Helper functions for expansion passes -----------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 |
| 10 #include "llvm/IR/BasicBlock.h" |
| 11 #include "llvm/IR/Constants.h" |
| 12 #include "llvm/IR/Function.h" |
| 13 #include "llvm/IR/Instructions.h" |
| 14 #include "llvm/IR/Module.h" |
| 15 #include "llvm/Support/raw_ostream.h" |
| 16 #include "llvm/Transforms/NaCl.h" |
| 17 |
| 18 using namespace llvm; |
| 19 |
| 20 Instruction *llvm::PhiSafeInsertPt(Use *U) { |
| 21 Instruction *InsertPt = cast<Instruction>(U->getUser()); |
| 22 if (PHINode *PN = dyn_cast<PHINode>(InsertPt)) { |
| 23 // We cannot insert instructions before a PHI node, so insert |
| 24 // before the incoming block's terminator. This could be |
| 25 // suboptimal if the terminator is a conditional. |
| 26 InsertPt = PN->getIncomingBlock(*U)->getTerminator(); |
| 27 } |
| 28 return InsertPt; |
| 29 } |
| 30 |
| 31 void llvm::PhiSafeReplaceUses(Use *U, Value *NewVal) { |
| 32 User *UR = U->getUser(); |
| 33 if (PHINode *PN = dyn_cast<PHINode>(UR)) { |
| 34 // A PHI node can have multiple incoming edges from the same |
| 35 // block, in which case all these edges must have the same |
| 36 // incoming value. |
| 37 BasicBlock *BB = PN->getIncomingBlock(*U); |
| 38 for (unsigned I = 0; I < PN->getNumIncomingValues(); ++I) { |
| 39 if (PN->getIncomingBlock(I) == BB) |
| 40 PN->setIncomingValue(I, NewVal); |
| 41 } |
| 42 } else { |
| 43 UR->replaceUsesOfWith(U->get(), NewVal); |
| 44 } |
| 45 } |
| 46 |
| 47 Function *llvm::RecreateFunction(Function *Func, FunctionType *NewType) { |
| 48 Function *NewFunc = Function::Create(NewType, Func->getLinkage()); |
| 49 NewFunc->copyAttributesFrom(Func); |
| 50 Func->getParent()->getFunctionList().insert(Func, NewFunc); |
| 51 NewFunc->takeName(Func); |
| 52 NewFunc->getBasicBlockList().splice(NewFunc->begin(), |
| 53 Func->getBasicBlockList()); |
| 54 Func->replaceAllUsesWith( |
| 55 ConstantExpr::getBitCast(NewFunc, |
| 56 Func->getFunctionType()->getPointerTo())); |
| 57 return NewFunc; |
| 58 } |
OLD | NEW |