ManagerAffineExpr.hpp

Go to the documentation of this file.
00001 #ifndef ManagerAffineExpr_H
00002 #define ManagerAffineExpr_H
00003 
00004 #include <OpenAnalysis/ExprTree/ExprTreeVisitor.hpp>
00005 #include <OpenAnalysis/IRInterface/AffineExprIRInterface.hpp>
00006 #include <OpenAnalysis/AffineExpr/AffineExprAbstraction.hpp>
00007 #include <OpenAnalysis/Alias/Interface.hpp>
00008 #include <stack>
00009 using namespace std;
00010 
00011 namespace OA {
00012 namespace AffineExpr {
00013 
00016 enum AffineAnlState {
00017     INVALID_OPERATOR = 0,       // ex:  using '/', '%', or '-'
00018     NON_LINEAR_TERM,            // ex:  i * i or i * j or a function call
00019     INVALID_VAR,                // ex:  3 * j when j is not a global param.
00020     INDIRECT_REF,               // ex:  A[i]
00021     VALID_AFFINE_EXP            // ex:  3 * i + 4
00022 };
00023 
00024 // used by the affine expression visitor class when attempting to constructing
00025 // an affine expression.  Values and variables are pushed onto a stack and
00026 // popped off when operators are read in.
00027 struct StackObj {
00028     bool isVar;
00029 
00030     // these two can't be put into a union because the OA_ptr object
00031     // requires construction
00032     int val;
00033     OA_ptr<NamedLoc> var;
00034 };
00035 
00036 
00040 class AffineExprExprTreeVisitor : public ExprTreeVisitor {
00041   public:
00042     AffineExprExprTreeVisitor(
00043         OA_ptr<AffineExprAbstraction> afExp,
00044         OA_ptr<AffineExprIRInterface> ir,
00045         ProcHandle hProc,
00046         const set<OA_ptr<NamedLoc> > & indexVars,
00047         const set<OA_ptr<NamedLoc> > & nonConstVars,
00048         OA_ptr<Alias::Interface> aliasResults)
00049           :
00050         mAfExp(afExp),
00051         mIR(ir),
00052         mhProc(hProc),
00053         mIndexVars(indexVars),
00054         mNonConstVars(nonConstVars),
00055         mAliasResults(aliasResults),
00056         mState(VALID_AFFINE_EXP)
00057     { }
00058 
00059     virtual ~AffineExprExprTreeVisitor() {}
00060 
00061     virtual void visitExprTreeBefore(ExprTree&) { }
00062     virtual void visitExprTreeAfter(ExprTree&) { }
00063     virtual void visitNode(ExprTree::Node& n) { }
00064 
00065     virtual void visitOpNode(ExprTree::OpNode& n);
00066     virtual void visitCallNode(ExprTree::CallNode& n);
00067     virtual void visitMemRefNode(ExprTree::MemRefNode& n);
00068     virtual void visitConstSymNode(ExprTree::ConstSymNode& n);
00069     virtual void visitConstValNode(ExprTree::ConstValNode& n);
00070 
00071     AffineAnlState getState() { return mState; }
00072 
00073   private:
00074     void pushVar(OA_ptr<NamedLoc> var);
00075     void pushScaler(int val);
00076     void factor();
00077     void term();
00078 
00079     stack<StackObj> mStack;
00080 
00081     OA_ptr<AffineExprAbstraction> mAfExp;
00082     OA_ptr<AffineExprIRInterface> mIR;
00083     ProcHandle mhProc;
00084     const set<OA_ptr<NamedLoc> > & mIndexVars;
00085     const set<OA_ptr<NamedLoc> > & mNonConstVars;
00086     OA_ptr<Alias::Interface> mAliasResults;
00087     AffineAnlState mState;
00088 };
00089 
00090 
00091 
00094 class ManagerAffineExpr {
00095   public:
00097     ManagerAffineExpr(
00098         OA_ptr<AffineExprIRInterface> _ir);
00099         
00102     OA_ptr<AffineExprAbstraction> analyzeMemRefExp(
00103         ExprHandle hExp,
00104         ProcHandle hProc,
00105         const set<OA_ptr<NamedLoc> > & indexVars,
00106         const set<OA_ptr<NamedLoc> > & nonConstVars,
00107         OA_ptr<Alias::Interface> aliasResults,
00108         AffineAnlState *state);
00109 
00110     static string toString(AffineAnlState state) {
00111         switch(state) {
00112             case INVALID_OPERATOR: return "INVALID_OPERATOR"; break;
00113             case NON_LINEAR_TERM:  return "NON_LINEAR_TERM";  break;
00114             case INVALID_VAR:      return "INVALID_VAR";      break;
00115             case INDIRECT_REF:     return "INDIRECT_REF";     break;
00116             case VALID_AFFINE_EXP: return "VALID_AFFINE_EXP"; break;
00117         };
00118         return "INVALID AffineAnlState!";
00119     }
00120 
00121   private:
00122     OA_ptr<AffineExprIRInterface> mIR;
00123 };
00124 
00125 } } // end namespaces
00126 
00127 #endif