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