ManagerAliasMapBasic.cpp

Go to the documentation of this file.
00001 
00017 #include "ManagerAliasMapBasic.hpp"
00018 #include <Utils/Util.hpp>
00019 
00020 
00021 namespace OA {
00022   namespace Alias {
00023 
00024 static bool debug = false;
00025 
00033 class CreateLocationVisitor : public virtual MemRefExprVisitor {
00034   public:
00035     OA_ptr<Location> mLoc;
00036     CreateLocationVisitor(OA_ptr<AliasIRInterface> ir,
00037                           ProcHandle proc) : mIR(ir),mProc(proc) {}
00038     ~CreateLocationVisitor() {}
00039     void visitNamedRef(NamedRef& ref) 
00040       {
00041           mLoc = mIR->getLocation(mProc,ref.getSymHandle());
00042 
00043           /* PLM 1/23/07 deprecated hasFullAccuracy
00044           // if the memory reference doesn't have full accuracy then
00045           // create a partial subset of the base memory location
00046           if (!ref.hasFullAccuracy()) {
00047               mLoc = new LocSubSet(mLoc,false);
00048           }
00049           */
00050 
00051           // mre's with addressOf do not have locations associated with them
00052           //assert(ref.hasAddressTaken()!=true);
00053       }
00054     void visitUnnamedRef(UnnamedRef& ref) { mLoc = new UnknownLoc; }
00055     void visitUnknownRef(UnknownRef& ref) { mLoc = new UnknownLoc; }
00056     void visitAddressOf(AddressOf& ref) 
00057     {
00058         mLoc = new UnknownLoc();
00059     }
00060 
00061     void visitDeref(Deref& ref) { mLoc = new UnknownLoc; }
00062     // default handling of more specific SubSet specificiations
00063     void visitSubSetRef(SubSetRef& ref) 
00064       {
00065           // will set mLoc to our base location
00066           ref.getMemRefExpr()->acceptVisitor(*this);
00067           if (mLoc->isaNamed()) {
00068             mLoc = new LocSubSet(mLoc,false);
00069           }
00070       }
00071 
00072   private:
00073     OA_ptr<AliasIRInterface> mIR;
00074     ProcHandle mProc;
00075 
00076 };
00077 
00078 
00081 OA_ptr<Alias::AliasMap> ManagerAliasMapBasic::performAnalysis(ProcHandle proc)
00082 {
00083   OA_DEBUG_CTRL_MACRO("DEBUG_ManagerAliasMapBasic:ALL", debug);
00084 
00085   OA_ptr<AliasMap> retAliasMap; 
00086   retAliasMap = new AliasMap(proc);
00087 
00088   mProc = proc;
00089 
00090   // semantics of the AliasMap are that the zeroth set specifies that
00091   // no alias info is known about a memory reference, it could reference
00092   // any location
00093   unsigned int unknownSetId = 0;
00094 
00095   // for each statement stmt
00096   OA_ptr<OA::IRStmtIterator> sIt = mIR->getStmtIterator(proc);
00097   for ( ; sIt->isValid(); (*sIt)++) {
00098     OA::StmtHandle stmt = sIt->current();
00099         
00100     if (debug) {
00101         std::cout << "\tstmt = ";
00102         mIR->dump(stmt,std::cout);
00103     }
00104 
00105     //   for each memory reference in the stmt
00106     OA_ptr<MemRefHandleIterator> mrIterPtr = mIR->getAllMemRefs(stmt);
00107     for (; mrIterPtr->isValid(); (*mrIterPtr)++ )
00108     {
00109       MemRefHandle memref = mrIterPtr->current();
00110       if (debug) {
00111         std::cout << "\tmemref = ";
00112         mIR->dump(memref,std::cout);
00113       }
00114 
00115       // each memref should only be mapped to one set,
00116       // therefore if memref has already been mapped once
00117       // we will want to add locations for other mres
00118       // to the same set
00119       int setId = AliasMap::SET_ID_NONE;
00120 
00121       // get the memory reference expressions for this handle
00122       OA_ptr<MemRefExprIterator> mreIterPtr 
00123           = mIR->getMemRefExprIterator(memref);
00124       
00125       // for each mem-ref-expr associated with this memref
00126       for (; mreIterPtr->isValid(); (*mreIterPtr)++) {
00127         OA_ptr<OA::MemRefExpr> mre = mreIterPtr->current();
00128         if (debug) {
00129             std::cout << "\tmre = ";
00130             mre->dump(std::cout,mIR);
00131         }
00132 
00133         // if mre is addressOf then do not need to map to a set
00134         if(mre->isaRefOp()) {
00135            OA_ptr<RefOp> refop = mre.convert<RefOp>();
00136            if(refop->isaAddressOf()) { continue; }
00137         }
00138 
00139         
00140         // if our memref has already been mapped then our locations
00141         // must be added to its set
00142         if (setId == AliasMap::SET_ID_NONE) {
00143             // see if we have not already mapped an equivalent memrefexpr
00144             setId = retAliasMap->getMapSetId(mre);
00145         }
00146         if (debug) {
00147             std::cout << "\tsetId = " << setId << std::endl;
00148         }
00149 
00150         // need to determine what locations mre maps to 
00151         OA_ptr<Location> loc;
00152         if (setId == AliasMap::SET_ID_NONE ) {
00153             
00154             // default set id
00155             setId = unknownSetId;
00156 
00157             // use visitor to get Location this MRE maps to
00158             CreateLocationVisitor locVisitor(mIR,proc);
00159             mre->acceptVisitor(locVisitor);
00160             loc = locVisitor.mLoc;
00161             
00162             // attempt to find location in current location sets
00163             setId = retAliasMap->getMapSetId(loc);
00164             if (debug) {
00165                 std::cout << "\tsetId = " << setId << ", loc = ";
00166                 loc->dump(std::cout, mIR);
00167             }
00168         }
00169 
00170         // if we didn't find the location in the aliasmap
00171         // sets then we need to put the location in a
00172         // new aliasmap set 
00173         if (setId==AliasMap::SET_ID_NONE) {
00174             // get a new set
00175             setId = retAliasMap->makeEmptySet();
00176 
00177             // put the loc into the new map set
00178             retAliasMap->addLocation(loc,setId);
00179         } 
00180 
00181         // map mre to set only if wasn't already mapped
00182         if (!mre.ptrEqual(0)) { retAliasMap->mapMemRefToMapSet(mre,setId); }
00183 
00184       } // loop over mre's
00185       
00186       // map memref to set
00187       retAliasMap->mapMemRefToMapSet(memref,setId);
00188 
00189     } // loop over memory references
00190   } // loop over statements
00191 
00192   return retAliasMap;
00193 }
00194 
00195   } // end of namespace Alias
00196 } // end of namespace OA