Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Analysis / DataStructure / MemoryDepAnalysis.cpp
1 //===- MemoryDepAnalysis.cpp - Compute dep graph for memory ops --*-C++-*--===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a pass (MemoryDepAnalysis) that computes memory-based
11 // data dependences between instructions for each function in a module.  
12 // Memory-based dependences occur due to load and store operations, but
13 // also the side-effects of call instructions.
14 //
15 // The result of this pass is a DependenceGraph for each function
16 // representing the memory-based data dependences between instructions.
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Analysis/MemoryDepAnalysis.h"
20 #include "llvm/Analysis/IPModRef.h"
21 #include "llvm/Analysis/DataStructure.h"
22 #include "llvm/Analysis/DSGraph.h"
23 #include "llvm/Module.h"
24 #include "llvm/iMemory.h"
25 #include "llvm/iOther.h"
26 #include "llvm/Support/InstVisitor.h"
27 #include "llvm/Support/CFG.h"
28 #include "Support/SCCIterator.h"
29 #include "Support/Statistic.h"
30 #include "Support/STLExtras.h"
31 #include "Support/hash_map"
32 #include "Support/hash_set"
33
34 namespace llvm {
35
36 ///--------------------------------------------------------------------------
37 /// struct ModRefTable:
38 /// 
39 /// A data structure that tracks ModRefInfo for instructions:
40 ///   -- modRefMap is a map of Instruction* -> ModRefInfo for the instr.
41 ///   -- definers  is a vector of instructions that define    any node
42 ///   -- users     is a vector of instructions that reference any node
43 ///   -- numUsersBeforeDef is a vector indicating that the number of users
44 ///                seen before definers[i] is numUsersBeforeDef[i].
45 /// 
46 /// numUsersBeforeDef[] effectively tells us the exact interleaving of
47 /// definers and users within the ModRefTable.
48 /// This is only maintained when constructing the table for one SCC, and
49 /// not copied over from one table to another since it is no longer useful.
50 ///--------------------------------------------------------------------------
51
52 struct ModRefTable {
53   typedef hash_map<Instruction*, ModRefInfo> ModRefMap;
54   typedef ModRefMap::const_iterator                 const_map_iterator;
55   typedef ModRefMap::      iterator                        map_iterator;
56   typedef std::vector<Instruction*>::const_iterator const_ref_iterator;
57   typedef std::vector<Instruction*>::      iterator       ref_iterator;
58
59   ModRefMap                 modRefMap;
60   std::vector<Instruction*> definers;
61   std::vector<Instruction*> users;
62   std::vector<unsigned>     numUsersBeforeDef;
63
64   // Iterators to enumerate all the defining instructions
65   const_ref_iterator defsBegin()  const {  return definers.begin(); }
66         ref_iterator defsBegin()        {  return definers.begin(); }
67   const_ref_iterator defsEnd()    const {  return definers.end(); }
68         ref_iterator defsEnd()          {  return definers.end(); }
69
70   // Iterators to enumerate all the user instructions
71   const_ref_iterator usersBegin() const {  return users.begin(); }
72         ref_iterator usersBegin()       {  return users.begin(); }
73   const_ref_iterator usersEnd()   const {  return users.end(); }
74         ref_iterator usersEnd()         {  return users.end(); }
75
76   // Iterator identifying the last user that was seen *before* a
77   // specified def.  In particular, all users in the half-closed range
78   //    [ usersBegin(), usersBeforeDef_End(defPtr) )
79   // were seen *before* the specified def.  All users in the half-closed range
80   //    [ usersBeforeDef_End(defPtr), usersEnd() )
81   // were seen *after* the specified def.
82   // 
83   ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) {
84     unsigned defIndex = (unsigned) (defPtr - defsBegin());
85     assert(defIndex < numUsersBeforeDef.size());
86     assert(usersBegin() + numUsersBeforeDef[defIndex] <= usersEnd()); 
87     return usersBegin() + numUsersBeforeDef[defIndex]; 
88   }
89   const_ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) const {
90     return const_cast<ModRefTable*>(this)->usersBeforeDef_End(defPtr);
91   }
92
93   // 
94   // Modifier methods
95   // 
96   void AddDef(Instruction* D) {
97     definers.push_back(D);
98     numUsersBeforeDef.push_back(users.size());
99   }
100   void AddUse(Instruction* U) {
101     users.push_back(U);
102   }
103   void Insert(const ModRefTable& fromTable) {
104     modRefMap.insert(fromTable.modRefMap.begin(), fromTable.modRefMap.end());
105     definers.insert(definers.end(),
106                     fromTable.definers.begin(), fromTable.definers.end());
107     users.insert(users.end(),
108                  fromTable.users.begin(), fromTable.users.end());
109     numUsersBeforeDef.clear(); /* fromTable.numUsersBeforeDef is ignored */
110   }
111 };
112
113
114 ///--------------------------------------------------------------------------
115 /// class ModRefInfoBuilder:
116 /// 
117 /// A simple InstVisitor<> class that retrieves the Mod/Ref info for
118 /// Load/Store/Call instructions and inserts this information in
119 /// a ModRefTable.  It also records all instructions that Mod any node
120 /// and all that use any node.
121 ///--------------------------------------------------------------------------
122
123 class ModRefInfoBuilder : public InstVisitor<ModRefInfoBuilder> {
124   const DSGraph&            funcGraph;
125   const FunctionModRefInfo& funcModRef;
126   struct ModRefTable&       modRefTable;
127
128   ModRefInfoBuilder();                         // DO NOT IMPLEMENT
129   ModRefInfoBuilder(const ModRefInfoBuilder&); // DO NOT IMPLEMENT
130   void operator=(const ModRefInfoBuilder&);    // DO NOT IMPLEMENT
131
132 public:
133   /*ctor*/      ModRefInfoBuilder(const DSGraph&  _funcGraph,
134                                   const FunctionModRefInfo& _funcModRef,
135                                   ModRefTable&    _modRefTable)
136     : funcGraph(_funcGraph), funcModRef(_funcModRef), modRefTable(_modRefTable)
137   {
138   }
139
140   // At a call instruction, retrieve the ModRefInfo using IPModRef results.
141   // Add the call to the defs list if it modifies any nodes and to the uses
142   // list if it refs any nodes.
143   // 
144   void          visitCallInst   (CallInst& callInst) {
145     ModRefInfo safeModRef(funcGraph.getGraphSize());
146     const ModRefInfo* callModRef = funcModRef.getModRefInfo(callInst);
147     if (callModRef == NULL)
148       { // call to external/unknown function: mark all nodes as Mod and Ref
149         safeModRef.getModSet().set();
150         safeModRef.getRefSet().set();
151         callModRef = &safeModRef;
152       }
153
154     modRefTable.modRefMap.insert(std::make_pair(&callInst,
155                                                 ModRefInfo(*callModRef)));
156     if (callModRef->getModSet().any())
157       modRefTable.AddDef(&callInst);
158     if (callModRef->getRefSet().any())
159       modRefTable.AddUse(&callInst);
160   }
161
162   // At a store instruction, add to the mod set the single node pointed to
163   // by the pointer argument of the store.  Interestingly, if there is no
164   // such node, that would be a null pointer reference!
165   void          visitStoreInst  (StoreInst& storeInst) {
166     const DSNodeHandle& ptrNode =
167       funcGraph.getNodeForValue(storeInst.getPointerOperand());
168     if (const DSNode* target = ptrNode.getNode())
169       {
170         unsigned nodeId = funcModRef.getNodeId(target);
171         ModRefInfo& minfo =
172           modRefTable.modRefMap.insert(
173             std::make_pair(&storeInst,
174                            ModRefInfo(funcGraph.getGraphSize()))).first->second;
175         minfo.setNodeIsMod(nodeId);
176         modRefTable.AddDef(&storeInst);
177       }
178     else
179       std::cerr << "Warning: Uninitialized pointer reference!\n";
180   }
181
182   // At a load instruction, add to the ref set the single node pointed to
183   // by the pointer argument of the load.  Interestingly, if there is no
184   // such node, that would be a null pointer reference!
185   void          visitLoadInst  (LoadInst& loadInst) {
186     const DSNodeHandle& ptrNode =
187       funcGraph.getNodeForValue(loadInst.getPointerOperand());
188     if (const DSNode* target = ptrNode.getNode())
189       {
190         unsigned nodeId = funcModRef.getNodeId(target);
191         ModRefInfo& minfo =
192           modRefTable.modRefMap.insert(
193             std::make_pair(&loadInst,
194                            ModRefInfo(funcGraph.getGraphSize()))).first->second;
195         minfo.setNodeIsRef(nodeId);
196         modRefTable.AddUse(&loadInst);
197       }
198     else
199       std::cerr << "Warning: Uninitialized pointer reference!\n";
200   }
201 };
202
203
204 //----------------------------------------------------------------------------
205 // class MemoryDepAnalysis: A dep. graph for load/store/call instructions
206 //----------------------------------------------------------------------------
207
208
209 /// getAnalysisUsage - This does not modify anything.  It uses the Top-Down DS
210 /// Graph and IPModRef.
211 ///
212 void MemoryDepAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
213   AU.setPreservesAll();
214   AU.addRequired<TDDataStructures>();
215   AU.addRequired<IPModRef>();
216 }
217
218
219 /// Basic dependence gathering algorithm, using scc_iterator on CFG:
220 /// 
221 /// for every SCC S in the CFG in PostOrder on the SCC DAG
222 ///     {
223 ///       for every basic block BB in S in *postorder*
224 ///         for every instruction I in BB in reverse
225 ///           Add (I, ModRef[I]) to ModRefCurrent
226 ///           if (Mod[I] != NULL)
227 ///               Add I to DefSetCurrent:  { I \in S : Mod[I] != NULL }
228 ///           if (Ref[I] != NULL)
229 ///               Add I to UseSetCurrent:  { I       : Ref[I] != NULL }
230 /// 
231 ///       for every def D in DefSetCurrent
232 /// 
233 ///           // NOTE: D comes after itself iff S contains a loop
234 ///           if (HasLoop(S) && D & D)
235 ///               Add output-dep: D -> D2
236 /// 
237 ///           for every def D2 *after* D in DefSetCurrent
238 ///               // NOTE: D2 comes before D in execution order
239 ///               if (D & D2)
240 ///                   Add output-dep: D2 -> D
241 ///                   if (HasLoop(S))
242 ///                       Add output-dep: D -> D2
243 /// 
244 ///           for every use U in UseSetCurrent that was seen *before* D
245 ///               // NOTE: U comes after D in execution order
246 ///               if (U & D)
247 ///                   if (U != D || HasLoop(S))
248 ///                       Add true-dep: D -> U
249 ///                   if (HasLoop(S))
250 ///                       Add anti-dep: U -> D
251 /// 
252 ///           for every use U in UseSetCurrent that was seen *after* D
253 ///               // NOTE: U comes before D in execution order
254 ///               if (U & D)
255 ///                   if (U != D || HasLoop(S))
256 ///                       Add anti-dep: U -> D
257 ///                   if (HasLoop(S))
258 ///                       Add true-dep: D -> U
259 /// 
260 ///           for every def Dnext in DefSetAfter
261 ///               // NOTE: Dnext comes after D in execution order
262 ///               if (Dnext & D)
263 ///                   Add output-dep: D -> Dnext
264 /// 
265 ///           for every use Unext in UseSetAfter
266 ///               // NOTE: Unext comes after D in execution order
267 ///               if (Unext & D)
268 ///                   Add true-dep: D -> Unext
269 /// 
270 ///       for every use U in UseSetCurrent
271 ///           for every def Dnext in DefSetAfter
272 ///               // NOTE: Dnext comes after U in execution order
273 ///               if (Dnext & D)
274 ///                   Add anti-dep: U -> Dnext
275 /// 
276 ///       Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) }
277 ///       Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL }
278 ///       Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL }
279 ///     }
280 ///         
281 ///
282 void MemoryDepAnalysis::ProcessSCC(std::vector<BasicBlock*> &S,
283                                    ModRefTable& ModRefAfter, bool hasLoop) {
284   ModRefTable ModRefCurrent;
285   ModRefTable::ModRefMap& mapCurrent = ModRefCurrent.modRefMap;
286   ModRefTable::ModRefMap& mapAfter   = ModRefAfter.modRefMap;
287
288   // Builder class fills out a ModRefTable one instruction at a time.
289   // To use it, we just invoke it's visit function for each basic block:
290   // 
291   //   for each basic block BB in the SCC in *postorder*
292   //       for each instruction  I in BB in *reverse*
293   //           ModRefInfoBuilder::visit(I)
294   //           : Add (I, ModRef[I]) to ModRefCurrent.modRefMap
295   //           : Add I  to ModRefCurrent.definers if it defines any node
296   //           : Add I  to ModRefCurrent.users    if it uses any node
297   // 
298   ModRefInfoBuilder builder(*funcGraph, *funcModRef, ModRefCurrent);
299   for (std::vector<BasicBlock*>::iterator BI = S.begin(), BE = S.end();
300        BI != BE; ++BI)
301     // Note: BBs in the SCC<> created by scc_iterator are in postorder.
302     for (BasicBlock::reverse_iterator II=(*BI)->rbegin(), IE=(*BI)->rend();
303          II != IE; ++II)
304       builder.visit(*II);
305
306   ///       for every def D in DefSetCurrent
307   /// 
308   for (ModRefTable::ref_iterator II=ModRefCurrent.defsBegin(),
309          IE=ModRefCurrent.defsEnd(); II != IE; ++II)
310     {
311       ///           // NOTE: D comes after itself iff S contains a loop
312       ///           if (HasLoop(S))
313       ///               Add output-dep: D -> D2
314       if (hasLoop)
315         funcDepGraph->AddSimpleDependence(**II, **II, OutputDependence);
316
317       ///           for every def D2 *after* D in DefSetCurrent
318       ///               // NOTE: D2 comes before D in execution order
319       ///               if (D2 & D)
320       ///                   Add output-dep: D2 -> D
321       ///                   if (HasLoop(S))
322       ///                       Add output-dep: D -> D2
323       for (ModRefTable::ref_iterator JI=II+1; JI != IE; ++JI)
324         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
325                       mapCurrent.find(*JI)->second.getModSet()))
326           {
327             funcDepGraph->AddSimpleDependence(**JI, **II, OutputDependence);
328             if (hasLoop)
329               funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence);
330           }
331   
332       ///           for every use U in UseSetCurrent that was seen *before* D
333       ///               // NOTE: U comes after D in execution order
334       ///               if (U & D)
335       ///                   if (U != D || HasLoop(S))
336       ///                       Add true-dep: U -> D
337       ///                   if (HasLoop(S))
338       ///                       Add anti-dep: D -> U
339       ModRefTable::ref_iterator JI=ModRefCurrent.usersBegin();
340       ModRefTable::ref_iterator JE = ModRefCurrent.usersBeforeDef_End(II);
341       for ( ; JI != JE; ++JI)
342         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
343                       mapCurrent.find(*JI)->second.getRefSet()))
344           {
345             if (*II != *JI || hasLoop)
346               funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
347             if (hasLoop)
348               funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
349           }
350
351       ///           for every use U in UseSetCurrent that was seen *after* D
352       ///               // NOTE: U comes before D in execution order
353       ///               if (U & D)
354       ///                   if (U != D || HasLoop(S))
355       ///                       Add anti-dep: U -> D
356       ///                   if (HasLoop(S))
357       ///                       Add true-dep: D -> U
358       for (/*continue JI*/ JE = ModRefCurrent.usersEnd(); JI != JE; ++JI)
359         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
360                       mapCurrent.find(*JI)->second.getRefSet()))
361           {
362             if (*II != *JI || hasLoop)
363               funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
364             if (hasLoop)
365               funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
366           }
367
368       ///           for every def Dnext in DefSetPrev
369       ///               // NOTE: Dnext comes after D in execution order
370       ///               if (Dnext & D)
371       ///                   Add output-dep: D -> Dnext
372       for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(),
373              JE=ModRefAfter.defsEnd(); JI != JE; ++JI)
374         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
375                       mapAfter.find(*JI)->second.getModSet()))
376           funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence);
377
378       ///           for every use Unext in UseSetAfter
379       ///               // NOTE: Unext comes after D in execution order
380       ///               if (Unext & D)
381       ///                   Add true-dep: D -> Unext
382       for (ModRefTable::ref_iterator JI=ModRefAfter.usersBegin(),
383              JE=ModRefAfter.usersEnd(); JI != JE; ++JI)
384         if (!Disjoint(mapCurrent.find(*II)->second.getModSet(),
385                       mapAfter.find(*JI)->second.getRefSet()))
386           funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
387     }
388
389   /// 
390   ///       for every use U in UseSetCurrent
391   ///           for every def Dnext in DefSetAfter
392   ///               // NOTE: Dnext comes after U in execution order
393   ///               if (Dnext & D)
394   ///                   Add anti-dep: U -> Dnext
395   for (ModRefTable::ref_iterator II=ModRefCurrent.usersBegin(),
396          IE=ModRefCurrent.usersEnd(); II != IE; ++II)
397     for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(),
398            JE=ModRefAfter.defsEnd(); JI != JE; ++JI)
399       if (!Disjoint(mapCurrent.find(*II)->second.getRefSet(),
400                     mapAfter.find(*JI)->second.getModSet()))
401         funcDepGraph->AddSimpleDependence(**II, **JI, AntiDependence);
402     
403   ///       Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) }
404   ///       Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL }
405   ///       Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL }
406   ModRefAfter.Insert(ModRefCurrent);
407 }
408
409
410 /// Debugging support methods
411 /// 
412 void MemoryDepAnalysis::print(std::ostream &O) const
413 {
414   // TEMPORARY LOOP
415   for (hash_map<Function*, DependenceGraph*>::const_iterator
416          I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
417     {
418       Function* func = I->first;
419       DependenceGraph* depGraph = I->second;
420
421   O << "\n================================================================\n";
422   O << "DEPENDENCE GRAPH FOR MEMORY OPERATIONS IN FUNCTION " << func->getName();
423   O << "\n================================================================\n\n";
424   depGraph->print(*func, O);
425
426     }
427 }
428
429
430 /// 
431 /// Run the pass on a function
432 /// 
433 bool MemoryDepAnalysis::runOnFunction(Function &F) {
434   assert(!F.isExternal());
435
436   // Get the FunctionModRefInfo holding IPModRef results for this function.
437   // Use the TD graph recorded within the FunctionModRefInfo object, which
438   // may not be the same as the original TD graph computed by DS analysis.
439   // 
440   funcModRef = &getAnalysis<IPModRef>().getFunctionModRefInfo(F);
441   funcGraph  = &funcModRef->getFuncGraph();
442
443   // TEMPORARY: ptr to depGraph (later just becomes "this").
444   assert(!funcMap.count(&F) && "Analyzing function twice?");
445   funcDepGraph = funcMap[&F] = new DependenceGraph();
446
447   ModRefTable ModRefAfter;
448
449   for (scc_iterator<Function*> I = scc_begin(&F), E = scc_end(&F); I != E; ++I)
450     ProcessSCC(*I, ModRefAfter, I.hasLoop());
451
452   return true;
453 }
454
455
456 //-------------------------------------------------------------------------
457 // TEMPORARY FUNCTIONS TO MAKE THIS A MODULE PASS ---
458 // These functions will go away once this class becomes a FunctionPass.
459 // 
460
461 // Driver function to compute dependence graphs for every function.
462 // This is temporary and will go away once this is a FunctionPass.
463 // 
464 bool MemoryDepAnalysis::run(Module& M)
465 {
466   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
467     if (! FI->isExternal())
468       runOnFunction(*FI); // automatically inserts each depGraph into funcMap
469   return true;
470 }
471   
472 // Release all the dependence graphs in the map.
473 void MemoryDepAnalysis::releaseMemory()
474 {
475   for (hash_map<Function*, DependenceGraph*>::const_iterator
476          I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
477     delete I->second;
478   funcMap.clear();
479
480   // Clear pointers because the pass constructor will not be invoked again.
481   funcDepGraph = NULL;
482   funcGraph = NULL;
483   funcModRef = NULL;
484 }
485
486 MemoryDepAnalysis::~MemoryDepAnalysis()
487 {
488   releaseMemory();
489 }
490
491 //----END TEMPORARY FUNCTIONS----------------------------------------------
492
493
494 void MemoryDepAnalysis::dump() const
495 {
496   this->print(std::cerr);
497 }
498
499 static RegisterAnalysis<MemoryDepAnalysis>
500 Z("memdep", "Memory Dependence Analysis");
501
502
503 } // End llvm namespace