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