When remat'ing and destination virtual register has a sub-register index. Make sure...
[oota-llvm.git] / lib / CodeGen / SimpleRegisterCoalescing.cpp
1 //===-- SimpleRegisterCoalescing.cpp - Register Coalescing ----------------===//
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 a simple register coalescing pass that attempts to
11 // aggressively coalesce every register copy that it can.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "regcoalescing"
16 #include "SimpleRegisterCoalescing.h"
17 #include "VirtRegMap.h"
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/Value.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineLoopInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/RegisterCoalescer.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/ADT/SmallSet.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include <algorithm>
37 #include <cmath>
38 using namespace llvm;
39
40 STATISTIC(numJoins    , "Number of interval joins performed");
41 STATISTIC(numCrossRCs , "Number of cross class joins performed");
42 STATISTIC(numCommutes , "Number of instruction commuting performed");
43 STATISTIC(numExtends  , "Number of copies extended");
44 STATISTIC(NumReMats   , "Number of instructions re-materialized");
45 STATISTIC(numPeep     , "Number of identity moves eliminated after coalescing");
46 STATISTIC(numAborts   , "Number of times interval joining aborted");
47 STATISTIC(numDeadValNo, "Number of valno def marked dead");
48
49 char SimpleRegisterCoalescing::ID = 0;
50 static cl::opt<bool>
51 EnableJoining("join-liveintervals",
52               cl::desc("Coalesce copies (default=true)"),
53               cl::init(true));
54
55 static cl::opt<bool>
56 NewHeuristic("new-coalescer-heuristic",
57              cl::desc("Use new coalescer heuristic"),
58              cl::init(false), cl::Hidden);
59
60 static cl::opt<bool>
61 DisableCrossClassJoin("disable-cross-class-join",
62                cl::desc("Avoid coalescing cross register class copies"),
63                cl::init(false), cl::Hidden);
64
65 static cl::opt<bool>
66 PhysJoinTweak("tweak-phys-join-heuristics",
67                cl::desc("Tweak heuristics for joining phys reg with vr"),
68                cl::init(false), cl::Hidden);
69
70 static RegisterPass<SimpleRegisterCoalescing> 
71 X("simple-register-coalescing", "Simple Register Coalescing");
72
73 // Declare that we implement the RegisterCoalescer interface
74 static RegisterAnalysisGroup<RegisterCoalescer, true/*The Default*/> V(X);
75
76 const PassInfo *const llvm::SimpleRegisterCoalescingID = &X;
77
78 void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage &AU) const {
79   AU.setPreservesCFG();
80   AU.addRequired<LiveIntervals>();
81   AU.addPreserved<LiveIntervals>();
82   AU.addRequired<MachineLoopInfo>();
83   AU.addPreserved<MachineLoopInfo>();
84   AU.addPreservedID(MachineDominatorsID);
85   if (StrongPHIElim)
86     AU.addPreservedID(StrongPHIEliminationID);
87   else
88     AU.addPreservedID(PHIEliminationID);
89   AU.addPreservedID(TwoAddressInstructionPassID);
90   MachineFunctionPass::getAnalysisUsage(AU);
91 }
92
93 /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
94 /// being the source and IntB being the dest, thus this defines a value number
95 /// in IntB.  If the source value number (in IntA) is defined by a copy from B,
96 /// see if we can merge these two pieces of B into a single value number,
97 /// eliminating a copy.  For example:
98 ///
99 ///  A3 = B0
100 ///    ...
101 ///  B1 = A3      <- this copy
102 ///
103 /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
104 /// value number to be replaced with B0 (which simplifies the B liveinterval).
105 ///
106 /// This returns true if an interval was modified.
107 ///
108 bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval &IntA,
109                                                     LiveInterval &IntB,
110                                                     MachineInstr *CopyMI) {
111   MachineInstrIndex CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
112
113   // BValNo is a value number in B that is defined by a copy from A.  'B3' in
114   // the example above.
115   LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
116   assert(BLR != IntB.end() && "Live range not found!");
117   VNInfo *BValNo = BLR->valno;
118   
119   // Get the location that B is defined at.  Two options: either this value has
120   // an unknown definition point or it is defined at CopyIdx.  If unknown, we 
121   // can't process it.
122   if (!BValNo->getCopy()) return false;
123   assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
124   
125   // AValNo is the value number in A that defines the copy, A3 in the example.
126   MachineInstrIndex CopyUseIdx = li_->getUseIndex(CopyIdx);
127   LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyUseIdx);
128   assert(ALR != IntA.end() && "Live range not found!");
129   VNInfo *AValNo = ALR->valno;
130   // If it's re-defined by an early clobber somewhere in the live range, then
131   // it's not safe to eliminate the copy. FIXME: This is a temporary workaround.
132   // See PR3149:
133   // 172     %ECX<def> = MOV32rr %reg1039<kill>
134   // 180     INLINEASM <es:subl $5,$1
135   //         sbbl $3,$0>, 10, %EAX<def>, 14, %ECX<earlyclobber,def>, 9, %EAX<kill>,
136   // 36, <fi#0>, 1, %reg0, 0, 9, %ECX<kill>, 36, <fi#1>, 1, %reg0, 0
137   // 188     %EAX<def> = MOV32rr %EAX<kill>
138   // 196     %ECX<def> = MOV32rr %ECX<kill>
139   // 204     %ECX<def> = MOV32rr %ECX<kill>
140   // 212     %EAX<def> = MOV32rr %EAX<kill>
141   // 220     %EAX<def> = MOV32rr %EAX
142   // 228     %reg1039<def> = MOV32rr %ECX<kill>
143   // The early clobber operand ties ECX input to the ECX def.
144   //
145   // The live interval of ECX is represented as this:
146   // %reg20,inf = [46,47:1)[174,230:0)  0@174-(230) 1@46-(47)
147   // The coalescer has no idea there was a def in the middle of [174,230].
148   if (AValNo->hasRedefByEC())
149     return false;
150   
151   // If AValNo is defined as a copy from IntB, we can potentially process this.  
152   // Get the instruction that defines this value number.
153   unsigned SrcReg = li_->getVNInfoSourceReg(AValNo);
154   if (!SrcReg) return false;  // Not defined by a copy.
155     
156   // If the value number is not defined by a copy instruction, ignore it.
157
158   // If the source register comes from an interval other than IntB, we can't
159   // handle this.
160   if (SrcReg != IntB.reg) return false;
161   
162   // Get the LiveRange in IntB that this value number starts with.
163   LiveInterval::iterator ValLR =
164     IntB.FindLiveRangeContaining(li_->getPrevSlot(AValNo->def));
165   assert(ValLR != IntB.end() && "Live range not found!");
166   
167   // Make sure that the end of the live range is inside the same block as
168   // CopyMI.
169   MachineInstr *ValLREndInst =
170     li_->getInstructionFromIndex(li_->getPrevSlot(ValLR->end));
171   if (!ValLREndInst || 
172       ValLREndInst->getParent() != CopyMI->getParent()) return false;
173
174   // Okay, we now know that ValLR ends in the same block that the CopyMI
175   // live-range starts.  If there are no intervening live ranges between them in
176   // IntB, we can merge them.
177   if (ValLR+1 != BLR) return false;
178
179   // If a live interval is a physical register, conservatively check if any
180   // of its sub-registers is overlapping the live interval of the virtual
181   // register. If so, do not coalesce.
182   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg) &&
183       *tri_->getSubRegisters(IntB.reg)) {
184     for (const unsigned* SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR)
185       if (li_->hasInterval(*SR) && IntA.overlaps(li_->getInterval(*SR))) {
186         DEBUG({
187             errs() << "Interfere with sub-register ";
188             li_->getInterval(*SR).print(errs(), tri_);
189           });
190         return false;
191       }
192   }
193   
194   DEBUG({
195       errs() << "\nExtending: ";
196       IntB.print(errs(), tri_);
197     });
198   
199   MachineInstrIndex FillerStart = ValLR->end, FillerEnd = BLR->start;
200   // We are about to delete CopyMI, so need to remove it as the 'instruction
201   // that defines this value #'. Update the the valnum with the new defining
202   // instruction #.
203   BValNo->def  = FillerStart;
204   BValNo->setCopy(0);
205   
206   // Okay, we can merge them.  We need to insert a new liverange:
207   // [ValLR.end, BLR.begin) of either value number, then we merge the
208   // two value numbers.
209   IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
210
211   // If the IntB live range is assigned to a physical register, and if that
212   // physreg has sub-registers, update their live intervals as well. 
213   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
214     for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
215       LiveInterval &SRLI = li_->getInterval(*SR);
216       SRLI.addRange(LiveRange(FillerStart, FillerEnd,
217                               SRLI.getNextValue(FillerStart, 0, true,
218                                                 li_->getVNInfoAllocator())));
219     }
220   }
221
222   // Okay, merge "B1" into the same value number as "B0".
223   if (BValNo != ValLR->valno) {
224     IntB.addKills(ValLR->valno, BValNo->kills);
225     IntB.MergeValueNumberInto(BValNo, ValLR->valno);
226   }
227   DEBUG({
228       errs() << "   result = ";
229       IntB.print(errs(), tri_);
230       errs() << "\n";
231     });
232
233   // If the source instruction was killing the source register before the
234   // merge, unset the isKill marker given the live range has been extended.
235   int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
236   if (UIdx != -1) {
237     ValLREndInst->getOperand(UIdx).setIsKill(false);
238     ValLR->valno->removeKill(FillerStart);
239   }
240
241   // If the copy instruction was killing the destination register before the
242   // merge, find the last use and trim the live range. That will also add the
243   // isKill marker.
244   if (CopyMI->killsRegister(IntA.reg))
245     TrimLiveIntervalToLastUse(CopyUseIdx, CopyMI->getParent(), IntA, ALR);
246
247   ++numExtends;
248   return true;
249 }
250
251 /// HasOtherReachingDefs - Return true if there are definitions of IntB
252 /// other than BValNo val# that can reach uses of AValno val# of IntA.
253 bool SimpleRegisterCoalescing::HasOtherReachingDefs(LiveInterval &IntA,
254                                                     LiveInterval &IntB,
255                                                     VNInfo *AValNo,
256                                                     VNInfo *BValNo) {
257   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
258        AI != AE; ++AI) {
259     if (AI->valno != AValNo) continue;
260     LiveInterval::Ranges::iterator BI =
261       std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start);
262     if (BI != IntB.ranges.begin())
263       --BI;
264     for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
265       if (BI->valno == BValNo)
266         continue;
267       if (BI->start <= AI->start && BI->end > AI->start)
268         return true;
269       if (BI->start > AI->start && BI->start < AI->end)
270         return true;
271     }
272   }
273   return false;
274 }
275
276 /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy with IntA
277 /// being the source and IntB being the dest, thus this defines a value number
278 /// in IntB.  If the source value number (in IntA) is defined by a commutable
279 /// instruction and its other operand is coalesced to the copy dest register,
280 /// see if we can transform the copy into a noop by commuting the definition. For
281 /// example,
282 ///
283 ///  A3 = op A2 B0<kill>
284 ///    ...
285 ///  B1 = A3      <- this copy
286 ///    ...
287 ///     = op A3   <- more uses
288 ///
289 /// ==>
290 ///
291 ///  B2 = op B0 A2<kill>
292 ///    ...
293 ///  B1 = B2      <- now an identify copy
294 ///    ...
295 ///     = op B2   <- more uses
296 ///
297 /// This returns true if an interval was modified.
298 ///
299 bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
300                                                         LiveInterval &IntB,
301                                                         MachineInstr *CopyMI) {
302   MachineInstrIndex CopyIdx =
303     li_->getDefIndex(li_->getInstructionIndex(CopyMI));
304
305   // FIXME: For now, only eliminate the copy by commuting its def when the
306   // source register is a virtual register. We want to guard against cases
307   // where the copy is a back edge copy and commuting the def lengthen the
308   // live interval of the source register to the entire loop.
309   if (TargetRegisterInfo::isPhysicalRegister(IntA.reg))
310     return false;
311
312   // BValNo is a value number in B that is defined by a copy from A. 'B3' in
313   // the example above.
314   LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
315   assert(BLR != IntB.end() && "Live range not found!");
316   VNInfo *BValNo = BLR->valno;
317   
318   // Get the location that B is defined at.  Two options: either this value has
319   // an unknown definition point or it is defined at CopyIdx.  If unknown, we 
320   // can't process it.
321   if (!BValNo->getCopy()) return false;
322   assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
323   
324   // AValNo is the value number in A that defines the copy, A3 in the example.
325   LiveInterval::iterator ALR =
326     IntA.FindLiveRangeContaining(li_->getPrevSlot(CopyIdx));
327
328   assert(ALR != IntA.end() && "Live range not found!");
329   VNInfo *AValNo = ALR->valno;
330   // If other defs can reach uses of this def, then it's not safe to perform
331   // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
332   // tested?
333   if (AValNo->isPHIDef() || !AValNo->isDefAccurate() ||
334       AValNo->isUnused() || AValNo->hasPHIKill())
335     return false;
336   MachineInstr *DefMI = li_->getInstructionFromIndex(AValNo->def);
337   const TargetInstrDesc &TID = DefMI->getDesc();
338   if (!TID.isCommutable())
339     return false;
340   // If DefMI is a two-address instruction then commuting it will change the
341   // destination register.
342   int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg);
343   assert(DefIdx != -1);
344   unsigned UseOpIdx;
345   if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
346     return false;
347   unsigned Op1, Op2, NewDstIdx;
348   if (!tii_->findCommutedOpIndices(DefMI, Op1, Op2))
349     return false;
350   if (Op1 == UseOpIdx)
351     NewDstIdx = Op2;
352   else if (Op2 == UseOpIdx)
353     NewDstIdx = Op1;
354   else
355     return false;
356
357   MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
358   unsigned NewReg = NewDstMO.getReg();
359   if (NewReg != IntB.reg || !NewDstMO.isKill())
360     return false;
361
362   // Make sure there are no other definitions of IntB that would reach the
363   // uses which the new definition can reach.
364   if (HasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
365     return false;
366
367   // If some of the uses of IntA.reg is already coalesced away, return false.
368   // It's not possible to determine whether it's safe to perform the coalescing.
369   for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg),
370          UE = mri_->use_end(); UI != UE; ++UI) {
371     MachineInstr *UseMI = &*UI;
372     MachineInstrIndex UseIdx = li_->getInstructionIndex(UseMI);
373     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
374     if (ULR == IntA.end())
375       continue;
376     if (ULR->valno == AValNo && JoinedCopies.count(UseMI))
377       return false;
378   }
379
380   // At this point we have decided that it is legal to do this
381   // transformation.  Start by commuting the instruction.
382   MachineBasicBlock *MBB = DefMI->getParent();
383   MachineInstr *NewMI = tii_->commuteInstruction(DefMI);
384   if (!NewMI)
385     return false;
386   if (NewMI != DefMI) {
387     li_->ReplaceMachineInstrInMaps(DefMI, NewMI);
388     MBB->insert(DefMI, NewMI);
389     MBB->erase(DefMI);
390   }
391   unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
392   NewMI->getOperand(OpIdx).setIsKill();
393
394   bool BHasPHIKill = BValNo->hasPHIKill();
395   SmallVector<VNInfo*, 4> BDeadValNos;
396   VNInfo::KillSet BKills;
397   std::map<MachineInstrIndex, MachineInstrIndex> BExtend;
398
399   // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
400   // A = or A, B
401   // ...
402   // B = A
403   // ...
404   // C = A<kill>
405   // ...
406   //   = B
407   //
408   // then do not add kills of A to the newly created B interval.
409   bool Extended = BLR->end > ALR->end && ALR->end != ALR->start;
410   if (Extended)
411     BExtend[ALR->end] = BLR->end;
412
413   // Update uses of IntA of the specific Val# with IntB.
414   bool BHasSubRegs = false;
415   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg))
416     BHasSubRegs = *tri_->getSubRegisters(IntB.reg);
417   for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg),
418          UE = mri_->use_end(); UI != UE;) {
419     MachineOperand &UseMO = UI.getOperand();
420     MachineInstr *UseMI = &*UI;
421     ++UI;
422     if (JoinedCopies.count(UseMI))
423       continue;
424     MachineInstrIndex UseIdx = li_->getInstructionIndex(UseMI);
425     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
426     if (ULR == IntA.end() || ULR->valno != AValNo)
427       continue;
428     UseMO.setReg(NewReg);
429     if (UseMI == CopyMI)
430       continue;
431     if (UseMO.isKill()) {
432       if (Extended)
433         UseMO.setIsKill(false);
434       else
435         BKills.push_back(li_->getNextSlot(li_->getUseIndex(UseIdx)));
436     }
437     unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
438     if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
439       continue;
440     if (DstReg == IntB.reg) {
441       // This copy will become a noop. If it's defining a new val#,
442       // remove that val# as well. However this live range is being
443       // extended to the end of the existing live range defined by the copy.
444       MachineInstrIndex DefIdx = li_->getDefIndex(UseIdx);
445       const LiveRange *DLR = IntB.getLiveRangeContaining(DefIdx);
446       BHasPHIKill |= DLR->valno->hasPHIKill();
447       assert(DLR->valno->def == DefIdx);
448       BDeadValNos.push_back(DLR->valno);
449       BExtend[DLR->start] = DLR->end;
450       JoinedCopies.insert(UseMI);
451       // If this is a kill but it's going to be removed, the last use
452       // of the same val# is the new kill.
453       if (UseMO.isKill())
454         BKills.pop_back();
455     }
456   }
457
458   // We need to insert a new liverange: [ALR.start, LastUse). It may be we can
459   // simply extend BLR if CopyMI doesn't end the range.
460   DEBUG({
461       errs() << "\nExtending: ";
462       IntB.print(errs(), tri_);
463     });
464
465   // Remove val#'s defined by copies that will be coalesced away.
466   for (unsigned i = 0, e = BDeadValNos.size(); i != e; ++i) {
467     VNInfo *DeadVNI = BDeadValNos[i];
468     if (BHasSubRegs) {
469       for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
470         LiveInterval &SRLI = li_->getInterval(*SR);
471         const LiveRange *SRLR = SRLI.getLiveRangeContaining(DeadVNI->def);
472         SRLI.removeValNo(SRLR->valno);
473       }
474     }
475     IntB.removeValNo(BDeadValNos[i]);
476   }
477
478   // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
479   // is updated. Kills are also updated.
480   VNInfo *ValNo = BValNo;
481   ValNo->def = AValNo->def;
482   ValNo->setCopy(0);
483   for (unsigned j = 0, ee = ValNo->kills.size(); j != ee; ++j) {
484     if (ValNo->kills[j] != BLR->end)
485       BKills.push_back(ValNo->kills[j]);
486   }
487   ValNo->kills.clear();
488   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
489        AI != AE; ++AI) {
490     if (AI->valno != AValNo) continue;
491     MachineInstrIndex End = AI->end;
492     std::map<MachineInstrIndex, MachineInstrIndex>::iterator
493       EI = BExtend.find(End);
494     if (EI != BExtend.end())
495       End = EI->second;
496     IntB.addRange(LiveRange(AI->start, End, ValNo));
497
498     // If the IntB live range is assigned to a physical register, and if that
499     // physreg has sub-registers, update their live intervals as well. 
500     if (BHasSubRegs) {
501       for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
502         LiveInterval &SRLI = li_->getInterval(*SR);
503         SRLI.MergeInClobberRange(AI->start, End, li_->getVNInfoAllocator());
504       }
505     }
506   }
507   IntB.addKills(ValNo, BKills);
508   ValNo->setHasPHIKill(BHasPHIKill);
509
510   DEBUG({
511       errs() << "   result = ";
512       IntB.print(errs(), tri_);
513       errs() << '\n';
514       errs() << "\nShortening: ";
515       IntA.print(errs(), tri_);
516     });
517
518   IntA.removeValNo(AValNo);
519
520   DEBUG({
521       errs() << "   result = ";
522       IntA.print(errs(), tri_);
523       errs() << '\n';
524     });
525
526   ++numCommutes;
527   return true;
528 }
529
530 /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
531 /// fallthoughs to SuccMBB.
532 static bool isSameOrFallThroughBB(MachineBasicBlock *MBB,
533                                   MachineBasicBlock *SuccMBB,
534                                   const TargetInstrInfo *tii_) {
535   if (MBB == SuccMBB)
536     return true;
537   MachineBasicBlock *TBB = 0, *FBB = 0;
538   SmallVector<MachineOperand, 4> Cond;
539   return !tii_->AnalyzeBranch(*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
540     MBB->isSuccessor(SuccMBB);
541 }
542
543 /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range
544 /// from a physical register live interval as well as from the live intervals
545 /// of its sub-registers.
546 static void removeRange(LiveInterval &li,
547                         MachineInstrIndex Start, MachineInstrIndex End,
548                         LiveIntervals *li_, const TargetRegisterInfo *tri_) {
549   li.removeRange(Start, End, true);
550   if (TargetRegisterInfo::isPhysicalRegister(li.reg)) {
551     for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
552       if (!li_->hasInterval(*SR))
553         continue;
554       LiveInterval &sli = li_->getInterval(*SR);
555       MachineInstrIndex RemoveEnd = Start;
556       while (RemoveEnd != End) {
557         LiveInterval::iterator LR = sli.FindLiveRangeContaining(Start);
558         if (LR == sli.end())
559           break;
560         RemoveEnd = (LR->end < End) ? LR->end : End;
561         sli.removeRange(Start, RemoveEnd, true);
562         Start = RemoveEnd;
563       }
564     }
565   }
566 }
567
568 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic block
569 /// as the copy instruction, trim the live interval to the last use and return
570 /// true.
571 bool
572 SimpleRegisterCoalescing::TrimLiveIntervalToLastUse(MachineInstrIndex CopyIdx,
573                                                     MachineBasicBlock *CopyMBB,
574                                                     LiveInterval &li,
575                                                     const LiveRange *LR) {
576   MachineInstrIndex MBBStart = li_->getMBBStartIdx(CopyMBB);
577   MachineInstrIndex LastUseIdx;
578   MachineOperand *LastUse =
579     lastRegisterUse(LR->start, li_->getPrevSlot(CopyIdx), li.reg, LastUseIdx);
580   if (LastUse) {
581     MachineInstr *LastUseMI = LastUse->getParent();
582     if (!isSameOrFallThroughBB(LastUseMI->getParent(), CopyMBB, tii_)) {
583       // r1024 = op
584       // ...
585       // BB1:
586       //       = r1024
587       //
588       // BB2:
589       // r1025<dead> = r1024<kill>
590       if (MBBStart < LR->end)
591         removeRange(li, MBBStart, LR->end, li_, tri_);
592       return true;
593     }
594
595     // There are uses before the copy, just shorten the live range to the end
596     // of last use.
597     LastUse->setIsKill();
598     removeRange(li, li_->getDefIndex(LastUseIdx), LR->end, li_, tri_);
599     LR->valno->addKill(li_->getNextSlot(LastUseIdx));
600     unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
601     if (tii_->isMoveInstr(*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
602         DstReg == li.reg) {
603       // Last use is itself an identity code.
604       int DeadIdx = LastUseMI->findRegisterDefOperandIdx(li.reg, false, tri_);
605       LastUseMI->getOperand(DeadIdx).setIsDead();
606     }
607     return true;
608   }
609
610   // Is it livein?
611   if (LR->start <= MBBStart && LR->end > MBBStart) {
612     if (LR->start == MachineInstrIndex()) {
613       assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
614       // Live-in to the function but dead. Remove it from entry live-in set.
615       mf_->begin()->removeLiveIn(li.reg);
616     }
617     // FIXME: Shorten intervals in BBs that reaches this BB.
618   }
619
620   return false;
621 }
622
623 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
624 /// computation, replace the copy by rematerialize the definition.
625 bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
626                                                        unsigned DstReg,
627                                                        unsigned DstSubIdx,
628                                                        MachineInstr *CopyMI) {
629   MachineInstrIndex CopyIdx = li_->getUseIndex(li_->getInstructionIndex(CopyMI));
630   LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx);
631   assert(SrcLR != SrcInt.end() && "Live range not found!");
632   VNInfo *ValNo = SrcLR->valno;
633   // If other defs can reach uses of this def, then it's not safe to perform
634   // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
635   // tested?
636   if (ValNo->isPHIDef() || !ValNo->isDefAccurate() ||
637       ValNo->isUnused() || ValNo->hasPHIKill())
638     return false;
639   MachineInstr *DefMI = li_->getInstructionFromIndex(ValNo->def);
640   const TargetInstrDesc &TID = DefMI->getDesc();
641   if (!TID.isAsCheapAsAMove())
642     return false;
643   if (!DefMI->getDesc().isRematerializable() ||
644       !tii_->isTriviallyReMaterializable(DefMI))
645     return false;
646   bool SawStore = false;
647   if (!DefMI->isSafeToMove(tii_, SawStore))
648     return false;
649   if (TID.getNumDefs() != 1)
650     return false;
651   if (DefMI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
652     // Make sure the copy destination register class fits the instruction
653     // definition register class. The mismatch can happen as a result of earlier
654     // extract_subreg, insert_subreg, subreg_to_reg coalescing.
655     const TargetRegisterClass *RC = TID.OpInfo[0].getRegClass(tri_);
656     if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
657       if (mri_->getRegClass(DstReg) != RC)
658         return false;
659     } else if (!RC->contains(DstReg))
660       return false;
661   }
662
663   // If destination register has a sub-register index on it, make sure it mtches
664   // the instruction register class.
665   if (DstSubIdx) {
666     const TargetInstrDesc &TID = DefMI->getDesc();
667     if (TID.getNumDefs() != 1)
668       return false;
669     const TargetRegisterClass *DstRC = mri_->getRegClass(DstReg);
670     const TargetRegisterClass *DstSubRC =
671       DstRC->getSubRegisterRegClass(DstSubIdx);
672     const TargetRegisterClass *DefRC = TID.OpInfo[0].getRegClass(tri_);
673     if (DefRC == DstRC)
674       DstSubIdx = 0;
675     else if (DefRC != DstSubRC)
676       return false;
677   }
678
679   MachineInstrIndex DefIdx = li_->getDefIndex(CopyIdx);
680   const LiveRange *DLR= li_->getInterval(DstReg).getLiveRangeContaining(DefIdx);
681   DLR->valno->setCopy(0);
682   // Don't forget to update sub-register intervals.
683   if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
684     for (const unsigned* SR = tri_->getSubRegisters(DstReg); *SR; ++SR) {
685       if (!li_->hasInterval(*SR))
686         continue;
687       DLR = li_->getInterval(*SR).getLiveRangeContaining(DefIdx);
688       if (DLR && DLR->valno->getCopy() == CopyMI)
689         DLR->valno->setCopy(0);
690     }
691   }
692
693   // If copy kills the source register, find the last use and propagate
694   // kill.
695   bool checkForDeadDef = false;
696   MachineBasicBlock *MBB = CopyMI->getParent();
697   if (CopyMI->killsRegister(SrcInt.reg))
698     if (!TrimLiveIntervalToLastUse(CopyIdx, MBB, SrcInt, SrcLR)) {
699       checkForDeadDef = true;
700     }
701
702   MachineBasicBlock::iterator MII = next(MachineBasicBlock::iterator(CopyMI));
703   tii_->reMaterialize(*MBB, MII, DstReg, DstSubIdx, DefMI);
704   MachineInstr *NewMI = prior(MII);
705
706   if (checkForDeadDef) {
707     // PR4090 fix: Trim interval failed because there was no use of the
708     // source interval in this MBB. If the def is in this MBB too then we
709     // should mark it dead:
710     if (DefMI->getParent() == MBB) {
711       DefMI->addRegisterDead(SrcInt.reg, tri_);
712       SrcLR->end = li_->getNextSlot(SrcLR->start);
713     }
714   }
715
716   // CopyMI may have implicit operands, transfer them over to the newly
717   // rematerialized instruction. And update implicit def interval valnos.
718   for (unsigned i = CopyMI->getDesc().getNumOperands(),
719          e = CopyMI->getNumOperands(); i != e; ++i) {
720     MachineOperand &MO = CopyMI->getOperand(i);
721     if (MO.isReg() && MO.isImplicit())
722       NewMI->addOperand(MO);
723     if (MO.isDef() && li_->hasInterval(MO.getReg())) {
724       unsigned Reg = MO.getReg();
725       DLR = li_->getInterval(Reg).getLiveRangeContaining(DefIdx);
726       if (DLR && DLR->valno->getCopy() == CopyMI)
727         DLR->valno->setCopy(0);
728     }
729   }
730
731   li_->ReplaceMachineInstrInMaps(CopyMI, NewMI);
732   CopyMI->eraseFromParent();
733   ReMatCopies.insert(CopyMI);
734   ReMatDefs.insert(DefMI);
735   ++NumReMats;
736   return true;
737 }
738
739 /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
740 ///
741 bool SimpleRegisterCoalescing::isBackEdgeCopy(MachineInstr *CopyMI,
742                                               unsigned DstReg) const {
743   MachineBasicBlock *MBB = CopyMI->getParent();
744   const MachineLoop *L = loopInfo->getLoopFor(MBB);
745   if (!L)
746     return false;
747   if (MBB != L->getLoopLatch())
748     return false;
749
750   LiveInterval &LI = li_->getInterval(DstReg);
751   MachineInstrIndex DefIdx = li_->getInstructionIndex(CopyMI);
752   LiveInterval::const_iterator DstLR =
753     LI.FindLiveRangeContaining(li_->getDefIndex(DefIdx));
754   if (DstLR == LI.end())
755     return false;
756   if (DstLR->valno->kills.size() == 1 && DstLR->valno->kills[0].isPHIIndex())
757     return true;
758   return false;
759 }
760
761 /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
762 /// update the subregister number if it is not zero. If DstReg is a
763 /// physical register and the existing subregister number of the def / use
764 /// being updated is not zero, make sure to set it to the correct physical
765 /// subregister.
766 void
767 SimpleRegisterCoalescing::UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg,
768                                             unsigned SubIdx) {
769   bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
770   if (DstIsPhys && SubIdx) {
771     // Figure out the real physical register we are updating with.
772     DstReg = tri_->getSubReg(DstReg, SubIdx);
773     SubIdx = 0;
774   }
775
776   for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(SrcReg),
777          E = mri_->reg_end(); I != E; ) {
778     MachineOperand &O = I.getOperand();
779     MachineInstr *UseMI = &*I;
780     ++I;
781     unsigned OldSubIdx = O.getSubReg();
782     if (DstIsPhys) {
783       unsigned UseDstReg = DstReg;
784       if (OldSubIdx)
785           UseDstReg = tri_->getSubReg(DstReg, OldSubIdx);
786
787       unsigned CopySrcReg, CopyDstReg, CopySrcSubIdx, CopyDstSubIdx;
788       if (tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg,
789                             CopySrcSubIdx, CopyDstSubIdx) &&
790           CopySrcReg != CopyDstReg &&
791           CopySrcReg == SrcReg && CopyDstReg != UseDstReg) {
792         // If the use is a copy and it won't be coalesced away, and its source
793         // is defined by a trivial computation, try to rematerialize it instead.
794         if (ReMaterializeTrivialDef(li_->getInterval(SrcReg), CopyDstReg,
795                                     CopyDstSubIdx, UseMI))
796           continue;
797       }
798
799       O.setReg(UseDstReg);
800       O.setSubReg(0);
801       continue;
802     }
803
804     // Sub-register indexes goes from small to large. e.g.
805     // RAX: 1 -> AL, 2 -> AX, 3 -> EAX
806     // EAX: 1 -> AL, 2 -> AX
807     // So RAX's sub-register 2 is AX, RAX's sub-regsiter 3 is EAX, whose
808     // sub-register 2 is also AX.
809     if (SubIdx && OldSubIdx && SubIdx != OldSubIdx)
810       assert(OldSubIdx < SubIdx && "Conflicting sub-register index!");
811     else if (SubIdx)
812       O.setSubReg(SubIdx);
813     // Remove would-be duplicated kill marker.
814     if (O.isKill() && UseMI->killsRegister(DstReg))
815       O.setIsKill(false);
816     O.setReg(DstReg);
817
818     // After updating the operand, check if the machine instruction has
819     // become a copy. If so, update its val# information.
820     if (JoinedCopies.count(UseMI))
821       continue;
822
823     const TargetInstrDesc &TID = UseMI->getDesc();
824     unsigned CopySrcReg, CopyDstReg, CopySrcSubIdx, CopyDstSubIdx;
825     if (TID.getNumDefs() == 1 && TID.getNumOperands() > 2 &&
826         tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg,
827                           CopySrcSubIdx, CopyDstSubIdx) &&
828         CopySrcReg != CopyDstReg &&
829         (TargetRegisterInfo::isVirtualRegister(CopyDstReg) ||
830          allocatableRegs_[CopyDstReg])) {
831       LiveInterval &LI = li_->getInterval(CopyDstReg);
832       MachineInstrIndex DefIdx =
833         li_->getDefIndex(li_->getInstructionIndex(UseMI));
834       if (const LiveRange *DLR = LI.getLiveRangeContaining(DefIdx)) {
835         if (DLR->valno->def == DefIdx)
836           DLR->valno->setCopy(UseMI);
837       }
838     }
839   }
840 }
841
842 /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
843 /// due to live range lengthening as the result of coalescing.
844 void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg,
845                                                       LiveInterval &LI) {
846   for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
847          UE = mri_->use_end(); UI != UE; ++UI) {
848     MachineOperand &UseMO = UI.getOperand();
849     if (!UseMO.isKill())
850       continue;
851     MachineInstr *UseMI = UseMO.getParent();
852     MachineInstrIndex UseIdx =
853       li_->getUseIndex(li_->getInstructionIndex(UseMI));
854     const LiveRange *LR = LI.getLiveRangeContaining(UseIdx);
855     if (!LR || !LR->valno->isKill(li_->getNextSlot(UseIdx))) {
856       if (LR->valno->def != li_->getNextSlot(UseIdx)) {
857         // Interesting problem. After coalescing reg1027's def and kill are both
858         // at the same point:  %reg1027,0.000000e+00 = [56,814:0)  0@70-(814)
859         //
860         // bb5:
861         // 60   %reg1027<def> = t2MOVr %reg1027, 14, %reg0, %reg0
862         // 68   %reg1027<def> = t2LDRi12 %reg1027<kill>, 8, 14, %reg0
863         // 76   t2CMPzri %reg1038<kill,undef>, 0, 14, %reg0, %CPSR<imp-def>
864         // 84   %reg1027<def> = t2MOVr %reg1027, 14, %reg0, %reg0
865         // 96   t2Bcc mbb<bb5,0x2030910>, 1, %CPSR<kill>
866         //
867         // Do not remove the kill marker on t2LDRi12.
868         UseMO.setIsKill(false);
869       }
870     }
871   }
872 }
873
874 /// removeIntervalIfEmpty - Check if the live interval of a physical register
875 /// is empty, if so remove it and also remove the empty intervals of its
876 /// sub-registers. Return true if live interval is removed.
877 static bool removeIntervalIfEmpty(LiveInterval &li, LiveIntervals *li_,
878                                   const TargetRegisterInfo *tri_) {
879   if (li.empty()) {
880     if (TargetRegisterInfo::isPhysicalRegister(li.reg))
881       for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
882         if (!li_->hasInterval(*SR))
883           continue;
884         LiveInterval &sli = li_->getInterval(*SR);
885         if (sli.empty())
886           li_->removeInterval(*SR);
887       }
888     li_->removeInterval(li.reg);
889     return true;
890   }
891   return false;
892 }
893
894 /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy.
895 /// Return true if live interval is removed.
896 bool SimpleRegisterCoalescing::ShortenDeadCopyLiveRange(LiveInterval &li,
897                                                         MachineInstr *CopyMI) {
898   MachineInstrIndex CopyIdx = li_->getInstructionIndex(CopyMI);
899   LiveInterval::iterator MLR =
900     li.FindLiveRangeContaining(li_->getDefIndex(CopyIdx));
901   if (MLR == li.end())
902     return false;  // Already removed by ShortenDeadCopySrcLiveRange.
903   MachineInstrIndex RemoveStart = MLR->start;
904   MachineInstrIndex RemoveEnd = MLR->end;
905   MachineInstrIndex DefIdx = li_->getDefIndex(CopyIdx);
906   // Remove the liverange that's defined by this.
907   if (RemoveStart == DefIdx && RemoveEnd == li_->getNextSlot(DefIdx)) {
908     removeRange(li, RemoveStart, RemoveEnd, li_, tri_);
909     return removeIntervalIfEmpty(li, li_, tri_);
910   }
911   return false;
912 }
913
914 /// RemoveDeadDef - If a def of a live interval is now determined dead, remove
915 /// the val# it defines. If the live interval becomes empty, remove it as well.
916 bool SimpleRegisterCoalescing::RemoveDeadDef(LiveInterval &li,
917                                              MachineInstr *DefMI) {
918   MachineInstrIndex DefIdx = li_->getDefIndex(li_->getInstructionIndex(DefMI));
919   LiveInterval::iterator MLR = li.FindLiveRangeContaining(DefIdx);
920   if (DefIdx != MLR->valno->def)
921     return false;
922   li.removeValNo(MLR->valno);
923   return removeIntervalIfEmpty(li, li_, tri_);
924 }
925
926 /// PropagateDeadness - Propagate the dead marker to the instruction which
927 /// defines the val#.
928 static void PropagateDeadness(LiveInterval &li, MachineInstr *CopyMI,
929                               MachineInstrIndex &LRStart, LiveIntervals *li_,
930                               const TargetRegisterInfo* tri_) {
931   MachineInstr *DefMI =
932     li_->getInstructionFromIndex(li_->getDefIndex(LRStart));
933   if (DefMI && DefMI != CopyMI) {
934     int DeadIdx = DefMI->findRegisterDefOperandIdx(li.reg, false);
935     if (DeadIdx != -1)
936       DefMI->getOperand(DeadIdx).setIsDead();
937     else
938       DefMI->addOperand(MachineOperand::CreateReg(li.reg,
939                                                   true, true, false, true));
940     LRStart = li_->getNextSlot(LRStart);
941   }
942 }
943
944 /// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
945 /// extended by a dead copy. Mark the last use (if any) of the val# as kill as
946 /// ends the live range there. If there isn't another use, then this live range
947 /// is dead. Return true if live interval is removed.
948 bool
949 SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li,
950                                                       MachineInstr *CopyMI) {
951   MachineInstrIndex CopyIdx = li_->getInstructionIndex(CopyMI);
952   if (CopyIdx == MachineInstrIndex()) {
953     // FIXME: special case: function live in. It can be a general case if the
954     // first instruction index starts at > 0 value.
955     assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
956     // Live-in to the function but dead. Remove it from entry live-in set.
957     if (mf_->begin()->isLiveIn(li.reg))
958       mf_->begin()->removeLiveIn(li.reg);
959     const LiveRange *LR = li.getLiveRangeContaining(CopyIdx);
960     removeRange(li, LR->start, LR->end, li_, tri_);
961     return removeIntervalIfEmpty(li, li_, tri_);
962   }
963
964   LiveInterval::iterator LR =
965     li.FindLiveRangeContaining(li_->getPrevSlot(CopyIdx));
966   if (LR == li.end())
967     // Livein but defined by a phi.
968     return false;
969
970   MachineInstrIndex RemoveStart = LR->start;
971   MachineInstrIndex RemoveEnd = li_->getNextSlot(li_->getDefIndex(CopyIdx));
972   if (LR->end > RemoveEnd)
973     // More uses past this copy? Nothing to do.
974     return false;
975
976   // If there is a last use in the same bb, we can't remove the live range.
977   // Shorten the live interval and return.
978   MachineBasicBlock *CopyMBB = CopyMI->getParent();
979   if (TrimLiveIntervalToLastUse(CopyIdx, CopyMBB, li, LR))
980     return false;
981
982   // There are other kills of the val#. Nothing to do.
983   if (!li.isOnlyLROfValNo(LR))
984     return false;
985
986   MachineBasicBlock *StartMBB = li_->getMBBFromIndex(RemoveStart);
987   if (!isSameOrFallThroughBB(StartMBB, CopyMBB, tii_))
988     // If the live range starts in another mbb and the copy mbb is not a fall
989     // through mbb, then we can only cut the range from the beginning of the
990     // copy mbb.
991     RemoveStart = li_->getNextSlot(li_->getMBBStartIdx(CopyMBB));
992
993   if (LR->valno->def == RemoveStart) {
994     // If the def MI defines the val# and this copy is the only kill of the
995     // val#, then propagate the dead marker.
996     PropagateDeadness(li, CopyMI, RemoveStart, li_, tri_);
997     ++numDeadValNo;
998
999     if (LR->valno->isKill(RemoveEnd))
1000       LR->valno->removeKill(RemoveEnd);
1001   }
1002
1003   removeRange(li, RemoveStart, RemoveEnd, li_, tri_);
1004   return removeIntervalIfEmpty(li, li_, tri_);
1005 }
1006
1007 /// CanCoalesceWithImpDef - Returns true if the specified copy instruction
1008 /// from an implicit def to another register can be coalesced away.
1009 bool SimpleRegisterCoalescing::CanCoalesceWithImpDef(MachineInstr *CopyMI,
1010                                                      LiveInterval &li,
1011                                                      LiveInterval &ImpLi) const{
1012   if (!CopyMI->killsRegister(ImpLi.reg))
1013     return false;
1014   // Make sure this is the only use.
1015   for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(ImpLi.reg),
1016          UE = mri_->use_end(); UI != UE;) {
1017     MachineInstr *UseMI = &*UI;
1018     ++UI;
1019     if (CopyMI == UseMI || JoinedCopies.count(UseMI))
1020       continue;
1021     return false;
1022   }
1023   return true;
1024 }
1025
1026
1027 /// isWinToJoinVRWithSrcPhysReg - Return true if it's worth while to join a
1028 /// a virtual destination register with physical source register.
1029 bool
1030 SimpleRegisterCoalescing::isWinToJoinVRWithSrcPhysReg(MachineInstr *CopyMI,
1031                                                      MachineBasicBlock *CopyMBB,
1032                                                      LiveInterval &DstInt,
1033                                                      LiveInterval &SrcInt) {
1034   // If the virtual register live interval is long but it has low use desity,
1035   // do not join them, instead mark the physical register as its allocation
1036   // preference.
1037   const TargetRegisterClass *RC = mri_->getRegClass(DstInt.reg);
1038   unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
1039   unsigned Length = li_->getApproximateInstructionCount(DstInt);
1040   if (Length > Threshold &&
1041       (((float)std::distance(mri_->use_begin(DstInt.reg),
1042                              mri_->use_end()) / Length) < (1.0 / Threshold)))
1043     return false;
1044
1045   // If the virtual register live interval extends into a loop, turn down
1046   // aggressiveness.
1047   MachineInstrIndex CopyIdx =
1048     li_->getDefIndex(li_->getInstructionIndex(CopyMI));
1049   const MachineLoop *L = loopInfo->getLoopFor(CopyMBB);
1050   if (!L) {
1051     // Let's see if the virtual register live interval extends into the loop.
1052     LiveInterval::iterator DLR = DstInt.FindLiveRangeContaining(CopyIdx);
1053     assert(DLR != DstInt.end() && "Live range not found!");
1054     DLR = DstInt.FindLiveRangeContaining(li_->getNextSlot(DLR->end));
1055     if (DLR != DstInt.end()) {
1056       CopyMBB = li_->getMBBFromIndex(DLR->start);
1057       L = loopInfo->getLoopFor(CopyMBB);
1058     }
1059   }
1060
1061   if (!L || Length <= Threshold)
1062     return true;
1063
1064   MachineInstrIndex UseIdx = li_->getUseIndex(CopyIdx);
1065   LiveInterval::iterator SLR = SrcInt.FindLiveRangeContaining(UseIdx);
1066   MachineBasicBlock *SMBB = li_->getMBBFromIndex(SLR->start);
1067   if (loopInfo->getLoopFor(SMBB) != L) {
1068     if (!loopInfo->isLoopHeader(CopyMBB))
1069       return false;
1070     // If vr's live interval extends pass the loop header, do not join.
1071     for (MachineBasicBlock::succ_iterator SI = CopyMBB->succ_begin(),
1072            SE = CopyMBB->succ_end(); SI != SE; ++SI) {
1073       MachineBasicBlock *SuccMBB = *SI;
1074       if (SuccMBB == CopyMBB)
1075         continue;
1076       if (DstInt.overlaps(li_->getMBBStartIdx(SuccMBB),
1077                           li_->getNextSlot(li_->getMBBEndIdx(SuccMBB))))
1078         return false;
1079     }
1080   }
1081   return true;
1082 }
1083
1084 /// isWinToJoinVRWithDstPhysReg - Return true if it's worth while to join a
1085 /// copy from a virtual source register to a physical destination register.
1086 bool
1087 SimpleRegisterCoalescing::isWinToJoinVRWithDstPhysReg(MachineInstr *CopyMI,
1088                                                      MachineBasicBlock *CopyMBB,
1089                                                      LiveInterval &DstInt,
1090                                                      LiveInterval &SrcInt) {
1091   // If the virtual register live interval is long but it has low use desity,
1092   // do not join them, instead mark the physical register as its allocation
1093   // preference.
1094   const TargetRegisterClass *RC = mri_->getRegClass(SrcInt.reg);
1095   unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
1096   unsigned Length = li_->getApproximateInstructionCount(SrcInt);
1097   if (Length > Threshold &&
1098       (((float)std::distance(mri_->use_begin(SrcInt.reg),
1099                              mri_->use_end()) / Length) < (1.0 / Threshold)))
1100     return false;
1101
1102   if (SrcInt.empty())
1103     // Must be implicit_def.
1104     return false;
1105
1106   // If the virtual register live interval is defined or cross a loop, turn
1107   // down aggressiveness.
1108   MachineInstrIndex CopyIdx =
1109     li_->getDefIndex(li_->getInstructionIndex(CopyMI));
1110   MachineInstrIndex UseIdx = li_->getUseIndex(CopyIdx);
1111   LiveInterval::iterator SLR = SrcInt.FindLiveRangeContaining(UseIdx);
1112   assert(SLR != SrcInt.end() && "Live range not found!");
1113   SLR = SrcInt.FindLiveRangeContaining(li_->getPrevSlot(SLR->start));
1114   if (SLR == SrcInt.end())
1115     return true;
1116   MachineBasicBlock *SMBB = li_->getMBBFromIndex(SLR->start);
1117   const MachineLoop *L = loopInfo->getLoopFor(SMBB);
1118
1119   if (!L || Length <= Threshold)
1120     return true;
1121
1122   if (loopInfo->getLoopFor(CopyMBB) != L) {
1123     if (SMBB != L->getLoopLatch())
1124       return false;
1125     // If vr's live interval is extended from before the loop latch, do not
1126     // join.
1127     for (MachineBasicBlock::pred_iterator PI = SMBB->pred_begin(),
1128            PE = SMBB->pred_end(); PI != PE; ++PI) {
1129       MachineBasicBlock *PredMBB = *PI;
1130       if (PredMBB == SMBB)
1131         continue;
1132       if (SrcInt.overlaps(li_->getMBBStartIdx(PredMBB),
1133                           li_->getNextSlot(li_->getMBBEndIdx(PredMBB))))
1134         return false;
1135     }
1136   }
1137   return true;
1138 }
1139
1140 /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
1141 /// two virtual registers from different register classes.
1142 bool
1143 SimpleRegisterCoalescing::isWinToJoinCrossClass(unsigned LargeReg,
1144                                                 unsigned SmallReg,
1145                                                 unsigned Threshold) {
1146   // Then make sure the intervals are *short*.
1147   LiveInterval &LargeInt = li_->getInterval(LargeReg);
1148   LiveInterval &SmallInt = li_->getInterval(SmallReg);
1149   unsigned LargeSize = li_->getApproximateInstructionCount(LargeInt);
1150   unsigned SmallSize = li_->getApproximateInstructionCount(SmallInt);
1151   if (SmallSize > Threshold || LargeSize > Threshold)
1152     if ((float)std::distance(mri_->use_begin(SmallReg),
1153                              mri_->use_end()) / SmallSize <
1154         (float)std::distance(mri_->use_begin(LargeReg),
1155                              mri_->use_end()) / LargeSize)
1156       return false;
1157   return true;
1158 }
1159
1160 /// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual
1161 /// register with a physical register, check if any of the virtual register
1162 /// operand is a sub-register use or def. If so, make sure it won't result
1163 /// in an illegal extract_subreg or insert_subreg instruction. e.g.
1164 /// vr1024 = extract_subreg vr1025, 1
1165 /// ...
1166 /// vr1024 = mov8rr AH
1167 /// If vr1024 is coalesced with AH, the extract_subreg is now illegal since
1168 /// AH does not have a super-reg whose sub-register 1 is AH.
1169 bool
1170 SimpleRegisterCoalescing::HasIncompatibleSubRegDefUse(MachineInstr *CopyMI,
1171                                                       unsigned VirtReg,
1172                                                       unsigned PhysReg) {
1173   for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(VirtReg),
1174          E = mri_->reg_end(); I != E; ++I) {
1175     MachineOperand &O = I.getOperand();
1176     MachineInstr *MI = &*I;
1177     if (MI == CopyMI || JoinedCopies.count(MI))
1178       continue;
1179     unsigned SubIdx = O.getSubReg();
1180     if (SubIdx && !tri_->getSubReg(PhysReg, SubIdx))
1181       return true;
1182     if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1183       SubIdx = MI->getOperand(2).getImm();
1184       if (O.isUse() && !tri_->getSubReg(PhysReg, SubIdx))
1185         return true;
1186       if (O.isDef()) {
1187         unsigned SrcReg = MI->getOperand(1).getReg();
1188         const TargetRegisterClass *RC =
1189           TargetRegisterInfo::isPhysicalRegister(SrcReg)
1190           ? tri_->getPhysicalRegisterRegClass(SrcReg)
1191           : mri_->getRegClass(SrcReg);
1192         if (!tri_->getMatchingSuperReg(PhysReg, SubIdx, RC))
1193           return true;
1194       }
1195     }
1196     if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1197         MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
1198       SubIdx = MI->getOperand(3).getImm();
1199       if (VirtReg == MI->getOperand(0).getReg()) {
1200         if (!tri_->getSubReg(PhysReg, SubIdx))
1201           return true;
1202       } else {
1203         unsigned DstReg = MI->getOperand(0).getReg();
1204         const TargetRegisterClass *RC =
1205           TargetRegisterInfo::isPhysicalRegister(DstReg)
1206           ? tri_->getPhysicalRegisterRegClass(DstReg)
1207           : mri_->getRegClass(DstReg);
1208         if (!tri_->getMatchingSuperReg(PhysReg, SubIdx, RC))
1209           return true;
1210       }
1211     }
1212   }
1213   return false;
1214 }
1215
1216
1217 /// CanJoinExtractSubRegToPhysReg - Return true if it's possible to coalesce
1218 /// an extract_subreg where dst is a physical register, e.g.
1219 /// cl = EXTRACT_SUBREG reg1024, 1
1220 bool
1221 SimpleRegisterCoalescing::CanJoinExtractSubRegToPhysReg(unsigned DstReg,
1222                                                unsigned SrcReg, unsigned SubIdx,
1223                                                unsigned &RealDstReg) {
1224   const TargetRegisterClass *RC = mri_->getRegClass(SrcReg);
1225   RealDstReg = tri_->getMatchingSuperReg(DstReg, SubIdx, RC);
1226   assert(RealDstReg && "Invalid extract_subreg instruction!");
1227
1228   // For this type of EXTRACT_SUBREG, conservatively
1229   // check if the live interval of the source register interfere with the
1230   // actual super physical register we are trying to coalesce with.
1231   LiveInterval &RHS = li_->getInterval(SrcReg);
1232   if (li_->hasInterval(RealDstReg) &&
1233       RHS.overlaps(li_->getInterval(RealDstReg))) {
1234     DEBUG({
1235         errs() << "Interfere with register ";
1236         li_->getInterval(RealDstReg).print(errs(), tri_);
1237       });
1238     return false; // Not coalescable
1239   }
1240   for (const unsigned* SR = tri_->getSubRegisters(RealDstReg); *SR; ++SR)
1241     if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
1242       DEBUG({
1243           errs() << "Interfere with sub-register ";
1244           li_->getInterval(*SR).print(errs(), tri_);
1245         });
1246       return false; // Not coalescable
1247     }
1248   return true;
1249 }
1250
1251 /// CanJoinInsertSubRegToPhysReg - Return true if it's possible to coalesce
1252 /// an insert_subreg where src is a physical register, e.g.
1253 /// reg1024 = INSERT_SUBREG reg1024, c1, 0
1254 bool
1255 SimpleRegisterCoalescing::CanJoinInsertSubRegToPhysReg(unsigned DstReg,
1256                                                unsigned SrcReg, unsigned SubIdx,
1257                                                unsigned &RealSrcReg) {
1258   const TargetRegisterClass *RC = mri_->getRegClass(DstReg);
1259   RealSrcReg = tri_->getMatchingSuperReg(SrcReg, SubIdx, RC);
1260   assert(RealSrcReg && "Invalid extract_subreg instruction!");
1261
1262   LiveInterval &RHS = li_->getInterval(DstReg);
1263   if (li_->hasInterval(RealSrcReg) &&
1264       RHS.overlaps(li_->getInterval(RealSrcReg))) {
1265     DEBUG({
1266         errs() << "Interfere with register ";
1267         li_->getInterval(RealSrcReg).print(errs(), tri_);
1268       });
1269     return false; // Not coalescable
1270   }
1271   for (const unsigned* SR = tri_->getSubRegisters(RealSrcReg); *SR; ++SR)
1272     if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
1273       DEBUG({
1274           errs() << "Interfere with sub-register ";
1275           li_->getInterval(*SR).print(errs(), tri_);
1276         });
1277       return false; // Not coalescable
1278     }
1279   return true;
1280 }
1281
1282 /// getRegAllocPreference - Return register allocation preference register.
1283 ///
1284 static unsigned getRegAllocPreference(unsigned Reg, MachineFunction &MF,
1285                                       MachineRegisterInfo *MRI,
1286                                       const TargetRegisterInfo *TRI) {
1287   if (TargetRegisterInfo::isPhysicalRegister(Reg))
1288     return 0;
1289   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
1290   return TRI->ResolveRegAllocHint(Hint.first, Hint.second, MF);
1291 }
1292
1293 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
1294 /// which are the src/dst of the copy instruction CopyMI.  This returns true
1295 /// if the copy was successfully coalesced away. If it is not currently
1296 /// possible to coalesce this interval, but it may be possible if other
1297 /// things get coalesced, then it returns true by reference in 'Again'.
1298 bool SimpleRegisterCoalescing::JoinCopy(CopyRec &TheCopy, bool &Again) {
1299   MachineInstr *CopyMI = TheCopy.MI;
1300
1301   Again = false;
1302   if (JoinedCopies.count(CopyMI) || ReMatCopies.count(CopyMI))
1303     return false; // Already done.
1304
1305   DEBUG(errs() << li_->getInstructionIndex(CopyMI) << '\t' << *CopyMI);
1306
1307   unsigned SrcReg, DstReg, SrcSubIdx = 0, DstSubIdx = 0;
1308   bool isExtSubReg = CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG;
1309   bool isInsSubReg = CopyMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG;
1310   bool isSubRegToReg = CopyMI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG;
1311   unsigned SubIdx = 0;
1312   if (isExtSubReg) {
1313     DstReg    = CopyMI->getOperand(0).getReg();
1314     DstSubIdx = CopyMI->getOperand(0).getSubReg();
1315     SrcReg    = CopyMI->getOperand(1).getReg();
1316     SrcSubIdx = CopyMI->getOperand(2).getImm();
1317   } else if (isInsSubReg || isSubRegToReg) {
1318     DstReg    = CopyMI->getOperand(0).getReg();
1319     DstSubIdx = CopyMI->getOperand(3).getImm();
1320     SrcReg    = CopyMI->getOperand(2).getReg();
1321     SrcSubIdx = CopyMI->getOperand(2).getSubReg();
1322     if (SrcSubIdx && SrcSubIdx != DstSubIdx) {
1323       // r1025 = INSERT_SUBREG r1025, r1024<2>, 2 Then r1024 has already been
1324       // coalesced to a larger register so the subreg indices cancel out.
1325       DEBUG(errs() << "\tSource of insert_subreg is already coalesced "
1326                    << "to another register.\n");
1327       return false;  // Not coalescable.
1328     }
1329   } else if (!tii_->isMoveInstr(*CopyMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)){
1330     llvm_unreachable("Unrecognized copy instruction!");
1331   }
1332
1333   // If they are already joined we continue.
1334   if (SrcReg == DstReg) {
1335     DEBUG(errs() << "\tCopy already coalesced.\n");
1336     return false;  // Not coalescable.
1337   }
1338   
1339   bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
1340   bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
1341
1342   // If they are both physical registers, we cannot join them.
1343   if (SrcIsPhys && DstIsPhys) {
1344     DEBUG(errs() << "\tCan not coalesce physregs.\n");
1345     return false;  // Not coalescable.
1346   }
1347   
1348   // We only join virtual registers with allocatable physical registers.
1349   if (SrcIsPhys && !allocatableRegs_[SrcReg]) {
1350     DEBUG(errs() << "\tSrc reg is unallocatable physreg.\n");
1351     return false;  // Not coalescable.
1352   }
1353   if (DstIsPhys && !allocatableRegs_[DstReg]) {
1354     DEBUG(errs() << "\tDst reg is unallocatable physreg.\n");
1355     return false;  // Not coalescable.
1356   }
1357
1358   // Check that a physical source register is compatible with dst regclass
1359   if (SrcIsPhys) {
1360     unsigned SrcSubReg = SrcSubIdx ?
1361       tri_->getSubReg(SrcReg, SrcSubIdx) : SrcReg;
1362     const TargetRegisterClass *DstRC = mri_->getRegClass(DstReg);
1363     const TargetRegisterClass *DstSubRC = DstRC;
1364     if (DstSubIdx)
1365       DstSubRC = DstRC->getSubRegisterRegClass(DstSubIdx);
1366     assert(DstSubRC && "Illegal subregister index");
1367     if (!DstSubRC->contains(SrcSubReg)) {
1368       DEBUG(errs() << "\tIncompatible destination regclass: "
1369                    << tri_->getName(SrcSubReg) << " not in "
1370                    << DstSubRC->getName() << ".\n");
1371       return false;             // Not coalescable.
1372     }
1373   }
1374
1375   // Check that a physical dst register is compatible with source regclass
1376   if (DstIsPhys) {
1377     unsigned DstSubReg = DstSubIdx ?
1378       tri_->getSubReg(DstReg, DstSubIdx) : DstReg;
1379     const TargetRegisterClass *SrcRC = mri_->getRegClass(SrcReg);
1380     const TargetRegisterClass *SrcSubRC = SrcRC;
1381     if (SrcSubIdx)
1382       SrcSubRC = SrcRC->getSubRegisterRegClass(SrcSubIdx);
1383     assert(SrcSubRC && "Illegal subregister index");
1384     if (!SrcSubRC->contains(DstReg)) {
1385       DEBUG(errs() << "\tIncompatible source regclass: "
1386                    << tri_->getName(DstSubReg) << " not in "
1387                    << SrcSubRC->getName() << ".\n");
1388       (void)DstSubReg;
1389       return false;             // Not coalescable.
1390     }
1391   }
1392
1393   // Should be non-null only when coalescing to a sub-register class.
1394   bool CrossRC = false;
1395   const TargetRegisterClass *SrcRC= SrcIsPhys ? 0 : mri_->getRegClass(SrcReg);
1396   const TargetRegisterClass *DstRC= DstIsPhys ? 0 : mri_->getRegClass(DstReg);
1397   const TargetRegisterClass *NewRC = NULL;
1398   MachineBasicBlock *CopyMBB = CopyMI->getParent();
1399   unsigned RealDstReg = 0;
1400   unsigned RealSrcReg = 0;
1401   if (isExtSubReg || isInsSubReg || isSubRegToReg) {
1402     SubIdx = CopyMI->getOperand(isExtSubReg ? 2 : 3).getImm();
1403     if (SrcIsPhys && isExtSubReg) {
1404       // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be
1405       // coalesced with AX.
1406       unsigned DstSubIdx = CopyMI->getOperand(0).getSubReg();
1407       if (DstSubIdx) {
1408         // r1024<2> = EXTRACT_SUBREG EAX, 2. Then r1024 has already been
1409         // coalesced to a larger register so the subreg indices cancel out.
1410         if (DstSubIdx != SubIdx) {
1411           DEBUG(errs() << "\t Sub-register indices mismatch.\n");
1412           return false; // Not coalescable.
1413         }
1414       } else
1415         SrcReg = tri_->getSubReg(SrcReg, SubIdx);
1416       SubIdx = 0;
1417     } else if (DstIsPhys && (isInsSubReg || isSubRegToReg)) {
1418       // EAX = INSERT_SUBREG EAX, r1024, 0
1419       unsigned SrcSubIdx = CopyMI->getOperand(2).getSubReg();
1420       if (SrcSubIdx) {
1421         // EAX = INSERT_SUBREG EAX, r1024<2>, 2 Then r1024 has already been
1422         // coalesced to a larger register so the subreg indices cancel out.
1423         if (SrcSubIdx != SubIdx) {
1424           DEBUG(errs() << "\t Sub-register indices mismatch.\n");
1425           return false; // Not coalescable.
1426         }
1427       } else
1428         DstReg = tri_->getSubReg(DstReg, SubIdx);
1429       SubIdx = 0;
1430     } else if ((DstIsPhys && isExtSubReg) ||
1431                (SrcIsPhys && (isInsSubReg || isSubRegToReg))) {
1432       if (!isSubRegToReg && CopyMI->getOperand(1).getSubReg()) {
1433         DEBUG(errs() << "\tSrc of extract_subreg already coalesced with reg"
1434                      << " of a super-class.\n");
1435         return false; // Not coalescable.
1436       }
1437
1438       if (isExtSubReg) {
1439         if (!CanJoinExtractSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealDstReg))
1440           return false; // Not coalescable
1441       } else {
1442         if (!CanJoinInsertSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealSrcReg))
1443           return false; // Not coalescable
1444       }
1445       SubIdx = 0;
1446     } else {
1447       unsigned OldSubIdx = isExtSubReg ? CopyMI->getOperand(0).getSubReg()
1448         : CopyMI->getOperand(2).getSubReg();
1449       if (OldSubIdx) {
1450         if (OldSubIdx == SubIdx && !differingRegisterClasses(SrcReg, DstReg))
1451           // r1024<2> = EXTRACT_SUBREG r1025, 2. Then r1024 has already been
1452           // coalesced to a larger register so the subreg indices cancel out.
1453           // Also check if the other larger register is of the same register
1454           // class as the would be resulting register.
1455           SubIdx = 0;
1456         else {
1457           DEBUG(errs() << "\t Sub-register indices mismatch.\n");
1458           return false; // Not coalescable.
1459         }
1460       }
1461       if (SubIdx) {
1462         if (!DstIsPhys && !SrcIsPhys) {
1463           if (isInsSubReg || isSubRegToReg) {
1464             NewRC = tri_->getMatchingSuperRegClass(DstRC, SrcRC, SubIdx);
1465           } else // extract_subreg {
1466             NewRC = tri_->getMatchingSuperRegClass(SrcRC, DstRC, SubIdx);
1467           }
1468         if (!NewRC) {
1469           DEBUG(errs() << "\t Conflicting sub-register indices.\n");
1470           return false;  // Not coalescable
1471         }
1472
1473         unsigned LargeReg = isExtSubReg ? SrcReg : DstReg;
1474         unsigned SmallReg = isExtSubReg ? DstReg : SrcReg;
1475         unsigned Limit= allocatableRCRegs_[mri_->getRegClass(SmallReg)].count();
1476         if (!isWinToJoinCrossClass(LargeReg, SmallReg, Limit)) {
1477           Again = true;  // May be possible to coalesce later.
1478           return false;
1479         }
1480       }
1481     }
1482   } else if (differingRegisterClasses(SrcReg, DstReg)) {
1483     if (DisableCrossClassJoin)
1484       return false;
1485     CrossRC = true;
1486
1487     // FIXME: What if the result of a EXTRACT_SUBREG is then coalesced
1488     // with another? If it's the resulting destination register, then
1489     // the subidx must be propagated to uses (but only those defined
1490     // by the EXTRACT_SUBREG). If it's being coalesced into another
1491     // register, it should be safe because register is assumed to have
1492     // the register class of the super-register.
1493
1494     // Process moves where one of the registers have a sub-register index.
1495     MachineOperand *DstMO = CopyMI->findRegisterDefOperand(DstReg);
1496     MachineOperand *SrcMO = CopyMI->findRegisterUseOperand(SrcReg);
1497     SubIdx = DstMO->getSubReg();
1498     if (SubIdx) {
1499       if (SrcMO->getSubReg())
1500         // FIXME: can we handle this?
1501         return false;
1502       // This is not an insert_subreg but it looks like one.
1503       // e.g. %reg1024:4 = MOV32rr %EAX
1504       isInsSubReg = true;
1505       if (SrcIsPhys) {
1506         if (!CanJoinInsertSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealSrcReg))
1507           return false; // Not coalescable
1508         SubIdx = 0;
1509       }
1510     } else {
1511       SubIdx = SrcMO->getSubReg();
1512       if (SubIdx) {
1513         // This is not a extract_subreg but it looks like one.
1514         // e.g. %cl = MOV16rr %reg1024:1
1515         isExtSubReg = true;
1516         if (DstIsPhys) {
1517           if (!CanJoinExtractSubRegToPhysReg(DstReg, SrcReg, SubIdx,RealDstReg))
1518             return false; // Not coalescable
1519           SubIdx = 0;
1520         }
1521       }
1522     }
1523
1524     unsigned LargeReg = SrcReg;
1525     unsigned SmallReg = DstReg;
1526
1527     // Now determine the register class of the joined register.
1528     if (isExtSubReg) {
1529       if (SubIdx && DstRC && DstRC->isASubClass()) {
1530         // This is a move to a sub-register class. However, the source is a
1531         // sub-register of a larger register class. We don't know what should
1532         // the register class be. FIXME.
1533         Again = true;
1534         return false;
1535       }
1536       if (!DstIsPhys && !SrcIsPhys)
1537         NewRC = SrcRC;
1538     } else if (!SrcIsPhys && !DstIsPhys) {
1539       NewRC = getCommonSubClass(SrcRC, DstRC);
1540       if (!NewRC) {
1541         DEBUG(errs() << "\tDisjoint regclasses: "
1542                      << SrcRC->getName() << ", "
1543                      << DstRC->getName() << ".\n");
1544         return false;           // Not coalescable.
1545       }
1546       if (DstRC->getSize() > SrcRC->getSize())
1547         std::swap(LargeReg, SmallReg);
1548     }
1549
1550     // If we are joining two virtual registers and the resulting register
1551     // class is more restrictive (fewer register, smaller size). Check if it's
1552     // worth doing the merge.
1553     if (!SrcIsPhys && !DstIsPhys &&
1554         (isExtSubReg || DstRC->isASubClass()) &&
1555         !isWinToJoinCrossClass(LargeReg, SmallReg,
1556                                allocatableRCRegs_[NewRC].count())) {
1557       DEBUG(errs() << "\tSrc/Dest are different register classes.\n");
1558       // Allow the coalescer to try again in case either side gets coalesced to
1559       // a physical register that's compatible with the other side. e.g.
1560       // r1024 = MOV32to32_ r1025
1561       // But later r1024 is assigned EAX then r1025 may be coalesced with EAX.
1562       Again = true;  // May be possible to coalesce later.
1563       return false;
1564     }
1565   }
1566
1567   // Will it create illegal extract_subreg / insert_subreg?
1568   if (SrcIsPhys && HasIncompatibleSubRegDefUse(CopyMI, DstReg, SrcReg))
1569     return false;
1570   if (DstIsPhys && HasIncompatibleSubRegDefUse(CopyMI, SrcReg, DstReg))
1571     return false;
1572   
1573   LiveInterval &SrcInt = li_->getInterval(SrcReg);
1574   LiveInterval &DstInt = li_->getInterval(DstReg);
1575   assert(SrcInt.reg == SrcReg && DstInt.reg == DstReg &&
1576          "Register mapping is horribly broken!");
1577
1578   DEBUG({
1579       errs() << "\t\tInspecting "; SrcInt.print(errs(), tri_);
1580       errs() << " and "; DstInt.print(errs(), tri_);
1581       errs() << ": ";
1582     });
1583
1584   // Save a copy of the virtual register live interval. We'll manually
1585   // merge this into the "real" physical register live interval this is
1586   // coalesced with.
1587   LiveInterval *SavedLI = 0;
1588   if (RealDstReg)
1589     SavedLI = li_->dupInterval(&SrcInt);
1590   else if (RealSrcReg)
1591     SavedLI = li_->dupInterval(&DstInt);
1592
1593   // Check if it is necessary to propagate "isDead" property.
1594   if (!isExtSubReg && !isInsSubReg && !isSubRegToReg) {
1595     MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg, false);
1596     bool isDead = mopd->isDead();
1597
1598     // We need to be careful about coalescing a source physical register with a
1599     // virtual register. Once the coalescing is done, it cannot be broken and
1600     // these are not spillable! If the destination interval uses are far away,
1601     // think twice about coalescing them!
1602     if (!isDead && (SrcIsPhys || DstIsPhys)) {
1603       // If the copy is in a loop, take care not to coalesce aggressively if the
1604       // src is coming in from outside the loop (or the dst is out of the loop).
1605       // If it's not in a loop, then determine whether to join them base purely
1606       // by the length of the interval.
1607       if (PhysJoinTweak) {
1608         if (SrcIsPhys) {
1609           if (!isWinToJoinVRWithSrcPhysReg(CopyMI, CopyMBB, DstInt, SrcInt)) {
1610             mri_->setRegAllocationHint(DstInt.reg, 0, SrcReg);
1611             ++numAborts;
1612             DEBUG(errs() << "\tMay tie down a physical register, abort!\n");
1613             Again = true;  // May be possible to coalesce later.
1614             return false;
1615           }
1616         } else {
1617           if (!isWinToJoinVRWithDstPhysReg(CopyMI, CopyMBB, DstInt, SrcInt)) {
1618             mri_->setRegAllocationHint(SrcInt.reg, 0, DstReg);
1619             ++numAborts;
1620             DEBUG(errs() << "\tMay tie down a physical register, abort!\n");
1621             Again = true;  // May be possible to coalesce later.
1622             return false;
1623           }
1624         }
1625       } else {
1626         // If the virtual register live interval is long but it has low use desity,
1627         // do not join them, instead mark the physical register as its allocation
1628         // preference.
1629         LiveInterval &JoinVInt = SrcIsPhys ? DstInt : SrcInt;
1630         unsigned JoinVReg = SrcIsPhys ? DstReg : SrcReg;
1631         unsigned JoinPReg = SrcIsPhys ? SrcReg : DstReg;
1632         const TargetRegisterClass *RC = mri_->getRegClass(JoinVReg);
1633         unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
1634         if (TheCopy.isBackEdge)
1635           Threshold *= 2; // Favors back edge copies.
1636
1637         unsigned Length = li_->getApproximateInstructionCount(JoinVInt);
1638         float Ratio = 1.0 / Threshold;
1639         if (Length > Threshold &&
1640             (((float)std::distance(mri_->use_begin(JoinVReg),
1641                                    mri_->use_end()) / Length) < Ratio)) {
1642           mri_->setRegAllocationHint(JoinVInt.reg, 0, JoinPReg);
1643           ++numAborts;
1644           DEBUG(errs() << "\tMay tie down a physical register, abort!\n");
1645           Again = true;  // May be possible to coalesce later.
1646           return false;
1647         }
1648       }
1649     }
1650   }
1651
1652   // Okay, attempt to join these two intervals.  On failure, this returns false.
1653   // Otherwise, if one of the intervals being joined is a physreg, this method
1654   // always canonicalizes DstInt to be it.  The output "SrcInt" will not have
1655   // been modified, so we can use this information below to update aliases.
1656   bool Swapped = false;
1657   // If SrcInt is implicitly defined, it's safe to coalesce.
1658   bool isEmpty = SrcInt.empty();
1659   if (isEmpty && !CanCoalesceWithImpDef(CopyMI, DstInt, SrcInt)) {
1660     // Only coalesce an empty interval (defined by implicit_def) with
1661     // another interval which has a valno defined by the CopyMI and the CopyMI
1662     // is a kill of the implicit def.
1663     DEBUG(errs() << "Not profitable!\n");
1664     return false;
1665   }
1666
1667   if (!isEmpty && !JoinIntervals(DstInt, SrcInt, Swapped)) {
1668     // Coalescing failed.
1669
1670     // If definition of source is defined by trivial computation, try
1671     // rematerializing it.
1672     if (!isExtSubReg && !isInsSubReg && !isSubRegToReg &&
1673         ReMaterializeTrivialDef(SrcInt, DstReg, DstSubIdx, CopyMI))
1674       return true;
1675     
1676     // If we can eliminate the copy without merging the live ranges, do so now.
1677     if (!isExtSubReg && !isInsSubReg && !isSubRegToReg &&
1678         (AdjustCopiesBackFrom(SrcInt, DstInt, CopyMI) ||
1679          RemoveCopyByCommutingDef(SrcInt, DstInt, CopyMI))) {
1680       JoinedCopies.insert(CopyMI);
1681       return true;
1682     }
1683     
1684     // Otherwise, we are unable to join the intervals.
1685     DEBUG(errs() << "Interference!\n");
1686     Again = true;  // May be possible to coalesce later.
1687     return false;
1688   }
1689
1690   LiveInterval *ResSrcInt = &SrcInt;
1691   LiveInterval *ResDstInt = &DstInt;
1692   if (Swapped) {
1693     std::swap(SrcReg, DstReg);
1694     std::swap(ResSrcInt, ResDstInt);
1695   }
1696   assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1697          "LiveInterval::join didn't work right!");
1698                                
1699   // If we're about to merge live ranges into a physical register live interval,
1700   // we have to update any aliased register's live ranges to indicate that they
1701   // have clobbered values for this range.
1702   if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
1703     // If this is a extract_subreg where dst is a physical register, e.g.
1704     // cl = EXTRACT_SUBREG reg1024, 1
1705     // then create and update the actual physical register allocated to RHS.
1706     if (RealDstReg || RealSrcReg) {
1707       LiveInterval &RealInt =
1708         li_->getOrCreateInterval(RealDstReg ? RealDstReg : RealSrcReg);
1709       for (LiveInterval::const_vni_iterator I = SavedLI->vni_begin(),
1710              E = SavedLI->vni_end(); I != E; ++I) {
1711         const VNInfo *ValNo = *I;
1712         VNInfo *NewValNo = RealInt.getNextValue(ValNo->def, ValNo->getCopy(),
1713                                                 false, // updated at *
1714                                                 li_->getVNInfoAllocator());
1715         NewValNo->setFlags(ValNo->getFlags()); // * updated here.
1716         RealInt.addKills(NewValNo, ValNo->kills);
1717         RealInt.MergeValueInAsValue(*SavedLI, ValNo, NewValNo);
1718       }
1719       RealInt.weight += SavedLI->weight;      
1720       DstReg = RealDstReg ? RealDstReg : RealSrcReg;
1721     }
1722
1723     // Update the liveintervals of sub-registers.
1724     for (const unsigned *AS = tri_->getSubRegisters(DstReg); *AS; ++AS)
1725       li_->getOrCreateInterval(*AS).MergeInClobberRanges(*ResSrcInt,
1726                                                  li_->getVNInfoAllocator());
1727   }
1728
1729   // If this is a EXTRACT_SUBREG, make sure the result of coalescing is the
1730   // larger super-register.
1731   if ((isExtSubReg || isInsSubReg || isSubRegToReg) &&
1732       !SrcIsPhys && !DstIsPhys) {
1733     if ((isExtSubReg && !Swapped) ||
1734         ((isInsSubReg || isSubRegToReg) && Swapped)) {
1735       ResSrcInt->Copy(*ResDstInt, mri_, li_->getVNInfoAllocator());
1736       std::swap(SrcReg, DstReg);
1737       std::swap(ResSrcInt, ResDstInt);
1738     }
1739   }
1740
1741   // Coalescing to a virtual register that is of a sub-register class of the
1742   // other. Make sure the resulting register is set to the right register class.
1743   if (CrossRC)
1744     ++numCrossRCs;
1745
1746   // This may happen even if it's cross-rc coalescing. e.g.
1747   // %reg1026<def> = SUBREG_TO_REG 0, %reg1037<kill>, 4
1748   // reg1026 -> GR64, reg1037 -> GR32_ABCD. The resulting register will have to
1749   // be allocate a register from GR64_ABCD.
1750   if (NewRC)
1751     mri_->setRegClass(DstReg, NewRC);
1752
1753   if (NewHeuristic) {
1754     // Add all copies that define val# in the source interval into the queue.
1755     for (LiveInterval::const_vni_iterator i = ResSrcInt->vni_begin(),
1756            e = ResSrcInt->vni_end(); i != e; ++i) {
1757       const VNInfo *vni = *i;
1758       // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
1759       if (vni->def == MachineInstrIndex() || vni->isUnused() || vni->isPHIDef() ||
1760           !vni->isDefAccurate())
1761         continue;
1762       MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
1763       unsigned NewSrcReg, NewDstReg, NewSrcSubIdx, NewDstSubIdx;
1764       if (CopyMI &&
1765           JoinedCopies.count(CopyMI) == 0 &&
1766           tii_->isMoveInstr(*CopyMI, NewSrcReg, NewDstReg,
1767                             NewSrcSubIdx, NewDstSubIdx)) {
1768         unsigned LoopDepth = loopInfo->getLoopDepth(CopyMBB);
1769         JoinQueue->push(CopyRec(CopyMI, LoopDepth,
1770                                 isBackEdgeCopy(CopyMI, DstReg)));
1771       }
1772     }
1773   }
1774
1775   // Remember to delete the copy instruction.
1776   JoinedCopies.insert(CopyMI);
1777
1778   // Some live range has been lengthened due to colaescing, eliminate the
1779   // unnecessary kills.
1780   RemoveUnnecessaryKills(SrcReg, *ResDstInt);
1781   if (TargetRegisterInfo::isVirtualRegister(DstReg))
1782     RemoveUnnecessaryKills(DstReg, *ResDstInt);
1783
1784   UpdateRegDefsUses(SrcReg, DstReg, SubIdx);
1785
1786   // SrcReg is guarateed to be the register whose live interval that is
1787   // being merged.
1788   li_->removeInterval(SrcReg);
1789
1790   // Update regalloc hint.
1791   tri_->UpdateRegAllocHint(SrcReg, DstReg, *mf_);
1792
1793   // Manually deleted the live interval copy.
1794   if (SavedLI) {
1795     SavedLI->clear();
1796     delete SavedLI;
1797   }
1798
1799   // If resulting interval has a preference that no longer fits because of subreg
1800   // coalescing, just clear the preference.
1801   unsigned Preference = getRegAllocPreference(ResDstInt->reg, *mf_, mri_, tri_);
1802   if (Preference && (isExtSubReg || isInsSubReg || isSubRegToReg) &&
1803       TargetRegisterInfo::isVirtualRegister(ResDstInt->reg)) {
1804     const TargetRegisterClass *RC = mri_->getRegClass(ResDstInt->reg);
1805     if (!RC->contains(Preference))
1806       mri_->setRegAllocationHint(ResDstInt->reg, 0, 0);
1807   }
1808
1809   DEBUG({
1810       errs() << "\n\t\tJoined.  Result = ";
1811       ResDstInt->print(errs(), tri_);
1812       errs() << "\n";
1813     });
1814
1815   ++numJoins;
1816   return true;
1817 }
1818
1819 /// ComputeUltimateVN - Assuming we are going to join two live intervals,
1820 /// compute what the resultant value numbers for each value in the input two
1821 /// ranges will be.  This is complicated by copies between the two which can
1822 /// and will commonly cause multiple value numbers to be merged into one.
1823 ///
1824 /// VN is the value number that we're trying to resolve.  InstDefiningValue
1825 /// keeps track of the new InstDefiningValue assignment for the result
1826 /// LiveInterval.  ThisFromOther/OtherFromThis are sets that keep track of
1827 /// whether a value in this or other is a copy from the opposite set.
1828 /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
1829 /// already been assigned.
1830 ///
1831 /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1832 /// contains the value number the copy is from.
1833 ///
1834 static unsigned ComputeUltimateVN(VNInfo *VNI,
1835                                   SmallVector<VNInfo*, 16> &NewVNInfo,
1836                                   DenseMap<VNInfo*, VNInfo*> &ThisFromOther,
1837                                   DenseMap<VNInfo*, VNInfo*> &OtherFromThis,
1838                                   SmallVector<int, 16> &ThisValNoAssignments,
1839                                   SmallVector<int, 16> &OtherValNoAssignments) {
1840   unsigned VN = VNI->id;
1841
1842   // If the VN has already been computed, just return it.
1843   if (ThisValNoAssignments[VN] >= 0)
1844     return ThisValNoAssignments[VN];
1845 //  assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?");
1846
1847   // If this val is not a copy from the other val, then it must be a new value
1848   // number in the destination.
1849   DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI);
1850   if (I == ThisFromOther.end()) {
1851     NewVNInfo.push_back(VNI);
1852     return ThisValNoAssignments[VN] = NewVNInfo.size()-1;
1853   }
1854   VNInfo *OtherValNo = I->second;
1855
1856   // Otherwise, this *is* a copy from the RHS.  If the other side has already
1857   // been computed, return it.
1858   if (OtherValNoAssignments[OtherValNo->id] >= 0)
1859     return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id];
1860   
1861   // Mark this value number as currently being computed, then ask what the
1862   // ultimate value # of the other value is.
1863   ThisValNoAssignments[VN] = -2;
1864   unsigned UltimateVN =
1865     ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther,
1866                       OtherValNoAssignments, ThisValNoAssignments);
1867   return ThisValNoAssignments[VN] = UltimateVN;
1868 }
1869
1870 static bool InVector(VNInfo *Val, const SmallVector<VNInfo*, 8> &V) {
1871   return std::find(V.begin(), V.end(), Val) != V.end();
1872 }
1873
1874 /// RangeIsDefinedByCopyFromReg - Return true if the specified live range of
1875 /// the specified live interval is defined by a copy from the specified
1876 /// register.
1877 bool SimpleRegisterCoalescing::RangeIsDefinedByCopyFromReg(LiveInterval &li,
1878                                                            LiveRange *LR,
1879                                                            unsigned Reg) {
1880   unsigned SrcReg = li_->getVNInfoSourceReg(LR->valno);
1881   if (SrcReg == Reg)
1882     return true;
1883   // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
1884   if ((LR->valno->isPHIDef() || !LR->valno->isDefAccurate()) &&
1885       TargetRegisterInfo::isPhysicalRegister(li.reg) &&
1886       *tri_->getSuperRegisters(li.reg)) {
1887     // It's a sub-register live interval, we may not have precise information.
1888     // Re-compute it.
1889     MachineInstr *DefMI = li_->getInstructionFromIndex(LR->start);
1890     unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1891     if (DefMI &&
1892         tii_->isMoveInstr(*DefMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
1893         DstReg == li.reg && SrcReg == Reg) {
1894       // Cache computed info.
1895       LR->valno->def  = LR->start;
1896       LR->valno->setCopy(DefMI);
1897       return true;
1898     }
1899   }
1900   return false;
1901 }
1902
1903 /// SimpleJoin - Attempt to joint the specified interval into this one. The
1904 /// caller of this method must guarantee that the RHS only contains a single
1905 /// value number and that the RHS is not defined by a copy from this
1906 /// interval.  This returns false if the intervals are not joinable, or it
1907 /// joins them and returns true.
1908 bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS){
1909   assert(RHS.containsOneValue());
1910   
1911   // Some number (potentially more than one) value numbers in the current
1912   // interval may be defined as copies from the RHS.  Scan the overlapping
1913   // portions of the LHS and RHS, keeping track of this and looking for
1914   // overlapping live ranges that are NOT defined as copies.  If these exist, we
1915   // cannot coalesce.
1916   
1917   LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end();
1918   LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end();
1919   
1920   if (LHSIt->start < RHSIt->start) {
1921     LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start);
1922     if (LHSIt != LHS.begin()) --LHSIt;
1923   } else if (RHSIt->start < LHSIt->start) {
1924     RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start);
1925     if (RHSIt != RHS.begin()) --RHSIt;
1926   }
1927   
1928   SmallVector<VNInfo*, 8> EliminatedLHSVals;
1929   
1930   while (1) {
1931     // Determine if these live intervals overlap.
1932     bool Overlaps = false;
1933     if (LHSIt->start <= RHSIt->start)
1934       Overlaps = LHSIt->end > RHSIt->start;
1935     else
1936       Overlaps = RHSIt->end > LHSIt->start;
1937     
1938     // If the live intervals overlap, there are two interesting cases: if the
1939     // LHS interval is defined by a copy from the RHS, it's ok and we record
1940     // that the LHS value # is the same as the RHS.  If it's not, then we cannot
1941     // coalesce these live ranges and we bail out.
1942     if (Overlaps) {
1943       // If we haven't already recorded that this value # is safe, check it.
1944       if (!InVector(LHSIt->valno, EliminatedLHSVals)) {
1945         // Copy from the RHS?
1946         if (!RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg))
1947           return false;    // Nope, bail out.
1948
1949         if (LHSIt->contains(RHSIt->valno->def))
1950           // Here is an interesting situation:
1951           // BB1:
1952           //   vr1025 = copy vr1024
1953           //   ..
1954           // BB2:
1955           //   vr1024 = op 
1956           //          = vr1025
1957           // Even though vr1025 is copied from vr1024, it's not safe to
1958           // coalesce them since the live range of vr1025 intersects the
1959           // def of vr1024. This happens because vr1025 is assigned the
1960           // value of the previous iteration of vr1024.
1961           return false;
1962         EliminatedLHSVals.push_back(LHSIt->valno);
1963       }
1964       
1965       // We know this entire LHS live range is okay, so skip it now.
1966       if (++LHSIt == LHSEnd) break;
1967       continue;
1968     }
1969     
1970     if (LHSIt->end < RHSIt->end) {
1971       if (++LHSIt == LHSEnd) break;
1972     } else {
1973       // One interesting case to check here.  It's possible that we have
1974       // something like "X3 = Y" which defines a new value number in the LHS,
1975       // and is the last use of this liverange of the RHS.  In this case, we
1976       // want to notice this copy (so that it gets coalesced away) even though
1977       // the live ranges don't actually overlap.
1978       if (LHSIt->start == RHSIt->end) {
1979         if (InVector(LHSIt->valno, EliminatedLHSVals)) {
1980           // We already know that this value number is going to be merged in
1981           // if coalescing succeeds.  Just skip the liverange.
1982           if (++LHSIt == LHSEnd) break;
1983         } else {
1984           // Otherwise, if this is a copy from the RHS, mark it as being merged
1985           // in.
1986           if (RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg)) {
1987             if (LHSIt->contains(RHSIt->valno->def))
1988               // Here is an interesting situation:
1989               // BB1:
1990               //   vr1025 = copy vr1024
1991               //   ..
1992               // BB2:
1993               //   vr1024 = op 
1994               //          = vr1025
1995               // Even though vr1025 is copied from vr1024, it's not safe to
1996               // coalesced them since live range of vr1025 intersects the
1997               // def of vr1024. This happens because vr1025 is assigned the
1998               // value of the previous iteration of vr1024.
1999               return false;
2000             EliminatedLHSVals.push_back(LHSIt->valno);
2001
2002             // We know this entire LHS live range is okay, so skip it now.
2003             if (++LHSIt == LHSEnd) break;
2004           }
2005         }
2006       }
2007       
2008       if (++RHSIt == RHSEnd) break;
2009     }
2010   }
2011   
2012   // If we got here, we know that the coalescing will be successful and that
2013   // the value numbers in EliminatedLHSVals will all be merged together.  Since
2014   // the most common case is that EliminatedLHSVals has a single number, we
2015   // optimize for it: if there is more than one value, we merge them all into
2016   // the lowest numbered one, then handle the interval as if we were merging
2017   // with one value number.
2018   VNInfo *LHSValNo = NULL;
2019   if (EliminatedLHSVals.size() > 1) {
2020     // Loop through all the equal value numbers merging them into the smallest
2021     // one.
2022     VNInfo *Smallest = EliminatedLHSVals[0];
2023     for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) {
2024       if (EliminatedLHSVals[i]->id < Smallest->id) {
2025         // Merge the current notion of the smallest into the smaller one.
2026         LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]);
2027         Smallest = EliminatedLHSVals[i];
2028       } else {
2029         // Merge into the smallest.
2030         LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest);
2031       }
2032     }
2033     LHSValNo = Smallest;
2034   } else if (EliminatedLHSVals.empty()) {
2035     if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) &&
2036         *tri_->getSuperRegisters(LHS.reg))
2037       // Imprecise sub-register information. Can't handle it.
2038       return false;
2039     llvm_unreachable("No copies from the RHS?");
2040   } else {
2041     LHSValNo = EliminatedLHSVals[0];
2042   }
2043   
2044   // Okay, now that there is a single LHS value number that we're merging the
2045   // RHS into, update the value number info for the LHS to indicate that the
2046   // value number is defined where the RHS value number was.
2047   const VNInfo *VNI = RHS.getValNumInfo(0);
2048   LHSValNo->def  = VNI->def;
2049   LHSValNo->setCopy(VNI->getCopy());
2050   
2051   // Okay, the final step is to loop over the RHS live intervals, adding them to
2052   // the LHS.
2053   if (VNI->hasPHIKill())
2054     LHSValNo->setHasPHIKill(true);
2055   LHS.addKills(LHSValNo, VNI->kills);
2056   LHS.MergeRangesInAsValue(RHS, LHSValNo);
2057
2058   LHS.ComputeJoinedWeight(RHS);
2059
2060   // Update regalloc hint if both are virtual registers.
2061   if (TargetRegisterInfo::isVirtualRegister(LHS.reg) && 
2062       TargetRegisterInfo::isVirtualRegister(RHS.reg)) {
2063     std::pair<unsigned, unsigned> RHSPref = mri_->getRegAllocationHint(RHS.reg);
2064     std::pair<unsigned, unsigned> LHSPref = mri_->getRegAllocationHint(LHS.reg);
2065     if (RHSPref != LHSPref)
2066       mri_->setRegAllocationHint(LHS.reg, RHSPref.first, RHSPref.second);
2067   }
2068
2069   // Update the liveintervals of sub-registers.
2070   if (TargetRegisterInfo::isPhysicalRegister(LHS.reg))
2071     for (const unsigned *AS = tri_->getSubRegisters(LHS.reg); *AS; ++AS)
2072       li_->getOrCreateInterval(*AS).MergeInClobberRanges(LHS,
2073                                                     li_->getVNInfoAllocator());
2074
2075   return true;
2076 }
2077
2078 /// JoinIntervals - Attempt to join these two intervals.  On failure, this
2079 /// returns false.  Otherwise, if one of the intervals being joined is a
2080 /// physreg, this method always canonicalizes LHS to be it.  The output
2081 /// "RHS" will not have been modified, so we can use this information
2082 /// below to update aliases.
2083 bool
2084 SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS, LiveInterval &RHS,
2085                                         bool &Swapped) {
2086   // Compute the final value assignment, assuming that the live ranges can be
2087   // coalesced.
2088   SmallVector<int, 16> LHSValNoAssignments;
2089   SmallVector<int, 16> RHSValNoAssignments;
2090   DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS;
2091   DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS;
2092   SmallVector<VNInfo*, 16> NewVNInfo;
2093
2094   // If a live interval is a physical register, conservatively check if any
2095   // of its sub-registers is overlapping the live interval of the virtual
2096   // register. If so, do not coalesce.
2097   if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) &&
2098       *tri_->getSubRegisters(LHS.reg)) {
2099     // If it's coalescing a virtual register to a physical register, estimate
2100     // its live interval length. This is the *cost* of scanning an entire live
2101     // interval. If the cost is low, we'll do an exhaustive check instead.
2102
2103     // If this is something like this:
2104     // BB1:
2105     // v1024 = op
2106     // ...
2107     // BB2:
2108     // ...
2109     // RAX   = v1024
2110     //
2111     // That is, the live interval of v1024 crosses a bb. Then we can't rely on
2112     // less conservative check. It's possible a sub-register is defined before
2113     // v1024 (or live in) and live out of BB1.
2114     if (RHS.containsOneValue() &&
2115         li_->intervalIsInOneMBB(RHS) &&
2116         li_->getApproximateInstructionCount(RHS) <= 10) {
2117       // Perform a more exhaustive check for some common cases.
2118       if (li_->conflictsWithPhysRegRef(RHS, LHS.reg, true, JoinedCopies))
2119         return false;
2120     } else {
2121       for (const unsigned* SR = tri_->getSubRegisters(LHS.reg); *SR; ++SR)
2122         if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
2123           DEBUG({
2124               errs() << "Interfere with sub-register ";
2125               li_->getInterval(*SR).print(errs(), tri_);
2126             });
2127           return false;
2128         }
2129     }
2130   } else if (TargetRegisterInfo::isPhysicalRegister(RHS.reg) &&
2131              *tri_->getSubRegisters(RHS.reg)) {
2132     if (LHS.containsOneValue() &&
2133         li_->getApproximateInstructionCount(LHS) <= 10) {
2134       // Perform a more exhaustive check for some common cases.
2135       if (li_->conflictsWithPhysRegRef(LHS, RHS.reg, false, JoinedCopies))
2136         return false;
2137     } else {
2138       for (const unsigned* SR = tri_->getSubRegisters(RHS.reg); *SR; ++SR)
2139         if (li_->hasInterval(*SR) && LHS.overlaps(li_->getInterval(*SR))) {
2140           DEBUG({
2141               errs() << "Interfere with sub-register ";
2142               li_->getInterval(*SR).print(errs(), tri_);
2143             });
2144           return false;
2145         }
2146     }
2147   }
2148                           
2149   // Compute ultimate value numbers for the LHS and RHS values.
2150   if (RHS.containsOneValue()) {
2151     // Copies from a liveinterval with a single value are simple to handle and
2152     // very common, handle the special case here.  This is important, because
2153     // often RHS is small and LHS is large (e.g. a physreg).
2154     
2155     // Find out if the RHS is defined as a copy from some value in the LHS.
2156     int RHSVal0DefinedFromLHS = -1;
2157     int RHSValID = -1;
2158     VNInfo *RHSValNoInfo = NULL;
2159     VNInfo *RHSValNoInfo0 = RHS.getValNumInfo(0);
2160     unsigned RHSSrcReg = li_->getVNInfoSourceReg(RHSValNoInfo0);
2161     if (RHSSrcReg == 0 || RHSSrcReg != LHS.reg) {
2162       // If RHS is not defined as a copy from the LHS, we can use simpler and
2163       // faster checks to see if the live ranges are coalescable.  This joiner
2164       // can't swap the LHS/RHS intervals though.
2165       if (!TargetRegisterInfo::isPhysicalRegister(RHS.reg)) {
2166         return SimpleJoin(LHS, RHS);
2167       } else {
2168         RHSValNoInfo = RHSValNoInfo0;
2169       }
2170     } else {
2171       // It was defined as a copy from the LHS, find out what value # it is.
2172       RHSValNoInfo =
2173         LHS.getLiveRangeContaining(li_->getPrevSlot(RHSValNoInfo0->def))->valno;
2174       RHSValID = RHSValNoInfo->id;
2175       RHSVal0DefinedFromLHS = RHSValID;
2176     }
2177     
2178     LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
2179     RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
2180     NewVNInfo.resize(LHS.getNumValNums(), NULL);
2181     
2182     // Okay, *all* of the values in LHS that are defined as a copy from RHS
2183     // should now get updated.
2184     for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
2185          i != e; ++i) {
2186       VNInfo *VNI = *i;
2187       unsigned VN = VNI->id;
2188       if (unsigned LHSSrcReg = li_->getVNInfoSourceReg(VNI)) {
2189         if (LHSSrcReg != RHS.reg) {
2190           // If this is not a copy from the RHS, its value number will be
2191           // unmodified by the coalescing.
2192           NewVNInfo[VN] = VNI;
2193           LHSValNoAssignments[VN] = VN;
2194         } else if (RHSValID == -1) {
2195           // Otherwise, it is a copy from the RHS, and we don't already have a
2196           // value# for it.  Keep the current value number, but remember it.
2197           LHSValNoAssignments[VN] = RHSValID = VN;
2198           NewVNInfo[VN] = RHSValNoInfo;
2199           LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
2200         } else {
2201           // Otherwise, use the specified value #.
2202           LHSValNoAssignments[VN] = RHSValID;
2203           if (VN == (unsigned)RHSValID) {  // Else this val# is dead.
2204             NewVNInfo[VN] = RHSValNoInfo;
2205             LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
2206           }
2207         }
2208       } else {
2209         NewVNInfo[VN] = VNI;
2210         LHSValNoAssignments[VN] = VN;
2211       }
2212     }
2213     
2214     assert(RHSValID != -1 && "Didn't find value #?");
2215     RHSValNoAssignments[0] = RHSValID;
2216     if (RHSVal0DefinedFromLHS != -1) {
2217       // This path doesn't go through ComputeUltimateVN so just set
2218       // it to anything.
2219       RHSValsDefinedFromLHS[RHSValNoInfo0] = (VNInfo*)1;
2220     }
2221   } else {
2222     // Loop over the value numbers of the LHS, seeing if any are defined from
2223     // the RHS.
2224     for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
2225          i != e; ++i) {
2226       VNInfo *VNI = *i;
2227       if (VNI->isUnused() || VNI->getCopy() == 0)  // Src not defined by a copy?
2228         continue;
2229       
2230       // DstReg is known to be a register in the LHS interval.  If the src is
2231       // from the RHS interval, we can use its value #.
2232       if (li_->getVNInfoSourceReg(VNI) != RHS.reg)
2233         continue;
2234       
2235       // Figure out the value # from the RHS.
2236       LHSValsDefinedFromRHS[VNI]=
2237         RHS.getLiveRangeContaining(li_->getPrevSlot(VNI->def))->valno;
2238     }
2239     
2240     // Loop over the value numbers of the RHS, seeing if any are defined from
2241     // the LHS.
2242     for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
2243          i != e; ++i) {
2244       VNInfo *VNI = *i;
2245       if (VNI->isUnused() || VNI->getCopy() == 0)  // Src not defined by a copy?
2246         continue;
2247       
2248       // DstReg is known to be a register in the RHS interval.  If the src is
2249       // from the LHS interval, we can use its value #.
2250       if (li_->getVNInfoSourceReg(VNI) != LHS.reg)
2251         continue;
2252       
2253       // Figure out the value # from the LHS.
2254       RHSValsDefinedFromLHS[VNI]=
2255         LHS.getLiveRangeContaining(li_->getPrevSlot(VNI->def))->valno;
2256     }
2257     
2258     LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
2259     RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
2260     NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
2261     
2262     for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
2263          i != e; ++i) {
2264       VNInfo *VNI = *i;
2265       unsigned VN = VNI->id;
2266       if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused()) 
2267         continue;
2268       ComputeUltimateVN(VNI, NewVNInfo,
2269                         LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
2270                         LHSValNoAssignments, RHSValNoAssignments);
2271     }
2272     for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
2273          i != e; ++i) {
2274       VNInfo *VNI = *i;
2275       unsigned VN = VNI->id;
2276       if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
2277         continue;
2278       // If this value number isn't a copy from the LHS, it's a new number.
2279       if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
2280         NewVNInfo.push_back(VNI);
2281         RHSValNoAssignments[VN] = NewVNInfo.size()-1;
2282         continue;
2283       }
2284       
2285       ComputeUltimateVN(VNI, NewVNInfo,
2286                         RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
2287                         RHSValNoAssignments, LHSValNoAssignments);
2288     }
2289   }
2290   
2291   // Armed with the mappings of LHS/RHS values to ultimate values, walk the
2292   // interval lists to see if these intervals are coalescable.
2293   LiveInterval::const_iterator I = LHS.begin();
2294   LiveInterval::const_iterator IE = LHS.end();
2295   LiveInterval::const_iterator J = RHS.begin();
2296   LiveInterval::const_iterator JE = RHS.end();
2297   
2298   // Skip ahead until the first place of potential sharing.
2299   if (I->start < J->start) {
2300     I = std::upper_bound(I, IE, J->start);
2301     if (I != LHS.begin()) --I;
2302   } else if (J->start < I->start) {
2303     J = std::upper_bound(J, JE, I->start);
2304     if (J != RHS.begin()) --J;
2305   }
2306   
2307   while (1) {
2308     // Determine if these two live ranges overlap.
2309     bool Overlaps;
2310     if (I->start < J->start) {
2311       Overlaps = I->end > J->start;
2312     } else {
2313       Overlaps = J->end > I->start;
2314     }
2315
2316     // If so, check value # info to determine if they are really different.
2317     if (Overlaps) {
2318       // If the live range overlap will map to the same value number in the
2319       // result liverange, we can still coalesce them.  If not, we can't.
2320       if (LHSValNoAssignments[I->valno->id] !=
2321           RHSValNoAssignments[J->valno->id])
2322         return false;
2323     }
2324     
2325     if (I->end < J->end) {
2326       ++I;
2327       if (I == IE) break;
2328     } else {
2329       ++J;
2330       if (J == JE) break;
2331     }
2332   }
2333
2334   // Update kill info. Some live ranges are extended due to copy coalescing.
2335   for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(),
2336          E = LHSValsDefinedFromRHS.end(); I != E; ++I) {
2337     VNInfo *VNI = I->first;
2338     unsigned LHSValID = LHSValNoAssignments[VNI->id];
2339     NewVNInfo[LHSValID]->removeKill(VNI->def);
2340     if (VNI->hasPHIKill())
2341       NewVNInfo[LHSValID]->setHasPHIKill(true);
2342     RHS.addKills(NewVNInfo[LHSValID], VNI->kills);
2343   }
2344
2345   // Update kill info. Some live ranges are extended due to copy coalescing.
2346   for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(),
2347          E = RHSValsDefinedFromLHS.end(); I != E; ++I) {
2348     VNInfo *VNI = I->first;
2349     unsigned RHSValID = RHSValNoAssignments[VNI->id];
2350     NewVNInfo[RHSValID]->removeKill(VNI->def);
2351     if (VNI->hasPHIKill())
2352       NewVNInfo[RHSValID]->setHasPHIKill(true);
2353     LHS.addKills(NewVNInfo[RHSValID], VNI->kills);
2354   }
2355
2356   // If we get here, we know that we can coalesce the live ranges.  Ask the
2357   // intervals to coalesce themselves now.
2358   if ((RHS.ranges.size() > LHS.ranges.size() &&
2359       TargetRegisterInfo::isVirtualRegister(LHS.reg)) ||
2360       TargetRegisterInfo::isPhysicalRegister(RHS.reg)) {
2361     RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0], NewVNInfo,
2362              mri_);
2363     Swapped = true;
2364   } else {
2365     LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo,
2366              mri_);
2367     Swapped = false;
2368   }
2369   return true;
2370 }
2371
2372 namespace {
2373   // DepthMBBCompare - Comparison predicate that sort first based on the loop
2374   // depth of the basic block (the unsigned), and then on the MBB number.
2375   struct DepthMBBCompare {
2376     typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
2377     bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
2378       if (LHS.first > RHS.first) return true;   // Deeper loops first
2379       return LHS.first == RHS.first &&
2380         LHS.second->getNumber() < RHS.second->getNumber();
2381     }
2382   };
2383 }
2384
2385 /// getRepIntervalSize - Returns the size of the interval that represents the
2386 /// specified register.
2387 template<class SF>
2388 unsigned JoinPriorityQueue<SF>::getRepIntervalSize(unsigned Reg) {
2389   return Rc->getRepIntervalSize(Reg);
2390 }
2391
2392 /// CopyRecSort::operator - Join priority queue sorting function.
2393 ///
2394 bool CopyRecSort::operator()(CopyRec left, CopyRec right) const {
2395   // Inner loops first.
2396   if (left.LoopDepth > right.LoopDepth)
2397     return false;
2398   else if (left.LoopDepth == right.LoopDepth)
2399     if (left.isBackEdge && !right.isBackEdge)
2400       return false;
2401   return true;
2402 }
2403
2404 void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock *MBB,
2405                                                std::vector<CopyRec> &TryAgain) {
2406   DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
2407
2408   std::vector<CopyRec> VirtCopies;
2409   std::vector<CopyRec> PhysCopies;
2410   std::vector<CopyRec> ImpDefCopies;
2411   unsigned LoopDepth = loopInfo->getLoopDepth(MBB);
2412   for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
2413        MII != E;) {
2414     MachineInstr *Inst = MII++;
2415     
2416     // If this isn't a copy nor a extract_subreg, we can't join intervals.
2417     unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2418     if (Inst->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
2419       DstReg = Inst->getOperand(0).getReg();
2420       SrcReg = Inst->getOperand(1).getReg();
2421     } else if (Inst->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
2422                Inst->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
2423       DstReg = Inst->getOperand(0).getReg();
2424       SrcReg = Inst->getOperand(2).getReg();
2425     } else if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
2426       continue;
2427
2428     bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
2429     bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
2430     if (NewHeuristic) {
2431       JoinQueue->push(CopyRec(Inst, LoopDepth, isBackEdgeCopy(Inst, DstReg)));
2432     } else {
2433       if (li_->hasInterval(SrcReg) && li_->getInterval(SrcReg).empty())
2434         ImpDefCopies.push_back(CopyRec(Inst, 0, false));
2435       else if (SrcIsPhys || DstIsPhys)
2436         PhysCopies.push_back(CopyRec(Inst, 0, false));
2437       else
2438         VirtCopies.push_back(CopyRec(Inst, 0, false));
2439     }
2440   }
2441
2442   if (NewHeuristic)
2443     return;
2444
2445   // Try coalescing implicit copies first, followed by copies to / from
2446   // physical registers, then finally copies from virtual registers to
2447   // virtual registers.
2448   for (unsigned i = 0, e = ImpDefCopies.size(); i != e; ++i) {
2449     CopyRec &TheCopy = ImpDefCopies[i];
2450     bool Again = false;
2451     if (!JoinCopy(TheCopy, Again))
2452       if (Again)
2453         TryAgain.push_back(TheCopy);
2454   }
2455   for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) {
2456     CopyRec &TheCopy = PhysCopies[i];
2457     bool Again = false;
2458     if (!JoinCopy(TheCopy, Again))
2459       if (Again)
2460         TryAgain.push_back(TheCopy);
2461   }
2462   for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) {
2463     CopyRec &TheCopy = VirtCopies[i];
2464     bool Again = false;
2465     if (!JoinCopy(TheCopy, Again))
2466       if (Again)
2467         TryAgain.push_back(TheCopy);
2468   }
2469 }
2470
2471 void SimpleRegisterCoalescing::joinIntervals() {
2472   DEBUG(errs() << "********** JOINING INTERVALS ***********\n");
2473
2474   if (NewHeuristic)
2475     JoinQueue = new JoinPriorityQueue<CopyRecSort>(this);
2476
2477   std::vector<CopyRec> TryAgainList;
2478   if (loopInfo->empty()) {
2479     // If there are no loops in the function, join intervals in function order.
2480     for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
2481          I != E; ++I)
2482       CopyCoalesceInMBB(I, TryAgainList);
2483   } else {
2484     // Otherwise, join intervals in inner loops before other intervals.
2485     // Unfortunately we can't just iterate over loop hierarchy here because
2486     // there may be more MBB's than BB's.  Collect MBB's for sorting.
2487
2488     // Join intervals in the function prolog first. We want to join physical
2489     // registers with virtual registers before the intervals got too long.
2490     std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
2491     for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();I != E;++I){
2492       MachineBasicBlock *MBB = I;
2493       MBBs.push_back(std::make_pair(loopInfo->getLoopDepth(MBB), I));
2494     }
2495
2496     // Sort by loop depth.
2497     std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
2498
2499     // Finally, join intervals in loop nest order.
2500     for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
2501       CopyCoalesceInMBB(MBBs[i].second, TryAgainList);
2502   }
2503   
2504   // Joining intervals can allow other intervals to be joined.  Iteratively join
2505   // until we make no progress.
2506   if (NewHeuristic) {
2507     SmallVector<CopyRec, 16> TryAgain;
2508     bool ProgressMade = true;
2509     while (ProgressMade) {
2510       ProgressMade = false;
2511       while (!JoinQueue->empty()) {
2512         CopyRec R = JoinQueue->pop();
2513         bool Again = false;
2514         bool Success = JoinCopy(R, Again);
2515         if (Success)
2516           ProgressMade = true;
2517         else if (Again)
2518           TryAgain.push_back(R);
2519       }
2520
2521       if (ProgressMade) {
2522         while (!TryAgain.empty()) {
2523           JoinQueue->push(TryAgain.back());
2524           TryAgain.pop_back();
2525         }
2526       }
2527     }
2528   } else {
2529     bool ProgressMade = true;
2530     while (ProgressMade) {
2531       ProgressMade = false;
2532
2533       for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
2534         CopyRec &TheCopy = TryAgainList[i];
2535         if (TheCopy.MI) {
2536           bool Again = false;
2537           bool Success = JoinCopy(TheCopy, Again);
2538           if (Success || !Again) {
2539             TheCopy.MI = 0;   // Mark this one as done.
2540             ProgressMade = true;
2541           }
2542         }
2543       }
2544     }
2545   }
2546
2547   if (NewHeuristic)
2548     delete JoinQueue;  
2549 }
2550
2551 /// Return true if the two specified registers belong to different register
2552 /// classes.  The registers may be either phys or virt regs.
2553 bool
2554 SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA,
2555                                                    unsigned RegB) const {
2556   // Get the register classes for the first reg.
2557   if (TargetRegisterInfo::isPhysicalRegister(RegA)) {
2558     assert(TargetRegisterInfo::isVirtualRegister(RegB) &&
2559            "Shouldn't consider two physregs!");
2560     return !mri_->getRegClass(RegB)->contains(RegA);
2561   }
2562
2563   // Compare against the regclass for the second reg.
2564   const TargetRegisterClass *RegClassA = mri_->getRegClass(RegA);
2565   if (TargetRegisterInfo::isVirtualRegister(RegB)) {
2566     const TargetRegisterClass *RegClassB = mri_->getRegClass(RegB);
2567     return RegClassA != RegClassB;
2568   }
2569   return !RegClassA->contains(RegB);
2570 }
2571
2572 /// lastRegisterUse - Returns the last use of the specific register between
2573 /// cycles Start and End or NULL if there are no uses.
2574 MachineOperand *
2575 SimpleRegisterCoalescing::lastRegisterUse(MachineInstrIndex Start,
2576                                           MachineInstrIndex End,
2577                                           unsigned Reg,
2578                                           MachineInstrIndex &UseIdx) const{
2579   UseIdx = MachineInstrIndex();
2580   if (TargetRegisterInfo::isVirtualRegister(Reg)) {
2581     MachineOperand *LastUse = NULL;
2582     for (MachineRegisterInfo::use_iterator I = mri_->use_begin(Reg),
2583            E = mri_->use_end(); I != E; ++I) {
2584       MachineOperand &Use = I.getOperand();
2585       MachineInstr *UseMI = Use.getParent();
2586       unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2587       if (tii_->isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
2588           SrcReg == DstReg)
2589         // Ignore identity copies.
2590         continue;
2591       MachineInstrIndex Idx = li_->getInstructionIndex(UseMI);
2592       if (Idx >= Start && Idx < End && Idx >= UseIdx) {
2593         LastUse = &Use;
2594         UseIdx = li_->getUseIndex(Idx);
2595       }
2596     }
2597     return LastUse;
2598   }
2599
2600   MachineInstrIndex s = Start;
2601   MachineInstrIndex e = li_->getBaseIndex(li_->getPrevSlot(End));
2602   while (e >= s) {
2603     // Skip deleted instructions
2604     MachineInstr *MI = li_->getInstructionFromIndex(e);
2605     while (e != MachineInstrIndex() && li_->getPrevIndex(e) >= s && !MI) {
2606       e = li_->getPrevIndex(e);
2607       MI = li_->getInstructionFromIndex(e);
2608     }
2609     if (e < s || MI == NULL)
2610       return NULL;
2611
2612     // Ignore identity copies.
2613     unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2614     if (!(tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
2615           SrcReg == DstReg))
2616       for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) {
2617         MachineOperand &Use = MI->getOperand(i);
2618         if (Use.isReg() && Use.isUse() && Use.getReg() &&
2619             tri_->regsOverlap(Use.getReg(), Reg)) {
2620           UseIdx = li_->getUseIndex(e);
2621           return &Use;
2622         }
2623       }
2624
2625     e = li_->getPrevIndex(e);
2626   }
2627
2628   return NULL;
2629 }
2630
2631
2632 void SimpleRegisterCoalescing::printRegName(unsigned reg) const {
2633   if (TargetRegisterInfo::isPhysicalRegister(reg))
2634     errs() << tri_->getName(reg);
2635   else
2636     errs() << "%reg" << reg;
2637 }
2638
2639 void SimpleRegisterCoalescing::releaseMemory() {
2640   JoinedCopies.clear();
2641   ReMatCopies.clear();
2642   ReMatDefs.clear();
2643 }
2644
2645 bool SimpleRegisterCoalescing::isZeroLengthInterval(LiveInterval *li) const {
2646   for (LiveInterval::Ranges::const_iterator
2647          i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
2648     if (li_->getPrevIndex(i->end) > i->start)
2649       return false;
2650   return true;
2651 }
2652
2653
2654 bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
2655   mf_ = &fn;
2656   mri_ = &fn.getRegInfo();
2657   tm_ = &fn.getTarget();
2658   tri_ = tm_->getRegisterInfo();
2659   tii_ = tm_->getInstrInfo();
2660   li_ = &getAnalysis<LiveIntervals>();
2661   loopInfo = &getAnalysis<MachineLoopInfo>();
2662
2663   DEBUG(errs() << "********** SIMPLE REGISTER COALESCING **********\n"
2664                << "********** Function: "
2665                << ((Value*)mf_->getFunction())->getName() << '\n');
2666
2667   allocatableRegs_ = tri_->getAllocatableSet(fn);
2668   for (TargetRegisterInfo::regclass_iterator I = tri_->regclass_begin(),
2669          E = tri_->regclass_end(); I != E; ++I)
2670     allocatableRCRegs_.insert(std::make_pair(*I,
2671                                              tri_->getAllocatableSet(fn, *I)));
2672
2673   // Join (coalesce) intervals if requested.
2674   if (EnableJoining) {
2675     joinIntervals();
2676     DEBUG({
2677         errs() << "********** INTERVALS POST JOINING **********\n";
2678         for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I){
2679           I->second->print(errs(), tri_);
2680           errs() << "\n";
2681         }
2682       });
2683   }
2684
2685   // Perform a final pass over the instructions and compute spill weights
2686   // and remove identity moves.
2687   SmallVector<unsigned, 4> DeadDefs;
2688   for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
2689        mbbi != mbbe; ++mbbi) {
2690     MachineBasicBlock* mbb = mbbi;
2691     unsigned loopDepth = loopInfo->getLoopDepth(mbb);
2692
2693     for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
2694          mii != mie; ) {
2695       MachineInstr *MI = mii;
2696       unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2697       if (JoinedCopies.count(MI)) {
2698         // Delete all coalesced copies.
2699         if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
2700           assert((MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
2701                   MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
2702                   MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) &&
2703                  "Unrecognized copy instruction");
2704           DstReg = MI->getOperand(0).getReg();
2705         }
2706         if (MI->registerDefIsDead(DstReg)) {
2707           LiveInterval &li = li_->getInterval(DstReg);
2708           if (!ShortenDeadCopySrcLiveRange(li, MI))
2709             ShortenDeadCopyLiveRange(li, MI);
2710         }
2711         li_->RemoveMachineInstrFromMaps(MI);
2712         mii = mbbi->erase(mii);
2713         ++numPeep;
2714         continue;
2715       }
2716
2717       // Now check if this is a remat'ed def instruction which is now dead.
2718       if (ReMatDefs.count(MI)) {
2719         bool isDead = true;
2720         for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2721           const MachineOperand &MO = MI->getOperand(i);
2722           if (!MO.isReg())
2723             continue;
2724           unsigned Reg = MO.getReg();
2725           if (!Reg)
2726             continue;
2727           if (TargetRegisterInfo::isVirtualRegister(Reg))
2728             DeadDefs.push_back(Reg);
2729           if (MO.isDead())
2730             continue;
2731           if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
2732               !mri_->use_empty(Reg)) {
2733             isDead = false;
2734             break;
2735           }
2736         }
2737         if (isDead) {
2738           while (!DeadDefs.empty()) {
2739             unsigned DeadDef = DeadDefs.back();
2740             DeadDefs.pop_back();
2741             RemoveDeadDef(li_->getInterval(DeadDef), MI);
2742           }
2743           li_->RemoveMachineInstrFromMaps(mii);
2744           mii = mbbi->erase(mii);
2745           continue;
2746         } else
2747           DeadDefs.clear();
2748       }
2749
2750       // If the move will be an identity move delete it
2751       bool isMove= tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx);
2752       if (isMove && SrcReg == DstReg) {
2753         if (li_->hasInterval(SrcReg)) {
2754           LiveInterval &RegInt = li_->getInterval(SrcReg);
2755           // If def of this move instruction is dead, remove its live range
2756           // from the dstination register's live interval.
2757           if (MI->registerDefIsDead(DstReg)) {
2758             if (!ShortenDeadCopySrcLiveRange(RegInt, MI))
2759               ShortenDeadCopyLiveRange(RegInt, MI);
2760           }
2761         }
2762         li_->RemoveMachineInstrFromMaps(MI);
2763         mii = mbbi->erase(mii);
2764         ++numPeep;
2765       } else {
2766         SmallSet<unsigned, 4> UniqueUses;
2767         for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2768           const MachineOperand &mop = MI->getOperand(i);
2769           if (mop.isReg() && mop.getReg() &&
2770               TargetRegisterInfo::isVirtualRegister(mop.getReg())) {
2771             unsigned reg = mop.getReg();
2772             // Multiple uses of reg by the same instruction. It should not
2773             // contribute to spill weight again.
2774             if (UniqueUses.count(reg) != 0)
2775               continue;
2776             LiveInterval &RegInt = li_->getInterval(reg);
2777             RegInt.weight +=
2778               li_->getSpillWeight(mop.isDef(), mop.isUse(), loopDepth);
2779             UniqueUses.insert(reg);
2780           }
2781         }
2782         ++mii;
2783       }
2784     }
2785   }
2786
2787   for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) {
2788     LiveInterval &LI = *I->second;
2789     if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
2790       // If the live interval length is essentially zero, i.e. in every live
2791       // range the use follows def immediately, it doesn't make sense to spill
2792       // it and hope it will be easier to allocate for this li.
2793       if (isZeroLengthInterval(&LI))
2794         LI.weight = HUGE_VALF;
2795       else {
2796         bool isLoad = false;
2797         SmallVector<LiveInterval*, 4> SpillIs;
2798         if (li_->isReMaterializable(LI, SpillIs, isLoad)) {
2799           // If all of the definitions of the interval are re-materializable,
2800           // it is a preferred candidate for spilling. If non of the defs are
2801           // loads, then it's potentially very cheap to re-materialize.
2802           // FIXME: this gets much more complicated once we support non-trivial
2803           // re-materialization.
2804           if (isLoad)
2805             LI.weight *= 0.9F;
2806           else
2807             LI.weight *= 0.5F;
2808         }
2809       }
2810
2811       // Slightly prefer live interval that has been assigned a preferred reg.
2812       std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(LI.reg);
2813       if (Hint.first || Hint.second)
2814         LI.weight *= 1.01F;
2815
2816       // Divide the weight of the interval by its size.  This encourages 
2817       // spilling of intervals that are large and have few uses, and
2818       // discourages spilling of small intervals with many uses.
2819       LI.weight /= li_->getApproximateInstructionCount(LI) * InstrSlots::NUM;
2820     }
2821   }
2822
2823   DEBUG(dump());
2824   return true;
2825 }
2826
2827 /// print - Implement the dump method.
2828 void SimpleRegisterCoalescing::print(raw_ostream &O, const Module* m) const {
2829    li_->print(O, m);
2830 }
2831
2832 RegisterCoalescer* llvm::createSimpleRegisterCoalescer() {
2833   return new SimpleRegisterCoalescing();
2834 }
2835
2836 // Make sure that anything that uses RegisterCoalescer pulls in this file...
2837 DEFINING_FILE_FOR(SimpleRegisterCoalescing)