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