1 //===- DependenceGraph.h - Dependence graph for a function ------*- C++ -*-===//
3 // This file provides an explicit representation for the dependence graph
4 // of a function, with one node per instruction and one edge per dependence.
5 // Dependences include both data and control dependences.
7 // Each dep. graph node (class DepGraphNode) keeps lists of incoming and
8 // outgoing dependence edges.
10 // Each dep. graph edge (class Dependence) keeps a pointer to one end-point
11 // of the dependence. This saves space and is important because dep. graphs
12 // can grow quickly. It works just fine because the standard idiom is to
13 // start with a known node and enumerate the dependences to or from that node.
15 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ANALYSIS_DEPENDENCEGRAPH_H
19 #define LLVM_ANALYSIS_DEPENDENCEGRAPH_H
21 #include "Support/hash_map"
32 class DependenceGraph;
35 //----------------------------------------------------------------------------
36 // enum DependenceType: The standard data dependence types.
37 //----------------------------------------------------------------------------
43 OutputDependence = 0x4,
44 ControlDependence = 0x8, // from a terminator to some other instr.
45 IncomingFlag = 0x10 // is this an incoming or outgoing dep?
49 //----------------------------------------------------------------------------
52 // A representation of a simple (non-loop-related) dependence.
53 //----------------------------------------------------------------------------
56 DepGraphNode* toOrFromNode;
57 unsigned char depType;
60 Dependence(DepGraphNode* toOrFromN, DependenceType type, bool isIncoming)
61 : toOrFromNode(toOrFromN),
62 depType(type | (isIncoming? IncomingFlag : 0x0)) { }
64 /* copy ctor*/ Dependence (const Dependence& D)
65 : toOrFromNode(D.toOrFromNode),
66 depType(D.depType) { }
68 bool operator==(const Dependence& D) const {
69 return toOrFromNode == D.toOrFromNode && depType == D.depType;
72 /// Get information about the type of dependence.
74 unsigned getDepType() const {
78 /// Get source or sink depending on what type of node this is!
80 DepGraphNode* getSrc() {
81 assert(depType & IncomingFlag); return toOrFromNode;
83 const DepGraphNode* getSrc() const {
84 assert(depType & IncomingFlag); return toOrFromNode;
87 DepGraphNode* getSink() {
88 assert(! (depType & IncomingFlag)); return toOrFromNode;
90 const DepGraphNode* getSink() const {
91 assert(! (depType & IncomingFlag)); return toOrFromNode;
94 /// Debugging support methods
96 void print(std::ostream &O) const;
98 // Default constructor: Do not use directly except for graph builder code
100 /*ctor*/ Dependence() : toOrFromNode(NULL), depType(NoDependence) { }
104 #ifdef SUPPORTING_LOOP_DEPENDENCES
105 struct LoopDependence: public Dependence {
106 DependenceDirection dir;
109 LoopInfo* enclosingLoop;
114 //----------------------------------------------------------------------------
115 // class DepGraphNode:
117 // A representation of a single node in a dependence graph, corresponding
118 // to a single instruction.
119 //----------------------------------------------------------------------------
123 std::vector<Dependence> inDeps;
124 std::vector<Dependence> outDeps;
125 friend class DependenceGraph;
127 typedef std::vector<Dependence>:: iterator iterator;
128 typedef std::vector<Dependence>::const_iterator const_iterator;
130 iterator inDepBegin() { return inDeps.begin(); }
131 const_iterator inDepBegin() const { return inDeps.begin(); }
132 iterator inDepEnd() { return inDeps.end(); }
133 const_iterator inDepEnd() const { return inDeps.end(); }
135 iterator outDepBegin() { return outDeps.begin(); }
136 const_iterator outDepBegin() const { return outDeps.begin(); }
137 iterator outDepEnd() { return outDeps.end(); }
138 const_iterator outDepEnd() const { return outDeps.end(); }
142 DepGraphNode(Instruction& I) : instr(&I) { }
144 Instruction& getInstr() { return *instr; }
145 const Instruction& getInstr() const { return *instr; }
147 /// Debugging support methods
149 void print(std::ostream &O) const;
153 //----------------------------------------------------------------------------
154 // class DependenceGraph:
156 // A representation of a dependence graph for a procedure.
157 // The primary query operation here is to look up a DepGraphNode for
158 // a particular instruction, and then use the in/out dependence iterators
160 //----------------------------------------------------------------------------
162 class DependenceGraph {
163 DependenceGraph(const DependenceGraph&); // DO NOT IMPLEMENT
164 void operator=(const DependenceGraph&); // DO NOT IMPLEMENT
166 typedef hash_map<Instruction*, DepGraphNode*> DepNodeMapType;
167 typedef DepNodeMapType:: iterator map_iterator;
168 typedef DepNodeMapType::const_iterator const_map_iterator;
170 DepNodeMapType depNodeMap;
172 inline DepGraphNode* getNodeInternal(Instruction& inst,
173 bool createIfMissing = false) {
174 map_iterator I = depNodeMap.find(&inst);
175 if (I == depNodeMap.end())
176 return (!createIfMissing)? NULL :
178 std::make_pair(&inst, new DepGraphNode(inst))).first->second;
184 typedef std::vector<Dependence>:: iterator iterator;
185 typedef std::vector<Dependence>::const_iterator const_iterator;
188 DependenceGraph() { }
191 /// Get the graph node for an instruction. There will be one if and
192 /// only if there are any dependences incident on this instruction.
193 /// If there is none, these methods will return NULL.
195 DepGraphNode* getNode(Instruction& inst, bool createIfMissing = false) {
196 return getNodeInternal(inst, createIfMissing);
198 const DepGraphNode* getNode(const Instruction& inst) const {
199 return const_cast<DependenceGraph*>(this)
200 ->getNodeInternal(const_cast<Instruction&>(inst));
203 iterator inDepBegin(DepGraphNode& T) {
204 return T.inDeps.begin();
206 const_iterator inDepBegin (const DepGraphNode& T) const {
207 return T.inDeps.begin();
210 iterator inDepEnd(DepGraphNode& T) {
211 return T.inDeps.end();
213 const_iterator inDepEnd(const DepGraphNode& T) const {
214 return T.inDeps.end();
217 iterator outDepBegin(DepGraphNode& F) {
218 return F.outDeps.begin();
220 const_iterator outDepBegin(const DepGraphNode& F) const {
221 return F.outDeps.begin();
224 iterator outDepEnd(DepGraphNode& F) {
225 return F.outDeps.end();
227 const_iterator outDepEnd(const DepGraphNode& F) const {
228 return F.outDeps.end();
231 /// Debugging support methods
233 void print(const Function& func, std::ostream &O) const;
236 /// Functions for adding and modifying the dependence graph.
237 /// These should to be used only by dependence analysis implementations.
238 void AddSimpleDependence(Instruction& fromI,
240 DependenceType depType) {
241 DepGraphNode* fromNode = getNodeInternal(fromI, /*create*/ true);
242 DepGraphNode* toNode = getNodeInternal(toI, /*create*/ true);
243 fromNode->outDeps.push_back(Dependence(toNode, depType, false));
244 toNode-> inDeps. push_back(Dependence(fromNode, depType, true));
247 #ifdef SUPPORTING_LOOP_DEPENDENCES
248 /// This interface is a placeholder to show what information is needed.
249 /// It will probably change when it starts being used.
250 void AddLoopDependence(Instruction& fromI,
252 DependenceType depType,
253 DependenceDirection dir,
256 LoopInfo* enclosingLoop);
257 #endif // SUPPORTING_LOOP_DEPENDENCES
260 //===----------------------------------------------------------------------===//