AliasMap.cpp

Go to the documentation of this file.
00001 
00015 #include "AliasMap.hpp"
00016 #include <Utils/Util.hpp>
00017 
00018 namespace OA {
00019   namespace Alias {
00020 
00021 static bool debug = false;
00022 
00023 const int AliasMap::SET_ID_NONE = -1; 
00024 
00025 AliasMap::AliasMap() {
00026     OA_DEBUG_CTRL_MACRO("DEBUG_AliasMap:ALL", debug);
00027 }
00028 
00029 AliasMap::AliasMap(ProcHandle p) : mProcHandle(p), mNumSets(1), mStartId(0)
00030 {
00031   OA_DEBUG_CTRL_MACRO("DEBUG_AliasMap:ALL", debug);
00032   // default set for things MemRefHandles that we don't know enough
00033   // information about
00034   mIdToSetStatusMap[0] = MAYALIAS;
00035   OA_ptr<Location> loc;
00036   loc = dynamic_cast<Location*>(new UnknownLoc());
00037   mIdToLocSetMap[0] = new LocSet;
00038   mIdToLocSetMap[0]->insert( loc );
00039 
00040 }
00041 
00045 AliasMap::AliasMap(AliasMap& other)
00046 {
00047     mProcHandle = other.mProcHandle;
00048     mNumSets = other.mNumSets;
00049     mStartId = other.mStartId;
00050     mIdToLocSetMap = other.mIdToLocSetMap;
00051     mIdToSetStatusMap = other.mIdToSetStatusMap;
00052     mIdToMemRefSetMap = other.mIdToMemRefSetMap;
00053     mMemRefToIdMap = other.mMemRefToIdMap;
00054     mMREToIdMap = other.mMREToIdMap;
00055     mLocToIdMap = other.mLocToIdMap;
00056 }
00057 
00058 
00059 
00070 AliasResultType AliasMap::alias(MemRefHandle ref1, MemRefHandle ref2)
00071 { 
00072     AliasResultType retval = MAYALIAS;
00073 
00074     // If either of the refs map to 0 (the unknown loc set), return
00075     // MAYALIAS.
00076     std::set<int>::iterator it1;
00077     std::set<int>::iterator it2;
00078 
00079     OA_ptr<std::set<int> > ids1 = getMapSetIds(ref1);
00080     for(it1 = ids1->begin(); it1 != ids1->end(); ++it1) {
00081       if (*it1 == 0) 
00082         return retval;
00083     }
00084 
00085     OA_ptr<std::set<int> > ids2 = getMapSetIds(ref2);
00086     for(it2 = ids2->begin(); it2 != ids2->end(); ++it2) {
00087       if (*it2 == 0) 
00088         return retval;
00089     }
00090 
00091     // Check to see if there is any overlap in the sets.
00092     std::set<int> temp; 
00093 /*
00094     std::set_intersection(ids1->begin(), 
00095                           ids1->end(),
00096                           ids2->begin(), 
00097                           ids2->end(),
00098                           std::inserter(temp,temp.end()));
00099 */
00100 
00101     std::set_intersection(ids1->begin(),
00102                           ids1->end(),
00103                           ids2->begin(),
00104                           ids2->end(),
00105                           std::inserter(temp,temp.begin()));
00106  
00107  
00108     if (!temp.empty()) {
00109 
00110       // The sets do overlap.  If there is an overlapping MUST alias
00111       // set, return MUSTALIAS.  Else return MAYAliaS.
00112       for (it1 = temp.begin(); it1 != temp.end(); ++it1) {
00113         if (mIdToSetStatusMap[*it1]==MUSTALIAS)
00114           return MUSTALIAS;
00115       }
00116 
00117       return MAYALIAS;
00118     } 
00119 
00120     bool locationsOverlap = false;
00121     retval = MAYALIAS;
00122 
00123     for(it1 = ids1->begin(); it1 != ids1->end(); ++it1) {
00124        
00125       if (*it1 == 0)
00126       {
00127         return retval;
00128       } 
00129           
00130       for(it2 = ids2->begin(); it2 != ids2->end(); ++it2) {
00131 
00132        if (*it2 == 0) 
00133        { 
00134         return retval;
00135        }  
00136 
00137         if (mayOverlapLocSets(*mIdToLocSetMap[*it1],
00138                               *mIdToLocSetMap[*it2]) ) {
00139           locationsOverlap = true;
00140           if (isMust(*it1) && isMust(*it2)) {
00141             retval = MUSTALIAS;
00142             return retval;
00143           }
00144           // At this point, we know we have at least a MAYALIAS.
00145           // However, subsequent mayOverlapLocSets may determine
00146           // this is a MUSTALIAS, so keep looking.
00147         }
00148 
00149       }
00150     }
00151 
00152     if (locationsOverlap) {
00153       return retval;
00154     }
00155 
00156     // otherwise these two references do not alias
00157     retval = NOALIAS; 
00158 
00159     return retval; 
00160 } 
00161 
00163 OA_ptr<LocIterator> AliasMap::getLocIterator(int setId) 
00164 { OA_ptr<LocSetIterator> retval;
00165   if (mIdToLocSetMap[setId].ptrEqual(0)) {
00166     OA_ptr<LocSet> emptySet; emptySet = new LocSet;
00167     retval = new LocSetIterator(emptySet);  // empty loc set
00168   } else {
00169     retval = new LocSetIterator(mIdToLocSetMap[setId]); 
00170   }
00171   return retval;
00172 }
00173 
00175 OA_ptr<LocIterator> AliasMap::getMayLocs(MemRefHandle ref)
00176 {
00177 
00178     /*
00179     std::cout << "reference MemRefHandle" << ref.hval() << std::endl;
00180 
00181     std::map<MemRefHandle, std::set<int> >::iterator reg_mMemRefToIdMap_iterator;
00182 
00183 
00184     std::cout << "before mMemRefToIdMap" << std::endl; 
00185     for(reg_mMemRefToIdMap_iterator = mMemRefToIdMap.begin();
00186         reg_mMemRefToIdMap_iterator != mMemRefToIdMap.end();
00187         reg_mMemRefToIdMap_iterator++)
00188     {
00189         const MemRefHandle &first = reg_mMemRefToIdMap_iterator->first;
00190         std::cout << "MemRefHandle" << first.hval() << std::endl;
00191     }
00192     std::cout << "after mMemRefToIdMap" << std::endl;
00193     */
00194     
00195     OA_ptr<LocSet> locSet;
00196     locSet = new LocSet;
00197     OA_ptr<LocSetIterator> retval;
00198 
00199     // iterate over all the sets for this MemRefHandle and return
00200     // an iterator over all the locations in those sets
00201     std::set<int>::iterator iter;
00202     for (iter=mMemRefToIdMap[ref].begin();
00203          iter!=mMemRefToIdMap[ref].end(); iter++)
00204     {
00205       if (!mIdToLocSetMap[*iter].ptrEqual(0))
00206       {
00207 
00208          locSet = unionLocSets(*locSet, 
00209                             *(mIdToLocSetMap[*iter]));
00210       }
00211     }
00212 
00213     retval = new LocSetIterator(locSet);
00214     return retval;
00215 }
00216 
00219 OA_ptr<LocIterator> AliasMap::getMustLocs(MemRefHandle ref)
00220 {
00221     OA_ptr<LocSet> locSet;
00222     locSet = new LocSet;
00223     OA_ptr<LocSetIterator> retval;
00224 
00225     // iterate over all the sets for this MemRefHandle and return
00226     // an iterator over all the locations in those sets
00227     std::set<int>::iterator iter;
00228     for (iter=mMemRefToIdMap[ref].begin();
00229          iter!=mMemRefToIdMap[ref].end(); iter++)
00230     {
00231       if (mIdToSetStatusMap[*iter]==MUSTALIAS 
00232           && !mIdToLocSetMap[*iter].ptrEqual(0)) 
00233       {
00234         locSet = unionLocSets(*locSet, 
00235                               *(mIdToLocSetMap[*iter]));
00236       }
00237     }
00238 
00239     retval = new LocSetIterator(locSet);
00240     return retval;
00241 }
00242 
00244 OA_ptr<LocIterator> AliasMap::getMayLocs(MemRefExpr &ref, ProcHandle proc)
00245 {
00246     OA_ptr<LocSet> locSet;
00247     locSet = new LocSet;
00248     OA_ptr<LocSetIterator> retval;
00249 
00250     // iterate over all the sets for this MemRefHandle and return
00251     // an iterator over all the locations in those sets
00252     OA_ptr<MemRefExpr> refPtr = ref.clone();
00253     if( mMREToIdMap.find(refPtr) != mMREToIdMap.end() ) {
00254       int id = mMREToIdMap[refPtr];
00255       if (!mIdToLocSetMap[id].ptrEqual(0))
00256       {
00257          locSet = unionLocSets(*locSet, *(mIdToLocSetMap[id]));
00258       }
00259     }
00260 
00261     // if nothing was found then return the may locs for 
00262     // the partially or fully accurate counterpart
00263     // It is necessary when
00264     // an aggregate is only accessed one way or another and then
00265     // the other MRE is used to look up mayloc info.
00266     OA_ptr<MemRefExpr> mreClone = refPtr->clone();
00267 
00268     /* PLM 1/23/07 deprecated hasFullAccuracy
00269     if (refPtr->hasFullAccuracy() ) {
00270         mreClone->setAccuracy(false);
00271     } else {
00272         mreClone->setAccuracy(true);
00273     }
00274     */
00275 
00276    if(!mreClone->isaSubSetRef()) {
00277       OA::OA_ptr<OA::SubSetRef> subset_mre;
00278       OA::OA_ptr<OA::MemRefExpr> nullMRE;
00279       OA::OA_ptr<OA::MemRefExpr> composed_mre;
00280 
00281       subset_mre = new OA::SubSetRef(
00282                                      OA::MemRefExpr::USE,
00283                                      nullMRE
00284                                     );
00285 
00286       mreClone = subset_mre->composeWith(mreClone->clone());
00287     } else {     
00288       mreClone = mreClone.convert<RefOp>()->getMemRefExpr();   
00289     }
00290    
00291     
00292     if( mMREToIdMap.find(mreClone) != mMREToIdMap.end() ) {
00293       int id = mMREToIdMap[mreClone];
00294       if (!mIdToLocSetMap[id].ptrEqual(0))
00295       {
00296          locSet = unionLocSets(*locSet, *(mIdToLocSetMap[id]));
00297       }
00298     }
00299 
00300     retval = new LocSetIterator(locSet);
00301     return retval;
00302 }
00303 
00305 OA_ptr<LocIterator> AliasMap::getMustLocs(MemRefExpr &ref, ProcHandle proc)
00306 {
00307     OA_ptr<LocSet> locSet;
00308     locSet = new LocSet;
00309     OA_ptr<LocSetIterator> retval;
00310 
00311     OA_ptr<MemRefExpr> refPtr = ref.clone();
00312     if ( mMREToIdMap.find(refPtr) != mMREToIdMap.end() ) {
00313       int id = mMREToIdMap[refPtr];
00314       if (mIdToSetStatusMap[id]==MUSTALIAS 
00315           && !mIdToLocSetMap[id].ptrEqual(0)) 
00316       {
00317         locSet = unionLocSets(*locSet, 
00318                               *(mIdToLocSetMap[id]));
00319       }
00320     }
00321 
00322     retval = new LocSetIterator(locSet);
00323     return retval;
00324 }
00325 
00327 OA_ptr<MemRefIterator> AliasMap::getMustAliases(MemRefHandle ref)
00328 { 
00329     // create an empty MemRefSet that will be given to iterator
00330     OA_ptr<MemRefSet> mustSet; mustSet = new MemRefSet;
00331 
00332     // determine what sets the references map to
00333     OA_ptr<std::set<int> > ids = getMapSetIds(ref);
00334     std::set<int>::iterator it;
00335 
00336     if (debug) {
00337       std::cout << "AliasMap::getMustAliases: id = ";
00338       for(it = ids->begin(); it != ids->end(); ++it) {
00339         std::cout << *it << " ";
00340       }
00341     }
00342 
00343     // find all the map sets that contain locations which may overlap with
00344     // this map set (it should find itself) and both alias map sets have
00345     // MUST status
00346     std::set<int> mustOverlapMapSetIds;
00347     std::map<int,OA_ptr<LocSet> >::iterator mapIter;
00348     for (it = ids->begin(); it != ids->end(); ++it) {
00349 
00350       int id = *it;
00351 
00352       for (mapIter = mIdToLocSetMap.begin(); mapIter != mIdToLocSetMap.end();
00353            mapIter++) 
00354       {
00355           int otherId = mapIter->first;
00356           if (mIdToSetStatusMap[otherId]==MUSTALIAS 
00357               && mIdToSetStatusMap[id]==MUSTALIAS
00358               && mayOverlapLocSets(*(mapIter->second),*mIdToLocSetMap[id])) 
00359           {
00360               mustOverlapMapSetIds.insert(otherId);
00361           }
00362       }
00363     }
00364 
00365     // find all the memory references that map to those loc sets
00366     // and put them in the set of must aliases
00367     std::set<int>::iterator setIter;
00368     for (setIter=mustOverlapMapSetIds.begin(); 
00369          setIter!=mustOverlapMapSetIds.end(); setIter++ )
00370     {
00371         int setId = *setIter;
00372 
00373         MemRefSet::iterator memrefSetIter;
00374         for (memrefSetIter=mIdToMemRefSetMap[setId].begin();
00375              memrefSetIter!=mIdToMemRefSetMap[setId].end();
00376              memrefSetIter++) 
00377         {
00378             mustSet->insert(*memrefSetIter);
00379         }
00380     }
00381 
00382     OA_ptr<MemRefIterator> retIter; retIter = new AliasMapMemRefIter(mustSet);
00383     return retIter;
00384 
00385 }
00386 
00388 OA_ptr<MemRefIterator> AliasMap::getMayAliases(MemRefHandle ref)
00389 { 
00390     // create an empty MemRefSet that will be given to iterator
00391     OA_ptr<MemRefSet> maySet; maySet = new MemRefSet;
00392 
00393     // determine what sets the references map to
00394     OA_ptr<std::set<int> > ids = getMapSetIds(ref);
00395     std::set<int>::iterator it;
00396 
00397     if (debug) {
00398       std::cout << "AliasMap::getMayAliases: id = ";
00399       for(it = ids->begin(); it != ids->end(); ++it) {
00400         std::cout << *it << " ";
00401       }
00402     }
00403 
00404     // find all the map sets that contain locations which may overlap with
00405     // this map set (it should find itself)
00406     std::set<int> mayOverlapMapSetIds;
00407     std::map<int,OA_ptr<LocSet> >::iterator mapIter;
00408     for (it = ids->begin(); it != ids->end(); ++it) {
00409 
00410       int id = *it;
00411 
00412       for (mapIter = mIdToLocSetMap.begin(); mapIter != mIdToLocSetMap.end();
00413            mapIter++) 
00414       {
00415           int otherId = mapIter->first;
00416           if (mayOverlapLocSets(*(mapIter->second),*mIdToLocSetMap[id])) 
00417           {
00418               mayOverlapMapSetIds.insert(otherId);
00419           }
00420       }
00421     }
00422 
00423     // find all the memory references that map to those loc sets
00424     // and put them in the set of must aliases
00425     std::set<int>::iterator setIter;
00426     for (setIter=mayOverlapMapSetIds.begin(); 
00427          setIter!=mayOverlapMapSetIds.end(); setIter++ )
00428     {
00429         int setId = *setIter;
00430 
00431         MemRefSet::iterator memrefSetIter;
00432         for (memrefSetIter=mIdToMemRefSetMap[setId].begin();
00433              memrefSetIter!=mIdToMemRefSetMap[setId].end();
00434              memrefSetIter++) 
00435         {
00436             maySet->insert(*memrefSetIter);
00437         }
00438     }
00439 
00440     OA_ptr<MemRefIterator> retIter; retIter = new AliasMapMemRefIter(maySet);
00441     return retIter;
00442 }
00443     
00445 OA_ptr<MemRefIterator> AliasMap::getMustAliases(OA_ptr<Location> loc)
00446 {
00447     // create an empty MemRefSet that will be given to iterator
00448     OA_ptr<MemRefSet> mustSet; mustSet = new MemRefSet;
00449 
00450     // find all the map sets that contain locations which may overlap with
00451     // this location and the alias map set is a MUST alias and this location
00452     // is fully accurate
00453     LocSet singleEntrySet;
00454     singleEntrySet.insert(loc);
00455     std::set<int> mustOverlapMapSetIds;
00456     std::map<int,OA_ptr<LocSet> >::iterator mapIter;
00457     for (mapIter = mIdToLocSetMap.begin(); mapIter != mIdToLocSetMap.end();
00458          mapIter++) 
00459     {
00460         int otherId = mapIter->first;
00461 
00462         /* PLM 1/23/07 deprecated accuracy field
00463         if (mIdToSetStatusMap[otherId]==MUSTALIAS 
00464             && loc->hasFullAccuracy()
00465             && mayOverlapLocSets(*(mapIter->second),
00466                                            singleEntrySet)) 
00467         {
00468             mustOverlapMapSetIds.insert(otherId);
00469         }
00470         */
00471 
00472         if (mIdToSetStatusMap[otherId]==MUSTALIAS
00473             && (!loc->isaSubSet())
00474             && mayOverlapLocSets(*(mapIter->second),
00475                                            singleEntrySet))
00476         {
00477             mustOverlapMapSetIds.insert(otherId);
00478         }
00479 
00480     }
00481 
00482     // find all the memory references that map to those loc sets
00483     // and put them in the set of must aliases
00484     std::set<int>::iterator setIter;
00485     for (setIter=mustOverlapMapSetIds.begin(); 
00486          setIter!=mustOverlapMapSetIds.end(); setIter++ )
00487     {
00488         int setId = *setIter;
00489 
00490         MemRefSet::iterator memrefSetIter;
00491         for (memrefSetIter=mIdToMemRefSetMap[setId].begin();
00492              memrefSetIter!=mIdToMemRefSetMap[setId].end();
00493              memrefSetIter++) 
00494         {
00495             mustSet->insert(*memrefSetIter);
00496         }
00497     }
00498 
00499     OA_ptr<MemRefIterator> retIter; retIter = new AliasMapMemRefIter(mustSet);
00500     return retIter;
00501 }
00502 
00504 OA_ptr<MemRefIterator> AliasMap::getMayAliases(OA_ptr<Location> loc)
00505 {
00506     // create an empty MemRefSet that will be given to iterator
00507     OA_ptr<MemRefSet> maySet; maySet = new MemRefSet;
00508 
00509     // find all the map sets that contain locations which may overlap with
00510     // this location 
00511     LocSet singleEntrySet;
00512     singleEntrySet.insert(loc);
00513     std::set<int> mayOverlapMapSetIds;
00514     std::map<int,OA_ptr<LocSet> >::iterator mapIter;
00515     for (mapIter = mIdToLocSetMap.begin(); mapIter != mIdToLocSetMap.end();
00516          mapIter++) 
00517     {
00518         int otherId = mapIter->first;
00519         if (mayOverlapLocSets(*(mapIter->second),singleEntrySet)) 
00520         {
00521             mayOverlapMapSetIds.insert(otherId);
00522         }
00523     }
00524 
00525     // find all the memory references that map to those loc sets
00526     // and put them in the set of may aliases
00527     std::set<int>::iterator setIter;
00528     for (setIter=mayOverlapMapSetIds.begin(); 
00529          setIter!=mayOverlapMapSetIds.end(); setIter++ )
00530     {
00531         int setId = *setIter;
00532 
00533         MemRefSet::iterator memrefSetIter;
00534         for (memrefSetIter=mIdToMemRefSetMap[setId].begin();
00535              memrefSetIter!=mIdToMemRefSetMap[setId].end();
00536              memrefSetIter++) 
00537         {
00538             maySet->insert(*memrefSetIter);
00539         }
00540     }
00541 
00542     OA_ptr<MemRefIterator> retIter; retIter = new AliasMapMemRefIter(maySet);
00543     return retIter;
00544 }
00545 
00548 OA_ptr<MemRefIterator> AliasMap::getMemRefIter()
00549 {
00550     // create an empty MemRefSet that will be given to iterator
00551     OA_ptr<MemRefSet> memrefSet; memrefSet = new MemRefSet;
00552 
00553     // put all memory references that we know about into the set
00554     std::map<MemRefHandle,std::set<int> >::iterator mapIter;
00555     for (mapIter=mMemRefToIdMap.begin(); mapIter!=mMemRefToIdMap.end();
00556          mapIter++) 
00557     {
00558         memrefSet->insert(mapIter->first);
00559     }
00560 
00561     OA_ptr<MemRefIterator> retIter; retIter = new AliasMapMemRefIter(memrefSet);
00562     return retIter;
00563 }
00564 
00565 //*****************************************************************
00566 // DataFlowSet interface
00567 //*****************************************************************
00568 /*
00569 bool AliasMap::operator ==(DataFlow::DataFlowSet &other) const
00570 {
00571     if (mIdToLocSetMap==other.mIdToLocSetMap
00572         && mIdToMemRefSetMap==other.mIdToMemRefSetMap
00573         && mIdToMRESetMap == other.mIdToMRESetMap)
00574     {
00575         return true;
00576     } else {
00577         return false;
00578     }
00579 }
00580 
00581 bool AliasMap::operator !=(DataFlow::DataFlowSet &other) const
00582 {
00583     return ! operator==(other);
00584 }
00585 
00586 OA_ptr<DataFlow::DataFlowSet> AliasMap::clone()
00587 {
00588     OA_ptr<AliasMap> retval;
00589     retval = new AliasMap(*this);
00590     return retval;
00591 }
00592 */
00593 //*****************************************************************
00594 // Meet routine
00595 //*****************************************************************
00596 
00603 /*
00604 OA_ptr<AliasMap> AliasMap::meet(AliasMap& other)
00605 {
00606     OA_ptr<AliasMap> retval;
00607 
00608     // first make a copy of ourselves
00609     retval = new AliasMap(*this); 
00610     
00611     // loop through all sets in other
00612     OA_ptr<IdIterator> idIterPtr = getIdIterator();
00613     for ( ; idIterPtr->isValid(); ++(*idIterPtr) ) {
00614       int i = idIterPtr->current();
00615       // if there is more than one location
00616     
00617 }
00618 */ 
00619 
00620 //*****************************************************************
00621 // Info methods unique to Alias::AliasMap
00622 //*****************************************************************
00623     
00627 OA_ptr<std::set<int> > AliasMap::getMapSetIds(MemRefHandle ref) 
00628 {
00629     std::map<MemRefHandle,std::set<int> >::iterator pos;
00630     pos = mMemRefToIdMap.find(ref);
00631 
00632     OA_ptr<std::set<int> > retval;
00633     retval = new std::set<int>;
00634 
00635     if (pos != mMemRefToIdMap.end()) {
00636         std::set<int>::iterator setIt;
00637         for(setIt = pos->second.begin(); setIt != pos->second.end(); ++setIt) {
00638            retval->insert(*setIt);
00639         }
00640     } else {
00641         retval->insert(AliasMap::SET_ID_NONE);
00642     }
00643 
00644     return retval;
00645 }
00646 
00653 int AliasMap::getMapSetId(OA_ptr<MemRefExpr> mre) 
00654 {
00655     int retval = AliasMap::SET_ID_NONE;
00656 
00657     std::map<OA_ptr<MemRefExpr>,int>::iterator pos;
00658     pos = mMREToIdMap.find(mre);
00659     if (pos != mMREToIdMap.end()) {
00660         return pos->second;
00661     } else {
00662         return AliasMap::SET_ID_NONE;
00663     }
00664 }
00665     
00666 
00676 int AliasMap::getMapSetId(OA_ptr<OA::Location> pLoc)
00677 {
00678     int retval = SET_ID_NONE;
00679 
00680     // loop through mapping of locations's to alias map ids to determine if
00681     // the given location is equivalent
00682     std::map<OA_ptr<Location>,int>::iterator mapIter;
00683     for (mapIter=mLocToIdMap.begin(); mapIter!=mLocToIdMap.end(); mapIter++) {
00684         OA_ptr<Location> knownloc = mapIter->first;
00685         // see if the already mapped location is equivalent to the given location
00686         if ( knownloc == pLoc ) {
00687             retval = mapIter->second;
00688             break;
00689         }
00690     }
00691 
00692     return retval;
00693 }
00694 
00698 int AliasMap::getMapSetId(LocSet pLocSet)
00699 {
00700     int retval = SET_ID_NONE;
00701 
00702     // loop through location sets and compare them to given location set
00703     std::map<int,OA_ptr<LocSet> >::iterator mapIter;
00704     for (mapIter=mIdToLocSetMap.begin(); mapIter!=mIdToLocSetMap.end();
00705          mapIter++)
00706     {
00707         OA_ptr<LocSet> mapLocSet = mapIter->second;
00708         if (subSetOf(pLocSet,*mapLocSet) 
00709             && subSetOf(*mapLocSet,pLocSet) )
00710         {
00711             return mapIter->first;
00712         } 
00713     }
00714 
00715     return retval;
00716 }
00717 
00718 OA_ptr<IdIterator> AliasMap::getIdIterator()
00719 {
00720     OA_ptr<IdIterator> retval;
00721     retval = new IdIterator(mIdToLocSetMap);
00722     return retval;
00723 }
00724  
00725 /*
00729 OA_ptr<Location> AliasMap::getLocForSym(SymHandle sym)
00730 {
00731     OA_ptr<Location> retval; retval = NULL;
00732     std::map<SymHandle,OA_ptr<Location> >::iterator pos;
00733     pos = mSymToLocMap.find(sym);
00734     if (pos!=mSymToLocMap.end()) {
00735         retval = pos->second;
00736     }
00737     return retval;
00738 }
00739 */
00740 
00742 int AliasMap::makeEmptySet()
00743 {
00744     int newsetId = mStartId + mNumSets;
00745     mIdToSetStatusMap[newsetId] = MUSTALIAS;
00746     mIdToLocSetMap[newsetId] = new LocSet;
00747     mNumSets++;
00748     return newsetId;
00749 }
00750      
00751 
00752 /*
00754 void AliasMap::insertInto(MemRefHandle ref, int setId)
00755 {
00756     assert(setId < mNumSets);
00757     assert(setId >= 0);
00758 
00759     mMemRef2IdMap[ref] = setId;
00760 }
00761 */
00762 
00764 void AliasMap::addLocation(OA_ptr<Location> pLoc, int setId)
00765 {
00766 
00767     if (debug) {
00768         std::cout << "AliasMap::addLocation: setId = " << setId << ", pLoc = ";
00769         pLoc->dump(std::cout);
00770     }
00771     // check that an empty location set exists for this setId
00772     if (mIdToLocSetMap[setId].ptrEqual(0)) {
00773        mIdToLocSetMap[setId] = new LocSet;
00774     }
00775 
00776     // if new location doesn't have full accuracy then need
00777     // to give the set MAYALIAS status
00778     if (mIdToSetStatusMap[setId]==MUSTALIAS) {
00779 
00780         // subclasses of LocSubSet are fully accurate
00781         if(pLoc->isaSubSet()) {
00782            OA_ptr<LocSubSet> subLoc = pLoc.convert<LocSubSet>();
00783            if(!subLoc->isSubClassOfLocSubSet()) {
00784                 mIdToSetStatusMap[setId] = MAYALIAS;
00785                 if (debug) {
00786                   std::cout << "\tpLoc not fully accurate, making set " 
00787                             << setId << " MAYALIAS\n";
00788                 }
00789            }
00790         } else {
00791             /* MMS 5/17/07, this is too expensive and only useful
00792              * when have NamedLocs with different SymHandles that
00793              * statically alias.
00794             // also if there are other locations in the set already 
00795             // and they don't all fully overlap with this location
00796             LocSetIterator locIter(mIdToLocSetMap[setId]);
00797             for (; locIter.isValid(); ++locIter) {
00798                 OA_ptr<Location> loc = locIter.current();
00799                 if (!loc->mustOverlap(*pLoc)) {
00800                     mIdToSetStatusMap[setId] = MAYALIAS;
00801                 }
00802             }
00803             */
00804 
00805             // conservatively make a set MAYALIAS as soon as a second
00806             // location is being added, checking that this location
00807             // is not already in set
00808             // If size is greater than one, then will have already been 
00809             // set to MAYALIAS.
00810             if (!mIdToLocSetMap[setId]->empty() && 
00811                 mIdToLocSetMap[setId]->find(pLoc)==mIdToLocSetMap[setId]->end()
00812                ) 
00813             {
00814                 mIdToSetStatusMap[setId] = MAYALIAS;
00815                 if (debug) {
00816                   std::cout << "\tmultiple locs within set " << setId 
00817                             << "  becoming MAYALIAS\n";
00818                 } 
00819             }
00820 
00821             // make the set MAYALIAS if pLoc is an InvisibleLoc
00822             if(pLoc->isaInvisible()) {
00823               mIdToSetStatusMap[setId] = MAYALIAS;
00824               if (debug) {
00825                 std::cout << "\npLoc is InvLoc, making set " << setId
00826                           << " MAYALIAS\n";
00827               }
00828             }
00829         }
00830     }
00831 
00832     // insert new location into set
00833     mIdToLocSetMap[setId]->insert(pLoc);
00834 
00835     // only map the location to this set if it is not already mapped
00836     // or if it is mapped to a set with less accuracy
00837     bool moreAccurate = false;
00838     if (mLocToIdMap.find(pLoc)!=mLocToIdMap.end() 
00839         && mIdToSetStatusMap[mLocToIdMap[pLoc]]==MUSTALIAS)
00840     {
00841         moreAccurate = true;
00842     }
00843     if (!moreAccurate) {
00844         mLocToIdMap[pLoc] = setId;
00845     }
00846 }
00847 
00848 OA_ptr<std::map<int,OA_ptr<LocSet> > > AliasMap::getIdToLocSetMap()
00849 {
00850    OA_ptr<std::map<int, OA_ptr<LocSet> > > retVal;
00851    retVal = new std::map<int, OA_ptr<LocSet> >;
00852 
00853    OA_ptr<IdIterator> idIterPtr = getIdIterator();
00854    for ( ; idIterPtr->isValid(); ++(*idIterPtr) ) {
00855      int i = idIterPtr->current();
00856      (*retVal)[i] = new LocSet;
00857      OA_ptr<LocIterator> locIterPtr = getLocIterator(i);
00858      for (; locIterPtr->isValid(); (*locIterPtr)++ ) {
00859        OA_ptr<Location> loc = (locIterPtr->current());
00860        (*retVal)[i]->insert(loc);
00861      }
00862    }
00863 
00864    return retVal;
00865 }
00866 
00876 void AliasMap::aliasLocs(OA_ptr<Location> loc1, OA_ptr<Location> loc2)
00877 {
00878     std::set<int> affectedSets;
00879 
00880     // loop through all the locations set to find all map sets
00881     // that contain the given locations
00882     OA_ptr<IdIterator> idIterPtr = getIdIterator();
00883     for ( ; idIterPtr->isValid(); ++(*idIterPtr) ) {
00884       int i = idIterPtr->current();
00885       if (mIdToLocSetMap[i]->find(loc1)!=mIdToLocSetMap[i]->end()) {
00886           affectedSets.insert(i);
00887       }
00888       if (mIdToLocSetMap[i]->find(loc2)!=mIdToLocSetMap[i]->end()) {
00889           affectedSets.insert(i);
00890       }
00891     }
00892 
00893     // if only one affected set then just put both locations in there,
00894     // means at least one is already in there
00895     if (affectedSets.size()==1) {
00896         std::set<int>::iterator iter;
00897         iter = affectedSets.begin();
00898         addLocation(loc1, *iter);
00899         addLocation(loc2, *iter);
00900     } else {
00901 
00902         // create a new alias map set
00903         int newSetId = makeEmptySet();
00904 
00905         // put all the locations from the affected sets into the new set
00906         // remap all the memory references in the affected sets
00907         std::set<int>::iterator iter;
00908         for (iter=affectedSets.begin(); iter!=affectedSets.end(); iter++) {
00909             OA_ptr<LocIterator> locIterPtr = getLocIterator(*iter);
00910             for (; locIterPtr->isValid(); (*locIterPtr)++ ) {
00911                 OA_ptr<Location> loc = (locIterPtr->current());
00912                 addLocation(loc, newSetId);
00913             }
00914 
00915             remapMemRefs(*iter, newSetId);
00916         }
00917 
00918         // make sure both locations are in new set
00919         addLocation(loc1, newSetId);
00920         addLocation(loc2, newSetId);
00921     }
00922 
00923  }
00924 
00931 void AliasMap::remapMemRefs(int oldSetId, int newSetId)
00932 {
00934     // first the MemRefHandles
00935     MemRefSet::iterator mrIter;
00936     for (mrIter = mIdToMemRefSetMap[oldSetId].begin();
00937          mrIter != mIdToMemRefSetMap[oldSetId].end(); mrIter++ )
00938     {
00939         mMemRefToIdMap[*mrIter].erase(oldSetId);
00940         mMemRefToIdMap[*mrIter].insert(newSetId);
00941         mIdToMemRefSetMap[newSetId].insert(*mrIter);
00942     }
00943     // then the MemRefExprs
00944     MemRefExprSet::iterator mreIter;
00945     for (mreIter = mIdToMRESetMap[oldSetId].begin();
00946          mreIter != mIdToMRESetMap[oldSetId].end(); mreIter++ )
00947     {
00948         mMREToIdMap[*mreIter] = newSetId;
00949         mIdToMRESetMap[newSetId].insert(*mreIter);
00950     }
00951 
00953     int lastSetId = mStartId+mNumSets-1;
00954 
00955     if ( lastSetId != oldSetId ) {
00956        // move  the MemRefHandles
00957        mIdToMemRefSetMap[oldSetId] = mIdToMemRefSetMap[lastSetId];
00958        for (mrIter = mIdToMemRefSetMap[oldSetId].begin();
00959             mrIter != mIdToMemRefSetMap[oldSetId].end(); mrIter++ )
00960        {
00961            mMemRefToIdMap[*mrIter].erase(lastSetId);
00962            mMemRefToIdMap[*mrIter].insert(oldSetId);
00963        }
00964 
00965        // move  the MemRefExprs
00966        mIdToMRESetMap[oldSetId] = mIdToMRESetMap[lastSetId];
00967        for (mreIter = mIdToMRESetMap[oldSetId].begin();
00968             mreIter != mIdToMRESetMap[oldSetId].end(); mreIter++ )
00969        {
00970            mMREToIdMap[*mreIter] = oldSetId;
00971        }
00972 
00973        mIdToSetStatusMap[oldSetId] = mIdToSetStatusMap[lastSetId];
00974     }
00975 
00976     // any locations which point to the oldSetId should now
00977     // reference the newSetId
00978     LocSetIterator oldLocIter(mIdToLocSetMap[oldSetId]);
00979     for (; oldLocIter.isValid(); ++oldLocIter) {
00980         OA_ptr<Location> loc = oldLocIter.current();
00981         mLocToIdMap[loc] = newSetId;
00982     }
00983 
00984     if ( lastSetId != oldSetId ) {
00985        // any locations which point to the lastSetId should now
00986        // reference the oldSetId. 
00987        // NB:  this loop can not proceed the previous.
00988       if ( !mIdToLocSetMap[lastSetId].ptrEqual(0)) {
00989         LocSetIterator lastLocIter(mIdToLocSetMap[lastSetId]);
00990         for (; lastLocIter.isValid(); ++lastLocIter) {
00991           OA_ptr<Location> loc = lastLocIter.current();
00992           mLocToIdMap[loc] = oldSetId;
00993         }
00994       }
00995     }
00996 
00997     // remove the lastSetId from the various maps; it has been
00998     // moved to oldSetId.
00999     mIdToLocSetMap.erase(lastSetId);
01000     mIdToSetStatusMap.erase(lastSetId);
01001     mIdToMemRefSetMap.erase(lastSetId);
01002     mIdToMRESetMap.erase(lastSetId);
01003 
01004     // reduce the number of sets
01005     mNumSets--;
01006 }
01007 
01010 void AliasMap::mapToUnknown(int setId)
01011 {
01012     // remove any locations which point to the setId.
01013     LocSetIterator oldLocIter(mIdToLocSetMap[setId]);
01014     for (; oldLocIter.isValid(); ++oldLocIter) {
01015         OA_ptr<Location> loc = oldLocIter.current();
01016         mLocToIdMap.erase(loc);
01017     }
01018 
01019     // Remove any locations to which setId pointed.
01020     mIdToLocSetMap.erase(setId);
01021 
01022     // And establish that it only points to one location, UnknownLoc.
01023     OA_ptr<Location> loc;
01024     loc = dynamic_cast<Location*>(new UnknownLoc());
01025     mIdToLocSetMap[setId] = new LocSet;
01026     mIdToLocSetMap[setId]->insert( loc );
01027 
01028     // Change the status of the map to MAYALIAS, as appropriate for unknown.
01029     mIdToSetStatusMap[setId] = MAYALIAS;
01030 }
01031 
01035 void AliasMap::mapMemRefToMapSet(MemRefHandle ref, int setId)
01036 {
01037     mMemRefToIdMap[ref].insert(setId);
01038     mIdToMemRefSetMap[setId].insert(ref);
01039 }
01040 
01044 void AliasMap::mapMemRefToMapSet(OA_ptr<MemRefExpr> ref, int setId)
01045 {
01046     if (!ref.ptrEqual(0)) { 
01047         mMREToIdMap[ref] = setId; 
01048         mIdToMRESetMap[setId].insert(ref);
01049     }
01050 }
01051 
01053 void AliasMap::removeInvisibleLocs(int setId)
01054 {
01055     // check that a location set exists for this setId
01056     if (!mIdToLocSetMap[setId].ptrEqual(0)) {
01057        
01058         // also if there are other locations in the set already 
01059         // and they don't all fully overlap with this location
01060         std::set<OA_ptr<Location> > removeSet;
01061         LocSetIterator locIter(mIdToLocSetMap[setId]);
01062         for (; locIter.isValid(); ++locIter) {
01063             OA_ptr<Location> loc = locIter.current();
01064             if (loc->isaInvisible()) {
01065                 removeSet.insert(loc);
01066             }
01067         }
01068 
01069         for (std::set<OA_ptr<Location> >::iterator setIter = removeSet.begin();
01070              setIter!=removeSet.end(); setIter++ )
01071         {
01072             mIdToLocSetMap[setId]->erase(*setIter);
01073         }
01074     }
01075 }
01076 
01077 /*********************************************************************/
01078 
01081 void AliasMap::removeInvisibleLocs(int setId, OA_ptr<MemRefExpr> inv_memref)
01082 {
01083      std::set<OA_ptr<Location> > removeSet;
01084      LocSetIterator locIter(mIdToLocSetMap[setId]);
01085 
01086      if (debug) { 
01087          std::cout << "AliasMap::removeInvisibleLocs" << std::endl;
01088          std::cout << "\tmIdToLocSetMap[" << setId << "]->size() = "
01089                    << mIdToLocSetMap[setId]->size() << std::endl;
01090      }
01091      
01092      for (; locIter.isValid(); ++locIter) {
01093           OA_ptr<Location> loc = locIter.current(); 
01094           if (loc->isaInvisible()) {
01095                OA_ptr<InvisibleLoc>  temp_inv_loc 
01096                          = loc.convert<InvisibleLoc>();
01097 
01098                OA_ptr<MemRefExpr> loc_memref 
01099                          = temp_inv_loc->getMemRefExpr();
01100 
01101                if(inv_memref == loc_memref) {
01102                    removeSet.insert(loc);
01103                }
01104           }
01105      }
01106      if (debug) {
01107          std::cout << "\tremoveSet.size() = " << removeSet.size() << std::endl;
01108      }
01109 
01110      for (std::set<OA_ptr<Location> >::iterator setIter = removeSet.begin();
01111              setIter!=removeSet.end(); setIter++ )
01112      {
01113         OA_ptr<Location> loc = *setIter;
01114         if (debug) {
01115             std::cout << "\terasing invisible loc = " << std::endl;
01116             loc->dump(std::cout);
01117         }
01118         mIdToLocSetMap[setId]->erase(*setIter);
01119      }
01120 }
01121 
01122 
01123 void AliasMap::removeBaseLoc(OA_ptr<Location> baseLoc, int setId)
01124 {
01125      std::set<OA_ptr<Location> > removeSet;
01126      LocSetIterator locIter(mIdToLocSetMap[setId]);
01127 
01128      for (; locIter.isValid(); ++locIter) {
01129           OA_ptr<Location> loc = locIter.current();
01130           if( loc == baseLoc ) {
01131               removeSet.insert(loc);
01132           }     
01133      }
01134  
01135      for (std::set<OA_ptr<Location> >::iterator setIter = removeSet.begin();
01136               setIter!=removeSet.end(); setIter++ )
01137      {
01138             mIdToLocSetMap[setId]->erase(*setIter);
01139      }
01140 }
01141 
01142 
01145 bool AliasMap::isPartial(int setId, OA_ptr<MemRefExpr> inv_memref)
01146 {
01147      LocSetIterator locIter(mIdToLocSetMap[setId]);
01148      for (; locIter.isValid(); ++locIter) {
01149             OA_ptr<Location> loc = locIter.current();
01150             if (loc->isaInvisible()) {
01151                 OA_ptr<InvisibleLoc>  temp_inv_loc =
01152                              loc.convert<InvisibleLoc>();
01153                 OA_ptr<MemRefExpr> loc_memref = 
01154                              temp_inv_loc->getMemRefExpr();
01155                 if(inv_memref == loc_memref) {
01156                    return true;
01157                 }
01158              }
01159      }
01160      return false;
01161 }
01162 
01163 /***********************************************************************/
01164 
01165 
01168 void AliasMap::dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir)
01169 {
01170     // print locations for each set, ID : { locs }
01171     os << "============= AliasMap ============" << std::endl;
01172     OA_ptr<IdIterator> idIterPtr = getIdIterator();
01173     for ( ; idIterPtr->isValid(); ++(*idIterPtr) ) {
01174       int i = idIterPtr->current();
01175       os << "AliasMapSet[" << i << "] = { ";
01176       OA_ptr<LocIterator> locIterPtr = getLocIterator(i);
01177       for (; locIterPtr->isValid(); (*locIterPtr)++ ) {
01178         OA_ptr<Location> loc = (locIterPtr->current());
01179         os << ", ";
01180         os << "<";
01181         loc->dump(os,ir);
01182         os << "> ";
01183       }
01184       os << " }" << std::endl;
01185     }
01186 
01187     // print all memrefs and their mapping
01188     os << "MemRef mapping to AliasMap sets:" << std::endl;
01189     OA_ptr<MemRefIterator> memIterPtr = getMemRefIter();
01190     for (; memIterPtr->isValid(); (*memIterPtr)++) {
01191       MemRefHandle memref = memIterPtr->current();
01192       os << "(" << memref.hval() << ") " << ir->toString(memref);
01193       os << " --> [";
01194 
01195       OA_ptr<std::set<int> > ids = getMapSetIds(memref);
01196       std::set<int>::iterator it;
01197       for(it = ids->begin(); it != ids->end(); ++it) {
01198         os << *it << " ";
01199       }
01200       os << "]" << std::endl;
01201 
01202     }
01203  
01204 }
01205 
01206 
01207   } // end of Alias namespace
01208 } // end of OA namespace

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