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 | |
| 10 | #define DEBUG_TYPE "calcspillweights" |
| 11 | |
| 12 | #include "llvm/Function.h" |
| 13 | #include "llvm/ADT/SmallSet.h" |
| 14 | #include "llvm/CodeGen/CalcSpillWeights.h" |
| 15 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 19 | #include "llvm/CodeGen/SlotIndexes.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 8112b53 | 2010-02-10 01:21:02 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | char CalculateSpillWeights::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 28 | INITIALIZE_PASS_BEGIN(CalculateSpillWeights, "calcspillweights", |
| 29 | "Calculate spill weights", false, false) |
| 30 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 31 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 32 | INITIALIZE_PASS_END(CalculateSpillWeights, "calcspillweights", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 33 | "Calculate spill weights", false, false) |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 34 | |
| 35 | void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const { |
| 36 | au.addRequired<LiveIntervals>(); |
| 37 | au.addRequired<MachineLoopInfo>(); |
| 38 | au.setPreservesAll(); |
| 39 | MachineFunctionPass::getAnalysisUsage(au); |
| 40 | } |
| 41 | |
| 42 | bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &fn) { |
| 43 | |
David Greene | 7ed6dd6 | 2009-12-24 00:39:02 +0000 | [diff] [blame] | 44 | DEBUG(dbgs() << "********** Compute Spill Weights **********\n" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 45 | << "********** Function: " |
| 46 | << fn.getFunction()->getName() << '\n'); |
| 47 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 48 | LiveIntervals &lis = getAnalysis<LiveIntervals>(); |
| 49 | VirtRegAuxInfo vrai(fn, lis, getAnalysis<MachineLoopInfo>()); |
| 50 | for (LiveIntervals::iterator I = lis.begin(), E = lis.end(); I != E; ++I) { |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 51 | LiveInterval &li = *I->second; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 52 | if (TargetRegisterInfo::isVirtualRegister(li.reg)) |
| 53 | vrai.CalculateWeightAndHint(li); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 54 | } |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 58 | // Return the preferred allocation register for reg, given a COPY instruction. |
| 59 | static unsigned copyHint(const MachineInstr *mi, unsigned reg, |
| 60 | const TargetRegisterInfo &tri, |
| 61 | const MachineRegisterInfo &mri) { |
| 62 | unsigned sub, hreg, hsub; |
| 63 | if (mi->getOperand(0).getReg() == reg) { |
| 64 | sub = mi->getOperand(0).getSubReg(); |
| 65 | hreg = mi->getOperand(1).getReg(); |
| 66 | hsub = mi->getOperand(1).getSubReg(); |
| 67 | } else { |
| 68 | sub = mi->getOperand(1).getSubReg(); |
| 69 | hreg = mi->getOperand(0).getReg(); |
| 70 | hsub = mi->getOperand(0).getSubReg(); |
| 71 | } |
| 72 | |
| 73 | if (!hreg) |
| 74 | return 0; |
| 75 | |
| 76 | if (TargetRegisterInfo::isVirtualRegister(hreg)) |
| 77 | return sub == hsub ? hreg : 0; |
| 78 | |
| 79 | const TargetRegisterClass *rc = mri.getRegClass(reg); |
| 80 | |
| 81 | // Only allow physreg hints in rc. |
| 82 | if (sub == 0) |
| 83 | return rc->contains(hreg) ? hreg : 0; |
| 84 | |
| 85 | // reg:sub should match the physreg hreg. |
| 86 | return tri.getMatchingSuperReg(hreg, sub, rc); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 87 | } |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 88 | |
| 89 | void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) { |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 90 | MachineRegisterInfo &mri = MF.getRegInfo(); |
| 91 | const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo(); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 92 | MachineBasicBlock *mbb = 0; |
| 93 | MachineLoop *loop = 0; |
| 94 | unsigned loopDepth = 0; |
| 95 | bool isExiting = false; |
| 96 | float totalWeight = 0; |
| 97 | SmallPtrSet<MachineInstr*, 8> visited; |
| 98 | |
| 99 | // Find the best physreg hist and the best virtreg hint. |
| 100 | float bestPhys = 0, bestVirt = 0; |
| 101 | unsigned hintPhys = 0, hintVirt = 0; |
| 102 | |
| 103 | // Don't recompute a target specific hint. |
| 104 | bool noHint = mri.getRegAllocationHint(li.reg).first != 0; |
| 105 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 106 | // Don't recompute spill weight for an unspillable register. |
| 107 | bool Spillable = li.isSpillable(); |
| 108 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 109 | for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg); |
| 110 | MachineInstr *mi = I.skipInstruction();) { |
| 111 | if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue()) |
| 112 | continue; |
| 113 | if (!visited.insert(mi)) |
| 114 | continue; |
| 115 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 116 | float weight = 1.0f; |
| 117 | if (Spillable) { |
| 118 | // Get loop info for mi. |
| 119 | if (mi->getParent() != mbb) { |
| 120 | mbb = mi->getParent(); |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 121 | loop = Loops.getLoopFor(mbb); |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 122 | loopDepth = loop ? loop->getLoopDepth() : 0; |
| 123 | isExiting = loop ? loop->isLoopExiting(mbb) : false; |
| 124 | } |
| 125 | |
| 126 | // Calculate instr weight. |
| 127 | bool reads, writes; |
| 128 | tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg); |
| 129 | weight = LiveIntervals::getSpillWeight(writes, reads, loopDepth); |
| 130 | |
| 131 | // 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^] | 132 | if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb)) |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 133 | weight *= 3; |
| 134 | |
| 135 | totalWeight += weight; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 138 | // Get allocation hints from copies. |
| 139 | if (noHint || !mi->isCopy()) |
| 140 | continue; |
| 141 | unsigned hint = copyHint(mi, li.reg, tri, mri); |
| 142 | if (!hint) |
| 143 | continue; |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 144 | float hweight = Hint[hint] += weight; |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 145 | if (TargetRegisterInfo::isPhysicalRegister(hint)) { |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 146 | if (hweight > bestPhys && LIS.isAllocatable(hint)) |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 147 | bestPhys = hweight, hintPhys = hint; |
| 148 | } else { |
| 149 | if (hweight > bestVirt) |
| 150 | bestVirt = hweight, hintVirt = hint; |
| 151 | } |
| 152 | } |
| 153 | |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 154 | Hint.clear(); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 155 | |
| 156 | // Always prefer the physreg hint. |
| 157 | if (unsigned hint = hintPhys ? hintPhys : hintVirt) { |
| 158 | mri.setRegAllocationHint(li.reg, 0, hint); |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 159 | // Weakly boost the spill weight of hinted registers. |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 160 | totalWeight *= 1.01F; |
| 161 | } |
| 162 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 163 | // If the live interval was already unspillable, leave it that way. |
| 164 | if (!Spillable) |
| 165 | return; |
| 166 | |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 167 | // Mark li as unspillable if all live ranges are tiny. |
| 168 | if (li.isZeroLength()) { |
| 169 | li.markNotSpillable(); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // If all of the definitions of the interval are re-materializable, |
| 174 | // it is a preferred candidate for spilling. If none of the defs are |
| 175 | // loads, then it's potentially very cheap to re-materialize. |
| 176 | // FIXME: this gets much more complicated once we support non-trivial |
| 177 | // re-materialization. |
| 178 | bool isLoad = false; |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 179 | if (LIS.isReMaterializable(li, 0, isLoad)) { |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 180 | if (isLoad) |
| 181 | totalWeight *= 0.9F; |
| 182 | else |
| 183 | totalWeight *= 0.5F; |
| 184 | } |
| 185 | |
Jakob Stoklund Olesen | eb9f040 | 2011-02-14 23:15:38 +0000 | [diff] [blame] | 186 | li.weight = normalizeSpillWeight(totalWeight, li.getSize()); |
Jakob Stoklund Olesen | df30cf9 | 2010-08-10 00:02:26 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 189 | void VirtRegAuxInfo::CalculateRegClass(unsigned reg) { |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 190 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 191 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
| 192 | const TargetRegisterClass *OldRC = MRI.getRegClass(reg); |
| 193 | const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC); |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 194 | |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 195 | // Stop early if there is no room to grow. |
| 196 | if (NewRC == OldRC) |
| 197 | return; |
| 198 | |
| 199 | // Accumulate constraints from all uses. |
| 200 | for (MachineRegisterInfo::reg_nodbg_iterator I = MRI.reg_nodbg_begin(reg), |
| 201 | E = MRI.reg_nodbg_end(); I != E; ++I) { |
| 202 | // TRI doesn't have accurate enough information to model this yet. |
| 203 | if (I.getOperand().getSubReg()) |
Jakob Stoklund Olesen | a516e94 | 2010-08-10 21:16:16 +0000 | [diff] [blame] | 204 | return; |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 205 | const TargetRegisterClass *OpRC = |
| 206 | I->getDesc().getRegClass(I.getOperandNo(), TRI); |
| 207 | if (OpRC) |
| 208 | NewRC = getCommonSubClass(NewRC, OpRC); |
| 209 | if (!NewRC || NewRC == OldRC) |
| 210 | return; |
Jakob Stoklund Olesen | a516e94 | 2010-08-10 21:16:16 +0000 | [diff] [blame] | 211 | } |
Jakob Stoklund Olesen | 1394e6d | 2011-04-26 18:52:36 +0000 | [diff] [blame^] | 212 | DEBUG(dbgs() << "Inflating " << OldRC->getName() << ':' << PrintReg(reg) |
| 213 | << " to " << NewRC->getName() <<".\n"); |
| 214 | MRI.setRegClass(reg, NewRC); |
Jakob Stoklund Olesen | 9db3ea4 | 2010-08-10 18:37:40 +0000 | [diff] [blame] | 215 | } |