llvm.org GIT mirror llvm / 91725b7
avoid calling the virtual isMoveInstr method endlessly by caching its results. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29994 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
4 changed file(s) with 112 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
7777 float weight; // weight of this interval
7878 Ranges ranges; // the ranges in which this register is live
7979 private:
80 /// InstDefiningValue - This tracks the def index of the instruction that
81 /// defines a particular value number in the interval. This may be ~0,
82 /// which is treated as unknown, or ~1, which is a deleted value number.
83 SmallVector InstDefiningValue;
80 /// ValueNumberInfo - If this value number is not defined by a copy, this
81 /// holds ~0,x. If the value number is not in use, it contains ~1,x to
82 /// indicate that the value # is not used. If the val# is defined by a
83 /// copy, the first entry is the instruction # of the copy, and the second
84 /// is the register number copied from.
85 SmallVector, 4> ValueNumberInfo;
8486 public:
8587
8688 LiveInterval(unsigned Reg, float Weight)
112114 std::swap(reg, other.reg);
113115 std::swap(weight, other.weight);
114116 std::swap(ranges, other.ranges);
115 std::swap(InstDefiningValue, other.InstDefiningValue);
116 }
117
118 bool containsOneValue() const { return InstDefiningValue.size() == 1; }
119
120 unsigned getNumValNums() const { return InstDefiningValue.size(); }
117 std::swap(ValueNumberInfo, other.ValueNumberInfo);
118 }
119
120 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
121
122 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
121123
122124 /// getNextValue - Create a new value number and return it. MIIdx specifies
123125 /// the instruction that defines the value number.
124 unsigned getNextValue(unsigned MIIdx) {
125 InstDefiningValue.push_back(MIIdx);
126 return InstDefiningValue.size()-1;
126 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
127 ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
128 return ValueNumberInfo.size()-1;
127129 }
128130
129131 /// getInstForValNum - Return the machine instruction index that defines the
130132 /// specified value number.
131133 unsigned getInstForValNum(unsigned ValNo) const {
132 assert(ValNo < InstDefiningValue.size());
133 return InstDefiningValue[ValNo];
134 }
135
136 /// setInstDefiningValNum - Change the instruction defining the specified
137 /// value number to the specified instruction.
138 void setInstDefiningValNum(unsigned ValNo, unsigned MIIdx) {
139 InstDefiningValue[ValNo] = MIIdx;
134 assert(ValNo < ValueNumberInfo.size());
135 return ValueNumberInfo[ValNo].first;
136 }
137
138 unsigned getSrcRegForValNum(unsigned ValNo) const {
139 assert(ValNo < ValueNumberInfo.size());
140 if (ValueNumberInfo[ValNo].first < ~2U)
141 return ValueNumberInfo[ValNo].second;
142 return 0;
143 }
144
145 std::pair getValNumInfo(unsigned ValNo) const {
146 assert(ValNo < ValueNumberInfo.size());
147 return ValueNumberInfo[ValNo];
148 }
149
150 /// setValueNumberInfo - Change the value number info for the specified
151 /// value number.
152 void setValueNumberInfo(unsigned ValNo,
153 const std::pair &I){
154 ValueNumberInfo[ValNo] = I;
140155 }
141156
142157 /// MergeValueNumberInto - This method is called when two value nubmers
215230 /// the intervals are not joinable, this aborts.
216231 void join(LiveInterval &Other, int *ValNoAssignments,
217232 int *RHSValNoAssignments,
218 SmallVector<unsigned, 16> &NewInstDefiningValue);
233 SmallVector<std::pair,16> &NewValueNumberInfo);
219234
220235
221236 /// removeRange - Remove the specified range from this interval. Note that
197197 /// def.
198198 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
199199 MachineBasicBlock::iterator mi,
200 LiveInterval& interval,
201 bool isLiveIn = false);
200 LiveInterval &interval,
201 unsigned SrcReg);
202202
203203 /// Return true if the two specified registers belong to different
204204 /// register classes. The registers may be either phys or virt regs.
279279 /// the intervals are not joinable, this aborts.
280280 void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
281281 int *RHSValNoAssignments,
282 SmallVector<unsigned, 16> &NewInstDefiningValue) {
282 SmallVector<std::pair
283 unsigned>, 16> &NewValueNumberInfo) {
283284
284285 // Try to do the least amount of work possible. In particular, if there are
285286 // more liverange chunks in the other set than there are in the 'this' set,
299300 // we want to avoid the interval scan if not.
300301 bool MustMapCurValNos = false;
301302 for (unsigned i = 0, e = getNumValNums(); i != e; ++i) {
302 if (InstDefiningValue[i] == ~2U) continue; // tombstone value #
303 if (ValueNumberInfo[i].first == ~2U) continue; // tombstone value #
303304 if (i != (unsigned)LHSValNoAssignments[i]) {
304305 MustMapCurValNos = true;
305306 break;
344345 InsertPos = addRangeFrom(*I, InsertPos);
345346 }
346347
347 InstDefiningValue.clear();
348 InstDefiningValue.append(NewInstDefiningValue.begin(),
349 NewInstDefiningValue.end());
348 ValueNumberInfo.clear();
349 ValueNumberInfo.append(NewValueNumberInfo.begin(), NewValueNumberInfo.end());
350350 weight += Other.weight;
351351 }
352352
359359 // Find a value # to use for the clobber ranges. If there is already a value#
360360 // for unknown values, use it.
361361 // FIXME: Use a single sentinal number for these!
362 unsigned ClobberValNo = getNextValue(~0U);
362 unsigned ClobberValNo = getNextValue(~0U, 0);
363363
364364 iterator IP = begin();
365365 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
398398
399399 // Make sure V2 is smaller than V1.
400400 if (V1 < V2) {
401 setInstDefiningValNum(V1, getInstForValNum(V2));
401 setValueNumberInfo(V1, getValNumInfo(V2));
402402 std::swap(V1, V2);
403403 }
404404
442442 // ~1U so it can be nuked later.
443443 if (V1 == getNumValNums()-1) {
444444 do {
445 InstDefiningValue.pop_back();
446 } while (InstDefiningValue.back() == ~1U);
445 ValueNumberInfo.pop_back();
446 } while (ValueNumberInfo.back().first == ~1U);
447447 } else {
448 InstDefiningValue[V1] = ~1U;
448 ValueNumberInfo[V1].first = ~1U;
449449 }
450450 }
451451
481481 for (unsigned i = 0; i != getNumValNums(); ++i) {
482482 if (i) OS << " ";
483483 OS << i << "@";
484 if (InstDefiningValue[i] == ~0U) {
484 if (ValueNumberInfo[i].first == ~0U) {
485485 OS << "?";
486486 } else {
487 OS << InstDefiningValue[i];
487 OS << ValueNumberInfo[i].first;
488488 }
489489 }
490490 }
137137 for (MachineFunction::livein_iterator I = fn.livein_begin(),
138138 E = fn.livein_end(); I != E; ++I) {
139139 handlePhysicalRegisterDef(Entry, Entry->begin(),
140 getOrCreateInterval(I->first), true);
140 getOrCreateInterval(I->first), 0);
141141 for (const unsigned* AS = mri_->getAliasSet(I->first); *AS; ++AS)
142142 handlePhysicalRegisterDef(Entry, Entry->begin(),
143 getOrCreateInterval(*AS), true);
143 getOrCreateInterval(*AS), 0);
144144 }
145145 }
146146
320320 // the spill weight is now infinity as it
321321 // cannot be spilled again
322322 nI.weight = float(HUGE_VAL);
323 LiveRange LR(start, end, nI.getNextValue(~0U));
323 LiveRange LR(start, end, nI.getNextValue(~0U, 0));
324324 DEBUG(std::cerr << " +" << LR);
325325 nI.addRange(LR);
326326 added.push_back(&nI);
365365 // Get the Idx of the defining instructions.
366366 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
367367
368 unsigned ValNum = interval.getNextValue(defIndex);
368 unsigned ValNum;
369 unsigned SrcReg, DstReg;
370 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
371 ValNum = interval.getNextValue(~0U, 0);
372 else
373 ValNum = interval.getNextValue(defIndex, SrcReg);
374
369375 assert(ValNum == 0 && "First value in interval is not 0?");
370376 ValNum = 0; // Clue in the optimizer.
371377
454460 // that at this point, there should be exactly one value number in it.
455461 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
456462
457 // The new value number is defined by the instruction we claimed defined
458 // value #0.
459 unsigned ValNo = interval.getNextValue(DefIndex);
463 // The new value number (#1) is defined by the instruction we claimed
464 // defined value #0.
465 unsigned ValNo = interval.getNextValue(0, 0);
466 interval.setValueNumberInfo(1, interval.getValNumInfo(0));
460467
461 // Value#1 is now defined by the 2-addr instruction.
462 interval.setInstDefiningValNum(0, RedefIndex);
468 // Value#0 is now defined by the 2-addr instruction.
469 interval.setValueNumberInfo(0, std::make_pair(~0U, 0U));
463470
464471 // Add the new live interval which replaces the range for the input copy.
465472 LiveRange LR(DefIndex, RedefIndex, ValNo);
492499
493500 // Replace the interval with one of a NEW value number. Note that this
494501 // value number isn't actually defined by an instruction, weird huh? :)
495 LiveRange LR(Start, End, interval.getNextValue(~0U));
502 LiveRange LR(Start, End, interval.getNextValue(~0U, 0));
496503 DEBUG(std::cerr << " replace range with " << LR);
497504 interval.addRange(LR);
498505 DEBUG(std::cerr << "RESULT: "; interval.print(std::cerr, mri_));
502509 // live until the end of the block. We've already taken care of the
503510 // rest of the live range.
504511 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
512
513 unsigned ValNum;
514 unsigned SrcReg, DstReg;
515 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
516 ValNum = interval.getNextValue(~0U, 0);
517 else
518 ValNum = interval.getNextValue(defIndex, SrcReg);
519
505520 LiveRange LR(defIndex,
506 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
507 interval.getNextValue(defIndex));
521 getInstructionIndex(&mbb->back()) + InstrSlots::NUM, ValNum);
508522 interval.addRange(LR);
509523 DEBUG(std::cerr << " +" << LR);
510524 }
515529
516530 void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
517531 MachineBasicBlock::iterator mi,
518 LiveInterval& interval,
519 bool isLiveIn) {
532 LiveInterval &interval,
533 unsigned SrcReg) {
520534 // A physical register cannot be live across basic block, so its
521535 // lifetime must end somewhere in its defining basic block.
522536 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
550564 // The only case we should have a dead physreg here without a killing or
551565 // instruction where we know it's dead is if it is live-in to the function
552566 // and never used.
553 assert(isLiveIn && "physreg was not killed in defining block!");
567 assert(!SrcReg && "physreg was not killed in defining block!");
554568 end = getDefIndex(start) + 1; // It's dead.
555569
556570 exit:
557571 assert(start < end && "did not find end of interval?");
558572
559 LiveRange LR(start, end, interval.getNextValue(isLiveIn ? ~0U : start));
573 LiveRange LR(start, end, interval.getNextValue(SrcReg != 0 ? start : ~0U,
574 SrcReg));
560575 interval.addRange(LR);
561576 DEBUG(std::cerr << " +" << LR << '\n');
562577 }
567582 if (MRegisterInfo::isVirtualRegister(reg))
568583 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
569584 else if (allocatableRegs_[reg]) {
570 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg));
585 unsigned SrcReg, DstReg;
586 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
587 SrcReg = 0;
588 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg), SrcReg);
571589 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
572 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS), true);
590 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS), 0);
573591 }
574592 }
575593
651669 // If AValNo is defined as a copy from IntB, we can potentially process this.
652670
653671 // Get the instruction that defines this value number.
654 unsigned AValNoInstIdx = IntA.getInstForValNum(AValNo);
655
656 // If it's unknown, ignore it.
657 if (AValNoInstIdx == ~0U || AValNoInstIdx == ~1U) return false;
658 // Otherwise, get the instruction for it.
659 MachineInstr *AValNoInstMI = getInstructionFromIndex(AValNoInstIdx);
672 unsigned SrcReg = IntA.getSrcRegForValNum(AValNo);
673 if (!SrcReg) return false; // Not defined by a copy.
660674
661675 // If the value number is not defined by a copy instruction, ignore it.
662 unsigned SrcReg, DstReg;
663 if (!tii_->isMoveInstr(*AValNoInstMI, SrcReg, DstReg))
664 return false;
665676
666677 // If the source register comes from an interval other than IntB, we can't
667678 // handle this.
668 assert(rep(DstReg) == IntA.reg && "Not defining a reg in IntA?");
669679 if (rep(SrcReg) != IntB.reg) return false;
670
680
671681 // Get the LiveRange in IntB that this value number starts with.
682 unsigned AValNoInstIdx = IntA.getInstForValNum(AValNo);
672683 LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNoInstIdx-1);
673684
674685 // Make sure that the end of the live range is inside the same block as
686697
687698 // We are about to delete CopyMI, so need to remove it as the 'instruction
688699 // that defines this value #'.
689 IntB.setInstDefiningValNum(BValNo, ~0U);
700 IntB.setValueNumberInfo(BValNo, std::make_pair(~0U, 0));
690701
691702 // Okay, we can merge them. We need to insert a new liverange:
692703 // [ValLR.end, BLR.begin) of either value number, then we merge the
700711 for (const unsigned *AS = mri_->getAliasSet(IntB.reg); *AS; ++AS) {
701712 LiveInterval &AliasLI = getInterval(*AS);
702713 AliasLI.addRange(LiveRange(FillerStart, FillerEnd,
703 AliasLI.getNextValue(~0U)));
714 AliasLI.getNextValue(~0U, 0)));
704715 }
705716 }
706717
831842 /// contains the value number the copy is from.
832843 ///
833844 static unsigned ComputeUltimateVN(unsigned VN,
834 SmallVector<unsigned, 16> &InstDefiningValue,
845 SmallVector<std::pair,
846 unsigned>, 16> &ValueNumberInfo,
835847 SmallVector &ThisFromOther,
836848 SmallVector &OtherFromThis,
837849 SmallVector &ThisValNoAssignments,
846858 // number in the destination.
847859 int OtherValNo = ThisFromOther[VN];
848860 if (OtherValNo == -1) {
849 InstDefiningValue.push_back(ThisLI.getInstForValNum(VN));
850 return ThisValNoAssignments[VN] = InstDefiningValue.size()-1;
861 ValueNumberInfo.push_back(ThisLI.getValNumInfo(VN));
862 return ThisValNoAssignments[VN] = ValueNumberInfo.size()-1;
851863 }
852864
853865 // Otherwise, this *is* a copy from the RHS. Mark this value number as
855867 // value is.
856868 ThisValNoAssignments[VN] = -2;
857869 unsigned UltimateVN =
858 ComputeUltimateVN(OtherValNo, InstDefiningValue,
870 ComputeUltimateVN(OtherValNo, ValueNumberInfo,
859871 OtherFromThis, ThisFromOther,
860872 OtherValNoAssignments, ThisValNoAssignments,
861873 OtherLI, ThisLI);
874886 SmallVector LHSValsDefinedFromRHS;
875887 LHSValsDefinedFromRHS.resize(LHS.getNumValNums(), -1);
876888 for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) {
877 unsigned ValInst = LHS.getInstForValNum(VN);
878 if (ValInst == ~0U || ValInst == ~1U)
889 unsigned ValSrcReg = LHS.getSrcRegForValNum(VN);
890 if (ValSrcReg == 0) // Src not defined by a copy?
879891 continue;
880
881 // If the instruction defining the LHS's value is a copy.
882 MachineInstr *ValInstMI = getInstructionFromIndex(ValInst);
883
884 // If the value number is not defined by a copy instruction, ignore it.
885 unsigned SrcReg, DstReg;
886 if (!tii_->isMoveInstr(*ValInstMI, SrcReg, DstReg))
887 continue;
888
892
889893 // DstReg is known to be a register in the LHS interval. If the src is from
890894 // the RHS interval, we can use its value #.
891 if (rep(SrcReg) != RHS.reg)
895 if (rep(ValSrcReg) != RHS.reg)
892896 continue;
893
897
894898 // Figure out the value # from the RHS.
899 unsigned ValInst = LHS.getInstForValNum(VN);
895900 LHSValsDefinedFromRHS[VN] = RHS.getLiveRangeContaining(ValInst-1)->ValId;
896901 }
897902
900905 SmallVector RHSValsDefinedFromLHS;
901906 RHSValsDefinedFromLHS.resize(RHS.getNumValNums(), -1);
902907 for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) {
903 unsigned ValInst = RHS.getInstForValNum(VN);
904 if (ValInst == ~0U || ValInst == ~1U)
905 continue;
906
907 // If the instruction defining the RHS's value is a copy.
908 MachineInstr *ValInstMI = getInstructionFromIndex(ValInst);
909
910 // If the value number is not defined by a copy instruction, ignore it.
911 unsigned SrcReg, DstReg;
912 if (!tii_->isMoveInstr(*ValInstMI, SrcReg, DstReg))
908 unsigned ValSrcReg = RHS.getSrcRegForValNum(VN);
909 if (ValSrcReg == 0) // Src not defined by a copy?
913910 continue;
914911
915912 // DstReg is known to be a register in the RHS interval. If the src is from
916913 // the LHS interval, we can use its value #.
917 if (rep(SrcReg) != LHS.reg)
914 if (rep(ValSrcReg) != LHS.reg)
918915 continue;
919916
920917 // Figure out the value # from the LHS.
918 unsigned ValInst = RHS.getInstForValNum(VN);
921919 RHSValsDefinedFromLHS[VN] = LHS.getLiveRangeContaining(ValInst-1)->ValId;
922920 }
923921
925923 // assuming that the live ranges can be coallesced.
926924 SmallVector LHSValNoAssignments;
927925 SmallVector RHSValNoAssignments;
928 SmallVector<unsigned, 16> InstDefiningValue;
926 SmallVector<std::pair, 16> ValueNumberInfo;
929927 LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
930928 RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
931929
932930 // Compute ultimate value numbers for the LHS and RHS values.
933931 for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) {
934932 if (LHS.getInstForValNum(VN) == ~2U) continue;
935 ComputeUltimateVN(VN, InstDefiningValue,
933 ComputeUltimateVN(VN, ValueNumberInfo,
936934 LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
937935 LHSValNoAssignments, RHSValNoAssignments, LHS, RHS);
938936 }
939937 for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) {
940938 if (RHS.getInstForValNum(VN) == ~2U) continue;
941 ComputeUltimateVN(VN, InstDefiningValue,
939 ComputeUltimateVN(VN, ValueNumberInfo,
942940 RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
943941 RHSValNoAssignments, LHSValNoAssignments, RHS, LHS);
944942 }
988986 // If we get here, we know that we can coallesce the live ranges. Ask the
989987 // intervals to coallesce themselves now.
990988 LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0],
991 InstDefiningValue);
989 ValueNumberInfo);
992990 return true;
993991 }
994992