llvm.org GIT mirror llvm / 857c4e0
VNInfo cleanup. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73634 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 11 years ago
10 changed file(s) with 271 addition(s) and 143 deletion(s). Raw diff Collapse all Expand all
3232 class TargetRegisterInfo;
3333 struct LiveInterval;
3434
35 /// VNInfo - If the value number definition is undefined (e.g. phi
36 /// merge point), it contains ~0u,x. If the value number is not in use, it
37 /// contains ~1u,x to indicate that the value # is not used.
38 /// def - Instruction # of the definition.
39 /// - or reg # of the definition if it's a stack slot liveinterval.
40 /// copy - Copy iff val# is defined by a copy; zero otherwise.
41 /// hasPHIKill - One or more of the kills are PHI nodes.
42 /// redefByEC - Re-defined by early clobber somewhere during the live range.
43 /// kills - Instruction # of the kills.
44 struct VNInfo {
35 /// VNInfo - Value Number Information.
36 /// This class holds information about a machine level values, including
37 /// definition and use points.
38 ///
39 /// Care must be taken in interpreting the def index of the value. The
40 /// following rules apply:
41 ///
42 /// If the isDefAccurate() method returns false then the def index does not
43 /// actually point to the defining MachineInstr, or even (necessarily) a
44 /// valid MachineInstr at all. In general such a def index should not be
45 /// used as an index to obtain a MachineInstr. The exception is Values
46 /// defined by PHI instructions, after PHI elimination has occured. In this
47 /// case the def should point to the start of the block in which the PHI
48 /// existed. This fact can be used to insert code dealing with the PHI value
49 /// at the merge point (e.g. to spill or split it).
50
51 class VNInfo {
52 private:
53 static const uint8_t HAS_PHI_KILL = 1,
54 REDEF_BY_EC = 1 << 1,
55 IS_PHI_DEF = 1 << 2,
56 IS_UNUSED = 1 << 3,
57 IS_DEF_ACCURATE = 1 << 4;
58
59 uint8_t flags;
60
61 public:
62 /// The ID number of this value.
4563 unsigned id;
64
65 /// The index of the defining instruction (if isDefAccurate() returns true).
4666 unsigned def;
4767 MachineInstr *copy;
48 bool hasPHIKill : 1;
49 bool redefByEC : 1;
5068 SmallVector kills;
69
5170 VNInfo()
52 : id(~1U), def(~1U), copy(0), hasPHIKill(false), redefByEC(false) {}
71 : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
72
73 /// VNInfo constructor.
74 /// d is presumed to point to the actual defining instr. If it doesn't
75 /// setIsDefAccurate(false) should be called after construction.
5376 VNInfo(unsigned i, unsigned d, MachineInstr *c)
54 : id(i), def(d), copy(c), hasPHIKill(false), redefByEC(false) {}
77 : flags(IS_DEF_ACCURATE), id(i), def(d), copy(c) {}
78
79 /// VNInfo construtor, copies values from orig, except for the value number.
80 VNInfo(unsigned i, const VNInfo &orig)
81 : flags(orig.flags), id(i), def(orig.def), copy(orig.copy),
82 kills(orig.kills) {}
83
84 /// Used for copying value number info.
85 unsigned getFlags() const { return flags; }
86 void setFlags(unsigned flags) { this->flags = flags; }
87
88 /// Returns true if one or more kills are PHI nodes.
89 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
90 void setHasPHIKill(bool hasKill) {
91 if (hasKill)
92 flags |= HAS_PHI_KILL;
93 else
94 flags &= ~HAS_PHI_KILL;
95 }
96
97 /// Returns true if this value is re-defined by an early clobber somewhere
98 /// during the live range.
99 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
100 void setHasRedefByEC(bool hasRedef) {
101 if (hasRedef)
102 flags |= REDEF_BY_EC;
103 else
104 flags &= ~REDEF_BY_EC;
105 }
106
107 /// Returns true if this value is defined by a PHI instruction (or was,
108 /// PHI instrucions may have been eliminated).
109 bool isPHIDef() const { return flags & IS_PHI_DEF; }
110 void setIsPHIDef(bool phiDef) {
111 if (phiDef)
112 flags |= IS_PHI_DEF;
113 else
114 flags &= ~IS_PHI_DEF;
115 }
116
117 /// Returns true if this value is unused.
118 bool isUnused() const { return flags & IS_UNUSED; }
119 void setIsUnused(bool unused) {
120 if (unused)
121 flags |= IS_UNUSED;
122 else
123 flags &= ~IS_UNUSED;
124 }
125
126 /// Returns true if the def is accurate.
127 bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
128 void setIsDefAccurate(bool defAccurate) {
129 if (defAccurate)
130 flags |= IS_DEF_ACCURATE;
131 else
132 flags &= ~IS_DEF_ACCURATE;
133 }
134
55135 };
56136
57137 /// LiveRange structure - This represents a simple register range in the
209289 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
210290 DstValNo->def = SrcValNo->def;
211291 DstValNo->copy = SrcValNo->copy;
212 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
213 DstValNo->redefByEC = SrcValNo->redefByEC;
292 DstValNo->setFlags(SrcValNo->getFlags());
214293 DstValNo->kills = SrcValNo->kills;
215294 }
216295
217296 /// getNextValue - Create a new value number and return it. MIIdx specifies
218297 /// the instruction that defines the value number.
219298 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
220 BumpPtrAllocator &VNInfoAllocator) {
299 bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
300
301 assert(MIIdx != ~0u && MIIdx != ~1u &&
302 "PHI def / unused flags should now be passed explicitly.");
221303 #ifdef __GNUC__
222304 unsigned Alignment = (unsigned)__alignof__(VNInfo);
223305 #else
228310 static_cast(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
229311 Alignment));
230312 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
313 VNI->setIsDefAccurate(isDefAccurate);
314 valnos.push_back(VNI);
315 return VNI;
316 }
317
318 /// Create a copy of the given value. The new value will be identical except
319 /// for the Value number.
320 VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
321
322 #ifdef __GNUC__
323 unsigned Alignment = (unsigned)__alignof__(VNInfo);
324 #else
325 // FIXME: ugly.
326 unsigned Alignment = 8;
327 #endif
328 VNInfo *VNI =
329 static_cast(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
330 Alignment));
331
332 new (VNI) VNInfo((unsigned)valnos.size(), *orig);
231333 valnos.push_back(VNI);
232334 return VNI;
233335 }
305305 VNInfo *VNI = valnos.back();
306306 valnos.pop_back();
307307 VNI->~VNInfo();
308 } while (!valnos.empty() && valnos.back()->def == ~1U);
308 } while (!valnos.empty() && valnos.back()->isUnused());
309309 } else {
310 ValNo->def = ~1U;
310 ValNo->setIsUnused(true);
311311 }
312312 }
313313 }
353353 VNInfo *VNI = valnos.back();
354354 valnos.pop_back();
355355 VNI->~VNInfo();
356 } while (!valnos.empty() && valnos.back()->def == ~1U);
356 } while (!valnos.empty() && valnos.back()->isUnused());
357357 } else {
358 ValNo->def = ~1U;
358 ValNo->setIsUnused(true);
359359 }
360360 }
361361
371371 // Scale VNI info.
372372 for (vni_iterator VNI = vni_begin(), VNIE = vni_end(); VNI != VNIE; ++VNI) {
373373 VNInfo *vni = *VNI;
374 if (vni->def != ~0U && vni->def != ~1U) {
375 vni->def = InstrSlots::scale(vni->def, factor);
376 }
374
375 vni->def = InstrSlots::scale(vni->def, factor);
377376
378377 for (unsigned i = 0; i < vni->kills.size(); ++i) {
379378 if (vni->kills[i] != 0)
592591 VNInfo *VNI = valnos.back();
593592 valnos.pop_back();
594593 VNI->~VNInfo();
595 } while (!valnos.empty() && valnos.back()->def == ~1U);
594 } while (!valnos.empty() && valnos.back()->isUnused());
596595 } else {
597 V1->def = ~1U;
596 V1->setIsUnused(true);
598597 }
599598 }
600599 }
621620 else if (UnusedValNo)
622621 ClobberValNo = UnusedValNo;
623622 else {
624 UnusedValNo = ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
623 UnusedValNo = ClobberValNo = getNextValue(0, 0, false, VNInfoAllocator);
625624 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
626625 }
627626
674673 BumpPtrAllocator &VNInfoAllocator) {
675674 // Find a value # to use for the clobber ranges. If there is already a value#
676675 // for unknown values, use it.
677 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
676 VNInfo *ClobberValNo = getNextValue(0, 0, false, VNInfoAllocator);
678677
679678 iterator IP = begin();
680679 IP = std::upper_bound(IP, end(), Start);
757756 VNInfo *VNI = valnos.back();
758757 valnos.pop_back();
759758 VNI->~VNInfo();
760 } while (valnos.back()->def == ~1U);
759 } while (valnos.back()->isUnused());
761760 } else {
762 V1->def = ~1U;
761 V1->setIsUnused(true);
763762 }
764763
765764 return V2;
776775 weight = RHS.weight;
777776 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
778777 const VNInfo *VNI = RHS.getValNumInfo(i);
779 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
780 copyValNumInfo(NewVNI, VNI);
778 createValueCopy(VNI, VNInfoAllocator);
781779 }
782780 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
783781 const LiveRange &LR = RHS.ranges[i];
829827 const VNInfo *vni = *i;
830828 if (vnum) OS << " ";
831829 OS << vnum << "@";
832 if (vni->def == ~1U) {
830 if (vni->isUnused()) {
833831 OS << "x";
834832 } else {
835 if (vni->def == ~0U)
833 if (!vni->isDefAccurate())
836834 OS << "?";
837835 else
838836 OS << vni->def;
839837 unsigned ee = vni->kills.size();
840 if (ee || vni->hasPHIKill) {
838 if (ee || vni->hasPHIKill()) {
841839 OS << "-(";
842840 for (unsigned j = 0; j != ee; ++j) {
843841 OS << vni->kills[j];
844842 if (j != ee-1)
845843 OS << " ";
846844 }
847 if (vni->hasPHIKill) {
845 if (vni->hasPHIKill()) {
848846 if (ee)
849847 OS << " ";
850848 OS << "phi";
198198 // Remap the VNInfo def index, which works the same as the
199199 // start indices above. VN's with special sentinel defs
200200 // don't need to be remapped.
201 if (vni->def != ~0U && vni->def != ~1U) {
201 if (vni->isDefAccurate() && !vni->isUnused()) {
202202 unsigned index = vni->def / InstrSlots::NUM;
203203 unsigned offset = vni->def % InstrSlots::NUM;
204204 if (offset == InstrSlots::LOAD) {
446446 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
447447 CopyMI = mi;
448448 // Earlyclobbers move back one.
449 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
449 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
450450
451451 assert(ValNo->id == 0 && "First value in interval is not 0?");
452452
538538 // The new value number (#1) is defined by the instruction we claimed
539539 // defined value #0.
540540 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
541 false, // update at *
541542 VNInfoAllocator);
542
543 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
544
543545 // Value#0 is now defined by the 2-addr instruction.
544546 OldValNo->def = RedefIndex;
545547 OldValNo->copy = 0;
546548 if (MO.isEarlyClobber())
547 OldValNo->redefByEC = true;
549 OldValNo->setHasRedefByEC(true);
548550
549551 // Add the new live interval which replaces the range for the input copy.
550552 LiveRange LR(DefIndex, RedefIndex, ValNo);
576578 DOUT << " Removing [" << Start << "," << End << "] from: ";
577579 interval.print(DOUT, tri_); DOUT << "\n";
578580 interval.removeRange(Start, End);
579 VNI->hasPHIKill = true;
581 VNI->setHasPHIKill(true);
580582 DOUT << " RESULT: "; interval.print(DOUT, tri_);
581583
582584 // Replace the interval with one of a NEW value number. Note that this
583585 // value number isn't actually defined by an instruction, weird huh? :)
584 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
586 LiveRange LR(Start, End, interval.getNextValue(0, 0, false, VNInfoAllocator));
587 LR.valno->setIsPHIDef(true);
585588 DOUT << " replace range with " << LR;
586589 interval.addRange(LR);
587590 interval.addKill(LR.valno, End);
603606 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
604607 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
605608 CopyMI = mi;
606 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
609 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
607610
608611 unsigned killIndex = getMBBEndIdx(mbb) + 1;
609612 LiveRange LR(defIndex, killIndex, ValNo);
610613 interval.addRange(LR);
611614 interval.addKill(ValNo, killIndex);
612 ValNo->hasPHIKill = true;
615 ValNo->setHasPHIKill(true);
613616 DOUT << " +" << LR;
614617 }
615618 }
691694 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
692695 bool Extend = OldLR != interval.end();
693696 VNInfo *ValNo = Extend
694 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
697 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
695698 if (MO.isEarlyClobber() && Extend)
696 ValNo->redefByEC = true;
699 ValNo->setHasRedefByEC(true);
697700 LiveRange LR(start, end, ValNo);
698701 interval.addRange(LR);
699702 interval.addKill(LR.valno, end);
782785 }
783786 }
784787
785 LiveRange LR(start, end, interval.getNextValue(~0U, 0, VNInfoAllocator));
788 LiveRange LR(start, end, interval.getNextValue(0, 0, false, VNInfoAllocator));
786789 interval.addRange(LR);
787790 interval.addKill(LR.valno, end);
788791 DOUT << " +" << LR << '\n';
10981101 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
10991102 i != e; ++i) {
11001103 const VNInfo *VNI = *i;
1101 unsigned DefIdx = VNI->def;
1102 if (DefIdx == ~1U)
1104 if (VNI->isUnused())
11031105 continue; // Dead val#.
11041106 // Is the def for the val# rematerializable?
1105 if (DefIdx == ~0u)
1107 if (!VNI->isDefAccurate())
11061108 return false;
1107 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
1109 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
11081110 bool DefIsLoad = false;
11091111 if (!ReMatDefMI ||
11101112 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
14491451 if (HasUse) {
14501452 if (CreatedNewVReg) {
14511453 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1452 nI.getNextValue(~0U, 0, VNInfoAllocator));
1454 nI.getNextValue(0, 0, false, VNInfoAllocator));
14531455 DOUT << " +" << LR;
14541456 nI.addRange(LR);
14551457 } else {
14631465 }
14641466 if (HasDef) {
14651467 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1466 nI.getNextValue(~0U, 0, VNInfoAllocator));
1468 nI.getNextValue(0, 0, false, VNInfoAllocator));
14671469 DOUT << " +" << LR;
14681470 nI.addRange(LR);
14691471 }
18391841 unsigned index = getInstructionIndex(MI);
18401842 if (HasUse) {
18411843 LiveRange LR(getLoadIndex(index), getUseIndex(index),
1842 nI.getNextValue(~0U, 0, getVNInfoAllocator()));
1844 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
18431845 DOUT << " +" << LR;
18441846 nI.addRange(LR);
18451847 vrm.addRestorePoint(NewVReg, MI);
18461848 }
18471849 if (HasDef) {
18481850 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1849 nI.getNextValue(~0U, 0, getVNInfoAllocator()));
1851 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
18501852 DOUT << " +" << LR;
18511853 nI.addRange(LR);
18521854 vrm.addSpillPoint(NewVReg, true, MI);
19601962 i != e; ++i) {
19611963 const VNInfo *VNI = *i;
19621964 unsigned VN = VNI->id;
1963 unsigned DefIdx = VNI->def;
1964 if (DefIdx == ~1U)
1965 if (VNI->isUnused())
19651966 continue; // Dead val#.
19661967 // Is the def for the val# rematerializable?
1967 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1968 ? 0 : getInstructionFromIndex(DefIdx);
1968 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1969 ? getInstructionFromIndex(VNI->def) : 0;
19691970 bool dummy;
19701971 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
19711972 // Remember how to remat the def of this val#.
19761977 ReMatDefs[VN] = Clone;
19771978
19781979 bool CanDelete = true;
1979 if (VNI->hasPHIKill) {
1980 if (VNI->hasPHIKill()) {
19801981 // A kill is a phi node, not all of its uses can be rematerialized.
19811982 // It must not be deleted.
19821983 CanDelete = false;
22862287 LiveInterval& Interval = getOrCreateInterval(reg);
22872288 VNInfo* VN = Interval.getNextValue(
22882289 getInstructionIndex(startInst) + InstrSlots::DEF,
2289 startInst, getVNInfoAllocator());
2290 VN->hasPHIKill = true;
2290 startInst, true, getVNInfoAllocator());
2291 VN->setHasPHIKill(true);
22912292 VN->kills.push_back(getMBBEndIdx(startInst->getParent()));
22922293 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
22932294 getMBBEndIdx(startInst->getParent()) + 1, VN);
342342 if (CurrSLI->hasAtLeastOneValue())
343343 CurrSValNo = CurrSLI->getValNumInfo(0);
344344 else
345 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
345 CurrSValNo = CurrSLI->getNextValue(0, 0, false, LSs->getVNInfoAllocator());
346346 return SS;
347347 }
348348
636636 if (Phis.count(MBB)) return Phis[MBB];
637637
638638 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
639 VNInfo *RetVNI = Phis[MBB] = LI->getNextValue(~0U, /*FIXME*/ 0,
640 LIs->getVNInfoAllocator());
639 VNInfo *RetVNI = Phis[MBB] =
640 LI->getNextValue(0, /*FIXME*/ 0, false, LIs->getVNInfoAllocator());
641
641642 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
642643
643644 // If there are no uses or defs between our starting point and the
653654 IncomingVNs[*PI] = Incoming;
654655 }
655656
656 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill) {
657 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
657658 VNInfo* OldVN = RetVNI;
658659 VNInfo* NewVN = IncomingVNs.begin()->second;
659660 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
677678 // VNInfo to represent the joined value.
678679 for (DenseMap::iterator I =
679680 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
680 I->second->hasPHIKill = true;
681 I->second->setHasPHIKill(true);
681682 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
682683 if (!LiveInterval::isKill(I->second, KillIndex))
683684 LI->addKill(I->second, KillIndex);
729730 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
730731 DefIdx = LiveIntervals::getDefIndex(DefIdx);
731732
732 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
733 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
734 "Following NewVN isPHIDef flag incorrect. Fix me!");
735 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
733736
734737 // If the def is a move, set the copy field.
735738 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
792795
793796 // Bail out if we ever encounter a valno that has a PHI kill. We can't
794797 // renumber these.
795 if (OldVN->hasPHIKill) return;
798 if (OldVN->hasPHIKill()) return;
796799
797800 VNsToCopy.push_back(OldVN);
798801
822825 VNInfo* OldVN = *OI;
823826
824827 // Copy the valno over
825 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
826 LIs->getVNInfoAllocator());
827 NewLI.copyValNumInfo(NewVN, OldVN);
828 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
828829 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
829830
830831 // Remove the valno from the old interval
872873
873874 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
874875 unsigned KillIdx = 0;
875 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
876 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
876877 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
877878 else
878879 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
941942 if (CurrSLI->hasAtLeastOneValue())
942943 CurrSValNo = CurrSLI->getValNumInfo(0);
943944 else
944 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
945 CurrSValNo = CurrSLI->getNextValue(0, 0, false, LSs->getVNInfoAllocator());
945946 }
946947
947948 return FMI;
10311032 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
10321033 VNInfo *ValNo = LR->valno;
10331034
1034 if (ValNo->def == ~1U) {
1035 if (ValNo->isUnused()) {
10351036 // Defined by a dead def? How can this be?
10361037 assert(0 && "Val# is defined by a dead def?");
10371038 abort();
10381039 }
10391040
1040 MachineInstr *DefMI = (ValNo->def != ~0U)
1041 MachineInstr *DefMI = ValNo->isDefAccurate()
10411042 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
10421043
10431044 // If this would create a new join point, do not split.
10711072 unsigned SpillIndex = 0;
10721073 MachineInstr *SpillMI = NULL;
10731074 int SS = -1;
1074 if (ValNo->def == ~0U) {
1075 // If it's defined by a phi, we must split just before the barrier.
1075 if (!ValNo->isDefAccurate()) {
1076 // If we don't know where the def is we must split just before the barrier.
10761077 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
10771078 BarrierMBB, SS, RefsInMBB))) {
10781079 SpillIndex = LIs->getInstructionIndex(SpillMI);
12531254
12541255 // We don't currently try to handle definitions with PHI kills, because
12551256 // it would involve processing more than one VNInfo at once.
1256 if (CurrVN->hasPHIKill) continue;
1257 if (CurrVN->hasPHIKill()) continue;
12571258
12581259 // We also don't try to handle the results of PHI joins, since there's
12591260 // no defining instruction to analyze.
1260 unsigned DefIdx = CurrVN->def;
1261 if (DefIdx == ~0U || DefIdx == ~1U) continue;
1261 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
12621262
12631263 // We're only interested in eliminating cruft introduced by the splitter,
12641264 // is of the form load-use or load-use-store. First, check that the
12651265 // definition is a load, and remember what stack slot we loaded it from.
1266 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1266 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
12671267 int FrameIndex;
12681268 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
12691269
13821382 if (DefMBB == BarrierMBB)
13831383 return false;
13841384
1385 if (LR->valno->hasPHIKill)
1385 if (LR->valno->hasPHIKill())
13861386 return false;
13871387
13881388 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
357357 return Reg;
358358
359359 VNInfo *vni = cur.begin()->valno;
360 if (!vni->def || vni->def == ~1U || vni->def == ~0U)
360 if (!vni->def || vni->isUnused() || !vni->isDefAccurate())
361361 return Reg;
362362 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
363363 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg, PhysReg;
744744 if (SI.hasAtLeastOneValue())
745745 VNI = SI.getValNumInfo(0);
746746 else
747 VNI = SI.getNextValue(~0U, 0, ls_->getVNInfoAllocator());
747 VNI = SI.getNextValue(0, 0, false, ls_->getVNInfoAllocator());
748748
749749 LiveInterval &RI = li_->getInterval(cur->reg);
750750 // FIXME: This may be overly conservative.
920920 // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
921921 if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) {
922922 VNInfo *vni = cur->begin()->valno;
923 if (vni->def && vni->def != ~1U && vni->def != ~0U) {
923 if (vni->def && !vni->isUnused() && vni->isDefAccurate()) {
924924 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
925925 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
926926 if (CopyMI &&
650650 if (stackInterval.getNumValNums() != 0)
651651 vni = stackInterval.getValNumInfo(0);
652652 else
653 vni = stackInterval.getNextValue(-0U, 0, lss->getVNInfoAllocator());
653 vni = stackInterval.getNextValue(0, 0, false, lss->getVNInfoAllocator());
654654
655655 LiveInterval &rhsInterval = lis->getInterval(spilled->reg);
656656 stackInterval.MergeRangesInAsValue(rhsInterval, vni);
140140 // The live interval of ECX is represented as this:
141141 // %reg20,inf = [46,47:1)[174,230:0) 0@174-(230) 1@46-(47)
142142 // The coalescer has no idea there was a def in the middle of [174,230].
143 if (AValNo->redefByEC)
143 if (AValNo->hasRedefByEC())
144144 return false;
145145
146146 // If AValNo is defined as a copy from IntB, we can potentially process this.
202202 for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
203203 LiveInterval &SRLI = li_->getInterval(*SR);
204204 SRLI.addRange(LiveRange(FillerStart, FillerEnd,
205 SRLI.getNextValue(FillerStart, 0, li_->getVNInfoAllocator())));
205 SRLI.getNextValue(FillerStart, 0, true,
206 li_->getVNInfoAllocator())));
206207 }
207208 }
208209
303304 assert(ALR != IntA.end() && "Live range not found!");
304305 VNInfo *AValNo = ALR->valno;
305306 // If other defs can reach uses of this def, then it's not safe to perform
306 // the optimization.
307 if (AValNo->def == ~0U || AValNo->def == ~1U || AValNo->hasPHIKill)
307 // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
308 // tested?
309 if (AValNo->isPHIDef() || !AValNo->isDefAccurate() ||
310 AValNo->isUnused() || AValNo->hasPHIKill())
308311 return false;
309312 MachineInstr *DefMI = li_->getInstructionFromIndex(AValNo->def);
310313 const TargetInstrDesc &TID = DefMI->getDesc();
350353 unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
351354 NewMI->getOperand(OpIdx).setIsKill();
352355
353 bool BHasPHIKill = BValNo->hasPHIKill;
356 bool BHasPHIKill = BValNo->hasPHIKill();
354357 SmallVector BDeadValNos;
355358 SmallVector BKills;
356359 std::map BExtend;
402405 // extended to the end of the existing live range defined by the copy.
403406 unsigned DefIdx = li_->getDefIndex(UseIdx);
404407 const LiveRange *DLR = IntB.getLiveRangeContaining(DefIdx);
405 BHasPHIKill |= DLR->valno->hasPHIKill;
408 BHasPHIKill |= DLR->valno->hasPHIKill();
406409 assert(DLR->valno->def == DefIdx);
407410 BDeadValNos.push_back(DLR->valno);
408411 BExtend[DLR->start] = DLR->end;
461464 }
462465 }
463466 IntB.addKills(ValNo, BKills);
464 ValNo->hasPHIKill = BHasPHIKill;
467 ValNo->setHasPHIKill(BHasPHIKill);
465468
466469 DOUT << " result = "; IntB.print(DOUT, tri_);
467470 DOUT << "\n";
577580 assert(SrcLR != SrcInt.end() && "Live range not found!");
578581 VNInfo *ValNo = SrcLR->valno;
579582 // If other defs can reach uses of this def, then it's not safe to perform
580 // the optimization.
581 if (ValNo->def == ~0U || ValNo->def == ~1U || ValNo->hasPHIKill)
583 // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
584 // tested?
585 if (ValNo->isPHIDef() || !ValNo->isDefAccurate() ||
586 ValNo->isUnused() || ValNo->hasPHIKill())
582587 return false;
583588 MachineInstr *DefMI = li_->getInstructionFromIndex(ValNo->def);
584589 const TargetInstrDesc &TID = DefMI->getDesc();
670675 return false;
671676 unsigned KillIdx = li_->getMBBEndIdx(MBB) + 1;
672677 if (DstLR->valno->kills.size() == 1 &&
673 DstLR->valno->kills[0] == KillIdx && DstLR->valno->hasPHIKill)
678 DstLR->valno->kills[0] == KillIdx && DstLR->valno->hasPHIKill())
674679 return true;
675680 return false;
676681 }
934939 LiveInterval::iterator LR = li.FindLiveRangeContaining(CopyIdx);
935940 if (LR == li.end())
936941 return false;
937 if (LR->valno->hasPHIKill)
942 if (LR->valno->hasPHIKill())
938943 return false;
939944 if (LR->valno->def != CopyIdx)
940945 return false;
16811686 E = SavedLI->vni_end(); I != E; ++I) {
16821687 const VNInfo *ValNo = *I;
16831688 VNInfo *NewValNo = RealInt.getNextValue(ValNo->def, ValNo->copy,
1689 false, // updated at *
16841690 li_->getVNInfoAllocator());
1685 NewValNo->hasPHIKill = ValNo->hasPHIKill;
1686 NewValNo->redefByEC = ValNo->redefByEC;
1691 NewValNo->setFlags(ValNo->getFlags()); // * updated here.
16871692 RealInt.addKills(NewValNo, ValNo->kills);
16881693 RealInt.MergeValueInAsValue(*SavedLI, ValNo, NewValNo);
16891694 }
17221727 for (LiveInterval::const_vni_iterator i = ResSrcInt->vni_begin(),
17231728 e = ResSrcInt->vni_end(); i != e; ++i) {
17241729 const VNInfo *vni = *i;
1725 if (!vni->def || vni->def == ~1U || vni->def == ~0U)
1730 // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
1731 if (!vni->def || vni->isUnused() || vni->isPHIDef() || !vni->isDefAccurate())
17261732 continue;
17271733 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
17281734 unsigned NewSrcReg, NewDstReg, NewSrcSubIdx, NewDstSubIdx;
18691875 unsigned SrcReg = li_->getVNInfoSourceReg(LR->valno);
18701876 if (SrcReg == Reg)
18711877 return true;
1872 if (LR->valno->def == ~0U &&
1878 // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
1879 if ((LR->valno->isPHIDef() || !LR->valno->isDefAccurate()) &&
18731880 TargetRegisterInfo::isPhysicalRegister(li.reg) &&
18741881 *tri_->getSuperRegisters(li.reg)) {
18751882 // It's a sub-register live interval, we may not have precise information.
20382045
20392046 // Okay, the final step is to loop over the RHS live intervals, adding them to
20402047 // the LHS.
2041 LHSValNo->hasPHIKill |= VNI->hasPHIKill;
2048 if (VNI->hasPHIKill())
2049 LHSValNo->setHasPHIKill(true);
20422050 LHS.addKills(LHSValNo, VNI->kills);
20432051 LHS.MergeRangesInAsValue(RHS, LHSValNo);
20442052 LHS.weight += RHS.weight;
22052213 for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
22062214 i != e; ++i) {
22072215 VNInfo *VNI = *i;
2208 if (VNI->def == ~1U || VNI->copy == 0) // Src not defined by a copy?
2216 if (VNI->isUnused() || VNI->copy == 0) // Src not defined by a copy?
22092217 continue;
22102218
22112219 // DstReg is known to be a register in the LHS interval. If the src is
22222230 for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
22232231 i != e; ++i) {
22242232 VNInfo *VNI = *i;
2225 if (VNI->def == ~1U || VNI->copy == 0) // Src not defined by a copy?
2233 if (VNI->isUnused() || VNI->copy == 0) // Src not defined by a copy?
22262234 continue;
22272235
22282236 // DstReg is known to be a register in the RHS interval. If the src is
22422250 i != e; ++i) {
22432251 VNInfo *VNI = *i;
22442252 unsigned VN = VNI->id;
2245 if (LHSValNoAssignments[VN] >= 0 || VNI->def == ~1U)
2253 if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused())
22462254 continue;
22472255 ComputeUltimateVN(VNI, NewVNInfo,
22482256 LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
22522260 i != e; ++i) {
22532261 VNInfo *VNI = *i;
22542262 unsigned VN = VNI->id;
2255 if (RHSValNoAssignments[VN] >= 0 || VNI->def == ~1U)
2263 if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
22562264 continue;
22572265 // If this value number isn't a copy from the LHS, it's a new number.
22582266 if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
23162324 VNInfo *VNI = I->first;
23172325 unsigned LHSValID = LHSValNoAssignments[VNI->id];
23182326 LiveInterval::removeKill(NewVNInfo[LHSValID], VNI->def);
2319 NewVNInfo[LHSValID]->hasPHIKill |= VNI->hasPHIKill;
2327 if (VNI->hasPHIKill())
2328 NewVNInfo[LHSValID]->setHasPHIKill(true);
23202329 RHS.addKills(NewVNInfo[LHSValID], VNI->kills);
23212330 }
23222331
23262335 VNInfo *VNI = I->first;
23272336 unsigned RHSValID = RHSValNoAssignments[VNI->id];
23282337 LiveInterval::removeKill(NewVNInfo[RHSValID], VNI->def);
2329 NewVNInfo[RHSValID]->hasPHIKill |= VNI->hasPHIKill;
2338 if (VNI->hasPHIKill())
2339 NewVNInfo[RHSValID]->setHasPHIKill(true);
23302340 LHS.addKills(NewVNInfo[RHSValID], VNI->kills);
23312341 }
23322342
4646 tii = mf->getTarget().getInstrInfo();
4747 }
4848
49 /// Ensures there is space before the given machine instruction, returns the
50 /// instruction's new number.
51 unsigned makeSpaceBefore(MachineInstr *mi) {
52 if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) {
53 lis->scaleNumbering(2);
54 ls->scaleNumbering(2);
55 }
56
57 unsigned miIdx = lis->getInstructionIndex(mi);
58
59 assert(lis->hasGapBeforeInstr(miIdx));
60
61 return miIdx;
62 }
63
64 /// Ensure there is space after the given machine instruction, returns the
65 /// instruction's new number.
66 unsigned makeSpaceAfter(MachineInstr *mi) {
67 if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) {
68 lis->scaleNumbering(2);
69 ls->scaleNumbering(2);
70 }
71
72 unsigned miIdx = lis->getInstructionIndex(mi);
73
74 assert(lis->hasGapAfterInstr(miIdx));
75
76 return miIdx;
77 }
78
79
4980 /// Insert a store of the given vreg to the given stack slot immediately
5081 /// after the given instruction. Returns the base index of the inserted
5182 /// instruction. The caller is responsible for adding an appropriate
5283 /// LiveInterval to the LiveIntervals analysis.
5384 unsigned insertStoreFor(MachineInstr *mi, unsigned ss,
54 unsigned newVReg,
85 unsigned vreg,
5586 const TargetRegisterClass *trc) {
5687 MachineBasicBlock::iterator nextInstItr(mi);
5788 ++nextInstItr;
5889
59 if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) {
60 lis->scaleNumbering(2);
61 ls->scaleNumbering(2);
62 }
63
64 unsigned miIdx = lis->getInstructionIndex(mi);
65
66 assert(lis->hasGapAfterInstr(miIdx));
67
68 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, newVReg,
90 unsigned miIdx = makeSpaceAfter(mi);
91
92 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg,
6993 true, ss, trc);
7094 MachineBasicBlock::iterator storeInstItr(mi);
7195 ++storeInstItr;
85109 /// instruction. The caller is responsible for adding an appropriate
86110 /// LiveInterval to the LiveIntervals analysis.
87111 unsigned insertLoadFor(MachineInstr *mi, unsigned ss,
88 unsigned newVReg,
112 unsigned vreg,
89113 const TargetRegisterClass *trc) {
90114 MachineBasicBlock::iterator useInstItr(mi);
91
92 if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) {
93 lis->scaleNumbering(2);
94 ls->scaleNumbering(2);
95 }
96
97 unsigned miIdx = lis->getInstructionIndex(mi);
98
99 assert(lis->hasGapBeforeInstr(miIdx));
100
101 tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, newVReg, ss, trc);
115
116 unsigned miIdx = makeSpaceBefore(mi);
117
118 tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, vreg, ss, trc);
102119 MachineBasicBlock::iterator loadInstItr(mi);
103120 --loadInstItr;
104121 MachineInstr *loadInst = &*loadInstItr;
111128
112129 return loadInstIdx;
113130 }
114
115131
116132 /// Add spill ranges for every use/def of the live interval, inserting loads
117133 /// immediately before each use, and stores after each def. No folding is
177193 end = lis->getUseIndex(lis->getInstructionIndex(mi));
178194
179195 VNInfo *vni =
180 newLI->getNextValue(loadInstIdx, 0, lis->getVNInfoAllocator());
196 newLI->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator());
181197 vni->kills.push_back(lis->getInstructionIndex(mi));
182198 LiveRange lr(start, end, vni);
183199
190206 end = lis->getUseIndex(storeInstIdx);
191207
192208 VNInfo *vni =
193 newLI->getNextValue(storeInstIdx, 0, lis->getVNInfoAllocator());
209 newLI->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator());
194210 vni->kills.push_back(storeInstIdx);
195211 LiveRange lr(start, end, vni);
196212
199215
200216 added.push_back(newLI);
201217 }
202
203218
204219 return added;
205220 }
1717 class LiveStacks;
1818 class MachineFunction;
1919 class VirtRegMap;
20 class MachineInstr;
2021
2122 /// Spiller interface.
2223 ///
2526 class Spiller {
2627 public:
2728 virtual ~Spiller() = 0;
29
30 /// Spill the given live range. The method used will depend on the Spiller
31 /// implementation selected.
2832 virtual std::vector spill(LiveInterval *li) = 0;
33
2934 };
3035
3136 /// Create and return a spiller object, as specified on the command line.
826826 // Add a live range for the new vreg
827827 LiveInterval& Int = LI.getInterval(I->getOperand(i).getReg());
828828 VNInfo* FirstVN = *Int.vni_begin();
829 FirstVN->hasPHIKill = false;
829 FirstVN->setHasPHIKill(false);
830830 if (I->getOperand(i).isKill())
831831 FirstVN->kills.push_back(
832832 LiveIntervals::getUseIndex(LI.getInstructionIndex(I)));
885885 VNInfo* OldVN = R.valno;
886886 VNInfo*& NewVN = VNMap[OldVN];
887887 if (!NewVN) {
888 NewVN = LHS.getNextValue(OldVN->def,
889 OldVN->copy,
890 LI.getVNInfoAllocator());
891 NewVN->kills = OldVN->kills;
888 NewVN = LHS.createValueCopy(OldVN, LI.getVNInfoAllocator());
892889 }
893890
894891 LiveRange LR (R.start, R.end, NewVN);
986983 LiveInterval& Int = LI.getOrCreateInterval(I->first);
987984 const LiveRange* LR =
988985 Int.getLiveRangeContaining(LI.getMBBEndIdx(SI->second));
989 LR->valno->hasPHIKill = true;
986 LR->valno->setHasPHIKill(true);
990987
991988 I->second.erase(SI->first);
992989 }
10361033 // now has an unknown def.
10371034 unsigned idx = LI.getDefIndex(LI.getInstructionIndex(PInstr));
10381035 const LiveRange* PLR = PI.getLiveRangeContaining(idx);
1039 PLR->valno->def = ~0U;
1036 PLR->valno->setIsPHIDef(true);
10401037 LiveRange R (LI.getMBBStartIdx(PInstr->getParent()),
10411038 PLR->start, PLR->valno);
10421039 PI.addRange(R);