LLVM API Documentation

CodeExtractor.h
Go to the documentation of this file.
00001 //===-- Transform/Utils/CodeExtractor.h - Code extraction util --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // A utility to support extracting code from one function into its own
00011 // stand-alone function.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
00016 #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
00017 
00018 #include "llvm/ADT/ArrayRef.h"
00019 #include "llvm/ADT/SetVector.h"
00020 
00021 namespace llvm {
00022   class BasicBlock;
00023   class DominatorTree;
00024   class Function;
00025   class Loop;
00026   class Module;
00027   class RegionNode;
00028   class Type;
00029   class Value;
00030 
00031   /// \brief Utility class for extracting code into a new function.
00032   ///
00033   /// This utility provides a simple interface for extracting some sequence of
00034   /// code into its own function, replacing it with a call to that function. It
00035   /// also provides various methods to query about the nature and result of
00036   /// such a transformation.
00037   ///
00038   /// The rough algorithm used is:
00039   /// 1) Find both the inputs and outputs for the extracted region.
00040   /// 2) Pass the inputs as arguments, remapping them within the extracted
00041   ///    function to arguments.
00042   /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
00043   ///    as arguments, and inserting stores to the arguments for any scalars.
00044   class CodeExtractor {
00045     typedef SetVector<Value *> ValueSet;
00046 
00047     // Various bits of state computed on construction.
00048     DominatorTree *const DT;
00049     const bool AggregateArgs;
00050 
00051     // Bits of intermediate state computed at various phases of extraction.
00052     SetVector<BasicBlock *> Blocks;
00053     unsigned NumExitBlocks;
00054     Type *RetTy;
00055 
00056   public:
00057     /// \brief Create a code extractor for a single basic block.
00058     ///
00059     /// In this formation, we don't require a dominator tree. The given basic
00060     /// block is set up for extraction.
00061     CodeExtractor(BasicBlock *BB, bool AggregateArgs = false);
00062 
00063     /// \brief Create a code extractor for a sequence of blocks.
00064     ///
00065     /// Given a sequence of basic blocks where the first block in the sequence
00066     /// dominates the rest, prepare a code extractor object for pulling this
00067     /// sequence out into its new function. When a DominatorTree is also given,
00068     /// extra checking and transformations are enabled.
00069     CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
00070                   bool AggregateArgs = false);
00071 
00072     /// \brief Create a code extractor for a loop body.
00073     ///
00074     /// Behaves just like the generic code sequence constructor, but uses the
00075     /// block sequence of the loop.
00076     CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false);
00077 
00078     /// \brief Create a code extractor for a region node.
00079     ///
00080     /// Behaves just like the generic code sequence constructor, but uses the
00081     /// block sequence of the region node passed in.
00082     CodeExtractor(DominatorTree &DT, const RegionNode &RN,
00083                   bool AggregateArgs = false);
00084 
00085     /// \brief Perform the extraction, returning the new function.
00086     ///
00087     /// Returns zero when called on a CodeExtractor instance where isEligible
00088     /// returns false.
00089     Function *extractCodeRegion();
00090 
00091     /// \brief Test whether this code extractor is eligible.
00092     ///
00093     /// Based on the blocks used when constructing the code extractor,
00094     /// determine whether it is eligible for extraction.
00095     bool isEligible() const { return !Blocks.empty(); }
00096 
00097     /// \brief Compute the set of input values and output values for the code.
00098     ///
00099     /// These can be used either when performing the extraction or to evaluate
00100     /// the expected size of a call to the extracted function. Note that this
00101     /// work cannot be cached between the two as once we decide to extract
00102     /// a code sequence, that sequence is modified, including changing these
00103     /// sets, before extraction occurs. These modifications won't have any
00104     /// significant impact on the cost however.
00105     void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs) const;
00106 
00107   private:
00108     void severSplitPHINodes(BasicBlock *&Header);
00109     void splitReturnBlocks();
00110 
00111     Function *constructFunction(const ValueSet &inputs,
00112                                 const ValueSet &outputs,
00113                                 BasicBlock *header,
00114                                 BasicBlock *newRootNode, BasicBlock *newHeader,
00115                                 Function *oldFunction, Module *M);
00116 
00117     void moveCodeToFunction(Function *newFunction);
00118 
00119     void emitCallAndSwitchStatement(Function *newFunction,
00120                                     BasicBlock *newHeader,
00121                                     ValueSet &inputs,
00122                                     ValueSet &outputs);
00123   };
00124 }
00125 
00126 #endif