Fix the sense of joinable
[oota-llvm.git] / lib / CodeGen / LiveInterval.cpp
index e5331530807c3e4983efcd50b78673461e8b6865..6bd441f6431b4091fd15ed66f0ac7d1f0ed38fc7 100644 (file)
@@ -60,6 +60,7 @@ bool LiveInterval::overlaps(const LiveInterval& other) const {
   Ranges::const_iterator ie = ranges.end();
   Ranges::const_iterator j = other.ranges.begin();
   Ranges::const_iterator je = other.ranges.end();
+
   if (i->start < j->start) {
     i = std::upper_bound(i, ie, j->start);
     if (i != ranges.begin()) --i;
@@ -92,7 +93,48 @@ bool LiveInterval::overlaps(const LiveInterval& other) const {
 /// or if the destination of the copy is a single assignment value, and it
 /// only overlaps with one value in the source interval.
 bool LiveInterval::joinable(const LiveInterval &other, unsigned CopyIdx) const {
-  return overlaps(other);
+  const LiveRange *SourceLR = other.getLiveRangeContaining(CopyIdx-1);
+  const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
+  assert(SourceLR && DestLR && "Not joining due to a copy?");
+  unsigned OtherValIdx = SourceLR->ValId;
+  unsigned ThisValIdx = DestLR->ValId;
+
+  Ranges::const_iterator i = ranges.begin();
+  Ranges::const_iterator ie = ranges.end();
+  Ranges::const_iterator j = other.ranges.begin();
+  Ranges::const_iterator je = other.ranges.end();
+
+  if (i->start < j->start) {
+    i = std::upper_bound(i, ie, j->start);
+    if (i != ranges.begin()) --i;
+  } else if (j->start < i->start) {
+    j = std::upper_bound(j, je, i->start);
+    if (j != other.ranges.begin()) --j;
+  }
+
+  while (i != ie && j != je) {
+    if (i->start == j->start) {
+      // If this is not the allowed value merge, we cannot join.
+      if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
+        return false;
+    } else if (i->start < j->start) {
+      if (i->end > j->start) {
+        if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
+          return false;
+      }
+    } else {
+      if (j->end > i->start) {
+        if (i->ValId != ThisValIdx || j->ValId != OtherValIdx)
+          return false;
+      }
+    }
+    if (i->end < j->end)
+      ++i;
+    else
+      ++j;
+  }
+
+  return true;
 }
 
 
@@ -237,10 +279,10 @@ void LiveInterval::removeRange(unsigned Start, unsigned End) {
 
 /// getLiveRangeContaining - Return the live range that contains the
 /// specified index, or null if there is none.
-LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) {
-  Ranges::iterator It = std::upper_bound(ranges.begin(), ranges.end(), Idx);
+const LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) const {
+  Ranges::const_iterator It = std::upper_bound(ranges.begin(),ranges.end(),Idx);
   if (It != ranges.begin()) {
-    LiveRange &LR = *prior(It);
+    const LiveRange &LR = *prior(It);
     if (LR.contains(Idx))
       return &LR;
   }
@@ -254,8 +296,8 @@ LiveRange *LiveInterval::getLiveRangeContaining(unsigned Idx) {
 /// is the result of a copy instruction in the source program, that occurs at
 /// index 'CopyIdx' that copies from 'Other' to 'this'.
 void LiveInterval::join(LiveInterval &Other, unsigned CopyIdx) {
-  LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
-  LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
+  const LiveRange *SourceLR = Other.getLiveRangeContaining(CopyIdx-1);
+  const LiveRange *DestLR = getLiveRangeContaining(CopyIdx);
   assert(SourceLR && DestLR && "Not joining due to a copy?");
   unsigned MergedSrcValIdx = SourceLR->ValId;
   unsigned MergedDstValIdx = DestLR->ValId;