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 "llvm/CodeGen/LiveIntervalAnalysis.h"
14 #include "llvm/CodeGen/LiveRangeEdit.h"
15 #include "llvm/CodeGen/LiveStackAnalysis.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineLoopInfo.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/VirtRegMap.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
32 enum SpillerName { trivial, inline_ };
35 static cl::opt<SpillerName>
37 cl::desc("Spiller to use: (default: standard)"),
39 cl::values(clEnumVal(trivial, "trivial spiller"),
40 clEnumValN(inline_, "inline", "inline spiller"),
44 // Spiller virtual destructor implementation.
45 Spiller::~Spiller() {}
49 /// Utility class for spillers.
50 class SpillerBase : public Spiller {
52 MachineFunctionPass *pass;
56 MachineFrameInfo *mfi;
57 MachineRegisterInfo *mri;
58 const TargetInstrInfo *tii;
59 const TargetRegisterInfo *tri;
61 /// Construct a spiller base.
62 SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
63 : pass(&pass), mf(&mf), vrm(&vrm)
65 lis = &pass.getAnalysis<LiveIntervals>();
66 mfi = mf.getFrameInfo();
67 mri = &mf.getRegInfo();
68 tii = mf.getTarget().getInstrInfo();
69 tri = mf.getTarget().getRegisterInfo();
72 /// Add spill ranges for every use/def of the live interval, inserting loads
73 /// immediately before each use, and stores after each def. No folding or
74 /// remat is attempted.
75 void trivialSpillEverywhere(LiveRangeEdit& LRE) {
76 LiveInterval* li = &LRE.getParent();
78 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
80 assert(li->weight != llvm::huge_valf &&
81 "Attempting to spill already spilled value.");
83 assert(!TargetRegisterInfo::isStackSlot(li->reg) &&
84 "Trying to spill a stack slot.");
86 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
88 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
89 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
91 // Iterate over reg uses/defs.
92 for (MachineRegisterInfo::reg_iterator
93 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
95 // Grab the use/def instr.
96 MachineInstr *mi = &*regItr;
98 DEBUG(dbgs() << " Processing " << *mi);
100 // Step regItr to the next use/def instr.
103 } while (regItr != mri->reg_end() && (&*regItr == mi));
105 // Collect uses & defs for this instr.
106 SmallVector<unsigned, 2> indices;
109 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
110 MachineOperand &op = mi->getOperand(i);
111 if (!op.isReg() || op.getReg() != li->reg)
113 hasUse |= mi->getOperand(i).isUse();
114 hasDef |= mi->getOperand(i).isDef();
115 indices.push_back(i);
118 // Create a new virtual register for the load and/or store.
119 unsigned NewVReg = LRE.create();
121 // Update the reg operands & kill flags.
122 for (unsigned i = 0; i < indices.size(); ++i) {
123 unsigned mopIdx = indices[i];
124 MachineOperand &mop = mi->getOperand(mopIdx);
126 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
130 assert(hasUse || hasDef);
132 // Insert reload if necessary.
133 MachineBasicBlock::iterator miItr(mi);
135 MachineInstrSpan MIS(miItr);
137 tii->loadRegFromStackSlot(*mi->getParent(), miItr, NewVReg, ss, trc,
139 lis->InsertMachineInstrRangeInMaps(MIS.begin(), miItr);
142 // Insert store if necessary.
144 MachineInstrSpan MIS(miItr);
146 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), NewVReg,
148 lis->InsertMachineInstrRangeInMaps(llvm::next(miItr), MIS.end());
154 } // end anonymous namespace
158 /// Spills any live range using the spill-everywhere method with no attempt at
160 class TrivialSpiller : public SpillerBase {
163 TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
165 : SpillerBase(pass, mf, vrm) {}
167 void spill(LiveRangeEdit &LRE) {
168 // Ignore spillIs - we don't use it.
169 trivialSpillEverywhere(LRE);
173 } // end anonymous namespace
175 void Spiller::anchor() { }
177 llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
180 switch (spillerOpt) {
181 case trivial: return new TrivialSpiller(pass, mf, vrm);
182 case inline_: return createInlineSpiller(pass, mf, vrm);
184 llvm_unreachable("Invalid spiller optimization");