ManagerAliasMapXAIF.cpp

Go to the documentation of this file.
00001 
00015 #include "ManagerAliasMapXAIF.hpp"
00016 #include <Utils/Util.hpp>
00017 
00018 namespace OA {
00019   namespace XAIF {
00020 
00021     bool debug = false;
00022 
00025     ManagerAliasMapXAIF::ManagerAliasMapXAIF(OA_ptr<XAIFIRInterface> _ir) : mIR(_ir) {
00026       OA_DEBUG_CTRL_MACRO("DEBUG_ManagerAliasMapXAIF:ALL", debug);
00027     }
00028 
00031     OA_ptr<XAIF::AliasMapXAIF> ManagerAliasMapXAIF::performAnalysis(ProcHandle proc, 
00032                                                                     OA_ptr<Alias::Interface> alias) {
00033       OA_ptr<AliasMapXAIF> retAliasMap; retAliasMap = new AliasMapXAIF(proc);
00034 
00035       if(debug) {
00036         std::cout << "The Procedure is" << mIR->toString(proc) << std::endl;
00037       }
00038 
00039       //---------------------------------
00040       // First iterate over all locations and collect the locations
00041       // into groups based on their base symbol or stmt.
00042       // Therefore all locations that are statically equivalenced will result
00043       // in only one entry and all locations that are partial or full subsets
00044       // of a datastructure like an array will be in the same set.
00045       // The sets will be indexed by SymHandle or StmtHandle
00046 
00047       // named locations indexed by symbol and invisible locations indexed by base location symbol
00048       std::map<SymHandle,LocSet> symToLocSetMap;
00049       // unnamed locations indexed by StmtHandle
00050       std::map<ExprHandle,LocSet> exprToLocSetMap;
00051   
00052       // for each stmt
00053       for (OA_ptr<OA::IRStmtIterator> sItPtr = mIR->getStmtIterator(proc); sItPtr->isValid(); (*sItPtr)++) {
00054         OA::StmtHandle stmt = sItPtr->current();
00055         if (debug) {
00056           std::cout << "===========================" << std::endl << "stmt = ";
00057           mIR->dump(stmt,std::cout);
00058           std::cout << std::endl;
00059         }
00060 
00061         // get all memory references
00062         for (OA_ptr<MemRefHandleIterator> mrItPtr = mIR->getAllMemRefs(stmt); mrItPtr->isValid(); (*mrItPtr)++) {
00063           MemRefHandle memref = mrItPtr->current();
00064           if (debug) {
00065             std::cout << "memref = " << mIR->toString(memref) << std::endl;
00066           }
00067 
00068           // get locations and insert into appropriate set
00069           for (OA_ptr<LocIterator> mayIterPtr = alias->getMayLocs(memref); mayIterPtr->isValid(); (*mayIterPtr)++) {
00070             OA_ptr<Location> loc = mayIterPtr->current();
00071           
00072             if (loc->isaNamed()) { // NamedLoc
00073               OA_ptr<NamedLoc> namedLoc = loc.convert<NamedLoc>();
00074               SymHandle sym = namedLoc->getSymHandle();
00075               symToLocSetMap[sym].insert(namedLoc.convert<Location>());
00076             }
00077             else if (loc->isaUnnamed()) { // UnnamedLoc
00078               OA_ptr<UnnamedLoc> unnamedLoc = loc.convert<UnnamedLoc>();
00079               ExprHandle expr = unnamedLoc->getExprHandle();
00080               exprToLocSetMap[expr].insert(unnamedLoc.convert<Location>());
00081           
00082             }
00083             else if (loc->isaInvisible()) { // Invisible Loc
00084               OA_ptr<InvisibleLoc> invLoc = loc.convert<InvisibleLoc>();
00085               OA_ptr<MemRefExpr> mre = invLoc->getMemRefExpr();
00086               assert(mre->isaRefOp());
00087               OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00088               symToLocSetMap[refOp->getBaseSym()].insert(loc);
00089 
00090             }
00091             else if (loc->isaSubSet()) { // LocSubSets
00092               // get base location from locsubset
00093               OA_ptr<Location> base = loc->getBaseLoc();
00094 
00095               // FIXME: refactor with above
00096               if (base->isaNamed()) { // NamedLoc
00097                 OA_ptr<NamedLoc> namedLoc = base.convert<NamedLoc>();
00098                 SymHandle sym = namedLoc->getSymHandle();
00099                 symToLocSetMap[sym].insert(loc);
00100                   
00101               }
00102               else if (base->isaUnnamed()) { // UnnamedLoc
00103                 OA_ptr<UnnamedLoc> unnamedLoc = base.convert<UnnamedLoc>();
00104                 ExprHandle expr = unnamedLoc->getExprHandle();
00105                 exprToLocSetMap[expr].insert(loc);
00106               }
00107               else if (base->isaInvisible()) { // Invisible Loc
00108                 OA_ptr<InvisibleLoc> invLoc = base.convert<InvisibleLoc>();
00109                 OA_ptr<MemRefExpr> mre = invLoc->getMemRefExpr();
00110                 assert(mre->isaRefOp());
00111                 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00112                 symToLocSetMap[refOp->getBaseSym()].insert(loc);
00113               }
00114             } // end LocSubSets
00115             else if (loc->isaUnknown()) { // UnknownLoc
00116               // do nothing
00117             }
00118           }
00119         }
00120       }
00121               
00122       //---------------------------------
00123       // Create a location tuple for all locations based on the set they
00124       // are a part of and by maintaining a unique id.
00125       int uniqueId = 1;
00126 
00127       std::map<OA_ptr<Location>,LocTuple> locToLocTuple;
00128 
00129       std::map<SymHandle,LocSet>::iterator symMapIter;
00130       for (symMapIter=symToLocSetMap.begin(); symMapIter!=symToLocSetMap.end(); symMapIter++) {
00131         // within each location set count the number of LocSubSets that are partial but have full accuracy,
00132         // that is the number of virtual location ids that will be needed if have any partial accuracy
00133         // sub set locations then will add in two virtual location ids
00134         int count = 0;
00135         bool partialFlag = false;
00136         LocSet::iterator locIter;
00137         LocSet locSet;
00138         locSet = symMapIter->second;
00139         for (locIter=locSet.begin(); locIter!=locSet.end(); locIter++) {
00140           OA_ptr<Location> loc = *locIter;
00141           if (debug) { std::cout << "loc = "; loc->dump(std::cout,mIR); }
00142 
00143           if (loc->isaSubSet()) {
00144             if (partialFlag==false) { // first time we see this
00145               count += 2;  // 2 spaces for all partial references to a loc
00146             }
00147             partialFlag = true;
00148           }
00149           else if(loc->isaNamed()) {
00150             count++;
00151           }
00152           else if(loc->isaUnnamed()) { 
00153             count++;
00154           }
00155           else if(loc->isaUnknown()) { 
00156             count++;
00157           }
00158           else if(loc->isaInvisible()) { 
00159             OA_ptr<InvisibleLoc> invisibleLoc = loc.convert<InvisibleLoc>();
00160             OA_ptr<MemRefExpr> mre = invisibleLoc->getMemRefExpr();
00161             assert(mre->isaRefOp());
00162             OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00163             if(refOp->isaSubSetRef()) {
00164               count += 2;
00165             }
00166             else {
00167               count++;
00168             }
00169           }
00170         }
00171         if (count == 0) { count = 1; }
00172         if (debug) { std::cout << "count = " << count << std::endl; }
00173       
00174         // if there were partials then start currentOffset 2 in
00175         int currentSubOffset = 0;
00176         if (partialFlag==true) {
00177           currentSubOffset = 2;
00178         } 
00179 
00180         // now loop over all locations in this list and map to a LocTuple
00181         for (locIter=locSet.begin(); locIter!=locSet.end(); locIter++) {
00182           OA_ptr<Location> loc = *locIter;
00183 
00184           if (loc->isaSubSet()) {
00185             OA_ptr<LocSubSet> subLoc = loc.convert<LocSubSet>();
00186             locToLocTuple[loc] = LocTuple(uniqueId,uniqueId+count-1,false);
00187           }
00188           else {
00189             locToLocTuple[loc] = LocTuple(uniqueId,uniqueId+count-1,true);
00190           }
00191         }
00192      
00193         // must increase uniqueId by at least one
00194         uniqueId += count;
00195       }
00196 
00197       std::map<ExprHandle,LocSet>::iterator exprMapIter;
00198       for (exprMapIter=exprToLocSetMap.begin(); exprMapIter!=exprToLocSetMap.end(); exprMapIter++ ) {
00199         // within each location set count the number of LocSubSets that are partial but have full accuracy,
00200         // that is the number of virtual location ids that will be needed if have any partial accuracy
00201         // sub set locations then will add in two virtual location ids
00202         int count = 0;
00203         LocSet::iterator locIter;
00204         LocSet locSet;
00205         locSet= exprMapIter->second;
00206 
00207         // now loop over all locations in this list and map to a LocTuple
00208         for (locIter=locSet.begin(); locIter!=locSet.end(); locIter++) {
00209           OA_ptr<Location> loc = *locIter;
00210           if (loc->isaUnnamed()) {
00211             count+=2;
00212             locToLocTuple[loc] = LocTuple(uniqueId,uniqueId+count-1,false);
00213           }
00214           else {
00215             assert(0);
00216           }
00217         }
00218         uniqueId += count;
00219       }
00220 
00221       //---------------------------------
00222       // Then iterate over the aliasing information again using the newly determined LocTuples 
00223   
00224       // get all memory references that alias analysis has info for
00225       if (debug) {
00226         std::cout << "==== MemRefHandles that alias analysis has info for" << std::endl;
00227       }
00228       for (OA_ptr<MemRefHandleIterator> mrItPtr = alias->getMemRefIter(); mrItPtr->isValid(); (*mrItPtr)++) {
00229         MemRefHandle memref = mrItPtr->current();
00230         if (debug) { std::cout << "memref = " << mIR->toString(memref) << std::endl; } 
00231         // trying to determine what set we will map this memref to
00232         int setId = Alias::AliasMap::SET_ID_NONE;
00233 
00234         OA_ptr<std::set<LocTuple> > maySet;
00235         maySet = new std::set<LocTuple>;
00236         bool foundUnknown = false;
00237         for (OA_ptr<LocIterator> mayIterPtr = alias->getMayLocs(memref); mayIterPtr->isValid(); (*mayIterPtr)++) {
00238           OA_ptr<Location> loc = mayIterPtr->current();
00239           if (loc->isaUnknown()) {
00240             foundUnknown = true;
00241           }
00242           else {
00243             maySet->insert(locToLocTuple[loc]);
00244           }
00245         } 
00246 
00247         if (foundUnknown) { // if contains the UnknownLoc just map to special 0 alias map set
00248           setId = 0;
00249         }
00250         else if (maySet->empty()) {
00251           // if the set is empty then map to special 1 alias map set
00252           // (this is an addressOf MemRef and does not access a location)
00253           setId = 1;
00254         }
00255         else { // The loc set is neither unknown nor empty => either find an existing map set or create a new one
00256           setId = retAliasMap->findMapSet(maySet);
00257           if (setId == Alias::AliasMap::SET_ID_NONE) {
00258             setId = getNextSetId();
00259             retAliasMap->mapLocTupleSet(maySet,setId); // insert all locations into this new set
00260           }
00261         }
00262         retAliasMap->mapMemRefToMapSet(memref,setId);
00263       }
00264       return retAliasMap;
00265     } // end ManagerAliasMapXAIF::performAnalysis()
00266 
00267     int ManagerAliasMapXAIF::sCurrentStartId = 2;
00268 
00269     int ManagerAliasMapXAIF::getNextSetId() {
00270       return sCurrentStartId++;
00271     } // end ManagerAliasMapXAIF::getNextSetId()
00272 
00273   } // end of namespace XAIF
00274 } // end of namespace OA

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