blob: a67785ddf9b0e20c5044af0179d7117956cfcab4 [file] [log] [blame]
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +00001//===-- 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
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000013// about the CFG. The real work is done by prepare(), addConstraints(), and
14// finish() which are called by the register allocator.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000015//
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
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000030#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +000031#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
33
34namespace llvm {
35
36class BitVector;
37class EdgeBundles;
38class MachineBasicBlock;
39class MachineLoopInfo;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000040
41class SpillPlacement : public MachineFunctionPass {
42 struct Node;
43 const MachineFunction *MF;
44 const EdgeBundles *bundles;
45 const MachineLoopInfo *loops;
46 Node *nodes;
47
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000048 // Nodes that are active in the current computation. Owned by the prepare()
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000049 // caller.
50 BitVector *ActiveNodes;
51
Jakob Stoklund Olesen70d43702011-04-06 19:14:00 +000052 // The number of active nodes with a positive bias.
53 unsigned PositiveNodes;
54
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +000055 // Block frequencies are computed once. Indexed by block number.
56 SmallVector<float, 4> BlockFrequency;
57
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000058public:
59 static char ID; // Pass identification, replacement for typeid.
60
61 SpillPlacement() : MachineFunctionPass(ID), nodes(0) {}
62 ~SpillPlacement() { releaseMemory(); }
63
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.
71 };
72
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.
78 };
79
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000080 /// prepare - Reset state and prepare for a new spill placement computation.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000081 /// @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
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000086 /// spilled. This vector is retained.
87 void prepare(BitVector &RegBundles);
88
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. DontCare/DontCare means the variable is live
93 /// through the block. DontCare/X means the variable is live
94 /// out, but not live in.
95 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
96
Jakob Stoklund Olesen70d43702011-04-06 19:14:00 +000097 /// getPositiveNodes - Return the total number of graph nodes with a positive
98 /// bias after adding constraints.
99 unsigned getPositiveNodes() const { return PositiveNodes; }
100
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000101 /// finish - Compute the optimal spill code placement given the
102 /// constraints. No MustSpill constraints will be violated, and the smallest
103 /// possible number of PrefX constraints will be violated, weighted by
104 /// expected execution frequencies.
105 /// The selected bundles are returned in the bitvector passed to prepare().
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000106 /// @return True if a perfect solution was found, allowing the variable to be
107 /// in a register through all relevant bundles.
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000108 bool finish();
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000109
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000110 /// getBlockFrequency - Return the estimated block execution frequency per
111 /// function invocation.
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000112 float getBlockFrequency(unsigned Number) const {
113 return BlockFrequency[Number];
114 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000115
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000116private:
117 virtual bool runOnMachineFunction(MachineFunction&);
118 virtual void getAnalysisUsage(AnalysisUsage&) const;
119 virtual void releaseMemory();
120
121 void activate(unsigned);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000122 void iterate(const SmallVectorImpl<unsigned>&);
123};
124
125} // end namespace llvm
126
127#endif