blob: 622361e7e80cbdffa3919aef3bea63d31baaede3 [file] [log] [blame]
Jakob Stoklund Olesen8e236ea2011-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 Olesen36b5d8a2011-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 Olesen8e236ea2011-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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000027#ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
28#define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000029
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +000030#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +000031#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenc5454ff2013-07-16 18:26:15 +000033#include "llvm/Support/BlockFrequency.h"
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000034
35namespace llvm {
36
37class BitVector;
38class EdgeBundles;
39class MachineBasicBlock;
40class MachineLoopInfo;
Michael Gottesman092647b2013-12-14 00:25:47 +000041class MachineBlockFrequencyInfo;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000042
Hans Wennborg12b996d2014-08-11 02:21:30 +000043class SpillPlacement : public MachineFunctionPass {
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000044 struct Node;
45 const MachineFunction *MF;
46 const EdgeBundles *bundles;
47 const MachineLoopInfo *loops;
Michael Gottesman092647b2013-12-14 00:25:47 +000048 const MachineBlockFrequencyInfo *MBFI;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000049 Node *nodes;
50
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +000051 // Nodes that are active in the current computation. Owned by the prepare()
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000052 // caller.
53 BitVector *ActiveNodes;
54
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000055 // Nodes with active links. Populated by scanActiveBundles.
56 SmallVector<unsigned, 8> Linked;
57
58 // Nodes that went positive during the last call to scanActiveBundles or
59 // iterate.
60 SmallVector<unsigned, 8> RecentPositive;
Jakob Stoklund Olesen6895b872011-04-06 19:14:00 +000061
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +000062 // Block frequencies are computed once. Indexed by block number.
Hans Wennborg12b996d2014-08-11 02:21:30 +000063 SmallVector<BlockFrequency, 8> BlockFrequencies;
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +000064
Chandler Carruth7425c8c2014-10-02 22:23:14 +000065 /// Decision threshold. A node gets the output value 0 if the weighted sum of
66 /// its inputs falls in the open interval (-Threshold;Threshold).
67 BlockFrequency Threshold;
68
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000069public:
70 static char ID; // Pass identification, replacement for typeid.
71
Craig Topperada08572014-04-16 04:21:27 +000072 SpillPlacement() : MachineFunctionPass(ID), nodes(nullptr) {}
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000073 ~SpillPlacement() { releaseMemory(); }
74
75 /// BorderConstraint - A basic block has separate constraints for entry and
76 /// exit.
77 enum BorderConstraint {
78 DontCare, ///< Block doesn't care / variable not live.
79 PrefReg, ///< Block entry/exit prefers a register.
80 PrefSpill, ///< Block entry/exit prefers a stack slot.
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +000081 PrefBoth, ///< Block entry prefers both register and stack.
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000082 MustSpill ///< A register is impossible, variable must be spilled.
83 };
84
85 /// BlockConstraint - Entry and exit constraints for a basic block.
86 struct BlockConstraint {
87 unsigned Number; ///< Basic block number (from MBB::getNumber()).
88 BorderConstraint Entry : 8; ///< Constraint on block entry.
89 BorderConstraint Exit : 8; ///< Constraint on block exit.
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +000090
91 /// True when this block changes the value of the live range. This means
92 /// the block has a non-PHI def. When this is false, a live-in value on
93 /// the stack can be live-out on the stack without inserting a spill.
94 bool ChangesValue;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000095 };
96
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +000097 /// prepare - Reset state and prepare for a new spill placement computation.
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000098 /// @param RegBundles Bit vector to receive the edge bundles where the
99 /// variable should be kept in a register. Each bit
100 /// corresponds to an edge bundle, a set bit means the
101 /// variable should be kept in a register through the
102 /// bundle. A clear bit means the variable should be
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000103 /// spilled. This vector is retained.
104 void prepare(BitVector &RegBundles);
105
106 /// addConstraints - Add constraints and biases. This method may be called
107 /// more than once to accumulate constraints.
108 /// @param LiveBlocks Constraints for blocks that have the variable live in or
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000109 /// live out.
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000110 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
111
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +0000112 /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is
113 /// equivalent to calling addConstraint with identical BlockConstraints with
114 /// Entry = Exit = PrefSpill, and ChangesValue = false.
115 ///
Jakob Stoklund Olesen0ab5d0e2011-07-23 03:10:19 +0000116 /// @param Blocks Array of block numbers that prefer to spill in and out.
Jakob Stoklund Olesen86954522011-08-03 23:09:38 +0000117 /// @param Strong When true, double the negative bias for these blocks.
118 void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
Jakob Stoklund Olesen0ab5d0e2011-07-23 03:10:19 +0000119
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000120 /// addLinks - Add transparent blocks with the given numbers.
121 void addLinks(ArrayRef<unsigned> Links);
122
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000123 /// scanActiveBundles - Perform an initial scan of all bundles activated by
124 /// addConstraints and addLinks, updating their state. Add all the bundles
125 /// that now prefer a register to RecentPositive.
126 /// Prepare internal data structures for iterate.
127 /// Return true is there are any positive nodes.
128 bool scanActiveBundles();
129
130 /// iterate - Update the network iteratively until convergence, or new bundles
131 /// are found.
132 void iterate();
133
134 /// getRecentPositive - Return an array of bundles that became positive during
135 /// the previous call to scanActiveBundles or iterate.
136 ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
Jakob Stoklund Olesen6895b872011-04-06 19:14:00 +0000137
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000138 /// finish - Compute the optimal spill code placement given the
139 /// constraints. No MustSpill constraints will be violated, and the smallest
140 /// possible number of PrefX constraints will be violated, weighted by
141 /// expected execution frequencies.
142 /// The selected bundles are returned in the bitvector passed to prepare().
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000143 /// @return True if a perfect solution was found, allowing the variable to be
144 /// in a register through all relevant bundles.
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000145 bool finish();
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000146
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000147 /// getBlockFrequency - Return the estimated block execution frequency per
148 /// function invocation.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000149 BlockFrequency getBlockFrequency(unsigned Number) const {
150 return BlockFrequencies[Number];
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +0000151 }
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000152
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000153private:
Craig Topper4584cd52014-03-07 09:26:03 +0000154 bool runOnMachineFunction(MachineFunction&) override;
155 void getAnalysisUsage(AnalysisUsage&) const override;
156 void releaseMemory() override;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000157
158 void activate(unsigned);
Chandler Carruth7425c8c2014-10-02 22:23:14 +0000159 void setThreshold(const BlockFrequency &Entry);
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000160};
161
162} // end namespace llvm
163
164#endif