LiveInterval.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. // CompEnd - Compare LiveRange ends.
  32. namespace {
  33. struct CompEnd {
  34. bool operator()(const LiveRange &A, const LiveRange &B) const {
  35. return A.end < B.end;
  36. }
  37. };
  38. }
  39. LiveInterval::iterator LiveInterval::find(SlotIndex Pos) {
  40. assert(Pos.isValid() && "Cannot search for an invalid index");
  41. return std::upper_bound(begin(), end(), LiveRange(SlotIndex(), Pos, 0),
  42. CompEnd());
  43. }
  44. /// killedInRange - Return true if the interval has kills in [Start,End).
  45. bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
  46. Ranges::const_iterator r =
  47. std::lower_bound(ranges.begin(), ranges.end(), End);
  48. // Now r points to the first interval with start >= End, or ranges.end().
  49. if (r == ranges.begin())
  50. return false;
  51. --r;
  52. // Now r points to the last interval with end <= End.
  53. // r->end is the kill point.
  54. return r->end >= Start && r->end < End;
  55. }
  56. // overlaps - Return true if the intersection of the two live intervals is
  57. // not empty.
  58. //
  59. // An example for overlaps():
  60. //
  61. // 0: A = ...
  62. // 4: B = ...
  63. // 8: C = A + B ;; last use of A
  64. //
  65. // The live intervals should look like:
  66. //
  67. // A = [3, 11)
  68. // B = [7, x)
  69. // C = [11, y)
  70. //
  71. // A->overlaps(C) should return false since we want to be able to join
  72. // A and C.
  73. //
  74. bool LiveInterval::overlapsFrom(const LiveInterval& other,
  75. const_iterator StartPos) const {
  76. assert(!empty() && "empty interval");
  77. const_iterator i = begin();
  78. const_iterator ie = end();
  79. const_iterator j = StartPos;
  80. const_iterator je = other.end();
  81. assert((StartPos->start <= i->start || StartPos == other.begin()) &&
  82. StartPos != other.end() && "Bogus start position hint!");
  83. if (i->start < j->start) {
  84. i = std::upper_bound(i, ie, j->start);
  85. if (i != ranges.begin()) --i;
  86. } else if (j->start < i->start) {
  87. ++StartPos;
  88. if (StartPos != other.end() && StartPos->start <= i->start) {
  89. assert(StartPos < other.end() && i < end());
  90. j = std::upper_bound(j, je, i->start);
  91. if (j != other.ranges.begin()) --j;
  92. }
  93. } else {
  94. return true;
  95. }
  96. if (j == je) return false;
  97. while (i != ie) {
  98. if (i->start > j->start) {
  99. std::swap(i, j);
  100. std::swap(ie, je);
  101. }
  102. if (i->end > j->start)
  103. return true;
  104. ++i;
  105. }
  106. return false;
  107. }
  108. /// overlaps - Return true if the live interval overlaps a range specified
  109. /// by [Start, End).
  110. bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
  111. assert(Start < End && "Invalid range");
  112. const_iterator I = std::lower_bound(begin(), end(), End);
  113. return I != begin() && (--I)->end > Start;
  114. }
  115. /// ValNo is dead, remove it. If it is the largest value number, just nuke it
  116. /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
  117. /// it can be nuked later.
  118. void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
  119. if (ValNo->id == getNumValNums()-1) {
  120. do {
  121. valnos.pop_back();
  122. } while (!valnos.empty() && valnos.back()->isUnused());
  123. } else {
  124. ValNo->setIsUnused(true);
  125. }
  126. }
  127. /// RenumberValues - Renumber all values in order of appearance and delete the
  128. /// remaining unused values.
  129. void LiveInterval::RenumberValues(LiveIntervals &lis) {
  130. SmallPtrSet<VNInfo*, 8> Seen;
  131. bool seenPHIDef = false;
  132. valnos.clear();
  133. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  134. VNInfo *VNI = I->valno;
  135. if (!Seen.insert(VNI))
  136. continue;
  137. assert(!VNI->isUnused() && "Unused valno used by live range");
  138. VNI->id = (unsigned)valnos.size();
  139. valnos.push_back(VNI);
  140. VNI->setHasPHIKill(false);
  141. if (VNI->isPHIDef())
  142. seenPHIDef = true;
  143. }
  144. // Recompute phi kill flags.
  145. if (!seenPHIDef)
  146. return;
  147. for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
  148. VNInfo *VNI = *I;
  149. if (!VNI->isPHIDef())
  150. continue;
  151. const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
  152. assert(PHIBB && "No basic block for phi-def");
  153. for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
  154. PE = PHIBB->pred_end(); PI != PE; ++PI) {
  155. VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
  156. if (KVNI)
  157. KVNI->setHasPHIKill(true);
  158. }
  159. }
  160. }
  161. /// extendIntervalEndTo - This method is used when we want to extend the range
  162. /// specified by I to end at the specified endpoint. To do this, we should
  163. /// merge and eliminate all ranges that this will overlap with. The iterator is
  164. /// not invalidated.
  165. void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
  166. assert(I != ranges.end() && "Not a valid interval!");
  167. VNInfo *ValNo = I->valno;
  168. // Search for the first interval that we can't merge with.
  169. Ranges::iterator MergeTo = llvm::next(I);
  170. for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
  171. assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
  172. }
  173. // If NewEnd was in the middle of an interval, make sure to get its endpoint.
  174. I->end = std::max(NewEnd, prior(MergeTo)->end);
  175. // Erase any dead ranges.
  176. ranges.erase(llvm::next(I), MergeTo);
  177. // If the newly formed range now touches the range after it and if they have
  178. // the same value number, merge the two ranges into one range.
  179. Ranges::iterator Next = llvm::next(I);
  180. if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
  181. I->end = Next->end;
  182. ranges.erase(Next);
  183. }
  184. }
  185. /// extendIntervalStartTo - This method is used when we want to extend the range
  186. /// specified by I to start at the specified endpoint. To do this, we should
  187. /// merge and eliminate all ranges that this will overlap with.
  188. LiveInterval::Ranges::iterator
  189. LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
  190. assert(I != ranges.end() && "Not a valid interval!");
  191. VNInfo *ValNo = I->valno;
  192. // Search for the first interval that we can't merge with.
  193. Ranges::iterator MergeTo = I;
  194. do {
  195. if (MergeTo == ranges.begin()) {
  196. I->start = NewStart;
  197. ranges.erase(MergeTo, I);
  198. return I;
  199. }
  200. assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
  201. --MergeTo;
  202. } while (NewStart <= MergeTo->start);
  203. // If we start in the middle of another interval, just delete a range and
  204. // extend that interval.
  205. if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
  206. MergeTo->end = I->end;
  207. } else {
  208. // Otherwise, extend the interval right after.
  209. ++MergeTo;
  210. MergeTo->start = NewStart;
  211. MergeTo->end = I->end;
  212. }
  213. ranges.erase(llvm::next(MergeTo), llvm::next(I));
  214. return MergeTo;
  215. }
  216. LiveInterval::iterator
  217. LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
  218. SlotIndex Start = LR.start, End = LR.end;
  219. iterator it = std::upper_bound(From, ranges.end(), Start);
  220. // If the inserted interval starts in the middle or right at the end of
  221. // another interval, just extend that interval to contain the range of LR.
  222. if (it != ranges.begin()) {
  223. iterator B = prior(it);
  224. if (LR.valno == B->valno) {
  225. if (B->start <= Start && B->end >= Start) {
  226. extendIntervalEndTo(B, End);
  227. return B;
  228. }
  229. } else {
  230. // Check to make sure that we are not overlapping two live ranges with
  231. // different valno's.
  232. assert(B->end <= Start &&
  233. "Cannot overlap two LiveRanges with differing ValID's"
  234. " (did you def the same reg twice in a MachineInstr?)");
  235. }
  236. }
  237. // Otherwise, if this range ends in the middle of, or right next to, another
  238. // interval, merge it into that interval.
  239. if (it != ranges.end()) {
  240. if (LR.valno == it->valno) {
  241. if (it->start <= End) {
  242. it = extendIntervalStartTo(it, Start);
  243. // If LR is a complete superset of an interval, we may need to grow its
  244. // endpoint as well.
  245. if (End > it->end)
  246. extendIntervalEndTo(it, End);
  247. return it;
  248. }
  249. } else {
  250. // Check to make sure that we are not overlapping two live ranges with
  251. // different valno's.
  252. assert(it->start >= End &&
  253. "Cannot overlap two LiveRanges with differing ValID's");
  254. }
  255. }
  256. // Otherwise, this is just a new range that doesn't interact with anything.
  257. // Insert it.
  258. return ranges.insert(it, LR);
  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. SmallVector<VNInfo*, 4> ReplacedValNos;
  425. iterator IP = begin();
  426. for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
  427. assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
  428. if (I->valno != RHSValNo)
  429. continue;
  430. SlotIndex Start = I->start, End = I->end;
  431. IP = std::upper_bound(IP, end(), Start);
  432. // If the start of this range overlaps with an existing liverange, trim it.
  433. if (IP != begin() && IP[-1].end > Start) {
  434. if (IP[-1].valno != LHSValNo) {
  435. ReplacedValNos.push_back(IP[-1].valno);
  436. IP[-1].valno = LHSValNo; // Update val#.
  437. }
  438. Start = IP[-1].end;
  439. // Trimmed away the whole range?
  440. if (Start >= End) continue;
  441. }
  442. // If the end of this range overlaps with an existing liverange, trim it.
  443. if (IP != end() && End > IP->start) {
  444. if (IP->valno != LHSValNo) {
  445. ReplacedValNos.push_back(IP->valno);
  446. IP->valno = LHSValNo; // Update val#.
  447. }
  448. End = IP->start;
  449. // If this trimmed away the whole range, ignore it.
  450. if (Start == End) continue;
  451. }
  452. // Map the valno in the other live range to the current live range.
  453. IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
  454. }
  455. SmallSet<VNInfo*, 4> Seen;
  456. for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
  457. VNInfo *V1 = ReplacedValNos[i];
  458. if (Seen.insert(V1)) {
  459. bool isDead = true;
  460. for (const_iterator I = begin(), E = end(); I != E; ++I)
  461. if (I->valno == V1) {
  462. isDead = false;
  463. break;
  464. }
  465. if (isDead) {
  466. // Now that V1 is dead, remove it.
  467. markValNoForDeletion(V1);
  468. }
  469. }
  470. }
  471. }
  472. /// MergeValueNumberInto - This method is called when two value nubmers
  473. /// are found to be equivalent. This eliminates V1, replacing all
  474. /// LiveRanges with the V1 value number with the V2 value number. This can
  475. /// cause merging of V1/V2 values numbers and compaction of the value space.
  476. VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
  477. assert(V1 != V2 && "Identical value#'s are always equivalent!");
  478. // This code actually merges the (numerically) larger value number into the
  479. // smaller value number, which is likely to allow us to compactify the value
  480. // space. The only thing we have to be careful of is to preserve the
  481. // instruction that defines the result value.
  482. // Make sure V2 is smaller than V1.
  483. if (V1->id < V2->id) {
  484. V1->copyFrom(*V2);
  485. std::swap(V1, V2);
  486. }
  487. // Merge V1 live ranges into V2.
  488. for (iterator I = begin(); I != end(); ) {
  489. iterator LR = I++;
  490. if (LR->valno != V1) continue; // Not a V1 LiveRange.
  491. // Okay, we found a V1 live range. If it had a previous, touching, V2 live
  492. // range, extend it.
  493. if (LR != begin()) {
  494. iterator Prev = LR-1;
  495. if (Prev->valno == V2 && Prev->end == LR->start) {
  496. Prev->end = LR->end;
  497. // Erase this live-range.
  498. ranges.erase(LR);
  499. I = Prev+1;
  500. LR = Prev;
  501. }
  502. }
  503. // Okay, now we have a V1 or V2 live range that is maximally merged forward.
  504. // Ensure that it is a V2 live-range.
  505. LR->valno = V2;
  506. // If we can merge it into later V2 live ranges, do so now. We ignore any
  507. // following V1 live ranges, as they will be merged in subsequent iterations
  508. // of the loop.
  509. if (I != end()) {
  510. if (I->start == LR->end && I->valno == V2) {
  511. LR->end = I->end;
  512. ranges.erase(I);
  513. I = LR+1;
  514. }
  515. }
  516. }
  517. // Merge the relevant flags.
  518. V2->mergeFlags(V1);
  519. // Now that V1 is dead, remove it.
  520. markValNoForDeletion(V1);
  521. return V2;
  522. }
  523. void LiveInterval::Copy(const LiveInterval &RHS,
  524. MachineRegisterInfo *MRI,
  525. VNInfo::Allocator &VNInfoAllocator) {
  526. ranges.clear();
  527. valnos.clear();
  528. std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
  529. MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
  530. weight = RHS.weight;
  531. for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
  532. const VNInfo *VNI = RHS.getValNumInfo(i);
  533. createValueCopy(VNI, VNInfoAllocator);
  534. }
  535. for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
  536. const LiveRange &LR = RHS.ranges[i];
  537. addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
  538. }
  539. }
  540. unsigned LiveInterval::getSize() const {
  541. unsigned Sum = 0;
  542. for (const_iterator I = begin(), E = end(); I != E; ++I)
  543. Sum += I->start.distance(I->end);
  544. return Sum;
  545. }
  546. /// ComputeJoinedWeight - Set the weight of a live interval Joined
  547. /// after Other has been merged into it.
  548. void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
  549. // If either of these intervals was spilled, the weight is the
  550. // weight of the non-spilled interval. This can only happen with
  551. // iterative coalescers.
  552. if (Other.weight != HUGE_VALF) {
  553. weight += Other.weight;
  554. }
  555. else if (weight == HUGE_VALF &&
  556. !TargetRegisterInfo::isPhysicalRegister(reg)) {
  557. // Remove this assert if you have an iterative coalescer
  558. assert(0 && "Joining to spilled interval");
  559. weight = Other.weight;
  560. }
  561. else {
  562. // Otherwise the weight stays the same
  563. // Remove this assert if you have an iterative coalescer
  564. assert(0 && "Joining from spilled interval");
  565. }
  566. }
  567. raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
  568. return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
  569. }
  570. void LiveRange::dump() const {
  571. dbgs() << *this << "\n";
  572. }
  573. void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
  574. OS << PrintReg(reg, TRI) << ',' << weight;
  575. if (empty())
  576. OS << " EMPTY";
  577. else {
  578. OS << " = ";
  579. for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
  580. E = ranges.end(); I != E; ++I) {
  581. OS << *I;
  582. assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
  583. }
  584. }
  585. // Print value number info.
  586. if (getNumValNums()) {
  587. OS << " ";
  588. unsigned vnum = 0;
  589. for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
  590. ++i, ++vnum) {
  591. const VNInfo *vni = *i;
  592. if (vnum) OS << " ";
  593. OS << vnum << "@";
  594. if (vni->isUnused()) {
  595. OS << "x";
  596. } else {
  597. OS << vni->def;
  598. if (vni->isPHIDef())
  599. OS << "-phidef";
  600. if (vni->hasPHIKill())
  601. OS << "-phikill";
  602. if (vni->hasRedefByEC())
  603. OS << "-ec";
  604. }
  605. }
  606. }
  607. }
  608. void LiveInterval::dump() const {
  609. dbgs() << *this << "\n";
  610. }
  611. void LiveRange::print(raw_ostream &os) const {
  612. os << *this;
  613. }
  614. unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
  615. // Create initial equivalence classes.
  616. eqClass_.clear();
  617. eqClass_.grow(LI->getNumValNums());
  618. const VNInfo *used = 0, *unused = 0;
  619. // Determine connections.
  620. for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end();
  621. I != E; ++I) {
  622. const VNInfo *VNI = *I;
  623. // Group all unused values into one class.
  624. if (VNI->isUnused()) {
  625. if (unused)
  626. eqClass_.join(unused->id, VNI->id);
  627. unused = VNI;
  628. continue;
  629. }
  630. used = VNI;
  631. if (VNI->isPHIDef()) {
  632. const MachineBasicBlock *MBB = lis_.getMBBFromIndex(VNI->def);
  633. assert(MBB && "Phi-def has no defining MBB");
  634. // Connect to values live out of predecessors.
  635. for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
  636. PE = MBB->pred_end(); PI != PE; ++PI)
  637. if (const VNInfo *PVNI =
  638. LI->getVNInfoAt(lis_.getMBBEndIdx(*PI).getPrevSlot()))
  639. eqClass_.join(VNI->id, PVNI->id);
  640. } else {
  641. // Normal value defined by an instruction. Check for two-addr redef.
  642. // FIXME: This could be coincidental. Should we really check for a tied
  643. // operand constraint?
  644. // Note that VNI->def may be a use slot for an early clobber def.
  645. if (const VNInfo *UVNI = LI->getVNInfoAt(VNI->def.getPrevSlot()))
  646. eqClass_.join(VNI->id, UVNI->id);
  647. }
  648. }
  649. // Lump all the unused values in with the last used value.
  650. if (used && unused)
  651. eqClass_.join(used->id, unused->id);
  652. eqClass_.compress();
  653. return eqClass_.getNumClasses();
  654. }
  655. void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[]) {
  656. assert(LIV[0] && "LIV[0] must be set");
  657. LiveInterval &LI = *LIV[0];
  658. // First move runs to new intervals.
  659. LiveInterval::iterator J = LI.begin(), E = LI.end();
  660. while (J != E && eqClass_[J->valno->id] == 0)
  661. ++J;
  662. for (LiveInterval::iterator I = J; I != E; ++I) {
  663. if (unsigned eq = eqClass_[I->valno->id]) {
  664. assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
  665. "New intervals should be empty");
  666. LIV[eq]->ranges.push_back(*I);
  667. } else
  668. *J++ = *I;
  669. }
  670. LI.ranges.erase(J, E);
  671. // Transfer VNInfos to their new owners and renumber them.
  672. unsigned j = 0, e = LI.getNumValNums();
  673. while (j != e && eqClass_[j] == 0)
  674. ++j;
  675. for (unsigned i = j; i != e; ++i) {
  676. VNInfo *VNI = LI.getValNumInfo(i);
  677. if (unsigned eq = eqClass_[i]) {
  678. VNI->id = LIV[eq]->getNumValNums();
  679. LIV[eq]->valnos.push_back(VNI);
  680. } else {
  681. VNI->id = j;
  682. LI.valnos[j++] = VNI;
  683. }
  684. }
  685. LI.valnos.resize(j);
  686. }