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
135public:
136 /// Create a code extractor for a sequence of blocks.
137 ///
138 /// Given a sequence of basic blocks where the first block in the sequence
139 /// dominates the rest, prepare a code extractor object for pulling this
140 /// sequence out into its new function. When a DominatorTree is also given,
141 /// extra checking and transformations are enabled. If AllowVarArgs is true,
142 /// vararg functions can be extracted. This is safe, if all vararg handling
143 /// code is extracted, including vastart. If AllowAlloca is true, then
144 /// extraction of blocks containing alloca instructions would be possible,
145 /// however code extractor won't validate whether extraction is legal. Any new
146 /// allocations will be placed in the AllocationBlock, unless it is null, in
147 /// which case it will be placed in the entry block of the function from which
148 /// the code is being extracted. If ArgsInZeroAddressSpace param is set to
149 /// true, then the aggregate param pointer of the outlined function is
150 /// declared in zero address space.
152 bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
153 BranchProbabilityInfo *BPI = nullptr,
154 AssumptionCache *AC = nullptr, bool AllowVarArgs = false,
155 bool AllowAlloca = false, BasicBlock *AllocationBlock = nullptr,
156 std::string Suffix = "", bool ArgsInZeroAddressSpace = false);
157
158 /// Perform the extraction, returning the new function.
159 ///
160 /// Returns zero when called on a CodeExtractor instance where isEligible
161 /// returns false.
163
164 /// Perform the extraction, returning the new function and providing an
165 /// interface to see what was categorized as inputs and outputs.
166 ///
167 /// \param CEAC - Cache to speed up operations for the CodeExtractor when
168 /// hoisting, and extracting lifetime values and assumes.
169 /// \param Inputs [in/out] - filled with values marked as inputs to the newly
170 /// outlined function.
171 /// \param Outputs [out] - filled with values marked as outputs to the newly
172 /// outlined function.
173 /// \returns zero when called on a CodeExtractor instance where isEligible
174 /// returns false.
176 ValueSet &Inputs, ValueSet &Outputs);
177
178 /// Verify that assumption cache isn't stale after a region is extracted.
179 /// Returns true when verifier finds errors. AssumptionCache is passed as
180 /// parameter to make this function stateless.
181 static bool verifyAssumptionCache(const Function &OldFunc,
182 const Function &NewFunc,
183 AssumptionCache *AC);
184
185 /// Test whether this code extractor is eligible.
186 ///
187 /// Based on the blocks used when constructing the code extractor, determine
188 /// whether it is eligible for extraction.
189 ///
190 /// Checks that varargs handling (with vastart and vaend) is only done in the
191 /// outlined blocks.
192 bool isEligible() const;
193
194 /// Compute the set of input values and output values for the code.
195 ///
196 /// These can be used either when performing the extraction or to evaluate the
197 /// expected size of a call to the extracted function. Note that this work
198 /// cannot be cached between the two as once we decide to extract a code
199 /// sequence, that sequence is modified, including changing these sets, before
200 /// extraction occurs. These modifications won't have any significant impact
201 /// on the cost however.
202 void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
203 const ValueSet &Allocas,
204 bool CollectGlobalInputs = false) const;
205
206 /// Check if life time marker nodes can be hoisted/sunk into the outline
207 /// region.
208 ///
209 /// Returns true if it is safe to do the code motion.
210 bool
212 Instruction *AllocaAddr) const;
213
214 /// Find the set of allocas whose life ranges are contained within the
215 /// outlined region.
216 ///
217 /// Allocas which have life_time markers contained in the outlined region
218 /// should be pushed to the outlined function. The address bitcasts that are
219 /// used by the lifetime markers are also candidates for shrink-wrapping. The
220 /// instructions that need to be sunk are collected in 'Allocas'.
221 void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands,
222 ValueSet &HoistCands, BasicBlock *&ExitBlock) const;
223
224 /// Find or create a block within the outline region for placing hoisted code.
225 ///
226 /// CommonExitBlock is block outside the outline region. It is the common
227 /// successor of blocks inside the region. If there exists a single block
228 /// inside the region that is the predecessor of CommonExitBlock, that block
229 /// will be returned. Otherwise CommonExitBlock will be split and the original
230 /// block will be added to the outline region.
232
233 /// Exclude a value from aggregate argument passing when extracting a code
234 /// region, passing it instead as a scalar.
236
237private:
238 struct LifetimeMarkerInfo {
239 bool SinkLifeStart = false;
240 bool HoistLifeEnd = false;
241 Instruction *LifeStart = nullptr;
242 Instruction *LifeEnd = nullptr;
243 };
244
245 ValueSet ExcludeArgsFromAggregate;
246
247 LifetimeMarkerInfo getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
248 Instruction *Addr,
249 BasicBlock *ExitBlock) const;
250
251 /// Updates the list of SwitchCases (corresponding to exit blocks) after
252 /// changes of the control flow or the Blocks list.
253 void computeExtractedFuncRetVals();
254
255 /// Return the type used for the return code of the extracted function to
256 /// indicate which exit block to jump to.
257 Type *getSwitchType();
258
259 void severSplitPHINodesOfEntry(BasicBlock *&Header);
260 void severSplitPHINodesOfExits();
261 void splitReturnBlocks();
262
263 void moveCodeToFunction(Function *newFunction);
264
265 void calculateNewCallTerminatorWeights(
266 BasicBlock *CodeReplacer,
269
270 /// Normalizes the control flow of the extracted regions, such as ensuring
271 /// that the extracted region does not contain a return instruction.
272 void normalizeCFGForExtraction(BasicBlock *&header);
273
274 /// Generates the function declaration for the function containing the
275 /// extracted code.
276 Function *
277 constructFunctionDeclaration(const ValueSet &inputs, const ValueSet &outputs,
278 BlockFrequency EntryFreq, const Twine &Name,
279 ValueSet &StructValues, StructType *&StructTy);
280
281 /// Generates the code for the extracted function. That is: a prolog, the
282 /// moved or copied code from the original function, and epilogs for each
283 /// exit.
284 void emitFunctionBody(const ValueSet &inputs, const ValueSet &outputs,
285 const ValueSet &StructValues, Function *newFunction,
286 StructType *StructArgTy, BasicBlock *header,
287 const ValueSet &SinkingCands,
288 SmallVectorImpl<Value *> &NewValues);
289
290 /// Generates a Basic Block that calls the extracted function.
291 CallInst *emitReplacerCall(const ValueSet &inputs, const ValueSet &outputs,
292 const ValueSet &StructValues,
293 Function *newFunction, StructType *StructArgTy,
294 Function *oldFunction, BasicBlock *ReplIP,
295 BlockFrequency EntryFreq,
296 ArrayRef<Value *> LifetimesStart,
297 std::vector<Value *> &Reloads);
298
299 /// Connects the basic block containing the call to the extracted function
300 /// into the original function's control flow.
301 void
302 insertReplacerCall(Function *oldFunction, BasicBlock *header,
303 BasicBlock *codeReplacer, const ValueSet &outputs,
304 ArrayRef<Value *> Reloads,
305 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights);
306};
307
308} // end namespace llvm
309
310#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...
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)
Create a code extractor for a sequence of blocks.
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted 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.
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas, bool CollectGlobalInputs=false) const
Compute the set of input values and output values for the code.
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:164
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:45
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26