Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 1 | //===- SpillPlacement.h - Optimal Spill Code Placement ---------*- C++ -*--===// |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 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 Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 13 | // about the CFG. The real work is done by prepare(), addConstraints(), and |
| 14 | // finish() which are called by the register allocator. |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 15 | // |
| 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 Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 27 | #ifndef LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |
| 28 | #define LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 29 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 40a42a2 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Quentin Colombet | a499216 | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | 6d9fe79 | 2013-07-16 18:26:15 +0000 | [diff] [blame] | 34 | #include "llvm/Support/BlockFrequency.h" |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | class BitVector; |
| 39 | class EdgeBundles; |
Michael Gottesman | 1a938c2 | 2013-12-14 00:25:47 +0000 | [diff] [blame] | 40 | class MachineBlockFrequencyInfo; |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 41 | class MachineFunction; |
| 42 | class MachineLoopInfo; |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 43 | |
Hans Wennborg | e4506cd | 2014-08-11 02:21:30 +0000 | [diff] [blame] | 44 | class SpillPlacement : public MachineFunctionPass { |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 45 | struct Node; |
| 46 | const MachineFunction *MF; |
| 47 | const EdgeBundles *bundles; |
| 48 | const MachineLoopInfo *loops; |
Michael Gottesman | 1a938c2 | 2013-12-14 00:25:47 +0000 | [diff] [blame] | 49 | const MachineBlockFrequencyInfo *MBFI; |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 50 | Node *nodes = nullptr; |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 51 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 52 | // Nodes that are active in the current computation. Owned by the prepare() |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 53 | // caller. |
| 54 | BitVector *ActiveNodes; |
| 55 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 56 | // Nodes with active links. Populated by scanActiveBundles. |
| 57 | SmallVector<unsigned, 8> Linked; |
| 58 | |
| 59 | // Nodes that went positive during the last call to scanActiveBundles or |
| 60 | // iterate. |
| 61 | SmallVector<unsigned, 8> RecentPositive; |
Jakob Stoklund Olesen | 70d4370 | 2011-04-06 19:14:00 +0000 | [diff] [blame] | 62 | |
Jakob Stoklund Olesen | 40a42a2 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 63 | // Block frequencies are computed once. Indexed by block number. |
Hans Wennborg | e4506cd | 2014-08-11 02:21:30 +0000 | [diff] [blame] | 64 | SmallVector<BlockFrequency, 8> BlockFrequencies; |
Jakob Stoklund Olesen | 40a42a2 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 65 | |
Chandler Carruth | bbb28e7 | 2014-10-02 22:23:14 +0000 | [diff] [blame] | 66 | /// Decision threshold. A node gets the output value 0 if the weighted sum of |
| 67 | /// its inputs falls in the open interval (-Threshold;Threshold). |
| 68 | BlockFrequency Threshold; |
| 69 | |
Quentin Colombet | a499216 | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 70 | /// List of nodes that need to be updated in ::iterate. |
| 71 | SparseSet<unsigned> TodoList; |
| 72 | |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 73 | public: |
| 74 | static char ID; // Pass identification, replacement for typeid. |
| 75 | |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 76 | SpillPlacement() : MachineFunctionPass(ID) {} |
Alexander Kornienko | c16fc54 | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 77 | ~SpillPlacement() override { releaseMemory(); } |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 78 | |
| 79 | /// BorderConstraint - A basic block has separate constraints for entry and |
| 80 | /// exit. |
| 81 | enum BorderConstraint { |
| 82 | DontCare, ///< Block doesn't care / variable not live. |
| 83 | PrefReg, ///< Block entry/exit prefers a register. |
| 84 | PrefSpill, ///< Block entry/exit prefers a stack slot. |
Jakob Stoklund Olesen | 0e0a880 | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 85 | PrefBoth, ///< Block entry prefers both register and stack. |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 86 | MustSpill ///< A register is impossible, variable must be spilled. |
| 87 | }; |
| 88 | |
| 89 | /// BlockConstraint - Entry and exit constraints for a basic block. |
| 90 | struct BlockConstraint { |
| 91 | unsigned Number; ///< Basic block number (from MBB::getNumber()). |
| 92 | BorderConstraint Entry : 8; ///< Constraint on block entry. |
| 93 | BorderConstraint Exit : 8; ///< Constraint on block exit. |
Jakob Stoklund Olesen | 0e0a880 | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 94 | |
| 95 | /// True when this block changes the value of the live range. This means |
| 96 | /// the block has a non-PHI def. When this is false, a live-in value on |
| 97 | /// the stack can be live-out on the stack without inserting a spill. |
| 98 | bool ChangesValue; |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 101 | /// prepare - Reset state and prepare for a new spill placement computation. |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 102 | /// @param RegBundles Bit vector to receive the edge bundles where the |
| 103 | /// variable should be kept in a register. Each bit |
| 104 | /// corresponds to an edge bundle, a set bit means the |
| 105 | /// variable should be kept in a register through the |
| 106 | /// bundle. A clear bit means the variable should be |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 107 | /// spilled. This vector is retained. |
| 108 | void prepare(BitVector &RegBundles); |
| 109 | |
| 110 | /// addConstraints - Add constraints and biases. This method may be called |
| 111 | /// more than once to accumulate constraints. |
| 112 | /// @param LiveBlocks Constraints for blocks that have the variable live in or |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 113 | /// live out. |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 114 | void addConstraints(ArrayRef<BlockConstraint> LiveBlocks); |
| 115 | |
Jakob Stoklund Olesen | 0e0a880 | 2011-08-02 21:53:03 +0000 | [diff] [blame] | 116 | /// addPrefSpill - Add PrefSpill constraints to all blocks listed. This is |
| 117 | /// equivalent to calling addConstraint with identical BlockConstraints with |
| 118 | /// Entry = Exit = PrefSpill, and ChangesValue = false. |
| 119 | /// |
Jakob Stoklund Olesen | e60f103 | 2011-07-23 03:10:19 +0000 | [diff] [blame] | 120 | /// @param Blocks Array of block numbers that prefer to spill in and out. |
Jakob Stoklund Olesen | b87f91b | 2011-08-03 23:09:38 +0000 | [diff] [blame] | 121 | /// @param Strong When true, double the negative bias for these blocks. |
| 122 | void addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong); |
Jakob Stoklund Olesen | e60f103 | 2011-07-23 03:10:19 +0000 | [diff] [blame] | 123 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 124 | /// addLinks - Add transparent blocks with the given numbers. |
| 125 | void addLinks(ArrayRef<unsigned> Links); |
| 126 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 127 | /// scanActiveBundles - Perform an initial scan of all bundles activated by |
| 128 | /// addConstraints and addLinks, updating their state. Add all the bundles |
| 129 | /// that now prefer a register to RecentPositive. |
| 130 | /// Prepare internal data structures for iterate. |
| 131 | /// Return true is there are any positive nodes. |
| 132 | bool scanActiveBundles(); |
| 133 | |
| 134 | /// iterate - Update the network iteratively until convergence, or new bundles |
| 135 | /// are found. |
| 136 | void iterate(); |
| 137 | |
| 138 | /// getRecentPositive - Return an array of bundles that became positive during |
| 139 | /// the previous call to scanActiveBundles or iterate. |
| 140 | ArrayRef<unsigned> getRecentPositive() { return RecentPositive; } |
Jakob Stoklund Olesen | 70d4370 | 2011-04-06 19:14:00 +0000 | [diff] [blame] | 141 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 142 | /// finish - Compute the optimal spill code placement given the |
| 143 | /// constraints. No MustSpill constraints will be violated, and the smallest |
| 144 | /// possible number of PrefX constraints will be violated, weighted by |
| 145 | /// expected execution frequencies. |
| 146 | /// The selected bundles are returned in the bitvector passed to prepare(). |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 147 | /// @return True if a perfect solution was found, allowing the variable to be |
| 148 | /// in a register through all relevant bundles. |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 149 | bool finish(); |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 150 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 151 | /// getBlockFrequency - Return the estimated block execution frequency per |
| 152 | /// function invocation. |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 153 | BlockFrequency getBlockFrequency(unsigned Number) const { |
| 154 | return BlockFrequencies[Number]; |
Jakob Stoklund Olesen | 40a42a2 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 155 | } |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 156 | |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 157 | private: |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 158 | bool runOnMachineFunction(MachineFunction &mf) override; |
| 159 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 160 | void releaseMemory() override; |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 161 | |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 162 | void activate(unsigned n); |
Chandler Carruth | bbb28e7 | 2014-10-02 22:23:14 +0000 | [diff] [blame] | 163 | void setThreshold(const BlockFrequency &Entry); |
Quentin Colombet | a499216 | 2016-05-19 22:40:37 +0000 | [diff] [blame] | 164 | |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 165 | bool update(unsigned n); |
Jakob Stoklund Olesen | 8bfe508 | 2011-01-06 01:21:53 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | } // end namespace llvm |
| 169 | |
Eugene Zelenko | 79ea5b5 | 2017-09-21 23:20:16 +0000 | [diff] [blame] | 170 | #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H |