Cloning.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines various functions that are used to clone chunks of LLVM
  15. // code for various purposes. This varies from copying whole modules into new
  16. // modules, to cloning functions with different arguments, to inlining
  17. // functions, to copying basic blocks to support loop unrolling or superblock
  18. // formation, etc.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H
  22. #define LLVM_TRANSFORMS_UTILS_CLONING_H
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/Twine.h"
  25. #include "llvm/Analysis/AssumptionCache.h"
  26. #include "llvm/Analysis/InlineCost.h"
  27. #include "llvm/IR/ValueHandle.h"
  28. #include "llvm/Transforms/Utils/ValueMapper.h"
  29. #include <functional>
  30. #include <memory>
  31. #include <vector>
  32. namespace llvm {
  33. class AAResults;
  34. class AllocaInst;
  35. class BasicBlock;
  36. class BlockFrequencyInfo;
  37. class CallGraph;
  38. class DebugInfoFinder;
  39. class DominatorTree;
  40. class Function;
  41. class Instruction;
  42. class Loop;
  43. class LoopInfo;
  44. class Module;
  45. class ProfileSummaryInfo;
  46. class ReturnInst;
  47. class DomTreeUpdater;
  48. /// Return an exact copy of the specified module
  49. std::unique_ptr<Module> CloneModule(const Module &M);
  50. std::unique_ptr<Module> CloneModule(const Module &M, ValueToValueMapTy &VMap);
  51. /// Return a copy of the specified module. The ShouldCloneDefinition function
  52. /// controls whether a specific GlobalValue's definition is cloned. If the
  53. /// function returns false, the module copy will contain an external reference
  54. /// in place of the global definition.
  55. std::unique_ptr<Module>
  56. CloneModule(const Module &M, ValueToValueMapTy &VMap,
  57. function_ref<bool(const GlobalValue *)> ShouldCloneDefinition);
  58. /// This struct can be used to capture information about code
  59. /// being cloned, while it is being cloned.
  60. struct ClonedCodeInfo {
  61. /// This is set to true if the cloned code contains a normal call instruction.
  62. bool ContainsCalls = false;
  63. /// This is set to true if there is memprof related metadata (memprof or
  64. /// callsite metadata) in the cloned code.
  65. bool ContainsMemProfMetadata = false;
  66. /// This is set to true if the cloned code contains a 'dynamic' alloca.
  67. /// Dynamic allocas are allocas that are either not in the entry block or they
  68. /// are in the entry block but are not a constant size.
  69. bool ContainsDynamicAllocas = false;
  70. /// All cloned call sites that have operand bundles attached are appended to
  71. /// this vector. This vector may contain nulls or undefs if some of the
  72. /// originally inserted callsites were DCE'ed after they were cloned.
  73. std::vector<WeakTrackingVH> OperandBundleCallSites;
  74. /// Like VMap, but maps only unsimplified instructions. Values in the map
  75. /// may be dangling, it is only intended to be used via isSimplified(), to
  76. /// check whether the main VMap mapping involves simplification or not.
  77. DenseMap<const Value *, const Value *> OrigVMap;
  78. ClonedCodeInfo() = default;
  79. bool isSimplified(const Value *From, const Value *To) const {
  80. return OrigVMap.lookup(From) != To;
  81. }
  82. };
  83. /// Return a copy of the specified basic block, but without
  84. /// embedding the block into a particular function. The block returned is an
  85. /// exact copy of the specified basic block, without any remapping having been
  86. /// performed. Because of this, this is only suitable for applications where
  87. /// the basic block will be inserted into the same function that it was cloned
  88. /// from (loop unrolling would use this, for example).
  89. ///
  90. /// Also, note that this function makes a direct copy of the basic block, and
  91. /// can thus produce illegal LLVM code. In particular, it will copy any PHI
  92. /// nodes from the original block, even though there are no predecessors for the
  93. /// newly cloned block (thus, phi nodes will have to be updated). Also, this
  94. /// block will branch to the old successors of the original block: these
  95. /// successors will have to have any PHI nodes updated to account for the new
  96. /// incoming edges.
  97. ///
  98. /// The correlation between instructions in the source and result basic blocks
  99. /// is recorded in the VMap map.
  100. ///
  101. /// If you have a particular suffix you'd like to use to add to any cloned
  102. /// names, specify it as the optional third parameter.
  103. ///
  104. /// If you would like the basic block to be auto-inserted into the end of a
  105. /// function, you can specify it as the optional fourth parameter.
  106. ///
  107. /// If you would like to collect additional information about the cloned
  108. /// function, you can specify a ClonedCodeInfo object with the optional fifth
  109. /// parameter.
  110. BasicBlock *CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
  111. const Twine &NameSuffix = "", Function *F = nullptr,
  112. ClonedCodeInfo *CodeInfo = nullptr,
  113. DebugInfoFinder *DIFinder = nullptr);
  114. /// Return a copy of the specified function and add it to that
  115. /// function's module. Also, any references specified in the VMap are changed
  116. /// to refer to their mapped value instead of the original one. If any of the
  117. /// arguments to the function are in the VMap, the arguments are deleted from
  118. /// the resultant function. The VMap is updated to include mappings from all of
  119. /// the instructions and basicblocks in the function from their old to new
  120. /// values. The final argument captures information about the cloned code if
  121. /// non-null.
  122. ///
  123. /// \pre VMap contains no non-identity GlobalValue mappings.
  124. ///
  125. Function *CloneFunction(Function *F, ValueToValueMapTy &VMap,
  126. ClonedCodeInfo *CodeInfo = nullptr);
  127. enum class CloneFunctionChangeType {
  128. LocalChangesOnly,
  129. GlobalChanges,
  130. DifferentModule,
  131. ClonedModule,
  132. };
  133. /// Clone OldFunc into NewFunc, transforming the old arguments into references
  134. /// to VMap values. Note that if NewFunc already has basic blocks, the ones
  135. /// cloned into it will be added to the end of the function. This function
  136. /// fills in a list of return instructions, and can optionally remap types
  137. /// and/or append the specified suffix to all values cloned.
  138. ///
  139. /// If \p Changes is \a CloneFunctionChangeType::LocalChangesOnly, VMap is
  140. /// required to contain no non-identity GlobalValue mappings. Otherwise,
  141. /// referenced metadata will be cloned.
  142. ///
  143. /// If \p Changes is less than \a CloneFunctionChangeType::DifferentModule
  144. /// indicating cloning into the same module (even if it's LocalChangesOnly), if
  145. /// debug info metadata transitively references a \a DISubprogram, it will be
  146. /// cloned, effectively upgrading \p Changes to GlobalChanges while suppressing
  147. /// cloning of types and compile units.
  148. ///
  149. /// If \p Changes is \a CloneFunctionChangeType::DifferentModule, the new
  150. /// module's \c !llvm.dbg.cu will get updated with any newly created compile
  151. /// units. (\a CloneFunctionChangeType::ClonedModule leaves that work for the
  152. /// caller.)
  153. ///
  154. /// FIXME: Consider simplifying this function by splitting out \a
  155. /// CloneFunctionMetadataInto() and expecting / updating callers to call it
  156. /// first when / how it's needed.
  157. void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  158. ValueToValueMapTy &VMap, CloneFunctionChangeType Changes,
  159. SmallVectorImpl<ReturnInst *> &Returns,
  160. const char *NameSuffix = "",
  161. ClonedCodeInfo *CodeInfo = nullptr,
  162. ValueMapTypeRemapper *TypeMapper = nullptr,
  163. ValueMaterializer *Materializer = nullptr);
  164. void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
  165. const Instruction *StartingInst,
  166. ValueToValueMapTy &VMap, bool ModuleLevelChanges,
  167. SmallVectorImpl<ReturnInst *> &Returns,
  168. const char *NameSuffix = "",
  169. ClonedCodeInfo *CodeInfo = nullptr);
  170. /// This works exactly like CloneFunctionInto,
  171. /// except that it does some simple constant prop and DCE on the fly. The
  172. /// effect of this is to copy significantly less code in cases where (for
  173. /// example) a function call with constant arguments is inlined, and those
  174. /// constant arguments cause a significant amount of code in the callee to be
  175. /// dead. Since this doesn't produce an exactly copy of the input, it can't be
  176. /// used for things like CloneFunction or CloneModule.
  177. ///
  178. /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
  179. /// mappings.
  180. ///
  181. void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
  182. ValueToValueMapTy &VMap, bool ModuleLevelChanges,
  183. SmallVectorImpl<ReturnInst*> &Returns,
  184. const char *NameSuffix = "",
  185. ClonedCodeInfo *CodeInfo = nullptr);
  186. /// This class captures the data input to the InlineFunction call, and records
  187. /// the auxiliary results produced by it.
  188. class InlineFunctionInfo {
  189. public:
  190. explicit InlineFunctionInfo(
  191. CallGraph *cg = nullptr,
  192. function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr,
  193. ProfileSummaryInfo *PSI = nullptr,
  194. BlockFrequencyInfo *CallerBFI = nullptr,
  195. BlockFrequencyInfo *CalleeBFI = nullptr, bool UpdateProfile = true)
  196. : CG(cg), GetAssumptionCache(GetAssumptionCache), PSI(PSI),
  197. CallerBFI(CallerBFI), CalleeBFI(CalleeBFI),
  198. UpdateProfile(UpdateProfile) {}
  199. /// If non-null, InlineFunction will update the callgraph to reflect the
  200. /// changes it makes.
  201. CallGraph *CG;
  202. function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
  203. ProfileSummaryInfo *PSI;
  204. BlockFrequencyInfo *CallerBFI, *CalleeBFI;
  205. /// InlineFunction fills this in with all static allocas that get copied into
  206. /// the caller.
  207. SmallVector<AllocaInst *, 4> StaticAllocas;
  208. /// InlineFunction fills this in with callsites that were inlined from the
  209. /// callee. This is only filled in if CG is non-null.
  210. SmallVector<WeakTrackingVH, 8> InlinedCalls;
  211. /// All of the new call sites inlined into the caller.
  212. ///
  213. /// 'InlineFunction' fills this in by scanning the inlined instructions, and
  214. /// only if CG is null. If CG is non-null, instead the value handle
  215. /// `InlinedCalls` above is used.
  216. SmallVector<CallBase *, 8> InlinedCallSites;
  217. /// Update profile for callee as well as cloned version. We need to do this
  218. /// for regular inlining, but not for inlining from sample profile loader.
  219. bool UpdateProfile;
  220. void reset() {
  221. StaticAllocas.clear();
  222. InlinedCalls.clear();
  223. InlinedCallSites.clear();
  224. }
  225. };
  226. /// This function inlines the called function into the basic
  227. /// block of the caller. This returns false if it is not possible to inline
  228. /// this call. The program is still in a well defined state if this occurs
  229. /// though.
  230. ///
  231. /// Note that this only does one level of inlining. For example, if the
  232. /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
  233. /// exists in the instruction stream. Similarly this will inline a recursive
  234. /// function by one level.
  235. ///
  236. /// Note that while this routine is allowed to cleanup and optimize the
  237. /// *inlined* code to minimize the actual inserted code, it must not delete
  238. /// code in the caller as users of this routine may have pointers to
  239. /// instructions in the caller that need to remain stable.
  240. ///
  241. /// If ForwardVarArgsTo is passed, inlining a function with varargs is allowed
  242. /// and all varargs at the callsite will be passed to any calls to
  243. /// ForwardVarArgsTo. The caller of InlineFunction has to make sure any varargs
  244. /// are only used by ForwardVarArgsTo.
  245. ///
  246. /// The callee's function attributes are merged into the callers' if
  247. /// MergeAttributes is set to true.
  248. InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
  249. bool MergeAttributes = false,
  250. AAResults *CalleeAAR = nullptr,
  251. bool InsertLifetime = true,
  252. Function *ForwardVarArgsTo = nullptr);
  253. /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
  254. /// Blocks.
  255. ///
  256. /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
  257. /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
  258. /// Note: Only innermost loops are supported.
  259. Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
  260. Loop *OrigLoop, ValueToValueMapTy &VMap,
  261. const Twine &NameSuffix, LoopInfo *LI,
  262. DominatorTree *DT,
  263. SmallVectorImpl<BasicBlock *> &Blocks);
  264. /// Remaps instructions in \p Blocks using the mapping in \p VMap.
  265. void remapInstructionsInBlocks(const SmallVectorImpl<BasicBlock *> &Blocks,
  266. ValueToValueMapTy &VMap);
  267. /// Split edge between BB and PredBB and duplicate all non-Phi instructions
  268. /// from BB between its beginning and the StopAt instruction into the split
  269. /// block. Phi nodes are not duplicated, but their uses are handled correctly:
  270. /// we replace them with the uses of corresponding Phi inputs. ValueMapping
  271. /// is used to map the original instructions from BB to their newly-created
  272. /// copies. Returns the split block.
  273. BasicBlock *DuplicateInstructionsInSplitBetween(BasicBlock *BB,
  274. BasicBlock *PredBB,
  275. Instruction *StopAt,
  276. ValueToValueMapTy &ValueMapping,
  277. DomTreeUpdater &DTU);
  278. /// Updates profile information by adjusting the entry count by adding
  279. /// EntryDelta then scaling callsite information by the new count divided by the
  280. /// old count. VMap is used during inlinng to also update the new clone
  281. void updateProfileCallee(
  282. Function *Callee, int64_t EntryDelta,
  283. const ValueMap<const Value *, WeakTrackingVH> *VMap = nullptr);
  284. /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified
  285. /// basic blocks and extract their scope. These are candidates for duplication
  286. /// when cloning.
  287. void identifyNoAliasScopesToClone(
  288. ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
  289. /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified
  290. /// instruction range and extract their scope. These are candidates for
  291. /// duplication when cloning.
  292. void identifyNoAliasScopesToClone(
  293. BasicBlock::iterator Start, BasicBlock::iterator End,
  294. SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
  295. /// Duplicate the specified list of noalias decl scopes.
  296. /// The 'Ext' string is added as an extension to the name.
  297. /// Afterwards, the ClonedScopes contains the mapping of the original scope
  298. /// MDNode onto the cloned scope.
  299. /// Be aware that the cloned scopes are still part of the original scope domain.
  300. void cloneNoAliasScopes(
  301. ArrayRef<MDNode *> NoAliasDeclScopes,
  302. DenseMap<MDNode *, MDNode *> &ClonedScopes,
  303. StringRef Ext, LLVMContext &Context);
  304. /// Adapt the metadata for the specified instruction according to the
  305. /// provided mapping. This is normally used after cloning an instruction, when
  306. /// some noalias scopes needed to be cloned.
  307. void adaptNoAliasScopes(
  308. llvm::Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
  309. LLVMContext &Context);
  310. /// Clone the specified noalias decl scopes. Then adapt all instructions in the
  311. /// NewBlocks basicblocks to the cloned versions.
  312. /// 'Ext' will be added to the duplicate scope names.
  313. void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
  314. ArrayRef<BasicBlock *> NewBlocks,
  315. LLVMContext &Context, StringRef Ext);
  316. /// Clone the specified noalias decl scopes. Then adapt all instructions in the
  317. /// [IStart, IEnd] (IEnd included !) range to the cloned versions. 'Ext' will be
  318. /// added to the duplicate scope names.
  319. void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
  320. Instruction *IStart, Instruction *IEnd,
  321. LLVMContext &Context, StringRef Ext);
  322. } // end namespace llvm
  323. #endif // LLVM_TRANSFORMS_UTILS_CLONING_H
  324. #ifdef __GNUC__
  325. #pragma GCC diagnostic pop
  326. #endif