Add a function for profiling to run at shutdown. Unlike the existing API, this
[llvm/stm8.git] / lib / CodeGen / SpillPlacement.h
blob46e64e6fcbf6ef204a87abcd92e1699349929db4
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.
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.
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.
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.
25 //===----------------------------------------------------------------------===//
27 #ifndef LLVM_CODEGEN_SPILLPLACEMENT_H
28 #define LLVM_CODEGEN_SPILLPLACEMENT_H
30 #include "llvm/ADT/ArrayRef.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
34 namespace llvm {
36 class BitVector;
37 class EdgeBundles;
38 class MachineBasicBlock;
39 class MachineLoopInfo;
41 class SpillPlacement : public MachineFunctionPass {
42 struct Node;
43 const MachineFunction *MF;
44 const EdgeBundles *bundles;
45 const MachineLoopInfo *loops;
46 Node *nodes;
48 // Nodes that are active in the current computation. Owned by the prepare()
49 // caller.
50 BitVector *ActiveNodes;
52 // The number of active nodes with a positive bias.
53 unsigned PositiveNodes;
55 // Block frequencies are computed once. Indexed by block number.
56 SmallVector<float, 4> BlockFrequency;
58 public:
59 static char ID; // Pass identification, replacement for typeid.
61 SpillPlacement() : MachineFunctionPass(ID), nodes(0) {}
62 ~SpillPlacement() { releaseMemory(); }
64 /// BorderConstraint - A basic block has separate constraints for entry and
65 /// exit.
66 enum BorderConstraint {
67 DontCare, ///< Block doesn't care / variable not live.
68 PrefReg, ///< Block entry/exit prefers a register.
69 PrefSpill, ///< Block entry/exit prefers a stack slot.
70 MustSpill ///< A register is impossible, variable must be spilled.
73 /// BlockConstraint - Entry and exit constraints for a basic block.
74 struct BlockConstraint {
75 unsigned Number; ///< Basic block number (from MBB::getNumber()).
76 BorderConstraint Entry : 8; ///< Constraint on block entry.
77 BorderConstraint Exit : 8; ///< Constraint on block exit.
80 /// prepare - Reset state and prepare for a new spill placement computation.
81 /// @param RegBundles Bit vector to receive the edge bundles where the
82 /// variable should be kept in a register. Each bit
83 /// corresponds to an edge bundle, a set bit means the
84 /// variable should be kept in a register through the
85 /// bundle. A clear bit means the variable should be
86 /// spilled. This vector is retained.
87 void prepare(BitVector &RegBundles);
89 /// addConstraints - Add constraints and biases. This method may be called
90 /// more than once to accumulate constraints.
91 /// @param LiveBlocks Constraints for blocks that have the variable live in or
92 /// live out.
93 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
95 /// addLinks - Add transparent blocks with the given numbers.
96 void addLinks(ArrayRef<unsigned> Links);
98 /// getPositiveNodes - Return the total number of graph nodes with a positive
99 /// bias after adding constraints.
100 unsigned getPositiveNodes() const { return PositiveNodes; }
102 /// finish - Compute the optimal spill code placement given the
103 /// constraints. No MustSpill constraints will be violated, and the smallest
104 /// possible number of PrefX constraints will be violated, weighted by
105 /// expected execution frequencies.
106 /// The selected bundles are returned in the bitvector passed to prepare().
107 /// @return True if a perfect solution was found, allowing the variable to be
108 /// in a register through all relevant bundles.
109 bool finish();
111 /// getBlockFrequency - Return the estimated block execution frequency per
112 /// function invocation.
113 float getBlockFrequency(unsigned Number) const {
114 return BlockFrequency[Number];
117 private:
118 virtual bool runOnMachineFunction(MachineFunction&);
119 virtual void getAnalysisUsage(AnalysisUsage&) const;
120 virtual void releaseMemory();
122 void activate(unsigned);
123 void iterate(const SmallVectorImpl<unsigned>&);
126 } // end namespace llvm
128 #endif