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