Set an allocation hint when rematting before a COPY.
[oota-llvm.git] / lib / CodeGen / InlineSpiller.cpp
1 //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
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 // The inline spiller modifies the machine function directly instead of
11 // inserting spills and restores in VirtRegMap.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "regalloc"
16 #include "Spiller.h"
17 #include "LiveRangeEdit.h"
18 #include "VirtRegMap.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
21 #include "llvm/CodeGen/LiveStackAnalysis.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30
31 using namespace llvm;
32
33 static cl::opt<bool>
34 VerifySpills("verify-spills", cl::desc("Verify after each spill/split"));
35
36 namespace {
37 class InlineSpiller : public Spiller {
38   MachineFunctionPass &pass_;
39   MachineFunction &mf_;
40   LiveIntervals &lis_;
41   LiveStacks &lss_;
42   AliasAnalysis *aa_;
43   VirtRegMap &vrm_;
44   MachineFrameInfo &mfi_;
45   MachineRegisterInfo &mri_;
46   const TargetInstrInfo &tii_;
47   const TargetRegisterInfo &tri_;
48   const BitVector reserved_;
49
50   // Variables that are valid during spill(), but used by multiple methods.
51   LiveRangeEdit *edit_;
52   const TargetRegisterClass *rc_;
53   int stackSlot_;
54
55   // Values that failed to remat at some point.
56   SmallPtrSet<VNInfo*, 8> usedValues_;
57
58   ~InlineSpiller() {}
59
60 public:
61   InlineSpiller(MachineFunctionPass &pass,
62                 MachineFunction &mf,
63                 VirtRegMap &vrm)
64     : pass_(pass),
65       mf_(mf),
66       lis_(pass.getAnalysis<LiveIntervals>()),
67       lss_(pass.getAnalysis<LiveStacks>()),
68       aa_(&pass.getAnalysis<AliasAnalysis>()),
69       vrm_(vrm),
70       mfi_(*mf.getFrameInfo()),
71       mri_(mf.getRegInfo()),
72       tii_(*mf.getTarget().getInstrInfo()),
73       tri_(*mf.getTarget().getRegisterInfo()),
74       reserved_(tri_.getReservedRegs(mf_)) {}
75
76   void spill(LiveInterval *li,
77              SmallVectorImpl<LiveInterval*> &newIntervals,
78              const SmallVectorImpl<LiveInterval*> &spillIs);
79
80   void spill(LiveRangeEdit &);
81
82 private:
83   bool reMaterializeFor(MachineBasicBlock::iterator MI);
84   void reMaterializeAll();
85
86   bool coalesceStackAccess(MachineInstr *MI);
87   bool foldMemoryOperand(MachineBasicBlock::iterator MI,
88                          const SmallVectorImpl<unsigned> &Ops,
89                          MachineInstr *LoadMI = 0);
90   void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
91   void insertSpill(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
92 };
93 }
94
95 namespace llvm {
96 Spiller *createInlineSpiller(MachineFunctionPass &pass,
97                              MachineFunction &mf,
98                              VirtRegMap &vrm) {
99   if (VerifySpills)
100     mf.verify(&pass, "When creating inline spiller");
101   return new InlineSpiller(pass, mf, vrm);
102 }
103 }
104
105 /// reMaterializeFor - Attempt to rematerialize edit_->getReg() before MI instead of
106 /// reloading it.
107 bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
108   SlotIndex UseIdx = lis_.getInstructionIndex(MI).getUseIndex();
109   VNInfo *OrigVNI = edit_->getParent().getVNInfoAt(UseIdx);
110
111   if (!OrigVNI) {
112     DEBUG(dbgs() << "\tadding <undef> flags: ");
113     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
114       MachineOperand &MO = MI->getOperand(i);
115       if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg())
116         MO.setIsUndef();
117     }
118     DEBUG(dbgs() << UseIdx << '\t' << *MI);
119     return true;
120   }
121
122   LiveRangeEdit::Remat RM(OrigVNI);
123   if (!edit_->canRematerializeAt(RM, UseIdx, false, lis_)) {
124     usedValues_.insert(OrigVNI);
125     DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
126     return false;
127   }
128
129   // If the instruction also writes edit_->getReg(), it had better not require
130   // the same register for uses and defs.
131   bool Reads, Writes;
132   SmallVector<unsigned, 8> Ops;
133   tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit_->getReg(), &Ops);
134   if (Writes) {
135     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
136       MachineOperand &MO = MI->getOperand(Ops[i]);
137       if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
138         usedValues_.insert(OrigVNI);
139         DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
140         return false;
141       }
142     }
143   }
144
145   // Before rematerializing into a register for a single instruction, try to
146   // fold a load into the instruction. That avoids allocating a new register.
147   if (RM.OrigMI->getDesc().canFoldAsLoad() &&
148       foldMemoryOperand(MI, Ops, RM.OrigMI)) {
149     edit_->markRematerialized(RM.ParentVNI);
150     return true;
151   }
152
153   // Alocate a new register for the remat.
154   LiveInterval &NewLI = edit_->create(mri_, lis_, vrm_);
155   NewLI.markNotSpillable();
156
157   // Rematting for a copy: Set allocation hint to be the destination register.
158   if (MI->isCopy())
159     mri_.setRegAllocationHint(NewLI.reg, 0, MI->getOperand(0).getReg());
160
161   // Finally we can rematerialize OrigMI before MI.
162   SlotIndex DefIdx = edit_->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
163                                             lis_, tii_, tri_);
164   DEBUG(dbgs() << "\tremat:  " << DefIdx << '\t'
165                << *lis_.getInstructionFromIndex(DefIdx));
166
167   // Replace operands
168   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
169     MachineOperand &MO = MI->getOperand(Ops[i]);
170     if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg()) {
171       MO.setReg(NewLI.reg);
172       MO.setIsKill();
173     }
174   }
175   DEBUG(dbgs() << "\t        " << UseIdx << '\t' << *MI);
176
177   VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, lis_.getVNInfoAllocator());
178   NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
179   DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
180   return true;
181 }
182
183 /// reMaterializeAll - Try to rematerialize as many uses as possible,
184 /// and trim the live ranges after.
185 void InlineSpiller::reMaterializeAll() {
186   // Do a quick scan of the interval values to find if any are remattable.
187   if (!edit_->anyRematerializable(lis_, tii_, aa_))
188     return;
189
190   usedValues_.clear();
191
192   // Try to remat before all uses of edit_->getReg().
193   bool anyRemat = false;
194   for (MachineRegisterInfo::use_nodbg_iterator
195        RI = mri_.use_nodbg_begin(edit_->getReg());
196        MachineInstr *MI = RI.skipInstruction();)
197      anyRemat |= reMaterializeFor(MI);
198
199   if (!anyRemat)
200     return;
201
202   // Remove any values that were completely rematted.
203   bool anyRemoved = false;
204   for (LiveInterval::vni_iterator I = edit_->getParent().vni_begin(),
205        E = edit_->getParent().vni_end(); I != E; ++I) {
206     VNInfo *VNI = *I;
207     if (VNI->hasPHIKill() || !edit_->didRematerialize(VNI) ||
208         usedValues_.count(VNI))
209       continue;
210     MachineInstr *DefMI = lis_.getInstructionFromIndex(VNI->def);
211     DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
212     lis_.RemoveMachineInstrFromMaps(DefMI);
213     vrm_.RemoveMachineInstrFromMaps(DefMI);
214     DefMI->eraseFromParent();
215     VNI->def = SlotIndex();
216     anyRemoved = true;
217   }
218
219   if (!anyRemoved)
220     return;
221
222   // Removing values may cause debug uses where parent is not live.
223   for (MachineRegisterInfo::use_iterator RI = mri_.use_begin(edit_->getReg());
224        MachineInstr *MI = RI.skipInstruction();) {
225     if (!MI->isDebugValue())
226       continue;
227     // Try to preserve the debug value if parent is live immediately after it.
228     MachineBasicBlock::iterator NextMI = MI;
229     ++NextMI;
230     if (NextMI != MI->getParent()->end() && !lis_.isNotInMIMap(NextMI)) {
231       SlotIndex Idx = lis_.getInstructionIndex(NextMI);
232       VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx);
233       if (VNI && (VNI->hasPHIKill() || usedValues_.count(VNI)))
234         continue;
235     }
236     DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
237     MI->eraseFromParent();
238   }
239 }
240
241 /// If MI is a load or store of stackSlot_, it can be removed.
242 bool InlineSpiller::coalesceStackAccess(MachineInstr *MI) {
243   int FI = 0;
244   unsigned reg;
245   if (!(reg = tii_.isLoadFromStackSlot(MI, FI)) &&
246       !(reg = tii_.isStoreToStackSlot(MI, FI)))
247     return false;
248
249   // We have a stack access. Is it the right register and slot?
250   if (reg != edit_->getReg() || FI != stackSlot_)
251     return false;
252
253   DEBUG(dbgs() << "Coalescing stack access: " << *MI);
254   lis_.RemoveMachineInstrFromMaps(MI);
255   MI->eraseFromParent();
256   return true;
257 }
258
259 /// foldMemoryOperand - Try folding stack slot references in Ops into MI.
260 /// @param MI     Instruction using or defining the current register.
261 /// @param Ops    Operand indices from readsWritesVirtualRegister().
262 /// @param LoadMI Load instruction to use instead of stack slot when non-null.
263 /// @return       True on success, and MI will be erased.
264 bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
265                                       const SmallVectorImpl<unsigned> &Ops,
266                                       MachineInstr *LoadMI) {
267   // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
268   // operands.
269   SmallVector<unsigned, 8> FoldOps;
270   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
271     unsigned Idx = Ops[i];
272     MachineOperand &MO = MI->getOperand(Idx);
273     if (MO.isImplicit())
274       continue;
275     // FIXME: Teach targets to deal with subregs.
276     if (MO.getSubReg())
277       return false;
278     // We cannot fold a load instruction into a def.
279     if (LoadMI && MO.isDef())
280       return false;
281     // Tied use operands should not be passed to foldMemoryOperand.
282     if (!MI->isRegTiedToDefOperand(Idx))
283       FoldOps.push_back(Idx);
284   }
285
286   MachineInstr *FoldMI =
287                 LoadMI ? tii_.foldMemoryOperand(MI, FoldOps, LoadMI)
288                        : tii_.foldMemoryOperand(MI, FoldOps, stackSlot_);
289   if (!FoldMI)
290     return false;
291   lis_.ReplaceMachineInstrInMaps(MI, FoldMI);
292   if (!LoadMI)
293     vrm_.addSpillSlotUse(stackSlot_, FoldMI);
294   MI->eraseFromParent();
295   DEBUG(dbgs() << "\tfolded: " << *FoldMI);
296   return true;
297 }
298
299 /// insertReload - Insert a reload of NewLI.reg before MI.
300 void InlineSpiller::insertReload(LiveInterval &NewLI,
301                                  MachineBasicBlock::iterator MI) {
302   MachineBasicBlock &MBB = *MI->getParent();
303   SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
304   tii_.loadRegFromStackSlot(MBB, MI, NewLI.reg, stackSlot_, rc_, &tri_);
305   --MI; // Point to load instruction.
306   SlotIndex LoadIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
307   vrm_.addSpillSlotUse(stackSlot_, MI);
308   DEBUG(dbgs() << "\treload:  " << LoadIdx << '\t' << *MI);
309   VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
310                                        lis_.getVNInfoAllocator());
311   NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
312 }
313
314 /// insertSpill - Insert a spill of NewLI.reg after MI.
315 void InlineSpiller::insertSpill(LiveInterval &NewLI,
316                                 MachineBasicBlock::iterator MI) {
317   MachineBasicBlock &MBB = *MI->getParent();
318
319   // Get the defined value. It could be an early clobber so keep the def index.
320   SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
321   VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx);
322   assert(VNI && VNI->def.getDefIndex() == Idx && "Inconsistent VNInfo");
323   Idx = VNI->def;
324
325   tii_.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, stackSlot_, rc_, &tri_);
326   --MI; // Point to store instruction.
327   SlotIndex StoreIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
328   vrm_.addSpillSlotUse(stackSlot_, MI);
329   DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
330   VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, lis_.getVNInfoAllocator());
331   NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
332 }
333
334 void InlineSpiller::spill(LiveInterval *li,
335                           SmallVectorImpl<LiveInterval*> &newIntervals,
336                           const SmallVectorImpl<LiveInterval*> &spillIs) {
337   LiveRangeEdit edit(*li, newIntervals, spillIs);
338   spill(edit);
339   if (VerifySpills)
340     mf_.verify(&pass_, "After inline spill");
341 }
342
343 void InlineSpiller::spill(LiveRangeEdit &edit) {
344   edit_ = &edit;
345   assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
346          && "Trying to spill a stack slot.");
347   DEBUG(dbgs() << "Inline spilling "
348                << mri_.getRegClass(edit.getReg())->getName()
349                << ':' << edit.getParent() << "\n");
350   assert(edit.getParent().isSpillable() &&
351          "Attempting to spill already spilled value.");
352
353   reMaterializeAll();
354
355   // Remat may handle everything.
356   if (edit_->getParent().empty())
357     return;
358
359   rc_ = mri_.getRegClass(edit.getReg());
360   stackSlot_ = vrm_.assignVirt2StackSlot(edit_->getReg());
361
362   // Update LiveStacks now that we are committed to spilling.
363   LiveInterval &stacklvr = lss_.getOrCreateInterval(stackSlot_, rc_);
364   assert(stacklvr.empty() && "Just created stack slot not empty");
365   stacklvr.getNextValue(SlotIndex(), 0, lss_.getVNInfoAllocator());
366   stacklvr.MergeRangesInAsValue(edit_->getParent(), stacklvr.getValNumInfo(0));
367
368   // Iterate over instructions using register.
369   for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(edit.getReg());
370        MachineInstr *MI = RI.skipInstruction();) {
371
372     // Debug values are not allowed to affect codegen.
373     if (MI->isDebugValue()) {
374       // Modify DBG_VALUE now that the value is in a spill slot.
375       uint64_t Offset = MI->getOperand(1).getImm();
376       const MDNode *MDPtr = MI->getOperand(2).getMetadata();
377       DebugLoc DL = MI->getDebugLoc();
378       if (MachineInstr *NewDV = tii_.emitFrameIndexDebugValue(mf_, stackSlot_,
379                                                            Offset, MDPtr, DL)) {
380         DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
381         MachineBasicBlock *MBB = MI->getParent();
382         MBB->insert(MBB->erase(MI), NewDV);
383       } else {
384         DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
385         MI->eraseFromParent();
386       }
387       continue;
388     }
389
390     // Stack slot accesses may coalesce away.
391     if (coalesceStackAccess(MI))
392       continue;
393
394     // Analyze instruction.
395     bool Reads, Writes;
396     SmallVector<unsigned, 8> Ops;
397     tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit.getReg(), &Ops);
398
399     // Attempt to fold memory ops.
400     if (foldMemoryOperand(MI, Ops))
401       continue;
402
403     // Allocate interval around instruction.
404     // FIXME: Infer regclass from instruction alone.
405     LiveInterval &NewLI = edit.create(mri_, lis_, vrm_);
406     NewLI.markNotSpillable();
407
408     if (Reads)
409       insertReload(NewLI, MI);
410
411     // Rewrite instruction operands.
412     bool hasLiveDef = false;
413     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
414       MachineOperand &MO = MI->getOperand(Ops[i]);
415       MO.setReg(NewLI.reg);
416       if (MO.isUse()) {
417         if (!MI->isRegTiedToDefOperand(Ops[i]))
418           MO.setIsKill();
419       } else {
420         if (!MO.isDead())
421           hasLiveDef = true;
422       }
423     }
424
425     // FIXME: Use a second vreg if instruction has no tied ops.
426     if (Writes && hasLiveDef)
427       insertSpill(NewLI, MI);
428
429     DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
430   }
431 }