1 //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
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 // The inline spiller modifies the machine function directly instead of
11 // inserting spills and restores in VirtRegMap.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "spiller"
17 #include "VirtRegMap.h"
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
30 class InlineSpiller : public Spiller {
34 MachineFrameInfo &mfi_;
35 MachineRegisterInfo &mri_;
36 const TargetInstrInfo &tii_;
37 const TargetRegisterInfo &tri_;
42 InlineSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
43 : mf_(*mf), lis_(*lis), vrm_(*vrm),
44 mfi_(*mf->getFrameInfo()),
45 mri_(mf->getRegInfo()),
46 tii_(*mf->getTarget().getInstrInfo()),
47 tri_(*mf->getTarget().getRegisterInfo()) {}
49 void spill(LiveInterval *li,
50 std::vector<LiveInterval*> &newIntervals,
51 SmallVectorImpl<LiveInterval*> &spillIs,
52 SlotIndex *earliestIndex);
57 Spiller *createInlineSpiller(MachineFunction *mf,
59 const MachineLoopInfo *mli,
61 return new InlineSpiller(mf, lis, vrm);
65 void InlineSpiller::spill(LiveInterval *li,
66 std::vector<LiveInterval*> &newIntervals,
67 SmallVectorImpl<LiveInterval*> &spillIs,
68 SlotIndex *earliestIndex) {
69 DEBUG(dbgs() << "Inline spilling " << *li << "\n");
70 assert(li->isSpillable() && "Attempting to spill already spilled value.");
71 assert(!li->isStackSlot() && "Trying to spill a stack slot.");
73 const TargetRegisterClass *RC = mri_.getRegClass(li->reg);
74 unsigned SS = vrm_.assignVirt2StackSlot(li->reg);
76 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(li->reg);
77 MachineInstr *MI = RI.skipInstruction();) {
78 SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
80 // Analyze instruction.
82 SmallVector<unsigned, 8> Ops;
83 tie(Reads, Writes) = MI->readsWritesVirtualRegister(li->reg, &Ops);
85 // Allocate interval around instruction.
86 // FIXME: Infer regclass from instruction alone.
87 unsigned NewVReg = mri_.createVirtualRegister(RC);
89 LiveInterval &NewLI = lis_.getOrCreateInterval(NewVReg);
90 NewLI.markNotSpillable();
92 // Reload if instruction reads register.
94 MachineBasicBlock::iterator MII = MI;
95 tii_.loadRegFromStackSlot(*MI->getParent(), MII, NewVReg, SS, RC, &tri_);
96 --MII; // Point to load instruction.
97 SlotIndex LoadIdx = lis_.InsertMachineInstrInMaps(MII).getDefIndex();
98 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MII);
99 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0, true,
100 lis_.getVNInfoAllocator());
101 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
104 // Rewrite instruction operands.
105 bool hasLiveDef = false;
106 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
107 MachineOperand &MO = MI->getOperand(Ops[i]);
110 if (!MI->isRegTiedToDefOperand(Ops[i]))
117 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI);
119 // Spill is instruction writes register.
120 // FIXME: Use a second vreg if instruction has no tied ops.
121 if (Writes && hasLiveDef) {
122 MachineBasicBlock::iterator MII = MI;
123 tii_.storeRegToStackSlot(*MI->getParent(), ++MII, NewVReg, true, SS, RC,
125 --MII; // Point to store instruction.
126 SlotIndex StoreIdx = lis_.InsertMachineInstrInMaps(MII).getDefIndex();
127 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MII);
128 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, true,
129 lis_.getVNInfoAllocator());
130 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
133 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
134 newIntervals.push_back(&NewLI);