1 //===------------------------ CalcSpillWeights.cpp ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "calcspillweights"
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"
23 #include "llvm/Target/TargetRegisterInfo.h"
27 char CalculateSpillWeights::ID = 0;
28 static RegisterPass<CalculateSpillWeights> X("calcspillweights",
29 "Calculate spill weights");
31 void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
32 au.addRequired<LiveIntervals>();
33 au.addRequired<MachineLoopInfo>();
35 MachineFunctionPass::getAnalysisUsage(au);
38 bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &fn) {
40 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
41 << "********** Function: "
42 << fn.getFunction()->getName() << '\n');
44 LiveIntervals *lis = &getAnalysis<LiveIntervals>();
45 MachineLoopInfo *loopInfo = &getAnalysis<MachineLoopInfo>();
46 const TargetInstrInfo *tii = fn.getTarget().getInstrInfo();
47 MachineRegisterInfo *mri = &fn.getRegInfo();
49 SmallSet<unsigned, 4> processed;
50 for (MachineFunction::iterator mbbi = fn.begin(), mbbe = fn.end();
51 mbbi != mbbe; ++mbbi) {
52 MachineBasicBlock* mbb = mbbi;
53 SlotIndex mbbEnd = lis->getMBBEndIdx(mbb);
54 MachineLoop* loop = loopInfo->getLoopFor(mbb);
55 unsigned loopDepth = loop ? loop->getLoopDepth() : 0;
56 bool isExiting = loop ? loop->isLoopExiting(mbb) : false;
58 for (MachineBasicBlock::const_iterator mii = mbb->begin(), mie = mbb->end();
60 const MachineInstr *mi = mii;
61 if (tii->isIdentityCopy(*mi))
64 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
67 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
68 const MachineOperand &mopi = mi->getOperand(i);
69 if (!mopi.isReg() || mopi.getReg() == 0)
71 unsigned reg = mopi.getReg();
72 if (!TargetRegisterInfo::isVirtualRegister(mopi.getReg()))
74 // Multiple uses of reg by the same instruction. It should not
75 // contribute to spill weight again.
76 if (!processed.insert(reg))
79 bool hasDef = mopi.isDef();
80 bool hasUse = !hasDef;
81 for (unsigned j = i+1; j != e; ++j) {
82 const MachineOperand &mopj = mi->getOperand(j);
83 if (!mopj.isReg() || mopj.getReg() != reg)
85 hasDef |= mopj.isDef();
86 hasUse |= mopj.isUse();
91 LiveInterval ®Int = lis->getInterval(reg);
92 float weight = lis->getSpillWeight(hasDef, hasUse, loopDepth);
93 if (hasDef && isExiting) {
94 // Looks like this is a loop count variable update.
95 SlotIndex defIdx = lis->getInstructionIndex(mi).getDefIndex();
96 const LiveRange *dlr =
97 lis->getInterval(reg).getLiveRangeContaining(defIdx);
98 if (dlr->end >= mbbEnd)
101 regInt.weight += weight;
107 for (LiveIntervals::iterator I = lis->begin(), E = lis->end(); I != E; ++I) {
108 LiveInterval &li = *I->second;
109 if (TargetRegisterInfo::isVirtualRegister(li.reg)) {
110 // If the live interval length is essentially zero, i.e. in every live
111 // range the use follows def immediately, it doesn't make sense to spill
112 // it and hope it will be easier to allocate for this li.
113 if (isZeroLengthInterval(&li)) {
114 li.weight = HUGE_VALF;
119 SmallVector<LiveInterval*, 4> spillIs;
120 if (lis->isReMaterializable(li, spillIs, isLoad)) {
121 // If all of the definitions of the interval are re-materializable,
122 // it is a preferred candidate for spilling. If non of the defs are
123 // loads, then it's potentially very cheap to re-materialize.
124 // FIXME: this gets much more complicated once we support non-trivial
125 // re-materialization.
132 // Slightly prefer live interval that has been assigned a preferred reg.
133 std::pair<unsigned, unsigned> Hint = mri->getRegAllocationHint(li.reg);
134 if (Hint.first || Hint.second)
137 // Divide the weight of the interval by its size. This encourages
138 // spilling of intervals that are large and have few uses, and
139 // discourages spilling of small intervals with many uses.
140 li.weight /= lis->getApproximateInstructionCount(li) * SlotIndex::NUM;
147 /// Returns true if the given live interval is zero length.
148 bool CalculateSpillWeights::isZeroLengthInterval(LiveInterval *li) const {
149 for (LiveInterval::Ranges::const_iterator
150 i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
151 if (i->end.getPrevIndex() > i->start)