LLVM 23.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"
21#include <limits>
22
23namespace llvm {
24
25template <typename PtrType> class SmallPtrSetImpl;
26class AllocaInst;
27class BasicBlock;
28class BlockFrequency;
31class AssumptionCache;
32class CallInst;
33class DominatorTree;
34class Function;
35class Instruction;
36class Module;
37class Type;
38class Value;
39class StructType;
40
41/// A cache for the CodeExtractor analysis. The operation \ref
42/// CodeExtractor::extractCodeRegion is guaranteed not to invalidate this
43/// object. This object should conservatively be considered invalid if any
44/// other mutating operations on the IR occur.
45///
46/// Constructing this object is O(n) in the size of the function.
48 /// The allocas in the function.
50
51 /// Base memory addresses of load/store instructions, grouped by block.
53
54 /// Blocks which contain instructions which may have unknown side-effects
55 /// on memory.
56 DenseSet<BasicBlock *> SideEffectingBlocks;
57
58 void findSideEffectInfoForBlock(BasicBlock &BB);
59
60public:
62
63 /// Get the allocas in the function at the time the analysis was created.
64 /// Note that some of these allocas may no longer be present in the function,
65 /// due to \ref CodeExtractor::extractCodeRegion.
66 ArrayRef<AllocaInst *> getAllocas() const { return Allocas; }
67
68 /// Check whether \p BB contains an instruction thought to load from, store
69 /// to, or otherwise clobber the alloca \p Addr.
71 AllocaInst *Addr) const;
72};
73
74/// Utility class for extracting code into a new function.
75///
76/// This utility provides a simple interface for extracting some sequence of
77/// code into its own function, replacing it with a call to that function. It
78/// also provides various methods to query about the nature and result of such a
79/// transformation.
80///
81/// The rough algorithm used is:
82/// 1) Find both the inputs and outputs for the extracted region.
83/// 2) Pass the inputs as arguments, remapping them within the extracted
84/// function to arguments.
85/// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas as
86/// arguments, and inserting stores to the arguments for any scalars.
88 using ValueSet = SetVector<Value *>;
89
90 // Various bits of state computed on construction.
91 DominatorTree *const DT;
92 const bool AggregateArgs;
96
97 // A block outside of the extraction set where any intermediate allocations
98 // will be placed inside. If this is null, allocations will be placed in the
99 // entry block of the function.
100 BasicBlock *AllocationBlock;
101
102 // If true, varargs functions can be extracted.
103 bool AllowVarArgs;
104
105 // Bits of intermediate state computed at various phases of extraction.
107
108 /// Lists of blocks that are branched from the code region to be extracted,
109 /// also called the exit blocks. Each block is contained at most once. Its
110 /// order defines the return value of the extracted function.
111 ///
112 /// When there is just one (or no) exit block, the return value is irrelevant.
113 ///
114 /// When there are exactly two exit blocks, the extracted function returns a
115 /// boolean. For ExtractedFuncRetVals[0], it returns 'true'. For
116 /// ExtractedFuncRetVals[1] it returns 'false'.
117 /// NOTE: Since a boolean is represented by i1, ExtractedFuncRetVals[0]
118 /// returns 1 and ExtractedFuncRetVals[1] returns 0, which opposite of
119 /// the regular pattern below.
120 ///
121 /// When there are 3 or more exit blocks, leaving the extracted function via
122 /// the first block it returns 0. When leaving via the second entry it returns
123 /// 1, etc.
124 SmallVector<BasicBlock *> ExtractedFuncRetVals;
125
126 // Suffix to use when creating extracted function (appended to the original
127 // function name + "."). If empty, the default is to use the entry block
128 // label, if non-empty, otherwise "extracted".
129 std::string Suffix;
130
131 // If true, the outlined function has aggregate argument in zero address
132 // space.
133 bool ArgsInZeroAddressSpace;
134
135 // If true, the outlined function always return void even when there is only
136 // one output.
137 bool VoidReturnWithSingleOutput;
138
139 // If set, the return value of the outline function.
140 Value *FuncRetVal = nullptr;
141
142public:
143 /// Create a code extractor for a sequence of blocks.
144 ///
145 /// Given a sequence of basic blocks where the first block in the sequence
146 /// dominates the rest, prepare a code extractor object for pulling this
147 /// sequence out into its new function. When a DominatorTree is also given,
148 /// extra checking and transformations are enabled. If AllowVarArgs is true,
149 /// vararg functions can be extracted. This is safe, if all vararg handling
150 /// code is extracted, including vastart. If AllowAlloca is true, then
151 /// extraction of blocks containing alloca instructions would be possible,
152 /// however code extractor won't validate whether extraction is legal. Any new
153 /// allocations will be placed in the AllocationBlock, unless it is null, in
154 /// which case it will be placed in the entry block of the function from which
155 /// the code is being extracted. If ArgsInZeroAddressSpace param is set to
156 /// true, then the aggregate param pointer of the outlined function is
157 /// declared in zero address space. If VoidReturnWithSingleOutput is set to
158 /// true, then the return type of the outlined function is set void even if
159 /// there is only one output.
161 bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
162 BranchProbabilityInfo *BPI = nullptr,
163 AssumptionCache *AC = nullptr, bool AllowVarArgs = false,
164 bool AllowAlloca = false, BasicBlock *AllocationBlock = nullptr,
165 std::string Suffix = "", bool ArgsInZeroAddressSpace = false,
166 bool VoidReturnWithSingleOutput = true);
167
168 /// Perform the extraction, returning the new function.
169 ///
170 /// Returns zero when called on a CodeExtractor instance where isEligible
171 /// returns false.
173
174 /// Perform the extraction, returning the new function and providing an
175 /// interface to see what was categorized as inputs and outputs.
176 ///
177 /// \param CEAC - Cache to speed up operations for the CodeExtractor when
178 /// hoisting, and extracting lifetime values and assumes.
179 /// \param Inputs [in/out] - filled with values marked as inputs to the newly
180 /// outlined function.
181 /// \param Outputs [out] - filled with values marked as outputs to the newly
182 /// outlined function.
183 /// \returns zero when called on a CodeExtractor instance where isEligible
184 /// returns false.
186 ValueSet &Inputs, ValueSet &Outputs);
187
188 /// Verify that assumption cache isn't stale after a region is extracted.
189 /// Returns true when verifier finds errors. AssumptionCache is passed as
190 /// parameter to make this function stateless.
191 static bool verifyAssumptionCache(const Function &OldFunc,
192 const Function &NewFunc,
193 AssumptionCache *AC);
194
195 /// Test whether this code extractor is eligible.
196 ///
197 /// Based on the blocks used when constructing the code extractor, determine
198 /// whether it is eligible for extraction.
199 ///
200 /// Checks that varargs handling (with vastart and vaend) is only done in the
201 /// outlined blocks.
202 bool isEligible() const;
203
204 /// Compute the set of input values and output values for the code.
205 ///
206 /// These can be used either when performing the extraction or to evaluate the
207 /// expected size of a call to the extracted function. Note that this work
208 /// cannot be cached between the two as once we decide to extract a code
209 /// sequence, that sequence is modified, including changing these sets, before
210 /// extraction occurs. These modifications won't have any significant impact
211 /// on the cost however.
212 void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
213 const ValueSet &Allocas,
214 bool CollectGlobalInputs = false);
215
216 /// Check if life time marker nodes can be hoisted/sunk into the outline
217 /// region.
218 ///
219 /// Returns true if it is safe to do the code motion.
220 bool
222 Instruction *AllocaAddr) const;
223
224 /// Find the set of allocas whose life ranges are contained within the
225 /// outlined region.
226 ///
227 /// Allocas which have life_time markers contained in the outlined region
228 /// should be pushed to the outlined function. The address bitcasts that are
229 /// used by the lifetime markers are also candidates for shrink-wrapping. The
230 /// instructions that need to be sunk are collected in 'Allocas'.
231 void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands,
232 ValueSet &HoistCands, BasicBlock *&ExitBlock) const;
233
234 /// Find or create a block within the outline region for placing hoisted code.
235 ///
236 /// CommonExitBlock is block outside the outline region. It is the common
237 /// successor of blocks inside the region. If there exists a single block
238 /// inside the region that is the predecessor of CommonExitBlock, that block
239 /// will be returned. Otherwise CommonExitBlock will be split and the original
240 /// block will be added to the outline region.
242
243 /// Exclude a value from aggregate argument passing when extracting a code
244 /// region, passing it instead as a scalar.
246
247private:
248 struct LifetimeMarkerInfo {
249 bool SinkLifeStart = false;
250 bool HoistLifeEnd = false;
251 Instruction *LifeStart = nullptr;
252 Instruction *LifeEnd = nullptr;
253 };
254
255 ValueSet ExcludeArgsFromAggregate;
256
257 LifetimeMarkerInfo getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
258 Instruction *Addr,
259 BasicBlock *ExitBlock) const;
260
261 /// Updates the list of SwitchCases (corresponding to exit blocks) after
262 /// changes of the control flow or the Blocks list.
263 void computeExtractedFuncRetVals();
264
265 /// Return the type used for the return code of the extracted function to
266 /// indicate which exit block to jump to.
267 Type *getSwitchType();
268
269 void severSplitPHINodesOfEntry(BasicBlock *&Header);
270 void severSplitPHINodesOfExits();
271 void splitReturnBlocks();
272
273 void moveCodeToFunction(Function *newFunction);
274
275 void calculateNewCallTerminatorWeights(
276 BasicBlock *CodeReplacer,
279
280 /// Normalizes the control flow of the extracted regions, such as ensuring
281 /// that the extracted region does not contain a return instruction.
282 void normalizeCFGForExtraction(BasicBlock *&header);
283
284 /// Generates the function declaration for the function containing the
285 /// extracted code.
286 Function *
287 constructFunctionDeclaration(const ValueSet &inputs, const ValueSet &outputs,
288 BlockFrequency EntryFreq, const Twine &Name,
289 ValueSet &StructValues, StructType *&StructTy);
290
291 /// Generates the code for the extracted function. That is: a prolog, the
292 /// moved or copied code from the original function, and epilogs for each
293 /// exit.
294 void emitFunctionBody(const ValueSet &inputs, const ValueSet &outputs,
295 const ValueSet &StructValues, Function *newFunction,
296 StructType *StructArgTy, BasicBlock *header,
297 const ValueSet &SinkingCands,
298 SmallVectorImpl<Value *> &NewValues);
299
300 /// Generates a Basic Block that calls the extracted function.
301 CallInst *emitReplacerCall(const ValueSet &inputs, const ValueSet &outputs,
302 const ValueSet &StructValues,
303 Function *newFunction, StructType *StructArgTy,
304 Function *oldFunction, BasicBlock *ReplIP,
305 BlockFrequency EntryFreq,
306 ArrayRef<Value *> LifetimesStart,
307 std::vector<Value *> &Reloads);
308
309 /// Connects the basic block containing the call to the extracted function
310 /// into the original function's control flow.
311 void
312 insertReplacerCall(Function *oldFunction, BasicBlock *header,
313 CallInst *ReplacerCall, const ValueSet &outputs,
314 ArrayRef<Value *> Reloads,
315 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights);
316};
317
318} // end namespace llvm
319
320#endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
This file implements a set that has insertion order iteration characteristics.
an instruction to allocate memory on the stack
ArrayRef - 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:62
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.
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
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, std::string Suffix="", bool ArgsInZeroAddressSpace=false, bool VoidReturnWithSingleOutput=true)
Create a code extractor for a sequence of blocks.
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
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.
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A vector that has set insertion semantics.
Definition SetVector.h:57
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
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.