blob: 82e5dd619b3ea84af363f62a1ae24c4ed3691d97 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- SchedGraph.h - Scheduling Graph --------------------------*- C++ -*--=//
2//
3// Purpose:
4// Scheduling graph based on SSA graph plus extra dependence edges
5// capturing dependences due to machine resources (machine registers,
6// CC registers, and any others).
7//
8// Strategy:
9// This graph tries to leverage the SSA graph as much as possible,
10// but captures the extra dependences through a common interface.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +000013
14#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
15#define LLVM_CODEGEN_SCHEDGRAPH_H
16
Vikram S. Advee64574c2001-11-08 05:20:23 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/GraphTraits.h"
Chris Lattnere5a61cc2003-07-26 23:22:19 +000019#include "Support/hash_map"
Tanya Lattnerb6489f32003-08-25 22:42:20 +000020#include "llvm/Transforms/Scalar.h"
21#include "llvm/CodeGen/SchedGraphCommon.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000022
Vikram S. Adve4a87b382001-09-30 23:37:26 +000023class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000024class ValueToDefVecMap;
25class RefVec;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000026
Tanya Lattnerb6489f32003-08-25 22:42:20 +000027class SchedGraphNode : public SchedGraphNodeCommon {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000028
Vikram S. Adve5b43af92001-11-11 01:23:27 +000029 int origIndexInBB; // original position of machine instr in BB
Tanya Lattnerb6489f32003-08-25 22:42:20 +000030 MachineBasicBlock *MBB;
31 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000032
Tanya Lattnerb6489f32003-08-25 22:42:20 +000033
34 SchedGraphNode (unsigned nodeId, MachineBasicBlock *mbb,
35 int indexInBB, const TargetMachine& Target);
36 ~SchedGraphNode ();
37
Vikram S. Adve78ef1392001-08-28 23:06:02 +000038 friend class SchedGraph; // give access for ctor and dtor
39 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000040
41public:
42 // Accessor methods
43 const MachineInstr* getMachineInstr () const { return MI; }
44 const MachineOpCode getOpCode () const { return MI->getOpCode(); }
45 bool isDummyNode () const { return (MI == NULL); }
46 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
47
48 int getOrigIndexInBB() const { return origIndexInBB; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000049};
50
Tanya Lattnerb6489f32003-08-25 22:42:20 +000051class SchedGraph : public SchedGraphCommon {
52 MachineBasicBlock &MBB;
53 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000054
55public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000056 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
57 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
58
59 MachineBasicBlock& getBasicBlock() const{return MBB;}
60 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
61 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
62 const_iterator onePair = find(MI);
63 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000064 }
65
Tanya Lattnerb6489f32003-08-25 22:42:20 +000066 // Debugging support
67 void dump () const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000068
Tanya Lattnerb6489f32003-08-25 22:42:20 +000069protected:
70 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
71 ~SchedGraph();
72
Vikram S. Adve78ef1392001-08-28 23:06:02 +000073 // Unordered iterators.
74 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
75 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000076 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
77 return GraphMap.begin();
78 }
79 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
80 return GraphMap.end();
81 }
82
83 unsigned size() { return GraphMap.size(); }
84 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000085
Tanya Lattnerb6489f32003-08-25 22:42:20 +000086 SchedGraphNode *&operator[](const MachineInstr *MI) {
87 return GraphMap[MI];
88 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000089
90private:
91 friend class SchedGraphSet; // give access to ctor
92
Tanya Lattnerb6489f32003-08-25 22:42:20 +000093
Vikram S. Adve78ef1392001-08-28 23:06:02 +000094
95 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
96 SchedGraphNode* node)
97 {
98 assert((*this)[minstr] == NULL);
99 (*this)[minstr] = node;
100 }
101
102 //
103 // Graph builder
104 //
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000105 void buildGraph (const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000107 void buildNodesForBB (const TargetMachine& target,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000108 MachineBasicBlock &MBB,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000109 std::vector<SchedGraphNode*>& memNV,
110 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000111 RegToRefVecMap& regToRefVecMap,
112 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000113
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000114
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000115 void findDefUseInfoAtInstr (const TargetMachine& target,
116 SchedGraphNode* node,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000117 std::vector<SchedGraphNode*>& memNV,
118 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000119 RegToRefVecMap& regToRefVecMap,
120 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000121
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000122
Vikram S. Advee64574c2001-11-08 05:20:23 +0000123 void addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000124 const ValueToDefVecMap& valueToDefVecMap,
125 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000126
127 void addCDEdges (const TerminatorInst* term,
128 const TargetMachine& target);
129
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000130 void addMemEdges (const std::vector<SchedGraphNode*>& memNod,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000131 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000132
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000133 void addCallCCEdges (const std::vector<SchedGraphNode*>& memNod,
134 MachineBasicBlock& bbMvec,
135 const TargetMachine& target);
Vikram S. Adve7952d602003-05-31 07:37:05 +0000136 void addCallDepEdges (const std::vector<SchedGraphNode*>& callNV,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000137 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000138
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000139 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000140 const TargetMachine& target);
141
Vikram S. Adve200a4352001-11-12 18:53:43 +0000142 void addEdgesForValue (SchedGraphNode* refNode,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000143 const RefVec& defVec,
Vikram S. Advef43e3362001-10-17 23:55:16 +0000144 const Value* defValue,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000145 bool refNodeIsDef,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000146 bool refNodeIsDefAndUse,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000147 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000148 void addDummyEdges();
149
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000150};
151
152
Chris Lattner9efc4d62003-06-02 22:45:07 +0000153
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000154class SchedGraphSet {
155 const Function* function;
156 std::vector<SchedGraph*> Graphs;
157
158 // Graph builder
159 void buildGraphsForMethod (const Function *F, const TargetMachine& target);
160
161 inline void addGraph(SchedGraph* graph) {
162 assert(graph != NULL);
163 Graphs.push_back(graph);
164 }
165
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000166public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000167 SchedGraphSet(const Function * function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000168 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000169
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000170 //iterators
171 typedef std::vector<SchedGraph*>::const_iterator iterator;
172 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
173
174 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
175 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
176
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000177 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000178 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179};
180
181
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000182
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000183//********************** Sched Graph Iterators *****************************/
184
185// Ok to make it a template because it shd get instantiated at most twice:
186// for <SchedGraphNode, SchedGraphNode::iterator> and
187// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
188//
189template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000190class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000191protected:
192 _EdgeIter oi;
193public:
Chris Lattner455889a2002-02-12 22:39:50 +0000194 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000195
Chris Lattner455889a2002-02-12 22:39:50 +0000196 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000197
198 inline bool operator==(const _Self& x) const { return oi == x.oi; }
199 inline bool operator!=(const _Self& x) const { return !operator==(x); }
200
201 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000202 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000203 inline _NodeType* operator->() const { return operator*(); }
204
205 inline _EdgeType* getEdge() const { return *(oi); }
206
Chris Lattnercffebdc2001-09-07 21:07:10 +0000207 inline _Self &operator++() { ++oi; return *this; } // Preincrement
208 inline _Self operator++(int) { // Postincrement
209 _Self tmp(*this); ++*this; return tmp;
210 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000211
Chris Lattnercffebdc2001-09-07 21:07:10 +0000212 inline _Self &operator--() { --oi; return *this; } // Predecrement
213 inline _Self operator--(int) { // Postdecrement
214 _Self tmp = *this; --*this; return tmp;
215 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000216};
217
218template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000219class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000220protected:
221 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000222public:
Chris Lattner455889a2002-02-12 22:39:50 +0000223 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000224
Chris Lattner455889a2002-02-12 22:39:50 +0000225 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000226
Chris Lattnercffebdc2001-09-07 21:07:10 +0000227 inline bool operator==(const _Self& x) const { return oi == x.oi; }
228 inline bool operator!=(const _Self& x) const { return !operator==(x); }
229
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000230 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000231 inline _NodeType* operator->() const { return operator*(); }
232
233 inline _EdgeType* getEdge() const { return *(oi); }
234
235 inline _Self &operator++() { ++oi; return *this; } // Preincrement
236 inline _Self operator++(int) { // Postincrement
237 _Self tmp(*this); ++*this; return tmp;
238 }
239
240 inline _Self &operator--() { --oi; return *this; } // Predecrement
241 inline _Self operator--(int) { // Postdecrement
242 _Self tmp = *this; --*this; return tmp;
243 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244};
245
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000246//
247// sg_pred_iterator
248// sg_pred_const_iterator
249//
Chris Lattner455889a2002-02-12 22:39:50 +0000250typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000251 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000252typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000253 sg_pred_const_iterator;
254
255inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
256 return sg_pred_iterator(N->beginInEdges());
257}
258inline sg_pred_iterator pred_end( SchedGraphNode *N) {
259 return sg_pred_iterator(N->endInEdges());
260}
261inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
262 return sg_pred_const_iterator(N->beginInEdges());
263}
264inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
265 return sg_pred_const_iterator(N->endInEdges());
266}
267
268
269//
270// sg_succ_iterator
271// sg_succ_const_iterator
272//
Chris Lattner455889a2002-02-12 22:39:50 +0000273typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000274 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000275typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000276 sg_succ_const_iterator;
277
278inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
279 return sg_succ_iterator(N->beginOutEdges());
280}
281inline sg_succ_iterator succ_end( SchedGraphNode *N) {
282 return sg_succ_iterator(N->endOutEdges());
283}
284inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
285 return sg_succ_const_iterator(N->beginOutEdges());
286}
287inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
288 return sg_succ_const_iterator(N->endOutEdges());
289}
290
Chris Lattner3ff43872001-09-28 22:56:31 +0000291// Provide specializations of GraphTraits to be able to use graph iterators on
292// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000293//
Chris Lattner3ff43872001-09-28 22:56:31 +0000294template <> struct GraphTraits<SchedGraph*> {
295 typedef SchedGraphNode NodeType;
296 typedef sg_succ_iterator ChildIteratorType;
297
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000298 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000299 static inline ChildIteratorType child_begin(NodeType *N) {
300 return succ_begin(N);
301 }
302 static inline ChildIteratorType child_end(NodeType *N) {
303 return succ_end(N);
304 }
305};
306
307template <> struct GraphTraits<const SchedGraph*> {
308 typedef const SchedGraphNode NodeType;
309 typedef sg_succ_const_iterator ChildIteratorType;
310
311 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000312 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000313 }
314 static inline ChildIteratorType child_begin(NodeType *N) {
315 return succ_begin(N);
316 }
317 static inline ChildIteratorType child_end(NodeType *N) {
318 return succ_end(N);
319 }
320};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000321
322
Chris Lattner697954c2002-01-20 22:54:45 +0000323std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
324std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000325
326#endif