Add an assertion, update DefInst even though no one uses it (dangling pointers
[oota-llvm.git] / lib / CodeGen / VirtRegMap.cpp
1 //===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the VirtRegMap class.
11 //
12 // It also contains implementations of the the Spiller interface, which, given a
13 // virtual register map and a machine function, eliminates all virtual
14 // references by replacing them with physical register references - adding spill
15 // code as necessary.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #define DEBUG_TYPE "spiller"
20 #include "VirtRegMap.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/SSARegMap.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/ADT/Statistic.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include <algorithm>
32 using namespace llvm;
33
34 namespace {
35   Statistic<> NumSpills("spiller", "Number of register spills");
36   Statistic<> NumStores("spiller", "Number of stores added");
37   Statistic<> NumLoads ("spiller", "Number of loads added");
38   Statistic<> NumReused("spiller", "Number of values reused");
39   Statistic<> NumDSE   ("spiller", "Number of dead stores elided");
40
41   enum SpillerName { simple, local };
42
43   cl::opt<SpillerName>
44   SpillerOpt("spiller",
45              cl::desc("Spiller to use: (default: local)"),
46              cl::Prefix,
47              cl::values(clEnumVal(simple, "  simple spiller"),
48                         clEnumVal(local,  "  local spiller"),
49                         clEnumValEnd),
50              cl::init(local));
51 }
52
53 //===----------------------------------------------------------------------===//
54 //  VirtRegMap implementation
55 //===----------------------------------------------------------------------===//
56
57 void VirtRegMap::grow() {
58   Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
59   Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
60 }
61
62 int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
63   assert(MRegisterInfo::isVirtualRegister(virtReg));
64   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
65          "attempt to assign stack slot to already spilled register");
66   const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
67   int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
68                                                         RC->getAlignment());
69   Virt2StackSlotMap[virtReg] = frameIndex;
70   ++NumSpills;
71   return frameIndex;
72 }
73
74 void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
75   assert(MRegisterInfo::isVirtualRegister(virtReg));
76   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
77          "attempt to assign stack slot to already spilled register");
78   Virt2StackSlotMap[virtReg] = frameIndex;
79 }
80
81 void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
82                             unsigned OpNo, MachineInstr *NewMI) {
83   // Move previous memory references folded to new instruction.
84   MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
85   for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
86          E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
87     MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
88     MI2VirtMap.erase(I++);
89   }
90
91   ModRef MRInfo;
92   if (!OldMI->getOperand(OpNo).isDef()) {
93     assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
94     MRInfo = isRef;
95   } else {
96     MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
97   }
98
99   // add new memory reference
100   MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
101 }
102
103 void VirtRegMap::print(std::ostream &OS) const {
104   const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
105
106   OS << "********** REGISTER MAP **********\n";
107   for (unsigned i = MRegisterInfo::FirstVirtualRegister,
108          e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
109     if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
110       OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
111
112   }
113
114   for (unsigned i = MRegisterInfo::FirstVirtualRegister,
115          e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
116     if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
117       OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
118   OS << '\n';
119 }
120
121 void VirtRegMap::dump() const { print(std::cerr); }
122
123
124 //===----------------------------------------------------------------------===//
125 // Simple Spiller Implementation
126 //===----------------------------------------------------------------------===//
127
128 Spiller::~Spiller() {}
129
130 namespace {
131   struct SimpleSpiller : public Spiller {
132     bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
133   };
134 }
135
136 bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
137                                          const VirtRegMap &VRM) {
138   DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
139   DEBUG(std::cerr << "********** Function: "
140                   << MF.getFunction()->getName() << '\n');
141   const TargetMachine &TM = MF.getTarget();
142   const MRegisterInfo &MRI = *TM.getRegisterInfo();
143   bool *PhysRegsUsed = MF.getUsedPhysregs();
144
145   // LoadedRegs - Keep track of which vregs are loaded, so that we only load
146   // each vreg once (in the case where a spilled vreg is used by multiple
147   // operands).  This is always smaller than the number of operands to the
148   // current machine instr, so it should be small.
149   std::vector<unsigned> LoadedRegs;
150
151   for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
152        MBBI != E; ++MBBI) {
153     DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
154     MachineBasicBlock &MBB = *MBBI;
155     for (MachineBasicBlock::iterator MII = MBB.begin(),
156            E = MBB.end(); MII != E; ++MII) {
157       MachineInstr &MI = *MII;
158       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
159         MachineOperand &MO = MI.getOperand(i);
160         if (MO.isRegister() && MO.getReg())
161           if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
162             unsigned VirtReg = MO.getReg();
163             unsigned PhysReg = VRM.getPhys(VirtReg);
164             if (VRM.hasStackSlot(VirtReg)) {
165               int StackSlot = VRM.getStackSlot(VirtReg);
166               const TargetRegisterClass* RC =
167                 MF.getSSARegMap()->getRegClass(VirtReg);
168
169               if (MO.isUse() &&
170                   std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
171                   == LoadedRegs.end()) {
172                 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
173                 LoadedRegs.push_back(VirtReg);
174                 ++NumLoads;
175                 DEBUG(std::cerr << '\t' << *prior(MII));
176               }
177
178               if (MO.isDef()) {
179                 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
180                 ++NumStores;
181               }
182             }
183             PhysRegsUsed[PhysReg] = true;
184             MI.SetMachineOperandReg(i, PhysReg);
185           } else {
186             PhysRegsUsed[MO.getReg()] = true;
187           }
188       }
189
190       DEBUG(std::cerr << '\t' << MI);
191       LoadedRegs.clear();
192     }
193   }
194   return true;
195 }
196
197 //===----------------------------------------------------------------------===//
198 //  Local Spiller Implementation
199 //===----------------------------------------------------------------------===//
200
201 namespace {
202   /// LocalSpiller - This spiller does a simple pass over the machine basic
203   /// block to attempt to keep spills in registers as much as possible for
204   /// blocks that have low register pressure (the vreg may be spilled due to
205   /// register pressure in other blocks).
206   class LocalSpiller : public Spiller {
207     const MRegisterInfo *MRI;
208     const TargetInstrInfo *TII;
209   public:
210     bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
211       MRI = MF.getTarget().getRegisterInfo();
212       TII = MF.getTarget().getInstrInfo();
213       DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
214                       << MF.getFunction()->getName() << "':\n");
215
216       for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
217            MBB != E; ++MBB)
218         RewriteMBB(*MBB, VRM);
219       return true;
220     }
221   private:
222     void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
223     void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
224                         std::map<unsigned, int> &PhysRegs);
225     void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
226                             std::map<unsigned, int> &PhysRegs);
227   };
228 }
229
230 void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg,
231                                       std::map<int, unsigned> &SpillSlots,
232                                       std::map<unsigned, int> &PhysRegs) {
233   std::map<unsigned, int>::iterator I = PhysRegs.find(PhysReg);
234   if (I != PhysRegs.end()) {
235     int Slot = I->second;
236     PhysRegs.erase(I);
237     assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!");
238     SpillSlots.erase(Slot);
239     DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
240           << " clobbered, invalidating SS#" << Slot << "\n");
241
242   }
243 }
244
245 void LocalSpiller::ClobberPhysReg(unsigned PhysReg,
246                                   std::map<int, unsigned> &SpillSlots,
247                                   std::map<unsigned, int> &PhysRegs) {
248   for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
249     ClobberPhysRegOnly(*AS, SpillSlots, PhysRegs);
250   ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegs);
251 }
252
253
254 // ReusedOp - For each reused operand, we keep track of a bit of information, in
255 // case we need to rollback upon processing a new operand.  See comments below.
256 namespace {
257   struct ReusedOp {
258     // The MachineInstr operand that reused an available value.
259     unsigned Operand;
260
261     // StackSlot - The spill slot of the value being reused.
262     unsigned StackSlot;
263
264     // PhysRegReused - The physical register the value was available in.
265     unsigned PhysRegReused;
266
267     // AssignedPhysReg - The physreg that was assigned for use by the reload.
268     unsigned AssignedPhysReg;
269     
270     // VirtReg - The virtual register itself.
271     unsigned VirtReg;
272
273     ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
274              unsigned vreg)
275       : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
276       VirtReg(vreg) {}
277   };
278 }
279
280
281 /// rewriteMBB - Keep track of which spills are available even after the
282 /// register allocator is done with them.  If possible, avoid reloading vregs.
283 void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
284
285   // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
286   // register values that are still available, due to being loaded to stored to,
287   // but not invalidated yet.
288   std::map<int, unsigned> SpillSlotsAvailable;
289
290   // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
291   // which physregs are in use holding a stack slot value.
292   std::map<unsigned, int> PhysRegsAvailable;
293
294   DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
295
296   std::vector<ReusedOp> ReusedOperands;
297
298   // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
299   // of it.  ".first" is the machine operand index (should always be 0 for now),
300   // and ".second" is the virtual register that is spilled.
301   std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
302
303   // MaybeDeadStores - When we need to write a value back into a stack slot,
304   // keep track of the inserted store.  If the stack slot value is never read
305   // (because the value was used from some available register, for example), and
306   // subsequently stored to, the original store is dead.  This map keeps track
307   // of inserted stores that are not used.  If we see a subsequent store to the
308   // same stack slot, the original store is deleted.
309   std::map<int, MachineInstr*> MaybeDeadStores;
310
311   bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
312
313   for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
314        MII != E; ) {
315     MachineInstr &MI = *MII;
316     MachineBasicBlock::iterator NextMII = MII; ++NextMII;
317
318     ReusedOperands.clear();
319     DefAndUseVReg.clear();
320
321     // Process all of the spilled uses and all non spilled reg references.
322     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
323       MachineOperand &MO = MI.getOperand(i);
324       if (!MO.isRegister() || MO.getReg() == 0)
325         continue;   // Ignore non-register operands.
326       
327       if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
328         // Ignore physregs for spilling, but remember that it is used by this
329         // function.
330         PhysRegsUsed[MO.getReg()] = true;
331         continue;
332       }
333       
334       assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
335              "Not a virtual or a physical register?");
336       
337       unsigned VirtReg = MO.getReg();
338       if (!VRM.hasStackSlot(VirtReg)) {
339         // This virtual register was assigned a physreg!
340         unsigned Phys = VRM.getPhys(VirtReg);
341         PhysRegsUsed[Phys] = true;
342         MI.SetMachineOperandReg(i, Phys);
343         continue;
344       }
345       
346       // This virtual register is now known to be a spilled value.
347       if (!MO.isUse())
348         continue;  // Handle defs in the loop below (handle use&def here though)
349
350       // If this is both a def and a use, we need to emit a store to the
351       // stack slot after the instruction.  Keep track of D&U operands
352       // because we are about to change it to a physreg here.
353       if (MO.isDef()) {
354         // Remember that this was a def-and-use operand, and that the
355         // stack slot is live after this instruction executes.
356         DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
357       }
358       
359       int StackSlot = VRM.getStackSlot(VirtReg);
360       unsigned PhysReg;
361
362       // Check to see if this stack slot is available.
363       std::map<int, unsigned>::iterator SSI =
364         SpillSlotsAvailable.find(StackSlot);
365       if (SSI != SpillSlotsAvailable.end()) {
366         DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
367                         << MRI->getName(SSI->second) << " for vreg"
368                         << VirtReg <<" instead of reloading into physreg "
369                         << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
370         // If this stack slot value is already available, reuse it!
371         PhysReg = SSI->second;
372         MI.SetMachineOperandReg(i, PhysReg);
373
374         // The only technical detail we have is that we don't know that
375         // PhysReg won't be clobbered by a reloaded stack slot that occurs
376         // later in the instruction.  In particular, consider 'op V1, V2'.
377         // If V1 is available in physreg R0, we would choose to reuse it
378         // here, instead of reloading it into the register the allocator
379         // indicated (say R1).  However, V2 might have to be reloaded
380         // later, and it might indicate that it needs to live in R0.  When
381         // this occurs, we need to have information available that
382         // indicates it is safe to use R1 for the reload instead of R0.
383         //
384         // To further complicate matters, we might conflict with an alias,
385         // or R0 and R1 might not be compatible with each other.  In this
386         // case, we actually insert a reload for V1 in R1, ensuring that
387         // we can get at R0 or its alias.
388         ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg,
389                                           VRM.getPhys(VirtReg), VirtReg));
390         ++NumReused;
391         continue;
392       }
393       
394       // Otherwise, reload it and remember that we have it.
395       PhysReg = VRM.getPhys(VirtReg);
396       assert(PhysReg && "Must map virtreg to physreg!");
397       const TargetRegisterClass* RC =
398         MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
399
400     RecheckRegister:
401       // Note that, if we reused a register for a previous operand, the
402       // register we want to reload into might not actually be
403       // available.  If this occurs, use the register indicated by the
404       // reuser.
405       if (!ReusedOperands.empty())   // This is most often empty.
406         for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro)
407           if (ReusedOperands[ro].PhysRegReused == PhysReg) {
408             // Yup, use the reload register that we didn't use before.
409             PhysReg = ReusedOperands[ro].AssignedPhysReg;
410             goto RecheckRegister;
411           } else {
412             ReusedOp &Op = ReusedOperands[ro];
413             unsigned PRRU = Op.PhysRegReused;
414             if (MRI->areAliases(PRRU, PhysReg)) {
415               // Okay, we found out that an alias of a reused register
416               // was used.  This isn't good because it means we have
417               // to undo a previous reuse.
418               const TargetRegisterClass *AliasRC =
419                 MBB.getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
420               MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg,
421                                         Op.StackSlot, AliasRC);
422               ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
423                              PhysRegsAvailable);
424
425               // Any stores to this stack slot are not dead anymore.
426               MaybeDeadStores.erase(Op.StackSlot);
427
428               MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
429               PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot;
430               SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
431               PhysRegsAvailable.erase(Op.PhysRegReused);
432               DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot
433                     << " in physreg "
434                     << MRI->getName(Op.AssignedPhysReg) << "\n");
435               ++NumLoads;
436               DEBUG(std::cerr << '\t' << *prior(MII));
437
438               DEBUG(std::cerr << "Reuse undone!\n");
439               ReusedOperands.erase(ReusedOperands.begin()+ro);
440               --NumReused;
441               goto ContinueReload;
442             }
443           }
444     ContinueReload:
445       PhysRegsUsed[PhysReg] = true;
446       MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
447       // This invalidates PhysReg.
448       ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
449
450       // Any stores to this stack slot are not dead anymore.
451       MaybeDeadStores.erase(StackSlot);
452
453       MI.SetMachineOperandReg(i, PhysReg);
454       PhysRegsAvailable[PhysReg] = StackSlot;
455       SpillSlotsAvailable[StackSlot] = PhysReg;
456       DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg "
457                       << MRI->getName(PhysReg) << "\n");
458       ++NumLoads;
459       DEBUG(std::cerr << '\t' << *prior(MII));
460     }
461
462     // Loop over all of the implicit defs, clearing them from our available
463     // sets.
464     for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
465          *ImpDef; ++ImpDef) {
466       PhysRegsUsed[*ImpDef] = true;
467       ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
468     }
469
470     DEBUG(std::cerr << '\t' << MI);
471
472     // If we have folded references to memory operands, make sure we clear all
473     // physical registers that may contain the value of the spilled virtual
474     // register
475     VirtRegMap::MI2VirtMapTy::const_iterator I, End;
476     for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
477       DEBUG(std::cerr << "Folded vreg: " << I->second.first << "  MR: "
478                       << I->second.second);
479       unsigned VirtReg = I->second.first;
480       VirtRegMap::ModRef MR = I->second.second;
481       if (!VRM.hasStackSlot(VirtReg)) {
482         DEBUG(std::cerr << ": No stack slot!\n");
483         continue;
484       }
485       int SS = VRM.getStackSlot(VirtReg);
486       DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
487       
488       // If this folded instruction is just a use, check to see if it's a
489       // straight load from the virt reg slot.
490       if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
491         int FrameIdx;
492         if (unsigned DestReg = MRI->isLoadFromStackSlot(&MI, FrameIdx)) {
493           // If this spill slot is available, insert a copy for it!
494           std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
495           if (FrameIdx == SS && It != SpillSlotsAvailable.end()) {
496             DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
497             MachineFunction &MF = *MBB.getParent();
498             if (DestReg != It->second) {
499               MRI->copyRegToReg(MBB, &MI, DestReg, It->second,
500                                 MF.getSSARegMap()->getRegClass(VirtReg));
501               // Revisit the copy so we make sure to notice the effects of the
502               // operation on the destreg (either needing to RA it if it's 
503               // virtual or needing to clobber any values if it's physical).
504               NextMII = &MI;
505               --NextMII;  // backtrack to the copy.
506             }
507             MBB.erase(&MI);
508             goto ProcessNextInst;
509           }
510         }
511       }
512
513       // If this reference is not a use, any previous store is now dead.
514       // Otherwise, the store to this stack slot is not dead anymore.
515       std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
516       if (MDSI != MaybeDeadStores.end()) {
517         if (MR & VirtRegMap::isRef)   // Previous store is not dead.
518           MaybeDeadStores.erase(MDSI);
519         else {
520           // If we get here, the store is dead, nuke it now.
521           assert(MR == VirtRegMap::isMod && "Can't be modref!");
522           MBB.erase(MDSI->second);
523           MaybeDeadStores.erase(MDSI);
524           ++NumDSE;
525         }
526       }
527
528       // If the spill slot value is available, and this is a new definition of
529       // the value, the value is not available anymore.
530       if (MR & VirtRegMap::isMod) {
531         std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
532         if (It != SpillSlotsAvailable.end()) {
533           PhysRegsAvailable.erase(It->second);
534           SpillSlotsAvailable.erase(It);
535         }
536       }
537     }
538
539     // Process all of the spilled defs.
540     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
541       MachineOperand &MO = MI.getOperand(i);
542       if (MO.isRegister() && MO.getReg() && MO.isDef()) {
543         unsigned VirtReg = MO.getReg();
544
545         bool TakenCareOf = false;
546         if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
547           // Check to see if this is a def-and-use vreg operand that we do need
548           // to insert a store for.
549           bool OpTakenCareOf = false;
550           if (MO.isUse() && !DefAndUseVReg.empty()) {
551             for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
552               if (DefAndUseVReg[dau].first == i) {
553                 VirtReg = DefAndUseVReg[dau].second;
554                 OpTakenCareOf = true;
555                 break;
556               }
557           }
558
559           if (!OpTakenCareOf) {
560             ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable);
561             TakenCareOf = true;
562           }
563         }
564
565         if (!TakenCareOf) {
566           // The only vregs left are stack slot definitions.
567           int StackSlot    = VRM.getStackSlot(VirtReg);
568           const TargetRegisterClass *RC =
569             MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
570           unsigned PhysReg;
571
572           // If this is a def&use operand, and we used a different physreg for
573           // it than the one assigned, make sure to execute the store from the
574           // correct physical register.
575           if (MO.getReg() == VirtReg)
576             PhysReg = VRM.getPhys(VirtReg);
577           else
578             PhysReg = MO.getReg();
579
580           PhysRegsUsed[PhysReg] = true;
581           MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
582           DEBUG(std::cerr << "Store:\t" << *next(MII));
583           MI.SetMachineOperandReg(i, PhysReg);
584
585           // If there is a dead store to this stack slot, nuke it now.
586           MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
587           if (LastStore) {
588             DEBUG(std::cerr << " Killed store:\t" << *LastStore);
589             ++NumDSE;
590             MBB.erase(LastStore);
591           }
592           LastStore = next(MII);
593
594           // If the stack slot value was previously available in some other
595           // register, change it now.  Otherwise, make the register available,
596           // in PhysReg.
597           std::map<int, unsigned>::iterator SSA =
598             SpillSlotsAvailable.find(StackSlot);
599           if (SSA != SpillSlotsAvailable.end()) {
600             // Remove the record for physreg.
601             PhysRegsAvailable.erase(SSA->second);
602             SpillSlotsAvailable.erase(SSA);
603           }
604           ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
605
606           PhysRegsAvailable[PhysReg] = StackSlot;
607           SpillSlotsAvailable[StackSlot] = PhysReg;
608           DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
609                           << MRI->getName(PhysReg) << " for virtreg #"
610                           << VirtReg << "\n");
611
612           ++NumStores;
613           VirtReg = PhysReg;
614         }
615       }
616     }
617   ProcessNextInst:
618     MII = NextMII;
619   }
620 }
621
622
623
624 llvm::Spiller* llvm::createSpiller() {
625   switch (SpillerOpt) {
626   default: assert(0 && "Unreachable!");
627   case local:
628     return new LocalSpiller();
629   case simple:
630     return new SimpleSpiller();
631   }
632 }