blob: aa0e07ef92e342eacbd408f9b31a24ddf072d760 [file] [log] [blame]
Eugene Zelenkofb7f7922017-09-21 23:20:16 +00001//===- SpillPlacement.h - Optimal Spill Code Placement ---------*- C++ -*--===//
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This analysis computes the optimal spill code placement between basic blocks.
10//
11// The runOnMachineFunction() method only precomputes some profiling information
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +000012// about the CFG. The real work is done by prepare(), addConstraints(), and
13// finish() which are called by the register allocator.
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000014//
15// Given a variable that is live across multiple basic blocks, and given
16// constraints on the basic blocks where the variable is live, determine which
17// edge bundles should have the variable in a register and which edge bundles
18// should have the variable in a stack slot.
19//
20// The returned bit vector can be used to place optimal spill code at basic
21// block entries and exits. Spill code placement inside a basic block is not
22// considered.
23//
24//===----------------------------------------------------------------------===//
25
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000026#ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
27#define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000028
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +000029#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +000030#include "llvm/ADT/SmallVector.h"
Quentin Colombetb926bda2016-05-19 22:40:37 +000031#include "llvm/ADT/SparseSet.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;
Michael Gottesman092647b2013-12-14 00:25:47 +000039class MachineBlockFrequencyInfo;
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000040class MachineFunction;
41class MachineLoopInfo;
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;
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000049 Node *nodes = nullptr;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000050
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
Quentin Colombetb926bda2016-05-19 22:40:37 +000069 /// List of nodes that need to be updated in ::iterate.
70 SparseSet<unsigned> TodoList;
71
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000072public:
73 static char ID; // Pass identification, replacement for typeid.
74
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000075 SpillPlacement() : MachineFunctionPass(ID) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000076 ~SpillPlacement() override { releaseMemory(); }
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000077
78 /// BorderConstraint - A basic block has separate constraints for entry and
79 /// exit.
80 enum BorderConstraint {
81 DontCare, ///< Block doesn't care / variable not live.
82 PrefReg, ///< Block entry/exit prefers a register.
83 PrefSpill, ///< Block entry/exit prefers a stack slot.
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +000084 PrefBoth, ///< Block entry prefers both register and stack.
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000085 MustSpill ///< A register is impossible, variable must be spilled.
86 };
87
88 /// BlockConstraint - Entry and exit constraints for a basic block.
89 struct BlockConstraint {
90 unsigned Number; ///< Basic block number (from MBB::getNumber()).
91 BorderConstraint Entry : 8; ///< Constraint on block entry.
92 BorderConstraint Exit : 8; ///< Constraint on block exit.
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +000093
94 /// True when this block changes the value of the live range. This means
95 /// the block has a non-PHI def. When this is false, a live-in value on
96 /// the stack can be live-out on the stack without inserting a spill.
97 bool ChangesValue;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +000098 };
99
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000100 /// prepare - Reset state and prepare for a new spill placement computation.
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000101 /// @param RegBundles Bit vector to receive the edge bundles where the
102 /// variable should be kept in a register. Each bit
103 /// corresponds to an edge bundle, a set bit means the
104 /// variable should be kept in a register through the
105 /// bundle. A clear bit means the variable should be
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000106 /// spilled. This vector is retained.
107 void prepare(BitVector &RegBundles);
108
109 /// addConstraints - Add constraints and biases. This method may be called
110 /// more than once to accumulate constraints.
111 /// @param LiveBlocks Constraints for blocks that have the variable live in or
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000112 /// live out.
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000113 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
114
Jakob Stoklund Olesend2a7d1e2011-08-02 21:53:03 +0000115 /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is
116 /// equivalent to calling addConstraint with identical BlockConstraints with
117 /// Entry = Exit = PrefSpill, and ChangesValue = false.
118 ///
Jakob Stoklund Olesen0ab5d0e2011-07-23 03:10:19 +0000119 /// @param Blocks Array of block numbers that prefer to spill in and out.
Jakob Stoklund Olesen86954522011-08-03 23:09:38 +0000120 /// @param Strong When true, double the negative bias for these blocks.
121 void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
Jakob Stoklund Olesen0ab5d0e2011-07-23 03:10:19 +0000122
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000123 /// addLinks - Add transparent blocks with the given numbers.
124 void addLinks(ArrayRef<unsigned> Links);
125
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000126 /// scanActiveBundles - Perform an initial scan of all bundles activated by
127 /// addConstraints and addLinks, updating their state. Add all the bundles
128 /// that now prefer a register to RecentPositive.
129 /// Prepare internal data structures for iterate.
130 /// Return true is there are any positive nodes.
131 bool scanActiveBundles();
132
133 /// iterate - Update the network iteratively until convergence, or new bundles
134 /// are found.
135 void iterate();
136
137 /// getRecentPositive - Return an array of bundles that became positive during
138 /// the previous call to scanActiveBundles or iterate.
139 ArrayRef<unsigned> getRecentPositive() { return RecentPositive; }
Jakob Stoklund Olesen6895b872011-04-06 19:14:00 +0000140
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000141 /// finish - Compute the optimal spill code placement given the
142 /// constraints. No MustSpill constraints will be violated, and the smallest
143 /// possible number of PrefX constraints will be violated, weighted by
144 /// expected execution frequencies.
145 /// The selected bundles are returned in the bitvector passed to prepare().
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000146 /// @return True if a perfect solution was found, allowing the variable to be
147 /// in a register through all relevant bundles.
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +0000148 bool finish();
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000149
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000150 /// getBlockFrequency - Return the estimated block execution frequency per
151 /// function invocation.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000152 BlockFrequency getBlockFrequency(unsigned Number) const {
153 return BlockFrequencies[Number];
Jakob Stoklund Olesenc332e722011-03-04 00:58:40 +0000154 }
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000155
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000156private:
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000157 bool runOnMachineFunction(MachineFunction &mf) override;
158 void getAnalysisUsage(AnalysisUsage &AU) const override;
Craig Topper4584cd52014-03-07 09:26:03 +0000159 void releaseMemory() override;
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000160
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000161 void activate(unsigned n);
Chandler Carruth7425c8c2014-10-02 22:23:14 +0000162 void setThreshold(const BlockFrequency &Entry);
Quentin Colombetb926bda2016-05-19 22:40:37 +0000163
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000164 bool update(unsigned n);
Jakob Stoklund Olesen8e236ea2011-01-06 01:21:53 +0000165};
166
167} // end namespace llvm
168
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000169#endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H