Make another kill check LiveIntervals-aware.
[oota-llvm.git] / lib / CodeGen / TwoAddressInstructionPass.cpp
1 //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
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 // This file implements the TwoAddress instruction pass which is used
11 // by most register allocators. Two-Address instructions are rewritten
12 // from:
13 //
14 //     A = B op C
15 //
16 // to:
17 //
18 //     A = B
19 //     A op= C
20 //
21 // Note that if a register allocator chooses to use this pass, that it
22 // has to be capable of handling the non-SSA nature of these rewritten
23 // virtual registers.
24 //
25 // It is also worth noting that the duplicate operand of the two
26 // address instruction is removed.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #define DEBUG_TYPE "twoaddrinstr"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SmallSet.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Analysis/AliasAnalysis.h"
38 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
39 #include "llvm/CodeGen/LiveVariables.h"
40 #include "llvm/CodeGen/MachineFunctionPass.h"
41 #include "llvm/CodeGen/MachineInstr.h"
42 #include "llvm/CodeGen/MachineInstrBuilder.h"
43 #include "llvm/CodeGen/MachineRegisterInfo.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/MC/MCInstrItineraries.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Target/TargetInstrInfo.h"
49 #include "llvm/Target/TargetMachine.h"
50 #include "llvm/Target/TargetOptions.h"
51 #include "llvm/Target/TargetRegisterInfo.h"
52 using namespace llvm;
53
54 STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
55 STATISTIC(NumCommuted        , "Number of instructions commuted to coalesce");
56 STATISTIC(NumAggrCommuted    , "Number of instructions aggressively commuted");
57 STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
58 STATISTIC(Num3AddrSunk,        "Number of 3-address instructions sunk");
59 STATISTIC(NumReSchedUps,       "Number of instructions re-scheduled up");
60 STATISTIC(NumReSchedDowns,     "Number of instructions re-scheduled down");
61
62 namespace {
63 class TwoAddressInstructionPass : public MachineFunctionPass {
64   MachineFunction *MF;
65   const TargetInstrInfo *TII;
66   const TargetRegisterInfo *TRI;
67   const InstrItineraryData *InstrItins;
68   MachineRegisterInfo *MRI;
69   LiveVariables *LV;
70   LiveIntervals *LIS;
71   AliasAnalysis *AA;
72   CodeGenOpt::Level OptLevel;
73
74   // The current basic block being processed.
75   MachineBasicBlock *MBB;
76
77   // DistanceMap - Keep track the distance of a MI from the start of the
78   // current basic block.
79   DenseMap<MachineInstr*, unsigned> DistanceMap;
80
81   // Set of already processed instructions in the current block.
82   SmallPtrSet<MachineInstr*, 8> Processed;
83
84   // SrcRegMap - A map from virtual registers to physical registers which are
85   // likely targets to be coalesced to due to copies from physical registers to
86   // virtual registers. e.g. v1024 = move r0.
87   DenseMap<unsigned, unsigned> SrcRegMap;
88
89   // DstRegMap - A map from virtual registers to physical registers which are
90   // likely targets to be coalesced to due to copies to physical registers from
91   // virtual registers. e.g. r1 = move v1024.
92   DenseMap<unsigned, unsigned> DstRegMap;
93
94   bool sink3AddrInstruction(MachineInstr *MI, unsigned Reg,
95                             MachineBasicBlock::iterator OldPos);
96
97   bool noUseAfterLastDef(unsigned Reg, unsigned Dist, unsigned &LastDef);
98
99   bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
100                              MachineInstr *MI, unsigned Dist);
101
102   bool commuteInstruction(MachineBasicBlock::iterator &mi,
103                           unsigned RegB, unsigned RegC, unsigned Dist);
104
105   bool isProfitableToConv3Addr(unsigned RegA, unsigned RegB);
106
107   bool convertInstTo3Addr(MachineBasicBlock::iterator &mi,
108                           MachineBasicBlock::iterator &nmi,
109                           unsigned RegA, unsigned RegB, unsigned Dist);
110
111   bool isDefTooClose(unsigned Reg, unsigned Dist, MachineInstr *MI);
112
113   bool rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
114                              MachineBasicBlock::iterator &nmi,
115                              unsigned Reg);
116   bool rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
117                              MachineBasicBlock::iterator &nmi,
118                              unsigned Reg);
119
120   bool tryInstructionTransform(MachineBasicBlock::iterator &mi,
121                                MachineBasicBlock::iterator &nmi,
122                                unsigned SrcIdx, unsigned DstIdx,
123                                unsigned Dist);
124
125   void scanUses(unsigned DstReg);
126
127   void processCopy(MachineInstr *MI);
128
129   typedef SmallVector<std::pair<unsigned, unsigned>, 4> TiedPairList;
130   typedef SmallDenseMap<unsigned, TiedPairList> TiedOperandMap;
131   bool collectTiedOperands(MachineInstr *MI, TiedOperandMap&);
132   void processTiedPairs(MachineInstr *MI, TiedPairList&, unsigned &Dist);
133   void eliminateRegSequence(MachineBasicBlock::iterator&);
134
135 public:
136   static char ID; // Pass identification, replacement for typeid
137   TwoAddressInstructionPass() : MachineFunctionPass(ID) {
138     initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry());
139   }
140
141   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
142     AU.setPreservesCFG();
143     AU.addRequired<AliasAnalysis>();
144     AU.addPreserved<LiveVariables>();
145     AU.addPreserved<SlotIndexes>();
146     AU.addPreserved<LiveIntervals>();
147     AU.addPreservedID(MachineLoopInfoID);
148     AU.addPreservedID(MachineDominatorsID);
149     MachineFunctionPass::getAnalysisUsage(AU);
150   }
151
152   /// runOnMachineFunction - Pass entry point.
153   bool runOnMachineFunction(MachineFunction&);
154 };
155 } // end anonymous namespace
156
157 char TwoAddressInstructionPass::ID = 0;
158 INITIALIZE_PASS_BEGIN(TwoAddressInstructionPass, "twoaddressinstruction",
159                 "Two-Address instruction pass", false, false)
160 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
161 INITIALIZE_PASS_END(TwoAddressInstructionPass, "twoaddressinstruction",
162                 "Two-Address instruction pass", false, false)
163
164 char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID;
165
166 /// sink3AddrInstruction - A two-address instruction has been converted to a
167 /// three-address instruction to avoid clobbering a register. Try to sink it
168 /// past the instruction that would kill the above mentioned register to reduce
169 /// register pressure.
170 bool TwoAddressInstructionPass::
171 sink3AddrInstruction(MachineInstr *MI, unsigned SavedReg,
172                      MachineBasicBlock::iterator OldPos) {
173   // FIXME: Shouldn't we be trying to do this before we three-addressify the
174   // instruction?  After this transformation is done, we no longer need
175   // the instruction to be in three-address form.
176
177   // Check if it's safe to move this instruction.
178   bool SeenStore = true; // Be conservative.
179   if (!MI->isSafeToMove(TII, AA, SeenStore))
180     return false;
181
182   unsigned DefReg = 0;
183   SmallSet<unsigned, 4> UseRegs;
184
185   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
186     const MachineOperand &MO = MI->getOperand(i);
187     if (!MO.isReg())
188       continue;
189     unsigned MOReg = MO.getReg();
190     if (!MOReg)
191       continue;
192     if (MO.isUse() && MOReg != SavedReg)
193       UseRegs.insert(MO.getReg());
194     if (!MO.isDef())
195       continue;
196     if (MO.isImplicit())
197       // Don't try to move it if it implicitly defines a register.
198       return false;
199     if (DefReg)
200       // For now, don't move any instructions that define multiple registers.
201       return false;
202     DefReg = MO.getReg();
203   }
204
205   // Find the instruction that kills SavedReg.
206   MachineInstr *KillMI = NULL;
207   for (MachineRegisterInfo::use_nodbg_iterator
208          UI = MRI->use_nodbg_begin(SavedReg),
209          UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
210     MachineOperand &UseMO = UI.getOperand();
211     if (!UseMO.isKill())
212       continue;
213     KillMI = UseMO.getParent();
214     break;
215   }
216
217   // If we find the instruction that kills SavedReg, and it is in an
218   // appropriate location, we can try to sink the current instruction
219   // past it.
220   if (!KillMI || KillMI->getParent() != MBB || KillMI == MI ||
221       KillMI == OldPos || KillMI->isTerminator())
222     return false;
223
224   // If any of the definitions are used by another instruction between the
225   // position and the kill use, then it's not safe to sink it.
226   //
227   // FIXME: This can be sped up if there is an easy way to query whether an
228   // instruction is before or after another instruction. Then we can use
229   // MachineRegisterInfo def / use instead.
230   MachineOperand *KillMO = NULL;
231   MachineBasicBlock::iterator KillPos = KillMI;
232   ++KillPos;
233
234   unsigned NumVisited = 0;
235   for (MachineBasicBlock::iterator I = llvm::next(OldPos); I != KillPos; ++I) {
236     MachineInstr *OtherMI = I;
237     // DBG_VALUE cannot be counted against the limit.
238     if (OtherMI->isDebugValue())
239       continue;
240     if (NumVisited > 30)  // FIXME: Arbitrary limit to reduce compile time cost.
241       return false;
242     ++NumVisited;
243     for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
244       MachineOperand &MO = OtherMI->getOperand(i);
245       if (!MO.isReg())
246         continue;
247       unsigned MOReg = MO.getReg();
248       if (!MOReg)
249         continue;
250       if (DefReg == MOReg)
251         return false;
252
253       if (MO.isKill()) {
254         if (OtherMI == KillMI && MOReg == SavedReg)
255           // Save the operand that kills the register. We want to unset the kill
256           // marker if we can sink MI past it.
257           KillMO = &MO;
258         else if (UseRegs.count(MOReg))
259           // One of the uses is killed before the destination.
260           return false;
261       }
262     }
263   }
264   assert(KillMO && "Didn't find kill");
265
266   // Update kill and LV information.
267   KillMO->setIsKill(false);
268   KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
269   KillMO->setIsKill(true);
270
271   if (LV)
272     LV->replaceKillInstruction(SavedReg, KillMI, MI);
273
274   // Move instruction to its destination.
275   MBB->remove(MI);
276   MBB->insert(KillPos, MI);
277
278   if (LIS)
279     LIS->handleMove(MI);
280
281   ++Num3AddrSunk;
282   return true;
283 }
284
285 /// noUseAfterLastDef - Return true if there are no intervening uses between the
286 /// last instruction in the MBB that defines the specified register and the
287 /// two-address instruction which is being processed. It also returns the last
288 /// def location by reference
289 bool TwoAddressInstructionPass::noUseAfterLastDef(unsigned Reg, unsigned Dist,
290                                                   unsigned &LastDef) {
291   LastDef = 0;
292   unsigned LastUse = Dist;
293   for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
294          E = MRI->reg_end(); I != E; ++I) {
295     MachineOperand &MO = I.getOperand();
296     MachineInstr *MI = MO.getParent();
297     if (MI->getParent() != MBB || MI->isDebugValue())
298       continue;
299     DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
300     if (DI == DistanceMap.end())
301       continue;
302     if (MO.isUse() && DI->second < LastUse)
303       LastUse = DI->second;
304     if (MO.isDef() && DI->second > LastDef)
305       LastDef = DI->second;
306   }
307
308   return !(LastUse > LastDef && LastUse < Dist);
309 }
310
311 /// isCopyToReg - Return true if the specified MI is a copy instruction or
312 /// a extract_subreg instruction. It also returns the source and destination
313 /// registers and whether they are physical registers by reference.
314 static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
315                         unsigned &SrcReg, unsigned &DstReg,
316                         bool &IsSrcPhys, bool &IsDstPhys) {
317   SrcReg = 0;
318   DstReg = 0;
319   if (MI.isCopy()) {
320     DstReg = MI.getOperand(0).getReg();
321     SrcReg = MI.getOperand(1).getReg();
322   } else if (MI.isInsertSubreg() || MI.isSubregToReg()) {
323     DstReg = MI.getOperand(0).getReg();
324     SrcReg = MI.getOperand(2).getReg();
325   } else
326     return false;
327
328   IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
329   IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
330   return true;
331 }
332
333 /// isPLainlyKilled - Test if the given register value, which is used by the
334 // given instruction, is killed by the given instruction.
335 static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg,
336                             LiveIntervals *LIS) {
337   if (LIS && TargetRegisterInfo::isVirtualRegister(Reg) &&
338       !LIS->isNotInMIMap(MI)) {
339     // FIXME: Sometimes tryInstructionTransform() will add instructions and
340     // test whether they can be folded before keeping them. In this case it
341     // sets a kill before recursively calling tryInstructionTransform() again.
342     // If there is no interval available, we assume that this instruction is
343     // one of those. A kill flag is manually inserted on the operand so the
344     // check below will handle it.
345     LiveInterval &LI = LIS->getInterval(Reg);
346     // This is to match the kill flag version where undefs don't have kill
347     // flags.
348     if (!LI.hasAtLeastOneValue())
349       return false;
350
351     SlotIndex useIdx = LIS->getInstructionIndex(MI);
352     LiveInterval::const_iterator I = LI.find(useIdx);
353     assert(I != LI.end() && "Reg must be live-in to use.");
354     return SlotIndex::isSameInstr(I->end, useIdx);
355   }
356
357   return MI->killsRegister(Reg);
358 }
359
360 /// isKilled - Test if the given register value, which is used by the given
361 /// instruction, is killed by the given instruction. This looks through
362 /// coalescable copies to see if the original value is potentially not killed.
363 ///
364 /// For example, in this code:
365 ///
366 ///   %reg1034 = copy %reg1024
367 ///   %reg1035 = copy %reg1025<kill>
368 ///   %reg1036 = add %reg1034<kill>, %reg1035<kill>
369 ///
370 /// %reg1034 is not considered to be killed, since it is copied from a
371 /// register which is not killed. Treating it as not killed lets the
372 /// normal heuristics commute the (two-address) add, which lets
373 /// coalescing eliminate the extra copy.
374 ///
375 static bool isKilled(MachineInstr &MI, unsigned Reg,
376                      const MachineRegisterInfo *MRI,
377                      const TargetInstrInfo *TII,
378                      LiveIntervals *LIS) {
379   MachineInstr *DefMI = &MI;
380   for (;;) {
381     if (!isPlainlyKilled(DefMI, Reg, LIS))
382       return false;
383     if (TargetRegisterInfo::isPhysicalRegister(Reg))
384       return true;
385     MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
386     // If there are multiple defs, we can't do a simple analysis, so just
387     // go with what the kill flag says.
388     if (llvm::next(Begin) != MRI->def_end())
389       return true;
390     DefMI = &*Begin;
391     bool IsSrcPhys, IsDstPhys;
392     unsigned SrcReg,  DstReg;
393     // If the def is something other than a copy, then it isn't going to
394     // be coalesced, so follow the kill flag.
395     if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
396       return true;
397     Reg = SrcReg;
398   }
399 }
400
401 /// isTwoAddrUse - Return true if the specified MI uses the specified register
402 /// as a two-address use. If so, return the destination register by reference.
403 static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
404   const MCInstrDesc &MCID = MI.getDesc();
405   unsigned NumOps = MI.isInlineAsm()
406     ? MI.getNumOperands() : MCID.getNumOperands();
407   for (unsigned i = 0; i != NumOps; ++i) {
408     const MachineOperand &MO = MI.getOperand(i);
409     if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
410       continue;
411     unsigned ti;
412     if (MI.isRegTiedToDefOperand(i, &ti)) {
413       DstReg = MI.getOperand(ti).getReg();
414       return true;
415     }
416   }
417   return false;
418 }
419
420 /// findOnlyInterestingUse - Given a register, if has a single in-basic block
421 /// use, return the use instruction if it's a copy or a two-address use.
422 static
423 MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
424                                      MachineRegisterInfo *MRI,
425                                      const TargetInstrInfo *TII,
426                                      bool &IsCopy,
427                                      unsigned &DstReg, bool &IsDstPhys) {
428   if (!MRI->hasOneNonDBGUse(Reg))
429     // None or more than one use.
430     return 0;
431   MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg);
432   if (UseMI.getParent() != MBB)
433     return 0;
434   unsigned SrcReg;
435   bool IsSrcPhys;
436   if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
437     IsCopy = true;
438     return &UseMI;
439   }
440   IsDstPhys = false;
441   if (isTwoAddrUse(UseMI, Reg, DstReg)) {
442     IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
443     return &UseMI;
444   }
445   return 0;
446 }
447
448 /// getMappedReg - Return the physical register the specified virtual register
449 /// might be mapped to.
450 static unsigned
451 getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
452   while (TargetRegisterInfo::isVirtualRegister(Reg))  {
453     DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
454     if (SI == RegMap.end())
455       return 0;
456     Reg = SI->second;
457   }
458   if (TargetRegisterInfo::isPhysicalRegister(Reg))
459     return Reg;
460   return 0;
461 }
462
463 /// regsAreCompatible - Return true if the two registers are equal or aliased.
464 ///
465 static bool
466 regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
467   if (RegA == RegB)
468     return true;
469   if (!RegA || !RegB)
470     return false;
471   return TRI->regsOverlap(RegA, RegB);
472 }
473
474
475 /// isProfitableToCommute - Return true if it's potentially profitable to commute
476 /// the two-address instruction that's being processed.
477 bool
478 TwoAddressInstructionPass::
479 isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
480                       MachineInstr *MI, unsigned Dist) {
481   if (OptLevel == CodeGenOpt::None)
482     return false;
483
484   // Determine if it's profitable to commute this two address instruction. In
485   // general, we want no uses between this instruction and the definition of
486   // the two-address register.
487   // e.g.
488   // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
489   // %reg1029<def> = MOV8rr %reg1028
490   // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
491   // insert => %reg1030<def> = MOV8rr %reg1028
492   // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
493   // In this case, it might not be possible to coalesce the second MOV8rr
494   // instruction if the first one is coalesced. So it would be profitable to
495   // commute it:
496   // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
497   // %reg1029<def> = MOV8rr %reg1028
498   // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
499   // insert => %reg1030<def> = MOV8rr %reg1029
500   // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
501
502   if (!isPlainlyKilled(MI, regC, LIS))
503     return false;
504
505   // Ok, we have something like:
506   // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
507   // let's see if it's worth commuting it.
508
509   // Look for situations like this:
510   // %reg1024<def> = MOV r1
511   // %reg1025<def> = MOV r0
512   // %reg1026<def> = ADD %reg1024, %reg1025
513   // r0            = MOV %reg1026
514   // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
515   unsigned ToRegA = getMappedReg(regA, DstRegMap);
516   if (ToRegA) {
517     unsigned FromRegB = getMappedReg(regB, SrcRegMap);
518     unsigned FromRegC = getMappedReg(regC, SrcRegMap);
519     bool BComp = !FromRegB || regsAreCompatible(FromRegB, ToRegA, TRI);
520     bool CComp = !FromRegC || regsAreCompatible(FromRegC, ToRegA, TRI);
521     if (BComp != CComp)
522       return !BComp && CComp;
523   }
524
525   // If there is a use of regC between its last def (could be livein) and this
526   // instruction, then bail.
527   unsigned LastDefC = 0;
528   if (!noUseAfterLastDef(regC, Dist, LastDefC))
529     return false;
530
531   // If there is a use of regB between its last def (could be livein) and this
532   // instruction, then go ahead and make this transformation.
533   unsigned LastDefB = 0;
534   if (!noUseAfterLastDef(regB, Dist, LastDefB))
535     return true;
536
537   // Since there are no intervening uses for both registers, then commute
538   // if the def of regC is closer. Its live interval is shorter.
539   return LastDefB && LastDefC && LastDefC > LastDefB;
540 }
541
542 /// commuteInstruction - Commute a two-address instruction and update the basic
543 /// block, distance map, and live variables if needed. Return true if it is
544 /// successful.
545 bool TwoAddressInstructionPass::
546 commuteInstruction(MachineBasicBlock::iterator &mi,
547                    unsigned RegB, unsigned RegC, unsigned Dist) {
548   MachineInstr *MI = mi;
549   DEBUG(dbgs() << "2addr: COMMUTING  : " << *MI);
550   MachineInstr *NewMI = TII->commuteInstruction(MI);
551
552   if (NewMI == 0) {
553     DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
554     return false;
555   }
556
557   DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
558   // If the instruction changed to commute it, update livevar.
559   if (NewMI != MI) {
560     if (LV)
561       // Update live variables
562       LV->replaceKillInstruction(RegC, MI, NewMI);
563     if (LIS)
564       LIS->ReplaceMachineInstrInMaps(MI, NewMI);
565
566     MBB->insert(mi, NewMI);           // Insert the new inst
567     MBB->erase(mi);                   // Nuke the old inst.
568     mi = NewMI;
569     DistanceMap.insert(std::make_pair(NewMI, Dist));
570   }
571
572   // Update source register map.
573   unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
574   if (FromRegC) {
575     unsigned RegA = MI->getOperand(0).getReg();
576     SrcRegMap[RegA] = FromRegC;
577   }
578
579   return true;
580 }
581
582 /// isProfitableToConv3Addr - Return true if it is profitable to convert the
583 /// given 2-address instruction to a 3-address one.
584 bool
585 TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA,unsigned RegB){
586   // Look for situations like this:
587   // %reg1024<def> = MOV r1
588   // %reg1025<def> = MOV r0
589   // %reg1026<def> = ADD %reg1024, %reg1025
590   // r2            = MOV %reg1026
591   // Turn ADD into a 3-address instruction to avoid a copy.
592   unsigned FromRegB = getMappedReg(RegB, SrcRegMap);
593   if (!FromRegB)
594     return false;
595   unsigned ToRegA = getMappedReg(RegA, DstRegMap);
596   return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI));
597 }
598
599 /// convertInstTo3Addr - Convert the specified two-address instruction into a
600 /// three address one. Return true if this transformation was successful.
601 bool
602 TwoAddressInstructionPass::convertInstTo3Addr(MachineBasicBlock::iterator &mi,
603                                               MachineBasicBlock::iterator &nmi,
604                                               unsigned RegA, unsigned RegB,
605                                               unsigned Dist) {
606   // FIXME: Why does convertToThreeAddress() need an iterator reference?
607   MachineFunction::iterator MFI = MBB;
608   MachineInstr *NewMI = TII->convertToThreeAddress(MFI, mi, LV);
609   assert(MBB == MFI && "convertToThreeAddress changed iterator reference");
610   if (!NewMI)
611     return false;
612
613   DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
614   DEBUG(dbgs() << "2addr:         TO 3-ADDR: " << *NewMI);
615   bool Sunk = false;
616
617   if (LIS)
618     LIS->ReplaceMachineInstrInMaps(mi, NewMI);
619
620   if (NewMI->findRegisterUseOperand(RegB, false, TRI))
621     // FIXME: Temporary workaround. If the new instruction doesn't
622     // uses RegB, convertToThreeAddress must have created more
623     // then one instruction.
624     Sunk = sink3AddrInstruction(NewMI, RegB, mi);
625
626   MBB->erase(mi); // Nuke the old inst.
627
628   if (!Sunk) {
629     DistanceMap.insert(std::make_pair(NewMI, Dist));
630     mi = NewMI;
631     nmi = llvm::next(mi);
632   }
633
634   // Update source and destination register maps.
635   SrcRegMap.erase(RegA);
636   DstRegMap.erase(RegB);
637   return true;
638 }
639
640 /// scanUses - Scan forward recursively for only uses, update maps if the use
641 /// is a copy or a two-address instruction.
642 void
643 TwoAddressInstructionPass::scanUses(unsigned DstReg) {
644   SmallVector<unsigned, 4> VirtRegPairs;
645   bool IsDstPhys;
646   bool IsCopy = false;
647   unsigned NewReg = 0;
648   unsigned Reg = DstReg;
649   while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy,
650                                                       NewReg, IsDstPhys)) {
651     if (IsCopy && !Processed.insert(UseMI))
652       break;
653
654     DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
655     if (DI != DistanceMap.end())
656       // Earlier in the same MBB.Reached via a back edge.
657       break;
658
659     if (IsDstPhys) {
660       VirtRegPairs.push_back(NewReg);
661       break;
662     }
663     bool isNew = SrcRegMap.insert(std::make_pair(NewReg, Reg)).second;
664     if (!isNew)
665       assert(SrcRegMap[NewReg] == Reg && "Can't map to two src registers!");
666     VirtRegPairs.push_back(NewReg);
667     Reg = NewReg;
668   }
669
670   if (!VirtRegPairs.empty()) {
671     unsigned ToReg = VirtRegPairs.back();
672     VirtRegPairs.pop_back();
673     while (!VirtRegPairs.empty()) {
674       unsigned FromReg = VirtRegPairs.back();
675       VirtRegPairs.pop_back();
676       bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
677       if (!isNew)
678         assert(DstRegMap[FromReg] == ToReg &&"Can't map to two dst registers!");
679       ToReg = FromReg;
680     }
681     bool isNew = DstRegMap.insert(std::make_pair(DstReg, ToReg)).second;
682     if (!isNew)
683       assert(DstRegMap[DstReg] == ToReg && "Can't map to two dst registers!");
684   }
685 }
686
687 /// processCopy - If the specified instruction is not yet processed, process it
688 /// if it's a copy. For a copy instruction, we find the physical registers the
689 /// source and destination registers might be mapped to. These are kept in
690 /// point-to maps used to determine future optimizations. e.g.
691 /// v1024 = mov r0
692 /// v1025 = mov r1
693 /// v1026 = add v1024, v1025
694 /// r1    = mov r1026
695 /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
696 /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
697 /// potentially joined with r1 on the output side. It's worthwhile to commute
698 /// 'add' to eliminate a copy.
699 void TwoAddressInstructionPass::processCopy(MachineInstr *MI) {
700   if (Processed.count(MI))
701     return;
702
703   bool IsSrcPhys, IsDstPhys;
704   unsigned SrcReg, DstReg;
705   if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
706     return;
707
708   if (IsDstPhys && !IsSrcPhys)
709     DstRegMap.insert(std::make_pair(SrcReg, DstReg));
710   else if (!IsDstPhys && IsSrcPhys) {
711     bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
712     if (!isNew)
713       assert(SrcRegMap[DstReg] == SrcReg &&
714              "Can't map to two src physical registers!");
715
716     scanUses(DstReg);
717   }
718
719   Processed.insert(MI);
720   return;
721 }
722
723 /// rescheduleMIBelowKill - If there is one more local instruction that reads
724 /// 'Reg' and it kills 'Reg, consider moving the instruction below the kill
725 /// instruction in order to eliminate the need for the copy.
726 bool TwoAddressInstructionPass::
727 rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
728                       MachineBasicBlock::iterator &nmi,
729                       unsigned Reg) {
730   // Bail immediately if we don't have LV available. We use it to find kills
731   // efficiently.
732   if (!LV)
733     return false;
734
735   MachineInstr *MI = &*mi;
736   DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
737   if (DI == DistanceMap.end())
738     // Must be created from unfolded load. Don't waste time trying this.
739     return false;
740
741   MachineInstr *KillMI = LV->getVarInfo(Reg).findKill(MBB);
742   if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
743     // Don't mess with copies, they may be coalesced later.
744     return false;
745
746   if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() ||
747       KillMI->isBranch() || KillMI->isTerminator())
748     // Don't move pass calls, etc.
749     return false;
750
751   unsigned DstReg;
752   if (isTwoAddrUse(*KillMI, Reg, DstReg))
753     return false;
754
755   bool SeenStore = true;
756   if (!MI->isSafeToMove(TII, AA, SeenStore))
757     return false;
758
759   if (TII->getInstrLatency(InstrItins, MI) > 1)
760     // FIXME: Needs more sophisticated heuristics.
761     return false;
762
763   SmallSet<unsigned, 2> Uses;
764   SmallSet<unsigned, 2> Kills;
765   SmallSet<unsigned, 2> Defs;
766   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
767     const MachineOperand &MO = MI->getOperand(i);
768     if (!MO.isReg())
769       continue;
770     unsigned MOReg = MO.getReg();
771     if (!MOReg)
772       continue;
773     if (MO.isDef())
774       Defs.insert(MOReg);
775     else {
776       Uses.insert(MOReg);
777       if (MO.isKill() && MOReg != Reg)
778         Kills.insert(MOReg);
779     }
780   }
781
782   // Move the copies connected to MI down as well.
783   MachineBasicBlock::iterator From = MI;
784   MachineBasicBlock::iterator To = llvm::next(From);
785   while (To->isCopy() && Defs.count(To->getOperand(1).getReg())) {
786     Defs.insert(To->getOperand(0).getReg());
787     ++To;
788   }
789
790   // Check if the reschedule will not break depedencies.
791   unsigned NumVisited = 0;
792   MachineBasicBlock::iterator KillPos = KillMI;
793   ++KillPos;
794   for (MachineBasicBlock::iterator I = To; I != KillPos; ++I) {
795     MachineInstr *OtherMI = I;
796     // DBG_VALUE cannot be counted against the limit.
797     if (OtherMI->isDebugValue())
798       continue;
799     if (NumVisited > 10)  // FIXME: Arbitrary limit to reduce compile time cost.
800       return false;
801     ++NumVisited;
802     if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() ||
803         OtherMI->isBranch() || OtherMI->isTerminator())
804       // Don't move pass calls, etc.
805       return false;
806     for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
807       const MachineOperand &MO = OtherMI->getOperand(i);
808       if (!MO.isReg())
809         continue;
810       unsigned MOReg = MO.getReg();
811       if (!MOReg)
812         continue;
813       if (MO.isDef()) {
814         if (Uses.count(MOReg))
815           // Physical register use would be clobbered.
816           return false;
817         if (!MO.isDead() && Defs.count(MOReg))
818           // May clobber a physical register def.
819           // FIXME: This may be too conservative. It's ok if the instruction
820           // is sunken completely below the use.
821           return false;
822       } else {
823         if (Defs.count(MOReg))
824           return false;
825         if (MOReg != Reg &&
826             ((MO.isKill() && Uses.count(MOReg)) || Kills.count(MOReg)))
827           // Don't want to extend other live ranges and update kills.
828           return false;
829         if (MOReg == Reg && !MO.isKill())
830           // We can't schedule across a use of the register in question.
831           return false;
832         // Ensure that if this is register in question, its the kill we expect.
833         assert((MOReg != Reg || OtherMI == KillMI) &&
834                "Found multiple kills of a register in a basic block");
835       }
836     }
837   }
838
839   // Move debug info as well.
840   while (From != MBB->begin() && llvm::prior(From)->isDebugValue())
841     --From;
842
843   // Copies following MI may have been moved as well.
844   nmi = To;
845   MBB->splice(KillPos, MBB, From, To);
846   DistanceMap.erase(DI);
847
848   // Update live variables
849   LV->removeVirtualRegisterKilled(Reg, KillMI);
850   LV->addVirtualRegisterKilled(Reg, MI);
851   if (LIS)
852     LIS->handleMove(MI);
853
854   DEBUG(dbgs() << "\trescheduled below kill: " << *KillMI);
855   return true;
856 }
857
858 /// isDefTooClose - Return true if the re-scheduling will put the given
859 /// instruction too close to the defs of its register dependencies.
860 bool TwoAddressInstructionPass::isDefTooClose(unsigned Reg, unsigned Dist,
861                                               MachineInstr *MI) {
862   for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(Reg),
863          DE = MRI->def_end(); DI != DE; ++DI) {
864     MachineInstr *DefMI = &*DI;
865     if (DefMI->getParent() != MBB || DefMI->isCopy() || DefMI->isCopyLike())
866       continue;
867     if (DefMI == MI)
868       return true; // MI is defining something KillMI uses
869     DenseMap<MachineInstr*, unsigned>::iterator DDI = DistanceMap.find(DefMI);
870     if (DDI == DistanceMap.end())
871       return true;  // Below MI
872     unsigned DefDist = DDI->second;
873     assert(Dist > DefDist && "Visited def already?");
874     if (TII->getInstrLatency(InstrItins, DefMI) > (Dist - DefDist))
875       return true;
876   }
877   return false;
878 }
879
880 /// rescheduleKillAboveMI - If there is one more local instruction that reads
881 /// 'Reg' and it kills 'Reg, consider moving the kill instruction above the
882 /// current two-address instruction in order to eliminate the need for the
883 /// copy.
884 bool TwoAddressInstructionPass::
885 rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
886                       MachineBasicBlock::iterator &nmi,
887                       unsigned Reg) {
888   // Bail immediately if we don't have LV available. We use it to find kills
889   // efficiently.
890   if (!LV)
891     return false;
892
893   MachineInstr *MI = &*mi;
894   DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
895   if (DI == DistanceMap.end())
896     // Must be created from unfolded load. Don't waste time trying this.
897     return false;
898
899   MachineInstr *KillMI = LV->getVarInfo(Reg).findKill(MBB);
900   if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
901     // Don't mess with copies, they may be coalesced later.
902     return false;
903
904   unsigned DstReg;
905   if (isTwoAddrUse(*KillMI, Reg, DstReg))
906     return false;
907
908   bool SeenStore = true;
909   if (!KillMI->isSafeToMove(TII, AA, SeenStore))
910     return false;
911
912   SmallSet<unsigned, 2> Uses;
913   SmallSet<unsigned, 2> Kills;
914   SmallSet<unsigned, 2> Defs;
915   SmallSet<unsigned, 2> LiveDefs;
916   for (unsigned i = 0, e = KillMI->getNumOperands(); i != e; ++i) {
917     const MachineOperand &MO = KillMI->getOperand(i);
918     if (!MO.isReg())
919       continue;
920     unsigned MOReg = MO.getReg();
921     if (MO.isUse()) {
922       if (!MOReg)
923         continue;
924       if (isDefTooClose(MOReg, DI->second, MI))
925         return false;
926       if (MOReg == Reg && !MO.isKill())
927         return false;
928       Uses.insert(MOReg);
929       if (MO.isKill() && MOReg != Reg)
930         Kills.insert(MOReg);
931     } else if (TargetRegisterInfo::isPhysicalRegister(MOReg)) {
932       Defs.insert(MOReg);
933       if (!MO.isDead())
934         LiveDefs.insert(MOReg);
935     }
936   }
937
938   // Check if the reschedule will not break depedencies.
939   unsigned NumVisited = 0;
940   MachineBasicBlock::iterator KillPos = KillMI;
941   for (MachineBasicBlock::iterator I = mi; I != KillPos; ++I) {
942     MachineInstr *OtherMI = I;
943     // DBG_VALUE cannot be counted against the limit.
944     if (OtherMI->isDebugValue())
945       continue;
946     if (NumVisited > 10)  // FIXME: Arbitrary limit to reduce compile time cost.
947       return false;
948     ++NumVisited;
949     if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() ||
950         OtherMI->isBranch() || OtherMI->isTerminator())
951       // Don't move pass calls, etc.
952       return false;
953     SmallVector<unsigned, 2> OtherDefs;
954     for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
955       const MachineOperand &MO = OtherMI->getOperand(i);
956       if (!MO.isReg())
957         continue;
958       unsigned MOReg = MO.getReg();
959       if (!MOReg)
960         continue;
961       if (MO.isUse()) {
962         if (Defs.count(MOReg))
963           // Moving KillMI can clobber the physical register if the def has
964           // not been seen.
965           return false;
966         if (Kills.count(MOReg))
967           // Don't want to extend other live ranges and update kills.
968           return false;
969         if (OtherMI != MI && MOReg == Reg && !MO.isKill())
970           // We can't schedule across a use of the register in question.
971           return false;
972       } else {
973         OtherDefs.push_back(MOReg);
974       }
975     }
976
977     for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) {
978       unsigned MOReg = OtherDefs[i];
979       if (Uses.count(MOReg))
980         return false;
981       if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
982           LiveDefs.count(MOReg))
983         return false;
984       // Physical register def is seen.
985       Defs.erase(MOReg);
986     }
987   }
988
989   // Move the old kill above MI, don't forget to move debug info as well.
990   MachineBasicBlock::iterator InsertPos = mi;
991   while (InsertPos != MBB->begin() && llvm::prior(InsertPos)->isDebugValue())
992     --InsertPos;
993   MachineBasicBlock::iterator From = KillMI;
994   MachineBasicBlock::iterator To = llvm::next(From);
995   while (llvm::prior(From)->isDebugValue())
996     --From;
997   MBB->splice(InsertPos, MBB, From, To);
998
999   nmi = llvm::prior(InsertPos); // Backtrack so we process the moved instr.
1000   DistanceMap.erase(DI);
1001
1002   // Update live variables
1003   LV->removeVirtualRegisterKilled(Reg, KillMI);
1004   LV->addVirtualRegisterKilled(Reg, MI);
1005   if (LIS)
1006     LIS->handleMove(KillMI);
1007
1008   DEBUG(dbgs() << "\trescheduled kill: " << *KillMI);
1009   return true;
1010 }
1011
1012 /// tryInstructionTransform - For the case where an instruction has a single
1013 /// pair of tied register operands, attempt some transformations that may
1014 /// either eliminate the tied operands or improve the opportunities for
1015 /// coalescing away the register copy.  Returns true if no copy needs to be
1016 /// inserted to untie mi's operands (either because they were untied, or
1017 /// because mi was rescheduled, and will be visited again later).
1018 bool TwoAddressInstructionPass::
1019 tryInstructionTransform(MachineBasicBlock::iterator &mi,
1020                         MachineBasicBlock::iterator &nmi,
1021                         unsigned SrcIdx, unsigned DstIdx, unsigned Dist) {
1022   if (OptLevel == CodeGenOpt::None)
1023     return false;
1024
1025   MachineInstr &MI = *mi;
1026   unsigned regA = MI.getOperand(DstIdx).getReg();
1027   unsigned regB = MI.getOperand(SrcIdx).getReg();
1028
1029   assert(TargetRegisterInfo::isVirtualRegister(regB) &&
1030          "cannot make instruction into two-address form");
1031   bool regBKilled = isKilled(MI, regB, MRI, TII, LIS);
1032
1033   if (TargetRegisterInfo::isVirtualRegister(regA))
1034     scanUses(regA);
1035
1036   // Check if it is profitable to commute the operands.
1037   unsigned SrcOp1, SrcOp2;
1038   unsigned regC = 0;
1039   unsigned regCIdx = ~0U;
1040   bool TryCommute = false;
1041   bool AggressiveCommute = false;
1042   if (MI.isCommutable() && MI.getNumOperands() >= 3 &&
1043       TII->findCommutedOpIndices(&MI, SrcOp1, SrcOp2)) {
1044     if (SrcIdx == SrcOp1)
1045       regCIdx = SrcOp2;
1046     else if (SrcIdx == SrcOp2)
1047       regCIdx = SrcOp1;
1048
1049     if (regCIdx != ~0U) {
1050       regC = MI.getOperand(regCIdx).getReg();
1051       if (!regBKilled && isKilled(MI, regC, MRI, TII, LIS))
1052         // If C dies but B does not, swap the B and C operands.
1053         // This makes the live ranges of A and C joinable.
1054         TryCommute = true;
1055       else if (isProfitableToCommute(regA, regB, regC, &MI, Dist)) {
1056         TryCommute = true;
1057         AggressiveCommute = true;
1058       }
1059     }
1060   }
1061
1062   // If it's profitable to commute, try to do so.
1063   if (TryCommute && commuteInstruction(mi, regB, regC, Dist)) {
1064     ++NumCommuted;
1065     if (AggressiveCommute)
1066       ++NumAggrCommuted;
1067     return false;
1068   }
1069
1070   // If there is one more use of regB later in the same MBB, consider
1071   // re-schedule this MI below it.
1072   if (rescheduleMIBelowKill(mi, nmi, regB)) {
1073     ++NumReSchedDowns;
1074     return true;
1075   }
1076
1077   if (MI.isConvertibleTo3Addr()) {
1078     // This instruction is potentially convertible to a true
1079     // three-address instruction.  Check if it is profitable.
1080     if (!regBKilled || isProfitableToConv3Addr(regA, regB)) {
1081       // Try to convert it.
1082       if (convertInstTo3Addr(mi, nmi, regA, regB, Dist)) {
1083         ++NumConvertedTo3Addr;
1084         return true; // Done with this instruction.
1085       }
1086     }
1087   }
1088
1089   // If there is one more use of regB later in the same MBB, consider
1090   // re-schedule it before this MI if it's legal.
1091   if (rescheduleKillAboveMI(mi, nmi, regB)) {
1092     ++NumReSchedUps;
1093     return true;
1094   }
1095
1096   // If this is an instruction with a load folded into it, try unfolding
1097   // the load, e.g. avoid this:
1098   //   movq %rdx, %rcx
1099   //   addq (%rax), %rcx
1100   // in favor of this:
1101   //   movq (%rax), %rcx
1102   //   addq %rdx, %rcx
1103   // because it's preferable to schedule a load than a register copy.
1104   if (MI.mayLoad() && !regBKilled) {
1105     // Determine if a load can be unfolded.
1106     unsigned LoadRegIndex;
1107     unsigned NewOpc =
1108       TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
1109                                       /*UnfoldLoad=*/true,
1110                                       /*UnfoldStore=*/false,
1111                                       &LoadRegIndex);
1112     if (NewOpc != 0) {
1113       const MCInstrDesc &UnfoldMCID = TII->get(NewOpc);
1114       if (UnfoldMCID.getNumDefs() == 1) {
1115         // Unfold the load.
1116         DEBUG(dbgs() << "2addr:   UNFOLDING: " << MI);
1117         const TargetRegisterClass *RC =
1118           TRI->getAllocatableClass(
1119             TII->getRegClass(UnfoldMCID, LoadRegIndex, TRI, *MF));
1120         unsigned Reg = MRI->createVirtualRegister(RC);
1121         SmallVector<MachineInstr *, 2> NewMIs;
1122         if (!TII->unfoldMemoryOperand(*MF, &MI, Reg,
1123                                       /*UnfoldLoad=*/true,/*UnfoldStore=*/false,
1124                                       NewMIs)) {
1125           DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
1126           return false;
1127         }
1128         assert(NewMIs.size() == 2 &&
1129                "Unfolded a load into multiple instructions!");
1130         // The load was previously folded, so this is the only use.
1131         NewMIs[1]->addRegisterKilled(Reg, TRI);
1132
1133         // Tentatively insert the instructions into the block so that they
1134         // look "normal" to the transformation logic.
1135         MBB->insert(mi, NewMIs[0]);
1136         MBB->insert(mi, NewMIs[1]);
1137
1138         DEBUG(dbgs() << "2addr:    NEW LOAD: " << *NewMIs[0]
1139                      << "2addr:    NEW INST: " << *NewMIs[1]);
1140
1141         // Transform the instruction, now that it no longer has a load.
1142         unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
1143         unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
1144         MachineBasicBlock::iterator NewMI = NewMIs[1];
1145         bool TransformSuccess =
1146           tryInstructionTransform(NewMI, mi, NewSrcIdx, NewDstIdx, Dist);
1147         if (TransformSuccess ||
1148             NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
1149           // Success, or at least we made an improvement. Keep the unfolded
1150           // instructions and discard the original.
1151           if (LV) {
1152             for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1153               MachineOperand &MO = MI.getOperand(i);
1154               if (MO.isReg() &&
1155                   TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
1156                 if (MO.isUse()) {
1157                   if (MO.isKill()) {
1158                     if (NewMIs[0]->killsRegister(MO.getReg()))
1159                       LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[0]);
1160                     else {
1161                       assert(NewMIs[1]->killsRegister(MO.getReg()) &&
1162                              "Kill missing after load unfold!");
1163                       LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[1]);
1164                     }
1165                   }
1166                 } else if (LV->removeVirtualRegisterDead(MO.getReg(), &MI)) {
1167                   if (NewMIs[1]->registerDefIsDead(MO.getReg()))
1168                     LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]);
1169                   else {
1170                     assert(NewMIs[0]->registerDefIsDead(MO.getReg()) &&
1171                            "Dead flag missing after load unfold!");
1172                     LV->addVirtualRegisterDead(MO.getReg(), NewMIs[0]);
1173                   }
1174                 }
1175               }
1176             }
1177             LV->addVirtualRegisterKilled(Reg, NewMIs[1]);
1178           }
1179
1180           SmallVector<unsigned, 4> OrigRegs;
1181           if (LIS) {
1182             for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(),
1183                  MOE = MI.operands_end(); MOI != MOE; ++MOI) {
1184               if (MOI->isReg())
1185                 OrigRegs.push_back(MOI->getReg());
1186             }
1187           }
1188
1189           MI.eraseFromParent();
1190
1191           // Update LiveIntervals.
1192           if (LIS) {
1193             MachineBasicBlock::iterator Begin(NewMIs[0]);
1194             MachineBasicBlock::iterator End(NewMIs[1]);
1195             LIS->repairIntervalsInRange(MBB, Begin, End, OrigRegs);
1196           }
1197
1198           mi = NewMIs[1];
1199           if (TransformSuccess)
1200             return true;
1201         } else {
1202           // Transforming didn't eliminate the tie and didn't lead to an
1203           // improvement. Clean up the unfolded instructions and keep the
1204           // original.
1205           DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
1206           NewMIs[0]->eraseFromParent();
1207           NewMIs[1]->eraseFromParent();
1208         }
1209       }
1210     }
1211   }
1212
1213   return false;
1214 }
1215
1216 // Collect tied operands of MI that need to be handled.
1217 // Rewrite trivial cases immediately.
1218 // Return true if any tied operands where found, including the trivial ones.
1219 bool TwoAddressInstructionPass::
1220 collectTiedOperands(MachineInstr *MI, TiedOperandMap &TiedOperands) {
1221   const MCInstrDesc &MCID = MI->getDesc();
1222   bool AnyOps = false;
1223   unsigned NumOps = MI->getNumOperands();
1224
1225   for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
1226     unsigned DstIdx = 0;
1227     if (!MI->isRegTiedToDefOperand(SrcIdx, &DstIdx))
1228       continue;
1229     AnyOps = true;
1230     MachineOperand &SrcMO = MI->getOperand(SrcIdx);
1231     MachineOperand &DstMO = MI->getOperand(DstIdx);
1232     unsigned SrcReg = SrcMO.getReg();
1233     unsigned DstReg = DstMO.getReg();
1234     // Tied constraint already satisfied?
1235     if (SrcReg == DstReg)
1236       continue;
1237
1238     assert(SrcReg && SrcMO.isUse() && "two address instruction invalid");
1239
1240     // Deal with <undef> uses immediately - simply rewrite the src operand.
1241     if (SrcMO.isUndef()) {
1242       // Constrain the DstReg register class if required.
1243       if (TargetRegisterInfo::isVirtualRegister(DstReg))
1244         if (const TargetRegisterClass *RC = TII->getRegClass(MCID, SrcIdx,
1245                                                              TRI, *MF))
1246           MRI->constrainRegClass(DstReg, RC);
1247       SrcMO.setReg(DstReg);
1248       DEBUG(dbgs() << "\t\trewrite undef:\t" << *MI);
1249       continue;
1250     }
1251     TiedOperands[SrcReg].push_back(std::make_pair(SrcIdx, DstIdx));
1252   }
1253   return AnyOps;
1254 }
1255
1256 // Process a list of tied MI operands that all use the same source register.
1257 // The tied pairs are of the form (SrcIdx, DstIdx).
1258 void
1259 TwoAddressInstructionPass::processTiedPairs(MachineInstr *MI,
1260                                             TiedPairList &TiedPairs,
1261                                             unsigned &Dist) {
1262   bool IsEarlyClobber = false;
1263   for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
1264     const MachineOperand &DstMO = MI->getOperand(TiedPairs[tpi].second);
1265     IsEarlyClobber |= DstMO.isEarlyClobber();
1266   }
1267
1268   bool RemovedKillFlag = false;
1269   bool AllUsesCopied = true;
1270   unsigned LastCopiedReg = 0;
1271   SlotIndex LastCopyIdx;
1272   unsigned RegB = 0;
1273   for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
1274     unsigned SrcIdx = TiedPairs[tpi].first;
1275     unsigned DstIdx = TiedPairs[tpi].second;
1276
1277     const MachineOperand &DstMO = MI->getOperand(DstIdx);
1278     unsigned RegA = DstMO.getReg();
1279
1280     // Grab RegB from the instruction because it may have changed if the
1281     // instruction was commuted.
1282     RegB = MI->getOperand(SrcIdx).getReg();
1283
1284     if (RegA == RegB) {
1285       // The register is tied to multiple destinations (or else we would
1286       // not have continued this far), but this use of the register
1287       // already matches the tied destination.  Leave it.
1288       AllUsesCopied = false;
1289       continue;
1290     }
1291     LastCopiedReg = RegA;
1292
1293     assert(TargetRegisterInfo::isVirtualRegister(RegB) &&
1294            "cannot make instruction into two-address form");
1295
1296 #ifndef NDEBUG
1297     // First, verify that we don't have a use of "a" in the instruction
1298     // (a = b + a for example) because our transformation will not
1299     // work. This should never occur because we are in SSA form.
1300     for (unsigned i = 0; i != MI->getNumOperands(); ++i)
1301       assert(i == DstIdx ||
1302              !MI->getOperand(i).isReg() ||
1303              MI->getOperand(i).getReg() != RegA);
1304 #endif
1305
1306     // Emit a copy.
1307     BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1308             TII->get(TargetOpcode::COPY), RegA).addReg(RegB);
1309
1310     // Update DistanceMap.
1311     MachineBasicBlock::iterator PrevMI = MI;
1312     --PrevMI;
1313     DistanceMap.insert(std::make_pair(PrevMI, Dist));
1314     DistanceMap[MI] = ++Dist;
1315
1316     if (LIS) {
1317       LastCopyIdx = LIS->InsertMachineInstrInMaps(PrevMI).getRegSlot();
1318
1319       if (TargetRegisterInfo::isVirtualRegister(RegA)) {
1320         LiveInterval &LI = LIS->getInterval(RegA);
1321         VNInfo *VNI = LI.getNextValue(LastCopyIdx, LIS->getVNInfoAllocator());
1322         SlotIndex endIdx =
1323           LIS->getInstructionIndex(MI).getRegSlot(IsEarlyClobber);
1324         LI.addRange(LiveRange(LastCopyIdx, endIdx, VNI));
1325       }
1326     }
1327
1328     DEBUG(dbgs() << "\t\tprepend:\t" << *PrevMI);
1329
1330     MachineOperand &MO = MI->getOperand(SrcIdx);
1331     assert(MO.isReg() && MO.getReg() == RegB && MO.isUse() &&
1332            "inconsistent operand info for 2-reg pass");
1333     if (MO.isKill()) {
1334       MO.setIsKill(false);
1335       RemovedKillFlag = true;
1336     }
1337
1338     // Make sure regA is a legal regclass for the SrcIdx operand.
1339     if (TargetRegisterInfo::isVirtualRegister(RegA) &&
1340         TargetRegisterInfo::isVirtualRegister(RegB))
1341       MRI->constrainRegClass(RegA, MRI->getRegClass(RegB));
1342
1343     MO.setReg(RegA);
1344
1345     // Propagate SrcRegMap.
1346     SrcRegMap[RegA] = RegB;
1347   }
1348
1349
1350   if (AllUsesCopied) {
1351     if (!IsEarlyClobber) {
1352       // Replace other (un-tied) uses of regB with LastCopiedReg.
1353       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1354         MachineOperand &MO = MI->getOperand(i);
1355         if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) {
1356           if (MO.isKill()) {
1357             MO.setIsKill(false);
1358             RemovedKillFlag = true;
1359           }
1360           MO.setReg(LastCopiedReg);
1361         }
1362       }
1363     }
1364
1365     // Update live variables for regB.
1366     if (RemovedKillFlag && LV && LV->getVarInfo(RegB).removeKill(MI)) {
1367       MachineBasicBlock::iterator PrevMI = MI;
1368       --PrevMI;
1369       LV->addVirtualRegisterKilled(RegB, PrevMI);
1370     }
1371
1372     // Update LiveIntervals.
1373     if (LIS) {
1374       LiveInterval &LI = LIS->getInterval(RegB);
1375       SlotIndex MIIdx = LIS->getInstructionIndex(MI);
1376       LiveInterval::const_iterator I = LI.find(MIIdx);
1377       assert(I != LI.end() && "RegB must be live-in to use.");
1378
1379       SlotIndex UseIdx = MIIdx.getRegSlot(IsEarlyClobber);
1380       if (I->end == UseIdx)
1381         LI.removeRange(LastCopyIdx, UseIdx);
1382     }
1383
1384   } else if (RemovedKillFlag) {
1385     // Some tied uses of regB matched their destination registers, so
1386     // regB is still used in this instruction, but a kill flag was
1387     // removed from a different tied use of regB, so now we need to add
1388     // a kill flag to one of the remaining uses of regB.
1389     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1390       MachineOperand &MO = MI->getOperand(i);
1391       if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) {
1392         MO.setIsKill(true);
1393         break;
1394       }
1395     }
1396   }
1397 }
1398
1399 /// runOnMachineFunction - Reduce two-address instructions to two operands.
1400 ///
1401 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) {
1402   MF = &Func;
1403   const TargetMachine &TM = MF->getTarget();
1404   MRI = &MF->getRegInfo();
1405   TII = TM.getInstrInfo();
1406   TRI = TM.getRegisterInfo();
1407   InstrItins = TM.getInstrItineraryData();
1408   LV = getAnalysisIfAvailable<LiveVariables>();
1409   LIS = getAnalysisIfAvailable<LiveIntervals>();
1410   AA = &getAnalysis<AliasAnalysis>();
1411   OptLevel = TM.getOptLevel();
1412
1413   bool MadeChange = false;
1414
1415   DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
1416   DEBUG(dbgs() << "********** Function: "
1417         << MF->getName() << '\n');
1418
1419   // This pass takes the function out of SSA form.
1420   MRI->leaveSSA();
1421
1422   TiedOperandMap TiedOperands;
1423   for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
1424        MBBI != MBBE; ++MBBI) {
1425     MBB = MBBI;
1426     unsigned Dist = 0;
1427     DistanceMap.clear();
1428     SrcRegMap.clear();
1429     DstRegMap.clear();
1430     Processed.clear();
1431     for (MachineBasicBlock::iterator mi = MBB->begin(), me = MBB->end();
1432          mi != me; ) {
1433       MachineBasicBlock::iterator nmi = llvm::next(mi);
1434       if (mi->isDebugValue()) {
1435         mi = nmi;
1436         continue;
1437       }
1438
1439       // Expand REG_SEQUENCE instructions. This will position mi at the first
1440       // expanded instruction.
1441       if (mi->isRegSequence())
1442         eliminateRegSequence(mi);
1443
1444       DistanceMap.insert(std::make_pair(mi, ++Dist));
1445
1446       processCopy(&*mi);
1447
1448       // First scan through all the tied register uses in this instruction
1449       // and record a list of pairs of tied operands for each register.
1450       if (!collectTiedOperands(mi, TiedOperands)) {
1451         mi = nmi;
1452         continue;
1453       }
1454
1455       ++NumTwoAddressInstrs;
1456       MadeChange = true;
1457       DEBUG(dbgs() << '\t' << *mi);
1458
1459       // If the instruction has a single pair of tied operands, try some
1460       // transformations that may either eliminate the tied operands or
1461       // improve the opportunities for coalescing away the register copy.
1462       if (TiedOperands.size() == 1) {
1463         SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs
1464           = TiedOperands.begin()->second;
1465         if (TiedPairs.size() == 1) {
1466           unsigned SrcIdx = TiedPairs[0].first;
1467           unsigned DstIdx = TiedPairs[0].second;
1468           unsigned SrcReg = mi->getOperand(SrcIdx).getReg();
1469           unsigned DstReg = mi->getOperand(DstIdx).getReg();
1470           if (SrcReg != DstReg &&
1471               tryInstructionTransform(mi, nmi, SrcIdx, DstIdx, Dist)) {
1472             // The tied operands have been eliminated or shifted further down the
1473             // block to ease elimination. Continue processing with 'nmi'.
1474             TiedOperands.clear();
1475             mi = nmi;
1476             continue;
1477           }
1478         }
1479       }
1480
1481       // Now iterate over the information collected above.
1482       for (TiedOperandMap::iterator OI = TiedOperands.begin(),
1483              OE = TiedOperands.end(); OI != OE; ++OI) {
1484         processTiedPairs(mi, OI->second, Dist);
1485         DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
1486       }
1487
1488       // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form.
1489       if (mi->isInsertSubreg()) {
1490         // From %reg = INSERT_SUBREG %reg, %subreg, subidx
1491         // To   %reg:subidx = COPY %subreg
1492         unsigned SubIdx = mi->getOperand(3).getImm();
1493         mi->RemoveOperand(3);
1494         assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx");
1495         mi->getOperand(0).setSubReg(SubIdx);
1496         mi->getOperand(0).setIsUndef(mi->getOperand(1).isUndef());
1497         mi->RemoveOperand(1);
1498         mi->setDesc(TII->get(TargetOpcode::COPY));
1499         DEBUG(dbgs() << "\t\tconvert to:\t" << *mi);
1500       }
1501
1502       // Clear TiedOperands here instead of at the top of the loop
1503       // since most instructions do not have tied operands.
1504       TiedOperands.clear();
1505       mi = nmi;
1506     }
1507   }
1508
1509   if (LIS)
1510     MF->verify(this, "After two-address instruction pass");
1511
1512   return MadeChange;
1513 }
1514
1515 /// Eliminate a REG_SEQUENCE instruction as part of the de-ssa process.
1516 ///
1517 /// The instruction is turned into a sequence of sub-register copies:
1518 ///
1519 ///   %dst = REG_SEQUENCE %v1, ssub0, %v2, ssub1
1520 ///
1521 /// Becomes:
1522 ///
1523 ///   %dst:ssub0<def,undef> = COPY %v1
1524 ///   %dst:ssub1<def> = COPY %v2
1525 ///
1526 void TwoAddressInstructionPass::
1527 eliminateRegSequence(MachineBasicBlock::iterator &MBBI) {
1528   MachineInstr *MI = MBBI;
1529   unsigned DstReg = MI->getOperand(0).getReg();
1530   if (MI->getOperand(0).getSubReg() ||
1531       TargetRegisterInfo::isPhysicalRegister(DstReg) ||
1532       !(MI->getNumOperands() & 1)) {
1533     DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
1534     llvm_unreachable(0);
1535   }
1536
1537   SmallVector<unsigned, 4> OrigRegs;
1538   if (LIS) {
1539     OrigRegs.push_back(MI->getOperand(0).getReg());
1540     for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2)
1541       OrigRegs.push_back(MI->getOperand(i).getReg());
1542   }
1543
1544   bool DefEmitted = false;
1545   for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
1546     MachineOperand &UseMO = MI->getOperand(i);
1547     unsigned SrcReg = UseMO.getReg();
1548     unsigned SubIdx = MI->getOperand(i+1).getImm();
1549     // Nothing needs to be inserted for <undef> operands.
1550     if (UseMO.isUndef())
1551       continue;
1552
1553     // Defer any kill flag to the last operand using SrcReg. Otherwise, we
1554     // might insert a COPY that uses SrcReg after is was killed.
1555     bool isKill = UseMO.isKill();
1556     if (isKill)
1557       for (unsigned j = i + 2; j < e; j += 2)
1558         if (MI->getOperand(j).getReg() == SrcReg) {
1559           MI->getOperand(j).setIsKill();
1560           UseMO.setIsKill(false);
1561           isKill = false;
1562           break;
1563         }
1564
1565     // Insert the sub-register copy.
1566     MachineInstr *CopyMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1567                                    TII->get(TargetOpcode::COPY))
1568       .addReg(DstReg, RegState::Define, SubIdx)
1569       .addOperand(UseMO);
1570
1571     // The first def needs an <undef> flag because there is no live register
1572     // before it.
1573     if (!DefEmitted) {
1574       CopyMI->getOperand(0).setIsUndef(true);
1575       // Return an iterator pointing to the first inserted instr.
1576       MBBI = CopyMI;
1577     }
1578     DefEmitted = true;
1579
1580     // Update LiveVariables' kill info.
1581     if (LV && isKill && !TargetRegisterInfo::isPhysicalRegister(SrcReg))
1582       LV->replaceKillInstruction(SrcReg, MI, CopyMI);
1583
1584     DEBUG(dbgs() << "Inserted: " << *CopyMI);
1585   }
1586
1587   MachineBasicBlock::iterator EndMBBI =
1588       llvm::next(MachineBasicBlock::iterator(MI));
1589
1590   if (!DefEmitted) {
1591     DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF");
1592     MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
1593     for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
1594       MI->RemoveOperand(j);
1595   } else {
1596     DEBUG(dbgs() << "Eliminated: " << *MI);
1597     MI->eraseFromParent();
1598   }
1599
1600   // Udpate LiveIntervals.
1601   if (LIS)
1602     LIS->repairIntervalsInRange(MBB, MBBI, EndMBBI, OrigRegs);
1603 }