Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1 | //===- RegAllocGreedy.cpp - greedy register allocator ---------------------===// |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +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 file defines the RAGreedy function pass for register allocation in |
| 11 | // optimized builds. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 15 | #include "AllocationOrder.h" |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 16 | #include "InterferenceCache.h" |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 17 | #include "LiveDebugVariables.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 18 | #include "RegAllocBase.h" |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 19 | #include "SpillPlacement.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "Spiller.h" |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 21 | #include "SplitKit.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
| 23 | #include "llvm/ADT/BitVector.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
| 25 | #include "llvm/ADT/IndexedMap.h" |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/MapVector.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SetVector.h" |
| 28 | #include "llvm/ADT/SmallPtrSet.h" |
| 29 | #include "llvm/ADT/SmallSet.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringRef.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/AliasAnalysis.h" |
Adam Nemet | 3b8950a | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/EdgeBundles.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/LiveInterval.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/LiveIntervals.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/LiveRegMatrix.h" |
Matthias Braun | 209f048 | 2017-12-18 23:19:44 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/LiveStacks.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineDominators.h" |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 46 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/MachineFunction.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/MachineInstr.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/MachineOperand.h" |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 56 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 57 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 58 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 59 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 60 | #include "llvm/CodeGen/VirtRegMap.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Function.h" |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 62 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 63 | #include "llvm/MC/MCRegisterInfo.h" |
| 64 | #include "llvm/Pass.h" |
| 65 | #include "llvm/Support/BlockFrequency.h" |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 66 | #include "llvm/Support/BranchProbability.h" |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 67 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 69 | #include "llvm/Support/MathExtras.h" |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 70 | #include "llvm/Support/Timer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 71 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 72 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 73 | #include <algorithm> |
| 74 | #include <cassert> |
| 75 | #include <cstdint> |
| 76 | #include <memory> |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 77 | #include <queue> |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 78 | #include <tuple> |
| 79 | #include <utility> |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 80 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 81 | using namespace llvm; |
| 82 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 83 | #define DEBUG_TYPE "regalloc" |
| 84 | |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 85 | STATISTIC(NumGlobalSplits, "Number of split global live ranges"); |
| 86 | STATISTIC(NumLocalSplits, "Number of split local live ranges"); |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 87 | STATISTIC(NumEvicted, "Number of interferences evicted"); |
| 88 | |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 89 | static cl::opt<SplitEditor::ComplementSpillMode> SplitSpillMode( |
| 90 | "split-spill-mode", cl::Hidden, |
| 91 | cl::desc("Spill mode for splitting live ranges"), |
| 92 | cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"), |
| 93 | clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"), |
Mehdi Amini | 3ffe113 | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 94 | clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed")), |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 95 | cl::init(SplitEditor::SM_Speed)); |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 96 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 97 | static cl::opt<unsigned> |
| 98 | LastChanceRecoloringMaxDepth("lcr-max-depth", cl::Hidden, |
| 99 | cl::desc("Last chance recoloring max depth"), |
| 100 | cl::init(5)); |
| 101 | |
| 102 | static cl::opt<unsigned> LastChanceRecoloringMaxInterference( |
| 103 | "lcr-max-interf", cl::Hidden, |
| 104 | cl::desc("Last chance recoloring maximum number of considered" |
| 105 | " interference at a time"), |
| 106 | cl::init(8)); |
| 107 | |
Zachary Turner | 9a4e15c | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 108 | static cl::opt<bool> ExhaustiveSearch( |
| 109 | "exhaustive-register-search", cl::NotHidden, |
| 110 | cl::desc("Exhaustive Search for registers bypassing the depth " |
| 111 | "and interference cutoffs of last chance recoloring"), |
| 112 | cl::Hidden); |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 113 | |
Quentin Colombet | 230bb1b | 2014-07-01 14:08:37 +0000 | [diff] [blame] | 114 | static cl::opt<bool> EnableLocalReassignment( |
| 115 | "enable-local-reassign", cl::Hidden, |
| 116 | cl::desc("Local reassignment can yield better allocation decisions, but " |
| 117 | "may be compile time intensive"), |
Quentin Colombet | 5599fde | 2014-07-02 18:32:04 +0000 | [diff] [blame] | 118 | cl::init(false)); |
Quentin Colombet | 230bb1b | 2014-07-01 14:08:37 +0000 | [diff] [blame] | 119 | |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 120 | static cl::opt<bool> EnableDeferredSpilling( |
| 121 | "enable-deferred-spilling", cl::Hidden, |
| 122 | cl::desc("Instead of spilling a variable right away, defer the actual " |
| 123 | "code insertion to the end of the allocation. That way the " |
| 124 | "allocator might still find a suitable coloring for this " |
| 125 | "variable because of other evicted variables."), |
| 126 | cl::init(false)); |
| 127 | |
Wei Mi | 735bf22 | 2018-07-16 15:42:20 +0000 | [diff] [blame] | 128 | static cl::opt<unsigned> |
| 129 | HugeSizeForSplit("huge-size-for-split", cl::Hidden, |
Wei Mi | 19723d9 | 2018-07-18 16:56:33 +0000 | [diff] [blame] | 130 | cl::desc("A threshold of live range size which may cause " |
| 131 | "high compile time cost in global splitting."), |
Wei Mi | 735bf22 | 2018-07-16 15:42:20 +0000 | [diff] [blame] | 132 | cl::init(5000)); |
| 133 | |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 134 | // FIXME: Find a good default for this flag and remove the flag. |
| 135 | static cl::opt<unsigned> |
| 136 | CSRFirstTimeCost("regalloc-csr-first-time-cost", |
| 137 | cl::desc("Cost for first time use of callee-saved register."), |
| 138 | cl::init(0), cl::Hidden); |
| 139 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 140 | static cl::opt<bool> ConsiderLocalIntervalCost( |
| 141 | "condsider-local-interval-cost", cl::Hidden, |
| 142 | cl::desc("Consider the cost of local intervals created by a split " |
| 143 | "candidate when choosing the best split candidate."), |
| 144 | cl::init(false)); |
| 145 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 146 | static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", |
| 147 | createGreedyRegisterAllocator); |
| 148 | |
| 149 | namespace { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 150 | |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 151 | class RAGreedy : public MachineFunctionPass, |
| 152 | public RegAllocBase, |
| 153 | private LiveRangeEdit::Delegate { |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 154 | // Convenient shortcuts. |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 155 | using PQueue = std::priority_queue<std::pair<unsigned, unsigned>>; |
| 156 | using SmallLISet = SmallPtrSet<LiveInterval *, 4>; |
| 157 | using SmallVirtRegSet = SmallSet<unsigned, 16>; |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 158 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 159 | // context |
| 160 | MachineFunction *MF; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 161 | |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 162 | // Shortcuts to some useful interface. |
| 163 | const TargetInstrInfo *TII; |
| 164 | const TargetRegisterInfo *TRI; |
| 165 | RegisterClassInfo RCI; |
| 166 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 167 | // analyses |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 168 | SlotIndexes *Indexes; |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 169 | MachineBlockFrequencyInfo *MBFI; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 170 | MachineDominatorTree *DomTree; |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 171 | MachineLoopInfo *Loops; |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 172 | MachineOptimizationRemarkEmitter *ORE; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 173 | EdgeBundles *Bundles; |
| 174 | SpillPlacement *SpillPlacer; |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 175 | LiveDebugVariables *DebugVars; |
Wei Mi | 4eae278 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 176 | AliasAnalysis *AA; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 177 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 178 | // state |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 179 | std::unique_ptr<Spiller> SpillerInstance; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 180 | PQueue Queue; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 181 | unsigned NextCascade; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 182 | |
| 183 | // Live ranges pass through a number of stages as we try to allocate them. |
| 184 | // Some of the stages may also create new live ranges: |
| 185 | // |
| 186 | // - Region splitting. |
| 187 | // - Per-block splitting. |
| 188 | // - Local splitting. |
| 189 | // - Spilling. |
| 190 | // |
| 191 | // Ranges produced by one of the stages skip the previous stages when they are |
| 192 | // dequeued. This improves performance because we can skip interference checks |
| 193 | // that are unlikely to give any results. It also guarantees that the live |
| 194 | // range splitting algorithm terminates, something that is otherwise hard to |
| 195 | // ensure. |
| 196 | enum LiveRangeStage { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 197 | /// Newly created live range that has never been queued. |
| 198 | RS_New, |
| 199 | |
| 200 | /// Only attempt assignment and eviction. Then requeue as RS_Split. |
| 201 | RS_Assign, |
| 202 | |
| 203 | /// Attempt live range splitting if assignment is impossible. |
| 204 | RS_Split, |
| 205 | |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 206 | /// Attempt more aggressive live range splitting that is guaranteed to make |
| 207 | /// progress. This is used for split products that may not be making |
| 208 | /// progress. |
| 209 | RS_Split2, |
| 210 | |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 211 | /// Live range will be spilled. No more splitting will be attempted. |
| 212 | RS_Spill, |
| 213 | |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 214 | |
| 215 | /// Live range is in memory. Because of other evictions, it might get moved |
| 216 | /// in a register in the end. |
| 217 | RS_Memory, |
| 218 | |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 219 | /// There is nothing more we can do to this live range. Abort compilation |
| 220 | /// if it can't be assigned. |
| 221 | RS_Done |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 224 | // Enum CutOffStage to keep a track whether the register allocation failed |
| 225 | // because of the cutoffs encountered in last chance recoloring. |
| 226 | // Note: This is used as bitmask. New value should be next power of 2. |
| 227 | enum CutOffStage { |
| 228 | // No cutoffs encountered |
| 229 | CO_None = 0, |
| 230 | |
| 231 | // lcr-max-depth cutoff encountered |
| 232 | CO_Depth = 1, |
| 233 | |
| 234 | // lcr-max-interf cutoff encountered |
| 235 | CO_Interf = 2 |
| 236 | }; |
| 237 | |
| 238 | uint8_t CutOffInfo; |
| 239 | |
Eli Friedman | ae43dac | 2013-09-10 23:18:14 +0000 | [diff] [blame] | 240 | #ifndef NDEBUG |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 241 | static const char *const StageName[]; |
Eli Friedman | ae43dac | 2013-09-10 23:18:14 +0000 | [diff] [blame] | 242 | #endif |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 243 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 244 | // RegInfo - Keep additional information about each live range. |
| 245 | struct RegInfo { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 246 | LiveRangeStage Stage = RS_New; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 247 | |
| 248 | // Cascade - Eviction loop prevention. See canEvictInterference(). |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 249 | unsigned Cascade = 0; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 250 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 251 | RegInfo() = default; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 255 | |
| 256 | LiveRangeStage getStage(const LiveInterval &VirtReg) const { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 257 | return ExtraRegInfo[VirtReg.reg].Stage; |
| 258 | } |
| 259 | |
| 260 | void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) { |
| 261 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 262 | ExtraRegInfo[VirtReg.reg].Stage = Stage; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | template<typename Iterator> |
| 266 | void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 267 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 268 | for (;Begin != End; ++Begin) { |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 269 | unsigned Reg = *Begin; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 270 | if (ExtraRegInfo[Reg].Stage == RS_New) |
| 271 | ExtraRegInfo[Reg].Stage = NewStage; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 272 | } |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 273 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 274 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 275 | /// Cost of evicting interference. |
| 276 | struct EvictionCost { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 277 | unsigned BrokenHints = 0; ///< Total number of broken hints. |
| 278 | float MaxWeight = 0; ///< Maximum spill weight evicted. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 279 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 280 | EvictionCost() = default; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 281 | |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 282 | bool isMax() const { return BrokenHints == ~0u; } |
| 283 | |
Andrew Trick | 833a9e9 | 2013-11-22 19:07:38 +0000 | [diff] [blame] | 284 | void setMax() { BrokenHints = ~0u; } |
| 285 | |
| 286 | void setBrokenHints(unsigned NHints) { BrokenHints = NHints; } |
| 287 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 288 | bool operator<(const EvictionCost &O) const { |
Benjamin Kramer | 9efaf2f | 2014-03-03 19:58:30 +0000 | [diff] [blame] | 289 | return std::tie(BrokenHints, MaxWeight) < |
| 290 | std::tie(O.BrokenHints, O.MaxWeight); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 291 | } |
| 292 | }; |
| 293 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 294 | /// EvictionTrack - Keeps track of past evictions in order to optimize region |
| 295 | /// split decision. |
| 296 | class EvictionTrack { |
| 297 | |
| 298 | public: |
| 299 | using EvictorInfo = |
| 300 | std::pair<unsigned /* evictor */, unsigned /* physreg */>; |
Nirav Dave | 4c53b27 | 2018-06-05 03:16:28 +0000 | [diff] [blame] | 301 | using EvicteeInfo = llvm::DenseMap<unsigned /* evictee */, EvictorInfo>; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 302 | |
| 303 | private: |
| 304 | /// Each Vreg that has been evicted in the last stage of selectOrSplit will |
| 305 | /// be mapped to the evictor Vreg and the PhysReg it was evicted from. |
| 306 | EvicteeInfo Evictees; |
| 307 | |
| 308 | public: |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 309 | /// Clear all eviction information. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 310 | void clear() { Evictees.clear(); } |
| 311 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 312 | /// Clear eviction information for the given evictee Vreg. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 313 | /// E.g. when Vreg get's a new allocation, the old eviction info is no |
| 314 | /// longer relevant. |
| 315 | /// \param Evictee The evictee Vreg for whom we want to clear collected |
| 316 | /// eviction info. |
| 317 | void clearEvicteeInfo(unsigned Evictee) { Evictees.erase(Evictee); } |
| 318 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 319 | /// Track new eviction. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 320 | /// The Evictor vreg has evicted the Evictee vreg from Physreg. |
Hiroshi Inoue | 7a9527e | 2019-01-09 05:11:10 +0000 | [diff] [blame] | 321 | /// \param PhysReg The physical register Evictee was evicted from. |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 322 | /// \param Evictor The evictor Vreg that evicted Evictee. |
| 323 | /// \param Evictee The evictee Vreg. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 324 | void addEviction(unsigned PhysReg, unsigned Evictor, unsigned Evictee) { |
| 325 | Evictees[Evictee].first = Evictor; |
| 326 | Evictees[Evictee].second = PhysReg; |
| 327 | } |
| 328 | |
| 329 | /// Return the Evictor Vreg which evicted Evictee Vreg from PhysReg. |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 330 | /// \param Evictee The evictee vreg. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 331 | /// \return The Evictor vreg which evicted Evictee vreg from PhysReg. 0 if |
| 332 | /// nobody has evicted Evictee from PhysReg. |
| 333 | EvictorInfo getEvictor(unsigned Evictee) { |
| 334 | if (Evictees.count(Evictee)) { |
| 335 | return Evictees[Evictee]; |
| 336 | } |
| 337 | |
| 338 | return EvictorInfo(0, 0); |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | // Keeps track of past evictions in order to optimize region split decision. |
| 343 | EvictionTrack LastEvicted; |
| 344 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 345 | // splitting state. |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 346 | std::unique_ptr<SplitAnalysis> SA; |
| 347 | std::unique_ptr<SplitEditor> SE; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 348 | |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 349 | /// Cached per-block interference maps |
| 350 | InterferenceCache IntfCache; |
| 351 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 352 | /// All basic blocks where the current register has uses. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 353 | SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 354 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 355 | /// Global live range splitting candidate info. |
| 356 | struct GlobalSplitCandidate { |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 357 | // Register intended for assignment, or 0. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 358 | unsigned PhysReg; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 359 | |
| 360 | // SplitKit interval index for this candidate. |
| 361 | unsigned IntvIdx; |
| 362 | |
| 363 | // Interference for PhysReg. |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 364 | InterferenceCache::Cursor Intf; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 365 | |
| 366 | // Bundles where this candidate should be live. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 367 | BitVector LiveBundles; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 368 | SmallVector<unsigned, 8> ActiveBlocks; |
| 369 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 370 | void reset(InterferenceCache &Cache, unsigned Reg) { |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 371 | PhysReg = Reg; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 372 | IntvIdx = 0; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 373 | Intf.setPhysReg(Cache, Reg); |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 374 | LiveBundles.clear(); |
| 375 | ActiveBlocks.clear(); |
| 376 | } |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 377 | |
| 378 | // Set B[i] = C for every live bundle where B[i] was NoCand. |
| 379 | unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) { |
| 380 | unsigned Count = 0; |
Francis Visoiu Mistrih | 1179b5e | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 381 | for (unsigned i : LiveBundles.set_bits()) |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 382 | if (B[i] == NoCand) { |
| 383 | B[i] = C; |
| 384 | Count++; |
| 385 | } |
| 386 | return Count; |
| 387 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Aditya Nandakumar | 3da011f | 2013-11-19 23:51:32 +0000 | [diff] [blame] | 390 | /// Candidate info for each PhysReg in AllocationOrder. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 391 | /// This vector never shrinks, but grows to the size of the largest register |
| 392 | /// class. |
| 393 | SmallVector<GlobalSplitCandidate, 32> GlobalCand; |
| 394 | |
Alp Toker | 18510b7 | 2014-03-02 03:20:38 +0000 | [diff] [blame] | 395 | enum : unsigned { NoCand = ~0u }; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 396 | |
| 397 | /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to |
| 398 | /// NoCand which indicates the stack interval. |
| 399 | SmallVector<unsigned, 32> BundleCand; |
| 400 | |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 401 | /// Callee-save register cost, calculated once per machine function. |
| 402 | BlockFrequency CSRCost; |
| 403 | |
Quentin Colombet | 5599fde | 2014-07-02 18:32:04 +0000 | [diff] [blame] | 404 | /// Run or not the local reassignment heuristic. This information is |
| 405 | /// obtained from the TargetSubtargetInfo. |
| 406 | bool EnableLocalReassign; |
| 407 | |
Hiroshi Inoue | 5cba328 | 2018-01-17 12:29:38 +0000 | [diff] [blame] | 408 | /// Enable or not the consideration of the cost of local intervals created |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 409 | /// by a split candidate when choosing the best split candidate. |
| 410 | bool EnableAdvancedRASplitCost; |
| 411 | |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 412 | /// Set of broken hints that may be reconciled later because of eviction. |
| 413 | SmallSetVector<LiveInterval *, 8> SetOfBrokenHints; |
| 414 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 415 | public: |
| 416 | RAGreedy(); |
| 417 | |
| 418 | /// Return the pass name. |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 419 | StringRef getPassName() const override { return "Greedy Register Allocator"; } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 420 | |
| 421 | /// RAGreedy analysis usage. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 422 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 423 | void releaseMemory() override; |
| 424 | Spiller &spiller() override { return *SpillerInstance; } |
| 425 | void enqueue(LiveInterval *LI) override; |
| 426 | LiveInterval *dequeue() override; |
| 427 | unsigned selectOrSplit(LiveInterval&, SmallVectorImpl<unsigned>&) override; |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 428 | void aboutToRemoveInterval(LiveInterval &) override; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 429 | |
| 430 | /// Perform register allocation. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 431 | bool runOnMachineFunction(MachineFunction &mf) override; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 432 | |
Matthias Braun | db9ce2f | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 433 | MachineFunctionProperties getRequiredProperties() const override { |
| 434 | return MachineFunctionProperties().set( |
| 435 | MachineFunctionProperties::Property::NoPHIs); |
| 436 | } |
| 437 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 438 | static char ID; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 439 | |
| 440 | private: |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 441 | unsigned selectOrSplitImpl(LiveInterval &, SmallVectorImpl<unsigned> &, |
| 442 | SmallVirtRegSet &, unsigned = 0); |
| 443 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 444 | bool LRE_CanEraseVirtReg(unsigned) override; |
| 445 | void LRE_WillShrinkVirtReg(unsigned) override; |
| 446 | void LRE_DidCloneVirtReg(unsigned, unsigned) override; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 447 | void enqueue(PQueue &CurQueue, LiveInterval *LI); |
| 448 | LiveInterval *dequeue(PQueue &CurQueue); |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 449 | |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 450 | BlockFrequency calcSpillCost(); |
| 451 | bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency&); |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 452 | bool addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); |
| 453 | bool growRegion(GlobalSplitCandidate &Cand); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 454 | bool splitCanCauseEvictionChain(unsigned Evictee, GlobalSplitCandidate &Cand, |
| 455 | unsigned BBNumber, |
| 456 | const AllocationOrder &Order); |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 457 | bool splitCanCauseLocalSpill(unsigned VirtRegToSplit, |
| 458 | GlobalSplitCandidate &Cand, unsigned BBNumber, |
| 459 | const AllocationOrder &Order); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 460 | BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate &, |
| 461 | const AllocationOrder &Order, |
| 462 | bool *CanCauseEvictionChain); |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 463 | bool calcCompactRegion(GlobalSplitCandidate&); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 464 | void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 465 | void calcGapWeights(unsigned, SmallVectorImpl<float>&); |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 466 | unsigned canReassign(LiveInterval &VirtReg, unsigned PrevReg); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 467 | bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool); |
| 468 | bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 469 | bool canEvictInterferenceInRange(LiveInterval &VirtReg, unsigned PhysReg, |
| 470 | SlotIndex Start, SlotIndex End, |
| 471 | EvictionCost &MaxCost); |
| 472 | unsigned getCheapestEvicteeWeight(const AllocationOrder &Order, |
| 473 | LiveInterval &VirtReg, SlotIndex Start, |
| 474 | SlotIndex End, float *BestEvictWeight); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 475 | void evictInterference(LiveInterval&, unsigned, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 476 | SmallVectorImpl<unsigned>&); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 477 | bool mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg, |
| 478 | SmallLISet &RecoloringCandidates, |
| 479 | const SmallVirtRegSet &FixedRegisters); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 480 | |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 481 | unsigned tryAssign(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 482 | SmallVectorImpl<unsigned>&); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 483 | unsigned tryEvict(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 484 | SmallVectorImpl<unsigned>&, unsigned = ~0u); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 485 | unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 486 | SmallVectorImpl<unsigned>&); |
Wei Mi | 735bf22 | 2018-07-16 15:42:20 +0000 | [diff] [blame] | 487 | unsigned isSplitBenefitWorthCost(LiveInterval &VirtReg); |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 488 | /// Calculate cost of region splitting. |
| 489 | unsigned calculateRegionSplitCost(LiveInterval &VirtReg, |
| 490 | AllocationOrder &Order, |
| 491 | BlockFrequency &BestCost, |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 492 | unsigned &NumCands, bool IgnoreCSR, |
| 493 | bool *CanCauseEvictionChain = nullptr); |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 494 | /// Perform region splitting. |
| 495 | unsigned doRegionSplit(LiveInterval &VirtReg, unsigned BestCand, |
| 496 | bool HasCompact, |
| 497 | SmallVectorImpl<unsigned> &NewVRegs); |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 498 | /// Check other options before using a callee-saved register for the first |
| 499 | /// time. |
| 500 | unsigned tryAssignCSRFirstTime(LiveInterval &VirtReg, AllocationOrder &Order, |
| 501 | unsigned PhysReg, unsigned &CostPerUseLimit, |
| 502 | SmallVectorImpl<unsigned> &NewVRegs); |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 503 | void initializeCSRCost(); |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 504 | unsigned tryBlockSplit(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 505 | SmallVectorImpl<unsigned>&); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 506 | unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 507 | SmallVectorImpl<unsigned>&); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 508 | unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 509 | SmallVectorImpl<unsigned>&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 510 | unsigned trySplit(LiveInterval&, AllocationOrder&, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 511 | SmallVectorImpl<unsigned>&); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 512 | unsigned tryLastChanceRecoloring(LiveInterval &, AllocationOrder &, |
| 513 | SmallVectorImpl<unsigned> &, |
| 514 | SmallVirtRegSet &, unsigned); |
| 515 | bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<unsigned> &, |
| 516 | SmallVirtRegSet &, unsigned); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 517 | void tryHintRecoloring(LiveInterval &); |
| 518 | void tryHintsRecoloring(); |
| 519 | |
| 520 | /// Model the information carried by one end of a copy. |
| 521 | struct HintInfo { |
| 522 | /// The frequency of the copy. |
| 523 | BlockFrequency Freq; |
| 524 | /// The virtual register or physical register. |
| 525 | unsigned Reg; |
| 526 | /// Its currently assigned register. |
| 527 | /// In case of a physical register Reg == PhysReg. |
| 528 | unsigned PhysReg; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 529 | |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 530 | HintInfo(BlockFrequency Freq, unsigned Reg, unsigned PhysReg) |
| 531 | : Freq(Freq), Reg(Reg), PhysReg(PhysReg) {} |
| 532 | }; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 533 | using HintsInfo = SmallVector<HintInfo, 4>; |
| 534 | |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 535 | BlockFrequency getBrokenHintFreq(const HintsInfo &, unsigned); |
| 536 | void collectHintInfo(unsigned, HintsInfo &); |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 537 | |
| 538 | bool isUnusedCalleeSavedReg(unsigned PhysReg) const; |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 539 | |
| 540 | /// Compute and report the number of spills and reloads for a loop. |
| 541 | void reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads, |
| 542 | unsigned &FoldedReloads, unsigned &Spills, |
| 543 | unsigned &FoldedSpills); |
| 544 | |
| 545 | /// Report the number of spills and reloads for each loop. |
| 546 | void reportNumberOfSplillsReloads() { |
| 547 | for (MachineLoop *L : *Loops) { |
| 548 | unsigned Reloads, FoldedReloads, Spills, FoldedSpills; |
| 549 | reportNumberOfSplillsReloads(L, Reloads, FoldedReloads, Spills, |
| 550 | FoldedSpills); |
| 551 | } |
| 552 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 553 | }; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 554 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 555 | } // end anonymous namespace |
| 556 | |
| 557 | char RAGreedy::ID = 0; |
Tom Stellard | 107362c | 2016-11-14 21:50:13 +0000 | [diff] [blame] | 558 | char &llvm::RAGreedyID = RAGreedy::ID; |
| 559 | |
| 560 | INITIALIZE_PASS_BEGIN(RAGreedy, "greedy", |
| 561 | "Greedy Register Allocator", false, false) |
| 562 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) |
| 563 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 564 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 565 | INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer) |
| 566 | INITIALIZE_PASS_DEPENDENCY(MachineScheduler) |
| 567 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
| 568 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 569 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 570 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 571 | INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) |
| 572 | INITIALIZE_PASS_DEPENDENCY(EdgeBundles) |
| 573 | INITIALIZE_PASS_DEPENDENCY(SpillPlacement) |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 574 | INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) |
Tom Stellard | 107362c | 2016-11-14 21:50:13 +0000 | [diff] [blame] | 575 | INITIALIZE_PASS_END(RAGreedy, "greedy", |
| 576 | "Greedy Register Allocator", false, false) |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 577 | |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 578 | #ifndef NDEBUG |
| 579 | const char *const RAGreedy::StageName[] = { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 580 | "RS_New", |
| 581 | "RS_Assign", |
| 582 | "RS_Split", |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 583 | "RS_Split2", |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 584 | "RS_Spill", |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 585 | "RS_Memory", |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 586 | "RS_Done" |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 587 | }; |
| 588 | #endif |
| 589 | |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 590 | // Hysteresis to use when comparing floats. |
| 591 | // This helps stabilize decisions based on float comparisons. |
NAKAMURA Takumi | 1a3b253 | 2014-02-04 06:29:38 +0000 | [diff] [blame] | 592 | const float Hysteresis = (2007 / 2048.0f); // 0.97998046875 |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 593 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 594 | FunctionPass* llvm::createGreedyRegisterAllocator() { |
| 595 | return new RAGreedy(); |
| 596 | } |
| 597 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 598 | RAGreedy::RAGreedy(): MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 602 | AU.setPreservesCFG(); |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 603 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 604 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 605 | AU.addRequired<AAResultsWrapperPass>(); |
| 606 | AU.addPreserved<AAResultsWrapperPass>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 607 | AU.addRequired<LiveIntervals>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 608 | AU.addPreserved<LiveIntervals>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 609 | AU.addRequired<SlotIndexes>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 610 | AU.addPreserved<SlotIndexes>(); |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 611 | AU.addRequired<LiveDebugVariables>(); |
| 612 | AU.addPreserved<LiveDebugVariables>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 613 | AU.addRequired<LiveStacks>(); |
| 614 | AU.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 615 | AU.addRequired<MachineDominatorTree>(); |
| 616 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 617 | AU.addRequired<MachineLoopInfo>(); |
| 618 | AU.addPreserved<MachineLoopInfo>(); |
| 619 | AU.addRequired<VirtRegMap>(); |
| 620 | AU.addPreserved<VirtRegMap>(); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 621 | AU.addRequired<LiveRegMatrix>(); |
| 622 | AU.addPreserved<LiveRegMatrix>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 623 | AU.addRequired<EdgeBundles>(); |
| 624 | AU.addRequired<SpillPlacement>(); |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 625 | AU.addRequired<MachineOptimizationRemarkEmitterPass>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 626 | MachineFunctionPass::getAnalysisUsage(AU); |
| 627 | } |
| 628 | |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 629 | //===----------------------------------------------------------------------===// |
| 630 | // LiveRangeEdit delegate methods |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 633 | bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { |
Jonas Paulsson | 7913159 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 634 | LiveInterval &LI = LIS->getInterval(VirtReg); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 635 | if (VRM->hasPhys(VirtReg)) { |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 636 | Matrix->unassign(LI); |
| 637 | aboutToRemoveInterval(LI); |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | // Unassigned virtreg is probably in the priority queue. |
| 641 | // RegAllocBase will erase it after dequeueing. |
Jonas Paulsson | 7913159 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 642 | // Nonetheless, clear the live-range so that the debug |
| 643 | // dump will show the right state for that VirtReg. |
| 644 | LI.clear(); |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 645 | return false; |
| 646 | } |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 647 | |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 648 | void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 649 | if (!VRM->hasPhys(VirtReg)) |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 650 | return; |
| 651 | |
| 652 | // Register is assigned, put it back on the queue for reassignment. |
| 653 | LiveInterval &LI = LIS->getInterval(VirtReg); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 654 | Matrix->unassign(LI); |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 655 | enqueue(&LI); |
| 656 | } |
| 657 | |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 658 | void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { |
Jakob Stoklund Olesen | 0d4fea7 | 2011-09-14 17:34:37 +0000 | [diff] [blame] | 659 | // Cloning a register we haven't even heard about yet? Just ignore it. |
| 660 | if (!ExtraRegInfo.inBounds(Old)) |
| 661 | return; |
| 662 | |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 663 | // LRE may clone a virtual register because dead code elimination causes it to |
Jakob Stoklund Olesen | 165e231 | 2011-07-26 00:54:56 +0000 | [diff] [blame] | 664 | // be split into connected components. The new components are much smaller |
| 665 | // than the original, so they should get a new chance at being assigned. |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 666 | // same stage as the parent. |
Jakob Stoklund Olesen | 165e231 | 2011-07-26 00:54:56 +0000 | [diff] [blame] | 667 | ExtraRegInfo[Old].Stage = RS_Assign; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 668 | ExtraRegInfo.grow(New); |
| 669 | ExtraRegInfo[New] = ExtraRegInfo[Old]; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 672 | void RAGreedy::releaseMemory() { |
David Blaikie | ec31a30 | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 673 | SpillerInstance.reset(); |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 674 | ExtraRegInfo.clear(); |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 675 | GlobalCand.clear(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 678 | void RAGreedy::enqueue(LiveInterval *LI) { enqueue(Queue, LI); } |
| 679 | |
| 680 | void RAGreedy::enqueue(PQueue &CurQueue, LiveInterval *LI) { |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 681 | // Prioritize live ranges by size, assigning larger ranges first. |
| 682 | // The queue holds (size, reg) pairs. |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 683 | const unsigned Size = LI->getSize(); |
| 684 | const unsigned Reg = LI->reg; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 685 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 686 | "Can only enqueue virtual registers"); |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 687 | unsigned Prio; |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 688 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 689 | ExtraRegInfo.grow(Reg); |
| 690 | if (ExtraRegInfo[Reg].Stage == RS_New) |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 691 | ExtraRegInfo[Reg].Stage = RS_Assign; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 692 | |
Jakob Stoklund Olesen | cc07e04 | 2011-07-28 20:48:23 +0000 | [diff] [blame] | 693 | if (ExtraRegInfo[Reg].Stage == RS_Split) { |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 694 | // Unsplit ranges that couldn't be allocated immediately are deferred until |
Jakob Stoklund Olesen | a16a25d | 2011-09-12 16:54:42 +0000 | [diff] [blame] | 695 | // everything else has been allocated. |
| 696 | Prio = Size; |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 697 | } else if (ExtraRegInfo[Reg].Stage == RS_Memory) { |
| 698 | // Memory operand should be considered last. |
| 699 | // Change the priority such that Memory operand are assigned in |
| 700 | // the reverse order that they came in. |
| 701 | // TODO: Make this a member variable and probably do something about hints. |
| 702 | static unsigned MemOp = 0; |
| 703 | Prio = MemOp++; |
Jakob Stoklund Olesen | cc07e04 | 2011-07-28 20:48:23 +0000 | [diff] [blame] | 704 | } else { |
Andrew Trick | eaf8a32 | 2014-02-26 22:07:26 +0000 | [diff] [blame] | 705 | // Giant live ranges fall back to the global assignment heuristic, which |
| 706 | // prevents excessive spilling in pathological cases. |
| 707 | bool ReverseLocal = TRI->reverseLocalAssignment(); |
Matthias Braun | 8e4eaab | 2015-03-31 19:57:53 +0000 | [diff] [blame] | 708 | const TargetRegisterClass &RC = *MRI->getRegClass(Reg); |
Renato Golin | b157cb7 | 2014-10-03 12:20:53 +0000 | [diff] [blame] | 709 | bool ForceGlobal = !ReverseLocal && |
Matthias Braun | 8e4eaab | 2015-03-31 19:57:53 +0000 | [diff] [blame] | 710 | (Size / SlotIndex::InstrDist) > (2 * RC.getNumRegs()); |
Andrew Trick | eaf8a32 | 2014-02-26 22:07:26 +0000 | [diff] [blame] | 711 | |
| 712 | if (ExtraRegInfo[Reg].Stage == RS_Assign && !ForceGlobal && !LI->empty() && |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 713 | LIS->intervalIsInOneMBB(*LI)) { |
| 714 | // Allocate original local ranges in linear instruction order. Since they |
| 715 | // are singly defined, this produces optimal coloring in the absence of |
| 716 | // global interference and other constraints. |
Andrew Trick | eaf8a32 | 2014-02-26 22:07:26 +0000 | [diff] [blame] | 717 | if (!ReverseLocal) |
Andrew Trick | 2e3f799 | 2013-12-11 03:40:15 +0000 | [diff] [blame] | 718 | Prio = LI->beginIndex().getInstrDistance(Indexes->getLastIndex()); |
| 719 | else { |
| 720 | // Allocating bottom up may allow many short LRGs to be assigned first |
| 721 | // to one of the cheap registers. This could be much faster for very |
| 722 | // large blocks on targets with many physical registers. |
Matthias Braun | 3f1ec42 | 2015-03-31 19:57:49 +0000 | [diff] [blame] | 723 | Prio = Indexes->getZeroIndex().getInstrDistance(LI->endIndex()); |
Andrew Trick | 2e3f799 | 2013-12-11 03:40:15 +0000 | [diff] [blame] | 724 | } |
Matthias Braun | 8e4eaab | 2015-03-31 19:57:53 +0000 | [diff] [blame] | 725 | Prio |= RC.AllocationPriority << 24; |
| 726 | } else { |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 727 | // Allocate global and split ranges in long->short order. Long ranges that |
| 728 | // don't fit should be spilled (or split) ASAP so they don't create |
| 729 | // interference. Mark a bit to prioritize global above local ranges. |
| 730 | Prio = (1u << 29) + Size; |
| 731 | } |
| 732 | // Mark a higher bit to prioritize global and local above RS_Split. |
| 733 | Prio |= (1u << 31); |
Jakob Stoklund Olesen | d2a5073 | 2011-02-23 00:56:56 +0000 | [diff] [blame] | 734 | |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 735 | // Boost ranges that have a physical register hint. |
Jakob Stoklund Olesen | fc63744 | 2012-12-03 23:23:50 +0000 | [diff] [blame] | 736 | if (VRM->hasKnownPreference(Reg)) |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 737 | Prio |= (1u << 30); |
| 738 | } |
Andrew Trick | bef4c3e | 2013-07-25 18:35:22 +0000 | [diff] [blame] | 739 | // The virtual register number is a tie breaker for same-sized ranges. |
| 740 | // Give lower vreg numbers higher priority to assign them first. |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 741 | CurQueue.push(std::make_pair(Prio, ~Reg)); |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 744 | LiveInterval *RAGreedy::dequeue() { return dequeue(Queue); } |
| 745 | |
| 746 | LiveInterval *RAGreedy::dequeue(PQueue &CurQueue) { |
| 747 | if (CurQueue.empty()) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 748 | return nullptr; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 749 | LiveInterval *LI = &LIS->getInterval(~CurQueue.top().second); |
| 750 | CurQueue.pop(); |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 751 | return LI; |
| 752 | } |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 753 | |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 754 | //===----------------------------------------------------------------------===// |
| 755 | // Direct Assignment |
| 756 | //===----------------------------------------------------------------------===// |
| 757 | |
| 758 | /// tryAssign - Try to assign VirtReg to an available register. |
| 759 | unsigned RAGreedy::tryAssign(LiveInterval &VirtReg, |
| 760 | AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 761 | SmallVectorImpl<unsigned> &NewVRegs) { |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 762 | Order.rewind(); |
| 763 | unsigned PhysReg; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 764 | while ((PhysReg = Order.next())) |
| 765 | if (!Matrix->checkInterference(VirtReg, PhysReg)) |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 766 | break; |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 767 | if (!PhysReg || Order.isHint()) |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 768 | return PhysReg; |
| 769 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 770 | // PhysReg is available, but there may be a better choice. |
| 771 | |
| 772 | // If we missed a simple hint, try to cheaply evict interference from the |
| 773 | // preferred register. |
| 774 | if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg)) |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 775 | if (Order.isHint(Hint)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 776 | LLVM_DEBUG(dbgs() << "missed hint " << printReg(Hint, TRI) << '\n'); |
Andrew Trick | 833a9e9 | 2013-11-22 19:07:38 +0000 | [diff] [blame] | 777 | EvictionCost MaxCost; |
| 778 | MaxCost.setBrokenHints(1); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 779 | if (canEvictInterference(VirtReg, Hint, true, MaxCost)) { |
| 780 | evictInterference(VirtReg, Hint, NewVRegs); |
| 781 | return Hint; |
| 782 | } |
Quentin Colombet | 8934fe3 | 2016-11-16 01:07:12 +0000 | [diff] [blame] | 783 | // Record the missed hint, we may be able to recover |
| 784 | // at the end if the surrounding allocation changed. |
| 785 | SetOfBrokenHints.insert(&VirtReg); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // Try to evict interference from a cheaper alternative. |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 789 | unsigned Cost = TRI->getCostPerUse(PhysReg); |
| 790 | |
| 791 | // Most registers have 0 additional cost. |
| 792 | if (!Cost) |
| 793 | return PhysReg; |
| 794 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 795 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is available at cost " |
| 796 | << Cost << '\n'); |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 797 | unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost); |
| 798 | return CheapReg ? CheapReg : PhysReg; |
| 799 | } |
| 800 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 801 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 802 | // Interference eviction |
| 803 | //===----------------------------------------------------------------------===// |
| 804 | |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 805 | unsigned RAGreedy::canReassign(LiveInterval &VirtReg, unsigned PrevReg) { |
Matthias Braun | 2aa5727 | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 806 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 807 | unsigned PhysReg; |
| 808 | while ((PhysReg = Order.next())) { |
| 809 | if (PhysReg == PrevReg) |
| 810 | continue; |
| 811 | |
| 812 | MCRegUnitIterator Units(PhysReg, TRI); |
| 813 | for (; Units.isValid(); ++Units) { |
| 814 | // Instantiate a "subquery", not to be confused with the Queries array. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 815 | LiveIntervalUnion::Query subQ(VirtReg, Matrix->getLiveUnions()[*Units]); |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 816 | if (subQ.checkInterference()) |
| 817 | break; |
| 818 | } |
| 819 | // If no units have interference, break out with the current PhysReg. |
| 820 | if (!Units.isValid()) |
| 821 | break; |
| 822 | } |
| 823 | if (PhysReg) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 824 | LLVM_DEBUG(dbgs() << "can reassign: " << VirtReg << " from " |
| 825 | << printReg(PrevReg, TRI) << " to " |
| 826 | << printReg(PhysReg, TRI) << '\n'); |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 827 | return PhysReg; |
| 828 | } |
| 829 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 830 | /// shouldEvict - determine if A should evict the assigned live range B. The |
| 831 | /// eviction policy defined by this function together with the allocation order |
| 832 | /// defined by enqueue() decides which registers ultimately end up being split |
| 833 | /// and spilled. |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 834 | /// |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 835 | /// Cascade numbers are used to prevent infinite loops if this function is a |
| 836 | /// cyclic relation. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 837 | /// |
| 838 | /// @param A The live range to be assigned. |
| 839 | /// @param IsHint True when A is about to be assigned to its preferred |
| 840 | /// register. |
| 841 | /// @param B The live range to be evicted. |
| 842 | /// @param BreaksHint True when B is already assigned to its preferred register. |
| 843 | bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint, |
| 844 | LiveInterval &B, bool BreaksHint) { |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 845 | bool CanSplit = getStage(B) < RS_Spill; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 846 | |
| 847 | // Be fairly aggressive about following hints as long as the evictee can be |
| 848 | // split. |
| 849 | if (CanSplit && IsHint && !BreaksHint) |
| 850 | return true; |
| 851 | |
Andrew Trick | 09f1b25 | 2013-11-22 19:07:42 +0000 | [diff] [blame] | 852 | if (A.weight > B.weight) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 853 | LLVM_DEBUG(dbgs() << "should evict: " << B << " w= " << B.weight << '\n'); |
Andrew Trick | 09f1b25 | 2013-11-22 19:07:42 +0000 | [diff] [blame] | 854 | return true; |
| 855 | } |
| 856 | return false; |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 859 | /// canEvictInterference - Return true if all interferences between VirtReg and |
Manman Ren | d225511 | 2014-02-25 19:47:15 +0000 | [diff] [blame] | 860 | /// PhysReg can be evicted. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 861 | /// |
| 862 | /// @param VirtReg Live range that is about to be assigned. |
| 863 | /// @param PhysReg Desired register for assignment. |
Dmitri Gribenko | 67c8978 | 2012-09-12 16:59:47 +0000 | [diff] [blame] | 864 | /// @param IsHint True when PhysReg is VirtReg's preferred register. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 865 | /// @param MaxCost Only look for cheaper candidates and update with new cost |
| 866 | /// when returning true. |
| 867 | /// @returns True when interference can be evicted cheaper than MaxCost. |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 868 | bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 869 | bool IsHint, EvictionCost &MaxCost) { |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 870 | // It is only possible to evict virtual register interference. |
| 871 | if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg) |
| 872 | return false; |
| 873 | |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 874 | bool IsLocal = LIS->intervalIsInOneMBB(VirtReg); |
| 875 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 876 | // Find VirtReg's cascade number. This will be unassigned if VirtReg was never |
| 877 | // involved in an eviction before. If a cascade number was assigned, deny |
| 878 | // evicting anything with the same or a newer cascade number. This prevents |
| 879 | // infinite eviction loops. |
| 880 | // |
| 881 | // This works out so a register without a cascade number is allowed to evict |
| 882 | // anything, and it can be evicted by anything. |
| 883 | unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; |
| 884 | if (!Cascade) |
| 885 | Cascade = NextCascade; |
| 886 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 887 | EvictionCost Cost; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 888 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 889 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
Jakob Stoklund Olesen | 3f5bedf | 2011-04-11 21:47:01 +0000 | [diff] [blame] | 890 | // If there is 10 or more interferences, chances are one is heavier. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 891 | if (Q.collectInterferingVRegs(10) >= 10) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 892 | return false; |
| 893 | |
Jakob Stoklund Olesen | 3f5bedf | 2011-04-11 21:47:01 +0000 | [diff] [blame] | 894 | // Check if any interfering live range is heavier than MaxWeight. |
| 895 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 896 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 897 | assert(TargetRegisterInfo::isVirtualRegister(Intf->reg) && |
| 898 | "Only expecting virtual register interference from query"); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 899 | // Never evict spill products. They cannot split or spill. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 900 | if (getStage(*Intf) == RS_Done) |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 901 | return false; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 902 | // Once a live range becomes small enough, it is urgent that we find a |
| 903 | // register for it. This is indicated by an infinite spill weight. These |
| 904 | // urgent live ranges get to evict almost anything. |
Jakob Stoklund Olesen | 9cda1be | 2012-05-30 21:46:58 +0000 | [diff] [blame] | 905 | // |
| 906 | // Also allow urgent evictions of unspillable ranges from a strictly |
| 907 | // larger allocation order. |
| 908 | bool Urgent = !VirtReg.isSpillable() && |
| 909 | (Intf->isSpillable() || |
| 910 | RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) < |
| 911 | RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg))); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 912 | // Only evict older cascades or live ranges without a cascade. |
| 913 | unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade; |
| 914 | if (Cascade <= IntfCascade) { |
| 915 | if (!Urgent) |
| 916 | return false; |
| 917 | // We permit breaking cascades for urgent evictions. It should be the |
| 918 | // last resort, though, so make it really expensive. |
| 919 | Cost.BrokenHints += 10; |
| 920 | } |
| 921 | // Would this break a satisfied hint? |
| 922 | bool BreaksHint = VRM->hasPreferredPhys(Intf->reg); |
| 923 | // Update eviction cost. |
| 924 | Cost.BrokenHints += BreaksHint; |
| 925 | Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight); |
| 926 | // Abort if this would be too expensive. |
| 927 | if (!(Cost < MaxCost)) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 928 | return false; |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 929 | if (Urgent) |
| 930 | continue; |
Andrew Trick | 4dc7377 | 2013-11-29 23:49:38 +0000 | [diff] [blame] | 931 | // Apply the eviction policy for non-urgent evictions. |
| 932 | if (!shouldEvict(VirtReg, IsHint, *Intf, BreaksHint)) |
| 933 | return false; |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 934 | // If !MaxCost.isMax(), then we're just looking for a cheap register. |
| 935 | // Evicting another local live range in this case could lead to suboptimal |
| 936 | // coloring. |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 937 | if (!MaxCost.isMax() && IsLocal && LIS->intervalIsInOneMBB(*Intf) && |
Quentin Colombet | 5599fde | 2014-07-02 18:32:04 +0000 | [diff] [blame] | 938 | (!EnableLocalReassign || !canReassign(*Intf, PhysReg))) { |
Andrew Trick | 6ea2b96 | 2013-07-25 18:35:14 +0000 | [diff] [blame] | 939 | return false; |
Andrew Trick | 8adae96 | 2013-07-25 18:35:19 +0000 | [diff] [blame] | 940 | } |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 941 | } |
| 942 | } |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 943 | MaxCost = Cost; |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 944 | return true; |
| 945 | } |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 946 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 947 | /// Return true if all interferences between VirtReg and PhysReg between |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 948 | /// Start and End can be evicted. |
| 949 | /// |
| 950 | /// \param VirtReg Live range that is about to be assigned. |
| 951 | /// \param PhysReg Desired register for assignment. |
| 952 | /// \param Start Start of range to look for interferences. |
| 953 | /// \param End End of range to look for interferences. |
| 954 | /// \param MaxCost Only look for cheaper candidates and update with new cost |
| 955 | /// when returning true. |
| 956 | /// \return True when interference can be evicted cheaper than MaxCost. |
| 957 | bool RAGreedy::canEvictInterferenceInRange(LiveInterval &VirtReg, |
| 958 | unsigned PhysReg, SlotIndex Start, |
| 959 | SlotIndex End, |
| 960 | EvictionCost &MaxCost) { |
| 961 | EvictionCost Cost; |
| 962 | |
| 963 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 964 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
| 965 | |
| 966 | // Check if any interfering live range is heavier than MaxWeight. |
| 967 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 968 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
| 969 | |
| 970 | // Check if interference overlast the segment in interest. |
| 971 | if (!Intf->overlaps(Start, End)) |
| 972 | continue; |
| 973 | |
| 974 | // Cannot evict non virtual reg interference. |
| 975 | if (!TargetRegisterInfo::isVirtualRegister(Intf->reg)) |
| 976 | return false; |
| 977 | // Never evict spill products. They cannot split or spill. |
| 978 | if (getStage(*Intf) == RS_Done) |
| 979 | return false; |
| 980 | |
| 981 | // Would this break a satisfied hint? |
| 982 | bool BreaksHint = VRM->hasPreferredPhys(Intf->reg); |
| 983 | // Update eviction cost. |
| 984 | Cost.BrokenHints += BreaksHint; |
| 985 | Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight); |
| 986 | // Abort if this would be too expensive. |
| 987 | if (!(Cost < MaxCost)) |
| 988 | return false; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (Cost.MaxWeight == 0) |
| 993 | return false; |
| 994 | |
| 995 | MaxCost = Cost; |
| 996 | return true; |
| 997 | } |
| 998 | |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 999 | /// Return the physical register that will be best |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1000 | /// candidate for eviction by a local split interval that will be created |
| 1001 | /// between Start and End. |
| 1002 | /// |
| 1003 | /// \param Order The allocation order |
| 1004 | /// \param VirtReg Live range that is about to be assigned. |
| 1005 | /// \param Start Start of range to look for interferences |
| 1006 | /// \param End End of range to look for interferences |
| 1007 | /// \param BestEvictweight The eviction cost of that eviction |
| 1008 | /// \return The PhysReg which is the best candidate for eviction and the |
| 1009 | /// eviction cost in BestEvictweight |
| 1010 | unsigned RAGreedy::getCheapestEvicteeWeight(const AllocationOrder &Order, |
| 1011 | LiveInterval &VirtReg, |
| 1012 | SlotIndex Start, SlotIndex End, |
| 1013 | float *BestEvictweight) { |
| 1014 | EvictionCost BestEvictCost; |
| 1015 | BestEvictCost.setMax(); |
| 1016 | BestEvictCost.MaxWeight = VirtReg.weight; |
| 1017 | unsigned BestEvicteePhys = 0; |
| 1018 | |
| 1019 | // Go over all physical registers and find the best candidate for eviction |
| 1020 | for (auto PhysReg : Order.getOrder()) { |
| 1021 | |
| 1022 | if (!canEvictInterferenceInRange(VirtReg, PhysReg, Start, End, |
| 1023 | BestEvictCost)) |
| 1024 | continue; |
| 1025 | |
| 1026 | // Best so far. |
| 1027 | BestEvicteePhys = PhysReg; |
| 1028 | } |
| 1029 | *BestEvictweight = BestEvictCost.MaxWeight; |
| 1030 | return BestEvicteePhys; |
| 1031 | } |
| 1032 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1033 | /// evictInterference - Evict any interferring registers that prevent VirtReg |
| 1034 | /// from being assigned to Physreg. This assumes that canEvictInterference |
| 1035 | /// returned true. |
| 1036 | void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1037 | SmallVectorImpl<unsigned> &NewVRegs) { |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1038 | // Make sure that VirtReg has a cascade number, and assign that cascade |
| 1039 | // number to every evicted register. These live ranges than then only be |
| 1040 | // evicted by a newer cascade, preventing infinite loops. |
| 1041 | unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; |
| 1042 | if (!Cascade) |
| 1043 | Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++; |
| 1044 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1045 | LLVM_DEBUG(dbgs() << "evicting " << printReg(PhysReg, TRI) |
| 1046 | << " interference: Cascade " << Cascade << '\n'); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1047 | |
| 1048 | // Collect all interfering virtregs first. |
| 1049 | SmallVector<LiveInterval*, 8> Intfs; |
| 1050 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 1051 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
Matthias Braun | 7ed12a0 | 2017-03-03 23:27:20 +0000 | [diff] [blame] | 1052 | // We usually have the interfering VRegs cached so collectInterferingVRegs() |
| 1053 | // should be fast, we may need to recalculate if when different physregs |
| 1054 | // overlap the same register unit so we had different SubRanges queried |
| 1055 | // against it. |
| 1056 | Q.collectInterferingVRegs(); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1057 | ArrayRef<LiveInterval*> IVR = Q.interferingVRegs(); |
| 1058 | Intfs.append(IVR.begin(), IVR.end()); |
| 1059 | } |
| 1060 | |
| 1061 | // Evict them second. This will invalidate the queries. |
| 1062 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { |
| 1063 | LiveInterval *Intf = Intfs[i]; |
| 1064 | // The same VirtReg may be present in multiple RegUnits. Skip duplicates. |
| 1065 | if (!VRM->hasPhys(Intf->reg)) |
| 1066 | continue; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1067 | |
| 1068 | LastEvicted.addEviction(PhysReg, VirtReg.reg, Intf->reg); |
| 1069 | |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1070 | Matrix->unassign(*Intf); |
| 1071 | assert((ExtraRegInfo[Intf->reg].Cascade < Cascade || |
| 1072 | VirtReg.isSpillable() < Intf->isSpillable()) && |
| 1073 | "Cannot decrease cascade number, illegal eviction"); |
| 1074 | ExtraRegInfo[Intf->reg].Cascade = Cascade; |
| 1075 | ++NumEvicted; |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1076 | NewVRegs.push_back(Intf->reg); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 1080 | /// Returns true if the given \p PhysReg is a callee saved register and has not |
| 1081 | /// been used for allocation yet. |
| 1082 | bool RAGreedy::isUnusedCalleeSavedReg(unsigned PhysReg) const { |
| 1083 | unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg); |
| 1084 | if (CSR == 0) |
| 1085 | return false; |
| 1086 | |
| 1087 | return !Matrix->isPhysRegUsed(PhysReg); |
| 1088 | } |
| 1089 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1090 | /// tryEvict - Try to evict all interferences for a physreg. |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 1091 | /// @param VirtReg Currently unassigned virtual register. |
| 1092 | /// @param Order Physregs to try. |
| 1093 | /// @return Physreg to assign VirtReg, or 0. |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1094 | unsigned RAGreedy::tryEvict(LiveInterval &VirtReg, |
| 1095 | AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1096 | SmallVectorImpl<unsigned> &NewVRegs, |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 1097 | unsigned CostPerUseLimit) { |
Matthias Braun | 9262f00 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 1098 | NamedRegionTimer T("evict", "Evict", TimerGroupName, TimerGroupDescription, |
| 1099 | TimePassesIsEnabled); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1100 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1101 | // Keep track of the cheapest interference seen so far. |
Andrew Trick | 833a9e9 | 2013-11-22 19:07:38 +0000 | [diff] [blame] | 1102 | EvictionCost BestCost; |
| 1103 | BestCost.setMax(); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1104 | unsigned BestPhys = 0; |
Jakob Stoklund Olesen | 6d61329 | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 1105 | unsigned OrderLimit = Order.getOrder().size(); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1106 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1107 | // When we are just looking for a reduced cost per use, don't break any |
| 1108 | // hints, and only evict smaller spill weights. |
| 1109 | if (CostPerUseLimit < ~0u) { |
| 1110 | BestCost.BrokenHints = 0; |
| 1111 | BestCost.MaxWeight = VirtReg.weight; |
Jakob Stoklund Olesen | 6d61329 | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Check of any registers in RC are below CostPerUseLimit. |
| 1114 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg); |
| 1115 | unsigned MinCost = RegClassInfo.getMinCost(RC); |
| 1116 | if (MinCost >= CostPerUseLimit) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1117 | LLVM_DEBUG(dbgs() << TRI->getRegClassName(RC) << " minimum cost = " |
| 1118 | << MinCost << ", no cheaper registers to be found.\n"); |
Jakob Stoklund Olesen | 6d61329 | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | // It is normal for register classes to have a long tail of registers with |
| 1123 | // the same cost. We don't need to look at them if they're too expensive. |
| 1124 | if (TRI->getCostPerUse(Order.getOrder().back()) >= CostPerUseLimit) { |
| 1125 | OrderLimit = RegClassInfo.getLastCostChange(RC); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1126 | LLVM_DEBUG(dbgs() << "Only trying the first " << OrderLimit |
| 1127 | << " regs.\n"); |
Jakob Stoklund Olesen | 6d61329 | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 1128 | } |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1131 | Order.rewind(); |
Aditya Nandakumar | 226e3ea | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 1132 | while (unsigned PhysReg = Order.next(OrderLimit)) { |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 1133 | if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit) |
| 1134 | continue; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1135 | // The first use of a callee-saved register in a function has cost 1. |
| 1136 | // Don't start using a CSR when the CostPerUseLimit is low. |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 1137 | if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1138 | LLVM_DEBUG( |
| 1139 | dbgs() << printReg(PhysReg, TRI) << " would clobber CSR " |
| 1140 | << printReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI) |
| 1141 | << '\n'); |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 1142 | continue; |
| 1143 | } |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 1144 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1145 | if (!canEvictInterference(VirtReg, PhysReg, false, BestCost)) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1146 | continue; |
| 1147 | |
| 1148 | // Best so far. |
| 1149 | BestPhys = PhysReg; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1150 | |
Jakob Stoklund Olesen | 57f1e2c | 2011-02-25 01:04:22 +0000 | [diff] [blame] | 1151 | // Stop if the hint can be used. |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 1152 | if (Order.isHint()) |
Jakob Stoklund Olesen | 57f1e2c | 2011-02-25 01:04:22 +0000 | [diff] [blame] | 1153 | break; |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1156 | if (!BestPhys) |
| 1157 | return 0; |
| 1158 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 1159 | evictInterference(VirtReg, BestPhys, NewVRegs); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 1160 | return BestPhys; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 1163 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1164 | // Region Splitting |
| 1165 | //===----------------------------------------------------------------------===// |
| 1166 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1167 | /// addSplitConstraints - Fill out the SplitConstraints vector based on the |
| 1168 | /// interference pattern in Physreg and its aliases. Add the constraints to |
| 1169 | /// SpillPlacement and return the static cost of this split in Cost, assuming |
| 1170 | /// that all preferences in SplitConstraints are met. |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1171 | /// Return false if there are no bundles with positive bias. |
| 1172 | bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1173 | BlockFrequency &Cost) { |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1174 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 1175 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1176 | // Reset interference dependent info. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1177 | SplitConstraints.resize(UseBlocks.size()); |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1178 | BlockFrequency StaticCost = 0; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1179 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1180 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1181 | SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1182 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 1183 | BC.Number = BI.MBB->getNumber(); |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 1184 | Intf.moveToBlock(BC.Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1185 | BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; |
John Brawn | 98689c0 | 2018-12-14 14:07:57 +0000 | [diff] [blame] | 1186 | BC.Exit = (BI.LiveOut && |
| 1187 | !LIS->getInstructionFromIndex(BI.LastInstr)->isImplicitDef()) |
| 1188 | ? SpillPlacement::PrefReg |
| 1189 | : SpillPlacement::DontCare; |
David Blaikie | 453f4f0 | 2013-05-15 07:36:59 +0000 | [diff] [blame] | 1190 | BC.ChangesValue = BI.FirstDef.isValid(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1191 | |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 1192 | if (!Intf.hasInterference()) |
| 1193 | continue; |
| 1194 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1195 | // Number of spill code instructions to insert. |
| 1196 | unsigned Ins = 0; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1197 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1198 | // Interference for the live-in value. |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 1199 | if (BI.LiveIn) { |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1200 | if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) { |
| 1201 | BC.Entry = SpillPlacement::MustSpill; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1202 | ++Ins; |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1203 | } else if (Intf.first() < BI.FirstInstr) { |
| 1204 | BC.Entry = SpillPlacement::PrefSpill; |
| 1205 | ++Ins; |
| 1206 | } else if (Intf.first() < BI.LastInstr) { |
| 1207 | ++Ins; |
| 1208 | } |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1209 | |
| 1210 | // Abort if the spill cannot be inserted at the MBB' start |
| 1211 | if (((BC.Entry == SpillPlacement::MustSpill) || |
| 1212 | (BC.Entry == SpillPlacement::PrefSpill)) && |
| 1213 | SlotIndex::isEarlierInstr(BI.FirstInstr, |
| 1214 | SA->getFirstSplitPoint(BC.Number))) |
| 1215 | return false; |
Jakob Stoklund Olesen | a50c539 | 2011-02-08 23:02:58 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1218 | // Interference for the live-out value. |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 1219 | if (BI.LiveOut) { |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1220 | if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) { |
| 1221 | BC.Exit = SpillPlacement::MustSpill; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1222 | ++Ins; |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1223 | } else if (Intf.last() > BI.LastInstr) { |
| 1224 | BC.Exit = SpillPlacement::PrefSpill; |
| 1225 | ++Ins; |
| 1226 | } else if (Intf.last() > BI.FirstInstr) { |
| 1227 | ++Ins; |
| 1228 | } |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1229 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Accumulate the total frequency of inserted spill code. |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1232 | while (Ins--) |
| 1233 | StaticCost += SpillPlacer->getBlockFrequency(BC.Number); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1234 | } |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1235 | Cost = StaticCost; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1236 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1237 | // Add constraints for use-blocks. Note that these are the only constraints |
| 1238 | // that may add a positive bias, it is downhill from here. |
| 1239 | SpillPlacer->addConstraints(SplitConstraints); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1240 | return SpillPlacer->scanActiveBundles(); |
| 1241 | } |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1242 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1243 | /// addThroughConstraints - Add constraints and links to SpillPlacer from the |
| 1244 | /// live-through blocks in Blocks. |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1245 | bool RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1246 | ArrayRef<unsigned> Blocks) { |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1247 | const unsigned GroupSize = 8; |
| 1248 | SpillPlacement::BlockConstraint BCS[GroupSize]; |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1249 | unsigned TBS[GroupSize]; |
| 1250 | unsigned B = 0, T = 0; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1251 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1252 | for (unsigned i = 0; i != Blocks.size(); ++i) { |
| 1253 | unsigned Number = Blocks[i]; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1254 | Intf.moveToBlock(Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1255 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 1256 | if (!Intf.hasInterference()) { |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1257 | assert(T < GroupSize && "Array overflow"); |
| 1258 | TBS[T] = Number; |
| 1259 | if (++T == GroupSize) { |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 1260 | SpillPlacer->addLinks(makeArrayRef(TBS, T)); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1261 | T = 0; |
| 1262 | } |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 1263 | continue; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1264 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1265 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1266 | assert(B < GroupSize && "Array overflow"); |
| 1267 | BCS[B].Number = Number; |
| 1268 | |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1269 | // Abort if the spill cannot be inserted at the MBB' start |
| 1270 | MachineBasicBlock *MBB = MF->getBlockNumbered(Number); |
| 1271 | if (!MBB->empty() && |
| 1272 | SlotIndex::isEarlierInstr(LIS->getInstructionIndex(MBB->instr_front()), |
| 1273 | SA->getFirstSplitPoint(Number))) |
| 1274 | return false; |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 1275 | // Interference for the live-in value. |
| 1276 | if (Intf.first() <= Indexes->getMBBStartIdx(Number)) |
| 1277 | BCS[B].Entry = SpillPlacement::MustSpill; |
| 1278 | else |
| 1279 | BCS[B].Entry = SpillPlacement::PrefSpill; |
| 1280 | |
| 1281 | // Interference for the live-out value. |
| 1282 | if (Intf.last() >= SA->getLastSplitPoint(Number)) |
| 1283 | BCS[B].Exit = SpillPlacement::MustSpill; |
| 1284 | else |
| 1285 | BCS[B].Exit = SpillPlacement::PrefSpill; |
| 1286 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1287 | if (++B == GroupSize) { |
Craig Topper | 3512034 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1288 | SpillPlacer->addConstraints(makeArrayRef(BCS, B)); |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1289 | B = 0; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1290 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Craig Topper | 3512034 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1293 | SpillPlacer->addConstraints(makeArrayRef(BCS, B)); |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 1294 | SpillPlacer->addLinks(makeArrayRef(TBS, T)); |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1295 | return true; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1298 | bool RAGreedy::growRegion(GlobalSplitCandidate &Cand) { |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1299 | // Keep track of through blocks that have not been added to SpillPlacer. |
| 1300 | BitVector Todo = SA->getThroughBlocks(); |
| 1301 | SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; |
| 1302 | unsigned AddedTo = 0; |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1303 | #ifndef NDEBUG |
| 1304 | unsigned Visited = 0; |
| 1305 | #endif |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1306 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1307 | while (true) { |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1308 | ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1309 | // Find new through blocks in the periphery of PrefRegBundles. |
| 1310 | for (int i = 0, e = NewBundles.size(); i != e; ++i) { |
| 1311 | unsigned Bundle = NewBundles[i]; |
| 1312 | // Look at all blocks connected to Bundle in the full graph. |
| 1313 | ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); |
| 1314 | for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); |
| 1315 | I != E; ++I) { |
| 1316 | unsigned Block = *I; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1317 | if (!Todo.test(Block)) |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1318 | continue; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1319 | Todo.reset(Block); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1320 | // This is a new through block. Add it to SpillPlacer later. |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1321 | ActiveBlocks.push_back(Block); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1322 | #ifndef NDEBUG |
| 1323 | ++Visited; |
| 1324 | #endif |
| 1325 | } |
| 1326 | } |
| 1327 | // Any new blocks to add? |
Jakob Stoklund Olesen | 5490197 | 2011-07-05 18:46:42 +0000 | [diff] [blame] | 1328 | if (ActiveBlocks.size() == AddedTo) |
| 1329 | break; |
Jakob Stoklund Olesen | b466636 | 2011-07-23 03:22:33 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Compute through constraints from the interference, or assume that all |
| 1332 | // through blocks prefer spilling when forming compact regions. |
Craig Topper | 3512034 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1333 | auto NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo); |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1334 | if (Cand.PhysReg) { |
| 1335 | if (!addThroughConstraints(Cand.Intf, NewBlocks)) |
| 1336 | return false; |
| 1337 | } else |
Jakob Stoklund Olesen | b87f91b | 2011-08-03 23:09:38 +0000 | [diff] [blame] | 1338 | // Provide a strong negative bias on through blocks to prevent unwanted |
| 1339 | // liveness on loop backedges. |
| 1340 | SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true); |
Jakob Stoklund Olesen | 5490197 | 2011-07-05 18:46:42 +0000 | [diff] [blame] | 1341 | AddedTo = ActiveBlocks.size(); |
| 1342 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1343 | // Perhaps iterating can enable more bundles? |
| 1344 | SpillPlacer->iterate(); |
| 1345 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1346 | LLVM_DEBUG(dbgs() << ", v=" << Visited); |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1347 | return true; |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1348 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1349 | |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1350 | /// calcCompactRegion - Compute the set of edge bundles that should be live |
| 1351 | /// when splitting the current live range into compact regions. Compact |
| 1352 | /// regions can be computed without looking at interference. They are the |
| 1353 | /// regions formed by removing all the live-through blocks from the live range. |
| 1354 | /// |
| 1355 | /// Returns false if the current live range is already compact, or if the |
| 1356 | /// compact regions would form single block regions anyway. |
| 1357 | bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) { |
| 1358 | // Without any through blocks, the live range is already compact. |
| 1359 | if (!SA->getNumThroughBlocks()) |
| 1360 | return false; |
| 1361 | |
| 1362 | // Compact regions don't correspond to any physreg. |
| 1363 | Cand.reset(IntfCache, 0); |
| 1364 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1365 | LLVM_DEBUG(dbgs() << "Compact region bundles"); |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Use the spill placer to determine the live bundles. GrowRegion pretends |
| 1368 | // that all the through blocks have interference when PhysReg is unset. |
| 1369 | SpillPlacer->prepare(Cand.LiveBundles); |
| 1370 | |
| 1371 | // The static split cost will be zero since Cand.Intf reports no interference. |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1372 | BlockFrequency Cost; |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1373 | if (!addSplitConstraints(Cand.Intf, Cost)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1374 | LLVM_DEBUG(dbgs() << ", none.\n"); |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1375 | return false; |
| 1376 | } |
| 1377 | |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1378 | if (!growRegion(Cand)) { |
| 1379 | LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n"); |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1383 | SpillPlacer->finish(); |
| 1384 | |
| 1385 | if (!Cand.LiveBundles.any()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1386 | LLVM_DEBUG(dbgs() << ", none.\n"); |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1390 | LLVM_DEBUG({ |
Francis Visoiu Mistrih | 1179b5e | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 1391 | for (int i : Cand.LiveBundles.set_bits()) |
| 1392 | dbgs() << " EB#" << i; |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 1393 | dbgs() << ".\n"; |
| 1394 | }); |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1398 | /// calcSpillCost - Compute how expensive it would be to split the live range in |
| 1399 | /// SA around all use blocks instead of forming bundle regions. |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1400 | BlockFrequency RAGreedy::calcSpillCost() { |
| 1401 | BlockFrequency Cost = 0; |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1402 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 1403 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1404 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
| 1405 | unsigned Number = BI.MBB->getNumber(); |
| 1406 | // We normally only need one spill instruction - a load or a store. |
| 1407 | Cost += SpillPlacer->getBlockFrequency(Number); |
| 1408 | |
| 1409 | // Unless the value is redefined in the block. |
Jakob Stoklund Olesen | 3f5beed | 2011-08-02 23:04:08 +0000 | [diff] [blame] | 1410 | if (BI.LiveIn && BI.LiveOut && BI.FirstDef) |
| 1411 | Cost += SpillPlacer->getBlockFrequency(Number); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1412 | } |
| 1413 | return Cost; |
| 1414 | } |
| 1415 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1416 | /// Check if splitting Evictee will create a local split interval in |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1417 | /// basic block number BBNumber that may cause a bad eviction chain. This is |
| 1418 | /// intended to prevent bad eviction sequences like: |
| 1419 | /// movl %ebp, 8(%esp) # 4-byte Spill |
| 1420 | /// movl %ecx, %ebp |
| 1421 | /// movl %ebx, %ecx |
| 1422 | /// movl %edi, %ebx |
| 1423 | /// movl %edx, %edi |
| 1424 | /// cltd |
| 1425 | /// idivl %esi |
| 1426 | /// movl %edi, %edx |
| 1427 | /// movl %ebx, %edi |
| 1428 | /// movl %ecx, %ebx |
| 1429 | /// movl %ebp, %ecx |
| 1430 | /// movl 16(%esp), %ebp # 4 - byte Reload |
| 1431 | /// |
| 1432 | /// Such sequences are created in 2 scenarios: |
| 1433 | /// |
| 1434 | /// Scenario #1: |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1435 | /// %0 is evicted from physreg0 by %1. |
| 1436 | /// Evictee %0 is intended for region splitting with split candidate |
| 1437 | /// physreg0 (the reg %0 was evicted from). |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1438 | /// Region splitting creates a local interval because of interference with the |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 1439 | /// evictor %1 (normally region splitting creates 2 interval, the "by reg" |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1440 | /// and "by stack" intervals and local interval created when interference |
| 1441 | /// occurs). |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1442 | /// One of the split intervals ends up evicting %2 from physreg1. |
| 1443 | /// Evictee %2 is intended for region splitting with split candidate |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1444 | /// physreg1. |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1445 | /// One of the split intervals ends up evicting %3 from physreg2, etc. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1446 | /// |
| 1447 | /// Scenario #2 |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1448 | /// %0 is evicted from physreg0 by %1. |
| 1449 | /// %2 is evicted from physreg2 by %3 etc. |
| 1450 | /// Evictee %0 is intended for region splitting with split candidate |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1451 | /// physreg1. |
| 1452 | /// Region splitting creates a local interval because of interference with the |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1453 | /// evictor %1. |
| 1454 | /// One of the split intervals ends up evicting back original evictor %1 |
| 1455 | /// from physreg0 (the reg %0 was evicted from). |
| 1456 | /// Another evictee %2 is intended for region splitting with split candidate |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1457 | /// physreg1. |
Francis Visoiu Mistrih | 7384652 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 1458 | /// One of the split intervals ends up evicting %3 from physreg2, etc. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1459 | /// |
| 1460 | /// \param Evictee The register considered to be split. |
| 1461 | /// \param Cand The split candidate that determines the physical register |
| 1462 | /// we are splitting for and the interferences. |
| 1463 | /// \param BBNumber The number of a BB for which the region split process will |
| 1464 | /// create a local split interval. |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1465 | /// \param Order The physical registers that may get evicted by a split |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1466 | /// artifact of Evictee. |
| 1467 | /// \return True if splitting Evictee may cause a bad eviction chain, false |
| 1468 | /// otherwise. |
| 1469 | bool RAGreedy::splitCanCauseEvictionChain(unsigned Evictee, |
| 1470 | GlobalSplitCandidate &Cand, |
| 1471 | unsigned BBNumber, |
| 1472 | const AllocationOrder &Order) { |
| 1473 | EvictionTrack::EvictorInfo VregEvictorInfo = LastEvicted.getEvictor(Evictee); |
| 1474 | unsigned Evictor = VregEvictorInfo.first; |
| 1475 | unsigned PhysReg = VregEvictorInfo.second; |
| 1476 | |
| 1477 | // No actual evictor. |
| 1478 | if (!Evictor || !PhysReg) |
| 1479 | return false; |
| 1480 | |
| 1481 | float MaxWeight = 0; |
| 1482 | unsigned FutureEvictedPhysReg = |
| 1483 | getCheapestEvicteeWeight(Order, LIS->getInterval(Evictee), |
| 1484 | Cand.Intf.first(), Cand.Intf.last(), &MaxWeight); |
| 1485 | |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1486 | // The bad eviction chain occurs when either the split candidate is the |
| 1487 | // evicting reg or one of the split artifact will evict the evicting reg. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1488 | if ((PhysReg != Cand.PhysReg) && (PhysReg != FutureEvictedPhysReg)) |
| 1489 | return false; |
| 1490 | |
| 1491 | Cand.Intf.moveToBlock(BBNumber); |
| 1492 | |
| 1493 | // Check to see if the Evictor contains interference (with Evictee) in the |
| 1494 | // given BB. If so, this interference caused the eviction of Evictee from |
| 1495 | // PhysReg. This suggest that we will create a local interval during the |
| 1496 | // region split to avoid this interference This local interval may cause a bad |
| 1497 | // eviction chain. |
| 1498 | if (!LIS->hasInterval(Evictor)) |
| 1499 | return false; |
| 1500 | LiveInterval &EvictorLI = LIS->getInterval(Evictor); |
| 1501 | if (EvictorLI.FindSegmentContaining(Cand.Intf.first()) == EvictorLI.end()) |
| 1502 | return false; |
| 1503 | |
| 1504 | // Now, check to see if the local interval we will create is going to be |
| 1505 | // expensive enough to evict somebody If so, this may cause a bad eviction |
| 1506 | // chain. |
| 1507 | VirtRegAuxInfo VRAI(*MF, *LIS, VRM, getAnalysis<MachineLoopInfo>(), *MBFI); |
| 1508 | float splitArtifactWeight = |
| 1509 | VRAI.futureWeight(LIS->getInterval(Evictee), |
| 1510 | Cand.Intf.first().getPrevIndex(), Cand.Intf.last()); |
| 1511 | if (splitArtifactWeight >= 0 && splitArtifactWeight < MaxWeight) |
| 1512 | return false; |
| 1513 | |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1517 | /// Check if splitting VirtRegToSplit will create a local split interval |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1518 | /// in basic block number BBNumber that may cause a spill. |
| 1519 | /// |
| 1520 | /// \param VirtRegToSplit The register considered to be split. |
| 1521 | /// \param Cand The split candidate that determines the physical |
| 1522 | /// register we are splitting for and the interferences. |
| 1523 | /// \param BBNumber The number of a BB for which the region split process |
| 1524 | /// will create a local split interval. |
| 1525 | /// \param Order The physical registers that may get evicted by a |
| 1526 | /// split artifact of VirtRegToSplit. |
| 1527 | /// \return True if splitting VirtRegToSplit may cause a spill, false |
| 1528 | /// otherwise. |
| 1529 | bool RAGreedy::splitCanCauseLocalSpill(unsigned VirtRegToSplit, |
| 1530 | GlobalSplitCandidate &Cand, |
| 1531 | unsigned BBNumber, |
| 1532 | const AllocationOrder &Order) { |
| 1533 | Cand.Intf.moveToBlock(BBNumber); |
| 1534 | |
| 1535 | // Check if the local interval will find a non interfereing assignment. |
| 1536 | for (auto PhysReg : Order.getOrder()) { |
| 1537 | if (!Matrix->checkInterference(Cand.Intf.first().getPrevIndex(), |
| 1538 | Cand.Intf.last(), PhysReg)) |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
| 1542 | // Check if the local interval will evict a cheaper interval. |
| 1543 | float CheapestEvictWeight = 0; |
| 1544 | unsigned FutureEvictedPhysReg = getCheapestEvicteeWeight( |
| 1545 | Order, LIS->getInterval(VirtRegToSplit), Cand.Intf.first(), |
| 1546 | Cand.Intf.last(), &CheapestEvictWeight); |
| 1547 | |
| 1548 | // Have we found an interval that can be evicted? |
| 1549 | if (FutureEvictedPhysReg) { |
| 1550 | VirtRegAuxInfo VRAI(*MF, *LIS, VRM, getAnalysis<MachineLoopInfo>(), *MBFI); |
| 1551 | float splitArtifactWeight = |
| 1552 | VRAI.futureWeight(LIS->getInterval(VirtRegToSplit), |
| 1553 | Cand.Intf.first().getPrevIndex(), Cand.Intf.last()); |
| 1554 | // Will the weight of the local interval be higher than the cheapest evictee |
| 1555 | // weight? If so it will evict it and will not cause a spill. |
| 1556 | if (splitArtifactWeight >= 0 && splitArtifactWeight > CheapestEvictWeight) |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 1560 | // The local interval is not able to find non interferencing assignment and |
| 1561 | // not able to evict a less worthy interval, therfore, it can cause a spill. |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
| 1564 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1565 | /// calcGlobalSplitCost - Return the global split cost of following the split |
| 1566 | /// pattern in LiveBundles. This cost should be added to the local cost of the |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1567 | /// interference pattern in SplitConstraints. |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1568 | /// |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1569 | BlockFrequency RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand, |
| 1570 | const AllocationOrder &Order, |
| 1571 | bool *CanCauseEvictionChain) { |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1572 | BlockFrequency GlobalCost = 0; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1573 | const BitVector &LiveBundles = Cand.LiveBundles; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1574 | unsigned VirtRegToSplit = SA->getParent().reg; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1575 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 1576 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1577 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1578 | SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1579 | bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, false)]; |
| 1580 | bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, true)]; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1581 | unsigned Ins = 0; |
| 1582 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1583 | Cand.Intf.moveToBlock(BC.Number); |
| 1584 | // Check wheather a local interval is going to be created during the region |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1585 | // split. Calculate adavanced spilt cost (cost of local intervals) if option |
| 1586 | // is enabled. |
| 1587 | if (EnableAdvancedRASplitCost && Cand.Intf.hasInterference() && BI.LiveIn && |
| 1588 | BI.LiveOut && RegIn && RegOut) { |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1589 | |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1590 | if (CanCauseEvictionChain && |
| 1591 | splitCanCauseEvictionChain(VirtRegToSplit, Cand, BC.Number, Order)) { |
| 1592 | // This interference causes our eviction from this assignment, we might |
| 1593 | // evict somebody else and eventually someone will spill, add that cost. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1594 | // See splitCanCauseEvictionChain for detailed description of scenarios. |
| 1595 | GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); |
| 1596 | GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); |
| 1597 | |
| 1598 | *CanCauseEvictionChain = true; |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1599 | |
| 1600 | } else if (splitCanCauseLocalSpill(VirtRegToSplit, Cand, BC.Number, |
| 1601 | Order)) { |
| 1602 | // This interference causes local interval to spill, add that cost. |
| 1603 | GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); |
| 1604 | GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1608 | if (BI.LiveIn) |
| 1609 | Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); |
| 1610 | if (BI.LiveOut) |
| 1611 | Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1612 | while (Ins--) |
| 1613 | GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1614 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1615 | |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 1616 | for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { |
| 1617 | unsigned Number = Cand.ActiveBlocks[i]; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1618 | bool RegIn = LiveBundles[Bundles->getBundle(Number, false)]; |
| 1619 | bool RegOut = LiveBundles[Bundles->getBundle(Number, true)]; |
Jakob Stoklund Olesen | 9a54352 | 2011-04-06 21:32:41 +0000 | [diff] [blame] | 1620 | if (!RegIn && !RegOut) |
| 1621 | continue; |
| 1622 | if (RegIn && RegOut) { |
| 1623 | // We need double spill code if this block has interference. |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1624 | Cand.Intf.moveToBlock(Number); |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1625 | if (Cand.Intf.hasInterference()) { |
| 1626 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
| 1627 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Check wheather a local interval is going to be created during the |
| 1630 | // region split. |
| 1631 | if (EnableAdvancedRASplitCost && CanCauseEvictionChain && |
| 1632 | splitCanCauseEvictionChain(VirtRegToSplit, Cand, Number, Order)) { |
Marina Yatsina | 1eecb87 | 2018-01-31 13:31:08 +0000 | [diff] [blame] | 1633 | // This interference cause our eviction from this assignment, we might |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1634 | // evict somebody else, add that cost. |
| 1635 | // See splitCanCauseEvictionChain for detailed description of |
| 1636 | // scenarios. |
| 1637 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
| 1638 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
| 1639 | |
| 1640 | *CanCauseEvictionChain = true; |
| 1641 | } |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1642 | } |
Jakob Stoklund Olesen | 9a54352 | 2011-04-06 21:32:41 +0000 | [diff] [blame] | 1643 | continue; |
| 1644 | } |
| 1645 | // live-in / stack-out or stack-in live-out. |
| 1646 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1647 | } |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1648 | return GlobalCost; |
| 1649 | } |
| 1650 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1651 | /// splitAroundRegion - Split the current live range around the regions |
| 1652 | /// determined by BundleCand and GlobalCand. |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1653 | /// |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1654 | /// Before calling this function, GlobalCand and BundleCand must be initialized |
| 1655 | /// so each bundle is assigned to a valid candidate, or NoCand for the |
| 1656 | /// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor |
| 1657 | /// objects must be initialized for the current live range, and intervals |
| 1658 | /// created for the used candidates. |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1659 | /// |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1660 | /// @param LREdit The LiveRangeEdit object handling the current split. |
| 1661 | /// @param UsedCands List of used GlobalCand entries. Every BundleCand value |
| 1662 | /// must appear in this list. |
| 1663 | void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit, |
| 1664 | ArrayRef<unsigned> UsedCands) { |
| 1665 | // These are the intervals created for new global ranges. We may create more |
| 1666 | // intervals for local ranges. |
| 1667 | const unsigned NumGlobalIntvs = LREdit.size(); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1668 | LLVM_DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs |
| 1669 | << " globals.\n"); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1670 | assert(NumGlobalIntvs && "No global intervals configured"); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1671 | |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 1672 | // Isolate even single instructions when dealing with a proper sub-class. |
Jakob Stoklund Olesen | 69145ba | 2011-08-06 18:20:24 +0000 | [diff] [blame] | 1673 | // That guarantees register class inflation for the stack interval because it |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 1674 | // is all copies. |
| 1675 | unsigned Reg = SA->getParent().reg; |
| 1676 | bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); |
| 1677 | |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 1678 | // First handle all the blocks with uses. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1679 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 1680 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1681 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1682 | unsigned Number = BI.MBB->getNumber(); |
| 1683 | unsigned IntvIn = 0, IntvOut = 0; |
| 1684 | SlotIndex IntfIn, IntfOut; |
| 1685 | if (BI.LiveIn) { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1686 | unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1687 | if (CandIn != NoCand) { |
| 1688 | GlobalSplitCandidate &Cand = GlobalCand[CandIn]; |
| 1689 | IntvIn = Cand.IntvIdx; |
| 1690 | Cand.Intf.moveToBlock(Number); |
| 1691 | IntfIn = Cand.Intf.first(); |
| 1692 | } |
| 1693 | } |
| 1694 | if (BI.LiveOut) { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1695 | unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1696 | if (CandOut != NoCand) { |
| 1697 | GlobalSplitCandidate &Cand = GlobalCand[CandOut]; |
| 1698 | IntvOut = Cand.IntvIdx; |
| 1699 | Cand.Intf.moveToBlock(Number); |
| 1700 | IntfOut = Cand.Intf.last(); |
| 1701 | } |
| 1702 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1703 | |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1704 | // Create separate intervals for isolated blocks with multiple uses. |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1705 | if (!IntvIn && !IntvOut) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1706 | LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " isolated.\n"); |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 1707 | if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 1708 | SE->splitSingleBlock(BI); |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1709 | continue; |
| 1710 | } |
| 1711 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1712 | if (IntvIn && IntvOut) |
| 1713 | SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); |
| 1714 | else if (IntvIn) |
| 1715 | SE->splitRegInBlock(BI, IntvIn, IntfIn); |
Jakob Stoklund Olesen | b4ddedc | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1716 | else |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1717 | SE->splitRegOutBlock(BI, IntvOut, IntfOut); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1720 | // Handle live-through blocks. The relevant live-through blocks are stored in |
| 1721 | // the ActiveBlocks list with each candidate. We need to filter out |
| 1722 | // duplicates. |
| 1723 | BitVector Todo = SA->getThroughBlocks(); |
| 1724 | for (unsigned c = 0; c != UsedCands.size(); ++c) { |
| 1725 | ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks; |
| 1726 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 1727 | unsigned Number = Blocks[i]; |
| 1728 | if (!Todo.test(Number)) |
| 1729 | continue; |
| 1730 | Todo.reset(Number); |
| 1731 | |
| 1732 | unsigned IntvIn = 0, IntvOut = 0; |
| 1733 | SlotIndex IntfIn, IntfOut; |
| 1734 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1735 | unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1736 | if (CandIn != NoCand) { |
| 1737 | GlobalSplitCandidate &Cand = GlobalCand[CandIn]; |
| 1738 | IntvIn = Cand.IntvIdx; |
| 1739 | Cand.Intf.moveToBlock(Number); |
| 1740 | IntfIn = Cand.Intf.first(); |
| 1741 | } |
| 1742 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1743 | unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1744 | if (CandOut != NoCand) { |
| 1745 | GlobalSplitCandidate &Cand = GlobalCand[CandOut]; |
| 1746 | IntvOut = Cand.IntvIdx; |
| 1747 | Cand.Intf.moveToBlock(Number); |
| 1748 | IntfOut = Cand.Intf.last(); |
| 1749 | } |
| 1750 | if (!IntvIn && !IntvOut) |
| 1751 | continue; |
| 1752 | SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); |
| 1753 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 1756 | ++NumGlobalSplits; |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1757 | |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1758 | SmallVector<unsigned, 8> IntvMap; |
| 1759 | SE->finish(&IntvMap); |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1760 | DebugVars->splitRegister(Reg, LREdit.regs(), *LIS); |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 1761 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1762 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 1763 | unsigned OrigBlocks = SA->getNumLiveBlocks(); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Sort out the new intervals created by splitting. We get four kinds: |
| 1766 | // - Remainder intervals should not be split again. |
| 1767 | // - Candidate intervals can be assigned to Cand.PhysReg. |
| 1768 | // - Block-local splits are candidates for local splitting. |
| 1769 | // - DCE leftovers should go back on the queue. |
| 1770 | for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1771 | LiveInterval &Reg = LIS->getInterval(LREdit.get(i)); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Ignore old intervals from DCE. |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1774 | if (getStage(Reg) != RS_New) |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1775 | continue; |
| 1776 | |
| 1777 | // Remainder interval. Don't try splitting again, spill if it doesn't |
| 1778 | // allocate. |
| 1779 | if (IntvMap[i] == 0) { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1780 | setStage(Reg, RS_Spill); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1781 | continue; |
| 1782 | } |
| 1783 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1784 | // Global intervals. Allow repeated splitting as long as the number of live |
| 1785 | // blocks is strictly decreasing. |
| 1786 | if (IntvMap[i] < NumGlobalIntvs) { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1787 | if (SA->countLiveBlocks(&Reg) >= OrigBlocks) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1788 | LLVM_DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks |
| 1789 | << " blocks as original.\n"); |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 1790 | // Don't allow repeated splitting as a safe guard against looping. |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1791 | setStage(Reg, RS_Split2); |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 1792 | } |
| 1793 | continue; |
| 1794 | } |
| 1795 | |
| 1796 | // Other intervals are treated as new. This includes local intervals created |
| 1797 | // for blocks with multiple uses, and anything created by DCE. |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 1800 | if (VerifyEnabled) |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1801 | MF->verify(this, "After splitting live range around region"); |
| 1802 | } |
| 1803 | |
Wei Mi | 735bf22 | 2018-07-16 15:42:20 +0000 | [diff] [blame] | 1804 | // Global split has high compile time cost especially for large live range. |
| 1805 | // Return false for the case here where the potential benefit will never |
| 1806 | // worth the cost. |
| 1807 | unsigned RAGreedy::isSplitBenefitWorthCost(LiveInterval &VirtReg) { |
| 1808 | MachineInstr *MI = MRI->getUniqueVRegDef(VirtReg.reg); |
| 1809 | if (MI && TII->isTriviallyReMaterializable(*MI, AA) && |
| 1810 | VirtReg.size() > HugeSizeForSplit) |
| 1811 | return false; |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1815 | unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 1816 | SmallVectorImpl<unsigned> &NewVRegs) { |
Wei Mi | 735bf22 | 2018-07-16 15:42:20 +0000 | [diff] [blame] | 1817 | if (!isSplitBenefitWorthCost(VirtReg)) |
| 1818 | return 0; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1819 | unsigned NumCands = 0; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1820 | BlockFrequency SpillCost = calcSpillCost(); |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1821 | BlockFrequency BestCost; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1822 | |
| 1823 | // Check if we can split this live range around a compact region. |
Jakob Stoklund Olesen | a16a25d | 2011-09-12 16:54:42 +0000 | [diff] [blame] | 1824 | bool HasCompact = calcCompactRegion(GlobalCand.front()); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1825 | if (HasCompact) { |
| 1826 | // Yes, keep GlobalCand[0] as the compact region candidate. |
| 1827 | NumCands = 1; |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1828 | BestCost = BlockFrequency::getMaxFrequency(); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1829 | } else { |
| 1830 | // No benefit from the compact region, our fallback will be per-block |
| 1831 | // splitting. Make sure we find a solution that is cheaper than spilling. |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1832 | BestCost = SpillCost; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1833 | LLVM_DEBUG(dbgs() << "Cost of isolating all blocks = "; |
| 1834 | MBFI->printBlockFreq(dbgs(), BestCost) << '\n'); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1835 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1836 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1837 | bool CanCauseEvictionChain = false; |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 1838 | unsigned BestCand = |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 1839 | calculateRegionSplitCost(VirtReg, Order, BestCost, NumCands, |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1840 | false /*IgnoreCSR*/, &CanCauseEvictionChain); |
| 1841 | |
| 1842 | // Split candidates with compact regions can cause a bad eviction sequence. |
| 1843 | // See splitCanCauseEvictionChain for detailed description of scenarios. |
| 1844 | // To avoid it, we need to comapre the cost with the spill cost and not the |
| 1845 | // current max frequency. |
| 1846 | if (HasCompact && (BestCost > SpillCost) && (BestCand != NoCand) && |
| 1847 | CanCauseEvictionChain) { |
| 1848 | return 0; |
| 1849 | } |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 1850 | |
| 1851 | // No solutions found, fall back to single block splitting. |
| 1852 | if (!HasCompact && BestCand == NoCand) |
| 1853 | return 0; |
| 1854 | |
| 1855 | return doRegionSplit(VirtReg, BestCand, HasCompact, NewVRegs); |
| 1856 | } |
| 1857 | |
| 1858 | unsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg, |
| 1859 | AllocationOrder &Order, |
| 1860 | BlockFrequency &BestCost, |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1861 | unsigned &NumCands, bool IgnoreCSR, |
| 1862 | bool *CanCauseEvictionChain) { |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 1863 | unsigned BestCand = NoCand; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1864 | Order.rewind(); |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1865 | while (unsigned PhysReg = Order.next()) { |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 1866 | if (IgnoreCSR && isUnusedCalleeSavedReg(PhysReg)) |
| 1867 | continue; |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 1868 | |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1869 | // Discard bad candidates before we run out of interference cache cursors. |
| 1870 | // This will only affect register classes with a lot of registers (>32). |
| 1871 | if (NumCands == IntfCache.getMaxCursors()) { |
| 1872 | unsigned WorstCount = ~0u; |
| 1873 | unsigned Worst = 0; |
| 1874 | for (unsigned i = 0; i != NumCands; ++i) { |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1875 | if (i == BestCand || !GlobalCand[i].PhysReg) |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1876 | continue; |
| 1877 | unsigned Count = GlobalCand[i].LiveBundles.count(); |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1878 | if (Count < WorstCount) { |
| 1879 | Worst = i; |
| 1880 | WorstCount = Count; |
| 1881 | } |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1882 | } |
| 1883 | --NumCands; |
| 1884 | GlobalCand[Worst] = GlobalCand[NumCands]; |
Jakob Stoklund Olesen | 7bdf006 | 2011-11-01 00:02:31 +0000 | [diff] [blame] | 1885 | if (BestCand == NumCands) |
| 1886 | BestCand = Worst; |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1889 | if (GlobalCand.size() <= NumCands) |
| 1890 | GlobalCand.resize(NumCands+1); |
| 1891 | GlobalSplitCandidate &Cand = GlobalCand[NumCands]; |
| 1892 | Cand.reset(IntfCache, PhysReg); |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1893 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1894 | SpillPlacer->prepare(Cand.LiveBundles); |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1895 | BlockFrequency Cost; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1896 | if (!addSplitConstraints(Cand.Intf, Cost)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1897 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tno positive bundles\n"); |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1898 | continue; |
| 1899 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1900 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tstatic = "; |
| 1901 | MBFI->printBlockFreq(dbgs(), Cost)); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1902 | if (Cost >= BestCost) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1903 | LLVM_DEBUG({ |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1904 | if (BestCand == NoCand) |
| 1905 | dbgs() << " worse than no bundles\n"; |
| 1906 | else |
| 1907 | dbgs() << " worse than " |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1908 | << printReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'; |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1909 | }); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1910 | continue; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1911 | } |
Daniil Fukalov | 5bcb2da | 2018-09-25 18:37:38 +0000 | [diff] [blame] | 1912 | if (!growRegion(Cand)) { |
| 1913 | LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n"); |
| 1914 | continue; |
| 1915 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1916 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 1917 | SpillPlacer->finish(); |
| 1918 | |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1919 | // No live bundles, defer to splitSingleBlocks(). |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1920 | if (!Cand.LiveBundles.any()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1921 | LLVM_DEBUG(dbgs() << " no bundles.\n"); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1922 | continue; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1923 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1924 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1925 | bool HasEvictionChain = false; |
| 1926 | Cost += calcGlobalSplitCost(Cand, Order, &HasEvictionChain); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1927 | LLVM_DEBUG({ |
| 1928 | dbgs() << ", total = "; |
| 1929 | MBFI->printBlockFreq(dbgs(), Cost) << " with bundles"; |
Francis Visoiu Mistrih | 1179b5e | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 1930 | for (int i : Cand.LiveBundles.set_bits()) |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1931 | dbgs() << " EB#" << i; |
| 1932 | dbgs() << ".\n"; |
| 1933 | }); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1934 | if (Cost < BestCost) { |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1935 | BestCand = NumCands; |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 1936 | BestCost = Cost; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1937 | // See splitCanCauseEvictionChain for detailed description of bad |
| 1938 | // eviction chain scenarios. |
| 1939 | if (CanCauseEvictionChain) |
| 1940 | *CanCauseEvictionChain = HasEvictionChain; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1941 | } |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1942 | ++NumCands; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1943 | } |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (CanCauseEvictionChain && BestCand != NoCand) { |
| 1946 | // See splitCanCauseEvictionChain for detailed description of bad |
| 1947 | // eviction chain scenarios. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1948 | LLVM_DEBUG(dbgs() << "Best split candidate of vreg " |
| 1949 | << printReg(VirtReg.reg, TRI) << " may "); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1950 | if (!(*CanCauseEvictionChain)) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1951 | LLVM_DEBUG(dbgs() << "not "); |
| 1952 | LLVM_DEBUG(dbgs() << "cause bad eviction chain\n"); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 1955 | return BestCand; |
| 1956 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1957 | |
Manman Ren | 66124f9 | 2014-03-24 23:23:42 +0000 | [diff] [blame] | 1958 | unsigned RAGreedy::doRegionSplit(LiveInterval &VirtReg, unsigned BestCand, |
| 1959 | bool HasCompact, |
| 1960 | SmallVectorImpl<unsigned> &NewVRegs) { |
| 1961 | SmallVector<unsigned, 8> UsedCands; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1962 | // Prepare split editor. |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 1963 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 1964 | SE->reset(LREdit, SplitSpillMode); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Assign all edge bundles to the preferred candidate, or NoCand. |
| 1967 | BundleCand.assign(Bundles->getNumBundles(), NoCand); |
| 1968 | |
| 1969 | // Assign bundles for the best candidate region. |
| 1970 | if (BestCand != NoCand) { |
| 1971 | GlobalSplitCandidate &Cand = GlobalCand[BestCand]; |
| 1972 | if (unsigned B = Cand.getBundles(BundleCand, BestCand)) { |
| 1973 | UsedCands.push_back(BestCand); |
| 1974 | Cand.IntvIdx = SE->openIntv(); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1975 | LLVM_DEBUG(dbgs() << "Split for " << printReg(Cand.PhysReg, TRI) << " in " |
| 1976 | << B << " bundles, intv " << Cand.IntvIdx << ".\n"); |
Chandler Carruth | 32668ea | 2011-08-03 23:07:27 +0000 | [diff] [blame] | 1977 | (void)B; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | // Assign bundles for the compact region. |
| 1982 | if (HasCompact) { |
| 1983 | GlobalSplitCandidate &Cand = GlobalCand.front(); |
| 1984 | assert(!Cand.PhysReg && "Compact region has no physreg"); |
| 1985 | if (unsigned B = Cand.getBundles(BundleCand, 0)) { |
| 1986 | UsedCands.push_back(0); |
| 1987 | Cand.IntvIdx = SE->openIntv(); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1988 | LLVM_DEBUG(dbgs() << "Split for compact region in " << B |
| 1989 | << " bundles, intv " << Cand.IntvIdx << ".\n"); |
Chandler Carruth | 32668ea | 2011-08-03 23:07:27 +0000 | [diff] [blame] | 1990 | (void)B; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | splitAroundRegion(LREdit, UsedCands); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1995 | return 0; |
| 1996 | } |
| 1997 | |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1998 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1999 | // Per-Block Splitting |
| 2000 | //===----------------------------------------------------------------------===// |
| 2001 | |
| 2002 | /// tryBlockSplit - Split a global live range around every block with uses. This |
| 2003 | /// creates a lot of local live ranges, that will be split by tryLocalSplit if |
| 2004 | /// they don't allocate. |
| 2005 | unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2006 | SmallVectorImpl<unsigned> &NewVRegs) { |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 2007 | assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed"); |
| 2008 | unsigned Reg = VirtReg.reg; |
| 2009 | bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 2010 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 2011 | SE->reset(LREdit, SplitSpillMode); |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 2012 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 2013 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 2014 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
| 2015 | if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) |
| 2016 | SE->splitSingleBlock(BI); |
| 2017 | } |
| 2018 | // No blocks were split. |
| 2019 | if (LREdit.empty()) |
| 2020 | return 0; |
| 2021 | |
| 2022 | // We did split for some blocks. |
Jakob Stoklund Olesen | a9c41d3 | 2011-08-05 23:50:31 +0000 | [diff] [blame] | 2023 | SmallVector<unsigned, 8> IntvMap; |
| 2024 | SE->finish(&IntvMap); |
Jakob Stoklund Olesen | 1f88042 | 2011-08-05 23:10:40 +0000 | [diff] [blame] | 2025 | |
| 2026 | // Tell LiveDebugVariables about the new ranges. |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2027 | DebugVars->splitRegister(Reg, LREdit.regs(), *LIS); |
Jakob Stoklund Olesen | 1f88042 | 2011-08-05 23:10:40 +0000 | [diff] [blame] | 2028 | |
Jakob Stoklund Olesen | a9c41d3 | 2011-08-05 23:50:31 +0000 | [diff] [blame] | 2029 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 2030 | |
| 2031 | // Sort out the new intervals created by splitting. The remainder interval |
| 2032 | // goes straight to spilling, the new local ranges get to stay RS_New. |
| 2033 | for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2034 | LiveInterval &LI = LIS->getInterval(LREdit.get(i)); |
Jakob Stoklund Olesen | a9c41d3 | 2011-08-05 23:50:31 +0000 | [diff] [blame] | 2035 | if (getStage(LI) == RS_New && IntvMap[i] == 0) |
| 2036 | setStage(LI, RS_Spill); |
| 2037 | } |
| 2038 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 2039 | if (VerifyEnabled) |
| 2040 | MF->verify(this, "After splitting live range around basic blocks"); |
| 2041 | return 0; |
| 2042 | } |
| 2043 | |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2044 | //===----------------------------------------------------------------------===// |
| 2045 | // Per-Instruction Splitting |
| 2046 | //===----------------------------------------------------------------------===// |
| 2047 | |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 2048 | /// Get the number of allocatable registers that match the constraints of \p Reg |
| 2049 | /// on \p MI and that are also in \p SuperRC. |
| 2050 | static unsigned getNumAllocatableRegsForConstraints( |
| 2051 | const MachineInstr *MI, unsigned Reg, const TargetRegisterClass *SuperRC, |
| 2052 | const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, |
| 2053 | const RegisterClassInfo &RCI) { |
| 2054 | assert(SuperRC && "Invalid register class"); |
| 2055 | |
| 2056 | const TargetRegisterClass *ConstrainedRC = |
| 2057 | MI->getRegClassConstraintEffectForVReg(Reg, SuperRC, TII, TRI, |
| 2058 | /* ExploreBundle */ true); |
| 2059 | if (!ConstrainedRC) |
| 2060 | return 0; |
| 2061 | return RCI.getNumAllocatableRegs(ConstrainedRC); |
| 2062 | } |
| 2063 | |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2064 | /// tryInstructionSplit - Split a live range around individual instructions. |
| 2065 | /// This is normally not worthwhile since the spiller is doing essentially the |
| 2066 | /// same thing. However, when the live range is in a constrained register |
| 2067 | /// class, it may help to insert copies such that parts of the live range can |
| 2068 | /// be moved to a larger register class. |
| 2069 | /// |
| 2070 | /// This is similar to spilling to a larger register class. |
| 2071 | unsigned |
| 2072 | RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2073 | SmallVectorImpl<unsigned> &NewVRegs) { |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 2074 | const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2075 | // There is no point to this if there are no larger sub-classes. |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 2076 | if (!RegClassInfo.isProperSubClass(CurRC)) |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2077 | return 0; |
| 2078 | |
| 2079 | // Always enable split spill mode, since we're effectively spilling to a |
| 2080 | // register. |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 2081 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2082 | SE->reset(LREdit, SplitEditor::SM_Size); |
| 2083 | |
| 2084 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
| 2085 | if (Uses.size() <= 1) |
| 2086 | return 0; |
| 2087 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2088 | LLVM_DEBUG(dbgs() << "Split around " << Uses.size() |
| 2089 | << " individual instrs.\n"); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2090 | |
Eric Christopher | 4ec858e | 2015-03-10 23:46:01 +0000 | [diff] [blame] | 2091 | const TargetRegisterClass *SuperRC = |
| 2092 | TRI->getLargestLegalSuperClass(CurRC, *MF); |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 2093 | unsigned SuperRCNumAllocatableRegs = RCI.getNumAllocatableRegs(SuperRC); |
| 2094 | // Split around every non-copy instruction if this split will relax |
| 2095 | // the constraints on the virtual register. |
| 2096 | // Otherwise, splitting just inserts uncoalescable copies that do not help |
| 2097 | // the allocation. |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2098 | for (unsigned i = 0; i != Uses.size(); ++i) { |
| 2099 | if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i])) |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 2100 | if (MI->isFullCopy() || |
| 2101 | SuperRCNumAllocatableRegs == |
| 2102 | getNumAllocatableRegsForConstraints(MI, VirtReg.reg, SuperRC, TII, |
| 2103 | TRI, RCI)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2104 | LLVM_DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2105 | continue; |
| 2106 | } |
| 2107 | SE->openIntv(); |
| 2108 | SlotIndex SegStart = SE->enterIntvBefore(Uses[i]); |
| 2109 | SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]); |
| 2110 | SE->useIntv(SegStart, SegStop); |
| 2111 | } |
| 2112 | |
| 2113 | if (LREdit.empty()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2114 | LLVM_DEBUG(dbgs() << "All uses were copies.\n"); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2115 | return 0; |
| 2116 | } |
| 2117 | |
| 2118 | SmallVector<unsigned, 8> IntvMap; |
| 2119 | SE->finish(&IntvMap); |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2120 | DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2121 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 2122 | |
| 2123 | // Assign all new registers to RS_Spill. This was the last chance. |
| 2124 | setStage(LREdit.begin(), LREdit.end(), RS_Spill); |
| 2125 | return 0; |
| 2126 | } |
| 2127 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 2128 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2129 | // Local Splitting |
| 2130 | //===----------------------------------------------------------------------===// |
| 2131 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2132 | /// calcGapWeights - Compute the maximum spill weight that needs to be evicted |
| 2133 | /// in order to use PhysReg between two entries in SA->UseSlots. |
| 2134 | /// |
| 2135 | /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. |
| 2136 | /// |
| 2137 | void RAGreedy::calcGapWeights(unsigned PhysReg, |
| 2138 | SmallVectorImpl<float> &GapWeight) { |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 2139 | assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); |
| 2140 | const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 2141 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2142 | const unsigned NumGaps = Uses.size()-1; |
| 2143 | |
| 2144 | // Start and end points for the interference check. |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 2145 | SlotIndex StartIdx = |
| 2146 | BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr; |
| 2147 | SlotIndex StopIdx = |
| 2148 | BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2149 | |
| 2150 | GapWeight.assign(NumGaps, 0.0f); |
| 2151 | |
| 2152 | // Add interference from each overlapping register. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2153 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 2154 | if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units) |
| 2155 | .checkInterference()) |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2156 | continue; |
| 2157 | |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 2158 | // We know that VirtReg is a continuous interval from FirstInstr to |
| 2159 | // LastInstr, so we don't need InterferenceQuery. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2160 | // |
| 2161 | // Interference that overlaps an instruction is counted in both gaps |
| 2162 | // surrounding the instruction. The exception is interference before |
| 2163 | // StartIdx and after StopIdx. |
| 2164 | // |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2165 | LiveIntervalUnion::SegmentIter IntI = |
| 2166 | Matrix->getLiveUnions()[*Units] .find(StartIdx); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2167 | for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { |
| 2168 | // Skip the gaps before IntI. |
| 2169 | while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) |
| 2170 | if (++Gap == NumGaps) |
| 2171 | break; |
| 2172 | if (Gap == NumGaps) |
| 2173 | break; |
| 2174 | |
| 2175 | // Update the gaps covered by IntI. |
| 2176 | const float weight = IntI.value()->weight; |
| 2177 | for (; Gap != NumGaps; ++Gap) { |
| 2178 | GapWeight[Gap] = std::max(GapWeight[Gap], weight); |
| 2179 | if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) |
| 2180 | break; |
| 2181 | } |
| 2182 | if (Gap == NumGaps) |
| 2183 | break; |
| 2184 | } |
| 2185 | } |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2186 | |
| 2187 | // Add fixed interference. |
| 2188 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 2189 | const LiveRange &LR = LIS->getRegUnit(*Units); |
| 2190 | LiveRange::const_iterator I = LR.find(StartIdx); |
| 2191 | LiveRange::const_iterator E = LR.end(); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Same loop as above. Mark any overlapped gaps as HUGE_VALF. |
| 2194 | for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) { |
| 2195 | while (Uses[Gap+1].getBoundaryIndex() < I->start) |
| 2196 | if (++Gap == NumGaps) |
| 2197 | break; |
| 2198 | if (Gap == NumGaps) |
| 2199 | break; |
| 2200 | |
| 2201 | for (; Gap != NumGaps; ++Gap) { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 2202 | GapWeight[Gap] = huge_valf; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2203 | if (Uses[Gap+1].getBaseIndex() >= I->end) |
| 2204 | break; |
| 2205 | } |
| 2206 | if (Gap == NumGaps) |
| 2207 | break; |
| 2208 | } |
| 2209 | } |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2212 | /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only |
| 2213 | /// basic block. |
| 2214 | /// |
| 2215 | unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2216 | SmallVectorImpl<unsigned> &NewVRegs) { |
Walter Lee | a96dc0e | 2018-09-20 20:05:57 +0000 | [diff] [blame] | 2217 | // TODO: the function currently only handles a single UseBlock; it should be |
| 2218 | // possible to generalize. |
| 2219 | if (SA->getUseBlocks().size() != 1) |
| 2220 | return 0; |
| 2221 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 2222 | const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2223 | |
| 2224 | // Note that it is possible to have an interval that is live-in or live-out |
| 2225 | // while only covering a single block - A phi-def can use undef values from |
| 2226 | // predecessors, and the block could be a single-block loop. |
| 2227 | // We don't bother doing anything clever about such a case, we simply assume |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 2228 | // that the interval is continuous from FirstInstr to LastInstr. We should |
| 2229 | // make sure that we don't do anything illegal to such an interval, though. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2230 | |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 2231 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2232 | if (Uses.size() <= 2) |
| 2233 | return 0; |
| 2234 | const unsigned NumGaps = Uses.size()-1; |
| 2235 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2236 | LLVM_DEBUG({ |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2237 | dbgs() << "tryLocalSplit: "; |
| 2238 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 2239 | dbgs() << ' ' << Uses[i]; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2240 | dbgs() << '\n'; |
| 2241 | }); |
| 2242 | |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2243 | // If VirtReg is live across any register mask operands, compute a list of |
| 2244 | // gaps with register masks. |
| 2245 | SmallVector<unsigned, 8> RegMaskGaps; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2246 | if (Matrix->checkRegMaskInterference(VirtReg)) { |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2247 | // Get regmask slots for the whole block. |
| 2248 | ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber()); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2249 | LLVM_DEBUG(dbgs() << RMS.size() << " regmasks in block:"); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2250 | // Constrain to VirtReg's live range. |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 2251 | unsigned ri = std::lower_bound(RMS.begin(), RMS.end(), |
| 2252 | Uses.front().getRegSlot()) - RMS.begin(); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2253 | unsigned re = RMS.size(); |
| 2254 | for (unsigned i = 0; i != NumGaps && ri != re; ++i) { |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 2255 | // Look for Uses[i] <= RMS <= Uses[i+1]. |
| 2256 | assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i])); |
| 2257 | if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri])) |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2258 | continue; |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 2259 | // Skip a regmask on the same instruction as the last use. It doesn't |
| 2260 | // overlap the live range. |
| 2261 | if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps) |
| 2262 | break; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2263 | LLVM_DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' |
| 2264 | << Uses[i + 1]); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2265 | RegMaskGaps.push_back(i); |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 2266 | // Advance ri to the next gap. A regmask on one of the uses counts in |
| 2267 | // both gaps. |
| 2268 | while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1])) |
| 2269 | ++ri; |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2270 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2271 | LLVM_DEBUG(dbgs() << '\n'); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2274 | // Since we allow local split results to be split again, there is a risk of |
| 2275 | // creating infinite loops. It is tempting to require that the new live |
| 2276 | // ranges have less instructions than the original. That would guarantee |
| 2277 | // convergence, but it is too strict. A live range with 3 instructions can be |
| 2278 | // split 2+3 (including the COPY), and we want to allow that. |
| 2279 | // |
| 2280 | // Instead we use these rules: |
| 2281 | // |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2282 | // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2283 | // noop split, of course). |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2284 | // 2. Require progress be made for ranges with getStage() == RS_Split2. All |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2285 | // the new ranges must have fewer instructions than before the split. |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2286 | // 3. New ranges with the same number of instructions are marked RS_Split2, |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2287 | // smaller ranges are marked RS_New. |
| 2288 | // |
| 2289 | // These rules allow a 3 -> 2+3 split once, which we need. They also prevent |
| 2290 | // excessive splitting and infinite loops. |
| 2291 | // |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2292 | bool ProgressRequired = getStage(VirtReg) >= RS_Split2; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2293 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2294 | // Best split candidate. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2295 | unsigned BestBefore = NumGaps; |
| 2296 | unsigned BestAfter = 0; |
| 2297 | float BestDiff = 0; |
| 2298 | |
Jakob Stoklund Olesen | 03ef600 | 2013-07-16 18:26:18 +0000 | [diff] [blame] | 2299 | const float blockFreq = |
| 2300 | SpillPlacer->getBlockFrequency(BI.MBB->getNumber()).getFrequency() * |
Michael Gottesman | 523823b | 2013-12-14 02:37:38 +0000 | [diff] [blame] | 2301 | (1.0f / MBFI->getEntryFreq()); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2302 | SmallVector<float, 8> GapWeight; |
| 2303 | |
| 2304 | Order.rewind(); |
| 2305 | while (unsigned PhysReg = Order.next()) { |
| 2306 | // Keep track of the largest spill weight that would need to be evicted in |
| 2307 | // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. |
| 2308 | calcGapWeights(PhysReg, GapWeight); |
| 2309 | |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2310 | // Remove any gaps with regmask clobbers. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2311 | if (Matrix->checkRegMaskInterference(VirtReg, PhysReg)) |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2312 | for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i) |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 2313 | GapWeight[RegMaskGaps[i]] = huge_valf; |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 2314 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2315 | // Try to find the best sequence of gaps to close. |
| 2316 | // The new spill weight must be larger than any gap interference. |
| 2317 | |
| 2318 | // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2319 | unsigned SplitBefore = 0, SplitAfter = 1; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2320 | |
| 2321 | // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). |
| 2322 | // It is the spill weight that needs to be evicted. |
| 2323 | float MaxGap = GapWeight[0]; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2324 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 2325 | while (true) { |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2326 | // Live before/after split? |
| 2327 | const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; |
| 2328 | const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; |
| 2329 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2330 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << ' ' << Uses[SplitBefore] |
| 2331 | << '-' << Uses[SplitAfter] << " i=" << MaxGap); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2332 | |
| 2333 | // Stop before the interval gets so big we wouldn't be making progress. |
| 2334 | if (!LiveBefore && !LiveAfter) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2335 | LLVM_DEBUG(dbgs() << " all\n"); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2336 | break; |
| 2337 | } |
| 2338 | // Should the interval be extended or shrunk? |
| 2339 | bool Shrink = true; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2340 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2341 | // How many gaps would the new range have? |
| 2342 | unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter; |
| 2343 | |
| 2344 | // Legally, without causing looping? |
| 2345 | bool Legal = !ProgressRequired || NewGaps < NumGaps; |
| 2346 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 2347 | if (Legal && MaxGap < huge_valf) { |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2348 | // Estimate the new spill weight. Each instruction reads or writes the |
| 2349 | // register. Conservatively assume there are no read-modify-write |
| 2350 | // instructions. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2351 | // |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2352 | // Try to guess the size of the new interval. |
Arnaud A. de Grandmaison | 8025a39 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 2353 | const float EstWeight = normalizeSpillWeight( |
| 2354 | blockFreq * (NewGaps + 1), |
| 2355 | Uses[SplitBefore].distance(Uses[SplitAfter]) + |
| 2356 | (LiveBefore + LiveAfter) * SlotIndex::InstrDist, |
| 2357 | 1); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2358 | // Would this split be possible to allocate? |
| 2359 | // Never allocate all gaps, we wouldn't be making progress. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2360 | LLVM_DEBUG(dbgs() << " w=" << EstWeight); |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 2361 | if (EstWeight * Hysteresis >= MaxGap) { |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2362 | Shrink = false; |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 2363 | float Diff = EstWeight - MaxGap; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2364 | if (Diff > BestDiff) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2365 | LLVM_DEBUG(dbgs() << " (best)"); |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 2366 | BestDiff = Hysteresis * Diff; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2367 | BestBefore = SplitBefore; |
| 2368 | BestAfter = SplitAfter; |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | // Try to shrink. |
| 2374 | if (Shrink) { |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2375 | if (++SplitBefore < SplitAfter) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2376 | LLVM_DEBUG(dbgs() << " shrink\n"); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2377 | // Recompute the max when necessary. |
| 2378 | if (GapWeight[SplitBefore - 1] >= MaxGap) { |
| 2379 | MaxGap = GapWeight[SplitBefore]; |
| 2380 | for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) |
| 2381 | MaxGap = std::max(MaxGap, GapWeight[i]); |
| 2382 | } |
| 2383 | continue; |
| 2384 | } |
| 2385 | MaxGap = 0; |
| 2386 | } |
| 2387 | |
| 2388 | // Try to extend the interval. |
| 2389 | if (SplitAfter >= NumGaps) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2390 | LLVM_DEBUG(dbgs() << " end\n"); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2391 | break; |
| 2392 | } |
| 2393 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2394 | LLVM_DEBUG(dbgs() << " extend\n"); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2395 | MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | // Didn't find any candidates? |
| 2400 | if (BestBefore == NumGaps) |
| 2401 | return 0; |
| 2402 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2403 | LLVM_DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] << '-' |
| 2404 | << Uses[BestAfter] << ", " << BestDiff << ", " |
| 2405 | << (BestAfter - BestBefore + 1) << " instrs\n"); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2406 | |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 2407 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 2408 | SE->reset(LREdit); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2409 | |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 2410 | SE->openIntv(); |
| 2411 | SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); |
| 2412 | SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); |
| 2413 | SE->useIntv(SegStart, SegStop); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2414 | SmallVector<unsigned, 8> IntvMap; |
| 2415 | SE->finish(&IntvMap); |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2416 | DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2417 | |
| 2418 | // If the new range has the same number of instructions as before, mark it as |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2419 | // RS_Split2 so the next split will be forced to make progress. Otherwise, |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2420 | // leave the new intervals as RS_New so they can compete. |
| 2421 | bool LiveBefore = BestBefore != 0 || BI.LiveIn; |
| 2422 | bool LiveAfter = BestAfter != NumGaps || BI.LiveOut; |
| 2423 | unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter; |
| 2424 | if (NewGaps >= NumGaps) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2425 | LLVM_DEBUG(dbgs() << "Tagging non-progress ranges: "); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2426 | assert(!ProgressRequired && "Didn't make progress when it was required."); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2427 | for (unsigned i = 0, e = IntvMap.size(); i != e; ++i) |
| 2428 | if (IntvMap[i] == 1) { |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2429 | setStage(LIS->getInterval(LREdit.get(i)), RS_Split2); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2430 | LLVM_DEBUG(dbgs() << printReg(LREdit.get(i))); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2431 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2432 | LLVM_DEBUG(dbgs() << '\n'); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 2433 | } |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 2434 | ++NumLocalSplits; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2435 | |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
| 2439 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 2440 | // Live Range Splitting |
| 2441 | //===----------------------------------------------------------------------===// |
| 2442 | |
| 2443 | /// trySplit - Try to split VirtReg or one of its interferences, making it |
| 2444 | /// assignable. |
| 2445 | /// @return Physreg when VirtReg may be assigned and/or new NewVRegs. |
| 2446 | unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2447 | SmallVectorImpl<unsigned>&NewVRegs) { |
Jakob Stoklund Olesen | ccfa446 | 2011-08-05 23:50:33 +0000 | [diff] [blame] | 2448 | // Ranges must be Split2 or less. |
| 2449 | if (getStage(VirtReg) >= RS_Spill) |
| 2450 | return 0; |
| 2451 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 2452 | // Local intervals are handled separately. |
Jakob Stoklund Olesen | a2ebf60 | 2011-02-19 00:38:40 +0000 | [diff] [blame] | 2453 | if (LIS->intervalIsInOneMBB(VirtReg)) { |
Matthias Braun | 9262f00 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 2454 | NamedRegionTimer T("local_split", "Local Splitting", TimerGroupName, |
| 2455 | TimerGroupDescription, TimePassesIsEnabled); |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 2456 | SA->analyze(&VirtReg); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 2457 | unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs); |
| 2458 | if (PhysReg || !NewVRegs.empty()) |
| 2459 | return PhysReg; |
| 2460 | return tryInstructionSplit(VirtReg, Order, NewVRegs); |
Jakob Stoklund Olesen | a2ebf60 | 2011-02-19 00:38:40 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Matthias Braun | 9262f00 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 2463 | NamedRegionTimer T("global_split", "Global Splitting", TimerGroupName, |
| 2464 | TimerGroupDescription, TimePassesIsEnabled); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 2465 | |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 2466 | SA->analyze(&VirtReg); |
| 2467 | |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 2468 | // FIXME: SplitAnalysis may repair broken live ranges coming from the |
| 2469 | // coalescer. That may cause the range to become allocatable which means that |
| 2470 | // tryRegionSplit won't be making progress. This check should be replaced with |
| 2471 | // an assertion when the coalescer is fixed. |
| 2472 | if (SA->didRepairRange()) { |
| 2473 | // VirtReg has changed, so all cached queries are invalid. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 2474 | Matrix->invalidateVirtRegs(); |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 2475 | if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) |
| 2476 | return PhysReg; |
| 2477 | } |
| 2478 | |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 2479 | // First try to split around a region spanning multiple blocks. RS_Split2 |
| 2480 | // ranges already made dubious progress with region splitting, so they go |
| 2481 | // straight to single block splitting. |
| 2482 | if (getStage(VirtReg) < RS_Split2) { |
| 2483 | unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); |
| 2484 | if (PhysReg || !NewVRegs.empty()) |
| 2485 | return PhysReg; |
| 2486 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 2487 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 2488 | // Then isolate blocks. |
| 2489 | return tryBlockSplit(VirtReg, Order, NewVRegs); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2492 | //===----------------------------------------------------------------------===// |
| 2493 | // Last Chance Recoloring |
| 2494 | //===----------------------------------------------------------------------===// |
| 2495 | |
Mikael Holmen | e3cc458 | 2017-09-28 08:22:35 +0000 | [diff] [blame] | 2496 | /// Return true if \p reg has any tied def operand. |
| 2497 | static bool hasTiedDef(MachineRegisterInfo *MRI, unsigned reg) { |
| 2498 | for (const MachineOperand &MO : MRI->def_operands(reg)) |
| 2499 | if (MO.isTied()) |
| 2500 | return true; |
| 2501 | |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2505 | /// mayRecolorAllInterferences - Check if the virtual registers that |
| 2506 | /// interfere with \p VirtReg on \p PhysReg (or one of its aliases) may be |
| 2507 | /// recolored to free \p PhysReg. |
| 2508 | /// When true is returned, \p RecoloringCandidates has been augmented with all |
| 2509 | /// the live intervals that need to be recolored in order to free \p PhysReg |
| 2510 | /// for \p VirtReg. |
| 2511 | /// \p FixedRegisters contains all the virtual registers that cannot be |
| 2512 | /// recolored. |
| 2513 | bool |
| 2514 | RAGreedy::mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg, |
| 2515 | SmallLISet &RecoloringCandidates, |
| 2516 | const SmallVirtRegSet &FixedRegisters) { |
| 2517 | const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg); |
| 2518 | |
| 2519 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 2520 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
| 2521 | // If there is LastChanceRecoloringMaxInterference or more interferences, |
| 2522 | // chances are one would not be recolorable. |
| 2523 | if (Q.collectInterferingVRegs(LastChanceRecoloringMaxInterference) >= |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 2524 | LastChanceRecoloringMaxInterference && !ExhaustiveSearch) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2525 | LLVM_DEBUG(dbgs() << "Early abort: too many interferences.\n"); |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2526 | CutOffInfo |= CO_Interf; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2527 | return false; |
| 2528 | } |
| 2529 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 2530 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
| 2531 | // If Intf is done and sit on the same register class as VirtReg, |
| 2532 | // it would not be recolorable as it is in the same state as VirtReg. |
Mikael Holmen | e3cc458 | 2017-09-28 08:22:35 +0000 | [diff] [blame] | 2533 | // However, if VirtReg has tied defs and Intf doesn't, then |
| 2534 | // there is still a point in examining if it can be recolorable. |
| 2535 | if (((getStage(*Intf) == RS_Done && |
| 2536 | MRI->getRegClass(Intf->reg) == CurRC) && |
| 2537 | !(hasTiedDef(MRI, VirtReg.reg) && !hasTiedDef(MRI, Intf->reg))) || |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2538 | FixedRegisters.count(Intf->reg)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2539 | LLVM_DEBUG( |
| 2540 | dbgs() << "Early abort: the interference is not recolorable.\n"); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2541 | return false; |
| 2542 | } |
| 2543 | RecoloringCandidates.insert(Intf); |
| 2544 | } |
| 2545 | } |
| 2546 | return true; |
| 2547 | } |
| 2548 | |
| 2549 | /// tryLastChanceRecoloring - Try to assign a color to \p VirtReg by recoloring |
| 2550 | /// its interferences. |
| 2551 | /// Last chance recoloring chooses a color for \p VirtReg and recolors every |
| 2552 | /// virtual register that was using it. The recoloring process may recursively |
| 2553 | /// use the last chance recoloring. Therefore, when a virtual register has been |
| 2554 | /// assigned a color by this mechanism, it is marked as Fixed, i.e., it cannot |
| 2555 | /// be last-chance-recolored again during this recoloring "session". |
| 2556 | /// E.g., |
| 2557 | /// Let |
| 2558 | /// vA can use {R1, R2 } |
| 2559 | /// vB can use { R2, R3} |
| 2560 | /// vC can use {R1 } |
| 2561 | /// Where vA, vB, and vC cannot be split anymore (they are reloads for |
| 2562 | /// instance) and they all interfere. |
| 2563 | /// |
| 2564 | /// vA is assigned R1 |
| 2565 | /// vB is assigned R2 |
| 2566 | /// vC tries to evict vA but vA is already done. |
| 2567 | /// Regular register allocation fails. |
| 2568 | /// |
| 2569 | /// Last chance recoloring kicks in: |
| 2570 | /// vC does as if vA was evicted => vC uses R1. |
| 2571 | /// vC is marked as fixed. |
| 2572 | /// vA needs to find a color. |
| 2573 | /// None are available. |
| 2574 | /// vA cannot evict vC: vC is a fixed virtual register now. |
| 2575 | /// vA does as if vB was evicted => vA uses R2. |
| 2576 | /// vB needs to find a color. |
| 2577 | /// R3 is available. |
| 2578 | /// Recoloring => vC = R1, vA = R2, vB = R3 |
| 2579 | /// |
Alp Toker | bf930d5 | 2014-02-25 04:21:15 +0000 | [diff] [blame] | 2580 | /// \p Order defines the preferred allocation order for \p VirtReg. |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2581 | /// \p NewRegs will contain any new virtual register that have been created |
| 2582 | /// (split, spill) during the process and that must be assigned. |
| 2583 | /// \p FixedRegisters contains all the virtual registers that cannot be |
| 2584 | /// recolored. |
| 2585 | /// \p Depth gives the current depth of the last chance recoloring. |
| 2586 | /// \return a physical register that can be used for VirtReg or ~0u if none |
| 2587 | /// exists. |
| 2588 | unsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg, |
| 2589 | AllocationOrder &Order, |
| 2590 | SmallVectorImpl<unsigned> &NewVRegs, |
| 2591 | SmallVirtRegSet &FixedRegisters, |
| 2592 | unsigned Depth) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2593 | LLVM_DEBUG(dbgs() << "Try last chance recoloring for " << VirtReg << '\n'); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2594 | // Ranges must be Done. |
Quentin Colombet | 126099e | 2014-02-13 05:17:37 +0000 | [diff] [blame] | 2595 | assert((getStage(VirtReg) >= RS_Done || !VirtReg.isSpillable()) && |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2596 | "Last chance recoloring should really be last chance"); |
| 2597 | // Set the max depth to LastChanceRecoloringMaxDepth. |
| 2598 | // We may want to reconsider that if we end up with a too large search space |
| 2599 | // for target with hundreds of registers. |
| 2600 | // Indeed, in that case we may want to cut the search space earlier. |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 2601 | if (Depth >= LastChanceRecoloringMaxDepth && !ExhaustiveSearch) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2602 | LLVM_DEBUG(dbgs() << "Abort because max depth has been reached.\n"); |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2603 | CutOffInfo |= CO_Depth; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2604 | return ~0u; |
| 2605 | } |
| 2606 | |
| 2607 | // Set of Live intervals that will need to be recolored. |
| 2608 | SmallLISet RecoloringCandidates; |
| 2609 | // Record the original mapping virtual register to physical register in case |
| 2610 | // the recoloring fails. |
| 2611 | DenseMap<unsigned, unsigned> VirtRegToPhysReg; |
| 2612 | // Mark VirtReg as fixed, i.e., it will not be recolored pass this point in |
| 2613 | // this recoloring "session". |
| 2614 | FixedRegisters.insert(VirtReg.reg); |
Quentin Colombet | 005bfb8 | 2016-09-16 22:00:50 +0000 | [diff] [blame] | 2615 | SmallVector<unsigned, 4> CurrentNewVRegs; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2616 | |
| 2617 | Order.rewind(); |
| 2618 | while (unsigned PhysReg = Order.next()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2619 | LLVM_DEBUG(dbgs() << "Try to assign: " << VirtReg << " to " |
| 2620 | << printReg(PhysReg, TRI) << '\n'); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2621 | RecoloringCandidates.clear(); |
| 2622 | VirtRegToPhysReg.clear(); |
Quentin Colombet | 005bfb8 | 2016-09-16 22:00:50 +0000 | [diff] [blame] | 2623 | CurrentNewVRegs.clear(); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2624 | |
| 2625 | // It is only possible to recolor virtual register interference. |
| 2626 | if (Matrix->checkInterference(VirtReg, PhysReg) > |
| 2627 | LiveRegMatrix::IK_VirtReg) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2628 | LLVM_DEBUG( |
| 2629 | dbgs() << "Some interferences are not with virtual registers.\n"); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2630 | |
| 2631 | continue; |
| 2632 | } |
| 2633 | |
| 2634 | // Early give up on this PhysReg if it is obvious we cannot recolor all |
| 2635 | // the interferences. |
| 2636 | if (!mayRecolorAllInterferences(PhysReg, VirtReg, RecoloringCandidates, |
| 2637 | FixedRegisters)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2638 | LLVM_DEBUG(dbgs() << "Some interferences cannot be recolored.\n"); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2639 | continue; |
| 2640 | } |
| 2641 | |
| 2642 | // RecoloringCandidates contains all the virtual registers that interfer |
| 2643 | // with VirtReg on PhysReg (or one of its aliases). |
| 2644 | // Enqueue them for recoloring and perform the actual recoloring. |
| 2645 | PQueue RecoloringQueue; |
| 2646 | for (SmallLISet::iterator It = RecoloringCandidates.begin(), |
| 2647 | EndIt = RecoloringCandidates.end(); |
| 2648 | It != EndIt; ++It) { |
| 2649 | unsigned ItVirtReg = (*It)->reg; |
| 2650 | enqueue(RecoloringQueue, *It); |
| 2651 | assert(VRM->hasPhys(ItVirtReg) && |
Hiroshi Inoue | 73d058a | 2018-06-20 05:29:26 +0000 | [diff] [blame] | 2652 | "Interferences are supposed to be with allocated variables"); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2653 | |
| 2654 | // Record the current allocation. |
| 2655 | VirtRegToPhysReg[ItVirtReg] = VRM->getPhys(ItVirtReg); |
| 2656 | // unset the related struct. |
| 2657 | Matrix->unassign(**It); |
| 2658 | } |
| 2659 | |
| 2660 | // Do as if VirtReg was assigned to PhysReg so that the underlying |
| 2661 | // recoloring has the right information about the interferes and |
| 2662 | // available colors. |
| 2663 | Matrix->assign(VirtReg, PhysReg); |
| 2664 | |
| 2665 | // Save the current recoloring state. |
| 2666 | // If we cannot recolor all the interferences, we will have to start again |
| 2667 | // at this point for the next physical register. |
| 2668 | SmallVirtRegSet SaveFixedRegisters(FixedRegisters); |
Quentin Colombet | 005bfb8 | 2016-09-16 22:00:50 +0000 | [diff] [blame] | 2669 | if (tryRecoloringCandidates(RecoloringQueue, CurrentNewVRegs, |
| 2670 | FixedRegisters, Depth)) { |
| 2671 | // Push the queued vregs into the main queue. |
| 2672 | for (unsigned NewVReg : CurrentNewVRegs) |
| 2673 | NewVRegs.push_back(NewVReg); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2674 | // Do not mess up with the global assignment process. |
| 2675 | // I.e., VirtReg must be unassigned. |
| 2676 | Matrix->unassign(VirtReg); |
| 2677 | return PhysReg; |
| 2678 | } |
| 2679 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2680 | LLVM_DEBUG(dbgs() << "Fail to assign: " << VirtReg << " to " |
| 2681 | << printReg(PhysReg, TRI) << '\n'); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2682 | |
| 2683 | // The recoloring attempt failed, undo the changes. |
| 2684 | FixedRegisters = SaveFixedRegisters; |
| 2685 | Matrix->unassign(VirtReg); |
| 2686 | |
Wei Mi | 85bacc4 | 2016-11-08 18:19:36 +0000 | [diff] [blame] | 2687 | // For a newly created vreg which is also in RecoloringCandidates, |
| 2688 | // don't add it to NewVRegs because its physical register will be restored |
| 2689 | // below. Other vregs in CurrentNewVRegs are created by calling |
| 2690 | // selectOrSplit and should be added into NewVRegs. |
Quentin Colombet | 005bfb8 | 2016-09-16 22:00:50 +0000 | [diff] [blame] | 2691 | for (SmallVectorImpl<unsigned>::iterator Next = CurrentNewVRegs.begin(), |
| 2692 | End = CurrentNewVRegs.end(); |
| 2693 | Next != End; ++Next) { |
Wei Mi | 85bacc4 | 2016-11-08 18:19:36 +0000 | [diff] [blame] | 2694 | if (RecoloringCandidates.count(&LIS->getInterval(*Next))) |
Quentin Colombet | 005bfb8 | 2016-09-16 22:00:50 +0000 | [diff] [blame] | 2695 | continue; |
| 2696 | NewVRegs.push_back(*Next); |
| 2697 | } |
| 2698 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2699 | for (SmallLISet::iterator It = RecoloringCandidates.begin(), |
| 2700 | EndIt = RecoloringCandidates.end(); |
| 2701 | It != EndIt; ++It) { |
| 2702 | unsigned ItVirtReg = (*It)->reg; |
| 2703 | if (VRM->hasPhys(ItVirtReg)) |
| 2704 | Matrix->unassign(**It); |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 2705 | unsigned ItPhysReg = VirtRegToPhysReg[ItVirtReg]; |
| 2706 | Matrix->assign(**It, ItPhysReg); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | // Last chance recoloring did not worked either, give up. |
| 2711 | return ~0u; |
| 2712 | } |
| 2713 | |
| 2714 | /// tryRecoloringCandidates - Try to assign a new color to every register |
| 2715 | /// in \RecoloringQueue. |
| 2716 | /// \p NewRegs will contain any new virtual register created during the |
| 2717 | /// recoloring process. |
| 2718 | /// \p FixedRegisters[in/out] contains all the registers that have been |
| 2719 | /// recolored. |
| 2720 | /// \return true if all virtual registers in RecoloringQueue were successfully |
| 2721 | /// recolored, false otherwise. |
| 2722 | bool RAGreedy::tryRecoloringCandidates(PQueue &RecoloringQueue, |
| 2723 | SmallVectorImpl<unsigned> &NewVRegs, |
| 2724 | SmallVirtRegSet &FixedRegisters, |
| 2725 | unsigned Depth) { |
| 2726 | while (!RecoloringQueue.empty()) { |
| 2727 | LiveInterval *LI = dequeue(RecoloringQueue); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2728 | LLVM_DEBUG(dbgs() << "Try to recolor: " << *LI << '\n'); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2729 | unsigned PhysReg; |
| 2730 | PhysReg = selectOrSplitImpl(*LI, NewVRegs, FixedRegisters, Depth + 1); |
Quentin Colombet | 154f790 | 2016-10-13 19:27:48 +0000 | [diff] [blame] | 2731 | // When splitting happens, the live-range may actually be empty. |
| 2732 | // In that case, this is okay to continue the recoloring even |
| 2733 | // if we did not find an alternative color for it. Indeed, |
| 2734 | // there will not be anything to color for LI in the end. |
| 2735 | if (PhysReg == ~0u || (!PhysReg && !LI->empty())) |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2736 | return false; |
Quentin Colombet | 154f790 | 2016-10-13 19:27:48 +0000 | [diff] [blame] | 2737 | |
| 2738 | if (!PhysReg) { |
| 2739 | assert(LI->empty() && "Only empty live-range do not require a register"); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2740 | LLVM_DEBUG(dbgs() << "Recoloring of " << *LI |
| 2741 | << " succeeded. Empty LI.\n"); |
Quentin Colombet | 154f790 | 2016-10-13 19:27:48 +0000 | [diff] [blame] | 2742 | continue; |
| 2743 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2744 | LLVM_DEBUG(dbgs() << "Recoloring of " << *LI |
| 2745 | << " succeeded with: " << printReg(PhysReg, TRI) << '\n'); |
Quentin Colombet | 154f790 | 2016-10-13 19:27:48 +0000 | [diff] [blame] | 2746 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2747 | Matrix->assign(*LI, PhysReg); |
| 2748 | FixedRegisters.insert(LI->reg); |
| 2749 | } |
| 2750 | return true; |
| 2751 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 2752 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 2753 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 2754 | // Main Entry Point |
| 2755 | //===----------------------------------------------------------------------===// |
| 2756 | |
| 2757 | unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 2758 | SmallVectorImpl<unsigned> &NewVRegs) { |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2759 | CutOffInfo = CO_None; |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2760 | LLVMContext &Ctx = MF->getFunction().getContext(); |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2761 | SmallVirtRegSet FixedRegisters; |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2762 | unsigned Reg = selectOrSplitImpl(VirtReg, NewVRegs, FixedRegisters); |
| 2763 | if (Reg == ~0U && (CutOffInfo != CO_None)) { |
| 2764 | uint8_t CutOffEncountered = CutOffInfo & (CO_Depth | CO_Interf); |
| 2765 | if (CutOffEncountered == CO_Depth) |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 2766 | Ctx.emitError("register allocation failed: maximum depth for recoloring " |
| 2767 | "reached. Use -fexhaustive-register-search to skip " |
| 2768 | "cutoffs"); |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2769 | else if (CutOffEncountered == CO_Interf) |
| 2770 | Ctx.emitError("register allocation failed: maximum interference for " |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 2771 | "recoloring reached. Use -fexhaustive-register-search " |
| 2772 | "to skip cutoffs"); |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2773 | else if (CutOffEncountered == (CO_Depth | CO_Interf)) |
| 2774 | Ctx.emitError("register allocation failed: maximum interference and " |
Quentin Colombet | 92a892e | 2014-04-11 21:39:44 +0000 | [diff] [blame] | 2775 | "depth for recoloring reached. Use " |
| 2776 | "-fexhaustive-register-search to skip cutoffs"); |
Quentin Colombet | 3b2b5df | 2014-04-04 02:05:21 +0000 | [diff] [blame] | 2777 | } |
| 2778 | return Reg; |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 2781 | /// Using a CSR for the first time has a cost because it causes push|pop |
| 2782 | /// to be added to prologue|epilogue. Splitting a cold section of the live |
| 2783 | /// range can have lower cost than using the CSR for the first time; |
| 2784 | /// Spilling a live range in the cold path can have lower cost than using |
| 2785 | /// the CSR for the first time. Returns the physical register if we decide |
| 2786 | /// to use the CSR; otherwise return 0. |
| 2787 | unsigned RAGreedy::tryAssignCSRFirstTime(LiveInterval &VirtReg, |
| 2788 | AllocationOrder &Order, |
| 2789 | unsigned PhysReg, |
| 2790 | unsigned &CostPerUseLimit, |
| 2791 | SmallVectorImpl<unsigned> &NewVRegs) { |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 2792 | if (getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) { |
| 2793 | // We choose spill over using the CSR for the first time if the spill cost |
| 2794 | // is lower than CSRCost. |
| 2795 | SA->analyze(&VirtReg); |
| 2796 | if (calcSpillCost() >= CSRCost) |
| 2797 | return PhysReg; |
| 2798 | |
| 2799 | // We are going to spill, set CostPerUseLimit to 1 to make sure that |
| 2800 | // we will not use a callee-saved register in tryEvict. |
| 2801 | CostPerUseLimit = 1; |
| 2802 | return 0; |
| 2803 | } |
| 2804 | if (getStage(VirtReg) < RS_Split) { |
| 2805 | // We choose pre-splitting over using the CSR for the first time if |
| 2806 | // the cost of splitting is lower than CSRCost. |
| 2807 | SA->analyze(&VirtReg); |
| 2808 | unsigned NumCands = 0; |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 2809 | BlockFrequency BestCost = CSRCost; // Don't modify CSRCost. |
| 2810 | unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost, |
| 2811 | NumCands, true /*IgnoreCSR*/); |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 2812 | if (BestCand == NoCand) |
| 2813 | // Use the CSR if we can't find a region split below CSRCost. |
| 2814 | return PhysReg; |
| 2815 | |
| 2816 | // Perform the actual pre-splitting. |
| 2817 | doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs); |
| 2818 | return 0; |
| 2819 | } |
| 2820 | return PhysReg; |
| 2821 | } |
| 2822 | |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2823 | void RAGreedy::aboutToRemoveInterval(LiveInterval &LI) { |
| 2824 | // Do not keep invalid information around. |
| 2825 | SetOfBrokenHints.remove(&LI); |
| 2826 | } |
| 2827 | |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 2828 | void RAGreedy::initializeCSRCost() { |
| 2829 | // We use the larger one out of the command-line option and the value report |
| 2830 | // by TRI. |
| 2831 | CSRCost = BlockFrequency( |
| 2832 | std::max((unsigned)CSRFirstTimeCost, TRI->getCSRFirstUseCost())); |
| 2833 | if (!CSRCost.getFrequency()) |
| 2834 | return; |
| 2835 | |
| 2836 | // Raw cost is relative to Entry == 2^14; scale it appropriately. |
| 2837 | uint64_t ActualEntry = MBFI->getEntryFreq(); |
| 2838 | if (!ActualEntry) { |
| 2839 | CSRCost = 0; |
| 2840 | return; |
| 2841 | } |
| 2842 | uint64_t FixedEntry = 1 << 14; |
| 2843 | if (ActualEntry < FixedEntry) |
| 2844 | CSRCost *= BranchProbability(ActualEntry, FixedEntry); |
| 2845 | else if (ActualEntry <= UINT32_MAX) |
| 2846 | // Invert the fraction and divide. |
| 2847 | CSRCost /= BranchProbability(FixedEntry, ActualEntry); |
| 2848 | else |
| 2849 | // Can't use BranchProbability in general, since it takes 32-bit numbers. |
| 2850 | CSRCost = CSRCost.getFrequency() * (ActualEntry / FixedEntry); |
| 2851 | } |
| 2852 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2853 | /// Collect the hint info for \p Reg. |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2854 | /// The results are stored into \p Out. |
| 2855 | /// \p Out is not cleared before being populated. |
| 2856 | void RAGreedy::collectHintInfo(unsigned Reg, HintsInfo &Out) { |
| 2857 | for (const MachineInstr &Instr : MRI->reg_nodbg_instructions(Reg)) { |
| 2858 | if (!Instr.isFullCopy()) |
| 2859 | continue; |
| 2860 | // Look for the other end of the copy. |
| 2861 | unsigned OtherReg = Instr.getOperand(0).getReg(); |
| 2862 | if (OtherReg == Reg) { |
| 2863 | OtherReg = Instr.getOperand(1).getReg(); |
| 2864 | if (OtherReg == Reg) |
| 2865 | continue; |
| 2866 | } |
| 2867 | // Get the current assignment. |
| 2868 | unsigned OtherPhysReg = TargetRegisterInfo::isPhysicalRegister(OtherReg) |
| 2869 | ? OtherReg |
| 2870 | : VRM->getPhys(OtherReg); |
| 2871 | // Push the collected information. |
| 2872 | Out.push_back(HintInfo(MBFI->getBlockFreq(Instr.getParent()), OtherReg, |
| 2873 | OtherPhysReg)); |
| 2874 | } |
| 2875 | } |
| 2876 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2877 | /// Using the given \p List, compute the cost of the broken hints if |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2878 | /// \p PhysReg was used. |
| 2879 | /// \return The cost of \p List for \p PhysReg. |
| 2880 | BlockFrequency RAGreedy::getBrokenHintFreq(const HintsInfo &List, |
| 2881 | unsigned PhysReg) { |
| 2882 | BlockFrequency Cost = 0; |
| 2883 | for (const HintInfo &Info : List) { |
| 2884 | if (Info.PhysReg != PhysReg) |
| 2885 | Cost += Info.Freq; |
| 2886 | } |
| 2887 | return Cost; |
| 2888 | } |
| 2889 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2890 | /// Using the register assigned to \p VirtReg, try to recolor |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2891 | /// all the live ranges that are copy-related with \p VirtReg. |
| 2892 | /// The recoloring is then propagated to all the live-ranges that have |
| 2893 | /// been recolored and so on, until no more copies can be coalesced or |
| 2894 | /// it is not profitable. |
| 2895 | /// For a given live range, profitability is determined by the sum of the |
| 2896 | /// frequencies of the non-identity copies it would introduce with the old |
| 2897 | /// and new register. |
| 2898 | void RAGreedy::tryHintRecoloring(LiveInterval &VirtReg) { |
| 2899 | // We have a broken hint, check if it is possible to fix it by |
| 2900 | // reusing PhysReg for the copy-related live-ranges. Indeed, we evicted |
| 2901 | // some register and PhysReg may be available for the other live-ranges. |
| 2902 | SmallSet<unsigned, 4> Visited; |
| 2903 | SmallVector<unsigned, 2> RecoloringCandidates; |
| 2904 | HintsInfo Info; |
| 2905 | unsigned Reg = VirtReg.reg; |
| 2906 | unsigned PhysReg = VRM->getPhys(Reg); |
| 2907 | // Start the recoloring algorithm from the input live-interval, then |
| 2908 | // it will propagate to the ones that are copy-related with it. |
| 2909 | Visited.insert(Reg); |
| 2910 | RecoloringCandidates.push_back(Reg); |
| 2911 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2912 | LLVM_DEBUG(dbgs() << "Trying to reconcile hints for: " << printReg(Reg, TRI) |
| 2913 | << '(' << printReg(PhysReg, TRI) << ")\n"); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2914 | |
| 2915 | do { |
| 2916 | Reg = RecoloringCandidates.pop_back_val(); |
| 2917 | |
Hiroshi Inoue | 58d2b3a | 2017-07-10 12:44:25 +0000 | [diff] [blame] | 2918 | // We cannot recolor physical register. |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2919 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 2920 | continue; |
| 2921 | |
| 2922 | assert(VRM->hasPhys(Reg) && "We have unallocated variable!!"); |
| 2923 | |
| 2924 | // Get the live interval mapped with this virtual register to be able |
| 2925 | // to check for the interference with the new color. |
| 2926 | LiveInterval &LI = LIS->getInterval(Reg); |
| 2927 | unsigned CurrPhys = VRM->getPhys(Reg); |
| 2928 | // Check that the new color matches the register class constraints and |
| 2929 | // that it is free for this live range. |
| 2930 | if (CurrPhys != PhysReg && (!MRI->getRegClass(Reg)->contains(PhysReg) || |
| 2931 | Matrix->checkInterference(LI, PhysReg))) |
| 2932 | continue; |
| 2933 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2934 | LLVM_DEBUG(dbgs() << printReg(Reg, TRI) << '(' << printReg(CurrPhys, TRI) |
| 2935 | << ") is recolorable.\n"); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2936 | |
| 2937 | // Gather the hint info. |
| 2938 | Info.clear(); |
| 2939 | collectHintInfo(Reg, Info); |
| 2940 | // Check if recoloring the live-range will increase the cost of the |
| 2941 | // non-identity copies. |
| 2942 | if (CurrPhys != PhysReg) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2943 | LLVM_DEBUG(dbgs() << "Checking profitability:\n"); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2944 | BlockFrequency OldCopiesCost = getBrokenHintFreq(Info, CurrPhys); |
| 2945 | BlockFrequency NewCopiesCost = getBrokenHintFreq(Info, PhysReg); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2946 | LLVM_DEBUG(dbgs() << "Old Cost: " << OldCopiesCost.getFrequency() |
| 2947 | << "\nNew Cost: " << NewCopiesCost.getFrequency() |
| 2948 | << '\n'); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2949 | if (OldCopiesCost < NewCopiesCost) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2950 | LLVM_DEBUG(dbgs() << "=> Not profitable.\n"); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2951 | continue; |
| 2952 | } |
| 2953 | // At this point, the cost is either cheaper or equal. If it is |
| 2954 | // equal, we consider this is profitable because it may expose |
| 2955 | // more recoloring opportunities. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2956 | LLVM_DEBUG(dbgs() << "=> Profitable.\n"); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2957 | // Recolor the live-range. |
| 2958 | Matrix->unassign(LI); |
| 2959 | Matrix->assign(LI, PhysReg); |
| 2960 | } |
| 2961 | // Push all copy-related live-ranges to keep reconciling the broken |
| 2962 | // hints. |
| 2963 | for (const HintInfo &HI : Info) { |
| 2964 | if (Visited.insert(HI.Reg).second) |
| 2965 | RecoloringCandidates.push_back(HI.Reg); |
| 2966 | } |
| 2967 | } while (!RecoloringCandidates.empty()); |
| 2968 | } |
| 2969 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2970 | /// Try to recolor broken hints. |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 2971 | /// Broken hints may be repaired by recoloring when an evicted variable |
| 2972 | /// freed up a register for a larger live-range. |
| 2973 | /// Consider the following example: |
| 2974 | /// BB1: |
| 2975 | /// a = |
| 2976 | /// b = |
| 2977 | /// BB2: |
| 2978 | /// ... |
| 2979 | /// = b |
| 2980 | /// = a |
| 2981 | /// Let us assume b gets split: |
| 2982 | /// BB1: |
| 2983 | /// a = |
| 2984 | /// b = |
| 2985 | /// BB2: |
| 2986 | /// c = b |
| 2987 | /// ... |
| 2988 | /// d = c |
| 2989 | /// = d |
| 2990 | /// = a |
| 2991 | /// Because of how the allocation work, b, c, and d may be assigned different |
| 2992 | /// colors. Now, if a gets evicted later: |
| 2993 | /// BB1: |
| 2994 | /// a = |
| 2995 | /// st a, SpillSlot |
| 2996 | /// b = |
| 2997 | /// BB2: |
| 2998 | /// c = b |
| 2999 | /// ... |
| 3000 | /// d = c |
| 3001 | /// = d |
| 3002 | /// e = ld SpillSlot |
| 3003 | /// = e |
| 3004 | /// This is likely that we can assign the same register for b, c, and d, |
| 3005 | /// getting rid of 2 copies. |
| 3006 | void RAGreedy::tryHintsRecoloring() { |
| 3007 | for (LiveInterval *LI : SetOfBrokenHints) { |
| 3008 | assert(TargetRegisterInfo::isVirtualRegister(LI->reg) && |
| 3009 | "Recoloring is possible only for virtual registers"); |
| 3010 | // Some dead defs may be around (e.g., because of debug uses). |
| 3011 | // Ignore those. |
| 3012 | if (!VRM->hasPhys(LI->reg)) |
| 3013 | continue; |
| 3014 | tryHintRecoloring(*LI); |
| 3015 | } |
| 3016 | } |
| 3017 | |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 3018 | unsigned RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg, |
| 3019 | SmallVectorImpl<unsigned> &NewVRegs, |
| 3020 | SmallVirtRegSet &FixedRegisters, |
| 3021 | unsigned Depth) { |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 3022 | unsigned CostPerUseLimit = ~0u; |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 3023 | // First try assigning a free register. |
Matthias Braun | 2aa5727 | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 3024 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 3025 | if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) { |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3026 | // If VirtReg got an assignment, the eviction info is no longre relevant. |
| 3027 | LastEvicted.clearEvicteeInfo(VirtReg.reg); |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 3028 | // When NewVRegs is not empty, we may have made decisions such as evicting |
| 3029 | // a virtual register, go with the earlier decisions and use the physical |
| 3030 | // register. |
Matthias Braun | 65a4053 | 2015-07-14 17:38:17 +0000 | [diff] [blame] | 3031 | if (CSRCost.getFrequency() && isUnusedCalleeSavedReg(PhysReg) && |
| 3032 | NewVRegs.empty()) { |
Manman Ren | b5e6ddc | 2014-03-27 21:21:57 +0000 | [diff] [blame] | 3033 | unsigned CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg, |
| 3034 | CostPerUseLimit, NewVRegs); |
| 3035 | if (CSRReg || !NewVRegs.empty()) |
| 3036 | // Return now if we decide to use a CSR or create new vregs due to |
| 3037 | // pre-splitting. |
| 3038 | return CSRReg; |
Manman Ren | 692d183 | 2014-03-25 00:16:25 +0000 | [diff] [blame] | 3039 | } else |
| 3040 | return PhysReg; |
| 3041 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 3042 | |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 3043 | LiveRangeStage Stage = getStage(VirtReg); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3044 | LLVM_DEBUG(dbgs() << StageName[Stage] << " Cascade " |
| 3045 | << ExtraRegInfo[VirtReg.reg].Cascade << '\n'); |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 3046 | |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 3047 | // Try to evict a less worthy live range, but only for ranges from the primary |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 3048 | // queue. The RS_Split ranges already failed to do this, and they should not |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 3049 | // get a second chance until they have been split. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 3050 | if (Stage != RS_Split) |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 3051 | if (unsigned PhysReg = |
| 3052 | tryEvict(VirtReg, Order, NewVRegs, CostPerUseLimit)) { |
| 3053 | unsigned Hint = MRI->getSimpleHint(VirtReg.reg); |
| 3054 | // If VirtReg has a hint and that hint is broken record this |
| 3055 | // virtual register as a recoloring candidate for broken hint. |
| 3056 | // Indeed, since we evicted a variable in its neighborhood it is |
| 3057 | // likely we can at least partially recolor some of the |
| 3058 | // copy-related live-ranges. |
| 3059 | if (Hint && Hint != PhysReg) |
| 3060 | SetOfBrokenHints.insert(&VirtReg); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3061 | // If VirtReg eviction someone, the eviction info for it as an evictee is |
| 3062 | // no longre relevant. |
| 3063 | LastEvicted.clearEvicteeInfo(VirtReg.reg); |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 3064 | return PhysReg; |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 3065 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 3066 | |
Quentin Colombet | 12bac3e | 2016-09-16 22:00:42 +0000 | [diff] [blame] | 3067 | assert((NewVRegs.empty() || Depth) && "Cannot append to existing NewVRegs"); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 3068 | |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 3069 | // The first time we see a live range, don't try to split or spill. |
| 3070 | // Wait until the second time, when all smaller ranges have been allocated. |
| 3071 | // This gives a better picture of the interference to split around. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 3072 | if (Stage < RS_Split) { |
| 3073 | setStage(VirtReg, RS_Split); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3074 | LLVM_DEBUG(dbgs() << "wait for second round\n"); |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 3075 | NewVRegs.push_back(VirtReg.reg); |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 3076 | return 0; |
| 3077 | } |
| 3078 | |
Dylan McKay | 25d9f11 | 2016-10-11 01:04:36 +0000 | [diff] [blame] | 3079 | if (Stage < RS_Spill) { |
| 3080 | // Try splitting VirtReg or interferences. |
| 3081 | unsigned NewVRegSizeBefore = NewVRegs.size(); |
| 3082 | unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3083 | if (PhysReg || (NewVRegs.size() - NewVRegSizeBefore)) { |
| 3084 | // If VirtReg got split, the eviction info is no longre relevant. |
| 3085 | LastEvicted.clearEvicteeInfo(VirtReg.reg); |
Dylan McKay | 25d9f11 | 2016-10-11 01:04:36 +0000 | [diff] [blame] | 3086 | return PhysReg; |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3087 | } |
Dylan McKay | 25d9f11 | 2016-10-11 01:04:36 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 3090 | // If we couldn't allocate a register from spilling, there is probably some |
Hiroshi Inoue | 5591257 | 2017-06-29 18:03:28 +0000 | [diff] [blame] | 3091 | // invalid inline assembly. The base class will report it. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 3092 | if (Stage >= RS_Done || !VirtReg.isSpillable()) |
Quentin Colombet | 1a10a51 | 2014-02-05 22:13:59 +0000 | [diff] [blame] | 3093 | return tryLastChanceRecoloring(VirtReg, Order, NewVRegs, FixedRegisters, |
| 3094 | Depth); |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 3095 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 3096 | // Finally spill VirtReg itself. |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 3097 | if (EnableDeferredSpilling && getStage(VirtReg) < RS_Memory) { |
| 3098 | // TODO: This is experimental and in particular, we do not model |
| 3099 | // the live range splitting done by spilling correctly. |
| 3100 | // We would need a deep integration with the spiller to do the |
| 3101 | // right thing here. Anyway, that is still good for early testing. |
| 3102 | setStage(VirtReg, RS_Memory); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3103 | LLVM_DEBUG(dbgs() << "Do as if this register is in memory\n"); |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 3104 | NewVRegs.push_back(VirtReg.reg); |
| 3105 | } else { |
Matthias Braun | 9262f00 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 3106 | NamedRegionTimer T("spill", "Spiller", TimerGroupName, |
| 3107 | TimerGroupDescription, TimePassesIsEnabled); |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 3108 | LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 3109 | spiller().spill(LRE); |
| 3110 | setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3111 | |
Quentin Colombet | 3df507c | 2015-07-17 23:04:06 +0000 | [diff] [blame] | 3112 | if (VerifyEnabled) |
| 3113 | MF->verify(this, "After spilling"); |
| 3114 | } |
Jakob Stoklund Olesen | c46570d | 2011-03-16 22:56:08 +0000 | [diff] [blame] | 3115 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3116 | // The live virtual register requesting allocation was spilled, so tell |
| 3117 | // the caller not to allocate anything during this round. |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3121 | void RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads, |
| 3122 | unsigned &FoldedReloads, |
| 3123 | unsigned &Spills, |
| 3124 | unsigned &FoldedSpills) { |
| 3125 | Reloads = 0; |
| 3126 | FoldedReloads = 0; |
| 3127 | Spills = 0; |
| 3128 | FoldedSpills = 0; |
| 3129 | |
| 3130 | // Sum up the spill and reloads in subloops. |
| 3131 | for (MachineLoop *SubLoop : *L) { |
| 3132 | unsigned SubReloads; |
| 3133 | unsigned SubFoldedReloads; |
| 3134 | unsigned SubSpills; |
| 3135 | unsigned SubFoldedSpills; |
| 3136 | |
| 3137 | reportNumberOfSplillsReloads(SubLoop, SubReloads, SubFoldedReloads, |
| 3138 | SubSpills, SubFoldedSpills); |
| 3139 | Reloads += SubReloads; |
| 3140 | FoldedReloads += SubFoldedReloads; |
| 3141 | Spills += SubSpills; |
| 3142 | FoldedSpills += SubFoldedSpills; |
| 3143 | } |
| 3144 | |
| 3145 | const MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 3146 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
| 3147 | int FI; |
| 3148 | |
| 3149 | for (MachineBasicBlock *MBB : L->getBlocks()) |
| 3150 | // Handle blocks that were not included in subloops. |
| 3151 | if (Loops->getLoopFor(MBB) == L) |
| 3152 | for (MachineInstr &MI : *MBB) { |
Sander de Smalen | 7336954 | 2018-09-05 08:59:50 +0000 | [diff] [blame] | 3153 | SmallVector<const MachineMemOperand *, 2> Accesses; |
| 3154 | auto isSpillSlotAccess = [&MFI](const MachineMemOperand *A) { |
| 3155 | return MFI.isSpillSlotObjectIndex( |
| 3156 | cast<FixedStackPseudoSourceValue>(A->getPseudoValue()) |
| 3157 | ->getFrameIndex()); |
| 3158 | }; |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3159 | |
| 3160 | if (TII->isLoadFromStackSlot(MI, FI) && MFI.isSpillSlotObjectIndex(FI)) |
| 3161 | ++Reloads; |
Sander de Smalen | 1d40abd | 2018-09-03 09:15:58 +0000 | [diff] [blame] | 3162 | else if (TII->hasLoadFromStackSlot(MI, Accesses) && |
Sander de Smalen | 7336954 | 2018-09-05 08:59:50 +0000 | [diff] [blame] | 3163 | llvm::any_of(Accesses, isSpillSlotAccess)) |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3164 | ++FoldedReloads; |
| 3165 | else if (TII->isStoreToStackSlot(MI, FI) && |
| 3166 | MFI.isSpillSlotObjectIndex(FI)) |
| 3167 | ++Spills; |
Sander de Smalen | 1d40abd | 2018-09-03 09:15:58 +0000 | [diff] [blame] | 3168 | else if (TII->hasStoreToStackSlot(MI, Accesses) && |
Sander de Smalen | 7336954 | 2018-09-05 08:59:50 +0000 | [diff] [blame] | 3169 | llvm::any_of(Accesses, isSpillSlotAccess)) |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3170 | ++FoldedSpills; |
| 3171 | } |
| 3172 | |
| 3173 | if (Reloads || FoldedReloads || Spills || FoldedSpills) { |
| 3174 | using namespace ore; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 3175 | |
Vivek Pandya | 2540c74 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 3176 | ORE->emit([&]() { |
| 3177 | MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReload", |
| 3178 | L->getStartLoc(), L->getHeader()); |
| 3179 | if (Spills) |
| 3180 | R << NV("NumSpills", Spills) << " spills "; |
| 3181 | if (FoldedSpills) |
| 3182 | R << NV("NumFoldedSpills", FoldedSpills) << " folded spills "; |
| 3183 | if (Reloads) |
| 3184 | R << NV("NumReloads", Reloads) << " reloads "; |
| 3185 | if (FoldedReloads) |
| 3186 | R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads "; |
| 3187 | R << "generated in loop"; |
| 3188 | return R; |
| 3189 | }); |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3193 | bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3194 | LLVM_DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" |
| 3195 | << "********** Function: " << mf.getName() << '\n'); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3196 | |
| 3197 | MF = &mf; |
Eric Christopher | 1dd55ba | 2014-10-14 07:22:00 +0000 | [diff] [blame] | 3198 | TRI = MF->getSubtarget().getRegisterInfo(); |
| 3199 | TII = MF->getSubtarget().getInstrInfo(); |
Quentin Colombet | fb57392 | 2014-01-02 22:47:22 +0000 | [diff] [blame] | 3200 | RCI.runOnMachineFunction(mf); |
Quentin Colombet | 5599fde | 2014-07-02 18:32:04 +0000 | [diff] [blame] | 3201 | |
| 3202 | EnableLocalReassign = EnableLocalReassignment || |
Eric Christopher | 1dd55ba | 2014-10-14 07:22:00 +0000 | [diff] [blame] | 3203 | MF->getSubtarget().enableRALocalReassignment( |
| 3204 | MF->getTarget().getOptLevel()); |
Quentin Colombet | 5599fde | 2014-07-02 18:32:04 +0000 | [diff] [blame] | 3205 | |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3206 | EnableAdvancedRASplitCost = ConsiderLocalIntervalCost || |
| 3207 | MF->getSubtarget().enableAdvancedRASplitCost(); |
| 3208 | |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 3209 | if (VerifyEnabled) |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 3210 | MF->verify(this, "Before greedy register allocator"); |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 3211 | |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 3212 | RegAllocBase::init(getAnalysis<VirtRegMap>(), |
| 3213 | getAnalysis<LiveIntervals>(), |
| 3214 | getAnalysis<LiveRegMatrix>()); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 3215 | Indexes = &getAnalysis<SlotIndexes>(); |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 3216 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 3217 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3218 | ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE(); |
Jakob Stoklund Olesen | f6dff84 | 2010-12-10 22:54:44 +0000 | [diff] [blame] | 3219 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 3220 | Loops = &getAnalysis<MachineLoopInfo>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 3221 | Bundles = &getAnalysis<EdgeBundles>(); |
| 3222 | SpillPlacer = &getAnalysis<SpillPlacement>(); |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 3223 | DebugVars = &getAnalysis<LiveDebugVariables>(); |
Wei Mi | 4eae278 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 3224 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 3225 | |
Duncan P. N. Exon Smith | 861e4db | 2014-04-08 19:18:56 +0000 | [diff] [blame] | 3226 | initializeCSRCost(); |
| 3227 | |
Robert Lougher | 0d87d63 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 3228 | calculateSpillWeightsAndHints(*LIS, mf, VRM, *Loops, *MBFI); |
Arnaud A. de Grandmaison | a77da05 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 3229 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3230 | LLVM_DEBUG(LIS->dump()); |
Andrew Trick | 5dca613 | 2013-07-25 07:26:26 +0000 | [diff] [blame] | 3231 | |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 3232 | SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); |
Wei Mi | 4eae278 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 3233 | SE.reset(new SplitEditor(*SA, *AA, *LIS, *VRM, *DomTree, *MBFI)); |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 3234 | ExtraRegInfo.clear(); |
| 3235 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 3236 | NextCascade = 1; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 3237 | IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 3238 | GlobalCand.resize(32); // This will grow as needed. |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 3239 | SetOfBrokenHints.clear(); |
Marina Yatsina | b76f989 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 3240 | LastEvicted.clear(); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 3241 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3242 | allocatePhysRegs(); |
Quentin Colombet | 9d60e0f | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 3243 | tryHintsRecoloring(); |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 3244 | postOptimization(); |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 3245 | reportNumberOfSplillsReloads(); |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 3246 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3247 | releaseMemory(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 3248 | return true; |
| 3249 | } |