Home | History | Annotate | Download | only in CodeGen
      1 //===-- SpillPlacement.h - Optimal Spill Code Placement --------*- 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 // This analysis computes the optimal spill code placement between basic blocks.
     11 //
     12 // The runOnMachineFunction() method only precomputes some profiling information
     13 // about the CFG. The real work is done by prepare(), addConstraints(), and
     14 // finish() which are called by the register allocator.
     15 //
     16 // Given a variable that is live across multiple basic blocks, and given
     17 // constraints on the basic blocks where the variable is live, determine which
     18 // edge bundles should have the variable in a register and which edge bundles
     19 // should have the variable in a stack slot.
     20 //
     21 // The returned bit vector can be used to place optimal spill code at basic
     22 // block entries and exits. Spill code placement inside a basic block is not
     23 // considered.
     24 //
     25 //===----------------------------------------------------------------------===//
     26 
     27 #ifndef LLVM_CODEGEN_SPILLPLACEMENT_H
     28 #define LLVM_CODEGEN_SPILLPLACEMENT_H
     29 
     30 #include "llvm/ADT/ArrayRef.h"
     31 #include "llvm/ADT/SmallVector.h"
     32 #include "llvm/CodeGen/MachineFunctionPass.h"
     33 #include "llvm/Support/BlockFrequency.h"
     34 
     35 namespace llvm {
     36 
     37 class BitVector;
     38 class EdgeBundles;
     39 class MachineBasicBlock;
     40 class MachineLoopInfo;
     41 class MachineBlockFrequencyInfo;
     42 
     43 class SpillPlacement  : public MachineFunctionPass {
     44   struct Node;
     45   const MachineFunction *MF;
     46   const EdgeBundles *bundles;
     47   const MachineLoopInfo *loops;
     48   const MachineBlockFrequencyInfo *MBFI;
     49   Node *nodes;
     50 
     51   // Nodes that are active in the current computation. Owned by the prepare()
     52   // caller.
     53   BitVector *ActiveNodes;
     54 
     55   // Nodes with active links. Populated by scanActiveBundles.
     56   SmallVector<unsigned, 8> Linked;
     57 
     58   // Nodes that went positive during the last call to scanActiveBundles or
     59   // iterate.
     60   SmallVector<unsigned, 8> RecentPositive;
     61 
     62   // Block frequencies are computed once. Indexed by block number.
     63   SmallVector<BlockFrequency, 4> BlockFrequencies;
     64 
     65 public:
     66   static char ID; // Pass identification, replacement for typeid.
     67 
     68   SpillPlacement() : MachineFunctionPass(ID), nodes(nullptr) {}
     69   ~SpillPlacement() { releaseMemory(); }
     70 
     71   /// BorderConstraint - A basic block has separate constraints for entry and
     72   /// exit.
     73   enum BorderConstraint {
     74     DontCare,  ///< Block doesn't care / variable not live.
     75     PrefReg,   ///< Block entry/exit prefers a register.
     76     PrefSpill, ///< Block entry/exit prefers a stack slot.
     77     PrefBoth,  ///< Block entry prefers both register and stack.
     78     MustSpill  ///< A register is impossible, variable must be spilled.
     79   };
     80 
     81   /// BlockConstraint - Entry and exit constraints for a basic block.
     82   struct BlockConstraint {
     83     unsigned Number;            ///< Basic block number (from MBB::getNumber()).
     84     BorderConstraint Entry : 8; ///< Constraint on block entry.
     85     BorderConstraint Exit : 8;  ///< Constraint on block exit.
     86 
     87     /// True when this block changes the value of the live range. This means
     88     /// the block has a non-PHI def.  When this is false, a live-in value on
     89     /// the stack can be live-out on the stack without inserting a spill.
     90     bool ChangesValue;
     91   };
     92 
     93   /// prepare - Reset state and prepare for a new spill placement computation.
     94   /// @param RegBundles Bit vector to receive the edge bundles where the
     95   ///                   variable should be kept in a register. Each bit
     96   ///                   corresponds to an edge bundle, a set bit means the
     97   ///                   variable should be kept in a register through the
     98   ///                   bundle. A clear bit means the variable should be
     99   ///                   spilled. This vector is retained.
    100   void prepare(BitVector &RegBundles);
    101 
    102   /// addConstraints - Add constraints and biases. This method may be called
    103   /// more than once to accumulate constraints.
    104   /// @param LiveBlocks Constraints for blocks that have the variable live in or
    105   ///                   live out.
    106   void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
    107 
    108   /// addPrefSpill - Add PrefSpill constraints to all blocks listed.  This is
    109   /// equivalent to calling addConstraint with identical BlockConstraints with
    110   /// Entry = Exit = PrefSpill, and ChangesValue = false.
    111   ///
    112   /// @param Blocks Array of block numbers that prefer to spill in and out.
    113   /// @param Strong When true, double the negative bias for these blocks.
    114   void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
    115 
    116   /// addLinks - Add transparent blocks with the given numbers.
    117   void addLinks(ArrayRef<unsigned> Links);
    118 
    119   /// scanActiveBundles - Perform an initial scan of all bundles activated by
    120   /// addConstraints and addLinks, updating their state. Add all the bundles
    121   /// that now prefer a register to RecentPositive.
    122   /// Prepare internal data structures for iterate.
    123   /// Return true is there are any positive nodes.
    124   bool scanActiveBundles();
    125 
    126   /// iterate - Update the network iteratively until convergence, or new bundles
    127   /// are found.
    128   void iterate();
    129 
    130   /// getRecentPositive - Return an array of bundles that became positive during
    131   /// the previous call to scanActiveBundles or iterate.
    132   ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
    133 
    134   /// finish - Compute the optimal spill code placement given the
    135   /// constraints. No MustSpill constraints will be violated, and the smallest
    136   /// possible number of PrefX constraints will be violated, weighted by
    137   /// expected execution frequencies.
    138   /// The selected bundles are returned in the bitvector passed to prepare().
    139   /// @return True if a perfect solution was found, allowing the variable to be
    140   ///         in a register through all relevant bundles.
    141   bool finish();
    142 
    143   /// getBlockFrequency - Return the estimated block execution frequency per
    144   /// function invocation.
    145   BlockFrequency getBlockFrequency(unsigned Number) const {
    146     return BlockFrequencies[Number];
    147   }
    148 
    149 private:
    150   bool runOnMachineFunction(MachineFunction&) override;
    151   void getAnalysisUsage(AnalysisUsage&) const override;
    152   void releaseMemory() override;
    153 
    154   void activate(unsigned);
    155 };
    156 
    157 } // end namespace llvm
    158 
    159 #endif
    160