ConstantRangesSet renamed to IntegersSubset. CRSBuilder renamed to IntegersSubsetMapping.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157612 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Stepan Dyatkovskiy 2012-05-29 12:26:47 +00:00
parent b34d3aa35b
commit 0aa32d5d0f
13 changed files with 95 additions and 109 deletions

View File

@ -20,8 +20,8 @@
#include "llvm/DerivedTypes.h"
#include "llvm/Attributes.h"
#include "llvm/CallingConv.h"
#include "llvm/Support/ConstantRangesSet.h"
#include "llvm/Support/CRSBuilder.h"
#include "llvm/Support/IntegersSubset.h"
#include "llvm/Support/IntegersSubsetMapping.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ErrorHandling.h"
@ -2589,7 +2589,7 @@ public:
/// Note:
/// This action invalidates case_end(). Old case_end() iterator will
/// point to the added case.
void addCase(ConstantRangesSet& OnVal, BasicBlock *Dest);
void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
/// removeCase - This method removes the specified case and its successor
/// from the switch instruction. Note that this operation may reorder the
@ -2654,9 +2654,9 @@ public:
/// @Deprecated
ConstantIntTy *getCaseValue() {
assert(Index < SI->getNumCases() && "Index out the number of cases.");
ConstantRangesSet CRS =
IntegersSubset CaseRanges =
reinterpret_cast<Constant*>(SI->getOperand(2 + Index*2));
ConstantRangesSet::Range R = CRS.getItem(0);
IntegersSubset::Range R = CaseRanges.getItem(0);
// FIXME: Currently we work with ConstantInt based cases.
// So return CaseValue as ConstantInt.
@ -2664,7 +2664,7 @@ public:
}
/// Resolves case value for current case.
ConstantRangesSet getCaseValueEx() {
IntegersSubset getCaseValueEx() {
assert(Index < SI->getNumCases() && "Index out the number of cases.");
return reinterpret_cast<Constant*>(SI->getOperand(2 + Index*2));
}
@ -2736,16 +2736,16 @@ public:
/// @Deprecated.
void setValue(ConstantInt *V) {
assert(Index < SI->getNumCases() && "Index out the number of cases.");
CRSBuilder CB;
IntegersSubsetToBB Mapping;
// FIXME: Currently we work with ConstantInt based cases.
// So inititalize IntItem container directly from ConstantInt.
CB.add(IntItem::fromConstantInt(V));
Mapping.add(IntItem::fromConstantInt(V));
SI->setOperand(2 + Index*2,
reinterpret_cast<Value*>((Constant*)CB.getCase()));
reinterpret_cast<Value*>((Constant*)Mapping.getCase()));
}
/// Sets the new value for current case.
void setValueEx(ConstantRangesSet& V) {
void setValueEx(IntegersSubset& V) {
assert(Index < SI->getNumCases() && "Index out the number of cases.");
SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
}

View File

@ -223,14 +223,14 @@ struct IntRange {
/// Note: It is assumed that "holder" is inherited from Constant object.
/// ConstantRangesSet may be converted to and from Constant* pointer.
///
class ConstantRangesSet {
class IntegersSubset {
Constant *Array;
public:
bool IsWide;
// implicit
ConstantRangesSet(Constant *V) : Array(V) {
IntegersSubset(Constant *V) : Array(V) {
ArrayType *ArrTy = cast<ArrayType>(Array->getType());
VectorType *VecTy = cast<VectorType>(ArrTy->getElementType());
IntegerType *IntTy = cast<IntegerType>(VecTy->getElementType());

View File

@ -19,7 +19,7 @@
#ifndef CRSBUILDER_H_
#define CRSBUILDER_H_
#include "llvm/Support/ConstantRangesSet.h"
#include "llvm/Support/IntegersSubset.h"
#include <list>
#include <map>
#include <vector>
@ -27,13 +27,13 @@
namespace llvm {
template <class SuccessorClass>
class CRSBuilderBase {
class IntegersSubsetMapping {
public:
typedef ConstantRangesSet::Range RangeTy;
typedef IntegersSubset::Range RangeTy;
struct RangeEx : public RangeTy {
typedef ConstantRangesSet::Range RangeTy;
typedef IntegersSubset::Range RangeTy;
RangeEx() : Weight(1) {}
RangeEx(const RangeTy &R) : RangeTy(R.Low, R.High), Weight(1) {}
RangeEx(const IntItem &C) : RangeTy(C), Weight(1) {}
@ -50,6 +50,12 @@ protected:
typedef std::vector<Cluster> CaseItems;
typedef typename CaseItems::iterator CaseItemIt;
typedef typename CaseItems::const_iterator CaseItemConstIt;
typedef std::list<RangeTy> RangesCollection;
typedef typename RangesCollection::iterator RangesCollectionIt;
typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
typedef typename CRSMap::iterator CRSMapIt;
struct ClustersCmp {
bool operator()(const Cluster &C1, const Cluster &C2) {
@ -82,6 +88,31 @@ protected:
Sorted = true;
}
}
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);
}
public:
@ -91,7 +122,10 @@ public:
// factory.
typedef CaseItemIt RangeIterator;
CRSBuilderBase() {
typedef std::pair<SuccessorClass*, IntegersSubset> Case;
typedef std::list<Case> Cases;
IntegersSubsetMapping() {
Items.reserve(32);
Sorted = false;
}
@ -164,7 +198,7 @@ public:
/// Adds all ranges and values from given ranges set to the current
/// CRSBuilder object.
void add(const ConstantRangesSet &CRS, SuccessorClass *S = 0) {
void add(const IntegersSubset &CRS, SuccessorClass *S = 0) {
for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
RangeTy R = CRS.getItem(i);
add(R, S);
@ -174,59 +208,6 @@ public:
/// 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> {
public:
typedef typename CRSBuilderBase<SuccessorClass>::RangeTy RangeTy;
typedef typename CRSBuilderBase<SuccessorClass>::RangeIterator
RangeIterator;
private:
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) {
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 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;
@ -238,17 +219,22 @@ public:
/// Builds the finalized case objects ignoring successor values, as though
/// all ranges belongs to the same successor.
ConstantRangesSet getCase() {
IntegersSubset getCase() {
RangesCollection Ranges;
for (RangeIterator i = this->begin(); i != this->end(); ++i)
Ranges.push_back(i->first);
return getCase(Ranges);
}
}
/// 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(); }
};
class BasicBlock;
typedef CRSBuilderT<BasicBlock> CRSBuilder;
typedef CRSBuilderBase<BasicBlock> CRSBuilderConst;
typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
}

View File

@ -2234,7 +2234,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
unsigned CurIdx = 5;
for (unsigned i = 0; i != NumCases; ++i) {
CRSBuilder CaseBuilder;
IntegersSubsetToBB CaseBuilder;
unsigned NumItems = Record[CurIdx++];
for (unsigned ci = 0; ci != NumItems; ++ci) {
bool isSingleNumber = Record[CurIdx++];
@ -2262,7 +2262,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
CaseBuilder.add(IntItem::fromType(OpTy, Low));
}
BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
ConstantRangesSet Case = CaseBuilder.getCase();
IntegersSubset Case = CaseBuilder.getCase();
SI->addCase(Case, DestBB);
}
uint16_t Hash = SI->hash();

View File

@ -1157,12 +1157,12 @@ static void WriteInstruction(const Instruction &I, unsigned InstID,
Vals64.push_back(SI.getNumCases());
for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
i != e; ++i) {
ConstantRangesSet CRS = i.getCaseValueEx();
Vals64.push_back(CRS.getNumItems());
for (unsigned ri = 0, rn = CRS.getNumItems(); ri != rn; ++ri) {
ConstantRangesSet::Range r = CRS.getItem(ri);
IntegersSubset CaseRanges = i.getCaseValueEx();
Vals64.push_back(CaseRanges.getNumItems());
for (unsigned ri = 0, rn = CaseRanges.getNumItems(); ri != rn; ++ri) {
IntegersSubset::Range r = CaseRanges.getItem(ri);
Vals64.push_back(CRS.isSingleNumber(ri));
Vals64.push_back(CaseRanges.isSingleNumber(ri));
const APInt &Low = r.Low;
const APInt &High = r.High;

View File

@ -51,7 +51,7 @@
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/CRSBuilder.h"
#include "llvm/Support/IntegersSubsetMapping.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
@ -2427,7 +2427,7 @@ size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
/// Use a shorter form of declaration, and also
/// show the we want to use CRSBuilder as Clusterifier.
typedef CRSBuilderBase<MachineBasicBlock> Clusterifier;
typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Clusterifier TheClusterifier;

View File

@ -651,9 +651,9 @@ void Interpreter::visitSwitchInst(SwitchInst &I) {
// Check to see if any of the cases match...
BasicBlock *Dest = 0;
for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
ConstantRangesSet Case = i.getCaseValueEx();
IntegersSubset Case = i.getCaseValueEx();
for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
ConstantRangesSet::Range r = Case.getItem(n);
IntegersSubset::Range r = Case.getItem(n);
// FIXME: Currently work with ConstantInt based numbers.
const ConstantInt *LowCI = r.Low.getImplementation();
const ConstantInt *HighCI = r.High.getImplementation();

View File

@ -1105,7 +1105,7 @@ void CppWriter::printInstruction(const Instruction *I,
nl(Out);
for (SwitchInst::ConstCaseIt i = SI->case_begin(), e = SI->case_end();
i != e; ++i) {
const ConstantRangesSet CaseVal = i.getCaseValueEx();
const IntegersSubset CaseVal = i.getCaseValueEx();
const BasicBlock *BB = i.getCaseSuccessor();
Out << iName << "->addCase("
<< getOpName(CaseVal) << ", "

View File

@ -169,11 +169,11 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions) {
// Otherwise, we can fold this switch into a conditional branch
// instruction if it has only one non-default destination.
SwitchInst::CaseIt FirstCase = SI->case_begin();
ConstantRangesSet CRS = FirstCase.getCaseValueEx();
if (CRS.getNumItems() == 1 && CRS.isSingleNumber(0)) {
IntegersSubset CaseRanges = FirstCase.getCaseValueEx();
if (CaseRanges.getNumItems() == 1 && CaseRanges.isSingleNumber(0)) {
// FIXME: Currently work with ConstantInt based numbers.
Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
CRS.getItem(0).Low.toConstantInt(),
CaseRanges.getItem(0).Low.toConstantInt(),
"cond");
// Insert the new branch.

View File

@ -223,22 +223,22 @@ BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
// Clusterify - Transform simple list of Cases into list of CaseRange's
unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
CRSBuilder TheClusterifier;
IntegersSubsetToBB TheClusterifier;
// Start with "simple" cases
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
i != e; ++i) {
BasicBlock *SuccBB = i.getCaseSuccessor();
ConstantRangesSet CRS = i.getCaseValueEx();
TheClusterifier.add(CRS, SuccBB);
IntegersSubset CaseRanges = i.getCaseValueEx();
TheClusterifier.add(CaseRanges, SuccBB);
}
TheClusterifier.optimize();
size_t numCmps = 0;
for (CRSBuilder::RangeIterator i = TheClusterifier.begin(),
for (IntegersSubsetToBB::RangeIterator i = TheClusterifier.begin(),
e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
CRSBuilder::Cluster &C = *i;
IntegersSubsetToBB::Cluster &C = *i;
// FIXME: Currently work with ConstantInt based numbers.
// Changing it to APInt based is a pretty heavy for this commit.

View File

@ -3169,16 +3169,16 @@ SwitchInst::~SwitchInst() {
/// addCase - Add an entry to the switch instruction...
///
void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
CRSBuilder CB;
IntegersSubsetToBB Mapping;
// FIXME: Currently we work with ConstantInt based cases.
// So inititalize IntItem container directly from ConstantInt.
CB.add(IntItem::fromConstantInt(OnVal));
ConstantRangesSet CRS = CB.getCase();
addCase(CRS, Dest);
Mapping.add(IntItem::fromConstantInt(OnVal));
IntegersSubset CaseRanges = Mapping.getCase();
addCase(CaseRanges, Dest);
}
void SwitchInst::addCase(ConstantRangesSet& OnVal, BasicBlock *Dest) {
void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
unsigned NewCaseIdx = getNumCases();
unsigned OpNo = NumOperands;
if (OpNo+2 > ReservedSpace)

View File

@ -806,23 +806,23 @@ void Verifier::visitSwitchInst(SwitchInst &SI) {
// have the same type as the switched-on value.
Type *SwitchTy = SI.getCondition()->getType();
IntegerType *IntTy = cast<IntegerType>(SwitchTy);
CRSBuilder Builder;
std::map<ConstantRangesSet::Range, unsigned> RangeSetMap;
IntegersSubsetToBB Mapping;
std::map<IntegersSubset::Range, unsigned> RangeSetMap;
for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
ConstantRangesSet RS = i.getCaseValueEx();
for (unsigned ri = 0, rie = RS.getNumItems(); ri < rie; ++ri) {
ConstantRangesSet::Range r = RS.getItem(ri);
IntegersSubset CaseRanges = i.getCaseValueEx();
for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) {
IntegersSubset::Range r = CaseRanges.getItem(ri);
Assert1(r.Low->getBitWidth() == IntTy->getBitWidth(),
"Switch constants must all be same type as switch value!", &SI);
Assert1(r.High->getBitWidth() == IntTy->getBitWidth(),
"Switch constants must all be same type as switch value!", &SI);
Builder.add(r);
Mapping.add(r);
RangeSetMap[r] = i.getCaseIndex();
}
}
CRSBuilder::RangeIterator errItem;
if (!Builder.verify(errItem)) {
IntegersSubsetToBB::RangeIterator errItem;
if (!Mapping.verify(errItem)) {
unsigned CaseIndex = RangeSetMap[errItem->first];
SwitchInst::CaseIt i(&SI, CaseIndex);
Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx());

View File

@ -326,7 +326,7 @@ class FunctionDifferenceEngine {
for (SwitchInst::CaseIt I = RI->case_begin(), E = RI->case_end();
I != E; ++I) {
ConstantRangesSet CaseValue = I.getCaseValueEx();
IntegersSubset CaseValue = I.getCaseValueEx();
BasicBlock *LCase = LCases[CaseValue];
if (LCase) {
if (TryUnify) tryUnify(LCase, I.getCaseSuccessor());