Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 1 | //===- CalcSpillWeights.cpp -----------------------------------------------===// |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallPtrSet.h" |
| 12 | #include "llvm/CodeGen/LiveInterval.h" |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 14 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineInstr.h" |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineOperand.h" |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame^] | 20 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/VirtRegMap.h" |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | #include <tuple> |
| 27 | |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "calcspillweights" |
| 31 | |
Arnaud A. de Grandmaison | ea3ac16 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 32 | void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS, |
Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 33 | MachineFunction &MF, |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 34 | VirtRegMap *VRM, |
Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 35 | const MachineLoopInfo &MLI, |
Arnaud A. de Grandmaison | f5f040f | 2013-11-11 19:56:14 +0000 | [diff] [blame] | 36 | const MachineBlockFrequencyInfo &MBFI, |
| 37 | VirtRegAuxInfo::NormalizingFn norm) { |
David Greene | e40730d | 2009-12-24 00:39:02 +0000 | [diff] [blame] | 38 | DEBUG(dbgs() << "********** Compute Spill Weights **********\n" |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 39 | << "********** Function: " << MF.getName() << '\n'); |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 40 | |
Jakob Stoklund Olesen | a1f43dc | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 41 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 42 | VirtRegAuxInfo VRAI(MF, LIS, VRM, MLI, MBFI, norm); |
Jakob Stoklund Olesen | a1f43dc | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 43 | for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) { |
| 44 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 45 | if (MRI.reg_nodbg_empty(Reg)) |
| 46 | continue; |
Arnaud A. de Grandmaison | ea3ac16 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 47 | VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg)); |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 48 | } |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 51 | // Return the preferred allocation register for reg, given a COPY instruction. |
| 52 | static unsigned copyHint(const MachineInstr *mi, unsigned reg, |
| 53 | const TargetRegisterInfo &tri, |
| 54 | const MachineRegisterInfo &mri) { |
| 55 | unsigned sub, hreg, hsub; |
| 56 | if (mi->getOperand(0).getReg() == reg) { |
| 57 | sub = mi->getOperand(0).getSubReg(); |
| 58 | hreg = mi->getOperand(1).getReg(); |
| 59 | hsub = mi->getOperand(1).getSubReg(); |
| 60 | } else { |
| 61 | sub = mi->getOperand(1).getSubReg(); |
| 62 | hreg = mi->getOperand(0).getReg(); |
| 63 | hsub = mi->getOperand(0).getSubReg(); |
| 64 | } |
| 65 | |
| 66 | if (!hreg) |
| 67 | return 0; |
| 68 | |
| 69 | if (TargetRegisterInfo::isVirtualRegister(hreg)) |
| 70 | return sub == hsub ? hreg : 0; |
| 71 | |
| 72 | const TargetRegisterClass *rc = mri.getRegClass(reg); |
| 73 | |
| 74 | // Only allow physreg hints in rc. |
| 75 | if (sub == 0) |
| 76 | return rc->contains(hreg) ? hreg : 0; |
| 77 | |
| 78 | // reg:sub should match the physreg hreg. |
| 79 | return tri.getMatchingSuperReg(hreg, sub, rc); |
Lang Hames | d17e296 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 80 | } |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 81 | |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 82 | // Check if all values in LI are rematerializable |
| 83 | static bool isRematerializable(const LiveInterval &LI, |
| 84 | const LiveIntervals &LIS, |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 85 | VirtRegMap *VRM, |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 86 | const TargetInstrInfo &TII) { |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 87 | unsigned Reg = LI.reg; |
| 88 | unsigned Original = VRM ? VRM->getOriginal(Reg) : 0; |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 89 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); |
| 90 | I != E; ++I) { |
| 91 | const VNInfo *VNI = *I; |
| 92 | if (VNI->isUnused()) |
| 93 | continue; |
| 94 | if (VNI->isPHIDef()) |
| 95 | return false; |
| 96 | |
| 97 | MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def); |
| 98 | assert(MI && "Dead valno in interval"); |
| 99 | |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 100 | // Trace copies introduced by live range splitting. The inline |
| 101 | // spiller can rematerialize through these copies, so the spill |
| 102 | // weight must reflect this. |
| 103 | if (VRM) { |
| 104 | while (MI->isFullCopy()) { |
| 105 | // The copy destination must match the interval register. |
| 106 | if (MI->getOperand(0).getReg() != Reg) |
| 107 | return false; |
| 108 | |
| 109 | // Get the source register. |
| 110 | Reg = MI->getOperand(1).getReg(); |
| 111 | |
| 112 | // If the original (pre-splitting) registers match this |
| 113 | // copy came from a split. |
| 114 | if (!TargetRegisterInfo::isVirtualRegister(Reg) || |
| 115 | VRM->getOriginal(Reg) != Original) |
| 116 | return false; |
| 117 | |
| 118 | // Follow the copy live-in value. |
| 119 | const LiveInterval &SrcLI = LIS.getInterval(Reg); |
| 120 | LiveQueryResult SrcQ = SrcLI.Query(VNI->def); |
| 121 | VNI = SrcQ.valueIn(); |
| 122 | assert(VNI && "Copy from non-existing value"); |
| 123 | if (VNI->isPHIDef()) |
| 124 | return false; |
| 125 | MI = LIS.getInstructionFromIndex(VNI->def); |
| 126 | assert(MI && "Dead valno in interval"); |
| 127 | } |
| 128 | } |
| 129 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 130 | if (!TII.isTriviallyReMaterializable(*MI, LIS.getAliasAnalysis())) |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 136 | void VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) { |
| 137 | float weight = weightCalcHelper(li); |
| 138 | // Check if unspillable. |
| 139 | if (weight < 0) |
| 140 | return; |
| 141 | li.weight = weight; |
| 142 | } |
| 143 | |
| 144 | float VirtRegAuxInfo::futureWeight(LiveInterval &li, SlotIndex start, |
| 145 | SlotIndex end) { |
| 146 | return weightCalcHelper(li, &start, &end); |
| 147 | } |
| 148 | |
| 149 | float VirtRegAuxInfo::weightCalcHelper(LiveInterval &li, SlotIndex *start, |
| 150 | SlotIndex *end) { |
Jakob Stoklund Olesen | 3b7b7bc | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 151 | MachineRegisterInfo &mri = MF.getRegInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 152 | const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 153 | MachineBasicBlock *mbb = nullptr; |
| 154 | MachineLoop *loop = nullptr; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 155 | bool isExiting = false; |
| 156 | float totalWeight = 0; |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 157 | unsigned numInstr = 0; // Number of instructions using li |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 158 | SmallPtrSet<MachineInstr*, 8> visited; |
| 159 | |
Nadav Rotem | c4bd84c | 2013-04-06 04:24:12 +0000 | [diff] [blame] | 160 | // Find the best physreg hint and the best virtreg hint. |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 161 | float bestPhys = 0, bestVirt = 0; |
| 162 | unsigned hintPhys = 0, hintVirt = 0; |
| 163 | |
| 164 | // Don't recompute a target specific hint. |
| 165 | bool noHint = mri.getRegAllocationHint(li.reg).first != 0; |
| 166 | |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 167 | // Don't recompute spill weight for an unspillable register. |
| 168 | bool Spillable = li.isSpillable(); |
| 169 | |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 170 | bool localSplitArtifact = start && end; |
| 171 | |
| 172 | // Do not update future local split artifacts. |
| 173 | bool updateLI = !localSplitArtifact; |
| 174 | |
| 175 | if (localSplitArtifact) { |
| 176 | MachineBasicBlock *localMBB = LIS.getMBBFromIndex(*end); |
| 177 | assert(localMBB == LIS.getMBBFromIndex(*start) && |
| 178 | "start and end are expected to be in the same basic block"); |
| 179 | |
| 180 | // Local split artifact will have 2 additional copy instructions and they |
| 181 | // will be in the same BB. |
| 182 | // localLI = COPY other |
| 183 | // ... |
| 184 | // other = COPY localLI |
| 185 | totalWeight += LiveIntervals::getSpillWeight(true, false, &MBFI, localMBB); |
| 186 | totalWeight += LiveIntervals::getSpillWeight(false, true, &MBFI, localMBB); |
| 187 | |
| 188 | numInstr += 2; |
| 189 | } |
| 190 | |
Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 191 | for (MachineRegisterInfo::reg_instr_iterator |
| 192 | I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end(); |
| 193 | I != E; ) { |
| 194 | MachineInstr *mi = &*(I++); |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 195 | |
| 196 | // For local split artifacts, we are interested only in instructions between |
| 197 | // the expected start and end of the range. |
| 198 | SlotIndex si = LIS.getInstructionIndex(*mi); |
| 199 | if (localSplitArtifact && ((si < *start) || (si > *end))) |
| 200 | continue; |
| 201 | |
Arnaud A. de Grandmaison | 829dd81 | 2014-11-04 20:51:24 +0000 | [diff] [blame] | 202 | numInstr++; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 203 | if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue()) |
| 204 | continue; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 205 | if (!visited.insert(mi).second) |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 206 | continue; |
| 207 | |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 208 | float weight = 1.0f; |
| 209 | if (Spillable) { |
| 210 | // Get loop info for mi. |
| 211 | if (mi->getParent() != mbb) { |
| 212 | mbb = mi->getParent(); |
Jakob Stoklund Olesen | 3b7b7bc | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 213 | loop = Loops.getLoopFor(mbb); |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 214 | isExiting = loop ? loop->isLoopExiting(mbb) : false; |
| 215 | } |
| 216 | |
| 217 | // Calculate instr weight. |
| 218 | bool reads, writes; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 219 | std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg); |
Duncan P. N. Exon Smith | be8f8c4 | 2016-02-27 20:14:29 +0000 | [diff] [blame] | 220 | weight = LiveIntervals::getSpillWeight(writes, reads, &MBFI, *mi); |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 221 | |
| 222 | // Give extra weight to what looks like a loop induction variable update. |
Jakob Stoklund Olesen | 3b7b7bc | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 223 | if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb)) |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 224 | weight *= 3; |
| 225 | |
| 226 | totalWeight += weight; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 229 | // Get allocation hints from copies. |
| 230 | if (noHint || !mi->isCopy()) |
| 231 | continue; |
| 232 | unsigned hint = copyHint(mi, li.reg, tri, mri); |
| 233 | if (!hint) |
| 234 | continue; |
Duncan P. N. Exon Smith | 7af3432 | 2014-04-21 17:57:01 +0000 | [diff] [blame] | 235 | // Force hweight onto the stack so that x86 doesn't add hidden precision, |
| 236 | // making the comparison incorrectly pass (i.e., 1 > 1 == true??). |
| 237 | // |
| 238 | // FIXME: we probably shouldn't use floats at all. |
| 239 | volatile float hweight = Hint[hint] += weight; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 240 | if (TargetRegisterInfo::isPhysicalRegister(hint)) { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 241 | if (hweight > bestPhys && mri.isAllocatable(hint)) { |
| 242 | bestPhys = hweight; |
| 243 | hintPhys = hint; |
| 244 | } |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 245 | } else { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 246 | if (hweight > bestVirt) { |
| 247 | bestVirt = hweight; |
| 248 | hintVirt = hint; |
| 249 | } |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Jakob Stoklund Olesen | 3b7b7bc | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 253 | Hint.clear(); |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 254 | |
| 255 | // Always prefer the physreg hint. |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 256 | if (updateLI) { |
| 257 | if (unsigned hint = hintPhys ? hintPhys : hintVirt) { |
| 258 | mri.setRegAllocationHint(li.reg, 0, hint); |
| 259 | // Weakly boost the spill weight of hinted registers. |
| 260 | totalWeight *= 1.01F; |
| 261 | } |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 264 | // If the live interval was already unspillable, leave it that way. |
| 265 | if (!Spillable) |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 266 | return -1.0; |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 267 | |
Andrew Kaylor | 1224488 | 2016-02-08 22:52:51 +0000 | [diff] [blame] | 268 | // Mark li as unspillable if all live ranges are tiny and the interval |
| 269 | // is not live at any reg mask. If the interval is live at a reg mask |
| 270 | // spilling may be required. |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 271 | if (updateLI && li.isZeroLength(LIS.getSlotIndexes()) && |
Andrew Kaylor | 1224488 | 2016-02-08 22:52:51 +0000 | [diff] [blame] | 272 | !li.isLiveAtIndexes(LIS.getRegMaskSlots())) { |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 273 | li.markNotSpillable(); |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 274 | return -1.0; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // If all of the definitions of the interval are re-materializable, |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 278 | // it is a preferred candidate for spilling. |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 279 | // FIXME: this gets much more complicated once we support non-trivial |
| 280 | // re-materialization. |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 281 | if (isRematerializable(li, LIS, VRM, *MF.getSubtarget().getInstrInfo())) |
Jakob Stoklund Olesen | 9e27e26 | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 282 | totalWeight *= 0.5F; |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 283 | |
Marina Yatsina | f9371d8 | 2017-10-22 17:59:38 +0000 | [diff] [blame] | 284 | if (localSplitArtifact) |
| 285 | return normalize(totalWeight, start->distance(*end), numInstr); |
| 286 | return normalize(totalWeight, li.getSize(), numInstr); |
Jakob Stoklund Olesen | e00c49d | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 287 | } |