Precompute block frequencies, pow() isn't free.
[oota-llvm.git] / lib / CodeGen / SpillPlacement.h
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 placeSpills() which is called by the
14 // 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/SmallVector.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32
33 namespace llvm {
34
35 class BitVector;
36 class EdgeBundles;
37 class MachineBasicBlock;
38 class MachineLoopInfo;
39
40 class SpillPlacement  : public MachineFunctionPass {
41   struct Node;
42   const MachineFunction *MF;
43   const EdgeBundles *bundles;
44   const MachineLoopInfo *loops;
45   Node *nodes;
46
47   // Nodes that are active in the current computation. Owned by the placeSpills
48   // caller.
49   BitVector *ActiveNodes;
50
51   // Block frequencies are computed once. Indexed by block number.
52   SmallVector<float, 4> BlockFrequency;
53
54 public:
55   static char ID; // Pass identification, replacement for typeid.
56
57   SpillPlacement() : MachineFunctionPass(ID), nodes(0) {}
58   ~SpillPlacement() { releaseMemory(); }
59
60   /// BorderConstraint - A basic block has separate constraints for entry and
61   /// exit.
62   enum BorderConstraint {
63     DontCare,  ///< Block doesn't care / variable not live.
64     PrefReg,   ///< Block entry/exit prefers a register.
65     PrefSpill, ///< Block entry/exit prefers a stack slot.
66     MustSpill  ///< A register is impossible, variable must be spilled.
67   };
68
69   /// BlockConstraint - Entry and exit constraints for a basic block.
70   struct BlockConstraint {
71     unsigned Number;            ///< Basic block number (from MBB::getNumber()).
72     BorderConstraint Entry : 8; ///< Constraint on block entry.
73     BorderConstraint Exit : 8;  ///< Constraint on block exit.
74   };
75
76   /// placeSpills - Compute the optimal spill code placement given the
77   /// constraints. No MustSpill constraints will be violated, and the smallest
78   /// possible number of PrefX constraints will be violated, weighted by
79   /// expected execution frequencies.
80   /// @param LiveBlocks Constraints for blocks that have the variable live in or
81   ///                   live out. DontCare/DontCare means the variable is live
82   ///                   through the block. DontCare/X means the variable is live
83   ///                   out, but not live in.
84   /// @param RegBundles Bit vector to receive the edge bundles where the
85   ///                   variable should be kept in a register. Each bit
86   ///                   corresponds to an edge bundle, a set bit means the
87   ///                   variable should be kept in a register through the
88   ///                   bundle. A clear bit means the variable should be
89   ///                   spilled.
90   /// @return True if a perfect solution was found, allowing the variable to be
91   ///         in a register through all relevant bundles.
92   bool placeSpills(const SmallVectorImpl<BlockConstraint> &LiveBlocks,
93                    BitVector &RegBundles);
94
95   /// getBlockFrequency - Return the estimated block execution frequency per
96   /// function invocation.
97   float getBlockFrequency(unsigned Number) const {
98     return BlockFrequency[Number];
99   }
100
101 private:
102   virtual bool runOnMachineFunction(MachineFunction&);
103   virtual void getAnalysisUsage(AnalysisUsage&) const;
104   virtual void releaseMemory();
105
106   void activate(unsigned);
107   void prepareNodes(const SmallVectorImpl<BlockConstraint>&);
108   void iterate(const SmallVectorImpl<unsigned>&);
109 };
110
111 } // end namespace llvm
112
113 #endif