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/TargetMachine.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetMachine.h"
28 char CalculateSpillWeights::ID = 0;
29 static RegisterPass<CalculateSpillWeights> X("calcspillweights",
30 "Calculate spill weights");
32 void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
33 au.addRequired<LiveIntervals>();
34 au.addRequired<MachineLoopInfo>();
36 MachineFunctionPass::getAnalysisUsage(au);
39 bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &fn) {
41 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
42 << "********** Function: "
43 << fn.getFunction()->getName() << '\n');
45 LiveIntervals *lis = &getAnalysis<LiveIntervals>();
46 MachineLoopInfo *loopInfo = &getAnalysis<MachineLoopInfo>();
47 const TargetInstrInfo *tii = fn.getTarget().getInstrInfo();
48 MachineRegisterInfo *mri = &fn.getRegInfo();
50 SmallSet<unsigned, 4> processed;
51 for (MachineFunction::iterator mbbi = fn.begin(), mbbe = fn.end();
52 mbbi != mbbe; ++mbbi) {
53 MachineBasicBlock* mbb = mbbi;
54 SlotIndex mbbEnd = lis->getMBBEndIdx(mbb);
55 MachineLoop* loop = loopInfo->getLoopFor(mbb);
56 unsigned loopDepth = loop ? loop->getLoopDepth() : 0;
57 bool isExiting = loop ? loop->isLoopExiting(mbb) : false;
59 for (MachineBasicBlock::const_iterator mii = mbb->begin(), mie = mbb->end();
61 const MachineInstr *mi = mii;
62 if (tii->isIdentityCopy(*mi) || mi->isImplicitDef() || mi->isDebugValue())
65 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
66 const MachineOperand &mopi = mi->getOperand(i);
67 if (!mopi.isReg() || mopi.getReg() == 0)
69 unsigned reg = mopi.getReg();
70 if (!TargetRegisterInfo::isVirtualRegister(mopi.getReg()))
72 // Multiple uses of reg by the same instruction. It should not
73 // contribute to spill weight again.
74 if (!processed.insert(reg))
77 bool hasDef = mopi.isDef();
78 bool hasUse = !hasDef;
79 for (unsigned j = i+1; j != e; ++j) {
80 const MachineOperand &mopj = mi->getOperand(j);
81 if (!mopj.isReg() || mopj.getReg() != reg)
83 hasDef |= mopj.isDef();
84 hasUse |= mopj.isUse();
89 LiveInterval ®Int = lis->getInterval(reg);
90 float weight = lis->getSpillWeight(hasDef, hasUse, loopDepth);
91 if (hasDef && isExiting) {
92 // Looks like this is a loop count variable update.
93 SlotIndex defIdx = lis->getInstructionIndex(mi).getDefIndex();
94 const LiveRange *dlr =
95 lis->getInterval(reg).getLiveRangeContaining(defIdx);
96 if (dlr->end >= mbbEnd)
99 regInt.weight += weight;
105 for (LiveIntervals::iterator I = lis->begin(), E = lis->end(); I != E; ++I) {
106 LiveInterval &li = *I->second;
107 if (TargetRegisterInfo::isVirtualRegister(li.reg)) {
108 // If the live interval length is essentially zero, i.e. in every live
109 // range the use follows def immediately, it doesn't make sense to spill
110 // it and hope it will be easier to allocate for this li.
111 if (isZeroLengthInterval(&li)) {
112 li.weight = HUGE_VALF;
117 SmallVector<LiveInterval*, 4> spillIs;
118 if (lis->isReMaterializable(li, spillIs, isLoad)) {
119 // If all of the definitions of the interval are re-materializable,
120 // it is a preferred candidate for spilling. If non of the defs are
121 // loads, then it's potentially very cheap to re-materialize.
122 // FIXME: this gets much more complicated once we support non-trivial
123 // re-materialization.
130 // Slightly prefer live interval that has been assigned a preferred reg.
131 std::pair<unsigned, unsigned> Hint = mri->getRegAllocationHint(li.reg);
132 if (Hint.first || Hint.second)
135 // Divide the weight of the interval by its size. This encourages
136 // spilling of intervals that are large and have few uses, and
137 // discourages spilling of small intervals with many uses.
138 li.weight /= lis->getApproximateInstructionCount(li) * SlotIndex::NUM;
145 /// Returns true if the given live interval is zero length.
146 bool CalculateSpillWeights::isZeroLengthInterval(LiveInterval *li) const {
147 for (LiveInterval::Ranges::const_iterator
148 i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
149 if (i->end.getPrevIndex() > i->start)