LCOV - code coverage report
Current view: top level - include/llvm/Transforms/Scalar - ConstantHoisting.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 14 14 100.0 %
Date: 2017-09-14 15:23:50 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ConstantHoisting.h - Prepare code for expensive constants ---------===//
       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             : // This pass identifies expensive constants to hoist and coalesces them to
      11             : // better prepare it for SelectionDAG-based code generation. This works around
      12             : // the limitations of the basic-block-at-a-time approach.
      13             : //
      14             : // First it scans all instructions for integer constants and calculates its
      15             : // cost. If the constant can be folded into the instruction (the cost is
      16             : // TCC_Free) or the cost is just a simple operation (TCC_BASIC), then we don't
      17             : // consider it expensive and leave it alone. This is the default behavior and
      18             : // the default implementation of getIntImmCost will always return TCC_Free.
      19             : //
      20             : // If the cost is more than TCC_BASIC, then the integer constant can't be folded
      21             : // into the instruction and it might be beneficial to hoist the constant.
      22             : // Similar constants are coalesced to reduce register pressure and
      23             : // materialization code.
      24             : //
      25             : // When a constant is hoisted, it is also hidden behind a bitcast to force it to
      26             : // be live-out of the basic block. Otherwise the constant would be just
      27             : // duplicated and each basic block would have its own copy in the SelectionDAG.
      28             : // The SelectionDAG recognizes such constants as opaque and doesn't perform
      29             : // certain transformations on them, which would create a new expensive constant.
      30             : //
      31             : // This optimization is only applied to integer constants in instructions and
      32             : // simple (this means not nested) constant cast expressions. For example:
      33             : // %0 = load i64* inttoptr (i64 big_constant to i64*)
      34             : //===----------------------------------------------------------------------===//
      35             : 
      36             : #ifndef LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
      37             : #define LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
      38             : 
      39             : #include "llvm/Analysis/BlockFrequencyInfo.h"
      40             : #include "llvm/Analysis/TargetTransformInfo.h"
      41             : #include "llvm/IR/Dominators.h"
      42             : #include "llvm/IR/PassManager.h"
      43             : 
      44             : namespace llvm {
      45             : 
      46             : /// A private "module" namespace for types and utilities used by
      47             : /// ConstantHoisting. These are implementation details and should not be used by
      48             : /// clients.
      49             : namespace consthoist {
      50             : /// \brief Keeps track of the user of a constant and the operand index where the
      51             : /// constant is used.
      52             : struct ConstantUser {
      53             :   Instruction *Inst;
      54             :   unsigned OpndIdx;
      55             : 
      56        1708 :   ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) { }
      57             : };
      58             : 
      59             : typedef SmallVector<ConstantUser, 8> ConstantUseListType;
      60             : 
      61             : /// \brief Keeps track of a constant candidate and its uses.
      62        8962 : struct ConstantCandidate {
      63             :   ConstantUseListType Uses;
      64             :   ConstantInt *ConstInt;
      65             :   unsigned CumulativeCost;
      66             : 
      67             :   ConstantCandidate(ConstantInt *ConstInt)
      68        2158 :     : ConstInt(ConstInt), CumulativeCost(0) { }
      69             : 
      70             :   /// \brief Add the user to the use list and update the cost.
      71             :   void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) {
      72        1708 :     CumulativeCost += Cost;
      73        3416 :     Uses.push_back(ConstantUser(Inst, Idx));
      74             :   }
      75             : };
      76             : 
      77             : /// \brief This represents a constant that has been rebased with respect to a
      78             : /// base constant. The difference to the base constant is recorded in Offset.
      79        3110 : struct RebasedConstantInfo {
      80             :   ConstantUseListType Uses;
      81             :   Constant *Offset;
      82             : 
      83             :   RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset)
      84         622 :     : Uses(std::move(Uses)), Offset(Offset) { }
      85             : };
      86             : 
      87             : typedef SmallVector<RebasedConstantInfo, 4> RebasedConstantListType;
      88             : 
      89             : /// \brief A base constant and all its rebased constants.
      90        1820 : struct ConstantInfo {
      91             :   ConstantInt *BaseConstant;
      92             :   RebasedConstantListType RebasedConstants;
      93             : };
      94             : }
      95             : 
      96      109130 : class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
      97             : public:
      98             :   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
      99             : 
     100             :   // Glue for old PM.
     101             :   bool runImpl(Function &F, TargetTransformInfo &TTI, DominatorTree &DT,
     102             :                BlockFrequencyInfo *BFI, BasicBlock &Entry);
     103             : 
     104      136021 :   void releaseMemory() {
     105      272042 :     ConstantVec.clear();
     106      136021 :     ClonedCastMap.clear();
     107      136021 :     ConstCandVec.clear();
     108      136021 :   }
     109             : 
     110             : private:
     111             :   typedef DenseMap<ConstantInt *, unsigned> ConstCandMapType;
     112             :   typedef std::vector<consthoist::ConstantCandidate> ConstCandVecType;
     113             : 
     114             :   const TargetTransformInfo *TTI;
     115             :   DominatorTree *DT;
     116             :   BlockFrequencyInfo *BFI;
     117             :   BasicBlock *Entry;
     118             : 
     119             :   /// Keeps track of constant candidates found in the function.
     120             :   ConstCandVecType ConstCandVec;
     121             : 
     122             :   /// Keep track of cast instructions we already cloned.
     123             :   SmallDenseMap<Instruction *, Instruction *> ClonedCastMap;
     124             : 
     125             :   /// These are the final constants we decided to hoist.
     126             :   SmallVector<consthoist::ConstantInfo, 8> ConstantVec;
     127             : 
     128             :   Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const;
     129             :   SmallPtrSet<Instruction *, 8>
     130             :   findConstantInsertionPoint(const consthoist::ConstantInfo &ConstInfo) const;
     131             :   void collectConstantCandidates(ConstCandMapType &ConstCandMap,
     132             :                                  Instruction *Inst, unsigned Idx,
     133             :                                  ConstantInt *ConstInt);
     134             :   void collectConstantCandidates(ConstCandMapType &ConstCandMap,
     135             :                                  Instruction *Inst, unsigned Idx);
     136             :   void collectConstantCandidates(ConstCandMapType &ConstCandMap,
     137             :                                  Instruction *Inst);
     138             :   void collectConstantCandidates(Function &Fn);
     139             :   void findAndMakeBaseConstant(ConstCandVecType::iterator S,
     140             :                                ConstCandVecType::iterator E);
     141             :   unsigned maximizeConstantsInRange(ConstCandVecType::iterator S,
     142             :                                     ConstCandVecType::iterator E,
     143             :                                     ConstCandVecType::iterator &MaxCostItr);
     144             :   void findBaseConstants();
     145             :   void emitBaseConstants(Instruction *Base, Constant *Offset,
     146             :                          const consthoist::ConstantUser &ConstUser);
     147             :   bool emitBaseConstants();
     148             :   void deleteDeadCastInst() const;
     149             :   bool optimizeConstants(Function &Fn);
     150             : };
     151             : }
     152             : 
     153             : #endif // LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H

Generated by: LCOV version 1.13