Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 1 | //===- SpillPlacement.h - Optimal Spill Code Placement ---------*- C++ -*--===// |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 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 |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 12 | // about the CFG. The real work is done by prepare(), addConstraints(), and |
| 13 | // finish() which are called by the register allocator. |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 26 | #ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |
| 27 | #define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 28 | |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | c332e72 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Quentin Colombet | b926bda | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | c5454ff | 2013-07-16 18:26:15 +0000 | [diff] [blame] | 33 | #include "llvm/Support/BlockFrequency.h" |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | class BitVector; |
| 38 | class EdgeBundles; |
Michael Gottesman | 092647b | 2013-12-14 00:25:47 +0000 | [diff] [blame] | 39 | class MachineBlockFrequencyInfo; |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 40 | class MachineFunction; |
| 41 | class MachineLoopInfo; |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 42 | |
Hans Wennborg | 12b996d | 2014-08-11 02:21:30 +0000 | [diff] [blame] | 43 | class SpillPlacement : public MachineFunctionPass { |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 44 | struct Node; |
| 45 | const MachineFunction *MF; |
| 46 | const EdgeBundles *bundles; |
| 47 | const MachineLoopInfo *loops; |
Michael Gottesman | 092647b | 2013-12-14 00:25:47 +0000 | [diff] [blame] | 48 | const MachineBlockFrequencyInfo *MBFI; |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 49 | Node *nodes = nullptr; |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 50 | |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 51 | // Nodes that are active in the current computation. Owned by the prepare() |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 52 | // caller. |
| 53 | BitVector *ActiveNodes; |
| 54 | |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 55 | // 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 Olesen | 6895b87 | 2011-04-06 19:14:00 +0000 | [diff] [blame] | 61 | |
Jakob Stoklund Olesen | c332e72 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 62 | // Block frequencies are computed once. Indexed by block number. |
Hans Wennborg | 12b996d | 2014-08-11 02:21:30 +0000 | [diff] [blame] | 63 | SmallVector<BlockFrequency, 8> BlockFrequencies; |
Jakob Stoklund Olesen | c332e72 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 64 | |
Chandler Carruth | 7425c8c | 2014-10-02 22:23:14 +0000 | [diff] [blame] | 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; |
| 68 | |
Quentin Colombet | b926bda | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 69 | /// List of nodes that need to be updated in ::iterate. |
| 70 | SparseSet<unsigned> TodoList; |
| 71 | |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 72 | public: |
| 73 | static char ID; // Pass identification, replacement for typeid. |
| 74 | |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 75 | SpillPlacement() : MachineFunctionPass(ID) {} |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 76 | ~SpillPlacement() override { releaseMemory(); } |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 77 | |
| 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 Olesen | d2a7d1e | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 84 | PrefBoth, ///< Block entry prefers both register and stack. |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 85 | 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 Olesen | d2a7d1e | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 93 | |
| 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 Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 100 | /// prepare - Reset state and prepare for a new spill placement computation. |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 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 |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 106 | /// 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 Olesen | 6d2bbc1 | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 112 | /// live out. |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 113 | void addConstraints(ArrayRef<BlockConstraint> LiveBlocks); |
| 114 | |
Jakob Stoklund Olesen | d2a7d1e | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 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 | /// |
Jakob Stoklund Olesen | 0ab5d0e | 2011-07-23 03:10:19 +0000 | [diff] [blame] | 119 | /// @param Blocks Array of block numbers that prefer to spill in and out. |
Jakob Stoklund Olesen | 8695452 | 2011-08-03 23:09:38 +0000 | [diff] [blame] | 120 | /// @param Strong When true, double the negative bias for these blocks. |
| 121 | void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong); |
Jakob Stoklund Olesen | 0ab5d0e | 2011-07-23 03:10:19 +0000 | [diff] [blame] | 122 | |
Jakob Stoklund Olesen | 6d2bbc1 | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 123 | /// addLinks - Add transparent blocks with the given numbers. |
| 124 | void addLinks(ArrayRef<unsigned> Links); |
| 125 | |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 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(); |
| 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 Olesen | 6895b87 | 2011-04-06 19:14:00 +0000 | [diff] [blame] | 140 | |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 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(). |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 146 | /// @return True if a perfect solution was found, allowing the variable to be |
| 147 | /// in a register through all relevant bundles. |
Jakob Stoklund Olesen | 36b5d8a | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 148 | bool finish(); |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 149 | |
Jakob Stoklund Olesen | 267f6c1 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 150 | /// getBlockFrequency - Return the estimated block execution frequency per |
| 151 | /// function invocation. |
Jakob Stoklund Olesen | efeb3a1 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 152 | BlockFrequency getBlockFrequency(unsigned Number) const { |
| 153 | return BlockFrequencies[Number]; |
Jakob Stoklund Olesen | c332e72 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 154 | } |
Jakob Stoklund Olesen | 267f6c1 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 155 | |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 156 | private: |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 157 | bool runOnMachineFunction(MachineFunction &mf) override; |
| 158 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 159 | void releaseMemory() override; |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 160 | |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 161 | void activate(unsigned n); |
Chandler Carruth | 7425c8c | 2014-10-02 22:23:14 +0000 | [diff] [blame] | 162 | void setThreshold(const BlockFrequency &Entry); |
Quentin Colombet | b926bda | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 163 | |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 164 | bool update(unsigned n); |
Jakob Stoklund Olesen | 8e236ea | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // end namespace llvm |
| 168 | |
Eugene Zelenko | fb7f792 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 169 | #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |