Factor out a common base class between SCEVCommutativeExpr and
authorDan Gohman <gohman@apple.com>
Thu, 7 May 2009 14:00:19 +0000 (14:00 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 7 May 2009 14:00:19 +0000 (14:00 +0000)
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

include/llvm/Analysis/ScalarEvolutionExpressions.h
lib/Analysis/ScalarEvolution.cpp

index 00cc40eb3173af4029841698b3ee494a02f99684..96b24bf7a0210a5abb94bc4ffd1a91368e144635 100644 (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:
index 50cece03657565300f48be20805d24ae4201462d..e00d1d9f80650b6b746886655dff3d31cdb5cc42 100644 (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,