LLVM 24.0.0git
CodeExtractor.h
Go to the documentation of this file.
1//===- Transform/Utils/CodeExtractor.h - Code extraction util ---*- 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// A utility to support extracting code from one function into its own
10// stand-alone function.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
15#define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SetVector.h"
20#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/DebugLoc.h"
22#include "llvm/IR/IRBuilder.h"
24#include <limits>
25
26namespace llvm {
27
29class AllocaInst;
30class BlockFrequency;
33class AssumptionCache;
34class CallInst;
35class DominatorTree;
36class Function;
37class Instruction;
38class Type;
39class Value;
40class StructType;
41
42/// A cache for the CodeExtractor analysis. The operation \ref
43/// CodeExtractor::extractCodeRegion is guaranteed not to invalidate this
44/// object. This object should conservatively be considered invalid if any
45/// other mutating operations on the IR occur.
46///
47/// Constructing this object is O(n) in the size of the function.
49 /// The allocas in the function.
51
52 /// Base memory addresses of load/store instructions, grouped by block.
54
55 /// Blocks which contain instructions which may have unknown side-effects
56 /// on memory.
57 DenseSet<BasicBlock *> SideEffectingBlocks;
58
59 void findSideEffectInfoForBlock(BasicBlock &BB);
60
61public:
63
64 /// Get the allocas in the function at the time the analysis was created.
65 /// Note that some of these allocas may no longer be present in the function,
66 /// due to \ref CodeExtractor::extractCodeRegion.
67 ArrayRef<AllocaInst *> getAllocas() const { return Allocas; }
68
69 /// Check whether \p BB contains an instruction thought to load from, store
70 /// to, or otherwise clobber the alloca \p Addr.
72 AllocaInst *Addr) const;
73};
74
75/// Utility class for extracting code into a new function.
76///
77/// This utility provides a simple interface for extracting some sequence of
78/// code into its own function, replacing it with a call to that function. It
79/// also provides various methods to query about the nature and result of such a
80/// transformation.
81///
82/// The rough algorithm used is:
83/// 1) Find both the inputs and outputs for the extracted region.
84/// 2) Pass the inputs as arguments, remapping them within the extracted
85/// function to arguments.
86/// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas as
87/// arguments, and inserting stores to the arguments for any scalars.
89 using ValueSet = SetVector<Value *>;
90
91 // Various bits of state computed on construction.
92 DominatorTree *const DT;
93 const bool AggregateArgs;
97
98 /// A block outside of the extraction set where any intermediate allocations
99 /// will be placed inside. If this is null, allocations will be placed in the
100 /// entry block of the function.
101 BasicBlock *AllocationBlock;
102
103 /// A set of blocks outside of the extraction set where deallocations for
104 /// intermediate allocations should be placed. Not used for automatically
105 /// deallocated memory (e.g. `alloca`), which is the default.
106 ///
107 /// If it is empty and needed, the end of the replacement basic block will be
108 /// used to place deallocations.
109 SmallVector<BasicBlock *> DeallocationBlocks;
110
111 /// If true, varargs functions can be extracted.
112 bool AllowVarArgs;
113
114 /// Bits of intermediate state computed at various phases of extraction.
116
117 /// Lists of blocks that are branched from the code region to be extracted,
118 /// also called the exit blocks. Each block is contained at most once. Its
119 /// order defines the return value of the extracted function.
120 ///
121 /// When there is just one (or no) exit block, the return value is irrelevant.
122 ///
123 /// When there are exactly two exit blocks, the extracted function returns a
124 /// boolean. For ExtractedFuncRetVals[0], it returns 'true'. For
125 /// ExtractedFuncRetVals[1] it returns 'false'.
126 /// NOTE: Since a boolean is represented by i1, ExtractedFuncRetVals[0]
127 /// returns 1 and ExtractedFuncRetVals[1] returns 0, which opposite of
128 /// the regular pattern below.
129 ///
130 /// When there are 3 or more exit blocks, leaving the extracted function via
131 /// the first block it returns 0. When leaving via the second entry it returns
132 /// 1, etc.
133 SmallVector<BasicBlock *> ExtractedFuncRetVals;
134
135 /// Suffix to use when creating extracted function (appended to the original
136 /// function name + "."). If empty, the default is to use the entry block
137 /// label, if non-empty, otherwise "extracted".
138 std::string Suffix;
139
140 /// If true, the outlined function has aggregate argument in zero address
141 /// space.
142 bool ArgsInZeroAddressSpace;
143
144 // If true, the outlined function always return void even when there is only
145 // one output.
146 bool VoidReturnWithSingleOutput;
147
148 // If set, the return value of the outline function.
149 Value *FuncRetVal = nullptr;
150
151public:
152 /// Create a code extractor for a sequence of blocks.
153 ///
154 /// Given a sequence of basic blocks where the first block in the sequence
155 /// dominates the rest, prepare a code extractor object for pulling this
156 /// sequence out into its new function. When a DominatorTree is also given,
157 /// extra checking and transformations are enabled. If AllowVarArgs is true,
158 /// vararg functions can be extracted. This is safe, if all vararg handling
159 /// code is extracted, including vastart. If AllowAlloca is true, then
160 /// extraction of blocks containing alloca instructions would be possible,
161 /// however code extractor won't validate whether extraction is legal. Any new
162 /// allocations will be placed in the AllocationBlock, unless it is null, in
163 /// which case it will be placed in the entry block of the function from which
164 /// the code is being extracted. Explicit deallocations for the aforementioned
165 /// allocations will be placed, if needed, in all blocks in DeallocationBlocks
166 /// or the end of the replacement block. If ArgsInZeroAddressSpace param is
167 /// set to true, then the aggregate param pointer of the outlined function is
168 /// declared in zero address space. If VoidReturnWithSingleOutput is set to
169 /// true, then the return type of the outlined function is set void even if
170 /// there is only one output.
172 bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
173 BranchProbabilityInfo *BPI = nullptr,
174 AssumptionCache *AC = nullptr, bool AllowVarArgs = false,
175 bool AllowAlloca = false, BasicBlock *AllocationBlock = nullptr,
176 ArrayRef<BasicBlock *> DeallocationBlocks = {},
177 std::string Suffix = "", bool ArgsInZeroAddressSpace = false,
178 bool VoidReturnWithSingleOutput = true);
179
180 virtual ~CodeExtractor() = default;
181
182 /// Perform the extraction, returning the new function.
183 ///
184 /// Returns zero when called on a CodeExtractor instance where isEligible
185 /// returns false.
187
188 /// Perform the extraction, returning the new function and providing an
189 /// interface to see what was categorized as inputs and outputs.
190 ///
191 /// \param CEAC - Cache to speed up operations for the CodeExtractor when
192 /// hoisting, and extracting lifetime values and assumes.
193 /// \param Inputs [in/out] - filled with values marked as inputs to the newly
194 /// outlined function.
195 /// \param Outputs [out] - filled with values marked as outputs to the newly
196 /// outlined function.
197 /// \returns zero when called on a CodeExtractor instance where isEligible
198 /// returns false.
200 ValueSet &Inputs, ValueSet &Outputs);
201
202 /// Verify that assumption cache isn't stale after a region is extracted.
203 /// Returns true when verifier finds errors. AssumptionCache is passed as
204 /// parameter to make this function stateless.
205 static bool verifyAssumptionCache(const Function &OldFunc,
206 const Function &NewFunc,
207 AssumptionCache *AC);
208
209 /// Test whether this code extractor is eligible.
210 ///
211 /// Based on the blocks used when constructing the code extractor, determine
212 /// whether it is eligible for extraction.
213 ///
214 /// Checks that varargs handling (with vastart and vaend) is only done in the
215 /// outlined blocks.
216 bool isEligible() const;
217
218 /// Compute the set of input values and output values for the code.
219 ///
220 /// These can be used either when performing the extraction or to evaluate the
221 /// expected size of a call to the extracted function. Note that this work
222 /// cannot be cached between the two as once we decide to extract a code
223 /// sequence, that sequence is modified, including changing these sets, before
224 /// extraction occurs. These modifications won't have any significant impact
225 /// on the cost however.
226 void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
227 const ValueSet &Allocas,
228 bool CollectGlobalInputs = false);
229
230 /// Check if life time marker nodes can be hoisted/sunk into the outline
231 /// region.
232 ///
233 /// Returns true if it is safe to do the code motion.
234 bool
236 Instruction *AllocaAddr) const;
237
238 /// Find the set of allocas whose life ranges are contained within the
239 /// outlined region.
240 ///
241 /// Allocas which have life_time markers contained in the outlined region
242 /// should be pushed to the outlined function. The address bitcasts that are
243 /// used by the lifetime markers are also candidates for shrink-wrapping. The
244 /// instructions that need to be sunk are collected in 'Allocas'.
245 void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands,
246 ValueSet &HoistCands, BasicBlock *&ExitBlock) const;
247
248 /// Find or create a block within the outline region for placing hoisted code.
249 ///
250 /// CommonExitBlock is block outside the outline region. It is the common
251 /// successor of blocks inside the region. If there exists a single block
252 /// inside the region that is the predecessor of CommonExitBlock, that block
253 /// will be returned. Otherwise CommonExitBlock will be split and the original
254 /// block will be added to the outline region.
256
257 /// Exclude a value from aggregate argument passing when extracting a code
258 /// region, passing it instead as a scalar.
260
261protected:
262 /// Allocate an intermediate variable at the specified point.
264 DebugLoc DL, Type *VarType,
265 const Twine &Name = Twine(""),
266 AddrSpaceCastInst **CastedAlloc = nullptr);
267
268 /// Deallocate a previously-allocated intermediate variable at the specified
269 /// point.
271 DebugLoc DL, Value *Var, Type *VarType);
272
273private:
274 struct LifetimeMarkerInfo {
275 bool SinkLifeStart = false;
276 bool HoistLifeEnd = false;
277 Instruction *LifeStart = nullptr;
278 Instruction *LifeEnd = nullptr;
279 };
280
281 ValueSet ExcludeArgsFromAggregate;
282
283 LifetimeMarkerInfo getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
284 Instruction *Addr,
285 BasicBlock *ExitBlock) const;
286
287 /// Updates the list of SwitchCases (corresponding to exit blocks) after
288 /// changes of the control flow or the Blocks list.
289 void computeExtractedFuncRetVals();
290
291 /// Return the type used for the return code of the extracted function to
292 /// indicate which exit block to jump to.
293 Type *getSwitchType();
294
295 void severSplitPHINodesOfEntry(BasicBlock *&Header);
296 void severSplitPHINodesOfExits();
297 void splitReturnBlocks();
298
299 void moveCodeToFunction(Function *newFunction);
300
301 void calculateNewCallTerminatorWeights(
302 BasicBlock *CodeReplacer,
305
306 /// Normalizes the control flow of the extracted regions, such as ensuring
307 /// that the extracted region does not contain a return instruction.
308 void normalizeCFGForExtraction(BasicBlock *&header);
309
310 /// Generates the function declaration for the function containing the
311 /// extracted code.
312 Function *
313 constructFunctionDeclaration(const ValueSet &inputs, const ValueSet &outputs,
314 BlockFrequency EntryFreq, const Twine &Name,
315 ValueSet &StructValues, StructType *&StructTy);
316
317 /// Generates the code for the extracted function. That is: a prolog, the
318 /// moved or copied code from the original function, and epilogs for each
319 /// exit.
320 void emitFunctionBody(const ValueSet &inputs, const ValueSet &outputs,
321 const ValueSet &StructValues, Function *newFunction,
322 StructType *StructArgTy, BasicBlock *header,
323 const ValueSet &SinkingCands,
324 SmallVectorImpl<Value *> &NewValues);
325
326 /// Generates a Basic Block that calls the extracted function.
327 CallInst *emitReplacerCall(const ValueSet &inputs, const ValueSet &outputs,
328 const ValueSet &StructValues,
329 Function *newFunction, StructType *StructArgTy,
330 Function *oldFunction, BasicBlock *ReplIP,
331 BlockFrequency EntryFreq,
332 ArrayRef<Value *> LifetimesStart,
333 std::vector<Value *> &Reloads);
334
335 /// Connects the basic block containing the call to the extracted function
336 /// into the original function's control flow.
337 void
338 insertReplacerCall(Function *oldFunction, BasicBlock *header,
339 CallInst *ReplacerCall, const ValueSet &outputs,
340 ArrayRef<Value *> Reloads,
341 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights);
342};
343
344} // end namespace llvm
345
346#endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
Hexagon Lower HLX Pair Allocas
#define F(x, y, z)
Definition MD5.cpp:54
This file implements a set that has insertion order iteration characteristics.
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis providing branch probability information.
This class represents a function call, abstracting a target machine's calling convention.
A cache for the CodeExtractor analysis.
ArrayRef< AllocaInst * > getAllocas() const
Get the allocas in the function at the time the analysis was created.
LLVM_ABI CodeExtractorAnalysisCache(Function &F)
LLVM_ABI bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const
Check whether BB contains an instruction thought to load from, store to, or otherwise clobber the all...
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted code.
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas, bool CollectGlobalInputs=false)
Compute the set of input values and output values for the code.
void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands, ValueSet &HoistCands, BasicBlock *&ExitBlock) const
Find the set of allocas whose life ranges are contained within the outlined region.
CodeExtractor(ArrayRef< BasicBlock * > BBs, DominatorTree *DT=nullptr, bool AggregateArgs=false, BlockFrequencyInfo *BFI=nullptr, BranchProbabilityInfo *BPI=nullptr, AssumptionCache *AC=nullptr, bool AllowVarArgs=false, bool AllowAlloca=false, BasicBlock *AllocationBlock=nullptr, ArrayRef< BasicBlock * > DeallocationBlocks={}, std::string Suffix="", bool ArgsInZeroAddressSpace=false, bool VoidReturnWithSingleOutput=true)
Create a code extractor for a sequence of blocks.
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
virtual ~CodeExtractor()=default
virtual Instruction * allocateVar(IRBuilder<>::InsertPoint AllocaIP, DebugLoc DL, Type *VarType, const Twine &Name=Twine(""), AddrSpaceCastInst **CastedAlloc=nullptr)
Allocate an intermediate variable at the specified point.
bool isEligible() const
Test whether this code extractor is eligible.
void excludeArgFromAggregate(Value *Arg)
Exclude a value from aggregate argument passing when extracting a code region, passing it instead as ...
bool isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, Instruction *AllocaAddr) const
Check if life time marker nodes can be hoisted/sunk into the outline region.
virtual Instruction * deallocateVar(IRBuilder<>::InsertPoint DeallocIP, DebugLoc DL, Value *Var, Type *VarType)
Deallocate a previously-allocated intermediate variable at the specified point.
A debug info location.
Definition DebugLoc.h:126
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
BasicBlock::iterator InsertPoint
InsertPoint - A saved insertion point.
Definition IRBuilder.h:254
A vector that has set insertion semantics.
Definition SetVector.h:57
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.