1 //===- MemoryDepAnalysis.cpp - Compute dep graph for memory ops --*-C++-*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 // The result of this pass is a DependenceGraph for each function
16 // representing the memory-based data dependences between instructions.
17 //===----------------------------------------------------------------------===//
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"
36 ///--------------------------------------------------------------------------
37 /// struct ModRefTable:
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].
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 ///--------------------------------------------------------------------------
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;
60 std::vector<Instruction*> definers;
61 std::vector<Instruction*> users;
62 std::vector<unsigned> numUsersBeforeDef;
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(); }
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(); }
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.
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];
89 const_ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) const {
90 return const_cast<ModRefTable*>(this)->usersBeforeDef_End(defPtr);
96 void AddDef(Instruction* D) {
97 definers.push_back(D);
98 numUsersBeforeDef.push_back(users.size());
100 void AddUse(Instruction* U) {
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 */
114 ///--------------------------------------------------------------------------
115 /// class ModRefInfoBuilder:
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 ///--------------------------------------------------------------------------
123 class ModRefInfoBuilder : public InstVisitor<ModRefInfoBuilder> {
124 const DSGraph& funcGraph;
125 const FunctionModRefInfo& funcModRef;
126 struct ModRefTable& modRefTable;
128 ModRefInfoBuilder(); // DO NOT IMPLEMENT
129 ModRefInfoBuilder(const ModRefInfoBuilder&); // DO NOT IMPLEMENT
130 void operator=(const ModRefInfoBuilder&); // DO NOT IMPLEMENT
133 /*ctor*/ ModRefInfoBuilder(const DSGraph& _funcGraph,
134 const FunctionModRefInfo& _funcModRef,
135 ModRefTable& _modRefTable)
136 : funcGraph(_funcGraph), funcModRef(_funcModRef), modRefTable(_modRefTable)
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.
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;
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);
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())
170 unsigned nodeId = funcModRef.getNodeId(target);
172 modRefTable.modRefMap.insert(
173 std::make_pair(&storeInst,
174 ModRefInfo(funcGraph.getGraphSize()))).first->second;
175 minfo.setNodeIsMod(nodeId);
176 modRefTable.AddDef(&storeInst);
179 std::cerr << "Warning: Uninitialized pointer reference!\n";
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())
190 unsigned nodeId = funcModRef.getNodeId(target);
192 modRefTable.modRefMap.insert(
193 std::make_pair(&loadInst,
194 ModRefInfo(funcGraph.getGraphSize()))).first->second;
195 minfo.setNodeIsRef(nodeId);
196 modRefTable.AddUse(&loadInst);
199 std::cerr << "Warning: Uninitialized pointer reference!\n";
204 //----------------------------------------------------------------------------
205 // class MemoryDepAnalysis: A dep. graph for load/store/call instructions
206 //----------------------------------------------------------------------------
209 /// getAnalysisUsage - This does not modify anything. It uses the Top-Down DS
210 /// Graph and IPModRef.
212 void MemoryDepAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
213 AU.setPreservesAll();
214 AU.addRequired<TDDataStructures>();
215 AU.addRequired<IPModRef>();
219 /// Basic dependence gathering algorithm, using scc_iterator on CFG:
221 /// for every SCC S in the CFG in PostOrder on the SCC DAG
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 }
231 /// for every def D in DefSetCurrent
233 /// // NOTE: D comes after itself iff S contains a loop
234 /// if (HasLoop(S) && D & D)
235 /// Add output-dep: D -> D2
237 /// for every def D2 *after* D in DefSetCurrent
238 /// // NOTE: D2 comes before D in execution order
240 /// Add output-dep: D2 -> D
242 /// Add output-dep: D -> D2
244 /// for every use U in UseSetCurrent that was seen *before* D
245 /// // NOTE: U comes after D in execution order
247 /// if (U != D || HasLoop(S))
248 /// Add true-dep: D -> U
250 /// Add anti-dep: U -> D
252 /// for every use U in UseSetCurrent that was seen *after* D
253 /// // NOTE: U comes before D in execution order
255 /// if (U != D || HasLoop(S))
256 /// Add anti-dep: U -> D
258 /// Add true-dep: D -> U
260 /// for every def Dnext in DefSetAfter
261 /// // NOTE: Dnext comes after D in execution order
263 /// Add output-dep: D -> Dnext
265 /// for every use Unext in UseSetAfter
266 /// // NOTE: Unext comes after D in execution order
268 /// Add true-dep: D -> Unext
270 /// for every use U in UseSetCurrent
271 /// for every def Dnext in DefSetAfter
272 /// // NOTE: Dnext comes after U in execution order
274 /// Add anti-dep: U -> Dnext
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 }
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;
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:
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
298 ModRefInfoBuilder builder(*funcGraph, *funcModRef, ModRefCurrent);
299 for (std::vector<BasicBlock*>::iterator BI = S.begin(), BE = S.end();
301 // Note: BBs in the SCC<> created by scc_iterator are in postorder.
302 for (BasicBlock::reverse_iterator II=(*BI)->rbegin(), IE=(*BI)->rend();
306 /// for every def D in DefSetCurrent
308 for (ModRefTable::ref_iterator II=ModRefCurrent.defsBegin(),
309 IE=ModRefCurrent.defsEnd(); II != IE; ++II)
311 /// // NOTE: D comes after itself iff S contains a loop
313 /// Add output-dep: D -> D2
315 funcDepGraph->AddSimpleDependence(**II, **II, OutputDependence);
317 /// for every def D2 *after* D in DefSetCurrent
318 /// // NOTE: D2 comes before D in execution order
320 /// Add output-dep: D2 -> D
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()))
327 funcDepGraph->AddSimpleDependence(**JI, **II, OutputDependence);
329 funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence);
332 /// for every use U in UseSetCurrent that was seen *before* D
333 /// // NOTE: U comes after D in execution order
335 /// if (U != D || HasLoop(S))
336 /// Add true-dep: U -> D
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()))
345 if (*II != *JI || hasLoop)
346 funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
348 funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
351 /// for every use U in UseSetCurrent that was seen *after* D
352 /// // NOTE: U comes before D in execution order
354 /// if (U != D || HasLoop(S))
355 /// Add anti-dep: U -> D
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()))
362 if (*II != *JI || hasLoop)
363 funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence);
365 funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence);
368 /// for every def Dnext in DefSetPrev
369 /// // NOTE: Dnext comes after D in execution order
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);
378 /// for every use Unext in UseSetAfter
379 /// // NOTE: Unext comes after D in execution order
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);
390 /// for every use U in UseSetCurrent
391 /// for every def Dnext in DefSetAfter
392 /// // NOTE: Dnext comes after U in execution order
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);
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);
410 /// Debugging support methods
412 void MemoryDepAnalysis::print(std::ostream &O) const
415 for (hash_map<Function*, DependenceGraph*>::const_iterator
416 I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
418 Function* func = I->first;
419 DependenceGraph* depGraph = I->second;
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);
431 /// Run the pass on a function
433 bool MemoryDepAnalysis::runOnFunction(Function &F) {
434 assert(!F.isExternal());
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.
440 funcModRef = &getAnalysis<IPModRef>().getFunctionModRefInfo(F);
441 funcGraph = &funcModRef->getFuncGraph();
443 // TEMPORARY: ptr to depGraph (later just becomes "this").
444 assert(!funcMap.count(&F) && "Analyzing function twice?");
445 funcDepGraph = funcMap[&F] = new DependenceGraph();
447 ModRefTable ModRefAfter;
449 for (scc_iterator<Function*> I = scc_begin(&F), E = scc_end(&F); I != E; ++I)
450 ProcessSCC(*I, ModRefAfter, I.hasLoop());
456 //-------------------------------------------------------------------------
457 // TEMPORARY FUNCTIONS TO MAKE THIS A MODULE PASS ---
458 // These functions will go away once this class becomes a FunctionPass.
461 // Driver function to compute dependence graphs for every function.
462 // This is temporary and will go away once this is a FunctionPass.
464 bool MemoryDepAnalysis::run(Module& M)
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
472 // Release all the dependence graphs in the map.
473 void MemoryDepAnalysis::releaseMemory()
475 for (hash_map<Function*, DependenceGraph*>::const_iterator
476 I = funcMap.begin(), E = funcMap.end(); I != E; ++I)
480 // Clear pointers because the pass constructor will not be invoked again.
486 MemoryDepAnalysis::~MemoryDepAnalysis()
491 //----END TEMPORARY FUNCTIONS----------------------------------------------
494 void MemoryDepAnalysis::dump() const
496 this->print(std::cerr);
499 static RegisterAnalysis<MemoryDepAnalysis>
500 Z("memdep", "Memory Dependence Analysis");
503 } // End llvm namespace