[llvm-readobj] - Simplify stack-sizes.test test case.
[llvm-complete.git] / lib / CodeGen / SpillPlacement.h
blobaa0e07ef92e342eacbd408f9b31a24ddf072d760
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.
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.
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.
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.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
27 #define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
29 #include "llvm/ADT/ArrayRef.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/SparseSet.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/Support/BlockFrequency.h"
35 namespace llvm {
37 class BitVector;
38 class EdgeBundles;
39 class MachineBlockFrequencyInfo;
40 class MachineFunction;
41 class MachineLoopInfo;
43 class SpillPlacement : public MachineFunctionPass {
44 struct Node;
45 const MachineFunction *MF;
46 const EdgeBundles *bundles;
47 const MachineLoopInfo *loops;
48 const MachineBlockFrequencyInfo *MBFI;
49 Node *nodes = nullptr;
51 // Nodes that are active in the current computation. Owned by the prepare()
52 // caller.
53 BitVector *ActiveNodes;
55 // Nodes with active links. Populated by scanActiveBundles.
56 SmallVector<unsigned, 8> Linked;
58 // Nodes that went positive during the last call to scanActiveBundles or
59 // iterate.
60 SmallVector<unsigned, 8> RecentPositive;
62 // Block frequencies are computed once. Indexed by block number.
63 SmallVector<BlockFrequency, 8> BlockFrequencies;
65 /// 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;
69 /// List of nodes that need to be updated in ::iterate.
70 SparseSet<unsigned> TodoList;
72 public:
73 static char ID; // Pass identification, replacement for typeid.
75 SpillPlacement() : MachineFunctionPass(ID) {}
76 ~SpillPlacement() override { releaseMemory(); }
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.
84 PrefBoth, ///< Block entry prefers both register and stack.
85 MustSpill ///< A register is impossible, variable must be spilled.
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.
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;
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 void prepare(BitVector &RegBundles);
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.
113 void addConstraints(ArrayRef<BlockConstraint> LiveBlocks);
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.
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 void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong);
123 /// addLinks - Add transparent blocks with the given numbers.
124 void addLinks(ArrayRef<unsigned> Links);
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.
131 bool scanActiveBundles();
133 /// iterate - Update the network iteratively until convergence, or new bundles
134 /// are found.
135 void iterate();
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; }
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 bool finish();
150 /// getBlockFrequency - Return the estimated block execution frequency per
151 /// function invocation.
152 BlockFrequency getBlockFrequency(unsigned Number) const {
153 return BlockFrequencies[Number];
156 private:
157 bool runOnMachineFunction(MachineFunction &mf) override;
158 void getAnalysisUsage(AnalysisUsage &AU) const override;
159 void releaseMemory() override;
161 void activate(unsigned n);
162 void setThreshold(const BlockFrequency &Entry);
164 bool update(unsigned n);
167 } // end namespace llvm
169 #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H