blob: 36b76324cbe903c9c077b62e19fb1167c017eac5 [file] [log] [blame]
Tanya Lattner48a503b2004-03-01 02:50:57 +00001//===-- MSchedGraph.h - Scheduling Graph ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// A graph class for dependencies
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MSCHEDGRAPH_H
15#define LLVM_MSCHEDGRAPH_H
16
17#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/GraphTraits.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/iterator"
Tanya Lattner48a503b2004-03-01 02:50:57 +000022#include <vector>
23
Tanya Lattnerddebd1e2004-10-30 00:39:07 +000024
Tanya Lattner48a503b2004-03-01 02:50:57 +000025namespace llvm {
26 class MSchedGraph;
27 class MSchedGraphNode;
28 template<class IteratorType, class NodeType>
29 class MSchedGraphNodeIterator;
30
31
32 struct MSchedGraphEdge {
33 enum DataDepOrderType {
34 TrueDep, AntiDep, OutputDep, NonDataDep
35 };
36
37 enum MSchedGraphEdgeType {
38 MemoryDep, ValueDep, MachineRegister
39 };
40
41 MSchedGraphNode *getDest() const { return dest; }
42 unsigned getIteDiff() { return iteDiff; }
43 unsigned getDepOrderType() { return depOrderType; }
44
45 private:
46 friend class MSchedGraphNode;
47 MSchedGraphEdge(MSchedGraphNode *destination, MSchedGraphEdgeType type,
48 unsigned deptype, unsigned diff)
49 : dest(destination), depType(type), depOrderType(deptype), iteDiff(diff) {}
50
51 MSchedGraphNode *dest;
52 MSchedGraphEdgeType depType;
53 unsigned depOrderType;
54 unsigned iteDiff;
55 };
56
57 class MSchedGraphNode {
58
59 const MachineInstr* Inst; //Machine Instruction
60 MSchedGraph* Parent; //Graph this node belongs to
Tanya Lattner341828e2004-11-28 23:36:15 +000061 unsigned index; //Index in BB
Tanya Lattner48a503b2004-03-01 02:50:57 +000062 unsigned latency; //Latency of Instruction
Tanya Lattnera066df62004-05-26 06:27:18 +000063 bool isBranchInstr; //Is this node the branch instr or not
Tanya Lattner341828e2004-11-28 23:36:15 +000064
Tanya Lattnera066df62004-05-26 06:27:18 +000065
Tanya Lattner48a503b2004-03-01 02:50:57 +000066 std::vector<MSchedGraphNode*> Predecessors; //Predecessor Nodes
67 std::vector<MSchedGraphEdge> Successors;
68
69 public:
70 MSchedGraphNode(const MachineInstr *inst, MSchedGraph *graph,
Tanya Lattner341828e2004-11-28 23:36:15 +000071 unsigned index, unsigned late=0, bool isBranch=false);
Tanya Lattner48a503b2004-03-01 02:50:57 +000072
73 //Iterators
74 typedef std::vector<MSchedGraphNode*>::iterator pred_iterator;
75 pred_iterator pred_begin() { return Predecessors.begin(); }
76 pred_iterator pred_end() { return Predecessors.end(); }
77
78 typedef std::vector<MSchedGraphNode*>::const_iterator pred_const_iterator;
79 pred_const_iterator pred_begin() const { return Predecessors.begin(); }
80 pred_const_iterator pred_end() const { return Predecessors.end(); }
81
82 // Successor iterators.
83 typedef MSchedGraphNodeIterator<std::vector<MSchedGraphEdge>::const_iterator,
84 const MSchedGraphNode> succ_const_iterator;
85 succ_const_iterator succ_begin() const;
86 succ_const_iterator succ_end() const;
87
88 typedef MSchedGraphNodeIterator<std::vector<MSchedGraphEdge>::iterator,
89 MSchedGraphNode> succ_iterator;
90 succ_iterator succ_begin();
91 succ_iterator succ_end();
Tanya Lattnera066df62004-05-26 06:27:18 +000092
Tanya Lattner48a503b2004-03-01 02:50:57 +000093
94
95 void addOutEdge(MSchedGraphNode *destination,
96 MSchedGraphEdge::MSchedGraphEdgeType type,
97 unsigned deptype, unsigned diff=0) {
98 Successors.push_back(MSchedGraphEdge(destination, type, deptype,diff));
99 destination->Predecessors.push_back(this);
100 }
101 const MachineInstr* getInst() { return Inst; }
102 MSchedGraph* getParent() { return Parent; }
103 bool hasPredecessors() { return (Predecessors.size() > 0); }
104 bool hasSuccessors() { return (Successors.size() > 0); }
Tanya Lattnerddebd1e2004-10-30 00:39:07 +0000105 unsigned getLatency() { return latency; }
106 unsigned getLatency() const { return latency; }
Tanya Lattner341828e2004-11-28 23:36:15 +0000107 unsigned getIndex() { return index; }
Tanya Lattner48a503b2004-03-01 02:50:57 +0000108 MSchedGraphEdge getInEdge(MSchedGraphNode *pred);
Tanya Lattnera6820d62004-05-08 16:12:10 +0000109 unsigned getInEdgeNum(MSchedGraphNode *pred);
110
111 bool isSuccessor(MSchedGraphNode *);
112 bool isPredecessor(MSchedGraphNode *);
Tanya Lattnera066df62004-05-26 06:27:18 +0000113 bool isBranch() { return isBranchInstr; }
Tanya Lattner48a503b2004-03-01 02:50:57 +0000114 //Debug support
115 void print(std::ostream &os) const;
116
117 };
118
119 template<class IteratorType, class NodeType>
120 class MSchedGraphNodeIterator : public forward_iterator<NodeType*, ptrdiff_t> {
121 IteratorType I; // std::vector<MSchedGraphEdge>::iterator or const_iterator
122 public:
123 MSchedGraphNodeIterator(IteratorType i) : I(i) {}
124
125 bool operator==(const MSchedGraphNodeIterator RHS) const { return I == RHS.I; }
126 bool operator!=(const MSchedGraphNodeIterator RHS) const { return I != RHS.I; }
127
128 const MSchedGraphNodeIterator &operator=(const MSchedGraphNodeIterator &RHS) {
129 I = RHS.I;
130 return *this;
131 }
132
133 NodeType* operator*() const {
134 return I->getDest();
135 }
136 NodeType* operator->() const { return operator*(); }
137
138 MSchedGraphNodeIterator& operator++() { // Preincrement
139 ++I;
140 return *this;
141 }
142 MSchedGraphNodeIterator operator++(int) { // Postincrement
143 MSchedGraphNodeIterator tmp = *this; ++*this; return tmp;
144 }
145
146 MSchedGraphEdge &getEdge() {
147 return *I;
148 }
149 const MSchedGraphEdge &getEdge() const {
150 return *I;
151 }
152 };
153
154 inline MSchedGraphNode::succ_const_iterator MSchedGraphNode::succ_begin() const {
155 return succ_const_iterator(Successors.begin());
156 }
157 inline MSchedGraphNode::succ_const_iterator MSchedGraphNode::succ_end() const {
158 return succ_const_iterator(Successors.end());
159 }
160 inline MSchedGraphNode::succ_iterator MSchedGraphNode::succ_begin() {
161 return succ_iterator(Successors.begin());
162 }
163 inline MSchedGraphNode::succ_iterator MSchedGraphNode::succ_end() {
164 return succ_iterator(Successors.end());
165 }
166
167 // ostream << operator for MSGraphNode class
168 inline std::ostream &operator<<(std::ostream &os,
169 const MSchedGraphNode &node) {
170 node.print(os);
171 return os;
172 }
173
174
175
176 class MSchedGraph {
177
178 const MachineBasicBlock *BB; //Machine basic block
179 const TargetMachine &Target; //Target Machine
180
181 //Nodes
182 std::map<const MachineInstr*, MSchedGraphNode*> GraphMap;
183
184 //Add Nodes and Edges to this graph for our BB
185 typedef std::pair<int, MSchedGraphNode*> OpIndexNodePair;
186 void buildNodesAndEdges();
187 void addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
188 MSchedGraphNode *node,
189 bool nodeIsUse, bool nodeIsDef, int diff=0);
190 void addMachRegEdges(std::map<int,
191 std::vector<OpIndexNodePair> >& regNumtoNodeMap);
192 void addMemEdges(const std::vector<MSchedGraphNode*>& memInst);
193
194 public:
195 MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ);
196 ~MSchedGraph();
197
198 //Add Nodes to the Graph
199 void addNode(const MachineInstr* MI, MSchedGraphNode *node);
200
201 //iterators
202 typedef std::map<const MachineInstr*, MSchedGraphNode*>::iterator iterator;
203 typedef std::map<const MachineInstr*, MSchedGraphNode*>::const_iterator const_iterator;
204 typedef std::map<const MachineInstr*, MSchedGraphNode*>::reverse_iterator reverse_iterator;
205 iterator find(const MachineInstr* I) { return GraphMap.find(I); }
206 iterator end() { return GraphMap.end(); }
207 iterator begin() { return GraphMap.begin(); }
208 reverse_iterator rbegin() { return GraphMap.rbegin(); }
209 reverse_iterator rend() { return GraphMap.rend(); }
Tanya Lattnera066df62004-05-26 06:27:18 +0000210 const TargetMachine* getTarget() { return &Target; }
Tanya Lattner48a503b2004-03-01 02:50:57 +0000211 };
212
213
214 static MSchedGraphNode& getSecond(std::pair<const MachineInstr* const,
215 MSchedGraphNode*> &Pair) {
216 return *Pair.second;
217 }
218
219
220
221 // Provide specializations of GraphTraits to be able to use graph
222 // iterators on the scheduling graph!
223 //
224 template <> struct GraphTraits<MSchedGraph*> {
225 typedef MSchedGraphNode NodeType;
226 typedef MSchedGraphNode::succ_iterator ChildIteratorType;
227
228 static inline ChildIteratorType child_begin(NodeType *N) {
229 return N->succ_begin();
230 }
231 static inline ChildIteratorType child_end(NodeType *N) {
232 return N->succ_end();
233 }
234
235 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
236 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
237
238 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
239 static nodes_iterator nodes_begin(MSchedGraph *G) {
240 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
241 }
242 static nodes_iterator nodes_end(MSchedGraph *G) {
243 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
244 }
245
246
247 };
248
249 template <> struct GraphTraits<const MSchedGraph*> {
250 typedef const MSchedGraphNode NodeType;
251 typedef MSchedGraphNode::succ_const_iterator ChildIteratorType;
252
253 static inline ChildIteratorType child_begin(NodeType *N) {
254 return N->succ_begin();
255 }
256 static inline ChildIteratorType child_end(NodeType *N) {
257 return N->succ_end();
258 }
259 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
260 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
261
262 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
263 static nodes_iterator nodes_begin(MSchedGraph *G) {
264 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
265 }
266 static nodes_iterator nodes_end(MSchedGraph *G) {
267 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
268 }
269 };
270
271 template <> struct GraphTraits<Inverse<MSchedGraph*> > {
272 typedef MSchedGraphNode NodeType;
273 typedef MSchedGraphNode::pred_iterator ChildIteratorType;
274
275 static inline ChildIteratorType child_begin(NodeType *N) {
276 return N->pred_begin();
277 }
278 static inline ChildIteratorType child_end(NodeType *N) {
279 return N->pred_end();
280 }
281 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
282 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
283
284 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
285 static nodes_iterator nodes_begin(MSchedGraph *G) {
286 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
287 }
288 static nodes_iterator nodes_end(MSchedGraph *G) {
289 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
290 }
291 };
292
293 template <> struct GraphTraits<Inverse<const MSchedGraph*> > {
294 typedef const MSchedGraphNode NodeType;
295 typedef MSchedGraphNode::pred_const_iterator ChildIteratorType;
296
297 static inline ChildIteratorType child_begin(NodeType *N) {
298 return N->pred_begin();
299 }
300 static inline ChildIteratorType child_end(NodeType *N) {
301 return N->pred_end();
302 }
303
304 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
305 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
306
307 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
308 static nodes_iterator nodes_begin(MSchedGraph *G) {
309 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
310 }
311 static nodes_iterator nodes_end(MSchedGraph *G) {
312 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
313 }
314 };
315
316
Tanya Lattner48a503b2004-03-01 02:50:57 +0000317}
318
319#endif