LLVM API Documentation

SpillPlacement.h
Go to the documentation of this file.
00001 //===-- SpillPlacement.h - Optimal Spill Code Placement --------*- 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 // This analysis computes the optimal spill code placement between basic blocks.
00011 //
00012 // The runOnMachineFunction() method only precomputes some profiling information
00013 // about the CFG. The real work is done by prepare(), addConstraints(), and
00014 // finish() which are called by the register allocator.
00015 //
00016 // Given a variable that is live across multiple basic blocks, and given
00017 // constraints on the basic blocks where the variable is live, determine which
00018 // edge bundles should have the variable in a register and which edge bundles
00019 // should have the variable in a stack slot.
00020 //
00021 // The returned bit vector can be used to place optimal spill code at basic
00022 // block entries and exits. Spill code placement inside a basic block is not
00023 // considered.
00024 //
00025 //===----------------------------------------------------------------------===//
00026 
00027 #ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
00028 #define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
00029 
00030 #include "llvm/ADT/ArrayRef.h"
00031 #include "llvm/ADT/SmallVector.h"
00032 #include "llvm/CodeGen/MachineFunctionPass.h"
00033 #include "llvm/Support/BlockFrequency.h"
00034 
00035 namespace llvm {
00036 
00037 class BitVector;
00038 class EdgeBundles;
00039 class MachineBasicBlock;
00040 class MachineLoopInfo;
00041 class MachineBlockFrequencyInfo;
00042 
00043 class SpillPlacement : public MachineFunctionPass {
00044   struct Node;
00045   const MachineFunction *MF;
00046   const EdgeBundles *bundles;
00047   const MachineLoopInfo *loops;
00048   const MachineBlockFrequencyInfo *MBFI;
00049   Node *nodes;
00050 
00051   // Nodes that are active in the current computation. Owned by the prepare()
00052   // caller.
00053   BitVector *ActiveNodes;
00054 
00055   // Nodes with active links. Populated by scanActiveBundles.
00056   SmallVector<unsigned, 8> Linked;
00057 
00058   // Nodes that went positive during the last call to scanActiveBundles or
00059   // iterate.
00060   SmallVector<unsigned, 8> RecentPositive;
00061 
00062   // Block frequencies are computed once. Indexed by block number.
00063   SmallVector<BlockFrequency, 8> BlockFrequencies;
00064 
00065 public:
00066   static char ID; // Pass identification, replacement for typeid.
00067 
00068   SpillPlacement() : MachineFunctionPass(ID), nodes(nullptr) {}
00069   ~SpillPlacement() { releaseMemory(); }
00070 
00071   /// BorderConstraint - A basic block has separate constraints for entry and
00072   /// exit.
00073   enum BorderConstraint {
00074     DontCare,  ///< Block doesn't care / variable not live.
00075     PrefReg,   ///< Block entry/exit prefers a register.
00076     PrefSpill, ///< Block entry/exit prefers a stack slot.
00077     PrefBoth,  ///< Block entry prefers both register and stack.
00078     MustSpill  ///< A register is impossible, variable must be spilled.
00079   };
00080 
00081   /// BlockConstraint - Entry and exit constraints for a basic block.
00082   struct BlockConstraint {
00083     unsigned Number;            ///< Basic block number (from MBB::getNumber()).
00084     BorderConstraint Entry : 8; ///< Constraint on block entry.
00085     BorderConstraint Exit : 8;  ///< Constraint on block exit.
00086 
00087     /// True when this block changes the value of the live range. This means
00088     /// the block has a non-PHI def.  When this is false, a live-in value on
00089     /// the stack can be live-out on the stack without inserting a spill.
00090     bool ChangesValue;
00091   };
00092 
00093   /// prepare - Reset state and prepare for a new spill placement computation.
00094   /// @param RegBundles Bit vector to receive the edge bundles where the
00095   ///                   variable should be kept in a register. Each bit
00096   ///                   corresponds to an edge bundle, a set bit means the
00097   ///                   variable should be kept in a register through the
00098   ///                   bundle. A clear bit means the variable should be
00099   ///                   spilled. This vector is retained.
00100   void prepare(BitVector &RegBundles);
00101 
00102   /// addConstraints - Add constraints and biases. This method may be called
00103   /// more than once to accumulate constraints.
00104   /// @param LiveBlocks Constraints for blocks that have the variable live in or
00105   ///                   live out.
00106   void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
00107 
00108   /// addPrefSpill - Add PrefSpill constraints to all blocks listed.  This is
00109   /// equivalent to calling addConstraint with identical BlockConstraints with
00110   /// Entry = Exit = PrefSpill, and ChangesValue = false.
00111   ///
00112   /// @param Blocks Array of block numbers that prefer to spill in and out.
00113   /// @param Strong When true, double the negative bias for these blocks.
00114   void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
00115 
00116   /// addLinks - Add transparent blocks with the given numbers.
00117   void addLinks(ArrayRef<unsigned> Links);
00118 
00119   /// scanActiveBundles - Perform an initial scan of all bundles activated by
00120   /// addConstraints and addLinks, updating their state. Add all the bundles
00121   /// that now prefer a register to RecentPositive.
00122   /// Prepare internal data structures for iterate.
00123   /// Return true is there are any positive nodes.
00124   bool scanActiveBundles();
00125 
00126   /// iterate - Update the network iteratively until convergence, or new bundles
00127   /// are found.
00128   void iterate();
00129 
00130   /// getRecentPositive - Return an array of bundles that became positive during
00131   /// the previous call to scanActiveBundles or iterate.
00132   ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
00133 
00134   /// finish - Compute the optimal spill code placement given the
00135   /// constraints. No MustSpill constraints will be violated, and the smallest
00136   /// possible number of PrefX constraints will be violated, weighted by
00137   /// expected execution frequencies.
00138   /// The selected bundles are returned in the bitvector passed to prepare().
00139   /// @return True if a perfect solution was found, allowing the variable to be
00140   ///         in a register through all relevant bundles.
00141   bool finish();
00142 
00143   /// getBlockFrequency - Return the estimated block execution frequency per
00144   /// function invocation.
00145   BlockFrequency getBlockFrequency(unsigned Number) const {
00146     return BlockFrequencies[Number];
00147   }
00148 
00149 private:
00150   bool runOnMachineFunction(MachineFunction&) override;
00151   void getAnalysisUsage(AnalysisUsage&) const override;
00152   void releaseMemory() override;
00153 
00154   void activate(unsigned);
00155 };
00156 
00157 } // end namespace llvm
00158 
00159 #endif