LLVM  8.0.0svn
CodeExtractor.h
Go to the documentation of this file.
1 //===- Transform/Utils/CodeExtractor.h - Code extraction util ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // A utility to support extracting code from one function into its own
11 // stand-alone function.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
16 #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include <limits>
23 
24 namespace llvm {
25 
26 class BasicBlock;
27 class BlockFrequency;
28 class BlockFrequencyInfo;
29 class BranchProbabilityInfo;
30 class DominatorTree;
31 class Function;
32 class Instruction;
33 class Loop;
34 class Module;
35 class Type;
36 class Value;
37 
38  /// Utility class for extracting code into a new function.
39  ///
40  /// This utility provides a simple interface for extracting some sequence of
41  /// code into its own function, replacing it with a call to that function. It
42  /// also provides various methods to query about the nature and result of
43  /// such a transformation.
44  ///
45  /// The rough algorithm used is:
46  /// 1) Find both the inputs and outputs for the extracted region.
47  /// 2) Pass the inputs as arguments, remapping them within the extracted
48  /// function to arguments.
49  /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
50  /// as arguments, and inserting stores to the arguments for any scalars.
51  class CodeExtractor {
53 
54  // Various bits of state computed on construction.
55  DominatorTree *const DT;
56  const bool AggregateArgs;
57  BlockFrequencyInfo *BFI;
59 
60  // If true, varargs functions can be extracted.
61  bool AllowVarArgs;
62 
63  // Bits of intermediate state computed at various phases of extraction.
65  unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
66  Type *RetTy;
67 
68  // Suffix to use when creating extracted function (appended to the original
69  // function name + "."). If empty, the default is to use the entry block
70  // label, if non-empty, otherwise "extracted".
71  std::string Suffix;
72 
73  public:
74  /// Create a code extractor for a sequence of blocks.
75  ///
76  /// Given a sequence of basic blocks where the first block in the sequence
77  /// dominates the rest, prepare a code extractor object for pulling this
78  /// sequence out into its new function. When a DominatorTree is also given,
79  /// extra checking and transformations are enabled. If AllowVarArgs is true,
80  /// vararg functions can be extracted. This is safe, if all vararg handling
81  /// code is extracted, including vastart. If AllowAlloca is true, then
82  /// extraction of blocks containing alloca instructions would be possible,
83  /// however code extractor won't validate whether extraction is legal.
85  bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
86  BranchProbabilityInfo *BPI = nullptr,
87  bool AllowVarArgs = false, bool AllowAlloca = false,
88  std::string Suffix = "");
89 
90  /// Create a code extractor for a loop body.
91  ///
92  /// Behaves just like the generic code sequence constructor, but uses the
93  /// block sequence of the loop.
94  CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
95  BlockFrequencyInfo *BFI = nullptr,
96  BranchProbabilityInfo *BPI = nullptr,
97  std::string Suffix = "");
98 
99  /// Perform the extraction, returning the new function.
100  ///
101  /// Returns zero when called on a CodeExtractor instance where isEligible
102  /// returns false.
104 
105  /// Test whether this code extractor is eligible.
106  ///
107  /// Based on the blocks used when constructing the code extractor,
108  /// determine whether it is eligible for extraction.
109  bool isEligible() const { return !Blocks.empty(); }
110 
111  /// Compute the set of input values and output values for the code.
112  ///
113  /// These can be used either when performing the extraction or to evaluate
114  /// the expected size of a call to the extracted function. Note that this
115  /// work cannot be cached between the two as once we decide to extract
116  /// a code sequence, that sequence is modified, including changing these
117  /// sets, before extraction occurs. These modifications won't have any
118  /// significant impact on the cost however.
119  void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
120  const ValueSet &Allocas) const;
121 
122  /// Check if life time marker nodes can be hoisted/sunk into the outline
123  /// region.
124  ///
125  /// Returns true if it is safe to do the code motion.
126  bool isLegalToShrinkwrapLifetimeMarkers(Instruction *AllocaAddr) const;
127 
128  /// Find the set of allocas whose life ranges are contained within the
129  /// outlined region.
130  ///
131  /// Allocas which have life_time markers contained in the outlined region
132  /// should be pushed to the outlined function. The address bitcasts that
133  /// are used by the lifetime markers are also candidates for shrink-
134  /// wrapping. The instructions that need to be sunk are collected in
135  /// 'Allocas'.
136  void findAllocas(ValueSet &SinkCands, ValueSet &HoistCands,
137  BasicBlock *&ExitBlock) const;
138 
139  /// Find or create a block within the outline region for placing hoisted
140  /// code.
141  ///
142  /// CommonExitBlock is block outside the outline region. It is the common
143  /// successor of blocks inside the region. If there exists a single block
144  /// inside the region that is the predecessor of CommonExitBlock, that block
145  /// will be returned. Otherwise CommonExitBlock will be split and the
146  /// original block will be added to the outline region.
148 
149  private:
150  void severSplitPHINodesOfEntry(BasicBlock *&Header);
151  void severSplitPHINodesOfExits(const SmallPtrSetImpl<BasicBlock *> &Exits);
152  void splitReturnBlocks();
153 
154  Function *constructFunction(const ValueSet &inputs,
155  const ValueSet &outputs,
156  BasicBlock *header,
157  BasicBlock *newRootNode, BasicBlock *newHeader,
158  Function *oldFunction, Module *M);
159 
160  void moveCodeToFunction(Function *newFunction);
161 
162  void calculateNewCallTerminatorWeights(
163  BasicBlock *CodeReplacer,
165  BranchProbabilityInfo *BPI);
166 
167  void emitCallAndSwitchStatement(Function *newFunction,
168  BasicBlock *newHeader,
169  ValueSet &inputs,
170  ValueSet &outputs);
171  };
172 
173 } // end namespace llvm
174 
175 #endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:51
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:49
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Various leaf nodes.
Definition: ISDOpcodes.h:60
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas) const
Compute the set of input values and output values for the code.
CodeExtractor(ArrayRef< BasicBlock *> BBs, DominatorTree *DT=nullptr, bool AggregateArgs=false, BlockFrequencyInfo *BFI=nullptr, BranchProbabilityInfo *BPI=nullptr, bool AllowVarArgs=false, bool AllowAlloca=false, std::string Suffix="")
Create a code extractor for a sequence of blocks.
Function * extractCodeRegion()
Perform the extraction, returning the new function.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool isEligible() const
Test whether this code extractor is eligible.
bool isLegalToShrinkwrapLifetimeMarkers(Instruction *AllocaAddr) const
Check if life time marker nodes can be hoisted/sunk into the outline region.
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted code.
Analysis providing branch probability information.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:459
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:73
A vector that has set insertion semantics.
Definition: SetVector.h:41
void findAllocas(ValueSet &SinkCands, ValueSet &HoistCands, BasicBlock *&ExitBlock) const
Find the set of allocas whose life ranges are contained within the outlined region.