Update LocalRewriter::DistanceMap when inserting stack loads.
[oota-llvm.git] / lib / CodeGen / VirtRegRewriter.cpp
1 //===-- llvm/CodeGen/Rewriter.cpp -  Rewriter -----------------------------===//
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 #define DEBUG_TYPE "virtregrewriter"
11 #include "VirtRegRewriter.h"
12 #include "llvm/Support/Compiler.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Target/TargetLowering.h"
16 #include "llvm/ADT/DepthFirstIterator.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include <algorithm>
20 using namespace llvm;
21
22 STATISTIC(NumDSE     , "Number of dead stores elided");
23 STATISTIC(NumDSS     , "Number of dead spill slots removed");
24 STATISTIC(NumCommutes, "Number of instructions commuted");
25 STATISTIC(NumDRM     , "Number of re-materializable defs elided");
26 STATISTIC(NumStores  , "Number of stores added");
27 STATISTIC(NumPSpills , "Number of physical register spills");
28 STATISTIC(NumOmitted , "Number of reloads omited");
29 STATISTIC(NumAvoided , "Number of reloads deemed unnecessary");
30 STATISTIC(NumCopified, "Number of available reloads turned into copies");
31 STATISTIC(NumReMats  , "Number of re-materialization");
32 STATISTIC(NumLoads   , "Number of loads added");
33 STATISTIC(NumReused  , "Number of values reused");
34 STATISTIC(NumDCE     , "Number of copies elided");
35 STATISTIC(NumSUnfold , "Number of stores unfolded");
36 STATISTIC(NumModRefUnfold, "Number of modref unfolded");
37
38 namespace {
39   enum RewriterName { local, trivial };
40 }
41
42 static cl::opt<RewriterName>
43 RewriterOpt("rewriter",
44             cl::desc("Rewriter to use: (default: local)"),
45             cl::Prefix,
46             cl::values(clEnumVal(local,   "local rewriter"),
47                        clEnumVal(trivial, "trivial rewriter"),
48                        clEnumValEnd),
49             cl::init(local));
50
51 static cl::opt<bool>
52 ScheduleSpills("schedule-spills",
53                cl::desc("Schedule spill code"),
54                cl::init(false));
55
56 VirtRegRewriter::~VirtRegRewriter() {}
57
58 namespace {
59
60 /// This class is intended for use with the new spilling framework only. It
61 /// rewrites vreg def/uses to use the assigned preg, but does not insert any
62 /// spill code.
63 struct VISIBILITY_HIDDEN TrivialRewriter : public VirtRegRewriter {
64
65   bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM,
66                             LiveIntervals* LIs) {
67     DOUT << "********** REWRITE MACHINE CODE **********\n";
68     DEBUG(errs() << "********** Function: " 
69           << MF.getFunction()->getName() << '\n');
70     DOUT << "**** Machine Instrs"
71          << "(NOTE! Does not include spills and reloads!) ****\n";
72     DEBUG(MF.dump());
73
74     MachineRegisterInfo *mri = &MF.getRegInfo();
75
76     bool changed = false;
77
78     for (LiveIntervals::iterator liItr = LIs->begin(), liEnd = LIs->end();
79          liItr != liEnd; ++liItr) {
80
81       if (TargetRegisterInfo::isVirtualRegister(liItr->first)) {
82         if (VRM.hasPhys(liItr->first)) {
83           unsigned preg = VRM.getPhys(liItr->first);
84           mri->replaceRegWith(liItr->first, preg);
85           mri->setPhysRegUsed(preg);
86           changed = true;
87         }
88       }
89       else {
90         if (!liItr->second->empty()) {
91           mri->setPhysRegUsed(liItr->first);
92         }
93       }
94     }
95
96     
97     DOUT << "**** Post Machine Instrs ****\n";
98     DEBUG(MF.dump());
99     
100     return changed;
101   }
102
103 };
104
105 }
106
107 // ************************************************************************ //
108
109 namespace {
110
111 /// AvailableSpills - As the local rewriter is scanning and rewriting an MBB
112 /// from top down, keep track of which spill slots or remat are available in
113 /// each register.
114 ///
115 /// Note that not all physregs are created equal here.  In particular, some
116 /// physregs are reloads that we are allowed to clobber or ignore at any time.
117 /// Other physregs are values that the register allocated program is using
118 /// that we cannot CHANGE, but we can read if we like.  We keep track of this
119 /// on a per-stack-slot / remat id basis as the low bit in the value of the
120 /// SpillSlotsAvailable entries.  The predicate 'canClobberPhysReg()' checks
121 /// this bit and addAvailable sets it if.
122 class VISIBILITY_HIDDEN AvailableSpills {
123   const TargetRegisterInfo *TRI;
124   const TargetInstrInfo *TII;
125
126   // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled
127   // or remat'ed virtual register values that are still available, due to
128   // being loaded or stored to, but not invalidated yet.
129   std::map<int, unsigned> SpillSlotsOrReMatsAvailable;
130
131   // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable,
132   // indicating which stack slot values are currently held by a physreg.  This
133   // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a
134   // physreg is modified.
135   std::multimap<unsigned, int> PhysRegsAvailable;
136
137   void disallowClobberPhysRegOnly(unsigned PhysReg);
138
139   void ClobberPhysRegOnly(unsigned PhysReg);
140 public:
141   AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii)
142     : TRI(tri), TII(tii) {
143   }
144
145   /// clear - Reset the state.
146   void clear() {
147     SpillSlotsOrReMatsAvailable.clear();
148     PhysRegsAvailable.clear();
149   }
150
151   const TargetRegisterInfo *getRegInfo() const { return TRI; }
152
153   /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is
154   /// available in a physical register, return that PhysReg, otherwise
155   /// return 0.
156   unsigned getSpillSlotOrReMatPhysReg(int Slot) const {
157     std::map<int, unsigned>::const_iterator I =
158       SpillSlotsOrReMatsAvailable.find(Slot);
159     if (I != SpillSlotsOrReMatsAvailable.end()) {
160       return I->second >> 1;  // Remove the CanClobber bit.
161     }
162     return 0;
163   }
164
165   /// addAvailable - Mark that the specified stack slot / remat is available
166   /// in the specified physreg.  If CanClobber is true, the physreg can be
167   /// modified at any time without changing the semantics of the program.
168   void addAvailable(int SlotOrReMat, unsigned Reg, bool CanClobber = true) {
169     // If this stack slot is thought to be available in some other physreg, 
170     // remove its record.
171     ModifyStackSlotOrReMat(SlotOrReMat);
172
173     PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat));
174     SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) |
175                                               (unsigned)CanClobber;
176
177     if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
178       DOUT << "Remembering RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1;
179     else
180       DOUT << "Remembering SS#" << SlotOrReMat;
181     DOUT << " in physreg " << TRI->getName(Reg) << "\n";
182   }
183
184   /// canClobberPhysRegForSS - Return true if the spiller is allowed to change
185   /// the value of the specified stackslot register if it desires. The
186   /// specified stack slot must be available in a physreg for this query to
187   /// make sense.
188   bool canClobberPhysRegForSS(int SlotOrReMat) const {
189     assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) &&
190            "Value not available!");
191     return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1;
192   }
193
194   /// canClobberPhysReg - Return true if the spiller is allowed to clobber the
195   /// physical register where values for some stack slot(s) might be
196   /// available.
197   bool canClobberPhysReg(unsigned PhysReg) const {
198     std::multimap<unsigned, int>::const_iterator I =
199       PhysRegsAvailable.lower_bound(PhysReg);
200     while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
201       int SlotOrReMat = I->second;
202       I++;
203       if (!canClobberPhysRegForSS(SlotOrReMat))
204         return false;
205     }
206     return true;
207   }
208
209   /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
210   /// stackslot register. The register is still available but is no longer
211   /// allowed to be modifed.
212   void disallowClobberPhysReg(unsigned PhysReg);
213
214   /// ClobberPhysReg - This is called when the specified physreg changes
215   /// value.  We use this to invalidate any info about stuff that lives in
216   /// it and any of its aliases.
217   void ClobberPhysReg(unsigned PhysReg);
218
219   /// ModifyStackSlotOrReMat - This method is called when the value in a stack
220   /// slot changes.  This removes information about which register the
221   /// previous value for this slot lives in (as the previous value is dead
222   /// now).
223   void ModifyStackSlotOrReMat(int SlotOrReMat);
224
225   /// AddAvailableRegsToLiveIn - Availability information is being kept coming
226   /// into the specified MBB. Add available physical registers as potential
227   /// live-in's. If they are reused in the MBB, they will be added to the
228   /// live-in set to make register scavenger and post-allocation scheduler.
229   void AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, BitVector &RegKills,
230                                 std::vector<MachineOperand*> &KillOps);
231 };
232
233 }
234
235 // ************************************************************************ //
236
237 // Given a location where a reload of a spilled register or a remat of
238 // a constant is to be inserted, attempt to find a safe location to
239 // insert the load at an earlier point in the basic-block, to hide
240 // latency of the load and to avoid address-generation interlock
241 // issues.
242 static MachineBasicBlock::iterator
243 ComputeReloadLoc(MachineBasicBlock::iterator const InsertLoc,
244                  MachineBasicBlock::iterator const Begin,
245                  unsigned PhysReg,
246                  const TargetRegisterInfo *TRI,
247                  bool DoReMat,
248                  int SSorRMId,
249                  const TargetInstrInfo *TII,
250                  const MachineFunction &MF)
251 {
252   if (!ScheduleSpills)
253     return InsertLoc;
254
255   // Spill backscheduling is of primary interest to addresses, so
256   // don't do anything if the register isn't in the register class
257   // used for pointers.
258
259   const TargetLowering *TL = MF.getTarget().getTargetLowering();
260
261   if (!TL->isTypeLegal(TL->getPointerTy()))
262     // Believe it or not, this is true on PIC16.
263     return InsertLoc;
264
265   const TargetRegisterClass *ptrRegClass =
266     TL->getRegClassFor(TL->getPointerTy());
267   if (!ptrRegClass->contains(PhysReg))
268     return InsertLoc;
269
270   // Scan upwards through the preceding instructions. If an instruction doesn't
271   // reference the stack slot or the register we're loading, we can
272   // backschedule the reload up past it.
273   MachineBasicBlock::iterator NewInsertLoc = InsertLoc;
274   while (NewInsertLoc != Begin) {
275     MachineBasicBlock::iterator Prev = prior(NewInsertLoc);
276     for (unsigned i = 0; i < Prev->getNumOperands(); ++i) {
277       MachineOperand &Op = Prev->getOperand(i);
278       if (!DoReMat && Op.isFI() && Op.getIndex() == SSorRMId)
279         goto stop;
280     }
281     if (Prev->findRegisterUseOperandIdx(PhysReg) != -1 ||
282         Prev->findRegisterDefOperand(PhysReg))
283       goto stop;
284     for (const unsigned *Alias = TRI->getAliasSet(PhysReg); *Alias; ++Alias)
285       if (Prev->findRegisterUseOperandIdx(*Alias) != -1 ||
286           Prev->findRegisterDefOperand(*Alias))
287         goto stop;
288     NewInsertLoc = Prev;
289   }
290 stop:;
291
292   // If we made it to the beginning of the block, turn around and move back
293   // down just past any existing reloads. They're likely to be reloads/remats
294   // for instructions earlier than what our current reload/remat is for, so
295   // they should be scheduled earlier.
296   if (NewInsertLoc == Begin) {
297     int FrameIdx;
298     while (InsertLoc != NewInsertLoc &&
299            (TII->isLoadFromStackSlot(NewInsertLoc, FrameIdx) ||
300             TII->isTriviallyReMaterializable(NewInsertLoc)))
301       ++NewInsertLoc;
302   }
303
304   return NewInsertLoc;
305 }
306
307 namespace {
308
309 // ReusedOp - For each reused operand, we keep track of a bit of information,
310 // in case we need to rollback upon processing a new operand.  See comments
311 // below.
312 struct ReusedOp {
313   // The MachineInstr operand that reused an available value.
314   unsigned Operand;
315
316   // StackSlotOrReMat - The spill slot or remat id of the value being reused.
317   unsigned StackSlotOrReMat;
318
319   // PhysRegReused - The physical register the value was available in.
320   unsigned PhysRegReused;
321
322   // AssignedPhysReg - The physreg that was assigned for use by the reload.
323   unsigned AssignedPhysReg;
324   
325   // VirtReg - The virtual register itself.
326   unsigned VirtReg;
327
328   ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
329            unsigned vreg)
330     : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr),
331       AssignedPhysReg(apr), VirtReg(vreg) {}
332 };
333
334 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
335 /// is reused instead of reloaded.
336 class VISIBILITY_HIDDEN ReuseInfo {
337   MachineInstr &MI;
338   std::vector<ReusedOp> Reuses;
339   BitVector PhysRegsClobbered;
340 public:
341   ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) {
342     PhysRegsClobbered.resize(tri->getNumRegs());
343   }
344   
345   bool hasReuses() const {
346     return !Reuses.empty();
347   }
348   
349   /// addReuse - If we choose to reuse a virtual register that is already
350   /// available instead of reloading it, remember that we did so.
351   void addReuse(unsigned OpNo, unsigned StackSlotOrReMat,
352                 unsigned PhysRegReused, unsigned AssignedPhysReg,
353                 unsigned VirtReg) {
354     // If the reload is to the assigned register anyway, no undo will be
355     // required.
356     if (PhysRegReused == AssignedPhysReg) return;
357     
358     // Otherwise, remember this.
359     Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused, 
360                               AssignedPhysReg, VirtReg));
361   }
362
363   void markClobbered(unsigned PhysReg) {
364     PhysRegsClobbered.set(PhysReg);
365   }
366
367   bool isClobbered(unsigned PhysReg) const {
368     return PhysRegsClobbered.test(PhysReg);
369   }
370   
371   /// GetRegForReload - We are about to emit a reload into PhysReg.  If there
372   /// is some other operand that is using the specified register, either pick
373   /// a new register to use, or evict the previous reload and use this reg. 
374   unsigned GetRegForReload(const TargetRegisterClass *RC, unsigned PhysReg,
375                            MachineFunction &MF, MachineInstr *MI,
376                            AvailableSpills &Spills,
377                            std::vector<MachineInstr*> &MaybeDeadStores,
378                            SmallSet<unsigned, 8> &Rejected,
379                            BitVector &RegKills,
380                            std::vector<MachineOperand*> &KillOps,
381                            VirtRegMap &VRM);
382
383   /// GetRegForReload - Helper for the above GetRegForReload(). Add a
384   /// 'Rejected' set to remember which registers have been considered and
385   /// rejected for the reload. This avoids infinite looping in case like
386   /// this:
387   /// t1 := op t2, t3
388   /// t2 <- assigned r0 for use by the reload but ended up reuse r1
389   /// t3 <- assigned r1 for use by the reload but ended up reuse r0
390   /// t1 <- desires r1
391   ///       sees r1 is taken by t2, tries t2's reload register r0
392   ///       sees r0 is taken by t3, tries t3's reload register r1
393   ///       sees r1 is taken by t2, tries t2's reload register r0 ...
394   unsigned GetRegForReload(unsigned VirtReg, unsigned PhysReg, MachineInstr *MI,
395                            AvailableSpills &Spills,
396                            std::vector<MachineInstr*> &MaybeDeadStores,
397                            BitVector &RegKills,
398                            std::vector<MachineOperand*> &KillOps,
399                            VirtRegMap &VRM) {
400     SmallSet<unsigned, 8> Rejected;
401     MachineFunction &MF = *MI->getParent()->getParent();
402     const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg);
403     return GetRegForReload(RC, PhysReg, MF, MI, Spills, MaybeDeadStores,
404                            Rejected, RegKills, KillOps, VRM);
405   }
406 };
407
408 }
409
410 // ****************** //
411 // Utility Functions  //
412 // ****************** //
413
414 /// findSinglePredSuccessor - Return via reference a vector of machine basic
415 /// blocks each of which is a successor of the specified BB and has no other
416 /// predecessor.
417 static void findSinglePredSuccessor(MachineBasicBlock *MBB,
418                                    SmallVectorImpl<MachineBasicBlock *> &Succs) {
419   for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
420          SE = MBB->succ_end(); SI != SE; ++SI) {
421     MachineBasicBlock *SuccMBB = *SI;
422     if (SuccMBB->pred_size() == 1)
423       Succs.push_back(SuccMBB);
424   }
425 }
426
427 /// InvalidateKill - Invalidate register kill information for a specific
428 /// register. This also unsets the kills marker on the last kill operand.
429 static void InvalidateKill(unsigned Reg,
430                            const TargetRegisterInfo* TRI,
431                            BitVector &RegKills,
432                            std::vector<MachineOperand*> &KillOps) {
433   if (RegKills[Reg]) {
434     KillOps[Reg]->setIsKill(false);
435     // KillOps[Reg] might be a def of a super-register.
436     unsigned KReg = KillOps[Reg]->getReg();
437     KillOps[KReg] = NULL;
438     RegKills.reset(KReg);
439     for (const unsigned *SR = TRI->getSubRegisters(KReg); *SR; ++SR) {
440       if (RegKills[*SR]) {
441         KillOps[*SR]->setIsKill(false);
442         KillOps[*SR] = NULL;
443         RegKills.reset(*SR);
444       }
445     }
446   }
447 }
448
449 /// InvalidateKills - MI is going to be deleted. If any of its operands are
450 /// marked kill, then invalidate the information.
451 static void InvalidateKills(MachineInstr &MI,
452                             const TargetRegisterInfo* TRI,
453                             BitVector &RegKills,
454                             std::vector<MachineOperand*> &KillOps,
455                             SmallVector<unsigned, 2> *KillRegs = NULL) {
456   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
457     MachineOperand &MO = MI.getOperand(i);
458     if (!MO.isReg() || !MO.isUse() || !MO.isKill() || MO.isUndef())
459       continue;
460     unsigned Reg = MO.getReg();
461     if (TargetRegisterInfo::isVirtualRegister(Reg))
462       continue;
463     if (KillRegs)
464       KillRegs->push_back(Reg);
465     assert(Reg < KillOps.size());
466     if (KillOps[Reg] == &MO) {
467       KillOps[Reg] = NULL;
468       RegKills.reset(Reg);
469       for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) {
470         if (RegKills[*SR]) {
471           KillOps[*SR] = NULL;
472           RegKills.reset(*SR);
473         }
474       }
475     }
476   }
477 }
478
479 /// InvalidateRegDef - If the def operand of the specified def MI is now dead
480 /// (since it's spill instruction is removed), mark it isDead. Also checks if
481 /// the def MI has other definition operands that are not dead. Returns it by
482 /// reference.
483 static bool InvalidateRegDef(MachineBasicBlock::iterator I,
484                              MachineInstr &NewDef, unsigned Reg,
485                              bool &HasLiveDef) {
486   // Due to remat, it's possible this reg isn't being reused. That is,
487   // the def of this reg (by prev MI) is now dead.
488   MachineInstr *DefMI = I;
489   MachineOperand *DefOp = NULL;
490   for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) {
491     MachineOperand &MO = DefMI->getOperand(i);
492     if (!MO.isReg() || !MO.isUse() || !MO.isKill() || MO.isUndef())
493       continue;
494     if (MO.getReg() == Reg)
495       DefOp = &MO;
496     else if (!MO.isDead())
497       HasLiveDef = true;
498   }
499   if (!DefOp)
500     return false;
501
502   bool FoundUse = false, Done = false;
503   MachineBasicBlock::iterator E = &NewDef;
504   ++I; ++E;
505   for (; !Done && I != E; ++I) {
506     MachineInstr *NMI = I;
507     for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) {
508       MachineOperand &MO = NMI->getOperand(j);
509       if (!MO.isReg() || MO.getReg() != Reg)
510         continue;
511       if (MO.isUse())
512         FoundUse = true;
513       Done = true; // Stop after scanning all the operands of this MI.
514     }
515   }
516   if (!FoundUse) {
517     // Def is dead!
518     DefOp->setIsDead();
519     return true;
520   }
521   return false;
522 }
523
524 /// UpdateKills - Track and update kill info. If a MI reads a register that is
525 /// marked kill, then it must be due to register reuse. Transfer the kill info
526 /// over.
527 static void UpdateKills(MachineInstr &MI, const TargetRegisterInfo* TRI,
528                         BitVector &RegKills,
529                         std::vector<MachineOperand*> &KillOps) {
530   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
531     MachineOperand &MO = MI.getOperand(i);
532     if (!MO.isReg() || !MO.isUse() || MO.isUndef())
533       continue;
534     unsigned Reg = MO.getReg();
535     if (Reg == 0)
536       continue;
537     
538     if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) {
539       // That can't be right. Register is killed but not re-defined and it's
540       // being reused. Let's fix that.
541       KillOps[Reg]->setIsKill(false);
542       // KillOps[Reg] might be a def of a super-register.
543       unsigned KReg = KillOps[Reg]->getReg();
544       KillOps[KReg] = NULL;
545       RegKills.reset(KReg);
546
547       // Must be a def of a super-register. Its other sub-regsters are no
548       // longer killed as well.
549       for (const unsigned *SR = TRI->getSubRegisters(KReg); *SR; ++SR) {
550         KillOps[*SR] = NULL;
551         RegKills.reset(*SR);
552       }
553
554       if (!MI.isRegTiedToDefOperand(i))
555         // Unless it's a two-address operand, this is the new kill.
556         MO.setIsKill();
557     }
558     if (MO.isKill()) {
559       RegKills.set(Reg);
560       KillOps[Reg] = &MO;
561       for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) {
562         RegKills.set(*SR);
563         KillOps[*SR] = &MO;
564       }
565     }
566   }
567
568   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
569     const MachineOperand &MO = MI.getOperand(i);
570     if (!MO.isReg() || !MO.isDef())
571       continue;
572     unsigned Reg = MO.getReg();
573     RegKills.reset(Reg);
574     KillOps[Reg] = NULL;
575     // It also defines (or partially define) aliases.
576     for (const unsigned *SR = TRI->getSubRegisters(Reg); *SR; ++SR) {
577       RegKills.reset(*SR);
578       KillOps[*SR] = NULL;
579     }
580   }
581 }
582
583 /// ReMaterialize - Re-materialize definition for Reg targetting DestReg.
584 ///
585 static void ReMaterialize(MachineBasicBlock &MBB,
586                           MachineBasicBlock::iterator &MII,
587                           unsigned DestReg, unsigned Reg,
588                           const TargetInstrInfo *TII,
589                           const TargetRegisterInfo *TRI,
590                           VirtRegMap &VRM) {
591   MachineInstr *ReMatDefMI = VRM.getReMaterializedMI(Reg);
592 #ifndef NDEBUG
593   const TargetInstrDesc &TID = ReMatDefMI->getDesc();
594   assert(TID.getNumDefs() == 1 &&
595          "Don't know how to remat instructions that define > 1 values!");
596 #endif
597   TII->reMaterialize(MBB, MII, DestReg,
598                      ReMatDefMI->getOperand(0).getSubReg(), ReMatDefMI);
599   MachineInstr *NewMI = prior(MII);
600   for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
601     MachineOperand &MO = NewMI->getOperand(i);
602     if (!MO.isReg() || MO.getReg() == 0)
603       continue;
604     unsigned VirtReg = MO.getReg();
605     if (TargetRegisterInfo::isPhysicalRegister(VirtReg))
606       continue;
607     assert(MO.isUse());
608     unsigned SubIdx = MO.getSubReg();
609     unsigned Phys = VRM.getPhys(VirtReg);
610     assert(Phys);
611     unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
612     MO.setReg(RReg);
613     MO.setSubReg(0);
614   }
615   ++NumReMats;
616 }
617
618 /// findSuperReg - Find the SubReg's super-register of given register class
619 /// where its SubIdx sub-register is SubReg.
620 static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg,
621                              unsigned SubIdx, const TargetRegisterInfo *TRI) {
622   for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
623        I != E; ++I) {
624     unsigned Reg = *I;
625     if (TRI->getSubReg(Reg, SubIdx) == SubReg)
626       return Reg;
627   }
628   return 0;
629 }
630
631 // ******************************** //
632 // Available Spills Implementation  //
633 // ******************************** //
634
635 /// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified
636 /// stackslot register. The register is still available but is no longer
637 /// allowed to be modifed.
638 void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) {
639   std::multimap<unsigned, int>::iterator I =
640     PhysRegsAvailable.lower_bound(PhysReg);
641   while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
642     int SlotOrReMat = I->second;
643     I++;
644     assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
645            "Bidirectional map mismatch!");
646     SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1;
647     DOUT << "PhysReg " << TRI->getName(PhysReg)
648          << " copied, it is available for use but can no longer be modified\n";
649   }
650 }
651
652 /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
653 /// stackslot register and its aliases. The register and its aliases may
654 /// still available but is no longer allowed to be modifed.
655 void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) {
656   for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
657     disallowClobberPhysRegOnly(*AS);
658   disallowClobberPhysRegOnly(PhysReg);
659 }
660
661 /// ClobberPhysRegOnly - This is called when the specified physreg changes
662 /// value.  We use this to invalidate any info about stuff we thing lives in it.
663 void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
664   std::multimap<unsigned, int>::iterator I =
665     PhysRegsAvailable.lower_bound(PhysReg);
666   while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
667     int SlotOrReMat = I->second;
668     PhysRegsAvailable.erase(I++);
669     assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
670            "Bidirectional map mismatch!");
671     SpillSlotsOrReMatsAvailable.erase(SlotOrReMat);
672     DOUT << "PhysReg " << TRI->getName(PhysReg)
673          << " clobbered, invalidating ";
674     if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
675       DOUT << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 << "\n";
676     else
677       DOUT << "SS#" << SlotOrReMat << "\n";
678   }
679 }
680
681 /// ClobberPhysReg - This is called when the specified physreg changes
682 /// value.  We use this to invalidate any info about stuff we thing lives in
683 /// it and any of its aliases.
684 void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
685   for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
686     ClobberPhysRegOnly(*AS);
687   ClobberPhysRegOnly(PhysReg);
688 }
689
690 /// AddAvailableRegsToLiveIn - Availability information is being kept coming
691 /// into the specified MBB. Add available physical registers as potential
692 /// live-in's. If they are reused in the MBB, they will be added to the
693 /// live-in set to make register scavenger and post-allocation scheduler.
694 void AvailableSpills::AddAvailableRegsToLiveIn(MachineBasicBlock &MBB,
695                                         BitVector &RegKills,
696                                         std::vector<MachineOperand*> &KillOps) {
697   std::set<unsigned> NotAvailable;
698   for (std::multimap<unsigned, int>::iterator
699          I = PhysRegsAvailable.begin(), E = PhysRegsAvailable.end();
700        I != E; ++I) {
701     unsigned Reg = I->first;
702     const TargetRegisterClass* RC = TRI->getPhysicalRegisterRegClass(Reg);
703     // FIXME: A temporary workaround. We can't reuse available value if it's
704     // not safe to move the def of the virtual register's class. e.g.
705     // X86::RFP* register classes. Do not add it as a live-in.
706     if (!TII->isSafeToMoveRegClassDefs(RC))
707       // This is no longer available.
708       NotAvailable.insert(Reg);
709     else {
710       MBB.addLiveIn(Reg);
711       InvalidateKill(Reg, TRI, RegKills, KillOps);
712     }
713
714     // Skip over the same register.
715     std::multimap<unsigned, int>::iterator NI = next(I);
716     while (NI != E && NI->first == Reg) {
717       ++I;
718       ++NI;
719     }
720   }
721
722   for (std::set<unsigned>::iterator I = NotAvailable.begin(),
723          E = NotAvailable.end(); I != E; ++I) {
724     ClobberPhysReg(*I);
725     for (const unsigned *SubRegs = TRI->getSubRegisters(*I);
726        *SubRegs; ++SubRegs)
727       ClobberPhysReg(*SubRegs);
728   }
729 }
730
731 /// ModifyStackSlotOrReMat - This method is called when the value in a stack
732 /// slot changes.  This removes information about which register the previous
733 /// value for this slot lives in (as the previous value is dead now).
734 void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) {
735   std::map<int, unsigned>::iterator It =
736     SpillSlotsOrReMatsAvailable.find(SlotOrReMat);
737   if (It == SpillSlotsOrReMatsAvailable.end()) return;
738   unsigned Reg = It->second >> 1;
739   SpillSlotsOrReMatsAvailable.erase(It);
740   
741   // This register may hold the value of multiple stack slots, only remove this
742   // stack slot from the set of values the register contains.
743   std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
744   for (; ; ++I) {
745     assert(I != PhysRegsAvailable.end() && I->first == Reg &&
746            "Map inverse broken!");
747     if (I->second == SlotOrReMat) break;
748   }
749   PhysRegsAvailable.erase(I);
750 }
751
752 // ************************** //
753 // Reuse Info Implementation  //
754 // ************************** //
755
756 /// GetRegForReload - We are about to emit a reload into PhysReg.  If there
757 /// is some other operand that is using the specified register, either pick
758 /// a new register to use, or evict the previous reload and use this reg.
759 unsigned ReuseInfo::GetRegForReload(const TargetRegisterClass *RC,
760                          unsigned PhysReg,
761                          MachineFunction &MF,
762                          MachineInstr *MI, AvailableSpills &Spills,
763                          std::vector<MachineInstr*> &MaybeDeadStores,
764                          SmallSet<unsigned, 8> &Rejected,
765                          BitVector &RegKills,
766                          std::vector<MachineOperand*> &KillOps,
767                          VirtRegMap &VRM) {
768   const TargetInstrInfo* TII = MF.getTarget().getInstrInfo();
769   const TargetRegisterInfo *TRI = Spills.getRegInfo();
770   
771   if (Reuses.empty()) return PhysReg;  // This is most often empty.
772
773   for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
774     ReusedOp &Op = Reuses[ro];
775     // If we find some other reuse that was supposed to use this register
776     // exactly for its reload, we can change this reload to use ITS reload
777     // register. That is, unless its reload register has already been
778     // considered and subsequently rejected because it has also been reused
779     // by another operand.
780     if (Op.PhysRegReused == PhysReg &&
781         Rejected.count(Op.AssignedPhysReg) == 0 &&
782         RC->contains(Op.AssignedPhysReg)) {
783       // Yup, use the reload register that we didn't use before.
784       unsigned NewReg = Op.AssignedPhysReg;
785       Rejected.insert(PhysReg);
786       return GetRegForReload(RC, NewReg, MF, MI, Spills, MaybeDeadStores, Rejected,
787                              RegKills, KillOps, VRM);
788     } else {
789       // Otherwise, we might also have a problem if a previously reused
790       // value aliases the new register. If so, codegen the previous reload
791       // and use this one.          
792       unsigned PRRU = Op.PhysRegReused;
793       if (TRI->areAliases(PRRU, PhysReg)) {
794         // Okay, we found out that an alias of a reused register
795         // was used.  This isn't good because it means we have
796         // to undo a previous reuse.
797         MachineBasicBlock *MBB = MI->getParent();
798         const TargetRegisterClass *AliasRC =
799           MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg);
800
801         // Copy Op out of the vector and remove it, we're going to insert an
802         // explicit load for it.
803         ReusedOp NewOp = Op;
804         Reuses.erase(Reuses.begin()+ro);
805
806         // Ok, we're going to try to reload the assigned physreg into the
807         // slot that we were supposed to in the first place.  However, that
808         // register could hold a reuse.  Check to see if it conflicts or
809         // would prefer us to use a different register.
810         unsigned NewPhysReg = GetRegForReload(RC, NewOp.AssignedPhysReg,
811                                               MF, MI, Spills, MaybeDeadStores,
812                                               Rejected, RegKills, KillOps, VRM);
813
814         bool DoReMat = NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT;
815         int SSorRMId = DoReMat
816           ? VRM.getReMatId(NewOp.VirtReg) : NewOp.StackSlotOrReMat;
817
818         // Back-schedule reloads and remats.
819         MachineBasicBlock::iterator InsertLoc =
820           ComputeReloadLoc(MI, MBB->begin(), PhysReg, TRI,
821                            DoReMat, SSorRMId, TII, MF);
822
823         if (DoReMat) {
824           ReMaterialize(*MBB, InsertLoc, NewPhysReg, NewOp.VirtReg, TII,
825                         TRI, VRM);
826         } else { 
827           TII->loadRegFromStackSlot(*MBB, InsertLoc, NewPhysReg,
828                                     NewOp.StackSlotOrReMat, AliasRC);
829           MachineInstr *LoadMI = prior(InsertLoc);
830           VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI);
831           // Any stores to this stack slot are not dead anymore.
832           MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL;            
833           ++NumLoads;
834         }
835         Spills.ClobberPhysReg(NewPhysReg);
836         Spills.ClobberPhysReg(NewOp.PhysRegReused);
837
838         unsigned SubIdx = MI->getOperand(NewOp.Operand).getSubReg();
839         unsigned RReg = SubIdx ? TRI->getSubReg(NewPhysReg, SubIdx) : NewPhysReg;
840         MI->getOperand(NewOp.Operand).setReg(RReg);
841         MI->getOperand(NewOp.Operand).setSubReg(0);
842
843         Spills.addAvailable(NewOp.StackSlotOrReMat, NewPhysReg);
844         UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps);
845         DOUT << '\t' << *prior(InsertLoc);
846         
847         DOUT << "Reuse undone!\n";
848         --NumReused;
849         
850         // Finally, PhysReg is now available, go ahead and use it.
851         return PhysReg;
852       }
853     }
854   }
855   return PhysReg;
856 }
857
858 // ************************************************************************ //
859
860 /// FoldsStackSlotModRef - Return true if the specified MI folds the specified
861 /// stack slot mod/ref. It also checks if it's possible to unfold the
862 /// instruction by having it define a specified physical register instead.
863 static bool FoldsStackSlotModRef(MachineInstr &MI, int SS, unsigned PhysReg,
864                                  const TargetInstrInfo *TII,
865                                  const TargetRegisterInfo *TRI,
866                                  VirtRegMap &VRM) {
867   if (VRM.hasEmergencySpills(&MI) || VRM.isSpillPt(&MI))
868     return false;
869
870   bool Found = false;
871   VirtRegMap::MI2VirtMapTy::const_iterator I, End;
872   for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
873     unsigned VirtReg = I->second.first;
874     VirtRegMap::ModRef MR = I->second.second;
875     if (MR & VirtRegMap::isModRef)
876       if (VRM.getStackSlot(VirtReg) == SS) {
877         Found= TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), true, true) != 0;
878         break;
879       }
880   }
881   if (!Found)
882     return false;
883
884   // Does the instruction uses a register that overlaps the scratch register?
885   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
886     MachineOperand &MO = MI.getOperand(i);
887     if (!MO.isReg() || MO.getReg() == 0)
888       continue;
889     unsigned Reg = MO.getReg();
890     if (TargetRegisterInfo::isVirtualRegister(Reg)) {
891       if (!VRM.hasPhys(Reg))
892         continue;
893       Reg = VRM.getPhys(Reg);
894     }
895     if (TRI->regsOverlap(PhysReg, Reg))
896       return false;
897   }
898   return true;
899 }
900
901 /// FindFreeRegister - Find a free register of a given register class by looking
902 /// at (at most) the last two machine instructions.
903 static unsigned FindFreeRegister(MachineBasicBlock::iterator MII,
904                                  MachineBasicBlock &MBB,
905                                  const TargetRegisterClass *RC,
906                                  const TargetRegisterInfo *TRI,
907                                  BitVector &AllocatableRegs) {
908   BitVector Defs(TRI->getNumRegs());
909   BitVector Uses(TRI->getNumRegs());
910   SmallVector<unsigned, 4> LocalUses;
911   SmallVector<unsigned, 4> Kills;
912
913   // Take a look at 2 instructions at most.
914   for (unsigned Count = 0; Count < 2; ++Count) {
915     if (MII == MBB.begin())
916       break;
917     MachineInstr *PrevMI = prior(MII);
918     for (unsigned i = 0, e = PrevMI->getNumOperands(); i != e; ++i) {
919       MachineOperand &MO = PrevMI->getOperand(i);
920       if (!MO.isReg() || MO.getReg() == 0)
921         continue;
922       unsigned Reg = MO.getReg();
923       if (MO.isDef()) {
924         Defs.set(Reg);
925         for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
926           Defs.set(*AS);
927       } else  {
928         LocalUses.push_back(Reg);
929         if (MO.isKill() && AllocatableRegs[Reg])
930           Kills.push_back(Reg);
931       }
932     }
933
934     for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
935       unsigned Kill = Kills[i];
936       if (!Defs[Kill] && !Uses[Kill] &&
937           TRI->getPhysicalRegisterRegClass(Kill) == RC)
938         return Kill;
939     }
940     for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) {
941       unsigned Reg = LocalUses[i];
942       Uses.set(Reg);
943       for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
944         Uses.set(*AS);
945     }
946
947     MII = PrevMI;
948   }
949
950   return 0;
951 }
952
953 static
954 void AssignPhysToVirtReg(MachineInstr *MI, unsigned VirtReg, unsigned PhysReg) {
955   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
956     MachineOperand &MO = MI->getOperand(i);
957     if (MO.isReg() && MO.getReg() == VirtReg)
958       MO.setReg(PhysReg);
959   }
960 }
961
962 namespace {
963   struct RefSorter {
964     bool operator()(const std::pair<MachineInstr*, int> &A,
965                     const std::pair<MachineInstr*, int> &B) {
966       return A.second < B.second;
967     }
968   };
969 }
970
971 // ***************************** //
972 // Local Spiller Implementation  //
973 // ***************************** //
974
975 namespace {
976
977 class VISIBILITY_HIDDEN LocalRewriter : public VirtRegRewriter {
978   MachineRegisterInfo *RegInfo;
979   const TargetRegisterInfo *TRI;
980   const TargetInstrInfo *TII;
981   BitVector AllocatableRegs;
982   DenseMap<MachineInstr*, unsigned> DistanceMap;
983 public:
984
985   bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM,
986                             LiveIntervals* LIs) {
987     RegInfo = &MF.getRegInfo(); 
988     TRI = MF.getTarget().getRegisterInfo();
989     TII = MF.getTarget().getInstrInfo();
990     AllocatableRegs = TRI->getAllocatableSet(MF);
991     DEBUG(errs() << "\n**** Local spiller rewriting function '"
992           << MF.getFunction()->getName() << "':\n");
993     DOUT << "**** Machine Instrs (NOTE! Does not include spills and reloads!)"
994             " ****\n";
995     DEBUG(MF.dump());
996
997     // Spills - Keep track of which spilled values are available in physregs
998     // so that we can choose to reuse the physregs instead of emitting
999     // reloads. This is usually refreshed per basic block.
1000     AvailableSpills Spills(TRI, TII);
1001
1002     // Keep track of kill information.
1003     BitVector RegKills(TRI->getNumRegs());
1004     std::vector<MachineOperand*> KillOps;
1005     KillOps.resize(TRI->getNumRegs(), NULL);
1006
1007     // SingleEntrySuccs - Successor blocks which have a single predecessor.
1008     SmallVector<MachineBasicBlock*, 4> SinglePredSuccs;
1009     SmallPtrSet<MachineBasicBlock*,16> EarlyVisited;
1010
1011     // Traverse the basic blocks depth first.
1012     MachineBasicBlock *Entry = MF.begin();
1013     SmallPtrSet<MachineBasicBlock*,16> Visited;
1014     for (df_ext_iterator<MachineBasicBlock*,
1015            SmallPtrSet<MachineBasicBlock*,16> >
1016            DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1017          DFI != E; ++DFI) {
1018       MachineBasicBlock *MBB = *DFI;
1019       if (!EarlyVisited.count(MBB))
1020         RewriteMBB(*MBB, VRM, LIs, Spills, RegKills, KillOps);
1021
1022       // If this MBB is the only predecessor of a successor. Keep the
1023       // availability information and visit it next.
1024       do {
1025         // Keep visiting single predecessor successor as long as possible.
1026         SinglePredSuccs.clear();
1027         findSinglePredSuccessor(MBB, SinglePredSuccs);
1028         if (SinglePredSuccs.empty())
1029           MBB = 0;
1030         else {
1031           // FIXME: More than one successors, each of which has MBB has
1032           // the only predecessor.
1033           MBB = SinglePredSuccs[0];
1034           if (!Visited.count(MBB) && EarlyVisited.insert(MBB)) {
1035             Spills.AddAvailableRegsToLiveIn(*MBB, RegKills, KillOps);
1036             RewriteMBB(*MBB, VRM, LIs, Spills, RegKills, KillOps);
1037           }
1038         }
1039       } while (MBB);
1040
1041       // Clear the availability info.
1042       Spills.clear();
1043     }
1044
1045     DOUT << "**** Post Machine Instrs ****\n";
1046     DEBUG(MF.dump());
1047
1048     // Mark unused spill slots.
1049     MachineFrameInfo *MFI = MF.getFrameInfo();
1050     int SS = VRM.getLowSpillSlot();
1051     if (SS != VirtRegMap::NO_STACK_SLOT)
1052       for (int e = VRM.getHighSpillSlot(); SS <= e; ++SS)
1053         if (!VRM.isSpillSlotUsed(SS)) {
1054           MFI->RemoveStackObject(SS);
1055           ++NumDSS;
1056         }
1057
1058     return true;
1059   }
1060
1061 private:
1062
1063   /// OptimizeByUnfold2 - Unfold a series of load / store folding instructions if
1064   /// a scratch register is available.
1065   ///     xorq  %r12<kill>, %r13
1066   ///     addq  %rax, -184(%rbp)
1067   ///     addq  %r13, -184(%rbp)
1068   /// ==>
1069   ///     xorq  %r12<kill>, %r13
1070   ///     movq  -184(%rbp), %r12
1071   ///     addq  %rax, %r12
1072   ///     addq  %r13, %r12
1073   ///     movq  %r12, -184(%rbp)
1074   bool OptimizeByUnfold2(unsigned VirtReg, int SS,
1075                          MachineBasicBlock &MBB,
1076                          MachineBasicBlock::iterator &MII,
1077                          std::vector<MachineInstr*> &MaybeDeadStores,
1078                          AvailableSpills &Spills,
1079                          BitVector &RegKills,
1080                          std::vector<MachineOperand*> &KillOps,
1081                          VirtRegMap &VRM) {
1082
1083     MachineBasicBlock::iterator NextMII = next(MII);
1084     if (NextMII == MBB.end())
1085       return false;
1086
1087     if (TII->getOpcodeAfterMemoryUnfold(MII->getOpcode(), true, true) == 0)
1088       return false;
1089
1090     // Now let's see if the last couple of instructions happens to have freed up
1091     // a register.
1092     const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1093     unsigned PhysReg = FindFreeRegister(MII, MBB, RC, TRI, AllocatableRegs);
1094     if (!PhysReg)
1095       return false;
1096
1097     MachineFunction &MF = *MBB.getParent();
1098     TRI = MF.getTarget().getRegisterInfo();
1099     MachineInstr &MI = *MII;
1100     if (!FoldsStackSlotModRef(MI, SS, PhysReg, TII, TRI, VRM))
1101       return false;
1102
1103     // If the next instruction also folds the same SS modref and can be unfoled,
1104     // then it's worthwhile to issue a load from SS into the free register and
1105     // then unfold these instructions.
1106     if (!FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, VRM))
1107       return false;
1108
1109     // Back-schedule reloads and remats.
1110     MachineBasicBlock::iterator InsertLoc =
1111       ComputeReloadLoc(MII, MBB.begin(), PhysReg, TRI, false, SS, TII, MF);
1112
1113     // Load from SS to the spare physical register.
1114     TII->loadRegFromStackSlot(MBB, MII, PhysReg, SS, RC);
1115     // This invalidates Phys.
1116     Spills.ClobberPhysReg(PhysReg);
1117     // Remember it's available.
1118     Spills.addAvailable(SS, PhysReg);
1119     MaybeDeadStores[SS] = NULL;
1120
1121     // Unfold current MI.
1122     SmallVector<MachineInstr*, 4> NewMIs;
1123     if (!TII->unfoldMemoryOperand(MF, &MI, VirtReg, false, false, NewMIs))
1124       llvm_unreachable("Unable unfold the load / store folding instruction!");
1125     assert(NewMIs.size() == 1);
1126     AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg);
1127     VRM.transferRestorePts(&MI, NewMIs[0]);
1128     MII = MBB.insert(MII, NewMIs[0]);
1129     InvalidateKills(MI, TRI, RegKills, KillOps);
1130     VRM.RemoveMachineInstrFromMaps(&MI);
1131     MBB.erase(&MI);
1132     ++NumModRefUnfold;
1133
1134     // Unfold next instructions that fold the same SS.
1135     do {
1136       MachineInstr &NextMI = *NextMII;
1137       NextMII = next(NextMII);
1138       NewMIs.clear();
1139       if (!TII->unfoldMemoryOperand(MF, &NextMI, VirtReg, false, false, NewMIs))
1140         llvm_unreachable("Unable unfold the load / store folding instruction!");
1141       assert(NewMIs.size() == 1);
1142       AssignPhysToVirtReg(NewMIs[0], VirtReg, PhysReg);
1143       VRM.transferRestorePts(&NextMI, NewMIs[0]);
1144       MBB.insert(NextMII, NewMIs[0]);
1145       InvalidateKills(NextMI, TRI, RegKills, KillOps);
1146       VRM.RemoveMachineInstrFromMaps(&NextMI);
1147       MBB.erase(&NextMI);
1148       ++NumModRefUnfold;
1149       if (NextMII == MBB.end())
1150         break;
1151     } while (FoldsStackSlotModRef(*NextMII, SS, PhysReg, TII, TRI, VRM));
1152
1153     // Store the value back into SS.
1154     TII->storeRegToStackSlot(MBB, NextMII, PhysReg, true, SS, RC);
1155     MachineInstr *StoreMI = prior(NextMII);
1156     VRM.addSpillSlotUse(SS, StoreMI);
1157     VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
1158
1159     return true;
1160   }
1161
1162   /// OptimizeByUnfold - Turn a store folding instruction into a load folding
1163   /// instruction. e.g.
1164   ///     xorl  %edi, %eax
1165   ///     movl  %eax, -32(%ebp)
1166   ///     movl  -36(%ebp), %eax
1167   ///     orl   %eax, -32(%ebp)
1168   /// ==>
1169   ///     xorl  %edi, %eax
1170   ///     orl   -36(%ebp), %eax
1171   ///     mov   %eax, -32(%ebp)
1172   /// This enables unfolding optimization for a subsequent instruction which will
1173   /// also eliminate the newly introduced store instruction.
1174   bool OptimizeByUnfold(MachineBasicBlock &MBB,
1175                         MachineBasicBlock::iterator &MII,
1176                         std::vector<MachineInstr*> &MaybeDeadStores,
1177                         AvailableSpills &Spills,
1178                         BitVector &RegKills,
1179                         std::vector<MachineOperand*> &KillOps,
1180                         VirtRegMap &VRM) {
1181     MachineFunction &MF = *MBB.getParent();
1182     MachineInstr &MI = *MII;
1183     unsigned UnfoldedOpc = 0;
1184     unsigned UnfoldPR = 0;
1185     unsigned UnfoldVR = 0;
1186     int FoldedSS = VirtRegMap::NO_STACK_SLOT;
1187     VirtRegMap::MI2VirtMapTy::const_iterator I, End;
1188     for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
1189       // Only transform a MI that folds a single register.
1190       if (UnfoldedOpc)
1191         return false;
1192       UnfoldVR = I->second.first;
1193       VirtRegMap::ModRef MR = I->second.second;
1194       // MI2VirtMap be can updated which invalidate the iterator.
1195       // Increment the iterator first.
1196       ++I; 
1197       if (VRM.isAssignedReg(UnfoldVR))
1198         continue;
1199       // If this reference is not a use, any previous store is now dead.
1200       // Otherwise, the store to this stack slot is not dead anymore.
1201       FoldedSS = VRM.getStackSlot(UnfoldVR);
1202       MachineInstr* DeadStore = MaybeDeadStores[FoldedSS];
1203       if (DeadStore && (MR & VirtRegMap::isModRef)) {
1204         unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS);
1205         if (!PhysReg || !DeadStore->readsRegister(PhysReg))
1206           continue;
1207         UnfoldPR = PhysReg;
1208         UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
1209                                                       false, true);
1210       }
1211     }
1212
1213     if (!UnfoldedOpc) {
1214       if (!UnfoldVR)
1215         return false;
1216
1217       // Look for other unfolding opportunities.
1218       return OptimizeByUnfold2(UnfoldVR, FoldedSS, MBB, MII,
1219                                MaybeDeadStores, Spills, RegKills, KillOps, VRM);
1220     }
1221
1222     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1223       MachineOperand &MO = MI.getOperand(i);
1224       if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse())
1225         continue;
1226       unsigned VirtReg = MO.getReg();
1227       if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg())
1228         continue;
1229       if (VRM.isAssignedReg(VirtReg)) {
1230         unsigned PhysReg = VRM.getPhys(VirtReg);
1231         if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR))
1232           return false;
1233       } else if (VRM.isReMaterialized(VirtReg))
1234         continue;
1235       int SS = VRM.getStackSlot(VirtReg);
1236       unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1237       if (PhysReg) {
1238         if (TRI->regsOverlap(PhysReg, UnfoldPR))
1239           return false;
1240         continue;
1241       }
1242       if (VRM.hasPhys(VirtReg)) {
1243         PhysReg = VRM.getPhys(VirtReg);
1244         if (!TRI->regsOverlap(PhysReg, UnfoldPR))
1245           continue;
1246       }
1247
1248       // Ok, we'll need to reload the value into a register which makes
1249       // it impossible to perform the store unfolding optimization later.
1250       // Let's see if it is possible to fold the load if the store is
1251       // unfolded. This allows us to perform the store unfolding
1252       // optimization.
1253       SmallVector<MachineInstr*, 4> NewMIs;
1254       if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) {
1255         assert(NewMIs.size() == 1);
1256         MachineInstr *NewMI = NewMIs.back();
1257         NewMIs.clear();
1258         int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false);
1259         assert(Idx != -1);
1260         SmallVector<unsigned, 1> Ops;
1261         Ops.push_back(Idx);
1262         MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, NewMI, Ops, SS);
1263         if (FoldedMI) {
1264           VRM.addSpillSlotUse(SS, FoldedMI);
1265           if (!VRM.hasPhys(UnfoldVR))
1266             VRM.assignVirt2Phys(UnfoldVR, UnfoldPR);
1267           VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
1268           MII = MBB.insert(MII, FoldedMI);
1269           InvalidateKills(MI, TRI, RegKills, KillOps);
1270           VRM.RemoveMachineInstrFromMaps(&MI);
1271           MBB.erase(&MI);
1272           MF.DeleteMachineInstr(NewMI);
1273           return true;
1274         }
1275         MF.DeleteMachineInstr(NewMI);
1276       }
1277     }
1278
1279     return false;
1280   }
1281
1282   /// CommuteChangesDestination - We are looking for r0 = op r1, r2 and
1283   /// where SrcReg is r1 and it is tied to r0. Return true if after
1284   /// commuting this instruction it will be r0 = op r2, r1.
1285   static bool CommuteChangesDestination(MachineInstr *DefMI,
1286                                         const TargetInstrDesc &TID,
1287                                         unsigned SrcReg,
1288                                         const TargetInstrInfo *TII,
1289                                         unsigned &DstIdx) {
1290     if (TID.getNumDefs() != 1 && TID.getNumOperands() != 3)
1291       return false;
1292     if (!DefMI->getOperand(1).isReg() ||
1293         DefMI->getOperand(1).getReg() != SrcReg)
1294       return false;
1295     unsigned DefIdx;
1296     if (!DefMI->isRegTiedToDefOperand(1, &DefIdx) || DefIdx != 0)
1297       return false;
1298     unsigned SrcIdx1, SrcIdx2;
1299     if (!TII->findCommutedOpIndices(DefMI, SrcIdx1, SrcIdx2))
1300       return false;
1301     if (SrcIdx1 == 1 && SrcIdx2 == 2) {
1302       DstIdx = 2;
1303       return true;
1304     }
1305     return false;
1306   }
1307
1308   /// CommuteToFoldReload -
1309   /// Look for
1310   /// r1 = load fi#1
1311   /// r1 = op r1, r2<kill>
1312   /// store r1, fi#1
1313   ///
1314   /// If op is commutable and r2 is killed, then we can xform these to
1315   /// r2 = op r2, fi#1
1316   /// store r2, fi#1
1317   bool CommuteToFoldReload(MachineBasicBlock &MBB,
1318                            MachineBasicBlock::iterator &MII,
1319                            unsigned VirtReg, unsigned SrcReg, int SS,
1320                            AvailableSpills &Spills,
1321                            BitVector &RegKills,
1322                            std::vector<MachineOperand*> &KillOps,
1323                            const TargetRegisterInfo *TRI,
1324                            VirtRegMap &VRM) {
1325     if (MII == MBB.begin() || !MII->killsRegister(SrcReg))
1326       return false;
1327
1328     MachineFunction &MF = *MBB.getParent();
1329     MachineInstr &MI = *MII;
1330     MachineBasicBlock::iterator DefMII = prior(MII);
1331     MachineInstr *DefMI = DefMII;
1332     const TargetInstrDesc &TID = DefMI->getDesc();
1333     unsigned NewDstIdx;
1334     if (DefMII != MBB.begin() &&
1335         TID.isCommutable() &&
1336         CommuteChangesDestination(DefMI, TID, SrcReg, TII, NewDstIdx)) {
1337       MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
1338       unsigned NewReg = NewDstMO.getReg();
1339       if (!NewDstMO.isKill() || TRI->regsOverlap(NewReg, SrcReg))
1340         return false;
1341       MachineInstr *ReloadMI = prior(DefMII);
1342       int FrameIdx;
1343       unsigned DestReg = TII->isLoadFromStackSlot(ReloadMI, FrameIdx);
1344       if (DestReg != SrcReg || FrameIdx != SS)
1345         return false;
1346       int UseIdx = DefMI->findRegisterUseOperandIdx(DestReg, false);
1347       if (UseIdx == -1)
1348         return false;
1349       unsigned DefIdx;
1350       if (!MI.isRegTiedToDefOperand(UseIdx, &DefIdx))
1351         return false;
1352       assert(DefMI->getOperand(DefIdx).isReg() &&
1353              DefMI->getOperand(DefIdx).getReg() == SrcReg);
1354
1355       // Now commute def instruction.
1356       MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true);
1357       if (!CommutedMI)
1358         return false;
1359       SmallVector<unsigned, 1> Ops;
1360       Ops.push_back(NewDstIdx);
1361       MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS);
1362       // Not needed since foldMemoryOperand returns new MI.
1363       MF.DeleteMachineInstr(CommutedMI);
1364       if (!FoldedMI)
1365         return false;
1366
1367       VRM.addSpillSlotUse(SS, FoldedMI);
1368       VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
1369       // Insert new def MI and spill MI.
1370       const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1371       TII->storeRegToStackSlot(MBB, &MI, NewReg, true, SS, RC);
1372       MII = prior(MII);
1373       MachineInstr *StoreMI = MII;
1374       VRM.addSpillSlotUse(SS, StoreMI);
1375       VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
1376       MII = MBB.insert(MII, FoldedMI);  // Update MII to backtrack.
1377
1378       // Delete all 3 old instructions.
1379       InvalidateKills(*ReloadMI, TRI, RegKills, KillOps);
1380       VRM.RemoveMachineInstrFromMaps(ReloadMI);
1381       MBB.erase(ReloadMI);
1382       InvalidateKills(*DefMI, TRI, RegKills, KillOps);
1383       VRM.RemoveMachineInstrFromMaps(DefMI);
1384       MBB.erase(DefMI);
1385       InvalidateKills(MI, TRI, RegKills, KillOps);
1386       VRM.RemoveMachineInstrFromMaps(&MI);
1387       MBB.erase(&MI);
1388
1389       // If NewReg was previously holding value of some SS, it's now clobbered.
1390       // This has to be done now because it's a physical register. When this
1391       // instruction is re-visited, it's ignored.
1392       Spills.ClobberPhysReg(NewReg);
1393
1394       ++NumCommutes;
1395       return true;
1396     }
1397
1398     return false;
1399   }
1400
1401   /// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if
1402   /// the last store to the same slot is now dead. If so, remove the last store.
1403   void SpillRegToStackSlot(MachineBasicBlock &MBB,
1404                            MachineBasicBlock::iterator &MII,
1405                            int Idx, unsigned PhysReg, int StackSlot,
1406                            const TargetRegisterClass *RC,
1407                            bool isAvailable, MachineInstr *&LastStore,
1408                            AvailableSpills &Spills,
1409                            SmallSet<MachineInstr*, 4> &ReMatDefs,
1410                            BitVector &RegKills,
1411                            std::vector<MachineOperand*> &KillOps,
1412                            VirtRegMap &VRM) {
1413
1414     TII->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC);
1415     MachineInstr *StoreMI = next(MII);
1416     VRM.addSpillSlotUse(StackSlot, StoreMI);
1417     DOUT << "Store:\t" << *StoreMI;
1418
1419     // If there is a dead store to this stack slot, nuke it now.
1420     if (LastStore) {
1421       DOUT << "Removed dead store:\t" << *LastStore;
1422       ++NumDSE;
1423       SmallVector<unsigned, 2> KillRegs;
1424       InvalidateKills(*LastStore, TRI, RegKills, KillOps, &KillRegs);
1425       MachineBasicBlock::iterator PrevMII = LastStore;
1426       bool CheckDef = PrevMII != MBB.begin();
1427       if (CheckDef)
1428         --PrevMII;
1429       VRM.RemoveMachineInstrFromMaps(LastStore);
1430       MBB.erase(LastStore);
1431       if (CheckDef) {
1432         // Look at defs of killed registers on the store. Mark the defs
1433         // as dead since the store has been deleted and they aren't
1434         // being reused.
1435         for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) {
1436           bool HasOtherDef = false;
1437           if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) {
1438             MachineInstr *DeadDef = PrevMII;
1439             if (ReMatDefs.count(DeadDef) && !HasOtherDef) {
1440               // FIXME: This assumes a remat def does not have side effects.
1441               VRM.RemoveMachineInstrFromMaps(DeadDef);
1442               MBB.erase(DeadDef);
1443               ++NumDRM;
1444             }
1445           }
1446         }
1447       }
1448     }
1449
1450     LastStore = next(MII);
1451
1452     // If the stack slot value was previously available in some other
1453     // register, change it now.  Otherwise, make the register available,
1454     // in PhysReg.
1455     Spills.ModifyStackSlotOrReMat(StackSlot);
1456     Spills.ClobberPhysReg(PhysReg);
1457     Spills.addAvailable(StackSlot, PhysReg, isAvailable);
1458     ++NumStores;
1459   }
1460
1461   /// TransferDeadness - A identity copy definition is dead and it's being
1462   /// removed. Find the last def or use and mark it as dead / kill.
1463   void TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist,
1464                         unsigned Reg, BitVector &RegKills,
1465                         std::vector<MachineOperand*> &KillOps,
1466                         VirtRegMap &VRM) {
1467     SmallPtrSet<MachineInstr*, 4> Seens;
1468     SmallVector<std::pair<MachineInstr*, int>,8> Refs;
1469     for (MachineRegisterInfo::reg_iterator RI = RegInfo->reg_begin(Reg),
1470            RE = RegInfo->reg_end(); RI != RE; ++RI) {
1471       MachineInstr *UDMI = &*RI;
1472       if (UDMI->getParent() != MBB)
1473         continue;
1474       DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
1475       if (DI == DistanceMap.end() || DI->second > CurDist)
1476         continue;
1477       if (Seens.insert(UDMI))
1478         Refs.push_back(std::make_pair(UDMI, DI->second));
1479     }
1480
1481     if (Refs.empty())
1482       return;
1483     std::sort(Refs.begin(), Refs.end(), RefSorter());
1484
1485     while (!Refs.empty()) {
1486       MachineInstr *LastUDMI = Refs.back().first;
1487       Refs.pop_back();
1488
1489       MachineOperand *LastUD = NULL;
1490       for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) {
1491         MachineOperand &MO = LastUDMI->getOperand(i);
1492         if (!MO.isReg() || MO.getReg() != Reg)
1493           continue;
1494         if (!LastUD || (LastUD->isUse() && MO.isDef()))
1495           LastUD = &MO;
1496         if (LastUDMI->isRegTiedToDefOperand(i))
1497           break;
1498       }
1499       if (LastUD->isDef()) {
1500         // If the instruction has no side effect, delete it and propagate
1501         // backward further. Otherwise, mark is dead and we are done.
1502         if (!TII->isDeadInstruction(LastUDMI)) {
1503           LastUD->setIsDead();
1504           break;
1505         }
1506         VRM.RemoveMachineInstrFromMaps(LastUDMI);
1507         MBB->erase(LastUDMI);
1508       } else {
1509         LastUD->setIsKill();
1510         RegKills.set(Reg);
1511         KillOps[Reg] = LastUD;
1512         break;
1513       }
1514     }
1515   }
1516
1517   /// rewriteMBB - Keep track of which spills are available even after the
1518   /// register allocator is done with them.  If possible, avid reloading vregs.
1519   void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
1520                   LiveIntervals *LIs,
1521                   AvailableSpills &Spills, BitVector &RegKills,
1522                   std::vector<MachineOperand*> &KillOps) {
1523
1524     DEBUG(errs() << "\n**** Local spiller rewriting MBB '"
1525           << MBB.getBasicBlock()->getName() << "':\n");
1526
1527     MachineFunction &MF = *MBB.getParent();
1528     
1529     // MaybeDeadStores - When we need to write a value back into a stack slot,
1530     // keep track of the inserted store.  If the stack slot value is never read
1531     // (because the value was used from some available register, for example), and
1532     // subsequently stored to, the original store is dead.  This map keeps track
1533     // of inserted stores that are not used.  If we see a subsequent store to the
1534     // same stack slot, the original store is deleted.
1535     std::vector<MachineInstr*> MaybeDeadStores;
1536     MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL);
1537
1538     // ReMatDefs - These are rematerializable def MIs which are not deleted.
1539     SmallSet<MachineInstr*, 4> ReMatDefs;
1540
1541     // Clear kill info.
1542     SmallSet<unsigned, 2> KilledMIRegs;
1543     RegKills.reset();
1544     KillOps.clear();
1545     KillOps.resize(TRI->getNumRegs(), NULL);
1546
1547     unsigned Dist = 0;
1548     DistanceMap.clear();
1549     for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
1550          MII != E; ) {
1551       MachineBasicBlock::iterator NextMII = next(MII);
1552
1553       VirtRegMap::MI2VirtMapTy::const_iterator I, End;
1554       bool Erased = false;
1555       bool BackTracked = false;
1556       if (OptimizeByUnfold(MBB, MII,
1557                            MaybeDeadStores, Spills, RegKills, KillOps, VRM))
1558         NextMII = next(MII);
1559
1560       MachineInstr &MI = *MII;
1561
1562       if (VRM.hasEmergencySpills(&MI)) {
1563         // Spill physical register(s) in the rare case the allocator has run out
1564         // of registers to allocate.
1565         SmallSet<int, 4> UsedSS;
1566         std::vector<unsigned> &EmSpills = VRM.getEmergencySpills(&MI);
1567         for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) {
1568           unsigned PhysReg = EmSpills[i];
1569           const TargetRegisterClass *RC =
1570             TRI->getPhysicalRegisterRegClass(PhysReg);
1571           assert(RC && "Unable to determine register class!");
1572           int SS = VRM.getEmergencySpillSlot(RC);
1573           if (UsedSS.count(SS))
1574             llvm_unreachable("Need to spill more than one physical registers!");
1575           UsedSS.insert(SS);
1576           TII->storeRegToStackSlot(MBB, MII, PhysReg, true, SS, RC);
1577           MachineInstr *StoreMI = prior(MII);
1578           VRM.addSpillSlotUse(SS, StoreMI);
1579
1580           // Back-schedule reloads and remats.
1581           MachineBasicBlock::iterator InsertLoc =
1582             ComputeReloadLoc(next(MII), MBB.begin(), PhysReg, TRI, false,
1583                              SS, TII, MF);
1584
1585           TII->loadRegFromStackSlot(MBB, InsertLoc, PhysReg, SS, RC);
1586
1587           MachineInstr *LoadMI = prior(InsertLoc);
1588           VRM.addSpillSlotUse(SS, LoadMI);
1589           ++NumPSpills;
1590           DistanceMap.insert(std::make_pair(LoadMI, Dist++));
1591         }
1592         NextMII = next(MII);
1593       }
1594
1595       // Insert restores here if asked to.
1596       if (VRM.isRestorePt(&MI)) {
1597         std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI);
1598         for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) {
1599           unsigned VirtReg = RestoreRegs[e-i-1];  // Reverse order.
1600           if (!VRM.getPreSplitReg(VirtReg))
1601             continue; // Split interval spilled again.
1602           unsigned Phys = VRM.getPhys(VirtReg);
1603           RegInfo->setPhysRegUsed(Phys);
1604
1605           // Check if the value being restored if available. If so, it must be
1606           // from a predecessor BB that fallthrough into this BB. We do not
1607           // expect:
1608           // BB1:
1609           // r1 = load fi#1
1610           // ...
1611           //    = r1<kill>
1612           // ... # r1 not clobbered
1613           // ...
1614           //    = load fi#1
1615           bool DoReMat = VRM.isReMaterialized(VirtReg);
1616           int SSorRMId = DoReMat
1617             ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
1618           const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1619           unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
1620           if (InReg == Phys) {
1621             // If the value is already available in the expected register, save
1622             // a reload / remat.
1623             if (SSorRMId)
1624               DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1;
1625             else
1626               DOUT << "Reusing SS#" << SSorRMId;
1627             DOUT << " from physreg "
1628                  << TRI->getName(InReg) << " for vreg"
1629                  << VirtReg <<" instead of reloading into physreg "
1630                  << TRI->getName(Phys) << "\n";
1631             ++NumOmitted;
1632             continue;
1633           } else if (InReg && InReg != Phys) {
1634             if (SSorRMId)
1635               DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1;
1636             else
1637               DOUT << "Reusing SS#" << SSorRMId;
1638             DOUT << " from physreg "
1639                  << TRI->getName(InReg) << " for vreg"
1640                  << VirtReg <<" by copying it into physreg "
1641                  << TRI->getName(Phys) << "\n";
1642
1643             // If the reloaded / remat value is available in another register,
1644             // copy it to the desired register.
1645
1646             // Back-schedule reloads and remats.
1647             MachineBasicBlock::iterator InsertLoc =
1648               ComputeReloadLoc(MII, MBB.begin(), Phys, TRI, DoReMat,
1649                                SSorRMId, TII, MF);
1650
1651             TII->copyRegToReg(MBB, InsertLoc, Phys, InReg, RC, RC);
1652
1653             // This invalidates Phys.
1654             Spills.ClobberPhysReg(Phys);
1655             // Remember it's available.
1656             Spills.addAvailable(SSorRMId, Phys);
1657
1658             // Mark is killed.
1659             MachineInstr *CopyMI = prior(InsertLoc);
1660             MachineOperand *KillOpnd = CopyMI->findRegisterUseOperand(InReg);
1661             KillOpnd->setIsKill();
1662             UpdateKills(*CopyMI, TRI, RegKills, KillOps);
1663
1664             DOUT << '\t' << *CopyMI;
1665             ++NumCopified;
1666             continue;
1667           }
1668
1669           // Back-schedule reloads and remats.
1670           MachineBasicBlock::iterator InsertLoc =
1671             ComputeReloadLoc(MII, MBB.begin(), Phys, TRI, DoReMat,
1672                              SSorRMId, TII, MF);
1673
1674           if (VRM.isReMaterialized(VirtReg)) {
1675             ReMaterialize(MBB, InsertLoc, Phys, VirtReg, TII, TRI, VRM);
1676           } else {
1677             const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1678             TII->loadRegFromStackSlot(MBB, InsertLoc, Phys, SSorRMId, RC);
1679             MachineInstr *LoadMI = prior(InsertLoc);
1680             VRM.addSpillSlotUse(SSorRMId, LoadMI);
1681             ++NumLoads;
1682             DistanceMap.insert(std::make_pair(LoadMI, Dist++));
1683           }
1684
1685           // This invalidates Phys.
1686           Spills.ClobberPhysReg(Phys);
1687           // Remember it's available.
1688           Spills.addAvailable(SSorRMId, Phys);
1689
1690           UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps);
1691           DOUT << '\t' << *prior(MII);
1692         }
1693       }
1694
1695       // Insert spills here if asked to.
1696       if (VRM.isSpillPt(&MI)) {
1697         std::vector<std::pair<unsigned,bool> > &SpillRegs =
1698           VRM.getSpillPtSpills(&MI);
1699         for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) {
1700           unsigned VirtReg = SpillRegs[i].first;
1701           bool isKill = SpillRegs[i].second;
1702           if (!VRM.getPreSplitReg(VirtReg))
1703             continue; // Split interval spilled again.
1704           const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
1705           unsigned Phys = VRM.getPhys(VirtReg);
1706           int StackSlot = VRM.getStackSlot(VirtReg);
1707           TII->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC);
1708           MachineInstr *StoreMI = next(MII);
1709           VRM.addSpillSlotUse(StackSlot, StoreMI);
1710           DOUT << "Store:\t" << *StoreMI;
1711           VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
1712         }
1713         NextMII = next(MII);
1714       }
1715
1716       /// ReusedOperands - Keep track of operand reuse in case we need to undo
1717       /// reuse.
1718       ReuseInfo ReusedOperands(MI, TRI);
1719       SmallVector<unsigned, 4> VirtUseOps;
1720       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1721         MachineOperand &MO = MI.getOperand(i);
1722         if (!MO.isReg() || MO.getReg() == 0)
1723           continue;   // Ignore non-register operands.
1724         
1725         unsigned VirtReg = MO.getReg();
1726         if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) {
1727           // Ignore physregs for spilling, but remember that it is used by this
1728           // function.
1729           RegInfo->setPhysRegUsed(VirtReg);
1730           continue;
1731         }
1732
1733         // We want to process implicit virtual register uses first.
1734         if (MO.isImplicit())
1735           // If the virtual register is implicitly defined, emit a implicit_def
1736           // before so scavenger knows it's "defined".
1737           // FIXME: This is a horrible hack done the by register allocator to
1738           // remat a definition with virtual register operand.
1739           VirtUseOps.insert(VirtUseOps.begin(), i);
1740         else
1741           VirtUseOps.push_back(i);
1742       }
1743
1744       // Process all of the spilled uses and all non spilled reg references.
1745       SmallVector<int, 2> PotentialDeadStoreSlots;
1746       KilledMIRegs.clear();
1747       for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) {
1748         unsigned i = VirtUseOps[j];
1749         MachineOperand &MO = MI.getOperand(i);
1750         unsigned VirtReg = MO.getReg();
1751         assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
1752                "Not a virtual register?");
1753
1754         unsigned SubIdx = MO.getSubReg();
1755         if (VRM.isAssignedReg(VirtReg)) {
1756           // This virtual register was assigned a physreg!
1757           unsigned Phys = VRM.getPhys(VirtReg);
1758           RegInfo->setPhysRegUsed(Phys);
1759           if (MO.isDef())
1760             ReusedOperands.markClobbered(Phys);
1761           unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
1762           MI.getOperand(i).setReg(RReg);
1763           MI.getOperand(i).setSubReg(0);
1764           if (VRM.isImplicitlyDefined(VirtReg))
1765             // FIXME: Is this needed?
1766             BuildMI(MBB, &MI, MI.getDebugLoc(),
1767                     TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
1768           continue;
1769         }
1770         
1771         // This virtual register is now known to be a spilled value.
1772         if (!MO.isUse())
1773           continue;  // Handle defs in the loop below (handle use&def here though)
1774
1775         bool AvoidReload = MO.isUndef();
1776         // Check if it is defined by an implicit def. It should not be spilled.
1777         // Note, this is for correctness reason. e.g.
1778         // 8   %reg1024<def> = IMPLICIT_DEF
1779         // 12  %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1780         // The live range [12, 14) are not part of the r1024 live interval since
1781         // it's defined by an implicit def. It will not conflicts with live
1782         // interval of r1025. Now suppose both registers are spilled, you can
1783         // easily see a situation where both registers are reloaded before
1784         // the INSERT_SUBREG and both target registers that would overlap.
1785         bool DoReMat = VRM.isReMaterialized(VirtReg);
1786         int SSorRMId = DoReMat
1787           ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
1788         int ReuseSlot = SSorRMId;
1789
1790         // Check to see if this stack slot is available.
1791         unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
1792
1793         // If this is a sub-register use, make sure the reuse register is in the
1794         // right register class. For example, for x86 not all of the 32-bit
1795         // registers have accessible sub-registers.
1796         // Similarly so for EXTRACT_SUBREG. Consider this:
1797         // EDI = op
1798         // MOV32_mr fi#1, EDI
1799         // ...
1800         //       = EXTRACT_SUBREG fi#1
1801         // fi#1 is available in EDI, but it cannot be reused because it's not in
1802         // the right register file.
1803         if (PhysReg && !AvoidReload &&
1804             (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) {
1805           const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1806           if (!RC->contains(PhysReg))
1807             PhysReg = 0;
1808         }
1809
1810         if (PhysReg && !AvoidReload) {
1811           // This spilled operand might be part of a two-address operand.  If this
1812           // is the case, then changing it will necessarily require changing the 
1813           // def part of the instruction as well.  However, in some cases, we
1814           // aren't allowed to modify the reused register.  If none of these cases
1815           // apply, reuse it.
1816           bool CanReuse = true;
1817           bool isTied = MI.isRegTiedToDefOperand(i);
1818           if (isTied) {
1819             // Okay, we have a two address operand.  We can reuse this physreg as
1820             // long as we are allowed to clobber the value and there isn't an
1821             // earlier def that has already clobbered the physreg.
1822             CanReuse = !ReusedOperands.isClobbered(PhysReg) &&
1823               Spills.canClobberPhysReg(PhysReg);
1824           }
1825           
1826           if (CanReuse) {
1827             // If this stack slot value is already available, reuse it!
1828             if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1829               DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
1830             else
1831               DOUT << "Reusing SS#" << ReuseSlot;
1832             DOUT << " from physreg "
1833                  << TRI->getName(PhysReg) << " for vreg"
1834                  << VirtReg <<" instead of reloading into physreg "
1835                  << TRI->getName(VRM.getPhys(VirtReg)) << "\n";
1836             unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
1837             MI.getOperand(i).setReg(RReg);
1838             MI.getOperand(i).setSubReg(0);
1839
1840             // The only technical detail we have is that we don't know that
1841             // PhysReg won't be clobbered by a reloaded stack slot that occurs
1842             // later in the instruction.  In particular, consider 'op V1, V2'.
1843             // If V1 is available in physreg R0, we would choose to reuse it
1844             // here, instead of reloading it into the register the allocator
1845             // indicated (say R1).  However, V2 might have to be reloaded
1846             // later, and it might indicate that it needs to live in R0.  When
1847             // this occurs, we need to have information available that
1848             // indicates it is safe to use R1 for the reload instead of R0.
1849             //
1850             // To further complicate matters, we might conflict with an alias,
1851             // or R0 and R1 might not be compatible with each other.  In this
1852             // case, we actually insert a reload for V1 in R1, ensuring that
1853             // we can get at R0 or its alias.
1854             ReusedOperands.addReuse(i, ReuseSlot, PhysReg,
1855                                     VRM.getPhys(VirtReg), VirtReg);
1856             if (isTied)
1857               // Only mark it clobbered if this is a use&def operand.
1858               ReusedOperands.markClobbered(PhysReg);
1859             ++NumReused;
1860
1861             if (MI.getOperand(i).isKill() &&
1862                 ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
1863
1864               // The store of this spilled value is potentially dead, but we
1865               // won't know for certain until we've confirmed that the re-use
1866               // above is valid, which means waiting until the other operands
1867               // are processed. For now we just track the spill slot, we'll
1868               // remove it after the other operands are processed if valid.
1869
1870               PotentialDeadStoreSlots.push_back(ReuseSlot);
1871             }
1872
1873             // Mark is isKill if it's there no other uses of the same virtual
1874             // register and it's not a two-address operand. IsKill will be
1875             // unset if reg is reused.
1876             if (!isTied && KilledMIRegs.count(VirtReg) == 0) {
1877               MI.getOperand(i).setIsKill();
1878               KilledMIRegs.insert(VirtReg);
1879             }
1880
1881             continue;
1882           }  // CanReuse
1883           
1884           // Otherwise we have a situation where we have a two-address instruction
1885           // whose mod/ref operand needs to be reloaded.  This reload is already
1886           // available in some register "PhysReg", but if we used PhysReg as the
1887           // operand to our 2-addr instruction, the instruction would modify
1888           // PhysReg.  This isn't cool if something later uses PhysReg and expects
1889           // to get its initial value.
1890           //
1891           // To avoid this problem, and to avoid doing a load right after a store,
1892           // we emit a copy from PhysReg into the designated register for this
1893           // operand.
1894           unsigned DesignatedReg = VRM.getPhys(VirtReg);
1895           assert(DesignatedReg && "Must map virtreg to physreg!");
1896
1897           // Note that, if we reused a register for a previous operand, the
1898           // register we want to reload into might not actually be
1899           // available.  If this occurs, use the register indicated by the
1900           // reuser.
1901           if (ReusedOperands.hasReuses())
1902             DesignatedReg = ReusedOperands.GetRegForReload(VirtReg,
1903                                                            DesignatedReg, &MI, 
1904                                Spills, MaybeDeadStores, RegKills, KillOps, VRM);
1905           
1906           // If the mapped designated register is actually the physreg we have
1907           // incoming, we don't need to inserted a dead copy.
1908           if (DesignatedReg == PhysReg) {
1909             // If this stack slot value is already available, reuse it!
1910             if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1911               DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
1912             else
1913               DOUT << "Reusing SS#" << ReuseSlot;
1914             DOUT << " from physreg " << TRI->getName(PhysReg)
1915                  << " for vreg" << VirtReg
1916                  << " instead of reloading into same physreg.\n";
1917             unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
1918             MI.getOperand(i).setReg(RReg);
1919             MI.getOperand(i).setSubReg(0);
1920             ReusedOperands.markClobbered(RReg);
1921             ++NumReused;
1922             continue;
1923           }
1924           
1925           const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1926           RegInfo->setPhysRegUsed(DesignatedReg);
1927           ReusedOperands.markClobbered(DesignatedReg);
1928
1929           // Back-schedule reloads and remats.
1930           MachineBasicBlock::iterator InsertLoc =
1931             ComputeReloadLoc(&MI, MBB.begin(), PhysReg, TRI, DoReMat,
1932                              SSorRMId, TII, MF);
1933
1934           TII->copyRegToReg(MBB, InsertLoc, DesignatedReg, PhysReg, RC, RC);
1935
1936           MachineInstr *CopyMI = prior(InsertLoc);
1937           UpdateKills(*CopyMI, TRI, RegKills, KillOps);
1938
1939           // This invalidates DesignatedReg.
1940           Spills.ClobberPhysReg(DesignatedReg);
1941           
1942           Spills.addAvailable(ReuseSlot, DesignatedReg);
1943           unsigned RReg =
1944             SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg;
1945           MI.getOperand(i).setReg(RReg);
1946           MI.getOperand(i).setSubReg(0);
1947           DOUT << '\t' << *prior(MII);
1948           ++NumReused;
1949           continue;
1950         } // if (PhysReg)
1951         
1952         // Otherwise, reload it and remember that we have it.
1953         PhysReg = VRM.getPhys(VirtReg);
1954         assert(PhysReg && "Must map virtreg to physreg!");
1955
1956         // Note that, if we reused a register for a previous operand, the
1957         // register we want to reload into might not actually be
1958         // available.  If this occurs, use the register indicated by the
1959         // reuser.
1960         if (ReusedOperands.hasReuses())
1961           PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, 
1962                                Spills, MaybeDeadStores, RegKills, KillOps, VRM);
1963         
1964         RegInfo->setPhysRegUsed(PhysReg);
1965         ReusedOperands.markClobbered(PhysReg);
1966         if (AvoidReload)
1967           ++NumAvoided;
1968         else {
1969           // Back-schedule reloads and remats.
1970           MachineBasicBlock::iterator InsertLoc =
1971             ComputeReloadLoc(MII, MBB.begin(), PhysReg, TRI, DoReMat,
1972                              SSorRMId, TII, MF);
1973
1974           if (DoReMat) {
1975             ReMaterialize(MBB, InsertLoc, PhysReg, VirtReg, TII, TRI, VRM);
1976           } else {
1977             const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1978             TII->loadRegFromStackSlot(MBB, InsertLoc, PhysReg, SSorRMId, RC);
1979             MachineInstr *LoadMI = prior(InsertLoc);
1980             VRM.addSpillSlotUse(SSorRMId, LoadMI);
1981             ++NumLoads;
1982             DistanceMap.insert(std::make_pair(LoadMI, Dist++));
1983           }
1984           // This invalidates PhysReg.
1985           Spills.ClobberPhysReg(PhysReg);
1986
1987           // Any stores to this stack slot are not dead anymore.
1988           if (!DoReMat)
1989             MaybeDeadStores[SSorRMId] = NULL;
1990           Spills.addAvailable(SSorRMId, PhysReg);
1991           // Assumes this is the last use. IsKill will be unset if reg is reused
1992           // unless it's a two-address operand.
1993           if (!MI.isRegTiedToDefOperand(i) &&
1994               KilledMIRegs.count(VirtReg) == 0) {
1995             MI.getOperand(i).setIsKill();
1996             KilledMIRegs.insert(VirtReg);
1997           }
1998
1999           UpdateKills(*prior(InsertLoc), TRI, RegKills, KillOps);
2000           DOUT << '\t' << *prior(InsertLoc);
2001         }
2002         unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
2003         MI.getOperand(i).setReg(RReg);
2004         MI.getOperand(i).setSubReg(0);
2005       }
2006
2007       // Ok - now we can remove stores that have been confirmed dead.
2008       for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) {
2009         // This was the last use and the spilled value is still available
2010         // for reuse. That means the spill was unnecessary!
2011         int PDSSlot = PotentialDeadStoreSlots[j];
2012         MachineInstr* DeadStore = MaybeDeadStores[PDSSlot];
2013         if (DeadStore) {
2014           DOUT << "Removed dead store:\t" << *DeadStore;
2015           InvalidateKills(*DeadStore, TRI, RegKills, KillOps);
2016           VRM.RemoveMachineInstrFromMaps(DeadStore);
2017           MBB.erase(DeadStore);
2018           MaybeDeadStores[PDSSlot] = NULL;
2019           ++NumDSE;
2020         }
2021       }
2022
2023
2024       DOUT << '\t' << MI;
2025
2026
2027       // If we have folded references to memory operands, make sure we clear all
2028       // physical registers that may contain the value of the spilled virtual
2029       // register
2030       SmallSet<int, 2> FoldedSS;
2031       for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
2032         unsigned VirtReg = I->second.first;
2033         VirtRegMap::ModRef MR = I->second.second;
2034         DOUT << "Folded vreg: " << VirtReg << "  MR: " << MR;
2035
2036         // MI2VirtMap be can updated which invalidate the iterator.
2037         // Increment the iterator first.
2038         ++I;
2039         int SS = VRM.getStackSlot(VirtReg);
2040         if (SS == VirtRegMap::NO_STACK_SLOT)
2041           continue;
2042         FoldedSS.insert(SS);
2043         DOUT << " - StackSlot: " << SS << "\n";
2044         
2045         // If this folded instruction is just a use, check to see if it's a
2046         // straight load from the virt reg slot.
2047         if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
2048           int FrameIdx;
2049           unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx);
2050           if (DestReg && FrameIdx == SS) {
2051             // If this spill slot is available, turn it into a copy (or nothing)
2052             // instead of leaving it as a load!
2053             if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) {
2054               DOUT << "Promoted Load To Copy: " << MI;
2055               if (DestReg != InReg) {
2056                 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
2057                 TII->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC);
2058                 MachineOperand *DefMO = MI.findRegisterDefOperand(DestReg);
2059                 unsigned SubIdx = DefMO->getSubReg();
2060                 // Revisit the copy so we make sure to notice the effects of the
2061                 // operation on the destreg (either needing to RA it if it's 
2062                 // virtual or needing to clobber any values if it's physical).
2063                 NextMII = &MI;
2064                 --NextMII;  // backtrack to the copy.
2065                 // Propagate the sub-register index over.
2066                 if (SubIdx) {
2067                   DefMO = NextMII->findRegisterDefOperand(DestReg);
2068                   DefMO->setSubReg(SubIdx);
2069                 }
2070
2071                 // Mark is killed.
2072                 MachineOperand *KillOpnd = NextMII->findRegisterUseOperand(InReg);
2073                 KillOpnd->setIsKill();
2074
2075                 BackTracked = true;
2076               } else {
2077                 DOUT << "Removing now-noop copy: " << MI;
2078                 // Unset last kill since it's being reused.
2079                 InvalidateKill(InReg, TRI, RegKills, KillOps);
2080                 Spills.disallowClobberPhysReg(InReg);
2081               }
2082
2083               InvalidateKills(MI, TRI, RegKills, KillOps);
2084               VRM.RemoveMachineInstrFromMaps(&MI);
2085               MBB.erase(&MI);
2086               Erased = true;
2087               goto ProcessNextInst;
2088             }
2089           } else {
2090             unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
2091             SmallVector<MachineInstr*, 4> NewMIs;
2092             if (PhysReg &&
2093                 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) {
2094               MBB.insert(MII, NewMIs[0]);
2095               InvalidateKills(MI, TRI, RegKills, KillOps);
2096               VRM.RemoveMachineInstrFromMaps(&MI);
2097               MBB.erase(&MI);
2098               Erased = true;
2099               --NextMII;  // backtrack to the unfolded instruction.
2100               BackTracked = true;
2101               goto ProcessNextInst;
2102             }
2103           }
2104         }
2105
2106         // If this reference is not a use, any previous store is now dead.
2107         // Otherwise, the store to this stack slot is not dead anymore.
2108         MachineInstr* DeadStore = MaybeDeadStores[SS];
2109         if (DeadStore) {
2110           bool isDead = !(MR & VirtRegMap::isRef);
2111           MachineInstr *NewStore = NULL;
2112           if (MR & VirtRegMap::isModRef) {
2113             unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
2114             SmallVector<MachineInstr*, 4> NewMIs;
2115             // We can reuse this physreg as long as we are allowed to clobber
2116             // the value and there isn't an earlier def that has already clobbered
2117             // the physreg.
2118             if (PhysReg &&
2119                 !ReusedOperands.isClobbered(PhysReg) &&
2120                 Spills.canClobberPhysReg(PhysReg) &&
2121                 !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable!
2122               MachineOperand *KillOpnd =
2123                 DeadStore->findRegisterUseOperand(PhysReg, true);
2124               // Note, if the store is storing a sub-register, it's possible the
2125               // super-register is needed below.
2126               if (KillOpnd && !KillOpnd->getSubReg() &&
2127                   TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){
2128                 MBB.insert(MII, NewMIs[0]);
2129                 NewStore = NewMIs[1];
2130                 MBB.insert(MII, NewStore);
2131                 VRM.addSpillSlotUse(SS, NewStore);
2132                 InvalidateKills(MI, TRI, RegKills, KillOps);
2133                 VRM.RemoveMachineInstrFromMaps(&MI);
2134                 MBB.erase(&MI);
2135                 Erased = true;
2136                 --NextMII;
2137                 --NextMII;  // backtrack to the unfolded instruction.
2138                 BackTracked = true;
2139                 isDead = true;
2140                 ++NumSUnfold;
2141               }
2142             }
2143           }
2144
2145           if (isDead) {  // Previous store is dead.
2146             // If we get here, the store is dead, nuke it now.
2147             DOUT << "Removed dead store:\t" << *DeadStore;
2148             InvalidateKills(*DeadStore, TRI, RegKills, KillOps);
2149             VRM.RemoveMachineInstrFromMaps(DeadStore);
2150             MBB.erase(DeadStore);
2151             if (!NewStore)
2152               ++NumDSE;
2153           }
2154
2155           MaybeDeadStores[SS] = NULL;
2156           if (NewStore) {
2157             // Treat this store as a spill merged into a copy. That makes the
2158             // stack slot value available.
2159             VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod);
2160             goto ProcessNextInst;
2161           }
2162         }
2163
2164         // If the spill slot value is available, and this is a new definition of
2165         // the value, the value is not available anymore.
2166         if (MR & VirtRegMap::isMod) {
2167           // Notice that the value in this stack slot has been modified.
2168           Spills.ModifyStackSlotOrReMat(SS);
2169           
2170           // If this is *just* a mod of the value, check to see if this is just a
2171           // store to the spill slot (i.e. the spill got merged into the copy). If
2172           // so, realize that the vreg is available now, and add the store to the
2173           // MaybeDeadStore info.
2174           int StackSlot;
2175           if (!(MR & VirtRegMap::isRef)) {
2176             if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
2177               assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
2178                      "Src hasn't been allocated yet?");
2179
2180               if (CommuteToFoldReload(MBB, MII, VirtReg, SrcReg, StackSlot,
2181                                       Spills, RegKills, KillOps, TRI, VRM)) {
2182                 NextMII = next(MII);
2183                 BackTracked = true;
2184                 goto ProcessNextInst;
2185               }
2186
2187               // Okay, this is certainly a store of SrcReg to [StackSlot].  Mark
2188               // this as a potentially dead store in case there is a subsequent
2189               // store into the stack slot without a read from it.
2190               MaybeDeadStores[StackSlot] = &MI;
2191
2192               // If the stack slot value was previously available in some other
2193               // register, change it now.  Otherwise, make the register
2194               // available in PhysReg.
2195               Spills.addAvailable(StackSlot, SrcReg, MI.killsRegister(SrcReg));
2196             }
2197           }
2198         }
2199       }
2200
2201       // Process all of the spilled defs.
2202       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2203         MachineOperand &MO = MI.getOperand(i);
2204         if (!(MO.isReg() && MO.getReg() && MO.isDef()))
2205           continue;
2206
2207         unsigned VirtReg = MO.getReg();
2208         if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) {
2209           // Check to see if this is a noop copy.  If so, eliminate the
2210           // instruction before considering the dest reg to be changed.
2211           // Also check if it's copying from an "undef", if so, we can't
2212           // eliminate this or else the undef marker is lost and it will
2213           // confuses the scavenger. This is extremely rare.
2214           unsigned Src, Dst, SrcSR, DstSR;
2215           if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst &&
2216               !MI.findRegisterUseOperand(Src)->isUndef()) {
2217             ++NumDCE;
2218             DOUT << "Removing now-noop copy: " << MI;
2219             SmallVector<unsigned, 2> KillRegs;
2220             InvalidateKills(MI, TRI, RegKills, KillOps, &KillRegs);
2221             if (MO.isDead() && !KillRegs.empty()) {
2222               // Source register or an implicit super/sub-register use is killed.
2223               assert(KillRegs[0] == Dst ||
2224                      TRI->isSubRegister(KillRegs[0], Dst) ||
2225                      TRI->isSuperRegister(KillRegs[0], Dst));
2226               // Last def is now dead.
2227               TransferDeadness(&MBB, Dist, Src, RegKills, KillOps, VRM);
2228             }
2229             VRM.RemoveMachineInstrFromMaps(&MI);
2230             MBB.erase(&MI);
2231             Erased = true;
2232             Spills.disallowClobberPhysReg(VirtReg);
2233             goto ProcessNextInst;
2234           }
2235
2236           // If it's not a no-op copy, it clobbers the value in the destreg.
2237           Spills.ClobberPhysReg(VirtReg);
2238           ReusedOperands.markClobbered(VirtReg);
2239    
2240           // Check to see if this instruction is a load from a stack slot into
2241           // a register.  If so, this provides the stack slot value in the reg.
2242           int FrameIdx;
2243           if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
2244             assert(DestReg == VirtReg && "Unknown load situation!");
2245
2246             // If it is a folded reference, then it's not safe to clobber.
2247             bool Folded = FoldedSS.count(FrameIdx);
2248             // Otherwise, if it wasn't available, remember that it is now!
2249             Spills.addAvailable(FrameIdx, DestReg, !Folded);
2250             goto ProcessNextInst;
2251           }
2252               
2253           continue;
2254         }
2255
2256         unsigned SubIdx = MO.getSubReg();
2257         bool DoReMat = VRM.isReMaterialized(VirtReg);
2258         if (DoReMat)
2259           ReMatDefs.insert(&MI);
2260
2261         // The only vregs left are stack slot definitions.
2262         int StackSlot = VRM.getStackSlot(VirtReg);
2263         const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
2264
2265         // If this def is part of a two-address operand, make sure to execute
2266         // the store from the correct physical register.
2267         unsigned PhysReg;
2268         unsigned TiedOp;
2269         if (MI.isRegTiedToUseOperand(i, &TiedOp)) {
2270           PhysReg = MI.getOperand(TiedOp).getReg();
2271           if (SubIdx) {
2272             unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI);
2273             assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg &&
2274                    "Can't find corresponding super-register!");
2275             PhysReg = SuperReg;
2276           }
2277         } else {
2278           PhysReg = VRM.getPhys(VirtReg);
2279           if (ReusedOperands.isClobbered(PhysReg)) {
2280             // Another def has taken the assigned physreg. It must have been a
2281             // use&def which got it due to reuse. Undo the reuse!
2282             PhysReg = ReusedOperands.GetRegForReload(VirtReg, PhysReg, &MI, 
2283                                Spills, MaybeDeadStores, RegKills, KillOps, VRM);
2284           }
2285         }
2286
2287         assert(PhysReg && "VR not assigned a physical register?");
2288         RegInfo->setPhysRegUsed(PhysReg);
2289         unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
2290         ReusedOperands.markClobbered(RReg);
2291         MI.getOperand(i).setReg(RReg);
2292         MI.getOperand(i).setSubReg(0);
2293
2294         if (!MO.isDead()) {
2295           MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
2296           SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true,
2297                             LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM);
2298           NextMII = next(MII);
2299
2300           // Check to see if this is a noop copy.  If so, eliminate the
2301           // instruction before considering the dest reg to be changed.
2302           {
2303             unsigned Src, Dst, SrcSR, DstSR;
2304             if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) {
2305               ++NumDCE;
2306               DOUT << "Removing now-noop copy: " << MI;
2307               InvalidateKills(MI, TRI, RegKills, KillOps);
2308               VRM.RemoveMachineInstrFromMaps(&MI);
2309               MBB.erase(&MI);
2310               Erased = true;
2311               UpdateKills(*LastStore, TRI, RegKills, KillOps);
2312               goto ProcessNextInst;
2313             }
2314           }
2315         }    
2316       }
2317     ProcessNextInst:
2318       // Delete dead instructions without side effects.
2319       if (!Erased && !BackTracked && TII->isDeadInstruction(&MI)) {
2320         InvalidateKills(MI, TRI, RegKills, KillOps);
2321         VRM.RemoveMachineInstrFromMaps(&MI);
2322         MBB.erase(&MI);
2323         Erased = true;
2324       }
2325       if (!Erased)
2326         DistanceMap.insert(std::make_pair(&MI, Dist++));
2327       if (!Erased && !BackTracked) {
2328         for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II)
2329           UpdateKills(*II, TRI, RegKills, KillOps);
2330       }
2331       MII = NextMII;
2332     }
2333
2334   }
2335
2336 };
2337
2338 }
2339
2340 llvm::VirtRegRewriter* llvm::createVirtRegRewriter() {
2341   switch (RewriterOpt) {
2342   default: llvm_unreachable("Unreachable!");
2343   case local:
2344     return new LocalRewriter();
2345   case trivial:
2346     return new TrivialRewriter();
2347   }
2348 }