ManagerReachConstsStandard.cpp

Go to the documentation of this file.
00001 
00017 #include "ManagerReachConstsStandard.hpp"
00018 #include <Utils/Util.hpp>
00019 
00020 
00021 namespace OA {
00022   namespace ReachConsts {
00023 
00024 #if defined(DEBUG_ALL) || defined(DEBUG_ManagerReachConsts)
00025 static bool debug = true;
00026 static bool extra_debug = true;
00027 static bool meet_debug = true;
00028 static bool transfer_debug = true;
00029 #else
00030 static bool debug = false;
00031 static bool extra_debug = false;
00032 static bool meet_debug = false;
00033 static bool transfer_debug = false;
00034 #endif
00035 
00038 ManagerReachConstsStandard::ManagerReachConstsStandard(OA_ptr<ReachConstsIRInterface> _ir) 
00039     : mIR(_ir) 
00040 {
00041     OA_DEBUG_CTRL_MACRO("DEBUG_ManagerReachConstsStandard:ALL", debug);
00042     mSolver = new DataFlow::CFGDFSolver(DataFlow::CFGDFSolver::Forward,*this);
00043 }
00044 
00045 void ManagerReachConstsStandard::initializeTopAndBottom() 
00046 {
00047     if (debug) { 
00048         std::cout << "In ManagerReachConsts::initializeTopAndBottom";
00049         std::cout << std::endl;
00050     }
00051     mAllTop = new ConstDefSet;
00052     mAllBottom = new ConstDefSet;
00053     // Initialize mAllTop and mAllBottom
00054     // For each statement:
00055     OA_ptr<OA::IRStmtIterator> sIt = mIR->getStmtIterator(mProc);
00056     for ( ; sIt->isValid(); (*sIt)++) {
00057       OA::StmtHandle stmt = sIt->current();
00058     
00059       // for each mem ref in this statement
00060       OA_ptr<MemRefHandleIterator> refIterPtr = mIR->getAllMemRefs(stmt);
00061       for (; refIterPtr->isValid(); (*refIterPtr)++) {
00062           MemRefHandle ref = refIterPtr->current();
00063 
00064           // get all may locations for this mem ref, must instead?
00065           OA_ptr<LocIterator> locIterPtr = mAlias->getMustLocs(ref);
00066           for (; locIterPtr->isValid(); ++(*locIterPtr)) {
00067             OA_ptr<Location> loc = locIterPtr->current();
00068             if (debug) { 
00069                 std::cout << "\tMayLoc = ";
00070                 loc->dump(std::cout, mIR);
00071             }
00072           
00073             // create an entry in mAllTop and mAllBottom for this location
00074             OA_ptr<ConstValBasicInterface> nullVal; nullVal = 0; 
00075             OA_ptr<ConstDef> cd; cd = new ConstDef(loc,nullVal,TOP);
00076             if (debug) { 
00077                 std::cout << "\tBefore insert into mAllTop" << std::endl;
00078                 mAllTop->dump(std::cout, mIR);
00079             }
00080             mAllTop->insert(cd);
00081             if (debug) { 
00082                 std::cout << "\tAfter insert into mAllTop" << std::endl;
00083                 mAllTop->dump(std::cout, mIR);
00084             }
00085             cd = new ConstDef(loc,nullVal,BOTTOM);
00086             mAllBottom->insert(cd);
00087           }
00088       }
00089       
00090       // must or may defines from procedure calls?
00091       // NO, until get interprocedural ReachConsts working
00092       // won't get any constant definitions from procedure calls
00093       // just kills of existing constant definitions
00094     }
00095 }
00096 
00097 OA_ptr<DataFlow::DataFlowSet> ManagerReachConstsStandard::initializeTop()
00098 {
00099     if (mAllTop.ptrEqual(NULL)) {
00100       initializeTopAndBottom();
00101     }
00102     if (debug) { 
00103         std::cout << "mAllTop = ";
00104         mAllTop->dump(std::cout,mIR);
00105     }
00106     return mAllTop;
00107 }
00108 
00109 OA_ptr<DataFlow::DataFlowSet> ManagerReachConstsStandard::initializeBottom()
00110 {
00111     if (mAllBottom.ptrEqual(NULL)) {
00112       initializeTopAndBottom();
00113     }
00114     if (debug) { 
00115         std::cout << "mAllBottom = ";
00116         mAllBottom->dump(std::cout,mIR);
00117     }
00118     return mAllBottom;
00119 }
00120 
00126 OA_ptr<ReachConstsStandard> ManagerReachConstsStandard::performAnalysis(ProcHandle proc, 
00127     OA_ptr<CFG::CFGInterface> cfg, OA_ptr<Alias::Interface> alias,
00128     OA_ptr<SideEffect::InterSideEffectInterface> interSE,
00129     DataFlow::DFPImplement algorithm)
00130 {
00131   mProc = proc;
00132 
00133   // store a new (waiting to be filled) ReachConstsStandard as member
00134   mRCS = new ReachConstsStandard(proc);
00135 
00136   // store Alias information for use within the transfer function
00137   mAlias = alias;
00138 
00139   // store CFG for use within initializeNode function
00140   mCFG = cfg;
00141 
00142   // store side-effect information for use within transfer function
00143   mInterSE = interSE;
00144 
00145   // use the dataflow solver to get 
00146   //    - the In and Out sets for the BBs
00147   //    - the relationship of memref->reachingConstant (set inside solve)
00148   //DataFlow::CFGDFProblem::solve(cfg);
00149   mSolver->solve(cfg,algorithm);
00150 
00151   
00152 /*  OA_ptr<CFG::CFGInterface::NodesIterator> nIter
00153              = cfg->getNodesIterator();
00154  
00155   for ( ; nIter->isValid(); ++(*nIter)) {
00156  
00157         OA_ptr<CFG::CFGInterface::Node> node = nIter->current();
00158         OA_ptr<DataFlow::DataFlowSet> dfset = mSolver->getInSet(node); */
00159 
00160   // iterate over BBs:  using In sets and transfer function, 
00161   //    - create relationship of stmt->reachingConstDefSet
00162  /* std::map<OA_ptr<CFG::CFGInterface::Node>,
00163            OA_ptr<DataFlow::DataFlowSet> >::iterator mapIter;
00164   for (mapIter=mSolver->mNodeInSetMap.begin(); mapIter!=mSolver->mNodeInSetMap.end();
00165        mapIter++)
00166   {
00167       // CFG node
00168       OA_ptr<CFG::CFGInterface::Node> node = mapIter->first;
00169 
00170       // In set
00171       OA_ptr<DataFlow::DataFlowSet> dfset = mapIter->second;
00172 
00173 
00174       
00175       OA_ptr<ReachConsts::ConstDefSet> inSet 
00176           = dfset.convert<ReachConsts::ConstDefSet>();
00177       if (debug) { 
00178         std::cout << "ManReachConstsStd::perfAnal "
00179                   << "Node #" << node->getId() << ":" << std::endl;
00180         std::cout << "    inSet = " ;
00181         inSet->dump(std::cout,mIR); 
00182       }
00183       
00184       // get a copy of the IN set for BB
00185       OA_ptr<DataFlow::DataFlowSet> tempbase = inSet->clone();
00186       OA_ptr<ReachConsts::ConstDefSet> temp 
00187           = tempbase.convert<ReachConsts::ConstDefSet>();
00188       //OA_ptr<ReachConsts::ConstDefSet> temp = inSet->clone();
00189 
00190       // for each BB, iterate over the statements and find the 
00191       // ConstDefs for the statement
00192       OA_ptr<CFG::CFGInterface::NodeStatementsIterator> stmtIterPtr 
00193           = node->getNodeStatementsIterator();
00194       for (; stmtIterPtr->isValid(); ++(*stmtIterPtr)) { 
00195         StmtHandle s = stmtIterPtr->current();
00196 
00197         ConstDefSetIterator cdIter(*(temp.convert<ConstDefSet>()));
00198         for (; cdIter.isValid(); ++(cdIter)) {
00199           OA_ptr<ConstDef> constDef = cdIter.current();
00200           mRCS->insertConstDef(s,constDef);
00201         }
00202 
00203         // What is the use of temp ??? 
00204         // apply transfer function to temp set
00205         OA_ptr<DataFlow::DataFlowSet> tempdfset = transfer(temp,s);
00206         temp = tempdfset.convert<ReachConsts::ConstDefSet>();
00207 
00208         if (debug) { 
00209           std::cout << "ManReachConstsStd::perfAnal: after stmt "
00210                     << mIR->toString(s) << std::endl;
00211           std::cout << "    temp = ";
00212           temp->dump(std::cout,mIR); 
00213         } 
00214       }
00215   } */    
00216   return mRCS;
00217 }
00218 
00219 //------------------------------------------------------------------
00220 // Implementing the callbacks for CFGDFProblem
00221 //------------------------------------------------------------------
00222 /*void ManagerReachConstsStandard::initializeNode(OA_ptr<CFG::CFGInterface::Node> n)
00223 {
00224   if (n.ptrEqual(mCFG->getEntry())) {
00225     mNodeInSetMap[n]=mAllBottom->clone(); // from ManagerReachConstsStandard
00226     mNodeOutSetMap[n]=mAllBottom->clone();
00227   } else {
00228     mNodeInSetMap[n] = new ConstDefSet;
00229     mNodeOutSetMap[n]=mAllTop->clone(); // from ManagerReachConstsStandard
00230   }
00231 }*/
00232 
00233 
00234 OA_ptr<DataFlow::DataFlowSet>
00235 ManagerReachConstsStandard::initializeNodeIN(OA_ptr<CFG::NodeInterface> n)
00236 {
00237   if (mAllTop.ptrEqual(NULL)) {
00238     initializeTopAndBottom();
00239   }
00240   if (n.ptrEqual(mCFG->getEntry())) {
00241     return mAllBottom->clone();
00242   }
00243   OA_ptr<ConstDefSet> retval;
00244   retval = new ConstDefSet();
00245   return retval;
00246 }
00247 
00248 OA_ptr<DataFlow::DataFlowSet>
00249 ManagerReachConstsStandard::initializeNodeOUT(OA_ptr<CFG::NodeInterface> n)
00250 {
00251   if (mAllTop.ptrEqual(NULL)) {
00252     initializeTopAndBottom();
00253   }
00254   if (n.ptrEqual(mCFG->getEntry())) {
00255     return mAllBottom->clone();
00256   }
00257   return mAllTop->clone();
00258 }
00259 
00260 /*******************  First Attempt, didn't quite work
00261 OA_ptr<DataFlow::DataFlowSet>
00262 ManagerReachConstsStandard::initializeNodeIN(OA_ptr<CFG::CFGInterface::Node> n)
00263 {
00264      if (mAllTop.ptrEqual(NULL)) {
00265           initializeTopAndBottom();
00266      }
00267      if (debug) {
00268           std::cout << "mAllTop = ";
00269           mAllTop->dump(std::cout,mIR);
00270      }
00271      return mAllTop;
00272 }
00273 
00274 OA_ptr<DataFlow::DataFlowSet>
00275 ManagerReachConstsStandard::initializeNodeOUT(OA_ptr<CFG::CFGInterface::Node> n)
00276 {
00277      if (mAllTop.ptrEqual(NULL)) {
00278            initializeTopAndBottom();
00279      }
00280      if (debug) {
00281            std::cout << "mAllTop = ";
00282            mAllTop->dump(std::cout,mIR);
00283      }
00284      return mAllTop;
00285 }
00286 ***********************/
00287 
00289 OA_ptr<DataFlow::DataFlowSet> 
00290 ManagerReachConstsStandard::meet (OA_ptr<DataFlow::DataFlowSet> set1orig, 
00291                        OA_ptr<DataFlow::DataFlowSet> set2orig)
00292 {
00293   // can change set1orig if wanted:  Usage in CFGDFProblem::atDGraphNode()
00294   // sets set1 = meet(set1, set2)
00295 
00296   OA_ptr<DataFlow::DataFlowSet> set1clone = set1orig->clone(); 
00297   OA_ptr<DataFlow::DataFlowSet> set2clone = set2orig->clone(); 
00298   OA_ptr<ConstDefSet> set1 = set1clone.convert<ConstDefSet>();
00299   OA_ptr<ConstDefSet> set2 = set2clone.convert<ConstDefSet>();
00300 
00301   if (meet_debug) {
00302     std::cout << "MEET --------------------------------" << std::endl;
00303     std::cout << ">>>>> before pairwise refinement" << std::endl;
00304     std::cout << "set1 =" << std::endl;
00305     set1->dump(std::cout,mIR);
00306     std::cout << "set2 =" << std::endl;
00307     set2->dump(std::cout,mIR);
00308   }
00309 
00310   ConstDefSetIterator set1Iter(*set1);
00311   ConstDefSetIterator set2Iter(*set2);
00312 
00313   bool changeOccurred = true; // set to true to enable initial pass
00314   // need to loop over two sets multiple times because there might be locations
00315   // A and B in set one that overlap each other and only A overlaps location
00316   // C that is in set two?
00317   while (changeOccurred) {
00318     changeOccurred = false; 
00319 
00320     ConstDefSet killSet1;
00321     ConstDefSet genSet1;
00322 
00323     // Perform pairwise meet over Set1 and Set2
00324 
00325     // for each ConstDefLoc in Set1
00326     set1Iter.reset();
00327     for (; set1Iter.isValid(); ++(set1Iter)) {
00328       OA_ptr<ConstDef> constDef1 = set1Iter.current();
00329       OA_ptr<Location> cdLocPtr1 = constDef1->getLocPtr();
00330       OA_ptr<ConstValBasicInterface> cdConstPtr1 = constDef1->getConstPtr();
00331 
00332       ConstDefSet killSet2;
00333       ConstDefSet genSet2;
00334 
00335       // for each ConstDefLoc in Set2
00336       set2Iter.reset();
00337       for (; set2Iter.isValid(); ++(set2Iter)) {
00338         OA_ptr<ConstDef> constDef2 = set2Iter.current();
00339         OA_ptr<Location> cdLocPtr2 = constDef2->getLocPtr();
00340         OA_ptr<ConstValBasicInterface> cdConstPtr2 = constDef2->getConstPtr();
00341 
00342         if (meet_debug) {
00343           std::cout << "in meet: pair " << constDef1->toString(mIR)
00344                     << " vs. " << constDef2->toString(mIR) << std::endl;
00345         }
00346         
00347         ManagerReachConstsStandard::MeetOp op = NOTHING;
00348         // if Loc1 mustOverlap Loc2,
00349         if (cdLocPtr1->mustOverlap(*cdLocPtr2)) {
00350           if (meet_debug) {std::cout << "\tmustOp=";}
00351           op = getMustMeetOp(constDef1, constDef2); // must only
00352         } else if (cdLocPtr1->mayOverlap(*cdLocPtr2)) {
00353           if (meet_debug) {std::cout << "\tmayOp=";}
00354           op = getMayOnlyMeetOp(constDef1, constDef2); // may only
00355         }
00356 
00357         OA_ptr<ConstDef> cd;
00358         OA_ptr<ConstValBasicInterface> nullVal;
00359         switch (op) {
00360         case CD1toBOTTOM:
00361           if (meet_debug) {std::cout << "CD1toBOTTOM" << std::endl;}
00362           killSet1.insert(constDef1);
00363           cd = new ConstDef(cdLocPtr1,nullVal,BOTTOM);
00364           genSet1.insert(cd);
00365           changeOccurred = true;
00366           break;
00367         case CD2toBOTTOM:
00368           if (meet_debug) {std::cout << "CD2toBOTTOM" << std::endl;}
00369           killSet2.insert(constDef2);
00370           cd = new ConstDef(cdLocPtr2,nullVal,BOTTOM);
00371           genSet2.insert(cd);
00372           changeOccurred = true;
00373           break;
00374         case BOTHtoBOTTOM:
00375           if (meet_debug) {std::cout << "BOTHtoBOTTOM" << std::endl;}
00376           killSet1.insert(constDef1);
00377           cd = new ConstDef(cdLocPtr1,nullVal,BOTTOM);
00378           genSet1.insert(cd);
00379           killSet2.insert(constDef2);
00380           cd = new ConstDef(cdLocPtr2,nullVal,BOTTOM);
00381           genSet2.insert(cd);
00382           changeOccurred = true;
00383           break;
00384         case CD1toVALUECD2:
00385           if (meet_debug) {std::cout << "CD1toVALUECD2" << std::endl;}
00386           killSet1.insert(constDef1);
00387           cd = new ConstDef(cdLocPtr1,cdConstPtr2,VALUE);
00388           genSet1.insert(cd);
00389           changeOccurred = true;
00390           break;
00391         case CD2toVALUECD1:
00392           if (meet_debug) {std::cout << "CD2toVALUECD1" << std::endl;}
00393           killSet2.insert(constDef2);
00394           cd = new ConstDef(cdLocPtr2,cdConstPtr1,VALUE);
00395           genSet2.insert(cd);
00396           changeOccurred = true;
00397           break;
00398         case NOTHING:
00399           if (meet_debug) {std::cout << "NOTHING" << std::endl;}
00400           break;
00401         }
00402 
00403       } // end of set2 iteration
00404       
00405       // Update set2
00406       // Apply Kills
00407       ConstDefSetIterator setIter(killSet2);
00408       for (; setIter.isValid(); ++(setIter)) {
00409         OA_ptr<ConstDef> constDef = setIter.current();
00410         if (meet_debug) {
00411           int k = set2->removeANDtell(constDef);
00412           std::cout << "MEET: killed "<<k<<" from set2: (cd "
00413                     << constDef->toString(mIR) 
00414                     << ")" << std::endl;
00415         } else {
00416           set2->remove(constDef);
00417         }
00418       }
00419       // Apply Gens
00420       ConstDefSetIterator vsetIter(genSet2);
00421       for (; vsetIter.isValid(); ++(vsetIter)) {
00422         OA_ptr<ConstDef> constDef = vsetIter.current();
00423         set2->replace(constDef->getLocPtr(),constDef->getConstPtr(),
00424                      constDef->getConstDefType());
00425         if (meet_debug) {
00426           std::cout << "MEET: Replacement in set2: (cd "
00427                     << constDef->toString(mIR) 
00428                     << ")" << std::endl;
00429         }
00430       }
00431     }// end of set1 iteration
00432 
00433     // Update set1
00434     // Apply Kills
00435     ConstDefSetIterator setIter(killSet1);
00436     for (; setIter.isValid(); ++(setIter)) {
00437       OA_ptr<ConstDef> constDef = setIter.current();
00438       if (meet_debug) {
00439         int k = set1->removeANDtell(constDef);
00440         std::cout << "MEET: killed "<<k<<" from set1: (cd "
00441                   << constDef->toString(mIR) << ")" << std::endl;
00442       } else {
00443         set1->remove(constDef);
00444       }
00445     }
00446     // Apply Gens
00447     ConstDefSetIterator vsetIter(genSet1);
00448     for (; vsetIter.isValid(); ++(vsetIter)) {
00449       OA_ptr<ConstDef> constDef = vsetIter.current();
00450       set1->replace(constDef->getLocPtr(),constDef->getConstPtr(),
00451                    constDef->getConstDefType());
00452       if (meet_debug) {
00453         std::cout << "MEET: Val replacement in set1: (cd "
00454                   << constDef->toString(mIR) << ")" << std::endl;
00455       }
00456     }
00457   } // end of while changeOccurred 
00458 
00459   // Now, what do I do with set1 and set2? (should be equiv)
00460   if (meet_debug) {
00461     std::cout << ">>>>> after pairwise refinement, set1 and set2 should be equiv" << std::endl;
00462     std::cout << "set1 =" << std::endl;
00463     set1->dump(std::cout,mIR);
00464     std::cout << "set2 =" << std::endl;
00465     set2->dump(std::cout,mIR);
00466   }
00467 
00468   // returning set1
00469   //set1orig = set1;
00470   //return set1orig;
00471   return set1;
00472 
00473 }// end of meet function
00474 
00496 
00497 //
00498 // Brief Algorithm:
00499 // 1) Update mRCS mapping for all use memrefs for this statement
00500 // 2) Generate/apply KILL SET based upon ReachConstsStmtType
00501 // 3) Generate/apply GEN SET based upon ReachConstsStmtType
00502 // 4) Update mRCS mapping for all def memrefs for this statement
00503 //-----------------------------------------------------------------------
00504 OA_ptr<DataFlow::DataFlowSet> 
00505 ManagerReachConstsStandard::transfer(OA_ptr<DataFlow::DataFlowSet> in, OA::StmtHandle stmt) 
00506 {
00507   ConstDefSet killSet;
00508   ConstDefSet replaceSet;
00509   ConstDefSet genSet;
00510   OA_ptr<DataFlow::DataFlowSet> inclone = in->clone();
00511   OA_ptr<ConstDefSet> inRecast = inclone.convert<ConstDefSet>();
00512   // need another copy because can't iterate over set and update it at same time
00513   inclone = in->clone();  // need this or get segfault
00514   OA_ptr<ConstDefSet> inRecastCopy = inclone.convert<ConstDefSet>();
00515 
00516   /*  Added this for loop by PLM 07/27/06. It is moved out of performAnalysis.
00517    *  This loop will add Constant Definitions for every statment 
00518   */ 
00519  
00520   ConstDefSetIterator cdIter(*inRecast);
00521   for (; cdIter.isValid(); ++(cdIter)) {
00522          OA_ptr<ConstDef> constDef = cdIter.current();
00523          mRCS->insertConstDef(stmt,constDef);
00524   }
00525              
00526   
00527   if (transfer_debug) {
00528     std::cout << "--- --- --- --- --- Top of Transfer " << std::endl;
00529     //std::cout << "--- inRecastSet = ";
00530     //inRecast->dump(std::cout,mIR);
00531   }
00532 
00533   // 1) Update mapping for all use memrefs for this statement
00534   setUseMemRef2Const(stmt, *inRecast);
00535 
00536   if (debug) {
00537       std::cout << "------------------------------------------------"
00538                 << std::endl;
00539       std::cout << "transfer: stmt = (" << stmt.hval() << ": "
00540                 << mIR->toString(stmt) << ") :" << std::endl;
00541       std::cout << "\tConstDefsIn: ";
00542       inRecast->dump(std::cout,mIR);
00543       std::cout.flush();
00544   }
00545 
00546   // KILLSET construction algorithm (incl. KILLSET application)
00547   //--------------------------------
00548   // for each MemRefHandle of the DefMemRefs for this stmt:
00549   //   for each MayLoc of this MemRefHandle: 
00550   //     if the Loc of a ConstDef in inSet MayOverlap the MayLoc:
00551   //       replace ConstDef in the inSet with 
00552   //               (ConstDef.getLocPtr(),BOTTOM)
00553   //--------------------------------
00554     
00555   { // beginning of KILLSET creation/application 
00556     
00557     // for each def mem ref for this statement
00558     OA_ptr<MemRefHandleIterator> defIterPtr = mIR->getDefMemRefs(stmt);
00559     for (; defIterPtr->isValid(); (*defIterPtr)++) {
00560         MemRefHandle memref = defIterPtr->current();
00561         
00562         // for each mayLoc for this mem ref
00563         OA_ptr<LocIterator> locIterPtr = mAlias->getMayLocs(memref);
00564         for (; locIterPtr->isValid(); ++(*locIterPtr)) {
00565           OA_ptr<Location> mayLocPtr = locIterPtr->current();
00566           
00567           // for each ConstDefLoc in inSet
00568           OA_ptr<ConstDefSetIterator> inIter;
00569           inIter = new ConstDefSetIterator(*inRecastCopy);
00570           for (; inIter->isValid(); ++(*inIter)) {
00571             OA_ptr<ConstDef> constDef = inIter->current();
00572             OA_ptr<Location> cdLocPtr = constDef->getLocPtr();
00573             
00574             // if Loc mayOverLap MayLoc, replace any constDef with cdLocPtr
00575             // with ConstDef(cdLocPtr,BOTTOM)
00576             if (cdLocPtr->mayOverlap(*mayLocPtr)) {
00577               OA_ptr<ConstValBasicInterface> nullVal; nullVal = 0;
00578               inRecast->replace(cdLocPtr,nullVal,BOTTOM);
00579             }
00580           }
00581         }
00582     }
00583 
00584     // loop through all func calls in this statement and what they MOD
00585     OA_ptr<IRCallsiteIterator> callsiteItPtr = mIR->getCallsites(stmt);
00586     for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00587       CallHandle expr = callsiteItPtr->current();
00588 
00589       OA_ptr<LocIterator> locIterPtr;
00590       // MOD
00591       locIterPtr = mInterSE->getMODIterator(expr);
00592       for ( ; locIterPtr->isValid(); (*locIterPtr)++) {
00593           OA_ptr<Location> modLocPtr = locIterPtr->current();
00594           
00595           // for each ConstDefLoc in inSet
00596           OA_ptr<ConstDefSetIterator> inIter;
00597           inIter = new ConstDefSetIterator(*inRecastCopy);
00598           for (; inIter->isValid(); ++(*inIter)) {
00599             OA_ptr<ConstDef> constDef = inIter->current();
00600             OA_ptr<Location> cdLocPtr = constDef->getLocPtr();
00601             
00602             // if Loc mayOverLap modLoc, replace any constDef with cdLocPtr
00603             // with ConstDef(cdLocPtr,BOTTOM)
00604             if (cdLocPtr->mayOverlap(*modLocPtr)) {
00605               OA_ptr<ConstValBasicInterface> nullVal; nullVal = 0;
00606               inRecast->replace(cdLocPtr,nullVal,BOTTOM);
00607             }
00608           }
00609        } 
00610 
00611     }
00612  
00613   } // end of KILLSET creation/application 
00614 
00615 
00616   // GENSET construction/application algorithm for EXPR_STMT
00617   //--------------------------------
00618   // for each  AssignPair -- <MemRefHandle,ExprTree> -- for this stmt:
00619   //   get ExprConst from ExprTree if it exists
00620   //   if ExprConst exists:
00621   //     for each MustLoc of the MemRefHandle
00622   //       replace any ConstDef(MustLoc) in inSet with
00623   //                   ConstDef(MustLoc,ExprConst,VALUE)
00624   //--------------------------------
00625 
00626   OA_ptr<AssignPairIterator> espIterPtr 
00627          = mIR->getAssignPairIterator(stmt);
00628   for ( ; espIterPtr->isValid(); (*espIterPtr)++) {
00629         // unbundle pair
00630         MemRefHandle mref = espIterPtr->currentTarget();
00631         ExprHandle expr = espIterPtr->currentSource();
00632         
00633         // getConstValBasic if possible
00634         OA_ptr<ConstValBasicInterface> cvbiPtr; cvbiPtr = 0;
00635         OA_ptr<ExprTree> eTreePtr = mIR->getExprTree(expr);
00636 
00637         // get ConstValBasicInterface by evaluating expression tree
00638         EvalToConstVisitor evalVisitor(mIR,mRCS);
00639         eTreePtr->acceptVisitor(evalVisitor);
00640         cvbiPtr = evalVisitor.getConstVal();
00641 
00642         if (!cvbiPtr.ptrEqual(NULL)) { // GEN a constant value
00643           if (transfer_debug) {
00644             std::cout << "After eval, cvbiPtr = (" << cvbiPtr <<")"<<std::endl;
00645             std::cout.flush();
00646             std::cout << "           *cvbiPTr = (" << cvbiPtr->toString()
00647                       << ")" << std::endl;
00648             std::cout.flush();
00649           }
00650           
00651           // for each MustLoc, replace any ConstDef(MustLoc,...) in inSet
00652           //     with new constant value: ConstDef(MustLoc,cvbiPtr,VALUE)
00653           OA_ptr<LocIterator> lIterPtr = mAlias->getMustLocs(mref);
00654           for ( ; lIterPtr->isValid() ; ++(*lIterPtr)) {
00655             OA_ptr<Location> lPtr = lIterPtr->current();
00656             inRecast->replace(lPtr,cvbiPtr,VALUE);
00657           }
00658         } // end of "if we have a usable const val"
00659   } // end of loop over AssignPairList
00660  
00661     
00662   // 4) update mRCS for all mayLocs(???) of the DefMemRefs(stmt)
00663   setDefMemRef2Const(stmt, *inRecast);
00664   
00665   if (debug) {
00666     std::cout << "\tConstDefsOut: ";
00667     inRecast->dump(std::cout,mIR);
00668   }
00669  
00670   return inRecast;
00671 }
00672 
00674 ManagerReachConstsStandard::MeetOp 
00675 ManagerReachConstsStandard::getMustMeetOp(OA_ptr<ConstDef> cd1, OA_ptr<ConstDef> cd2) 
00676 {
00677   ConstDefType cdType1 = cd1->getConstDefType();
00678   OA_ptr<ConstValBasicInterface> cdConstPtr1 = cd1->getConstPtr();
00679   ConstDefType cdType2 = cd2->getConstDefType();
00680   OA_ptr<ConstValBasicInterface> cdConstPtr2 = cd2->getConstPtr();
00681   ManagerReachConstsStandard::MeetOp retval = NOTHING;
00682 
00683   // apply MustOverlap MEET rules
00684   switch (cdType1) {
00685   case TOP:
00686     switch (cdType2) {
00687     case TOP:
00688       // TOP MEET TOP (MustOverlap)
00689       retval = NOTHING; break;
00690     case VALUE:
00691       // TOP MEET VALUE (MustOverlap)
00692       retval = CD1toVALUECD2; break;
00693     case BOTTOM:      
00694       // TOP MEET BOTTOM (MustOverlap)
00695       retval = CD1toBOTTOM; break;
00696     }
00697     break;
00698 
00699   case VALUE:
00700     switch (cdType2) {
00701     case TOP:
00702       // VALUE MEET TOP (MustOverlap)
00703       retval = CD2toVALUECD1; break;
00704     case VALUE:
00705       // VALUE MEET VALUE (MustOverlap)
00706       if (cdConstPtr1 != cdConstPtr2) {
00707         retval = BOTHtoBOTTOM;
00708       } else {
00709         retval = NOTHING;
00710       }
00711       break;
00712     case BOTTOM:
00713       // VALUE MEET BOTTOM (MustOverlap)
00714       retval = CD1toBOTTOM; break;
00715     }
00716     break;
00717     
00718   case BOTTOM:
00719     switch (cdType2) {
00720     case TOP:     
00721       // BOTTOM MEET TOP (MustOverlap)
00722       retval = CD2toBOTTOM; break;
00723     case VALUE:
00724       // BOTTOM MEET VALUE (MustOverlap)
00725       retval = CD2toBOTTOM; break;
00726     case BOTTOM:
00727       // BOTTOM MEET BOTTOM (MustOverlap)
00728       retval = NOTHING; break;
00729     }
00730     break;
00731   } // end of switch (cdType1)
00732   
00733   return retval;
00734 }
00735 
00737 ManagerReachConstsStandard::MeetOp 
00738 ManagerReachConstsStandard::getMayOnlyMeetOp(OA_ptr<ConstDef> cd1, OA_ptr<ConstDef> cd2) 
00739 {
00740   ConstDefType cdType1 = cd1->getConstDefType();
00741   ConstDefType cdType2 = cd2->getConstDefType();
00742   ManagerReachConstsStandard::MeetOp retval = NOTHING;
00743 
00744   // apply (mayOverlap && !MustOverlap) MEET rules
00745   switch (cdType1) {
00746   case TOP:
00747     switch (cdType2) {
00748     case TOP:
00749       // TOP MEET TOP (MayOverlap && !MustOverlap)
00750       retval = NOTHING; break;
00751     case VALUE:
00752       // TOP MEET VALUE (MayOverlap && !MustOverlap)
00753       retval = BOTHtoBOTTOM; break;
00754     case BOTTOM:
00755       // TOP MEET BOTTOM (MayOverlap && !MustOverlap)
00756       retval = CD1toBOTTOM; break;
00757     }
00758     break;
00759     
00760   case VALUE:
00761     switch (cdType2) {
00762     case TOP:
00763       // VALUE MEET TOP (MayOverlap && !MustOverlap)
00764       retval = BOTHtoBOTTOM; break;      
00765     case VALUE:
00766       // VALUE MEET VALUE (MayOverlap && !MustOverlap)
00767       retval = BOTHtoBOTTOM; break;
00768     case BOTTOM:
00769       // VALUE MEET BOTTOM (MayOverlap && !MustOverlap)
00770       retval = CD1toBOTTOM; break;
00771     }
00772     break;
00773     
00774   case BOTTOM:
00775     switch (cdType2) {
00776     case TOP:     
00777       // BOTTOM MEET TOP (MayOverlap && !MustOverlap)
00778       retval = CD2toBOTTOM; break;
00779     case VALUE:
00780       // BOTTOM MEET VALUE (MayOverlap && !MustOverlap)
00781       retval = CD2toBOTTOM; break;
00782     case BOTTOM:    
00783       // BOTTOM MEET BOTTOM (MayOverlap && !MustOverlap)
00784       retval = NOTHING; break;
00785     }
00786     break;    
00787   } // end of switch (cdType1)
00788  
00789   return retval;
00790 }
00791 
00794 void 
00795 ManagerReachConstsStandard::setUseMemRef2Const(StmtHandle stmt, const ConstDefSet& in) {
00796 
00797   // for each use mem ref in this statement
00798   OA_ptr<MemRefHandleIterator> useIterPtr = mIR->getUseMemRefs(stmt);
00799   for (; useIterPtr->isValid(); (*useIterPtr)++) {
00800     MemRefHandle ref = useIterPtr->current();
00801 
00802     // get all may locations for this mem ref
00803     OA_ptr<LocIterator> locIterPtr = mAlias->getMayLocs(ref);
00804     
00805     // set up first location info
00806     OA_ptr<Location> loc = locIterPtr->current();
00807     if (transfer_debug) {
00808       std::cout << "Finding location (" << loc->toString(mIR) << ")" 
00809                 << std::endl;
00810       std::cout.flush();
00811     }
00812 
00813     OA_ptr<ConstDef> cdPtr = in.find(loc);
00814 
00815     // keep track of useful info for this memref
00816     bool allValuesAgree = true;
00817     if (cdPtr.ptrEqual(NULL)) {
00818       allValuesAgree = false; // first location not found in inSet
00819       if (transfer_debug) {
00820         std::cout << "a useMemRef (" << mIR->toString(ref) << ") "
00821                   << "missing ConstDef for first mayLoc ( "
00822                   << loc->toString(mIR) << ")" << std::endl;
00823         std::cout.flush();
00824       }
00825     } else if (cdPtr->getConstDefType() != VALUE) {
00826       allValuesAgree = false;
00827       // first value of mayLoc for this memref is not a value
00828     } 
00829 
00830     // Now check that all other mayLocs agree
00831     for (; (allValuesAgree) && (locIterPtr->isValid()); ++(*locIterPtr)) {
00832       OA_ptr<Location> loc = locIterPtr->current();
00833       OA_ptr<ConstDef> cdNextPtr = in.find(loc);
00834       if (cdPtr.ptrEqual(NULL)) {
00835         allValuesAgree = false; // next location not found in inSet
00836       } else if (!((*cdPtr).equiv(*cdNextPtr))) {
00837         allValuesAgree = false; // next location not equiv to previous
00838       }
00839     }
00840     if (allValuesAgree) {
00841       // record the value in cdPtr to mapping for this memref
00842       mRCS->updateReachConst(ref,cdPtr->getConstPtr());
00843     } else {
00844       // record no constant value for this memref
00845       OA_ptr<ConstValBasicInterface> nullVal;  nullVal = 0;
00846       mRCS->updateReachConst(ref,nullVal);
00847     }
00848   } // end of useMemRef loop
00849 
00850   if (transfer_debug) {
00851     OA_ptr<MemRefHandleIterator> useIterPtr = mIR->getUseMemRefs(stmt);
00852     for (; useIterPtr->isValid(); (*useIterPtr)++) {
00853       MemRefHandle ref = useIterPtr->current();
00854       OA_ptr<ConstValBasicInterface> cvbiPtr = mRCS->getReachConst(ref);
00855       std::cout << "useMemRef(" << mIR->toString(ref) ;
00856       if (cvbiPtr.ptrEqual(NULL)) {
00857         std::cout << ") has no constant value." << std::endl;
00858       } else {
00859         std::cout  << ") maps to VALUE="
00860                    << cvbiPtr->toString() << std::endl;
00861       }
00862     }
00863   }
00864 } // end of setUseMemRef2Const()
00865 
00868 void 
00869 ManagerReachConstsStandard::setDefMemRef2Const(StmtHandle stmt, const ConstDefSet& in) {
00870 
00871   // update mRCS for all mayLocs(???) of the DefMemRefs(stmt)
00872   OA_ptr<MemRefHandleIterator> defIterPtr = mIR->getDefMemRefs(stmt);
00873   for (; defIterPtr->isValid(); (*defIterPtr)++) {
00874     MemRefHandle ref = defIterPtr->current();
00875     
00876     // get all may locations for this mem ref
00877     OA_ptr<LocIterator> locIterPtr = mAlias->getMayLocs(ref);
00878     
00879     // set up first location info
00880     OA_ptr<Location> loc = locIterPtr->current();
00881     if (transfer_debug) {
00882       std::cout << "Finding def location (" << loc->toString(mIR) << ")" 
00883                 << std::endl;
00884       std::cout.flush();
00885     }
00886     
00887     OA_ptr<ConstDef> cdPtr = in.find(loc);
00888     
00889     // keep track of useful info for this memref
00890     bool allValuesAgree = true;
00891     if (cdPtr.ptrEqual(NULL)) {
00892       allValuesAgree = false; // first location not found in inSet
00893       if (transfer_debug) {
00894         std::cout << "a defMemRef (" << mIR->toString(ref) << ") "
00895                   << "missing ConstDef for first mayLoc ( "
00896                   << loc->toString(mIR) << ")" << std::endl;
00897         std::cout.flush();
00898       }
00899     } else if (cdPtr->getConstDefType() != VALUE) {
00900       allValuesAgree = false;
00901       // first value of mayLoc for this memref is not a value
00902     } 
00903     
00904     // Now check that all other mayLocs agree
00905     for (; (allValuesAgree) && (locIterPtr->isValid()); ++(*locIterPtr)) {
00906       OA_ptr<Location> loc = locIterPtr->current();
00907       OA_ptr<ConstDef> cdNextPtr = in.find(loc);
00908       if (cdPtr.ptrEqual(NULL)) {
00909         allValuesAgree = false; // next location not found in inSet
00910       } else if (!((*cdPtr).equiv(*cdNextPtr))) {
00911         allValuesAgree = false; // next location not equiv to previous
00912       }
00913     }
00914     if (allValuesAgree) {
00915       // record the value in cdPtr to mapping for this memref
00916       mRCS->updateReachConst(ref,cdPtr->getConstPtr());
00917     } else {
00918       // record no constant value for this memref
00919       OA_ptr<ConstValBasicInterface> nullValue; nullValue = NULL;
00920       mRCS->updateReachConst(ref,nullValue);
00921     }
00922   } // end of defMemRef loop
00923   
00924   if (transfer_debug) {
00925     OA_ptr<MemRefHandleIterator> defIterPtr = mIR->getDefMemRefs(stmt);
00926     for (; defIterPtr->isValid(); (*defIterPtr)++) {
00927       MemRefHandle ref = defIterPtr->current();
00928       OA_ptr<ConstValBasicInterface> cvbiPtr = mRCS->getReachConst(ref);
00929       std::cout << "defMemRef(" << mIR->toString(ref);
00930       if (cvbiPtr.ptrEqual(NULL)) {
00931         std::cout << ") has no constant value." << std::cout;
00932       } else {
00933         std::cout << ") maps to VALUE="
00934                   << (mRCS->getReachConst(ref))->toString() << std::endl;
00935       }
00936     }
00937   }
00938 } // end of setDefMemRef2Const()
00939 
00940   } // end of namespace ReachConsts
00941 } // end of namespace OA