1 //===- DependenceGraph.h - Dependence graph 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 // This file provides an explicit representation for the dependence graph
11 // of a function, with one node per instruction and one edge per dependence.
12 // Dependences include both data and control dependences.
14 // Each dep. graph node (class DepGraphNode) keeps lists of incoming and
15 // outgoing dependence edges.
17 // Each dep. graph edge (class Dependence) keeps a pointer to one end-point
18 // of the dependence. This saves space and is important because dep. graphs
19 // can grow quickly. It works just fine because the standard idiom is to
20 // start with a known node and enumerate the dependences to or from that node.
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_ANALYSIS_DEPENDENCEGRAPH_H
25 #define LLVM_ANALYSIS_DEPENDENCEGRAPH_H
27 #include "Support/hash_map"
39 class DependenceGraph;
42 //----------------------------------------------------------------------------
43 // enum DependenceType: The standard data dependence types.
44 //----------------------------------------------------------------------------
50 OutputDependence = 0x4,
51 ControlDependence = 0x8, // from a terminator to some other instr.
52 IncomingFlag = 0x10 // is this an incoming or outgoing dep?
56 //----------------------------------------------------------------------------
59 // A representation of a simple (non-loop-related) dependence.
60 //----------------------------------------------------------------------------
63 DepGraphNode* toOrFromNode;
64 unsigned char depType;
67 Dependence(DepGraphNode* toOrFromN, DependenceType type, bool isIncoming)
68 : toOrFromNode(toOrFromN),
69 depType(type | (isIncoming? IncomingFlag : 0x0)) { }
71 /* copy ctor*/ Dependence (const Dependence& D)
72 : toOrFromNode(D.toOrFromNode),
73 depType(D.depType) { }
75 bool operator==(const Dependence& D) const {
76 return toOrFromNode == D.toOrFromNode && depType == D.depType;
79 /// Get information about the type of dependence.
81 unsigned getDepType() const {
85 /// Get source or sink depending on what type of node this is!
87 DepGraphNode* getSrc() {
88 assert(depType & IncomingFlag); return toOrFromNode;
90 const DepGraphNode* getSrc() const {
91 assert(depType & IncomingFlag); return toOrFromNode;
94 DepGraphNode* getSink() {
95 assert(! (depType & IncomingFlag)); return toOrFromNode;
97 const DepGraphNode* getSink() const {
98 assert(! (depType & IncomingFlag)); return toOrFromNode;
101 /// Debugging support methods
103 void print(std::ostream &O) const;
105 // Default constructor: Do not use directly except for graph builder code
107 /*ctor*/ Dependence() : toOrFromNode(NULL), depType(NoDependence) { }
111 #ifdef SUPPORTING_LOOP_DEPENDENCES
112 struct LoopDependence: public Dependence {
113 DependenceDirection dir;
116 LoopInfo* enclosingLoop;
121 //----------------------------------------------------------------------------
122 // class DepGraphNode:
124 // A representation of a single node in a dependence graph, corresponding
125 // to a single instruction.
126 //----------------------------------------------------------------------------
130 std::vector<Dependence> inDeps;
131 std::vector<Dependence> outDeps;
132 friend class DependenceGraph;
134 typedef std::vector<Dependence>:: iterator iterator;
135 typedef std::vector<Dependence>::const_iterator const_iterator;
137 iterator inDepBegin() { return inDeps.begin(); }
138 const_iterator inDepBegin() const { return inDeps.begin(); }
139 iterator inDepEnd() { return inDeps.end(); }
140 const_iterator inDepEnd() const { return inDeps.end(); }
142 iterator outDepBegin() { return outDeps.begin(); }
143 const_iterator outDepBegin() const { return outDeps.begin(); }
144 iterator outDepEnd() { return outDeps.end(); }
145 const_iterator outDepEnd() const { return outDeps.end(); }
149 DepGraphNode(Instruction& I) : instr(&I) { }
151 Instruction& getInstr() { return *instr; }
152 const Instruction& getInstr() const { return *instr; }
154 /// Debugging support methods
156 void print(std::ostream &O) const;
160 //----------------------------------------------------------------------------
161 // class DependenceGraph:
163 // A representation of a dependence graph for a procedure.
164 // The primary query operation here is to look up a DepGraphNode for
165 // a particular instruction, and then use the in/out dependence iterators
167 //----------------------------------------------------------------------------
169 class DependenceGraph {
170 DependenceGraph(const DependenceGraph&); // DO NOT IMPLEMENT
171 void operator=(const DependenceGraph&); // DO NOT IMPLEMENT
173 typedef hash_map<Instruction*, DepGraphNode*> DepNodeMapType;
174 typedef DepNodeMapType:: iterator map_iterator;
175 typedef DepNodeMapType::const_iterator const_map_iterator;
177 DepNodeMapType depNodeMap;
179 inline DepGraphNode* getNodeInternal(Instruction& inst,
180 bool createIfMissing = false) {
181 map_iterator I = depNodeMap.find(&inst);
182 if (I == depNodeMap.end())
183 return (!createIfMissing)? NULL :
185 std::make_pair(&inst, new DepGraphNode(inst))).first->second;
191 typedef std::vector<Dependence>:: iterator iterator;
192 typedef std::vector<Dependence>::const_iterator const_iterator;
195 DependenceGraph() { }
198 /// Get the graph node for an instruction. There will be one if and
199 /// only if there are any dependences incident on this instruction.
200 /// If there is none, these methods will return NULL.
202 DepGraphNode* getNode(Instruction& inst, bool createIfMissing = false) {
203 return getNodeInternal(inst, createIfMissing);
205 const DepGraphNode* getNode(const Instruction& inst) const {
206 return const_cast<DependenceGraph*>(this)
207 ->getNodeInternal(const_cast<Instruction&>(inst));
210 iterator inDepBegin(DepGraphNode& T) {
211 return T.inDeps.begin();
213 const_iterator inDepBegin (const DepGraphNode& T) const {
214 return T.inDeps.begin();
217 iterator inDepEnd(DepGraphNode& T) {
218 return T.inDeps.end();
220 const_iterator inDepEnd(const DepGraphNode& T) const {
221 return T.inDeps.end();
224 iterator outDepBegin(DepGraphNode& F) {
225 return F.outDeps.begin();
227 const_iterator outDepBegin(const DepGraphNode& F) const {
228 return F.outDeps.begin();
231 iterator outDepEnd(DepGraphNode& F) {
232 return F.outDeps.end();
234 const_iterator outDepEnd(const DepGraphNode& F) const {
235 return F.outDeps.end();
238 /// Debugging support methods
240 void print(const Function& func, std::ostream &O) const;
243 /// Functions for adding and modifying the dependence graph.
244 /// These should to be used only by dependence analysis implementations.
245 void AddSimpleDependence(Instruction& fromI,
247 DependenceType depType) {
248 DepGraphNode* fromNode = getNodeInternal(fromI, /*create*/ true);
249 DepGraphNode* toNode = getNodeInternal(toI, /*create*/ true);
250 fromNode->outDeps.push_back(Dependence(toNode, depType, false));
251 toNode-> inDeps. push_back(Dependence(fromNode, depType, true));
254 #ifdef SUPPORTING_LOOP_DEPENDENCES
255 /// This interface is a placeholder to show what information is needed.
256 /// It will probably change when it starts being used.
257 void AddLoopDependence(Instruction& fromI,
259 DependenceType depType,
260 DependenceDirection dir,
263 LoopInfo* enclosingLoop);
264 #endif // SUPPORTING_LOOP_DEPENDENCES
267 //===----------------------------------------------------------------------===//
269 } // End llvm namespace