//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/LiveInterval.h"
+#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Streams.h"
#include "llvm/Target/MRegisterInfo.h"
ranges.erase(next(I), MergeTo);
// Update kill info.
- removeKills(*ValNo, OldEnd, I->end-1);
+ removeKills(ValNo, 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.
// If the span we are removing is at the start of the LiveRange, adjust it.
if (I->start == Start) {
if (I->end == End) {
- removeKills(*I->valno, Start, End);
+ removeKills(I->valno, Start, End);
ranges.erase(I); // Removed the whole LiveRange.
} else
I->start = End;
// Otherwise if the span we are removing is at the end of the LiveRange,
// adjust the other way.
if (I->end == End) {
- removeKills(*I->valno, Start, End);
+ removeKills(I->valno, Start, End);
I->end = Start;
return;
}
/// join - Join two live intervals (this, and other) together. This applies
/// mappings to the value numbers in the LHS/RHS intervals as specified. If
/// the intervals are not joinable, this aborts.
-void LiveInterval::join(LiveInterval &Other, int *LHSValNoAssignments,
- int *RHSValNoAssignments,
+void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments,
+ const int *RHSValNoAssignments,
SmallVector<VNInfo*, 16> &NewVNInfo) {
-
- // There might be some dead val#, create VNInfo for them.
- for (unsigned i = 0, e = NewVNInfo.size(); i != e; ++i) {
- VNInfo *VNI = NewVNInfo[i];
- if (!VNI) {
- VNI = new VNInfo(this, i, ~1U, 0);
- NewVNInfo[i] = VNI;
- }
- }
-
// Determine if any of our live range values are mapped. This is uncommon, so
- // we want to avoid the interval scan if not.
+ // we want to avoid the interval scan if not.
bool MustMapCurValNos = false;
- for (vni_iterator i = vni_begin(), e = vni_end(); i != e; ++i) {
- VNInfo *VNI = *i;
- unsigned VN = VNI->id;
- if (VNI->def == ~1U) continue; // tombstone value #
- if (VNI != NewVNInfo[LHSValNoAssignments[VN]]) {
+ unsigned NumVals = getNumValNums();
+ unsigned NumNewVals = NewVNInfo.size();
+ for (unsigned i = 0; i != NumVals; ++i) {
+ unsigned LHSValID = LHSValNoAssignments[i];
+ if (i != LHSValID ||
+ (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
MustMapCurValNos = true;
- break;
- }
}
// If we have to apply a mapping to our base interval assignment, rewrite it
}
// Remember assignements because val# ids are changing.
- std::vector<unsigned> OtherAssignments;
+ SmallVector<unsigned, 16> OtherAssignments;
for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
// Update val# info. Renumber them and make sure they all belong to this
- // LiveInterval now.
- valnos.clear();
- for (unsigned i = 0, e = NewVNInfo.size(); i != e; ++i) {
+ // LiveInterval now. Also remove dead val#'s.
+ unsigned NumValNos = 0;
+ for (unsigned i = 0; i < NumNewVals; ++i) {
VNInfo *VNI = NewVNInfo[i];
- VNI->parent = this;
- VNI->id = i; // Renumber val#.
- valnos.push_back(VNI);
+ if (VNI) {
+ if (i >= NumVals)
+ valnos.push_back(VNI);
+ else
+ valnos[NumValNos] = VNI;
+ VNI->id = NumValNos++; // Renumber val#.
+ }
}
+ if (NumNewVals < NumVals)
+ valnos.resize(NumNewVals); // shrinkify
// Okay, now insert the RHS live ranges into the LHS.
iterator InsertPos = begin();
for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
// Map the valno in the other live range to the current live range.
I->valno = NewVNInfo[OtherAssignments[RangeNo]];
+ assert(I->valno && "Adding a dead range?");
InsertPos = addRangeFrom(*I, InsertPos);
}
}
+/// MergeValueInAsValue - Merge all of the live ranges of a specific val#
+/// 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, it will replace the value numbers of the overlaped
+/// live ranges with the specified value number.
+void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS,
+ const VNInfo *RHSValNo, VNInfo *LHSValNo) {
+ SmallVector<VNInfo*, 4> ReplacedValNos;
+ iterator IP = begin();
+ for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
+ if (I->valno != RHSValNo)
+ continue;
+ unsigned Start = I->start, End = I->end;
+ IP = std::upper_bound(IP, end(), Start);
+ // If the start of this range overlaps with an existing liverange, trim it.
+ if (IP != begin() && IP[-1].end > Start) {
+ if (IP->valno != LHSValNo) {
+ ReplacedValNos.push_back(IP->valno);
+ IP->valno = LHSValNo; // Update val#.
+ }
+ Start = IP[-1].end;
+ // Trimmed away the whole range?
+ if (Start >= End) continue;
+ }
+ // If the end of this range overlaps with an existing liverange, trim it.
+ if (IP != end() && End > IP->start) {
+ if (IP->valno != LHSValNo) {
+ ReplacedValNos.push_back(IP->valno);
+ IP->valno = LHSValNo; // Update val#.
+ }
+ End = IP->start;
+ // If this trimmed away the whole range, ignore it.
+ if (Start == End) continue;
+ }
+
+ // Map the valno in the other live range to the current live range.
+ IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
+ }
+
+
+ SmallSet<VNInfo*, 4> Seen;
+ for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
+ VNInfo *V1 = ReplacedValNos[i];
+ if (Seen.insert(V1)) {
+ bool isDead = true;
+ for (const_iterator I = begin(), E = end(); I != E; ++I)
+ if (I->valno == V1) {
+ isDead = false;
+ break;
+ }
+ if (isDead) {
+ // Now that V1 is dead, remove it. If it is the largest value number,
+ // just nuke it (and any other deleted values neighboring it), otherwise
+ // mark it as ~1U so it can be nuked later.
+ if (V1->id == getNumValNums()-1) {
+ do {
+ VNInfo *VNI = valnos.back();
+ valnos.pop_back();
+ VNI->~VNInfo();
+ } while (valnos.back()->def == ~1U);
+ } else {
+ V1->def = ~1U;
+ }
+ }
+ }
+ }
+}
+
+
/// 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.
-void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers) {
+void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers,
+ BumpPtrAllocator &VNInfoAllocator) {
if (Clobbers.begin() == Clobbers.end()) return;
// 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!
- VNInfo *ClobberValNo = getNextValue(~0U, 0);
+ VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator);
iterator IP = begin();
for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
// Make sure V2 is smaller than V1.
if (V1->id < V2->id) {
- copyValNumInfo(*V1, *V2);
+ copyValNumInfo(V1, V2);
std::swap(V1, V2);
}
do {
VNInfo *VNI = valnos.back();
valnos.pop_back();
- delete VNI;
+ VNI->~VNInfo();
} while (valnos.back()->def == ~1U);
} else {
V1->def = ~1U;
}
}
+void LiveInterval::Copy(const LiveInterval &RHS,
+ BumpPtrAllocator &VNInfoAllocator) {
+ ranges.clear();
+ valnos.clear();
+ preference = RHS.preference;
+ weight = RHS.weight;
+ for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
+ const VNInfo *VNI = RHS.getValNumInfo(i);
+ VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator);
+ copyValNumInfo(NewVNI, VNI);
+ }
+ for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
+ const LiveRange &LR = RHS.ranges[i];
+ addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
+ }
+}
+
unsigned LiveInterval::getSize() const {
unsigned Sum = 0;
for (const_iterator I = begin(), E = end(); I != E; ++I)