LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MemoryDepAnalysis.cpp

Go to the documentation of this file.
00001 //===- MemoryDepAnalysis.cpp - Compute dep graph for memory ops -----------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a pass (MemoryDepAnalysis) that computes memory-based
00011 // data dependences between instructions for each function in a module.  
00012 // Memory-based dependences occur due to load and store operations, but
00013 // also the side-effects of call instructions.
00014 //
00015 // The result of this pass is a DependenceGraph for each function
00016 // representing the memory-based data dependences between instructions.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #include "MemoryDepAnalysis.h"
00021 #include "IPModRef.h"
00022 #include "llvm/Instructions.h"
00023 #include "llvm/Module.h"
00024 #include "llvm/Analysis/DataStructure/DataStructure.h"
00025 #include "llvm/Analysis/DataStructure/DSGraph.h"
00026 #include "llvm/Support/InstVisitor.h"
00027 #include "llvm/Support/CFG.h"
00028 #include "llvm/ADT/SCCIterator.h"
00029 #include "llvm/ADT/Statistic.h"
00030 #include "llvm/ADT/STLExtras.h"
00031 #include "llvm/ADT/hash_map"
00032 #include "llvm/ADT/hash_set"
00033 
00034 namespace llvm {
00035 
00036 ///--------------------------------------------------------------------------
00037 /// struct ModRefTable:
00038 /// 
00039 /// A data structure that tracks ModRefInfo for instructions:
00040 ///   -- modRefMap is a map of Instruction* -> ModRefInfo for the instr.
00041 ///   -- definers  is a vector of instructions that define    any node
00042 ///   -- users     is a vector of instructions that reference any node
00043 ///   -- numUsersBeforeDef is a vector indicating that the number of users
00044 ///                seen before definers[i] is numUsersBeforeDef[i].
00045 /// 
00046 /// numUsersBeforeDef[] effectively tells us the exact interleaving of
00047 /// definers and users within the ModRefTable.
00048 /// This is only maintained when constructing the table for one SCC, and
00049 /// not copied over from one table to another since it is no longer useful.
00050 ///--------------------------------------------------------------------------
00051 
00052 struct ModRefTable {
00053   typedef hash_map<Instruction*, ModRefInfo> ModRefMap;
00054   typedef ModRefMap::const_iterator                 const_map_iterator;
00055   typedef ModRefMap::      iterator                       map_iterator;
00056   typedef std::vector<Instruction*>::const_iterator const_ref_iterator;
00057   typedef std::vector<Instruction*>::      iterator       ref_iterator;
00058 
00059   ModRefMap                 modRefMap;
00060   std::vector<Instruction*> definers;
00061   std::vector<Instruction*> users;
00062   std::vector<unsigned>     numUsersBeforeDef;
00063 
00064   // Iterators to enumerate all the defining instructions
00065   const_ref_iterator defsBegin()  const {  return definers.begin(); }
00066         ref_iterator defsBegin()        {  return definers.begin(); }
00067   const_ref_iterator defsEnd()    const {  return definers.end(); }
00068         ref_iterator defsEnd()          {  return definers.end(); }
00069 
00070   // Iterators to enumerate all the user instructions
00071   const_ref_iterator usersBegin() const {  return users.begin(); }
00072         ref_iterator usersBegin()       {  return users.begin(); }
00073   const_ref_iterator usersEnd()   const {  return users.end(); }
00074         ref_iterator usersEnd()         {  return users.end(); }
00075 
00076   // Iterator identifying the last user that was seen *before* a
00077   // specified def.  In particular, all users in the half-closed range
00078   //    [ usersBegin(), usersBeforeDef_End(defPtr) )
00079   // were seen *before* the specified def.  All users in the half-closed range
00080   //    [ usersBeforeDef_End(defPtr), usersEnd() )
00081   // were seen *after* the specified def.
00082   // 
00083   ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) {
00084     unsigned defIndex = (unsigned) (defPtr - defsBegin());
00085     assert(defIndex < numUsersBeforeDef.size());
00086     assert(usersBegin() + numUsersBeforeDef[defIndex] <= usersEnd()); 
00087     return usersBegin() + numUsersBeforeDef[defIndex]; 
00088   }
00089   const_ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) const {
00090     return const_cast<ModRefTable*>(this)->usersBeforeDef_End(defPtr);
00091   }
00092 
00093   // 
00094   // Modifier methods
00095   // 
00096   void AddDef(Instruction* D) {
00097     definers.push_back(D);
00098     numUsersBeforeDef.push_back(users.size());
00099   }
00100   void AddUse(Instruction* U) {
00101     users.push_back(U);
00102   }
00103   void Insert(const ModRefTable& fromTable) {
00104     modRefMap.insert(fromTable.modRefMap.begin(), fromTable.modRefMap.end());
00105     definers.insert(definers.end(),
00106                     fromTable.definers.begin(), fromTable.definers.end());
00107     users.insert(users.end(),
00108                  fromTable.users.begin(), fromTable.users.end());
00109     numUsersBeforeDef.clear(); /* fromTable.numUsersBeforeDef is ignored */
00110   }
00111 };
00112 
00113 
00114 ///--------------------------------------------------------------------------
00115 /// class ModRefInfoBuilder:
00116 /// 
00117 /// A simple InstVisitor<> class that retrieves the Mod/Ref info for
00118 /// Load/Store/Call instructions and inserts this information in
00119 /// a ModRefTable.  It also records all instructions that Mod any node
00120 /// and all that use any node.
00121 ///--------------------------------------------------------------------------
00122 
00123 class ModRefInfoBuilder : public InstVisitor<ModRefInfoBuilder> {
00124   const DSGraph&            funcGraph;
00125   const FunctionModRefInfo& funcModRef;
00126   struct ModRefTable&       modRefTable;
00127 
00128   ModRefInfoBuilder();                         // DO NOT IMPLEMENT
00129   ModRefInfoBuilder(const ModRefInfoBuilder&); // DO NOT IMPLEMENT
00130   void operator=(const ModRefInfoBuilder&);    // DO NOT IMPLEMENT
00131 
00132 public:
00133   ModRefInfoBuilder(const DSGraph& _funcGraph,
00134                     const FunctionModRefInfo& _funcModRef,
00135                     ModRefTable& _modRefTable)
00136     : funcGraph(_funcGraph), funcModRef(_funcModRef), modRefTable(_modRefTable)
00137   {
00138   }
00139 
00140   // At a call instruction, retrieve the ModRefInfo using IPModRef results.
00141   // Add the call to the defs list if it modifies any nodes and to the uses
00142   // list if it refs any nodes.
00143   // 
00144   void visitCallInst(CallInst& callInst) {
00145     ModRefInfo safeModRef(funcGraph.getGraphSize());
00146     const ModRefInfo* callModRef = funcModRef.getModRefInfo(callInst);
00147     if (callModRef == NULL) {
00148       // call to external/unknown function: mark all nodes as Mod and Ref
00149       safeModRef.getModSet().set();
00150       safeModRef.getRefSet().set();
00151       callModRef = &safeModRef;
00152     }
00153 
00154     modRefTable.modRefMap.insert(std::make_pair(&callInst,
00155                                                 ModRefInfo(*callModRef)));
00156     if (callModRef->getModSet().any())
00157       modRefTable.AddDef(&callInst);
00158     if (callModRef->getRefSet().any())
00159       modRefTable.AddUse(&callInst);
00160   }
00161 
00162   // At a store instruction, add to the mod set the single node pointed to
00163   // by the pointer argument of the store.  Interestingly, if there is no
00164   // such node, that would be a null pointer reference!
00165   void visitStoreInst(StoreInst& storeInst) {
00166     const DSNodeHandle& ptrNode =
00167       funcGraph.getNodeForValue(storeInst.getPointerOperand());
00168     if (const DSNode* target = ptrNode.getNode()) {
00169       unsigned nodeId = funcModRef.getNodeId(target);
00170       ModRefInfo& minfo =
00171         modRefTable.modRefMap.insert(
00172           std::make_pair(&storeInst,
00173                          ModRefInfo(funcGraph.getGraphSize()))).first->second;
00174       minfo.setNodeIsMod(nodeId);
00175       modRefTable.AddDef(&storeInst);
00176     } else
00177       std::cerr << "Warning: Uninitialized pointer reference!\n";
00178   }
00179 
00180   // At a load instruction, add to the ref set the single node pointed to
00181   // by the pointer argument of the load.  Interestingly, if there is no
00182   // such node, that would be a null pointer reference!
00183   void visitLoadInst(LoadInst& loadInst) {
00184     const DSNodeHandle& ptrNode =
00185       funcGraph.getNodeForValue(loadInst.getPointerOperand());
00186     if (const DSNode* target = ptrNode.getNode()) {
00187       unsigned nodeId = funcModRef.getNodeId(target);
00188       ModRefInfo& minfo =
00189         modRefTable.modRefMap.insert(
00190           std::make_pair(&loadInst,
00191                          ModRefInfo(funcGraph.getGraphSize()))).first->second;
00192       minfo.setNodeIsRef(nodeId);
00193       modRefTable.AddUse(&loadInst);
00194     } else
00195       std::cerr << "Warning: Uninitialized pointer reference!\n";
00196   }
00197 };
00198 
00199 
00200 //----------------------------------------------------------------------------
00201 // class MemoryDepAnalysis: A dep. graph for load/store/call instructions
00202 //----------------------------------------------------------------------------
00203 
00204 
00205 /// getAnalysisUsage - This does not modify anything.  It uses the Top-Down DS
00206 /// Graph and IPModRef.
00207 ///
00208 void MemoryDepAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
00209   AU.setPreservesAll();
00210   AU.addRequired<TDDataStructures>();
00211   AU.addRequired<IPModRef>();
00212 }
00213 
00214 
00215 /// Basic dependence gathering algorithm, using scc_iterator on CFG:
00216 /// 
00217 /// for every SCC S in the CFG in PostOrder on the SCC DAG
00218 ///     {
00219 ///       for every basic block BB in S in *postorder*
00220 ///         for every instruction I in BB in reverse
00221 ///           Add (I, ModRef[I]) to ModRefCurrent
00222 ///           if (Mod[I] != NULL)
00223 ///               Add I to DefSetCurrent:  { I \in S : Mod[I] != NULL }
00224 ///           if (Ref[I] != NULL)
00225 ///               Add I to UseSetCurrent:  { I       : Ref[I] != NULL }
00226 /// 
00227 ///       for every def D in DefSetCurrent
00228 /// 
00229 ///           // NOTE: D comes after itself iff S contains a loop
00230 ///           if (HasLoop(S) && D & D)
00231 ///               Add output-dep: D -> D2
00232 /// 
00233 ///           for every def D2 *after* D in DefSetCurrent
00234 ///               // NOTE: D2 comes before D in execution order
00235 ///               if (D & D2)
00236 ///                   Add output-dep: D2 -> D
00237 ///                   if (HasLoop(S))
00238 ///                       Add output-dep: D -> D2
00239 /// 
00240 ///           for every use U in UseSetCurrent that was seen *before* D
00241 ///               // NOTE: U comes after D in execution order
00242 ///               if (U & D)
00243 ///                   if (U != D || HasLoop(S))
00244 ///                       Add true-dep: D -> U
00245 ///                   if (HasLoop(S))
00246 ///                       Add anti-dep: U -> D
00247 /// 
00248 ///           for every use U in UseSetCurrent that was seen *after* D
00249 ///               // NOTE: U comes before D in execution order
00250 ///               if (U & D)
00251 ///                   if (U != D || HasLoop(S))
00252 ///                       Add anti-dep: U -> D
00253 ///                   if (HasLoop(S))
00254 ///                       Add true-dep: D -> U
00255 /// 
00256 ///           for every def Dnext in DefSetAfter
00257 ///               // NOTE: Dnext comes after D in execution order
00258 ///               if (Dnext & D)
00259 ///                   Add output-dep: D -> Dnext
00260 /// 
00261 ///           for every use Unext in UseSetAfter
00262 ///               // NOTE: Unext comes after D in execution order
00263 ///               if (Unext & D)
00264 ///                   Add true-dep: D -> Unext
00265 /// 
00266 ///       for every use U in UseSetCurrent
00267 ///           for every def Dnext in DefSetAfter
00268 ///               // NOTE: Dnext comes after U in execution order
00269 ///               if (Dnext & D)
00270 ///                   Add anti-dep: U -> Dnext
00271 /// 
00272 ///       Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) }
00273 ///       Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL }
00274 ///       Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL }
00275 ///     }
00276 ///         
00277 ///
00278 void MemoryDepAnalysis::ProcessSCC(std::vector<BasicBlock*> &S,
00279                                    ModRefTable& ModRefAfter, bool hasLoop) {
00280   ModRefTable ModRefCurrent;
00281   ModRefTable::ModRefMap& mapCurrent = ModRefCurrent.modRefMap;
00282   ModRefTable::ModRefMap& mapAfter   = ModRefAfter.modRefMap;
00283 
00284   // Builder class fills out a ModRefTable one instruction at a time.
00285   // To use it, we just invoke it's visit function for each basic block:
00286   // 
00287   //   for each basic block BB in the SCC in *postorder*
00288   //       for each instruction  I in BB in *reverse*
00289   //           ModRefInfoBuilder::visit(I)
00290   //           : Add (I, ModRef[I]) to ModRefCurrent.modRefMap
00291   //           : Add I  to ModRefCurrent.definers if it defines any node
00292   //           : Add I  to ModRefCurrent.users    if it uses any node
00293   // 
00294   ModRefInfoBuilder builder(*funcGraph, *funcModRef, ModRefCurrent);
00295   for (std::vector<BasicBlock*>::iterator BI = S.begin(), BE = S.end();
00296        BI != BE; ++BI)
00297     // Note: BBs in the SCC<> created by scc_iterator are in postorder.
00298     for (BasicBlock::reverse_iterator II=(*BI)->rbegin(), IE=(*BI)->rend();
00299          II != IE; ++II)
00300       builder.visit(*II);
00301 
00302   ///       for every def D in DefSetCurrent
00303   /// 
00304   for (ModRefTable::ref_iterator II=ModRefCurrent.defsBegin(),
00305          IE=ModRefCurrent.defsEnd(); II != IE; ++II)
00306     {
00307       ///           // NOTE: D comes after itself iff S contains a loop
00308       ///           if (HasLoop(S))
00309       ///               Add output-dep: D -> D2
00310       if (hasLoop)
00311         funcDepGraph->AddSimpleDependence(**II, **II, OutputDependence);
00312 
00313       ///           for every def D2 *after* D in DefSetCurrent
00314       ///               // NOTE: D2 comes before D in execution order
00315       ///               if (D2 & D)
00316       ///                   Add output-dep: D2 -> D
00317       ///                   if (HasLoop(S))
00318       ///                       Add output-dep: D -> D2
00319       for (ModRefTable::ref_iterator JI=II+1; JI != IE; ++JI)
00320         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
00321                       mapCurrent.find(*JI)->second.getModSet()))
00322           {
00323             funcDepGraph->AddSimpleDependence(**JI, **II, OutputDependence);
00324             if (hasLoop)
00325               funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence);
00326           }
00327   
00328       ///           for every use U in UseSetCurrent that was seen *before* D
00329       ///               // NOTE: U comes after D in execution order
00330       ///               if (U & D)
00331       ///                   if (U != D || HasLoop(S))
00332       ///                       Add true-dep: U -> D
00333       ///                   if (HasLoop(S))
00334       ///                       Add anti-dep: D -> U
00335       ModRefTable::ref_iterator JI=ModRefCurrent.usersBegin();
00336       ModRefTable::ref_iterator JE = ModRefCurrent.usersBeforeDef_End(II);
00337       for ( ; JI != JE; ++JI)
00338         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
00339                       mapCurrent.find(*JI)->second.getRefSet()))
00340           {
00341             if (*II != *JI || hasLoop)
00342               funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
00343             if (hasLoop)
00344               funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
00345           }
00346 
00347       ///           for every use U in UseSetCurrent that was seen *after* D
00348       ///               // NOTE: U comes before D in execution order
00349       ///               if (U & D)
00350       ///                   if (U != D || HasLoop(S))
00351       ///                       Add anti-dep: U -> D
00352       ///                   if (HasLoop(S))
00353       ///                       Add true-dep: D -> U
00354       for (/*continue JI*/ JE = ModRefCurrent.usersEnd(); JI != JE; ++JI)
00355         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
00356                       mapCurrent.find(*JI)->second.getRefSet()))
00357           {
00358             if (*II != *JI || hasLoop)
00359               funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
00360             if (hasLoop)
00361               funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
00362           }
00363 
00364       ///           for every def Dnext in DefSetPrev
00365       ///               // NOTE: Dnext comes after D in execution order
00366       ///               if (Dnext & D)
00367       ///                   Add output-dep: D -> Dnext
00368       for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(),
00369              JE=ModRefAfter.defsEnd(); JI != JE; ++JI)
00370         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
00371                       mapAfter.find(*JI)->second.getModSet()))
00372           funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence);
00373 
00374       ///           for every use Unext in UseSetAfter
00375       ///               // NOTE: Unext comes after D in execution order
00376       ///               if (Unext & D)
00377       ///                   Add true-dep: D -> Unext
00378       for (ModRefTable::ref_iterator JI=ModRefAfter.usersBegin(),
00379              JE=ModRefAfter.usersEnd(); JI != JE; ++JI)
00380         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
00381                       mapAfter.find(*JI)->second.getRefSet()))
00382           funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
00383     }
00384 
00385   /// 
00386   ///       for every use U in UseSetCurrent
00387   ///           for every def Dnext in DefSetAfter
00388   ///               // NOTE: Dnext comes after U in execution order
00389   ///               if (Dnext & D)
00390   ///                   Add anti-dep: U -> Dnext
00391   for (ModRefTable::ref_iterator II=ModRefCurrent.usersBegin(),
00392          IE=ModRefCurrent.usersEnd(); II != IE; ++II)
00393     for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(),
00394            JE=ModRefAfter.defsEnd(); JI != JE; ++JI)
00395       if (!Disjoint(mapCurrent.find(*II)->second.getRefSet(),
00396                     mapAfter.find(*JI)->second.getModSet()))
00397         funcDepGraph->AddSimpleDependence(**II, **JI, AntiDependence);
00398     
00399   ///       Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) }
00400   ///       Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL }
00401   ///       Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL }
00402   ModRefAfter.Insert(ModRefCurrent);
00403 }
00404 
00405 
00406 /// Debugging support methods
00407 /// 
00408 void MemoryDepAnalysis::print(std::ostream &O) const
00409 {
00410   // TEMPORARY LOOP
00411   for (hash_map<Function*, DependenceGraph*>::const_iterator
00412          I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
00413     {
00414       Function* func = I->first;
00415       DependenceGraph* depGraph = I->second;
00416 
00417   O << "\n================================================================\n";
00418   O << "DEPENDENCE GRAPH FOR MEMORY OPERATIONS IN FUNCTION " << func->getName();
00419   O << "\n================================================================\n\n";
00420   depGraph->print(*func, O);
00421 
00422     }
00423 }
00424 
00425 
00426 /// 
00427 /// Run the pass on a function
00428 /// 
00429 bool MemoryDepAnalysis::runOnFunction(Function &F) {
00430   assert(!F.isExternal());
00431 
00432   // Get the FunctionModRefInfo holding IPModRef results for this function.
00433   // Use the TD graph recorded within the FunctionModRefInfo object, which
00434   // may not be the same as the original TD graph computed by DS analysis.
00435   // 
00436   funcModRef = &getAnalysis<IPModRef>().getFunctionModRefInfo(F);
00437   funcGraph  = &funcModRef->getFuncGraph();
00438 
00439   // TEMPORARY: ptr to depGraph (later just becomes "this").
00440   assert(!funcMap.count(&F) && "Analyzing function twice?");
00441   funcDepGraph = funcMap[&F] = new DependenceGraph();
00442 
00443   ModRefTable ModRefAfter;
00444 
00445   for (scc_iterator<Function*> I = scc_begin(&F), E = scc_end(&F); I != E; ++I)
00446     ProcessSCC(*I, ModRefAfter, I.hasLoop());
00447 
00448   return true;
00449 }
00450 
00451 
00452 //-------------------------------------------------------------------------
00453 // TEMPORARY FUNCTIONS TO MAKE THIS A MODULE PASS ---
00454 // These functions will go away once this class becomes a FunctionPass.
00455 // 
00456 
00457 // Driver function to compute dependence graphs for every function.
00458 // This is temporary and will go away once this is a FunctionPass.
00459 // 
00460 bool MemoryDepAnalysis::runOnModule(Module& M)
00461 {
00462   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
00463     if (! FI->isExternal())
00464       runOnFunction(*FI); // automatically inserts each depGraph into funcMap
00465   return true;
00466 }
00467   
00468 // Release all the dependence graphs in the map.
00469 void MemoryDepAnalysis::releaseMemory()
00470 {
00471   for (hash_map<Function*, DependenceGraph*>::const_iterator
00472          I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
00473     delete I->second;
00474   funcMap.clear();
00475 
00476   // Clear pointers because the pass constructor will not be invoked again.
00477   funcDepGraph = NULL;
00478   funcGraph = NULL;
00479   funcModRef = NULL;
00480 }
00481 
00482 MemoryDepAnalysis::~MemoryDepAnalysis()
00483 {
00484   releaseMemory();
00485 }
00486 
00487 //----END TEMPORARY FUNCTIONS----------------------------------------------
00488 
00489 
00490 void MemoryDepAnalysis::dump() const
00491 {
00492   this->print(std::cerr);
00493 }
00494 
00495 static RegisterAnalysis<MemoryDepAnalysis>
00496 Z("memdep", "Memory Dependence Analysis");
00497 
00498 
00499 } // End llvm namespace