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 18 #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H 19 #define LLVM_TRANSFORMS_UTILS_CLONING_H 20 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/Analysis/AliasAnalysis.h" 24 #include "llvm/Analysis/AssumptionCache.h" 25 #include "llvm/Analysis/InlineCost.h" 26 #include "llvm/IR/CallSite.h" 27 #include "llvm/IR/ValueHandle.h" 28 #include "llvm/Transforms/Utils/ValueMapper.h" 29 #include <functional> 30 #include <memory> 31 #include <vector> 32 33 namespace llvm { 34 35 class AllocaInst; 36 class BasicBlock; 37 class BlockFrequencyInfo; 38 class CallInst; 39 class CallGraph; 40 class DebugInfoFinder; 41 class DominatorTree; 42 class Function; 43 class Instruction; 44 class InvokeInst; 45 class Loop; 46 class LoopInfo; 47 class Module; 48 class ProfileSummaryInfo; 49 class ReturnInst; 50 class DomTreeUpdater; 51 52 /// Return an exact copy of the specified module 53 std::unique_ptr<Module> CloneModule(const Module &M); 54 std::unique_ptr<Module> CloneModule(const Module &M, ValueToValueMapTy &VMap); 55 56 /// Return a copy of the specified module. The ShouldCloneDefinition function 57 /// controls whether a specific GlobalValue's definition is cloned. If the 58 /// function returns false, the module copy will contain an external reference 59 /// in place of the global definition. 60 std::unique_ptr<Module> 61 CloneModule(const Module &M, ValueToValueMapTy &VMap, 62 function_ref<bool(const GlobalValue *)> ShouldCloneDefinition); 63 64 /// This struct can be used to capture information about code 65 /// being cloned, while it is being cloned. 66 struct ClonedCodeInfo { 67 /// This is set to true if the cloned code contains a normal call instruction. 68 bool ContainsCalls = false; 69 70 /// This is set to true if the cloned code contains a 'dynamic' alloca. 71 /// Dynamic allocas are allocas that are either not in the entry block or they 72 /// are in the entry block but are not a constant size. 73 bool ContainsDynamicAllocas = false; 74 75 /// All cloned call sites that have operand bundles attached are appended to 76 /// this vector. This vector may contain nulls or undefs if some of the 77 /// originally inserted callsites were DCE'ed after they were cloned. 78 std::vector<WeakTrackingVH> OperandBundleCallSites; 79 80 ClonedCodeInfo() = default; 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 115 /// Return a copy of the specified function and add it to that 116 /// function's module. Also, any references specified in the VMap are changed 117 /// to refer to their mapped value instead of the original one. If any of the 118 /// arguments to the function are in the VMap, the arguments are deleted from 119 /// the resultant function. The VMap is updated to include mappings from all of 120 /// the instructions and basicblocks in the function from their old to new 121 /// values. The final argument captures information about the cloned code if 122 /// non-null. 123 /// 124 /// VMap contains no non-identity GlobalValue mappings and debug info metadata 125 /// will not be cloned. 126 /// 127 Function *CloneFunction(Function *F, ValueToValueMapTy &VMap, 128 ClonedCodeInfo *CodeInfo = nullptr); 129 130 /// Clone OldFunc into NewFunc, transforming the old arguments into references 131 /// to VMap values. Note that if NewFunc already has basic blocks, the ones 132 /// cloned into it will be added to the end of the function. This function 133 /// fills in a list of return instructions, and can optionally remap types 134 /// and/or append the specified suffix to all values cloned. 135 /// 136 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 137 /// mappings. 138 /// 139 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 140 ValueToValueMapTy &VMap, bool ModuleLevelChanges, 141 SmallVectorImpl<ReturnInst*> &Returns, 142 const char *NameSuffix = "", 143 ClonedCodeInfo *CodeInfo = nullptr, 144 ValueMapTypeRemapper *TypeMapper = nullptr, 145 ValueMaterializer *Materializer = nullptr); 146 147 void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, 148 const Instruction *StartingInst, 149 ValueToValueMapTy &VMap, bool ModuleLevelChanges, 150 SmallVectorImpl<ReturnInst *> &Returns, 151 const char *NameSuffix = "", 152 ClonedCodeInfo *CodeInfo = nullptr); 153 154 /// This works exactly like CloneFunctionInto, 155 /// except that it does some simple constant prop and DCE on the fly. The 156 /// effect of this is to copy significantly less code in cases where (for 157 /// example) a function call with constant arguments is inlined, and those 158 /// constant arguments cause a significant amount of code in the callee to be 159 /// dead. Since this doesn't produce an exactly copy of the input, it can't be 160 /// used for things like CloneFunction or CloneModule. 161 /// 162 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 163 /// mappings. 164 /// 165 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 166 ValueToValueMapTy &VMap, bool ModuleLevelChanges, 167 SmallVectorImpl<ReturnInst*> &Returns, 168 const char *NameSuffix = "", 169 ClonedCodeInfo *CodeInfo = nullptr, 170 Instruction *TheCall = nullptr); 171 172 /// This class captures the data input to the InlineFunction call, and records 173 /// the auxiliary results produced by it. 174 class InlineFunctionInfo { 175 public: 176 explicit InlineFunctionInfo(CallGraph *cg = nullptr, 177 std::function<AssumptionCache &(Function &)> 178 *GetAssumptionCache = nullptr, 179 ProfileSummaryInfo *PSI = nullptr, 180 BlockFrequencyInfo *CallerBFI = nullptr, 181 BlockFrequencyInfo *CalleeBFI = nullptr) CG(cg)182 : CG(cg), GetAssumptionCache(GetAssumptionCache), PSI(PSI), 183 CallerBFI(CallerBFI), CalleeBFI(CalleeBFI) {} 184 185 /// If non-null, InlineFunction will update the callgraph to reflect the 186 /// changes it makes. 187 CallGraph *CG; 188 std::function<AssumptionCache &(Function &)> *GetAssumptionCache; 189 ProfileSummaryInfo *PSI; 190 BlockFrequencyInfo *CallerBFI, *CalleeBFI; 191 192 /// InlineFunction fills this in with all static allocas that get copied into 193 /// the caller. 194 SmallVector<AllocaInst *, 4> StaticAllocas; 195 196 /// InlineFunction fills this in with callsites that were inlined from the 197 /// callee. This is only filled in if CG is non-null. 198 SmallVector<WeakTrackingVH, 8> InlinedCalls; 199 200 /// All of the new call sites inlined into the caller. 201 /// 202 /// 'InlineFunction' fills this in by scanning the inlined instructions, and 203 /// only if CG is null. If CG is non-null, instead the value handle 204 /// `InlinedCalls` above is used. 205 SmallVector<CallSite, 8> InlinedCallSites; 206 reset()207 void reset() { 208 StaticAllocas.clear(); 209 InlinedCalls.clear(); 210 InlinedCallSites.clear(); 211 } 212 }; 213 214 /// This function inlines the called function into the basic 215 /// block of the caller. This returns false if it is not possible to inline 216 /// this call. The program is still in a well defined state if this occurs 217 /// though. 218 /// 219 /// Note that this only does one level of inlining. For example, if the 220 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 221 /// exists in the instruction stream. Similarly this will inline a recursive 222 /// function by one level. 223 /// 224 /// Note that while this routine is allowed to cleanup and optimize the 225 /// *inlined* code to minimize the actual inserted code, it must not delete 226 /// code in the caller as users of this routine may have pointers to 227 /// instructions in the caller that need to remain stable. 228 /// 229 /// If ForwardVarArgsTo is passed, inlining a function with varargs is allowed 230 /// and all varargs at the callsite will be passed to any calls to 231 /// ForwardVarArgsTo. The caller of InlineFunction has to make sure any varargs 232 /// are only used by ForwardVarArgsTo. 233 InlineResult InlineFunction(CallInst *C, InlineFunctionInfo &IFI, 234 AAResults *CalleeAAR = nullptr, 235 bool InsertLifetime = true); 236 InlineResult InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI, 237 AAResults *CalleeAAR = nullptr, 238 bool InsertLifetime = true); 239 InlineResult InlineFunction(CallSite CS, InlineFunctionInfo &IFI, 240 AAResults *CalleeAAR = nullptr, 241 bool InsertLifetime = true, 242 Function *ForwardVarArgsTo = nullptr); 243 244 /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p 245 /// Blocks. 246 /// 247 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block 248 /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. 249 /// Note: Only innermost loops are supported. 250 Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, 251 Loop *OrigLoop, ValueToValueMapTy &VMap, 252 const Twine &NameSuffix, LoopInfo *LI, 253 DominatorTree *DT, 254 SmallVectorImpl<BasicBlock *> &Blocks); 255 256 /// Remaps instructions in \p Blocks using the mapping in \p VMap. 257 void remapInstructionsInBlocks(const SmallVectorImpl<BasicBlock *> &Blocks, 258 ValueToValueMapTy &VMap); 259 260 /// Split edge between BB and PredBB and duplicate all non-Phi instructions 261 /// from BB between its beginning and the StopAt instruction into the split 262 /// block. Phi nodes are not duplicated, but their uses are handled correctly: 263 /// we replace them with the uses of corresponding Phi inputs. ValueMapping 264 /// is used to map the original instructions from BB to their newly-created 265 /// copies. Returns the split block. 266 BasicBlock *DuplicateInstructionsInSplitBetween(BasicBlock *BB, 267 BasicBlock *PredBB, 268 Instruction *StopAt, 269 ValueToValueMapTy &ValueMapping, 270 DomTreeUpdater &DTU); 271 272 } // end namespace llvm 273 274 #endif // LLVM_TRANSFORMS_UTILS_CLONING_H 275