LiveInterval.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the LiveRange and LiveInterval classes. Given some
  11. // numbering of each the machine instructions an interval [i, j) is said to be a
  12. // live interval for register v if there is no instruction with number j' > j
  13. // such that v is live at j' and there is no instruction with number i' < i such
  14. // that v is live at i'. In this implementation intervals can have holes,
  15. // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
  16. // individual range is represented as an instance of LiveRange, and the whole
  17. // interval is represented as an instance of LiveInterval.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/CodeGen/LiveInterval.h"
  21. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/SmallSet.h"
  25. #include "llvm/ADT/STLExtras.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/Target/TargetRegisterInfo.h"
  29. #include <algorithm>
  30. using namespace llvm;
  31. LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
  32. // This algorithm is basically std::upper_bound.
  33. // Unfortunately, std::upper_bound cannot be used with mixed types until we
  34. // adopt C++0x. Many libraries can do it, but not all.
  35. if (empty() || Pos >= endIndex())
  36. return end();
  37. iterator I = begin();
  38. size_t Len = ranges.size();
  39. do {
  40. size_t Mid = Len >> 1;
  41. if (Pos < I[Mid].end)
  42. Len = Mid;
  43. else
  44. I += Mid + 1, Len -= Mid + 1;
  45. } while (Len);
  46. return I;
  47. }
  48. /// killedInRange - Return true if the interval has kills in [Start,End).
  49. bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
  50. Ranges::const_iterator r =
  51. std::lower_bound(ranges.begin(), ranges.end(), End);
  52. // Now r points to the first interval with start >= End, or ranges.end().
  53. if (r == ranges.begin())
  54. return false;
  55. --r;
  56. // Now r points to the last interval with end <= End.
  57. // r->end is the kill point.
  58. return r->end >= Start && r->end < End;
  59. }
  60. // overlaps - Return true if the intersection of the two live intervals is
  61. // not empty.
  62. //
  63. // An example for overlaps():
  64. //
  65. // 0: A = ...
  66. // 4: B = ...
  67. // 8: C = A + B ;; last use of A
  68. //
  69. // The live intervals should look like:
  70. //
  71. // A = [3, 11)
  72. // B = [7, x)
  73. // C = [11, y)
  74. //
  75. // A->overlaps(C) should return false since we want to be able to join
  76. // A and C.
  77. //
  78. bool LiveInterval::overlapsFrom(const LiveInterval& other,
  79. const_iterator StartPos) const {
  80. assert(!empty() && "empty interval");
  81. const_iterator i = begin();
  82. const_iterator ie = end();
  83. const_iterator j = StartPos;
  84. const_iterator je = other.end();
  85. assert((StartPos->start <= i->start || StartPos == other.begin()) &&
  86. StartPos != other.end() && "Bogus start position hint!");
  87. if (i->start < j->start) {
  88. i = std::upper_bound(i, ie, j->start);
  89. if (i != ranges.begin()) --i;
  90. } else if (j->start < i->start) {
  91. ++StartPos;
  92. if (StartPos != other.end() && StartPos->start <= i->start) {
  93. assert(StartPos < other.end() && i < end());
  94. j = std::upper_bound(j, je, i->start);
  95. if (j != other.ranges.begin()) --j;
  96. }
  97. } else {
  98. return true;
  99. }
  100. if (j == je) return false;
  101. while (i != ie) {
  102. if (i->start > j->start) {
  103. std::swap(i, j);
  104. std::swap(ie, je);
  105. }
  106. if (i->end > j->start)
  107. return true;
  108. ++i;
  109. }
  110. return false;
  111. }
  112. /// overlaps - Return true if the live interval overlaps a range specified
  113. /// by [Start, End).
  114. bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
  115. assert(Start < End && "Invalid range");
  116. const_iterator I = std::lower_bound(begin(), end(), End);
  117. return I != begin() && (--I)->end > Start;
  118. }
  119. /// ValNo is dead, remove it. If it is the largest value number, just nuke it
  120. /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
  121. /// it can be nuked later.
  122. void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
  123. if (ValNo->id == getNumValNums()-1) {
  124. do {
  125. valnos.pop_back();
  126. } while (!valnos.empty() && valnos.back()->isUnused());
  127. } else {
  128. ValNo->setIsUnused(true);
  129. }
  130. }
  131. /// RenumberValues - Renumber all values in order of appearance and delete the
  132. /// remaining unused values.
  133. void LiveInterval::RenumberValues(LiveIntervals &lis) {
  134. SmallPtrSet<VNInfo*, 8> Seen;
  135. valnos.clear();
  136. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  137. VNInfo *VNI = I->valno;
  138. if (!Seen.insert(VNI))
  139. continue;
  140. assert(!VNI->isUnused() && "Unused valno used by live range");
  141. VNI->id = (unsigned)valnos.size();
  142. valnos.push_back(VNI);
  143. }
  144. }
  145. /// extendIntervalEndTo - This method is used when we want to extend the range
  146. /// specified by I to end at the specified endpoint. To do this, we should
  147. /// merge and eliminate all ranges that this will overlap with. The iterator is
  148. /// not invalidated.
  149. void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
  150. assert(I != ranges.end() && "Not a valid interval!");
  151. VNInfo *ValNo = I->valno;
  152. // Search for the first interval that we can't merge with.
  153. Ranges::iterator MergeTo = llvm::next(I);
  154. for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
  155. assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
  156. }
  157. // If NewEnd was in the middle of an interval, make sure to get its endpoint.
  158. I->end = std::max(NewEnd, prior(MergeTo)->end);
  159. // Erase any dead ranges.
  160. ranges.erase(llvm::next(I), MergeTo);
  161. // If the newly formed range now touches the range after it and if they have
  162. // the same value number, merge the two ranges into one range.
  163. Ranges::iterator Next = llvm::next(I);
  164. if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
  165. I->end = Next->end;
  166. ranges.erase(Next);
  167. }
  168. }
  169. /// extendIntervalStartTo - This method is used when we want to extend the range
  170. /// specified by I to start at the specified endpoint. To do this, we should
  171. /// merge and eliminate all ranges that this will overlap with.
  172. LiveInterval::Ranges::iterator
  173. LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
  174. assert(I != ranges.end() && "Not a valid interval!");
  175. VNInfo *ValNo = I->valno;
  176. // Search for the first interval that we can't merge with.
  177. Ranges::iterator MergeTo = I;
  178. do {
  179. if (MergeTo == ranges.begin()) {
  180. I->start = NewStart;
  181. ranges.erase(MergeTo, I);
  182. return I;
  183. }
  184. assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
  185. --MergeTo;
  186. } while (NewStart <= MergeTo->start);
  187. // If we start in the middle of another interval, just delete a range and
  188. // extend that interval.
  189. if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
  190. MergeTo->end = I->end;
  191. } else {
  192. // Otherwise, extend the interval right after.
  193. ++MergeTo;
  194. MergeTo->start = NewStart;
  195. MergeTo->end = I->end;
  196. }
  197. ranges.erase(llvm::next(MergeTo), llvm::next(I));
  198. return MergeTo;
  199. }
  200. LiveInterval::iterator
  201. LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
  202. SlotIndex Start = LR.start, End = LR.end;
  203. iterator it = std::upper_bound(From, ranges.end(), Start);
  204. // If the inserted interval starts in the middle or right at the end of
  205. // another interval, just extend that interval to contain the range of LR.
  206. if (it != ranges.begin()) {
  207. iterator B = prior(it);
  208. if (LR.valno == B->valno) {
  209. if (B->start <= Start && B->end >= Start) {
  210. extendIntervalEndTo(B, End);
  211. return B;
  212. }
  213. } else {
  214. // Check to make sure that we are not overlapping two live ranges with
  215. // different valno's.
  216. assert(B->end <= Start &&
  217. "Cannot overlap two LiveRanges with differing ValID's"
  218. " (did you def the same reg twice in a MachineInstr?)");
  219. }
  220. }
  221. // Otherwise, if this range ends in the middle of, or right next to, another
  222. // interval, merge it into that interval.
  223. if (it != ranges.end()) {
  224. if (LR.valno == it->valno) {
  225. if (it->start <= End) {
  226. it = extendIntervalStartTo(it, Start);
  227. // If LR is a complete superset of an interval, we may need to grow its
  228. // endpoint as well.
  229. if (End > it->end)
  230. extendIntervalEndTo(it, End);
  231. return it;
  232. }
  233. } else {
  234. // Check to make sure that we are not overlapping two live ranges with
  235. // different valno's.
  236. assert(it->start >= End &&
  237. "Cannot overlap two LiveRanges with differing ValID's");
  238. }
  239. }
  240. // Otherwise, this is just a new range that doesn't interact with anything.
  241. // Insert it.
  242. return ranges.insert(it, LR);
  243. }
  244. /// extendInBlock - If this interval is live before Kill in the basic
  245. /// block that starts at StartIdx, extend it to be live up to Kill and return
  246. /// the value. If there is no live range before Kill, return NULL.
  247. VNInfo *LiveInterval::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
  248. if (empty())
  249. return 0;
  250. iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
  251. if (I == begin())
  252. return 0;
  253. --I;
  254. if (I->end <= StartIdx)
  255. return 0;
  256. if (I->end < Kill)
  257. extendIntervalEndTo(I, Kill);
  258. return I->valno;
  259. }
  260. /// removeRange - Remove the specified range from this interval. Note that
  261. /// the range must be in a single LiveRange in its entirety.
  262. void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
  263. bool RemoveDeadValNo) {
  264. // Find the LiveRange containing this span.
  265. Ranges::iterator I = find(Start);
  266. assert(I != ranges.end() && "Range is not in interval!");
  267. assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
  268. // If the span we are removing is at the start of the LiveRange, adjust it.
  269. VNInfo *ValNo = I->valno;
  270. if (I->start == Start) {
  271. if (I->end == End) {
  272. if (RemoveDeadValNo) {
  273. // Check if val# is dead.
  274. bool isDead = true;
  275. for (const_iterator II = begin(), EE = end(); II != EE; ++II)
  276. if (II != I && II->valno == ValNo) {
  277. isDead = false;
  278. break;
  279. }
  280. if (isDead) {
  281. // Now that ValNo is dead, remove it.
  282. markValNoForDeletion(ValNo);
  283. }
  284. }
  285. ranges.erase(I); // Removed the whole LiveRange.
  286. } else
  287. I->start = End;
  288. return;
  289. }
  290. // Otherwise if the span we are removing is at the end of the LiveRange,
  291. // adjust the other way.
  292. if (I->end == End) {
  293. I->end = Start;
  294. return;
  295. }
  296. // Otherwise, we are splitting the LiveRange into two pieces.
  297. SlotIndex OldEnd = I->end;
  298. I->end = Start; // Trim the old interval.
  299. // Insert the new one.
  300. ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
  301. }
  302. /// removeValNo - Remove all the ranges defined by the specified value#.
  303. /// Also remove the value# from value# list.
  304. void LiveInterval::removeValNo(VNInfo *ValNo) {
  305. if (empty()) return;
  306. Ranges::iterator I = ranges.end();
  307. Ranges::iterator E = ranges.begin();
  308. do {
  309. --I;
  310. if (I->valno == ValNo)
  311. ranges.erase(I);
  312. } while (I != E);
  313. // Now that ValNo is dead, remove it.
  314. markValNoForDeletion(ValNo);
  315. }
  316. /// findDefinedVNInfo - Find the VNInfo defined by the specified
  317. /// index (register interval).
  318. VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
  319. for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
  320. i != e; ++i) {
  321. if ((*i)->def == Idx)
  322. return *i;
  323. }
  324. return 0;
  325. }
  326. /// join - Join two live intervals (this, and other) together. This applies
  327. /// mappings to the value numbers in the LHS/RHS intervals as specified. If
  328. /// the intervals are not joinable, this aborts.
  329. void LiveInterval::join(LiveInterval &Other,
  330. const int *LHSValNoAssignments,
  331. const int *RHSValNoAssignments,
  332. SmallVector<VNInfo*, 16> &NewVNInfo,
  333. MachineRegisterInfo *MRI) {
  334. // Determine if any of our live range values are mapped. This is uncommon, so
  335. // we want to avoid the interval scan if not.
  336. bool MustMapCurValNos = false;
  337. unsigned NumVals = getNumValNums();
  338. unsigned NumNewVals = NewVNInfo.size();
  339. for (unsigned i = 0; i != NumVals; ++i) {
  340. unsigned LHSValID = LHSValNoAssignments[i];
  341. if (i != LHSValID ||
  342. (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
  343. MustMapCurValNos = true;
  344. }
  345. // If we have to apply a mapping to our base interval assignment, rewrite it
  346. // now.
  347. if (MustMapCurValNos) {
  348. // Map the first live range.
  349. iterator OutIt = begin();
  350. OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
  351. ++OutIt;
  352. for (iterator I = OutIt, E = end(); I != E; ++I) {
  353. OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
  354. // If this live range has the same value # as its immediate predecessor,
  355. // and if they are neighbors, remove one LiveRange. This happens when we
  356. // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
  357. if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
  358. (OutIt-1)->end = OutIt->end;
  359. } else {
  360. if (I != OutIt) {
  361. OutIt->start = I->start;
  362. OutIt->end = I->end;
  363. }
  364. // Didn't merge, on to the next one.
  365. ++OutIt;
  366. }
  367. }
  368. // If we merge some live ranges, chop off the end.
  369. ranges.erase(OutIt, end());
  370. }
  371. // Remember assignements because val# ids are changing.
  372. SmallVector<unsigned, 16> OtherAssignments;
  373. for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
  374. OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
  375. // Update val# info. Renumber them and make sure they all belong to this
  376. // LiveInterval now. Also remove dead val#'s.
  377. unsigned NumValNos = 0;
  378. for (unsigned i = 0; i < NumNewVals; ++i) {
  379. VNInfo *VNI = NewVNInfo[i];
  380. if (VNI) {
  381. if (NumValNos >= NumVals)
  382. valnos.push_back(VNI);
  383. else
  384. valnos[NumValNos] = VNI;
  385. VNI->id = NumValNos++; // Renumber val#.
  386. }
  387. }
  388. if (NumNewVals < NumVals)
  389. valnos.resize(NumNewVals); // shrinkify
  390. // Okay, now insert the RHS live ranges into the LHS.
  391. iterator InsertPos = begin();
  392. unsigned RangeNo = 0;
  393. for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
  394. // Map the valno in the other live range to the current live range.
  395. I->valno = NewVNInfo[OtherAssignments[RangeNo]];
  396. assert(I->valno && "Adding a dead range?");
  397. InsertPos = addRangeFrom(*I, InsertPos);
  398. }
  399. ComputeJoinedWeight(Other);
  400. }
  401. /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
  402. /// interval as the specified value number. The LiveRanges in RHS are
  403. /// allowed to overlap with LiveRanges in the current interval, but only if
  404. /// the overlapping LiveRanges have the specified value number.
  405. void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
  406. VNInfo *LHSValNo) {
  407. // TODO: Make this more efficient.
  408. iterator InsertPos = begin();
  409. for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
  410. // Map the valno in the other live range to the current live range.
  411. LiveRange Tmp = *I;
  412. Tmp.valno = LHSValNo;
  413. InsertPos = addRangeFrom(Tmp, InsertPos);
  414. }
  415. }
  416. /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
  417. /// in RHS into this live interval as the specified value number.
  418. /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
  419. /// current interval, it will replace the value numbers of the overlaped
  420. /// live ranges with the specified value number.
  421. void LiveInterval::MergeValueInAsValue(
  422. const LiveInterval &RHS,
  423. const VNInfo *RHSValNo, VNInfo *LHSValNo) {
  424. // TODO: Make this more efficient.
  425. iterator InsertPos = begin();
  426. for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
  427. if (I->valno != RHSValNo)
  428. continue;
  429. // Map the valno in the other live range to the current live range.
  430. LiveRange Tmp = *I;
  431. Tmp.valno = LHSValNo;
  432. InsertPos = addRangeFrom(Tmp, InsertPos);
  433. }
  434. }
  435. /// MergeValueNumberInto - This method is called when two value nubmers
  436. /// are found to be equivalent. This eliminates V1, replacing all
  437. /// LiveRanges with the V1 value number with the V2 value number. This can
  438. /// cause merging of V1/V2 values numbers and compaction of the value space.
  439. VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
  440. assert(V1 != V2 && "Identical value#'s are always equivalent!");
  441. // This code actually merges the (numerically) larger value number into the
  442. // smaller value number, which is likely to allow us to compactify the value
  443. // space. The only thing we have to be careful of is to preserve the
  444. // instruction that defines the result value.
  445. // Make sure V2 is smaller than V1.
  446. if (V1->id < V2->id) {
  447. V1->copyFrom(*V2);
  448. std::swap(V1, V2);
  449. }
  450. // Merge V1 live ranges into V2.
  451. for (iterator I = begin(); I != end(); ) {
  452. iterator LR = I++;
  453. if (LR->valno != V1) continue; // Not a V1 LiveRange.
  454. // Okay, we found a V1 live range. If it had a previous, touching, V2 live
  455. // range, extend it.
  456. if (LR != begin()) {
  457. iterator Prev = LR-1;
  458. if (Prev->valno == V2 && Prev->end == LR->start) {
  459. Prev->end = LR->end;
  460. // Erase this live-range.
  461. ranges.erase(LR);
  462. I = Prev+1;
  463. LR = Prev;
  464. }
  465. }
  466. // Okay, now we have a V1 or V2 live range that is maximally merged forward.
  467. // Ensure that it is a V2 live-range.
  468. LR->valno = V2;
  469. // If we can merge it into later V2 live ranges, do so now. We ignore any
  470. // following V1 live ranges, as they will be merged in subsequent iterations
  471. // of the loop.
  472. if (I != end()) {
  473. if (I->start == LR->end && I->valno == V2) {
  474. LR->end = I->end;
  475. ranges.erase(I);
  476. I = LR+1;
  477. }
  478. }
  479. }
  480. // Merge the relevant flags.
  481. V2->mergeFlags(V1);
  482. // Now that V1 is dead, remove it.
  483. markValNoForDeletion(V1);
  484. return V2;
  485. }
  486. void LiveInterval::Copy(const LiveInterval &RHS,
  487. MachineRegisterInfo *MRI,
  488. VNInfo::Allocator &VNInfoAllocator) {
  489. ranges.clear();
  490. valnos.clear();
  491. std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
  492. MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
  493. weight = RHS.weight;
  494. for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
  495. const VNInfo *VNI = RHS.getValNumInfo(i);
  496. createValueCopy(VNI, VNInfoAllocator);
  497. }
  498. for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
  499. const LiveRange &LR = RHS.ranges[i];
  500. addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
  501. }
  502. }
  503. unsigned LiveInterval::getSize() const {
  504. unsigned Sum = 0;
  505. for (const_iterator I = begin(), E = end(); I != E; ++I)
  506. Sum += I->start.distance(I->end);
  507. return Sum;
  508. }
  509. /// ComputeJoinedWeight - Set the weight of a live interval Joined
  510. /// after Other has been merged into it.
  511. void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
  512. // If either of these intervals was spilled, the weight is the
  513. // weight of the non-spilled interval. This can only happen with
  514. // iterative coalescers.
  515. if (Other.weight != HUGE_VALF) {
  516. weight += Other.weight;
  517. }
  518. else if (weight == HUGE_VALF &&
  519. !TargetRegisterInfo::isPhysicalRegister(reg)) {
  520. // Remove this assert if you have an iterative coalescer
  521. assert(0 && "Joining to spilled interval");
  522. weight = Other.weight;
  523. }
  524. else {
  525. // Otherwise the weight stays the same
  526. // Remove this assert if you have an iterative coalescer
  527. assert(0 && "Joining from spilled interval");
  528. }
  529. }
  530. raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
  531. return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
  532. }
  533. void LiveRange::dump() const {
  534. dbgs() << *this << "\n";
  535. }
  536. void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
  537. OS << PrintReg(reg, TRI);
  538. if (weight != 0)
  539. OS << ',' << weight;
  540. if (empty())
  541. OS << " EMPTY";
  542. else {
  543. OS << " = ";
  544. for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
  545. E = ranges.end(); I != E; ++I) {
  546. OS << *I;
  547. assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
  548. }
  549. }
  550. // Print value number info.
  551. if (getNumValNums()) {
  552. OS << " ";
  553. unsigned vnum = 0;
  554. for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
  555. ++i, ++vnum) {
  556. const VNInfo *vni = *i;
  557. if (vnum) OS << " ";
  558. OS << vnum << "@";
  559. if (vni->isUnused()) {
  560. OS << "x";
  561. } else {
  562. OS << vni->def;
  563. if (vni->isPHIDef())
  564. OS << "-phidef";
  565. if (vni->hasPHIKill())
  566. OS << "-phikill";
  567. if (vni->hasRedefByEC())
  568. OS << "-ec";
  569. }
  570. }
  571. }
  572. }
  573. void LiveInterval::dump() const {
  574. dbgs() << *this << "\n";
  575. }
  576. void LiveRange::print(raw_ostream &os) const {
  577. os << *this;
  578. }
  579. unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
  580. // Create initial equivalence classes.
  581. EqClass.clear();
  582. EqClass.grow(LI->getNumValNums());
  583. const VNInfo *used = 0, *unused = 0;
  584. // Determine connections.
  585. for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
  586. I != E; ++I) {
  587. const VNInfo *VNI = *I;
  588. // Group all unused values into one class.
  589. if (VNI->isUnused()) {
  590. if (unused)
  591. EqClass.join(unused->id, VNI->id);
  592. unused = VNI;
  593. continue;
  594. }
  595. used = VNI;
  596. if (VNI->isPHIDef()) {
  597. const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
  598. assert(MBB && "Phi-def has no defining MBB");
  599. // Connect to values live out of predecessors.
  600. for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
  601. PE = MBB->pred_end(); PI != PE; ++PI)
  602. if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
  603. EqClass.join(VNI->id, PVNI->id);
  604. } else {
  605. // Normal value defined by an instruction. Check for two-addr redef.
  606. // FIXME: This could be coincidental. Should we really check for a tied
  607. // operand constraint?
  608. // Note that VNI->def may be a use slot for an early clobber def.
  609. if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
  610. EqClass.join(VNI->id, UVNI->id);
  611. }
  612. }
  613. // Lump all the unused values in with the last used value.
  614. if (used && unused)
  615. EqClass.join(used->id, unused->id);
  616. EqClass.compress();
  617. return EqClass.getNumClasses();
  618. }
  619. void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
  620. MachineRegisterInfo &MRI) {
  621. assert(LIV[0] && "LIV[0] must be set");
  622. LiveInterval &LI = *LIV[0];
  623. // Rewrite instructions.
  624. for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
  625. RE = MRI.reg_end(); RI != RE;) {
  626. MachineOperand &MO = RI.getOperand();
  627. MachineInstr *MI = MO.getParent();
  628. ++RI;
  629. if (MO.isUse() && MO.isUndef())
  630. continue;
  631. // DBG_VALUE instructions should have been eliminated earlier.
  632. SlotIndex Idx = LIS.getInstructionIndex(MI);
  633. Idx = Idx.getRegSlot(MO.isUse());
  634. const VNInfo *VNI = LI.getVNInfoAt(Idx);
  635. assert(VNI && "Interval not live at use.");
  636. MO.setReg(LIV[getEqClass(VNI)]->reg);
  637. }
  638. // Move runs to new intervals.
  639. LiveInterval::iterator J = LI.begin(), E = LI.end();
  640. while (J != E && EqClass[J->valno->id] == 0)
  641. ++J;
  642. for (LiveInterval::iterator I = J; I != E; ++I) {
  643. if (unsigned eq = EqClass[I->valno->id]) {
  644. assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
  645. "New intervals should be empty");
  646. LIV[eq]->ranges.push_back(*I);
  647. } else
  648. *J++ = *I;
  649. }
  650. LI.ranges.erase(J, E);
  651. // Transfer VNInfos to their new owners and renumber them.
  652. unsigned j = 0, e = LI.getNumValNums();
  653. while (j != e && EqClass[j] == 0)
  654. ++j;
  655. for (unsigned i = j; i != e; ++i) {
  656. VNInfo *VNI = LI.getValNumInfo(i);
  657. if (unsigned eq = EqClass[i]) {
  658. VNI->id = LIV[eq]->getNumValNums();
  659. LIV[eq]->valnos.push_back(VNI);
  660. } else {
  661. VNI->id = j;
  662. LI.valnos[j++] = VNI;
  663. }
  664. }
  665. LI.valnos.resize(j);
  666. }