Use uint16_t to store register overlaps to reduce static data.
[oota-llvm.git] / lib / CodeGen / RegisterCoalescer.cpp
1 //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the generic RegisterCoalescer interface which
11 // is used as the common interface used by all clients and
12 // implementations of register coalescing.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "regalloc"
17 #include "RegisterCoalescer.h"
18 #include "LiveDebugVariables.h"
19 #include "RegisterClassInfo.h"
20 #include "VirtRegMap.h"
21
22 #include "llvm/Pass.h"
23 #include "llvm/Value.h"
24 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
30 #include "llvm/Analysis/AliasAnalysis.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineLoopInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/Passes.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/ADT/OwningPtr.h"
44 #include "llvm/ADT/SmallSet.h"
45 #include "llvm/ADT/Statistic.h"
46 #include "llvm/ADT/STLExtras.h"
47 #include <algorithm>
48 #include <cmath>
49 using namespace llvm;
50
51 STATISTIC(numJoins    , "Number of interval joins performed");
52 STATISTIC(numCrossRCs , "Number of cross class joins performed");
53 STATISTIC(numCommutes , "Number of instruction commuting performed");
54 STATISTIC(numExtends  , "Number of copies extended");
55 STATISTIC(NumReMats   , "Number of instructions re-materialized");
56 STATISTIC(numPeep     , "Number of identity moves eliminated after coalescing");
57 STATISTIC(numAborts   , "Number of times interval joining aborted");
58 STATISTIC(NumInflated , "Number of register classes inflated");
59
60 static cl::opt<bool>
61 EnableJoining("join-liveintervals",
62               cl::desc("Coalesce copies (default=true)"),
63               cl::init(true));
64
65 static cl::opt<bool>
66 DisableCrossClassJoin("disable-cross-class-join",
67                cl::desc("Avoid coalescing cross register class copies"),
68                cl::init(false), cl::Hidden);
69
70 static cl::opt<bool>
71 EnablePhysicalJoin("join-physregs",
72                    cl::desc("Join physical register copies"),
73                    cl::init(false), cl::Hidden);
74
75 static cl::opt<bool>
76 VerifyCoalescing("verify-coalescing",
77          cl::desc("Verify machine instrs before and after register coalescing"),
78          cl::Hidden);
79
80 namespace {
81   class RegisterCoalescer : public MachineFunctionPass {
82     MachineFunction* MF;
83     MachineRegisterInfo* MRI;
84     const TargetMachine* TM;
85     const TargetRegisterInfo* TRI;
86     const TargetInstrInfo* TII;
87     LiveIntervals *LIS;
88     LiveDebugVariables *LDV;
89     const MachineLoopInfo* Loops;
90     AliasAnalysis *AA;
91     RegisterClassInfo RegClassInfo;
92
93     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
94     ///
95     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
96
97     /// ReMatCopies - Keep track of copies eliminated due to remat.
98     ///
99     SmallPtrSet<MachineInstr*, 32> ReMatCopies;
100
101     /// ReMatDefs - Keep track of definition instructions which have
102     /// been remat'ed.
103     SmallPtrSet<MachineInstr*, 8> ReMatDefs;
104
105     /// joinIntervals - join compatible live intervals
106     void joinIntervals();
107
108     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
109     /// copies that cannot yet be coalesced into the "TryAgain" list.
110     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
111                            std::vector<MachineInstr*> &TryAgain);
112
113     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
114     /// which are the src/dst of the copy instruction CopyMI.  This returns
115     /// true if the copy was successfully coalesced away. If it is not
116     /// currently possible to coalesce this interval, but it may be possible if
117     /// other things get coalesced, then it returns true by reference in
118     /// 'Again'.
119     bool JoinCopy(MachineInstr *TheCopy, bool &Again);
120
121     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
122     /// returns false.  The output "SrcInt" will not have been modified, so we
123     /// can use this information below to update aliases.
124     bool JoinIntervals(CoalescerPair &CP);
125
126     /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
127     /// the source value number is defined by a copy from the destination reg
128     /// see if we can merge these two destination reg valno# into a single
129     /// value number, eliminating a copy.
130     bool AdjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
131
132     /// HasOtherReachingDefs - Return true if there are definitions of IntB
133     /// other than BValNo val# that can reach uses of AValno val# of IntA.
134     bool HasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
135                               VNInfo *AValNo, VNInfo *BValNo);
136
137     /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy.
138     /// If the source value number is defined by a commutable instruction and
139     /// its other operand is coalesced to the copy dest register, see if we
140     /// can transform the copy into a noop by commuting the definition.
141     bool RemoveCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
142
143     /// ReMaterializeTrivialDef - If the source of a copy is defined by a
144     /// trivial computation, replace the copy by rematerialize the definition.
145     /// If PreserveSrcInt is true, make sure SrcInt is valid after the call.
146     bool ReMaterializeTrivialDef(LiveInterval &SrcInt, bool PreserveSrcInt,
147                                  unsigned DstReg, MachineInstr *CopyMI);
148
149     /// shouldJoinPhys - Return true if a physreg copy should be joined.
150     bool shouldJoinPhys(CoalescerPair &CP);
151
152     /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
153     /// two virtual registers from different register classes.
154     bool isWinToJoinCrossClass(unsigned SrcReg,
155                                unsigned DstReg,
156                                const TargetRegisterClass *SrcRC,
157                                const TargetRegisterClass *DstRC,
158                                const TargetRegisterClass *NewRC);
159
160     /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
161     /// update the subregister number if it is not zero. If DstReg is a
162     /// physical register and the existing subregister number of the def / use
163     /// being updated is not zero, make sure to set it to the correct physical
164     /// subregister.
165     void UpdateRegDefsUses(const CoalescerPair &CP);
166
167     /// RemoveDeadDef - If a def of a live interval is now determined dead,
168     /// remove the val# it defines. If the live interval becomes empty, remove
169     /// it as well.
170     bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI);
171
172     /// markAsJoined - Remember that CopyMI has already been joined.
173     void markAsJoined(MachineInstr *CopyMI);
174
175     /// eliminateUndefCopy - Handle copies of undef values.
176     bool eliminateUndefCopy(MachineInstr *CopyMI, const CoalescerPair &CP);
177
178   public:
179     static char ID; // Class identification, replacement for typeinfo
180     RegisterCoalescer() : MachineFunctionPass(ID) {
181       initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
182     }
183
184     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
185
186     virtual void releaseMemory();
187
188     /// runOnMachineFunction - pass entry point
189     virtual bool runOnMachineFunction(MachineFunction&);
190
191     /// print - Implement the dump method.
192     virtual void print(raw_ostream &O, const Module* = 0) const;
193   };
194 } /// end anonymous namespace
195
196 char &llvm::RegisterCoalescerID = RegisterCoalescer::ID;
197
198 INITIALIZE_PASS_BEGIN(RegisterCoalescer, "simple-register-coalescing",
199                       "Simple Register Coalescing", false, false)
200 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
201 INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
202 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
203 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
204 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
205 INITIALIZE_PASS_END(RegisterCoalescer, "simple-register-coalescing",
206                     "Simple Register Coalescing", false, false)
207
208 char RegisterCoalescer::ID = 0;
209
210 static unsigned compose(const TargetRegisterInfo &tri, unsigned a, unsigned b) {
211   if (!a) return b;
212   if (!b) return a;
213   return tri.composeSubRegIndices(a, b);
214 }
215
216 static bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI,
217                         unsigned &Src, unsigned &Dst,
218                         unsigned &SrcSub, unsigned &DstSub) {
219   if (MI->isCopy()) {
220     Dst = MI->getOperand(0).getReg();
221     DstSub = MI->getOperand(0).getSubReg();
222     Src = MI->getOperand(1).getReg();
223     SrcSub = MI->getOperand(1).getSubReg();
224   } else if (MI->isSubregToReg()) {
225     Dst = MI->getOperand(0).getReg();
226     DstSub = compose(tri, MI->getOperand(0).getSubReg(),
227                      MI->getOperand(3).getImm());
228     Src = MI->getOperand(2).getReg();
229     SrcSub = MI->getOperand(2).getSubReg();
230   } else
231     return false;
232   return true;
233 }
234
235 bool CoalescerPair::setRegisters(const MachineInstr *MI) {
236   SrcReg = DstReg = SubIdx = 0;
237   NewRC = 0;
238   Flipped = CrossClass = false;
239
240   unsigned Src, Dst, SrcSub, DstSub;
241   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
242     return false;
243   Partial = SrcSub || DstSub;
244
245   // If one register is a physreg, it must be Dst.
246   if (TargetRegisterInfo::isPhysicalRegister(Src)) {
247     if (TargetRegisterInfo::isPhysicalRegister(Dst))
248       return false;
249     std::swap(Src, Dst);
250     std::swap(SrcSub, DstSub);
251     Flipped = true;
252   }
253
254   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
255
256   if (TargetRegisterInfo::isPhysicalRegister(Dst)) {
257     // Eliminate DstSub on a physreg.
258     if (DstSub) {
259       Dst = TRI.getSubReg(Dst, DstSub);
260       if (!Dst) return false;
261       DstSub = 0;
262     }
263
264     // Eliminate SrcSub by picking a corresponding Dst superregister.
265     if (SrcSub) {
266       Dst = TRI.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src));
267       if (!Dst) return false;
268       SrcSub = 0;
269     } else if (!MRI.getRegClass(Src)->contains(Dst)) {
270       return false;
271     }
272   } else {
273     // Both registers are virtual.
274
275     // Both registers have subreg indices.
276     if (SrcSub && DstSub) {
277       // For now we only handle the case of identical indices in commensurate
278       // registers: Dreg:ssub_1 + Dreg:ssub_1 -> Dreg
279       // FIXME: Handle Qreg:ssub_3 + Dreg:ssub_1 as QReg:dsub_1 + Dreg.
280       if (SrcSub != DstSub)
281         return false;
282       const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
283       const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
284       if (!TRI.getCommonSubClass(DstRC, SrcRC))
285         return false;
286       SrcSub = DstSub = 0;
287     }
288
289     // There can be no SrcSub.
290     if (SrcSub) {
291       std::swap(Src, Dst);
292       DstSub = SrcSub;
293       SrcSub = 0;
294       assert(!Flipped && "Unexpected flip");
295       Flipped = true;
296     }
297
298     // Find the new register class.
299     const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
300     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
301     if (DstSub)
302       NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
303     else
304       NewRC = TRI.getCommonSubClass(DstRC, SrcRC);
305     if (!NewRC)
306       return false;
307     CrossClass = NewRC != DstRC || NewRC != SrcRC;
308   }
309   // Check our invariants
310   assert(TargetRegisterInfo::isVirtualRegister(Src) && "Src must be virtual");
311   assert(!(TargetRegisterInfo::isPhysicalRegister(Dst) && DstSub) &&
312          "Cannot have a physical SubIdx");
313   SrcReg = Src;
314   DstReg = Dst;
315   SubIdx = DstSub;
316   return true;
317 }
318
319 bool CoalescerPair::flip() {
320   if (SubIdx || TargetRegisterInfo::isPhysicalRegister(DstReg))
321     return false;
322   std::swap(SrcReg, DstReg);
323   Flipped = !Flipped;
324   return true;
325 }
326
327 bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
328   if (!MI)
329     return false;
330   unsigned Src, Dst, SrcSub, DstSub;
331   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
332     return false;
333
334   // Find the virtual register that is SrcReg.
335   if (Dst == SrcReg) {
336     std::swap(Src, Dst);
337     std::swap(SrcSub, DstSub);
338   } else if (Src != SrcReg) {
339     return false;
340   }
341
342   // Now check that Dst matches DstReg.
343   if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
344     if (!TargetRegisterInfo::isPhysicalRegister(Dst))
345       return false;
346     assert(!SubIdx && "Inconsistent CoalescerPair state.");
347     // DstSub could be set for a physreg from INSERT_SUBREG.
348     if (DstSub)
349       Dst = TRI.getSubReg(Dst, DstSub);
350     // Full copy of Src.
351     if (!SrcSub)
352       return DstReg == Dst;
353     // This is a partial register copy. Check that the parts match.
354     return TRI.getSubReg(DstReg, SrcSub) == Dst;
355   } else {
356     // DstReg is virtual.
357     if (DstReg != Dst)
358       return false;
359     // Registers match, do the subregisters line up?
360     return compose(TRI, SubIdx, SrcSub) == DstSub;
361   }
362 }
363
364 void RegisterCoalescer::getAnalysisUsage(AnalysisUsage &AU) const {
365   AU.setPreservesCFG();
366   AU.addRequired<AliasAnalysis>();
367   AU.addRequired<LiveIntervals>();
368   AU.addPreserved<LiveIntervals>();
369   AU.addRequired<LiveDebugVariables>();
370   AU.addPreserved<LiveDebugVariables>();
371   AU.addPreserved<SlotIndexes>();
372   AU.addRequired<MachineLoopInfo>();
373   AU.addPreserved<MachineLoopInfo>();
374   AU.addPreservedID(MachineDominatorsID);
375   MachineFunctionPass::getAnalysisUsage(AU);
376 }
377
378 void RegisterCoalescer::markAsJoined(MachineInstr *CopyMI) {
379   /// Joined copies are not deleted immediately, but kept in JoinedCopies.
380   JoinedCopies.insert(CopyMI);
381
382   /// Mark all register operands of CopyMI as <undef> so they won't affect dead
383   /// code elimination.
384   for (MachineInstr::mop_iterator I = CopyMI->operands_begin(),
385        E = CopyMI->operands_end(); I != E; ++I)
386     if (I->isReg())
387       I->setIsUndef(true);
388 }
389
390 /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
391 /// being the source and IntB being the dest, thus this defines a value number
392 /// in IntB.  If the source value number (in IntA) is defined by a copy from B,
393 /// see if we can merge these two pieces of B into a single value number,
394 /// eliminating a copy.  For example:
395 ///
396 ///  A3 = B0
397 ///    ...
398 ///  B1 = A3      <- this copy
399 ///
400 /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
401 /// value number to be replaced with B0 (which simplifies the B liveinterval).
402 ///
403 /// This returns true if an interval was modified.
404 ///
405 bool RegisterCoalescer::AdjustCopiesBackFrom(const CoalescerPair &CP,
406                                                     MachineInstr *CopyMI) {
407   // Bail if there is no dst interval - can happen when merging physical subreg
408   // operations.
409   if (!LIS->hasInterval(CP.getDstReg()))
410     return false;
411
412   LiveInterval &IntA =
413     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
414   LiveInterval &IntB =
415     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
416   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
417
418   // BValNo is a value number in B that is defined by a copy from A.  'B3' in
419   // the example above.
420   LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
421   if (BLR == IntB.end()) return false;
422   VNInfo *BValNo = BLR->valno;
423
424   // Get the location that B is defined at.  Two options: either this value has
425   // an unknown definition point or it is defined at CopyIdx.  If unknown, we
426   // can't process it.
427   if (BValNo->def != CopyIdx) return false;
428
429   // AValNo is the value number in A that defines the copy, A3 in the example.
430   SlotIndex CopyUseIdx = CopyIdx.getRegSlot(true);
431   LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyUseIdx);
432   // The live range might not exist after fun with physreg coalescing.
433   if (ALR == IntA.end()) return false;
434   VNInfo *AValNo = ALR->valno;
435
436   // If AValNo is defined as a copy from IntB, we can potentially process this.
437   // Get the instruction that defines this value number.
438   MachineInstr *ACopyMI = LIS->getInstructionFromIndex(AValNo->def);
439   if (!CP.isCoalescable(ACopyMI))
440     return false;
441
442   // Get the LiveRange in IntB that this value number starts with.
443   LiveInterval::iterator ValLR =
444     IntB.FindLiveRangeContaining(AValNo->def.getPrevSlot());
445   if (ValLR == IntB.end())
446     return false;
447
448   // Make sure that the end of the live range is inside the same block as
449   // CopyMI.
450   MachineInstr *ValLREndInst =
451     LIS->getInstructionFromIndex(ValLR->end.getPrevSlot());
452   if (!ValLREndInst || ValLREndInst->getParent() != CopyMI->getParent())
453     return false;
454
455   // Okay, we now know that ValLR ends in the same block that the CopyMI
456   // live-range starts.  If there are no intervening live ranges between them in
457   // IntB, we can merge them.
458   if (ValLR+1 != BLR) return false;
459
460   // If a live interval is a physical register, conservatively check if any
461   // of its aliases is overlapping the live interval of the virtual register.
462   // If so, do not coalesce.
463   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
464     for (const uint16_t *AS = TRI->getAliasSet(IntB.reg); *AS; ++AS)
465       if (LIS->hasInterval(*AS) && IntA.overlaps(LIS->getInterval(*AS))) {
466         DEBUG({
467             dbgs() << "\t\tInterfere with alias ";
468             LIS->getInterval(*AS).print(dbgs(), TRI);
469           });
470         return false;
471       }
472   }
473
474   DEBUG({
475       dbgs() << "Extending: ";
476       IntB.print(dbgs(), TRI);
477     });
478
479   SlotIndex FillerStart = ValLR->end, FillerEnd = BLR->start;
480   // We are about to delete CopyMI, so need to remove it as the 'instruction
481   // that defines this value #'. Update the valnum with the new defining
482   // instruction #.
483   BValNo->def = FillerStart;
484
485   // Okay, we can merge them.  We need to insert a new liverange:
486   // [ValLR.end, BLR.begin) of either value number, then we merge the
487   // two value numbers.
488   IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
489
490   // If the IntB live range is assigned to a physical register, and if that
491   // physreg has sub-registers, update their live intervals as well.
492   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
493     for (const unsigned *SR = TRI->getSubRegisters(IntB.reg); *SR; ++SR) {
494       if (!LIS->hasInterval(*SR))
495         continue;
496       LiveInterval &SRLI = LIS->getInterval(*SR);
497       SRLI.addRange(LiveRange(FillerStart, FillerEnd,
498                               SRLI.getNextValue(FillerStart,
499                                                 LIS->getVNInfoAllocator())));
500     }
501   }
502
503   // Okay, merge "B1" into the same value number as "B0".
504   if (BValNo != ValLR->valno) {
505     // If B1 is killed by a PHI, then the merged live range must also be killed
506     // by the same PHI, as B0 and B1 can not overlap.
507     bool HasPHIKill = BValNo->hasPHIKill();
508     IntB.MergeValueNumberInto(BValNo, ValLR->valno);
509     if (HasPHIKill)
510       ValLR->valno->setHasPHIKill(true);
511   }
512   DEBUG({
513       dbgs() << "   result = ";
514       IntB.print(dbgs(), TRI);
515       dbgs() << "\n";
516     });
517
518   // If the source instruction was killing the source register before the
519   // merge, unset the isKill marker given the live range has been extended.
520   int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
521   if (UIdx != -1) {
522     ValLREndInst->getOperand(UIdx).setIsKill(false);
523   }
524
525   // Rewrite the copy. If the copy instruction was killing the destination
526   // register before the merge, find the last use and trim the live range. That
527   // will also add the isKill marker.
528   CopyMI->substituteRegister(IntA.reg, IntB.reg, CP.getSubIdx(),
529                              *TRI);
530   if (ALR->end == CopyIdx)
531     LIS->shrinkToUses(&IntA);
532
533   ++numExtends;
534   return true;
535 }
536
537 /// HasOtherReachingDefs - Return true if there are definitions of IntB
538 /// other than BValNo val# that can reach uses of AValno val# of IntA.
539 bool RegisterCoalescer::HasOtherReachingDefs(LiveInterval &IntA,
540                                                     LiveInterval &IntB,
541                                                     VNInfo *AValNo,
542                                                     VNInfo *BValNo) {
543   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
544        AI != AE; ++AI) {
545     if (AI->valno != AValNo) continue;
546     LiveInterval::Ranges::iterator BI =
547       std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start);
548     if (BI != IntB.ranges.begin())
549       --BI;
550     for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
551       if (BI->valno == BValNo)
552         continue;
553       if (BI->start <= AI->start && BI->end > AI->start)
554         return true;
555       if (BI->start > AI->start && BI->start < AI->end)
556         return true;
557     }
558   }
559   return false;
560 }
561
562 /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy with
563 /// IntA being the source and IntB being the dest, thus this defines a value
564 /// number in IntB.  If the source value number (in IntA) is defined by a
565 /// commutable instruction and its other operand is coalesced to the copy dest
566 /// register, see if we can transform the copy into a noop by commuting the
567 /// definition. For example,
568 ///
569 ///  A3 = op A2 B0<kill>
570 ///    ...
571 ///  B1 = A3      <- this copy
572 ///    ...
573 ///     = op A3   <- more uses
574 ///
575 /// ==>
576 ///
577 ///  B2 = op B0 A2<kill>
578 ///    ...
579 ///  B1 = B2      <- now an identify copy
580 ///    ...
581 ///     = op B2   <- more uses
582 ///
583 /// This returns true if an interval was modified.
584 ///
585 bool RegisterCoalescer::RemoveCopyByCommutingDef(const CoalescerPair &CP,
586                                                         MachineInstr *CopyMI) {
587   // FIXME: For now, only eliminate the copy by commuting its def when the
588   // source register is a virtual register. We want to guard against cases
589   // where the copy is a back edge copy and commuting the def lengthen the
590   // live interval of the source register to the entire loop.
591   if (CP.isPhys() && CP.isFlipped())
592     return false;
593
594   // Bail if there is no dst interval.
595   if (!LIS->hasInterval(CP.getDstReg()))
596     return false;
597
598   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
599
600   LiveInterval &IntA =
601     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
602   LiveInterval &IntB =
603     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
604
605   // BValNo is a value number in B that is defined by a copy from A. 'B3' in
606   // the example above.
607   VNInfo *BValNo = IntB.getVNInfoAt(CopyIdx);
608   if (!BValNo || BValNo->def != CopyIdx)
609     return false;
610
611   assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
612
613   // AValNo is the value number in A that defines the copy, A3 in the example.
614   VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx.getRegSlot(true));
615   assert(AValNo && "COPY source not live");
616
617   // If other defs can reach uses of this def, then it's not safe to perform
618   // the optimization.
619   if (AValNo->isPHIDef() || AValNo->isUnused() || AValNo->hasPHIKill())
620     return false;
621   MachineInstr *DefMI = LIS->getInstructionFromIndex(AValNo->def);
622   if (!DefMI)
623     return false;
624   if (!DefMI->isCommutable())
625     return false;
626   // If DefMI is a two-address instruction then commuting it will change the
627   // destination register.
628   int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg);
629   assert(DefIdx != -1);
630   unsigned UseOpIdx;
631   if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
632     return false;
633   unsigned Op1, Op2, NewDstIdx;
634   if (!TII->findCommutedOpIndices(DefMI, Op1, Op2))
635     return false;
636   if (Op1 == UseOpIdx)
637     NewDstIdx = Op2;
638   else if (Op2 == UseOpIdx)
639     NewDstIdx = Op1;
640   else
641     return false;
642
643   MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
644   unsigned NewReg = NewDstMO.getReg();
645   if (NewReg != IntB.reg || !NewDstMO.isKill())
646     return false;
647
648   // Make sure there are no other definitions of IntB that would reach the
649   // uses which the new definition can reach.
650   if (HasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
651     return false;
652
653   // Abort if the aliases of IntB.reg have values that are not simply the
654   // clobbers from the superreg.
655   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg))
656     for (const uint16_t *AS = TRI->getAliasSet(IntB.reg); *AS; ++AS)
657       if (LIS->hasInterval(*AS) &&
658           HasOtherReachingDefs(IntA, LIS->getInterval(*AS), AValNo, 0))
659         return false;
660
661   // If some of the uses of IntA.reg is already coalesced away, return false.
662   // It's not possible to determine whether it's safe to perform the coalescing.
663   for (MachineRegisterInfo::use_nodbg_iterator UI =
664          MRI->use_nodbg_begin(IntA.reg),
665        UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
666     MachineInstr *UseMI = &*UI;
667     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI);
668     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
669     if (ULR == IntA.end())
670       continue;
671     if (ULR->valno == AValNo && JoinedCopies.count(UseMI))
672       return false;
673   }
674
675   DEBUG(dbgs() << "\tRemoveCopyByCommutingDef: " << AValNo->def << '\t'
676                << *DefMI);
677
678   // At this point we have decided that it is legal to do this
679   // transformation.  Start by commuting the instruction.
680   MachineBasicBlock *MBB = DefMI->getParent();
681   MachineInstr *NewMI = TII->commuteInstruction(DefMI);
682   if (!NewMI)
683     return false;
684   if (TargetRegisterInfo::isVirtualRegister(IntA.reg) &&
685       TargetRegisterInfo::isVirtualRegister(IntB.reg) &&
686       !MRI->constrainRegClass(IntB.reg, MRI->getRegClass(IntA.reg)))
687     return false;
688   if (NewMI != DefMI) {
689     LIS->ReplaceMachineInstrInMaps(DefMI, NewMI);
690     MachineBasicBlock::iterator Pos = DefMI;
691     MBB->insert(Pos, NewMI);
692     MBB->erase(DefMI);
693   }
694   unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
695   NewMI->getOperand(OpIdx).setIsKill();
696
697   // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
698   // A = or A, B
699   // ...
700   // B = A
701   // ...
702   // C = A<kill>
703   // ...
704   //   = B
705
706   // Update uses of IntA of the specific Val# with IntB.
707   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(IntA.reg),
708          UE = MRI->use_end(); UI != UE;) {
709     MachineOperand &UseMO = UI.getOperand();
710     MachineInstr *UseMI = &*UI;
711     ++UI;
712     if (JoinedCopies.count(UseMI))
713       continue;
714     if (UseMI->isDebugValue()) {
715       // FIXME These don't have an instruction index.  Not clear we have enough
716       // info to decide whether to do this replacement or not.  For now do it.
717       UseMO.setReg(NewReg);
718       continue;
719     }
720     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI).getRegSlot(true);
721     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
722     if (ULR == IntA.end() || ULR->valno != AValNo)
723       continue;
724     if (TargetRegisterInfo::isPhysicalRegister(NewReg))
725       UseMO.substPhysReg(NewReg, *TRI);
726     else
727       UseMO.setReg(NewReg);
728     if (UseMI == CopyMI)
729       continue;
730     if (!UseMI->isCopy())
731       continue;
732     if (UseMI->getOperand(0).getReg() != IntB.reg ||
733         UseMI->getOperand(0).getSubReg())
734       continue;
735
736     // This copy will become a noop. If it's defining a new val#, merge it into
737     // BValNo.
738     SlotIndex DefIdx = UseIdx.getRegSlot();
739     VNInfo *DVNI = IntB.getVNInfoAt(DefIdx);
740     if (!DVNI)
741       continue;
742     DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI);
743     assert(DVNI->def == DefIdx);
744     BValNo = IntB.MergeValueNumberInto(BValNo, DVNI);
745     markAsJoined(UseMI);
746   }
747
748   // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
749   // is updated.
750   VNInfo *ValNo = BValNo;
751   ValNo->def = AValNo->def;
752   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
753        AI != AE; ++AI) {
754     if (AI->valno != AValNo) continue;
755     IntB.addRange(LiveRange(AI->start, AI->end, ValNo));
756   }
757   DEBUG(dbgs() << "\t\textended: " << IntB << '\n');
758
759   IntA.removeValNo(AValNo);
760   DEBUG(dbgs() << "\t\ttrimmed:  " << IntA << '\n');
761   ++numCommutes;
762   return true;
763 }
764
765 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
766 /// computation, replace the copy by rematerialize the definition.
767 bool RegisterCoalescer::ReMaterializeTrivialDef(LiveInterval &SrcInt,
768                                                        bool preserveSrcInt,
769                                                        unsigned DstReg,
770                                                        MachineInstr *CopyMI) {
771   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(true);
772   LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx);
773   assert(SrcLR != SrcInt.end() && "Live range not found!");
774   VNInfo *ValNo = SrcLR->valno;
775   if (ValNo->isPHIDef() || ValNo->isUnused())
776     return false;
777   MachineInstr *DefMI = LIS->getInstructionFromIndex(ValNo->def);
778   if (!DefMI)
779     return false;
780   assert(DefMI && "Defining instruction disappeared");
781   if (!DefMI->isAsCheapAsAMove())
782     return false;
783   if (!TII->isTriviallyReMaterializable(DefMI, AA))
784     return false;
785   bool SawStore = false;
786   if (!DefMI->isSafeToMove(TII, AA, SawStore))
787     return false;
788   const MCInstrDesc &MCID = DefMI->getDesc();
789   if (MCID.getNumDefs() != 1)
790     return false;
791   if (!DefMI->isImplicitDef()) {
792     // Make sure the copy destination register class fits the instruction
793     // definition register class. The mismatch can happen as a result of earlier
794     // extract_subreg, insert_subreg, subreg_to_reg coalescing.
795     const TargetRegisterClass *RC = TII->getRegClass(MCID, 0, TRI);
796     if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
797       if (MRI->getRegClass(DstReg) != RC)
798         return false;
799     } else if (!RC->contains(DstReg))
800       return false;
801   }
802
803   MachineBasicBlock *MBB = CopyMI->getParent();
804   MachineBasicBlock::iterator MII =
805     llvm::next(MachineBasicBlock::iterator(CopyMI));
806   TII->reMaterialize(*MBB, MII, DstReg, 0, DefMI, *TRI);
807   MachineInstr *NewMI = prior(MII);
808
809   // NewMI may have dead implicit defs (E.g. EFLAGS for MOV<bits>r0 on X86).
810   // We need to remember these so we can add intervals once we insert
811   // NewMI into SlotIndexes.
812   SmallVector<unsigned, 4> NewMIImplDefs;
813   for (unsigned i = NewMI->getDesc().getNumOperands(),
814          e = NewMI->getNumOperands(); i != e; ++i) {
815     MachineOperand &MO = NewMI->getOperand(i);
816     if (MO.isReg()) {
817       assert(MO.isDef() && MO.isImplicit() && MO.isDead() &&
818              TargetRegisterInfo::isPhysicalRegister(MO.getReg()));
819       NewMIImplDefs.push_back(MO.getReg());
820     }
821   }
822
823   // CopyMI may have implicit operands, transfer them over to the newly
824   // rematerialized instruction. And update implicit def interval valnos.
825   for (unsigned i = CopyMI->getDesc().getNumOperands(),
826          e = CopyMI->getNumOperands(); i != e; ++i) {
827     MachineOperand &MO = CopyMI->getOperand(i);
828     if (MO.isReg()) {
829       assert(MO.isImplicit() && "No explicit operands after implict operands.");
830       // Discard VReg implicit defs.
831       if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
832         NewMI->addOperand(MO);
833       }
834     }
835   }
836
837   LIS->ReplaceMachineInstrInMaps(CopyMI, NewMI);
838
839   SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
840   for (unsigned i = 0, e = NewMIImplDefs.size(); i != e; ++i) {
841     unsigned reg = NewMIImplDefs[i];
842     LiveInterval &li = LIS->getInterval(reg);
843     VNInfo *DeadDefVN = li.getNextValue(NewMIIdx.getRegSlot(),
844                                         LIS->getVNInfoAllocator());
845     LiveRange lr(NewMIIdx.getRegSlot(), NewMIIdx.getDeadSlot(), DeadDefVN);
846     li.addRange(lr);
847   }
848
849   CopyMI->eraseFromParent();
850   ReMatCopies.insert(CopyMI);
851   ReMatDefs.insert(DefMI);
852   DEBUG(dbgs() << "Remat: " << *NewMI);
853   ++NumReMats;
854
855   // The source interval can become smaller because we removed a use.
856   if (preserveSrcInt)
857     LIS->shrinkToUses(&SrcInt);
858
859   return true;
860 }
861
862 /// eliminateUndefCopy - ProcessImpicitDefs may leave some copies of <undef>
863 /// values, it only removes local variables. When we have a copy like:
864 ///
865 ///   %vreg1 = COPY %vreg2<undef>
866 ///
867 /// We delete the copy and remove the corresponding value number from %vreg1.
868 /// Any uses of that value number are marked as <undef>.
869 bool RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI,
870                                            const CoalescerPair &CP) {
871   SlotIndex Idx = LIS->getInstructionIndex(CopyMI);
872   LiveInterval *SrcInt = &LIS->getInterval(CP.getSrcReg());
873   if (SrcInt->liveAt(Idx))
874     return false;
875   LiveInterval *DstInt = &LIS->getInterval(CP.getDstReg());
876   if (DstInt->liveAt(Idx))
877     return false;
878
879   // No intervals are live-in to CopyMI - it is undef.
880   if (CP.isFlipped())
881     DstInt = SrcInt;
882   SrcInt = 0;
883
884   VNInfo *DeadVNI = DstInt->getVNInfoAt(Idx.getRegSlot());
885   assert(DeadVNI && "No value defined in DstInt");
886   DstInt->removeValNo(DeadVNI);
887
888   // Find new undef uses.
889   for (MachineRegisterInfo::reg_nodbg_iterator
890          I = MRI->reg_nodbg_begin(DstInt->reg), E = MRI->reg_nodbg_end();
891        I != E; ++I) {
892     MachineOperand &MO = I.getOperand();
893     if (MO.isDef() || MO.isUndef())
894       continue;
895     MachineInstr *MI = MO.getParent();
896     SlotIndex Idx = LIS->getInstructionIndex(MI);
897     if (DstInt->liveAt(Idx))
898       continue;
899     MO.setIsUndef(true);
900     DEBUG(dbgs() << "\tnew undef: " << Idx << '\t' << *MI);
901   }
902   return true;
903 }
904
905 /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
906 /// update the subregister number if it is not zero. If DstReg is a
907 /// physical register and the existing subregister number of the def / use
908 /// being updated is not zero, make sure to set it to the correct physical
909 /// subregister.
910 void
911 RegisterCoalescer::UpdateRegDefsUses(const CoalescerPair &CP) {
912   bool DstIsPhys = CP.isPhys();
913   unsigned SrcReg = CP.getSrcReg();
914   unsigned DstReg = CP.getDstReg();
915   unsigned SubIdx = CP.getSubIdx();
916
917   // Update LiveDebugVariables.
918   LDV->renameRegister(SrcReg, DstReg, SubIdx);
919
920   for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(SrcReg);
921        MachineInstr *UseMI = I.skipInstruction();) {
922     // A PhysReg copy that won't be coalesced can perhaps be rematerialized
923     // instead.
924     if (DstIsPhys) {
925       if (UseMI->isFullCopy() &&
926           UseMI->getOperand(1).getReg() == SrcReg &&
927           UseMI->getOperand(0).getReg() != SrcReg &&
928           UseMI->getOperand(0).getReg() != DstReg &&
929           !JoinedCopies.count(UseMI) &&
930           ReMaterializeTrivialDef(LIS->getInterval(SrcReg), false,
931                                   UseMI->getOperand(0).getReg(), UseMI))
932         continue;
933     }
934
935     SmallVector<unsigned,8> Ops;
936     bool Reads, Writes;
937     tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops);
938     bool Kills = false, Deads = false;
939
940     // Replace SrcReg with DstReg in all UseMI operands.
941     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
942       MachineOperand &MO = UseMI->getOperand(Ops[i]);
943       Kills |= MO.isKill();
944       Deads |= MO.isDead();
945
946       // Make sure we don't create read-modify-write defs accidentally.  We
947       // assume here that a SrcReg def cannot be joined into a live DstReg.  If
948       // RegisterCoalescer starts tracking partially live registers, we will
949       // need to check the actual LiveInterval to determine if DstReg is live
950       // here.
951       if (SubIdx && !Reads)
952         MO.setIsUndef();
953
954       if (DstIsPhys)
955         MO.substPhysReg(DstReg, *TRI);
956       else
957         MO.substVirtReg(DstReg, SubIdx, *TRI);
958     }
959
960     // This instruction is a copy that will be removed.
961     if (JoinedCopies.count(UseMI))
962       continue;
963
964     if (SubIdx) {
965       // If UseMI was a simple SrcReg def, make sure we didn't turn it into a
966       // read-modify-write of DstReg.
967       if (Deads)
968         UseMI->addRegisterDead(DstReg, TRI);
969       else if (!Reads && Writes)
970         UseMI->addRegisterDefined(DstReg, TRI);
971
972       // Kill flags apply to the whole physical register.
973       if (DstIsPhys && Kills)
974         UseMI->addRegisterKilled(DstReg, TRI);
975     }
976
977     DEBUG({
978         dbgs() << "\t\tupdated: ";
979         if (!UseMI->isDebugValue())
980           dbgs() << LIS->getInstructionIndex(UseMI) << "\t";
981         dbgs() << *UseMI;
982       });
983   }
984 }
985
986 /// removeIntervalIfEmpty - Check if the live interval of a physical register
987 /// is empty, if so remove it and also remove the empty intervals of its
988 /// sub-registers. Return true if live interval is removed.
989 static bool removeIntervalIfEmpty(LiveInterval &li, LiveIntervals *LIS,
990                                   const TargetRegisterInfo *TRI) {
991   if (li.empty()) {
992     if (TargetRegisterInfo::isPhysicalRegister(li.reg))
993       for (const unsigned* SR = TRI->getSubRegisters(li.reg); *SR; ++SR) {
994         if (!LIS->hasInterval(*SR))
995           continue;
996         LiveInterval &sli = LIS->getInterval(*SR);
997         if (sli.empty())
998           LIS->removeInterval(*SR);
999       }
1000     LIS->removeInterval(li.reg);
1001     return true;
1002   }
1003   return false;
1004 }
1005
1006 /// RemoveDeadDef - If a def of a live interval is now determined dead, remove
1007 /// the val# it defines. If the live interval becomes empty, remove it as well.
1008 bool RegisterCoalescer::RemoveDeadDef(LiveInterval &li,
1009                                              MachineInstr *DefMI) {
1010   SlotIndex DefIdx = LIS->getInstructionIndex(DefMI).getRegSlot();
1011   LiveInterval::iterator MLR = li.FindLiveRangeContaining(DefIdx);
1012   if (DefIdx != MLR->valno->def)
1013     return false;
1014   li.removeValNo(MLR->valno);
1015   return removeIntervalIfEmpty(li, LIS, TRI);
1016 }
1017
1018 /// shouldJoinPhys - Return true if a copy involving a physreg should be joined.
1019 /// We need to be careful about coalescing a source physical register with a
1020 /// virtual register. Once the coalescing is done, it cannot be broken and these
1021 /// are not spillable! If the destination interval uses are far away, think
1022 /// twice about coalescing them!
1023 bool RegisterCoalescer::shouldJoinPhys(CoalescerPair &CP) {
1024   bool Allocatable = LIS->isAllocatable(CP.getDstReg());
1025   LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg());
1026
1027   /// Always join simple intervals that are defined by a single copy from a
1028   /// reserved register. This doesn't increase register pressure, so it is
1029   /// always beneficial.
1030   if (!Allocatable && CP.isFlipped() && JoinVInt.containsOneValue())
1031     return true;
1032
1033   if (!EnablePhysicalJoin) {
1034     DEBUG(dbgs() << "\tPhysreg joins disabled.\n");
1035     return false;
1036   }
1037
1038   // Only coalesce to allocatable physreg, we don't want to risk modifying
1039   // reserved registers.
1040   if (!Allocatable) {
1041     DEBUG(dbgs() << "\tRegister is an unallocatable physreg.\n");
1042     return false;  // Not coalescable.
1043   }
1044
1045   // Don't join with physregs that have a ridiculous number of live
1046   // ranges. The data structure performance is really bad when that
1047   // happens.
1048   if (LIS->hasInterval(CP.getDstReg()) &&
1049       LIS->getInterval(CP.getDstReg()).ranges.size() > 1000) {
1050     ++numAborts;
1051     DEBUG(dbgs()
1052           << "\tPhysical register live interval too complicated, abort!\n");
1053     return false;
1054   }
1055
1056   // FIXME: Why are we skipping this test for partial copies?
1057   //        CodeGen/X86/phys_subreg_coalesce-3.ll needs it.
1058   if (!CP.isPartial()) {
1059     const TargetRegisterClass *RC = MRI->getRegClass(CP.getSrcReg());
1060     unsigned Threshold = RegClassInfo.getNumAllocatableRegs(RC) * 2;
1061     unsigned Length = LIS->getApproximateInstructionCount(JoinVInt);
1062     if (Length > Threshold) {
1063       ++numAborts;
1064       DEBUG(dbgs() << "\tMay tie down a physical register, abort!\n");
1065       return false;
1066     }
1067   }
1068   return true;
1069 }
1070
1071 /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
1072 /// two virtual registers from different register classes.
1073 bool
1074 RegisterCoalescer::isWinToJoinCrossClass(unsigned SrcReg,
1075                                              unsigned DstReg,
1076                                              const TargetRegisterClass *SrcRC,
1077                                              const TargetRegisterClass *DstRC,
1078                                              const TargetRegisterClass *NewRC) {
1079   unsigned NewRCCount = RegClassInfo.getNumAllocatableRegs(NewRC);
1080   // This heuristics is good enough in practice, but it's obviously not *right*.
1081   // 4 is a magic number that works well enough for x86, ARM, etc. It filter
1082   // out all but the most restrictive register classes.
1083   if (NewRCCount > 4 ||
1084       // Early exit if the function is fairly small, coalesce aggressively if
1085       // that's the case. For really special register classes with 3 or
1086       // fewer registers, be a bit more careful.
1087       (LIS->getFuncInstructionCount() / NewRCCount) < 8)
1088     return true;
1089   LiveInterval &SrcInt = LIS->getInterval(SrcReg);
1090   LiveInterval &DstInt = LIS->getInterval(DstReg);
1091   unsigned SrcSize = LIS->getApproximateInstructionCount(SrcInt);
1092   unsigned DstSize = LIS->getApproximateInstructionCount(DstInt);
1093
1094   // Coalesce aggressively if the intervals are small compared to the number of
1095   // registers in the new class. The number 4 is fairly arbitrary, chosen to be
1096   // less aggressive than the 8 used for the whole function size.
1097   const unsigned ThresSize = 4 * NewRCCount;
1098   if (SrcSize <= ThresSize && DstSize <= ThresSize)
1099     return true;
1100
1101   // Estimate *register use density*. If it doubles or more, abort.
1102   unsigned SrcUses = std::distance(MRI->use_nodbg_begin(SrcReg),
1103                                    MRI->use_nodbg_end());
1104   unsigned DstUses = std::distance(MRI->use_nodbg_begin(DstReg),
1105                                    MRI->use_nodbg_end());
1106   unsigned NewUses = SrcUses + DstUses;
1107   unsigned NewSize = SrcSize + DstSize;
1108   if (SrcRC != NewRC && SrcSize > ThresSize) {
1109     unsigned SrcRCCount = RegClassInfo.getNumAllocatableRegs(SrcRC);
1110     if (NewUses*SrcSize*SrcRCCount > 2*SrcUses*NewSize*NewRCCount)
1111       return false;
1112   }
1113   if (DstRC != NewRC && DstSize > ThresSize) {
1114     unsigned DstRCCount = RegClassInfo.getNumAllocatableRegs(DstRC);
1115     if (NewUses*DstSize*DstRCCount > 2*DstUses*NewSize*NewRCCount)
1116       return false;
1117   }
1118   return true;
1119 }
1120
1121
1122 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
1123 /// which are the src/dst of the copy instruction CopyMI.  This returns true
1124 /// if the copy was successfully coalesced away. If it is not currently
1125 /// possible to coalesce this interval, but it may be possible if other
1126 /// things get coalesced, then it returns true by reference in 'Again'.
1127 bool RegisterCoalescer::JoinCopy(MachineInstr *CopyMI, bool &Again) {
1128
1129   Again = false;
1130   if (JoinedCopies.count(CopyMI) || ReMatCopies.count(CopyMI))
1131     return false; // Already done.
1132
1133   DEBUG(dbgs() << LIS->getInstructionIndex(CopyMI) << '\t' << *CopyMI);
1134
1135   CoalescerPair CP(*TII, *TRI);
1136   if (!CP.setRegisters(CopyMI)) {
1137     DEBUG(dbgs() << "\tNot coalescable.\n");
1138     return false;
1139   }
1140
1141   // If they are already joined we continue.
1142   if (CP.getSrcReg() == CP.getDstReg()) {
1143     markAsJoined(CopyMI);
1144     DEBUG(dbgs() << "\tCopy already coalesced.\n");
1145     return false;  // Not coalescable.
1146   }
1147
1148   // Eliminate undefs.
1149   if (!CP.isPhys() && eliminateUndefCopy(CopyMI, CP)) {
1150     markAsJoined(CopyMI);
1151     DEBUG(dbgs() << "\tEliminated copy of <undef> value.\n");
1152     return false;  // Not coalescable.
1153   }
1154
1155   DEBUG(dbgs() << "\tConsidering merging " << PrintReg(CP.getSrcReg(), TRI)
1156                << " with " << PrintReg(CP.getDstReg(), TRI, CP.getSubIdx())
1157                << "\n");
1158
1159   // Enforce policies.
1160   if (CP.isPhys()) {
1161     if (!shouldJoinPhys(CP)) {
1162       // Before giving up coalescing, if definition of source is defined by
1163       // trivial computation, try rematerializing it.
1164       if (!CP.isFlipped() &&
1165           ReMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()), true,
1166                                   CP.getDstReg(), CopyMI))
1167         return true;
1168       return false;
1169     }
1170   } else {
1171     // Avoid constraining virtual register regclass too much.
1172     if (CP.isCrossClass()) {
1173       DEBUG(dbgs() << "\tCross-class to " << CP.getNewRC()->getName() << ".\n");
1174       if (DisableCrossClassJoin) {
1175         DEBUG(dbgs() << "\tCross-class joins disabled.\n");
1176         return false;
1177       }
1178       if (!isWinToJoinCrossClass(CP.getSrcReg(), CP.getDstReg(),
1179                                  MRI->getRegClass(CP.getSrcReg()),
1180                                  MRI->getRegClass(CP.getDstReg()),
1181                                  CP.getNewRC())) {
1182         DEBUG(dbgs() << "\tAvoid coalescing to constrained register class.\n");
1183         Again = true;  // May be possible to coalesce later.
1184         return false;
1185       }
1186     }
1187
1188     // When possible, let DstReg be the larger interval.
1189     if (!CP.getSubIdx() && LIS->getInterval(CP.getSrcReg()).ranges.size() >
1190                            LIS->getInterval(CP.getDstReg()).ranges.size())
1191       CP.flip();
1192   }
1193
1194   // Okay, attempt to join these two intervals.  On failure, this returns false.
1195   // Otherwise, if one of the intervals being joined is a physreg, this method
1196   // always canonicalizes DstInt to be it.  The output "SrcInt" will not have
1197   // been modified, so we can use this information below to update aliases.
1198   if (!JoinIntervals(CP)) {
1199     // Coalescing failed.
1200
1201     // If definition of source is defined by trivial computation, try
1202     // rematerializing it.
1203     if (!CP.isFlipped() &&
1204         ReMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()), true,
1205                                 CP.getDstReg(), CopyMI))
1206       return true;
1207
1208     // If we can eliminate the copy without merging the live ranges, do so now.
1209     if (!CP.isPartial()) {
1210       if (AdjustCopiesBackFrom(CP, CopyMI) ||
1211           RemoveCopyByCommutingDef(CP, CopyMI)) {
1212         markAsJoined(CopyMI);
1213         DEBUG(dbgs() << "\tTrivial!\n");
1214         return true;
1215       }
1216     }
1217
1218     // Otherwise, we are unable to join the intervals.
1219     DEBUG(dbgs() << "\tInterference!\n");
1220     Again = true;  // May be possible to coalesce later.
1221     return false;
1222   }
1223
1224   // Coalescing to a virtual register that is of a sub-register class of the
1225   // other. Make sure the resulting register is set to the right register class.
1226   if (CP.isCrossClass()) {
1227     ++numCrossRCs;
1228     MRI->setRegClass(CP.getDstReg(), CP.getNewRC());
1229   }
1230
1231   // Remember to delete the copy instruction.
1232   markAsJoined(CopyMI);
1233
1234   UpdateRegDefsUses(CP);
1235
1236   // If we have extended the live range of a physical register, make sure we
1237   // update live-in lists as well.
1238   if (CP.isPhys()) {
1239     SmallVector<MachineBasicBlock*, 16> BlockSeq;
1240     // JoinIntervals invalidates the VNInfos in SrcInt, but we only need the
1241     // ranges for this, and they are preserved.
1242     LiveInterval &SrcInt = LIS->getInterval(CP.getSrcReg());
1243     for (LiveInterval::const_iterator I = SrcInt.begin(), E = SrcInt.end();
1244          I != E; ++I ) {
1245       LIS->findLiveInMBBs(I->start, I->end, BlockSeq);
1246       for (unsigned idx = 0, size = BlockSeq.size(); idx != size; ++idx) {
1247         MachineBasicBlock &block = *BlockSeq[idx];
1248         if (!block.isLiveIn(CP.getDstReg()))
1249           block.addLiveIn(CP.getDstReg());
1250       }
1251       BlockSeq.clear();
1252     }
1253   }
1254
1255   // SrcReg is guaranteed to be the register whose live interval that is
1256   // being merged.
1257   LIS->removeInterval(CP.getSrcReg());
1258
1259   // Update regalloc hint.
1260   TRI->UpdateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF);
1261
1262   DEBUG({
1263     LiveInterval &DstInt = LIS->getInterval(CP.getDstReg());
1264     dbgs() << "\tJoined. Result = ";
1265     DstInt.print(dbgs(), TRI);
1266     dbgs() << "\n";
1267   });
1268
1269   ++numJoins;
1270   return true;
1271 }
1272
1273 /// ComputeUltimateVN - Assuming we are going to join two live intervals,
1274 /// compute what the resultant value numbers for each value in the input two
1275 /// ranges will be.  This is complicated by copies between the two which can
1276 /// and will commonly cause multiple value numbers to be merged into one.
1277 ///
1278 /// VN is the value number that we're trying to resolve.  InstDefiningValue
1279 /// keeps track of the new InstDefiningValue assignment for the result
1280 /// LiveInterval.  ThisFromOther/OtherFromThis are sets that keep track of
1281 /// whether a value in this or other is a copy from the opposite set.
1282 /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
1283 /// already been assigned.
1284 ///
1285 /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1286 /// contains the value number the copy is from.
1287 ///
1288 static unsigned ComputeUltimateVN(VNInfo *VNI,
1289                                   SmallVector<VNInfo*, 16> &NewVNInfo,
1290                                   DenseMap<VNInfo*, VNInfo*> &ThisFromOther,
1291                                   DenseMap<VNInfo*, VNInfo*> &OtherFromThis,
1292                                   SmallVector<int, 16> &ThisValNoAssignments,
1293                                   SmallVector<int, 16> &OtherValNoAssignments) {
1294   unsigned VN = VNI->id;
1295
1296   // If the VN has already been computed, just return it.
1297   if (ThisValNoAssignments[VN] >= 0)
1298     return ThisValNoAssignments[VN];
1299   assert(ThisValNoAssignments[VN] != -2 && "Cyclic value numbers");
1300
1301   // If this val is not a copy from the other val, then it must be a new value
1302   // number in the destination.
1303   DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI);
1304   if (I == ThisFromOther.end()) {
1305     NewVNInfo.push_back(VNI);
1306     return ThisValNoAssignments[VN] = NewVNInfo.size()-1;
1307   }
1308   VNInfo *OtherValNo = I->second;
1309
1310   // Otherwise, this *is* a copy from the RHS.  If the other side has already
1311   // been computed, return it.
1312   if (OtherValNoAssignments[OtherValNo->id] >= 0)
1313     return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id];
1314
1315   // Mark this value number as currently being computed, then ask what the
1316   // ultimate value # of the other value is.
1317   ThisValNoAssignments[VN] = -2;
1318   unsigned UltimateVN =
1319     ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther,
1320                       OtherValNoAssignments, ThisValNoAssignments);
1321   return ThisValNoAssignments[VN] = UltimateVN;
1322 }
1323
1324
1325 // Find out if we have something like
1326 // A = X
1327 // B = X
1328 // if so, we can pretend this is actually
1329 // A = X
1330 // B = A
1331 // which allows us to coalesce A and B.
1332 // VNI is the definition of B. LR is the life range of A that includes
1333 // the slot just before B. If we return true, we add "B = X" to DupCopies.
1334 // This implies that A dominates B.
1335 static bool RegistersDefinedFromSameValue(LiveIntervals &li,
1336                                           const TargetRegisterInfo &tri,
1337                                           CoalescerPair &CP,
1338                                           VNInfo *VNI,
1339                                           LiveRange *LR,
1340                                      SmallVector<MachineInstr*, 8> &DupCopies) {
1341   // FIXME: This is very conservative. For example, we don't handle
1342   // physical registers.
1343
1344   MachineInstr *MI = li.getInstructionFromIndex(VNI->def);
1345
1346   if (!MI || !MI->isFullCopy() || CP.isPartial() || CP.isPhys())
1347     return false;
1348
1349   unsigned Dst = MI->getOperand(0).getReg();
1350   unsigned Src = MI->getOperand(1).getReg();
1351
1352   if (!TargetRegisterInfo::isVirtualRegister(Src) ||
1353       !TargetRegisterInfo::isVirtualRegister(Dst))
1354     return false;
1355
1356   unsigned A = CP.getDstReg();
1357   unsigned B = CP.getSrcReg();
1358
1359   if (B == Dst)
1360     std::swap(A, B);
1361   assert(Dst == A);
1362
1363   VNInfo *Other = LR->valno;
1364   const MachineInstr *OtherMI = li.getInstructionFromIndex(Other->def);
1365
1366   if (!OtherMI || !OtherMI->isFullCopy())
1367     return false;
1368
1369   unsigned OtherDst = OtherMI->getOperand(0).getReg();
1370   unsigned OtherSrc = OtherMI->getOperand(1).getReg();
1371
1372   if (!TargetRegisterInfo::isVirtualRegister(OtherSrc) ||
1373       !TargetRegisterInfo::isVirtualRegister(OtherDst))
1374     return false;
1375
1376   assert(OtherDst == B);
1377
1378   if (Src != OtherSrc)
1379     return false;
1380
1381   // If the copies use two different value numbers of X, we cannot merge
1382   // A and B.
1383   LiveInterval &SrcInt = li.getInterval(Src);
1384   // getVNInfoBefore returns NULL for undef copies. In this case, the
1385   // optimization is still safe.
1386   if (SrcInt.getVNInfoBefore(Other->def) != SrcInt.getVNInfoBefore(VNI->def))
1387     return false;
1388
1389   DupCopies.push_back(MI);
1390
1391   return true;
1392 }
1393
1394 /// JoinIntervals - Attempt to join these two intervals.  On failure, this
1395 /// returns false.
1396 bool RegisterCoalescer::JoinIntervals(CoalescerPair &CP) {
1397   LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1398   DEBUG({ dbgs() << "\t\tRHS = "; RHS.print(dbgs(), TRI); dbgs() << "\n"; });
1399
1400   // If a live interval is a physical register, check for interference with any
1401   // aliases. The interference check implemented here is a bit more conservative
1402   // than the full interfeence check below. We allow overlapping live ranges
1403   // only when one is a copy of the other.
1404   if (CP.isPhys()) {
1405     // Optimization for reserved registers like ESP.
1406     // We can only merge with a reserved physreg if RHS has a single value that
1407     // is a copy of CP.DstReg().  The live range of the reserved register will
1408     // look like a set of dead defs - we don't properly track the live range of
1409     // reserved registers.
1410     if (RegClassInfo.isReserved(CP.getDstReg())) {
1411       assert(CP.isFlipped() && RHS.containsOneValue() &&
1412              "Invalid join with reserved register");
1413       // Deny any overlapping intervals.  This depends on all the reserved
1414       // register live ranges to look like dead defs.
1415       for (const uint16_t *AS = TRI->getOverlaps(CP.getDstReg()); *AS; ++AS) {
1416         if (!LIS->hasInterval(*AS)) {
1417           // Make sure at least DstReg itself exists before attempting a join.
1418           if (*AS == CP.getDstReg())
1419             LIS->getOrCreateInterval(CP.getDstReg());
1420           continue;
1421         }
1422         if (RHS.overlaps(LIS->getInterval(*AS))) {
1423           DEBUG(dbgs() << "\t\tInterference: " << PrintReg(*AS, TRI) << '\n');
1424           return false;
1425         }
1426       }
1427       // Skip any value computations, we are not adding new values to the
1428       // reserved register.  Also skip merging the live ranges, the reserved
1429       // register live range doesn't need to be accurate as long as all the
1430       // defs are there.
1431       return true;
1432     }
1433
1434     // Check if a register mask clobbers DstReg.
1435     BitVector UsableRegs;
1436     if (LIS->checkRegMaskInterference(RHS, UsableRegs) &&
1437         !UsableRegs.test(CP.getDstReg())) {
1438       DEBUG(dbgs() << "\t\tRegister mask interference.\n");
1439       return false;
1440     }
1441
1442     for (const uint16_t *AS = TRI->getAliasSet(CP.getDstReg()); *AS; ++AS){
1443       if (!LIS->hasInterval(*AS))
1444         continue;
1445       const LiveInterval &LHS = LIS->getInterval(*AS);
1446       LiveInterval::const_iterator LI = LHS.begin();
1447       for (LiveInterval::const_iterator RI = RHS.begin(), RE = RHS.end();
1448            RI != RE; ++RI) {
1449         LI = std::lower_bound(LI, LHS.end(), RI->start);
1450         // Does LHS have an overlapping live range starting before RI?
1451         if ((LI != LHS.begin() && LI[-1].end > RI->start) &&
1452             (RI->start != RI->valno->def ||
1453              !CP.isCoalescable(LIS->getInstructionFromIndex(RI->start)))) {
1454           DEBUG({
1455             dbgs() << "\t\tInterference from alias: ";
1456             LHS.print(dbgs(), TRI);
1457             dbgs() << "\n\t\tOverlap at " << RI->start << " and no copy.\n";
1458           });
1459           return false;
1460         }
1461
1462         // Check that LHS ranges beginning in this range are copies.
1463         for (; LI != LHS.end() && LI->start < RI->end; ++LI) {
1464           if (LI->start != LI->valno->def ||
1465               !CP.isCoalescable(LIS->getInstructionFromIndex(LI->start))) {
1466             DEBUG({
1467               dbgs() << "\t\tInterference from alias: ";
1468               LHS.print(dbgs(), TRI);
1469               dbgs() << "\n\t\tDef at " << LI->start << " is not a copy.\n";
1470             });
1471             return false;
1472           }
1473         }
1474       }
1475     }
1476   }
1477
1478   // Compute the final value assignment, assuming that the live ranges can be
1479   // coalesced.
1480   SmallVector<int, 16> LHSValNoAssignments;
1481   SmallVector<int, 16> RHSValNoAssignments;
1482   DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS;
1483   DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS;
1484   SmallVector<VNInfo*, 16> NewVNInfo;
1485
1486   SmallVector<MachineInstr*, 8> DupCopies;
1487
1488   LiveInterval &LHS = LIS->getOrCreateInterval(CP.getDstReg());
1489   DEBUG({ dbgs() << "\t\tLHS = "; LHS.print(dbgs(), TRI); dbgs() << "\n"; });
1490
1491   // Loop over the value numbers of the LHS, seeing if any are defined from
1492   // the RHS.
1493   for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1494        i != e; ++i) {
1495     VNInfo *VNI = *i;
1496     if (VNI->isUnused() || VNI->isPHIDef())
1497       continue;
1498     MachineInstr *MI = LIS->getInstructionFromIndex(VNI->def);
1499     assert(MI && "Missing def");
1500     if (!MI->isCopyLike())  // Src not defined by a copy?
1501       continue;
1502
1503     // Figure out the value # from the RHS.
1504     LiveRange *lr = RHS.getLiveRangeContaining(VNI->def.getPrevSlot());
1505     // The copy could be to an aliased physreg.
1506     if (!lr) continue;
1507
1508     // DstReg is known to be a register in the LHS interval.  If the src is
1509     // from the RHS interval, we can use its value #.
1510     if (!CP.isCoalescable(MI) &&
1511         !RegistersDefinedFromSameValue(*LIS, *TRI, CP, VNI, lr, DupCopies))
1512       continue;
1513
1514     LHSValsDefinedFromRHS[VNI] = lr->valno;
1515   }
1516
1517   // Loop over the value numbers of the RHS, seeing if any are defined from
1518   // the LHS.
1519   for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1520        i != e; ++i) {
1521     VNInfo *VNI = *i;
1522     if (VNI->isUnused() || VNI->isPHIDef())
1523       continue;
1524     MachineInstr *MI = LIS->getInstructionFromIndex(VNI->def);
1525     assert(MI && "Missing def");
1526     if (!MI->isCopyLike())  // Src not defined by a copy?
1527       continue;
1528
1529     // Figure out the value # from the LHS.
1530     LiveRange *lr = LHS.getLiveRangeContaining(VNI->def.getPrevSlot());
1531     // The copy could be to an aliased physreg.
1532     if (!lr) continue;
1533
1534     // DstReg is known to be a register in the RHS interval.  If the src is
1535     // from the LHS interval, we can use its value #.
1536     if (!CP.isCoalescable(MI) &&
1537         !RegistersDefinedFromSameValue(*LIS, *TRI, CP, VNI, lr, DupCopies))
1538         continue;
1539
1540     RHSValsDefinedFromLHS[VNI] = lr->valno;
1541   }
1542
1543   LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
1544   RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
1545   NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
1546
1547   for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1548        i != e; ++i) {
1549     VNInfo *VNI = *i;
1550     unsigned VN = VNI->id;
1551     if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused())
1552       continue;
1553     ComputeUltimateVN(VNI, NewVNInfo,
1554                       LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
1555                       LHSValNoAssignments, RHSValNoAssignments);
1556   }
1557   for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1558        i != e; ++i) {
1559     VNInfo *VNI = *i;
1560     unsigned VN = VNI->id;
1561     if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
1562       continue;
1563     // If this value number isn't a copy from the LHS, it's a new number.
1564     if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
1565       NewVNInfo.push_back(VNI);
1566       RHSValNoAssignments[VN] = NewVNInfo.size()-1;
1567       continue;
1568     }
1569
1570     ComputeUltimateVN(VNI, NewVNInfo,
1571                       RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
1572                       RHSValNoAssignments, LHSValNoAssignments);
1573   }
1574
1575   // Armed with the mappings of LHS/RHS values to ultimate values, walk the
1576   // interval lists to see if these intervals are coalescable.
1577   LiveInterval::const_iterator I = LHS.begin();
1578   LiveInterval::const_iterator IE = LHS.end();
1579   LiveInterval::const_iterator J = RHS.begin();
1580   LiveInterval::const_iterator JE = RHS.end();
1581
1582   // Skip ahead until the first place of potential sharing.
1583   if (I != IE && J != JE) {
1584     if (I->start < J->start) {
1585       I = std::upper_bound(I, IE, J->start);
1586       if (I != LHS.begin()) --I;
1587     } else if (J->start < I->start) {
1588       J = std::upper_bound(J, JE, I->start);
1589       if (J != RHS.begin()) --J;
1590     }
1591   }
1592
1593   while (I != IE && J != JE) {
1594     // Determine if these two live ranges overlap.
1595     bool Overlaps;
1596     if (I->start < J->start) {
1597       Overlaps = I->end > J->start;
1598     } else {
1599       Overlaps = J->end > I->start;
1600     }
1601
1602     // If so, check value # info to determine if they are really different.
1603     if (Overlaps) {
1604       // If the live range overlap will map to the same value number in the
1605       // result liverange, we can still coalesce them.  If not, we can't.
1606       if (LHSValNoAssignments[I->valno->id] !=
1607           RHSValNoAssignments[J->valno->id])
1608         return false;
1609     }
1610
1611     if (I->end < J->end)
1612       ++I;
1613     else
1614       ++J;
1615   }
1616
1617   // Update kill info. Some live ranges are extended due to copy coalescing.
1618   for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(),
1619          E = LHSValsDefinedFromRHS.end(); I != E; ++I) {
1620     VNInfo *VNI = I->first;
1621     unsigned LHSValID = LHSValNoAssignments[VNI->id];
1622     if (VNI->hasPHIKill())
1623       NewVNInfo[LHSValID]->setHasPHIKill(true);
1624   }
1625
1626   // Update kill info. Some live ranges are extended due to copy coalescing.
1627   for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(),
1628          E = RHSValsDefinedFromLHS.end(); I != E; ++I) {
1629     VNInfo *VNI = I->first;
1630     unsigned RHSValID = RHSValNoAssignments[VNI->id];
1631     if (VNI->hasPHIKill())
1632       NewVNInfo[RHSValID]->setHasPHIKill(true);
1633   }
1634
1635   if (LHSValNoAssignments.empty())
1636     LHSValNoAssignments.push_back(-1);
1637   if (RHSValNoAssignments.empty())
1638     RHSValNoAssignments.push_back(-1);
1639
1640   SmallVector<unsigned, 8> SourceRegisters;
1641   for (SmallVector<MachineInstr*, 8>::iterator I = DupCopies.begin(),
1642          E = DupCopies.end(); I != E; ++I) {
1643     MachineInstr *MI = *I;
1644
1645     // We have pretended that the assignment to B in
1646     // A = X
1647     // B = X
1648     // was actually a copy from A. Now that we decided to coalesce A and B,
1649     // transform the code into
1650     // A = X
1651     // X = X
1652     // and mark the X as coalesced to keep the illusion.
1653     unsigned Src = MI->getOperand(1).getReg();
1654     SourceRegisters.push_back(Src);
1655     MI->getOperand(0).substVirtReg(Src, 0, *TRI);
1656
1657     markAsJoined(MI);
1658   }
1659
1660   // If B = X was the last use of X in a liverange, we have to shrink it now
1661   // that B = X is gone.
1662   for (SmallVector<unsigned, 8>::iterator I = SourceRegisters.begin(),
1663          E = SourceRegisters.end(); I != E; ++I) {
1664     LIS->shrinkToUses(&LIS->getInterval(*I));
1665   }
1666
1667   // If we get here, we know that we can coalesce the live ranges.  Ask the
1668   // intervals to coalesce themselves now.
1669   LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo,
1670            MRI);
1671   return true;
1672 }
1673
1674 namespace {
1675   // DepthMBBCompare - Comparison predicate that sort first based on the loop
1676   // depth of the basic block (the unsigned), and then on the MBB number.
1677   struct DepthMBBCompare {
1678     typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
1679     bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
1680       // Deeper loops first
1681       if (LHS.first != RHS.first)
1682         return LHS.first > RHS.first;
1683
1684       // Prefer blocks that are more connected in the CFG. This takes care of
1685       // the most difficult copies first while intervals are short.
1686       unsigned cl = LHS.second->pred_size() + LHS.second->succ_size();
1687       unsigned cr = RHS.second->pred_size() + RHS.second->succ_size();
1688       if (cl != cr)
1689         return cl > cr;
1690
1691       // As a last resort, sort by block number.
1692       return LHS.second->getNumber() < RHS.second->getNumber();
1693     }
1694   };
1695 }
1696
1697 void RegisterCoalescer::CopyCoalesceInMBB(MachineBasicBlock *MBB,
1698                                             std::vector<MachineInstr*> &TryAgain) {
1699   DEBUG(dbgs() << MBB->getName() << ":\n");
1700
1701   SmallVector<MachineInstr*, 8> VirtCopies;
1702   SmallVector<MachineInstr*, 8> PhysCopies;
1703   SmallVector<MachineInstr*, 8> ImpDefCopies;
1704   for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
1705        MII != E;) {
1706     MachineInstr *Inst = MII++;
1707
1708     // If this isn't a copy nor a extract_subreg, we can't join intervals.
1709     unsigned SrcReg, DstReg;
1710     if (Inst->isCopy()) {
1711       DstReg = Inst->getOperand(0).getReg();
1712       SrcReg = Inst->getOperand(1).getReg();
1713     } else if (Inst->isSubregToReg()) {
1714       DstReg = Inst->getOperand(0).getReg();
1715       SrcReg = Inst->getOperand(2).getReg();
1716     } else
1717       continue;
1718
1719     bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
1720     bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
1721     if (LIS->hasInterval(SrcReg) && LIS->getInterval(SrcReg).empty())
1722       ImpDefCopies.push_back(Inst);
1723     else if (SrcIsPhys || DstIsPhys)
1724       PhysCopies.push_back(Inst);
1725     else
1726       VirtCopies.push_back(Inst);
1727   }
1728
1729   // Try coalescing implicit copies and insert_subreg <undef> first,
1730   // followed by copies to / from physical registers, then finally copies
1731   // from virtual registers to virtual registers.
1732   for (unsigned i = 0, e = ImpDefCopies.size(); i != e; ++i) {
1733     MachineInstr *TheCopy = ImpDefCopies[i];
1734     bool Again = false;
1735     if (!JoinCopy(TheCopy, Again))
1736       if (Again)
1737         TryAgain.push_back(TheCopy);
1738   }
1739   for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) {
1740     MachineInstr *TheCopy = PhysCopies[i];
1741     bool Again = false;
1742     if (!JoinCopy(TheCopy, Again))
1743       if (Again)
1744         TryAgain.push_back(TheCopy);
1745   }
1746   for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) {
1747     MachineInstr *TheCopy = VirtCopies[i];
1748     bool Again = false;
1749     if (!JoinCopy(TheCopy, Again))
1750       if (Again)
1751         TryAgain.push_back(TheCopy);
1752   }
1753 }
1754
1755 void RegisterCoalescer::joinIntervals() {
1756   DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n");
1757
1758   std::vector<MachineInstr*> TryAgainList;
1759   if (Loops->empty()) {
1760     // If there are no loops in the function, join intervals in function order.
1761     for (MachineFunction::iterator I = MF->begin(), E = MF->end();
1762          I != E; ++I)
1763       CopyCoalesceInMBB(I, TryAgainList);
1764   } else {
1765     // Otherwise, join intervals in inner loops before other intervals.
1766     // Unfortunately we can't just iterate over loop hierarchy here because
1767     // there may be more MBB's than BB's.  Collect MBB's for sorting.
1768
1769     // Join intervals in the function prolog first. We want to join physical
1770     // registers with virtual registers before the intervals got too long.
1771     std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
1772     for (MachineFunction::iterator I = MF->begin(), E = MF->end();I != E;++I){
1773       MachineBasicBlock *MBB = I;
1774       MBBs.push_back(std::make_pair(Loops->getLoopDepth(MBB), I));
1775     }
1776
1777     // Sort by loop depth.
1778     std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
1779
1780     // Finally, join intervals in loop nest order.
1781     for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
1782       CopyCoalesceInMBB(MBBs[i].second, TryAgainList);
1783   }
1784
1785   // Joining intervals can allow other intervals to be joined.  Iteratively join
1786   // until we make no progress.
1787   bool ProgressMade = true;
1788   while (ProgressMade) {
1789     ProgressMade = false;
1790
1791     for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
1792       MachineInstr *&TheCopy = TryAgainList[i];
1793       if (!TheCopy)
1794         continue;
1795
1796       bool Again = false;
1797       bool Success = JoinCopy(TheCopy, Again);
1798       if (Success || !Again) {
1799         TheCopy= 0;   // Mark this one as done.
1800         ProgressMade = true;
1801       }
1802     }
1803   }
1804 }
1805
1806 void RegisterCoalescer::releaseMemory() {
1807   JoinedCopies.clear();
1808   ReMatCopies.clear();
1809   ReMatDefs.clear();
1810 }
1811
1812 bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
1813   MF = &fn;
1814   MRI = &fn.getRegInfo();
1815   TM = &fn.getTarget();
1816   TRI = TM->getRegisterInfo();
1817   TII = TM->getInstrInfo();
1818   LIS = &getAnalysis<LiveIntervals>();
1819   LDV = &getAnalysis<LiveDebugVariables>();
1820   AA = &getAnalysis<AliasAnalysis>();
1821   Loops = &getAnalysis<MachineLoopInfo>();
1822
1823   DEBUG(dbgs() << "********** SIMPLE REGISTER COALESCING **********\n"
1824                << "********** Function: "
1825                << ((Value*)MF->getFunction())->getName() << '\n');
1826
1827   if (VerifyCoalescing)
1828     MF->verify(this, "Before register coalescing");
1829
1830   RegClassInfo.runOnMachineFunction(fn);
1831
1832   // Join (coalesce) intervals if requested.
1833   if (EnableJoining) {
1834     joinIntervals();
1835     DEBUG({
1836         dbgs() << "********** INTERVALS POST JOINING **********\n";
1837         for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end();
1838              I != E; ++I){
1839           I->second->print(dbgs(), TRI);
1840           dbgs() << "\n";
1841         }
1842       });
1843   }
1844
1845   // Perform a final pass over the instructions and compute spill weights
1846   // and remove identity moves.
1847   SmallVector<unsigned, 4> DeadDefs, InflateRegs;
1848   for (MachineFunction::iterator mbbi = MF->begin(), mbbe = MF->end();
1849        mbbi != mbbe; ++mbbi) {
1850     MachineBasicBlock* mbb = mbbi;
1851     for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
1852          mii != mie; ) {
1853       MachineInstr *MI = mii;
1854       if (JoinedCopies.count(MI)) {
1855         // Delete all coalesced copies.
1856         bool DoDelete = true;
1857         assert(MI->isCopyLike() && "Unrecognized copy instruction");
1858         unsigned SrcReg = MI->getOperand(MI->isSubregToReg() ? 2 : 1).getReg();
1859         unsigned DstReg = MI->getOperand(0).getReg();
1860
1861         // Collect candidates for register class inflation.
1862         if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1863             RegClassInfo.isProperSubClass(MRI->getRegClass(SrcReg)))
1864           InflateRegs.push_back(SrcReg);
1865         if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
1866             RegClassInfo.isProperSubClass(MRI->getRegClass(DstReg)))
1867           InflateRegs.push_back(DstReg);
1868
1869         if (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
1870             MI->getNumOperands() > 2)
1871           // Do not delete extract_subreg, insert_subreg of physical
1872           // registers unless the definition is dead. e.g.
1873           // %DO<def> = INSERT_SUBREG %D0<undef>, %S0<kill>, 1
1874           // or else the scavenger may complain. LowerSubregs will
1875           // delete them later.
1876           DoDelete = false;
1877
1878         if (MI->allDefsAreDead()) {
1879           if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1880               LIS->hasInterval(SrcReg))
1881             LIS->shrinkToUses(&LIS->getInterval(SrcReg));
1882           DoDelete = true;
1883         }
1884         if (!DoDelete) {
1885           // We need the instruction to adjust liveness, so make it a KILL.
1886           if (MI->isSubregToReg()) {
1887             MI->RemoveOperand(3);
1888             MI->RemoveOperand(1);
1889           }
1890           MI->setDesc(TII->get(TargetOpcode::KILL));
1891           mii = llvm::next(mii);
1892         } else {
1893           LIS->RemoveMachineInstrFromMaps(MI);
1894           mii = mbbi->erase(mii);
1895           ++numPeep;
1896         }
1897         continue;
1898       }
1899
1900       // Now check if this is a remat'ed def instruction which is now dead.
1901       if (ReMatDefs.count(MI)) {
1902         bool isDead = true;
1903         for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1904           const MachineOperand &MO = MI->getOperand(i);
1905           if (!MO.isReg())
1906             continue;
1907           unsigned Reg = MO.getReg();
1908           if (!Reg)
1909             continue;
1910           DeadDefs.push_back(Reg);
1911           if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1912             // Remat may also enable register class inflation.
1913             if (RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)))
1914               InflateRegs.push_back(Reg);
1915           }
1916           if (MO.isDead())
1917             continue;
1918           if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
1919               !MRI->use_nodbg_empty(Reg)) {
1920             isDead = false;
1921             break;
1922           }
1923         }
1924         if (isDead) {
1925           while (!DeadDefs.empty()) {
1926             unsigned DeadDef = DeadDefs.back();
1927             DeadDefs.pop_back();
1928             RemoveDeadDef(LIS->getInterval(DeadDef), MI);
1929           }
1930           LIS->RemoveMachineInstrFromMaps(mii);
1931           mii = mbbi->erase(mii);
1932           continue;
1933         } else
1934           DeadDefs.clear();
1935       }
1936
1937       ++mii;
1938
1939       // Check for now unnecessary kill flags.
1940       if (LIS->isNotInMIMap(MI)) continue;
1941       SlotIndex DefIdx = LIS->getInstructionIndex(MI).getRegSlot();
1942       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1943         MachineOperand &MO = MI->getOperand(i);
1944         if (!MO.isReg() || !MO.isKill()) continue;
1945         unsigned reg = MO.getReg();
1946         if (!reg || !LIS->hasInterval(reg)) continue;
1947         if (!LIS->getInterval(reg).killedAt(DefIdx)) {
1948           MO.setIsKill(false);
1949           continue;
1950         }
1951         // When leaving a kill flag on a physreg, check if any subregs should
1952         // remain alive.
1953         if (!TargetRegisterInfo::isPhysicalRegister(reg))
1954           continue;
1955         for (const unsigned *SR = TRI->getSubRegisters(reg);
1956              unsigned S = *SR; ++SR)
1957           if (LIS->hasInterval(S) && LIS->getInterval(S).liveAt(DefIdx))
1958             MI->addRegisterDefined(S, TRI);
1959       }
1960     }
1961   }
1962
1963   // After deleting a lot of copies, register classes may be less constrained.
1964   // Removing sub-register opreands may alow GR32_ABCD -> GR32 and DPR_VFP2 ->
1965   // DPR inflation.
1966   array_pod_sort(InflateRegs.begin(), InflateRegs.end());
1967   InflateRegs.erase(std::unique(InflateRegs.begin(), InflateRegs.end()),
1968                     InflateRegs.end());
1969   DEBUG(dbgs() << "Trying to inflate " << InflateRegs.size() << " regs.\n");
1970   for (unsigned i = 0, e = InflateRegs.size(); i != e; ++i) {
1971     unsigned Reg = InflateRegs[i];
1972     if (MRI->reg_nodbg_empty(Reg))
1973       continue;
1974     if (MRI->recomputeRegClass(Reg, *TM)) {
1975       DEBUG(dbgs() << PrintReg(Reg) << " inflated to "
1976                    << MRI->getRegClass(Reg)->getName() << '\n');
1977       ++NumInflated;
1978     }
1979   }
1980
1981   DEBUG(dump());
1982   DEBUG(LDV->dump());
1983   if (VerifyCoalescing)
1984     MF->verify(this, "After register coalescing");
1985   return true;
1986 }
1987
1988 /// print - Implement the dump method.
1989 void RegisterCoalescer::print(raw_ostream &O, const Module* m) const {
1990    LIS->print(O, m);
1991 }