blob: baa6373510edc164b7a65723ffe68d3c96563592 [file] [log] [blame]
Tanya Lattner9b3cbdb2004-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 Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/GraphTraits.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/iterator"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000022#include <vector>
23
Tanya Lattner260652a2004-10-30 00:39:07 +000024
Tanya Lattner9b3cbdb2004-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
61 unsigned latency; //Latency of Instruction
Tanya Lattner4cffb582004-05-26 06:27:18 +000062 bool isBranchInstr; //Is this node the branch instr or not
63
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000064 std::vector<MSchedGraphNode*> Predecessors; //Predecessor Nodes
65 std::vector<MSchedGraphEdge> Successors;
66
67 public:
68 MSchedGraphNode(const MachineInstr *inst, MSchedGraph *graph,
Tanya Lattner4cffb582004-05-26 06:27:18 +000069 unsigned late=0, bool isBranch=false);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000070
71 //Iterators
72 typedef std::vector<MSchedGraphNode*>::iterator pred_iterator;
73 pred_iterator pred_begin() { return Predecessors.begin(); }
74 pred_iterator pred_end() { return Predecessors.end(); }
75
76 typedef std::vector<MSchedGraphNode*>::const_iterator pred_const_iterator;
77 pred_const_iterator pred_begin() const { return Predecessors.begin(); }
78 pred_const_iterator pred_end() const { return Predecessors.end(); }
79
80 // Successor iterators.
81 typedef MSchedGraphNodeIterator<std::vector<MSchedGraphEdge>::const_iterator,
82 const MSchedGraphNode> succ_const_iterator;
83 succ_const_iterator succ_begin() const;
84 succ_const_iterator succ_end() const;
85
86 typedef MSchedGraphNodeIterator<std::vector<MSchedGraphEdge>::iterator,
87 MSchedGraphNode> succ_iterator;
88 succ_iterator succ_begin();
89 succ_iterator succ_end();
Tanya Lattner4cffb582004-05-26 06:27:18 +000090
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000091
92
93 void addOutEdge(MSchedGraphNode *destination,
94 MSchedGraphEdge::MSchedGraphEdgeType type,
95 unsigned deptype, unsigned diff=0) {
96 Successors.push_back(MSchedGraphEdge(destination, type, deptype,diff));
97 destination->Predecessors.push_back(this);
98 }
99 const MachineInstr* getInst() { return Inst; }
100 MSchedGraph* getParent() { return Parent; }
101 bool hasPredecessors() { return (Predecessors.size() > 0); }
102 bool hasSuccessors() { return (Successors.size() > 0); }
Tanya Lattner260652a2004-10-30 00:39:07 +0000103 unsigned getLatency() { return latency; }
104 unsigned getLatency() const { return latency; }
105
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000106 MSchedGraphEdge getInEdge(MSchedGraphNode *pred);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000107 unsigned getInEdgeNum(MSchedGraphNode *pred);
108
109 bool isSuccessor(MSchedGraphNode *);
110 bool isPredecessor(MSchedGraphNode *);
Tanya Lattner4cffb582004-05-26 06:27:18 +0000111 bool isBranch() { return isBranchInstr; }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000112 //Debug support
113 void print(std::ostream &os) const;
114
115 };
116
117 template<class IteratorType, class NodeType>
118 class MSchedGraphNodeIterator : public forward_iterator<NodeType*, ptrdiff_t> {
119 IteratorType I; // std::vector<MSchedGraphEdge>::iterator or const_iterator
120 public:
121 MSchedGraphNodeIterator(IteratorType i) : I(i) {}
122
123 bool operator==(const MSchedGraphNodeIterator RHS) const { return I == RHS.I; }
124 bool operator!=(const MSchedGraphNodeIterator RHS) const { return I != RHS.I; }
125
126 const MSchedGraphNodeIterator &operator=(const MSchedGraphNodeIterator &RHS) {
127 I = RHS.I;
128 return *this;
129 }
130
131 NodeType* operator*() const {
132 return I->getDest();
133 }
134 NodeType* operator->() const { return operator*(); }
135
136 MSchedGraphNodeIterator& operator++() { // Preincrement
137 ++I;
138 return *this;
139 }
140 MSchedGraphNodeIterator operator++(int) { // Postincrement
141 MSchedGraphNodeIterator tmp = *this; ++*this; return tmp;
142 }
143
144 MSchedGraphEdge &getEdge() {
145 return *I;
146 }
147 const MSchedGraphEdge &getEdge() const {
148 return *I;
149 }
150 };
151
152 inline MSchedGraphNode::succ_const_iterator MSchedGraphNode::succ_begin() const {
153 return succ_const_iterator(Successors.begin());
154 }
155 inline MSchedGraphNode::succ_const_iterator MSchedGraphNode::succ_end() const {
156 return succ_const_iterator(Successors.end());
157 }
158 inline MSchedGraphNode::succ_iterator MSchedGraphNode::succ_begin() {
159 return succ_iterator(Successors.begin());
160 }
161 inline MSchedGraphNode::succ_iterator MSchedGraphNode::succ_end() {
162 return succ_iterator(Successors.end());
163 }
164
165 // ostream << operator for MSGraphNode class
166 inline std::ostream &operator<<(std::ostream &os,
167 const MSchedGraphNode &node) {
168 node.print(os);
169 return os;
170 }
171
172
173
174 class MSchedGraph {
175
176 const MachineBasicBlock *BB; //Machine basic block
177 const TargetMachine &Target; //Target Machine
178
179 //Nodes
180 std::map<const MachineInstr*, MSchedGraphNode*> GraphMap;
181
182 //Add Nodes and Edges to this graph for our BB
183 typedef std::pair<int, MSchedGraphNode*> OpIndexNodePair;
184 void buildNodesAndEdges();
185 void addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
186 MSchedGraphNode *node,
187 bool nodeIsUse, bool nodeIsDef, int diff=0);
188 void addMachRegEdges(std::map<int,
189 std::vector<OpIndexNodePair> >& regNumtoNodeMap);
190 void addMemEdges(const std::vector<MSchedGraphNode*>& memInst);
191
192 public:
193 MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ);
194 ~MSchedGraph();
195
196 //Add Nodes to the Graph
197 void addNode(const MachineInstr* MI, MSchedGraphNode *node);
198
199 //iterators
200 typedef std::map<const MachineInstr*, MSchedGraphNode*>::iterator iterator;
201 typedef std::map<const MachineInstr*, MSchedGraphNode*>::const_iterator const_iterator;
202 typedef std::map<const MachineInstr*, MSchedGraphNode*>::reverse_iterator reverse_iterator;
203 iterator find(const MachineInstr* I) { return GraphMap.find(I); }
204 iterator end() { return GraphMap.end(); }
205 iterator begin() { return GraphMap.begin(); }
206 reverse_iterator rbegin() { return GraphMap.rbegin(); }
207 reverse_iterator rend() { return GraphMap.rend(); }
Tanya Lattner4cffb582004-05-26 06:27:18 +0000208 const TargetMachine* getTarget() { return &Target; }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000209 };
210
211
212 static MSchedGraphNode& getSecond(std::pair<const MachineInstr* const,
213 MSchedGraphNode*> &Pair) {
214 return *Pair.second;
215 }
216
217
218
219 // Provide specializations of GraphTraits to be able to use graph
220 // iterators on the scheduling graph!
221 //
222 template <> struct GraphTraits<MSchedGraph*> {
223 typedef MSchedGraphNode NodeType;
224 typedef MSchedGraphNode::succ_iterator ChildIteratorType;
225
226 static inline ChildIteratorType child_begin(NodeType *N) {
227 return N->succ_begin();
228 }
229 static inline ChildIteratorType child_end(NodeType *N) {
230 return N->succ_end();
231 }
232
233 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
234 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
235
236 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
237 static nodes_iterator nodes_begin(MSchedGraph *G) {
238 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
239 }
240 static nodes_iterator nodes_end(MSchedGraph *G) {
241 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
242 }
243
244
245 };
246
247 template <> struct GraphTraits<const MSchedGraph*> {
248 typedef const MSchedGraphNode NodeType;
249 typedef MSchedGraphNode::succ_const_iterator ChildIteratorType;
250
251 static inline ChildIteratorType child_begin(NodeType *N) {
252 return N->succ_begin();
253 }
254 static inline ChildIteratorType child_end(NodeType *N) {
255 return N->succ_end();
256 }
257 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
258 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
259
260 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
261 static nodes_iterator nodes_begin(MSchedGraph *G) {
262 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
263 }
264 static nodes_iterator nodes_end(MSchedGraph *G) {
265 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
266 }
267 };
268
269 template <> struct GraphTraits<Inverse<MSchedGraph*> > {
270 typedef MSchedGraphNode NodeType;
271 typedef MSchedGraphNode::pred_iterator ChildIteratorType;
272
273 static inline ChildIteratorType child_begin(NodeType *N) {
274 return N->pred_begin();
275 }
276 static inline ChildIteratorType child_end(NodeType *N) {
277 return N->pred_end();
278 }
279 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
280 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
281
282 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
283 static nodes_iterator nodes_begin(MSchedGraph *G) {
284 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
285 }
286 static nodes_iterator nodes_end(MSchedGraph *G) {
287 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
288 }
289 };
290
291 template <> struct GraphTraits<Inverse<const MSchedGraph*> > {
292 typedef const MSchedGraphNode NodeType;
293 typedef MSchedGraphNode::pred_const_iterator ChildIteratorType;
294
295 static inline ChildIteratorType child_begin(NodeType *N) {
296 return N->pred_begin();
297 }
298 static inline ChildIteratorType child_end(NodeType *N) {
299 return N->pred_end();
300 }
301
302 typedef std::pointer_to_unary_function<std::pair<const MachineInstr* const,
303 MSchedGraphNode*>&, MSchedGraphNode&> DerefFun;
304
305 typedef mapped_iterator<MSchedGraph::iterator, DerefFun> nodes_iterator;
306 static nodes_iterator nodes_begin(MSchedGraph *G) {
307 return map_iterator(((MSchedGraph*)G)->begin(), DerefFun(getSecond));
308 }
309 static nodes_iterator nodes_end(MSchedGraph *G) {
310 return map_iterator(((MSchedGraph*)G)->end(), DerefFun(getSecond));
311 }
312 };
313
314
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000315}
316
317#endif