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