blob: 7da833bbbf8f0b48b144e302151d29a0369e0204 [file] [log] [blame]
Lang Hamesa937f222009-12-14 06:49:42 +00001//===------------------------ 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"
23#include "llvm/Target/TargetRegisterInfo.h"
24
25using namespace llvm;
26
27char CalculateSpillWeights::ID = 0;
28static RegisterPass<CalculateSpillWeights> X("calcspillweights",
29 "Calculate spill weights");
30
31void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
32 au.addRequired<LiveIntervals>();
33 au.addRequired<MachineLoopInfo>();
34 au.setPreservesAll();
35 MachineFunctionPass::getAnalysisUsage(au);
36}
37
38bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &fn) {
39
David Greene7ed6dd62009-12-24 00:39:02 +000040 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
Lang Hamesa937f222009-12-14 06:49:42 +000041 << "********** Function: "
42 << fn.getFunction()->getName() << '\n');
43
44 LiveIntervals *lis = &getAnalysis<LiveIntervals>();
45 MachineLoopInfo *loopInfo = &getAnalysis<MachineLoopInfo>();
46 const TargetInstrInfo *tii = fn.getTarget().getInstrInfo();
47 MachineRegisterInfo *mri = &fn.getRegInfo();
48
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;
57
58 for (MachineBasicBlock::const_iterator mii = mbb->begin(), mie = mbb->end();
59 mii != mie; ++mii) {
60 const MachineInstr *mi = mii;
61 if (tii->isIdentityCopy(*mi))
62 continue;
63
64 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
65 continue;
66
Dale Johannesend94998f2010-02-09 02:01:46 +000067 if (mi->getOpcode() == TargetInstrInfo::DEBUG_VALUE)
68 continue;
69
Lang Hamesa937f222009-12-14 06:49:42 +000070 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
71 const MachineOperand &mopi = mi->getOperand(i);
72 if (!mopi.isReg() || mopi.getReg() == 0)
73 continue;
74 unsigned reg = mopi.getReg();
75 if (!TargetRegisterInfo::isVirtualRegister(mopi.getReg()))
76 continue;
77 // Multiple uses of reg by the same instruction. It should not
78 // contribute to spill weight again.
79 if (!processed.insert(reg))
80 continue;
81
82 bool hasDef = mopi.isDef();
83 bool hasUse = !hasDef;
84 for (unsigned j = i+1; j != e; ++j) {
85 const MachineOperand &mopj = mi->getOperand(j);
86 if (!mopj.isReg() || mopj.getReg() != reg)
87 continue;
88 hasDef |= mopj.isDef();
89 hasUse |= mopj.isUse();
90 if (hasDef && hasUse)
91 break;
92 }
93
94 LiveInterval &regInt = lis->getInterval(reg);
95 float weight = lis->getSpillWeight(hasDef, hasUse, loopDepth);
96 if (hasDef && isExiting) {
97 // Looks like this is a loop count variable update.
98 SlotIndex defIdx = lis->getInstructionIndex(mi).getDefIndex();
99 const LiveRange *dlr =
100 lis->getInterval(reg).getLiveRangeContaining(defIdx);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000101 if (dlr->end >= mbbEnd)
Lang Hamesa937f222009-12-14 06:49:42 +0000102 weight *= 3.0F;
103 }
104 regInt.weight += weight;
105 }
106 processed.clear();
107 }
108 }
109
110 for (LiveIntervals::iterator I = lis->begin(), E = lis->end(); I != E; ++I) {
111 LiveInterval &li = *I->second;
112 if (TargetRegisterInfo::isVirtualRegister(li.reg)) {
113 // If the live interval length is essentially zero, i.e. in every live
114 // range the use follows def immediately, it doesn't make sense to spill
115 // it and hope it will be easier to allocate for this li.
116 if (isZeroLengthInterval(&li)) {
117 li.weight = HUGE_VALF;
118 continue;
119 }
120
121 bool isLoad = false;
122 SmallVector<LiveInterval*, 4> spillIs;
123 if (lis->isReMaterializable(li, spillIs, isLoad)) {
124 // If all of the definitions of the interval are re-materializable,
125 // it is a preferred candidate for spilling. If non of the defs are
126 // loads, then it's potentially very cheap to re-materialize.
127 // FIXME: this gets much more complicated once we support non-trivial
128 // re-materialization.
129 if (isLoad)
130 li.weight *= 0.9F;
131 else
132 li.weight *= 0.5F;
133 }
134
135 // Slightly prefer live interval that has been assigned a preferred reg.
136 std::pair<unsigned, unsigned> Hint = mri->getRegAllocationHint(li.reg);
137 if (Hint.first || Hint.second)
138 li.weight *= 1.01F;
139
140 // Divide the weight of the interval by its size. This encourages
141 // spilling of intervals that are large and have few uses, and
142 // discourages spilling of small intervals with many uses.
143 li.weight /= lis->getApproximateInstructionCount(li) * SlotIndex::NUM;
144 }
145 }
146
147 return false;
148}
149
150/// Returns true if the given live interval is zero length.
151bool CalculateSpillWeights::isZeroLengthInterval(LiveInterval *li) const {
152 for (LiveInterval::Ranges::const_iterator
153 i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
154 if (i->end.getPrevIndex() > i->start)
155 return false;
156 return true;
157}