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