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