llvm-6502/include/llvm/Support/CRSBuilder.h

253 lines
7.4 KiB
C
Raw Normal View History

Related to PR1255. Let's begin. I'll commit classes that corresponds to our latest PR1255 discussion posts in llvm-commits. Strategy. 0. Implement new classes. Classes doesn't affect anything. They still work with ConstantInt base values at this stage. 1. Fictitious replacement of current ConstantInt case values with ConstantRangesSet. Case ranges set will still hold single value, and ConstantInt *getCaseValue() will return it. But additionally implement new method in SwitchInst that allows to work with case ranges. Currenly I think it should be some wrapper that returns either single value or ConstantRangesSet object. 2. Step-by-step replacement of old "ConstantInt* getCaseValue()" with new alternative. Modify algorithms for all passes that works with SwitchInst. But don't modify LLParser and BitcodeReader/Writer. Still hold single value in each ConstantRangesSet object. On this stage some parts of LLVM will use old-style methods, and some ones new-style. 3. After all getCaseValue() usages will removed and whole LLVM and its clients will work in new style - modify LLParser, Reader and Writer. Remove getCaseValue(). 4. Replace ConstantInt*-based case ranges set items with APInt ones. Currently we are on Zero Stage: New classes. ConstantRangesSet. I selected ConstantArrays as case ranges set "holder" object (it is a temporary decision, I'll explain why below). The array items are may be ConstantVectors with single item, and ConstantVectors with two items (that means single number and range respectively). The ConstantInt will used as basic value representation. It will replaced with APInt then. Of course ConstantArray and ConstantVector will go away after ConstantInt => APInt replacement. New class mandatory features: - bool isSatisfies(ConstantInt *V) method (need better name?). Returns true if the given value satisfies this case. - Case's ranges and values enumeration. In some passes we need to analize each case (SwitchLowering for example). Factory + unified clusterify. I also propose to implement the factory that allows to build case object with user friendly way. I called it CRSBuilder by now. Currenly I implemented the factory that allows add,remove pairs of range+successor. It also allows add existing ConstantRangesSet decompiling it to separated ranges. Factory can emit either clusters set (single case range + successor) or the set of "ConstantRangesSet + Successor" pairs. So you can use it either as builder for new cases set for SwitchInst, or for clusterification of existing cases set. Just call Factory.optimize() and it emits optimized and sorted clusters collection for you! I tested clusterification on SelectionDAGBuilder - it works fine. Don't worry it was not included in this patch. Just new classes. Factory is a template. There are two params: SuccessorClass and IsReadonly. So you can specify what successor you need (BB or MBB). And you can also restrict your factory to use values in read-only mode (SelectionDAGBuilder need IsReadonly=true). Read-only factory couldn't build the cases ranges. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155464 91177308-0d34-0410-b5e6-96231b3b80d8
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_
#include "llvm/Support/ConstantRangesSet.h"
Related to PR1255. Let's begin. I'll commit classes that corresponds to our latest PR1255 discussion posts in llvm-commits. Strategy. 0. Implement new classes. Classes doesn't affect anything. They still work with ConstantInt base values at this stage. 1. Fictitious replacement of current ConstantInt case values with ConstantRangesSet. Case ranges set will still hold single value, and ConstantInt *getCaseValue() will return it. But additionally implement new method in SwitchInst that allows to work with case ranges. Currenly I think it should be some wrapper that returns either single value or ConstantRangesSet object. 2. Step-by-step replacement of old "ConstantInt* getCaseValue()" with new alternative. Modify algorithms for all passes that works with SwitchInst. But don't modify LLParser and BitcodeReader/Writer. Still hold single value in each ConstantRangesSet object. On this stage some parts of LLVM will use old-style methods, and some ones new-style. 3. After all getCaseValue() usages will removed and whole LLVM and its clients will work in new style - modify LLParser, Reader and Writer. Remove getCaseValue(). 4. Replace ConstantInt*-based case ranges set items with APInt ones. Currently we are on Zero Stage: New classes. ConstantRangesSet. I selected ConstantArrays as case ranges set "holder" object (it is a temporary decision, I'll explain why below). The array items are may be ConstantVectors with single item, and ConstantVectors with two items (that means single number and range respectively). The ConstantInt will used as basic value representation. It will replaced with APInt then. Of course ConstantArray and ConstantVector will go away after ConstantInt => APInt replacement. New class mandatory features: - bool isSatisfies(ConstantInt *V) method (need better name?). Returns true if the given value satisfies this case. - Case's ranges and values enumeration. In some passes we need to analize each case (SwitchLowering for example). Factory + unified clusterify. I also propose to implement the factory that allows to build case object with user friendly way. I called it CRSBuilder by now. Currenly I implemented the factory that allows add,remove pairs of range+successor. It also allows add existing ConstantRangesSet decompiling it to separated ranges. Factory can emit either clusters set (single case range + successor) or the set of "ConstantRangesSet + Successor" pairs. So you can use it either as builder for new cases set for SwitchInst, or for clusterification of existing cases set. Just call Factory.optimize() and it emits optimized and sorted clusters collection for you! I tested clusterification on SelectionDAGBuilder - it works fine. Don't worry it was not included in this patch. Just new classes. Factory is a template. There are two params: SuccessorClass and IsReadonly. So you can specify what successor you need (BB or MBB). And you can also restrict your factory to use values in read-only mode (SelectionDAGBuilder need IsReadonly=true). Read-only factory couldn't build the cases ranges. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155464 91177308-0d34-0410-b5e6-96231b3b80d8
2012-04-24 18:31:10 +00:00
#include <list>
#include <map>
#include <vector>
namespace llvm {
template <class SuccessorClass, bool IsReadonly>
class CRSBuilderBase {
public:
typedef ConstantRangesSet::RangeT<IsReadonly> RangeTy;
struct RangeEx : public RangeTy {
typedef ConstantRangesSet::RangeT<IsReadonly> RangeTy;
typedef typename RangeTy::ConstantIntTy ConstantIntTy;
RangeEx() : Weight(1) {}
RangeEx(const RangeTy &R) : RangeTy(R.Low, R.High), Weight(1) {}
RangeEx(ConstantIntTy *C) : RangeTy(C), Weight(1) {}
RangeEx(ConstantIntTy *L, ConstantIntTy *H) : RangeTy(L, H), Weight(1) {}
RangeEx(ConstantIntTy *L, ConstantIntTy *H, unsigned W) :
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;
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) {
return LItem->first.High->getValue().uge(RItem->first.Low->getValue());
}
bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
if (LItem->second != RItem->second) {
assert(!isIntersected(LItem, RItem) &&
"Intersected items with different successors!");
return false;
}
APInt RLow = RItem->first.Low->getValue();
if (RLow != APInt::getNullValue(RLow.getBitWidth()))
--RLow;
return LItem->first.High->getValue().uge(RLow);
}
void sort() {
if (!Sorted) {
std::sort(Items.begin(), Items.end(), ClustersCmp());
Sorted = true;
}
}
public:
typedef typename CRSConstantTypes<IsReadonly>::ConstantIntTy ConstantIntTy;
typedef typename CRSConstantTypes<IsReadonly>::ConstantRangesSetTy ConstantRangesSetTy;
// 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;
CRSBuilderBase() {
Items.reserve(32);
Sorted = false;
}
bool verify() {
if (Items.empty())
return true;
sort();
for (CaseItemIt i = Items.begin(), j = i+1, e = Items.end();
j != e; i = j++) {
if (isIntersected(j, i) && j->second != i->second)
return false;
}
return true;
}
void optimize() {
if (Items.size() < 2)
return;
sort();
CaseItems OldItems = Items;
Items.clear();
ConstantIntTy *Low = OldItems.begin()->first.Low;
ConstantIntTy *High = OldItems.begin()->first.High;
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)) {
ConstantIntTy *CurHigh = j->first.High;
++Weight;
if (CurHigh->getValue().ugt(High->getValue()))
High = CurHigh;
} else {
RangeEx R(Low, High, Weight);
add(R, Successor);
Low = j->first.Low;
High = j->first.High;
Weight = 1;
Successor = j->second;
}
}
RangeEx R(Low, High, Weight);
add(R, Successor);
// We recollected the Items, but we kept it sorted.
Sorted = true;
}
/// Adds a constant value.
void add(ConstantIntTy *C, SuccessorClass *S = 0) {
RangeTy R(C);
add(R, S);
}
/// Adds a range.
void add(ConstantIntTy *Low, ConstantIntTy *High, SuccessorClass *S = 0) {
RangeTy R(Low, High);
add(R, S);
}
void add(RangeTy &R, SuccessorClass *S = 0) {
RangeEx REx = R;
add(REx, S);
}
void add(RangeEx &R, SuccessorClass *S = 0) {
Items.push_back(std::make_pair(R, S));
Sorted = false;
}
/// Adds all ranges and values from given ranges set to the current
/// CRSBuilder object.
void add(ConstantRangesSetTy &CRS, SuccessorClass *S = 0) {
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); }
/// 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(); }
};
template <class SuccessorClass>
class CRSBuilderT : public CRSBuilderBase<SuccessorClass, false> {
typedef typename CRSBuilderBase<SuccessorClass, false>::RangeTy RangeTy;
typedef typename CRSBuilderBase<SuccessorClass, false>::RangeIterator
RangeIterator;
typedef std::list<RangeTy> RangesCollection;
typedef typename RangesCollection::iterator RangesCollectionIt;
typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
typedef typename CRSMap::iterator CRSMapIt;
ConstantRangesSet getCase(RangesCollection& Src) {
std::vector<Constant*> Elts;
Elts.reserve(Src.size());
for (RangesCollectionIt i = Src.begin(), e = Src.end(); i != e; ++i) {
const RangeTy &R = *i;
std::vector<Constant*> r;
if (R.Low != R.High) {
r.reserve(2);
r.push_back(R.Low);
r.push_back(R.High);
} else {
r.reserve(1);
r.push_back(R.Low);
}
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 ConstantRangesSet(Array);
}
public:
typedef std::pair<SuccessorClass*, ConstantRangesSet> Case;
typedef std::list<Case> Cases;
/// 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.
ConstantRangesSet getCase() {
RangesCollection Ranges;
for (RangeIterator i = this->begin(); i != this->end(); ++i)
Ranges.push_back(i->first);
return getCase(Ranges);
}
};
class BasicBlock;
typedef CRSBuilderT<BasicBlock> CRSBuilder;
typedef CRSBuilderBase<BasicBlock, true> CRSBuilderConst;
}
#endif /* CRSBUILDER_H_ */