Lang Hames | a937f22 | 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 | |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/CalcSpillWeights.h" |
| 11 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunction.h" |
| 14 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 15 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetRegisterInfo.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 23 | #define DEBUG_TYPE "calcspillweights" |
| 24 | |
Arnaud A. de Grandmaison | 095f994 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 25 | void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS, |
Arnaud A. de Grandmaison | a77da05 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 26 | MachineFunction &MF, |
| 27 | const MachineLoopInfo &MLI, |
Arnaud A. de Grandmaison | d736763 | 2013-11-11 19:56:14 +0000 | [diff] [blame] | 28 | const MachineBlockFrequencyInfo &MBFI, |
| 29 | VirtRegAuxInfo::NormalizingFn norm) { |
David Greene | 7ed6dd6 | 2009-12-24 00:39:02 +0000 | [diff] [blame] | 30 | DEBUG(dbgs() << "********** Compute Spill Weights **********\n" |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 31 | << "********** Function: " << MF.getName() << '\n'); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 32 | |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 33 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Arnaud A. de Grandmaison | d736763 | 2013-11-11 19:56:14 +0000 | [diff] [blame] | 34 | VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm); |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 35 | for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) { |
| 36 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 37 | if (MRI.reg_nodbg_empty(Reg)) |
| 38 | continue; |
Arnaud A. de Grandmaison | 095f994 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 39 | VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg)); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 40 | } |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 43 | // Return the preferred allocation register for reg, given a COPY instruction. |
| 44 | static unsigned copyHint(const MachineInstr *mi, unsigned reg, |
| 45 | const TargetRegisterInfo &tri, |
| 46 | const MachineRegisterInfo &mri) { |
| 47 | unsigned sub, hreg, hsub; |
| 48 | if (mi->getOperand(0).getReg() == reg) { |
| 49 | sub = mi->getOperand(0).getSubReg(); |
| 50 | hreg = mi->getOperand(1).getReg(); |
| 51 | hsub = mi->getOperand(1).getSubReg(); |
| 52 | } else { |
| 53 | sub = mi->getOperand(1).getSubReg(); |
| 54 | hreg = mi->getOperand(0).getReg(); |
| 55 | hsub = mi->getOperand(0).getSubReg(); |
| 56 | } |
| 57 | |
| 58 | if (!hreg) |
| 59 | return 0; |
| 60 | |
| 61 | if (TargetRegisterInfo::isVirtualRegister(hreg)) |
| 62 | return sub == hsub ? hreg : 0; |
| 63 | |
| 64 | const TargetRegisterClass *rc = mri.getRegClass(reg); |
| 65 | |
| 66 | // Only allow physreg hints in rc. |
| 67 | if (sub == 0) |
| 68 | return rc->contains(hreg) ? hreg : 0; |
| 69 | |
| 70 | // reg:sub should match the physreg hreg. |
| 71 | return tri.getMatchingSuperReg(hreg, sub, rc); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 72 | } |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 73 | |
Jakob Stoklund Olesen | 3dfd59b | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 74 | // Check if all values in LI are rematerializable |
| 75 | static bool isRematerializable(const LiveInterval &LI, |
| 76 | const LiveIntervals &LIS, |
| 77 | const TargetInstrInfo &TII) { |
| 78 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); |
| 79 | I != E; ++I) { |
| 80 | const VNInfo *VNI = *I; |
| 81 | if (VNI->isUnused()) |
| 82 | continue; |
| 83 | if (VNI->isPHIDef()) |
| 84 | return false; |
| 85 | |
| 86 | MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def); |
| 87 | assert(MI && "Dead valno in interval"); |
| 88 | |
| 89 | if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis())) |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 95 | void |
Arnaud A. de Grandmaison | 095f994 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 96 | VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) { |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 97 | MachineRegisterInfo &mri = MF.getRegInfo(); |
| 98 | const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 99 | MachineBasicBlock *mbb = nullptr; |
| 100 | MachineLoop *loop = nullptr; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 101 | bool isExiting = false; |
| 102 | float totalWeight = 0; |
| 103 | SmallPtrSet<MachineInstr*, 8> visited; |
| 104 | |
Nadav Rotem | 6e01dcb | 2013-04-06 04:24:12 +0000 | [diff] [blame] | 105 | // Find the best physreg hint and the best virtreg hint. |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 106 | float bestPhys = 0, bestVirt = 0; |
| 107 | unsigned hintPhys = 0, hintVirt = 0; |
| 108 | |
| 109 | // Don't recompute a target specific hint. |
| 110 | bool noHint = mri.getRegAllocationHint(li.reg).first != 0; |
| 111 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 112 | // Don't recompute spill weight for an unspillable register. |
| 113 | bool Spillable = li.isSpillable(); |
| 114 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 115 | for (MachineRegisterInfo::reg_instr_iterator |
| 116 | I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end(); |
| 117 | I != E; ) { |
| 118 | MachineInstr *mi = &*(I++); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 119 | if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue()) |
| 120 | continue; |
| 121 | if (!visited.insert(mi)) |
| 122 | continue; |
| 123 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 124 | float weight = 1.0f; |
| 125 | if (Spillable) { |
| 126 | // Get loop info for mi. |
| 127 | if (mi->getParent() != mbb) { |
| 128 | mbb = mi->getParent(); |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 129 | loop = Loops.getLoopFor(mbb); |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 130 | isExiting = loop ? loop->isLoopExiting(mbb) : false; |
| 131 | } |
| 132 | |
| 133 | // Calculate instr weight. |
| 134 | bool reads, writes; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 135 | std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg); |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 136 | weight = LiveIntervals::getSpillWeight( |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 137 | writes, reads, &MBFI, mi); |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 138 | |
| 139 | // Give extra weight to what looks like a loop induction variable update. |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 140 | if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb)) |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 141 | weight *= 3; |
| 142 | |
| 143 | totalWeight += weight; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 146 | // Get allocation hints from copies. |
| 147 | if (noHint || !mi->isCopy()) |
| 148 | continue; |
| 149 | unsigned hint = copyHint(mi, li.reg, tri, mri); |
| 150 | if (!hint) |
| 151 | continue; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 152 | // Force hweight onto the stack so that x86 doesn't add hidden precision, |
| 153 | // making the comparison incorrectly pass (i.e., 1 > 1 == true??). |
| 154 | // |
| 155 | // FIXME: we probably shouldn't use floats at all. |
| 156 | volatile float hweight = Hint[hint] += weight; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 157 | if (TargetRegisterInfo::isPhysicalRegister(hint)) { |
Jakob Stoklund Olesen | 7900476 | 2012-10-15 22:14:34 +0000 | [diff] [blame] | 158 | if (hweight > bestPhys && mri.isAllocatable(hint)) |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 159 | bestPhys = hweight, hintPhys = hint; |
| 160 | } else { |
| 161 | if (hweight > bestVirt) |
| 162 | bestVirt = hweight, hintVirt = hint; |
| 163 | } |
| 164 | } |
| 165 | |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame] | 166 | Hint.clear(); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 167 | |
| 168 | // Always prefer the physreg hint. |
| 169 | if (unsigned hint = hintPhys ? hintPhys : hintVirt) { |
| 170 | mri.setRegAllocationHint(li.reg, 0, hint); |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 171 | // Weakly boost the spill weight of hinted registers. |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 172 | totalWeight *= 1.01F; |
| 173 | } |
| 174 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 175 | // If the live interval was already unspillable, leave it that way. |
| 176 | if (!Spillable) |
| 177 | return; |
| 178 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 179 | // Mark li as unspillable if all live ranges are tiny. |
Jakob Stoklund Olesen | f5497fb | 2011-05-16 23:50:05 +0000 | [diff] [blame] | 180 | if (li.isZeroLength(LIS.getSlotIndexes())) { |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 181 | li.markNotSpillable(); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // If all of the definitions of the interval are re-materializable, |
Jakob Stoklund Olesen | 3dfd59b | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 186 | // it is a preferred candidate for spilling. |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 187 | // FIXME: this gets much more complicated once we support non-trivial |
| 188 | // re-materialization. |
Jakob Stoklund Olesen | 3dfd59b | 2012-06-05 01:06:12 +0000 | [diff] [blame] | 189 | if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo())) |
| 190 | totalWeight *= 0.5F; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 191 | |
Arnaud A. de Grandmaison | d736763 | 2013-11-11 19:56:14 +0000 | [diff] [blame] | 192 | li.weight = normalize(totalWeight, li.getSize()); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 193 | } |