CalleeToCallerVisitor.cpp

Go to the documentation of this file.
00001 
00015 #include "CalleeToCallerVisitor.hpp"
00016 #include <Utils/Util.hpp>
00017 
00018 
00019 namespace OA {
00020   namespace DataFlow {
00021 
00022 static bool debug = false;
00023 
00024 
00025         
00026 CalleeToCallerVisitor::CalleeToCallerVisitor(
00027         ProcHandle callee, CallHandle call, ProcHandle caller,
00028         OA_ptr<Alias::InterAliasInterface> interAlias,
00029         OA_ptr<DataFlow::ParamBindings> paramBind,
00030         OA_ptr<CalleeToCallerVisitorIRInterface> _ir)
00031   :  mIR(_ir),mCall(call), mCaller(caller), 
00032       mInterAlias(interAlias), mParamBind(paramBind), mCallee(callee)
00033 {
00034     OA_DEBUG_CTRL_MACRO("DEBUG_CalleeToCallerVisitor:ALL", debug);
00035 
00036     mCallerAlias = mInterAlias->getAliasResults(caller);
00037 }
00038 
00039 OA_ptr<LocIterator> CalleeToCallerVisitor::getCallerLocIterator()
00040 {
00041 
00042     OA_ptr<LocSetIterator> retval;
00043     retval = new LocSetIterator(mLocSet);
00044     return retval;
00045 }
00046 
00047 
00048 void CalleeToCallerVisitor::visitNamedLoc(NamedLoc& loc) 
00049 {
00050     // should start newly empty each time
00051     mLocSet = new LocSet;
00052 
00053     // if the location is not local then just return it
00054     if (loc.isLocal()==false) {
00055         OA_ptr<Location> locCopy; locCopy = new NamedLoc(loc);
00056         mLocSet->insert(locCopy);
00057         if (debug) {
00058             std::cout << "CalleeToCallerVisitor: loc is not local, loc = ";
00059             loc.dump(std::cout);
00060             std::cout << std::endl;
00061         }
00062     
00063     }
00064         /*
00065     } else {
00066         // assume it is a parameter and try to get corresponding memref
00067         SymHandle formal = loc.getSymHandle();
00068         if (debug) {
00069             std::cout << "CalleeToCallerVisitor: formal = " << formal.hval();
00070             std::cout << std::endl;
00071         }
00072     
00073         // get the memory reference in the caller
00074         MemRefHandle memref = mParamBind->getCallerMemRef(mCall,formal);
00075     
00076         // only do the conversion if actual parameter is a memory reference
00077         if (memref!=MemRefHandle(0)) {
00078             // get all the may locs for the mem ref in caller
00079             // and insert them into the caller locs for this reference
00080             OA_ptr<LocIterator> mayLocIter = mCallerAlias->getMayLocs(memref);
00081             for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {
00082                 if (debug) {
00083                     std::cout << "\tmemref = " << memref.hval()
00084                             << " has mayLoc ";
00085                     mayLocIter->current()->dump(std::cout);
00086                     std::cout << std::endl;
00087                 }
00088                 mLocSet->insert(mayLocIter->current());
00089             }
00090         }
00091     }
00092     */
00093 }
00094 
00095 void CalleeToCallerVisitor::visitUnnamedLoc(UnnamedLoc& loc)
00096 {
00097     mLocSet = new LocSet;
00098     OA_ptr<Location> locptr;
00099     locptr = new UnnamedLoc(loc);
00100     mLocSet->insert(locptr);
00101 }
00102 
00103 
00112 class ConstructMREVisitor :  public virtual MemRefExprVisitor{
00113 
00114 public:
00115 
00116   OA_ptr<MemRefExpr> getMemRefExpr() {
00117     return mNewMRE;
00118   }
00119 
00120   ConstructMREVisitor(OA_ptr<MemRefExpr> actualMRE, 
00121                       OA_ptr<CalleeToCallerVisitorIRInterface> _ir) 
00122       : mActualMRE(actualMRE), mIR(_ir)
00123   {
00124   }
00125 
00126   void visitNamedRef(NamedRef& ref) {
00127     // should never get here because will stop at RefOp that 
00128     // points at the base NamedRef
00129   }
00130 
00131   void visitUnnamedRef(UnnamedRef& ref) {
00132     // should never get here because the base ref in an InvisibleLoc 
00133     // is always a NamedRef
00134   }
00135 
00136   void visitUnknownRef(UnknownRef& ref) {
00137     // should never get here because the base ref in an InvisibleLoc is 
00138     // always a NamedRef
00139   }
00140 
00141   void visitAddressOf(AddressOf& ref) {
00142 
00143 
00144     OA_ptr<AddressOf> refOp;
00145 
00146     refOp = new AddressOf(ref);
00147 
00148     mRefOpStack.push(refOp);
00149 
00150     // see if this is the last RefOp before the NamedRef to the formal parameter
00151     OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
00152     if (mre->isaNamed()) {
00153 
00154       // if it is then call the method that will create the newMRE
00155       createNewMRE();
00156 
00157       if (debug) {
00158         std::cout << "\t Immediately after mActualMRE = ";
00159         mActualMRE->output(*mIR);
00160       }
00161       if (debug) {
00162        std::cout << "\t Immediately after mNewMRE = ";
00163        mNewMRE->output(*mIR);
00164       }
00165 
00166     } else {
00167       mre->acceptVisitor(*this);
00168     }
00169 
00170     if (debug) {
00171           std::cout << "\t Inside (end) of visitAddressOf mNewMRE = ";
00172           mNewMRE->output(*mIR);
00173     }
00174     
00175   }
00176 
00177 
00178   void visitDeref(Deref& ref) {
00179     // push empty version of this kind of RefOp onto stack
00180     OA_ptr<MemRefExpr> nullMRE;
00181     // will need to write getMRType()
00182 
00183     /* PLM 1/23/07 deprecated accuracy field    
00184     OA_ptr<Deref> refOp;
00185 
00186     refOp = 
00187         new Deref(ref.hasFullAccuracy(), ref.getMRType(), 
00188                   nullMRE, ref.getNumDerefs()) ;
00189     */
00190 
00191     
00192     
00193     OA_ptr<Deref> refOp;
00194     
00195     refOp =
00196         new Deref(ref.getMRType(), nullMRE, ref.getNumDerefs()) ;
00197    
00198 
00199     mRefOpStack.push(refOp);
00200 
00201     // see if this is the last RefOp before the NamedRef to the formal parameter
00202     OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
00203     if (mre->isaNamed()) {
00204 
00205       // if it is then call the method that will create the newMRE
00206       createNewMRE();
00207       
00208       if (debug) {
00209         std::cout << "\t Immediately after mActualMRE = ";
00210         mActualMRE->output(*mIR);
00211       }
00212       if (debug) {
00213            std::cout << "\t Immediately after mNewMRE = ";
00214            mNewMRE->output(*mIR);
00215       }
00216       
00217     } else {
00218       mre->acceptVisitor(*this);
00219     }
00220     
00221     if (debug) {
00222           std::cout << "\t Inside visiDeref mNewMRE = ";
00223           mNewMRE->output(*mIR);
00224     }
00225   }
00226 
00227   void visitSubSetRef(SubSetRef& ref) {
00228       //assert(0);  // not implemented yet, MMS
00229       
00230        OA::OA_ptr<OA::SubSetRef> subset_mre;
00231         OA::OA_ptr<OA::MemRefExpr> nullMRE;
00232 
00233         subset_mre = new OA::SubSetRef(
00234                                    OA::MemRefExpr::USE,
00235                                    nullMRE
00236                                   );
00237 
00238         mRefOpStack.push(subset_mre);
00239 
00240         // see if this is the last RefOp before the NamedRef to the formal parameter
00241         OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
00242         if (mre->isaNamed()) {
00243             // if it is then call the method that will create the newMRE
00244             createNewMRE();
00245         } else {
00246             mre->acceptVisitor(*this);
00247         }
00248   }
00249 
00250 private:
00251 
00252   void createNewMRE()
00253   {
00254     mNewMRE = mActualMRE;
00255 
00256 
00257     // pop things on the list one at a time, 
00258     // for each RefOp, create one of the same kind but have 
00259     // it point to mNewMRE and then assign newly constructed MRE to mNewMRE
00260     while(!mRefOpStack.empty())
00261     { 
00262         OA_ptr<RefOp> refop = mRefOpStack.top(); 
00263         mRefOpStack.pop();  
00264         
00265 
00266         mNewMRE = refop->composeWith(mNewMRE);
00267 
00268         
00269             if (debug) {
00270             std::cout << "\t Inside createNewMRE mNewMRE = ";
00271                 mNewMRE->output(*mIR);
00272         }
00273         }
00274   }
00275 
00276 private:
00277   std::stack<OA_ptr<RefOp> > mRefOpStack;
00278   OA_ptr<MemRefExpr> mActualMRE;
00279   OA_ptr<MemRefExpr> mNewMRE;
00280   OA_ptr<CalleeToCallerVisitorIRInterface> mIR; 
00281 
00282 
00283 };
00284 
00285 
00286 void CalleeToCallerVisitor::visitInvisibleLoc(InvisibleLoc& loc)
00287 {
00288   
00289   if (debug) {
00290       std::cout << "In CalleeToCallerVisitor::visitInvisibleLoc" << std::endl;
00291   }
00292   mLocSet = new LocSet;
00293   
00294   // if base symbols is not local
00295   // InvisibleLoc
00296   
00297   
00298   SymHandle baseSym = loc.getBaseSym();
00299  
00300  
00301   OA_ptr<Location> baseSymLoc = mIR->getLocation(mCallee, baseSym); 
00302   if (!baseSymLoc->isLocal())   
00303   {   
00304    // pull out the MRE
00305     OA_ptr<MemRefExpr> memref = loc.getMemRefExpr();  
00306     if (debug) {
00307       std::cout << "\tBaseSymLoc is NOT local = ";
00308       memref->output(*mIR);
00309     }
00310     
00311     // ask caller alias analysis to getMayLocs
00312     // insert each of the may locs into mLocSet
00313     OA_ptr<LocIterator> mayLocIter = mCallerAlias->getMayLocs(*memref, mCaller);
00314 
00315     for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {
00316       mLocSet->insert(mayLocIter->current());
00317     }
00318 
00319   // base symbol is local
00320   // the base symbol should be a parameter
00321   } else {
00322     OA_ptr<MemRefExpr> mre = loc.getMemRefExpr();
00323     if (debug) {
00324       std::cout << "\tBaseSymLoc IS local = ";
00325       mre->output(*mIR);
00326     }
00327     
00328     // get the base symbol for the mre within the invisible location
00329     // it should be a formal
00330     OA_ptr<RefOp> refop;
00331     if (mre->isaRefOp()) {
00332         refop = mre.convert<RefOp>();
00333     } else { 
00334         assert(0);  // should not have an MRE in InvLoc that isn't a RefOp
00335     }
00336     SymHandle formal = refop->getBaseSym();
00337     
00338     // get the memory reference in the caller
00339     MemRefHandle memref = mParamBind->getCallerMemRef(mCall,formal);
00340 
00341     // iterate over all mres for the actual memory reference in caller
00342     OA_ptr<MemRefExprIterator> mreIterPtr
00343         = mIR->getMemRefExprIterator(memref);
00344     for (; mreIterPtr->isValid(); (*mreIterPtr)++) {
00345         OA_ptr<OA::MemRefExpr> actual_mre = mreIterPtr->current();
00346 
00347         // create a visitor parameterized with mre in caller      
00348         ConstructMREVisitor visitor(actual_mre, mIR);    
00349 
00350             if (debug) {
00351               std::cout << "\tActual mre = ";
00352               actual_mre->output(*mIR);
00353             }
00354          
00355         // visit the mre in the callee
00356         mre->acceptVisitor(visitor);
00357         OA_ptr<MemRefExpr> composed_mre = visitor.getMemRefExpr();
00358 
00359         if (debug) {
00360               std::cout << "\tComposed mre = ";
00361               composed_mre->output(*mIR);
00362             }
00363          
00364         
00365         // visit the may locs for resulting mre
00366         OA_ptr<LocIterator> mayLocIter 
00367             = mCallerAlias->getMayLocs(*composed_mre, mCaller);
00368         if (debug) {
00369           std::cout << "\tConstructing mayLocs from Caller Proc " << mIR->toString(mCaller)
00370                     << " for Composed mre:\n";
00371         }
00372         for ( ; mayLocIter->isValid(); (*mayLocIter)++ ) {       
00373           mLocSet->insert(mayLocIter->current());
00374           if (debug) {
00375             std::cout << "\t\tinserted==> ";
00376             (mayLocIter->current())->output(*mIR);
00377           }
00378         }         
00379     }
00380   }
00381 }
00382 
00383 void CalleeToCallerVisitor::visitUnknownLoc(UnknownLoc& loc)
00384 {
00385     mLocSet = new LocSet;
00386     OA_ptr<Location> locptr;
00387     locptr = new UnknownLoc();
00388     mLocSet->insert(locptr);
00389 }
00390 
00391 void CalleeToCallerVisitor::visitLocSubSet(LocSubSet& loc)
00392 {
00393     // not handling any of the more precise LocSubSet classes
00394     // just going to make a generic LocSubSet to wrap around
00395     // all the NamedLoc's the underlying location maps to
00396     OA_ptr<Location> baseLoc = loc.getLoc();
00397     if (debug) {
00398         std::cout << "CalleeToCallerVisitor::visitLocSubSet" << std::endl;
00399         std::cout << "\tbaseLoc = ";
00400         baseLoc->dump(std::cout);
00401     }
00402     baseLoc->acceptVisitor(*this);
00403     OA_ptr<LocSet> temp = mLocSet;
00404     mLocSet = new LocSet;
00405     OA_ptr<LocIterator> locIter; locIter = new LocSetIterator(temp);
00406     for ( ; locIter->isValid(); (*locIter)++ ) {
00407         OA_ptr<Location> mayLoc = locIter->current();
00408         if (debug) {
00409             std::cout << "\t\tmayLoc = ";
00410             mayLoc->dump(std::cout);
00411         }
00412         
00413         // the constructor for LocSubSet handles various mayLoc
00414         // subclasses
00415         OA_ptr<Location> subLoc;
00416         subLoc = new LocSubSet(mayLoc);
00417         mLocSet->insert(subLoc);
00418     }
00419 
00420 }
00421 
00422 
00423   } // end of DataFlow namespace
00424 } // end of OA namespace
00425 
00426 
00427 
00428 
00429 
00430 
00431 
00432 
00433 /*
00434 class ConstructMREVisitor {
00435 
00436 public:
00437 
00438   ConstructMREVisitor(OA_ptr<MemRefExpr> actualMRE) : mActualMRE(actualMRE)
00439   {
00440     mMREStack = new std::stack<OA_ptr<MemRefExpr> >;
00441   }
00442   
00443   void visitNamedRef(NamedRef& ref) {
00444     // should never get here because will stop at RefOp that points at the base NamedRef
00445       }
00446   
00447    void visitUnnamedRef(UnnamedRef& ref) {
00448     // should never get here because the base ref in an InvisibleLoc is always a NamedRef
00449       }
00450   
00451   void visitUnknownRef(UnknownRef& ref) {
00452     // should never get here because the base ref in an InvisibleLoc is always a NamedRef
00453   }
00454 
00455   void visitDeref(Deref& ref)
00456   {
00457     // push empty version of this kind of RefOp onto stack
00458     OA_ptr<MemRefExpr> nullMRE;
00459     // will need to write getMRType()
00460     OA_ptr<MemRefExpr> refOp = new RefOp(ref.hasAddressTaken(), ref.hasFullAccuracy(), ref.getMRType(), nullMRE );
00461     mRefOpStack.push(refOp);
00462 
00463     // see if this is the last RefOp before the NamedRef to the formal parameter
00464     OA_ptr<MemRefExpr> mre = ref.getMemRefExpr();
00465     if (mre->isaNamed()) {
00466 
00467          // if it is then call the method that will create the newMRE 
00468       // createNewMRE();
00469     }else {
00470       mre->acceptVisitor(*this);
00471     }
00472    }
00473 
00474   void visitSubSetRef(SubSetRef& ref) { 
00475 
00476   }  
00477 
00478 private:
00479 
00480 
00481       void createNewMRE()
00482     {
00483       OA_ptr<MemRefExpr>mNewMRE = mActualMRE;
00484       // pop things on the list one at a time, for each RefOp, create one of the same kind but have it point to mNewMRE and then assign newly constructed MRE to mNewMRE 
00485 
00486       OA_ptr<MemRefExprIterator> mreIterPtr
00487             = mIR->getMemRefExprIterator(mRefOpStack);
00488 
00489       OA_ptr<OA::MemRefExpr> mre;
00490       for (; mreIterPtr->isValid(); (*mreIterPtr)++) {
00491            mre = mreIterPtr->current();
00492            mre.push(mNewMRE);
00493            mNewMRE = mre;         
00494            }
00495 
00496 
00497 
00498        if (mre->isaDeref()) {
00499 
00500            MRE = new Deref(mre->hasAddressTaken, ..., mNewMRE);
00501 
00502        } else if isaSubsetRef   {
00503 
00504            MRE = new Deref(mre->hasAddressTaken, ..., mNewMRE);
00505        }
00506 
00507        }
00508 private:
00509 
00510   std::stack<OA_ptr<MemRefExpr> > mRefOpStack;
00511   OA_ptr<MemRefExpr>mActualMRE;
00512   OA_ptr<MemRefExpr> mNewMRE;
00513 
00514 } */
00515 

Generated on Sat Oct 31 05:21:20 2009 for OpenAnalysis by  doxygen 1.6.1