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 /// VMap contains no non-identity GlobalValue mappings and debug info metadata
123 /// will not be cloned.
124 ///
125 Function *CloneFunction(Function *F, ValueToValueMapTy &VMap,
126                         ClonedCodeInfo *CodeInfo = nullptr);
127 
128 /// Clone OldFunc into NewFunc, transforming the old arguments into references
129 /// to VMap values.  Note that if NewFunc already has basic blocks, the ones
130 /// cloned into it will be added to the end of the function.  This function
131 /// fills in a list of return instructions, and can optionally remap types
132 /// and/or append the specified suffix to all values cloned.
133 ///
134 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
135 /// mappings.
136 ///
137 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
138                        ValueToValueMapTy &VMap, bool ModuleLevelChanges,
139                        SmallVectorImpl<ReturnInst*> &Returns,
140                        const char *NameSuffix = "",
141                        ClonedCodeInfo *CodeInfo = nullptr,
142                        ValueMapTypeRemapper *TypeMapper = nullptr,
143                        ValueMaterializer *Materializer = nullptr);
144 
145 void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
146                                const Instruction *StartingInst,
147                                ValueToValueMapTy &VMap, bool ModuleLevelChanges,
148                                SmallVectorImpl<ReturnInst *> &Returns,
149                                const char *NameSuffix = "",
150                                ClonedCodeInfo *CodeInfo = nullptr);
151 
152 /// This works exactly like CloneFunctionInto,
153 /// except that it does some simple constant prop and DCE on the fly.  The
154 /// effect of this is to copy significantly less code in cases where (for
155 /// example) a function call with constant arguments is inlined, and those
156 /// constant arguments cause a significant amount of code in the callee to be
157 /// dead.  Since this doesn't produce an exactly copy of the input, it can't be
158 /// used for things like CloneFunction or CloneModule.
159 ///
160 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
161 /// mappings.
162 ///
163 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
164                                ValueToValueMapTy &VMap, bool ModuleLevelChanges,
165                                SmallVectorImpl<ReturnInst*> &Returns,
166                                const char *NameSuffix = "",
167                                ClonedCodeInfo *CodeInfo = nullptr,
168                                Instruction *TheCall = nullptr);
169 
170 /// This class captures the data input to the InlineFunction call, and records
171 /// the auxiliary results produced by it.
172 class InlineFunctionInfo {
173 public:
174   explicit InlineFunctionInfo(
175       CallGraph *cg = nullptr,
176       function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr,
177       ProfileSummaryInfo *PSI = nullptr,
178       BlockFrequencyInfo *CallerBFI = nullptr,
179       BlockFrequencyInfo *CalleeBFI = nullptr)
180       : CG(cg), GetAssumptionCache(GetAssumptionCache), PSI(PSI),
181         CallerBFI(CallerBFI), CalleeBFI(CalleeBFI) {}
182 
183   /// If non-null, InlineFunction will update the callgraph to reflect the
184   /// changes it makes.
185   CallGraph *CG;
186   function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
187   ProfileSummaryInfo *PSI;
188   BlockFrequencyInfo *CallerBFI, *CalleeBFI;
189 
190   /// InlineFunction fills this in with all static allocas that get copied into
191   /// the caller.
192   SmallVector<AllocaInst *, 4> StaticAllocas;
193 
194   /// InlineFunction fills this in with callsites that were inlined from the
195   /// callee. This is only filled in if CG is non-null.
196   SmallVector<WeakTrackingVH, 8> InlinedCalls;
197 
198   /// All of the new call sites inlined into the caller.
199   ///
200   /// 'InlineFunction' fills this in by scanning the inlined instructions, and
201   /// only if CG is null. If CG is non-null, instead the value handle
202   /// `InlinedCalls` above is used.
203   SmallVector<CallBase *, 8> InlinedCallSites;
204 
205   void reset() {
206     StaticAllocas.clear();
207     InlinedCalls.clear();
208     InlinedCallSites.clear();
209   }
210 };
211 
212 /// This function inlines the called function into the basic
213 /// block of the caller.  This returns false if it is not possible to inline
214 /// this call.  The program is still in a well defined state if this occurs
215 /// though.
216 ///
217 /// Note that this only does one level of inlining.  For example, if the
218 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
219 /// exists in the instruction stream.  Similarly this will inline a recursive
220 /// function by one level.
221 ///
222 /// Note that while this routine is allowed to cleanup and optimize the
223 /// *inlined* code to minimize the actual inserted code, it must not delete
224 /// code in the caller as users of this routine may have pointers to
225 /// instructions in the caller that need to remain stable.
226 ///
227 /// If ForwardVarArgsTo is passed, inlining a function with varargs is allowed
228 /// and all varargs at the callsite will be passed to any calls to
229 /// ForwardVarArgsTo. The caller of InlineFunction has to make sure any varargs
230 /// are only used by ForwardVarArgsTo.
231 InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
232                             AAResults *CalleeAAR = nullptr,
233                             bool InsertLifetime = true,
234                             Function *ForwardVarArgsTo = nullptr);
235 
236 /// Clones a loop \p OrigLoop.  Returns the loop and the blocks in \p
237 /// Blocks.
238 ///
239 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
240 /// \p LoopDomBB.  Insert the new blocks before block specified in \p Before.
241 /// Note: Only innermost loops are supported.
242 Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
243                              Loop *OrigLoop, ValueToValueMapTy &VMap,
244                              const Twine &NameSuffix, LoopInfo *LI,
245                              DominatorTree *DT,
246                              SmallVectorImpl<BasicBlock *> &Blocks);
247 
248 /// Remaps instructions in \p Blocks using the mapping in \p VMap.
249 void remapInstructionsInBlocks(const SmallVectorImpl<BasicBlock *> &Blocks,
250                                ValueToValueMapTy &VMap);
251 
252 /// Split edge between BB and PredBB and duplicate all non-Phi instructions
253 /// from BB between its beginning and the StopAt instruction into the split
254 /// block. Phi nodes are not duplicated, but their uses are handled correctly:
255 /// we replace them with the uses of corresponding Phi inputs. ValueMapping
256 /// is used to map the original instructions from BB to their newly-created
257 /// copies. Returns the split block.
258 BasicBlock *DuplicateInstructionsInSplitBetween(BasicBlock *BB,
259                                                 BasicBlock *PredBB,
260                                                 Instruction *StopAt,
261                                                 ValueToValueMapTy &ValueMapping,
262                                                 DomTreeUpdater &DTU);
263 
264 /// Updates profile information by adjusting the entry count by adding
265 /// entryDelta then scaling callsite information by the new count divided by the
266 /// old count. VMap is used during inlinng to also update the new clone
267 void updateProfileCallee(
268     Function *Callee, int64_t entryDelta,
269     const ValueMap<const Value *, WeakTrackingVH> *VMap = nullptr);
270 
271 } // end namespace llvm
272 
273 #endif // LLVM_TRANSFORMS_UTILS_CLONING_H
274