llvm.org GIT mirror llvm / 1a66f0a
Recover most of the compile time regression due to recent live interval changes. 1. Eliminate the costly live interval "swapping". 2. Change ValueNumberInfo container from SmallVector to std::vector. The former performs slowly when the vector size is very large. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41536 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 12 years ago
4 changed file(s) with 102 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
9898 VNInfo() : def(~1U), reg(0) {}
9999 VNInfo(unsigned d, unsigned r) : def(d), reg(r) {}
100100 };
101 private:
102 SmallVector ValueNumberInfo;
101
102 typedef std::vector VNInfoList;
103 VNInfoList ValueNumberInfo;
104
103105 public:
104
105106 LiveInterval(unsigned Reg, float Weight)
106107 : reg(Reg), preference(0), weight(Weight) {
108 }
109
110 ~LiveInterval() {
111 ValueNumberInfo.clear();
107112 }
108113
109114 typedef Ranges::iterator iterator;
114119 const_iterator begin() const { return ranges.begin(); }
115120 const_iterator end() const { return ranges.end(); }
116121
122 typedef VNInfoList::iterator vni_iterator;
123 vni_iterator vni_begin() { return ValueNumberInfo.begin(); }
124 vni_iterator vni_end() { return ValueNumberInfo.end(); }
125
126 typedef VNInfoList::const_iterator const_vni_iterator;
127 const_vni_iterator vni_begin() const { return ValueNumberInfo.begin(); }
128 const_vni_iterator vni_end() const { return ValueNumberInfo.end(); }
117129
118130 /// advanceTo - Advance the specified iterator to point to the LiveRange
119131 /// containing the specified position, or end() if the position is past the
127139 return I;
128140 }
129141
130 void swap(LiveInterval& other) {
131 std::swap(reg, other.reg);
132 std::swap(weight, other.weight);
133 std::swap(ranges, other.ranges);
134 std::swap(ValueNumberInfo, other.ValueNumberInfo);
135 }
136
137142 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
138143
139144 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
140145
146 /// getValNumInfo - Returns a copy of the specified val#.
147 ///
148 inline VNInfo& getValNumInfo(unsigned ValNo) {
149 return ValueNumberInfo[ValNo];
150 }
151 inline const VNInfo& getValNumInfo(unsigned ValNo) const {
152 return ValueNumberInfo[ValNo];
153 }
154
155 /// copyValNumInfo - Copy the value number info for one value number to
156 /// another.
157 void copyValNumInfo(unsigned DstValNo, unsigned SrcValNo) {
158 VNInfo &vnd = getValNumInfo(DstValNo);
159 const VNInfo &vns = getValNumInfo(SrcValNo);
160 vnd.def = vns.def;
161 vnd.reg = vns.reg;
162 vnd.kills = vns.kills;
163 }
164 void copyValNumInfo(unsigned DstValNo, const LiveInterval &SrcLI,
165 unsigned SrcValNo) {
166 VNInfo &vnd = getValNumInfo(DstValNo);
167 const VNInfo &vns = SrcLI.getValNumInfo(SrcValNo);
168 vnd.def = vns.def;
169 vnd.reg = vns.reg;
170 vnd.kills = vns.kills;
171 }
172
141173 /// getNextValue - Create a new value number and return it. MIIdx specifies
142174 /// the instruction that defines the value number.
143175 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
148180 /// getDefForValNum - Return the machine instruction index that defines the
149181 /// specified value number.
150182 unsigned getDefForValNum(unsigned ValNo) const {
151 assert(ValNo < ValueNumberInfo.size());
152 return ValueNumberInfo[ValNo].def;
183 return getValNumInfo(ValNo).def;
153184 }
154185
155186 /// getSrcRegForValNum - If the machine instruction that defines the
156187 /// specified value number is a copy, returns the source register. Otherwise,
157188 /// returns zero.
158189 unsigned getSrcRegForValNum(unsigned ValNo) const {
159 assert(ValNo < ValueNumberInfo.size());
160 return ValueNumberInfo[ValNo].reg;
190 return getValNumInfo(ValNo).reg;
161191 }
162192
163193 /// setDefForValNum - Set the machine instruction index that defines the
164194 /// specified value number.
165195 void setDefForValNum(unsigned ValNo, unsigned NewDef) {
166 assert(ValNo < ValueNumberInfo.size());
167 ValueNumberInfo[ValNo].def = NewDef;
196 getValNumInfo(ValNo).def = NewDef;
168197 }
169198
170199 /// setSrcRegForValNum - Set the source register of the specified value
171200 /// number.
172201 void setSrcRegForValNum(unsigned ValNo, unsigned NewReg) {
173 assert(ValNo < ValueNumberInfo.size());
174 ValueNumberInfo[ValNo].reg = NewReg;
202 getValNumInfo(ValNo).reg = NewReg;
175203 }
176204
177205 /// getKillsForValNum - Return the kill instruction indexes of the specified
178206 /// value number.
179207 const SmallVector &getKillsForValNum(unsigned ValNo) const {
180 assert(ValNo < ValueNumberInfo.size());
181 return ValueNumberInfo[ValNo].kills;
208 return getValNumInfo(ValNo).kills;
182209 }
183210
184211 /// addKillForValNum - Add a kill instruction index to the specified value
185212 /// number.
186213 void addKillForValNum(unsigned ValNo, unsigned KillIdx) {
187 assert(ValNo < ValueNumberInfo.size());
188 SmallVector &kills = ValueNumberInfo[ValNo].kills;
214 SmallVector &kills = getValNumInfo(ValNo).kills;
189215 if (kills.empty()) {
190216 kills.push_back(KillIdx);
191217 } else {
212238 /// the specified value number.
213239 void addKillsForValNum(unsigned ValNo,
214240 const SmallVector &kills) {
215 addKills(ValueNumberInfo[ValNo], kills);
241 addKills(getValNumInfo(ValNo), kills);
216242 }
217243
218244 /// isKillForValNum - Returns true if KillIdx is a kill of the specified
219245 /// val#.
220246 bool isKillForValNum(unsigned ValNo, unsigned KillIdx) const {
221 assert(ValNo < ValueNumberInfo.size());
222 const SmallVector &kills = ValueNumberInfo[ValNo].kills;
247 const SmallVector &kills = getValNumInfo(ValNo).kills;
223248 SmallVector::const_iterator
224249 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
225250 if (I == kills.end())
243268 /// removeKillForValNum - Remove the specified kill from the list of kills
244269 /// of the specified val#.
245270 bool removeKillForValNum(unsigned ValNo, unsigned KillIdx) {
246 assert(ValNo < ValueNumberInfo.size());
247 return removeKill(ValueNumberInfo[ValNo], KillIdx);
271 return removeKill(getValNumInfo(ValNo), KillIdx);
248272 }
249273
250274 /// removeKillForValNum - Remove all the kills in specified range
251275 /// [Start, End] of the specified val#.
252276 void removeKillForValNum(unsigned ValNo, unsigned Start, unsigned End) {
253 assert(ValNo < ValueNumberInfo.size());
254 SmallVector &kills = ValueNumberInfo[ValNo].kills;
277 SmallVector &kills = getValNumInfo(ValNo).kills;
255278 SmallVector::iterator
256279 I = std::lower_bound(kills.begin(), kills.end(), Start);
257280 SmallVector::iterator
277300 bool replaceKillForValNum(unsigned ValNo, unsigned OldKill,
278301 unsigned NewKill) {
279302 assert(ValNo < ValueNumberInfo.size());
280 return replaceKill(ValueNumberInfo[ValNo], OldKill, NewKill);
281 }
282
283 /// getValNumInfo - Returns a copy of the specified val#.
284 ///
285 VNInfo getValNumInfo(unsigned ValNo) const {
286 assert(ValNo < ValueNumberInfo.size());
287 return ValueNumberInfo[ValNo];
288 }
289
290 /// setValNumInfo - Change the value number info for the specified
291 /// value number.
292 void setValNumInfo(unsigned ValNo, const VNInfo &I) {
293 ValueNumberInfo[ValNo] = I;
294 }
295
296 /// copyValNumInfo - Copy the value number info for one value number to
297 /// another.
298 void copyValNumInfo(unsigned DstValNo, unsigned SrcValNo) {
299 ValueNumberInfo[DstValNo] = ValueNumberInfo[SrcValNo];
300 }
301 void copyValNumInfo(unsigned DstValNo, const LiveInterval &SrcLI,
302 unsigned SrcValNo) {
303 ValueNumberInfo[DstValNo] = SrcLI.ValueNumberInfo[SrcValNo];
304 }
305
303 return replaceKill(getValNumInfo(ValNo), OldKill, NewKill);
304 }
305
306306 /// MergeValueNumberInto - This method is called when two value nubmers
307307 /// are found to be equivalent. This eliminates V1, replacing all
308308 /// LiveRanges with the V1 value number with the V2 value number. This can
104104 /// physreg, this method always canonicalizes DestInt to be it. The output
105105 /// "SrcInt" will not have been modified, so we can use this information
106106 /// below to update aliases.
107 bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS);
107 bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped);
108108
109109 /// SimpleJoin - Attempt to join the specified interval into this one. The
110110 /// caller of this method must guarantee that the RHS only contains a single
287287 void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
288288 int *RHSValNoAssignments,
289289 SmallVector &NewValueNumberInfo) {
290
291 // Try to do the least amount of work possible. In particular, if there are
292 // more liverange chunks in the other set than there are in the 'this' set,
293 // swap sets to merge the fewest chunks in possible.
294 //
295 // Also, if one range is a physreg and one is a vreg, we always merge from the
296 // vreg into the physreg, which leaves the vreg intervals pristine.
297 if ((Other.ranges.size() > ranges.size() &&
298 MRegisterInfo::isVirtualRegister(reg)) ||
299 MRegisterInfo::isPhysicalRegister(Other.reg)) {
300 swap(Other);
301 std::swap(LHSValNoAssignments, RHSValNoAssignments);
302 }
303290
304291 // Determine if any of our live range values are mapped. This is uncommon, so
305292 // we want to avoid the interval scan if not.
306293 bool MustMapCurValNos = false;
307294 for (unsigned i = 0, e = getNumValNums(); i != e; ++i) {
308 if (ValueNumberInfo[i].def == ~1U) continue; // tombstone value #
295 if (getDefForValNum(i) == ~1U) continue; // tombstone value #
309296 if (i != (unsigned)LHSValNoAssignments[i]) {
310297 MustMapCurValNos = true;
311298 break;
344331
345332 // Update val# info first. Increasing live ranges may invalidate some kills.
346333 ValueNumberInfo.clear();
347 ValueNumberInfo.append(NewValueNumberInfo.begin(), NewValueNumberInfo.end());
334 for (SmallVector::iterator I = NewValueNumberInfo.begin(),
335 E = NewValueNumberInfo.end(); I != E; ++I)
336 ValueNumberInfo.push_back(*I);
348337
349338 // Okay, now insert the RHS live ranges into the LHS.
350339 iterator InsertPos = begin();
471460 ValueNumberInfo.pop_back();
472461 } while (ValueNumberInfo.back().def == ~1U);
473462 } else {
474 ValueNumberInfo[V1].def = ~1U;
463 setDefForValNum(V1, ~1U);
475464 }
476465 }
477466
510499 // Print value number info.
511500 if (getNumValNums()) {
512501 OS << " ";
513 for (unsigned i = 0; i != getNumValNums(); ++i) {
514 if (i) OS << " ";
515 OS << i << "@";
516 if (ValueNumberInfo[i].def == ~1U) {
502 unsigned vnum = 0;
503 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
504 ++i, ++vnum) {
505 const VNInfo &vni = *i;
506 if (vnum) OS << " ";
507 OS << vnum << "@";
508 if (vni.def == ~1U) {
517509 OS << "x";
518510 } else {
519 if (ValueNumberInfo[i].def == ~0U)
511 if (vni.def == ~0U)
520512 OS << "?";
521513 else
522 OS << ValueNumberInfo[i].def;
523 unsigned e = ValueNumberInfo[i].kills.size();
524 if (e) {
514 OS << vni.def;
515 unsigned ee = vni.kills.size();
516 if (ee) {
525517 OS << "-(";
526 for (unsigned j = 0; j != e; ++j) {
527 OS << ValueNumberInfo[i].kills[j];
528 if (j != e-1)
518 for (unsigned j = 0; j != ee; ++j) {
519 OS << vni.kills[j];
520 if (j != ee-1)
529521 OS << " ";
530522 }
531523 OS << ")";
308308 // Otherwise, if one of the intervals being joined is a physreg, this method
309309 // always canonicalizes DstInt to be it. The output "SrcInt" will not have
310310 // been modified, so we can use this information below to update aliases.
311 if (JoinIntervals(DstInt, SrcInt)) {
311 bool Swapped = false;
312 if (JoinIntervals(DstInt, SrcInt, Swapped)) {
312313 if (isDead) {
313314 // Result of the copy is dead. Propagate this property.
314315 if (SrcStart == 0) {
329330
330331 if (isShorten || isDead) {
331332 // Shorten the destination live interval.
332 if (repSrcReg == DstInt.reg)
333 DstInt.removeRange(RemoveStart, RemoveEnd);
333 if (Swapped)
334 SrcInt.removeRange(RemoveStart, RemoveEnd);
334335 }
335336 } else {
336337 // Coalescing failed.
344345 return false;
345346 }
346347
347 bool Swapped = repSrcReg == DstInt.reg;
348 if (Swapped)
348 LiveInterval *ResSrcInt = &SrcInt;
349 LiveInterval *ResDstInt = &DstInt;
350 if (Swapped) {
349351 std::swap(repSrcReg, repDstReg);
352 std::swap(ResSrcInt, ResDstInt);
353 }
350354 assert(MRegisterInfo::isVirtualRegister(repSrcReg) &&
351355 "LiveInterval::join didn't work right!");
352356
355359 // have clobbered values for this range.
356360 if (MRegisterInfo::isPhysicalRegister(repDstReg)) {
357361 // Unset unnecessary kills.
358 if (!DstInt.containsOneValue()) {
359 for (LiveInterval::Ranges::const_iterator I = SrcInt.begin(),
360 E = SrcInt.end(); I != E; ++I)
362 if (!ResDstInt->containsOneValue()) {
363 for (LiveInterval::Ranges::const_iterator I = ResSrcInt->begin(),
364 E = ResSrcInt->end(); I != E; ++I)
361365 unsetRegisterKills(I->start, I->end, repDstReg);
362366 }
363367
364368 // Update the liveintervals of sub-registers.
365369 for (const unsigned *AS = mri_->getSubRegisters(repDstReg); *AS; ++AS)
366 li_->getInterval(*AS).MergeInClobberRanges(SrcInt);
370 li_->getInterval(*AS).MergeInClobberRanges(*ResSrcInt);
367371 } else {
368372 // Merge use info if the destination is a virtual register.
369373 LiveVariables::VarInfo& dVI = lv_->getVarInfo(repDstReg);
371375 dVI.NumUses += sVI.NumUses;
372376 }
373377
374 DOUT << "\n\t\tJoined. Result = "; DstInt.print(DOUT, mri_);
378 DOUT << "\n\t\tJoined. Result = "; ResDstInt->print(DOUT, mri_);
375379 DOUT << "\n";
376380
377381 // Remember these liveintervals have been joined.
378382 JoinedLIs.set(repSrcReg - MRegisterInfo::FirstVirtualRegister);
379383 if (MRegisterInfo::isVirtualRegister(repDstReg))
380384 JoinedLIs.set(repDstReg - MRegisterInfo::FirstVirtualRegister);
381
382 // If the intervals were swapped by Join, swap them back so that the register
383 // mapping (in the r2i map) is correct.
384 if (Swapped) SrcInt.swap(DstInt);
385385
386386 // repSrcReg is guarateed to be the register whose live interval that is
387387 // being merged.
585585 /// physreg, this method always canonicalizes LHS to be it. The output
586586 /// "RHS" will not have been modified, so we can use this information
587587 /// below to update aliases.
588 bool SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS, LiveInterval &RHS) {
588 bool SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS,
589 LiveInterval &RHS, bool &Swapped) {
589590 // Compute the final value assignment, assuming that the live ranges can be
590591 // coalesced.
591592 SmallVector LHSValNoAssignments;
814815
815816 // If we get here, we know that we can coalesce the live ranges. Ask the
816817 // intervals to coalesce themselves now.
817 LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0],
818 ValueNumberInfo);
818 if ((RHS.ranges.size() > LHS.ranges.size() &&
819 MRegisterInfo::isVirtualRegister(LHS.reg)) ||
820 MRegisterInfo::isPhysicalRegister(RHS.reg)) {
821 RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0],
822 ValueNumberInfo);
823 Swapped = true;
824 } else {
825 LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0],
826 ValueNumberInfo);
827 Swapped = false;
828 }
819829 return true;
820830 }
821831