blob: 9d1d676c54aeae52bc454e9dd65fd08b660e437c [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 Olesenf4afdfc2011-04-09 02:59:09 +000052 // Nodes with active links. Populated by scanActiveBundles.
53 SmallVector<unsigned, 8> Linked;
54
55 // Nodes that went positive during the last call to scanActiveBundles or
56 // iterate.
57 SmallVector<unsigned, 8> RecentPositive;
Jakob Stoklund Olesen70d43702011-04-06 19:14:00 +000058
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +000059 // Block frequencies are computed once. Indexed by block number.
60 SmallVector<float, 4> BlockFrequency;
61
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000062public:
63 static char ID; // Pass identification, replacement for typeid.
64
65 SpillPlacement() : MachineFunctionPass(ID), nodes(0) {}
66 ~SpillPlacement() { releaseMemory(); }
67
68 /// BorderConstraint - A basic block has separate constraints for entry and
69 /// exit.
70 enum BorderConstraint {
71 DontCare, ///< Block doesn't care / variable not live.
72 PrefReg, ///< Block entry/exit prefers a register.
73 PrefSpill, ///< Block entry/exit prefers a stack slot.
Jakob Stoklund Olesen0e0a8802011-08-02 21:53:03 +000074 PrefBoth, ///< Block entry prefers both register and stack.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000075 MustSpill ///< A register is impossible, variable must be spilled.
76 };
77
78 /// BlockConstraint - Entry and exit constraints for a basic block.
79 struct BlockConstraint {
80 unsigned Number; ///< Basic block number (from MBB::getNumber()).
81 BorderConstraint Entry : 8; ///< Constraint on block entry.
82 BorderConstraint Exit : 8; ///< Constraint on block exit.
Jakob Stoklund Olesen0e0a8802011-08-02 21:53:03 +000083
84 /// True when this block changes the value of the live range. This means
85 /// the block has a non-PHI def. When this is false, a live-in value on
86 /// the stack can be live-out on the stack without inserting a spill.
87 bool ChangesValue;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000088 };
89
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000090 /// prepare - Reset state and prepare for a new spill placement computation.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000091 /// @param RegBundles Bit vector to receive the edge bundles where the
92 /// variable should be kept in a register. Each bit
93 /// corresponds to an edge bundle, a set bit means the
94 /// variable should be kept in a register through the
95 /// bundle. A clear bit means the variable should be
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +000096 /// spilled. This vector is retained.
97 void prepare(BitVector &RegBundles);
98
99 /// addConstraints - Add constraints and biases. This method may be called
100 /// more than once to accumulate constraints.
101 /// @param LiveBlocks Constraints for blocks that have the variable live in or
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000102 /// live out.
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000103 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
104
Jakob Stoklund Olesen0e0a8802011-08-02 21:53:03 +0000105 /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is
106 /// equivalent to calling addConstraint with identical BlockConstraints with
107 /// Entry = Exit = PrefSpill, and ChangesValue = false.
108 ///
Jakob Stoklund Olesene60f1032011-07-23 03:10:19 +0000109 /// @param Blocks Array of block numbers that prefer to spill in and out.
110 void addPrefSpill(ArrayRef<unsigned> Blocks);
111
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000112 /// addLinks - Add transparent blocks with the given numbers.
113 void addLinks(ArrayRef<unsigned> Links);
114
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000115 /// scanActiveBundles - Perform an initial scan of all bundles activated by
116 /// addConstraints and addLinks, updating their state. Add all the bundles
117 /// that now prefer a register to RecentPositive.
118 /// Prepare internal data structures for iterate.
119 /// Return true is there are any positive nodes.
120 bool scanActiveBundles();
121
122 /// iterate - Update the network iteratively until convergence, or new bundles
123 /// are found.
124 void iterate();
125
126 /// getRecentPositive - Return an array of bundles that became positive during
127 /// the previous call to scanActiveBundles or iterate.
128 ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
Jakob Stoklund Olesen70d43702011-04-06 19:14:00 +0000129
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000130 /// finish - Compute the optimal spill code placement given the
131 /// constraints. No MustSpill constraints will be violated, and the smallest
132 /// possible number of PrefX constraints will be violated, weighted by
133 /// expected execution frequencies.
134 /// The selected bundles are returned in the bitvector passed to prepare().
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000135 /// @return True if a perfect solution was found, allowing the variable to be
136 /// in a register through all relevant bundles.
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000137 bool finish();
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000138
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000139 /// getBlockFrequency - Return the estimated block execution frequency per
140 /// function invocation.
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +0000141 float getBlockFrequency(unsigned Number) const {
142 return BlockFrequency[Number];
143 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000144
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000145private:
146 virtual bool runOnMachineFunction(MachineFunction&);
147 virtual void getAnalysisUsage(AnalysisUsage&) const;
148 virtual void releaseMemory();
149
150 void activate(unsigned);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000151};
152
153} // end namespace llvm
154
155#endif