CloneFunction.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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/SetVector.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Analysis/ConstantFolding.h"
  19. #include "llvm/Analysis/InstructionSimplify.h"
  20. #include "llvm/Analysis/LoopInfo.h"
  21. #include "llvm/IR/CFG.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DebugInfo.h"
  24. #include "llvm/IR/DerivedTypes.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/IntrinsicInst.h"
  29. #include "llvm/IR/LLVMContext.h"
  30. #include "llvm/IR/Metadata.h"
  31. #include "llvm/IR/Module.h"
  32. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  33. #include "llvm/Transforms/Utils/Local.h"
  34. #include "llvm/Transforms/Utils/ValueMapper.h"
  35. #include <map>
  36. using namespace llvm;
  37. /// See comments in Cloning.h.
  38. BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
  39. ValueToValueMapTy &VMap,
  40. const Twine &NameSuffix, Function *F,
  41. ClonedCodeInfo *CodeInfo) {
  42. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
  43. if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
  44. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  45. // Loop over all instructions, and copy them over.
  46. for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
  47. II != IE; ++II) {
  48. Instruction *NewInst = II->clone();
  49. if (II->hasName())
  50. NewInst->setName(II->getName()+NameSuffix);
  51. NewBB->getInstList().push_back(NewInst);
  52. VMap[&*II] = NewInst; // Add instruction map to value.
  53. hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
  54. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  55. if (isa<ConstantInt>(AI->getArraySize()))
  56. hasStaticAllocas = true;
  57. else
  58. hasDynamicAllocas = true;
  59. }
  60. }
  61. if (CodeInfo) {
  62. CodeInfo->ContainsCalls |= hasCalls;
  63. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  64. CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
  65. BB != &BB->getParent()->getEntryBlock();
  66. }
  67. return NewBB;
  68. }
  69. // Clone OldFunc into NewFunc, transforming the old arguments into references to
  70. // VMap values.
  71. //
  72. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  73. ValueToValueMapTy &VMap,
  74. bool ModuleLevelChanges,
  75. SmallVectorImpl<ReturnInst*> &Returns,
  76. const char *NameSuffix, ClonedCodeInfo *CodeInfo,
  77. ValueMapTypeRemapper *TypeMapper,
  78. ValueMaterializer *Materializer) {
  79. assert(NameSuffix && "NameSuffix cannot be null!");
  80. #ifndef NDEBUG
  81. for (const Argument &I : OldFunc->args())
  82. assert(VMap.count(&I) && "No mapping from source argument specified!");
  83. #endif
  84. // Copy all attributes other than those stored in the AttributeList. We need
  85. // to remap the parameter indices of the AttributeList.
  86. AttributeList NewAttrs = NewFunc->getAttributes();
  87. NewFunc->copyAttributesFrom(OldFunc);
  88. NewFunc->setAttributes(NewAttrs);
  89. // Fix up the personality function that got copied over.
  90. if (OldFunc->hasPersonalityFn())
  91. NewFunc->setPersonalityFn(
  92. MapValue(OldFunc->getPersonalityFn(), VMap,
  93. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  94. TypeMapper, Materializer));
  95. SmallVector<AttributeSet, 4> AttrVec(NewFunc->arg_size() + 2);
  96. AttributeList OldAttrs = OldFunc->getAttributes();
  97. // Copy the return attributes.
  98. AttrVec[0] = OldAttrs.getRetAttributes();
  99. // Clone any argument attributes that are present in the VMap.
  100. for (const Argument &OldArg : OldFunc->args())
  101. if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
  102. AttrVec[NewArg->getArgNo() + 1] =
  103. OldAttrs.getParamAttributes(OldArg.getArgNo() + 1);
  104. }
  105. // Copy any function attributes.
  106. AttrVec.back() = OldAttrs.getFnAttributes();
  107. NewFunc->setAttributes(AttributeList::get(NewFunc->getContext(), AttrVec));
  108. SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
  109. OldFunc->getAllMetadata(MDs);
  110. for (auto MD : MDs)
  111. NewFunc->addMetadata(
  112. MD.first,
  113. *MapMetadata(MD.second, VMap,
  114. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  115. TypeMapper, Materializer));
  116. // Loop over all of the basic blocks in the function, cloning them as
  117. // appropriate. Note that we save BE this way in order to handle cloning of
  118. // recursive functions into themselves.
  119. //
  120. for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
  121. BI != BE; ++BI) {
  122. const BasicBlock &BB = *BI;
  123. // Create a new basic block and copy instructions into it!
  124. BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
  125. // Add basic block mapping.
  126. VMap[&BB] = CBB;
  127. // It is only legal to clone a function if a block address within that
  128. // function is never referenced outside of the function. Given that, we
  129. // want to map block addresses from the old function to block addresses in
  130. // the clone. (This is different from the generic ValueMapper
  131. // implementation, which generates an invalid blockaddress when
  132. // cloning a function.)
  133. if (BB.hasAddressTaken()) {
  134. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  135. const_cast<BasicBlock*>(&BB));
  136. VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
  137. }
  138. // Note return instructions for the caller.
  139. if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
  140. Returns.push_back(RI);
  141. }
  142. // Loop over all of the instructions in the function, fixing up operand
  143. // references as we go. This uses VMap to do all the hard work.
  144. for (Function::iterator BB =
  145. cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
  146. BE = NewFunc->end();
  147. BB != BE; ++BB)
  148. // Loop over all instructions, fixing each one as we find it...
  149. for (Instruction &II : *BB)
  150. RemapInstruction(&II, VMap,
  151. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  152. TypeMapper, Materializer);
  153. }
  154. /// Return a copy of the specified function and add it to that function's
  155. /// module. Also, any references specified in the VMap are changed to refer to
  156. /// their mapped value instead of the original one. If any of the arguments to
  157. /// the function are in the VMap, the arguments are deleted from the resultant
  158. /// function. The VMap is updated to include mappings from all of the
  159. /// instructions and basicblocks in the function from their old to new values.
  160. ///
  161. Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
  162. ClonedCodeInfo *CodeInfo) {
  163. std::vector<Type*> ArgTypes;
  164. // The user might be deleting arguments to the function by specifying them in
  165. // the VMap. If so, we need to not add the arguments to the arg ty vector
  166. //
  167. for (const Argument &I : F->args())
  168. if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
  169. ArgTypes.push_back(I.getType());
  170. // Create a new function type...
  171. FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
  172. ArgTypes, F->getFunctionType()->isVarArg());
  173. // Create the new function...
  174. Function *NewF =
  175. Function::Create(FTy, F->getLinkage(), F->getName(), F->getParent());
  176. // Loop over the arguments, copying the names of the mapped arguments over...
  177. Function::arg_iterator DestI = NewF->arg_begin();
  178. for (const Argument & I : F->args())
  179. if (VMap.count(&I) == 0) { // Is this argument preserved?
  180. DestI->setName(I.getName()); // Copy the name over...
  181. VMap[&I] = &*DestI++; // Add mapping to VMap
  182. }
  183. SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
  184. CloneFunctionInto(NewF, F, VMap, /*ModuleLevelChanges=*/false, Returns, "",
  185. CodeInfo);
  186. return NewF;
  187. }
  188. namespace {
  189. /// This is a private class used to implement CloneAndPruneFunctionInto.
  190. struct PruningFunctionCloner {
  191. Function *NewFunc;
  192. const Function *OldFunc;
  193. ValueToValueMapTy &VMap;
  194. bool ModuleLevelChanges;
  195. const char *NameSuffix;
  196. ClonedCodeInfo *CodeInfo;
  197. public:
  198. PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
  199. ValueToValueMapTy &valueMap, bool moduleLevelChanges,
  200. const char *nameSuffix, ClonedCodeInfo *codeInfo)
  201. : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
  202. ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
  203. CodeInfo(codeInfo) {}
  204. /// The specified block is found to be reachable, clone it and
  205. /// anything that it can reach.
  206. void CloneBlock(const BasicBlock *BB,
  207. BasicBlock::const_iterator StartingInst,
  208. std::vector<const BasicBlock*> &ToClone);
  209. };
  210. }
  211. /// The specified block is found to be reachable, clone it and
  212. /// anything that it can reach.
  213. void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
  214. BasicBlock::const_iterator StartingInst,
  215. std::vector<const BasicBlock*> &ToClone){
  216. WeakVH &BBEntry = VMap[BB];
  217. // Have we already cloned this block?
  218. if (BBEntry) return;
  219. // Nope, clone it now.
  220. BasicBlock *NewBB;
  221. BBEntry = NewBB = BasicBlock::Create(BB->getContext());
  222. if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
  223. // It is only legal to clone a function if a block address within that
  224. // function is never referenced outside of the function. Given that, we
  225. // want to map block addresses from the old function to block addresses in
  226. // the clone. (This is different from the generic ValueMapper
  227. // implementation, which generates an invalid blockaddress when
  228. // cloning a function.)
  229. //
  230. // Note that we don't need to fix the mapping for unreachable blocks;
  231. // the default mapping there is safe.
  232. if (BB->hasAddressTaken()) {
  233. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  234. const_cast<BasicBlock*>(BB));
  235. VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
  236. }
  237. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  238. // Loop over all instructions, and copy them over, DCE'ing as we go. This
  239. // loop doesn't include the terminator.
  240. for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
  241. II != IE; ++II) {
  242. Instruction *NewInst = II->clone();
  243. // Eagerly remap operands to the newly cloned instruction, except for PHI
  244. // nodes for which we defer processing until we update the CFG.
  245. if (!isa<PHINode>(NewInst)) {
  246. RemapInstruction(NewInst, VMap,
  247. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  248. // If we can simplify this instruction to some other value, simply add
  249. // a mapping to that value rather than inserting a new instruction into
  250. // the basic block.
  251. if (Value *V =
  252. SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
  253. // On the off-chance that this simplifies to an instruction in the old
  254. // function, map it back into the new function.
  255. if (Value *MappedV = VMap.lookup(V))
  256. V = MappedV;
  257. if (!NewInst->mayHaveSideEffects()) {
  258. VMap[&*II] = V;
  259. delete NewInst;
  260. continue;
  261. }
  262. }
  263. }
  264. if (II->hasName())
  265. NewInst->setName(II->getName()+NameSuffix);
  266. VMap[&*II] = NewInst; // Add instruction map to value.
  267. NewBB->getInstList().push_back(NewInst);
  268. hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
  269. if (CodeInfo)
  270. if (auto CS = ImmutableCallSite(&*II))
  271. if (CS.hasOperandBundles())
  272. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  273. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  274. if (isa<ConstantInt>(AI->getArraySize()))
  275. hasStaticAllocas = true;
  276. else
  277. hasDynamicAllocas = true;
  278. }
  279. }
  280. // Finally, clone over the terminator.
  281. const TerminatorInst *OldTI = BB->getTerminator();
  282. bool TerminatorDone = false;
  283. if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
  284. if (BI->isConditional()) {
  285. // If the condition was a known constant in the callee...
  286. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  287. // Or is a known constant in the caller...
  288. if (!Cond) {
  289. Value *V = VMap.lookup(BI->getCondition());
  290. Cond = dyn_cast_or_null<ConstantInt>(V);
  291. }
  292. // Constant fold to uncond branch!
  293. if (Cond) {
  294. BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
  295. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  296. ToClone.push_back(Dest);
  297. TerminatorDone = true;
  298. }
  299. }
  300. } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
  301. // If switching on a value known constant in the caller.
  302. ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
  303. if (!Cond) { // Or known constant after constant prop in the callee...
  304. Value *V = VMap.lookup(SI->getCondition());
  305. Cond = dyn_cast_or_null<ConstantInt>(V);
  306. }
  307. if (Cond) { // Constant fold to uncond branch!
  308. SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
  309. BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
  310. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  311. ToClone.push_back(Dest);
  312. TerminatorDone = true;
  313. }
  314. }
  315. if (!TerminatorDone) {
  316. Instruction *NewInst = OldTI->clone();
  317. if (OldTI->hasName())
  318. NewInst->setName(OldTI->getName()+NameSuffix);
  319. NewBB->getInstList().push_back(NewInst);
  320. VMap[OldTI] = NewInst; // Add instruction map to value.
  321. if (CodeInfo)
  322. if (auto CS = ImmutableCallSite(OldTI))
  323. if (CS.hasOperandBundles())
  324. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  325. // Recursively clone any reachable successor blocks.
  326. const TerminatorInst *TI = BB->getTerminator();
  327. for (const BasicBlock *Succ : TI->successors())
  328. ToClone.push_back(Succ);
  329. }
  330. if (CodeInfo) {
  331. CodeInfo->ContainsCalls |= hasCalls;
  332. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  333. CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
  334. BB != &BB->getParent()->front();
  335. }
  336. }
  337. /// This works like CloneAndPruneFunctionInto, except that it does not clone the
  338. /// entire function. Instead it starts at an instruction provided by the caller
  339. /// and copies (and prunes) only the code reachable from that instruction.
  340. void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
  341. const Instruction *StartingInst,
  342. ValueToValueMapTy &VMap,
  343. bool ModuleLevelChanges,
  344. SmallVectorImpl<ReturnInst *> &Returns,
  345. const char *NameSuffix,
  346. ClonedCodeInfo *CodeInfo) {
  347. assert(NameSuffix && "NameSuffix cannot be null!");
  348. ValueMapTypeRemapper *TypeMapper = nullptr;
  349. ValueMaterializer *Materializer = nullptr;
  350. #ifndef NDEBUG
  351. // If the cloning starts at the beginning of the function, verify that
  352. // the function arguments are mapped.
  353. if (!StartingInst)
  354. for (const Argument &II : OldFunc->args())
  355. assert(VMap.count(&II) && "No mapping from source argument specified!");
  356. #endif
  357. PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
  358. NameSuffix, CodeInfo);
  359. const BasicBlock *StartingBB;
  360. if (StartingInst)
  361. StartingBB = StartingInst->getParent();
  362. else {
  363. StartingBB = &OldFunc->getEntryBlock();
  364. StartingInst = &StartingBB->front();
  365. }
  366. // Clone the entry block, and anything recursively reachable from it.
  367. std::vector<const BasicBlock*> CloneWorklist;
  368. PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
  369. while (!CloneWorklist.empty()) {
  370. const BasicBlock *BB = CloneWorklist.back();
  371. CloneWorklist.pop_back();
  372. PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
  373. }
  374. // Loop over all of the basic blocks in the old function. If the block was
  375. // reachable, we have cloned it and the old block is now in the value map:
  376. // insert it into the new function in the right order. If not, ignore it.
  377. //
  378. // Defer PHI resolution until rest of function is resolved.
  379. SmallVector<const PHINode*, 16> PHIToResolve;
  380. for (const BasicBlock &BI : *OldFunc) {
  381. Value *V = VMap.lookup(&BI);
  382. BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
  383. if (!NewBB) continue; // Dead block.
  384. // Add the new block to the new function.
  385. NewFunc->getBasicBlockList().push_back(NewBB);
  386. // Handle PHI nodes specially, as we have to remove references to dead
  387. // blocks.
  388. for (BasicBlock::const_iterator I = BI.begin(), E = BI.end(); I != E; ++I) {
  389. // PHI nodes may have been remapped to non-PHI nodes by the caller or
  390. // during the cloning process.
  391. if (const PHINode *PN = dyn_cast<PHINode>(I)) {
  392. if (isa<PHINode>(VMap[PN]))
  393. PHIToResolve.push_back(PN);
  394. else
  395. break;
  396. } else {
  397. break;
  398. }
  399. }
  400. // Finally, remap the terminator instructions, as those can't be remapped
  401. // until all BBs are mapped.
  402. RemapInstruction(NewBB->getTerminator(), VMap,
  403. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  404. TypeMapper, Materializer);
  405. }
  406. // Defer PHI resolution until rest of function is resolved, PHI resolution
  407. // requires the CFG to be up-to-date.
  408. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
  409. const PHINode *OPN = PHIToResolve[phino];
  410. unsigned NumPreds = OPN->getNumIncomingValues();
  411. const BasicBlock *OldBB = OPN->getParent();
  412. BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
  413. // Map operands for blocks that are live and remove operands for blocks
  414. // that are dead.
  415. for (; phino != PHIToResolve.size() &&
  416. PHIToResolve[phino]->getParent() == OldBB; ++phino) {
  417. OPN = PHIToResolve[phino];
  418. PHINode *PN = cast<PHINode>(VMap[OPN]);
  419. for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
  420. Value *V = VMap.lookup(PN->getIncomingBlock(pred));
  421. if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
  422. Value *InVal = MapValue(PN->getIncomingValue(pred),
  423. VMap,
  424. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  425. assert(InVal && "Unknown input value?");
  426. PN->setIncomingValue(pred, InVal);
  427. PN->setIncomingBlock(pred, MappedBlock);
  428. } else {
  429. PN->removeIncomingValue(pred, false);
  430. --pred; // Revisit the next entry.
  431. --e;
  432. }
  433. }
  434. }
  435. // The loop above has removed PHI entries for those blocks that are dead
  436. // and has updated others. However, if a block is live (i.e. copied over)
  437. // but its terminator has been changed to not go to this block, then our
  438. // phi nodes will have invalid entries. Update the PHI nodes in this
  439. // case.
  440. PHINode *PN = cast<PHINode>(NewBB->begin());
  441. NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
  442. if (NumPreds != PN->getNumIncomingValues()) {
  443. assert(NumPreds < PN->getNumIncomingValues());
  444. // Count how many times each predecessor comes to this block.
  445. std::map<BasicBlock*, unsigned> PredCount;
  446. for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
  447. PI != E; ++PI)
  448. --PredCount[*PI];
  449. // Figure out how many entries to remove from each PHI.
  450. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  451. ++PredCount[PN->getIncomingBlock(i)];
  452. // At this point, the excess predecessor entries are positive in the
  453. // map. Loop over all of the PHIs and remove excess predecessor
  454. // entries.
  455. BasicBlock::iterator I = NewBB->begin();
  456. for (; (PN = dyn_cast<PHINode>(I)); ++I) {
  457. for (const auto &PCI : PredCount) {
  458. BasicBlock *Pred = PCI.first;
  459. for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
  460. PN->removeIncomingValue(Pred, false);
  461. }
  462. }
  463. }
  464. // If the loops above have made these phi nodes have 0 or 1 operand,
  465. // replace them with undef or the input value. We must do this for
  466. // correctness, because 0-operand phis are not valid.
  467. PN = cast<PHINode>(NewBB->begin());
  468. if (PN->getNumIncomingValues() == 0) {
  469. BasicBlock::iterator I = NewBB->begin();
  470. BasicBlock::const_iterator OldI = OldBB->begin();
  471. while ((PN = dyn_cast<PHINode>(I++))) {
  472. Value *NV = UndefValue::get(PN->getType());
  473. PN->replaceAllUsesWith(NV);
  474. assert(VMap[&*OldI] == PN && "VMap mismatch");
  475. VMap[&*OldI] = NV;
  476. PN->eraseFromParent();
  477. ++OldI;
  478. }
  479. }
  480. }
  481. // Make a second pass over the PHINodes now that all of them have been
  482. // remapped into the new function, simplifying the PHINode and performing any
  483. // recursive simplifications exposed. This will transparently update the
  484. // WeakVH in the VMap. Notably, we rely on that so that if we coalesce
  485. // two PHINodes, the iteration over the old PHIs remains valid, and the
  486. // mapping will just map us to the new node (which may not even be a PHI
  487. // node).
  488. const DataLayout &DL = NewFunc->getParent()->getDataLayout();
  489. SmallSetVector<const Value *, 8> Worklist;
  490. for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
  491. if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
  492. Worklist.insert(PHIToResolve[Idx]);
  493. // Note that we must test the size on each iteration, the worklist can grow.
  494. for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
  495. const Value *OrigV = Worklist[Idx];
  496. auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
  497. if (!I)
  498. continue;
  499. // Skip over non-intrinsic callsites, we don't want to remove any nodes from
  500. // the CGSCC.
  501. CallSite CS = CallSite(I);
  502. if (CS && CS.getCalledFunction() && !CS.getCalledFunction()->isIntrinsic())
  503. continue;
  504. // See if this instruction simplifies.
  505. Value *SimpleV = SimplifyInstruction(I, DL);
  506. if (!SimpleV)
  507. continue;
  508. // Stash away all the uses of the old instruction so we can check them for
  509. // recursive simplifications after a RAUW. This is cheaper than checking all
  510. // uses of To on the recursive step in most cases.
  511. for (const User *U : OrigV->users())
  512. Worklist.insert(cast<Instruction>(U));
  513. // Replace the instruction with its simplified value.
  514. I->replaceAllUsesWith(SimpleV);
  515. // If the original instruction had no side effects, remove it.
  516. if (isInstructionTriviallyDead(I))
  517. I->eraseFromParent();
  518. else
  519. VMap[OrigV] = I;
  520. }
  521. // Now that the inlined function body has been fully constructed, go through
  522. // and zap unconditional fall-through branches. This happens all the time when
  523. // specializing code: code specialization turns conditional branches into
  524. // uncond branches, and this code folds them.
  525. Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
  526. Function::iterator I = Begin;
  527. while (I != NewFunc->end()) {
  528. // Check if this block has become dead during inlining or other
  529. // simplifications. Note that the first block will appear dead, as it has
  530. // not yet been wired up properly.
  531. if (I != Begin && (pred_begin(&*I) == pred_end(&*I) ||
  532. I->getSinglePredecessor() == &*I)) {
  533. BasicBlock *DeadBB = &*I++;
  534. DeleteDeadBlock(DeadBB);
  535. continue;
  536. }
  537. // We need to simplify conditional branches and switches with a constant
  538. // operand. We try to prune these out when cloning, but if the
  539. // simplification required looking through PHI nodes, those are only
  540. // available after forming the full basic block. That may leave some here,
  541. // and we still want to prune the dead code as early as possible.
  542. ConstantFoldTerminator(&*I);
  543. BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
  544. if (!BI || BI->isConditional()) { ++I; continue; }
  545. BasicBlock *Dest = BI->getSuccessor(0);
  546. if (!Dest->getSinglePredecessor()) {
  547. ++I; continue;
  548. }
  549. // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
  550. // above should have zapped all of them..
  551. assert(!isa<PHINode>(Dest->begin()));
  552. // We know all single-entry PHI nodes in the inlined function have been
  553. // removed, so we just need to splice the blocks.
  554. BI->eraseFromParent();
  555. // Make all PHI nodes that referred to Dest now refer to I as their source.
  556. Dest->replaceAllUsesWith(&*I);
  557. // Move all the instructions in the succ to the pred.
  558. I->getInstList().splice(I->end(), Dest->getInstList());
  559. // Remove the dest block.
  560. Dest->eraseFromParent();
  561. // Do not increment I, iteratively merge all things this block branches to.
  562. }
  563. // Make a final pass over the basic blocks from the old function to gather
  564. // any return instructions which survived folding. We have to do this here
  565. // because we can iteratively remove and merge returns above.
  566. for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
  567. E = NewFunc->end();
  568. I != E; ++I)
  569. if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
  570. Returns.push_back(RI);
  571. }
  572. /// This works exactly like CloneFunctionInto,
  573. /// except that it does some simple constant prop and DCE on the fly. The
  574. /// effect of this is to copy significantly less code in cases where (for
  575. /// example) a function call with constant arguments is inlined, and those
  576. /// constant arguments cause a significant amount of code in the callee to be
  577. /// dead. Since this doesn't produce an exact copy of the input, it can't be
  578. /// used for things like CloneFunction or CloneModule.
  579. void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
  580. ValueToValueMapTy &VMap,
  581. bool ModuleLevelChanges,
  582. SmallVectorImpl<ReturnInst*> &Returns,
  583. const char *NameSuffix,
  584. ClonedCodeInfo *CodeInfo,
  585. Instruction *TheCall) {
  586. CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
  587. ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
  588. }
  589. /// \brief Remaps instructions in \p Blocks using the mapping in \p VMap.
  590. void llvm::remapInstructionsInBlocks(
  591. const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
  592. // Rewrite the code to refer to itself.
  593. for (auto *BB : Blocks)
  594. for (auto &Inst : *BB)
  595. RemapInstruction(&Inst, VMap,
  596. RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
  597. }
  598. /// \brief Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
  599. /// Blocks.
  600. ///
  601. /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
  602. /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
  603. Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
  604. Loop *OrigLoop, ValueToValueMapTy &VMap,
  605. const Twine &NameSuffix, LoopInfo *LI,
  606. DominatorTree *DT,
  607. SmallVectorImpl<BasicBlock *> &Blocks) {
  608. assert(OrigLoop->getSubLoops().empty() &&
  609. "Loop to be cloned cannot have inner loop");
  610. Function *F = OrigLoop->getHeader()->getParent();
  611. Loop *ParentLoop = OrigLoop->getParentLoop();
  612. Loop *NewLoop = new Loop();
  613. if (ParentLoop)
  614. ParentLoop->addChildLoop(NewLoop);
  615. else
  616. LI->addTopLevelLoop(NewLoop);
  617. BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
  618. assert(OrigPH && "No preheader");
  619. BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
  620. // To rename the loop PHIs.
  621. VMap[OrigPH] = NewPH;
  622. Blocks.push_back(NewPH);
  623. // Update LoopInfo.
  624. if (ParentLoop)
  625. ParentLoop->addBasicBlockToLoop(NewPH, *LI);
  626. // Update DominatorTree.
  627. DT->addNewBlock(NewPH, LoopDomBB);
  628. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  629. BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
  630. VMap[BB] = NewBB;
  631. // Update LoopInfo.
  632. NewLoop->addBasicBlockToLoop(NewBB, *LI);
  633. // Add DominatorTree node. After seeing all blocks, update to correct IDom.
  634. DT->addNewBlock(NewBB, NewPH);
  635. Blocks.push_back(NewBB);
  636. }
  637. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  638. // Update DominatorTree.
  639. BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
  640. DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
  641. cast<BasicBlock>(VMap[IDomBB]));
  642. }
  643. // Move them physically from the end of the block list.
  644. F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
  645. NewPH);
  646. F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
  647. NewLoop->getHeader()->getIterator(), F->end());
  648. return NewLoop;
  649. }
  650. /// \brief Duplicate non-Phi instructions from the beginning of block up to
  651. /// StopAt instruction into a split block between BB and its predecessor.
  652. BasicBlock *
  653. llvm::DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB,
  654. Instruction *StopAt,
  655. ValueToValueMapTy &ValueMapping) {
  656. // We are going to have to map operands from the original BB block to the new
  657. // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
  658. // account for entry from PredBB.
  659. BasicBlock::iterator BI = BB->begin();
  660. for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
  661. ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
  662. BasicBlock *NewBB = SplitEdge(PredBB, BB);
  663. NewBB->setName(PredBB->getName() + ".split");
  664. Instruction *NewTerm = NewBB->getTerminator();
  665. // Clone the non-phi instructions of BB into NewBB, keeping track of the
  666. // mapping and using it to remap operands in the cloned instructions.
  667. for (; StopAt != &*BI; ++BI) {
  668. Instruction *New = BI->clone();
  669. New->setName(BI->getName());
  670. New->insertBefore(NewTerm);
  671. ValueMapping[&*BI] = New;
  672. // Remap operands to patch up intra-block references.
  673. for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
  674. if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
  675. auto I = ValueMapping.find(Inst);
  676. if (I != ValueMapping.end())
  677. New->setOperand(i, I->second);
  678. }
  679. }
  680. return NewBB;
  681. }