Never attempt to join an early-clobber def with a regular kill.
[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 "VirtRegMap.h"
20
21 #include "llvm/Pass.h"
22 #include "llvm/Value.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
29 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
30 #include "llvm/CodeGen/LiveRangeEdit.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineLoopInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/Passes.h"
38 #include "llvm/CodeGen/RegisterClassInfo.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 STATISTIC(NumLaneConflicts, "Number of dead lane conflicts tested");
59 STATISTIC(NumLaneResolves,  "Number of dead lane conflicts resolved");
60
61 static cl::opt<bool>
62 EnableJoining("join-liveintervals",
63               cl::desc("Coalesce copies (default=true)"),
64               cl::init(true));
65
66 static cl::opt<bool>
67 VerifyCoalescing("verify-coalescing",
68          cl::desc("Verify machine instrs before and after register coalescing"),
69          cl::Hidden);
70
71 namespace {
72   class RegisterCoalescer : public MachineFunctionPass,
73                             private LiveRangeEdit::Delegate {
74     MachineFunction* MF;
75     MachineRegisterInfo* MRI;
76     const TargetMachine* TM;
77     const TargetRegisterInfo* TRI;
78     const TargetInstrInfo* TII;
79     LiveIntervals *LIS;
80     LiveDebugVariables *LDV;
81     const MachineLoopInfo* Loops;
82     AliasAnalysis *AA;
83     RegisterClassInfo RegClassInfo;
84
85     /// WorkList - Copy instructions yet to be coalesced.
86     SmallVector<MachineInstr*, 8> WorkList;
87
88     /// ErasedInstrs - Set of instruction pointers that have been erased, and
89     /// that may be present in WorkList.
90     SmallPtrSet<MachineInstr*, 8> ErasedInstrs;
91
92     /// Dead instructions that are about to be deleted.
93     SmallVector<MachineInstr*, 8> DeadDefs;
94
95     /// Virtual registers to be considered for register class inflation.
96     SmallVector<unsigned, 8> InflateRegs;
97
98     /// Recursively eliminate dead defs in DeadDefs.
99     void eliminateDeadDefs();
100
101     /// LiveRangeEdit callback.
102     void LRE_WillEraseInstruction(MachineInstr *MI);
103
104     /// joinAllIntervals - join compatible live intervals
105     void joinAllIntervals();
106
107     /// copyCoalesceInMBB - Coalesce copies in the specified MBB, putting
108     /// copies that cannot yet be coalesced into WorkList.
109     void copyCoalesceInMBB(MachineBasicBlock *MBB);
110
111     /// copyCoalesceWorkList - Try to coalesce all copies in WorkList after
112     /// position From. Return true if any progress was made.
113     bool copyCoalesceWorkList(unsigned From = 0);
114
115     /// joinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
116     /// which are the src/dst of the copy instruction CopyMI.  This returns
117     /// true if the copy was successfully coalesced away. If it is not
118     /// currently possible to coalesce this interval, but it may be possible if
119     /// other things get coalesced, then it returns true by reference in
120     /// 'Again'.
121     bool joinCopy(MachineInstr *TheCopy, bool &Again);
122
123     /// joinIntervals - Attempt to join these two intervals.  On failure, this
124     /// returns false.  The output "SrcInt" will not have been modified, so we
125     /// can use this information below to update aliases.
126     bool joinIntervals(CoalescerPair &CP);
127
128     /// Attempt joining two virtual registers. Return true on success.
129     bool joinVirtRegs(CoalescerPair &CP);
130
131     /// Attempt joining with a reserved physreg.
132     bool joinReservedPhysReg(CoalescerPair &CP);
133
134     /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
135     /// the source value number is defined by a copy from the destination reg
136     /// see if we can merge these two destination reg valno# into a single
137     /// value number, eliminating a copy.
138     bool adjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
139
140     /// hasOtherReachingDefs - Return true if there are definitions of IntB
141     /// other than BValNo val# that can reach uses of AValno val# of IntA.
142     bool hasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
143                               VNInfo *AValNo, VNInfo *BValNo);
144
145     /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy.
146     /// If the source value number is defined by a commutable instruction and
147     /// its other operand is coalesced to the copy dest register, see if we
148     /// can transform the copy into a noop by commuting the definition.
149     bool removeCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
150
151     /// reMaterializeTrivialDef - If the source of a copy is defined by a
152     /// trivial computation, replace the copy by rematerialize the definition.
153     bool reMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg,
154                                  MachineInstr *CopyMI);
155
156     /// canJoinPhys - Return true if a physreg copy should be joined.
157     bool canJoinPhys(CoalescerPair &CP);
158
159     /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
160     /// update the subregister number if it is not zero. If DstReg is a
161     /// physical register and the existing subregister number of the def / use
162     /// being updated is not zero, make sure to set it to the correct physical
163     /// subregister.
164     void updateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx);
165
166     /// eliminateUndefCopy - Handle copies of undef values.
167     bool eliminateUndefCopy(MachineInstr *CopyMI, const CoalescerPair &CP);
168
169   public:
170     static char ID; // Class identification, replacement for typeinfo
171     RegisterCoalescer() : MachineFunctionPass(ID) {
172       initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
173     }
174
175     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
176
177     virtual void releaseMemory();
178
179     /// runOnMachineFunction - pass entry point
180     virtual bool runOnMachineFunction(MachineFunction&);
181
182     /// print - Implement the dump method.
183     virtual void print(raw_ostream &O, const Module* = 0) const;
184   };
185 } /// end anonymous namespace
186
187 char &llvm::RegisterCoalescerID = RegisterCoalescer::ID;
188
189 INITIALIZE_PASS_BEGIN(RegisterCoalescer, "simple-register-coalescing",
190                       "Simple Register Coalescing", false, false)
191 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
192 INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
193 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
194 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
195 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
196 INITIALIZE_PASS_END(RegisterCoalescer, "simple-register-coalescing",
197                     "Simple Register Coalescing", false, false)
198
199 char RegisterCoalescer::ID = 0;
200
201 static unsigned compose(const TargetRegisterInfo &tri, unsigned a, unsigned b) {
202   if (!a) return b;
203   if (!b) return a;
204   return tri.composeSubRegIndices(a, b);
205 }
206
207 static bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI,
208                         unsigned &Src, unsigned &Dst,
209                         unsigned &SrcSub, unsigned &DstSub) {
210   if (MI->isCopy()) {
211     Dst = MI->getOperand(0).getReg();
212     DstSub = MI->getOperand(0).getSubReg();
213     Src = MI->getOperand(1).getReg();
214     SrcSub = MI->getOperand(1).getSubReg();
215   } else if (MI->isSubregToReg()) {
216     Dst = MI->getOperand(0).getReg();
217     DstSub = compose(tri, MI->getOperand(0).getSubReg(),
218                      MI->getOperand(3).getImm());
219     Src = MI->getOperand(2).getReg();
220     SrcSub = MI->getOperand(2).getSubReg();
221   } else
222     return false;
223   return true;
224 }
225
226 bool CoalescerPair::setRegisters(const MachineInstr *MI) {
227   SrcReg = DstReg = 0;
228   SrcIdx = DstIdx = 0;
229   NewRC = 0;
230   Flipped = CrossClass = false;
231
232   unsigned Src, Dst, SrcSub, DstSub;
233   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
234     return false;
235   Partial = SrcSub || DstSub;
236
237   // If one register is a physreg, it must be Dst.
238   if (TargetRegisterInfo::isPhysicalRegister(Src)) {
239     if (TargetRegisterInfo::isPhysicalRegister(Dst))
240       return false;
241     std::swap(Src, Dst);
242     std::swap(SrcSub, DstSub);
243     Flipped = true;
244   }
245
246   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
247
248   if (TargetRegisterInfo::isPhysicalRegister(Dst)) {
249     // Eliminate DstSub on a physreg.
250     if (DstSub) {
251       Dst = TRI.getSubReg(Dst, DstSub);
252       if (!Dst) return false;
253       DstSub = 0;
254     }
255
256     // Eliminate SrcSub by picking a corresponding Dst superregister.
257     if (SrcSub) {
258       Dst = TRI.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src));
259       if (!Dst) return false;
260       SrcSub = 0;
261     } else if (!MRI.getRegClass(Src)->contains(Dst)) {
262       return false;
263     }
264   } else {
265     // Both registers are virtual.
266     const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
267     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
268
269     // Both registers have subreg indices.
270     if (SrcSub && DstSub) {
271       // Copies between different sub-registers are never coalescable.
272       if (Src == Dst && SrcSub != DstSub)
273         return false;
274
275       NewRC = TRI.getCommonSuperRegClass(SrcRC, SrcSub, DstRC, DstSub,
276                                          SrcIdx, DstIdx);
277       if (!NewRC)
278         return false;
279     } else if (DstSub) {
280       // SrcReg will be merged with a sub-register of DstReg.
281       SrcIdx = DstSub;
282       NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
283     } else if (SrcSub) {
284       // DstReg will be merged with a sub-register of SrcReg.
285       DstIdx = SrcSub;
286       NewRC = TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSub);
287     } else {
288       // This is a straight copy without sub-registers.
289       NewRC = TRI.getCommonSubClass(DstRC, SrcRC);
290     }
291
292     // The combined constraint may be impossible to satisfy.
293     if (!NewRC)
294       return false;
295
296     // Prefer SrcReg to be a sub-register of DstReg.
297     // FIXME: Coalescer should support subregs symmetrically.
298     if (DstIdx && !SrcIdx) {
299       std::swap(Src, Dst);
300       std::swap(SrcIdx, DstIdx);
301       Flipped = !Flipped;
302     }
303
304     CrossClass = NewRC != DstRC || NewRC != SrcRC;
305   }
306   // Check our invariants
307   assert(TargetRegisterInfo::isVirtualRegister(Src) && "Src must be virtual");
308   assert(!(TargetRegisterInfo::isPhysicalRegister(Dst) && DstSub) &&
309          "Cannot have a physical SubIdx");
310   SrcReg = Src;
311   DstReg = Dst;
312   return true;
313 }
314
315 bool CoalescerPair::flip() {
316   if (TargetRegisterInfo::isPhysicalRegister(DstReg))
317     return false;
318   std::swap(SrcReg, DstReg);
319   std::swap(SrcIdx, DstIdx);
320   Flipped = !Flipped;
321   return true;
322 }
323
324 bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
325   if (!MI)
326     return false;
327   unsigned Src, Dst, SrcSub, DstSub;
328   if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
329     return false;
330
331   // Find the virtual register that is SrcReg.
332   if (Dst == SrcReg) {
333     std::swap(Src, Dst);
334     std::swap(SrcSub, DstSub);
335   } else if (Src != SrcReg) {
336     return false;
337   }
338
339   // Now check that Dst matches DstReg.
340   if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
341     if (!TargetRegisterInfo::isPhysicalRegister(Dst))
342       return false;
343     assert(!DstIdx && !SrcIdx && "Inconsistent CoalescerPair state.");
344     // DstSub could be set for a physreg from INSERT_SUBREG.
345     if (DstSub)
346       Dst = TRI.getSubReg(Dst, DstSub);
347     // Full copy of Src.
348     if (!SrcSub)
349       return DstReg == Dst;
350     // This is a partial register copy. Check that the parts match.
351     return TRI.getSubReg(DstReg, SrcSub) == Dst;
352   } else {
353     // DstReg is virtual.
354     if (DstReg != Dst)
355       return false;
356     // Registers match, do the subregisters line up?
357     return compose(TRI, SrcIdx, SrcSub) == compose(TRI, DstIdx, DstSub);
358   }
359 }
360
361 void RegisterCoalescer::getAnalysisUsage(AnalysisUsage &AU) const {
362   AU.setPreservesCFG();
363   AU.addRequired<AliasAnalysis>();
364   AU.addRequired<LiveIntervals>();
365   AU.addPreserved<LiveIntervals>();
366   AU.addRequired<LiveDebugVariables>();
367   AU.addPreserved<LiveDebugVariables>();
368   AU.addPreserved<SlotIndexes>();
369   AU.addRequired<MachineLoopInfo>();
370   AU.addPreserved<MachineLoopInfo>();
371   AU.addPreservedID(MachineDominatorsID);
372   MachineFunctionPass::getAnalysisUsage(AU);
373 }
374
375 void RegisterCoalescer::eliminateDeadDefs() {
376   SmallVector<LiveInterval*, 8> NewRegs;
377   LiveRangeEdit(0, NewRegs, *MF, *LIS, 0, this).eliminateDeadDefs(DeadDefs);
378 }
379
380 // Callback from eliminateDeadDefs().
381 void RegisterCoalescer::LRE_WillEraseInstruction(MachineInstr *MI) {
382   // MI may be in WorkList. Make sure we don't visit it.
383   ErasedInstrs.insert(MI);
384 }
385
386 /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
387 /// being the source and IntB being the dest, thus this defines a value number
388 /// in IntB.  If the source value number (in IntA) is defined by a copy from B,
389 /// see if we can merge these two pieces of B into a single value number,
390 /// eliminating a copy.  For example:
391 ///
392 ///  A3 = B0
393 ///    ...
394 ///  B1 = A3      <- this copy
395 ///
396 /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
397 /// value number to be replaced with B0 (which simplifies the B liveinterval).
398 ///
399 /// This returns true if an interval was modified.
400 ///
401 bool RegisterCoalescer::adjustCopiesBackFrom(const CoalescerPair &CP,
402                                              MachineInstr *CopyMI) {
403   assert(!CP.isPartial() && "This doesn't work for partial copies.");
404   assert(!CP.isPhys() && "This doesn't work for physreg copies.");
405
406   LiveInterval &IntA =
407     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
408   LiveInterval &IntB =
409     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
410   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
411
412   // BValNo is a value number in B that is defined by a copy from A.  'B3' in
413   // the example above.
414   LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
415   if (BLR == IntB.end()) return false;
416   VNInfo *BValNo = BLR->valno;
417
418   // Get the location that B is defined at.  Two options: either this value has
419   // an unknown definition point or it is defined at CopyIdx.  If unknown, we
420   // can't process it.
421   if (BValNo->def != CopyIdx) return false;
422
423   // AValNo is the value number in A that defines the copy, A3 in the example.
424   SlotIndex CopyUseIdx = CopyIdx.getRegSlot(true);
425   LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyUseIdx);
426   // The live range might not exist after fun with physreg coalescing.
427   if (ALR == IntA.end()) return false;
428   VNInfo *AValNo = ALR->valno;
429
430   // If AValNo is defined as a copy from IntB, we can potentially process this.
431   // Get the instruction that defines this value number.
432   MachineInstr *ACopyMI = LIS->getInstructionFromIndex(AValNo->def);
433   if (!CP.isCoalescable(ACopyMI))
434     return false;
435
436   // Get the LiveRange in IntB that this value number starts with.
437   LiveInterval::iterator ValLR =
438     IntB.FindLiveRangeContaining(AValNo->def.getPrevSlot());
439   if (ValLR == IntB.end())
440     return false;
441
442   // Make sure that the end of the live range is inside the same block as
443   // CopyMI.
444   MachineInstr *ValLREndInst =
445     LIS->getInstructionFromIndex(ValLR->end.getPrevSlot());
446   if (!ValLREndInst || ValLREndInst->getParent() != CopyMI->getParent())
447     return false;
448
449   // Okay, we now know that ValLR ends in the same block that the CopyMI
450   // live-range starts.  If there are no intervening live ranges between them in
451   // IntB, we can merge them.
452   if (ValLR+1 != BLR) return false;
453
454   DEBUG(dbgs() << "Extending: " << PrintReg(IntB.reg, TRI));
455
456   SlotIndex FillerStart = ValLR->end, FillerEnd = BLR->start;
457   // We are about to delete CopyMI, so need to remove it as the 'instruction
458   // that defines this value #'. Update the valnum with the new defining
459   // instruction #.
460   BValNo->def = FillerStart;
461
462   // Okay, we can merge them.  We need to insert a new liverange:
463   // [ValLR.end, BLR.begin) of either value number, then we merge the
464   // two value numbers.
465   IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
466
467   // Okay, merge "B1" into the same value number as "B0".
468   if (BValNo != ValLR->valno)
469     IntB.MergeValueNumberInto(BValNo, ValLR->valno);
470   DEBUG(dbgs() << "   result = " << IntB << '\n');
471
472   // If the source instruction was killing the source register before the
473   // merge, unset the isKill marker given the live range has been extended.
474   int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
475   if (UIdx != -1) {
476     ValLREndInst->getOperand(UIdx).setIsKill(false);
477   }
478
479   // Rewrite the copy. If the copy instruction was killing the destination
480   // register before the merge, find the last use and trim the live range. That
481   // will also add the isKill marker.
482   CopyMI->substituteRegister(IntA.reg, IntB.reg, 0, *TRI);
483   if (ALR->end == CopyIdx)
484     LIS->shrinkToUses(&IntA);
485
486   ++numExtends;
487   return true;
488 }
489
490 /// hasOtherReachingDefs - Return true if there are definitions of IntB
491 /// other than BValNo val# that can reach uses of AValno val# of IntA.
492 bool RegisterCoalescer::hasOtherReachingDefs(LiveInterval &IntA,
493                                              LiveInterval &IntB,
494                                              VNInfo *AValNo,
495                                              VNInfo *BValNo) {
496   // If AValNo has PHI kills, conservatively assume that IntB defs can reach
497   // the PHI values.
498   if (LIS->hasPHIKill(IntA, AValNo))
499     return true;
500
501   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
502        AI != AE; ++AI) {
503     if (AI->valno != AValNo) continue;
504     LiveInterval::Ranges::iterator BI =
505       std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start);
506     if (BI != IntB.ranges.begin())
507       --BI;
508     for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
509       if (BI->valno == BValNo)
510         continue;
511       if (BI->start <= AI->start && BI->end > AI->start)
512         return true;
513       if (BI->start > AI->start && BI->start < AI->end)
514         return true;
515     }
516   }
517   return false;
518 }
519
520 /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy with
521 /// IntA being the source and IntB being the dest, thus this defines a value
522 /// number in IntB.  If the source value number (in IntA) is defined by a
523 /// commutable instruction and its other operand is coalesced to the copy dest
524 /// register, see if we can transform the copy into a noop by commuting the
525 /// definition. For example,
526 ///
527 ///  A3 = op A2 B0<kill>
528 ///    ...
529 ///  B1 = A3      <- this copy
530 ///    ...
531 ///     = op A3   <- more uses
532 ///
533 /// ==>
534 ///
535 ///  B2 = op B0 A2<kill>
536 ///    ...
537 ///  B1 = B2      <- now an identify copy
538 ///    ...
539 ///     = op B2   <- more uses
540 ///
541 /// This returns true if an interval was modified.
542 ///
543 bool RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP,
544                                                  MachineInstr *CopyMI) {
545   assert (!CP.isPhys());
546
547   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
548
549   LiveInterval &IntA =
550     LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
551   LiveInterval &IntB =
552     LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
553
554   // BValNo is a value number in B that is defined by a copy from A. 'B3' in
555   // the example above.
556   VNInfo *BValNo = IntB.getVNInfoAt(CopyIdx);
557   if (!BValNo || BValNo->def != CopyIdx)
558     return false;
559
560   assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
561
562   // AValNo is the value number in A that defines the copy, A3 in the example.
563   VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx.getRegSlot(true));
564   assert(AValNo && "COPY source not live");
565   if (AValNo->isPHIDef() || AValNo->isUnused())
566     return false;
567   MachineInstr *DefMI = LIS->getInstructionFromIndex(AValNo->def);
568   if (!DefMI)
569     return false;
570   if (!DefMI->isCommutable())
571     return false;
572   // If DefMI is a two-address instruction then commuting it will change the
573   // destination register.
574   int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg);
575   assert(DefIdx != -1);
576   unsigned UseOpIdx;
577   if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
578     return false;
579   unsigned Op1, Op2, NewDstIdx;
580   if (!TII->findCommutedOpIndices(DefMI, Op1, Op2))
581     return false;
582   if (Op1 == UseOpIdx)
583     NewDstIdx = Op2;
584   else if (Op2 == UseOpIdx)
585     NewDstIdx = Op1;
586   else
587     return false;
588
589   MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
590   unsigned NewReg = NewDstMO.getReg();
591   if (NewReg != IntB.reg || !LiveRangeQuery(IntB, AValNo->def).isKill())
592     return false;
593
594   // Make sure there are no other definitions of IntB that would reach the
595   // uses which the new definition can reach.
596   if (hasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
597     return false;
598
599   // If some of the uses of IntA.reg is already coalesced away, return false.
600   // It's not possible to determine whether it's safe to perform the coalescing.
601   for (MachineRegisterInfo::use_nodbg_iterator UI =
602          MRI->use_nodbg_begin(IntA.reg),
603        UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
604     MachineInstr *UseMI = &*UI;
605     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI);
606     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
607     if (ULR == IntA.end() || ULR->valno != AValNo)
608       continue;
609     // If this use is tied to a def, we can't rewrite the register.
610     if (UseMI->isRegTiedToDefOperand(UI.getOperandNo()))
611       return false;
612   }
613
614   DEBUG(dbgs() << "\tremoveCopyByCommutingDef: " << AValNo->def << '\t'
615                << *DefMI);
616
617   // At this point we have decided that it is legal to do this
618   // transformation.  Start by commuting the instruction.
619   MachineBasicBlock *MBB = DefMI->getParent();
620   MachineInstr *NewMI = TII->commuteInstruction(DefMI);
621   if (!NewMI)
622     return false;
623   if (TargetRegisterInfo::isVirtualRegister(IntA.reg) &&
624       TargetRegisterInfo::isVirtualRegister(IntB.reg) &&
625       !MRI->constrainRegClass(IntB.reg, MRI->getRegClass(IntA.reg)))
626     return false;
627   if (NewMI != DefMI) {
628     LIS->ReplaceMachineInstrInMaps(DefMI, NewMI);
629     MachineBasicBlock::iterator Pos = DefMI;
630     MBB->insert(Pos, NewMI);
631     MBB->erase(DefMI);
632   }
633   unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
634   NewMI->getOperand(OpIdx).setIsKill();
635
636   // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
637   // A = or A, B
638   // ...
639   // B = A
640   // ...
641   // C = A<kill>
642   // ...
643   //   = B
644
645   // Update uses of IntA of the specific Val# with IntB.
646   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(IntA.reg),
647          UE = MRI->use_end(); UI != UE;) {
648     MachineOperand &UseMO = UI.getOperand();
649     MachineInstr *UseMI = &*UI;
650     ++UI;
651     if (UseMI->isDebugValue()) {
652       // FIXME These don't have an instruction index.  Not clear we have enough
653       // info to decide whether to do this replacement or not.  For now do it.
654       UseMO.setReg(NewReg);
655       continue;
656     }
657     SlotIndex UseIdx = LIS->getInstructionIndex(UseMI).getRegSlot(true);
658     LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
659     if (ULR == IntA.end() || ULR->valno != AValNo)
660       continue;
661     // Kill flags are no longer accurate. They are recomputed after RA.
662     UseMO.setIsKill(false);
663     if (TargetRegisterInfo::isPhysicalRegister(NewReg))
664       UseMO.substPhysReg(NewReg, *TRI);
665     else
666       UseMO.setReg(NewReg);
667     if (UseMI == CopyMI)
668       continue;
669     if (!UseMI->isCopy())
670       continue;
671     if (UseMI->getOperand(0).getReg() != IntB.reg ||
672         UseMI->getOperand(0).getSubReg())
673       continue;
674
675     // This copy will become a noop. If it's defining a new val#, merge it into
676     // BValNo.
677     SlotIndex DefIdx = UseIdx.getRegSlot();
678     VNInfo *DVNI = IntB.getVNInfoAt(DefIdx);
679     if (!DVNI)
680       continue;
681     DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI);
682     assert(DVNI->def == DefIdx);
683     BValNo = IntB.MergeValueNumberInto(BValNo, DVNI);
684     ErasedInstrs.insert(UseMI);
685     LIS->RemoveMachineInstrFromMaps(UseMI);
686     UseMI->eraseFromParent();
687   }
688
689   // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
690   // is updated.
691   VNInfo *ValNo = BValNo;
692   ValNo->def = AValNo->def;
693   for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
694        AI != AE; ++AI) {
695     if (AI->valno != AValNo) continue;
696     IntB.addRange(LiveRange(AI->start, AI->end, ValNo));
697   }
698   DEBUG(dbgs() << "\t\textended: " << IntB << '\n');
699
700   IntA.removeValNo(AValNo);
701   DEBUG(dbgs() << "\t\ttrimmed:  " << IntA << '\n');
702   ++numCommutes;
703   return true;
704 }
705
706 /// reMaterializeTrivialDef - If the source of a copy is defined by a trivial
707 /// computation, replace the copy by rematerialize the definition.
708 bool RegisterCoalescer::reMaterializeTrivialDef(LiveInterval &SrcInt,
709                                                 unsigned DstReg,
710                                                 MachineInstr *CopyMI) {
711   SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(true);
712   LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx);
713   assert(SrcLR != SrcInt.end() && "Live range not found!");
714   VNInfo *ValNo = SrcLR->valno;
715   if (ValNo->isPHIDef() || ValNo->isUnused())
716     return false;
717   MachineInstr *DefMI = LIS->getInstructionFromIndex(ValNo->def);
718   if (!DefMI)
719     return false;
720   assert(DefMI && "Defining instruction disappeared");
721   if (!DefMI->isAsCheapAsAMove())
722     return false;
723   if (!TII->isTriviallyReMaterializable(DefMI, AA))
724     return false;
725   bool SawStore = false;
726   if (!DefMI->isSafeToMove(TII, AA, SawStore))
727     return false;
728   const MCInstrDesc &MCID = DefMI->getDesc();
729   if (MCID.getNumDefs() != 1)
730     return false;
731   if (!DefMI->isImplicitDef()) {
732     // Make sure the copy destination register class fits the instruction
733     // definition register class. The mismatch can happen as a result of earlier
734     // extract_subreg, insert_subreg, subreg_to_reg coalescing.
735     const TargetRegisterClass *RC = TII->getRegClass(MCID, 0, TRI, *MF);
736     if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
737       if (MRI->getRegClass(DstReg) != RC)
738         return false;
739     } else if (!RC->contains(DstReg))
740       return false;
741   }
742
743   MachineBasicBlock *MBB = CopyMI->getParent();
744   MachineBasicBlock::iterator MII =
745     llvm::next(MachineBasicBlock::iterator(CopyMI));
746   TII->reMaterialize(*MBB, MII, DstReg, 0, DefMI, *TRI);
747   MachineInstr *NewMI = prior(MII);
748
749   // NewMI may have dead implicit defs (E.g. EFLAGS for MOV<bits>r0 on X86).
750   // We need to remember these so we can add intervals once we insert
751   // NewMI into SlotIndexes.
752   SmallVector<unsigned, 4> NewMIImplDefs;
753   for (unsigned i = NewMI->getDesc().getNumOperands(),
754          e = NewMI->getNumOperands(); i != e; ++i) {
755     MachineOperand &MO = NewMI->getOperand(i);
756     if (MO.isReg()) {
757       assert(MO.isDef() && MO.isImplicit() && MO.isDead() &&
758              TargetRegisterInfo::isPhysicalRegister(MO.getReg()));
759       NewMIImplDefs.push_back(MO.getReg());
760     }
761   }
762
763   // CopyMI may have implicit operands, transfer them over to the newly
764   // rematerialized instruction. And update implicit def interval valnos.
765   for (unsigned i = CopyMI->getDesc().getNumOperands(),
766          e = CopyMI->getNumOperands(); i != e; ++i) {
767     MachineOperand &MO = CopyMI->getOperand(i);
768     if (MO.isReg()) {
769       assert(MO.isImplicit() && "No explicit operands after implict operands.");
770       // Discard VReg implicit defs.
771       if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
772         NewMI->addOperand(MO);
773       }
774     }
775   }
776
777   LIS->ReplaceMachineInstrInMaps(CopyMI, NewMI);
778
779   SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
780   for (unsigned i = 0, e = NewMIImplDefs.size(); i != e; ++i) {
781     unsigned Reg = NewMIImplDefs[i];
782     for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
783       if (LiveInterval *LI = LIS->getCachedRegUnit(*Units))
784         LI->createDeadDef(NewMIIdx.getRegSlot(), LIS->getVNInfoAllocator());
785   }
786
787   CopyMI->eraseFromParent();
788   ErasedInstrs.insert(CopyMI);
789   DEBUG(dbgs() << "Remat: " << *NewMI);
790   ++NumReMats;
791
792   // The source interval can become smaller because we removed a use.
793   LIS->shrinkToUses(&SrcInt, &DeadDefs);
794   if (!DeadDefs.empty())
795     eliminateDeadDefs();
796
797   return true;
798 }
799
800 /// eliminateUndefCopy - ProcessImpicitDefs may leave some copies of <undef>
801 /// values, it only removes local variables. When we have a copy like:
802 ///
803 ///   %vreg1 = COPY %vreg2<undef>
804 ///
805 /// We delete the copy and remove the corresponding value number from %vreg1.
806 /// Any uses of that value number are marked as <undef>.
807 bool RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI,
808                                            const CoalescerPair &CP) {
809   SlotIndex Idx = LIS->getInstructionIndex(CopyMI);
810   LiveInterval *SrcInt = &LIS->getInterval(CP.getSrcReg());
811   if (SrcInt->liveAt(Idx))
812     return false;
813   LiveInterval *DstInt = &LIS->getInterval(CP.getDstReg());
814   if (DstInt->liveAt(Idx))
815     return false;
816
817   // No intervals are live-in to CopyMI - it is undef.
818   if (CP.isFlipped())
819     DstInt = SrcInt;
820   SrcInt = 0;
821
822   VNInfo *DeadVNI = DstInt->getVNInfoAt(Idx.getRegSlot());
823   assert(DeadVNI && "No value defined in DstInt");
824   DstInt->removeValNo(DeadVNI);
825
826   // Find new undef uses.
827   for (MachineRegisterInfo::reg_nodbg_iterator
828          I = MRI->reg_nodbg_begin(DstInt->reg), E = MRI->reg_nodbg_end();
829        I != E; ++I) {
830     MachineOperand &MO = I.getOperand();
831     if (MO.isDef() || MO.isUndef())
832       continue;
833     MachineInstr *MI = MO.getParent();
834     SlotIndex Idx = LIS->getInstructionIndex(MI);
835     if (DstInt->liveAt(Idx))
836       continue;
837     MO.setIsUndef(true);
838     DEBUG(dbgs() << "\tnew undef: " << Idx << '\t' << *MI);
839   }
840   return true;
841 }
842
843 /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
844 /// update the subregister number if it is not zero. If DstReg is a
845 /// physical register and the existing subregister number of the def / use
846 /// being updated is not zero, make sure to set it to the correct physical
847 /// subregister.
848 void RegisterCoalescer::updateRegDefsUses(unsigned SrcReg,
849                                           unsigned DstReg,
850                                           unsigned SubIdx) {
851   bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
852   LiveInterval *DstInt = DstIsPhys ? 0 : &LIS->getInterval(DstReg);
853
854   // Update LiveDebugVariables.
855   LDV->renameRegister(SrcReg, DstReg, SubIdx);
856
857   for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(SrcReg);
858        MachineInstr *UseMI = I.skipInstruction();) {
859     SmallVector<unsigned,8> Ops;
860     bool Reads, Writes;
861     tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops);
862
863     // If SrcReg wasn't read, it may still be the case that DstReg is live-in
864     // because SrcReg is a sub-register.
865     if (DstInt && !Reads && SubIdx)
866       Reads = DstInt->liveAt(LIS->getInstructionIndex(UseMI));
867
868     // Replace SrcReg with DstReg in all UseMI operands.
869     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
870       MachineOperand &MO = UseMI->getOperand(Ops[i]);
871
872       // Adjust <undef> flags in case of sub-register joins. We don't want to
873       // turn a full def into a read-modify-write sub-register def and vice
874       // versa.
875       if (SubIdx && MO.isDef())
876         MO.setIsUndef(!Reads);
877
878       if (DstIsPhys)
879         MO.substPhysReg(DstReg, *TRI);
880       else
881         MO.substVirtReg(DstReg, SubIdx, *TRI);
882     }
883
884     DEBUG({
885         dbgs() << "\t\tupdated: ";
886         if (!UseMI->isDebugValue())
887           dbgs() << LIS->getInstructionIndex(UseMI) << "\t";
888         dbgs() << *UseMI;
889       });
890   }
891 }
892
893 /// canJoinPhys - Return true if a copy involving a physreg should be joined.
894 bool RegisterCoalescer::canJoinPhys(CoalescerPair &CP) {
895   /// Always join simple intervals that are defined by a single copy from a
896   /// reserved register. This doesn't increase register pressure, so it is
897   /// always beneficial.
898   if (!MRI->isReserved(CP.getDstReg())) {
899     DEBUG(dbgs() << "\tCan only merge into reserved registers.\n");
900     return false;
901   }
902
903   LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg());
904   if (CP.isFlipped() && JoinVInt.containsOneValue())
905     return true;
906
907   DEBUG(dbgs() << "\tCannot join defs into reserved register.\n");
908   return false;
909 }
910
911 /// joinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
912 /// which are the src/dst of the copy instruction CopyMI.  This returns true
913 /// if the copy was successfully coalesced away. If it is not currently
914 /// possible to coalesce this interval, but it may be possible if other
915 /// things get coalesced, then it returns true by reference in 'Again'.
916 bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
917
918   Again = false;
919   DEBUG(dbgs() << LIS->getInstructionIndex(CopyMI) << '\t' << *CopyMI);
920
921   CoalescerPair CP(*TRI);
922   if (!CP.setRegisters(CopyMI)) {
923     DEBUG(dbgs() << "\tNot coalescable.\n");
924     return false;
925   }
926
927   // Dead code elimination. This really should be handled by MachineDCE, but
928   // sometimes dead copies slip through, and we can't generate invalid live
929   // ranges.
930   if (!CP.isPhys() && CopyMI->allDefsAreDead()) {
931     DEBUG(dbgs() << "\tCopy is dead.\n");
932     DeadDefs.push_back(CopyMI);
933     eliminateDeadDefs();
934     return true;
935   }
936
937   // Eliminate undefs.
938   if (!CP.isPhys() && eliminateUndefCopy(CopyMI, CP)) {
939     DEBUG(dbgs() << "\tEliminated copy of <undef> value.\n");
940     LIS->RemoveMachineInstrFromMaps(CopyMI);
941     CopyMI->eraseFromParent();
942     return false;  // Not coalescable.
943   }
944
945   // Coalesced copies are normally removed immediately, but transformations
946   // like removeCopyByCommutingDef() can inadvertently create identity copies.
947   // When that happens, just join the values and remove the copy.
948   if (CP.getSrcReg() == CP.getDstReg()) {
949     LiveInterval &LI = LIS->getInterval(CP.getSrcReg());
950     DEBUG(dbgs() << "\tCopy already coalesced: " << LI << '\n');
951     LiveRangeQuery LRQ(LI, LIS->getInstructionIndex(CopyMI));
952     if (VNInfo *DefVNI = LRQ.valueDefined()) {
953       VNInfo *ReadVNI = LRQ.valueIn();
954       assert(ReadVNI && "No value before copy and no <undef> flag.");
955       assert(ReadVNI != DefVNI && "Cannot read and define the same value.");
956       LI.MergeValueNumberInto(DefVNI, ReadVNI);
957       DEBUG(dbgs() << "\tMerged values:          " << LI << '\n');
958     }
959     LIS->RemoveMachineInstrFromMaps(CopyMI);
960     CopyMI->eraseFromParent();
961     return true;
962   }
963
964   // Enforce policies.
965   if (CP.isPhys()) {
966     DEBUG(dbgs() << "\tConsidering merging " << PrintReg(CP.getSrcReg(), TRI)
967                  << " with " << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx())
968                  << '\n');
969     if (!canJoinPhys(CP)) {
970       // Before giving up coalescing, if definition of source is defined by
971       // trivial computation, try rematerializing it.
972       if (!CP.isFlipped() &&
973           reMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()),
974                                   CP.getDstReg(), CopyMI))
975         return true;
976       return false;
977     }
978   } else {
979     DEBUG({
980       dbgs() << "\tConsidering merging to " << CP.getNewRC()->getName()
981              << " with ";
982       if (CP.getDstIdx() && CP.getSrcIdx())
983         dbgs() << PrintReg(CP.getDstReg()) << " in "
984                << TRI->getSubRegIndexName(CP.getDstIdx()) << " and "
985                << PrintReg(CP.getSrcReg()) << " in "
986                << TRI->getSubRegIndexName(CP.getSrcIdx()) << '\n';
987       else
988         dbgs() << PrintReg(CP.getSrcReg(), TRI) << " in "
989                << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n';
990     });
991
992     // When possible, let DstReg be the larger interval.
993     if (!CP.isPartial() && LIS->getInterval(CP.getSrcReg()).ranges.size() >
994                            LIS->getInterval(CP.getDstReg()).ranges.size())
995       CP.flip();
996   }
997
998   // Okay, attempt to join these two intervals.  On failure, this returns false.
999   // Otherwise, if one of the intervals being joined is a physreg, this method
1000   // always canonicalizes DstInt to be it.  The output "SrcInt" will not have
1001   // been modified, so we can use this information below to update aliases.
1002   if (!joinIntervals(CP)) {
1003     // Coalescing failed.
1004
1005     // If definition of source is defined by trivial computation, try
1006     // rematerializing it.
1007     if (!CP.isFlipped() &&
1008         reMaterializeTrivialDef(LIS->getInterval(CP.getSrcReg()),
1009                                 CP.getDstReg(), CopyMI))
1010       return true;
1011
1012     // If we can eliminate the copy without merging the live ranges, do so now.
1013     if (!CP.isPartial() && !CP.isPhys()) {
1014       if (adjustCopiesBackFrom(CP, CopyMI) ||
1015           removeCopyByCommutingDef(CP, CopyMI)) {
1016         LIS->RemoveMachineInstrFromMaps(CopyMI);
1017         CopyMI->eraseFromParent();
1018         DEBUG(dbgs() << "\tTrivial!\n");
1019         return true;
1020       }
1021     }
1022
1023     // Otherwise, we are unable to join the intervals.
1024     DEBUG(dbgs() << "\tInterference!\n");
1025     Again = true;  // May be possible to coalesce later.
1026     return false;
1027   }
1028
1029   // Coalescing to a virtual register that is of a sub-register class of the
1030   // other. Make sure the resulting register is set to the right register class.
1031   if (CP.isCrossClass()) {
1032     ++numCrossRCs;
1033     MRI->setRegClass(CP.getDstReg(), CP.getNewRC());
1034   }
1035
1036   // Removing sub-register copies can ease the register class constraints.
1037   // Make sure we attempt to inflate the register class of DstReg.
1038   if (!CP.isPhys() && RegClassInfo.isProperSubClass(CP.getNewRC()))
1039     InflateRegs.push_back(CP.getDstReg());
1040
1041   // CopyMI has been erased by joinIntervals at this point. Remove it from
1042   // ErasedInstrs since copyCoalesceWorkList() won't add a successful join back
1043   // to the work list. This keeps ErasedInstrs from growing needlessly.
1044   ErasedInstrs.erase(CopyMI);
1045
1046   // Rewrite all SrcReg operands to DstReg.
1047   // Also update DstReg operands to include DstIdx if it is set.
1048   if (CP.getDstIdx())
1049     updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx());
1050   updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx());
1051
1052   // SrcReg is guaranteed to be the register whose live interval that is
1053   // being merged.
1054   LIS->removeInterval(CP.getSrcReg());
1055
1056   // Update regalloc hint.
1057   TRI->UpdateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF);
1058
1059   DEBUG({
1060     dbgs() << "\tJoined. Result = " << PrintReg(CP.getDstReg(), TRI);
1061     if (!CP.isPhys())
1062       dbgs() << LIS->getInterval(CP.getDstReg());
1063      dbgs() << '\n';
1064   });
1065
1066   ++numJoins;
1067   return true;
1068 }
1069
1070 /// Attempt joining with a reserved physreg.
1071 bool RegisterCoalescer::joinReservedPhysReg(CoalescerPair &CP) {
1072   assert(CP.isPhys() && "Must be a physreg copy");
1073   assert(MRI->isReserved(CP.getDstReg()) && "Not a reserved register");
1074   LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1075   DEBUG(dbgs() << "\t\tRHS = " << PrintReg(CP.getSrcReg()) << ' ' << RHS
1076                << '\n');
1077
1078   assert(CP.isFlipped() && RHS.containsOneValue() &&
1079          "Invalid join with reserved register");
1080
1081   // Optimization for reserved registers like ESP. We can only merge with a
1082   // reserved physreg if RHS has a single value that is a copy of CP.DstReg().
1083   // The live range of the reserved register will look like a set of dead defs
1084   // - we don't properly track the live range of reserved registers.
1085
1086   // Deny any overlapping intervals.  This depends on all the reserved
1087   // register live ranges to look like dead defs.
1088   for (MCRegUnitIterator UI(CP.getDstReg(), TRI); UI.isValid(); ++UI)
1089     if (RHS.overlaps(LIS->getRegUnit(*UI))) {
1090       DEBUG(dbgs() << "\t\tInterference: " << PrintRegUnit(*UI, TRI) << '\n');
1091       return false;
1092     }
1093
1094   // Skip any value computations, we are not adding new values to the
1095   // reserved register.  Also skip merging the live ranges, the reserved
1096   // register live range doesn't need to be accurate as long as all the
1097   // defs are there.
1098
1099   // Delete the identity copy.
1100   MachineInstr *CopyMI = MRI->getVRegDef(RHS.reg);
1101   LIS->RemoveMachineInstrFromMaps(CopyMI);
1102   CopyMI->eraseFromParent();
1103
1104   // We don't track kills for reserved registers.
1105   MRI->clearKillFlags(CP.getSrcReg());
1106
1107   return true;
1108 }
1109
1110 //===----------------------------------------------------------------------===//
1111 //                 Interference checking and interval joining
1112 //===----------------------------------------------------------------------===//
1113 //
1114 // In the easiest case, the two live ranges being joined are disjoint, and
1115 // there is no interference to consider. It is quite common, though, to have
1116 // overlapping live ranges, and we need to check if the interference can be
1117 // resolved.
1118 //
1119 // The live range of a single SSA value forms a sub-tree of the dominator tree.
1120 // This means that two SSA values overlap if and only if the def of one value
1121 // is contained in the live range of the other value. As a special case, the
1122 // overlapping values can be defined at the same index.
1123 //
1124 // The interference from an overlapping def can be resolved in these cases:
1125 //
1126 // 1. Coalescable copies. The value is defined by a copy that would become an
1127 //    identity copy after joining SrcReg and DstReg. The copy instruction will
1128 //    be removed, and the value will be merged with the source value.
1129 //
1130 //    There can be several copies back and forth, causing many values to be
1131 //    merged into one. We compute a list of ultimate values in the joined live
1132 //    range as well as a mappings from the old value numbers.
1133 //
1134 // 2. IMPLICIT_DEF. This instruction is only inserted to ensure all PHI
1135 //    predecessors have a live out value. It doesn't cause real interference,
1136 //    and can be merged into the value it overlaps. Like a coalescable copy, it
1137 //    can be erased after joining.
1138 //
1139 // 3. Copy of external value. The overlapping def may be a copy of a value that
1140 //    is already in the other register. This is like a coalescable copy, but
1141 //    the live range of the source register must be trimmed after erasing the
1142 //    copy instruction:
1143 //
1144 //      %src = COPY %ext
1145 //      %dst = COPY %ext  <-- Remove this COPY, trim the live range of %ext.
1146 //
1147 // 4. Clobbering undefined lanes. Vector registers are sometimes built by
1148 //    defining one lane at a time:
1149 //
1150 //      %dst:ssub0<def,read-undef> = FOO
1151 //      %src = BAR
1152 //      %dst:ssub1<def> = COPY %src
1153 //
1154 //    The live range of %src overlaps the %dst value defined by FOO, but
1155 //    merging %src into %dst:ssub1 is only going to clobber the ssub1 lane
1156 //    which was undef anyway.
1157 //
1158 //    The value mapping is more complicated in this case. The final live range
1159 //    will have different value numbers for both FOO and BAR, but there is no
1160 //    simple mapping from old to new values. It may even be necessary to add
1161 //    new PHI values.
1162 //
1163 // 5. Clobbering dead lanes. A def may clobber a lane of a vector register that
1164 //    is live, but never read. This can happen because we don't compute
1165 //    individual live ranges per lane.
1166 //
1167 //      %dst<def> = FOO
1168 //      %src = BAR
1169 //      %dst:ssub1<def> = COPY %src
1170 //
1171 //    This kind of interference is only resolved locally. If the clobbered
1172 //    lane value escapes the block, the join is aborted.
1173
1174 namespace {
1175 /// Track information about values in a single virtual register about to be
1176 /// joined. Objects of this class are always created in pairs - one for each
1177 /// side of the CoalescerPair.
1178 class JoinVals {
1179   LiveInterval &LI;
1180
1181   // Location of this register in the final joined register.
1182   // Either CP.DstIdx or CP.SrcIdx.
1183   unsigned SubIdx;
1184
1185   // Values that will be present in the final live range.
1186   SmallVectorImpl<VNInfo*> &NewVNInfo;
1187
1188   const CoalescerPair &CP;
1189   LiveIntervals *LIS;
1190   SlotIndexes *Indexes;
1191   const TargetRegisterInfo *TRI;
1192
1193   // Value number assignments. Maps value numbers in LI to entries in NewVNInfo.
1194   // This is suitable for passing to LiveInterval::join().
1195   SmallVector<int, 8> Assignments;
1196
1197   // Conflict resolution for overlapping values.
1198   enum ConflictResolution {
1199     // No overlap, simply keep this value.
1200     CR_Keep,
1201
1202     // Merge this value into OtherVNI and erase the defining instruction.
1203     // Used for IMPLICIT_DEF, coalescable copies, and copies from external
1204     // values.
1205     CR_Erase,
1206
1207     // Merge this value into OtherVNI but keep the defining instruction.
1208     // This is for the special case where OtherVNI is defined by the same
1209     // instruction.
1210     CR_Merge,
1211
1212     // Keep this value, and have it replace OtherVNI where possible. This
1213     // complicates value mapping since OtherVNI maps to two different values
1214     // before and after this def.
1215     // Used when clobbering undefined or dead lanes.
1216     CR_Replace,
1217
1218     // Unresolved conflict. Visit later when all values have been mapped.
1219     CR_Unresolved,
1220
1221     // Unresolvable conflict. Abort the join.
1222     CR_Impossible
1223   };
1224
1225   // Per-value info for LI. The lane bit masks are all relative to the final
1226   // joined register, so they can be compared directly between SrcReg and
1227   // DstReg.
1228   struct Val {
1229     ConflictResolution Resolution;
1230
1231     // Lanes written by this def, 0 for unanalyzed values.
1232     unsigned WriteLanes;
1233
1234     // Lanes with defined values in this register. Other lanes are undef and
1235     // safe to clobber.
1236     unsigned ValidLanes;
1237
1238     // Value in LI being redefined by this def.
1239     VNInfo *RedefVNI;
1240
1241     // Value in the other live range that overlaps this def, if any.
1242     VNInfo *OtherVNI;
1243
1244     // Is this value an IMPLICIT_DEF?
1245     bool IsImplicitDef;
1246
1247     // True when the live range of this value will be pruned because of an
1248     // overlapping CR_Replace value in the other live range.
1249     bool Pruned;
1250
1251     // True once Pruned above has been computed.
1252     bool PrunedComputed;
1253
1254     Val() : Resolution(CR_Keep), WriteLanes(0), ValidLanes(0),
1255             RedefVNI(0), OtherVNI(0), IsImplicitDef(false), Pruned(false),
1256             PrunedComputed(false) {}
1257
1258     bool isAnalyzed() const { return WriteLanes != 0; }
1259   };
1260
1261   // One entry per value number in LI.
1262   SmallVector<Val, 8> Vals;
1263
1264   unsigned computeWriteLanes(const MachineInstr *DefMI, bool &Redef);
1265   VNInfo *stripCopies(VNInfo *VNI);
1266   ConflictResolution analyzeValue(unsigned ValNo, JoinVals &Other);
1267   void computeAssignment(unsigned ValNo, JoinVals &Other);
1268   bool taintExtent(unsigned, unsigned, JoinVals&,
1269                    SmallVectorImpl<std::pair<SlotIndex, unsigned> >&);
1270   bool usesLanes(MachineInstr *MI, unsigned, unsigned, unsigned);
1271   bool isPrunedValue(unsigned ValNo, JoinVals &Other);
1272
1273 public:
1274   JoinVals(LiveInterval &li, unsigned subIdx,
1275            SmallVectorImpl<VNInfo*> &newVNInfo,
1276            const CoalescerPair &cp,
1277            LiveIntervals *lis,
1278            const TargetRegisterInfo *tri)
1279     : LI(li), SubIdx(subIdx), NewVNInfo(newVNInfo), CP(cp), LIS(lis),
1280       Indexes(LIS->getSlotIndexes()), TRI(tri),
1281       Assignments(LI.getNumValNums(), -1), Vals(LI.getNumValNums())
1282   {}
1283
1284   /// Analyze defs in LI and compute a value mapping in NewVNInfo.
1285   /// Returns false if any conflicts were impossible to resolve.
1286   bool mapValues(JoinVals &Other);
1287
1288   /// Try to resolve conflicts that require all values to be mapped.
1289   /// Returns false if any conflicts were impossible to resolve.
1290   bool resolveConflicts(JoinVals &Other);
1291
1292   /// Prune the live range of values in Other.LI where they would conflict with
1293   /// CR_Replace values in LI. Collect end points for restoring the live range
1294   /// after joining.
1295   void pruneValues(JoinVals &Other, SmallVectorImpl<SlotIndex> &EndPoints);
1296
1297   /// Erase any machine instructions that have been coalesced away.
1298   /// Add erased instructions to ErasedInstrs.
1299   /// Add foreign virtual registers to ShrinkRegs if their live range ended at
1300   /// the erased instrs.
1301   void eraseInstrs(SmallPtrSet<MachineInstr*, 8> &ErasedInstrs,
1302                    SmallVectorImpl<unsigned> &ShrinkRegs);
1303
1304   /// Get the value assignments suitable for passing to LiveInterval::join.
1305   const int *getAssignments() const { return Assignments.data(); }
1306 };
1307 } // end anonymous namespace
1308
1309 /// Compute the bitmask of lanes actually written by DefMI.
1310 /// Set Redef if there are any partial register definitions that depend on the
1311 /// previous value of the register.
1312 unsigned JoinVals::computeWriteLanes(const MachineInstr *DefMI, bool &Redef) {
1313   unsigned L = 0;
1314   for (ConstMIOperands MO(DefMI); MO.isValid(); ++MO) {
1315     if (!MO->isReg() || MO->getReg() != LI.reg || !MO->isDef())
1316       continue;
1317     L |= TRI->getSubRegIndexLaneMask(compose(*TRI, SubIdx, MO->getSubReg()));
1318     if (MO->readsReg())
1319       Redef = true;
1320   }
1321   return L;
1322 }
1323
1324 /// Find the ultimate value that VNI was copied from.
1325 VNInfo *JoinVals::stripCopies(VNInfo *VNI) {
1326   while (!VNI->isPHIDef()) {
1327     MachineInstr *MI = Indexes->getInstructionFromIndex(VNI->def);
1328     assert(MI && "No defining instruction");
1329     if (!MI->isFullCopy())
1330       break;
1331     unsigned Reg = MI->getOperand(1).getReg();
1332     if (!TargetRegisterInfo::isVirtualRegister(Reg))
1333       break;
1334     LiveRangeQuery LRQ(LIS->getInterval(Reg), VNI->def);
1335     if (!LRQ.valueIn())
1336       break;
1337     VNI = LRQ.valueIn();
1338   }
1339   return VNI;
1340 }
1341
1342 /// Analyze ValNo in this live range, and set all fields of Vals[ValNo].
1343 /// Return a conflict resolution when possible, but leave the hard cases as
1344 /// CR_Unresolved.
1345 /// Recursively calls computeAssignment() on this and Other, guaranteeing that
1346 /// both OtherVNI and RedefVNI have been analyzed and mapped before returning.
1347 /// The recursion always goes upwards in the dominator tree, making loops
1348 /// impossible.
1349 JoinVals::ConflictResolution
1350 JoinVals::analyzeValue(unsigned ValNo, JoinVals &Other) {
1351   Val &V = Vals[ValNo];
1352   assert(!V.isAnalyzed() && "Value has already been analyzed!");
1353   VNInfo *VNI = LI.getValNumInfo(ValNo);
1354   if (VNI->isUnused()) {
1355     V.WriteLanes = ~0u;
1356     return CR_Keep;
1357   }
1358
1359   // Get the instruction defining this value, compute the lanes written.
1360   const MachineInstr *DefMI = 0;
1361   if (VNI->isPHIDef()) {
1362     // Conservatively assume that all lanes in a PHI are valid.
1363     V.ValidLanes = V.WriteLanes = TRI->getSubRegIndexLaneMask(SubIdx);
1364   } else {
1365     DefMI = Indexes->getInstructionFromIndex(VNI->def);
1366     bool Redef = false;
1367     V.ValidLanes = V.WriteLanes = computeWriteLanes(DefMI, Redef);
1368
1369     // If this is a read-modify-write instruction, there may be more valid
1370     // lanes than the ones written by this instruction.
1371     // This only covers partial redef operands. DefMI may have normal use
1372     // operands reading the register. They don't contribute valid lanes.
1373     //
1374     // This adds ssub1 to the set of valid lanes in %src:
1375     //
1376     //   %src:ssub1<def> = FOO
1377     //
1378     // This leaves only ssub1 valid, making any other lanes undef:
1379     //
1380     //   %src:ssub1<def,read-undef> = FOO %src:ssub2
1381     //
1382     // The <read-undef> flag on the def operand means that old lane values are
1383     // not important.
1384     if (Redef) {
1385       V.RedefVNI = LiveRangeQuery(LI, VNI->def).valueIn();
1386       assert(V.RedefVNI && "Instruction is reading nonexistent value");
1387       computeAssignment(V.RedefVNI->id, Other);
1388       V.ValidLanes |= Vals[V.RedefVNI->id].ValidLanes;
1389     }
1390
1391     // An IMPLICIT_DEF writes undef values.
1392     if (DefMI->isImplicitDef()) {
1393       V.IsImplicitDef = true;
1394       V.ValidLanes &= ~V.WriteLanes;
1395     }
1396   }
1397
1398   // Find the value in Other that overlaps VNI->def, if any.
1399   LiveRangeQuery OtherLRQ(Other.LI, VNI->def);
1400
1401   // It is possible that both values are defined by the same instruction, or
1402   // the values are PHIs defined in the same block. When that happens, the two
1403   // values should be merged into one, but not into any preceding value.
1404   // The first value defined or visited gets CR_Keep, the other gets CR_Merge.
1405   if (VNInfo *OtherVNI = OtherLRQ.valueDefined()) {
1406     assert(SlotIndex::isSameInstr(VNI->def, OtherVNI->def) && "Broken LRQ");
1407
1408     // One value stays, the other is merged. Keep the earlier one, or the first
1409     // one we see.
1410     if (OtherVNI->def < VNI->def)
1411       Other.computeAssignment(OtherVNI->id, *this);
1412     else if (VNI->def < OtherVNI->def && OtherLRQ.valueIn()) {
1413       // This is an early-clobber def overlapping a live-in value in the other
1414       // register. Not mergeable.
1415       V.OtherVNI = OtherLRQ.valueIn();
1416       return CR_Impossible;
1417     }
1418     V.OtherVNI = OtherVNI;
1419     Val &OtherV = Other.Vals[OtherVNI->id];
1420     // Keep this value, check for conflicts when analyzing OtherVNI.
1421     if (!OtherV.isAnalyzed())
1422       return CR_Keep;
1423     // Both sides have been analyzed now.
1424     // Allow overlapping PHI values. Any real interference would show up in a
1425     // predecessor, the PHI itself can't introduce any conflicts.
1426     if (VNI->isPHIDef())
1427       return CR_Merge;
1428     if (V.ValidLanes & OtherV.ValidLanes)
1429       // Overlapping lanes can't be resolved.
1430       return CR_Impossible;
1431     else
1432       return CR_Merge;
1433   }
1434
1435   // No simultaneous def. Is Other live at the def?
1436   V.OtherVNI = OtherLRQ.valueIn();
1437   if (!V.OtherVNI)
1438     // No overlap, no conflict.
1439     return CR_Keep;
1440
1441   assert(!SlotIndex::isSameInstr(VNI->def, V.OtherVNI->def) && "Broken LRQ");
1442
1443   // We have overlapping values, or possibly a kill of Other.
1444   // Recursively compute assignments up the dominator tree.
1445   Other.computeAssignment(V.OtherVNI->id, *this);
1446   const Val &OtherV = Other.Vals[V.OtherVNI->id];
1447
1448   // Allow overlapping PHI values. Any real interference would show up in a
1449   // predecessor, the PHI itself can't introduce any conflicts.
1450   if (VNI->isPHIDef())
1451     return CR_Replace;
1452
1453   // Check for simple erasable conflicts.
1454   if (DefMI->isImplicitDef())
1455     return CR_Erase;
1456
1457   // Include the non-conflict where DefMI is a coalescable copy that kills
1458   // OtherVNI. We still want the copy erased and value numbers merged.
1459   if (CP.isCoalescable(DefMI)) {
1460     // Some of the lanes copied from OtherVNI may be undef, making them undef
1461     // here too.
1462     V.ValidLanes &= ~V.WriteLanes | OtherV.ValidLanes;
1463     return CR_Erase;
1464   }
1465
1466   // This may not be a real conflict if DefMI simply kills Other and defines
1467   // VNI.
1468   if (OtherLRQ.isKill() && OtherLRQ.endPoint() <= VNI->def)
1469     return CR_Keep;
1470
1471   // Handle the case where VNI and OtherVNI can be proven to be identical:
1472   //
1473   //   %other = COPY %ext
1474   //   %this  = COPY %ext <-- Erase this copy
1475   //
1476   if (DefMI->isFullCopy() && !CP.isPartial() &&
1477       stripCopies(VNI) == stripCopies(V.OtherVNI))
1478     return CR_Erase;
1479
1480   // If the lanes written by this instruction were all undef in OtherVNI, it is
1481   // still safe to join the live ranges. This can't be done with a simple value
1482   // mapping, though - OtherVNI will map to multiple values:
1483   //
1484   //   1 %dst:ssub0 = FOO                <-- OtherVNI
1485   //   2 %src = BAR                      <-- VNI
1486   //   3 %dst:ssub1 = COPY %src<kill>    <-- Eliminate this copy.
1487   //   4 BAZ %dst<kill>
1488   //   5 QUUX %src<kill>
1489   //
1490   // Here OtherVNI will map to itself in [1;2), but to VNI in [2;5). CR_Replace
1491   // handles this complex value mapping.
1492   if ((V.WriteLanes & OtherV.ValidLanes) == 0)
1493     return CR_Replace;
1494
1495   // If the other live range is killed by DefMI and the live ranges are still
1496   // overlapping, it must be because we're looking at an early clobber def:
1497   //
1498   //   %dst<def,early-clobber> = ASM %src<kill>
1499   //
1500   // In this case, it is illegal to merge the two live ranges since the early
1501   // clobber def would clobber %src before it was read.
1502   if (OtherLRQ.isKill()) {
1503     // This case where the def doesn't overlap the kill is handled above.
1504     assert(VNI->def.isEarlyClobber() &&
1505            "Only early clobber defs can overlap a kill");
1506     return CR_Impossible;
1507   }
1508
1509   // VNI is clobbering live lanes in OtherVNI, but there is still the
1510   // possibility that no instructions actually read the clobbered lanes.
1511   // If we're clobbering all the lanes in OtherVNI, at least one must be read.
1512   // Otherwise Other.LI wouldn't be live here.
1513   if ((TRI->getSubRegIndexLaneMask(Other.SubIdx) & ~V.WriteLanes) == 0)
1514     return CR_Impossible;
1515
1516   // We need to verify that no instructions are reading the clobbered lanes. To
1517   // save compile time, we'll only check that locally. Don't allow the tainted
1518   // value to escape the basic block.
1519   MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
1520   if (OtherLRQ.endPoint() >= Indexes->getMBBEndIdx(MBB))
1521     return CR_Impossible;
1522
1523   // There are still some things that could go wrong besides clobbered lanes
1524   // being read, for example OtherVNI may be only partially redefined in MBB,
1525   // and some clobbered lanes could escape the block. Save this analysis for
1526   // resolveConflicts() when all values have been mapped. We need to know
1527   // RedefVNI and WriteLanes for any later defs in MBB, and we can't compute
1528   // that now - the recursive analyzeValue() calls must go upwards in the
1529   // dominator tree.
1530   return CR_Unresolved;
1531 }
1532
1533 /// Compute the value assignment for ValNo in LI.
1534 /// This may be called recursively by analyzeValue(), but never for a ValNo on
1535 /// the stack.
1536 void JoinVals::computeAssignment(unsigned ValNo, JoinVals &Other) {
1537   Val &V = Vals[ValNo];
1538   if (V.isAnalyzed()) {
1539     // Recursion should always move up the dominator tree, so ValNo is not
1540     // supposed to reappear before it has been assigned.
1541     assert(Assignments[ValNo] != -1 && "Bad recursion?");
1542     return;
1543   }
1544   switch ((V.Resolution = analyzeValue(ValNo, Other))) {
1545   case CR_Erase:
1546   case CR_Merge:
1547     // Merge this ValNo into OtherVNI.
1548     assert(V.OtherVNI && "OtherVNI not assigned, can't merge.");
1549     assert(Other.Vals[V.OtherVNI->id].isAnalyzed() && "Missing recursion");
1550     Assignments[ValNo] = Other.Assignments[V.OtherVNI->id];
1551     DEBUG(dbgs() << "\t\tmerge " << PrintReg(LI.reg) << ':' << ValNo << '@'
1552                  << LI.getValNumInfo(ValNo)->def << " into "
1553                  << PrintReg(Other.LI.reg) << ':' << V.OtherVNI->id << '@'
1554                  << V.OtherVNI->def << " --> @"
1555                  << NewVNInfo[Assignments[ValNo]]->def << '\n');
1556     break;
1557   case CR_Replace:
1558   case CR_Unresolved:
1559     // The other value is going to be pruned if this join is successful.
1560     assert(V.OtherVNI && "OtherVNI not assigned, can't prune");
1561     Other.Vals[V.OtherVNI->id].Pruned = true;
1562     // Fall through.
1563   default:
1564     // This value number needs to go in the final joined live range.
1565     Assignments[ValNo] = NewVNInfo.size();
1566     NewVNInfo.push_back(LI.getValNumInfo(ValNo));
1567     break;
1568   }
1569 }
1570
1571 bool JoinVals::mapValues(JoinVals &Other) {
1572   for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) {
1573     computeAssignment(i, Other);
1574     if (Vals[i].Resolution == CR_Impossible) {
1575       DEBUG(dbgs() << "\t\tinterference at " << PrintReg(LI.reg) << ':' << i
1576                    << '@' << LI.getValNumInfo(i)->def << '\n');
1577       return false;
1578     }
1579   }
1580   return true;
1581 }
1582
1583 /// Assuming ValNo is going to clobber some valid lanes in Other.LI, compute
1584 /// the extent of the tainted lanes in the block.
1585 ///
1586 /// Multiple values in Other.LI can be affected since partial redefinitions can
1587 /// preserve previously tainted lanes.
1588 ///
1589 ///   1 %dst = VLOAD           <-- Define all lanes in %dst
1590 ///   2 %src = FOO             <-- ValNo to be joined with %dst:ssub0
1591 ///   3 %dst:ssub1 = BAR       <-- Partial redef doesn't clear taint in ssub0
1592 ///   4 %dst:ssub0 = COPY %src <-- Conflict resolved, ssub0 wasn't read
1593 ///
1594 /// For each ValNo in Other that is affected, add an (EndIndex, TaintedLanes)
1595 /// entry to TaintedVals.
1596 ///
1597 /// Returns false if the tainted lanes extend beyond the basic block.
1598 bool JoinVals::
1599 taintExtent(unsigned ValNo, unsigned TaintedLanes, JoinVals &Other,
1600             SmallVectorImpl<std::pair<SlotIndex, unsigned> > &TaintExtent) {
1601   VNInfo *VNI = LI.getValNumInfo(ValNo);
1602   MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
1603   SlotIndex MBBEnd = Indexes->getMBBEndIdx(MBB);
1604
1605   // Scan Other.LI from VNI.def to MBBEnd.
1606   LiveInterval::iterator OtherI = Other.LI.find(VNI->def);
1607   assert(OtherI != Other.LI.end() && "No conflict?");
1608   do {
1609     // OtherI is pointing to a tainted value. Abort the join if the tainted
1610     // lanes escape the block.
1611     SlotIndex End = OtherI->end;
1612     if (End >= MBBEnd) {
1613       DEBUG(dbgs() << "\t\ttaints global " << PrintReg(Other.LI.reg) << ':'
1614                    << OtherI->valno->id << '@' << OtherI->start << '\n');
1615       return false;
1616     }
1617     DEBUG(dbgs() << "\t\ttaints local " << PrintReg(Other.LI.reg) << ':'
1618                  << OtherI->valno->id << '@' << OtherI->start
1619                  << " to " << End << '\n');
1620     // A dead def is not a problem.
1621     if (End.isDead())
1622       break;
1623     TaintExtent.push_back(std::make_pair(End, TaintedLanes));
1624
1625     // Check for another def in the MBB.
1626     if (++OtherI == Other.LI.end() || OtherI->start >= MBBEnd)
1627       break;
1628
1629     // Lanes written by the new def are no longer tainted.
1630     const Val &OV = Other.Vals[OtherI->valno->id];
1631     TaintedLanes &= ~OV.WriteLanes;
1632     if (!OV.RedefVNI)
1633       break;
1634   } while (TaintedLanes);
1635   return true;
1636 }
1637
1638 /// Return true if MI uses any of the given Lanes from Reg.
1639 /// This does not include partial redefinitions of Reg.
1640 bool JoinVals::usesLanes(MachineInstr *MI, unsigned Reg, unsigned SubIdx,
1641                          unsigned Lanes) {
1642   if (MI->isDebugValue())
1643     return false;
1644   for (ConstMIOperands MO(MI); MO.isValid(); ++MO) {
1645     if (!MO->isReg() || MO->isDef() || MO->getReg() != Reg)
1646       continue;
1647     if (!MO->readsReg())
1648       continue;
1649     if (Lanes &
1650         TRI->getSubRegIndexLaneMask(compose(*TRI, SubIdx, MO->getSubReg())))
1651       return true;
1652   }
1653   return false;
1654 }
1655
1656 bool JoinVals::resolveConflicts(JoinVals &Other) {
1657   for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) {
1658     Val &V = Vals[i];
1659     assert (V.Resolution != CR_Impossible && "Unresolvable conflict");
1660     if (V.Resolution != CR_Unresolved)
1661       continue;
1662     DEBUG(dbgs() << "\t\tconflict at " << PrintReg(LI.reg) << ':' << i
1663                  << '@' << LI.getValNumInfo(i)->def << '\n');
1664     ++NumLaneConflicts;
1665     assert(V.OtherVNI && "Inconsistent conflict resolution.");
1666     VNInfo *VNI = LI.getValNumInfo(i);
1667     const Val &OtherV = Other.Vals[V.OtherVNI->id];
1668
1669     // VNI is known to clobber some lanes in OtherVNI. If we go ahead with the
1670     // join, those lanes will be tainted with a wrong value. Get the extent of
1671     // the tainted lanes.
1672     unsigned TaintedLanes = V.WriteLanes & OtherV.ValidLanes;
1673     SmallVector<std::pair<SlotIndex, unsigned>, 8> TaintExtent;
1674     if (!taintExtent(i, TaintedLanes, Other, TaintExtent))
1675       // Tainted lanes would extend beyond the basic block.
1676       return false;
1677
1678     assert(!TaintExtent.empty() && "There should be at least one conflict.");
1679
1680     // Now look at the instructions from VNI->def to TaintExtent (inclusive).
1681     MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def);
1682     MachineBasicBlock::iterator MI = MBB->begin();
1683     if (!VNI->isPHIDef()) {
1684       MI = Indexes->getInstructionFromIndex(VNI->def);
1685       // No need to check the instruction defining VNI for reads.
1686       ++MI;
1687     }
1688     assert(!SlotIndex::isSameInstr(VNI->def, TaintExtent.front().first) &&
1689            "Interference ends on VNI->def. Should have been handled earlier");
1690     MachineInstr *LastMI =
1691       Indexes->getInstructionFromIndex(TaintExtent.front().first);
1692     assert(LastMI && "Range must end at a proper instruction");
1693     unsigned TaintNum = 0;
1694     for(;;) {
1695       assert(MI != MBB->end() && "Bad LastMI");
1696       if (usesLanes(MI, Other.LI.reg, Other.SubIdx, TaintedLanes)) {
1697         DEBUG(dbgs() << "\t\ttainted lanes used by: " << *MI);
1698         return false;
1699       }
1700       // LastMI is the last instruction to use the current value.
1701       if (&*MI == LastMI) {
1702         if (++TaintNum == TaintExtent.size())
1703           break;
1704         LastMI = Indexes->getInstructionFromIndex(TaintExtent[TaintNum].first);
1705         assert(LastMI && "Range must end at a proper instruction");
1706         TaintedLanes = TaintExtent[TaintNum].second;
1707       }
1708       ++MI;
1709     }
1710
1711     // The tainted lanes are unused.
1712     V.Resolution = CR_Replace;
1713     ++NumLaneResolves;
1714   }
1715   return true;
1716 }
1717
1718 // Determine if ValNo is a copy of a value number in LI or Other.LI that will
1719 // be pruned:
1720 //
1721 //   %dst = COPY %src
1722 //   %src = COPY %dst  <-- This value to be pruned.
1723 //   %dst = COPY %src  <-- This value is a copy of a pruned value.
1724 //
1725 bool JoinVals::isPrunedValue(unsigned ValNo, JoinVals &Other) {
1726   Val &V = Vals[ValNo];
1727   if (V.Pruned || V.PrunedComputed)
1728     return V.Pruned;
1729
1730   if (V.Resolution != CR_Erase && V.Resolution != CR_Merge)
1731     return V.Pruned;
1732
1733   // Follow copies up the dominator tree and check if any intermediate value
1734   // has been pruned.
1735   V.PrunedComputed = true;
1736   V.Pruned = Other.isPrunedValue(V.OtherVNI->id, *this);
1737   return V.Pruned;
1738 }
1739
1740 void JoinVals::pruneValues(JoinVals &Other,
1741                            SmallVectorImpl<SlotIndex> &EndPoints) {
1742   for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) {
1743     SlotIndex Def = LI.getValNumInfo(i)->def;
1744     switch (Vals[i].Resolution) {
1745     case CR_Keep:
1746       break;
1747     case CR_Replace: {
1748       // This value takes precedence over the value in Other.LI.
1749       LIS->pruneValue(&Other.LI, Def, &EndPoints);
1750       // Check if we're replacing an IMPLICIT_DEF value. The IMPLICIT_DEF
1751       // instructions are only inserted to provide a live-out value for PHI
1752       // predecessors, so the instruction should simply go away once its value
1753       // has been replaced.
1754       Val &OtherV = Other.Vals[Vals[i].OtherVNI->id];
1755       bool EraseImpDef = OtherV.IsImplicitDef && OtherV.Resolution == CR_Keep;
1756       if (!Def.isBlock()) {
1757         // Remove <def,read-undef> flags. This def is now a partial redef.
1758         // Also remove <def,dead> flags since the joined live range will
1759         // continue past this instruction.
1760         for (MIOperands MO(Indexes->getInstructionFromIndex(Def));
1761              MO.isValid(); ++MO)
1762           if (MO->isReg() && MO->isDef() && MO->getReg() == LI.reg) {
1763             MO->setIsUndef(EraseImpDef);
1764             MO->setIsDead(false);
1765           }
1766         // This value will reach instructions below, but we need to make sure
1767         // the live range also reaches the instruction at Def.
1768         if (!EraseImpDef)
1769           EndPoints.push_back(Def);
1770       }
1771       DEBUG(dbgs() << "\t\tpruned " << PrintReg(Other.LI.reg) << " at " << Def
1772                    << ": " << Other.LI << '\n');
1773       break;
1774     }
1775     case CR_Erase:
1776     case CR_Merge:
1777       if (isPrunedValue(i, Other)) {
1778         // This value is ultimately a copy of a pruned value in LI or Other.LI.
1779         // We can no longer trust the value mapping computed by
1780         // computeAssignment(), the value that was originally copied could have
1781         // been replaced.
1782         LIS->pruneValue(&LI, Def, &EndPoints);
1783         DEBUG(dbgs() << "\t\tpruned all of " << PrintReg(LI.reg) << " at "
1784                      << Def << ": " << LI << '\n');
1785       }
1786       break;
1787     case CR_Unresolved:
1788     case CR_Impossible:
1789       llvm_unreachable("Unresolved conflicts");
1790     }
1791   }
1792 }
1793
1794 void JoinVals::eraseInstrs(SmallPtrSet<MachineInstr*, 8> &ErasedInstrs,
1795                            SmallVectorImpl<unsigned> &ShrinkRegs) {
1796   for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) {
1797     // Get the def location before markUnused() below invalidates it.
1798     SlotIndex Def = LI.getValNumInfo(i)->def;
1799     switch (Vals[i].Resolution) {
1800     case CR_Keep:
1801       // If an IMPLICIT_DEF value is pruned, it doesn't serve a purpose any
1802       // longer. The IMPLICIT_DEF instructions are only inserted by
1803       // PHIElimination to guarantee that all PHI predecessors have a value.
1804       if (!Vals[i].IsImplicitDef || !Vals[i].Pruned)
1805         break;
1806       // Remove value number i from LI. Note that this VNInfo is still present
1807       // in NewVNInfo, so it will appear as an unused value number in the final
1808       // joined interval.
1809       LI.getValNumInfo(i)->markUnused();
1810       LI.removeValNo(LI.getValNumInfo(i));
1811       DEBUG(dbgs() << "\t\tremoved " << i << '@' << Def << ": " << LI << '\n');
1812       // FALL THROUGH.
1813
1814     case CR_Erase: {
1815       MachineInstr *MI = Indexes->getInstructionFromIndex(Def);
1816       assert(MI && "No instruction to erase");
1817       if (MI->isCopy()) {
1818         unsigned Reg = MI->getOperand(1).getReg();
1819         if (TargetRegisterInfo::isVirtualRegister(Reg) &&
1820             Reg != CP.getSrcReg() && Reg != CP.getDstReg())
1821           ShrinkRegs.push_back(Reg);
1822       }
1823       ErasedInstrs.insert(MI);
1824       DEBUG(dbgs() << "\t\terased:\t" << Def << '\t' << *MI);
1825       LIS->RemoveMachineInstrFromMaps(MI);
1826       MI->eraseFromParent();
1827       break;
1828     }
1829     default:
1830       break;
1831     }
1832   }
1833 }
1834
1835 bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) {
1836   SmallVector<VNInfo*, 16> NewVNInfo;
1837   LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1838   LiveInterval &LHS = LIS->getInterval(CP.getDstReg());
1839   JoinVals RHSVals(RHS, CP.getSrcIdx(), NewVNInfo, CP, LIS, TRI);
1840   JoinVals LHSVals(LHS, CP.getDstIdx(), NewVNInfo, CP, LIS, TRI);
1841
1842   DEBUG(dbgs() << "\t\tRHS = " << PrintReg(CP.getSrcReg()) << ' ' << RHS
1843                << "\n\t\tLHS = " << PrintReg(CP.getDstReg()) << ' ' << LHS
1844                << '\n');
1845
1846   // First compute NewVNInfo and the simple value mappings.
1847   // Detect impossible conflicts early.
1848   if (!LHSVals.mapValues(RHSVals) || !RHSVals.mapValues(LHSVals))
1849     return false;
1850
1851   // Some conflicts can only be resolved after all values have been mapped.
1852   if (!LHSVals.resolveConflicts(RHSVals) || !RHSVals.resolveConflicts(LHSVals))
1853     return false;
1854
1855   // All clear, the live ranges can be merged.
1856
1857   // The merging algorithm in LiveInterval::join() can't handle conflicting
1858   // value mappings, so we need to remove any live ranges that overlap a
1859   // CR_Replace resolution. Collect a set of end points that can be used to
1860   // restore the live range after joining.
1861   SmallVector<SlotIndex, 8> EndPoints;
1862   LHSVals.pruneValues(RHSVals, EndPoints);
1863   RHSVals.pruneValues(LHSVals, EndPoints);
1864
1865   // Erase COPY and IMPLICIT_DEF instructions. This may cause some external
1866   // registers to require trimming.
1867   SmallVector<unsigned, 8> ShrinkRegs;
1868   LHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs);
1869   RHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs);
1870   while (!ShrinkRegs.empty())
1871     LIS->shrinkToUses(&LIS->getInterval(ShrinkRegs.pop_back_val()));
1872
1873   // Join RHS into LHS.
1874   LHS.join(RHS, LHSVals.getAssignments(), RHSVals.getAssignments(), NewVNInfo,
1875            MRI);
1876
1877   // Kill flags are going to be wrong if the live ranges were overlapping.
1878   // Eventually, we should simply clear all kill flags when computing live
1879   // ranges. They are reinserted after register allocation.
1880   MRI->clearKillFlags(LHS.reg);
1881   MRI->clearKillFlags(RHS.reg);
1882
1883   if (EndPoints.empty())
1884     return true;
1885
1886   // Recompute the parts of the live range we had to remove because of
1887   // CR_Replace conflicts.
1888   DEBUG(dbgs() << "\t\trestoring liveness to " << EndPoints.size()
1889                << " points: " << LHS << '\n');
1890   LIS->extendToIndices(&LHS, EndPoints);
1891   return true;
1892 }
1893
1894 /// joinIntervals - Attempt to join these two intervals.  On failure, this
1895 /// returns false.
1896 bool RegisterCoalescer::joinIntervals(CoalescerPair &CP) {
1897   return CP.isPhys() ? joinReservedPhysReg(CP) : joinVirtRegs(CP);
1898 }
1899
1900 namespace {
1901   // DepthMBBCompare - Comparison predicate that sort first based on the loop
1902   // depth of the basic block (the unsigned), and then on the MBB number.
1903   struct DepthMBBCompare {
1904     typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
1905     bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
1906       // Deeper loops first
1907       if (LHS.first != RHS.first)
1908         return LHS.first > RHS.first;
1909
1910       // Prefer blocks that are more connected in the CFG. This takes care of
1911       // the most difficult copies first while intervals are short.
1912       unsigned cl = LHS.second->pred_size() + LHS.second->succ_size();
1913       unsigned cr = RHS.second->pred_size() + RHS.second->succ_size();
1914       if (cl != cr)
1915         return cl > cr;
1916
1917       // As a last resort, sort by block number.
1918       return LHS.second->getNumber() < RHS.second->getNumber();
1919     }
1920   };
1921 }
1922
1923 // Try joining WorkList copies starting from index From.
1924 // Null out any successful joins.
1925 bool RegisterCoalescer::copyCoalesceWorkList(unsigned From) {
1926   assert(From <= WorkList.size() && "Out of range");
1927   bool Progress = false;
1928   for (unsigned i = From, e = WorkList.size(); i != e; ++i) {
1929     if (!WorkList[i])
1930       continue;
1931     // Skip instruction pointers that have already been erased, for example by
1932     // dead code elimination.
1933     if (ErasedInstrs.erase(WorkList[i])) {
1934       WorkList[i] = 0;
1935       continue;
1936     }
1937     bool Again = false;
1938     bool Success = joinCopy(WorkList[i], Again);
1939     Progress |= Success;
1940     if (Success || !Again)
1941       WorkList[i] = 0;
1942   }
1943   return Progress;
1944 }
1945
1946 void
1947 RegisterCoalescer::copyCoalesceInMBB(MachineBasicBlock *MBB) {
1948   DEBUG(dbgs() << MBB->getName() << ":\n");
1949
1950   // Collect all copy-like instructions in MBB. Don't start coalescing anything
1951   // yet, it might invalidate the iterator.
1952   const unsigned PrevSize = WorkList.size();
1953   for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
1954        MII != E; ++MII)
1955     if (MII->isCopyLike())
1956       WorkList.push_back(MII);
1957
1958   // Try coalescing the collected copies immediately, and remove the nulls.
1959   // This prevents the WorkList from getting too large since most copies are
1960   // joinable on the first attempt.
1961   if (copyCoalesceWorkList(PrevSize))
1962     WorkList.erase(std::remove(WorkList.begin() + PrevSize, WorkList.end(),
1963                                (MachineInstr*)0), WorkList.end());
1964 }
1965
1966 void RegisterCoalescer::joinAllIntervals() {
1967   DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n");
1968   assert(WorkList.empty() && "Old data still around.");
1969
1970   if (Loops->empty()) {
1971     // If there are no loops in the function, join intervals in function order.
1972     for (MachineFunction::iterator I = MF->begin(), E = MF->end();
1973          I != E; ++I)
1974       copyCoalesceInMBB(I);
1975   } else {
1976     // Otherwise, join intervals in inner loops before other intervals.
1977     // Unfortunately we can't just iterate over loop hierarchy here because
1978     // there may be more MBB's than BB's.  Collect MBB's for sorting.
1979
1980     // Join intervals in the function prolog first. We want to join physical
1981     // registers with virtual registers before the intervals got too long.
1982     std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
1983     for (MachineFunction::iterator I = MF->begin(), E = MF->end();I != E;++I){
1984       MachineBasicBlock *MBB = I;
1985       MBBs.push_back(std::make_pair(Loops->getLoopDepth(MBB), I));
1986     }
1987
1988     // Sort by loop depth.
1989     std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
1990
1991     // Finally, join intervals in loop nest order.
1992     for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
1993       copyCoalesceInMBB(MBBs[i].second);
1994   }
1995
1996   // Joining intervals can allow other intervals to be joined.  Iteratively join
1997   // until we make no progress.
1998   while (copyCoalesceWorkList())
1999     /* empty */ ;
2000 }
2001
2002 void RegisterCoalescer::releaseMemory() {
2003   ErasedInstrs.clear();
2004   WorkList.clear();
2005   DeadDefs.clear();
2006   InflateRegs.clear();
2007 }
2008
2009 bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
2010   MF = &fn;
2011   MRI = &fn.getRegInfo();
2012   TM = &fn.getTarget();
2013   TRI = TM->getRegisterInfo();
2014   TII = TM->getInstrInfo();
2015   LIS = &getAnalysis<LiveIntervals>();
2016   LDV = &getAnalysis<LiveDebugVariables>();
2017   AA = &getAnalysis<AliasAnalysis>();
2018   Loops = &getAnalysis<MachineLoopInfo>();
2019
2020   DEBUG(dbgs() << "********** SIMPLE REGISTER COALESCING **********\n"
2021                << "********** Function: " << MF->getName() << '\n');
2022
2023   if (VerifyCoalescing)
2024     MF->verify(this, "Before register coalescing");
2025
2026   RegClassInfo.runOnMachineFunction(fn);
2027
2028   // Join (coalesce) intervals if requested.
2029   if (EnableJoining)
2030     joinAllIntervals();
2031
2032   // After deleting a lot of copies, register classes may be less constrained.
2033   // Removing sub-register operands may allow GR32_ABCD -> GR32 and DPR_VFP2 ->
2034   // DPR inflation.
2035   array_pod_sort(InflateRegs.begin(), InflateRegs.end());
2036   InflateRegs.erase(std::unique(InflateRegs.begin(), InflateRegs.end()),
2037                     InflateRegs.end());
2038   DEBUG(dbgs() << "Trying to inflate " << InflateRegs.size() << " regs.\n");
2039   for (unsigned i = 0, e = InflateRegs.size(); i != e; ++i) {
2040     unsigned Reg = InflateRegs[i];
2041     if (MRI->reg_nodbg_empty(Reg))
2042       continue;
2043     if (MRI->recomputeRegClass(Reg, *TM)) {
2044       DEBUG(dbgs() << PrintReg(Reg) << " inflated to "
2045                    << MRI->getRegClass(Reg)->getName() << '\n');
2046       ++NumInflated;
2047     }
2048   }
2049
2050   DEBUG(dump());
2051   DEBUG(LDV->dump());
2052   if (VerifyCoalescing)
2053     MF->verify(this, "After register coalescing");
2054   return true;
2055 }
2056
2057 /// print - Implement the dump method.
2058 void RegisterCoalescer::print(raw_ostream &O, const Module* m) const {
2059    LIS->print(O, m);
2060 }