CloneFunction.cpp 33 KB

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