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