comparison lib/CodeGen/LiveIntervalAnalysis.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents 7d135dc70f03
children 803732b1fca8
comparison
equal deleted inserted replaced
101:34baf5011add 120:1172e4bd9c6f
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file implements the LiveInterval analysis pass which is used 10 // This file implements the LiveInterval analysis pass which is used
11 // by the Linear Scan Register allocator. This pass linearizes the 11 // by the Linear Scan Register allocator. This pass linearizes the
12 // basic blocks of the function in DFS order and uses the 12 // basic blocks of the function in DFS order and computes live intervals for
13 // LiveVariables pass to conservatively compute live intervals for
14 // each virtual and physical register. 13 // each virtual and physical register.
15 // 14 //
16 //===----------------------------------------------------------------------===// 15 //===----------------------------------------------------------------------===//
17 16
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 17 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "LiveRangeCalc.h" 18 #include "LiveRangeCalc.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Analysis/AliasAnalysis.h" 20 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/CodeGen/LiveVariables.h" 21 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 22 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
25 #include "llvm/CodeGen/MachineDominators.h" 23 #include "llvm/CodeGen/MachineDominators.h"
36 #include "llvm/Target/TargetInstrInfo.h" 34 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetRegisterInfo.h" 35 #include "llvm/Target/TargetRegisterInfo.h"
38 #include "llvm/Target/TargetSubtargetInfo.h" 36 #include "llvm/Target/TargetSubtargetInfo.h"
39 #include <algorithm> 37 #include <algorithm>
40 #include <cmath> 38 #include <cmath>
41 #include <limits>
42 using namespace llvm; 39 using namespace llvm;
43 40
44 #define DEBUG_TYPE "regalloc" 41 #define DEBUG_TYPE "regalloc"
45 42
46 char LiveIntervals::ID = 0; 43 char LiveIntervals::ID = 0;
47 char &llvm::LiveIntervalsID = LiveIntervals::ID; 44 char &llvm::LiveIntervalsID = LiveIntervals::ID;
48 INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", 45 INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals",
49 "Live Interval Analysis", false, false) 46 "Live Interval Analysis", false, false)
50 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 47 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
51 INITIALIZE_PASS_DEPENDENCY(LiveVariables)
52 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 48 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
53 INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 49 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
54 INITIALIZE_PASS_END(LiveIntervals, "liveintervals", 50 INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
55 "Live Interval Analysis", false, false) 51 "Live Interval Analysis", false, false)
56 52
60 cl::desc("Eagerly compute live intervals for all physreg units.")); 56 cl::desc("Eagerly compute live intervals for all physreg units."));
61 #else 57 #else
62 static bool EnablePrecomputePhysRegs = false; 58 static bool EnablePrecomputePhysRegs = false;
63 #endif // NDEBUG 59 #endif // NDEBUG
64 60
65 static cl::opt<bool> EnableSubRegLiveness(
66 "enable-subreg-liveness", cl::Hidden, cl::init(true),
67 cl::desc("Enable subregister liveness tracking."));
68
69 namespace llvm { 61 namespace llvm {
70 cl::opt<bool> UseSegmentSetForPhysRegs( 62 cl::opt<bool> UseSegmentSetForPhysRegs(
71 "use-segment-set-for-physregs", cl::Hidden, cl::init(true), 63 "use-segment-set-for-physregs", cl::Hidden, cl::init(true),
72 cl::desc( 64 cl::desc(
73 "Use segment set for the computation of the live ranges of physregs.")); 65 "Use segment set for the computation of the live ranges of physregs."));
75 67
76 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { 68 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.setPreservesCFG(); 69 AU.setPreservesCFG();
78 AU.addRequired<AAResultsWrapperPass>(); 70 AU.addRequired<AAResultsWrapperPass>();
79 AU.addPreserved<AAResultsWrapperPass>(); 71 AU.addPreserved<AAResultsWrapperPass>();
80 // LiveVariables isn't really required by this analysis, it is only required
81 // here to make sure it is live during TwoAddressInstructionPass and
82 // PHIElimination. This is temporary.
83 AU.addRequired<LiveVariables>();
84 AU.addPreserved<LiveVariables>(); 72 AU.addPreserved<LiveVariables>();
85 AU.addPreservedID(MachineLoopInfoID); 73 AU.addPreservedID(MachineLoopInfoID);
86 AU.addRequiredTransitiveID(MachineDominatorsID); 74 AU.addRequiredTransitiveID(MachineDominatorsID);
87 AU.addPreservedID(MachineDominatorsID); 75 AU.addPreservedID(MachineDominatorsID);
88 AU.addPreserved<SlotIndexes>(); 76 AU.addPreserved<SlotIndexes>();
125 TII = MF->getSubtarget().getInstrInfo(); 113 TII = MF->getSubtarget().getInstrInfo();
126 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 114 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
127 Indexes = &getAnalysis<SlotIndexes>(); 115 Indexes = &getAnalysis<SlotIndexes>();
128 DomTree = &getAnalysis<MachineDominatorTree>(); 116 DomTree = &getAnalysis<MachineDominatorTree>();
129 117
130 if (EnableSubRegLiveness && MF->getSubtarget().enableSubRegLiveness())
131 MRI->enableSubRegLiveness(true);
132
133 if (!LRCalc) 118 if (!LRCalc)
134 LRCalc = new LiveRangeCalc(); 119 LRCalc = new LiveRangeCalc();
135 120
136 // Allocate space for all virtual registers. 121 // Allocate space for all virtual registers.
137 VirtRegIntervals.resize(MRI->getNumVirtRegs()); 122 VirtRegIntervals.resize(MRI->getNumVirtRegs());
195 /// computeVirtRegInterval - Compute the live interval of a virtual register, 180 /// computeVirtRegInterval - Compute the live interval of a virtual register,
196 /// based on defs and uses. 181 /// based on defs and uses.
197 void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { 182 void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) {
198 assert(LRCalc && "LRCalc not initialized."); 183 assert(LRCalc && "LRCalc not initialized.");
199 assert(LI.empty() && "Should only compute empty intervals."); 184 assert(LI.empty() && "Should only compute empty intervals.");
200 bool ShouldTrackSubRegLiveness = MRI->shouldTrackSubRegLiveness(LI.reg);
201 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 185 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
202 LRCalc->calculate(LI, ShouldTrackSubRegLiveness); 186 LRCalc->calculate(LI, MRI->shouldTrackSubRegLiveness(LI.reg));
203 bool SeparatedComponents = computeDeadValues(LI, nullptr); 187 computeDeadValues(LI, nullptr);
204 if (SeparatedComponents) {
205 assert(ShouldTrackSubRegLiveness
206 && "Separated components should only occur for unused subreg defs");
207 SmallVector<LiveInterval*, 8> SplitLIs;
208 splitSeparateComponents(LI, SplitLIs);
209 }
210 } 188 }
211 189
212 void LiveIntervals::computeVirtRegs() { 190 void LiveIntervals::computeVirtRegs() {
213 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 191 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
214 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 192 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
234 212
235 for (MachineInstr &MI : MBB) { 213 for (MachineInstr &MI : MBB) {
236 for (const MachineOperand &MO : MI.operands()) { 214 for (const MachineOperand &MO : MI.operands()) {
237 if (!MO.isRegMask()) 215 if (!MO.isRegMask())
238 continue; 216 continue;
239 RegMaskSlots.push_back(Indexes->getInstructionIndex(&MI).getRegSlot()); 217 RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot());
240 RegMaskBits.push_back(MO.getRegMask()); 218 RegMaskBits.push_back(MO.getRegMask());
241 } 219 }
242 } 220 }
243 221
244 // Some block ends, such as funclet returns, create masks. 222 // Some block ends, such as funclet returns, create masks. Put the mask on
223 // the last instruction of the block, because MBB slot index intervals are
224 // half-open.
245 if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) { 225 if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) {
246 RegMaskSlots.push_back(Indexes->getMBBEndIdx(&MBB)); 226 assert(!MBB.empty() && "empty return block?");
227 RegMaskSlots.push_back(
228 Indexes->getInstructionIndex(MBB.back()).getRegSlot());
247 RegMaskBits.push_back(Mask); 229 RegMaskBits.push_back(Mask);
248 } 230 }
249 231
250 // Compute the number of register mask instructions in this block. 232 // Compute the number of register mask instructions in this block.
251 RMB.second = RegMaskSlots.size() - RMB.first; 233 RMB.second = RegMaskSlots.size() - RMB.first;
437 I = MRI->reg_instr_begin(li->reg), E = MRI->reg_instr_end(); 419 I = MRI->reg_instr_begin(li->reg), E = MRI->reg_instr_end();
438 I != E; ) { 420 I != E; ) {
439 MachineInstr *UseMI = &*(I++); 421 MachineInstr *UseMI = &*(I++);
440 if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg)) 422 if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg))
441 continue; 423 continue;
442 SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); 424 SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot();
443 LiveQueryResult LRQ = li->Query(Idx); 425 LiveQueryResult LRQ = li->Query(Idx);
444 VNInfo *VNI = LRQ.valueIn(); 426 VNInfo *VNI = LRQ.valueIn();
445 if (!VNI) { 427 if (!VNI) {
446 // This shouldn't happen: readsVirtualRegister returns true, but there is 428 // This shouldn't happen: readsVirtualRegister returns true, but there is
447 // no live value. It is likely caused by a target getting <undef> flags 429 // no live value. It is likely caused by a target getting <undef> flags
483 LiveRange::iterator I = LI.FindSegmentContaining(Def); 465 LiveRange::iterator I = LI.FindSegmentContaining(Def);
484 assert(I != LI.end() && "Missing segment for VNI"); 466 assert(I != LI.end() && "Missing segment for VNI");
485 467
486 // Is the register live before? Otherwise we may have to add a read-undef 468 // Is the register live before? Otherwise we may have to add a read-undef
487 // flag for subregister defs. 469 // flag for subregister defs.
488 bool DeadBeforeDef = false;
489 unsigned VReg = LI.reg; 470 unsigned VReg = LI.reg;
490 if (MRI->shouldTrackSubRegLiveness(VReg)) { 471 if (MRI->shouldTrackSubRegLiveness(VReg)) {
491 if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) { 472 if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) {
492 MachineInstr *MI = getInstructionFromIndex(Def); 473 MachineInstr *MI = getInstructionFromIndex(Def);
493 MI->setRegisterDefReadUndef(VReg); 474 MI->setRegisterDefReadUndef(VReg);
494 DeadBeforeDef = true;
495 } 475 }
496 } 476 }
497 477
498 if (I->end != Def.getDeadSlot()) 478 if (I->end != Def.getDeadSlot())
499 continue; 479 continue;
505 MayHaveSplitComponents = true; 485 MayHaveSplitComponents = true;
506 } else { 486 } else {
507 // This is a dead def. Make sure the instruction knows. 487 // This is a dead def. Make sure the instruction knows.
508 MachineInstr *MI = getInstructionFromIndex(Def); 488 MachineInstr *MI = getInstructionFromIndex(Def);
509 assert(MI && "No instruction defining live value"); 489 assert(MI && "No instruction defining live value");
510 MI->addRegisterDead(VReg, TRI); 490 MI->addRegisterDead(LI.reg, TRI);
511
512 // If we have a dead def that is completely separate from the rest of
513 // the liverange then we rewrite it to use a different VReg to not violate
514 // the rule that the liveness of a virtual register forms a connected
515 // component. This should only happen if subregister liveness is tracked.
516 if (DeadBeforeDef)
517 MayHaveSplitComponents = true;
518
519 if (dead && MI->allDefsAreDead()) { 491 if (dead && MI->allDefsAreDead()) {
520 DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI); 492 DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI);
521 dead->push_back(MI); 493 dead->push_back(MI);
522 } 494 }
523 } 495 }
524 } 496 }
525 return MayHaveSplitComponents; 497 return MayHaveSplitComponents;
526 } 498 }
527 499
528 void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg) 500 void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg) {
529 {
530 DEBUG(dbgs() << "Shrink: " << SR << '\n'); 501 DEBUG(dbgs() << "Shrink: " << SR << '\n');
531 assert(TargetRegisterInfo::isVirtualRegister(Reg) 502 assert(TargetRegisterInfo::isVirtualRegister(Reg)
532 && "Can only shrink virtual registers"); 503 && "Can only shrink virtual registers");
533 // Find all the values used, including PHI kills. 504 // Find all the values used, including PHI kills.
534 ShrinkToUsesWorkList WorkList; 505 ShrinkToUsesWorkList WorkList;
535 506
536 // Visit all instructions reading Reg. 507 // Visit all instructions reading Reg.
537 SlotIndex LastIdx; 508 SlotIndex LastIdx;
538 for (MachineOperand &MO : MRI->reg_operands(Reg)) { 509 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
539 MachineInstr *UseMI = MO.getParent(); 510 // Skip "undef" uses.
540 if (UseMI->isDebugValue()) 511 if (!MO.readsReg())
541 continue; 512 continue;
542 // Maybe the operand is for a subregister we don't care about. 513 // Maybe the operand is for a subregister we don't care about.
543 unsigned SubReg = MO.getSubReg(); 514 unsigned SubReg = MO.getSubReg();
544 if (SubReg != 0) { 515 if (SubReg != 0) {
545 LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg); 516 LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg);
546 if ((LaneMask & SR.LaneMask) == 0) 517 if ((LaneMask & SR.LaneMask) == 0)
547 continue; 518 continue;
548 } 519 }
549 // We only need to visit each instruction once. 520 // We only need to visit each instruction once.
550 SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); 521 MachineInstr *UseMI = MO.getParent();
522 SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot();
551 if (Idx == LastIdx) 523 if (Idx == LastIdx)
552 continue; 524 continue;
553 LastIdx = Idx; 525 LastIdx = Idx;
554 526
555 LiveQueryResult LRQ = SR.Query(Idx); 527 LiveQueryResult LRQ = SR.Query(Idx);
583 assert(Segment != nullptr && "Missing segment for VNI"); 555 assert(Segment != nullptr && "Missing segment for VNI");
584 if (Segment->end != VNI->def.getDeadSlot()) 556 if (Segment->end != VNI->def.getDeadSlot())
585 continue; 557 continue;
586 if (VNI->isPHIDef()) { 558 if (VNI->isPHIDef()) {
587 // This is a dead PHI. Remove it. 559 // This is a dead PHI. Remove it.
560 DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n");
588 VNI->markUnused(); 561 VNI->markUnused();
589 SR.removeSegment(*Segment); 562 SR.removeSegment(*Segment);
590 DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n");
591 } 563 }
592 } 564 }
593 565
594 DEBUG(dbgs() << "Shrunk: " << SR << '\n'); 566 DEBUG(dbgs() << "Shrunk: " << SR << '\n');
595 } 567 }
596 568
597 void LiveIntervals::extendToIndices(LiveRange &LR, 569 void LiveIntervals::extendToIndices(LiveRange &LR,
598 ArrayRef<SlotIndex> Indices) { 570 ArrayRef<SlotIndex> Indices,
571 ArrayRef<SlotIndex> Undefs) {
599 assert(LRCalc && "LRCalc not initialized."); 572 assert(LRCalc && "LRCalc not initialized.");
600 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 573 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
601 for (unsigned i = 0, e = Indices.size(); i != e; ++i) 574 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
602 LRCalc->extend(LR, Indices[i]); 575 LRCalc->extend(LR, Indices[i], /*PhysReg=*/0, Undefs);
603 } 576 }
604 577
605 void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill, 578 void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill,
606 SmallVectorImpl<SlotIndex> *EndPoints) { 579 SmallVectorImpl<SlotIndex> *EndPoints) {
607 LiveQueryResult LRQ = LR.Query(Kill); 580 LiveQueryResult LRQ = LR.Query(Kill);
624 if (EndPoints) EndPoints->push_back(MBBEnd); 597 if (EndPoints) EndPoints->push_back(MBBEnd);
625 598
626 // Find all blocks that are reachable from KillMBB without leaving VNI's live 599 // Find all blocks that are reachable from KillMBB without leaving VNI's live
627 // range. It is possible that KillMBB itself is reachable, so start a DFS 600 // range. It is possible that KillMBB itself is reachable, so start a DFS
628 // from each successor. 601 // from each successor.
629 typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy; 602 typedef df_iterator_default_set<MachineBasicBlock*,9> VisitedTy;
630 VisitedTy Visited; 603 VisitedTy Visited;
631 for (MachineBasicBlock::succ_iterator 604 for (MachineBasicBlock::succ_iterator
632 SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end(); 605 SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();
633 SuccI != SuccE; ++SuccI) { 606 SuccI != SuccE; ++SuccI) {
634 for (df_ext_iterator<MachineBasicBlock*, VisitedTy> 607 for (df_ext_iterator<MachineBasicBlock*, VisitedTy>
835 return true; 808 return true;
836 } 809 }
837 return false; 810 return false;
838 } 811 }
839 812
840 float 813 float LiveIntervals::getSpillWeight(bool isDef, bool isUse,
841 LiveIntervals::getSpillWeight(bool isDef, bool isUse, 814 const MachineBlockFrequencyInfo *MBFI,
842 const MachineBlockFrequencyInfo *MBFI, 815 const MachineInstr &MI) {
843 const MachineInstr *MI) { 816 BlockFrequency Freq = MBFI->getBlockFreq(MI.getParent());
844 BlockFrequency Freq = MBFI->getBlockFreq(MI->getParent());
845 const float Scale = 1.0f / MBFI->getEntryFreq(); 817 const float Scale = 1.0f / MBFI->getEntryFreq();
846 return (isDef + isUse) * (Freq.getFrequency() * Scale); 818 return (isDef + isUse) * (Freq.getFrequency() * Scale);
847 } 819 }
848 820
849 LiveRange::Segment 821 LiveRange::Segment
850 LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr* startInst) { 822 LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr &startInst) {
851 LiveInterval& Interval = createEmptyInterval(reg); 823 LiveInterval& Interval = createEmptyInterval(reg);
852 VNInfo* VN = Interval.getNextValue( 824 VNInfo *VN = Interval.getNextValue(
853 SlotIndex(getInstructionIndex(startInst).getRegSlot()), 825 SlotIndex(getInstructionIndex(startInst).getRegSlot()),
854 getVNInfoAllocator()); 826 getVNInfoAllocator());
855 LiveRange::Segment S( 827 LiveRange::Segment S(SlotIndex(getInstructionIndex(startInst).getRegSlot()),
856 SlotIndex(getInstructionIndex(startInst).getRegSlot()), 828 getMBBEndIdx(startInst.getParent()), VN);
857 getMBBEndIdx(startInst->getParent()), VN);
858 Interval.addSegment(S); 829 Interval.addSegment(S);
859 830
860 return S; 831 return S;
861 } 832 }
862 833
960 for (MachineOperand &MO : MI->operands()) { 931 for (MachineOperand &MO : MI->operands()) {
961 if (MO.isRegMask()) 932 if (MO.isRegMask())
962 hasRegMask = true; 933 hasRegMask = true;
963 if (!MO.isReg()) 934 if (!MO.isReg())
964 continue; 935 continue;
965 // Aggressively clear all kill flags. 936 if (MO.isUse()) {
966 // They are reinserted by VirtRegRewriter. 937 if (!MO.readsReg())
967 if (MO.isUse()) 938 continue;
939 // Aggressively clear all kill flags.
940 // They are reinserted by VirtRegRewriter.
968 MO.setIsKill(false); 941 MO.setIsKill(false);
942 }
969 943
970 unsigned Reg = MO.getReg(); 944 unsigned Reg = MO.getReg();
971 if (!Reg) 945 if (!Reg)
972 continue; 946 continue;
973 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 947 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
974 LiveInterval &LI = LIS.getInterval(Reg); 948 LiveInterval &LI = LIS.getInterval(Reg);
975 if (LI.hasSubRanges()) { 949 if (LI.hasSubRanges()) {
976 unsigned SubReg = MO.getSubReg(); 950 unsigned SubReg = MO.getSubReg();
977 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg); 951 LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg)
952 : MRI.getMaxLaneMaskForVReg(Reg);
978 for (LiveInterval::SubRange &S : LI.subranges()) { 953 for (LiveInterval::SubRange &S : LI.subranges()) {
979 if ((S.LaneMask & LaneMask) == 0) 954 if ((S.LaneMask & LaneMask) == 0)
980 continue; 955 continue;
981 updateRange(S, Reg, S.LaneMask); 956 updateRange(S, Reg, S.LaneMask);
982 } 957 }
1039 return; 1014 return;
1040 // Aggressively remove all kill flags from the old kill point. 1015 // Aggressively remove all kill flags from the old kill point.
1041 // Kill flags shouldn't be used while live intervals exist, they will be 1016 // Kill flags shouldn't be used while live intervals exist, they will be
1042 // reinserted by VirtRegRewriter. 1017 // reinserted by VirtRegRewriter.
1043 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end)) 1018 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end))
1044 for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO) 1019 for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO)
1045 if (MO->isReg() && MO->isUse()) 1020 if (MO->isReg() && MO->isUse())
1046 MO->setIsKill(false); 1021 MO->setIsKill(false);
1022
1023 // Is there a def before NewIdx which is not OldIdx?
1024 LiveRange::iterator Next = std::next(OldIdxIn);
1025 if (Next != E && !SlotIndex::isSameInstr(OldIdx, Next->start) &&
1026 SlotIndex::isEarlierInstr(Next->start, NewIdx)) {
1027 // If we are here then OldIdx was just a use but not a def. We only have
1028 // to ensure liveness extends to NewIdx.
1029 LiveRange::iterator NewIdxIn =
1030 LR.advanceTo(Next, NewIdx.getBaseIndex());
1031 // Extend the segment before NewIdx if necessary.
1032 if (NewIdxIn == E ||
1033 !SlotIndex::isEarlierInstr(NewIdxIn->start, NewIdx)) {
1034 LiveRange::iterator Prev = std::prev(NewIdxIn);
1035 Prev->end = NewIdx.getRegSlot();
1036 }
1037 // Extend OldIdxIn.
1038 OldIdxIn->end = Next->start;
1039 return;
1040 }
1041
1047 // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR 1042 // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR
1048 // invalid by overlapping ranges. 1043 // invalid by overlapping ranges.
1049 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); 1044 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end);
1050 OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()); 1045 OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber());
1051 // If this was not a kill, then there was no def and we're done. 1046 // If this was not a kill, then there was no def and we're done.
1052 if (!isKill) 1047 if (!isKill)
1053 return; 1048 return;
1054 1049
1055 // Did we have a Def at OldIdx? 1050 // Did we have a Def at OldIdx?
1056 OldIdxOut = std::next(OldIdxIn); 1051 OldIdxOut = Next;
1057 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) 1052 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start))
1058 return; 1053 return;
1059 } else { 1054 } else {
1060 OldIdxOut = OldIdxIn; 1055 OldIdxOut = OldIdxIn;
1061 } 1056 }
1075 OldIdxOut->start = OldIdxVNI->def; 1070 OldIdxOut->start = OldIdxVNI->def;
1076 return; 1071 return;
1077 } 1072 }
1078 1073
1079 // If we are here then we have a Definition at OldIdx which ends before 1074 // If we are here then we have a Definition at OldIdx which ends before
1080 // NewIdx. Moving across unrelated defs is not allowed; That means we either 1075 // NewIdx.
1081 // had a dead-def at OldIdx or the OldIdxOut segment ends at NewIdx. 1076
1082 assert((OldIdxOut->end == OldIdx.getDeadSlot() ||
1083 SlotIndex::isSameInstr(OldIdxOut->end, NewIdxDef)) &&
1084 "Cannot move def below kill");
1085 // Is there an existing Def at NewIdx? 1077 // Is there an existing Def at NewIdx?
1086 LiveRange::iterator AfterNewIdx 1078 LiveRange::iterator AfterNewIdx
1087 = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot()); 1079 = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot());
1080 bool OldIdxDefIsDead = OldIdxOut->end.isDead();
1081 if (!OldIdxDefIsDead &&
1082 SlotIndex::isEarlierInstr(OldIdxOut->end, NewIdxDef)) {
1083 // OldIdx is not a dead def, and NewIdxDef is inside a new interval.
1084 VNInfo *DefVNI;
1085 if (OldIdxOut != LR.begin() &&
1086 !SlotIndex::isEarlierInstr(std::prev(OldIdxOut)->end,
1087 OldIdxOut->start)) {
1088 // There is no gap between OldIdxOut and its predecessor anymore,
1089 // merge them.
1090 LiveRange::iterator IPrev = std::prev(OldIdxOut);
1091 DefVNI = OldIdxVNI;
1092 IPrev->end = OldIdxOut->end;
1093 } else {
1094 // The value is live in to OldIdx
1095 LiveRange::iterator INext = std::next(OldIdxOut);
1096 assert(INext != E && "Must have following segment");
1097 // We merge OldIdxOut and its successor. As we're dealing with subreg
1098 // reordering, there is always a successor to OldIdxOut in the same BB
1099 // We don't need INext->valno anymore and will reuse for the new segment
1100 // we create later.
1101 DefVNI = OldIdxVNI;
1102 INext->start = OldIdxOut->end;
1103 INext->valno->def = INext->start;
1104 }
1105 // If NewIdx is behind the last segment, extend that and append a new one.
1106 if (AfterNewIdx == E) {
1107 // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up
1108 // one position.
1109 // |- ?/OldIdxOut -| |- X0 -| ... |- Xn -| end
1110 // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS -| end
1111 std::copy(std::next(OldIdxOut), E, OldIdxOut);
1112 // The last segment is undefined now, reuse it for a dead def.
1113 LiveRange::iterator NewSegment = std::prev(E);
1114 *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(),
1115 DefVNI);
1116 DefVNI->def = NewIdxDef;
1117
1118 LiveRange::iterator Prev = std::prev(NewSegment);
1119 Prev->end = NewIdxDef;
1120 } else {
1121 // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up
1122 // one position.
1123 // |- ?/OldIdxOut -| |- X0 -| ... |- Xn/AfterNewIdx -| |- Next -|
1124 // => |- X0/OldIdxOut -| ... |- Xn -| |- Xn/AfterNewIdx -| |- Next -|
1125 std::copy(std::next(OldIdxOut), std::next(AfterNewIdx), OldIdxOut);
1126 LiveRange::iterator Prev = std::prev(AfterNewIdx);
1127 // We have two cases:
1128 if (SlotIndex::isEarlierInstr(Prev->start, NewIdxDef)) {
1129 // Case 1: NewIdx is inside a liverange. Split this liverange at
1130 // NewIdxDef into the segment "Prev" followed by "NewSegment".
1131 LiveRange::iterator NewSegment = AfterNewIdx;
1132 *NewSegment = LiveRange::Segment(NewIdxDef, Prev->end, Prev->valno);
1133 Prev->valno->def = NewIdxDef;
1134
1135 *Prev = LiveRange::Segment(Prev->start, NewIdxDef, DefVNI);
1136 DefVNI->def = Prev->start;
1137 } else {
1138 // Case 2: NewIdx is in a lifetime hole. Keep AfterNewIdx as is and
1139 // turn Prev into a segment from NewIdx to AfterNewIdx->start.
1140 *Prev = LiveRange::Segment(NewIdxDef, AfterNewIdx->start, DefVNI);
1141 DefVNI->def = NewIdxDef;
1142 assert(DefVNI != AfterNewIdx->valno);
1143 }
1144 }
1145 return;
1146 }
1147
1088 if (AfterNewIdx != E && 1148 if (AfterNewIdx != E &&
1089 SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) { 1149 SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) {
1090 // There is an existing def at NewIdx. The def at OldIdx is coalesced into 1150 // There is an existing def at NewIdx. The def at OldIdx is coalesced into
1091 // that value. 1151 // that value.
1092 assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?"); 1152 assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?");
1128 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); 1188 bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end);
1129 if (!isKill) 1189 if (!isKill)
1130 return; 1190 return;
1131 1191
1132 // At this point we have to move OldIdxIn->end back to the nearest 1192 // At this point we have to move OldIdxIn->end back to the nearest
1133 // previous use but no further than NewIdx. Moreover OldIdx is a Def then 1193 // previous use or (dead-)def but no further than NewIdx.
1134 // we cannot have any intermediate uses or the move would be illegal. 1194 SlotIndex DefBeforeOldIdx
1135 1195 = std::max(OldIdxIn->start.getDeadSlot(),
1196 NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()));
1197 OldIdxIn->end = findLastUseBefore(DefBeforeOldIdx, Reg, LaneMask);
1198
1199 // Did we have a Def at OldIdx? If not we are done now.
1136 OldIdxOut = std::next(OldIdxIn); 1200 OldIdxOut = std::next(OldIdxIn);
1137 // Did we have a Def at OldIdx? 1201 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start))
1138 if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) {
1139 // No def, search for the nearest previous use.
1140 // This can never be an early clobber kill since there is no def.
1141 OldIdxIn->end = findLastUseBefore(Reg, LaneMask).getRegSlot();
1142 // We are done if there is no def at OldIdx.
1143 return; 1202 return;
1144 } else {
1145 // There can't have been any intermediate uses or defs, so move
1146 // OldIdxIn->end to NewIdx.
1147 OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber());
1148 }
1149 } else { 1203 } else {
1150 OldIdxOut = OldIdxIn; 1204 OldIdxOut = OldIdxIn;
1205 OldIdxIn = OldIdxOut != LR.begin() ? std::prev(OldIdxOut) : E;
1151 } 1206 }
1152 1207
1153 // If we are here then there is a Definition at OldIdx. OldIdxOut points 1208 // If we are here then there is a Definition at OldIdx. OldIdxOut points
1154 // to the segment starting there. 1209 // to the segment starting there.
1155 assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && 1210 assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) &&
1177 } 1232 }
1178 } else { 1233 } else {
1179 // Previously nothing was live after NewIdx, so all we have to do now is 1234 // Previously nothing was live after NewIdx, so all we have to do now is
1180 // move the begin of OldIdxOut to NewIdx. 1235 // move the begin of OldIdxOut to NewIdx.
1181 if (!OldIdxDefIsDead) { 1236 if (!OldIdxDefIsDead) {
1182 // Leave the end point of a live def. 1237 // Do we have any intermediate Defs between OldIdx and NewIdx?
1183 OldIdxVNI->def = NewIdxDef; 1238 if (OldIdxIn != E &&
1184 OldIdxOut->start = NewIdxDef; 1239 SlotIndex::isEarlierInstr(NewIdxDef, OldIdxIn->start)) {
1240 // OldIdx is not a dead def and NewIdx is before predecessor start.
1241 LiveRange::iterator NewIdxIn = NewIdxOut;
1242 assert(NewIdxIn == LR.find(NewIdx.getBaseIndex()));
1243 const SlotIndex SplitPos = NewIdxDef;
1244
1245 // Merge the OldIdxIn and OldIdxOut segments into OldIdxOut.
1246 *OldIdxOut = LiveRange::Segment(OldIdxIn->start, OldIdxOut->end,
1247 OldIdxIn->valno);
1248 // OldIdxIn and OldIdxVNI are now undef and can be overridden.
1249 // We Slide [NewIdxIn, OldIdxIn) down one position.
1250 // |- X0/NewIdxIn -| ... |- Xn-1 -||- Xn/OldIdxIn -||- OldIdxOut -|
1251 // => |- undef/NexIdxIn -| |- X0 -| ... |- Xn-1 -| |- Xn/OldIdxOut -|
1252 std::copy_backward(NewIdxIn, OldIdxIn, OldIdxOut);
1253 // NewIdxIn is now considered undef so we can reuse it for the moved
1254 // value.
1255 LiveRange::iterator NewSegment = NewIdxIn;
1256 LiveRange::iterator Next = std::next(NewSegment);
1257 if (SlotIndex::isEarlierInstr(Next->start, NewIdx)) {
1258 // There is no gap between NewSegment and its predecessor.
1259 *NewSegment = LiveRange::Segment(Next->start, SplitPos,
1260 Next->valno);
1261 *Next = LiveRange::Segment(SplitPos, Next->end, OldIdxVNI);
1262 Next->valno->def = SplitPos;
1263 } else {
1264 // There is a gap between NewSegment and its predecessor
1265 // Value becomes live in.
1266 *NewSegment = LiveRange::Segment(SplitPos, Next->start, OldIdxVNI);
1267 NewSegment->valno->def = SplitPos;
1268 }
1269 } else {
1270 // Leave the end point of a live def.
1271 OldIdxOut->start = NewIdxDef;
1272 OldIdxVNI->def = NewIdxDef;
1273 if (OldIdxIn != E && SlotIndex::isEarlierInstr(NewIdx, OldIdxIn->end))
1274 OldIdxIn->end = NewIdx.getRegSlot();
1275 }
1185 } else { 1276 } else {
1186 // OldIdxVNI is a dead def. It may have been moved across other values 1277 // OldIdxVNI is a dead def. It may have been moved across other values
1187 // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) 1278 // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut)
1188 // down one position. 1279 // down one position.
1189 // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | 1280 // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - |
1213 SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && 1304 SlotIndex::isEarlierInstr(*RI, *std::next(RI))) &&
1214 "Cannot move regmask instruction below another call"); 1305 "Cannot move regmask instruction below another call");
1215 } 1306 }
1216 1307
1217 // Return the last use of reg between NewIdx and OldIdx. 1308 // Return the last use of reg between NewIdx and OldIdx.
1218 SlotIndex findLastUseBefore(unsigned Reg, LaneBitmask LaneMask) { 1309 SlotIndex findLastUseBefore(SlotIndex Before, unsigned Reg,
1219 1310 LaneBitmask LaneMask) {
1220 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 1311 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1221 SlotIndex LastUse = NewIdx; 1312 SlotIndex LastUse = Before;
1222 for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { 1313 for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) {
1314 if (MO.isUndef())
1315 continue;
1223 unsigned SubReg = MO.getSubReg(); 1316 unsigned SubReg = MO.getSubReg();
1224 if (SubReg != 0 && LaneMask != 0 1317 if (SubReg != 0 && LaneMask != 0
1225 && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask) == 0) 1318 && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask) == 0)
1226 continue; 1319 continue;
1227 1320
1228 const MachineInstr *MI = MO.getParent(); 1321 const MachineInstr &MI = *MO.getParent();
1229 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); 1322 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI);
1230 if (InstSlot > LastUse && InstSlot < OldIdx) 1323 if (InstSlot > LastUse && InstSlot < OldIdx)
1231 LastUse = InstSlot; 1324 LastUse = InstSlot.getRegSlot();
1232 } 1325 }
1233 return LastUse; 1326 return LastUse;
1234 } 1327 }
1235 1328
1236 // This is a regunit interval, so scanning the use list could be very 1329 // This is a regunit interval, so scanning the use list could be very
1237 // expensive. Scan upwards from OldIdx instead. 1330 // expensive. Scan upwards from OldIdx instead.
1238 assert(NewIdx < OldIdx && "Expected upwards move"); 1331 assert(Before < OldIdx && "Expected upwards move");
1239 SlotIndexes *Indexes = LIS.getSlotIndexes(); 1332 SlotIndexes *Indexes = LIS.getSlotIndexes();
1240 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx); 1333 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Before);
1241 1334
1242 // OldIdx may not correspond to an instruction any longer, so set MII to 1335 // OldIdx may not correspond to an instruction any longer, so set MII to
1243 // point to the next instruction after OldIdx, or MBB->end(). 1336 // point to the next instruction after OldIdx, or MBB->end().
1244 MachineBasicBlock::iterator MII = MBB->end(); 1337 MachineBasicBlock::iterator MII = MBB->end();
1245 if (MachineInstr *MI = Indexes->getInstructionFromIndex( 1338 if (MachineInstr *MI = Indexes->getInstructionFromIndex(
1249 1342
1250 MachineBasicBlock::iterator Begin = MBB->begin(); 1343 MachineBasicBlock::iterator Begin = MBB->begin();
1251 while (MII != Begin) { 1344 while (MII != Begin) {
1252 if ((--MII)->isDebugValue()) 1345 if ((--MII)->isDebugValue())
1253 continue; 1346 continue;
1254 SlotIndex Idx = Indexes->getInstructionIndex(MII); 1347 SlotIndex Idx = Indexes->getInstructionIndex(*MII);
1255 1348
1256 // Stop searching when NewIdx is reached. 1349 // Stop searching when Before is reached.
1257 if (!SlotIndex::isEarlierInstr(NewIdx, Idx)) 1350 if (!SlotIndex::isEarlierInstr(Before, Idx))
1258 return NewIdx; 1351 return Before;
1259 1352
1260 // Check if MII uses Reg. 1353 // Check if MII uses Reg.
1261 for (MIBundleOperands MO(MII); MO.isValid(); ++MO) 1354 for (MIBundleOperands MO(*MII); MO.isValid(); ++MO)
1262 if (MO->isReg() && 1355 if (MO->isReg() && !MO->isUndef() &&
1263 TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && 1356 TargetRegisterInfo::isPhysicalRegister(MO->getReg()) &&
1264 TRI.hasRegUnit(MO->getReg(), Reg)) 1357 TRI.hasRegUnit(MO->getReg(), Reg))
1265 return Idx; 1358 return Idx.getRegSlot();
1266 } 1359 }
1267 // Didn't reach NewIdx. It must be the first instruction in the block. 1360 // Didn't reach Before. It must be the first instruction in the block.
1268 return NewIdx; 1361 return Before;
1269 } 1362 }
1270 }; 1363 };
1271 1364
1272 void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) { 1365 void LiveIntervals::handleMove(MachineInstr &MI, bool UpdateFlags) {
1273 assert(!MI->isBundled() && "Can't handle bundled instructions yet."); 1366 assert(!MI.isBundled() && "Can't handle bundled instructions yet.");
1274 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 1367 SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
1275 Indexes->removeMachineInstrFromMaps(MI); 1368 Indexes->removeMachineInstrFromMaps(MI);
1276 SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); 1369 SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI);
1277 assert(getMBBStartIdx(MI->getParent()) <= OldIndex && 1370 assert(getMBBStartIdx(MI.getParent()) <= OldIndex &&
1278 OldIndex < getMBBEndIdx(MI->getParent()) && 1371 OldIndex < getMBBEndIdx(MI.getParent()) &&
1279 "Cannot handle moves across basic block boundaries."); 1372 "Cannot handle moves across basic block boundaries.");
1280 1373
1281 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 1374 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
1282 HME.updateAllRanges(MI); 1375 HME.updateAllRanges(&MI);
1283 } 1376 }
1284 1377
1285 void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI, 1378 void LiveIntervals::handleMoveIntoBundle(MachineInstr &MI,
1286 MachineInstr* BundleStart, 1379 MachineInstr &BundleStart,
1287 bool UpdateFlags) { 1380 bool UpdateFlags) {
1288 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 1381 SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
1289 SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); 1382 SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart);
1290 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 1383 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
1291 HME.updateAllRanges(MI); 1384 HME.updateAllRanges(&MI);
1292 } 1385 }
1293 1386
1294 void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin, 1387 void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin,
1295 const MachineBasicBlock::iterator End, 1388 const MachineBasicBlock::iterator End,
1296 const SlotIndex endIdx, 1389 const SlotIndex endIdx,
1297 LiveRange &LR, const unsigned Reg, 1390 LiveRange &LR, const unsigned Reg,
1298 LaneBitmask LaneMask) { 1391 LaneBitmask LaneMask) {
1299 LiveInterval::iterator LII = LR.find(endIdx); 1392 LiveInterval::iterator LII = LR.find(endIdx);
1300 SlotIndex lastUseIdx; 1393 SlotIndex lastUseIdx;
1394 if (LII == LR.begin()) {
1395 // This happens when the function is called for a subregister that only
1396 // occurs _after_ the range that is to be repaired.
1397 return;
1398 }
1301 if (LII != LR.end() && LII->start < endIdx) 1399 if (LII != LR.end() && LII->start < endIdx)
1302 lastUseIdx = LII->end; 1400 lastUseIdx = LII->end;
1303 else 1401 else
1304 --LII; 1402 --LII;
1305 1403
1306 for (MachineBasicBlock::iterator I = End; I != Begin;) { 1404 for (MachineBasicBlock::iterator I = End; I != Begin;) {
1307 --I; 1405 --I;
1308 MachineInstr *MI = I; 1406 MachineInstr &MI = *I;
1309 if (MI->isDebugValue()) 1407 if (MI.isDebugValue())
1310 continue; 1408 continue;
1311 1409
1312 SlotIndex instrIdx = getInstructionIndex(MI); 1410 SlotIndex instrIdx = getInstructionIndex(MI);
1313 bool isStartValid = getInstructionFromIndex(LII->start); 1411 bool isStartValid = getInstructionFromIndex(LII->start);
1314 bool isEndValid = getInstructionFromIndex(LII->end); 1412 bool isEndValid = getInstructionFromIndex(LII->end);
1315 1413
1316 // FIXME: This doesn't currently handle early-clobber or multiple removed 1414 // FIXME: This doesn't currently handle early-clobber or multiple removed
1317 // defs inside of the region to repair. 1415 // defs inside of the region to repair.
1318 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 1416 for (MachineInstr::mop_iterator OI = MI.operands_begin(),
1319 OE = MI->operands_end(); OI != OE; ++OI) { 1417 OE = MI.operands_end();
1418 OI != OE; ++OI) {
1320 const MachineOperand &MO = *OI; 1419 const MachineOperand &MO = *OI;
1321 if (!MO.isReg() || MO.getReg() != Reg) 1420 if (!MO.isReg() || MO.getReg() != Reg)
1322 continue; 1421 continue;
1323 1422
1324 unsigned SubReg = MO.getSubReg(); 1423 unsigned SubReg = MO.getSubReg();
1384 MachineBasicBlock::iterator Begin, 1483 MachineBasicBlock::iterator Begin,
1385 MachineBasicBlock::iterator End, 1484 MachineBasicBlock::iterator End,
1386 ArrayRef<unsigned> OrigRegs) { 1485 ArrayRef<unsigned> OrigRegs) {
1387 // Find anchor points, which are at the beginning/end of blocks or at 1486 // Find anchor points, which are at the beginning/end of blocks or at
1388 // instructions that already have indexes. 1487 // instructions that already have indexes.
1389 while (Begin != MBB->begin() && !Indexes->hasIndex(Begin)) 1488 while (Begin != MBB->begin() && !Indexes->hasIndex(*Begin))
1390 --Begin; 1489 --Begin;
1391 while (End != MBB->end() && !Indexes->hasIndex(End)) 1490 while (End != MBB->end() && !Indexes->hasIndex(*End))
1392 ++End; 1491 ++End;
1393 1492
1394 SlotIndex endIdx; 1493 SlotIndex endIdx;
1395 if (End == MBB->end()) 1494 if (End == MBB->end())
1396 endIdx = getMBBEndIdx(MBB).getPrevSlot(); 1495 endIdx = getMBBEndIdx(MBB).getPrevSlot();
1397 else 1496 else
1398 endIdx = getInstructionIndex(End); 1497 endIdx = getInstructionIndex(*End);
1399 1498
1400 Indexes->repairIndexesInRange(MBB, Begin, End); 1499 Indexes->repairIndexesInRange(MBB, Begin, End);
1401 1500
1402 for (MachineBasicBlock::iterator I = End; I != Begin;) { 1501 for (MachineBasicBlock::iterator I = End; I != Begin;) {
1403 --I; 1502 --I;
1404 MachineInstr *MI = I; 1503 MachineInstr &MI = *I;
1405 if (MI->isDebugValue()) 1504 if (MI.isDebugValue())
1406 continue; 1505 continue;
1407 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(), 1506 for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(),
1408 MOE = MI->operands_end(); MOI != MOE; ++MOI) { 1507 MOE = MI.operands_end();
1508 MOI != MOE; ++MOI) {
1409 if (MOI->isReg() && 1509 if (MOI->isReg() &&
1410 TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && 1510 TargetRegisterInfo::isVirtualRegister(MOI->getReg()) &&
1411 !hasInterval(MOI->getReg())) { 1511 !hasInterval(MOI->getReg())) {
1412 createAndComputeVirtRegInterval(MOI->getReg()); 1512 createAndComputeVirtRegInterval(MOI->getReg());
1413 } 1513 }
1438 LR->removeValNo(VNI); 1538 LR->removeValNo(VNI);
1439 } 1539 }
1440 } 1540 }
1441 1541
1442 void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) { 1542 void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) {
1543 // LI may not have the main range computed yet, but its subranges may
1544 // be present.
1443 VNInfo *VNI = LI.getVNInfoAt(Pos); 1545 VNInfo *VNI = LI.getVNInfoAt(Pos);
1444 if (VNI == nullptr) 1546 if (VNI != nullptr) {
1445 return; 1547 assert(VNI->def.getBaseIndex() == Pos.getBaseIndex());
1446 LI.removeValNo(VNI); 1548 LI.removeValNo(VNI);
1447 1549 }
1448 // Also remove the value in subranges. 1550
1551 // Also remove the value defined in subranges.
1449 for (LiveInterval::SubRange &S : LI.subranges()) { 1552 for (LiveInterval::SubRange &S : LI.subranges()) {
1450 if (VNInfo *SVNI = S.getVNInfoAt(Pos)) 1553 if (VNInfo *SVNI = S.getVNInfoAt(Pos))
1451 S.removeValNo(SVNI); 1554 if (SVNI->def.getBaseIndex() == Pos.getBaseIndex())
1555 S.removeValNo(SVNI);
1452 } 1556 }
1453 LI.removeEmptySubRanges(); 1557 LI.removeEmptySubRanges();
1454 } 1558 }
1455 1559
1456 void LiveIntervals::splitSeparateComponents(LiveInterval &LI, 1560 void LiveIntervals::splitSeparateComponents(LiveInterval &LI,
1468 SplitLIs.push_back(&NewLI); 1572 SplitLIs.push_back(&NewLI);
1469 } 1573 }
1470 ConEQ.Distribute(LI, SplitLIs.data(), *MRI); 1574 ConEQ.Distribute(LI, SplitLIs.data(), *MRI);
1471 } 1575 }
1472 1576
1473 void LiveIntervals::renameDisconnectedComponents() { 1577 void LiveIntervals::constructMainRangeFromSubranges(LiveInterval &LI) {
1474 ConnectedSubRegClasses SubRegClasses(*this, *MRI); 1578 assert(LRCalc && "LRCalc not initialized.");
1475 1579 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
1476 // Iterate over all vregs. Note that we query getNumVirtRegs() the newly 1580 LRCalc->constructMainRangeFromSubranges(LI);
1477 // created vregs end up with higher numbers but do not need to be visited as 1581 }
1478 // there can't be any further splitting.
1479 for (size_t I = 0, E = MRI->getNumVirtRegs(); I < E; ++I) {
1480 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
1481 LiveInterval *LI = VirtRegIntervals[Reg];
1482 if (LI == nullptr || !LI->hasSubRanges())
1483 continue;
1484
1485 SubRegClasses.renameComponents(*LI);
1486 }
1487 }