LLVM  14.0.0git
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 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include <limits>
23 namespace llvm {
25 class AllocaInst;
26 class BasicBlock;
27 class BlockFrequency;
28 class BlockFrequencyInfo;
29 class BranchProbabilityInfo;
30 class AssumptionCache;
31 class CallInst;
32 class DominatorTree;
33 class Function;
34 class Instruction;
35 class Loop;
36 class Module;
37 class Type;
38 class Value;
40 /// A cache for the CodeExtractor analysis. The operation \ref
41 /// CodeExtractor::extractCodeRegion is guaranteed not to invalidate this
42 /// object. This object should conservatively be considered invalid if any
43 /// other mutating operations on the IR occur.
44 ///
45 /// Constructing this object is O(n) in the size of the function.
47  /// The allocas in the function.
50  /// Base memory addresses of load/store instructions, grouped by block.
53  /// Blocks which contain instructions which may have unknown side-effects
54  /// on memory.
55  DenseSet<BasicBlock *> SideEffectingBlocks;
57  void findSideEffectInfoForBlock(BasicBlock &BB);
59 public:
62  /// Get the allocas in the function at the time the analysis was created.
63  /// Note that some of these allocas may no longer be present in the function,
64  /// due to \ref CodeExtractor::extractCodeRegion.
65  ArrayRef<AllocaInst *> getAllocas() const { return Allocas; }
67  /// Check whether \p BB contains an instruction thought to load from, store
68  /// to, or otherwise clobber the alloca \p Addr.
70 };
72  /// Utility class for extracting code into a new function.
73  ///
74  /// This utility provides a simple interface for extracting some sequence of
75  /// code into its own function, replacing it with a call to that function. It
76  /// also provides various methods to query about the nature and result of
77  /// such a transformation.
78  ///
79  /// The rough algorithm used is:
80  /// 1) Find both the inputs and outputs for the extracted region.
81  /// 2) Pass the inputs as arguments, remapping them within the extracted
82  /// function to arguments.
83  /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
84  /// as arguments, and inserting stores to the arguments for any scalars.
85  class CodeExtractor {
88  // Various bits of state computed on construction.
89  DominatorTree *const DT;
90  const bool AggregateArgs;
91  BlockFrequencyInfo *BFI;
93  AssumptionCache *AC;
95  // If true, varargs functions can be extracted.
96  bool AllowVarArgs;
98  // Bits of intermediate state computed at various phases of extraction.
100  unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
101  Type *RetTy;
103  // Mapping from the original exit blocks, to the new blocks inside
104  // the function.
105  SmallVector<BasicBlock *, 4> OldTargets;
107  // Suffix to use when creating extracted function (appended to the original
108  // function name + "."). If empty, the default is to use the entry block
109  // label, if non-empty, otherwise "extracted".
110  std::string Suffix;
112  public:
113  /// Create a code extractor for a sequence of blocks.
114  ///
115  /// Given a sequence of basic blocks where the first block in the sequence
116  /// dominates the rest, prepare a code extractor object for pulling this
117  /// sequence out into its new function. When a DominatorTree is also given,
118  /// extra checking and transformations are enabled. If AllowVarArgs is true,
119  /// vararg functions can be extracted. This is safe, if all vararg handling
120  /// code is extracted, including vastart. If AllowAlloca is true, then
121  /// extraction of blocks containing alloca instructions would be possible,
122  /// however code extractor won't validate whether extraction is legal.
124  bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
125  BranchProbabilityInfo *BPI = nullptr,
126  AssumptionCache *AC = nullptr,
127  bool AllowVarArgs = false, bool AllowAlloca = false,
128  std::string Suffix = "");
130  /// Create a code extractor for a loop body.
131  ///
132  /// Behaves just like the generic code sequence constructor, but uses the
133  /// block sequence of the loop.
134  CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
135  BlockFrequencyInfo *BFI = nullptr,
136  BranchProbabilityInfo *BPI = nullptr,
137  AssumptionCache *AC = nullptr,
138  std::string Suffix = "");
140  /// Perform the extraction, returning the new function.
141  ///
142  /// Returns zero when called on a CodeExtractor instance where isEligible
143  /// returns false.
146  /// Perform the extraction, returning the new function and providing an
147  /// interface to see what was categorized as inputs and outputs.
148  ///
149  /// \param CEAC - Cache to speed up operations for the CodeExtractor when
150  /// hoisting, and extracting lifetime values and assumes.
151  /// \param Inputs [out] - filled with values marked as inputs to the
152  /// newly outlined function.
153  /// \param Outputs [out] - filled with values marked as outputs to the
154  /// newly outlined function.
155  /// \returns zero when called on a CodeExtractor instance where isEligible
156  /// returns false.
158  ValueSet &Inputs, ValueSet &Outputs);
160  /// Verify that assumption cache isn't stale after a region is extracted.
161  /// Returns true when verifier finds errors. AssumptionCache is passed as
162  /// parameter to make this function stateless.
163  static bool verifyAssumptionCache(const Function &OldFunc,
164  const Function &NewFunc,
165  AssumptionCache *AC);
167  /// Test whether this code extractor is eligible.
168  ///
169  /// Based on the blocks used when constructing the code extractor,
170  /// determine whether it is eligible for extraction.
171  ///
172  /// Checks that varargs handling (with vastart and vaend) is only done in
173  /// the outlined blocks.
174  bool isEligible() const;
176  /// Compute the set of input values and output values for the code.
177  ///
178  /// These can be used either when performing the extraction or to evaluate
179  /// the expected size of a call to the extracted function. Note that this
180  /// work cannot be cached between the two as once we decide to extract
181  /// a code sequence, that sequence is modified, including changing these
182  /// sets, before extraction occurs. These modifications won't have any
183  /// significant impact on the cost however.
184  void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
185  const ValueSet &Allocas) const;
187  /// Check if life time marker nodes can be hoisted/sunk into the outline
188  /// region.
189  ///
190  /// Returns true if it is safe to do the code motion.
191  bool
193  Instruction *AllocaAddr) const;
195  /// Find the set of allocas whose life ranges are contained within the
196  /// outlined region.
197  ///
198  /// Allocas which have life_time markers contained in the outlined region
199  /// should be pushed to the outlined function. The address bitcasts that
200  /// are used by the lifetime markers are also candidates for shrink-
201  /// wrapping. The instructions that need to be sunk are collected in
202  /// 'Allocas'.
203  void findAllocas(const CodeExtractorAnalysisCache &CEAC,
204  ValueSet &SinkCands, ValueSet &HoistCands,
205  BasicBlock *&ExitBlock) const;
207  /// Find or create a block within the outline region for placing hoisted
208  /// code.
209  ///
210  /// CommonExitBlock is block outside the outline region. It is the common
211  /// successor of blocks inside the region. If there exists a single block
212  /// inside the region that is the predecessor of CommonExitBlock, that block
213  /// will be returned. Otherwise CommonExitBlock will be split and the
214  /// original block will be added to the outline region.
217  private:
218  struct LifetimeMarkerInfo {
219  bool SinkLifeStart = false;
220  bool HoistLifeEnd = false;
221  Instruction *LifeStart = nullptr;
222  Instruction *LifeEnd = nullptr;
223  };
225  LifetimeMarkerInfo
226  getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
227  Instruction *Addr, BasicBlock *ExitBlock) const;
229  void severSplitPHINodesOfEntry(BasicBlock *&Header);
230  void severSplitPHINodesOfExits(const SmallPtrSetImpl<BasicBlock *> &Exits);
231  void splitReturnBlocks();
233  Function *constructFunction(const ValueSet &inputs,
234  const ValueSet &outputs,
235  BasicBlock *header,
236  BasicBlock *newRootNode, BasicBlock *newHeader,
237  Function *oldFunction, Module *M);
239  void moveCodeToFunction(Function *newFunction);
241  void calculateNewCallTerminatorWeights(
242  BasicBlock *CodeReplacer,
244  BranchProbabilityInfo *BPI);
246  CallInst *emitCallAndSwitchStatement(Function *newFunction,
247  BasicBlock *newHeader,
248  ValueSet &inputs, ValueSet &outputs);
249  };
251 } // end namespace llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const
Check whether BB contains an instruction thought to load from, store to, or otherwise clobber the all...
Definition: CodeExtractor.cpp:364
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.
Definition: CodeExtractor.cpp:495
Definition: Function.h:62
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:530
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
Definition: CodeExtractor.cpp:1584
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1177
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
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, std::string Suffix="")
Create a code extractor for a sequence of blocks.
Definition: CodeExtractor.cpp:247
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
ArrayRef< AllocaInst * > getAllocas() const
Get the allocas in the function at the time the analysis was created.
Definition: CodeExtractor.h:65
#define F(x, y, z)
Definition: MD5.cpp:55
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
CodeExtractorAnalysisCache(Function &F)
Definition: CodeExtractor.cpp:311
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
Analysis providing branch probability information.
Definition: BranchProbabilityInfo.h:115
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas) const
Compute the set of input values and output values for the code.
Definition: CodeExtractor.cpp:647
Definition: Instruction.h:45
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
uint64_t Addr
Definition: ELFObjHandler.cpp:79
Definition: DenseMap.h:714
@ Type
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:42
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:85
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted code.
Definition: CodeExtractor.cpp:388
Definition: TargetFrameLowering.h:27
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
Definition: CodeExtractor.cpp:1810
A cache for the CodeExtractor analysis.
Definition: CodeExtractor.h:46
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
bool isEligible() const
Test whether this code extractor is eligible.
Definition: CodeExtractor.cpp:620
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1478
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
bool isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, Instruction *AllocaAddr) const
Check if life time marker nodes can be hoisted/sunk into the outline region.
Definition: CodeExtractor.cpp:374
an instruction to allocate memory on the stack
Definition: Instructions.h:62
llvm::SetVector< Value * >
@ Function