ReachConstsStandard.cpp

Go to the documentation of this file.
00001 
00016 #include "ReachConstsStandard.hpp"
00017 #include <Utils/Util.hpp>
00018 
00019 namespace OA {
00020   namespace ReachConsts {
00021 
00022 static bool debug = false;
00023 
00024 //------------------------------------------------------------------
00025 // ConstDef methods
00026 //------------------------------------------------------------------
00027 
00031 ConstDef::ConstDef(OA_ptr<Location> locP, 
00032                    OA_ptr<ConstValBasicInterface> cvbP,
00033                    ConstDefType cdType) 
00034 {
00035   OA_DEBUG_CTRL_MACRO("DEBUG_ReachConstsStandard:ALL", debug);
00036   mLocPtr = locP;
00037   mConstPtr = cvbP; 
00038   mCDType = cdType;
00039 }
00040 
00043 ConstDef::ConstDef(OA_ptr<Location> locP, 
00044                    OA_ptr<ConstValBasicInterface> cvbP) 
00045 {
00046   mLocPtr = locP;
00047   mConstPtr = cvbP; 
00048   if (cvbP.ptrEqual(0)) { 
00049     mCDType = TOP; 
00050   } else { 
00051     mCDType = VALUE;
00052   }
00053 }
00054 
00058 ConstDef::ConstDef(OA_ptr<Location> locP, ConstDefType cdType) {
00059 
00060   assert(cdType!=VALUE);
00061 
00062   mLocPtr = locP;
00063   mCDType = cdType; // only TOP or BOTTOM make sense in this constructor
00064   mConstPtr = 0;
00065 }
00066 
00069 ConstDef::ConstDef(OA_ptr<Location> locP) {
00070   mLocPtr = locP; 
00071   mConstPtr = 0; 
00072   mCDType = TOP; // assume TOP
00073 }
00074 
00077 ConstDef& ConstDef::operator=(const ConstDef& other) {
00078   mLocPtr = other.getLocPtr();
00079   mConstPtr = other.getConstPtr();
00080   mCDType = other.getConstDefType();
00081   return *this;
00082 }
00083 
00085 bool ConstDef::operator== (const ConstDef &other) const {
00086   //if (debug) { std::cout << "In ConstDef::operator==" << std::endl; }
00087   if (mLocPtr==other.getLocPtr()) { 
00088       return true;
00089   } else {
00090       return false;
00091   }
00092 }
00093 
00096 bool ConstDef::operator< (const ConstDef &other) const 
00097 { 
00098   if (mLocPtr<other.getLocPtr()) { 
00099       return true; 
00100   } else {
00101       return false;
00102   }
00103 }
00104  
00109 bool ConstDef::equiv(const ConstDef& other){
00110   if (mLocPtr==other.getLocPtr()) {
00111     if (mCDType == other.getConstDefType()) {
00112       switch (mCDType) {
00113       case TOP: case BOTTOM: return true; break;
00114       case VALUE:
00115         if (mConstPtr.ptrEqual(0)&&other.getConstPtr().ptrEqual(0)) 
00116         { return true; }
00117         else if (!mConstPtr.ptrEqual(0)&&!other.getConstPtr().ptrEqual(0)) {
00118           if (mConstPtr==other.getConstPtr()) { return true; }
00119         }
00120         break;
00121       }
00122     }
00123   }
00124   return false;
00125 }
00126 
00127 /* this uses equivalence semantics
00128 bool ConstDef::operator< (const ConstDef &other) const 
00129 { 
00130   if (*this == other) {
00131       return false;
00132   }
00133   if (mLocPtr<other.getLocPtr()) { 
00134       return true; 
00135   } else if (other.getLocPtr()<mLocPtr) {
00136       return false;
00137   } else {// loc's are equal 
00138     if (mCDType==other.getConstDefType()) {
00139       switch (mCDType) {
00140       case TOP: 
00141       case BOTTOM: return false; break; // these are equal
00142       case VALUE:
00143         assert(!mConstPtr.ptrEqual(0) && !other.getConstPtr().ptrEqual(0)); 
00144         // if locs are equal and types are both VALUE, then compare
00145         // based upon the string representation ??? 
00146         // (only have == and != for ConstValBasicInterface) 
00147         return ((mConstPtr->toString())<((other.getConstPtr())->toString()));
00148         break;
00149       }
00150     } else { // types are not equal
00151       // use enumeration values of ConstDefType to make a decision
00152       return (mCDType < other.getConstDefType());
00153     }
00154   }
00155   return false; // shouldn't get there
00156 }
00157 */
00158 
00159 
00161 // a ConstDef.
00162 std::string ConstDef::toString(OA_ptr<IRHandlesIRInterface> pIR) 
00163 {
00164   std::ostringstream oss;
00165   oss << "<";
00166   oss << (*mLocPtr).toString(pIR);
00167   switch (mCDType) {
00168   case TOP: 
00169     oss << ",TOP>"; break;
00170   case BOTTOM: 
00171     oss << ",BOTTOM>"; break;
00172   case VALUE:
00173     oss << ",VALUE=" << (*mConstPtr).toString() << ">"; break;
00174   }
00175   return oss.str();
00176 }
00177 
00178 //------------------------------------------------------------------
00179 // ConstDefSet methods
00180 //------------------------------------------------------------------
00181 
00182 void ConstDefSet::replace(OA_ptr<ConstDef> cd)
00183 {
00184     replace(cd->getLocPtr(), cd->getConstPtr(), cd->getConstDefType());
00185 }
00186 
00189 void ConstDefSet::replace(OA_ptr<Location> locPtr,
00190                           OA_ptr<ConstValBasicInterface> constPtr,
00191                           ConstDefType cdType)
00192 {
00193   if (debug) {
00194     int r,i;
00195     OA_ptr<ConstDef> cd;
00196     cd = new ConstDef(locPtr);
00197     r = removeANDtell(cd);
00198     cd = new ConstDef(locPtr,constPtr,cdType);
00199     i = insertANDtell(cd);
00200     std::cout << "ConstDefSet.replace: (removed " << r << ") "
00201               << "(inserted " << i << " "
00202       //          << (ConstDef(locPtr,constPtr,cdType)).toString(mIR) 
00203               << ")" << std::endl;
00204   } else {
00205     OA_ptr<ConstDef> cd; 
00206     cd = new ConstDef(locPtr); 
00207     remove(cd);
00208     cd = new ConstDef(locPtr,constPtr,cdType);
00209     insert(cd);
00210   }
00211 }
00212 
00213 
00215 // underlying sets, because the == operator of an element of a ConstDefSet, 
00216 // namely a ConstDef, only considers the contents of the location pointer
00217 // and not any of the other fields.  So, need to use ConstDef's equal() method
00218 // here instead.
00219 bool ConstDefSet::operator==(DataFlow::DataFlowSet &other) const
00220 {
00221 
00222   // first dynamic cast to a ConstDefSet, if that doesn't work then 
00223   // other is a different kind of DataFlowSet and *this is not equal
00224   ConstDefSet& recastOther = dynamic_cast<ConstDefSet&>(other);
00225     /*
00226   try {
00227   } catch (?) {
00228   }
00229   */
00230 
00231   if (mSet->size() != recastOther.mSet->size()) {
00232     return false;
00233   }
00234 
00235   // FIXME: assumes that they both don't have all possible locations
00236   if (mDefaultType!=recastOther.mDefaultType) {
00237       return false;
00238   }
00239 
00240   // same size:  for every element in mSet, find the element in other.mSet
00241   std::set<OA_ptr<ConstDef> >::iterator set1Iter;
00242   for (set1Iter = mSet->begin(); set1Iter!=mSet->end(); ++set1Iter) {
00243     OA_ptr<ConstDef> cd1 = *set1Iter;
00244     std::set<OA_ptr<ConstDef> >::iterator set2Iter;
00245     set2Iter = recastOther.mSet->find(cd1);
00246 
00247     if (set2Iter == recastOther.mSet->end()) {
00248       return (false); // cd1 not found
00249     } else {
00250       OA_ptr<ConstDef> cd2 = *set2Iter;
00251       if (!(cd1->equiv(*cd2))) { // use full equiv operator
00252         return (false); // cd1 not equiv to cd2
00253       }
00254     }
00255   } // end of set1Iter loog
00256 
00257   // hopefully, if we got here, all elements of set1 equiv set2
00258   return(true);
00259 
00260 }
00261 
00264 OA_ptr<ConstDef> ConstDefSet::find(OA_ptr<Location> locPtr) const
00265 {
00266   OA_ptr<ConstDef> retval; retval = NULL;
00267   
00268   OA_ptr<ConstValBasicInterface> nullVal; nullVal = 0;
00269   OA_ptr<ConstDef> findCD; findCD = new ConstDef(locPtr,nullVal,TOP);
00270 
00271   std::set<OA_ptr<ConstDef> >::iterator cdIter = mSet->find(findCD);
00272   if (cdIter != mSet->end()) {
00273     retval = *cdIter;
00274   }
00275   return retval;
00276 }
00277 
00279 std::string ConstDefSet::toString(OA_ptr<IRHandlesIRInterface> pIR){
00280   std::ostringstream oss;
00281   oss << "{";
00282   
00283   oss << "default = ";
00284   switch (mDefaultType) {
00285       case TOP:
00286           oss << "TOP,";
00287           break;
00288       case BOTTOM:
00289           oss << "BOTTOM,";
00290           break;
00291       default:
00292           assert(0);
00293           break;
00294   }
00295   // iterate over ConstDef's and have the IR print them out
00296   ConstDefSetIterator iter(*this);
00297   //std::set<OA_ptr<ConstDef> >::iterator iter;
00298   OA_ptr<ConstDef> cd;
00299   
00300   // first one
00301   if (iter.isValid()) {
00302     cd = iter.current();
00303     oss << cd->toString(pIR);
00304     ++iter;
00305   }
00306   
00307   // rest
00308   for (; iter.isValid(); ++iter) {
00309     cd = iter.current();
00310     oss << ", " << cd->toString(pIR); 
00311   }
00312   
00313   oss << "}";
00314   return oss.str();
00315 }
00316 
00317 
00318 //------------------------------------------------------------------
00319 // ReachConstsStandard methods
00320 //------------------------------------------------------------------
00321 
00324 void ReachConstsStandard::dump(std::ostream& os, 
00325                              OA_ptr<OA::IRHandlesIRInterface> ir)
00326 {
00327     std::map<StmtHandle, OA_ptr<ConstDefSet> >::iterator mapIter;
00328     for (mapIter = mReachConsts.begin(); mapIter != mReachConsts.end(); mapIter++) {
00329         StmtHandle s = mapIter->first;
00330 
00331         os << "StmtHandle(" << s.hval() << ") " << ir->toString(s) 
00332            << std::endl << "\treachConsts: " << std::endl;
00333         // iterate over reaching consts for statement s
00334 
00335         OA_ptr<ConstDefSet> cdSet = getReachConsts(s);
00336         cdSet->dump(os,ir);
00337 
00338         os << std::endl;
00339 
00340     os << "Mapping of MemRefHandles to constants" << std::endl;
00341     std::map<MemRefHandle,OA_ptr<ConstValBasicInterface> >::iterator mIter;
00342     for (mIter=mMemRef2ReachConst.begin(); mIter!=mMemRef2ReachConst.end();
00343          mIter++)
00344     {
00345         os << "\t" << ir->toString(mIter->first) << " --> ";
00346         OA_ptr<ConstValBasicInterface> constPtr;
00347         constPtr = mIter->second;
00348         if (constPtr.ptrEqual(0)) {
00349             os << "no constant value" << std::endl;
00350         } else {
00351             os << constPtr->toString() << std::endl;
00352         }
00353     }
00354 
00355         /*
00356         ConstDefSetIterator* sSetIter = getConstDefIterator(s); //should be one per stmt
00357         for ( ; (bool)(*sSetIter); ++(*sSetIter))
00358         {
00359           ConstDef cd = *sSetIter;
00360           Location* l = cd.getLocPtr();
00361           ConstValBasicInterface * cvb = cd.getConstPtr();
00362 
00363           os << "\t" << "<";
00364           l->dump(os,ir);
00365           os << "," << cvb->toString() << ">";
00366           os << std::endl;
00367         }
00368         */
00369     }
00370 }
00371 
00372 
00373 
00374 void ConstDef::output(OA::IRHandlesIRInterface &ir)
00375 {
00376     sOutBuild->objStart("ConstDef");
00377 
00378     sOutBuild->fieldStart("Location");
00379     mLocPtr->output(ir);
00380     sOutBuild->fieldEnd("Location");
00381 
00382   // mCDType.output(ir);
00383 
00384     sOutBuild->fieldStart("mCDType");
00385     std::ostringstream oss;
00386     switch (mCDType) {
00387       case TOP:
00388          oss << ",TOP>"; break;
00389       case BOTTOM:
00390          oss << ",BOTTOM>"; break;
00391       case VALUE:
00392          oss << ",VALUE=" << (*mConstPtr).toString() << ">"; break;
00393      }
00394     sOutBuild->fieldEnd("mCDType");
00395      // return oss.str();
00396 
00397     sOutBuild->outputString( oss.str());
00398 
00399     sOutBuild->objEnd("ConstDef");
00400 }
00401 
00402 
00403 
00404 void ConstDefSet::output(OA::IRHandlesIRInterface &ir)
00405 {
00406     sOutBuild->objStart("ConstDefSet");
00407 
00408     sOutBuild->listStart();
00409     std::set<OA::OA_ptr<ConstDef> >::iterator reg_mSet_iterator;
00410     for(reg_mSet_iterator  = mSet->begin();
00411         reg_mSet_iterator != mSet->end();
00412         reg_mSet_iterator++)
00413     {
00414         OA::OA_ptr<ConstDef>  item = *reg_mSet_iterator;
00415         sOutBuild->listItemStart();
00416         sOutBuild->fieldStart("ConstDef");
00417         item->output(ir);
00418         sOutBuild->fieldEnd("ConstDef");
00419         sOutBuild->listItemEnd();
00420     }
00421     sOutBuild->listEnd();
00422     sOutBuild->objEnd("ConstDefSet");
00423 }
00424 
00425 
00426 
00427 
00428 void ReachConstsStandard::output(OA::IRHandlesIRInterface &ir)
00429 {
00430     sOutBuild->objStart("ReachConstsStandard");
00431 
00432     sOutBuild->mapStart("mReachConsts", "StmtHandle", "OA::OA_ptr<ConstDefSet> ");
00433     std::map<StmtHandle, OA::OA_ptr<ConstDefSet> >::iterator reg_mReachConsts_iterator;
00434     for(reg_mReachConsts_iterator = mReachConsts.begin();
00435         reg_mReachConsts_iterator != mReachConsts.end();
00436         reg_mReachConsts_iterator++)
00437     {
00438         const StmtHandle &first = reg_mReachConsts_iterator->first;
00439         OA::OA_ptr<ConstDefSet>  &second = reg_mReachConsts_iterator->second;
00440         sOutBuild->mapEntryStart();
00441         sOutBuild->mapKeyStart();
00442         sOutBuild->fieldStart("Statement");
00443         sOutBuild->outputIRHandle(first, ir);
00444         sOutBuild->fieldEnd("Statement");
00445 
00446         sOutBuild->mapKeyEnd();
00447         sOutBuild->mapValueStart();
00448         second->output(ir);
00449         sOutBuild->mapValueEnd();
00450         sOutBuild->mapEntryEnd();
00451     }
00452     sOutBuild->mapEnd("mReachConsts");
00453 
00454     sOutBuild->objEnd("ReachConstsStandard");
00455 }
00456 
00457 
00458 
00459 
00460 
00461 
00467 std::string ReachConstsStandard::getMemRefConstInfo()
00468 {
00469   std::ostringstream oss;
00470   int totalMemRef = 0, constMemRef = 0;
00471 
00472   std::map<MemRefHandle,OA_ptr<ConstValBasicInterface> >::iterator mIter;
00473   for (mIter=mMemRef2ReachConst.begin(); mIter!=mMemRef2ReachConst.end(); mIter++) {
00474     totalMemRef++;
00475     if (! mIter->second.ptrEqual(NULL)) {
00476       constMemRef++;
00477     }
00478   }
00479 
00480   oss << totalMemRef << "\t" << constMemRef;
00481   
00482   return oss.str();
00483 }
00484 
00485 
00486 
00487   } // end of ReachConsts namespace
00488 } // end of OA namespace

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