[llvm-commits] CVS: llvm/lib/CodeGen/LiveInterval.cpp LiveInterval.h

Chris Lattner lattner at cs.uiuc.edu
Fri Jul 23 12:38:54 PDT 2004



Changes in directory llvm/lib/CodeGen:

LiveInterval.cpp updated: 1.3 -> 1.4
LiveInterval.h updated: 1.3 -> 1.4

---
Log message:

Change addRange and join to be a little bit smarter.  In particular, we don't
want to insert a new range into the middle of the vector, then delete ranges
one at a time next to the inserted one as they are merged.

Instead, if the inserted interval overlaps, just start merging.  The only time
we insert into the middle of the vector is when we don't overlap at all.  Also
delete blocks of live ranges if we overlap with many of them.

This patch speeds up joining by .7 seconds on a large testcase, but more 
importantly gets all of the range adding code into addRangeFrom.



---
Diffs of the changes:  (+85 -36)

Index: llvm/lib/CodeGen/LiveInterval.cpp
diff -u llvm/lib/CodeGen/LiveInterval.cpp:1.3 llvm/lib/CodeGen/LiveInterval.cpp:1.4
--- llvm/lib/CodeGen/LiveInterval.cpp:1.3	Fri Jul 23 13:40:00 2004
+++ llvm/lib/CodeGen/LiveInterval.cpp	Fri Jul 23 14:38:44 2004
@@ -87,50 +87,93 @@
   return false;
 }
 
-void LiveInterval::addRange(LiveRange LR) {
-  Ranges::iterator it =
-    ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), LR.start), LR);
-
-  mergeRangesBackward(mergeRangesForward(it));
+/// extendIntervalEndTo - This method is used when we want to extend the range
+/// specified by I to end at the specified endpoint.  To do this, we should
+/// merge and eliminate all ranges that this will overlap with.  The iterator is
+/// not invalidated.
+void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
+  assert(I != ranges.end() && "Not a valid interval!");
+
+  // Search for the first interval that we can't merge with.
+  Ranges::iterator MergeTo = next(I);
+  for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo)
+    /*empty*/;
+
+  // If NewEnd was in the middle of an interval, make sure to get its endpoint.
+  I->end = std::max(NewEnd, prior(MergeTo)->end);
+
+  // Erase any dead ranges
+  ranges.erase(next(I), MergeTo);
 }
 
-void LiveInterval::join(const LiveInterval& other) {
-  Ranges::iterator cur = ranges.begin();
-  isDefinedOnce &= other.isDefinedOnce;
 
-  for (Ranges::const_iterator i = other.ranges.begin(),
-         e = other.ranges.end(); i != e; ++i) {
-    cur = ranges.insert(std::upper_bound(cur, ranges.end(), i->start), *i);
-    cur = mergeRangesBackward(mergeRangesForward(cur));
+/// extendIntervalStartTo - This method is used when we want to extend the range
+/// specified by I to start at the specified endpoint.  To do this, we should
+/// merge and eliminate all ranges that this will overlap with.
+LiveInterval::Ranges::iterator 
+LiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) {
+  assert(I != ranges.end() && "Not a valid interval!");
+
+  // Search for the first interval that we can't merge with.
+  Ranges::iterator MergeTo = I;
+  do {
+    if (MergeTo == ranges.begin()) {
+      I->start = NewStart;
+      return I;
+    }
+    --MergeTo;
+  } while (NewStart <= MergeTo->start);
+
+  // If we start in the middle of another interval, just delete a range and
+  // extend that interval.
+  if (MergeTo->end >= NewStart) {
+    MergeTo->end = I->end;
+  } else {
+    // Otherwise, extend the interval right after.
+    ++MergeTo;
+    MergeTo->start = NewStart;
+    MergeTo->end = I->end;
   }
-  weight += other.weight;
+
+  ranges.erase(next(MergeTo), next(I));
+  return MergeTo;
 }
 
 LiveInterval::Ranges::iterator
-LiveInterval::mergeRangesForward(Ranges::iterator it) {
-  Ranges::iterator n;
-  while ((n = next(it)) != ranges.end()) {
-    if (n->start > it->end)
-      break;
-    it->end = std::max(it->end, n->end);
-    n = ranges.erase(n);
+LiveInterval::addRangeFrom(LiveRange LR, Ranges::iterator From) {
+  unsigned Start = LR.start, End = LR.end;
+  Ranges::iterator it = std::upper_bound(From, ranges.end(), Start);
+
+  // If the inserted interval starts in the middle or right at the end of
+  // another interval, just extend that interval to contain the range of LR.
+  if (it != ranges.begin()) {
+    Ranges::iterator B = prior(it);
+    if (B->start <= Start && B->end >= Start) {
+      extendIntervalEndTo(B, End);
+      return B;
+    }
   }
-  return it;
+
+  // Otherwise, if this range ends in the middle of, or right next to, another
+  // interval, merge it into that interval.
+  if (it != ranges.end() && it->start <= End)
+    return extendIntervalStartTo(it, Start);
+
+  // Otherwise, this is just a new range that doesn't interact with anything.
+  // Insert it.
+  return ranges.insert(it, LR);
 }
 
-LiveInterval::Ranges::iterator
-LiveInterval::mergeRangesBackward(Ranges::iterator it) {
-  while (it != ranges.begin()) {
-    Ranges::iterator p = prior(it);
-    if (it->start > p->end)
-      break;
-
-    it->start = std::min(it->start, p->start);
-    it->end = std::max(it->end, p->end);
-    it = ranges.erase(p);
-  }
+void LiveInterval::join(const LiveInterval& other) {
+  isDefinedOnce &= other.isDefinedOnce;
 
-  return it;
+  // Join the ranges of other into the ranges of this interval.
+  Ranges::iterator cur = ranges.begin();
+  for (Ranges::const_iterator i = other.ranges.begin(),
+         e = other.ranges.end(); i != e; ++i)
+    cur = addRangeFrom(*i, cur);
+
+  weight += other.weight;
 }
 
 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {


Index: llvm/lib/CodeGen/LiveInterval.h
diff -u llvm/lib/CodeGen/LiveInterval.h:1.3 llvm/lib/CodeGen/LiveInterval.h:1.4
--- llvm/lib/CodeGen/LiveInterval.h:1.3	Fri Jul 23 13:39:12 2004
+++ llvm/lib/CodeGen/LiveInterval.h	Fri Jul 23 14:38:44 2004
@@ -97,7 +97,12 @@
 
     bool overlaps(const LiveInterval& other) const;
 
-    void addRange(LiveRange R);
+    /// addRange - Add the specified LiveRange to this interval, merging
+    /// intervals as appropriate.  This returns an iterator to the inserted live
+    /// range (which may have grown since it was inserted.
+    void addRange(LiveRange LR) {
+      addRangeFrom(LR, ranges.begin());
+    }
 
     void join(const LiveInterval& other);
 
@@ -110,8 +115,9 @@
     }
 
   private:
-    Ranges::iterator mergeRangesForward(Ranges::iterator it);
-    Ranges::iterator mergeRangesBackward(Ranges::iterator it);
+    Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
+    void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
+    Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
   };
 
   std::ostream& operator<<(std::ostream& os, const LiveInterval& li);





More information about the llvm-commits mailing list