1 //===- PgmDependenceGraph.cpp - Enumerate PDG for a function ----*- C++ -*-===//
3 // The Program Dependence Graph (PDG) for a single function represents all
4 // data and control dependences for the function. This file provides an
5 // iterator to enumerate all these dependences. In particular, it enumerates:
7 // -- Data dependences on memory locations, computed using the
8 // MemoryDepAnalysis pass;
9 // -- Data dependences on SSA registers, directly from Def-Use edges of Values;
10 // -- Control dependences, computed using postdominance frontiers
11 // (NOT YET IMPLEMENTED).
13 // Note that this file does not create an explicit dependence graph --
14 // it only provides an iterator to traverse the PDG conceptually.
15 // The MemoryDepAnalysis does build an explicit graph, which is used internally
16 // here. That graph could be augmented with the other dependences above if
17 // desired, but for most uses there will be little need to do that.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Analysis/PgmDependenceGraph.h"
22 #include "llvm/Analysis/MemoryDepAnalysis.h"
23 #include "llvm/Analysis/PostDominators.h"
24 #include "llvm/Function.h"
27 //----------------------------------------------------------------------------
29 //----------------------------------------------------------------------------
31 const DepIterState::IterStateFlags DepIterState::NoFlag = 0x0;
32 const DepIterState::IterStateFlags DepIterState::MemDone = 0x1;
33 const DepIterState::IterStateFlags DepIterState::SSADone = 0x2;
34 const DepIterState::IterStateFlags DepIterState::AllDone = 0x4;
35 const DepIterState::IterStateFlags DepIterState::FirstTimeFlag= 0x8;
37 // Find the first memory dependence for the current Mem In/Out iterators.
38 // Find the first memory dependence for the current Mem In/Out iterators.
39 // Sets dep to that dependence and returns true if one is found.
41 bool DepIterState::SetFirstMemoryDep()
43 if (! (depFlags & MemoryDeps))
46 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
48 if (( doIncomingDeps && memDepIter == memDepGraph->inDepEnd( *depNode)) ||
49 (!doIncomingDeps && memDepIter == memDepGraph->outDepEnd(*depNode)))
55 dep = *memDepIter; // simple copy from dependence in memory DepGraph
61 // Find the first valid data dependence for the current SSA In/Out iterators.
62 // A valid data dependence is one that is to/from an Instruction.
63 // E.g., an SSA edge from a formal parameter is not a valid dependence.
64 // Sets dep to that dependence and returns true if a valid one is found.
65 // Returns false and leaves dep unchanged otherwise.
67 bool DepIterState::SetFirstSSADep()
69 if (! (depFlags & SSADeps))
72 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
73 Instruction* firstTarget = NULL;
75 // Increment the In or Out iterator till it runs out or we find a valid dep
77 for (Instruction::op_iterator E = depNode->getInstr().op_end();
79 (firstTarget = dyn_cast<Instruction>(ssaInEdgeIter))== NULL; )
82 for (Value::use_iterator E = depNode->getInstr().use_end();
83 ssaOutEdgeIter != E &&
84 (firstTarget = dyn_cast<Instruction>(*ssaOutEdgeIter)) == NULL; )
87 // If the iterator ran out before we found a valid dep, there isn't one.
94 // Create a simple dependence object to represent this SSA dependence.
95 dep = Dependence(memDepGraph->getNode(*firstTarget, /*create*/ true),
96 TrueDependence, doIncomingDeps);
102 DepIterState::DepIterState(DependenceGraph* _memDepGraph,
105 PDGIteratorFlags whichDeps)
106 : memDepGraph(_memDepGraph),
110 depNode = memDepGraph->getNode(I, /*create*/ true);
114 if (whichDeps & MemoryDeps) memDepIter= memDepGraph->inDepBegin(*depNode);
115 if (whichDeps & SSADeps) ssaInEdgeIter = I.op_begin();
116 /* Initialize control dependence iterator here. */
120 if (whichDeps & MemoryDeps) memDepIter=memDepGraph->outDepBegin(*depNode);
121 if (whichDeps & SSADeps) ssaOutEdgeIter = I.use_begin();
122 /* Initialize control dependence iterator here. */
125 // Set the dependence to the first of a memory dep or an SSA dep
126 // and set the done flag if either is found. Otherwise, set the
127 // init flag to indicate that the iterators have just been initialized.
129 if (!SetFirstMemoryDep() && !SetFirstSSADep())
130 iterFlags |= AllDone;
132 iterFlags |= FirstTimeFlag;
136 // Helper function for ++ operator that bumps iterator by 1 (to next
137 // dependence) and resets the dep field to represent the new dependence.
139 void DepIterState::Next()
141 // firstMemDone and firstSsaDone are used to indicate when the memory or
142 // SSA iterators just ran out, or when this is the very first increment.
143 // In either case, the next iterator (if any) should not be incremented.
145 bool firstMemDone = iterFlags & FirstTimeFlag;
146 bool firstSsaDone = iterFlags & FirstTimeFlag;
147 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
149 if (depFlags & MemoryDeps && ! (iterFlags & MemDone))
151 iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
153 if (SetFirstMemoryDep())
155 firstMemDone = true; // flags that we _just_ rolled over
158 if (depFlags & SSADeps && ! (iterFlags & SSADone))
160 // Don't increment the SSA iterator if we either just rolled over from
161 // the memory dep iterator, or if the SSA iterator is already done.
162 iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
164 if (doIncomingDeps) ++ssaInEdgeIter;
165 else ++ssaOutEdgeIter;
166 if (SetFirstSSADep())
168 firstSsaDone = true; // flags if we just rolled over
171 if (depFlags & ControlDeps != 0)
173 assert(0 && "Cannot handle control deps");
174 // iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
177 // This iterator is now complete.
178 iterFlags |= AllDone;
182 //----------------------------------------------------------------------------
183 // class PgmDependenceGraph
184 //----------------------------------------------------------------------------
187 // MakeIterator -- Create and initialize an iterator as specified.
189 PDGIterator PgmDependenceGraph::MakeIterator(Instruction& I,
191 PDGIteratorFlags whichDeps)
193 assert(memDepGraph && "Function not initialized!");
194 return PDGIterator(new DepIterState(memDepGraph, I, incomingDeps, whichDeps));
198 void PgmDependenceGraph::printOutgoingSSADeps(Instruction& I,
201 iterator SI = this->outDepBegin(I, SSADeps);
202 iterator SE = this->outDepEnd(I, SSADeps);
206 O << "\n Outgoing SSA dependences:\n";
207 for ( ; SI != SE; ++SI)
211 O << " to instruction:";
212 O << SI->getSink()->getInstr();
217 void PgmDependenceGraph::print(std::ostream &O) const
219 MemoryDepAnalysis& graphSet = getAnalysis<MemoryDepAnalysis>();
222 for (hash_map<Function*, DependenceGraph*>::iterator
223 I = graphSet.funcMap.begin(), E = graphSet.funcMap.end();
226 Function* func = I->first;
227 DependenceGraph* depGraph = I->second;
228 const_cast<PgmDependenceGraph*>(this)->runOnFunction(*func);
230 O << "DEPENDENCE GRAPH FOR FUNCTION " << func->getName() << ":\n";
231 for (Function::iterator BB=func->begin(), FE=func->end(); BB != FE; ++BB)
232 for (BasicBlock::iterator II=BB->begin(), IE=BB->end(); II !=IE; ++II)
234 DepGraphNode* dgNode = depGraph->getNode(*II, /*create*/ true);
236 const_cast<PgmDependenceGraph*>(this)->printOutgoingSSADeps(*II, O);
238 } // END TEMPORARY LOOP
242 void PgmDependenceGraph::dump() const
244 this->print(std::cerr);
247 static RegisterAnalysis<PgmDependenceGraph>
248 Z("pgmdep", "Enumerate Program Dependence Graph (data and control)");