First cut trivial re-materialization support.
[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/Support/Compiler.h"
30 #include "llvm/ADT/BitVector.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallSet.h"
34 #include <algorithm>
35 using namespace llvm;
36
37 STATISTIC(NumSpills, "Number of register spills");
38 STATISTIC(NumReMats, "Number of re-materialization");
39 STATISTIC(NumStores, "Number of stores added");
40 STATISTIC(NumLoads , "Number of loads added");
41 STATISTIC(NumReused, "Number of values reused");
42 STATISTIC(NumDSE   , "Number of dead stores elided");
43 STATISTIC(NumDCE   , "Number of copies elided");
44
45 namespace {
46   enum SpillerName { simple, local };
47
48   static cl::opt<SpillerName>
49   SpillerOpt("spiller",
50              cl::desc("Spiller to use: (default: local)"),
51              cl::Prefix,
52              cl::values(clEnumVal(simple, "  simple spiller"),
53                         clEnumVal(local,  "  local spiller"),
54                         clEnumValEnd),
55              cl::init(local));
56 }
57
58 //===----------------------------------------------------------------------===//
59 //  VirtRegMap implementation
60 //===----------------------------------------------------------------------===//
61
62 VirtRegMap::VirtRegMap(MachineFunction &mf)
63   : TII(*mf.getTarget().getInstrInfo()), MF(mf), 
64     Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT),
65     ReMatId(MAX_STACK_SLOT+1) {
66   grow();
67 }
68
69 void VirtRegMap::grow() {
70   Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
71   Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
72 }
73
74 int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
75   assert(MRegisterInfo::isVirtualRegister(virtReg));
76   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
77          "attempt to assign stack slot to already spilled register");
78   const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
79   int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
80                                                         RC->getAlignment());
81   Virt2StackSlotMap[virtReg] = frameIndex;
82   ++NumSpills;
83   return frameIndex;
84 }
85
86 void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
87   assert(MRegisterInfo::isVirtualRegister(virtReg));
88   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
89          "attempt to assign stack slot to already spilled register");
90   Virt2StackSlotMap[virtReg] = frameIndex;
91 }
92
93 int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
94   assert(MRegisterInfo::isVirtualRegister(virtReg));
95   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
96          "attempt to assign re-mat id to already spilled register");
97   Virt2StackSlotMap[virtReg] = ReMatId;
98   ++NumReMats;
99   return ReMatId++;
100 }
101
102 void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
103                             unsigned OpNo, MachineInstr *NewMI) {
104   // Move previous memory references folded to new instruction.
105   MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
106   for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
107          E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
108     MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
109     MI2VirtMap.erase(I++);
110   }
111
112   ModRef MRInfo;
113   const TargetInstrDescriptor *TID = OldMI->getInstrDescriptor();
114   if (TID->getOperandConstraint(OpNo, TOI::TIED_TO) != -1 ||
115       TID->findTiedToSrcOperand(OpNo) != -1) {
116     // Folded a two-address operand.
117     MRInfo = isModRef;
118   } else if (OldMI->getOperand(OpNo).isDef()) {
119     MRInfo = isMod;
120   } else {
121     MRInfo = isRef;
122   }
123
124   // add new memory reference
125   MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
126 }
127
128 void VirtRegMap::print(std::ostream &OS) const {
129   const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
130
131   OS << "********** REGISTER MAP **********\n";
132   for (unsigned i = MRegisterInfo::FirstVirtualRegister,
133          e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
134     if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
135       OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
136
137   }
138
139   for (unsigned i = MRegisterInfo::FirstVirtualRegister,
140          e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
141     if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
142       OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
143   OS << '\n';
144 }
145
146 void VirtRegMap::dump() const {
147   print(DOUT);
148 }
149
150
151 //===----------------------------------------------------------------------===//
152 // Simple Spiller Implementation
153 //===----------------------------------------------------------------------===//
154
155 Spiller::~Spiller() {}
156
157 namespace {
158   struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller {
159     bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM);
160   };
161 }
162
163 bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
164   DOUT << "********** REWRITE MACHINE CODE **********\n";
165   DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
166   const TargetMachine &TM = MF.getTarget();
167   const MRegisterInfo &MRI = *TM.getRegisterInfo();
168   bool *PhysRegsUsed = MF.getUsedPhysregs();
169
170   // LoadedRegs - Keep track of which vregs are loaded, so that we only load
171   // each vreg once (in the case where a spilled vreg is used by multiple
172   // operands).  This is always smaller than the number of operands to the
173   // current machine instr, so it should be small.
174   std::vector<unsigned> LoadedRegs;
175
176   for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
177        MBBI != E; ++MBBI) {
178     DOUT << MBBI->getBasicBlock()->getName() << ":\n";
179     MachineBasicBlock &MBB = *MBBI;
180     for (MachineBasicBlock::iterator MII = MBB.begin(),
181            E = MBB.end(); MII != E; ++MII) {
182       MachineInstr &MI = *MII;
183       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
184         MachineOperand &MO = MI.getOperand(i);
185         if (MO.isRegister() && MO.getReg())
186           if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
187             unsigned VirtReg = MO.getReg();
188             unsigned PhysReg = VRM.getPhys(VirtReg);
189             if (VRM.hasStackSlot(VirtReg)) {
190               int StackSlot = VRM.getStackSlot(VirtReg);
191               const TargetRegisterClass* RC =
192                 MF.getSSARegMap()->getRegClass(VirtReg);
193
194               if (MO.isUse() &&
195                   std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
196                   == LoadedRegs.end()) {
197                 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
198                 LoadedRegs.push_back(VirtReg);
199                 ++NumLoads;
200                 DOUT << '\t' << *prior(MII);
201               }
202
203               if (MO.isDef()) {
204                 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
205                 ++NumStores;
206               }
207             }
208             PhysRegsUsed[PhysReg] = true;
209             MI.getOperand(i).setReg(PhysReg);
210           } else {
211             PhysRegsUsed[MO.getReg()] = true;
212           }
213       }
214
215       DOUT << '\t' << MI;
216       LoadedRegs.clear();
217     }
218   }
219   return true;
220 }
221
222 //===----------------------------------------------------------------------===//
223 //  Local Spiller Implementation
224 //===----------------------------------------------------------------------===//
225
226 namespace {
227   /// LocalSpiller - This spiller does a simple pass over the machine basic
228   /// block to attempt to keep spills in registers as much as possible for
229   /// blocks that have low register pressure (the vreg may be spilled due to
230   /// register pressure in other blocks).
231   class VISIBILITY_HIDDEN LocalSpiller : public Spiller {
232     const MRegisterInfo *MRI;
233     const TargetInstrInfo *TII;
234   public:
235     bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
236       MRI = MF.getTarget().getRegisterInfo();
237       TII = MF.getTarget().getInstrInfo();
238       DOUT << "\n**** Local spiller rewriting function '"
239            << MF.getFunction()->getName() << "':\n";
240
241       std::vector<MachineInstr *> ReMatedMIs;
242       for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
243            MBB != E; ++MBB)
244         RewriteMBB(*MBB, VRM, ReMatedMIs);
245       for (unsigned i = 0, e = ReMatedMIs.size(); i != e; ++i)
246         delete ReMatedMIs[i];
247       return true;
248     }
249   private:
250     void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
251                     std::vector<MachineInstr*> &ReMatedMIs);
252   };
253 }
254
255 /// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
256 /// top down, keep track of which spills slots are available in each register.
257 ///
258 /// Note that not all physregs are created equal here.  In particular, some
259 /// physregs are reloads that we are allowed to clobber or ignore at any time.
260 /// Other physregs are values that the register allocated program is using that
261 /// we cannot CHANGE, but we can read if we like.  We keep track of this on a 
262 /// per-stack-slot basis as the low bit in the value of the SpillSlotsAvailable
263 /// entries.  The predicate 'canClobberPhysReg()' checks this bit and
264 /// addAvailable sets it if.
265 namespace {
266 class VISIBILITY_HIDDEN AvailableSpills {
267   const MRegisterInfo *MRI;
268   const TargetInstrInfo *TII;
269
270   // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
271   // register values that are still available, due to being loaded or stored to,
272   // but not invalidated yet. It also tracks the instructions that defined
273   // or used the register.
274   typedef std::pair<unsigned, std::vector<MachineInstr*> > SSInfo;
275   std::map<int, SSInfo> SpillSlotsAvailable;
276     
277   // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
278   // which stack slot values are currently held by a physreg.  This is used to
279   // invalidate entries in SpillSlotsAvailable when a physreg is modified.
280   std::multimap<unsigned, int> PhysRegsAvailable;
281   
282   void disallowClobberPhysRegOnly(unsigned PhysReg);
283
284   void ClobberPhysRegOnly(unsigned PhysReg);
285 public:
286   AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii)
287     : MRI(mri), TII(tii) {
288   }
289   
290   const MRegisterInfo *getRegInfo() const { return MRI; }
291
292   /// getSpillSlotPhysReg - If the specified stack slot is available in a 
293   /// physical register, return that PhysReg, otherwise return 0. It also
294   /// returns by reference the instruction that either defines or last uses
295   /// the register.
296   unsigned getSpillSlotPhysReg(int Slot, MachineInstr *&SSMI) const {
297     std::map<int, SSInfo>::const_iterator I = SpillSlotsAvailable.find(Slot);
298     if (I != SpillSlotsAvailable.end()) {
299       if (!I->second.second.empty())
300         SSMI = I->second.second.back();
301       return I->second.first >> 1;  // Remove the CanClobber bit.
302     }
303     return 0;
304   }
305
306   /// addLastUse - Add the last use information of all stack slots whose
307   /// values are available in the specific register.
308   void addLastUse(unsigned PhysReg, MachineInstr *Use) {
309     std::multimap<unsigned, int>::iterator I =
310       PhysRegsAvailable.lower_bound(PhysReg);
311     while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
312       int Slot = I->second;
313       I++;
314
315       std::map<int, SSInfo>::iterator II = SpillSlotsAvailable.find(Slot);
316       assert(II != SpillSlotsAvailable.end() && "Slot not available!");
317       unsigned Val = II->second.first;
318       assert((Val >> 1) == PhysReg && "Bidirectional map mismatch!");
319       II->second.second.push_back(Use);
320     }
321   }
322   
323   /// removeLastUse - Remove the last use information of all stack slots whose
324   /// values are available in the specific register.
325   void removeLastUse(unsigned PhysReg, MachineInstr *Use) {
326     std::multimap<unsigned, int>::iterator I =
327       PhysRegsAvailable.lower_bound(PhysReg);
328     while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
329       int Slot = I->second;
330       I++;
331
332       std::map<int, SSInfo>::iterator II = SpillSlotsAvailable.find(Slot);
333       assert(II != SpillSlotsAvailable.end() && "Slot not available!");
334       unsigned Val = II->second.first;
335       assert((Val >> 1) == PhysReg && "Bidirectional map mismatch!");
336       if (II->second.second.back() == Use)
337         II->second.second.pop_back();
338     }
339   }
340   
341   /// addAvailable - Mark that the specified stack slot is available in the
342   /// specified physreg.  If CanClobber is true, the physreg can be modified at
343   /// any time without changing the semantics of the program.
344   void addAvailable(int Slot, MachineInstr *MI, unsigned Reg,
345                     bool CanClobber = true) {
346     // If this stack slot is thought to be available in some other physreg, 
347     // remove its record.
348     ModifyStackSlot(Slot);
349     
350     PhysRegsAvailable.insert(std::make_pair(Reg, Slot));
351     std::vector<MachineInstr*> DefUses;
352     DefUses.push_back(MI);
353     SpillSlotsAvailable[Slot] =
354       std::make_pair((Reg << 1) | (unsigned)CanClobber, DefUses);
355   
356     if (Slot > VirtRegMap::MAX_STACK_SLOT)
357       DOUT << "Remembering RM#" << Slot-VirtRegMap::MAX_STACK_SLOT-1;
358     else
359       DOUT << "Remembering SS#" << Slot;
360     DOUT << " in physreg " << MRI->getName(Reg) << "\n";
361   }
362
363   /// canClobberPhysReg - Return true if the spiller is allowed to change the 
364   /// value of the specified stackslot register if it desires.  The specified
365   /// stack slot must be available in a physreg for this query to make sense.
366   bool canClobberPhysReg(int Slot) const {
367     assert(SpillSlotsAvailable.count(Slot) && "Slot not available!");
368     return SpillSlotsAvailable.find(Slot)->second.first & 1;
369   }
370   
371   /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
372   /// stackslot register. The register is still available but is no longer
373   /// allowed to be modifed.
374   void disallowClobberPhysReg(unsigned PhysReg);
375   
376   /// ClobberPhysReg - This is called when the specified physreg changes
377   /// value.  We use this to invalidate any info about stuff we thing lives in
378   /// it and any of its aliases.
379   void ClobberPhysReg(unsigned PhysReg);
380
381   /// ModifyStackSlot - This method is called when the value in a stack slot
382   /// changes.  This removes information about which register the previous value
383   /// for this slot lives in (as the previous value is dead now).
384   void ModifyStackSlot(int Slot);
385 };
386 }
387
388 /// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified
389 /// stackslot register. The register is still available but is no longer
390 /// allowed to be modifed.
391 void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) {
392   std::multimap<unsigned, int>::iterator I =
393     PhysRegsAvailable.lower_bound(PhysReg);
394   while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
395     int Slot = I->second;
396     I++;
397     assert((SpillSlotsAvailable[Slot].first >> 1) == PhysReg &&
398            "Bidirectional map mismatch!");
399     SpillSlotsAvailable[Slot].first &= ~1;
400     DOUT << "PhysReg " << MRI->getName(PhysReg)
401          << " copied, it is available for use but can no longer be modified\n";
402   }
403 }
404
405 /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
406 /// stackslot register and its aliases. The register and its aliases may
407 /// still available but is no longer allowed to be modifed.
408 void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) {
409   for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
410     disallowClobberPhysRegOnly(*AS);
411   disallowClobberPhysRegOnly(PhysReg);
412 }
413
414 /// ClobberPhysRegOnly - This is called when the specified physreg changes
415 /// value.  We use this to invalidate any info about stuff we thing lives in it.
416 void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
417   std::multimap<unsigned, int>::iterator I =
418     PhysRegsAvailable.lower_bound(PhysReg);
419   while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
420     int Slot = I->second;
421     PhysRegsAvailable.erase(I++);
422     assert((SpillSlotsAvailable[Slot].first >> 1) == PhysReg &&
423            "Bidirectional map mismatch!");
424     SpillSlotsAvailable.erase(Slot);
425     DOUT << "PhysReg " << MRI->getName(PhysReg)
426          << " clobbered, invalidating ";
427     if (Slot > VirtRegMap::MAX_STACK_SLOT)
428       DOUT << "RM#" << Slot-VirtRegMap::MAX_STACK_SLOT-1 << "\n";
429     else
430       DOUT << "SS#" << Slot << "\n";
431   }
432 }
433
434 /// ClobberPhysReg - This is called when the specified physreg changes
435 /// value.  We use this to invalidate any info about stuff we thing lives in
436 /// it and any of its aliases.
437 void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
438   for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
439     ClobberPhysRegOnly(*AS);
440   ClobberPhysRegOnly(PhysReg);
441 }
442
443 /// ModifyStackSlot - This method is called when the value in a stack slot
444 /// changes.  This removes information about which register the previous value
445 /// for this slot lives in (as the previous value is dead now).
446 void AvailableSpills::ModifyStackSlot(int Slot) {
447   std::map<int, SSInfo>::iterator It = SpillSlotsAvailable.find(Slot);
448   if (It == SpillSlotsAvailable.end()) return;
449   unsigned Reg = It->second.first >> 1;
450   SpillSlotsAvailable.erase(It);
451   
452   // This register may hold the value of multiple stack slots, only remove this
453   // stack slot from the set of values the register contains.
454   std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
455   for (; ; ++I) {
456     assert(I != PhysRegsAvailable.end() && I->first == Reg &&
457            "Map inverse broken!");
458     if (I->second == Slot) break;
459   }
460   PhysRegsAvailable.erase(I);
461 }
462
463
464
465 // ReusedOp - For each reused operand, we keep track of a bit of information, in
466 // case we need to rollback upon processing a new operand.  See comments below.
467 namespace {
468   struct ReusedOp {
469     // The MachineInstr operand that reused an available value.
470     unsigned Operand;
471
472     // StackSlot - The spill slot of the value being reused.
473     unsigned StackSlot;
474
475     // PhysRegReused - The physical register the value was available in.
476     unsigned PhysRegReused;
477
478     // AssignedPhysReg - The physreg that was assigned for use by the reload.
479     unsigned AssignedPhysReg;
480     
481     // VirtReg - The virtual register itself.
482     unsigned VirtReg;
483
484     ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
485              unsigned vreg)
486       : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
487       VirtReg(vreg) {}
488   };
489   
490   /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
491   /// is reused instead of reloaded.
492   class VISIBILITY_HIDDEN ReuseInfo {
493     MachineInstr &MI;
494     std::vector<ReusedOp> Reuses;
495     BitVector PhysRegsClobbered;
496   public:
497     ReuseInfo(MachineInstr &mi, const MRegisterInfo *mri) : MI(mi) {
498       PhysRegsClobbered.resize(mri->getNumRegs());
499     }
500     
501     bool hasReuses() const {
502       return !Reuses.empty();
503     }
504     
505     /// addReuse - If we choose to reuse a virtual register that is already
506     /// available instead of reloading it, remember that we did so.
507     void addReuse(unsigned OpNo, unsigned StackSlot,
508                   unsigned PhysRegReused, unsigned AssignedPhysReg,
509                   unsigned VirtReg) {
510       // If the reload is to the assigned register anyway, no undo will be
511       // required.
512       if (PhysRegReused == AssignedPhysReg) return;
513       
514       // Otherwise, remember this.
515       Reuses.push_back(ReusedOp(OpNo, StackSlot, PhysRegReused, 
516                                 AssignedPhysReg, VirtReg));
517     }
518
519     void markClobbered(unsigned PhysReg) {
520       PhysRegsClobbered.set(PhysReg);
521     }
522
523     bool isClobbered(unsigned PhysReg) const {
524       return PhysRegsClobbered.test(PhysReg);
525     }
526     
527     /// GetRegForReload - We are about to emit a reload into PhysReg.  If there
528     /// is some other operand that is using the specified register, either pick
529     /// a new register to use, or evict the previous reload and use this reg. 
530     unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
531                              AvailableSpills &Spills,
532                              std::map<int, MachineInstr*> &MaybeDeadStores,
533                              SmallSet<unsigned, 8> &Rejected) {
534       if (Reuses.empty()) return PhysReg;  // This is most often empty.
535
536       for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
537         ReusedOp &Op = Reuses[ro];
538         // If we find some other reuse that was supposed to use this register
539         // exactly for its reload, we can change this reload to use ITS reload
540         // register. That is, unless its reload register has already been
541         // considered and subsequently rejected because it has also been reused
542         // by another operand.
543         if (Op.PhysRegReused == PhysReg &&
544             Rejected.count(Op.AssignedPhysReg) == 0) {
545           // Yup, use the reload register that we didn't use before.
546           unsigned NewReg = Op.AssignedPhysReg;
547           Rejected.insert(PhysReg);
548           return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores, Rejected);
549         } else {
550           // Otherwise, we might also have a problem if a previously reused
551           // value aliases the new register.  If so, codegen the previous reload
552           // and use this one.          
553           unsigned PRRU = Op.PhysRegReused;
554           const MRegisterInfo *MRI = Spills.getRegInfo();
555           if (MRI->areAliases(PRRU, PhysReg)) {
556             // Okay, we found out that an alias of a reused register
557             // was used.  This isn't good because it means we have
558             // to undo a previous reuse.
559             MachineBasicBlock *MBB = MI->getParent();
560             const TargetRegisterClass *AliasRC =
561               MBB->getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
562
563             // Copy Op out of the vector and remove it, we're going to insert an
564             // explicit load for it.
565             ReusedOp NewOp = Op;
566             Reuses.erase(Reuses.begin()+ro);
567
568             // Ok, we're going to try to reload the assigned physreg into the
569             // slot that we were supposed to in the first place.  However, that
570             // register could hold a reuse.  Check to see if it conflicts or
571             // would prefer us to use a different register.
572             unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
573                                          MI, Spills, MaybeDeadStores, Rejected);
574             
575             MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
576                                       NewOp.StackSlot, AliasRC);
577             Spills.ClobberPhysReg(NewPhysReg);
578             Spills.ClobberPhysReg(NewOp.PhysRegReused);
579             
580             // Any stores to this stack slot are not dead anymore.
581             MaybeDeadStores.erase(NewOp.StackSlot);
582             
583             MI->getOperand(NewOp.Operand).setReg(NewPhysReg);
584             
585             Spills.addAvailable(NewOp.StackSlot, MI, NewPhysReg);
586             ++NumLoads;
587             DEBUG(MachineBasicBlock::iterator MII = MI;
588                   DOUT << '\t' << *prior(MII));
589             
590             DOUT << "Reuse undone!\n";
591             --NumReused;
592             
593             // Finally, PhysReg is now available, go ahead and use it.
594             return PhysReg;
595           }
596         }
597       }
598       return PhysReg;
599     }
600
601     /// GetRegForReload - Helper for the above GetRegForReload(). Add a
602     /// 'Rejected' set to remember which registers have been considered and
603     /// rejected for the reload. This avoids infinite looping in case like
604     /// this:
605     /// t1 := op t2, t3
606     /// t2 <- assigned r0 for use by the reload but ended up reuse r1
607     /// t3 <- assigned r1 for use by the reload but ended up reuse r0
608     /// t1 <- desires r1
609     ///       sees r1 is taken by t2, tries t2's reload register r0
610     ///       sees r0 is taken by t3, tries t3's reload register r1
611     ///       sees r1 is taken by t2, tries t2's reload register r0 ...
612     unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
613                              AvailableSpills &Spills,
614                              std::map<int, MachineInstr*> &MaybeDeadStores) {
615       SmallSet<unsigned, 8> Rejected;
616       return GetRegForReload(PhysReg, MI, Spills, MaybeDeadStores, Rejected);
617     }
618   };
619 }
620
621
622 /// rewriteMBB - Keep track of which spills are available even after the
623 /// register allocator is done with them.  If possible, avoid reloading vregs.
624 void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
625                               std::vector<MachineInstr*> &ReMatedMIs) {
626
627   DOUT << MBB.getBasicBlock()->getName() << ":\n";
628
629   // Spills - Keep track of which spilled values are available in physregs so
630   // that we can choose to reuse the physregs instead of emitting reloads.
631   AvailableSpills Spills(MRI, TII);
632   
633   // MaybeDeadStores - When we need to write a value back into a stack slot,
634   // keep track of the inserted store.  If the stack slot value is never read
635   // (because the value was used from some available register, for example), and
636   // subsequently stored to, the original store is dead.  This map keeps track
637   // of inserted stores that are not used.  If we see a subsequent store to the
638   // same stack slot, the original store is deleted.
639   std::map<int, MachineInstr*> MaybeDeadStores;
640
641   bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
642
643   for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
644        MII != E; ) {
645     MachineInstr &MI = *MII;
646     MachineBasicBlock::iterator NextMII = MII; ++NextMII;
647
648     /// ReusedOperands - Keep track of operand reuse in case we need to undo
649     /// reuse.
650     ReuseInfo ReusedOperands(MI, MRI);
651
652     // Loop over all of the implicit defs, clearing them from our available
653     // sets.
654     const TargetInstrDescriptor *TID = MI.getInstrDescriptor();
655
656     // If this instruction is being rematerialized, just remove it!
657     if (TID->Flags & M_REMATERIALIZIBLE) {
658       bool Remove = true;
659       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
660         MachineOperand &MO = MI.getOperand(i);
661         if (!MO.isRegister() || MO.getReg() == 0)
662           continue;   // Ignore non-register operands.
663         if (MO.isDef() && !VRM.isReMaterialized(MO.getReg())) {
664           Remove = false;
665           break;
666         }
667       }
668       if (Remove) {
669         VRM.RemoveFromFoldedVirtMap(&MI);
670         ReMatedMIs.push_back(MI.removeFromParent());
671         MII = NextMII;
672         continue;
673       }
674     }
675
676     const unsigned *ImpDef = TID->ImplicitDefs;
677     if (ImpDef) {
678       for ( ; *ImpDef; ++ImpDef) {
679         PhysRegsUsed[*ImpDef] = true;
680         ReusedOperands.markClobbered(*ImpDef);
681         Spills.ClobberPhysReg(*ImpDef);
682       }
683     }
684
685     // Process all of the spilled uses and all non spilled reg references.
686     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
687       MachineOperand &MO = MI.getOperand(i);
688       if (!MO.isRegister() || MO.getReg() == 0)
689         continue;   // Ignore non-register operands.
690       
691       if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
692         // Ignore physregs for spilling, but remember that it is used by this
693         // function.
694         PhysRegsUsed[MO.getReg()] = true;
695         ReusedOperands.markClobbered(MO.getReg());
696         continue;
697       }
698       
699       assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
700              "Not a virtual or a physical register?");
701       
702       unsigned VirtReg = MO.getReg();
703       if (!VRM.hasStackSlot(VirtReg)) {
704         // This virtual register was assigned a physreg!
705         unsigned Phys = VRM.getPhys(VirtReg);
706         PhysRegsUsed[Phys] = true;
707         if (MO.isDef())
708           ReusedOperands.markClobbered(Phys);
709         MI.getOperand(i).setReg(Phys);
710         continue;
711       }
712       
713       // This virtual register is now known to be a spilled value.
714       if (!MO.isUse())
715         continue;  // Handle defs in the loop below (handle use&def here though)
716
717       bool doReMat = VRM.isReMaterialized(VirtReg);
718       int StackSlot = VRM.getStackSlot(VirtReg);
719       unsigned PhysReg;
720
721       // Check to see if this stack slot is available.
722       MachineInstr *SSMI = NULL;
723       if ((PhysReg = Spills.getSpillSlotPhysReg(StackSlot, SSMI))) {
724         // This spilled operand might be part of a two-address operand.  If this
725         // is the case, then changing it will necessarily require changing the 
726         // def part of the instruction as well.  However, in some cases, we
727         // aren't allowed to modify the reused register.  If none of these cases
728         // apply, reuse it.
729         bool CanReuse = true;
730         int ti = TID->getOperandConstraint(i, TOI::TIED_TO);
731         if (ti != -1 &&
732             MI.getOperand(ti).isReg() && 
733             MI.getOperand(ti).getReg() == VirtReg) {
734           // Okay, we have a two address operand.  We can reuse this physreg as
735           // long as we are allowed to clobber the value and there isn't an
736           // earlier def that has already clobbered the physreg.
737           CanReuse = Spills.canClobberPhysReg(StackSlot) &&
738             !ReusedOperands.isClobbered(PhysReg);
739         }
740         
741         if (CanReuse) {
742           // If this stack slot value is already available, reuse it!
743           if (StackSlot > VirtRegMap::MAX_STACK_SLOT)
744             DOUT << "Reusing RM#" << StackSlot-VirtRegMap::MAX_STACK_SLOT-1;
745           else
746             DOUT << "Reusing SS#" << StackSlot;
747           DOUT << " from physreg "
748                << MRI->getName(PhysReg) << " for vreg"
749                << VirtReg <<" instead of reloading into physreg "
750                << MRI->getName(VRM.getPhys(VirtReg)) << "\n";
751           MI.getOperand(i).setReg(PhysReg);
752
753           // Extend the live range of the MI that last kill the register if
754           // necessary.
755           bool WasKill = false;
756           if (SSMI) {
757             MachineOperand *MOK = SSMI->findRegisterUseOperand(PhysReg, true);
758             if (MOK) {
759               WasKill = MOK->isKill();
760               MOK->unsetIsKill();
761             }
762           }
763           if (ti == -1) {
764             // Unless it's the use of a two-address code, transfer the kill
765             // of the reused register to this use.
766             if (WasKill)
767               MI.getOperand(i).setIsKill();
768             Spills.addLastUse(PhysReg, &MI);
769           }
770
771           // The only technical detail we have is that we don't know that
772           // PhysReg won't be clobbered by a reloaded stack slot that occurs
773           // later in the instruction.  In particular, consider 'op V1, V2'.
774           // If V1 is available in physreg R0, we would choose to reuse it
775           // here, instead of reloading it into the register the allocator
776           // indicated (say R1).  However, V2 might have to be reloaded
777           // later, and it might indicate that it needs to live in R0.  When
778           // this occurs, we need to have information available that
779           // indicates it is safe to use R1 for the reload instead of R0.
780           //
781           // To further complicate matters, we might conflict with an alias,
782           // or R0 and R1 might not be compatible with each other.  In this
783           // case, we actually insert a reload for V1 in R1, ensuring that
784           // we can get at R0 or its alias.
785           ReusedOperands.addReuse(i, StackSlot, PhysReg,
786                                   VRM.getPhys(VirtReg), VirtReg);
787           if (ti != -1)
788             // Only mark it clobbered if this is a use&def operand.
789             ReusedOperands.markClobbered(PhysReg);
790           ++NumReused;
791           continue;
792         }
793         
794         // Otherwise we have a situation where we have a two-address instruction
795         // whose mod/ref operand needs to be reloaded.  This reload is already
796         // available in some register "PhysReg", but if we used PhysReg as the
797         // operand to our 2-addr instruction, the instruction would modify
798         // PhysReg.  This isn't cool if something later uses PhysReg and expects
799         // to get its initial value.
800         //
801         // To avoid this problem, and to avoid doing a load right after a store,
802         // we emit a copy from PhysReg into the designated register for this
803         // operand.
804         unsigned DesignatedReg = VRM.getPhys(VirtReg);
805         assert(DesignatedReg && "Must map virtreg to physreg!");
806
807         // Note that, if we reused a register for a previous operand, the
808         // register we want to reload into might not actually be
809         // available.  If this occurs, use the register indicated by the
810         // reuser.
811         if (ReusedOperands.hasReuses())
812           DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI, 
813                                                       Spills, MaybeDeadStores);
814         
815         // If the mapped designated register is actually the physreg we have
816         // incoming, we don't need to inserted a dead copy.
817         if (DesignatedReg == PhysReg) {
818           // If this stack slot value is already available, reuse it!
819           if (StackSlot > VirtRegMap::MAX_STACK_SLOT)
820             DOUT << "Reusing RM#" << StackSlot-VirtRegMap::MAX_STACK_SLOT-1;
821           else
822             DOUT << "Reusing SS#" << StackSlot;
823           DOUT << " from physreg " << MRI->getName(PhysReg) << " for vreg"
824                << VirtReg
825                << " instead of reloading into same physreg.\n";
826           MI.getOperand(i).setReg(PhysReg);
827           ReusedOperands.markClobbered(PhysReg);
828           ++NumReused;
829           continue;
830         }
831         
832         const TargetRegisterClass* RC =
833           MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
834
835         PhysRegsUsed[DesignatedReg] = true;
836         ReusedOperands.markClobbered(DesignatedReg);
837         MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC);
838
839         // Extend the live range of the MI that last kill the register if
840         // necessary.
841         bool WasKill = false;
842         if (SSMI) {
843           MachineOperand *MOK = SSMI->findRegisterUseOperand(PhysReg, true);
844           if (MOK) {
845             WasKill = MOK->isKill();
846             MOK->unsetIsKill();
847           }
848         }
849         MachineInstr *CopyMI = prior(MII);
850         if (WasKill) {
851           // Transfer kill to the next use.
852           MachineOperand *MOU = CopyMI->findRegisterUseOperand(PhysReg);
853           MOU->setIsKill();
854         }
855         Spills.addLastUse(PhysReg, CopyMI);
856
857         // This invalidates DesignatedReg.
858         Spills.ClobberPhysReg(DesignatedReg);
859         
860         Spills.addAvailable(StackSlot, &MI, DesignatedReg);
861         MI.getOperand(i).setReg(DesignatedReg);
862         DOUT << '\t' << *prior(MII);
863         ++NumReused;
864         continue;
865       }
866       
867       // Otherwise, reload it and remember that we have it.
868       PhysReg = VRM.getPhys(VirtReg);
869       assert(PhysReg && "Must map virtreg to physreg!");
870       const TargetRegisterClass* RC =
871         MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
872
873       // Note that, if we reused a register for a previous operand, the
874       // register we want to reload into might not actually be
875       // available.  If this occurs, use the register indicated by the
876       // reuser.
877       if (ReusedOperands.hasReuses())
878         PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, 
879                                                  Spills, MaybeDeadStores);
880       
881       PhysRegsUsed[PhysReg] = true;
882       ReusedOperands.markClobbered(PhysReg);
883       if (doReMat)
884         MRI->reMaterialize(MBB, &MI, PhysReg, VRM.getReMaterializedMI(VirtReg));
885       else
886         MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
887       // This invalidates PhysReg.
888       Spills.ClobberPhysReg(PhysReg);
889
890       // Any stores to this stack slot are not dead anymore.
891       if (!doReMat)
892         MaybeDeadStores.erase(StackSlot);
893       Spills.addAvailable(StackSlot, &MI, PhysReg);
894       // Assumes this is the last use. IsKill will be unset if reg is reused
895       // unless it's a two-address operand.
896       if (TID->getOperandConstraint(i, TOI::TIED_TO) == -1)
897         MI.getOperand(i).setIsKill();
898       ++NumLoads;
899       MI.getOperand(i).setReg(PhysReg);
900       DOUT << '\t' << *prior(MII);
901     }
902
903     DOUT << '\t' << MI;
904
905     // If we have folded references to memory operands, make sure we clear all
906     // physical registers that may contain the value of the spilled virtual
907     // register
908     VirtRegMap::MI2VirtMapTy::const_iterator I, End;
909     for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
910       DOUT << "Folded vreg: " << I->second.first << "  MR: "
911            << I->second.second;
912       unsigned VirtReg = I->second.first;
913       VirtRegMap::ModRef MR = I->second.second;
914       if (!VRM.hasStackSlot(VirtReg)) {
915         DOUT << ": No stack slot!\n";
916         continue;
917       }
918       int SS = VRM.getStackSlot(VirtReg);
919       DOUT << " - StackSlot: " << SS << "\n";
920       
921       // If this folded instruction is just a use, check to see if it's a
922       // straight load from the virt reg slot.
923       if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
924         int FrameIdx;
925         if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
926           if (FrameIdx == SS) {
927             // If this spill slot is available, turn it into a copy (or nothing)
928             // instead of leaving it as a load!
929             MachineInstr *SSMI = NULL;
930             if (unsigned InReg = Spills.getSpillSlotPhysReg(SS, SSMI)) {
931               DOUT << "Promoted Load To Copy: " << MI;
932               MachineFunction &MF = *MBB.getParent();
933               if (DestReg != InReg) {
934                 MRI->copyRegToReg(MBB, &MI, DestReg, InReg,
935                                   MF.getSSARegMap()->getRegClass(VirtReg));
936                 // Revisit the copy so we make sure to notice the effects of the
937                 // operation on the destreg (either needing to RA it if it's 
938                 // virtual or needing to clobber any values if it's physical).
939                 NextMII = &MI;
940                 --NextMII;  // backtrack to the copy.
941               } else
942                 DOUT << "Removing now-noop copy: " << MI;
943
944               // Either way, the live range of the last kill of InReg has been
945               // extended. Remove its kill.
946               bool WasKill = false;
947               if (SSMI) {
948                 MachineOperand *MOK = SSMI->findRegisterUseOperand(InReg, true);
949                 if (MOK) {
950                   WasKill = MOK->isKill();
951                   MOK->unsetIsKill();
952                 }
953               }
954               if (NextMII != MBB.end()) {
955                 // If NextMII uses InReg (must be the copy?), mark it killed.
956                 MachineOperand *MOU = NextMII->findRegisterUseOperand(InReg);
957                 if (MOU) {
958                   if (WasKill)
959                     MOU->setIsKill();
960                   Spills.addLastUse(InReg, &(*NextMII));
961                 }
962               }
963
964               VRM.RemoveFromFoldedVirtMap(&MI);
965               MBB.erase(&MI);
966               goto ProcessNextInst;
967             }
968           }
969         }
970       }
971
972       // If this reference is not a use, any previous store is now dead.
973       // Otherwise, the store to this stack slot is not dead anymore.
974       std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
975       if (MDSI != MaybeDeadStores.end()) {
976         if (MR & VirtRegMap::isRef)   // Previous store is not dead.
977           MaybeDeadStores.erase(MDSI);
978         else {
979           // If we get here, the store is dead, nuke it now.
980           assert(VirtRegMap::isMod && "Can't be modref!");
981           DOUT << "Removed dead store:\t" << *MDSI->second;
982           MBB.erase(MDSI->second);
983           VRM.RemoveFromFoldedVirtMap(MDSI->second);
984           MaybeDeadStores.erase(MDSI);
985           ++NumDSE;
986         }
987       }
988
989       // If the spill slot value is available, and this is a new definition of
990       // the value, the value is not available anymore.
991       if (MR & VirtRegMap::isMod) {
992         // Notice that the value in this stack slot has been modified.
993         Spills.ModifyStackSlot(SS);
994         
995         // If this is *just* a mod of the value, check to see if this is just a
996         // store to the spill slot (i.e. the spill got merged into the copy). If
997         // so, realize that the vreg is available now, and add the store to the
998         // MaybeDeadStore info.
999         int StackSlot;
1000         if (!(MR & VirtRegMap::isRef)) {
1001           if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
1002             assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
1003                    "Src hasn't been allocated yet?");
1004             // Okay, this is certainly a store of SrcReg to [StackSlot].  Mark
1005             // this as a potentially dead store in case there is a subsequent
1006             // store into the stack slot without a read from it.
1007             MaybeDeadStores[StackSlot] = &MI;
1008
1009             // If the stack slot value was previously available in some other
1010             // register, change it now.  Otherwise, make the register available,
1011             // in PhysReg.
1012             Spills.addAvailable(StackSlot, &MI, SrcReg, false/*don't clobber*/);
1013           }
1014         }
1015       }
1016     }
1017
1018     // Process all of the spilled defs.
1019     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1020       MachineOperand &MO = MI.getOperand(i);
1021       if (MO.isRegister() && MO.getReg() && MO.isDef()) {
1022         unsigned VirtReg = MO.getReg();
1023
1024         if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
1025           // Check to see if this is a noop copy.  If so, eliminate the
1026           // instruction before considering the dest reg to be changed.
1027           unsigned Src, Dst;
1028           if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1029             ++NumDCE;
1030             DOUT << "Removing now-noop copy: " << MI;
1031             Spills.removeLastUse(Src, &MI);
1032             MBB.erase(&MI);
1033             VRM.RemoveFromFoldedVirtMap(&MI);
1034             Spills.disallowClobberPhysReg(VirtReg);
1035             goto ProcessNextInst;
1036           }
1037           
1038           // If it's not a no-op copy, it clobbers the value in the destreg.
1039           Spills.ClobberPhysReg(VirtReg);
1040           ReusedOperands.markClobbered(VirtReg);
1041  
1042           // Check to see if this instruction is a load from a stack slot into
1043           // a register.  If so, this provides the stack slot value in the reg.
1044           int FrameIdx;
1045           if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
1046             assert(DestReg == VirtReg && "Unknown load situation!");
1047             
1048             // Otherwise, if it wasn't available, remember that it is now!
1049             Spills.addAvailable(FrameIdx, &MI, DestReg);
1050             goto ProcessNextInst;
1051           }
1052             
1053           continue;
1054         }
1055
1056         // The only vregs left are stack slot definitions.
1057         int StackSlot = VRM.getStackSlot(VirtReg);
1058         const TargetRegisterClass *RC =
1059           MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
1060
1061         // If this def is part of a two-address operand, make sure to execute
1062         // the store from the correct physical register.
1063         unsigned PhysReg;
1064         int TiedOp = MI.getInstrDescriptor()->findTiedToSrcOperand(i);
1065         if (TiedOp != -1)
1066           PhysReg = MI.getOperand(TiedOp).getReg();
1067         else {
1068           PhysReg = VRM.getPhys(VirtReg);
1069           if (ReusedOperands.isClobbered(PhysReg)) {
1070             // Another def has taken the assigned physreg. It must have been a
1071             // use&def which got it due to reuse. Undo the reuse!
1072             PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, 
1073                                                      Spills, MaybeDeadStores);
1074           }
1075         }
1076
1077         PhysRegsUsed[PhysReg] = true;
1078         ReusedOperands.markClobbered(PhysReg);
1079         MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
1080         DOUT << "Store:\t" << *next(MII);
1081         MI.getOperand(i).setReg(PhysReg);
1082
1083         // If there is a dead store to this stack slot, nuke it now.
1084         MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
1085         if (LastStore) {
1086           DOUT << "Removed dead store:\t" << *LastStore;
1087           ++NumDSE;
1088           MBB.erase(LastStore);
1089           VRM.RemoveFromFoldedVirtMap(LastStore);
1090         }
1091         LastStore = next(MII);
1092
1093         // If the stack slot value was previously available in some other
1094         // register, change it now.  Otherwise, make the register available,
1095         // in PhysReg.
1096         Spills.ModifyStackSlot(StackSlot);
1097         Spills.ClobberPhysReg(PhysReg);
1098         Spills.addAvailable(StackSlot, LastStore, PhysReg);
1099         ++NumStores;
1100
1101         // Check to see if this is a noop copy.  If so, eliminate the
1102         // instruction before considering the dest reg to be changed.
1103         {
1104           unsigned Src, Dst;
1105           if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1106             ++NumDCE;
1107             DOUT << "Removing now-noop copy: " << MI;
1108             MBB.erase(&MI);
1109             VRM.RemoveFromFoldedVirtMap(&MI);
1110             goto ProcessNextInst;
1111           }
1112         }        
1113       }
1114     }
1115   ProcessNextInst:
1116     MII = NextMII;
1117   }
1118 }
1119
1120
1121
1122 llvm::Spiller* llvm::createSpiller() {
1123   switch (SpillerOpt) {
1124   default: assert(0 && "Unreachable!");
1125   case local:
1126     return new LocalSpiller();
1127   case simple:
1128     return new SimpleSpiller();
1129   }
1130 }