Move the CodeExtractor utility to a dedicated header file / source file,
[oota-llvm.git] / include / llvm / Transforms / Utils / CodeExtractor.h
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_CODE_EXTRACTOR_H
16 #define LLVM_TRANSFORMS_UTILS_CODE_EXTRACTOR_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SetVector.h"
20
21 namespace llvm {
22   class BasicBlock;
23   class DominatorTree;
24   class Function;
25   class Loop;
26   class Module;
27   class Type;
28   class Value;
29
30   /// \brief Utility class for extracting code into a new function.
31   ///
32   /// This utility provides a simple interface for extracting some sequence of
33   /// code into its own function, replacing it with a call to that function. It
34   /// also provides various methods to query about the nature and result of
35   /// such a transformation.
36   ///
37   /// The rough algorithm used is:
38   /// 1) Find both the inputs and outputs for the extracted region.
39   /// 2) Pass the inputs as arguments, remapping them within the extracted
40   ///    function to arguments.
41   /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
42   ///    as arguments, and inserting stores to the arguments for any scalars.
43   class CodeExtractor {
44     typedef SetVector<Value *> ValueSet;
45
46     // Various bits of state computed on construction.
47     DominatorTree *const DT;
48     const bool AggregateArgs;
49
50     // Bits of intermediate state computed at various phases of extraction.
51     SetVector<BasicBlock *> Blocks;
52     unsigned NumExitBlocks;
53     Type *RetTy;
54
55   public:
56     /// \brief Create a code extractor for a single basic block.
57     ///
58     /// In this formation, we don't require a dominator tree. The given basic
59     /// block is set up for extraction.
60     CodeExtractor(BasicBlock *BB, bool AggregateArgs = false);
61
62     /// \brief Create a code extractor for a sequence of blocks.
63     ///
64     /// Given a sequence of basic blocks where the first block in the sequence
65     /// dominates the rest, prepare a code extractor object for pulling this
66     /// sequence out into its new function. When a DominatorTree is also given,
67     /// extra checking and transformations are enabled.
68     CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = 0,
69                   bool AggregateArgs = false);
70
71     /// \brief Create a code extractor for a loop body.
72     ///
73     /// Behaves just like the generic code sequence constructor, but uses the
74     /// block sequence of the loop.
75     CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false);
76
77     /// \brief Perform the extraction, returning the new function.
78     ///
79     /// Returns zero when called on a CodeExtractor instance where isEligible
80     /// returns false.
81     Function *extractCodeRegion();
82
83     /// \brief Test whether this code extractor is eligible.
84     ///
85     /// Based on the blocks used when constructing the code extractor,
86     /// determine whether it is eligible for extraction.
87     bool isEligible() { return !Blocks.empty(); };
88
89   private:
90     void severSplitPHINodes(BasicBlock *&Header);
91     void splitReturnBlocks();
92     void findInputsOutputs(ValueSet &inputs, ValueSet &outputs);
93
94     Function *constructFunction(const ValueSet &inputs,
95                                 const ValueSet &outputs,
96                                 BasicBlock *header,
97                                 BasicBlock *newRootNode, BasicBlock *newHeader,
98                                 Function *oldFunction, Module *M);
99
100     void moveCodeToFunction(Function *newFunction);
101
102     void emitCallAndSwitchStatement(Function *newFunction,
103                                     BasicBlock *newHeader,
104                                     ValueSet &inputs,
105                                     ValueSet &outputs);
106
107   };
108 }
109
110 #endif