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_CODE_EXTRACTOR_H
16 #define LLVM_TRANSFORMS_UTILS_CODE_EXTRACTOR_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SetVector.h"
30 /// \brief Utility class for extracting code into a new function.
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.
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.
44 typedef SetVector<Value *> ValueSet;
46 // Various bits of state computed on construction.
47 DominatorTree *const DT;
48 const bool AggregateArgs;
50 // Bits of intermediate state computed at various phases of extraction.
51 SetVector<BasicBlock *> Blocks;
52 unsigned NumExitBlocks;
56 /// \brief Create a code extractor for a single basic block.
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);
62 /// \brief Create a code extractor for a sequence of blocks.
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);
71 /// \brief Create a code extractor for a loop body.
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);
77 /// \brief Perform the extraction, returning the new function.
79 /// Returns zero when called on a CodeExtractor instance where isEligible
81 Function *extractCodeRegion();
83 /// \brief Test whether this code extractor is eligible.
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(); };
90 void severSplitPHINodes(BasicBlock *&Header);
91 void splitReturnBlocks();
92 void findInputsOutputs(ValueSet &inputs, ValueSet &outputs);
94 Function *constructFunction(const ValueSet &inputs,
95 const ValueSet &outputs,
97 BasicBlock *newRootNode, BasicBlock *newHeader,
98 Function *oldFunction, Module *M);
100 void moveCodeToFunction(Function *newFunction);
102 void emitCallAndSwitchStatement(Function *newFunction,
103 BasicBlock *newHeader,