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