LLVM 24.0.0git
SpillPlacement.h
Go to the documentation of this file.
1//===- SpillPlacement.h - Optimal Spill Code Placement ---------*- C++ -*--===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This analysis computes the optimal spill code placement between basic blocks.
10//
11// The runOnMachineFunction() method only precomputes some profiling information
12// about the CFG. The real work is done by prepare(), addConstraints(), and
13// finish() which are called by the register allocator.
14//
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
26#ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
27#define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
28
29#include "llvm/ADT/ArrayRef.h"
31#include "llvm/ADT/SparseSet.h"
35
36namespace llvm {
37
38class BitVector;
39class EdgeBundles;
41class MachineFunction;
44
48
49 struct Node;
50
51 const EdgeBundles *bundles = nullptr;
52 const MachineBlockFrequencyInfo *MBFI = nullptr;
53
54 std::unique_ptr<Node[]> nodes;
55
56 // Nodes that are active in the current computation. Owned by the prepare()
57 // caller.
58 BitVector *ActiveNodes = nullptr;
59
60 // Nodes that went positive during the last call to scanActiveBundles or
61 // iterate.
62 SmallVector<unsigned, 8> RecentPositive;
63
64 // Block frequencies are computed once. Indexed by block number.
65 SmallVector<BlockFrequency, 8> BlockFrequencies;
66
67 /// Decision threshold. A node gets the output value 0 if the weighted sum of
68 /// its inputs falls in the open interval (-Threshold;Threshold).
69 BlockFrequency Threshold;
70
71 /// List of nodes that need to be updated in ::iterate.
72 SparseSet<unsigned> TodoList;
73
74public:
75 /// BorderConstraint - A basic block has separate constraints for entry and
76 /// exit.
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.
81 PrefBoth, ///< Block entry prefers both register and stack.
82 MustSpill ///< A register is impossible, variable must be spilled.
83 };
84
85 /// BlockConstraint - Entry and exit constraints for a basic block.
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.
90
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.
95
96 LLVM_ABI void print(raw_ostream &OS) const;
97 LLVM_ABI void dump() const;
98 };
99
100 /// prepare - Reset state and prepare for a new spill placement computation.
101 /// @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
106 /// spilled. This vector is retained.
107 LLVM_ABI 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
112 /// live out.
114
115 /// 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 ///
119 /// @param Blocks Array of block numbers that prefer to spill in and out.
120 /// @param Strong When true, double the negative bias for these blocks.
121 LLVM_ABI void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
122
123 /// addLinks - Add transparent blocks with the given numbers.
125
126 /// 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.
132
133 /// iterate - Update the network iteratively until convergence, or new bundles
134 /// are found.
135 LLVM_ABI 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; }
140
141 /// 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().
146 /// @return True if a perfect solution was found, allowing the variable to be
147 /// in a register through all relevant bundles.
148 LLVM_ABI bool finish();
149
150 /// getBlockFrequency - Return the estimated block execution frequency per
151 /// function invocation.
153 return BlockFrequencies[Number];
154 }
155
157 MachineFunctionAnalysisManager::Invalidator &Inv);
158
161
162private:
164
165 LLVM_ABI void releaseMemory();
166
167 void run(MachineFunction &MF, EdgeBundles *Bundles,
169 void activate(unsigned n);
170 void setThreshold(BlockFrequency Entry);
171
172 bool update(unsigned n);
173};
174
176public:
177 static char ID;
179
180 SpillPlacement &getResult() { return Impl; }
181 const SpillPlacement &getResult() const { return Impl; }
182
183private:
184 SpillPlacement Impl;
185 bool runOnMachineFunction(MachineFunction &MF) override;
186 void getAnalysisUsage(AnalysisUsage &AU) const override;
187 void releaseMemory() override { Impl.releaseMemory(); }
188};
189
191 : public AnalysisInfoMixin<SpillPlacementAnalysis> {
193 static AnalysisKey Key;
194
195public:
199};
200
201} // end namespace llvm
202
203#endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
bbsections prepare
#define LLVM_ABI
Definition Compiler.h:211
This file defines the SmallVector class.
This file defines the SparseSet class derived from the version described in Briggs,...
Represent the analysis usage information of a pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
SparseSet - Fast set implementation for objects that can be identified by small unsigned keys.
Definition SparseSet.h:118
LLVM_ABI SpillPlacement run(MachineFunction &, MachineFunctionAnalysisManager &)
const SpillPlacement & getResult() const
LLVM_ABI void addConstraints(ArrayRef< BlockConstraint > LiveBlocks)
addConstraints - Add constraints and biases.
LLVM_ABI bool finish()
finish - Compute the optimal spill code placement given the constraints.
LLVM_ABI void addPrefSpill(ArrayRef< unsigned > Blocks, bool Strong)
addPrefSpill - Add PrefSpill constraints to all blocks listed.
friend class SpillPlacementWrapperLegacy
LLVM_ABI bool scanActiveBundles()
scanActiveBundles - Perform an initial scan of all bundles activated by addConstraints and addLinks,...
LLVM_ABI bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
friend class SpillPlacementAnalysis
LLVM_ABI void addLinks(ArrayRef< unsigned > Links)
addLinks - Add transparent blocks with the given numbers.
LLVM_ABI void iterate()
iterate - Update the network iteratively until convergence, or new bundles are found.
BorderConstraint
BorderConstraint - A basic block has separate constraints for entry and exit.
@ MustSpill
A register is impossible, variable must be spilled.
@ DontCare
Block doesn't care / variable not live.
@ PrefBoth
Block entry prefers both register and stack.
@ PrefReg
Block entry/exit prefers a register.
@ PrefSpill
Block entry/exit prefers a stack slot.
LLVM_ABI ~SpillPlacement()
LLVM_ABI SpillPlacement(SpillPlacement &&)
ArrayRef< unsigned > getRecentPositive()
getRecentPositive - Return an array of bundles that became positive during the previous call to scanA...
BlockFrequency getBlockFrequency(unsigned Number) const
getBlockFrequency - Return the estimated block execution frequency per function invocation.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Node - Each edge bundle corresponds to a Hopfield node.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
BlockConstraint - Entry and exit constraints for a basic block.
BorderConstraint Exit
Constraint on block exit.
LLVM_ABI void print(raw_ostream &OS) const
bool ChangesValue
True when this block changes the value of the live range.
BorderConstraint Entry
Constraint on block entry.
unsigned Number
Basic block number (from MBB::getNumber()).