1 //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "regcoalescing"
17 #include "RegisterCoalescer.h"
18 #include "LiveDebugVariables.h"
19 #include "RegisterClassInfo.h"
20 #include "VirtRegMap.h"
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"
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");
61 EnableJoining("join-liveintervals",
62 cl::desc("Coalesce copies (default=true)"),
66 DisableCrossClassJoin("disable-cross-class-join",
67 cl::desc("Avoid coalescing cross register class copies"),
68 cl::init(false), cl::Hidden);
71 EnablePhysicalJoin("join-physregs",
72 cl::desc("Join physical register copies"),
73 cl::init(false), cl::Hidden);
76 VerifyCoalescing("verify-coalescing",
77 cl::desc("Verify machine instrs before and after register coalescing"),
81 class RegisterCoalescer : public MachineFunctionPass {
83 MachineRegisterInfo* MRI;
84 const TargetMachine* TM;
85 const TargetRegisterInfo* TRI;
86 const TargetInstrInfo* TII;
88 LiveDebugVariables *LDV;
89 const MachineLoopInfo* Loops;
91 RegisterClassInfo RegClassInfo;
93 /// JoinedCopies - Keep track of copies eliminated due to coalescing.
95 SmallPtrSet<MachineInstr*, 32> JoinedCopies;
97 /// ReMatCopies - Keep track of copies eliminated due to remat.
99 SmallPtrSet<MachineInstr*, 32> ReMatCopies;
101 /// ReMatDefs - Keep track of definition instructions which have
103 SmallPtrSet<MachineInstr*, 8> ReMatDefs;
105 /// joinIntervals - join compatible live intervals
106 void joinIntervals();
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);
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
119 bool JoinCopy(MachineInstr *TheCopy, bool &Again);
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);
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);
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);
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);
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);
149 /// shouldJoinPhys - Return true if a physreg copy should be joined.
150 bool shouldJoinPhys(CoalescerPair &CP);
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
157 void UpdateRegDefsUses(const CoalescerPair &CP);
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
162 bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI);
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);
168 /// markAsJoined - Remember that CopyMI has already been joined.
169 void markAsJoined(MachineInstr *CopyMI);
171 /// eliminateUndefCopy - Handle copies of undef values.
172 bool eliminateUndefCopy(MachineInstr *CopyMI, const CoalescerPair &CP);
175 static char ID; // Class identification, replacement for typeinfo
176 RegisterCoalescer() : MachineFunctionPass(ID) {
177 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
180 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
182 virtual void releaseMemory();
184 /// runOnMachineFunction - pass entry point
185 virtual bool runOnMachineFunction(MachineFunction&);
187 /// print - Implement the dump method.
188 virtual void print(raw_ostream &O, const Module* = 0) const;
190 } /// end anonymous namespace
192 char &llvm::RegisterCoalescerPassID = RegisterCoalescer::ID;
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)
207 char RegisterCoalescer::ID = 0;
209 static unsigned compose(const TargetRegisterInfo &tri, unsigned a, unsigned b) {
212 return tri.composeSubRegIndices(a, b);
215 static bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI,
216 unsigned &Src, unsigned &Dst,
217 unsigned &SrcSub, unsigned &DstSub) {
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();
234 bool CoalescerPair::setRegisters(const MachineInstr *MI) {
235 SrcReg = DstReg = SubIdx = 0;
237 Flipped = CrossClass = false;
239 unsigned Src, Dst, SrcSub, DstSub;
240 if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
242 Partial = SrcSub || DstSub;
244 // If one register is a physreg, it must be Dst.
245 if (TargetRegisterInfo::isPhysicalRegister(Src)) {
246 if (TargetRegisterInfo::isPhysicalRegister(Dst))
249 std::swap(SrcSub, DstSub);
253 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
255 if (TargetRegisterInfo::isPhysicalRegister(Dst)) {
256 // Eliminate DstSub on a physreg.
258 Dst = TRI.getSubReg(Dst, DstSub);
259 if (!Dst) return false;
263 // Eliminate SrcSub by picking a corresponding Dst superregister.
265 Dst = TRI.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src));
266 if (!Dst) return false;
268 } else if (!MRI.getRegClass(Src)->contains(Dst)) {
272 // Both registers are virtual.
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)
281 const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
282 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
283 if (!TRI.getCommonSubClass(DstRC, SrcRC))
288 // There can be no SrcSub.
293 assert(!Flipped && "Unexpected flip");
297 // Find the new register class.
298 const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
299 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
301 NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
303 NewRC = TRI.getCommonSubClass(DstRC, SrcRC);
306 CrossClass = NewRC != DstRC || NewRC != SrcRC;
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");
318 bool CoalescerPair::flip() {
319 if (SubIdx || TargetRegisterInfo::isPhysicalRegister(DstReg))
321 std::swap(SrcReg, DstReg);
326 bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
329 unsigned Src, Dst, SrcSub, DstSub;
330 if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub))
333 // Find the virtual register that is SrcReg.
336 std::swap(SrcSub, DstSub);
337 } else if (Src != SrcReg) {
341 // Now check that Dst matches DstReg.
342 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
343 if (!TargetRegisterInfo::isPhysicalRegister(Dst))
345 assert(!SubIdx && "Inconsistent CoalescerPair state.");
346 // DstSub could be set for a physreg from INSERT_SUBREG.
348 Dst = TRI.getSubReg(Dst, DstSub);
351 return DstReg == Dst;
352 // This is a partial register copy. Check that the parts match.
353 return TRI.getSubReg(DstReg, SrcSub) == Dst;
355 // DstReg is virtual.
358 // Registers match, do the subregisters line up?
359 return compose(TRI, SubIdx, SrcSub) == DstSub;
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);
380 void RegisterCoalescer::markAsJoined(MachineInstr *CopyMI) {
381 /// Joined copies are not deleted immediately, but kept in JoinedCopies.
382 JoinedCopies.insert(CopyMI);
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)
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:
400 /// B1 = A3 <- this copy
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).
405 /// This returns true if an interval was modified.
407 bool RegisterCoalescer::AdjustCopiesBackFrom(const CoalescerPair &CP,
408 MachineInstr *CopyMI) {
409 // Bail if there is no dst interval - can happen when merging physical subreg
411 if (!LIS->hasInterval(CP.getDstReg()))
415 LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
417 LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
418 SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
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;
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
429 if (!BValNo->isDefByCopy()) return false;
430 assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
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.
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,
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.
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())
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()))
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())
471 // Make sure that the end of the live range is inside the same block as
473 MachineInstr *ValLREndInst =
474 LIS->getInstructionFromIndex(ValLR->end.getPrevSlot());
475 if (!ValLREndInst || ValLREndInst->getParent() != CopyMI->getParent())
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;
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))) {
490 dbgs() << "\t\tInterfere with alias ";
491 LIS->getInterval(*AS).print(dbgs(), TRI);
498 dbgs() << "Extending: ";
499 IntB.print(dbgs(), TRI);
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
506 BValNo->def = FillerStart;
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));
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))
520 LiveInterval &SRLI = LIS->getInterval(*SR);
521 SRLI.addRange(LiveRange(FillerStart, FillerEnd,
522 SRLI.getNextValue(FillerStart, 0,
523 LIS->getVNInfoAllocator())));
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);
534 ValLR->valno->setHasPHIKill(true);
537 dbgs() << " result = ";
538 IntB.print(dbgs(), TRI);
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);
546 ValLREndInst->getOperand(UIdx).setIsKill(false);
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
552 if (ALR->end == CopyIdx)
553 LIS->shrinkToUses(&IntA);
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,
565 for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
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())
572 for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
573 if (BI->valno == BValNo)
575 if (BI->start <= AI->start && BI->end > AI->start)
577 if (BI->start > AI->start && BI->start < AI->end)
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,
591 /// A3 = op A2 B0<kill>
593 /// B1 = A3 <- this copy
595 /// = op A3 <- more uses
599 /// B2 = op B0 A2<kill>
601 /// B1 = B2 <- now an identify copy
603 /// = op B2 <- more uses
605 /// This returns true if an interval was modified.
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())
616 // Bail if there is no dst interval.
617 if (!LIS->hasInterval(CP.getDstReg()))
620 SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot();
623 LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
625 LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
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())
633 assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
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");
639 // If other defs can reach uses of this def, then it's not safe to perform
641 if (AValNo->isPHIDef() || AValNo->isUnused() || AValNo->hasPHIKill())
643 MachineInstr *DefMI = LIS->getInstructionFromIndex(AValNo->def);
646 const MCInstrDesc &MCID = DefMI->getDesc();
647 if (!MCID.isCommutable())
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);
654 if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
656 unsigned Op1, Op2, NewDstIdx;
657 if (!TII->findCommutedOpIndices(DefMI, Op1, Op2))
661 else if (Op2 == UseOpIdx)
666 MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
667 unsigned NewReg = NewDstMO.getReg();
668 if (NewReg != IntB.reg || !NewDstMO.isKill())
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))
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))
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())
694 if (ULR->valno == AValNo && JoinedCopies.count(UseMI))
698 DEBUG(dbgs() << "\tRemoveCopyByCommutingDef: " << AValNo->def << '\t'
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);
707 if (TargetRegisterInfo::isVirtualRegister(IntA.reg) &&
708 TargetRegisterInfo::isVirtualRegister(IntB.reg) &&
709 !MRI->constrainRegClass(IntB.reg, MRI->getRegClass(IntA.reg)))
711 if (NewMI != DefMI) {
712 LIS->ReplaceMachineInstrInMaps(DefMI, NewMI);
713 MachineBasicBlock::iterator Pos = DefMI;
714 MBB->insert(Pos, NewMI);
717 unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
718 NewMI->getOperand(OpIdx).setIsKill();
720 // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
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;
735 if (JoinedCopies.count(UseMI))
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);
743 SlotIndex UseIdx = LIS->getInstructionIndex(UseMI).getRegSlot(true);
744 LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
745 if (ULR == IntA.end() || ULR->valno != AValNo)
747 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
748 UseMO.substPhysReg(NewReg, *TRI);
750 UseMO.setReg(NewReg);
753 if (!UseMI->isCopy())
755 if (UseMI->getOperand(0).getReg() != IntB.reg ||
756 UseMI->getOperand(0).getSubReg())
759 // This copy will become a noop. If it's defining a new val#, merge it into
761 SlotIndex DefIdx = UseIdx.getRegSlot();
762 VNInfo *DVNI = IntB.getVNInfoAt(DefIdx);
765 DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI);
766 assert(DVNI->def == DefIdx);
767 BValNo = IntB.MergeValueNumberInto(BValNo, DVNI);
771 // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
773 VNInfo *ValNo = BValNo;
774 ValNo->def = AValNo->def;
776 for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
778 if (AI->valno != AValNo) continue;
779 IntB.addRange(LiveRange(AI->start, AI->end, ValNo));
781 DEBUG(dbgs() << "\t\textended: " << IntB << '\n');
783 IntA.removeValNo(AValNo);
784 DEBUG(dbgs() << "\t\ttrimmed: " << IntA << '\n');
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,
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())
801 MachineInstr *DefMI = LIS->getInstructionFromIndex(ValNo->def);
804 assert(DefMI && "Defining instruction disappeared");
805 const MCInstrDesc &MCID = DefMI->getDesc();
806 if (!MCID.isAsCheapAsAMove())
808 if (!TII->isTriviallyReMaterializable(DefMI, AA))
810 bool SawStore = false;
811 if (!DefMI->isSafeToMove(TII, AA, SawStore))
813 if (MCID.getNumDefs() != 1)
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)
823 } else if (!RC->contains(DstReg))
827 RemoveCopyFlag(DstReg, CopyMI);
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);
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);
843 RemoveCopyFlag(MO.getReg(), CopyMI);
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);
854 // The source interval can become smaller because we removed a use.
856 LIS->shrinkToUses(&SrcInt);
861 /// eliminateUndefCopy - ProcessImpicitDefs may leave some copies of <undef>
862 /// values, it only removes local variables. When we have a copy like:
864 /// %vreg1 = COPY %vreg2<undef>
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))
874 LiveInterval *DstInt = &LIS->getInterval(CP.getDstReg());
875 if (DstInt->liveAt(Idx))
878 // No intervals are live-in to CopyMI - it is undef.
883 VNInfo *DeadVNI = DstInt->getVNInfoAt(Idx.getRegSlot());
884 assert(DeadVNI && "No value defined in DstInt");
885 DstInt->removeValNo(DeadVNI);
887 // Find new undef uses.
888 for (MachineRegisterInfo::reg_nodbg_iterator
889 I = MRI->reg_nodbg_begin(DstInt->reg), E = MRI->reg_nodbg_end();
891 MachineOperand &MO = I.getOperand();
892 if (MO.isDef() || MO.isUndef())
894 MachineInstr *MI = MO.getParent();
895 SlotIndex Idx = LIS->getInstructionIndex(MI);
896 if (DstInt->liveAt(Idx))
899 DEBUG(dbgs() << "\tnew undef: " << Idx << '\t' << *MI);
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
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();
916 // Update LiveDebugVariables.
917 LDV->renameRegister(SrcReg, DstReg, SubIdx);
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
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))
934 SmallVector<unsigned,8> Ops;
936 tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops);
937 bool Kills = false, Deads = false;
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();
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
950 if (SubIdx && !Reads)
954 MO.substPhysReg(DstReg, *TRI);
956 MO.substVirtReg(DstReg, SubIdx, *TRI);
959 // This instruction is a copy that will be removed.
960 if (JoinedCopies.count(UseMI))
964 // If UseMI was a simple SrcReg def, make sure we didn't turn it into a
965 // read-modify-write of DstReg.
967 UseMI->addRegisterDead(DstReg, TRI);
968 else if (!Reads && Writes)
969 UseMI->addRegisterDefined(DstReg, TRI);
971 // Kill flags apply to the whole physical register.
972 if (DstIsPhys && Kills)
973 UseMI->addRegisterKilled(DstReg, TRI);
977 dbgs() << "\t\tupdated: ";
978 if (!UseMI->isDebugValue())
979 dbgs() << LIS->getInstructionIndex(UseMI) << "\t";
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) {
991 if (TargetRegisterInfo::isPhysicalRegister(li.reg))
992 for (const unsigned* SR = TRI->getSubRegisters(li.reg); *SR; ++SR) {
993 if (!LIS->hasInterval(*SR))
995 LiveInterval &sli = LIS->getInterval(*SR);
997 LIS->removeInterval(*SR);
999 LIS->removeInterval(li.reg);
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)
1013 li.removeValNo(MLR->valno);
1014 return removeIntervalIfEmpty(li, LIS, TRI);
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);
1026 if (!TargetRegisterInfo::isPhysicalRegister(DstReg))
1028 for (const unsigned* AS = TRI->getAliasSet(DstReg); *AS; ++AS) {
1029 if (!LIS->hasInterval(*AS))
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);
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());
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())
1053 if (!EnablePhysicalJoin) {
1054 DEBUG(dbgs() << "\tPhysreg joins disabled.\n");
1058 // Only coalesce to allocatable physreg, we don't want to risk modifying
1059 // reserved registers.
1061 DEBUG(dbgs() << "\tRegister is an unallocatable physreg.\n");
1062 return false; // Not coalescable.
1065 // Don't join with physregs that have a ridiculous number of live
1066 // ranges. The data structure performance is really bad when that
1068 if (LIS->hasInterval(CP.getDstReg()) &&
1069 LIS->getInterval(CP.getDstReg()).ranges.size() > 1000) {
1072 << "\tPhysical register live interval too complicated, abort!\n");
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) {
1084 DEBUG(dbgs() << "\tMay tie down a physical register, abort!\n");
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) {
1100 if (JoinedCopies.count(CopyMI) || ReMatCopies.count(CopyMI))
1101 return false; // Already done.
1103 DEBUG(dbgs() << LIS->getInstructionIndex(CopyMI) << '\t' << *CopyMI);
1105 CoalescerPair CP(*TII, *TRI);
1106 if (!CP.setRegisters(CopyMI)) {
1107 DEBUG(dbgs() << "\tNot coalescable.\n");
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.
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.
1125 DEBUG(dbgs() << "\tConsidering merging " << PrintReg(CP.getSrcReg(), TRI)
1126 << " with " << PrintReg(CP.getDstReg(), TRI, CP.getSubIdx())
1129 // Enforce policies.
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))
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");
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())
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.
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))
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");
1180 // Otherwise, we are unable to join the intervals.
1181 DEBUG(dbgs() << "\tInterference!\n");
1182 Again = true; // May be possible to coalesce later.
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()) {
1190 MRI->setRegClass(CP.getDstReg(), CP.getNewRC());
1193 // Remember to delete the copy instruction.
1194 markAsJoined(CopyMI);
1196 UpdateRegDefsUses(CP);
1198 // If we have extended the live range of a physical register, make sure we
1199 // update live-in lists as well.
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();
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());
1217 // SrcReg is guarateed to be the register whose live interval that is
1219 LIS->removeInterval(CP.getSrcReg());
1221 // Update regalloc hint.
1222 TRI->UpdateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF);
1225 LiveInterval &DstInt = LIS->getInterval(CP.getDstReg());
1226 dbgs() << "\tJoined. Result = ";
1227 DstInt.print(dbgs(), TRI);
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.
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.
1247 /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1248 /// contains the value number the copy is from.
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;
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");
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;
1270 VNInfo *OtherValNo = I->second;
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];
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;
1287 // Find out if we have something like
1290 // if so, we can pretend this is actually
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,
1302 SmallVector<MachineInstr*, 8> &DupCopies) {
1303 // FIXME: This is very conservative. For example, we don't handle
1304 // physical registers.
1306 MachineInstr *MI = VNI->getCopy();
1308 if (!MI->isFullCopy() || CP.isPartial() || CP.isPhys())
1311 unsigned Dst = MI->getOperand(0).getReg();
1312 unsigned Src = MI->getOperand(1).getReg();
1314 if (!TargetRegisterInfo::isVirtualRegister(Src) ||
1315 !TargetRegisterInfo::isVirtualRegister(Dst))
1318 unsigned A = CP.getDstReg();
1319 unsigned B = CP.getSrcReg();
1325 VNInfo *Other = LR->valno;
1326 if (!Other->isDefByCopy())
1328 const MachineInstr *OtherMI = Other->getCopy();
1330 if (!OtherMI->isFullCopy())
1333 unsigned OtherDst = OtherMI->getOperand(0).getReg();
1334 unsigned OtherSrc = OtherMI->getOperand(1).getReg();
1336 if (!TargetRegisterInfo::isVirtualRegister(OtherSrc) ||
1337 !TargetRegisterInfo::isVirtualRegister(OtherDst))
1340 assert(OtherDst == B);
1342 if (Src != OtherSrc)
1345 // If the copies use two different value numbers of X, we cannot merge
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))
1353 DupCopies.push_back(MI);
1358 /// JoinIntervals - Attempt to join these two intervals. On failure, this
1360 bool RegisterCoalescer::JoinIntervals(CoalescerPair &CP) {
1361 LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
1362 DEBUG({ dbgs() << "\t\tRHS = "; RHS.print(dbgs(), TRI); dbgs() << "\n"; });
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.
1369 for (const unsigned *AS = TRI->getAliasSet(CP.getDstReg()); *AS; ++AS){
1370 if (!LIS->hasInterval(*AS))
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();
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)))) {
1382 dbgs() << "\t\tInterference from alias: ";
1383 LHS.print(dbgs(), TRI);
1384 dbgs() << "\n\t\tOverlap at " << RI->start << " and no copy.\n";
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))) {
1394 dbgs() << "\t\tInterference from alias: ";
1395 LHS.print(dbgs(), TRI);
1396 dbgs() << "\n\t\tDef at " << LI->start << " is not a copy.\n";
1405 // Compute the final value assignment, assuming that the live ranges can be
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;
1413 SmallVector<MachineInstr*, 8> DupCopies;
1415 LiveInterval &LHS = LIS->getOrCreateInterval(CP.getDstReg());
1416 DEBUG({ dbgs() << "\t\tLHS = "; LHS.print(dbgs(), TRI); dbgs() << "\n"; });
1418 // Loop over the value numbers of the LHS, seeing if any are defined from
1420 for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1423 if (VNI->isUnused() || !VNI->isDefByCopy()) // Src not defined by a copy?
1426 // Never join with a register that has EarlyClobber redefs.
1427 if (VNI->hasRedefByEC())
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.
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))
1442 LHSValsDefinedFromRHS[VNI] = lr->valno;
1445 // Loop over the value numbers of the RHS, seeing if any are defined from
1447 for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1450 if (VNI->isUnused() || !VNI->isDefByCopy()) // Src not defined by a copy?
1453 // Never join with a register that has EarlyClobber redefs.
1454 if (VNI->hasRedefByEC())
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.
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))
1469 RHSValsDefinedFromLHS[VNI] = lr->valno;
1472 LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
1473 RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
1474 NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
1476 for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
1479 unsigned VN = VNI->id;
1480 if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused())
1482 ComputeUltimateVN(VNI, NewVNInfo,
1483 LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
1484 LHSValNoAssignments, RHSValNoAssignments);
1486 for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
1489 unsigned VN = VNI->id;
1490 if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
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;
1499 ComputeUltimateVN(VNI, NewVNInfo,
1500 RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
1501 RHSValNoAssignments, LHSValNoAssignments);
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();
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;
1522 while (I != IE && J != JE) {
1523 // Determine if these two live ranges overlap.
1525 if (I->start < J->start) {
1526 Overlaps = I->end > J->start;
1528 Overlaps = J->end > I->start;
1531 // If so, check value # info to determine if they are really different.
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])
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())
1544 if (I->end < J->end)
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);
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);
1568 if (LHSValNoAssignments.empty())
1569 LHSValNoAssignments.push_back(-1);
1570 if (RHSValNoAssignments.empty())
1571 RHSValNoAssignments.push_back(-1);
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;
1578 // We have pretended that the assignment to B in
1581 // was actually a copy from A. Now that we decided to coalesce A and B,
1582 // transform the code into
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);
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));
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,
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;
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();
1624 // As a last resort, sort by block number.
1625 return LHS.second->getNumber() < RHS.second->getNumber();
1630 void RegisterCoalescer::CopyCoalesceInMBB(MachineBasicBlock *MBB,
1631 std::vector<MachineInstr*> &TryAgain) {
1632 DEBUG(dbgs() << MBB->getName() << ":\n");
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();
1639 MachineInstr *Inst = MII++;
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();
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);
1659 VirtCopies.push_back(Inst);
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];
1668 if (!JoinCopy(TheCopy, Again))
1670 TryAgain.push_back(TheCopy);
1672 for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) {
1673 MachineInstr *TheCopy = PhysCopies[i];
1675 if (!JoinCopy(TheCopy, Again))
1677 TryAgain.push_back(TheCopy);
1679 for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) {
1680 MachineInstr *TheCopy = VirtCopies[i];
1682 if (!JoinCopy(TheCopy, Again))
1684 TryAgain.push_back(TheCopy);
1688 void RegisterCoalescer::joinIntervals() {
1689 DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n");
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();
1696 CopyCoalesceInMBB(I, TryAgainList);
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.
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));
1710 // Sort by loop depth.
1711 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
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);
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;
1724 for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
1725 MachineInstr *&TheCopy = TryAgainList[i];
1730 bool Success = JoinCopy(TheCopy, Again);
1731 if (Success || !Again) {
1732 TheCopy= 0; // Mark this one as done.
1733 ProgressMade = true;
1739 void RegisterCoalescer::releaseMemory() {
1740 JoinedCopies.clear();
1741 ReMatCopies.clear();
1745 bool RegisterCoalescer::runOnMachineFunction(MachineFunction &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>();
1756 DEBUG(dbgs() << "********** SIMPLE REGISTER COALESCING **********\n"
1757 << "********** Function: "
1758 << ((Value*)MF->getFunction())->getName() << '\n');
1760 if (VerifyCoalescing)
1761 MF->verify(this, "Before register coalescing");
1763 RegClassInfo.runOnMachineFunction(fn);
1765 // Join (coalesce) intervals if requested.
1766 if (EnableJoining) {
1769 dbgs() << "********** INTERVALS POST JOINING **********\n";
1770 for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end();
1772 I->second->print(dbgs(), TRI);
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();
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();
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);
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.
1811 if (MI->allDefsAreDead()) {
1812 if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1813 LIS->hasInterval(SrcReg))
1814 LIS->shrinkToUses(&LIS->getInterval(SrcReg));
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);
1823 MI->setDesc(TII->get(TargetOpcode::KILL));
1824 mii = llvm::next(mii);
1826 LIS->RemoveMachineInstrFromMaps(MI);
1827 mii = mbbi->erase(mii);
1833 // Now check if this is a remat'ed def instruction which is now dead.
1834 if (ReMatDefs.count(MI)) {
1836 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1837 const MachineOperand &MO = MI->getOperand(i);
1840 unsigned Reg = MO.getReg();
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);
1851 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
1852 !MRI->use_nodbg_empty(Reg)) {
1858 while (!DeadDefs.empty()) {
1859 unsigned DeadDef = DeadDefs.back();
1860 DeadDefs.pop_back();
1861 RemoveDeadDef(LIS->getInterval(DeadDef), MI);
1863 LIS->RemoveMachineInstrFromMaps(mii);
1864 mii = mbbi->erase(mii);
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);
1884 // When leaving a kill flag on a physreg, check if any subregs should
1886 if (!TargetRegisterInfo::isPhysicalRegister(reg))
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);
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 ->
1899 array_pod_sort(InflateRegs.begin(), InflateRegs.end());
1900 InflateRegs.erase(std::unique(InflateRegs.begin(), 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))
1907 if (MRI->recomputeRegClass(Reg, *TM)) {
1908 DEBUG(dbgs() << PrintReg(Reg) << " inflated to "
1909 << MRI->getRegClass(Reg)->getName() << '\n');
1916 if (VerifyCoalescing)
1917 MF->verify(this, "After register coalescing");
1921 /// print - Implement the dump method.
1922 void RegisterCoalescer::print(raw_ostream &O, const Module* m) const {