|
|
|||
File indexing completed on 2026-05-10 08:44:42
0001 //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file defines various functions that are used to clone chunks of LLVM 0010 // code for various purposes. This varies from copying whole modules into new 0011 // modules, to cloning functions with different arguments, to inlining 0012 // functions, to copying basic blocks to support loop unrolling or superblock 0013 // formation, etc. 0014 // 0015 //===----------------------------------------------------------------------===// 0016 0017 #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H 0018 #define LLVM_TRANSFORMS_UTILS_CLONING_H 0019 0020 #include "llvm/ADT/SmallVector.h" 0021 #include "llvm/ADT/Twine.h" 0022 #include "llvm/Analysis/AssumptionCache.h" 0023 #include "llvm/Analysis/InlineCost.h" 0024 #include "llvm/IR/BasicBlock.h" 0025 #include "llvm/IR/ValueHandle.h" 0026 #include "llvm/Transforms/Utils/ValueMapper.h" 0027 #include <functional> 0028 #include <memory> 0029 #include <vector> 0030 0031 namespace llvm { 0032 0033 class AAResults; 0034 class AllocaInst; 0035 class BasicBlock; 0036 class BlockFrequencyInfo; 0037 class DebugInfoFinder; 0038 class DominatorTree; 0039 class Function; 0040 class Instruction; 0041 class Loop; 0042 class LoopInfo; 0043 class Module; 0044 class PGOContextualProfile; 0045 class ProfileSummaryInfo; 0046 class ReturnInst; 0047 class DomTreeUpdater; 0048 0049 /// Return an exact copy of the specified module 0050 std::unique_ptr<Module> CloneModule(const Module &M); 0051 std::unique_ptr<Module> CloneModule(const Module &M, ValueToValueMapTy &VMap); 0052 0053 /// Return a copy of the specified module. The ShouldCloneDefinition function 0054 /// controls whether a specific GlobalValue's definition is cloned. If the 0055 /// function returns false, the module copy will contain an external reference 0056 /// in place of the global definition. 0057 std::unique_ptr<Module> 0058 CloneModule(const Module &M, ValueToValueMapTy &VMap, 0059 function_ref<bool(const GlobalValue *)> ShouldCloneDefinition); 0060 0061 /// This struct can be used to capture information about code 0062 /// being cloned, while it is being cloned. 0063 struct ClonedCodeInfo { 0064 /// This is set to true if the cloned code contains a normal call instruction. 0065 bool ContainsCalls = false; 0066 0067 /// This is set to true if there is memprof related metadata (memprof or 0068 /// callsite metadata) in the cloned code. 0069 bool ContainsMemProfMetadata = false; 0070 0071 /// This is set to true if the cloned code contains a 'dynamic' alloca. 0072 /// Dynamic allocas are allocas that are either not in the entry block or they 0073 /// are in the entry block but are not a constant size. 0074 bool ContainsDynamicAllocas = false; 0075 0076 /// All cloned call sites that have operand bundles attached are appended to 0077 /// this vector. This vector may contain nulls or undefs if some of the 0078 /// originally inserted callsites were DCE'ed after they were cloned. 0079 std::vector<WeakTrackingVH> OperandBundleCallSites; 0080 0081 /// Like VMap, but maps only unsimplified instructions. Values in the map 0082 /// may be dangling, it is only intended to be used via isSimplified(), to 0083 /// check whether the main VMap mapping involves simplification or not. 0084 DenseMap<const Value *, const Value *> OrigVMap; 0085 0086 ClonedCodeInfo() = default; 0087 0088 bool isSimplified(const Value *From, const Value *To) const { 0089 return OrigVMap.lookup(From) != To; 0090 } 0091 }; 0092 0093 /// Return a copy of the specified basic block, but without 0094 /// embedding the block into a particular function. The block returned is an 0095 /// exact copy of the specified basic block, without any remapping having been 0096 /// performed. Because of this, this is only suitable for applications where 0097 /// the basic block will be inserted into the same function that it was cloned 0098 /// from (loop unrolling would use this, for example). 0099 /// 0100 /// Also, note that this function makes a direct copy of the basic block, and 0101 /// can thus produce illegal LLVM code. In particular, it will copy any PHI 0102 /// nodes from the original block, even though there are no predecessors for the 0103 /// newly cloned block (thus, phi nodes will have to be updated). Also, this 0104 /// block will branch to the old successors of the original block: these 0105 /// successors will have to have any PHI nodes updated to account for the new 0106 /// incoming edges. 0107 /// 0108 /// The correlation between instructions in the source and result basic blocks 0109 /// is recorded in the VMap map. 0110 /// 0111 /// If you have a particular suffix you'd like to use to add to any cloned 0112 /// names, specify it as the optional third parameter. 0113 /// 0114 /// If you would like the basic block to be auto-inserted into the end of a 0115 /// function, you can specify it as the optional fourth parameter. 0116 /// 0117 /// If you would like to collect additional information about the cloned 0118 /// function, you can specify a ClonedCodeInfo object with the optional fifth 0119 /// parameter. 0120 BasicBlock *CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, 0121 const Twine &NameSuffix = "", Function *F = nullptr, 0122 ClonedCodeInfo *CodeInfo = nullptr); 0123 0124 /// Return a copy of the specified function and add it to that 0125 /// function's module. Also, any references specified in the VMap are changed 0126 /// to refer to their mapped value instead of the original one. If any of the 0127 /// arguments to the function are in the VMap, the arguments are deleted from 0128 /// the resultant function. The VMap is updated to include mappings from all of 0129 /// the instructions and basicblocks in the function from their old to new 0130 /// values. The final argument captures information about the cloned code if 0131 /// non-null. 0132 /// 0133 /// \pre VMap contains no non-identity GlobalValue mappings. 0134 /// 0135 Function *CloneFunction(Function *F, ValueToValueMapTy &VMap, 0136 ClonedCodeInfo *CodeInfo = nullptr); 0137 0138 enum class CloneFunctionChangeType { 0139 LocalChangesOnly, 0140 GlobalChanges, 0141 DifferentModule, 0142 ClonedModule, 0143 }; 0144 0145 /// Clone OldFunc into NewFunc, transforming the old arguments into references 0146 /// to VMap values. Note that if NewFunc already has basic blocks, the ones 0147 /// cloned into it will be added to the end of the function. This function 0148 /// fills in a list of return instructions, and can optionally remap types 0149 /// and/or append the specified suffix to all values cloned. 0150 /// 0151 /// If \p Changes is \a CloneFunctionChangeType::LocalChangesOnly, VMap is 0152 /// required to contain no non-identity GlobalValue mappings. Otherwise, 0153 /// referenced metadata will be cloned. 0154 /// 0155 /// If \p Changes is less than \a CloneFunctionChangeType::DifferentModule 0156 /// indicating cloning into the same module (even if it's LocalChangesOnly), if 0157 /// debug info metadata transitively references a \a DISubprogram, it will be 0158 /// cloned, effectively upgrading \p Changes to GlobalChanges while suppressing 0159 /// cloning of types and compile units. 0160 /// 0161 /// If \p Changes is \a CloneFunctionChangeType::DifferentModule, the new 0162 /// module's \c !llvm.dbg.cu will get updated with any newly created compile 0163 /// units. (\a CloneFunctionChangeType::ClonedModule leaves that work for the 0164 /// caller.) 0165 /// 0166 /// FIXME: Consider simplifying this function by splitting out \a 0167 /// CloneFunctionMetadataInto() and expecting / updating callers to call it 0168 /// first when / how it's needed. 0169 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 0170 ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, 0171 SmallVectorImpl<ReturnInst *> &Returns, 0172 const char *NameSuffix = "", 0173 ClonedCodeInfo *CodeInfo = nullptr, 0174 ValueMapTypeRemapper *TypeMapper = nullptr, 0175 ValueMaterializer *Materializer = nullptr); 0176 0177 /// Clone OldFunc's attributes into NewFunc, transforming values based on the 0178 /// mappings in VMap. 0179 void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc, 0180 ValueToValueMapTy &VMap, 0181 bool ModuleLevelChanges, 0182 ValueMapTypeRemapper *TypeMapper = nullptr, 0183 ValueMaterializer *Materializer = nullptr); 0184 0185 /// Clone OldFunc's metadata into NewFunc. 0186 /// 0187 /// The caller is expected to populate \p VMap beforehand and set an appropriate 0188 /// \p RemapFlag. Subprograms/CUs/types that were already mapped to themselves 0189 /// won't be duplicated. 0190 /// 0191 /// NOTE: This function doesn't clone !llvm.dbg.cu when cloning into a different 0192 /// module. Use CloneFunctionInto for that behavior. 0193 void CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc, 0194 ValueToValueMapTy &VMap, RemapFlags RemapFlag, 0195 ValueMapTypeRemapper *TypeMapper = nullptr, 0196 ValueMaterializer *Materializer = nullptr, 0197 const MetadataSetTy *IdentityMD = nullptr); 0198 0199 /// Clone OldFunc's body into NewFunc. 0200 void CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc, 0201 ValueToValueMapTy &VMap, RemapFlags RemapFlag, 0202 SmallVectorImpl<ReturnInst *> &Returns, 0203 const char *NameSuffix = "", 0204 ClonedCodeInfo *CodeInfo = nullptr, 0205 ValueMapTypeRemapper *TypeMapper = nullptr, 0206 ValueMaterializer *Materializer = nullptr, 0207 const MetadataSetTy *IdentityMD = nullptr); 0208 0209 void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, 0210 const Instruction *StartingInst, 0211 ValueToValueMapTy &VMap, bool ModuleLevelChanges, 0212 SmallVectorImpl<ReturnInst *> &Returns, 0213 const char *NameSuffix = "", 0214 ClonedCodeInfo *CodeInfo = nullptr); 0215 0216 /// This works exactly like CloneFunctionInto, 0217 /// except that it does some simple constant prop and DCE on the fly. The 0218 /// effect of this is to copy significantly less code in cases where (for 0219 /// example) a function call with constant arguments is inlined, and those 0220 /// constant arguments cause a significant amount of code in the callee to be 0221 /// dead. Since this doesn't produce an exactly copy of the input, it can't be 0222 /// used for things like CloneFunction or CloneModule. 0223 /// 0224 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 0225 /// mappings. 0226 /// 0227 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 0228 ValueToValueMapTy &VMap, bool ModuleLevelChanges, 0229 SmallVectorImpl<ReturnInst*> &Returns, 0230 const char *NameSuffix = "", 0231 ClonedCodeInfo *CodeInfo = nullptr); 0232 0233 /// Collect debug information such as types, compile units, and other 0234 /// subprograms that are reachable from \p F and can be considered global for 0235 /// the purposes of cloning (and hence not needing to be cloned). 0236 /// 0237 /// What debug information should be processed depends on \p Changes: when 0238 /// cloning into the same module we process \p F's subprogram and instructions; 0239 /// when into a cloned module, neither of those. 0240 /// 0241 /// Returns DISubprogram of the cloned function when cloning into the same 0242 /// module or nullptr otherwise. 0243 DISubprogram *CollectDebugInfoForCloning(const Function &F, 0244 CloneFunctionChangeType Changes, 0245 DebugInfoFinder &DIFinder); 0246 0247 /// Based on \p Changes and \p DIFinder return debug info that needs to be 0248 /// identity mapped during Metadata cloning. 0249 /// 0250 /// NOTE: Such \a MetadataSetTy can be used by \a CloneFunction* to directly 0251 /// specify metadata that should be identity mapped (and hence not cloned). The 0252 /// metadata will be identity mapped in \a ValueToValueMapTy on first use. There 0253 /// are several reasons for doing it this way rather than eagerly identity 0254 /// mapping metadata nodes in a \a ValueMap: 0255 /// 1. Mapping metadata is not cheap, particularly because of tracking. 0256 /// 2. When cloning a Function we identity map lots of global module-level 0257 /// metadata to avoid cloning it, while only a fraction of it is actually 0258 /// used by the function. Mapping on first use is a lot faster for modules 0259 /// with meaningful amount of debug info. 0260 /// 3. Eagerly identity mapping metadata makes it harder to cache module-level 0261 /// data (e.g. a set of metadata nodes in a \a DICompileUnit). 0262 MetadataSetTy FindDebugInfoToIdentityMap(CloneFunctionChangeType Changes, 0263 DebugInfoFinder &DIFinder, 0264 DISubprogram *SPClonedWithinModule); 0265 0266 /// This class captures the data input to the InlineFunction call, and records 0267 /// the auxiliary results produced by it. 0268 class InlineFunctionInfo { 0269 public: 0270 explicit InlineFunctionInfo( 0271 function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr, 0272 ProfileSummaryInfo *PSI = nullptr, 0273 BlockFrequencyInfo *CallerBFI = nullptr, 0274 BlockFrequencyInfo *CalleeBFI = nullptr, bool UpdateProfile = true) 0275 : GetAssumptionCache(GetAssumptionCache), PSI(PSI), CallerBFI(CallerBFI), 0276 CalleeBFI(CalleeBFI), UpdateProfile(UpdateProfile) {} 0277 0278 /// If non-null, InlineFunction will update the callgraph to reflect the 0279 /// changes it makes. 0280 function_ref<AssumptionCache &(Function &)> GetAssumptionCache; 0281 ProfileSummaryInfo *PSI; 0282 BlockFrequencyInfo *CallerBFI, *CalleeBFI; 0283 0284 /// InlineFunction fills this in with all static allocas that get copied into 0285 /// the caller. 0286 SmallVector<AllocaInst *, 4> StaticAllocas; 0287 0288 /// InlineFunction fills this in with callsites that were inlined from the 0289 /// callee. This is only filled in if CG is non-null. 0290 SmallVector<WeakTrackingVH, 8> InlinedCalls; 0291 0292 /// All of the new call sites inlined into the caller. 0293 /// 0294 /// 'InlineFunction' fills this in by scanning the inlined instructions, and 0295 /// only if CG is null. If CG is non-null, instead the value handle 0296 /// `InlinedCalls` above is used. 0297 SmallVector<CallBase *, 8> InlinedCallSites; 0298 0299 /// Update profile for callee as well as cloned version. We need to do this 0300 /// for regular inlining, but not for inlining from sample profile loader. 0301 bool UpdateProfile; 0302 0303 void reset() { 0304 StaticAllocas.clear(); 0305 InlinedCalls.clear(); 0306 InlinedCallSites.clear(); 0307 } 0308 }; 0309 0310 /// This function inlines the called function into the basic 0311 /// block of the caller. This returns false if it is not possible to inline 0312 /// this call. The program is still in a well defined state if this occurs 0313 /// though. 0314 /// 0315 /// Note that this only does one level of inlining. For example, if the 0316 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 0317 /// exists in the instruction stream. Similarly this will inline a recursive 0318 /// function by one level. 0319 /// 0320 /// Note that while this routine is allowed to cleanup and optimize the 0321 /// *inlined* code to minimize the actual inserted code, it must not delete 0322 /// code in the caller as users of this routine may have pointers to 0323 /// instructions in the caller that need to remain stable. 0324 /// 0325 /// If ForwardVarArgsTo is passed, inlining a function with varargs is allowed 0326 /// and all varargs at the callsite will be passed to any calls to 0327 /// ForwardVarArgsTo. The caller of InlineFunction has to make sure any varargs 0328 /// are only used by ForwardVarArgsTo. 0329 /// 0330 /// The callee's function attributes are merged into the callers' if 0331 /// MergeAttributes is set to true. 0332 InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, 0333 bool MergeAttributes = false, 0334 AAResults *CalleeAAR = nullptr, 0335 bool InsertLifetime = true, 0336 Function *ForwardVarArgsTo = nullptr); 0337 0338 /// Same as above, but it will update the contextual profile. If the contextual 0339 /// profile is invalid (i.e. not loaded because it is not present), it defaults 0340 /// to the behavior of the non-contextual profile updating variant above. This 0341 /// makes it easy to drop-in replace uses of the non-contextual overload. 0342 InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, 0343 PGOContextualProfile &CtxProf, 0344 bool MergeAttributes = false, 0345 AAResults *CalleeAAR = nullptr, 0346 bool InsertLifetime = true, 0347 Function *ForwardVarArgsTo = nullptr); 0348 0349 /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p 0350 /// Blocks. 0351 /// 0352 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block 0353 /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. 0354 /// Note: Only innermost loops are supported. 0355 Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, 0356 Loop *OrigLoop, ValueToValueMapTy &VMap, 0357 const Twine &NameSuffix, LoopInfo *LI, 0358 DominatorTree *DT, 0359 SmallVectorImpl<BasicBlock *> &Blocks); 0360 0361 /// Remaps instructions in \p Blocks using the mapping in \p VMap. 0362 void remapInstructionsInBlocks(ArrayRef<BasicBlock *> Blocks, 0363 ValueToValueMapTy &VMap); 0364 0365 /// Split edge between BB and PredBB and duplicate all non-Phi instructions 0366 /// from BB between its beginning and the StopAt instruction into the split 0367 /// block. Phi nodes are not duplicated, but their uses are handled correctly: 0368 /// we replace them with the uses of corresponding Phi inputs. ValueMapping 0369 /// is used to map the original instructions from BB to their newly-created 0370 /// copies. Returns the split block. 0371 BasicBlock *DuplicateInstructionsInSplitBetween(BasicBlock *BB, 0372 BasicBlock *PredBB, 0373 Instruction *StopAt, 0374 ValueToValueMapTy &ValueMapping, 0375 DomTreeUpdater &DTU); 0376 0377 /// Updates profile information by adjusting the entry count by adding 0378 /// EntryDelta then scaling callsite information by the new count divided by the 0379 /// old count. VMap is used during inlinng to also update the new clone 0380 void updateProfileCallee( 0381 Function *Callee, int64_t EntryDelta, 0382 const ValueMap<const Value *, WeakTrackingVH> *VMap = nullptr); 0383 0384 /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified 0385 /// basic blocks and extract their scope. These are candidates for duplication 0386 /// when cloning. 0387 void identifyNoAliasScopesToClone( 0388 ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes); 0389 0390 /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified 0391 /// instruction range and extract their scope. These are candidates for 0392 /// duplication when cloning. 0393 void identifyNoAliasScopesToClone( 0394 BasicBlock::iterator Start, BasicBlock::iterator End, 0395 SmallVectorImpl<MDNode *> &NoAliasDeclScopes); 0396 0397 /// Duplicate the specified list of noalias decl scopes. 0398 /// The 'Ext' string is added as an extension to the name. 0399 /// Afterwards, the ClonedScopes contains the mapping of the original scope 0400 /// MDNode onto the cloned scope. 0401 /// Be aware that the cloned scopes are still part of the original scope domain. 0402 void cloneNoAliasScopes( 0403 ArrayRef<MDNode *> NoAliasDeclScopes, 0404 DenseMap<MDNode *, MDNode *> &ClonedScopes, 0405 StringRef Ext, LLVMContext &Context); 0406 0407 /// Adapt the metadata for the specified instruction according to the 0408 /// provided mapping. This is normally used after cloning an instruction, when 0409 /// some noalias scopes needed to be cloned. 0410 void adaptNoAliasScopes( 0411 llvm::Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes, 0412 LLVMContext &Context); 0413 0414 /// Clone the specified noalias decl scopes. Then adapt all instructions in the 0415 /// NewBlocks basicblocks to the cloned versions. 0416 /// 'Ext' will be added to the duplicate scope names. 0417 void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes, 0418 ArrayRef<BasicBlock *> NewBlocks, 0419 LLVMContext &Context, StringRef Ext); 0420 0421 /// Clone the specified noalias decl scopes. Then adapt all instructions in the 0422 /// [IStart, IEnd] (IEnd included !) range to the cloned versions. 'Ext' will be 0423 /// added to the duplicate scope names. 0424 void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes, 0425 Instruction *IStart, Instruction *IEnd, 0426 LLVMContext &Context, StringRef Ext); 0427 } // end namespace llvm 0428 0429 #endif // LLVM_TRANSFORMS_UTILS_CLONING_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|