CloneFunction.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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/DomTreeUpdater.h"
  17. #include "llvm/Analysis/InstructionSimplify.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/IR/CFG.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DebugInfo.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/IntrinsicInst.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/MDBuilder.h"
  28. #include "llvm/IR/Metadata.h"
  29. #include "llvm/IR/Module.h"
  30. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  31. #include "llvm/Transforms/Utils/Cloning.h"
  32. #include "llvm/Transforms/Utils/Local.h"
  33. #include "llvm/Transforms/Utils/ValueMapper.h"
  34. #include <map>
  35. #include <optional>
  36. using namespace llvm;
  37. #define DEBUG_TYPE "clone-function"
  38. /// See comments in Cloning.h.
  39. BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
  40. const Twine &NameSuffix, Function *F,
  41. ClonedCodeInfo *CodeInfo,
  42. DebugInfoFinder *DIFinder) {
  43. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
  44. if (BB->hasName())
  45. NewBB->setName(BB->getName() + NameSuffix);
  46. bool hasCalls = false, hasDynamicAllocas = false, hasMemProfMetadata = 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. NewInst->insertInto(NewBB, NewBB->end());
  56. VMap[&I] = NewInst; // Add instruction map to value.
  57. if (isa<CallInst>(I) && !I.isDebugOrPseudoInst()) {
  58. hasCalls = true;
  59. hasMemProfMetadata |= I.hasMetadata(LLVMContext::MD_memprof);
  60. }
  61. if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
  62. if (!AI->isStaticAlloca()) {
  63. hasDynamicAllocas = true;
  64. }
  65. }
  66. }
  67. if (CodeInfo) {
  68. CodeInfo->ContainsCalls |= hasCalls;
  69. CodeInfo->ContainsMemProfMetadata |= hasMemProfMetadata;
  70. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  71. }
  72. return NewBB;
  73. }
  74. // Clone OldFunc into NewFunc, transforming the old arguments into references to
  75. // VMap values.
  76. //
  77. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  78. ValueToValueMapTy &VMap,
  79. CloneFunctionChangeType Changes,
  80. SmallVectorImpl<ReturnInst *> &Returns,
  81. const char *NameSuffix, ClonedCodeInfo *CodeInfo,
  82. ValueMapTypeRemapper *TypeMapper,
  83. ValueMaterializer *Materializer) {
  84. assert(NameSuffix && "NameSuffix cannot be null!");
  85. #ifndef NDEBUG
  86. for (const Argument &I : OldFunc->args())
  87. assert(VMap.count(&I) && "No mapping from source argument specified!");
  88. #endif
  89. bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
  90. // Copy all attributes other than those stored in the AttributeList. We need
  91. // to remap the parameter indices of the AttributeList.
  92. AttributeList NewAttrs = NewFunc->getAttributes();
  93. NewFunc->copyAttributesFrom(OldFunc);
  94. NewFunc->setAttributes(NewAttrs);
  95. const RemapFlags FuncGlobalRefFlags =
  96. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
  97. // Fix up the personality function that got copied over.
  98. if (OldFunc->hasPersonalityFn())
  99. NewFunc->setPersonalityFn(MapValue(OldFunc->getPersonalityFn(), VMap,
  100. FuncGlobalRefFlags, TypeMapper,
  101. Materializer));
  102. if (OldFunc->hasPrefixData()) {
  103. NewFunc->setPrefixData(MapValue(OldFunc->getPrefixData(), VMap,
  104. FuncGlobalRefFlags, TypeMapper,
  105. Materializer));
  106. }
  107. if (OldFunc->hasPrologueData()) {
  108. NewFunc->setPrologueData(MapValue(OldFunc->getPrologueData(), VMap,
  109. FuncGlobalRefFlags, TypeMapper,
  110. Materializer));
  111. }
  112. SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
  113. AttributeList OldAttrs = OldFunc->getAttributes();
  114. // Clone any argument attributes that are present in the VMap.
  115. for (const Argument &OldArg : OldFunc->args()) {
  116. if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
  117. NewArgAttrs[NewArg->getArgNo()] =
  118. OldAttrs.getParamAttrs(OldArg.getArgNo());
  119. }
  120. }
  121. NewFunc->setAttributes(
  122. AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttrs(),
  123. OldAttrs.getRetAttrs(), NewArgAttrs));
  124. // Everything else beyond this point deals with function instructions,
  125. // so if we are dealing with a function declaration, we're done.
  126. if (OldFunc->isDeclaration())
  127. return;
  128. // When we remap instructions within the same module, we want to avoid
  129. // duplicating inlined DISubprograms, so record all subprograms we find as we
  130. // duplicate instructions and then freeze them in the MD map. We also record
  131. // information about dbg.value and dbg.declare to avoid duplicating the
  132. // types.
  133. std::optional<DebugInfoFinder> DIFinder;
  134. // Track the subprogram attachment that needs to be cloned to fine-tune the
  135. // mapping within the same module.
  136. DISubprogram *SPClonedWithinModule = nullptr;
  137. if (Changes < CloneFunctionChangeType::DifferentModule) {
  138. assert((NewFunc->getParent() == nullptr ||
  139. NewFunc->getParent() == OldFunc->getParent()) &&
  140. "Expected NewFunc to have the same parent, or no parent");
  141. // Need to find subprograms, types, and compile units.
  142. DIFinder.emplace();
  143. SPClonedWithinModule = OldFunc->getSubprogram();
  144. if (SPClonedWithinModule)
  145. DIFinder->processSubprogram(SPClonedWithinModule);
  146. } else {
  147. assert((NewFunc->getParent() == nullptr ||
  148. NewFunc->getParent() != OldFunc->getParent()) &&
  149. "Expected NewFunc to have different parents, or no parent");
  150. if (Changes == CloneFunctionChangeType::DifferentModule) {
  151. assert(NewFunc->getParent() &&
  152. "Need parent of new function to maintain debug info invariants");
  153. // Need to find all the compile units.
  154. DIFinder.emplace();
  155. }
  156. }
  157. // Loop over all of the basic blocks in the function, cloning them as
  158. // appropriate. Note that we save BE this way in order to handle cloning of
  159. // recursive functions into themselves.
  160. for (const BasicBlock &BB : *OldFunc) {
  161. // Create a new basic block and copy instructions into it!
  162. BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo,
  163. DIFinder ? &*DIFinder : nullptr);
  164. // Add basic block mapping.
  165. VMap[&BB] = CBB;
  166. // It is only legal to clone a function if a block address within that
  167. // function is never referenced outside of the function. Given that, we
  168. // want to map block addresses from the old function to block addresses in
  169. // the clone. (This is different from the generic ValueMapper
  170. // implementation, which generates an invalid blockaddress when
  171. // cloning a function.)
  172. if (BB.hasAddressTaken()) {
  173. Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
  174. const_cast<BasicBlock *>(&BB));
  175. VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
  176. }
  177. // Note return instructions for the caller.
  178. if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
  179. Returns.push_back(RI);
  180. }
  181. if (Changes < CloneFunctionChangeType::DifferentModule &&
  182. DIFinder->subprogram_count() > 0) {
  183. // Turn on module-level changes, since we need to clone (some of) the
  184. // debug info metadata.
  185. //
  186. // FIXME: Metadata effectively owned by a function should be made
  187. // local, and only that local metadata should be cloned.
  188. ModuleLevelChanges = true;
  189. auto mapToSelfIfNew = [&VMap](MDNode *N) {
  190. // Avoid clobbering an existing mapping.
  191. (void)VMap.MD().try_emplace(N, N);
  192. };
  193. // Avoid cloning types, compile units, and (other) subprograms.
  194. SmallPtrSet<const DISubprogram *, 16> MappedToSelfSPs;
  195. for (DISubprogram *ISP : DIFinder->subprograms()) {
  196. if (ISP != SPClonedWithinModule) {
  197. mapToSelfIfNew(ISP);
  198. MappedToSelfSPs.insert(ISP);
  199. }
  200. }
  201. // If a subprogram isn't going to be cloned skip its lexical blocks as well.
  202. for (DIScope *S : DIFinder->scopes()) {
  203. auto *LScope = dyn_cast<DILocalScope>(S);
  204. if (LScope && MappedToSelfSPs.count(LScope->getSubprogram()))
  205. mapToSelfIfNew(S);
  206. }
  207. for (DICompileUnit *CU : DIFinder->compile_units())
  208. mapToSelfIfNew(CU);
  209. for (DIType *Type : DIFinder->types())
  210. mapToSelfIfNew(Type);
  211. } else {
  212. assert(!SPClonedWithinModule &&
  213. "Subprogram should be in DIFinder->subprogram_count()...");
  214. }
  215. const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
  216. // Duplicate the metadata that is attached to the cloned function.
  217. // Subprograms/CUs/types that were already mapped to themselves won't be
  218. // duplicated.
  219. SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
  220. OldFunc->getAllMetadata(MDs);
  221. for (auto MD : MDs) {
  222. NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag,
  223. TypeMapper, Materializer));
  224. }
  225. // Loop over all of the instructions in the new function, fixing up operand
  226. // references as we go. This uses VMap to do all the hard work.
  227. for (Function::iterator
  228. BB = cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
  229. BE = NewFunc->end();
  230. BB != BE; ++BB)
  231. // Loop over all instructions, fixing each one as we find it...
  232. for (Instruction &II : *BB)
  233. RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer);
  234. // Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the
  235. // same module, the compile unit will already be listed (or not). When
  236. // cloning a module, CloneModule() will handle creating the named metadata.
  237. if (Changes != CloneFunctionChangeType::DifferentModule)
  238. return;
  239. // Update !llvm.dbg.cu with compile units added to the new module if this
  240. // function is being cloned in isolation.
  241. //
  242. // FIXME: This is making global / module-level changes, which doesn't seem
  243. // like the right encapsulation Consider dropping the requirement to update
  244. // !llvm.dbg.cu (either obsoleting the node, or restricting it to
  245. // non-discardable compile units) instead of discovering compile units by
  246. // visiting the metadata attached to global values, which would allow this
  247. // code to be deleted. Alternatively, perhaps give responsibility for this
  248. // update to CloneFunctionInto's callers.
  249. auto *NewModule = NewFunc->getParent();
  250. auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
  251. // Avoid multiple insertions of the same DICompileUnit to NMD.
  252. SmallPtrSet<const void *, 8> Visited;
  253. for (auto *Operand : NMD->operands())
  254. Visited.insert(Operand);
  255. for (auto *Unit : DIFinder->compile_units()) {
  256. MDNode *MappedUnit =
  257. MapMetadata(Unit, VMap, RF_None, TypeMapper, Materializer);
  258. if (Visited.insert(MappedUnit).second)
  259. NMD->addOperand(MappedUnit);
  260. }
  261. }
  262. /// Return a copy of the specified function and add it to that function's
  263. /// module. Also, any references specified in the VMap are changed to refer to
  264. /// their mapped value instead of the original one. If any of the arguments to
  265. /// the function are in the VMap, the arguments are deleted from the resultant
  266. /// function. The VMap is updated to include mappings from all of the
  267. /// instructions and basicblocks in the function from their old to new values.
  268. ///
  269. Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
  270. ClonedCodeInfo *CodeInfo) {
  271. std::vector<Type *> ArgTypes;
  272. // The user might be deleting arguments to the function by specifying them in
  273. // the VMap. If so, we need to not add the arguments to the arg ty vector
  274. //
  275. for (const Argument &I : F->args())
  276. if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
  277. ArgTypes.push_back(I.getType());
  278. // Create a new function type...
  279. FunctionType *FTy =
  280. FunctionType::get(F->getFunctionType()->getReturnType(), ArgTypes,
  281. F->getFunctionType()->isVarArg());
  282. // Create the new function...
  283. Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(),
  284. F->getName(), F->getParent());
  285. // Loop over the arguments, copying the names of the mapped arguments over...
  286. Function::arg_iterator DestI = NewF->arg_begin();
  287. for (const Argument &I : F->args())
  288. if (VMap.count(&I) == 0) { // Is this argument preserved?
  289. DestI->setName(I.getName()); // Copy the name over...
  290. VMap[&I] = &*DestI++; // Add mapping to VMap
  291. }
  292. SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
  293. CloneFunctionInto(NewF, F, VMap, CloneFunctionChangeType::LocalChangesOnly,
  294. Returns, "", CodeInfo);
  295. return NewF;
  296. }
  297. namespace {
  298. /// This is a private class used to implement CloneAndPruneFunctionInto.
  299. struct PruningFunctionCloner {
  300. Function *NewFunc;
  301. const Function *OldFunc;
  302. ValueToValueMapTy &VMap;
  303. bool ModuleLevelChanges;
  304. const char *NameSuffix;
  305. ClonedCodeInfo *CodeInfo;
  306. bool HostFuncIsStrictFP;
  307. Instruction *cloneInstruction(BasicBlock::const_iterator II);
  308. public:
  309. PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
  310. ValueToValueMapTy &valueMap, bool moduleLevelChanges,
  311. const char *nameSuffix, ClonedCodeInfo *codeInfo)
  312. : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
  313. ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
  314. CodeInfo(codeInfo) {
  315. HostFuncIsStrictFP =
  316. newFunc->getAttributes().hasFnAttr(Attribute::StrictFP);
  317. }
  318. /// The specified block is found to be reachable, clone it and
  319. /// anything that it can reach.
  320. void CloneBlock(const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
  321. std::vector<const BasicBlock *> &ToClone);
  322. };
  323. } // namespace
  324. static bool hasRoundingModeOperand(Intrinsic::ID CIID) {
  325. switch (CIID) {
  326. #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
  327. case Intrinsic::INTRINSIC: \
  328. return ROUND_MODE == 1;
  329. #define FUNCTION INSTRUCTION
  330. #include "llvm/IR/ConstrainedOps.def"
  331. default:
  332. llvm_unreachable("Unexpected constrained intrinsic id");
  333. }
  334. }
  335. Instruction *
  336. PruningFunctionCloner::cloneInstruction(BasicBlock::const_iterator II) {
  337. const Instruction &OldInst = *II;
  338. Instruction *NewInst = nullptr;
  339. if (HostFuncIsStrictFP) {
  340. Intrinsic::ID CIID = getConstrainedIntrinsicID(OldInst);
  341. if (CIID != Intrinsic::not_intrinsic) {
  342. // Instead of cloning the instruction, a call to constrained intrinsic
  343. // should be created.
  344. // Assume the first arguments of constrained intrinsics are the same as
  345. // the operands of original instruction.
  346. // Determine overloaded types of the intrinsic.
  347. SmallVector<Type *, 2> TParams;
  348. SmallVector<Intrinsic::IITDescriptor, 8> Descriptor;
  349. getIntrinsicInfoTableEntries(CIID, Descriptor);
  350. for (unsigned I = 0, E = Descriptor.size(); I != E; ++I) {
  351. Intrinsic::IITDescriptor Operand = Descriptor[I];
  352. switch (Operand.Kind) {
  353. case Intrinsic::IITDescriptor::Argument:
  354. if (Operand.getArgumentKind() !=
  355. Intrinsic::IITDescriptor::AK_MatchType) {
  356. if (I == 0)
  357. TParams.push_back(OldInst.getType());
  358. else
  359. TParams.push_back(OldInst.getOperand(I - 1)->getType());
  360. }
  361. break;
  362. case Intrinsic::IITDescriptor::SameVecWidthArgument:
  363. ++I;
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. // Create intrinsic call.
  370. LLVMContext &Ctx = NewFunc->getContext();
  371. Function *IFn =
  372. Intrinsic::getDeclaration(NewFunc->getParent(), CIID, TParams);
  373. SmallVector<Value *, 4> Args;
  374. unsigned NumOperands = OldInst.getNumOperands();
  375. if (isa<CallInst>(OldInst))
  376. --NumOperands;
  377. for (unsigned I = 0; I < NumOperands; ++I) {
  378. Value *Op = OldInst.getOperand(I);
  379. Args.push_back(Op);
  380. }
  381. if (const auto *CmpI = dyn_cast<FCmpInst>(&OldInst)) {
  382. FCmpInst::Predicate Pred = CmpI->getPredicate();
  383. StringRef PredName = FCmpInst::getPredicateName(Pred);
  384. Args.push_back(MetadataAsValue::get(Ctx, MDString::get(Ctx, PredName)));
  385. }
  386. // The last arguments of a constrained intrinsic are metadata that
  387. // represent rounding mode (absents in some intrinsics) and exception
  388. // behavior. The inlined function uses default settings.
  389. if (hasRoundingModeOperand(CIID))
  390. Args.push_back(
  391. MetadataAsValue::get(Ctx, MDString::get(Ctx, "round.tonearest")));
  392. Args.push_back(
  393. MetadataAsValue::get(Ctx, MDString::get(Ctx, "fpexcept.ignore")));
  394. NewInst = CallInst::Create(IFn, Args, OldInst.getName() + ".strict");
  395. }
  396. }
  397. if (!NewInst)
  398. NewInst = II->clone();
  399. return NewInst;
  400. }
  401. /// The specified block is found to be reachable, clone it and
  402. /// anything that it can reach.
  403. void PruningFunctionCloner::CloneBlock(
  404. const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
  405. std::vector<const BasicBlock *> &ToClone) {
  406. WeakTrackingVH &BBEntry = VMap[BB];
  407. // Have we already cloned this block?
  408. if (BBEntry)
  409. return;
  410. // Nope, clone it now.
  411. BasicBlock *NewBB;
  412. BBEntry = NewBB = BasicBlock::Create(BB->getContext());
  413. if (BB->hasName())
  414. NewBB->setName(BB->getName() + NameSuffix);
  415. // It is only legal to clone a function if a block address within that
  416. // function is never referenced outside of the function. Given that, we
  417. // want to map block addresses from the old function to block addresses in
  418. // the clone. (This is different from the generic ValueMapper
  419. // implementation, which generates an invalid blockaddress when
  420. // cloning a function.)
  421. //
  422. // Note that we don't need to fix the mapping for unreachable blocks;
  423. // the default mapping there is safe.
  424. if (BB->hasAddressTaken()) {
  425. Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
  426. const_cast<BasicBlock *>(BB));
  427. VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
  428. }
  429. bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
  430. bool hasMemProfMetadata = false;
  431. // Loop over all instructions, and copy them over, DCE'ing as we go. This
  432. // loop doesn't include the terminator.
  433. for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); II != IE;
  434. ++II) {
  435. Instruction *NewInst = cloneInstruction(II);
  436. if (HostFuncIsStrictFP) {
  437. // All function calls in the inlined function must get 'strictfp'
  438. // attribute to prevent undesirable optimizations.
  439. if (auto *Call = dyn_cast<CallInst>(NewInst))
  440. Call->addFnAttr(Attribute::StrictFP);
  441. }
  442. // Eagerly remap operands to the newly cloned instruction, except for PHI
  443. // nodes for which we defer processing until we update the CFG. Also defer
  444. // debug intrinsic processing because they may contain use-before-defs.
  445. if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst)) {
  446. RemapInstruction(NewInst, VMap,
  447. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  448. // If we can simplify this instruction to some other value, simply add
  449. // a mapping to that value rather than inserting a new instruction into
  450. // the basic block.
  451. if (Value *V =
  452. simplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
  453. // On the off-chance that this simplifies to an instruction in the old
  454. // function, map it back into the new function.
  455. if (NewFunc != OldFunc)
  456. if (Value *MappedV = VMap.lookup(V))
  457. V = MappedV;
  458. if (!NewInst->mayHaveSideEffects()) {
  459. VMap[&*II] = V;
  460. NewInst->deleteValue();
  461. continue;
  462. }
  463. }
  464. }
  465. if (II->hasName())
  466. NewInst->setName(II->getName() + NameSuffix);
  467. VMap[&*II] = NewInst; // Add instruction map to value.
  468. NewInst->insertInto(NewBB, NewBB->end());
  469. if (isa<CallInst>(II) && !II->isDebugOrPseudoInst()) {
  470. hasCalls = true;
  471. hasMemProfMetadata |= II->hasMetadata(LLVMContext::MD_memprof);
  472. }
  473. if (CodeInfo) {
  474. CodeInfo->OrigVMap[&*II] = NewInst;
  475. if (auto *CB = dyn_cast<CallBase>(&*II))
  476. if (CB->hasOperandBundles())
  477. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  478. }
  479. if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
  480. if (isa<ConstantInt>(AI->getArraySize()))
  481. hasStaticAllocas = true;
  482. else
  483. hasDynamicAllocas = true;
  484. }
  485. }
  486. // Finally, clone over the terminator.
  487. const Instruction *OldTI = BB->getTerminator();
  488. bool TerminatorDone = false;
  489. if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
  490. if (BI->isConditional()) {
  491. // If the condition was a known constant in the callee...
  492. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  493. // Or is a known constant in the caller...
  494. if (!Cond) {
  495. Value *V = VMap.lookup(BI->getCondition());
  496. Cond = dyn_cast_or_null<ConstantInt>(V);
  497. }
  498. // Constant fold to uncond branch!
  499. if (Cond) {
  500. BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
  501. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  502. ToClone.push_back(Dest);
  503. TerminatorDone = true;
  504. }
  505. }
  506. } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
  507. // If switching on a value known constant in the caller.
  508. ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
  509. if (!Cond) { // Or known constant after constant prop in the callee...
  510. Value *V = VMap.lookup(SI->getCondition());
  511. Cond = dyn_cast_or_null<ConstantInt>(V);
  512. }
  513. if (Cond) { // Constant fold to uncond branch!
  514. SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
  515. BasicBlock *Dest = const_cast<BasicBlock *>(Case.getCaseSuccessor());
  516. VMap[OldTI] = BranchInst::Create(Dest, NewBB);
  517. ToClone.push_back(Dest);
  518. TerminatorDone = true;
  519. }
  520. }
  521. if (!TerminatorDone) {
  522. Instruction *NewInst = OldTI->clone();
  523. if (OldTI->hasName())
  524. NewInst->setName(OldTI->getName() + NameSuffix);
  525. NewInst->insertInto(NewBB, NewBB->end());
  526. VMap[OldTI] = NewInst; // Add instruction map to value.
  527. if (CodeInfo) {
  528. CodeInfo->OrigVMap[OldTI] = NewInst;
  529. if (auto *CB = dyn_cast<CallBase>(OldTI))
  530. if (CB->hasOperandBundles())
  531. CodeInfo->OperandBundleCallSites.push_back(NewInst);
  532. }
  533. // Recursively clone any reachable successor blocks.
  534. append_range(ToClone, successors(BB->getTerminator()));
  535. }
  536. if (CodeInfo) {
  537. CodeInfo->ContainsCalls |= hasCalls;
  538. CodeInfo->ContainsMemProfMetadata |= hasMemProfMetadata;
  539. CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
  540. CodeInfo->ContainsDynamicAllocas |=
  541. hasStaticAllocas && BB != &BB->getParent()->front();
  542. }
  543. }
  544. /// This works like CloneAndPruneFunctionInto, except that it does not clone the
  545. /// entire function. Instead it starts at an instruction provided by the caller
  546. /// and copies (and prunes) only the code reachable from that instruction.
  547. void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
  548. const Instruction *StartingInst,
  549. ValueToValueMapTy &VMap,
  550. bool ModuleLevelChanges,
  551. SmallVectorImpl<ReturnInst *> &Returns,
  552. const char *NameSuffix,
  553. ClonedCodeInfo *CodeInfo) {
  554. assert(NameSuffix && "NameSuffix cannot be null!");
  555. ValueMapTypeRemapper *TypeMapper = nullptr;
  556. ValueMaterializer *Materializer = nullptr;
  557. #ifndef NDEBUG
  558. // If the cloning starts at the beginning of the function, verify that
  559. // the function arguments are mapped.
  560. if (!StartingInst)
  561. for (const Argument &II : OldFunc->args())
  562. assert(VMap.count(&II) && "No mapping from source argument specified!");
  563. #endif
  564. PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
  565. NameSuffix, CodeInfo);
  566. const BasicBlock *StartingBB;
  567. if (StartingInst)
  568. StartingBB = StartingInst->getParent();
  569. else {
  570. StartingBB = &OldFunc->getEntryBlock();
  571. StartingInst = &StartingBB->front();
  572. }
  573. // Collect debug intrinsics for remapping later.
  574. SmallVector<const DbgVariableIntrinsic *, 8> DbgIntrinsics;
  575. for (const auto &BB : *OldFunc) {
  576. for (const auto &I : BB) {
  577. if (const auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
  578. DbgIntrinsics.push_back(DVI);
  579. }
  580. }
  581. // Clone the entry block, and anything recursively reachable from it.
  582. std::vector<const BasicBlock *> CloneWorklist;
  583. PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
  584. while (!CloneWorklist.empty()) {
  585. const BasicBlock *BB = CloneWorklist.back();
  586. CloneWorklist.pop_back();
  587. PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
  588. }
  589. // Loop over all of the basic blocks in the old function. If the block was
  590. // reachable, we have cloned it and the old block is now in the value map:
  591. // insert it into the new function in the right order. If not, ignore it.
  592. //
  593. // Defer PHI resolution until rest of function is resolved.
  594. SmallVector<const PHINode *, 16> PHIToResolve;
  595. for (const BasicBlock &BI : *OldFunc) {
  596. Value *V = VMap.lookup(&BI);
  597. BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
  598. if (!NewBB)
  599. continue; // Dead block.
  600. // Add the new block to the new function.
  601. NewFunc->insert(NewFunc->end(), NewBB);
  602. // Handle PHI nodes specially, as we have to remove references to dead
  603. // blocks.
  604. for (const PHINode &PN : BI.phis()) {
  605. // PHI nodes may have been remapped to non-PHI nodes by the caller or
  606. // during the cloning process.
  607. if (isa<PHINode>(VMap[&PN]))
  608. PHIToResolve.push_back(&PN);
  609. else
  610. break;
  611. }
  612. // Finally, remap the terminator instructions, as those can't be remapped
  613. // until all BBs are mapped.
  614. RemapInstruction(NewBB->getTerminator(), VMap,
  615. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  616. TypeMapper, Materializer);
  617. }
  618. // Defer PHI resolution until rest of function is resolved, PHI resolution
  619. // requires the CFG to be up-to-date.
  620. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e;) {
  621. const PHINode *OPN = PHIToResolve[phino];
  622. unsigned NumPreds = OPN->getNumIncomingValues();
  623. const BasicBlock *OldBB = OPN->getParent();
  624. BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
  625. // Map operands for blocks that are live and remove operands for blocks
  626. // that are dead.
  627. for (; phino != PHIToResolve.size() &&
  628. PHIToResolve[phino]->getParent() == OldBB;
  629. ++phino) {
  630. OPN = PHIToResolve[phino];
  631. PHINode *PN = cast<PHINode>(VMap[OPN]);
  632. for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
  633. Value *V = VMap.lookup(PN->getIncomingBlock(pred));
  634. if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
  635. Value *InVal =
  636. MapValue(PN->getIncomingValue(pred), VMap,
  637. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
  638. assert(InVal && "Unknown input value?");
  639. PN->setIncomingValue(pred, InVal);
  640. PN->setIncomingBlock(pred, MappedBlock);
  641. } else {
  642. PN->removeIncomingValue(pred, false);
  643. --pred; // Revisit the next entry.
  644. --e;
  645. }
  646. }
  647. }
  648. // The loop above has removed PHI entries for those blocks that are dead
  649. // and has updated others. However, if a block is live (i.e. copied over)
  650. // but its terminator has been changed to not go to this block, then our
  651. // phi nodes will have invalid entries. Update the PHI nodes in this
  652. // case.
  653. PHINode *PN = cast<PHINode>(NewBB->begin());
  654. NumPreds = pred_size(NewBB);
  655. if (NumPreds != PN->getNumIncomingValues()) {
  656. assert(NumPreds < PN->getNumIncomingValues());
  657. // Count how many times each predecessor comes to this block.
  658. std::map<BasicBlock *, unsigned> PredCount;
  659. for (BasicBlock *Pred : predecessors(NewBB))
  660. --PredCount[Pred];
  661. // Figure out how many entries to remove from each PHI.
  662. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  663. ++PredCount[PN->getIncomingBlock(i)];
  664. // At this point, the excess predecessor entries are positive in the
  665. // map. Loop over all of the PHIs and remove excess predecessor
  666. // entries.
  667. BasicBlock::iterator I = NewBB->begin();
  668. for (; (PN = dyn_cast<PHINode>(I)); ++I) {
  669. for (const auto &PCI : PredCount) {
  670. BasicBlock *Pred = PCI.first;
  671. for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
  672. PN->removeIncomingValue(Pred, false);
  673. }
  674. }
  675. }
  676. // If the loops above have made these phi nodes have 0 or 1 operand,
  677. // replace them with poison or the input value. We must do this for
  678. // correctness, because 0-operand phis are not valid.
  679. PN = cast<PHINode>(NewBB->begin());
  680. if (PN->getNumIncomingValues() == 0) {
  681. BasicBlock::iterator I = NewBB->begin();
  682. BasicBlock::const_iterator OldI = OldBB->begin();
  683. while ((PN = dyn_cast<PHINode>(I++))) {
  684. Value *NV = PoisonValue::get(PN->getType());
  685. PN->replaceAllUsesWith(NV);
  686. assert(VMap[&*OldI] == PN && "VMap mismatch");
  687. VMap[&*OldI] = NV;
  688. PN->eraseFromParent();
  689. ++OldI;
  690. }
  691. }
  692. }
  693. // Make a second pass over the PHINodes now that all of them have been
  694. // remapped into the new function, simplifying the PHINode and performing any
  695. // recursive simplifications exposed. This will transparently update the
  696. // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
  697. // two PHINodes, the iteration over the old PHIs remains valid, and the
  698. // mapping will just map us to the new node (which may not even be a PHI
  699. // node).
  700. const DataLayout &DL = NewFunc->getParent()->getDataLayout();
  701. SmallSetVector<const Value *, 8> Worklist;
  702. for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
  703. if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
  704. Worklist.insert(PHIToResolve[Idx]);
  705. // Note that we must test the size on each iteration, the worklist can grow.
  706. for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
  707. const Value *OrigV = Worklist[Idx];
  708. auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
  709. if (!I)
  710. continue;
  711. // Skip over non-intrinsic callsites, we don't want to remove any nodes from
  712. // the CGSCC.
  713. CallBase *CB = dyn_cast<CallBase>(I);
  714. if (CB && CB->getCalledFunction() &&
  715. !CB->getCalledFunction()->isIntrinsic())
  716. continue;
  717. // See if this instruction simplifies.
  718. Value *SimpleV = simplifyInstruction(I, DL);
  719. if (!SimpleV)
  720. continue;
  721. // Stash away all the uses of the old instruction so we can check them for
  722. // recursive simplifications after a RAUW. This is cheaper than checking all
  723. // uses of To on the recursive step in most cases.
  724. for (const User *U : OrigV->users())
  725. Worklist.insert(cast<Instruction>(U));
  726. // Replace the instruction with its simplified value.
  727. I->replaceAllUsesWith(SimpleV);
  728. // If the original instruction had no side effects, remove it.
  729. if (isInstructionTriviallyDead(I))
  730. I->eraseFromParent();
  731. else
  732. VMap[OrigV] = I;
  733. }
  734. // Remap debug intrinsic operands now that all values have been mapped.
  735. // Doing this now (late) preserves use-before-defs in debug intrinsics. If
  736. // we didn't do this, ValueAsMetadata(use-before-def) operands would be
  737. // replaced by empty metadata. This would signal later cleanup passes to
  738. // remove the debug intrinsics, potentially causing incorrect locations.
  739. for (const auto *DVI : DbgIntrinsics) {
  740. if (DbgVariableIntrinsic *NewDVI =
  741. cast_or_null<DbgVariableIntrinsic>(VMap.lookup(DVI)))
  742. RemapInstruction(NewDVI, VMap,
  743. ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
  744. TypeMapper, Materializer);
  745. }
  746. // Simplify conditional branches and switches with a constant operand. We try
  747. // to prune these out when cloning, but if the simplification required
  748. // looking through PHI nodes, those are only available after forming the full
  749. // basic block. That may leave some here, and we still want to prune the dead
  750. // code as early as possible.
  751. Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
  752. for (BasicBlock &BB : make_range(Begin, NewFunc->end()))
  753. ConstantFoldTerminator(&BB);
  754. // Some blocks may have become unreachable as a result. Find and delete them.
  755. {
  756. SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
  757. SmallVector<BasicBlock *, 16> Worklist;
  758. Worklist.push_back(&*Begin);
  759. while (!Worklist.empty()) {
  760. BasicBlock *BB = Worklist.pop_back_val();
  761. if (ReachableBlocks.insert(BB).second)
  762. append_range(Worklist, successors(BB));
  763. }
  764. SmallVector<BasicBlock *, 16> UnreachableBlocks;
  765. for (BasicBlock &BB : make_range(Begin, NewFunc->end()))
  766. if (!ReachableBlocks.contains(&BB))
  767. UnreachableBlocks.push_back(&BB);
  768. DeleteDeadBlocks(UnreachableBlocks);
  769. }
  770. // Now that the inlined function body has been fully constructed, go through
  771. // and zap unconditional fall-through branches. This happens all the time when
  772. // specializing code: code specialization turns conditional branches into
  773. // uncond branches, and this code folds them.
  774. Function::iterator I = Begin;
  775. while (I != NewFunc->end()) {
  776. BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
  777. if (!BI || BI->isConditional()) {
  778. ++I;
  779. continue;
  780. }
  781. BasicBlock *Dest = BI->getSuccessor(0);
  782. if (!Dest->getSinglePredecessor()) {
  783. ++I;
  784. continue;
  785. }
  786. // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
  787. // above should have zapped all of them..
  788. assert(!isa<PHINode>(Dest->begin()));
  789. // We know all single-entry PHI nodes in the inlined function have been
  790. // removed, so we just need to splice the blocks.
  791. BI->eraseFromParent();
  792. // Make all PHI nodes that referred to Dest now refer to I as their source.
  793. Dest->replaceAllUsesWith(&*I);
  794. // Move all the instructions in the succ to the pred.
  795. I->splice(I->end(), Dest);
  796. // Remove the dest block.
  797. Dest->eraseFromParent();
  798. // Do not increment I, iteratively merge all things this block branches to.
  799. }
  800. // Make a final pass over the basic blocks from the old function to gather
  801. // any return instructions which survived folding. We have to do this here
  802. // because we can iteratively remove and merge returns above.
  803. for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
  804. E = NewFunc->end();
  805. I != E; ++I)
  806. if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
  807. Returns.push_back(RI);
  808. }
  809. /// This works exactly like CloneFunctionInto,
  810. /// except that it does some simple constant prop and DCE on the fly. The
  811. /// effect of this is to copy significantly less code in cases where (for
  812. /// example) a function call with constant arguments is inlined, and those
  813. /// constant arguments cause a significant amount of code in the callee to be
  814. /// dead. Since this doesn't produce an exact copy of the input, it can't be
  815. /// used for things like CloneFunction or CloneModule.
  816. void llvm::CloneAndPruneFunctionInto(
  817. Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap,
  818. bool ModuleLevelChanges, SmallVectorImpl<ReturnInst *> &Returns,
  819. const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
  820. CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
  821. ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
  822. }
  823. /// Remaps instructions in \p Blocks using the mapping in \p VMap.
  824. void llvm::remapInstructionsInBlocks(
  825. const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
  826. // Rewrite the code to refer to itself.
  827. for (auto *BB : Blocks)
  828. for (auto &Inst : *BB)
  829. RemapInstruction(&Inst, VMap,
  830. RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
  831. }
  832. /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
  833. /// Blocks.
  834. ///
  835. /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
  836. /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
  837. Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
  838. Loop *OrigLoop, ValueToValueMapTy &VMap,
  839. const Twine &NameSuffix, LoopInfo *LI,
  840. DominatorTree *DT,
  841. SmallVectorImpl<BasicBlock *> &Blocks) {
  842. Function *F = OrigLoop->getHeader()->getParent();
  843. Loop *ParentLoop = OrigLoop->getParentLoop();
  844. DenseMap<Loop *, Loop *> LMap;
  845. Loop *NewLoop = LI->AllocateLoop();
  846. LMap[OrigLoop] = NewLoop;
  847. if (ParentLoop)
  848. ParentLoop->addChildLoop(NewLoop);
  849. else
  850. LI->addTopLevelLoop(NewLoop);
  851. BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
  852. assert(OrigPH && "No preheader");
  853. BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
  854. // To rename the loop PHIs.
  855. VMap[OrigPH] = NewPH;
  856. Blocks.push_back(NewPH);
  857. // Update LoopInfo.
  858. if (ParentLoop)
  859. ParentLoop->addBasicBlockToLoop(NewPH, *LI);
  860. // Update DominatorTree.
  861. DT->addNewBlock(NewPH, LoopDomBB);
  862. for (Loop *CurLoop : OrigLoop->getLoopsInPreorder()) {
  863. Loop *&NewLoop = LMap[CurLoop];
  864. if (!NewLoop) {
  865. NewLoop = LI->AllocateLoop();
  866. // Establish the parent/child relationship.
  867. Loop *OrigParent = CurLoop->getParentLoop();
  868. assert(OrigParent && "Could not find the original parent loop");
  869. Loop *NewParentLoop = LMap[OrigParent];
  870. assert(NewParentLoop && "Could not find the new parent loop");
  871. NewParentLoop->addChildLoop(NewLoop);
  872. }
  873. }
  874. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  875. Loop *CurLoop = LI->getLoopFor(BB);
  876. Loop *&NewLoop = LMap[CurLoop];
  877. assert(NewLoop && "Expecting new loop to be allocated");
  878. BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
  879. VMap[BB] = NewBB;
  880. // Update LoopInfo.
  881. NewLoop->addBasicBlockToLoop(NewBB, *LI);
  882. // Add DominatorTree node. After seeing all blocks, update to correct
  883. // IDom.
  884. DT->addNewBlock(NewBB, NewPH);
  885. Blocks.push_back(NewBB);
  886. }
  887. for (BasicBlock *BB : OrigLoop->getBlocks()) {
  888. // Update loop headers.
  889. Loop *CurLoop = LI->getLoopFor(BB);
  890. if (BB == CurLoop->getHeader())
  891. LMap[CurLoop]->moveToHeader(cast<BasicBlock>(VMap[BB]));
  892. // Update DominatorTree.
  893. BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
  894. DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
  895. cast<BasicBlock>(VMap[IDomBB]));
  896. }
  897. // Move them physically from the end of the block list.
  898. F->splice(Before->getIterator(), F, NewPH->getIterator());
  899. F->splice(Before->getIterator(), F, NewLoop->getHeader()->getIterator(),
  900. F->end());
  901. return NewLoop;
  902. }
  903. /// Duplicate non-Phi instructions from the beginning of block up to
  904. /// StopAt instruction into a split block between BB and its predecessor.
  905. BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
  906. BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt,
  907. ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU) {
  908. assert(count(successors(PredBB), BB) == 1 &&
  909. "There must be a single edge between PredBB and BB!");
  910. // We are going to have to map operands from the original BB block to the new
  911. // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
  912. // account for entry from PredBB.
  913. BasicBlock::iterator BI = BB->begin();
  914. for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
  915. ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
  916. BasicBlock *NewBB = SplitEdge(PredBB, BB);
  917. NewBB->setName(PredBB->getName() + ".split");
  918. Instruction *NewTerm = NewBB->getTerminator();
  919. // FIXME: SplitEdge does not yet take a DTU, so we include the split edge
  920. // in the update set here.
  921. DTU.applyUpdates({{DominatorTree::Delete, PredBB, BB},
  922. {DominatorTree::Insert, PredBB, NewBB},
  923. {DominatorTree::Insert, NewBB, BB}});
  924. // Clone the non-phi instructions of BB into NewBB, keeping track of the
  925. // mapping and using it to remap operands in the cloned instructions.
  926. // Stop once we see the terminator too. This covers the case where BB's
  927. // terminator gets replaced and StopAt == BB's terminator.
  928. for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
  929. Instruction *New = BI->clone();
  930. New->setName(BI->getName());
  931. New->insertBefore(NewTerm);
  932. ValueMapping[&*BI] = New;
  933. // Remap operands to patch up intra-block references.
  934. for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
  935. if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
  936. auto I = ValueMapping.find(Inst);
  937. if (I != ValueMapping.end())
  938. New->setOperand(i, I->second);
  939. }
  940. }
  941. return NewBB;
  942. }
  943. void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
  944. DenseMap<MDNode *, MDNode *> &ClonedScopes,
  945. StringRef Ext, LLVMContext &Context) {
  946. MDBuilder MDB(Context);
  947. for (auto *ScopeList : NoAliasDeclScopes) {
  948. for (const auto &MDOperand : ScopeList->operands()) {
  949. if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
  950. AliasScopeNode SNANode(MD);
  951. std::string Name;
  952. auto ScopeName = SNANode.getName();
  953. if (!ScopeName.empty())
  954. Name = (Twine(ScopeName) + ":" + Ext).str();
  955. else
  956. Name = std::string(Ext);
  957. MDNode *NewScope = MDB.createAnonymousAliasScope(
  958. const_cast<MDNode *>(SNANode.getDomain()), Name);
  959. ClonedScopes.insert(std::make_pair(MD, NewScope));
  960. }
  961. }
  962. }
  963. }
  964. void llvm::adaptNoAliasScopes(Instruction *I,
  965. const DenseMap<MDNode *, MDNode *> &ClonedScopes,
  966. LLVMContext &Context) {
  967. auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
  968. bool NeedsReplacement = false;
  969. SmallVector<Metadata *, 8> NewScopeList;
  970. for (const auto &MDOp : ScopeList->operands()) {
  971. if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
  972. if (auto *NewMD = ClonedScopes.lookup(MD)) {
  973. NewScopeList.push_back(NewMD);
  974. NeedsReplacement = true;
  975. continue;
  976. }
  977. NewScopeList.push_back(MD);
  978. }
  979. }
  980. if (NeedsReplacement)
  981. return MDNode::get(Context, NewScopeList);
  982. return nullptr;
  983. };
  984. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
  985. if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
  986. Decl->setScopeList(NewScopeList);
  987. auto replaceWhenNeeded = [&](unsigned MD_ID) {
  988. if (const MDNode *CSNoAlias = I->getMetadata(MD_ID))
  989. if (auto *NewScopeList = CloneScopeList(CSNoAlias))
  990. I->setMetadata(MD_ID, NewScopeList);
  991. };
  992. replaceWhenNeeded(LLVMContext::MD_noalias);
  993. replaceWhenNeeded(LLVMContext::MD_alias_scope);
  994. }
  995. void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
  996. ArrayRef<BasicBlock *> NewBlocks,
  997. LLVMContext &Context, StringRef Ext) {
  998. if (NoAliasDeclScopes.empty())
  999. return;
  1000. DenseMap<MDNode *, MDNode *> ClonedScopes;
  1001. LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
  1002. << NoAliasDeclScopes.size() << " node(s)\n");
  1003. cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
  1004. // Identify instructions using metadata that needs adaptation
  1005. for (BasicBlock *NewBlock : NewBlocks)
  1006. for (Instruction &I : *NewBlock)
  1007. adaptNoAliasScopes(&I, ClonedScopes, Context);
  1008. }
  1009. void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
  1010. Instruction *IStart, Instruction *IEnd,
  1011. LLVMContext &Context, StringRef Ext) {
  1012. if (NoAliasDeclScopes.empty())
  1013. return;
  1014. DenseMap<MDNode *, MDNode *> ClonedScopes;
  1015. LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
  1016. << NoAliasDeclScopes.size() << " node(s)\n");
  1017. cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
  1018. // Identify instructions using metadata that needs adaptation
  1019. assert(IStart->getParent() == IEnd->getParent() && "different basic block ?");
  1020. auto ItStart = IStart->getIterator();
  1021. auto ItEnd = IEnd->getIterator();
  1022. ++ItEnd; // IEnd is included, increment ItEnd to get the end of the range
  1023. for (auto &I : llvm::make_range(ItStart, ItEnd))
  1024. adaptNoAliasScopes(&I, ClonedScopes, Context);
  1025. }
  1026. void llvm::identifyNoAliasScopesToClone(
  1027. ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
  1028. for (BasicBlock *BB : BBs)
  1029. for (Instruction &I : *BB)
  1030. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
  1031. NoAliasDeclScopes.push_back(Decl->getScopeList());
  1032. }
  1033. void llvm::identifyNoAliasScopesToClone(
  1034. BasicBlock::iterator Start, BasicBlock::iterator End,
  1035. SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
  1036. for (Instruction &I : make_range(Start, End))
  1037. if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
  1038. NoAliasDeclScopes.push_back(Decl->getScopeList());
  1039. }