Correctly deal with identity copies in RegisterCoalescer.
[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/ADT/OwningPtr.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/AliasAnalysis.h"
29 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
30 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
31 #include "llvm/CodeGen/LiveRangeEdit.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/MachineLoopInfo.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/Passes.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/Target/TargetInstrInfo.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include "llvm/Target/TargetRegisterInfo.h"
48 #include <algorithm>
49 #include <cmath>
50 using namespace llvm;
51
52 STATISTIC(numJoins    , "Number of interval joins performed");
53 STATISTIC(numCrossRCs , "Number of cross class joins performed");
54 STATISTIC(numCommutes , "Number of instruction commuting performed");
55 STATISTIC(numExtends  , "Number of copies extended");
56 STATISTIC(NumReMats   , "Number of instructions re-materialized");
57 STATISTIC(NumInflated , "Number of register classes inflated");
58
59 static cl::opt<bool>
60 EnableJoining("join-liveintervals",
61               cl::desc("Coalesce copies (default=true)"),
62               cl::init(true));
63
64 static cl::opt<bool>
65 VerifyCoalescing("verify-coalescing",
66          cl::desc("Verify machine instrs before and after register coalescing"),
67          cl::Hidden);
68
69 namespace {
70   class RegisterCoalescer : public MachineFunctionPass,
71                             private LiveRangeEdit::Delegate {
72     MachineFunction* MF;
73     MachineRegisterInfo* MRI;
74     const TargetMachine* TM;
75     const TargetRegisterInfo* TRI;
76     const TargetInstrInfo* TII;
77     LiveIntervals *LIS;
78     LiveDebugVariables *LDV;
79     const MachineLoopInfo* Loops;
80     AliasAnalysis *AA;
81     RegisterClassInfo RegClassInfo;
82
83     /// WorkList - Copy instructions yet to be coalesced.
84     SmallVector<MachineInstr*, 8> WorkList;
85
86     /// ErasedInstrs - Set of instruction pointers that have been erased, and
87     /// that may be present in WorkList.
88     SmallPtrSet<MachineInstr*, 8> ErasedInstrs;
89
90     /// Dead instructions that are about to be deleted.
91     SmallVector<MachineInstr*, 8> DeadDefs;
92
93     /// Virtual registers to be considered for register class inflation.
94     SmallVector<unsigned, 8> InflateRegs;
95
96     /// Recursively eliminate dead defs in DeadDefs.
97     void eliminateDeadDefs();
98
99     /// LiveRangeEdit callback.
100     void LRE_WillEraseInstruction(MachineInstr *MI);
101
102     /// joinAllIntervals - join compatible live intervals
103     void joinAllIntervals();
104
105     /// copyCoalesceInMBB - Coalesce copies in the specified MBB, putting
106     /// copies that cannot yet be coalesced into WorkList.
107     void copyCoalesceInMBB(MachineBasicBlock *MBB);
108
109     /// copyCoalesceWorkList - Try to coalesce all copies in WorkList after
110     /// position From. Return true if any progress was made.
111     bool copyCoalesceWorkList(unsigned From = 0);
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     /// Attempt joining with a reserved physreg.
127     bool joinReservedPhysReg(CoalescerPair &CP);
128
129     /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
130     /// the source value number is defined by a copy from the destination reg
131     /// see if we can merge these two destination reg valno# into a single
132     /// value number, eliminating a copy.
133     bool adjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
134
135     /// hasOtherReachingDefs - Return true if there are definitions of IntB
136     /// other than BValNo val# that can reach uses of AValno val# of IntA.
137     bool hasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
138                               VNInfo *AValNo, VNInfo *BValNo);
139
140     /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy.
141     /// If the source value number is defined by a commutable instruction and
142     /// its other operand is coalesced to the copy dest register, see if we
143     /// can transform the copy into a noop by commuting the definition.
144     bool removeCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
145
146     /// reMaterializeTrivialDef - If the source of a copy is defined by a
147     /// trivial computation, replace the copy by rematerialize the definition.
148     bool reMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg,
149                                  MachineInstr *CopyMI);
150
151     /// canJoinPhys - Return true if a physreg copy should be joined.
152     bool canJoinPhys(CoalescerPair &CP);
153
154     /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
155     /// update the subregister number if it is not zero. If DstReg is a
156     /// physical register and the existing subregister number of the def / use
157     /// being updated is not zero, make sure to set it to the correct physical
158     /// subregister.
159     void updateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx);
160
161     /// eliminateUndefCopy - Handle copies of undef values.
162     bool eliminateUndefCopy(MachineInstr *CopyMI, const CoalescerPair &CP);
163
164   public:
165     static char ID; // Class identification, replacement for typeinfo
166     RegisterCoalescer() : MachineFunctionPass(ID) {
167       initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
168     }
169
170     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
171
172     virtual void releaseMemory();
173
174     /// runOnMachineFunction - pass entry point
175     virtual bool runOnMachineFunction(MachineFunction&);
176
177     /// print - Implement the dump method.
178     virtual void print(raw_ostream &O, const Module* = 0) const;
179   };
180 } /// end anonymous namespace
181
182 char &llvm::RegisterCoalescerID = RegisterCoalescer::ID;
183
184 INITIALIZE_PASS_BEGIN(RegisterCoalescer, "simple-register-coalescing",
185                       "Simple Register Coalescing", false, false)
186 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
187 INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
188 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
189 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
190 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
191 INITIALIZE_PASS_END(RegisterCoalescer, "simple-register-coalescing",
192                     "Simple Register Coalescing", false, false)
193
194 char RegisterCoalescer::ID = 0;
195
196 static unsigned compose(const TargetRegisterInfo &tri, unsigned a, unsigned b) {
197   if (!a) return b;
198   if (!b) return a;
199   return tri.composeSubRegIndices(a, b);
200 }
201
202 static bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI,
203                         unsigned &Src, unsigned &Dst,
204                         unsigned &SrcSub, unsigned &DstSub) {
205   if (MI->isCopy()) {
206     Dst = MI->getOperand(0).getReg();
207     DstSub = MI->getOperand(0).getSubReg();
208     Src = MI->getOperand(1).getReg();
209     SrcSub = MI->getOperand(1).getSubReg();
210   } else if (MI->isSubregToReg()) {
211     Dst = MI->getOperand(0).getReg();
212     DstSub = compose(tri, MI->getOperand(0).getSubReg(),
213                      MI->getOperand(3).getImm());
214     Src = MI->getOperand(2).getReg();
215     SrcSub = MI->getOperand(2).getSubReg();
216   } else
217     return false;
218   return true;
219 }
220
221 bool CoalescerPair::setRegisters(const MachineInstr *MI) {
222   SrcReg = DstReg = 0;
223   SrcIdx = DstIdx = 0;
224   NewRC = 0;
225   Flipped = CrossClass = false;
226
227   unsigned Src, Dst, SrcSub, DstSub;
228   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
229     return false;
230   Partial = SrcSub || DstSub;
231
232   // If one register is a physreg, it must be Dst.
233   if (TargetRegisterInfo::isPhysicalRegister(Src)) {
234     if (TargetRegisterInfo::isPhysicalRegister(Dst))
235       return false;
236     std::swap(Src, Dst);
237     std::swap(SrcSub, DstSub);
238     Flipped = true;
239   }
240
241   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
242
243   if (TargetRegisterInfo::isPhysicalRegister(Dst)) {
244     // Eliminate DstSub on a physreg.
245     if (DstSub) {
246       Dst = TRI.getSubReg(Dst, DstSub);
247       if (!Dst) return false;
248       DstSub = 0;
249     }
250
251     // Eliminate SrcSub by picking a corresponding Dst superregister.
252     if (SrcSub) {
253       Dst = TRI.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src));
254       if (!Dst) return false;
255       SrcSub = 0;
256     } else if (!MRI.getRegClass(Src)->contains(Dst)) {
257       return false;
258     }
259   } else {
260     // Both registers are virtual.
261     const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
262     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
263
264     // Both registers have subreg indices.
265     if (SrcSub && DstSub) {
266       // Copies between different sub-registers are never coalescable.
267       if (Src == Dst && SrcSub != DstSub)
268         return false;
269
270       NewRC = TRI.getCommonSuperRegClass(SrcRC, SrcSub, DstRC, DstSub,
271                                          SrcIdx, DstIdx);
272       if (!NewRC)
273         return false;
274     } else if (DstSub) {
275       // SrcReg will be merged with a sub-register of DstReg.
276       SrcIdx = DstSub;
277       NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
278     } else if (SrcSub) {
279       // DstReg will be merged with a sub-register of SrcReg.
280       DstIdx = SrcSub;
281       NewRC = TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSub);
282     } else {
283       // This is a straight copy without sub-registers.
284       NewRC = TRI.getCommonSubClass(DstRC, SrcRC);
285     }
286
287     // The combined constraint may be impossible to satisfy.
288     if (!NewRC)
289       return false;
290
291     // Prefer SrcReg to be a sub-register of DstReg.
292     // FIXME: Coalescer should support subregs symmetrically.
293     if (DstIdx && !SrcIdx) {
294       std::swap(Src, Dst);
295       std::swap(SrcIdx, DstIdx);
296       Flipped = !Flipped;
297     }
298
299     CrossClass = NewRC != DstRC || NewRC != SrcRC;
300   }
301   // Check our invariants
302   assert(TargetRegisterInfo::isVirtualRegister(Src) && "Src must be virtual");
303   assert(!(TargetRegisterInfo::isPhysicalRegister(Dst) && DstSub) &&
304          "Cannot have a physical SubIdx");
305   SrcReg = Src;
306   DstReg = Dst;
307   return true;
308 }
309
310 bool CoalescerPair::flip() {
311   if (TargetRegisterInfo::isPhysicalRegister(DstReg))
312     return false;
313   std::swap(SrcReg, DstReg);
314   std::swap(SrcIdx, DstIdx);
315   Flipped = !Flipped;
316   return true;
317 }
318
319 bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
320   if (!MI)
321     return false;
322   unsigned Src, Dst, SrcSub, DstSub;
323   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
324     return false;
325
326   // Find the virtual register that is SrcReg.
327   if (Dst == SrcReg) {
328     std::swap(Src, Dst);
329     std::swap(SrcSub, DstSub);
330   } else if (Src != SrcReg) {
331     return false;
332   }
333
334   // Now check that Dst matches DstReg.
335   if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
336     if (!TargetRegisterInfo::isPhysicalRegister(Dst))
337       return false;
338     assert(!DstIdx && !SrcIdx && "Inconsistent CoalescerPair state.");
339     // DstSub could be set for a physreg from INSERT_SUBREG.
340     if (DstSub)
341       Dst = TRI.getSubReg(Dst, DstSub);
342     // Full copy of Src.
343     if (!SrcSub)
344       return DstReg == Dst;
345     // This is a partial register copy. Check that the parts match.
346     return TRI.getSubReg(DstReg, SrcSub) == Dst;
347   } else {
348     // DstReg is virtual.
349     if (DstReg != Dst)
350       return false;
351     // Registers match, do the subregisters line up?
352     return compose(TRI, SrcIdx, SrcSub) == compose(TRI, DstIdx, DstSub);
353   }
354 }
355
356 void RegisterCoalescer::getAnalysisUsage(AnalysisUsage &AU) const {
357   AU.setPreservesCFG();
358   AU.addRequired<AliasAnalysis>();
359   AU.addRequired<LiveIntervals>();
360   AU.addPreserved<LiveIntervals>();
361   AU.addRequired<LiveDebugVariables>();
362   AU.addPreserved<LiveDebugVariables>();
363   AU.addPreserved<SlotIndexes>();
364   AU.addRequired<MachineLoopInfo>();
365   AU.addPreserved<MachineLoopInfo>();
366   AU.addPreservedID(MachineDominatorsID);
367   MachineFunctionPass::getAnalysisUsage(AU);
368 }
369
370 void RegisterCoalescer::eliminateDeadDefs() {
371   SmallVector<LiveInterval*, 8> NewRegs;
372   LiveRangeEdit(0, NewRegs, *MF, *LIS, 0, this).eliminateDeadDefs(DeadDefs);
373 }
374
375 // Callback from eliminateDeadDefs().
376 void RegisterCoalescer::LRE_WillEraseInstruction(MachineInstr *MI) {
377   // MI may be in WorkList. Make sure we don't visit it.
378   ErasedInstrs.insert(MI);
379 }
380
381 /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
382 /// being the source and IntB being the dest, thus this defines a value number
383 /// in IntB.  If the source value number (in IntA) is defined by a copy from B,
384 /// see if we can merge these two pieces of B into a single value number,
385 /// eliminating a copy.  For example:
386 ///
387 ///  A3 = B0
388 ///    ...
389 ///  B1 = A3      <- this copy
390 ///
391 /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
392 /// value number to be replaced with B0 (which simplifies the B liveinterval).
393 ///
394 /// This returns true if an interval was modified.
395 ///
396 bool RegisterCoalescer::adjustCopiesBackFrom(const CoalescerPair &CP,
397                                              MachineInstr *CopyMI) {
398   assert(!CP.isPartial() && "This doesn't work for partial copies.");
399
400   // Bail if there is no dst interval - can happen when merging physical subreg
401   // operations.
402   if (!LIS->hasInterval(CP.getDstReg()))
403     return false;
404
405   LiveInterval &IntA =
406     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
407   LiveInterval &IntB =
408     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
409   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
410
411   // BValNo is a value number in B that is defined by a copy from A.  'B3' in
412   // the example above.
413   LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
414   if (BLR == IntB.end()) return false;
415   VNInfo *BValNo = BLR->valno;
416
417   // Get the location that B is defined at.  Two options: either this value has
418   // an unknown definition point or it is defined at CopyIdx.  If unknown, we
419   // can't process it.
420   if (BValNo->def != CopyIdx) return false;
421
422   // AValNo is the value number in A that defines the copy, A3 in the example.
423   SlotIndex CopyUseIdx = CopyIdx.getRegSlot(true);
424   LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyUseIdx);
425   // The live range might not exist after fun with physreg coalescing.
426   if (ALR == IntA.end()) return false;
427   VNInfo *AValNo = ALR->valno;
428
429   // If AValNo is defined as a copy from IntB, we can potentially process this.
430   // Get the instruction that defines this value number.
431   MachineInstr *ACopyMI = LIS->getInstructionFromIndex(AValNo->def);
432   if (!CP.isCoalescable(ACopyMI))
433     return false;
434
435   // Get the LiveRange in IntB that this value number starts with.
436   LiveInterval::iterator ValLR =
437     IntB.FindLiveRangeContaining(AValNo->def.getPrevSlot());
438   if (ValLR == IntB.end())
439     return false;
440
441   // Make sure that the end of the live range is inside the same block as
442   // CopyMI.
443   MachineInstr *ValLREndInst =
444     LIS->getInstructionFromIndex(ValLR->end.getPrevSlot());
445   if (!ValLREndInst || ValLREndInst->getParent() != CopyMI->getParent())
446     return false;
447
448   // Okay, we now know that ValLR ends in the same block that the CopyMI
449   // live-range starts.  If there are no intervening live ranges between them in
450   // IntB, we can merge them.
451   if (ValLR+1 != BLR) return false;
452
453   // If a live interval is a physical register, conservatively check if any
454   // of its aliases is overlapping the live interval of the virtual register.
455   // If so, do not coalesce.
456   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
457     for (const uint16_t *AS = TRI->getAliasSet(IntB.reg); *AS; ++AS)
458       if (LIS->hasInterval(*AS) && IntA.overlaps(LIS->getInterval(*AS))) {
459         DEBUG({
460             dbgs() << "\t\tInterfere with alias ";
461             LIS->getInterval(*AS).print(dbgs(), TRI);
462           });
463         return false;
464       }
465   }
466
467   DEBUG({
468       dbgs() << "Extending: ";
469       IntB.print(dbgs(), TRI);
470     });
471
472   SlotIndex FillerStart = ValLR->end, FillerEnd = BLR->start;
473   // We are about to delete CopyMI, so need to remove it as the 'instruction
474   // that defines this value #'. Update the valnum with the new defining
475   // instruction #.
476   BValNo->def = FillerStart;
477
478   // Okay, we can merge them.  We need to insert a new liverange:
479   // [ValLR.end, BLR.begin) of either value number, then we merge the
480   // two value numbers.
481   IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
482
483   // If the IntB live range is assigned to a physical register, and if that
484   // physreg has sub-registers, update their live intervals as well.
485   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
486     for (const uint16_t *SR = TRI->getSubRegisters(IntB.reg); *SR; ++SR) {
487       if (!LIS->hasInterval(*SR))
488         continue;
489       LiveInterval &SRLI = LIS->getInterval(*SR);
490       SRLI.addRange(LiveRange(FillerStart, FillerEnd,
491                               SRLI.getNextValue(FillerStart,
492                                                 LIS->getVNInfoAllocator())));
493     }
494   }
495
496   // Okay, merge "B1" into the same value number as "B0".
497   if (BValNo != ValLR->valno) {
498     // If B1 is killed by a PHI, then the merged live range must also be killed
499     // by the same PHI, as B0 and B1 can not overlap.
500     bool HasPHIKill = BValNo->hasPHIKill();
501     IntB.MergeValueNumberInto(BValNo, ValLR->valno);
502     if (HasPHIKill)
503       ValLR->valno->setHasPHIKill(true);
504   }
505   DEBUG({
506       dbgs() << "   result = ";
507       IntB.print(dbgs(), TRI);
508       dbgs() << "\n";
509     });
510
511   // If the source instruction was killing the source register before the
512   // merge, unset the isKill marker given the live range has been extended.
513   int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
514   if (UIdx != -1) {
515     ValLREndInst->getOperand(UIdx).setIsKill(false);
516   }
517
518   // Rewrite the copy. If the copy instruction was killing the destination
519   // register before the merge, find the last use and trim the live range. That
520   // will also add the isKill marker.
521   CopyMI->substituteRegister(IntA.reg, IntB.reg, 0, *TRI);
522   if (ALR->end == CopyIdx)
523     LIS->shrinkToUses(&IntA);
524
525   ++numExtends;
526   return true;
527 }
528
529 /// hasOtherReachingDefs - Return true if there are definitions of IntB
530 /// other than BValNo val# that can reach uses of AValno val# of IntA.
531 bool RegisterCoalescer::hasOtherReachingDefs(LiveInterval &IntA,
532                                              LiveInterval &IntB,
533                                              VNInfo *AValNo,
534                                              VNInfo *BValNo) {
535   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
536        AI != AE; ++AI) {
537     if (AI->valno != AValNo) continue;
538     LiveInterval::Ranges::iterator BI =
539       std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start);
540     if (BI != IntB.ranges.begin())
541       --BI;
542     for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
543       if (BI->valno == BValNo)
544         continue;
545       if (BI->start <= AI->start && BI->end > AI->start)
546         return true;
547       if (BI->start > AI->start && BI->start < AI->end)
548         return true;
549     }
550   }
551   return false;
552 }
553
554 /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy with
555 /// IntA being the source and IntB being the dest, thus this defines a value
556 /// number in IntB.  If the source value number (in IntA) is defined by a
557 /// commutable instruction and its other operand is coalesced to the copy dest
558 /// register, see if we can transform the copy into a noop by commuting the
559 /// definition. For example,
560 ///
561 ///  A3 = op A2 B0<kill>
562 ///    ...
563 ///  B1 = A3      <- this copy
564 ///    ...
565 ///     = op A3   <- more uses
566 ///
567 /// ==>
568 ///
569 ///  B2 = op B0 A2<kill>
570 ///    ...
571 ///  B1 = B2      <- now an identify copy
572 ///    ...
573 ///     = op B2   <- more uses
574 ///
575 /// This returns true if an interval was modified.
576 ///
577 bool RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP,
578                                                  MachineInstr *CopyMI) {
579   // FIXME: For now, only eliminate the copy by commuting its def when the
580   // source register is a virtual register. We want to guard against cases
581   // where the copy is a back edge copy and commuting the def lengthen the
582   // live interval of the source register to the entire loop.
583   if (CP.isPhys() && CP.isFlipped())
584     return false;
585
586   // Bail if there is no dst interval.
587   if (!LIS->hasInterval(CP.getDstReg()))
588     return false;
589
590   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
591
592   LiveInterval &IntA =
593     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
594   LiveInterval &IntB =
595     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
596
597   // BValNo is a value number in B that is defined by a copy from A. 'B3' in
598   // the example above.
599   VNInfo *BValNo = IntB.getVNInfoAt(CopyIdx);
600   if (!BValNo || BValNo->def != CopyIdx)
601     return false;
602
603   assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
604
605   // AValNo is the value number in A that defines the copy, A3 in the example.
606   VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx.getRegSlot(true));
607   assert(AValNo && "COPY source not live");
608
609   // If other defs can reach uses of this def, then it's not safe to perform
610   // the optimization.
611   if (AValNo->isPHIDef() || AValNo->isUnused() || AValNo->hasPHIKill())
612     return false;
613   MachineInstr *DefMI = LIS->getInstructionFromIndex(AValNo->def);
614   if (!DefMI)
615     return false;
616   if (!DefMI->isCommutable())
617     return false;
618   // If DefMI is a two-address instruction then commuting it will change the
619   // destination register.
620   int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg);
621   assert(DefIdx != -1);
622   unsigned UseOpIdx;
623   if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
624     return false;
625   unsigned Op1, Op2, NewDstIdx;
626   if (!TII->findCommutedOpIndices(DefMI, Op1, Op2))
627     return false;
628   if (Op1 == UseOpIdx)
629     NewDstIdx = Op2;
630   else if (Op2 == UseOpIdx)
631     NewDstIdx = Op1;
632   else
633     return false;
634
635   MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
636   unsigned NewReg = NewDstMO.getReg();
637   if (NewReg != IntB.reg || !NewDstMO.isKill())
638     return false;
639
640   // Make sure there are no other definitions of IntB that would reach the
641   // uses which the new definition can reach.
642   if (hasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
643     return false;
644
645   // Abort if the aliases of IntB.reg have values that are not simply the
646   // clobbers from the superreg.
647   if (TargetRegisterInfo::isPhysicalRegister(IntB.reg))
648     for (const uint16_t *AS = TRI->getAliasSet(IntB.reg); *AS; ++AS)
649       if (LIS->hasInterval(*AS) &&
650           hasOtherReachingDefs(IntA, LIS->getInterval(*AS), AValNo, 0))
651         return false;
652
653   // If some of the uses of IntA.reg is already coalesced away, return false.
654   // It's not possible to determine whether it's safe to perform the coalescing.
655   for (MachineRegisterInfo::use_nodbg_iterator UI =
656          MRI->use_nodbg_begin(IntA.reg),
657        UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
658     MachineInstr *UseMI = &*UI;
659     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI);
660     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
661     if (ULR == IntA.end() || ULR->valno != AValNo)
662       continue;
663     // If this use is tied to a def, we can't rewrite the register.
664     if (UseMI->isRegTiedToDefOperand(UI.getOperandNo()))
665       return false;
666   }
667
668   DEBUG(dbgs() << "\tremoveCopyByCommutingDef: " << AValNo->def << '\t'
669                << *DefMI);
670
671   // At this point we have decided that it is legal to do this
672   // transformation.  Start by commuting the instruction.
673   MachineBasicBlock *MBB = DefMI->getParent();
674   MachineInstr *NewMI = TII->commuteInstruction(DefMI);
675   if (!NewMI)
676     return false;
677   if (TargetRegisterInfo::isVirtualRegister(IntA.reg) &&
678       TargetRegisterInfo::isVirtualRegister(IntB.reg) &&
679       !MRI->constrainRegClass(IntB.reg, MRI->getRegClass(IntA.reg)))
680     return false;
681   if (NewMI != DefMI) {
682     LIS->ReplaceMachineInstrInMaps(DefMI, NewMI);
683     MachineBasicBlock::iterator Pos = DefMI;
684     MBB->insert(Pos, NewMI);
685     MBB->erase(DefMI);
686   }
687   unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
688   NewMI->getOperand(OpIdx).setIsKill();
689
690   // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
691   // A = or A, B
692   // ...
693   // B = A
694   // ...
695   // C = A<kill>
696   // ...
697   //   = B
698
699   // Update uses of IntA of the specific Val# with IntB.
700   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(IntA.reg),
701          UE = MRI->use_end(); UI != UE;) {
702     MachineOperand &UseMO = UI.getOperand();
703     MachineInstr *UseMI = &*UI;
704     ++UI;
705     if (UseMI->isDebugValue()) {
706       // FIXME These don't have an instruction index.  Not clear we have enough
707       // info to decide whether to do this replacement or not.  For now do it.
708       UseMO.setReg(NewReg);
709       continue;
710     }
711     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI).getRegSlot(true);
712     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
713     if (ULR == IntA.end() || ULR->valno != AValNo)
714       continue;
715     if (TargetRegisterInfo::isPhysicalRegister(NewReg))
716       UseMO.substPhysReg(NewReg, *TRI);
717     else
718       UseMO.setReg(NewReg);
719     if (UseMI == CopyMI)
720       continue;
721     if (!UseMI->isCopy())
722       continue;
723     if (UseMI->getOperand(0).getReg() != IntB.reg ||
724         UseMI->getOperand(0).getSubReg())
725       continue;
726
727     // This copy will become a noop. If it's defining a new val#, merge it into
728     // BValNo.
729     SlotIndex DefIdx = UseIdx.getRegSlot();
730     VNInfo *DVNI = IntB.getVNInfoAt(DefIdx);
731     if (!DVNI)
732       continue;
733     DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI);
734     assert(DVNI->def == DefIdx);
735     BValNo = IntB.MergeValueNumberInto(BValNo, DVNI);
736     ErasedInstrs.insert(UseMI);
737     LIS->RemoveMachineInstrFromMaps(UseMI);
738     UseMI->eraseFromParent();
739   }
740
741   // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
742   // is updated.
743   VNInfo *ValNo = BValNo;
744   ValNo->def = AValNo->def;
745   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
746        AI != AE; ++AI) {
747     if (AI->valno != AValNo) continue;
748     IntB.addRange(LiveRange(AI->start, AI->end, ValNo));
749   }
750   DEBUG(dbgs() << "\t\textended: " << IntB << '\n');
751
752   IntA.removeValNo(AValNo);
753   DEBUG(dbgs() << "\t\ttrimmed:  " << IntA << '\n');
754   ++numCommutes;
755   return true;
756 }
757
758 /// reMaterializeTrivialDef - If the source of a copy is defined by a trivial
759 /// computation, replace the copy by rematerialize the definition.
760 bool RegisterCoalescer::reMaterializeTrivialDef(LiveInterval &SrcInt,
761                                                 unsigned DstReg,
762                                                 MachineInstr *CopyMI) {
763   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(true);
764   LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx);
765   assert(SrcLR != SrcInt.end() && "Live range not found!");
766   VNInfo *ValNo = SrcLR->valno;
767   if (ValNo->isPHIDef() || ValNo->isUnused())
768     return false;
769   MachineInstr *DefMI = LIS->getInstructionFromIndex(ValNo->def);
770   if (!DefMI)
771     return false;
772   assert(DefMI && "Defining instruction disappeared");
773   if (!DefMI->isAsCheapAsAMove())
774     return false;
775   if (!TII->isTriviallyReMaterializable(DefMI, AA))
776     return false;
777   bool SawStore = false;
778   if (!DefMI->isSafeToMove(TII, AA, SawStore))
779     return false;
780   const MCInstrDesc &MCID = DefMI->getDesc();
781   if (MCID.getNumDefs() != 1)
782     return false;
783   if (!DefMI->isImplicitDef()) {
784     // Make sure the copy destination register class fits the instruction
785     // definition register class. The mismatch can happen as a result of earlier
786     // extract_subreg, insert_subreg, subreg_to_reg coalescing.
787     const TargetRegisterClass *RC = TII->getRegClass(MCID, 0, TRI, *MF);
788     if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
789       if (MRI->getRegClass(DstReg) != RC)
790         return false;
791     } else if (!RC->contains(DstReg))
792       return false;
793   }
794
795   MachineBasicBlock *MBB = CopyMI->getParent();
796   MachineBasicBlock::iterator MII =
797     llvm::next(MachineBasicBlock::iterator(CopyMI));
798   TII->reMaterialize(*MBB, MII, DstReg, 0, DefMI, *TRI);
799   MachineInstr *NewMI = prior(MII);
800
801   // NewMI may have dead implicit defs (E.g. EFLAGS for MOV<bits>r0 on X86).
802   // We need to remember these so we can add intervals once we insert
803   // NewMI into SlotIndexes.
804   SmallVector<unsigned, 4> NewMIImplDefs;
805   for (unsigned i = NewMI->getDesc().getNumOperands(),
806          e = NewMI->getNumOperands(); i != e; ++i) {
807     MachineOperand &MO = NewMI->getOperand(i);
808     if (MO.isReg()) {
809       assert(MO.isDef() && MO.isImplicit() && MO.isDead() &&
810              TargetRegisterInfo::isPhysicalRegister(MO.getReg()));
811       NewMIImplDefs.push_back(MO.getReg());
812     }
813   }
814
815   // CopyMI may have implicit operands, transfer them over to the newly
816   // rematerialized instruction. And update implicit def interval valnos.
817   for (unsigned i = CopyMI->getDesc().getNumOperands(),
818          e = CopyMI->getNumOperands(); i != e; ++i) {
819     MachineOperand &MO = CopyMI->getOperand(i);
820     if (MO.isReg()) {
821       assert(MO.isImplicit() && "No explicit operands after implict operands.");
822       // Discard VReg implicit defs.
823       if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
824         NewMI->addOperand(MO);
825       }
826     }
827   }
828
829   LIS->ReplaceMachineInstrInMaps(CopyMI, NewMI);
830
831   SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
832   for (unsigned i = 0, e = NewMIImplDefs.size(); i != e; ++i) {
833     unsigned reg = NewMIImplDefs[i];
834     LiveInterval &li = LIS->getInterval(reg);
835     VNInfo *DeadDefVN = li.getNextValue(NewMIIdx.getRegSlot(),
836                                         LIS->getVNInfoAllocator());
837     LiveRange lr(NewMIIdx.getRegSlot(), NewMIIdx.getDeadSlot(), DeadDefVN);
838     li.addRange(lr);
839   }
840
841   CopyMI->eraseFromParent();
842   ErasedInstrs.insert(CopyMI);
843   DEBUG(dbgs() << "Remat: " << *NewMI);
844   ++NumReMats;
845
846   // The source interval can become smaller because we removed a use.
847   LIS->shrinkToUses(&SrcInt, &DeadDefs);
848   if (!DeadDefs.empty())
849     eliminateDeadDefs();
850
851   return true;
852 }
853
854 /// eliminateUndefCopy - ProcessImpicitDefs may leave some copies of <undef>
855 /// values, it only removes local variables. When we have a copy like:
856 ///
857 ///   %vreg1 = COPY %vreg2<undef>
858 ///
859 /// We delete the copy and remove the corresponding value number from %vreg1.
860 /// Any uses of that value number are marked as <undef>.
861 bool RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI,
862                                            const CoalescerPair &CP) {
863   SlotIndex Idx = LIS->getInstructionIndex(CopyMI);
864   LiveInterval *SrcInt = &LIS->getInterval(CP.getSrcReg());
865   if (SrcInt->liveAt(Idx))
866     return false;
867   LiveInterval *DstInt = &LIS->getInterval(CP.getDstReg());
868   if (DstInt->liveAt(Idx))
869     return false;
870
871   // No intervals are live-in to CopyMI - it is undef.
872   if (CP.isFlipped())
873     DstInt = SrcInt;
874   SrcInt = 0;
875
876   VNInfo *DeadVNI = DstInt->getVNInfoAt(Idx.getRegSlot());
877   assert(DeadVNI && "No value defined in DstInt");
878   DstInt->removeValNo(DeadVNI);
879
880   // Find new undef uses.
881   for (MachineRegisterInfo::reg_nodbg_iterator
882          I = MRI->reg_nodbg_begin(DstInt->reg), E = MRI->reg_nodbg_end();
883        I != E; ++I) {
884     MachineOperand &MO = I.getOperand();
885     if (MO.isDef() || MO.isUndef())
886       continue;
887     MachineInstr *MI = MO.getParent();
888     SlotIndex Idx = LIS->getInstructionIndex(MI);
889     if (DstInt->liveAt(Idx))
890       continue;
891     MO.setIsUndef(true);
892     DEBUG(dbgs() << "\tnew undef: " << Idx << '\t' << *MI);
893   }
894   return true;
895 }
896
897 /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
898 /// update the subregister number if it is not zero. If DstReg is a
899 /// physical register and the existing subregister number of the def / use
900 /// being updated is not zero, make sure to set it to the correct physical
901 /// subregister.
902 void RegisterCoalescer::updateRegDefsUses(unsigned SrcReg,
903                                           unsigned DstReg,
904                                           unsigned SubIdx) {
905   bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
906   LiveInterval &DstInt = LIS->getInterval(DstReg);
907
908   // Update LiveDebugVariables.
909   LDV->renameRegister(SrcReg, DstReg, SubIdx);
910
911   for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(SrcReg);
912        MachineInstr *UseMI = I.skipInstruction();) {
913     SmallVector<unsigned,8> Ops;
914     bool Reads, Writes;
915     tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops);
916
917     // If SrcReg wasn't read, it may still be the case that DstReg is live-in
918     // because SrcReg is a sub-register.
919     if (!Reads && SubIdx)
920       Reads = DstInt.liveAt(LIS->getInstructionIndex(UseMI));
921
922     // Replace SrcReg with DstReg in all UseMI operands.
923     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
924       MachineOperand &MO = UseMI->getOperand(Ops[i]);
925
926       // Adjust <undef> flags in case of sub-register joins. We don't want to
927       // turn a full def into a read-modify-write sub-register def and vice
928       // versa.
929       if (SubIdx && MO.isDef())
930         MO.setIsUndef(!Reads);
931
932       if (DstIsPhys)
933         MO.substPhysReg(DstReg, *TRI);
934       else
935         MO.substVirtReg(DstReg, SubIdx, *TRI);
936     }
937
938     DEBUG({
939         dbgs() << "\t\tupdated: ";
940         if (!UseMI->isDebugValue())
941           dbgs() << LIS->getInstructionIndex(UseMI) << "\t";
942         dbgs() << *UseMI;
943       });
944   }
945 }
946
947 /// canJoinPhys - Return true if a copy involving a physreg should be joined.
948 bool RegisterCoalescer::canJoinPhys(CoalescerPair &CP) {
949   /// Always join simple intervals that are defined by a single copy from a
950   /// reserved register. This doesn't increase register pressure, so it is
951   /// always beneficial.
952   if (!RegClassInfo.isReserved(CP.getDstReg())) {
953     DEBUG(dbgs() << "\tCan only merge into reserved registers.\n");
954     return false;
955   }
956
957   LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg());
958   if (CP.isFlipped() && JoinVInt.containsOneValue())
959     return true;
960
961   DEBUG(dbgs() << "\tCannot join defs into reserved register.\n");
962   return false;
963 }
964
965 /// joinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
966 /// which are the src/dst of the copy instruction CopyMI.  This returns true
967 /// if the copy was successfully coalesced away. If it is not currently
968 /// possible to coalesce this interval, but it may be possible if other
969 /// things get coalesced, then it returns true by reference in 'Again'.
970 bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
971
972   Again = false;
973   DEBUG(dbgs() << LIS->getInstructionIndex(CopyMI) << '\t' << *CopyMI);
974
975   CoalescerPair CP(*TII, *TRI);
976   if (!CP.setRegisters(CopyMI)) {
977     DEBUG(dbgs() << "\tNot coalescable.\n");
978     return false;
979   }
980
981   // Dead code elimination. This really should be handled by MachineDCE, but
982   // sometimes dead copies slip through, and we can't generate invalid live
983   // ranges.
984   if (!CP.isPhys() && CopyMI->allDefsAreDead()) {
985     DEBUG(dbgs() << "\tCopy is dead.\n");
986     DeadDefs.push_back(CopyMI);
987     eliminateDeadDefs();
988     return true;
989   }
990
991   // Eliminate undefs.
992   if (!CP.isPhys() && eliminateUndefCopy(CopyMI, CP)) {
993     DEBUG(dbgs() << "\tEliminated copy of <undef> value.\n");
994     LIS->RemoveMachineInstrFromMaps(CopyMI);
995     CopyMI->eraseFromParent();
996     return false;  // Not coalescable.
997   }
998
999   // Coalesced copies are normally removed immediately, but transformations
1000   // like removeCopyByCommutingDef() can inadvertently create identity copies.
1001   // When that happens, just join the values and remove the copy.
1002   if (CP.getSrcReg() == CP.getDstReg()) {
1003     LiveInterval &LI = LIS->getInterval(CP.getSrcReg());
1004     DEBUG(dbgs() << "\tCopy already coalesced: " << LI << '\n');
1005     LiveRangeQuery LRQ(LI, LIS->getInstructionIndex(CopyMI));
1006     if (VNInfo *DefVNI = LRQ.valueDefined()) {
1007       VNInfo *ReadVNI = LRQ.valueIn();
1008       assert(ReadVNI && "No value before copy and no <undef> flag.");
1009       assert(ReadVNI != DefVNI && "Cannot read and define the same value.");
1010       LI.MergeValueNumberInto(DefVNI, ReadVNI);
1011       DEBUG(dbgs() << "\tMerged values:          " << LI << '\n');
1012     }
1013     LIS->RemoveMachineInstrFromMaps(CopyMI);
1014     CopyMI->eraseFromParent();
1015     return true;
1016   }
1017
1018   // Enforce policies.
1019   if (CP.isPhys()) {
1020     DEBUG(dbgs() << "\tConsidering merging " << PrintReg(CP.getSrcReg(), TRI)
1021                  << " with " << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx())
1022                  << '\n');
1023     if (!canJoinPhys(CP)) {
1024       // Before giving up coalescing, if definition of source is defined by
1025       // trivial computation, try rematerializing it.
1026       if (!CP.isFlipped() &&
1027           reMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()),
1028                                   CP.getDstReg(), CopyMI))
1029         return true;
1030       return false;
1031     }
1032   } else {
1033     DEBUG({
1034       dbgs() << "\tConsidering merging to " << CP.getNewRC()->getName()
1035              << " with ";
1036       if (CP.getDstIdx() && CP.getSrcIdx())
1037         dbgs() << PrintReg(CP.getDstReg()) << " in "
1038                << TRI->getSubRegIndexName(CP.getDstIdx()) << " and "
1039                << PrintReg(CP.getSrcReg()) << " in "
1040                << TRI->getSubRegIndexName(CP.getSrcIdx()) << '\n';
1041       else
1042         dbgs() << PrintReg(CP.getSrcReg(), TRI) << " in "
1043                << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n';
1044     });
1045
1046     // When possible, let DstReg be the larger interval.
1047     if (!CP.isPartial() && LIS->getInterval(CP.getSrcReg()).ranges.size() >
1048                            LIS->getInterval(CP.getDstReg()).ranges.size())
1049       CP.flip();
1050   }
1051
1052   // Okay, attempt to join these two intervals.  On failure, this returns false.
1053   // Otherwise, if one of the intervals being joined is a physreg, this method
1054   // always canonicalizes DstInt to be it.  The output "SrcInt" will not have
1055   // been modified, so we can use this information below to update aliases.
1056   if (!joinIntervals(CP)) {
1057     // Coalescing failed.
1058
1059     // If definition of source is defined by trivial computation, try
1060     // rematerializing it.
1061     if (!CP.isFlipped() &&
1062         reMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()),
1063                                 CP.getDstReg(), CopyMI))
1064       return true;
1065
1066     // If we can eliminate the copy without merging the live ranges, do so now.
1067     if (!CP.isPartial()) {
1068       if (adjustCopiesBackFrom(CP, CopyMI) ||
1069           removeCopyByCommutingDef(CP, CopyMI)) {
1070         LIS->RemoveMachineInstrFromMaps(CopyMI);
1071         CopyMI->eraseFromParent();
1072         DEBUG(dbgs() << "\tTrivial!\n");
1073         return true;
1074       }
1075     }
1076
1077     // Otherwise, we are unable to join the intervals.
1078     DEBUG(dbgs() << "\tInterference!\n");
1079     Again = true;  // May be possible to coalesce later.
1080     return false;
1081   }
1082
1083   // Coalescing to a virtual register that is of a sub-register class of the
1084   // other. Make sure the resulting register is set to the right register class.
1085   if (CP.isCrossClass()) {
1086     ++numCrossRCs;
1087     MRI->setRegClass(CP.getDstReg(), CP.getNewRC());
1088   }
1089
1090   // Removing sub-register copies can ease the register class constraints.
1091   // Make sure we attempt to inflate the register class of DstReg.
1092   if (!CP.isPhys() && RegClassInfo.isProperSubClass(CP.getNewRC()))
1093     InflateRegs.push_back(CP.getDstReg());
1094
1095   // CopyMI has been erased by joinIntervals at this point. Remove it from
1096   // ErasedInstrs since copyCoalesceWorkList() won't add a successful join back
1097   // to the work list. This keeps ErasedInstrs from growing needlessly.
1098   ErasedInstrs.erase(CopyMI);
1099
1100   // Rewrite all SrcReg operands to DstReg.
1101   // Also update DstReg operands to include DstIdx if it is set.
1102   if (CP.getDstIdx())
1103     updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx());
1104   updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx());
1105
1106   // SrcReg is guaranteed to be the register whose live interval that is
1107   // being merged.
1108   LIS->removeInterval(CP.getSrcReg());
1109
1110   // Update regalloc hint.
1111   TRI->UpdateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF);
1112
1113   DEBUG({
1114     LiveInterval &DstInt = LIS->getInterval(CP.getDstReg());
1115     dbgs() << "\tJoined. Result = ";
1116     DstInt.print(dbgs(), TRI);
1117     dbgs() << "\n";
1118   });
1119
1120   ++numJoins;
1121   return true;
1122 }
1123
1124 /// Attempt joining with a reserved physreg.
1125 bool RegisterCoalescer::joinReservedPhysReg(CoalescerPair &CP) {
1126   assert(CP.isPhys() && "Must be a physreg copy");
1127   assert(RegClassInfo.isReserved(CP.getDstReg()) && "Not a reserved register");
1128   LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1129   DEBUG({ dbgs() << "\t\tRHS = "; RHS.print(dbgs(), TRI); dbgs() << "\n"; });
1130
1131   assert(CP.isFlipped() && RHS.containsOneValue() &&
1132          "Invalid join with reserved register");
1133
1134   // Optimization for reserved registers like ESP. We can only merge with a
1135   // reserved physreg if RHS has a single value that is a copy of CP.DstReg().
1136   // The live range of the reserved register will look like a set of dead defs
1137   // - we don't properly track the live range of reserved registers.
1138
1139   // Deny any overlapping intervals.  This depends on all the reserved
1140   // register live ranges to look like dead defs.
1141   for (const uint16_t *AS = TRI->getOverlaps(CP.getDstReg()); *AS; ++AS) {
1142     if (!LIS->hasInterval(*AS)) {
1143       // Make sure at least DstReg itself exists before attempting a join.
1144       if (*AS == CP.getDstReg())
1145         LIS->getOrCreateInterval(CP.getDstReg());
1146       continue;
1147     }
1148     if (RHS.overlaps(LIS->getInterval(*AS))) {
1149       DEBUG(dbgs() << "\t\tInterference: " << PrintReg(*AS, TRI) << '\n');
1150       return false;
1151     }
1152   }
1153   // Skip any value computations, we are not adding new values to the
1154   // reserved register.  Also skip merging the live ranges, the reserved
1155   // register live range doesn't need to be accurate as long as all the
1156   // defs are there.
1157
1158   // We don't track kills for reserved registers.
1159   MRI->clearKillFlags(CP.getSrcReg());
1160
1161   return true;
1162 }
1163
1164 /// ComputeUltimateVN - Assuming we are going to join two live intervals,
1165 /// compute what the resultant value numbers for each value in the input two
1166 /// ranges will be.  This is complicated by copies between the two which can
1167 /// and will commonly cause multiple value numbers to be merged into one.
1168 ///
1169 /// VN is the value number that we're trying to resolve.  InstDefiningValue
1170 /// keeps track of the new InstDefiningValue assignment for the result
1171 /// LiveInterval.  ThisFromOther/OtherFromThis are sets that keep track of
1172 /// whether a value in this or other is a copy from the opposite set.
1173 /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
1174 /// already been assigned.
1175 ///
1176 /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1177 /// contains the value number the copy is from.
1178 ///
1179 static unsigned ComputeUltimateVN(VNInfo *VNI,
1180                                   SmallVector<VNInfo*, 16> &NewVNInfo,
1181                                   DenseMap<VNInfo*, VNInfo*> &ThisFromOther,
1182                                   DenseMap<VNInfo*, VNInfo*> &OtherFromThis,
1183                                   SmallVector<int, 16> &ThisValNoAssignments,
1184                                   SmallVector<int, 16> &OtherValNoAssignments) {
1185   unsigned VN = VNI->id;
1186
1187   // If the VN has already been computed, just return it.
1188   if (ThisValNoAssignments[VN] >= 0)
1189     return ThisValNoAssignments[VN];
1190   assert(ThisValNoAssignments[VN] != -2 && "Cyclic value numbers");
1191
1192   // If this val is not a copy from the other val, then it must be a new value
1193   // number in the destination.
1194   DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI);
1195   if (I == ThisFromOther.end()) {
1196     NewVNInfo.push_back(VNI);
1197     return ThisValNoAssignments[VN] = NewVNInfo.size()-1;
1198   }
1199   VNInfo *OtherValNo = I->second;
1200
1201   // Otherwise, this *is* a copy from the RHS.  If the other side has already
1202   // been computed, return it.
1203   if (OtherValNoAssignments[OtherValNo->id] >= 0)
1204     return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id];
1205
1206   // Mark this value number as currently being computed, then ask what the
1207   // ultimate value # of the other value is.
1208   ThisValNoAssignments[VN] = -2;
1209   unsigned UltimateVN =
1210     ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther,
1211                       OtherValNoAssignments, ThisValNoAssignments);
1212   return ThisValNoAssignments[VN] = UltimateVN;
1213 }
1214
1215
1216 // Find out if we have something like
1217 // A = X
1218 // B = X
1219 // if so, we can pretend this is actually
1220 // A = X
1221 // B = A
1222 // which allows us to coalesce A and B.
1223 // VNI is the definition of B. LR is the life range of A that includes
1224 // the slot just before B. If we return true, we add "B = X" to DupCopies.
1225 // This implies that A dominates B.
1226 static bool RegistersDefinedFromSameValue(LiveIntervals &li,
1227                                           const TargetRegisterInfo &tri,
1228                                           CoalescerPair &CP,
1229                                           VNInfo *VNI,
1230                                           VNInfo *OtherVNI,
1231                                      SmallVector<MachineInstr*, 8> &DupCopies) {
1232   // FIXME: This is very conservative. For example, we don't handle
1233   // physical registers.
1234
1235   MachineInstr *MI = li.getInstructionFromIndex(VNI->def);
1236
1237   if (!MI || !MI->isFullCopy() || CP.isPartial() || CP.isPhys())
1238     return false;
1239
1240   unsigned Dst = MI->getOperand(0).getReg();
1241   unsigned Src = MI->getOperand(1).getReg();
1242
1243   if (!TargetRegisterInfo::isVirtualRegister(Src) ||
1244       !TargetRegisterInfo::isVirtualRegister(Dst))
1245     return false;
1246
1247   unsigned A = CP.getDstReg();
1248   unsigned B = CP.getSrcReg();
1249
1250   if (B == Dst)
1251     std::swap(A, B);
1252   assert(Dst == A);
1253
1254   const MachineInstr *OtherMI = li.getInstructionFromIndex(OtherVNI->def);
1255
1256   if (!OtherMI || !OtherMI->isFullCopy())
1257     return false;
1258
1259   unsigned OtherDst = OtherMI->getOperand(0).getReg();
1260   unsigned OtherSrc = OtherMI->getOperand(1).getReg();
1261
1262   if (!TargetRegisterInfo::isVirtualRegister(OtherSrc) ||
1263       !TargetRegisterInfo::isVirtualRegister(OtherDst))
1264     return false;
1265
1266   assert(OtherDst == B);
1267
1268   if (Src != OtherSrc)
1269     return false;
1270
1271   // If the copies use two different value numbers of X, we cannot merge
1272   // A and B.
1273   LiveInterval &SrcInt = li.getInterval(Src);
1274   // getVNInfoBefore returns NULL for undef copies. In this case, the
1275   // optimization is still safe.
1276   if (SrcInt.getVNInfoBefore(OtherVNI->def) != SrcInt.getVNInfoBefore(VNI->def))
1277     return false;
1278
1279   DupCopies.push_back(MI);
1280
1281   return true;
1282 }
1283
1284 /// joinIntervals - Attempt to join these two intervals.  On failure, this
1285 /// returns false.
1286 bool RegisterCoalescer::joinIntervals(CoalescerPair &CP) {
1287   // Handle physreg joins separately.
1288   if (CP.isPhys())
1289     return joinReservedPhysReg(CP);
1290
1291   LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1292   DEBUG({ dbgs() << "\t\tRHS = "; RHS.print(dbgs(), TRI); dbgs() << "\n"; });
1293
1294   // Compute the final value assignment, assuming that the live ranges can be
1295   // coalesced.
1296   SmallVector<int, 16> LHSValNoAssignments;
1297   SmallVector<int, 16> RHSValNoAssignments;
1298   DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS;
1299   DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS;
1300   SmallVector<VNInfo*, 16> NewVNInfo;
1301
1302   SmallVector<MachineInstr*, 8> DupCopies;
1303   SmallVector<MachineInstr*, 8> DeadCopies;
1304
1305   LiveInterval &LHS = LIS->getOrCreateInterval(CP.getDstReg());
1306   DEBUG({ dbgs() << "\t\tLHS = "; LHS.print(dbgs(), TRI); dbgs() << "\n"; });
1307
1308   // Loop over the value numbers of the LHS, seeing if any are defined from
1309   // the RHS.
1310   for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1311        i != e; ++i) {
1312     VNInfo *VNI = *i;
1313     if (VNI->isUnused() || VNI->isPHIDef())
1314       continue;
1315     MachineInstr *MI = LIS->getInstructionFromIndex(VNI->def);
1316     assert(MI && "Missing def");
1317     if (!MI->isCopyLike())  // Src not defined by a copy?
1318       continue;
1319
1320     // Figure out the value # from the RHS.
1321     VNInfo *OtherVNI = RHS.getVNInfoBefore(VNI->def);
1322     // The copy could be to an aliased physreg.
1323     if (!OtherVNI)
1324       continue;
1325
1326     // DstReg is known to be a register in the LHS interval.  If the src is
1327     // from the RHS interval, we can use its value #.
1328     if (CP.isCoalescable(MI))
1329       DeadCopies.push_back(MI);
1330     else if (!RegistersDefinedFromSameValue(*LIS, *TRI, CP, VNI, OtherVNI,
1331                                             DupCopies))
1332       continue;
1333
1334     LHSValsDefinedFromRHS[VNI] = OtherVNI;
1335   }
1336
1337   // Loop over the value numbers of the RHS, seeing if any are defined from
1338   // the LHS.
1339   for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1340        i != e; ++i) {
1341     VNInfo *VNI = *i;
1342     if (VNI->isUnused() || VNI->isPHIDef())
1343       continue;
1344     MachineInstr *MI = LIS->getInstructionFromIndex(VNI->def);
1345     assert(MI && "Missing def");
1346     if (!MI->isCopyLike())  // Src not defined by a copy?
1347       continue;
1348
1349     // Figure out the value # from the LHS.
1350     VNInfo *OtherVNI = LHS.getVNInfoBefore(VNI->def);
1351     // The copy could be to an aliased physreg.
1352     if (!OtherVNI)
1353       continue;
1354
1355     // DstReg is known to be a register in the RHS interval.  If the src is
1356     // from the LHS interval, we can use its value #.
1357     if (CP.isCoalescable(MI))
1358       DeadCopies.push_back(MI);
1359     else if (!RegistersDefinedFromSameValue(*LIS, *TRI, CP, VNI, OtherVNI,
1360                                             DupCopies))
1361         continue;
1362
1363     RHSValsDefinedFromLHS[VNI] = OtherVNI;
1364   }
1365
1366   LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
1367   RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
1368   NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
1369
1370   for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1371        i != e; ++i) {
1372     VNInfo *VNI = *i;
1373     unsigned VN = VNI->id;
1374     if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused())
1375       continue;
1376     ComputeUltimateVN(VNI, NewVNInfo,
1377                       LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
1378                       LHSValNoAssignments, RHSValNoAssignments);
1379   }
1380   for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1381        i != e; ++i) {
1382     VNInfo *VNI = *i;
1383     unsigned VN = VNI->id;
1384     if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
1385       continue;
1386     // If this value number isn't a copy from the LHS, it's a new number.
1387     if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
1388       NewVNInfo.push_back(VNI);
1389       RHSValNoAssignments[VN] = NewVNInfo.size()-1;
1390       continue;
1391     }
1392
1393     ComputeUltimateVN(VNI, NewVNInfo,
1394                       RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
1395                       RHSValNoAssignments, LHSValNoAssignments);
1396   }
1397
1398   // Armed with the mappings of LHS/RHS values to ultimate values, walk the
1399   // interval lists to see if these intervals are coalescable.
1400   LiveInterval::const_iterator I = LHS.begin();
1401   LiveInterval::const_iterator IE = LHS.end();
1402   LiveInterval::const_iterator J = RHS.begin();
1403   LiveInterval::const_iterator JE = RHS.end();
1404
1405   // Collect interval end points that will no longer be kills.
1406   SmallVector<MachineInstr*, 8> LHSOldKills;
1407   SmallVector<MachineInstr*, 8> RHSOldKills;
1408
1409   // Skip ahead until the first place of potential sharing.
1410   if (I != IE && J != JE) {
1411     if (I->start < J->start) {
1412       I = std::upper_bound(I, IE, J->start);
1413       if (I != LHS.begin()) --I;
1414     } else if (J->start < I->start) {
1415       J = std::upper_bound(J, JE, I->start);
1416       if (J != RHS.begin()) --J;
1417     }
1418   }
1419
1420   while (I != IE && J != JE) {
1421     // Determine if these two live ranges overlap.
1422     // If so, check value # info to determine if they are really different.
1423     if (I->end > J->start && J->end > I->start) {
1424       // If the live range overlap will map to the same value number in the
1425       // result liverange, we can still coalesce them.  If not, we can't.
1426       if (LHSValNoAssignments[I->valno->id] !=
1427           RHSValNoAssignments[J->valno->id])
1428         return false;
1429
1430       // Extended live ranges should no longer be killed.
1431       if (!I->end.isBlock() && I->end < J->end)
1432         if (MachineInstr *MI = LIS->getInstructionFromIndex(I->end))
1433           LHSOldKills.push_back(MI);
1434       if (!J->end.isBlock() && J->end < I->end)
1435         if (MachineInstr *MI = LIS->getInstructionFromIndex(J->end))
1436           RHSOldKills.push_back(MI);
1437     }
1438
1439     if (I->end < J->end)
1440       ++I;
1441     else
1442       ++J;
1443   }
1444
1445   // Update kill info. Some live ranges are extended due to copy coalescing.
1446   for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(),
1447          E = LHSValsDefinedFromRHS.end(); I != E; ++I) {
1448     VNInfo *VNI = I->first;
1449     unsigned LHSValID = LHSValNoAssignments[VNI->id];
1450     if (VNI->hasPHIKill())
1451       NewVNInfo[LHSValID]->setHasPHIKill(true);
1452   }
1453
1454   // Update kill info. Some live ranges are extended due to copy coalescing.
1455   for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(),
1456          E = RHSValsDefinedFromLHS.end(); I != E; ++I) {
1457     VNInfo *VNI = I->first;
1458     unsigned RHSValID = RHSValNoAssignments[VNI->id];
1459     if (VNI->hasPHIKill())
1460       NewVNInfo[RHSValID]->setHasPHIKill(true);
1461   }
1462
1463   // Clear kill flags where live ranges are extended.
1464   while (!LHSOldKills.empty())
1465     LHSOldKills.pop_back_val()->clearRegisterKills(LHS.reg, TRI);
1466   while (!RHSOldKills.empty())
1467     RHSOldKills.pop_back_val()->clearRegisterKills(RHS.reg, TRI);
1468
1469   if (LHSValNoAssignments.empty())
1470     LHSValNoAssignments.push_back(-1);
1471   if (RHSValNoAssignments.empty())
1472     RHSValNoAssignments.push_back(-1);
1473
1474   // Now erase all the redundant copies.
1475   for (unsigned i = 0, e = DeadCopies.size(); i != e; ++i) {
1476     MachineInstr *MI = DeadCopies[i];
1477     if (!ErasedInstrs.insert(MI))
1478       continue;
1479     DEBUG(dbgs() << "\t\terased:\t" << LIS->getInstructionIndex(MI)
1480                  << '\t' << *MI);
1481     LIS->RemoveMachineInstrFromMaps(MI);
1482     MI->eraseFromParent();
1483   }
1484
1485   SmallVector<unsigned, 8> SourceRegisters;
1486   for (SmallVector<MachineInstr*, 8>::iterator I = DupCopies.begin(),
1487          E = DupCopies.end(); I != E; ++I) {
1488     MachineInstr *MI = *I;
1489     if (!ErasedInstrs.insert(MI))
1490       continue;
1491
1492     // We have pretended that the assignment to B in
1493     // A = X
1494     // B = X
1495     // was actually a copy from A. Now that we decided to coalesce A and B,
1496     // transform the code into
1497     // A = X
1498     unsigned Src = MI->getOperand(1).getReg();
1499     SourceRegisters.push_back(Src);
1500     LIS->RemoveMachineInstrFromMaps(MI);
1501     MI->eraseFromParent();
1502   }
1503
1504   // If B = X was the last use of X in a liverange, we have to shrink it now
1505   // that B = X is gone.
1506   for (SmallVector<unsigned, 8>::iterator I = SourceRegisters.begin(),
1507          E = SourceRegisters.end(); I != E; ++I) {
1508     LIS->shrinkToUses(&LIS->getInterval(*I));
1509   }
1510
1511   // If we get here, we know that we can coalesce the live ranges.  Ask the
1512   // intervals to coalesce themselves now.
1513   LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo,
1514            MRI);
1515   return true;
1516 }
1517
1518 namespace {
1519   // DepthMBBCompare - Comparison predicate that sort first based on the loop
1520   // depth of the basic block (the unsigned), and then on the MBB number.
1521   struct DepthMBBCompare {
1522     typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
1523     bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
1524       // Deeper loops first
1525       if (LHS.first != RHS.first)
1526         return LHS.first > RHS.first;
1527
1528       // Prefer blocks that are more connected in the CFG. This takes care of
1529       // the most difficult copies first while intervals are short.
1530       unsigned cl = LHS.second->pred_size() + LHS.second->succ_size();
1531       unsigned cr = RHS.second->pred_size() + RHS.second->succ_size();
1532       if (cl != cr)
1533         return cl > cr;
1534
1535       // As a last resort, sort by block number.
1536       return LHS.second->getNumber() < RHS.second->getNumber();
1537     }
1538   };
1539 }
1540
1541 // Try joining WorkList copies starting from index From.
1542 // Null out any successful joins.
1543 bool RegisterCoalescer::copyCoalesceWorkList(unsigned From) {
1544   assert(From <= WorkList.size() && "Out of range");
1545   bool Progress = false;
1546   for (unsigned i = From, e = WorkList.size(); i != e; ++i) {
1547     if (!WorkList[i])
1548       continue;
1549     // Skip instruction pointers that have already been erased, for example by
1550     // dead code elimination.
1551     if (ErasedInstrs.erase(WorkList[i])) {
1552       WorkList[i] = 0;
1553       continue;
1554     }
1555     bool Again = false;
1556     bool Success = joinCopy(WorkList[i], Again);
1557     Progress |= Success;
1558     if (Success || !Again)
1559       WorkList[i] = 0;
1560   }
1561   return Progress;
1562 }
1563
1564 void
1565 RegisterCoalescer::copyCoalesceInMBB(MachineBasicBlock *MBB) {
1566   DEBUG(dbgs() << MBB->getName() << ":\n");
1567
1568   // Collect all copy-like instructions in MBB. Don't start coalescing anything
1569   // yet, it might invalidate the iterator.
1570   const unsigned PrevSize = WorkList.size();
1571   for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
1572        MII != E; ++MII)
1573     if (MII->isCopyLike())
1574       WorkList.push_back(MII);
1575
1576   // Try coalescing the collected copies immediately, and remove the nulls.
1577   // This prevents the WorkList from getting too large since most copies are
1578   // joinable on the first attempt.
1579   if (copyCoalesceWorkList(PrevSize))
1580     WorkList.erase(std::remove(WorkList.begin() + PrevSize, WorkList.end(),
1581                                (MachineInstr*)0), WorkList.end());
1582 }
1583
1584 void RegisterCoalescer::joinAllIntervals() {
1585   DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n");
1586   assert(WorkList.empty() && "Old data still around.");
1587
1588   if (Loops->empty()) {
1589     // If there are no loops in the function, join intervals in function order.
1590     for (MachineFunction::iterator I = MF->begin(), E = MF->end();
1591          I != E; ++I)
1592       copyCoalesceInMBB(I);
1593   } else {
1594     // Otherwise, join intervals in inner loops before other intervals.
1595     // Unfortunately we can't just iterate over loop hierarchy here because
1596     // there may be more MBB's than BB's.  Collect MBB's for sorting.
1597
1598     // Join intervals in the function prolog first. We want to join physical
1599     // registers with virtual registers before the intervals got too long.
1600     std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
1601     for (MachineFunction::iterator I = MF->begin(), E = MF->end();I != E;++I){
1602       MachineBasicBlock *MBB = I;
1603       MBBs.push_back(std::make_pair(Loops->getLoopDepth(MBB), I));
1604     }
1605
1606     // Sort by loop depth.
1607     std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
1608
1609     // Finally, join intervals in loop nest order.
1610     for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
1611       copyCoalesceInMBB(MBBs[i].second);
1612   }
1613
1614   // Joining intervals can allow other intervals to be joined.  Iteratively join
1615   // until we make no progress.
1616   while (copyCoalesceWorkList())
1617     /* empty */ ;
1618 }
1619
1620 void RegisterCoalescer::releaseMemory() {
1621   ErasedInstrs.clear();
1622   WorkList.clear();
1623   DeadDefs.clear();
1624   InflateRegs.clear();
1625 }
1626
1627 bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
1628   MF = &fn;
1629   MRI = &fn.getRegInfo();
1630   TM = &fn.getTarget();
1631   TRI = TM->getRegisterInfo();
1632   TII = TM->getInstrInfo();
1633   LIS = &getAnalysis<LiveIntervals>();
1634   LDV = &getAnalysis<LiveDebugVariables>();
1635   AA = &getAnalysis<AliasAnalysis>();
1636   Loops = &getAnalysis<MachineLoopInfo>();
1637
1638   DEBUG(dbgs() << "********** SIMPLE REGISTER COALESCING **********\n"
1639                << "********** Function: "
1640                << ((Value*)MF->getFunction())->getName() << '\n');
1641
1642   if (VerifyCoalescing)
1643     MF->verify(this, "Before register coalescing");
1644
1645   RegClassInfo.runOnMachineFunction(fn);
1646
1647   // Join (coalesce) intervals if requested.
1648   if (EnableJoining) {
1649     joinAllIntervals();
1650     DEBUG({
1651         dbgs() << "********** INTERVALS POST JOINING **********\n";
1652         for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end();
1653              I != E; ++I){
1654           I->second->print(dbgs(), TRI);
1655           dbgs() << "\n";
1656         }
1657       });
1658   }
1659
1660   // After deleting a lot of copies, register classes may be less constrained.
1661   // Removing sub-register operands may allow GR32_ABCD -> GR32 and DPR_VFP2 ->
1662   // DPR inflation.
1663   array_pod_sort(InflateRegs.begin(), InflateRegs.end());
1664   InflateRegs.erase(std::unique(InflateRegs.begin(), InflateRegs.end()),
1665                     InflateRegs.end());
1666   DEBUG(dbgs() << "Trying to inflate " << InflateRegs.size() << " regs.\n");
1667   for (unsigned i = 0, e = InflateRegs.size(); i != e; ++i) {
1668     unsigned Reg = InflateRegs[i];
1669     if (MRI->reg_nodbg_empty(Reg))
1670       continue;
1671     if (MRI->recomputeRegClass(Reg, *TM)) {
1672       DEBUG(dbgs() << PrintReg(Reg) << " inflated to "
1673                    << MRI->getRegClass(Reg)->getName() << '\n');
1674       ++NumInflated;
1675     }
1676   }
1677
1678   DEBUG(dump());
1679   DEBUG(LDV->dump());
1680   if (VerifyCoalescing)
1681     MF->verify(this, "After register coalescing");
1682   return true;
1683 }
1684
1685 /// print - Implement the dump method.
1686 void RegisterCoalescer::print(raw_ostream &O, const Module* m) const {
1687    LIS->print(O, m);
1688 }