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 DebugInfoFinder;
37 class DominatorTree;
38 class Function;
39 class Instruction;
40 class Loop;
41 class LoopInfo;
42 class Module;
43 class ProfileSummaryInfo;
44 class ReturnInst;
45 class DomTreeUpdater;
46 
47 /// Return an exact copy of the specified module
48 std::unique_ptr<Module> CloneModule(const Module &M);
49 std::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.
55 std::unique_ptr<Module>
56 CloneModule(const Module &M, ValueToValueMapTy &VMap,
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.
61 struct ClonedCodeInfo {
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.
67   bool ContainsMemProfMetadata = false;
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.
72   bool ContainsDynamicAllocas = false;
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.
82   DenseMap<const Value *, const Value *> OrigVMap;
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.
118 BasicBlock *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 ///
134 Function *CloneFunction(Function *F, ValueToValueMapTy &VMap,
135                         ClonedCodeInfo *CodeInfo = nullptr);
136 
137 enum class CloneFunctionChangeType {
138   LocalChangesOnly,
139   GlobalChanges,
140   DifferentModule,
141   ClonedModule,
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.
168 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
169                        ValueToValueMapTy &VMap, CloneFunctionChangeType Changes,
170                        SmallVectorImpl<ReturnInst *> &Returns,
171                        const char *NameSuffix = "",
172                        ClonedCodeInfo *CodeInfo = nullptr,
173                        ValueMapTypeRemapper *TypeMapper = nullptr,
174                        ValueMaterializer *Materializer = nullptr);
175 
176 void 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 ///
194 void 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.
202 class InlineFunctionInfo {
203 public:
204   explicit InlineFunctionInfo(
205       function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr,
206       ProfileSummaryInfo *PSI = nullptr,
207       BlockFrequencyInfo *CallerBFI = nullptr,
208       BlockFrequencyInfo *CalleeBFI = nullptr, bool UpdateProfile = true)
209       : GetAssumptionCache(GetAssumptionCache), PSI(PSI), CallerBFI(CallerBFI),
210         CalleeBFI(CalleeBFI), UpdateProfile(UpdateProfile) {}
211 
212   /// If non-null, InlineFunction will update the callgraph to reflect the
213   /// changes it makes.
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 ///
264 /// The callee's function attributes are merged into the callers' if
265 /// MergeAttributes is set to true.
266 InlineResult 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.
278 Loop *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.
285 void 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.
294 BasicBlock *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
303 void updateProfileCallee(
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.
310 void identifyNoAliasScopesToClone(
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.
316 void identifyNoAliasScopesToClone(
317     BasicBlock::iterator Start, BasicBlock::iterator End,
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.
325 void cloneNoAliasScopes(
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.
333 void adaptNoAliasScopes(
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.
340 void 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.
347 void 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
353