1 //===- PgmDependenceGraph.h - Enumerate the 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.
28 // enum PDGIteratorFlags -- Specify which dependences to enumerate.
30 // class PDGIterator -- The PDG iterator. This is essentially like a
31 // pointer to class Dependence, but doesn't explicitly
32 // construct a Dependence object for each dependence.
34 // class PgmDependenceGraph -- Interface to obtain PDGIterators for each
37 //===----------------------------------------------------------------------===//
39 #ifndef LLVM_ANALYSIS_PGMDEPENDENCEGRAPH_H
40 #define LLVM_ANALYSIS_PGMDEPENDENCEGRAPH_H
42 #include "llvm/Analysis/DependenceGraph.h"
43 #include "llvm/Analysis/MemoryDepAnalysis.h"
44 /* #include "llvm/Analysis/PostDominators.h" -- see below */
45 #include "llvm/Instruction.h"
46 #include "llvm/Pass.h"
47 #include "Support/iterator"
52 class DependenceGraph;
53 class PgmDependenceGraph;
56 ///---------------------------------------------------------------------------
57 /// enum PDGIteratorFlags
59 /// These bit flags specify which dependences incident on a statement are to be
60 /// enumerated: Memory deps, SSA deps, Control deps, or any combination thereof.
61 ///---------------------------------------------------------------------------
63 enum PDGIteratorFlags {
64 MemoryDeps = 0x1, // load/store/call deps
65 SSADeps = 0x2, // SSA deps (true)
66 ControlDeps = /* 0x4*/ 0x0, // control dependences
67 AllDataDeps = MemoryDeps | SSADeps, // shorthand for data deps
68 AllDeps = MemoryDeps | SSADeps | ControlDeps // shorthand for all three
72 ///---------------------------------------------------------------------------
73 /// struct DepIterState
75 /// This data type is primarily an internal implementation detail.
76 /// It are exposed here only to give inlinable access to field dep,
77 /// which is the representation for the current dependence pointed to by
78 /// a PgmDependenceGraph::iterator.
79 ///---------------------------------------------------------------------------
83 typedef char IterStateFlags;
84 static const IterStateFlags NoFlag, MemDone, SSADone, AllDone, FirstTimeFlag;
87 DepGraphNode* depNode; // the node being enumerated
88 DependenceGraph::iterator memDepIter; // pointer to current memory dep
89 Instruction::op_iterator ssaInEdgeIter; // pointer to current SSA in-dep
90 Value::use_iterator ssaOutEdgeIter; // pointer to current SSA out-dep
91 DependenceGraph* memDepGraph; // the core dependence graph
92 Dependence dep; // the "current" dependence
93 PDGIteratorFlags depFlags:8; // which deps are we enumerating?
94 IterStateFlags iterFlags:8; // marking where the iter stands
96 /*ctor*/ DepIterState (DependenceGraph* _memDepGraph,
99 PDGIteratorFlags whichDeps);
101 bool operator==(const DepIterState& S) {
102 assert(memDepGraph == S.memDepGraph &&
103 "Incompatible iterators! This is a probable sign of something BAD.");
104 return (iterFlags == S.iterFlags &&
105 dep == S.dep && depFlags == S.depFlags && depNode == S.depNode &&
106 memDepIter == S.memDepIter && ssaInEdgeIter == S.ssaInEdgeIter &&
107 ssaOutEdgeIter == S.ssaOutEdgeIter);
110 // Is the iteration completely done?
112 bool done () const { return iterFlags & AllDone; }
114 // Bump this iterator logically by 1 (to next dependence) and reset the
115 // dep field to represent the new dependence if there is one.
116 // Set done = true otherwise.
120 // Find the first memory dependence for the current Mem In/Out iterators.
121 // Sets dep to that dependence and returns true if one is found.
122 // Returns false and leaves dep unchanged otherwise.
124 bool SetFirstMemoryDep();
126 // Find the next valid data dependence for the current SSA In/Out iterators.
127 // A valid data dependence is one that is to/from an Instruction.
128 // E.g., an SSA edge from a formal parameter is not a valid dependence.
129 // Sets dep to that dependence and returns true if a valid one is found.
130 // Returns false and leaves dep unchanged otherwise.
132 bool SetFirstSSADep ();
136 ///---------------------------------------------------------------------------
137 /// The dependence iterator class. This class represents a pointer to
138 /// a single dependence in the program dependence graph. It is essentially
139 /// like a pointer to an object of class Dependence but it is much more
140 /// efficient to retrieve information about the dependence directly rather
141 /// than constructing the equivalent Dependence object (since that object
142 /// is normally not constructed for SSA def-use dependences).
143 ///---------------------------------------------------------------------------
145 class PDGIterator: public forward_iterator<Dependence, ptrdiff_t> {
146 DepIterState* istate;
149 /*copy*/ PDGIterator (const PDGIterator& I); // do not implement!
150 PDGIterator& operator= (const PDGIterator& I); // do not implement!
152 /*copy*/ PDGIterator (PDGIterator& I) : istate(I.istate) {
153 I.istate = NULL; // ensure this is not deleted twice.
157 friend class PgmDependenceGraph;
160 typedef PDGIterator _Self;
162 /*ctor*/ PDGIterator (DepIterState* _istate) : istate(_istate) { }
163 /*dtor*/ ~PDGIterator () { delete istate; }
165 /*copy*/ PDGIterator (const PDGIterator& I)
166 : istate(new DepIterState(*I.istate)) { }
168 PDGIterator& operator= (const PDGIterator& I) {
169 if (istate) delete istate;
170 istate = new DepIterState(*I.istate);
174 // Check if the iteration is complete
176 bool fini() const { return !istate || istate->done(); }
178 // Retrieve the underlying Dependence. Returns NULL if fini().
180 Dependence* operator*() const { return fini() ? NULL : &istate->dep; }
181 Dependence* operator->() const { assert(!fini()); return &istate->dep; }
183 // Increment the iterator
185 _Self& operator++() { if (!fini()) istate->Next(); return *this;}
186 _Self& operator++(int); // do not implement!
188 // Equality comparison: a "null" state should compare equal to done
189 // This is efficient for comparing with "end" or with itself, but could
190 // be quite inefficient for other cases.
192 bool operator==(const PDGIterator& I) const {
193 if (I.istate == NULL) // most common case: iter == end()
194 return (istate == NULL || istate->done());
196 return (I.istate == NULL || I.istate->done());
197 return (*istate == *I.istate);
199 bool operator!=(const PDGIterator& I) const {
200 return ! (*this == I);
205 ///---------------------------------------------------------------------------
206 /// class PgmDependenceGraph:
208 /// This pass enumerates dependences incident on each instruction in a function.
209 /// It can be made a FunctionPass once a Pass (such as Parallelize) is
210 /// allowed to use a FunctionPass such as this one.
211 ///---------------------------------------------------------------------------
213 class PgmDependenceGraph: public Pass {
215 /// Information about the function being analyzed.
217 DependenceGraph* memDepGraph;
219 // print helper function.
220 void printOutgoingSSADeps(Instruction& I, std::ostream &O);
223 // The first version creates and initializes an iterator as specified.
224 // The second version creates a null iterator representing end-of-iteration.
226 PDGIterator MakeIterator (Instruction& I,
228 PDGIteratorFlags whichDeps);
230 PDGIterator MakeIterator () { return PDGIterator(NULL); }
232 friend class PDGIterator;
233 friend class DepIterState;
236 typedef PDGIterator iterator;
237 /* typedef PDGIterator<const Dependence> const iterator; */
240 PgmDependenceGraph() : memDepGraph(NULL) { }
241 ~PgmDependenceGraph() { }
243 /// Iterators to enumerate the program dependence graph for a function.
244 /// Note that this does not provide "end" iterators to check for completion.
245 /// Instead, just use iterator::fini() or iterator::operator*() == NULL
247 iterator inDepBegin(Instruction& I, PDGIteratorFlags whichDeps = AllDeps) {
248 return MakeIterator(I, /*inDeps*/ true, whichDeps);
250 iterator inDepEnd (Instruction& I, PDGIteratorFlags whichDeps = AllDeps) {
251 return MakeIterator();
253 iterator outDepBegin(Instruction& I, PDGIteratorFlags whichDeps = AllDeps) {
254 return MakeIterator(I, /*inDeps*/ false, whichDeps);
256 iterator outDepEnd (Instruction& I, PDGIteratorFlags whichDeps = AllDeps) {
257 return MakeIterator();
260 ///------------------------------------------------------------------------
261 /// TEMPORARY FUNCTIONS TO MAKE THIS A MODULE PASS ---
262 /// These functions will go away once this class becomes a FunctionPass.
264 /// Driver function to compute dependence graphs for every function.
266 bool run(Module& M) { return true; }
268 /// getGraph() -- Retrieve the pgm dependence graph for a function.
269 /// This is temporary and will go away once this is a FunctionPass.
270 /// At that point, this class itself will be the PgmDependenceGraph you want.
272 PgmDependenceGraph& getGraph(Function& F) {
278 void Visiting(Function& F) {
279 memDepGraph = &getAnalysis<MemoryDepAnalysis>().getGraph(F);
282 ///----END TEMPORARY FUNCTIONS---------------------------------------------
285 /// This initializes the program dependence graph iterator for a function.
287 bool runOnFunction(Function& func) {
292 /// getAnalysisUsage - This does not modify anything.
293 /// It uses the Memory Dependence Analysis pass.
294 /// It needs to use the PostDominanceFrontier pass, but cannot because
295 /// that is a FunctionPass. This means control dependence are not emumerated.
297 void getAnalysisUsage(AnalysisUsage &AU) const {
298 AU.setPreservesAll();
299 AU.addRequired<MemoryDepAnalysis>();
300 /* AU.addRequired<PostDominanceFrontier>(); */
303 /// Debugging support methods
305 void print(std::ostream &O) const;
309 //===----------------------------------------------------------------------===//
311 } // End llvm namespace