From: Dan Gohman Date: Thu, 7 May 2009 14:00:19 +0000 (+0000) Subject: Factor out a common base class between SCEVCommutativeExpr and X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=ecb403a9d3a340009c266d05cfca2bd778c7b156;p=oota-llvm.git 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 --- diff --git a/include/llvm/Analysis/ScalarEvolutionExpressions.h b/include/llvm/Analysis/ScalarEvolutionExpressions.h index 00cc40eb317..96b24bf7a02 100644 --- a/include/llvm/Analysis/ScalarEvolutionExpressions.h +++ b/include/llvm/Analysis/ScalarEvolutionExpressions.h @@ -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 Operands; - protected: - SCEVCommutativeExpr(enum SCEVTypes T, const std::vector &ops) - : SCEV(T) { - Operands.reserve(ops.size()); - Operands.insert(Operands.end(), ops.begin(), ops.end()); - } - ~SCEVCommutativeExpr(); + SCEVNAryExpr(enum SCEVTypes T, const std::vector &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 &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 Operands; const Loop *L; SCEVAddRecExpr(const std::vector &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::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: diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 50cece03657..e00d1d9f806 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -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,