1 //===-- Transform/Utils/CodeExtractor.h - Code extraction util --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A utility to support extracting code from one function into its own
11 // stand-alone function.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
16 #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
18 #include "llvm/ADT/SetVector.h"
21 template <typename T> class ArrayRef;
24 class BlockFrequencyInfo;
25 class BranchProbabilityInfo;
35 /// \brief Utility class for extracting code into a new function.
37 /// This utility provides a simple interface for extracting some sequence of
38 /// code into its own function, replacing it with a call to that function. It
39 /// also provides various methods to query about the nature and result of
40 /// such a transformation.
42 /// The rough algorithm used is:
43 /// 1) Find both the inputs and outputs for the extracted region.
44 /// 2) Pass the inputs as arguments, remapping them within the extracted
45 /// function to arguments.
46 /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
47 /// as arguments, and inserting stores to the arguments for any scalars.
49 typedef SetVector<Value *> ValueSet;
51 // Various bits of state computed on construction.
52 DominatorTree *const DT;
53 const bool AggregateArgs;
54 BlockFrequencyInfo *BFI;
55 BranchProbabilityInfo *BPI;
57 // Bits of intermediate state computed at various phases of extraction.
58 SetVector<BasicBlock *> Blocks;
59 unsigned NumExitBlocks;
64 /// \brief Check to see if a block is valid for extraction.
66 /// Blocks containing EHPads, allocas, invokes, or vastarts are not valid.
67 static bool isBlockValidForExtraction(const BasicBlock &BB);
69 /// \brief Create a code extractor for a sequence of blocks.
71 /// Given a sequence of basic blocks where the first block in the sequence
72 /// dominates the rest, prepare a code extractor object for pulling this
73 /// sequence out into its new function. When a DominatorTree is also given,
74 /// extra checking and transformations are enabled.
75 CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
76 bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
77 BranchProbabilityInfo *BPI = nullptr);
79 /// \brief Create a code extractor for a loop body.
81 /// Behaves just like the generic code sequence constructor, but uses the
82 /// block sequence of the loop.
83 CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
84 BlockFrequencyInfo *BFI = nullptr,
85 BranchProbabilityInfo *BPI = nullptr);
87 /// \brief Perform the extraction, returning the new function.
89 /// Returns zero when called on a CodeExtractor instance where isEligible
91 Function *extractCodeRegion();
93 /// \brief Test whether this code extractor is eligible.
95 /// Based on the blocks used when constructing the code extractor,
96 /// determine whether it is eligible for extraction.
97 bool isEligible() const { return !Blocks.empty(); }
99 /// \brief Compute the set of input values and output values for the code.
101 /// These can be used either when performing the extraction or to evaluate
102 /// the expected size of a call to the extracted function. Note that this
103 /// work cannot be cached between the two as once we decide to extract
104 /// a code sequence, that sequence is modified, including changing these
105 /// sets, before extraction occurs. These modifications won't have any
106 /// significant impact on the cost however.
107 void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
108 const ValueSet &Allocas) const;
109 /// Find the set of allocas whose life ranges are contained within the
112 /// Allocas which have life_time markers contained in the outlined region
113 /// should be pushed to the outlined function. The address bitcasts that
114 /// are used by the lifetime markers are also candidates for shrink-
115 /// wrapping. The instructions that need to be sinked are collected in
117 void findAllocas(ValueSet &Allocas) const;
120 void severSplitPHINodes(BasicBlock *&Header);
121 void splitReturnBlocks();
123 Function *constructFunction(const ValueSet &inputs,
124 const ValueSet &outputs,
126 BasicBlock *newRootNode, BasicBlock *newHeader,
127 Function *oldFunction, Module *M);
129 void moveCodeToFunction(Function *newFunction);
131 void calculateNewCallTerminatorWeights(
132 BasicBlock *CodeReplacer,
133 DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
134 BranchProbabilityInfo *BPI);
136 void emitCallAndSwitchStatement(Function *newFunction,
137 BasicBlock *newHeader,