1 //===-- llvm/CodeGen/Spiller.cpp - Spiller -------------------------------===//
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 "spiller"
13 #include "VirtRegMap.h"
14 #include "LiveRangeEdit.h"
15 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
16 #include "llvm/CodeGen/LiveStackAnalysis.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineLoopInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
33 enum SpillerName
{ trivial
, standard
, inline_
};
36 static cl::opt
<SpillerName
>
38 cl::desc("Spiller to use: (default: standard)"),
40 cl::values(clEnumVal(trivial
, "trivial spiller"),
41 clEnumVal(standard
, "default spiller"),
42 clEnumValN(inline_
, "inline", "inline spiller"),
46 // Spiller virtual destructor implementation.
47 Spiller::~Spiller() {}
51 /// Utility class for spillers.
52 class SpillerBase
: public Spiller
{
54 MachineFunctionPass
*pass
;
58 MachineFrameInfo
*mfi
;
59 MachineRegisterInfo
*mri
;
60 const TargetInstrInfo
*tii
;
61 const TargetRegisterInfo
*tri
;
63 /// Construct a spiller base.
64 SpillerBase(MachineFunctionPass
&pass
, MachineFunction
&mf
, VirtRegMap
&vrm
)
65 : pass(&pass
), mf(&mf
), vrm(&vrm
)
67 lis
= &pass
.getAnalysis
<LiveIntervals
>();
68 mfi
= mf
.getFrameInfo();
69 mri
= &mf
.getRegInfo();
70 tii
= mf
.getTarget().getInstrInfo();
71 tri
= mf
.getTarget().getRegisterInfo();
74 /// Add spill ranges for every use/def of the live interval, inserting loads
75 /// immediately before each use, and stores after each def. No folding or
76 /// remat is attempted.
77 void trivialSpillEverywhere(LiveInterval
*li
,
78 SmallVectorImpl
<LiveInterval
*> &newIntervals
) {
79 DEBUG(dbgs() << "Spilling everywhere " << *li
<< "\n");
81 assert(li
->weight
!= HUGE_VALF
&&
82 "Attempting to spill already spilled value.");
84 assert(!TargetRegisterInfo::isStackSlot(li
->reg
) &&
85 "Trying to spill a stack slot.");
87 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li
->reg
<< "\n");
89 const TargetRegisterClass
*trc
= mri
->getRegClass(li
->reg
);
90 unsigned ss
= vrm
->assignVirt2StackSlot(li
->reg
);
92 // Iterate over reg uses/defs.
93 for (MachineRegisterInfo::reg_iterator
94 regItr
= mri
->reg_begin(li
->reg
); regItr
!= mri
->reg_end();) {
96 // Grab the use/def instr.
97 MachineInstr
*mi
= &*regItr
;
99 DEBUG(dbgs() << " Processing " << *mi
);
101 // Step regItr to the next use/def instr.
104 } while (regItr
!= mri
->reg_end() && (&*regItr
== mi
));
106 // Collect uses & defs for this instr.
107 SmallVector
<unsigned, 2> indices
;
110 for (unsigned i
= 0; i
!= mi
->getNumOperands(); ++i
) {
111 MachineOperand
&op
= mi
->getOperand(i
);
112 if (!op
.isReg() || op
.getReg() != li
->reg
)
114 hasUse
|= mi
->getOperand(i
).isUse();
115 hasDef
|= mi
->getOperand(i
).isDef();
116 indices
.push_back(i
);
119 // Create a new vreg & interval for this instr.
120 unsigned newVReg
= mri
->createVirtualRegister(trc
);
122 vrm
->assignVirt2StackSlot(newVReg
, ss
);
123 LiveInterval
*newLI
= &lis
->getOrCreateInterval(newVReg
);
124 newLI
->weight
= HUGE_VALF
;
126 // Update the reg operands & kill flags.
127 for (unsigned i
= 0; i
< indices
.size(); ++i
) {
128 unsigned mopIdx
= indices
[i
];
129 MachineOperand
&mop
= mi
->getOperand(mopIdx
);
131 if (mop
.isUse() && !mi
->isRegTiedToDefOperand(mopIdx
)) {
135 assert(hasUse
|| hasDef
);
137 // Insert reload if necessary.
138 MachineBasicBlock::iterator
miItr(mi
);
140 tii
->loadRegFromStackSlot(*mi
->getParent(), miItr
, newVReg
, ss
, trc
,
142 MachineInstr
*loadInstr(prior(miItr
));
143 SlotIndex loadIndex
=
144 lis
->InsertMachineInstrInMaps(loadInstr
).getDefIndex();
145 vrm
->addSpillSlotUse(ss
, loadInstr
);
146 SlotIndex endIndex
= loadIndex
.getNextIndex();
148 newLI
->getNextValue(loadIndex
, 0, lis
->getVNInfoAllocator());
149 newLI
->addRange(LiveRange(loadIndex
, endIndex
, loadVNI
));
152 // Insert store if necessary.
154 tii
->storeRegToStackSlot(*mi
->getParent(), llvm::next(miItr
), newVReg
,
156 MachineInstr
*storeInstr(llvm::next(miItr
));
157 SlotIndex storeIndex
=
158 lis
->InsertMachineInstrInMaps(storeInstr
).getDefIndex();
159 vrm
->addSpillSlotUse(ss
, storeInstr
);
160 SlotIndex beginIndex
= storeIndex
.getPrevIndex();
162 newLI
->getNextValue(beginIndex
, 0, lis
->getVNInfoAllocator());
163 newLI
->addRange(LiveRange(beginIndex
, storeIndex
, storeVNI
));
166 newIntervals
.push_back(newLI
);
171 } // end anonymous namespace
175 /// Spills any live range using the spill-everywhere method with no attempt at
177 class TrivialSpiller
: public SpillerBase
{
180 TrivialSpiller(MachineFunctionPass
&pass
, MachineFunction
&mf
,
182 : SpillerBase(pass
, mf
, vrm
) {}
184 void spill(LiveRangeEdit
&LRE
) {
185 // Ignore spillIs - we don't use it.
186 trivialSpillEverywhere(&LRE
.getParent(), *LRE
.getNewVRegs());
190 } // end anonymous namespace
194 /// Falls back on LiveIntervals::addIntervalsForSpills.
195 class StandardSpiller
: public Spiller
{
200 MachineLoopInfo
*loopInfo
;
203 StandardSpiller(MachineFunctionPass
&pass
, MachineFunction
&mf
,
206 lis(&pass
.getAnalysis
<LiveIntervals
>()),
207 lss(&pass
.getAnalysis
<LiveStacks
>()),
208 loopInfo(pass
.getAnalysisIfAvailable
<MachineLoopInfo
>()),
211 /// Falls back on LiveIntervals::addIntervalsForSpills.
212 void spill(LiveRangeEdit
&LRE
) {
213 std::vector
<LiveInterval
*> added
=
214 lis
->addIntervalsForSpills(LRE
.getParent(), LRE
.getUselessVRegs(),
216 LRE
.getNewVRegs()->insert(LRE
.getNewVRegs()->end(),
217 added
.begin(), added
.end());
219 // Update LiveStacks.
220 int SS
= vrm
->getStackSlot(LRE
.getReg());
221 if (SS
== VirtRegMap::NO_STACK_SLOT
)
223 const TargetRegisterClass
*RC
= mf
->getRegInfo().getRegClass(LRE
.getReg());
224 LiveInterval
&SI
= lss
->getOrCreateInterval(SS
, RC
);
225 if (!SI
.hasAtLeastOneValue())
226 SI
.getNextValue(SlotIndex(), 0, lss
->getVNInfoAllocator());
227 SI
.MergeRangesInAsValue(LRE
.getParent(), SI
.getValNumInfo(0));
231 } // end anonymous namespace
233 llvm::Spiller
* llvm::createSpiller(MachineFunctionPass
&pass
,
236 switch (spillerOpt
) {
237 default: assert(0 && "unknown spiller");
238 case trivial
: return new TrivialSpiller(pass
, mf
, vrm
);
239 case standard
: return new StandardSpiller(pass
, mf
, vrm
);
240 case inline_
: return createInlineSpiller(pass
, mf
, vrm
);