1 //===- PgmDependenceGraph.cpp - Enumerate PDG for a function ----*- 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 // The Program Dependence Graph (PDG) for a single function represents all
11 // data and control dependences for the function. This file provides an
12 // iterator to enumerate all these dependences. In particular, it enumerates:
14 // -- Data dependences on memory locations, computed using the
15 // MemoryDepAnalysis pass;
16 // -- Data dependences on SSA registers, directly from Def-Use edges of Values;
17 // -- Control dependences, computed using postdominance frontiers
18 // (NOT YET IMPLEMENTED).
20 // Note that this file does not create an explicit dependence graph --
21 // it only provides an iterator to traverse the PDG conceptually.
22 // The MemoryDepAnalysis does build an explicit graph, which is used internally
23 // here. That graph could be augmented with the other dependences above if
24 // desired, but for most uses there will be little need to do that.
26 //===----------------------------------------------------------------------===//
28 #include "PgmDependenceGraph.h"
29 #include "llvm/Analysis/PostDominators.h"
30 #include "llvm/Function.h"
35 //----------------------------------------------------------------------------
37 //----------------------------------------------------------------------------
39 const DepIterState::IterStateFlags DepIterState::NoFlag = 0x0;
40 const DepIterState::IterStateFlags DepIterState::MemDone = 0x1;
41 const DepIterState::IterStateFlags DepIterState::SSADone = 0x2;
42 const DepIterState::IterStateFlags DepIterState::AllDone = 0x4;
43 const DepIterState::IterStateFlags DepIterState::FirstTimeFlag= 0x8;
45 // Find the first memory dependence for the current Mem In/Out iterators.
46 // Find the first memory dependence for the current Mem In/Out iterators.
47 // Sets dep to that dependence and returns true if one is found.
49 bool DepIterState::SetFirstMemoryDep()
51 if (! (depFlags & MemoryDeps))
54 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
56 if (( doIncomingDeps && memDepIter == memDepGraph->inDepEnd( *depNode)) ||
57 (!doIncomingDeps && memDepIter == memDepGraph->outDepEnd(*depNode)))
63 dep = *memDepIter; // simple copy from dependence in memory DepGraph
69 // Find the first valid data dependence for the current SSA In/Out iterators.
70 // A valid data dependence is one that is to/from an Instruction.
71 // E.g., an SSA edge from a formal parameter is not a valid dependence.
72 // Sets dep to that dependence and returns true if a valid one is found.
73 // Returns false and leaves dep unchanged otherwise.
75 bool DepIterState::SetFirstSSADep()
77 if (! (depFlags & SSADeps))
80 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
81 Instruction* firstTarget = NULL;
83 // Increment the In or Out iterator till it runs out or we find a valid dep
85 for (Instruction::op_iterator E = depNode->getInstr().op_end();
87 (firstTarget = dyn_cast<Instruction>(ssaInEdgeIter))== NULL; )
90 for (Value::use_iterator E = depNode->getInstr().use_end();
91 ssaOutEdgeIter != E &&
92 (firstTarget = dyn_cast<Instruction>(*ssaOutEdgeIter)) == NULL; )
95 // If the iterator ran out before we found a valid dep, there isn't one.
102 // Create a simple dependence object to represent this SSA dependence.
103 dep = Dependence(memDepGraph->getNode(*firstTarget, /*create*/ true),
104 TrueDependence, doIncomingDeps);
110 DepIterState::DepIterState(DependenceGraph* _memDepGraph,
113 PDGIteratorFlags whichDeps)
114 : memDepGraph(_memDepGraph),
118 depNode = memDepGraph->getNode(I, /*create*/ true);
122 if (whichDeps & MemoryDeps) memDepIter= memDepGraph->inDepBegin(*depNode);
123 if (whichDeps & SSADeps) ssaInEdgeIter = I.op_begin();
124 /* Initialize control dependence iterator here. */
128 if (whichDeps & MemoryDeps) memDepIter=memDepGraph->outDepBegin(*depNode);
129 if (whichDeps & SSADeps) ssaOutEdgeIter = I.use_begin();
130 /* Initialize control dependence iterator here. */
133 // Set the dependence to the first of a memory dep or an SSA dep
134 // and set the done flag if either is found. Otherwise, set the
135 // init flag to indicate that the iterators have just been initialized.
137 if (!SetFirstMemoryDep() && !SetFirstSSADep())
138 iterFlags |= AllDone;
140 iterFlags |= FirstTimeFlag;
144 // Helper function for ++ operator that bumps iterator by 1 (to next
145 // dependence) and resets the dep field to represent the new dependence.
147 void DepIterState::Next()
149 // firstMemDone and firstSsaDone are used to indicate when the memory or
150 // SSA iterators just ran out, or when this is the very first increment.
151 // In either case, the next iterator (if any) should not be incremented.
153 bool firstMemDone = iterFlags & FirstTimeFlag;
154 bool firstSsaDone = iterFlags & FirstTimeFlag;
155 bool doIncomingDeps = dep.getDepType() & IncomingFlag;
157 if (depFlags & MemoryDeps && ! (iterFlags & MemDone))
159 iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
161 if (SetFirstMemoryDep())
163 firstMemDone = true; // flags that we _just_ rolled over
166 if (depFlags & SSADeps && ! (iterFlags & SSADone))
168 // Don't increment the SSA iterator if we either just rolled over from
169 // the memory dep iterator, or if the SSA iterator is already done.
170 iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
172 if (doIncomingDeps) ++ssaInEdgeIter;
173 else ++ssaOutEdgeIter;
174 if (SetFirstSSADep())
176 firstSsaDone = true; // flags if we just rolled over
179 if (depFlags & ControlDeps != 0)
181 assert(0 && "Cannot handle control deps");
182 // iterFlags &= ~FirstTimeFlag; // clear "firstTime" flag
185 // This iterator is now complete.
186 iterFlags |= AllDone;
190 //----------------------------------------------------------------------------
191 // class PgmDependenceGraph
192 //----------------------------------------------------------------------------
195 // MakeIterator -- Create and initialize an iterator as specified.
197 PDGIterator PgmDependenceGraph::MakeIterator(Instruction& I,
199 PDGIteratorFlags whichDeps)
201 assert(memDepGraph && "Function not initialized!");
202 return PDGIterator(new DepIterState(memDepGraph, I, incomingDeps, whichDeps));
206 void PgmDependenceGraph::printOutgoingSSADeps(Instruction& I,
209 iterator SI = this->outDepBegin(I, SSADeps);
210 iterator SE = this->outDepEnd(I, SSADeps);
214 O << "\n Outgoing SSA dependences:\n";
215 for ( ; SI != SE; ++SI)
219 O << " to instruction:";
220 O << SI->getSink()->getInstr();
225 void PgmDependenceGraph::print(std::ostream &O, const Module*) const
227 MemoryDepAnalysis& graphSet = getAnalysis<MemoryDepAnalysis>();
230 for (hash_map<Function*, DependenceGraph*>::iterator
231 I = graphSet.funcMap.begin(), E = graphSet.funcMap.end();
234 Function* func = I->first;
235 DependenceGraph* depGraph = I->second;
236 const_cast<PgmDependenceGraph*>(this)->runOnFunction(*func);
238 O << "DEPENDENCE GRAPH FOR FUNCTION " << func->getName() << ":\n";
239 for (Function::iterator BB=func->begin(), FE=func->end(); BB != FE; ++BB)
240 for (BasicBlock::iterator II=BB->begin(), IE=BB->end(); II !=IE; ++II)
242 DepGraphNode* dgNode = depGraph->getNode(*II, /*create*/ true);
244 const_cast<PgmDependenceGraph*>(this)->printOutgoingSSADeps(*II, O);
246 } // END TEMPORARY LOOP
250 void PgmDependenceGraph::dump() const
252 this->print(std::cerr);
255 static RegisterAnalysis<PgmDependenceGraph>
256 Z("pgmdep", "Enumerate Program Dependence Graph (data and control)");
258 } // End llvm namespace