Cloning.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines various functions that are used to clone chunks of LLVM
  11. // code for various purposes. This varies from copying whole modules into new
  12. // modules, to cloning functions with different arguments, to inlining
  13. // functions, to copying basic blocks to support loop unrolling or superblock
  14. // formation, etc.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H
  18. #define LLVM_TRANSFORMS_UTILS_CLONING_H
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/Twine.h"
  21. #include "llvm/IR/ValueHandle.h"
  22. #include "llvm/IR/ValueMap.h"
  23. #include "llvm/Transforms/Utils/ValueMapper.h"
  24. namespace llvm {
  25. class Module;
  26. class Function;
  27. class Instruction;
  28. class Pass;
  29. class LPPassManager;
  30. class BasicBlock;
  31. class Value;
  32. class CallInst;
  33. class InvokeInst;
  34. class ReturnInst;
  35. class CallSite;
  36. class Trace;
  37. class CallGraph;
  38. class DataLayout;
  39. class Loop;
  40. class LoopInfo;
  41. class AllocaInst;
  42. class AliasAnalysis;
  43. class AssumptionCacheTracker;
  44. class DominatorTree;
  45. /// CloneModule - Return an exact copy of the specified module
  46. ///
  47. Module *CloneModule(const Module *M);
  48. Module *CloneModule(const Module *M, ValueToValueMapTy &VMap);
  49. /// ClonedCodeInfo - This struct can be used to capture information about code
  50. /// being cloned, while it is being cloned.
  51. struct ClonedCodeInfo {
  52. /// ContainsCalls - This is set to true if the cloned code contains a normal
  53. /// call instruction.
  54. bool ContainsCalls;
  55. /// ContainsDynamicAllocas - This is set to true if the cloned code contains
  56. /// a 'dynamic' alloca. Dynamic allocas are allocas that are either not in
  57. /// the entry block or they are in the entry block but are not a constant
  58. /// size.
  59. bool ContainsDynamicAllocas;
  60. ClonedCodeInfo() : ContainsCalls(false), ContainsDynamicAllocas(false) {}
  61. };
  62. /// CloneBasicBlock - Return a copy of the specified basic block, but without
  63. /// embedding the block into a particular function. The block returned is an
  64. /// exact copy of the specified basic block, without any remapping having been
  65. /// performed. Because of this, this is only suitable for applications where
  66. /// the basic block will be inserted into the same function that it was cloned
  67. /// from (loop unrolling would use this, for example).
  68. ///
  69. /// Also, note that this function makes a direct copy of the basic block, and
  70. /// can thus produce illegal LLVM code. In particular, it will copy any PHI
  71. /// nodes from the original block, even though there are no predecessors for the
  72. /// newly cloned block (thus, phi nodes will have to be updated). Also, this
  73. /// block will branch to the old successors of the original block: these
  74. /// successors will have to have any PHI nodes updated to account for the new
  75. /// incoming edges.
  76. ///
  77. /// The correlation between instructions in the source and result basic blocks
  78. /// is recorded in the VMap map.
  79. ///
  80. /// If you have a particular suffix you'd like to use to add to any cloned
  81. /// names, specify it as the optional third parameter.
  82. ///
  83. /// If you would like the basic block to be auto-inserted into the end of a
  84. /// function, you can specify it as the optional fourth parameter.
  85. ///
  86. /// If you would like to collect additional information about the cloned
  87. /// function, you can specify a ClonedCodeInfo object with the optional fifth
  88. /// parameter.
  89. ///
  90. BasicBlock *CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
  91. const Twine &NameSuffix = "", Function *F = nullptr,
  92. ClonedCodeInfo *CodeInfo = nullptr);
  93. /// CloneFunction - Return a copy of the specified function, but without
  94. /// embedding the function into another module. Also, any references specified
  95. /// in the VMap are changed to refer to their mapped value instead of the
  96. /// original one. If any of the arguments to the function are in the VMap,
  97. /// the arguments are deleted from the resultant function. The VMap is
  98. /// updated to include mappings from all of the instructions and basicblocks in
  99. /// the function from their old to new values. The final argument captures
  100. /// information about the cloned code if non-null.
  101. ///
  102. /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
  103. /// mappings, and debug info metadata will not be cloned.
  104. ///
  105. Function *CloneFunction(const Function *F, ValueToValueMapTy &VMap,
  106. bool ModuleLevelChanges,
  107. ClonedCodeInfo *CodeInfo = nullptr);
  108. /// Clone OldFunc into NewFunc, transforming the old arguments into references
  109. /// to VMap values. Note that if NewFunc already has basic blocks, the ones
  110. /// cloned into it will be added to the end of the function. This function
  111. /// fills in a list of return instructions, and can optionally remap types
  112. /// and/or append the specified suffix to all values cloned.
  113. ///
  114. /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
  115. /// mappings.
  116. ///
  117. void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
  118. ValueToValueMapTy &VMap, bool ModuleLevelChanges,
  119. SmallVectorImpl<ReturnInst*> &Returns,
  120. const char *NameSuffix = "",
  121. ClonedCodeInfo *CodeInfo = nullptr,
  122. ValueMapTypeRemapper *TypeMapper = nullptr,
  123. ValueMaterializer *Materializer = nullptr);
  124. /// A helper class used with CloneAndPruneIntoFromInst to change the default
  125. /// behavior while instructions are being cloned.
  126. class CloningDirector {
  127. public:
  128. /// This enumeration describes the way CloneAndPruneIntoFromInst should
  129. /// proceed after the CloningDirector has examined an instruction.
  130. enum CloningAction {
  131. ///< Continue cloning the instruction (default behavior).
  132. CloneInstruction,
  133. ///< Skip this instruction but continue cloning the current basic block.
  134. SkipInstruction,
  135. ///< Skip this instruction and stop cloning the current basic block.
  136. StopCloningBB,
  137. ///< Don't clone the terminator but clone the current block's successors.
  138. CloneSuccessors
  139. };
  140. virtual ~CloningDirector() {}
  141. /// Subclasses must override this function to customize cloning behavior.
  142. virtual CloningAction handleInstruction(ValueToValueMapTy &VMap,
  143. const Instruction *Inst,
  144. BasicBlock *NewBB) = 0;
  145. virtual ValueMapTypeRemapper *getTypeRemapper() { return nullptr; }
  146. virtual ValueMaterializer *getValueMaterializer() { return nullptr; }
  147. };
  148. void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
  149. const Instruction *StartingInst,
  150. ValueToValueMapTy &VMap, bool ModuleLevelChanges,
  151. SmallVectorImpl<ReturnInst*> &Returns,
  152. const char *NameSuffix = "",
  153. ClonedCodeInfo *CodeInfo = nullptr,
  154. CloningDirector *Director = nullptr);
  155. /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
  156. /// except that it does some simple constant prop and DCE on the fly. The
  157. /// effect of this is to copy significantly less code in cases where (for
  158. /// example) a function call with constant arguments is inlined, and those
  159. /// constant arguments cause a significant amount of code in the callee to be
  160. /// dead. Since this doesn't produce an exactly copy of the input, it can't be
  161. /// used for things like CloneFunction or CloneModule.
  162. ///
  163. /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
  164. /// mappings.
  165. ///
  166. void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
  167. ValueToValueMapTy &VMap, bool ModuleLevelChanges,
  168. SmallVectorImpl<ReturnInst*> &Returns,
  169. const char *NameSuffix = "",
  170. ClonedCodeInfo *CodeInfo = nullptr,
  171. Instruction *TheCall = nullptr);
  172. /// InlineFunctionInfo - This class captures the data input to the
  173. /// InlineFunction call, and records the auxiliary results produced by it.
  174. class InlineFunctionInfo {
  175. public:
  176. explicit InlineFunctionInfo(CallGraph *cg = nullptr,
  177. AliasAnalysis *AA = nullptr,
  178. AssumptionCacheTracker *ACT = nullptr)
  179. : CG(cg), AA(AA), ACT(ACT) {}
  180. /// CG - If non-null, InlineFunction will update the callgraph to reflect the
  181. /// changes it makes.
  182. CallGraph *CG;
  183. AliasAnalysis *AA;
  184. AssumptionCacheTracker *ACT;
  185. /// StaticAllocas - InlineFunction fills this in with all static allocas that
  186. /// get copied into the caller.
  187. SmallVector<AllocaInst *, 4> StaticAllocas;
  188. /// InlinedCalls - InlineFunction fills this in with callsites that were
  189. /// inlined from the callee. This is only filled in if CG is non-null.
  190. SmallVector<WeakVH, 8> InlinedCalls;
  191. void reset() {
  192. StaticAllocas.clear();
  193. InlinedCalls.clear();
  194. }
  195. };
  196. /// InlineFunction - This function inlines the called function into the basic
  197. /// block of the caller. This returns false if it is not possible to inline
  198. /// this call. The program is still in a well defined state if this occurs
  199. /// though.
  200. ///
  201. /// Note that this only does one level of inlining. For example, if the
  202. /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
  203. /// exists in the instruction stream. Similarly this will inline a recursive
  204. /// function by one level.
  205. ///
  206. bool InlineFunction(CallInst *C, InlineFunctionInfo &IFI,
  207. bool InsertLifetime = true);
  208. bool InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI,
  209. bool InsertLifetime = true);
  210. bool InlineFunction(CallSite CS, InlineFunctionInfo &IFI,
  211. bool InsertLifetime = true);
  212. /// \brief Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
  213. /// Blocks.
  214. ///
  215. /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
  216. /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
  217. Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
  218. Loop *OrigLoop, ValueToValueMapTy &VMap,
  219. const Twine &NameSuffix, LoopInfo *LI,
  220. DominatorTree *DT,
  221. SmallVectorImpl<BasicBlock *> &Blocks);
  222. /// \brief Remaps instructions in \p Blocks using the mapping in \p VMap.
  223. void remapInstructionsInBlocks(const SmallVectorImpl<BasicBlock *> &Blocks,
  224. ValueToValueMapTy &VMap);
  225. } // End llvm namespace
  226. #endif