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