CloneFunction.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. //===- CloneFunction.cpp - Clone a function into another function ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the CloneFunctionInto interface, which is used as the
  10. // low-level function cloner. This is used by the CloneFunction and function
  11. // inliner to do the dirty work of copying the body of a function around.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SetVector.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/Analysis/ConstantFolding.h"
  17. #include "llvm/Analysis/DomTreeUpdater.h"
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/Analysis/LoopInfo.h"
  20. #include "llvm/IR/CFG.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DebugInfo.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/Function.h"
  25. #include "llvm/IR/GlobalVariable.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/IntrinsicInst.h"
  28. #include "llvm/IR/LLVMContext.h"
  29. #include "llvm/IR/MDBuilder.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/Local.h"
  35. #include "llvm/Transforms/Utils/ValueMapper.h"
  36. #include <map>
  37. using namespace llvm;
  38. #define DEBUG_TYPE "clone-function"
  39. /// See comments in Cloning.h.
  40. BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
  41. const Twine &NameSuffix, Function *F,
  42. ClonedCodeInfo *CodeInfo,
  43. DebugInfoFinder *DIFinder) {
  44. DenseMap<const MDNode *, MDNode *> Cache;
  45. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
  46. if (BB->hasName())
  47. NewBB->setName(BB->getName() + NameSuffix);
  48. bool hasCalls = false, hasDynamicAllocas = false;
  49. Module *TheModule = F ? F->getParent() : nullptr;
  50. // Loop over all instructions, and copy them over.
  51. for (const Instruction &I : *BB) {
  52. if (DIFinder && TheModule)
  53. DIFinder->processInstruction(*TheModule, I);
  54. Instruction *NewInst = I.clone();
  55. if (I.hasName())
  56. NewInst->setName(I.getName() + NameSuffix);
  57. NewBB->getInstList().push_back(NewInst);
  58. VMap[&I] = NewInst; // Add instruction map to value.
  59. hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I));
  60. if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
  61. if (!AI->isStaticAlloca()) {
  62. hasDynamicAllocas = true;
  63. }
  64. }
  65. }
  66. if (CodeInfo) {
  67. CodeInfo->ContainsCalls |= hasCalls;
  68. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  69. }
  70. return NewBB;
  71. }
  72. // Clone OldFunc into NewFunc, transforming the old arguments into references to
  73. // VMap values.
  74. //
  75. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  76. ValueToValueMapTy &VMap,
  77. bool ModuleLevelChanges,
  78. SmallVectorImpl<ReturnInst*> &Returns,
  79. const char *NameSuffix, ClonedCodeInfo *CodeInfo,
  80. ValueMapTypeRemapper *TypeMapper,
  81. ValueMaterializer *Materializer) {
  82. assert(NameSuffix && "NameSuffix cannot be null!");
  83. #ifndef NDEBUG
  84. for (const Argument &I : OldFunc->args())
  85. assert(VMap.count(&I) && "No mapping from source argument specified!");
  86. #endif
  87. // Copy all attributes other than those stored in the AttributeList. We need
  88. // to remap the parameter indices of the AttributeList.
  89. AttributeList NewAttrs = NewFunc->getAttributes();
  90. NewFunc->copyAttributesFrom(OldFunc);
  91. NewFunc->setAttributes(NewAttrs);
  92. // Fix up the personality function that got copied over.
  93. if (OldFunc->hasPersonalityFn())
  94. NewFunc->setPersonalityFn(
  95. MapValue(OldFunc->getPersonalityFn(), VMap,
  96. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  97. TypeMapper, Materializer));
  98. SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
  99. AttributeList OldAttrs = OldFunc->getAttributes();
  100. // Clone any argument attributes that are present in the VMap.
  101. for (const Argument &OldArg : OldFunc->args()) {
  102. if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
  103. NewArgAttrs[NewArg->getArgNo()] =
  104. OldAttrs.getParamAttributes(OldArg.getArgNo());
  105. }
  106. }
  107. NewFunc->setAttributes(
  108. AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(),
  109. OldAttrs.getRetAttributes(), NewArgAttrs));
  110. bool MustCloneSP =
  111. OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent();
  112. DISubprogram *SP = OldFunc->getSubprogram();
  113. if (SP) {
  114. assert(!MustCloneSP || ModuleLevelChanges);
  115. // Add mappings for some DebugInfo nodes that we don't want duplicated
  116. // even if they're distinct.
  117. auto &MD = VMap.MD();
  118. MD[SP->getUnit()].reset(SP->getUnit());
  119. MD[SP->getType()].reset(SP->getType());
  120. MD[SP->getFile()].reset(SP->getFile());
  121. // If we're not cloning into the same module, no need to clone the
  122. // subprogram
  123. if (!MustCloneSP)
  124. MD[SP].reset(SP);
  125. }
  126. // Everything else beyond this point deals with function instructions,
  127. // so if we are dealing with a function declaration, we're done.
  128. if (OldFunc->isDeclaration())
  129. return;
  130. // When we remap instructions, we want to avoid duplicating inlined
  131. // DISubprograms, so record all subprograms we find as we duplicate
  132. // instructions and then freeze them in the MD map.
  133. // We also record information about dbg.value and dbg.declare to avoid
  134. // duplicating the types.
  135. DebugInfoFinder DIFinder;
  136. // Loop over all of the basic blocks in the function, cloning them as
  137. // appropriate. Note that we save BE this way in order to handle cloning of
  138. // recursive functions into themselves.
  139. for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
  140. BI != BE; ++BI) {
  141. const BasicBlock &BB = *BI;
  142. // Create a new basic block and copy instructions into it!
  143. BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo,
  144. ModuleLevelChanges ? &DIFinder : nullptr);
  145. // Add basic block mapping.
  146. VMap[&BB] = CBB;
  147. // It is only legal to clone a function if a block address within that
  148. // function is never referenced outside of the function. Given that, we
  149. // want to map block addresses from the old function to block addresses in
  150. // the clone. (This is different from the generic ValueMapper
  151. // implementation, which generates an invalid blockaddress when
  152. // cloning a function.)
  153. if (BB.hasAddressTaken()) {
  154. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  155. const_cast<BasicBlock*>(&BB));
  156. VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
  157. }
  158. // Note return instructions for the caller.
  159. if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
  160. Returns.push_back(RI);
  161. }
  162. for (DISubprogram *ISP : DIFinder.subprograms())
  163. if (ISP != SP)
  164. VMap.MD()[ISP].reset(ISP);
  165. for (DICompileUnit *CU : DIFinder.compile_units())
  166. VMap.MD()[CU].reset(CU);
  167. for (DIType *Type : DIFinder.types())
  168. VMap.MD()[Type].reset(Type);
  169. // Duplicate the metadata that is attached to the cloned function.
  170. // Subprograms/CUs/types that were already mapped to themselves won't be
  171. // duplicated.
  172. SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
  173. OldFunc->getAllMetadata(MDs);
  174. for (auto MD : MDs) {
  175. NewFunc->addMetadata(
  176. MD.first,
  177. *MapMetadata(MD.second, VMap,
  178. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  179. TypeMapper, Materializer));
  180. }
  181. // Loop over all of the instructions in the function, fixing up operand
  182. // references as we go. This uses VMap to do all the hard work.
  183. for (Function::iterator BB =
  184. cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
  185. BE = NewFunc->end();
  186. BB != BE; ++BB)
  187. // Loop over all instructions, fixing each one as we find it...
  188. for (Instruction &II : *BB)
  189. RemapInstruction(&II, VMap,
  190. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  191. TypeMapper, Materializer);
  192. // Register all DICompileUnits of the old parent module in the new parent module
  193. auto* OldModule = OldFunc->getParent();
  194. auto* NewModule = NewFunc->getParent();
  195. if (OldModule && NewModule && OldModule != NewModule && DIFinder.compile_unit_count()) {
  196. auto* NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
  197. // Avoid multiple insertions of the same DICompileUnit to NMD.
  198. SmallPtrSet<const void*, 8> Visited;
  199. for (auto* Operand : NMD->operands())
  200. Visited.insert(Operand);
  201. for (auto* Unit : DIFinder.compile_units())
  202. // VMap.MD()[Unit] == Unit
  203. if (Visited.insert(Unit).second)
  204. NMD->addOperand(Unit);
  205. }
  206. }
  207. /// Return a copy of the specified function and add it to that function's
  208. /// module. Also, any references specified in the VMap are changed to refer to
  209. /// their mapped value instead of the original one. If any of the arguments to
  210. /// the function are in the VMap, the arguments are deleted from the resultant
  211. /// function. The VMap is updated to include mappings from all of the
  212. /// instructions and basicblocks in the function from their old to new values.
  213. ///
  214. Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
  215. ClonedCodeInfo *CodeInfo) {
  216. std::vector<Type*> ArgTypes;
  217. // The user might be deleting arguments to the function by specifying them in
  218. // the VMap. If so, we need to not add the arguments to the arg ty vector
  219. //
  220. for (const Argument &I : F->args())
  221. if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
  222. ArgTypes.push_back(I.getType());
  223. // Create a new function type...
  224. FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
  225. ArgTypes, F->getFunctionType()->isVarArg());
  226. // Create the new function...
  227. Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(),
  228. F->getName(), F->getParent());
  229. // Loop over the arguments, copying the names of the mapped arguments over...
  230. Function::arg_iterator DestI = NewF->arg_begin();
  231. for (const Argument & I : F->args())
  232. if (VMap.count(&I) == 0) { // Is this argument preserved?
  233. DestI->setName(I.getName()); // Copy the name over...
  234. VMap[&I] = &*DestI++; // Add mapping to VMap
  235. }
  236. SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
  237. CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "",
  238. CodeInfo);
  239. return NewF;
  240. }
  241. namespace {
  242. /// This is a private class used to implement CloneAndPruneFunctionInto.
  243. struct PruningFunctionCloner {
  244. Function *NewFunc;
  245. const Function *OldFunc;
  246. ValueToValueMapTy &VMap;
  247. bool ModuleLevelChanges;
  248. const char *NameSuffix;
  249. ClonedCodeInfo *CodeInfo;
  250. public:
  251. PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
  252. ValueToValueMapTy &valueMap, bool moduleLevelChanges,
  253. const char *nameSuffix, ClonedCodeInfo *codeInfo)
  254. : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
  255. ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
  256. CodeInfo(codeInfo) {}
  257. /// The specified block is found to be reachable, clone it and
  258. /// anything that it can reach.
  259. void CloneBlock(const BasicBlock *BB,
  260. BasicBlock::const_iterator StartingInst,
  261. std::vector<const BasicBlock*> &ToClone);
  262. };
  263. }
  264. /// The specified block is found to be reachable, clone it and
  265. /// anything that it can reach.
  266. void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
  267. BasicBlock::const_iterator StartingInst,
  268. std::vector<const BasicBlock*> &ToClone){
  269. WeakTrackingVH &BBEntry = VMap[BB];
  270. // Have we already cloned this block?
  271. if (BBEntry) return;
  272. // Nope, clone it now.
  273. BasicBlock *NewBB;
  274. BBEntry = NewBB = BasicBlock::Create(BB->getContext());
  275. if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
  276. // It is only legal to clone a function if a block address within that
  277. // function is never referenced outside of the function. Given that, we
  278. // want to map block addresses from the old function to block addresses in
  279. // the clone. (This is different from the generic ValueMapper
  280. // implementation, which generates an invalid blockaddress when
  281. // cloning a function.)
  282. //
  283. // Note that we don't need to fix the mapping for unreachable blocks;
  284. // the default mapping there is safe.
  285. if (BB->hasAddressTaken()) {
  286. Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
  287. const_cast<BasicBlock*>(BB));
  288. VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
  289. }
  290. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  291. // Loop over all instructions, and copy them over, DCE'ing as we go. This
  292. // loop doesn't include the terminator.
  293. for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
  294. II != IE; ++II) {
  295. Instruction *NewInst = II->clone();
  296. // Eagerly remap operands to the newly cloned instruction, except for PHI
  297. // nodes for which we defer processing until we update the CFG.
  298. if (!isa<PHINode>(NewInst)) {
  299. RemapInstruction(NewInst, VMap,
  300. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  301. // If we can simplify this instruction to some other value, simply add
  302. // a mapping to that value rather than inserting a new instruction into
  303. // the basic block.
  304. if (Value *V =
  305. SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
  306. // On the off-chance that this simplifies to an instruction in the old
  307. // function, map it back into the new function.
  308. if (NewFunc != OldFunc)
  309. if (Value *MappedV = VMap.lookup(V))
  310. V = MappedV;
  311. if (!NewInst->mayHaveSideEffects()) {
  312. VMap[&*II] = V;
  313. NewInst->deleteValue();
  314. continue;
  315. }
  316. }
  317. }
  318. if (II->hasName())
  319. NewInst->setName(II->getName()+NameSuffix);
  320. VMap[&*II] = NewInst; // Add instruction map to value.
  321. NewBB->getInstList().push_back(NewInst);
  322. hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
  323. if (CodeInfo)
  324. if (auto *CB = dyn_cast<CallBase>(&*II))
  325. if (CB->hasOperandBundles())
  326. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  327. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  328. if (isa<ConstantInt>(AI->getArraySize()))
  329. hasStaticAllocas = true;
  330. else
  331. hasDynamicAllocas = true;
  332. }
  333. }
  334. // Finally, clone over the terminator.
  335. const Instruction *OldTI = BB->getTerminator();
  336. bool TerminatorDone = false;
  337. if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
  338. if (BI->isConditional()) {
  339. // If the condition was a known constant in the callee...
  340. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  341. // Or is a known constant in the caller...
  342. if (!Cond) {
  343. Value *V = VMap.lookup(BI->getCondition());
  344. Cond = dyn_cast_or_null<ConstantInt>(V);
  345. }
  346. // Constant fold to uncond branch!
  347. if (Cond) {
  348. BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
  349. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  350. ToClone.push_back(Dest);
  351. TerminatorDone = true;
  352. }
  353. }
  354. } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
  355. // If switching on a value known constant in the caller.
  356. ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
  357. if (!Cond) { // Or known constant after constant prop in the callee...
  358. Value *V = VMap.lookup(SI->getCondition());
  359. Cond = dyn_cast_or_null<ConstantInt>(V);
  360. }
  361. if (Cond) { // Constant fold to uncond branch!
  362. SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
  363. BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
  364. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  365. ToClone.push_back(Dest);
  366. TerminatorDone = true;
  367. }
  368. }
  369. if (!TerminatorDone) {
  370. Instruction *NewInst = OldTI->clone();
  371. if (OldTI->hasName())
  372. NewInst->setName(OldTI->getName()+NameSuffix);
  373. NewBB->getInstList().push_back(NewInst);
  374. VMap[OldTI] = NewInst; // Add instruction map to value.
  375. if (CodeInfo)
  376. if (auto *CB = dyn_cast<CallBase>(OldTI))
  377. if (CB->hasOperandBundles())
  378. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  379. // Recursively clone any reachable successor blocks.
  380. append_range(ToClone, successors(BB->getTerminator()));
  381. }
  382. if (CodeInfo) {
  383. CodeInfo->ContainsCalls |= hasCalls;
  384. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  385. CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
  386. BB != &BB->getParent()->front();
  387. }
  388. }
  389. /// This works like CloneAndPruneFunctionInto, except that it does not clone the
  390. /// entire function. Instead it starts at an instruction provided by the caller
  391. /// and copies (and prunes) only the code reachable from that instruction.
  392. void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
  393. const Instruction *StartingInst,
  394. ValueToValueMapTy &VMap,
  395. bool ModuleLevelChanges,
  396. SmallVectorImpl<ReturnInst *> &Returns,
  397. const char *NameSuffix,
  398. ClonedCodeInfo *CodeInfo) {
  399. assert(NameSuffix && "NameSuffix cannot be null!");
  400. ValueMapTypeRemapper *TypeMapper = nullptr;
  401. ValueMaterializer *Materializer = nullptr;
  402. #ifndef NDEBUG
  403. // If the cloning starts at the beginning of the function, verify that
  404. // the function arguments are mapped.
  405. if (!StartingInst)
  406. for (const Argument &II : OldFunc->args())
  407. assert(VMap.count(&II) && "No mapping from source argument specified!");
  408. #endif
  409. PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
  410. NameSuffix, CodeInfo);
  411. const BasicBlock *StartingBB;
  412. if (StartingInst)
  413. StartingBB = StartingInst->getParent();
  414. else {
  415. StartingBB = &OldFunc->getEntryBlock();
  416. StartingInst = &StartingBB->front();
  417. }
  418. // Clone the entry block, and anything recursively reachable from it.
  419. std::vector<const BasicBlock*> CloneWorklist;
  420. PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
  421. while (!CloneWorklist.empty()) {
  422. const BasicBlock *BB = CloneWorklist.back();
  423. CloneWorklist.pop_back();
  424. PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
  425. }
  426. // Loop over all of the basic blocks in the old function. If the block was
  427. // reachable, we have cloned it and the old block is now in the value map:
  428. // insert it into the new function in the right order. If not, ignore it.
  429. //
  430. // Defer PHI resolution until rest of function is resolved.
  431. SmallVector<const PHINode*, 16> PHIToResolve;
  432. for (const BasicBlock &BI : *OldFunc) {
  433. Value *V = VMap.lookup(&BI);
  434. BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
  435. if (!NewBB) continue; // Dead block.
  436. // Add the new block to the new function.
  437. NewFunc->getBasicBlockList().push_back(NewBB);
  438. // Handle PHI nodes specially, as we have to remove references to dead
  439. // blocks.
  440. for (const PHINode &PN : BI.phis()) {
  441. // PHI nodes may have been remapped to non-PHI nodes by the caller or
  442. // during the cloning process.
  443. if (isa<PHINode>(VMap[&PN]))
  444. PHIToResolve.push_back(&PN);
  445. else
  446. break;
  447. }
  448. // Finally, remap the terminator instructions, as those can't be remapped
  449. // until all BBs are mapped.
  450. RemapInstruction(NewBB->getTerminator(), VMap,
  451. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  452. TypeMapper, Materializer);
  453. }
  454. // Defer PHI resolution until rest of function is resolved, PHI resolution
  455. // requires the CFG to be up-to-date.
  456. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
  457. const PHINode *OPN = PHIToResolve[phino];
  458. unsigned NumPreds = OPN->getNumIncomingValues();
  459. const BasicBlock *OldBB = OPN->getParent();
  460. BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
  461. // Map operands for blocks that are live and remove operands for blocks
  462. // that are dead.
  463. for (; phino != PHIToResolve.size() &&
  464. PHIToResolve[phino]->getParent() == OldBB; ++phino) {
  465. OPN = PHIToResolve[phino];
  466. PHINode *PN = cast<PHINode>(VMap[OPN]);
  467. for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
  468. Value *V = VMap.lookup(PN->getIncomingBlock(pred));
  469. if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
  470. Value *InVal = MapValue(PN->getIncomingValue(pred),
  471. VMap,
  472. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  473. assert(InVal && "Unknown input value?");
  474. PN->setIncomingValue(pred, InVal);
  475. PN->setIncomingBlock(pred, MappedBlock);
  476. } else {
  477. PN->removeIncomingValue(pred, false);
  478. --pred; // Revisit the next entry.
  479. --e;
  480. }
  481. }
  482. }
  483. // The loop above has removed PHI entries for those blocks that are dead
  484. // and has updated others. However, if a block is live (i.e. copied over)
  485. // but its terminator has been changed to not go to this block, then our
  486. // phi nodes will have invalid entries. Update the PHI nodes in this
  487. // case.
  488. PHINode *PN = cast<PHINode>(NewBB->begin());
  489. NumPreds = pred_size(NewBB);
  490. if (NumPreds != PN->getNumIncomingValues()) {
  491. assert(NumPreds < PN->getNumIncomingValues());
  492. // Count how many times each predecessor comes to this block.
  493. std::map<BasicBlock*, unsigned> PredCount;
  494. for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
  495. PI != E; ++PI)
  496. --PredCount[*PI];
  497. // Figure out how many entries to remove from each PHI.
  498. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  499. ++PredCount[PN->getIncomingBlock(i)];
  500. // At this point, the excess predecessor entries are positive in the
  501. // map. Loop over all of the PHIs and remove excess predecessor
  502. // entries.
  503. BasicBlock::iterator I = NewBB->begin();
  504. for (; (PN = dyn_cast<PHINode>(I)); ++I) {
  505. for (const auto &PCI : PredCount) {
  506. BasicBlock *Pred = PCI.first;
  507. for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
  508. PN->removeIncomingValue(Pred, false);
  509. }
  510. }
  511. }
  512. // If the loops above have made these phi nodes have 0 or 1 operand,
  513. // replace them with undef or the input value. We must do this for
  514. // correctness, because 0-operand phis are not valid.
  515. PN = cast<PHINode>(NewBB->begin());
  516. if (PN->getNumIncomingValues() == 0) {
  517. BasicBlock::iterator I = NewBB->begin();
  518. BasicBlock::const_iterator OldI = OldBB->begin();
  519. while ((PN = dyn_cast<PHINode>(I++))) {
  520. Value *NV = UndefValue::get(PN->getType());
  521. PN->replaceAllUsesWith(NV);
  522. assert(VMap[&*OldI] == PN && "VMap mismatch");
  523. VMap[&*OldI] = NV;
  524. PN->eraseFromParent();
  525. ++OldI;
  526. }
  527. }
  528. }
  529. // Make a second pass over the PHINodes now that all of them have been
  530. // remapped into the new function, simplifying the PHINode and performing any
  531. // recursive simplifications exposed. This will transparently update the
  532. // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
  533. // two PHINodes, the iteration over the old PHIs remains valid, and the
  534. // mapping will just map us to the new node (which may not even be a PHI
  535. // node).
  536. const DataLayout &DL = NewFunc->getParent()->getDataLayout();
  537. SmallSetVector<const Value *, 8> Worklist;
  538. for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
  539. if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
  540. Worklist.insert(PHIToResolve[Idx]);
  541. // Note that we must test the size on each iteration, the worklist can grow.
  542. for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
  543. const Value *OrigV = Worklist[Idx];
  544. auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
  545. if (!I)
  546. continue;
  547. // Skip over non-intrinsic callsites, we don't want to remove any nodes from
  548. // the CGSCC.
  549. CallBase *CB = dyn_cast<CallBase>(I);
  550. if (CB && CB->getCalledFunction() &&
  551. !CB->getCalledFunction()->isIntrinsic())
  552. continue;
  553. // See if this instruction simplifies.
  554. Value *SimpleV = SimplifyInstruction(I, DL);
  555. if (!SimpleV)
  556. continue;
  557. // Stash away all the uses of the old instruction so we can check them for
  558. // recursive simplifications after a RAUW. This is cheaper than checking all
  559. // uses of To on the recursive step in most cases.
  560. for (const User *U : OrigV->users())
  561. Worklist.insert(cast<Instruction>(U));
  562. // Replace the instruction with its simplified value.
  563. I->replaceAllUsesWith(SimpleV);
  564. // If the original instruction had no side effects, remove it.
  565. if (isInstructionTriviallyDead(I))
  566. I->eraseFromParent();
  567. else
  568. VMap[OrigV] = I;
  569. }
  570. // Now that the inlined function body has been fully constructed, go through
  571. // and zap unconditional fall-through branches. This happens all the time when
  572. // specializing code: code specialization turns conditional branches into
  573. // uncond branches, and this code folds them.
  574. Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
  575. Function::iterator I = Begin;
  576. while (I != NewFunc->end()) {
  577. // We need to simplify conditional branches and switches with a constant
  578. // operand. We try to prune these out when cloning, but if the
  579. // simplification required looking through PHI nodes, those are only
  580. // available after forming the full basic block. That may leave some here,
  581. // and we still want to prune the dead code as early as possible.
  582. //
  583. // Do the folding before we check if the block is dead since we want code
  584. // like
  585. // bb:
  586. // br i1 undef, label %bb, label %bb
  587. // to be simplified to
  588. // bb:
  589. // br label %bb
  590. // before we call I->getSinglePredecessor().
  591. ConstantFoldTerminator(&*I);
  592. // Check if this block has become dead during inlining or other
  593. // simplifications. Note that the first block will appear dead, as it has
  594. // not yet been wired up properly.
  595. if (I != Begin && (pred_empty(&*I) || I->getSinglePredecessor() == &*I)) {
  596. BasicBlock *DeadBB = &*I++;
  597. DeleteDeadBlock(DeadBB);
  598. continue;
  599. }
  600. BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
  601. if (!BI || BI->isConditional()) { ++I; continue; }
  602. BasicBlock *Dest = BI->getSuccessor(0);
  603. if (!Dest->getSinglePredecessor()) {
  604. ++I; continue;
  605. }
  606. // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
  607. // above should have zapped all of them..
  608. assert(!isa<PHINode>(Dest->begin()));
  609. // We know all single-entry PHI nodes in the inlined function have been
  610. // removed, so we just need to splice the blocks.
  611. BI->eraseFromParent();
  612. // Make all PHI nodes that referred to Dest now refer to I as their source.
  613. Dest->replaceAllUsesWith(&*I);
  614. // Move all the instructions in the succ to the pred.
  615. I->getInstList().splice(I->end(), Dest->getInstList());
  616. // Remove the dest block.
  617. Dest->eraseFromParent();
  618. // Do not increment I, iteratively merge all things this block branches to.
  619. }
  620. // Make a final pass over the basic blocks from the old function to gather
  621. // any return instructions which survived folding. We have to do this here
  622. // because we can iteratively remove and merge returns above.
  623. for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
  624. E = NewFunc->end();
  625. I != E; ++I)
  626. if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
  627. Returns.push_back(RI);
  628. }
  629. /// This works exactly like CloneFunctionInto,
  630. /// except that it does some simple constant prop and DCE on the fly. The
  631. /// effect of this is to copy significantly less code in cases where (for
  632. /// example) a function call with constant arguments is inlined, and those
  633. /// constant arguments cause a significant amount of code in the callee to be
  634. /// dead. Since this doesn't produce an exact copy of the input, it can't be
  635. /// used for things like CloneFunction or CloneModule.
  636. void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
  637. ValueToValueMapTy &VMap,
  638. bool ModuleLevelChanges,
  639. SmallVectorImpl<ReturnInst*> &Returns,
  640. const char *NameSuffix,
  641. ClonedCodeInfo *CodeInfo,
  642. Instruction *TheCall) {
  643. CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
  644. ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
  645. }
  646. /// Remaps instructions in \p Blocks using the mapping in \p VMap.
  647. void llvm::remapInstructionsInBlocks(
  648. const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
  649. // Rewrite the code to refer to itself.
  650. for (auto *BB : Blocks)
  651. for (auto &Inst : *BB)
  652. RemapInstruction(&Inst, VMap,
  653. RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
  654. }
  655. /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
  656. /// Blocks.
  657. ///
  658. /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
  659. /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
  660. Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
  661. Loop *OrigLoop, ValueToValueMapTy &VMap,
  662. const Twine &NameSuffix, LoopInfo *LI,
  663. DominatorTree *DT,
  664. SmallVectorImpl<BasicBlock *> &Blocks) {
  665. Function *F = OrigLoop->getHeader()->getParent();
  666. Loop *ParentLoop = OrigLoop->getParentLoop();
  667. DenseMap<Loop *, Loop *> LMap;
  668. Loop *NewLoop = LI->AllocateLoop();
  669. LMap[OrigLoop] = NewLoop;
  670. if (ParentLoop)
  671. ParentLoop->addChildLoop(NewLoop);
  672. else
  673. LI->addTopLevelLoop(NewLoop);
  674. BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
  675. assert(OrigPH && "No preheader");
  676. BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
  677. // To rename the loop PHIs.
  678. VMap[OrigPH] = NewPH;
  679. Blocks.push_back(NewPH);
  680. // Update LoopInfo.
  681. if (ParentLoop)
  682. ParentLoop->addBasicBlockToLoop(NewPH, *LI);
  683. // Update DominatorTree.
  684. DT->addNewBlock(NewPH, LoopDomBB);
  685. for (Loop *CurLoop : OrigLoop->getLoopsInPreorder()) {
  686. Loop *&NewLoop = LMap[CurLoop];
  687. if (!NewLoop) {
  688. NewLoop = LI->AllocateLoop();
  689. // Establish the parent/child relationship.
  690. Loop *OrigParent = CurLoop->getParentLoop();
  691. assert(OrigParent && "Could not find the original parent loop");
  692. Loop *NewParentLoop = LMap[OrigParent];
  693. assert(NewParentLoop && "Could not find the new parent loop");
  694. NewParentLoop->addChildLoop(NewLoop);
  695. }
  696. }
  697. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  698. Loop *CurLoop = LI->getLoopFor(BB);
  699. Loop *&NewLoop = LMap[CurLoop];
  700. assert(NewLoop && "Expecting new loop to be allocated");
  701. BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
  702. VMap[BB] = NewBB;
  703. // Update LoopInfo.
  704. NewLoop->addBasicBlockToLoop(NewBB, *LI);
  705. // Add DominatorTree node. After seeing all blocks, update to correct
  706. // IDom.
  707. DT->addNewBlock(NewBB, NewPH);
  708. Blocks.push_back(NewBB);
  709. }
  710. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  711. // Update loop headers.
  712. Loop *CurLoop = LI->getLoopFor(BB);
  713. if (BB == CurLoop->getHeader())
  714. LMap[CurLoop]->moveToHeader(cast<BasicBlock>(VMap[BB]));
  715. // Update DominatorTree.
  716. BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
  717. DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
  718. cast<BasicBlock>(VMap[IDomBB]));
  719. }
  720. // Move them physically from the end of the block list.
  721. F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
  722. NewPH);
  723. F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
  724. NewLoop->getHeader()->getIterator(), F->end());
  725. return NewLoop;
  726. }
  727. /// Duplicate non-Phi instructions from the beginning of block up to
  728. /// StopAt instruction into a split block between BB and its predecessor.
  729. BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
  730. BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt,
  731. ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU) {
  732. assert(count(successors(PredBB), BB) == 1 &&
  733. "There must be a single edge between PredBB and BB!");
  734. // We are going to have to map operands from the original BB block to the new
  735. // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
  736. // account for entry from PredBB.
  737. BasicBlock::iterator BI = BB->begin();
  738. for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
  739. ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
  740. BasicBlock *NewBB = SplitEdge(PredBB, BB);
  741. NewBB->setName(PredBB->getName() + ".split");
  742. Instruction *NewTerm = NewBB->getTerminator();
  743. // FIXME: SplitEdge does not yet take a DTU, so we include the split edge
  744. // in the update set here.
  745. DTU.applyUpdates({{DominatorTree::Delete, PredBB, BB},
  746. {DominatorTree::Insert, PredBB, NewBB},
  747. {DominatorTree::Insert, NewBB, BB}});
  748. // Clone the non-phi instructions of BB into NewBB, keeping track of the
  749. // mapping and using it to remap operands in the cloned instructions.
  750. // Stop once we see the terminator too. This covers the case where BB's
  751. // terminator gets replaced and StopAt == BB's terminator.
  752. for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
  753. Instruction *New = BI->clone();
  754. New->setName(BI->getName());
  755. New->insertBefore(NewTerm);
  756. ValueMapping[&*BI] = New;
  757. // Remap operands to patch up intra-block references.
  758. for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
  759. if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
  760. auto I = ValueMapping.find(Inst);
  761. if (I != ValueMapping.end())
  762. New->setOperand(i, I->second);
  763. }
  764. }
  765. return NewBB;
  766. }
  767. void llvm::cloneNoAliasScopes(
  768. ArrayRef<MDNode *> NoAliasDeclScopes,
  769. DenseMap<MDNode *, MDNode *> &ClonedScopes,
  770. StringRef Ext, LLVMContext &Context) {
  771. MDBuilder MDB(Context);
  772. for (auto *ScopeList : NoAliasDeclScopes) {
  773. for (auto &MDOperand : ScopeList->operands()) {
  774. if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
  775. AliasScopeNode SNANode(MD);
  776. std::string Name;
  777. auto ScopeName = SNANode.getName();
  778. if (!ScopeName.empty())
  779. Name = (Twine(ScopeName) + ":" + Ext).str();
  780. else
  781. Name = std::string(Ext);
  782. MDNode *NewScope = MDB.createAnonymousAliasScope(
  783. const_cast<MDNode *>(SNANode.getDomain()), Name);
  784. ClonedScopes.insert(std::make_pair(MD, NewScope));
  785. }
  786. }
  787. }
  788. }
  789. void llvm::adaptNoAliasScopes(
  790. Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
  791. LLVMContext &Context) {
  792. auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
  793. bool NeedsReplacement = false;
  794. SmallVector<Metadata *, 8> NewScopeList;
  795. for (auto &MDOp : ScopeList->operands()) {
  796. if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
  797. if (auto *NewMD = ClonedScopes.lookup(MD)) {
  798. NewScopeList.push_back(NewMD);
  799. NeedsReplacement = true;
  800. continue;
  801. }
  802. NewScopeList.push_back(MD);
  803. }
  804. }
  805. if (NeedsReplacement)
  806. return MDNode::get(Context, NewScopeList);
  807. return nullptr;
  808. };
  809. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
  810. if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
  811. Decl->setScopeList(NewScopeList);
  812. auto replaceWhenNeeded = [&](unsigned MD_ID) {
  813. if (const MDNode *CSNoAlias = I->getMetadata(MD_ID))
  814. if (auto *NewScopeList = CloneScopeList(CSNoAlias))
  815. I->setMetadata(MD_ID, NewScopeList);
  816. };
  817. replaceWhenNeeded(LLVMContext::MD_noalias);
  818. replaceWhenNeeded(LLVMContext::MD_alias_scope);
  819. }
  820. void llvm::cloneAndAdaptNoAliasScopes(
  821. ArrayRef<MDNode *> NoAliasDeclScopes,
  822. ArrayRef<BasicBlock *> NewBlocks, LLVMContext &Context, StringRef Ext) {
  823. if (NoAliasDeclScopes.empty())
  824. return;
  825. DenseMap<MDNode *, MDNode *> ClonedScopes;
  826. LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
  827. << NoAliasDeclScopes.size() << " node(s)\n");
  828. cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
  829. // Identify instructions using metadata that needs adaptation
  830. for (BasicBlock *NewBlock : NewBlocks)
  831. for (Instruction &I : *NewBlock)
  832. adaptNoAliasScopes(&I, ClonedScopes, Context);
  833. }
  834. void llvm::cloneAndAdaptNoAliasScopes(
  835. ArrayRef<MDNode *> NoAliasDeclScopes, Instruction *IStart,
  836. Instruction *IEnd, LLVMContext &Context, StringRef Ext) {
  837. if (NoAliasDeclScopes.empty())
  838. return;
  839. DenseMap<MDNode *, MDNode *> ClonedScopes;
  840. LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
  841. << NoAliasDeclScopes.size() << " node(s)\n");
  842. cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
  843. // Identify instructions using metadata that needs adaptation
  844. assert(IStart->getParent() == IEnd->getParent() && "different basic block ?");
  845. auto ItStart = IStart->getIterator();
  846. auto ItEnd = IEnd->getIterator();
  847. ++ItEnd; // IEnd is included, increment ItEnd to get the end of the range
  848. for (auto &I : llvm::make_range(ItStart, ItEnd))
  849. adaptNoAliasScopes(&I, ClonedScopes, Context);
  850. }
  851. void llvm::identifyNoAliasScopesToClone(
  852. ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
  853. for (BasicBlock *BB : BBs)
  854. for (Instruction &I : *BB)
  855. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
  856. NoAliasDeclScopes.push_back(Decl->getScopeList());
  857. }
  858. void llvm::identifyNoAliasScopesToClone(
  859. BasicBlock::iterator Start, BasicBlock::iterator End,
  860. SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
  861. for (Instruction &I : make_range(Start, End))
  862. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
  863. NoAliasDeclScopes.push_back(Decl->getScopeList());
  864. }