LLVM  4.0.0
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/SetVector.h"
19 
20 namespace llvm {
21 template <typename T> class ArrayRef;
22  class BasicBlock;
23  class BlockFrequency;
24  class BlockFrequencyInfo;
25  class BranchProbabilityInfo;
26  class DominatorTree;
27  class Function;
28  class Loop;
29  class Module;
30  class RegionNode;
31  class Type;
32  class Value;
33 
34  /// \brief Utility class for extracting code into a new function.
35  ///
36  /// This utility provides a simple interface for extracting some sequence of
37  /// code into its own function, replacing it with a call to that function. It
38  /// also provides various methods to query about the nature and result of
39  /// such a transformation.
40  ///
41  /// The rough algorithm used is:
42  /// 1) Find both the inputs and outputs for the extracted region.
43  /// 2) Pass the inputs as arguments, remapping them within the extracted
44  /// function to arguments.
45  /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
46  /// as arguments, and inserting stores to the arguments for any scalars.
47  class CodeExtractor {
49 
50  // Various bits of state computed on construction.
51  DominatorTree *const DT;
52  const bool AggregateArgs;
53  BlockFrequencyInfo *BFI;
55 
56  // Bits of intermediate state computed at various phases of extraction.
58  unsigned NumExitBlocks;
59  Type *RetTy;
60 
61  public:
62 
63  /// \brief Check to see if a block is valid for extraction.
64  ///
65  /// Blocks containing EHPads, allocas, invokes, or vastarts are not valid.
66  static bool isBlockValidForExtraction(const BasicBlock &BB);
67 
68  /// \brief Create a code extractor for a single basic block.
69  ///
70  /// In this formation, we don't require a dominator tree. The given basic
71  /// block is set up for extraction.
72  CodeExtractor(BasicBlock *BB, bool AggregateArgs = false,
73  BlockFrequencyInfo *BFI = nullptr,
74  BranchProbabilityInfo *BPI = nullptr);
75 
76  /// \brief Create a code extractor for a sequence of blocks.
77  ///
78  /// Given a sequence of basic blocks where the first block in the sequence
79  /// dominates the rest, prepare a code extractor object for pulling this
80  /// sequence out into its new function. When a DominatorTree is also given,
81  /// extra checking and transformations are enabled.
83  bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
84  BranchProbabilityInfo *BPI = nullptr);
85 
86  /// \brief Create a code extractor for a loop body.
87  ///
88  /// Behaves just like the generic code sequence constructor, but uses the
89  /// block sequence of the loop.
90  CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
91  BlockFrequencyInfo *BFI = nullptr,
92  BranchProbabilityInfo *BPI = nullptr);
93 
94  /// \brief Create a code extractor for a region node.
95  ///
96  /// Behaves just like the generic code sequence constructor, but uses the
97  /// block sequence of the region node passed in.
99  bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
100  BranchProbabilityInfo *BPI = nullptr);
101 
102  /// \brief Perform the extraction, returning the new function.
103  ///
104  /// Returns zero when called on a CodeExtractor instance where isEligible
105  /// returns false.
107 
108  /// \brief Test whether this code extractor is eligible.
109  ///
110  /// Based on the blocks used when constructing the code extractor,
111  /// determine whether it is eligible for extraction.
112  bool isEligible() const { return !Blocks.empty(); }
113 
114  /// \brief Compute the set of input values and output values for the code.
115  ///
116  /// These can be used either when performing the extraction or to evaluate
117  /// the expected size of a call to the extracted function. Note that this
118  /// work cannot be cached between the two as once we decide to extract
119  /// a code sequence, that sequence is modified, including changing these
120  /// sets, before extraction occurs. These modifications won't have any
121  /// significant impact on the cost however.
122  void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs) const;
123 
124  private:
125  void severSplitPHINodes(BasicBlock *&Header);
126  void splitReturnBlocks();
127 
128  Function *constructFunction(const ValueSet &inputs,
129  const ValueSet &outputs,
130  BasicBlock *header,
131  BasicBlock *newRootNode, BasicBlock *newHeader,
132  Function *oldFunction, Module *M);
133 
134  void moveCodeToFunction(Function *newFunction);
135 
136  void calculateNewCallTerminatorWeights(
137  BasicBlock *CodeReplacer,
139  BranchProbabilityInfo *BPI);
140 
141  void emitCallAndSwitchStatement(Function *newFunction,
142  BasicBlock *newHeader,
143  ValueSet &inputs,
144  ValueSet &outputs);
145  };
146 }
147 
148 #endif
MachineLoop * L
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:47
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:52
Function * extractCodeRegion()
Perform the extraction, returning the new function.
static bool isBlockValidForExtraction(const BasicBlock &BB)
Check to see if a block is valid for extraction.
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:96
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs) const
Compute the set of input values and output values for the code.
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
CodeExtractor(BasicBlock *BB, bool AggregateArgs=false, BlockFrequencyInfo *BFI=nullptr, BranchProbabilityInfo *BPI=nullptr)
Create a code extractor for a single basic block.
bool isEligible() const
Test whether this code extractor is eligible.
Analysis providing branch probability information.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368