2012-06-02 13:47:12 +00:00
|
|
|
//===- IntegersSubsetMapping.h - Mapping subset ==> Successor ---*- C++ -*-===//
|
2012-04-24 18:31:10 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// @file
|
2012-06-02 13:47:12 +00:00
|
|
|
/// IntegersSubsetMapping is mapping from A to B, where
|
|
|
|
/// Items in A is subsets of integers,
|
|
|
|
/// Items in B some pointers (Successors).
|
|
|
|
/// If user which to add another subset for successor that is already
|
|
|
|
/// exists in mapping, IntegersSubsetMapping merges existing subset with
|
|
|
|
/// added one.
|
2012-04-24 18:31:10 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef CRSBUILDER_H_
|
|
|
|
#define CRSBUILDER_H_
|
|
|
|
|
2012-05-29 12:26:47 +00:00
|
|
|
#include "llvm/Support/IntegersSubset.h"
|
2012-04-24 18:31:10 +00:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-06-02 07:26:00 +00:00
|
|
|
template <class SuccessorClass,
|
|
|
|
class IntegersSubsetTy = IntegersSubset,
|
|
|
|
class IntTy = IntItem>
|
2012-05-29 12:26:47 +00:00
|
|
|
class IntegersSubsetMapping {
|
2012-06-26 11:57:43 +00:00
|
|
|
// FIXME: To much similar iterators typedefs, similar names.
|
|
|
|
// - Rename RangeIterator to the cluster iterator.
|
|
|
|
// - Remove unused "add" methods.
|
|
|
|
// - Class contents needs cleaning.
|
2012-04-24 18:31:10 +00:00
|
|
|
public:
|
|
|
|
|
2012-06-02 07:26:00 +00:00
|
|
|
typedef IntRange<IntTy> RangeTy;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
struct RangeEx : public RangeTy {
|
|
|
|
RangeEx() : Weight(1) {}
|
2012-06-02 09:42:43 +00:00
|
|
|
RangeEx(const RangeTy &R) : RangeTy(R), Weight(1) {}
|
2012-06-02 07:26:00 +00:00
|
|
|
RangeEx(const IntTy &C) : RangeTy(C), Weight(1) {}
|
|
|
|
RangeEx(const IntTy &L, const IntTy &H) : RangeTy(L, H), Weight(1) {}
|
|
|
|
RangeEx(const IntTy &L, const IntTy &H, unsigned W) :
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeTy(L, H), Weight(W) {}
|
|
|
|
unsigned Weight;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<RangeEx, SuccessorClass*> Cluster;
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
typedef std::list<RangeTy> RangesCollection;
|
|
|
|
typedef typename RangesCollection::iterator RangesCollectionIt;
|
|
|
|
typedef typename RangesCollection::const_iterator RangesCollectionConstIt;
|
|
|
|
typedef IntegersSubsetMapping<SuccessorClass, IntegersSubsetTy, IntTy> self;
|
|
|
|
|
2012-04-24 18:31:10 +00:00
|
|
|
protected:
|
|
|
|
|
2012-07-03 13:46:45 +00:00
|
|
|
typedef std::map<RangeEx, SuccessorClass*> CaseItems;
|
2012-04-24 18:31:10 +00:00
|
|
|
typedef typename CaseItems::iterator CaseItemIt;
|
|
|
|
typedef typename CaseItems::const_iterator CaseItemConstIt;
|
2012-05-29 12:26:47 +00:00
|
|
|
|
2012-06-02 13:47:12 +00:00
|
|
|
// TODO: Change unclean CRS prefixes to SubsetMap for example.
|
2012-05-29 12:26:47 +00:00
|
|
|
typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
|
|
|
|
typedef typename CRSMap::iterator CRSMapIt;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
CaseItems Items;
|
2012-07-03 13:29:14 +00:00
|
|
|
bool SingleNumbersOnly;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
|
2012-06-02 09:42:43 +00:00
|
|
|
return LItem->first.getHigh() >= RItem->first.getLow();
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
|
|
|
|
if (LItem->second != RItem->second) {
|
|
|
|
assert(!isIntersected(LItem, RItem) &&
|
|
|
|
"Intersected items with different successors!");
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-02 09:42:43 +00:00
|
|
|
APInt RLow = RItem->first.getLow();
|
2012-04-24 18:31:10 +00:00
|
|
|
if (RLow != APInt::getNullValue(RLow.getBitWidth()))
|
|
|
|
--RLow;
|
2012-06-02 09:42:43 +00:00
|
|
|
return LItem->first.getHigh() >= RLow;
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
enum DiffProcessState {
|
|
|
|
L_OPENED,
|
|
|
|
INTERSECT_OPENED,
|
|
|
|
R_OPENED,
|
|
|
|
ALL_IS_CLOSED
|
|
|
|
};
|
|
|
|
|
|
|
|
class DiffStateMachine {
|
|
|
|
|
|
|
|
DiffProcessState State;
|
|
|
|
IntTy OpenPt;
|
|
|
|
SuccessorClass *CurrentLSuccessor;
|
|
|
|
SuccessorClass *CurrentRSuccessor;
|
|
|
|
|
|
|
|
self *LeftMapping;
|
|
|
|
self *IntersectionMapping;
|
|
|
|
self *RightMapping;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef
|
|
|
|
IntegersSubsetMapping<SuccessorClass, IntegersSubsetTy, IntTy> MappingTy;
|
|
|
|
|
|
|
|
DiffStateMachine(MappingTy *L,
|
|
|
|
MappingTy *Intersection,
|
|
|
|
MappingTy *R) :
|
|
|
|
State(ALL_IS_CLOSED),
|
|
|
|
LeftMapping(L),
|
|
|
|
IntersectionMapping(Intersection),
|
|
|
|
RightMapping(R)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void onLOpen(const IntTy &Pt, SuccessorClass *S) {
|
|
|
|
switch (State) {
|
|
|
|
case R_OPENED:
|
|
|
|
if (Pt > OpenPt/*Don't add empty ranges.*/ && RightMapping)
|
|
|
|
RightMapping->add(OpenPt, Pt-1, CurrentRSuccessor);
|
|
|
|
State = INTERSECT_OPENED;
|
|
|
|
break;
|
|
|
|
case ALL_IS_CLOSED:
|
|
|
|
State = L_OPENED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CurrentLSuccessor = S;
|
|
|
|
OpenPt = Pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onLClose(const IntTy &Pt) {
|
|
|
|
switch (State) {
|
|
|
|
case L_OPENED:
|
|
|
|
assert(Pt >= OpenPt &&
|
|
|
|
"Subset is not sorted or contains overlapped ranges");
|
|
|
|
if (LeftMapping)
|
|
|
|
LeftMapping->add(OpenPt, Pt, CurrentLSuccessor);
|
|
|
|
State = ALL_IS_CLOSED;
|
|
|
|
break;
|
|
|
|
case INTERSECT_OPENED:
|
|
|
|
if (IntersectionMapping)
|
|
|
|
IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
|
|
|
|
OpenPt = Pt + 1;
|
|
|
|
State = R_OPENED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onROpen(const IntTy &Pt, SuccessorClass *S) {
|
|
|
|
switch (State) {
|
|
|
|
case L_OPENED:
|
|
|
|
if (Pt > OpenPt && LeftMapping)
|
|
|
|
LeftMapping->add(OpenPt, Pt-1, CurrentLSuccessor);
|
|
|
|
State = INTERSECT_OPENED;
|
|
|
|
break;
|
|
|
|
case ALL_IS_CLOSED:
|
|
|
|
State = R_OPENED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CurrentRSuccessor = S;
|
|
|
|
OpenPt = Pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onRClose(const IntTy &Pt) {
|
|
|
|
switch (State) {
|
|
|
|
case R_OPENED:
|
|
|
|
assert(Pt >= OpenPt &&
|
|
|
|
"Subset is not sorted or contains overlapped ranges");
|
|
|
|
if (RightMapping)
|
|
|
|
RightMapping->add(OpenPt, Pt, CurrentRSuccessor);
|
|
|
|
State = ALL_IS_CLOSED;
|
|
|
|
break;
|
|
|
|
case INTERSECT_OPENED:
|
|
|
|
if (IntersectionMapping)
|
|
|
|
IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
|
|
|
|
OpenPt = Pt + 1;
|
|
|
|
State = L_OPENED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:29:26 +00:00
|
|
|
void onLROpen(const IntTy &Pt,
|
2012-06-26 11:57:43 +00:00
|
|
|
SuccessorClass *LS,
|
|
|
|
SuccessorClass *RS) {
|
|
|
|
switch (State) {
|
|
|
|
case ALL_IS_CLOSED:
|
|
|
|
State = INTERSECT_OPENED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CurrentLSuccessor = LS;
|
|
|
|
CurrentRSuccessor = RS;
|
|
|
|
OpenPt = Pt;
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:29:26 +00:00
|
|
|
void onLRClose(const IntTy &Pt) {
|
2012-06-26 11:57:43 +00:00
|
|
|
switch (State) {
|
|
|
|
case INTERSECT_OPENED:
|
|
|
|
if (IntersectionMapping)
|
|
|
|
IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
|
|
|
|
State = ALL_IS_CLOSED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Got unexpected point.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLOpened() { return State == L_OPENED; }
|
|
|
|
bool isROpened() { return State == R_OPENED; }
|
|
|
|
};
|
2012-07-03 13:29:14 +00:00
|
|
|
|
|
|
|
void diff_single_numbers(self *LExclude, self *Intersection, self *RExclude,
|
|
|
|
const self& RHS) {
|
|
|
|
|
|
|
|
CaseItemConstIt L = Items.begin(), R = RHS.Items.begin();
|
|
|
|
CaseItemConstIt el = Items.end(), er = RHS.Items.end();
|
|
|
|
while (L != el && R != er) {
|
|
|
|
const Cluster &LCluster = *L;
|
|
|
|
const RangeEx &LRange = LCluster.first;
|
|
|
|
const Cluster &RCluster = *R;
|
|
|
|
const RangeEx &RRange = RCluster.first;
|
|
|
|
|
|
|
|
if (LRange.getLow() < RRange.getLow()) {
|
|
|
|
if (LExclude)
|
|
|
|
LExclude->add(LRange.getLow(), LCluster.second);
|
|
|
|
++L;
|
|
|
|
} else if (LRange.getLow() > RRange.getLow()) {
|
|
|
|
if (RExclude)
|
|
|
|
RExclude->add(RRange.getLow(), RCluster.second);
|
|
|
|
++R;
|
|
|
|
} else {
|
|
|
|
if (Intersection)
|
|
|
|
Intersection->add(LRange.getLow(), LCluster.second);
|
|
|
|
++L;
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (L != Items.end()) {
|
|
|
|
if (LExclude)
|
|
|
|
do {
|
|
|
|
LExclude->add(L->first, L->second);
|
|
|
|
++L;
|
|
|
|
} while (L != Items.end());
|
|
|
|
} else if (R != RHS.Items.end()) {
|
|
|
|
if (RExclude)
|
|
|
|
do {
|
|
|
|
RExclude->add(R->first, R->second);
|
|
|
|
++R;
|
|
|
|
} while (R != RHS.Items.end());
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 07:43:08 +00:00
|
|
|
|
2012-04-24 18:31:10 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Don't public CaseItems itself. Don't allow edit the Items directly.
|
|
|
|
// Just present the user way to iterate over the internal collection
|
|
|
|
// sharing iterator, begin() and end(). Editing should be controlled by
|
|
|
|
// factory.
|
|
|
|
typedef CaseItemIt RangeIterator;
|
|
|
|
|
2012-06-01 16:17:57 +00:00
|
|
|
typedef std::pair<SuccessorClass*, IntegersSubsetTy> Case;
|
2012-05-29 12:26:47 +00:00
|
|
|
typedef std::list<Case> Cases;
|
2012-07-03 14:15:36 +00:00
|
|
|
typedef typename Cases::iterator CasesIt;
|
2012-05-29 12:26:47 +00:00
|
|
|
|
2012-07-03 13:46:45 +00:00
|
|
|
IntegersSubsetMapping() : SingleNumbersOnly(true) {}
|
2012-04-24 18:31:10 +00:00
|
|
|
|
2012-07-03 14:15:36 +00:00
|
|
|
bool verify() {
|
|
|
|
RangeIterator DummyErrItem;
|
|
|
|
return verify(DummyErrItem);
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:44:40 +00:00
|
|
|
bool verify(RangeIterator& errItem) {
|
2012-04-24 18:31:10 +00:00
|
|
|
if (Items.empty())
|
|
|
|
return true;
|
2012-06-05 07:43:08 +00:00
|
|
|
for (CaseItemIt j = Items.begin(), i = j++, e = Items.end();
|
2012-04-24 18:31:10 +00:00
|
|
|
j != e; i = j++) {
|
2012-06-02 13:47:12 +00:00
|
|
|
if (isIntersected(i, j) && i->second != j->second) {
|
2012-05-21 10:44:40 +00:00
|
|
|
errItem = j;
|
2012-04-24 18:31:10 +00:00
|
|
|
return false;
|
2012-05-21 10:44:40 +00:00
|
|
|
}
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-03 13:46:45 +00:00
|
|
|
|
2012-07-03 14:15:36 +00:00
|
|
|
bool isOverlapped(self &RHS) {
|
|
|
|
if (Items.empty() || RHS.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (CaseItemIt L = Items.begin(), R = RHS.Items.begin(),
|
|
|
|
el = Items.end(), er = RHS.Items.end(); L != el && R != er;) {
|
|
|
|
|
|
|
|
const RangeTy &LRange = L->first;
|
|
|
|
const RangeTy &RRange = R->first;
|
|
|
|
|
|
|
|
if (LRange.getLow() > RRange.getLow()) {
|
|
|
|
if (RRange.isSingleNumber() || LRange.getLow() > RRange.getHigh())
|
|
|
|
++R;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
} else if (LRange.getLow() < RRange.getLow()) {
|
|
|
|
if (LRange.isSingleNumber() || LRange.getHigh() < RRange.getLow())
|
|
|
|
++L;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
} else // iRange.getLow() == jRange.getLow()
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 18:31:10 +00:00
|
|
|
void optimize() {
|
|
|
|
if (Items.size() < 2)
|
|
|
|
return;
|
|
|
|
CaseItems OldItems = Items;
|
|
|
|
Items.clear();
|
2012-06-02 09:42:43 +00:00
|
|
|
const IntTy *Low = &OldItems.begin()->first.getLow();
|
|
|
|
const IntTy *High = &OldItems.begin()->first.getHigh();
|
2012-04-24 18:31:10 +00:00
|
|
|
unsigned Weight = 1;
|
|
|
|
SuccessorClass *Successor = OldItems.begin()->second;
|
2012-06-05 07:43:08 +00:00
|
|
|
for (CaseItemIt j = OldItems.begin(), i = j++, e = OldItems.end();
|
|
|
|
j != e; i = j++) {
|
2012-04-24 18:31:10 +00:00
|
|
|
if (isJoinable(i, j)) {
|
2012-06-02 09:42:43 +00:00
|
|
|
const IntTy *CurHigh = &j->first.getHigh();
|
2012-04-24 18:31:10 +00:00
|
|
|
++Weight;
|
2012-06-01 10:06:14 +00:00
|
|
|
if (*CurHigh > *High)
|
2012-04-24 18:31:10 +00:00
|
|
|
High = CurHigh;
|
|
|
|
} else {
|
2012-05-28 12:39:09 +00:00
|
|
|
RangeEx R(*Low, *High, Weight);
|
2012-04-24 18:31:10 +00:00
|
|
|
add(R, Successor);
|
2012-06-02 09:42:43 +00:00
|
|
|
Low = &j->first.getLow();
|
|
|
|
High = &j->first.getHigh();
|
2012-04-24 18:31:10 +00:00
|
|
|
Weight = 1;
|
|
|
|
Successor = j->second;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 12:39:09 +00:00
|
|
|
RangeEx R(*Low, *High, Weight);
|
2012-04-24 18:31:10 +00:00
|
|
|
add(R, Successor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a constant value.
|
2012-06-02 07:26:00 +00:00
|
|
|
void add(const IntTy &C, SuccessorClass *S = 0) {
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeTy R(C);
|
|
|
|
add(R, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a range.
|
2012-06-02 07:26:00 +00:00
|
|
|
void add(const IntTy &Low, const IntTy &High, SuccessorClass *S = 0) {
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeTy R(Low, High);
|
|
|
|
add(R, S);
|
|
|
|
}
|
2012-05-28 12:39:09 +00:00
|
|
|
void add(const RangeTy &R, SuccessorClass *S = 0) {
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeEx REx = R;
|
|
|
|
add(REx, S);
|
|
|
|
}
|
2012-05-28 12:39:09 +00:00
|
|
|
void add(const RangeEx &R, SuccessorClass *S = 0) {
|
2012-07-03 13:46:45 +00:00
|
|
|
Items.insert(std::make_pair(R, S));
|
2012-07-03 13:29:14 +00:00
|
|
|
if (!R.isSingleNumber())
|
|
|
|
SingleNumbersOnly = false;
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds all ranges and values from given ranges set to the current
|
2012-06-02 13:47:12 +00:00
|
|
|
/// mapping.
|
2012-06-05 07:43:08 +00:00
|
|
|
void add(const IntegersSubsetTy &CRS, SuccessorClass *S = 0) {
|
2012-04-24 18:31:10 +00:00
|
|
|
for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
|
|
|
|
RangeTy R = CRS.getItem(i);
|
|
|
|
add(R, S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
void add(self& RHS) {
|
2012-07-03 13:46:45 +00:00
|
|
|
Items.insert(RHS.Items.begin(), RHS.Items.end());
|
2012-07-03 13:29:14 +00:00
|
|
|
if (!RHS.SingleNumbersOnly)
|
|
|
|
SingleNumbersOnly = false;
|
2012-06-26 11:57:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:15:36 +00:00
|
|
|
void add(self& RHS, SuccessorClass *S) {
|
|
|
|
for (CaseItemIt i = RHS.Items.begin(), e = RHS.Items.end(); i != e; ++i)
|
|
|
|
add(i->first, S);
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
void add(const RangesCollection& RHS, SuccessorClass *S = 0) {
|
|
|
|
for (RangesCollectionConstIt i = RHS.begin(), e = RHS.end(); i != e; ++i)
|
|
|
|
add(*i, S);
|
|
|
|
}
|
|
|
|
|
2012-04-24 18:31:10 +00:00
|
|
|
/// Removes items from set.
|
|
|
|
void removeItem(RangeIterator i) { Items.erase(i); }
|
|
|
|
|
2012-07-03 14:15:36 +00:00
|
|
|
/// Moves whole case from current mapping to the NewMapping object.
|
|
|
|
void detachCase(self& NewMapping, SuccessorClass *Succ) {
|
|
|
|
for (CaseItemIt i = Items.begin(); i != Items.end();)
|
|
|
|
if (i->second == Succ) {
|
|
|
|
NewMapping.add(i->first, i->second);
|
|
|
|
Items.erase(i++);
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes all clusters for given successor.
|
|
|
|
void removeCase(SuccessorClass *Succ) {
|
|
|
|
for (CaseItemIt i = Items.begin(); i != Items.end();)
|
|
|
|
if (i->second == Succ) {
|
|
|
|
Items.erase(i++);
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find successor that satisfies given value.
|
|
|
|
SuccessorClass *findSuccessor(const IntTy& Val) {
|
|
|
|
for (CaseItemIt i = Items.begin(); i != Items.end(); ++i) {
|
|
|
|
if (i->first.isInRange(Val))
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
/// Calculates the difference between this mapping and RHS.
|
|
|
|
/// THIS without RHS is placed into LExclude,
|
|
|
|
/// RHS without THIS is placed into RExclude,
|
|
|
|
/// THIS intersect RHS is placed into Intersection.
|
|
|
|
void diff(self *LExclude, self *Intersection, self *RExclude,
|
|
|
|
const self& RHS) {
|
|
|
|
|
2012-07-03 13:29:14 +00:00
|
|
|
if (SingleNumbersOnly && RHS.SingleNumbersOnly) {
|
|
|
|
diff_single_numbers(LExclude, Intersection, RExclude, RHS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
DiffStateMachine Machine(LExclude, Intersection, RExclude);
|
|
|
|
|
|
|
|
CaseItemConstIt L = Items.begin(), R = RHS.Items.begin();
|
2012-07-03 13:29:14 +00:00
|
|
|
CaseItemConstIt el = Items.end(), er = RHS.Items.end();
|
|
|
|
while (L != el && R != er) {
|
2012-06-26 11:57:43 +00:00
|
|
|
const Cluster &LCluster = *L;
|
|
|
|
const RangeEx &LRange = LCluster.first;
|
|
|
|
const Cluster &RCluster = *R;
|
|
|
|
const RangeEx &RRange = RCluster.first;
|
|
|
|
|
|
|
|
if (LRange.getHigh() < RRange.getLow()) {
|
|
|
|
Machine.onLOpen(LRange.getLow(), LCluster.second);
|
|
|
|
Machine.onLClose(LRange.getHigh());
|
|
|
|
++L;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LRange.getLow() > RRange.getHigh()) {
|
|
|
|
Machine.onROpen(RRange.getLow(), RCluster.second);
|
|
|
|
Machine.onRClose(RRange.getHigh());
|
|
|
|
++R;
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-03 13:29:14 +00:00
|
|
|
|
|
|
|
if (LRange.isSingleNumber() && RRange.isSingleNumber()) {
|
2012-07-03 14:29:26 +00:00
|
|
|
Machine.onLROpen(LRange.getLow(), LCluster.second, RCluster.second);
|
|
|
|
Machine.onLRClose(LRange.getLow());
|
2012-07-03 13:29:14 +00:00
|
|
|
++L;
|
|
|
|
++R;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-26 11:57:43 +00:00
|
|
|
|
2012-07-03 13:29:14 +00:00
|
|
|
if (LRange.isSingleNumber()) {
|
|
|
|
Machine.onLOpen(LRange.getLow(), LCluster.second);
|
|
|
|
Machine.onLClose(LRange.getLow());
|
|
|
|
++L;
|
|
|
|
while(L != Items.end() && L->first.getHigh() < RRange.getHigh()) {
|
|
|
|
Machine.onLOpen(LRange.getLow(), LCluster.second);
|
|
|
|
Machine.onLClose(LRange.getLow());
|
|
|
|
++L;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (RRange.isSingleNumber()) {
|
|
|
|
Machine.onROpen(R->first.getLow(), R->second);
|
|
|
|
Machine.onRClose(R->first.getHigh());
|
|
|
|
++R;
|
|
|
|
while(R != RHS.Items.end() && R->first.getHigh() < LRange.getHigh()) {
|
|
|
|
Machine.onROpen(R->first.getLow(), R->second);
|
|
|
|
Machine.onRClose(R->first.getHigh());
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else
|
2012-06-26 11:57:43 +00:00
|
|
|
if (LRange.getLow() < RRange.getLow()) {
|
|
|
|
// May be opened in previous iteration.
|
|
|
|
if (!Machine.isLOpened())
|
|
|
|
Machine.onLOpen(LRange.getLow(), LCluster.second);
|
|
|
|
Machine.onROpen(RRange.getLow(), RCluster.second);
|
|
|
|
}
|
|
|
|
else if (RRange.getLow() < LRange.getLow()) {
|
|
|
|
if (!Machine.isROpened())
|
|
|
|
Machine.onROpen(RRange.getLow(), RCluster.second);
|
|
|
|
Machine.onLOpen(LRange.getLow(), LCluster.second);
|
|
|
|
}
|
|
|
|
else
|
2012-07-03 14:29:26 +00:00
|
|
|
Machine.onLROpen(LRange.getLow(), LCluster.second, RCluster.second);
|
2012-06-26 11:57:43 +00:00
|
|
|
|
|
|
|
if (LRange.getHigh() < RRange.getHigh()) {
|
|
|
|
Machine.onLClose(LRange.getHigh());
|
|
|
|
++L;
|
|
|
|
while(L != Items.end() && L->first.getHigh() < RRange.getHigh()) {
|
|
|
|
Machine.onLOpen(L->first.getLow(), L->second);
|
|
|
|
Machine.onLClose(L->first.getHigh());
|
|
|
|
++L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RRange.getHigh() < LRange.getHigh()) {
|
|
|
|
Machine.onRClose(RRange.getHigh());
|
|
|
|
++R;
|
|
|
|
while(R != RHS.Items.end() && R->first.getHigh() < LRange.getHigh()) {
|
|
|
|
Machine.onROpen(R->first.getLow(), R->second);
|
|
|
|
Machine.onRClose(R->first.getHigh());
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-07-03 14:29:26 +00:00
|
|
|
Machine.onLRClose(LRange.getHigh());
|
2012-06-26 11:57:43 +00:00
|
|
|
++L;
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (L != Items.end()) {
|
|
|
|
if (Machine.isLOpened()) {
|
|
|
|
Machine.onLClose(L->first.getHigh());
|
|
|
|
++L;
|
|
|
|
}
|
|
|
|
if (LExclude)
|
|
|
|
while (L != Items.end()) {
|
|
|
|
LExclude->add(L->first, L->second);
|
|
|
|
++L;
|
|
|
|
}
|
|
|
|
} else if (R != RHS.Items.end()) {
|
|
|
|
if (Machine.isROpened()) {
|
|
|
|
Machine.onRClose(R->first.getHigh());
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
if (RExclude)
|
|
|
|
while (R != RHS.Items.end()) {
|
|
|
|
RExclude->add(R->first, R->second);
|
|
|
|
++R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 18:31:10 +00:00
|
|
|
/// Builds the finalized case objects.
|
2012-07-03 14:15:36 +00:00
|
|
|
void getCases(Cases& TheCases, bool PreventMerging = false) {
|
|
|
|
//FIXME: PreventMerging is a temporary parameter.
|
|
|
|
//Currently a set of passes is still knows nothing about
|
|
|
|
//switches with case ranges, and if these passes meet switch
|
|
|
|
//with complex case that crashs the application.
|
|
|
|
if (PreventMerging) {
|
|
|
|
for (RangeIterator i = this->begin(); i != this->end(); ++i) {
|
|
|
|
RangesCollection SingleRange;
|
|
|
|
SingleRange.push_back(i->first);
|
|
|
|
TheCases.push_back(std::make_pair(i->second,
|
|
|
|
IntegersSubsetTy(SingleRange)));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-04-24 18:31:10 +00:00
|
|
|
CRSMap TheCRSMap;
|
|
|
|
for (RangeIterator i = this->begin(); i != this->end(); ++i)
|
|
|
|
TheCRSMap[i->second].push_back(i->first);
|
|
|
|
for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
|
2012-06-01 16:17:57 +00:00
|
|
|
TheCases.push_back(std::make_pair(i->first, IntegersSubsetTy(i->second)));
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the finalized case objects ignoring successor values, as though
|
|
|
|
/// all ranges belongs to the same successor.
|
2012-06-05 07:43:08 +00:00
|
|
|
IntegersSubsetTy getCase() {
|
2012-04-24 18:31:10 +00:00
|
|
|
RangesCollection Ranges;
|
|
|
|
for (RangeIterator i = this->begin(); i != this->end(); ++i)
|
|
|
|
Ranges.push_back(i->first);
|
2012-06-01 16:17:57 +00:00
|
|
|
return IntegersSubsetTy(Ranges);
|
2012-05-29 12:26:47 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:15:36 +00:00
|
|
|
/// Returns pointer to value of case if it is single-numbered or 0
|
|
|
|
/// in another case.
|
|
|
|
const IntTy* getCaseSingleNumber(SuccessorClass *Succ) {
|
|
|
|
const IntTy* Res = 0;
|
|
|
|
for (CaseItemIt i = Items.begin(); i != Items.end(); ++i)
|
|
|
|
if (i->second == Succ) {
|
|
|
|
if (!i->first.isSingleNumber())
|
|
|
|
return 0;
|
|
|
|
if (Res)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
Res = &(i->first.getLow());
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2012-05-29 12:26:47 +00:00
|
|
|
/// Returns true if there is no ranges and values inside.
|
|
|
|
bool empty() const { return Items.empty(); }
|
|
|
|
|
2012-06-05 07:57:36 +00:00
|
|
|
void clear() {
|
|
|
|
Items.clear();
|
|
|
|
// Don't reset Sorted flag:
|
|
|
|
// 1. For empty mapping it matters nothing.
|
|
|
|
// 2. After first item will added Sorted flag will cleared.
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:57:43 +00:00
|
|
|
// Returns number of clusters
|
|
|
|
unsigned size() const {
|
|
|
|
return Items.size();
|
|
|
|
}
|
|
|
|
|
2012-05-29 12:26:47 +00:00
|
|
|
RangeIterator begin() { return Items.begin(); }
|
|
|
|
RangeIterator end() { return Items.end(); }
|
2012-04-24 18:31:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BasicBlock;
|
2012-06-02 07:26:00 +00:00
|
|
|
typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CRSBUILDER_H_ */
|