Factor out a common base class between SCEVCommutativeExpr and

SCEVAddRecExpr. This eliminates redundant code for visiting
all the operands of an expression.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71157 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-05-07 14:00:19 +00:00
parent cd76240f3d
commit ecb403a9d3
2 changed files with 35 additions and 40 deletions

View File

@ -194,20 +194,13 @@ namespace llvm {
};
//===--------------------------------------------------------------------===//
/// SCEVCommutativeExpr - This node is the base class for n'ary commutative
/// operators.
///
class SCEVCommutativeExpr : public SCEV {
class SCEVNAryExpr : public SCEV {
protected:
std::vector<SCEVHandle> Operands;
protected:
SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
: SCEV(T) {
Operands.reserve(ops.size());
Operands.insert(Operands.end(), ops.begin(), ops.end());
}
~SCEVCommutativeExpr();
SCEVNAryExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
: SCEV(T), Operands(ops) {}
virtual ~SCEVNAryExpr() {}
public:
unsigned getNumOperands() const { return (unsigned)Operands.size(); }
@ -221,7 +214,6 @@ namespace llvm {
op_iterator op_begin() const { return Operands.begin(); }
op_iterator op_end() const { return Operands.end(); }
virtual bool isLoopInvariant(const Loop *L) const {
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
if (!getOperand(i)->isLoopInvariant(L)) return false;
@ -243,15 +235,38 @@ namespace llvm {
return HasVarying;
}
bool dominates(BasicBlock *BB, DominatorTree *DT) const;
virtual const Type *getType() const { return getOperand(0)->getType(); }
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const SCEVNAryExpr *S) { return true; }
static inline bool classof(const SCEV *S) {
return S->getSCEVType() == scAddExpr ||
S->getSCEVType() == scMulExpr ||
S->getSCEVType() == scSMaxExpr ||
S->getSCEVType() == scUMaxExpr ||
S->getSCEVType() == scAddRecExpr;
}
};
//===--------------------------------------------------------------------===//
/// SCEVCommutativeExpr - This node is the base class for n'ary commutative
/// operators.
///
class SCEVCommutativeExpr : public SCEVNAryExpr {
protected:
SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
: SCEVNAryExpr(T, ops) {}
~SCEVCommutativeExpr();
public:
SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
const SCEVHandle &Conc,
ScalarEvolution &SE) const;
bool dominates(BasicBlock *BB, DominatorTree *DT) const;
virtual const char *getOperationStr() const = 0;
virtual const Type *getType() const { return getOperand(0)->getType(); }
virtual void print(raw_ostream &OS) const;
/// Methods for support type inquiry through isa, cast, and dyn_cast:
@ -364,30 +379,23 @@ namespace llvm {
///
/// All operands of an AddRec are required to be loop invariant.
///
class SCEVAddRecExpr : public SCEV {
class SCEVAddRecExpr : public SCEVNAryExpr {
friend class ScalarEvolution;
std::vector<SCEVHandle> Operands;
const Loop *L;
SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
: SCEV(scAddRecExpr), Operands(ops), L(l) {
: SCEVNAryExpr(scAddRecExpr, ops), L(l) {
for (size_t i = 0, e = Operands.size(); i != e; ++i)
assert(Operands[i]->isLoopInvariant(l) &&
"Operands of AddRec must be loop-invariant!");
}
~SCEVAddRecExpr();
public:
typedef std::vector<SCEVHandle>::const_iterator op_iterator;
op_iterator op_begin() const { return Operands.begin(); }
op_iterator op_end() const { return Operands.end(); }
unsigned getNumOperands() const { return (unsigned)Operands.size(); }
const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
public:
const SCEVHandle &getStart() const { return Operands[0]; }
const Loop *getLoop() const { return L; }
/// getStepRecurrence - This method constructs and returns the recurrence
/// indicating how much this expression steps by. If this is a polynomial
/// of degree N, it returns a chrec of degree N-1.
@ -404,8 +412,6 @@ namespace llvm {
virtual bool isLoopInvariant(const Loop *QueryLoop) const;
virtual const Type *getType() const { return Operands[0]->getType(); }
/// isAffine - Return true if this is an affine AddRec (i.e., it represents
/// an expressions A+B*x where A and B are loop invariant values.
bool isAffine() const {
@ -438,8 +444,6 @@ namespace llvm {
const SCEVHandle &Conc,
ScalarEvolution &SE) const;
bool dominates(BasicBlock *BB, DominatorTree *DT) const;
virtual void print(raw_ostream &OS) const;
/// Methods for support type inquiry through isa, cast, and dyn_cast:

View File

@ -316,7 +316,7 @@ replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
return this;
}
bool SCEVCommutativeExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
if (!getOperand(i)->dominates(BB, DT))
return false;
@ -359,15 +359,6 @@ SCEVAddRecExpr::~SCEVAddRecExpr() {
SCEVAddRecExprs->erase(std::make_pair(L, SCEVOps));
}
bool SCEVAddRecExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
if (!getOperand(i)->dominates(BB, DT))
return false;
}
return true;
}
SCEVHandle SCEVAddRecExpr::
replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
const SCEVHandle &Conc,