CloneFunction.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. //===- CloneFunction.cpp - Clone a function into another function ---------===//
  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. // This file implements the CloneFunctionInto interface, which is used as the
  11. // low-level function cloner. This is used by the CloneFunction and function
  12. // inliner to do the dirty work of copying the body of a function around.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/Utils/Cloning.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Analysis/ConstantFolding.h"
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/IR/CFG.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DebugInfo.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/GlobalVariable.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/IntrinsicInst.h"
  27. #include "llvm/IR/LLVMContext.h"
  28. #include "llvm/IR/Metadata.h"
  29. #include "llvm/IR/Module.h"
  30. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  31. #include "llvm/Transforms/Utils/Local.h"
  32. #include "llvm/Transforms/Utils/ValueMapper.h"
  33. #include <map>
  34. using namespace llvm;
  35. // CloneBasicBlock - See comments in Cloning.h
  36. BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
  37. ValueToValueMapTy &VMap,
  38. const Twine &NameSuffix, Function *F,
  39. ClonedCodeInfo *CodeInfo) {
  40. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
  41. if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
  42. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  43. // Loop over all instructions, and copy them over.
  44. for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
  45. II != IE; ++II) {
  46. Instruction *NewInst = II->clone();
  47. if (II->hasName())
  48. NewInst->setName(II->getName()+NameSuffix);
  49. NewBB->getInstList().push_back(NewInst);
  50. VMap[II] = NewInst; // Add instruction map to value.
  51. hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
  52. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  53. if (isa<ConstantInt>(AI->getArraySize()))
  54. hasStaticAllocas = true;
  55. else
  56. hasDynamicAllocas = true;
  57. }
  58. }
  59. if (CodeInfo) {
  60. CodeInfo->ContainsCalls |= hasCalls;
  61. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  62. CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
  63. BB != &BB->getParent()->getEntryBlock();
  64. }
  65. return NewBB;
  66. }
  67. // Clone OldFunc into NewFunc, transforming the old arguments into references to
  68. // VMap values.
  69. //
  70. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  71. ValueToValueMapTy &VMap,
  72. bool ModuleLevelChanges,
  73. SmallVectorImpl<ReturnInst*> &Returns,
  74. const char *NameSuffix, ClonedCodeInfo *CodeInfo,
  75. ValueMapTypeRemapper *TypeMapper,
  76. ValueMaterializer *Materializer) {
  77. assert(NameSuffix && "NameSuffix cannot be null!");
  78. #ifndef NDEBUG
  79. for (Function::const_arg_iterator I = OldFunc->arg_begin(),
  80. E = OldFunc->arg_end(); I != E; ++I)
  81. assert(VMap.count(I) && "No mapping from source argument specified!");
  82. #endif
  83. // Copy all attributes other than those stored in the AttributeSet. We need
  84. // to remap the parameter indices of the AttributeSet.
  85. AttributeSet NewAttrs = NewFunc->getAttributes();
  86. NewFunc->copyAttributesFrom(OldFunc);
  87. NewFunc->setAttributes(NewAttrs);
  88. AttributeSet OldAttrs = OldFunc->getAttributes();
  89. // Clone any argument attributes that are present in the VMap.
  90. for (const Argument &OldArg : OldFunc->args())
  91. if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
  92. AttributeSet attrs =
  93. OldAttrs.getParamAttributes(OldArg.getArgNo() + 1);
  94. if (attrs.getNumSlots() > 0)
  95. NewArg->addAttr(attrs);
  96. }
  97. NewFunc->setAttributes(
  98. NewFunc->getAttributes()
  99. .addAttributes(NewFunc->getContext(), AttributeSet::ReturnIndex,
  100. OldAttrs.getRetAttributes())
  101. .addAttributes(NewFunc->getContext(), AttributeSet::FunctionIndex,
  102. OldAttrs.getFnAttributes()));
  103. // Loop over all of the basic blocks in the function, cloning them as
  104. // appropriate. Note that we save BE this way in order to handle cloning of
  105. // recursive functions into themselves.
  106. //
  107. for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
  108. BI != BE; ++BI) {
  109. const BasicBlock &BB = *BI;
  110. // Create a new basic block and copy instructions into it!
  111. BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
  112. // Add basic block mapping.
  113. VMap[&BB] = CBB;
  114. // It is only legal to clone a function if a block address within that
  115. // function is never referenced outside of the function. Given that, we
  116. // want to map block addresses from the old function to block addresses in
  117. // the clone. (This is different from the generic ValueMapper
  118. // implementation, which generates an invalid blockaddress when
  119. // cloning a function.)
  120. if (BB.hasAddressTaken()) {
  121. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  122. const_cast<BasicBlock*>(&BB));
  123. VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
  124. }
  125. // Note return instructions for the caller.
  126. if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
  127. Returns.push_back(RI);
  128. }
  129. // Loop over all of the instructions in the function, fixing up operand
  130. // references as we go. This uses VMap to do all the hard work.
  131. for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]),
  132. BE = NewFunc->end(); BB != BE; ++BB)
  133. // Loop over all instructions, fixing each one as we find it...
  134. for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
  135. RemapInstruction(II, VMap,
  136. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  137. TypeMapper, Materializer);
  138. }
  139. // Find the MDNode which corresponds to the DISubprogram data that described F.
  140. static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) {
  141. for (DISubprogram Subprogram : Finder.subprograms()) {
  142. if (Subprogram.describes(F)) return Subprogram;
  143. }
  144. return nullptr;
  145. }
  146. // Add an operand to an existing MDNode. The new operand will be added at the
  147. // back of the operand list.
  148. static void AddOperand(DICompileUnit CU, DIArray SPs, Metadata *NewSP) {
  149. SmallVector<Metadata *, 16> NewSPs;
  150. NewSPs.reserve(SPs->getNumOperands() + 1);
  151. for (unsigned I = 0, E = SPs->getNumOperands(); I != E; ++I)
  152. NewSPs.push_back(SPs->getOperand(I));
  153. NewSPs.push_back(NewSP);
  154. CU.replaceSubprograms(DIArray(MDNode::get(CU->getContext(), NewSPs)));
  155. }
  156. // Clone the module-level debug info associated with OldFunc. The cloned data
  157. // will point to NewFunc instead.
  158. static void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc,
  159. ValueToValueMapTy &VMap) {
  160. DebugInfoFinder Finder;
  161. Finder.processModule(*OldFunc->getParent());
  162. const MDNode *OldSubprogramMDNode = FindSubprogram(OldFunc, Finder);
  163. if (!OldSubprogramMDNode) return;
  164. // Ensure that OldFunc appears in the map.
  165. // (if it's already there it must point to NewFunc anyway)
  166. VMap[OldFunc] = NewFunc;
  167. DISubprogram NewSubprogram(MapValue(OldSubprogramMDNode, VMap));
  168. for (DICompileUnit CU : Finder.compile_units()) {
  169. DIArray Subprograms(CU.getSubprograms());
  170. // If the compile unit's function list contains the old function, it should
  171. // also contain the new one.
  172. for (unsigned i = 0; i < Subprograms.getNumElements(); i++) {
  173. if ((MDNode*)Subprograms.getElement(i) == OldSubprogramMDNode) {
  174. AddOperand(CU, Subprograms, NewSubprogram);
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. /// CloneFunction - Return a copy of the specified function, but without
  181. /// embedding the function into another module. Also, any references specified
  182. /// in the VMap are changed to refer to their mapped value instead of the
  183. /// original one. If any of the arguments to the function are in the VMap,
  184. /// the arguments are deleted from the resultant function. The VMap is
  185. /// updated to include mappings from all of the instructions and basicblocks in
  186. /// the function from their old to new values.
  187. ///
  188. Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
  189. bool ModuleLevelChanges,
  190. ClonedCodeInfo *CodeInfo) {
  191. std::vector<Type*> ArgTypes;
  192. // The user might be deleting arguments to the function by specifying them in
  193. // the VMap. If so, we need to not add the arguments to the arg ty vector
  194. //
  195. for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
  196. I != E; ++I)
  197. if (VMap.count(I) == 0) // Haven't mapped the argument to anything yet?
  198. ArgTypes.push_back(I->getType());
  199. // Create a new function type...
  200. FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
  201. ArgTypes, F->getFunctionType()->isVarArg());
  202. // Create the new function...
  203. Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
  204. // Loop over the arguments, copying the names of the mapped arguments over...
  205. Function::arg_iterator DestI = NewF->arg_begin();
  206. for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
  207. I != E; ++I)
  208. if (VMap.count(I) == 0) { // Is this argument preserved?
  209. DestI->setName(I->getName()); // Copy the name over...
  210. VMap[I] = DestI++; // Add mapping to VMap
  211. }
  212. if (ModuleLevelChanges)
  213. CloneDebugInfoMetadata(NewF, F, VMap);
  214. SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
  215. CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
  216. return NewF;
  217. }
  218. namespace {
  219. /// PruningFunctionCloner - This class is a private class used to implement
  220. /// the CloneAndPruneFunctionInto method.
  221. struct PruningFunctionCloner {
  222. Function *NewFunc;
  223. const Function *OldFunc;
  224. ValueToValueMapTy &VMap;
  225. bool ModuleLevelChanges;
  226. const char *NameSuffix;
  227. ClonedCodeInfo *CodeInfo;
  228. const DataLayout *DL;
  229. public:
  230. PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
  231. ValueToValueMapTy &valueMap,
  232. bool moduleLevelChanges,
  233. const char *nameSuffix,
  234. ClonedCodeInfo *codeInfo,
  235. const DataLayout *DL)
  236. : NewFunc(newFunc), OldFunc(oldFunc),
  237. VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
  238. NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL) {
  239. }
  240. /// CloneBlock - The specified block is found to be reachable, clone it and
  241. /// anything that it can reach.
  242. void CloneBlock(const BasicBlock *BB,
  243. std::vector<const BasicBlock*> &ToClone);
  244. };
  245. }
  246. /// CloneBlock - The specified block is found to be reachable, clone it and
  247. /// anything that it can reach.
  248. void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
  249. std::vector<const BasicBlock*> &ToClone){
  250. WeakVH &BBEntry = VMap[BB];
  251. // Have we already cloned this block?
  252. if (BBEntry) return;
  253. // Nope, clone it now.
  254. BasicBlock *NewBB;
  255. BBEntry = NewBB = BasicBlock::Create(BB->getContext());
  256. if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
  257. // It is only legal to clone a function if a block address within that
  258. // function is never referenced outside of the function. Given that, we
  259. // want to map block addresses from the old function to block addresses in
  260. // the clone. (This is different from the generic ValueMapper
  261. // implementation, which generates an invalid blockaddress when
  262. // cloning a function.)
  263. //
  264. // Note that we don't need to fix the mapping for unreachable blocks;
  265. // the default mapping there is safe.
  266. if (BB->hasAddressTaken()) {
  267. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  268. const_cast<BasicBlock*>(BB));
  269. VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
  270. }
  271. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  272. // Loop over all instructions, and copy them over, DCE'ing as we go. This
  273. // loop doesn't include the terminator.
  274. for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end();
  275. II != IE; ++II) {
  276. Instruction *NewInst = II->clone();
  277. // Eagerly remap operands to the newly cloned instruction, except for PHI
  278. // nodes for which we defer processing until we update the CFG.
  279. if (!isa<PHINode>(NewInst)) {
  280. RemapInstruction(NewInst, VMap,
  281. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  282. // If we can simplify this instruction to some other value, simply add
  283. // a mapping to that value rather than inserting a new instruction into
  284. // the basic block.
  285. if (Value *V = SimplifyInstruction(NewInst, DL)) {
  286. // On the off-chance that this simplifies to an instruction in the old
  287. // function, map it back into the new function.
  288. if (Value *MappedV = VMap.lookup(V))
  289. V = MappedV;
  290. VMap[II] = V;
  291. delete NewInst;
  292. continue;
  293. }
  294. }
  295. if (II->hasName())
  296. NewInst->setName(II->getName()+NameSuffix);
  297. VMap[II] = NewInst; // Add instruction map to value.
  298. NewBB->getInstList().push_back(NewInst);
  299. hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
  300. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  301. if (isa<ConstantInt>(AI->getArraySize()))
  302. hasStaticAllocas = true;
  303. else
  304. hasDynamicAllocas = true;
  305. }
  306. }
  307. // Finally, clone over the terminator.
  308. const TerminatorInst *OldTI = BB->getTerminator();
  309. bool TerminatorDone = false;
  310. if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
  311. if (BI->isConditional()) {
  312. // If the condition was a known constant in the callee...
  313. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  314. // Or is a known constant in the caller...
  315. if (!Cond) {
  316. Value *V = VMap[BI->getCondition()];
  317. Cond = dyn_cast_or_null<ConstantInt>(V);
  318. }
  319. // Constant fold to uncond branch!
  320. if (Cond) {
  321. BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
  322. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  323. ToClone.push_back(Dest);
  324. TerminatorDone = true;
  325. }
  326. }
  327. } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
  328. // If switching on a value known constant in the caller.
  329. ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
  330. if (!Cond) { // Or known constant after constant prop in the callee...
  331. Value *V = VMap[SI->getCondition()];
  332. Cond = dyn_cast_or_null<ConstantInt>(V);
  333. }
  334. if (Cond) { // Constant fold to uncond branch!
  335. SwitchInst::ConstCaseIt Case = SI->findCaseValue(Cond);
  336. BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
  337. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  338. ToClone.push_back(Dest);
  339. TerminatorDone = true;
  340. }
  341. }
  342. if (!TerminatorDone) {
  343. Instruction *NewInst = OldTI->clone();
  344. if (OldTI->hasName())
  345. NewInst->setName(OldTI->getName()+NameSuffix);
  346. NewBB->getInstList().push_back(NewInst);
  347. VMap[OldTI] = NewInst; // Add instruction map to value.
  348. // Recursively clone any reachable successor blocks.
  349. const TerminatorInst *TI = BB->getTerminator();
  350. for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
  351. ToClone.push_back(TI->getSuccessor(i));
  352. }
  353. if (CodeInfo) {
  354. CodeInfo->ContainsCalls |= hasCalls;
  355. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  356. CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
  357. BB != &BB->getParent()->front();
  358. }
  359. }
  360. /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
  361. /// except that it does some simple constant prop and DCE on the fly. The
  362. /// effect of this is to copy significantly less code in cases where (for
  363. /// example) a function call with constant arguments is inlined, and those
  364. /// constant arguments cause a significant amount of code in the callee to be
  365. /// dead. Since this doesn't produce an exact copy of the input, it can't be
  366. /// used for things like CloneFunction or CloneModule.
  367. void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
  368. ValueToValueMapTy &VMap,
  369. bool ModuleLevelChanges,
  370. SmallVectorImpl<ReturnInst*> &Returns,
  371. const char *NameSuffix,
  372. ClonedCodeInfo *CodeInfo,
  373. const DataLayout *DL,
  374. Instruction *TheCall) {
  375. assert(NameSuffix && "NameSuffix cannot be null!");
  376. #ifndef NDEBUG
  377. for (Function::const_arg_iterator II = OldFunc->arg_begin(),
  378. E = OldFunc->arg_end(); II != E; ++II)
  379. assert(VMap.count(II) && "No mapping from source argument specified!");
  380. #endif
  381. PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
  382. NameSuffix, CodeInfo, DL);
  383. // Clone the entry block, and anything recursively reachable from it.
  384. std::vector<const BasicBlock*> CloneWorklist;
  385. CloneWorklist.push_back(&OldFunc->getEntryBlock());
  386. while (!CloneWorklist.empty()) {
  387. const BasicBlock *BB = CloneWorklist.back();
  388. CloneWorklist.pop_back();
  389. PFC.CloneBlock(BB, CloneWorklist);
  390. }
  391. // Loop over all of the basic blocks in the old function. If the block was
  392. // reachable, we have cloned it and the old block is now in the value map:
  393. // insert it into the new function in the right order. If not, ignore it.
  394. //
  395. // Defer PHI resolution until rest of function is resolved.
  396. SmallVector<const PHINode*, 16> PHIToResolve;
  397. for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
  398. BI != BE; ++BI) {
  399. Value *V = VMap[BI];
  400. BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
  401. if (!NewBB) continue; // Dead block.
  402. // Add the new block to the new function.
  403. NewFunc->getBasicBlockList().push_back(NewBB);
  404. // Handle PHI nodes specially, as we have to remove references to dead
  405. // blocks.
  406. for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I)
  407. if (const PHINode *PN = dyn_cast<PHINode>(I))
  408. PHIToResolve.push_back(PN);
  409. else
  410. break;
  411. // Finally, remap the terminator instructions, as those can't be remapped
  412. // until all BBs are mapped.
  413. RemapInstruction(NewBB->getTerminator(), VMap,
  414. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  415. }
  416. // Defer PHI resolution until rest of function is resolved, PHI resolution
  417. // requires the CFG to be up-to-date.
  418. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
  419. const PHINode *OPN = PHIToResolve[phino];
  420. unsigned NumPreds = OPN->getNumIncomingValues();
  421. const BasicBlock *OldBB = OPN->getParent();
  422. BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
  423. // Map operands for blocks that are live and remove operands for blocks
  424. // that are dead.
  425. for (; phino != PHIToResolve.size() &&
  426. PHIToResolve[phino]->getParent() == OldBB; ++phino) {
  427. OPN = PHIToResolve[phino];
  428. PHINode *PN = cast<PHINode>(VMap[OPN]);
  429. for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
  430. Value *V = VMap[PN->getIncomingBlock(pred)];
  431. if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
  432. Value *InVal = MapValue(PN->getIncomingValue(pred),
  433. VMap,
  434. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  435. assert(InVal && "Unknown input value?");
  436. PN->setIncomingValue(pred, InVal);
  437. PN->setIncomingBlock(pred, MappedBlock);
  438. } else {
  439. PN->removeIncomingValue(pred, false);
  440. --pred, --e; // Revisit the next entry.
  441. }
  442. }
  443. }
  444. // The loop above has removed PHI entries for those blocks that are dead
  445. // and has updated others. However, if a block is live (i.e. copied over)
  446. // but its terminator has been changed to not go to this block, then our
  447. // phi nodes will have invalid entries. Update the PHI nodes in this
  448. // case.
  449. PHINode *PN = cast<PHINode>(NewBB->begin());
  450. NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
  451. if (NumPreds != PN->getNumIncomingValues()) {
  452. assert(NumPreds < PN->getNumIncomingValues());
  453. // Count how many times each predecessor comes to this block.
  454. std::map<BasicBlock*, unsigned> PredCount;
  455. for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
  456. PI != E; ++PI)
  457. --PredCount[*PI];
  458. // Figure out how many entries to remove from each PHI.
  459. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  460. ++PredCount[PN->getIncomingBlock(i)];
  461. // At this point, the excess predecessor entries are positive in the
  462. // map. Loop over all of the PHIs and remove excess predecessor
  463. // entries.
  464. BasicBlock::iterator I = NewBB->begin();
  465. for (; (PN = dyn_cast<PHINode>(I)); ++I) {
  466. for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
  467. E = PredCount.end(); PCI != E; ++PCI) {
  468. BasicBlock *Pred = PCI->first;
  469. for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
  470. PN->removeIncomingValue(Pred, false);
  471. }
  472. }
  473. }
  474. // If the loops above have made these phi nodes have 0 or 1 operand,
  475. // replace them with undef or the input value. We must do this for
  476. // correctness, because 0-operand phis are not valid.
  477. PN = cast<PHINode>(NewBB->begin());
  478. if (PN->getNumIncomingValues() == 0) {
  479. BasicBlock::iterator I = NewBB->begin();
  480. BasicBlock::const_iterator OldI = OldBB->begin();
  481. while ((PN = dyn_cast<PHINode>(I++))) {
  482. Value *NV = UndefValue::get(PN->getType());
  483. PN->replaceAllUsesWith(NV);
  484. assert(VMap[OldI] == PN && "VMap mismatch");
  485. VMap[OldI] = NV;
  486. PN->eraseFromParent();
  487. ++OldI;
  488. }
  489. }
  490. }
  491. // Make a second pass over the PHINodes now that all of them have been
  492. // remapped into the new function, simplifying the PHINode and performing any
  493. // recursive simplifications exposed. This will transparently update the
  494. // WeakVH in the VMap. Notably, we rely on that so that if we coalesce
  495. // two PHINodes, the iteration over the old PHIs remains valid, and the
  496. // mapping will just map us to the new node (which may not even be a PHI
  497. // node).
  498. for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
  499. if (PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]]))
  500. recursivelySimplifyInstruction(PN, DL);
  501. // Now that the inlined function body has been fully constructed, go through
  502. // and zap unconditional fall-through branches. This happen all the time when
  503. // specializing code: code specialization turns conditional branches into
  504. // uncond branches, and this code folds them.
  505. Function::iterator Begin = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]);
  506. Function::iterator I = Begin;
  507. while (I != NewFunc->end()) {
  508. // Check if this block has become dead during inlining or other
  509. // simplifications. Note that the first block will appear dead, as it has
  510. // not yet been wired up properly.
  511. if (I != Begin && (pred_begin(I) == pred_end(I) ||
  512. I->getSinglePredecessor() == I)) {
  513. BasicBlock *DeadBB = I++;
  514. DeleteDeadBlock(DeadBB);
  515. continue;
  516. }
  517. // We need to simplify conditional branches and switches with a constant
  518. // operand. We try to prune these out when cloning, but if the
  519. // simplification required looking through PHI nodes, those are only
  520. // available after forming the full basic block. That may leave some here,
  521. // and we still want to prune the dead code as early as possible.
  522. ConstantFoldTerminator(I);
  523. BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
  524. if (!BI || BI->isConditional()) { ++I; continue; }
  525. BasicBlock *Dest = BI->getSuccessor(0);
  526. if (!Dest->getSinglePredecessor()) {
  527. ++I; continue;
  528. }
  529. // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
  530. // above should have zapped all of them..
  531. assert(!isa<PHINode>(Dest->begin()));
  532. // We know all single-entry PHI nodes in the inlined function have been
  533. // removed, so we just need to splice the blocks.
  534. BI->eraseFromParent();
  535. // Make all PHI nodes that referred to Dest now refer to I as their source.
  536. Dest->replaceAllUsesWith(I);
  537. // Move all the instructions in the succ to the pred.
  538. I->getInstList().splice(I->end(), Dest->getInstList());
  539. // Remove the dest block.
  540. Dest->eraseFromParent();
  541. // Do not increment I, iteratively merge all things this block branches to.
  542. }
  543. // Make a final pass over the basic blocks from theh old function to gather
  544. // any return instructions which survived folding. We have to do this here
  545. // because we can iteratively remove and merge returns above.
  546. for (Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]),
  547. E = NewFunc->end();
  548. I != E; ++I)
  549. if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
  550. Returns.push_back(RI);
  551. }