LCOV - code coverage report
Current view: top level - include/llvm/Transforms/Utils - CodeExtractor.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2 2 100.0 %
Date: 2018-02-19 03:08:00 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       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/ArrayRef.h"
      19             : #include "llvm/ADT/DenseMap.h"
      20             : #include "llvm/ADT/SetVector.h"
      21             : #include <limits>
      22             : 
      23             : namespace llvm {
      24             : 
      25             : class BasicBlock;
      26             : class BlockFrequency;
      27             : class BlockFrequencyInfo;
      28             : class BranchProbabilityInfo;
      29             : class DominatorTree;
      30             : class Function;
      31             : class Instruction;
      32             : class Loop;
      33             : class Module;
      34             : class Type;
      35             : class Value;
      36             : 
      37             :   /// \brief Utility class for extracting code into a new function.
      38             :   ///
      39             :   /// This utility provides a simple interface for extracting some sequence of
      40             :   /// code into its own function, replacing it with a call to that function. It
      41             :   /// also provides various methods to query about the nature and result of
      42             :   /// such a transformation.
      43             :   ///
      44             :   /// The rough algorithm used is:
      45             :   /// 1) Find both the inputs and outputs for the extracted region.
      46             :   /// 2) Pass the inputs as arguments, remapping them within the extracted
      47             :   ///    function to arguments.
      48             :   /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas
      49             :   ///    as arguments, and inserting stores to the arguments for any scalars.
      50         106 :   class CodeExtractor {
      51             :     using ValueSet = SetVector<Value *>;
      52             : 
      53             :     // Various bits of state computed on construction.
      54             :     DominatorTree *const DT;
      55             :     const bool AggregateArgs;
      56             :     BlockFrequencyInfo *BFI;
      57             :     BranchProbabilityInfo *BPI;
      58             : 
      59             :     // If true, varargs functions can be extracted.
      60             :     bool AllowVarArgs;
      61             : 
      62             :     // Bits of intermediate state computed at various phases of extraction.
      63             :     SetVector<BasicBlock *> Blocks;
      64             :     unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
      65             :     Type *RetTy;
      66             : 
      67             :   public:
      68             :     /// \brief Create a code extractor for a sequence of blocks.
      69             :     ///
      70             :     /// Given a sequence of basic blocks where the first block in the sequence
      71             :     /// dominates the rest, prepare a code extractor object for pulling this
      72             :     /// sequence out into its new function. When a DominatorTree is also given,
      73             :     /// extra checking and transformations are enabled. If AllowVarArgs is true,
      74             :     /// vararg functions can be extracted. This is safe, if all vararg handling
      75             :     /// code is extracted, including vastart.
      76             :     CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr,
      77             :                   bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr,
      78             :                   BranchProbabilityInfo *BPI = nullptr,
      79             :                   bool AllowVarArgs = false);
      80             : 
      81             :     /// \brief Create a code extractor for a loop body.
      82             :     ///
      83             :     /// Behaves just like the generic code sequence constructor, but uses the
      84             :     /// block sequence of the loop.
      85             :     CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs = false,
      86             :                   BlockFrequencyInfo *BFI = nullptr,
      87             :                   BranchProbabilityInfo *BPI = nullptr);
      88             : 
      89             :     /// \brief Check to see if a block is valid for extraction.
      90             :     ///
      91             :     /// Blocks containing EHPads, allocas and invokes are not valid. If
      92             :     /// AllowVarArgs is true, blocks with vastart can be extracted. This is
      93             :     /// safe, if all vararg handling code is extracted, including vastart.
      94             :     static bool isBlockValidForExtraction(const BasicBlock &BB,
      95             :                                           bool AllowVarArgs);
      96             : 
      97             :     /// \brief Perform the extraction, returning the new function.
      98             :     ///
      99             :     /// Returns zero when called on a CodeExtractor instance where isEligible
     100             :     /// returns false.
     101             :     Function *extractCodeRegion();
     102             : 
     103             :     /// \brief Test whether this code extractor is eligible.
     104             :     ///
     105             :     /// Based on the blocks used when constructing the code extractor,
     106             :     /// determine whether it is eligible for extraction.
     107           1 :     bool isEligible() const { return !Blocks.empty(); }
     108             : 
     109             :     /// \brief Compute the set of input values and output values for the code.
     110             :     ///
     111             :     /// These can be used either when performing the extraction or to evaluate
     112             :     /// the expected size of a call to the extracted function. Note that this
     113             :     /// work cannot be cached between the two as once we decide to extract
     114             :     /// a code sequence, that sequence is modified, including changing these
     115             :     /// sets, before extraction occurs. These modifications won't have any
     116             :     /// significant impact on the cost however.
     117             :     void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
     118             :                            const ValueSet &Allocas) const;
     119             : 
     120             :     /// Check if life time marker nodes can be hoisted/sunk into the outline
     121             :     /// region.
     122             :     ///
     123             :     /// Returns true if it is safe to do the code motion.
     124             :     bool isLegalToShrinkwrapLifetimeMarkers(Instruction *AllocaAddr) const;
     125             : 
     126             :     /// Find the set of allocas whose life ranges are contained within the
     127             :     /// outlined region.
     128             :     ///
     129             :     /// Allocas which have life_time markers contained in the outlined region
     130             :     /// should be pushed to the outlined function. The address bitcasts that
     131             :     /// are used by the lifetime markers are also candidates for shrink-
     132             :     /// wrapping. The instructions that need to be sunk are collected in
     133             :     /// 'Allocas'.
     134             :     void findAllocas(ValueSet &SinkCands, ValueSet &HoistCands,
     135             :                      BasicBlock *&ExitBlock) const;
     136             : 
     137             :     /// Find or create a block within the outline region for placing hoisted
     138             :     /// code.
     139             :     ///
     140             :     /// CommonExitBlock is block outside the outline region. It is the common
     141             :     /// successor of blocks inside the region. If there exists a single block
     142             :     /// inside the region that is the predecessor of CommonExitBlock, that block
     143             :     /// will be returned. Otherwise CommonExitBlock will be split and the
     144             :     /// original block will be added to the outline region.
     145             :     BasicBlock *findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock);
     146             : 
     147             :   private:
     148             :     void severSplitPHINodes(BasicBlock *&Header);
     149             :     void splitReturnBlocks();
     150             : 
     151             :     Function *constructFunction(const ValueSet &inputs,
     152             :                                 const ValueSet &outputs,
     153             :                                 BasicBlock *header,
     154             :                                 BasicBlock *newRootNode, BasicBlock *newHeader,
     155             :                                 Function *oldFunction, Module *M);
     156             : 
     157             :     void moveCodeToFunction(Function *newFunction);
     158             : 
     159             :     void calculateNewCallTerminatorWeights(
     160             :         BasicBlock *CodeReplacer,
     161             :         DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
     162             :         BranchProbabilityInfo *BPI);
     163             : 
     164             :     void emitCallAndSwitchStatement(Function *newFunction,
     165             :                                     BasicBlock *newHeader,
     166             :                                     ValueSet &inputs,
     167             :                                     ValueSet &outputs);
     168             :   };
     169             : 
     170             : } // end namespace llvm
     171             : 
     172             : #endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H

Generated by: LCOV version 1.13