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