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; |
| 28 | static RegisterPass<CalculateSpillWeights> X("calcspillweights", |
| 29 | "Calculate spill weights"); |
| 30 | |
| 31 | void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const { |
| 32 | au.addRequired<LiveIntervals>(); |
| 33 | au.addRequired<MachineLoopInfo>(); |
| 34 | au.setPreservesAll(); |
| 35 | MachineFunctionPass::getAnalysisUsage(au); |
| 36 | } |
| 37 | |
| 38 | bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &fn) { |
| 39 | |
David Greene | 7ed6dd6 | 2009-12-24 00:39:02 +0000 | [diff] [blame] | 40 | DEBUG(dbgs() << "********** Compute Spill Weights **********\n" |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 41 | << "********** Function: " |
| 42 | << fn.getFunction()->getName() << '\n'); |
| 43 | |
| 44 | LiveIntervals *lis = &getAnalysis<LiveIntervals>(); |
| 45 | MachineLoopInfo *loopInfo = &getAnalysis<MachineLoopInfo>(); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 46 | MachineRegisterInfo *mri = &fn.getRegInfo(); |
| 47 | |
| 48 | SmallSet<unsigned, 4> processed; |
| 49 | for (MachineFunction::iterator mbbi = fn.begin(), mbbe = fn.end(); |
| 50 | mbbi != mbbe; ++mbbi) { |
| 51 | MachineBasicBlock* mbb = mbbi; |
| 52 | SlotIndex mbbEnd = lis->getMBBEndIdx(mbb); |
| 53 | MachineLoop* loop = loopInfo->getLoopFor(mbb); |
| 54 | unsigned loopDepth = loop ? loop->getLoopDepth() : 0; |
| 55 | bool isExiting = loop ? loop->isLoopExiting(mbb) : false; |
| 56 | |
| 57 | for (MachineBasicBlock::const_iterator mii = mbb->begin(), mie = mbb->end(); |
| 58 | mii != mie; ++mii) { |
| 59 | const MachineInstr *mi = mii; |
Jakob Stoklund Olesen | af462c4 | 2010-07-16 21:03:55 +0000 | [diff] [blame] | 60 | if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue()) |
Dale Johannesen | d94998f | 2010-02-09 02:01:46 +0000 | [diff] [blame] | 61 | continue; |
| 62 | |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 63 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 64 | const MachineOperand &mopi = mi->getOperand(i); |
| 65 | if (!mopi.isReg() || mopi.getReg() == 0) |
| 66 | continue; |
| 67 | unsigned reg = mopi.getReg(); |
| 68 | if (!TargetRegisterInfo::isVirtualRegister(mopi.getReg())) |
| 69 | continue; |
| 70 | // Multiple uses of reg by the same instruction. It should not |
| 71 | // contribute to spill weight again. |
| 72 | if (!processed.insert(reg)) |
| 73 | continue; |
| 74 | |
| 75 | bool hasDef = mopi.isDef(); |
| 76 | bool hasUse = !hasDef; |
| 77 | for (unsigned j = i+1; j != e; ++j) { |
| 78 | const MachineOperand &mopj = mi->getOperand(j); |
| 79 | if (!mopj.isReg() || mopj.getReg() != reg) |
| 80 | continue; |
| 81 | hasDef |= mopj.isDef(); |
| 82 | hasUse |= mopj.isUse(); |
| 83 | if (hasDef && hasUse) |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | LiveInterval ®Int = lis->getInterval(reg); |
| 88 | float weight = lis->getSpillWeight(hasDef, hasUse, loopDepth); |
| 89 | if (hasDef && isExiting) { |
| 90 | // Looks like this is a loop count variable update. |
| 91 | SlotIndex defIdx = lis->getInstructionIndex(mi).getDefIndex(); |
| 92 | const LiveRange *dlr = |
| 93 | lis->getInterval(reg).getLiveRangeContaining(defIdx); |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 94 | if (dlr->end >= mbbEnd) |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 95 | weight *= 3.0F; |
| 96 | } |
| 97 | regInt.weight += weight; |
| 98 | } |
| 99 | processed.clear(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | for (LiveIntervals::iterator I = lis->begin(), E = lis->end(); I != E; ++I) { |
| 104 | LiveInterval &li = *I->second; |
| 105 | if (TargetRegisterInfo::isVirtualRegister(li.reg)) { |
| 106 | // If the live interval length is essentially zero, i.e. in every live |
| 107 | // range the use follows def immediately, it doesn't make sense to spill |
| 108 | // it and hope it will be easier to allocate for this li. |
| 109 | if (isZeroLengthInterval(&li)) { |
| 110 | li.weight = HUGE_VALF; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | bool isLoad = false; |
| 115 | SmallVector<LiveInterval*, 4> spillIs; |
| 116 | if (lis->isReMaterializable(li, spillIs, isLoad)) { |
| 117 | // If all of the definitions of the interval are re-materializable, |
Eric Christopher | 00e840f | 2010-07-03 01:09:18 +0000 | [diff] [blame] | 118 | // it is a preferred candidate for spilling. If none of the defs are |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 119 | // loads, then it's potentially very cheap to re-materialize. |
| 120 | // FIXME: this gets much more complicated once we support non-trivial |
| 121 | // re-materialization. |
| 122 | if (isLoad) |
| 123 | li.weight *= 0.9F; |
| 124 | else |
| 125 | li.weight *= 0.5F; |
| 126 | } |
| 127 | |
| 128 | // Slightly prefer live interval that has been assigned a preferred reg. |
| 129 | std::pair<unsigned, unsigned> Hint = mri->getRegAllocationHint(li.reg); |
| 130 | if (Hint.first || Hint.second) |
| 131 | li.weight *= 1.01F; |
| 132 | |
Jakob Stoklund Olesen | 352d352 | 2010-02-18 21:33:05 +0000 | [diff] [blame] | 133 | lis->normalizeSpillWeight(li); |
Lang Hames | a937f22 | 2009-12-14 06:49:42 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /// Returns true if the given live interval is zero length. |
| 141 | bool CalculateSpillWeights::isZeroLengthInterval(LiveInterval *li) const { |
| 142 | for (LiveInterval::Ranges::const_iterator |
| 143 | i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i) |
| 144 | if (i->end.getPrevIndex() > i->start) |
| 145 | return false; |
| 146 | return true; |
| 147 | } |