Add APFloat interface to ConstantFPSDNode. Change
[oota-llvm.git] / lib / CodeGen / LiveInterval.cpp
index 8be967792915f29df9cc08a95ae0bf950fe580cd..9697d43bb4d763e671056f57185f46fd2eace488 100644 (file)
 
 #include "llvm/CodeGen/LiveInterval.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Streams.h"
 #include "llvm/Target/MRegisterInfo.h"
 #include <algorithm>
-#include <iostream>
 #include <map>
+#include <ostream>
 using namespace llvm;
 
 // An example for liveAt():
@@ -108,6 +109,7 @@ bool LiveInterval::overlapsFrom(const LiveInterval& other,
 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
   assert(I != ranges.end() && "Not a valid interval!");
   unsigned ValId = I->ValId;
+  unsigned OldEnd = I->end;
 
   // Search for the first interval that we can't merge with.
   Ranges::iterator MergeTo = next(I);
@@ -120,7 +122,10 @@ void LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) {
 
   // Erase any dead ranges.
   ranges.erase(next(I), MergeTo);
-  
+
+  // Update kill info.
+  removeKillForValNum(ValId, OldEnd, I->end-1);
+
   // If the newly formed range now touches the range after it and if they have
   // the same value number, merge the two ranges into one range.
   Ranges::iterator Next = next(I);
@@ -227,9 +232,10 @@ void LiveInterval::removeRange(unsigned Start, unsigned End) {
 
   // If the span we are removing is at the start of the LiveRange, adjust it.
   if (I->start == Start) {
-    if (I->end == End)
+    if (I->end == End) {
+      removeKillForValNum(I->ValId, Start, End);
       ranges.erase(I);  // Removed the whole LiveRange.
-    else
+    else
       I->start = End;
     return;
   }
@@ -237,6 +243,7 @@ void LiveInterval::removeRange(unsigned Start, unsigned End) {
   // Otherwise if the span we are removing is at the end of the LiveRange,
   // adjust the other way.
   if (I->end == End) {
+    removeKillForValNum(I->ValId, Start, End);
     I->end = Start;
     return;
   }
@@ -280,7 +287,7 @@ LiveInterval::FindLiveRangeContaining(unsigned Idx) {
 /// the intervals are not joinable, this aborts.
 void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
                         int *RHSValNoAssignments, 
-                        SmallVector<unsigned, 16> &NewInstDefiningValue) {
+                        SmallVector<VNInfo, 16> &NewValueNumberInfo) {
   
   // Try to do the least amount of work possible.  In particular, if there are
   // more liverange chunks in the other set than there are in the 'this' set,
@@ -288,7 +295,6 @@ void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
   //
   // Also, if one range is a physreg and one is a vreg, we always merge from the
   // vreg into the physreg, which leaves the vreg intervals pristine.
-  unsigned OtherOffs = 1, ThisOffs = 0;
   if ((Other.ranges.size() > ranges.size() &&
       MRegisterInfo::isVirtualRegister(reg)) ||
       MRegisterInfo::isPhysicalRegister(Other.reg)) {
@@ -300,7 +306,7 @@ void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
   // we want to avoid the interval scan if not.
   bool MustMapCurValNos = false;
   for (unsigned i = 0, e = getNumValNums(); i != e; ++i) {
-    if (InstDefiningValue[i] == ~2U) continue;  // tombstone value #
+    if (ValueNumberInfo[i].def == ~1U) continue;  // tombstone value #
     if (i != (unsigned)LHSValNoAssignments[i]) {
       MustMapCurValNos = true;
       break;
@@ -336,7 +342,11 @@ void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
     // If we merge some live ranges, chop off the end.
     ranges.erase(OutIt, end());
   }
-  
+
+  // Update val# info first. Increasing live ranges may invalidate some kills.
+  ValueNumberInfo.clear();
+  ValueNumberInfo.append(NewValueNumberInfo.begin(), NewValueNumberInfo.end());
+
   // Okay, now insert the RHS live ranges into the LHS.
   iterator InsertPos = begin();
   for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) {
@@ -345,12 +355,28 @@ void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
     InsertPos = addRangeFrom(*I, InsertPos);
   }
 
-  InstDefiningValue.clear();
-  InstDefiningValue.append(NewInstDefiningValue.begin(), 
-                           NewInstDefiningValue.end());
   weight += Other.weight;
+  if (Other.preference && !preference)
+    preference = Other.preference;
+}
+
+/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
+/// interval as the specified value number.  The LiveRanges in RHS are
+/// allowed to overlap with LiveRanges in the current interval, but only if
+/// the overlapping LiveRanges have the specified value number.
+void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, 
+                                        unsigned LHSValNo) {
+  // TODO: Make this more efficient.
+  iterator InsertPos = begin();
+  for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
+    // Map the ValId in the other live range to the current live range.
+    LiveRange Tmp = *I;
+    Tmp.ValId = LHSValNo;
+    InsertPos = addRangeFrom(Tmp, InsertPos);
+  }
 }
 
+
 /// MergeInClobberRanges - For any live ranges that are not defined in the
 /// current interval, but are defined in the Clobbers interval, mark them
 /// used with an unknown definition value.
@@ -360,7 +386,7 @@ void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers) {
   // Find a value # to use for the clobber ranges.  If there is already a value#
   // for unknown values, use it.
   // FIXME: Use a single sentinal number for these!
-  unsigned ClobberValNo = getNextValue(~0U);
+  unsigned ClobberValNo = getNextValue(~0U, 0);
   
   iterator IP = begin();
   for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
@@ -399,7 +425,7 @@ void LiveInterval::MergeValueNumberInto(unsigned V1, unsigned V2) {
 
   // Make sure V2 is smaller than V1.
   if (V1 < V2) {
-    setInstDefiningValNum(V1, getInstForValNum(V2));
+    copyValNumInfo(V1, V2);
     std::swap(V1, V2);
   }
 
@@ -443,20 +469,26 @@ void LiveInterval::MergeValueNumberInto(unsigned V1, unsigned V2) {
   // ~1U so it can be nuked later.
   if (V1 == getNumValNums()-1) {
     do {
-      InstDefiningValue.pop_back();
-    } while (InstDefiningValue.back() == ~1U);
+      ValueNumberInfo.pop_back();
+    } while (ValueNumberInfo.back().def == ~1U);
   } else {
-    InstDefiningValue[V1] = ~1U;
+    ValueNumberInfo[V1].def = ~1U;
   }
 }
 
+unsigned LiveInterval::getSize() const {
+  unsigned Sum = 0;
+  for (const_iterator I = begin(), E = end(); I != E; ++I)
+    Sum += I->end - I->start;
+  return Sum;
+}
 
 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
   return os << '[' << LR.start << ',' << LR.end << ':' << LR.ValId << ")";
 }
 
 void LiveRange::dump() const {
-  std::cerr << *this << "\n";
+  cerr << *this << "\n";
 }
 
 void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
@@ -482,15 +514,33 @@ void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const {
     for (unsigned i = 0; i != getNumValNums(); ++i) {
       if (i) OS << " ";
       OS << i << "@";
-      if (InstDefiningValue[i] == ~0U) {
-        OS << "?";
+      if (ValueNumberInfo[i].def == ~1U) {
+        OS << "x";
       } else {
-        OS << InstDefiningValue[i];
+        if (ValueNumberInfo[i].def == ~0U)
+          OS << "?";
+        else
+          OS << ValueNumberInfo[i].def;
+        unsigned e = ValueNumberInfo[i].kills.size();
+        if (e) {
+          OS << "-(";
+          for (unsigned j = 0; j != e; ++j) {
+            OS << ValueNumberInfo[i].kills[j];
+            if (j != e-1)
+              OS << " ";
+          }
+          OS << ")";
+        }
       }
     }
   }
 }
 
 void LiveInterval::dump() const {
-  std::cerr << *this << "\n";
+  cerr << *this << "\n";
+}
+
+
+void LiveRange::print(std::ostream &os) const {
+  os << *this;
 }