2012-04-24 18:31:10 +00:00
|
|
|
//===- CRSBuilder.h - ConstantRangesSet Builder -----------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// @file
|
|
|
|
/// CRSBuilder allows to build and parse ConstantRangesSet objects.
|
|
|
|
/// There is such features like add/remove range, or combine
|
|
|
|
/// Two ConstantRangesSet object with neighboring ranges merging.
|
|
|
|
/// Set IsReadonly=true if you want to operate with "const ConstantInt" and
|
|
|
|
/// "const ConstantRangesSet" objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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-05-28 12:39:09 +00:00
|
|
|
template <class SuccessorClass>
|
2012-05-29 12:26:47 +00:00
|
|
|
class IntegersSubsetMapping {
|
2012-04-24 18:31:10 +00:00
|
|
|
public:
|
|
|
|
|
2012-05-29 12:26:47 +00:00
|
|
|
typedef IntegersSubset::Range RangeTy;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
struct RangeEx : public RangeTy {
|
2012-05-29 12:26:47 +00:00
|
|
|
typedef IntegersSubset::Range RangeTy;
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeEx() : Weight(1) {}
|
|
|
|
RangeEx(const RangeTy &R) : RangeTy(R.Low, R.High), Weight(1) {}
|
2012-05-28 12:39:09 +00:00
|
|
|
RangeEx(const IntItem &C) : RangeTy(C), Weight(1) {}
|
|
|
|
RangeEx(const IntItem &L, const IntItem &H) : RangeTy(L, H), Weight(1) {}
|
|
|
|
RangeEx(const IntItem &L, const IntItem &H, unsigned W) :
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeTy(L, H), Weight(W) {}
|
|
|
|
unsigned Weight;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<RangeEx, SuccessorClass*> Cluster;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
typedef std::vector<Cluster> CaseItems;
|
|
|
|
typedef typename CaseItems::iterator CaseItemIt;
|
|
|
|
typedef typename CaseItems::const_iterator CaseItemConstIt;
|
2012-05-29 12:26:47 +00:00
|
|
|
|
|
|
|
typedef std::list<RangeTy> RangesCollection;
|
|
|
|
typedef typename RangesCollection::iterator RangesCollectionIt;
|
|
|
|
|
|
|
|
typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
|
|
|
|
typedef typename CRSMap::iterator CRSMapIt;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
struct ClustersCmp {
|
|
|
|
bool operator()(const Cluster &C1, const Cluster &C2) {
|
|
|
|
return C1.first < C2.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CaseItems Items;
|
|
|
|
bool Sorted;
|
|
|
|
|
|
|
|
bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
|
2012-06-01 10:06:14 +00:00
|
|
|
return LItem->first.High >= RItem->first.Low;
|
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-05-28 12:39:09 +00:00
|
|
|
APInt RLow = RItem->first.Low;
|
2012-04-24 18:31:10 +00:00
|
|
|
if (RLow != APInt::getNullValue(RLow.getBitWidth()))
|
|
|
|
--RLow;
|
2012-06-01 10:06:14 +00:00
|
|
|
return LItem->first.High >= RLow;
|
2012-04-24 18:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sort() {
|
|
|
|
if (!Sorted) {
|
|
|
|
std::sort(Items.begin(), Items.end(), ClustersCmp());
|
|
|
|
Sorted = true;
|
|
|
|
}
|
|
|
|
}
|
2012-05-29 12:26:47 +00:00
|
|
|
|
|
|
|
IntegersSubset getCase(RangesCollection& Src) {
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
Elts.reserve(Src.size());
|
|
|
|
for (RangesCollectionIt i = Src.begin(), e = Src.end(); i != e; ++i) {
|
|
|
|
RangeTy &R = *i;
|
|
|
|
std::vector<Constant*> r;
|
|
|
|
if (R.isSingleNumber()) {
|
|
|
|
r.reserve(2);
|
|
|
|
// FIXME: Since currently we have ConstantInt based numbers
|
|
|
|
// use hack-conversion of IntItem to ConstantInt
|
|
|
|
r.push_back(R.Low.toConstantInt());
|
|
|
|
r.push_back(R.High.toConstantInt());
|
|
|
|
} else {
|
|
|
|
r.reserve(1);
|
|
|
|
r.push_back(R.Low.toConstantInt());
|
|
|
|
}
|
|
|
|
Constant *CV = ConstantVector::get(r);
|
|
|
|
Elts.push_back(CV);
|
|
|
|
}
|
|
|
|
ArrayType *ArrTy =
|
|
|
|
ArrayType::get(Elts.front()->getType(), (uint64_t)Elts.size());
|
|
|
|
Constant *Array = ConstantArray::get(ArrTy, Elts);
|
|
|
|
return IntegersSubset(Array);
|
|
|
|
}
|
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-05-29 12:26:47 +00:00
|
|
|
typedef std::pair<SuccessorClass*, IntegersSubset> Case;
|
|
|
|
typedef std::list<Case> Cases;
|
|
|
|
|
|
|
|
IntegersSubsetMapping() {
|
2012-04-24 18:31:10 +00:00
|
|
|
Items.reserve(32);
|
|
|
|
Sorted = false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
sort();
|
|
|
|
for (CaseItemIt i = Items.begin(), j = i+1, e = Items.end();
|
|
|
|
j != e; i = j++) {
|
2012-05-21 10:44:40 +00:00
|
|
|
if (isIntersected(j, i) && j->second != i->second) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void optimize() {
|
|
|
|
if (Items.size() < 2)
|
|
|
|
return;
|
|
|
|
sort();
|
|
|
|
CaseItems OldItems = Items;
|
|
|
|
Items.clear();
|
2012-05-28 12:39:09 +00:00
|
|
|
IntItem *Low = &OldItems.begin()->first.Low;
|
|
|
|
IntItem *High = &OldItems.begin()->first.High;
|
2012-04-24 18:31:10 +00:00
|
|
|
unsigned Weight = 1;
|
|
|
|
SuccessorClass *Successor = OldItems.begin()->second;
|
|
|
|
for (CaseItemIt i = OldItems.begin(), j = i+1, e = OldItems.end();
|
|
|
|
j != e; i = j++) {
|
|
|
|
if (isJoinable(i, j)) {
|
2012-05-28 12:39:09 +00:00
|
|
|
IntItem *CurHigh = &j->first.High;
|
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-05-28 12:39:09 +00:00
|
|
|
Low = &j->first.Low;
|
|
|
|
High = &j->first.High;
|
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);
|
|
|
|
// We recollected the Items, but we kept it sorted.
|
|
|
|
Sorted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a constant value.
|
2012-05-28 12:39:09 +00:00
|
|
|
void add(const IntItem &C, SuccessorClass *S = 0) {
|
2012-04-24 18:31:10 +00:00
|
|
|
RangeTy R(C);
|
|
|
|
add(R, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a range.
|
2012-05-28 12:39:09 +00:00
|
|
|
void add(const IntItem &Low, const IntItem &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-04-24 18:31:10 +00:00
|
|
|
Items.push_back(std::make_pair(R, S));
|
|
|
|
Sorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds all ranges and values from given ranges set to the current
|
|
|
|
/// CRSBuilder object.
|
2012-05-29 12:26:47 +00:00
|
|
|
void add(const IntegersSubset &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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes items from set.
|
|
|
|
void removeItem(RangeIterator i) { Items.erase(i); }
|
|
|
|
|
|
|
|
/// Builds the finalized case objects.
|
|
|
|
void getCases(Cases& TheCases) {
|
|
|
|
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)
|
|
|
|
TheCases.push_back(std::make_pair(i->first, getCase(i->second)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the finalized case objects ignoring successor values, as though
|
|
|
|
/// all ranges belongs to the same successor.
|
2012-05-29 12:26:47 +00:00
|
|
|
IntegersSubset 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);
|
|
|
|
return getCase(Ranges);
|
2012-05-29 12:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if there is no ranges and values inside.
|
|
|
|
bool empty() const { return Items.empty(); }
|
|
|
|
|
|
|
|
RangeIterator begin() { return Items.begin(); }
|
|
|
|
RangeIterator end() { return Items.end(); }
|
2012-04-24 18:31:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BasicBlock;
|
2012-05-29 12:26:47 +00:00
|
|
|
typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
|
2012-04-24 18:31:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CRSBUILDER_H_ */
|