blob: 4da761f0f85c8ff90b357c09190c0d85add382a8 [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
Tanya Lattnerb6489f32003-08-25 22:42:20 +000017#include "llvm/CodeGen/SchedGraphCommon.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000018#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/Transforms/Scalar.h"
20#include "Support/hash_map"
21#include "Support/GraphTraits.h"
22
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
Tanya Lattnerb6489f32003-08-25 22:42:20 +000029 MachineBasicBlock *MBB;
30 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000031
Tanya Lattnerb6489f32003-08-25 22:42:20 +000032
Tanya Lattnerc50ee552003-08-27 02:42:58 +000033 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
34 const TargetMachine& Target);
35 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000036
Vikram S. Adve78ef1392001-08-28 23:06:02 +000037 friend class SchedGraph; // give access for ctor and dtor
38 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000039
40public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000041
Tanya Lattnerc50ee552003-08-27 02:42:58 +000042 // 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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000048 void print(std::ostream &os) const;
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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000059 MachineBasicBlock& getBasicBlock() const{return MBB;}
60 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000061 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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000067 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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000092
Vikram S. Adve78ef1392001-08-28 23:06:02 +000093 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +000094 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000095 assert((*this)[minstr] == NULL);
96 (*this)[minstr] = node;
97 }
98
99 //
100 // Graph builder
101 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000102 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000103
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000104 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
105 std::vector<SchedGraphNode*>& memNV,
106 std::vector<SchedGraphNode*>& callNV,
107 RegToRefVecMap& regToRefVecMap,
108 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000109
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000110
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000111 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
112 std::vector<SchedGraphNode*>& memNV,
113 std::vector<SchedGraphNode*>& callNV,
114 RegToRefVecMap& regToRefVecMap,
115 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000116
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000117 void addEdgesForInstruction(const MachineInstr& minstr,
118 const ValueToDefVecMap& valueToDefVecMap,
119 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000120
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000121 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000123 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
124 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000126 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
127 MachineBasicBlock& bbMvec,
128 const TargetMachine& target);
129
130 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
131 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000132
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000133 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
134 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000135
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000136 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
137 const Value* defValue, bool refNodeIsDef,
138 bool refNodeIsDefAndUse,
139 const TargetMachine& target);
140
141 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000142
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000143};
144
145
Chris Lattner9efc4d62003-06-02 22:45:07 +0000146
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000147class SchedGraphSet {
148 const Function* function;
149 std::vector<SchedGraph*> Graphs;
150
151 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000152 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000153
154 inline void addGraph(SchedGraph* graph) {
155 assert(graph != NULL);
156 Graphs.push_back(graph);
157 }
158
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000159public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000160 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000161 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000163 //iterators
164 typedef std::vector<SchedGraph*>::const_iterator iterator;
165 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
166
167 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
168 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
169
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000170 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000171 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000172};
173
174
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000175
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000176//********************** Sched Graph Iterators *****************************/
177
178// Ok to make it a template because it shd get instantiated at most twice:
179// for <SchedGraphNode, SchedGraphNode::iterator> and
180// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
181//
182template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000183class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000184protected:
185 _EdgeIter oi;
186public:
Chris Lattner455889a2002-02-12 22:39:50 +0000187 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000188
Chris Lattner455889a2002-02-12 22:39:50 +0000189 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000190
191 inline bool operator==(const _Self& x) const { return oi == x.oi; }
192 inline bool operator!=(const _Self& x) const { return !operator==(x); }
193
194 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000195 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000196 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000197
198 inline _EdgeType* getEdge() const { return *(oi); }
199
Chris Lattnercffebdc2001-09-07 21:07:10 +0000200 inline _Self &operator++() { ++oi; return *this; } // Preincrement
201 inline _Self operator++(int) { // Postincrement
202 _Self tmp(*this); ++*this; return tmp;
203 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000204
Chris Lattnercffebdc2001-09-07 21:07:10 +0000205 inline _Self &operator--() { --oi; return *this; } // Predecrement
206 inline _Self operator--(int) { // Postdecrement
207 _Self tmp = *this; --*this; return tmp;
208 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000209};
210
211template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000212class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000213protected:
214 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000215public:
Chris Lattner455889a2002-02-12 22:39:50 +0000216 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217
Chris Lattner455889a2002-02-12 22:39:50 +0000218 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000219
Chris Lattnercffebdc2001-09-07 21:07:10 +0000220 inline bool operator==(const _Self& x) const { return oi == x.oi; }
221 inline bool operator!=(const _Self& x) const { return !operator==(x); }
222
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000223 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000224 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000225
226 inline _EdgeType* getEdge() const { return *(oi); }
227
228 inline _Self &operator++() { ++oi; return *this; } // Preincrement
229 inline _Self operator++(int) { // Postincrement
230 _Self tmp(*this); ++*this; return tmp;
231 }
232
233 inline _Self &operator--() { --oi; return *this; } // Predecrement
234 inline _Self operator--(int) { // Postdecrement
235 _Self tmp = *this; --*this; return tmp;
236 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000237};
238
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000239//
240// sg_pred_iterator
241// sg_pred_const_iterator
242//
Chris Lattner455889a2002-02-12 22:39:50 +0000243typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000245typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000246 sg_pred_const_iterator;
247
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000248inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000249 return sg_pred_iterator(N->beginInEdges());
250}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000251inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000252 return sg_pred_iterator(N->endInEdges());
253}
254inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
255 return sg_pred_const_iterator(N->beginInEdges());
256}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000257inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000258 return sg_pred_const_iterator(N->endInEdges());
259}
260
261
262//
263// sg_succ_iterator
264// sg_succ_const_iterator
265//
Chris Lattner455889a2002-02-12 22:39:50 +0000266typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000267 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000268typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000269 sg_succ_const_iterator;
270
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000271inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000272 return sg_succ_iterator(N->beginOutEdges());
273}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000274inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000275 return sg_succ_iterator(N->endOutEdges());
276}
277inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
278 return sg_succ_const_iterator(N->beginOutEdges());
279}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000280inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000281 return sg_succ_const_iterator(N->endOutEdges());
282}
283
Chris Lattner3ff43872001-09-28 22:56:31 +0000284// Provide specializations of GraphTraits to be able to use graph iterators on
285// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000286//
Chris Lattner3ff43872001-09-28 22:56:31 +0000287template <> struct GraphTraits<SchedGraph*> {
288 typedef SchedGraphNode NodeType;
289 typedef sg_succ_iterator ChildIteratorType;
290
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000291 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000292 static inline ChildIteratorType child_begin(NodeType *N) {
293 return succ_begin(N);
294 }
295 static inline ChildIteratorType child_end(NodeType *N) {
296 return succ_end(N);
297 }
298};
299
300template <> struct GraphTraits<const SchedGraph*> {
301 typedef const SchedGraphNode NodeType;
302 typedef sg_succ_const_iterator ChildIteratorType;
303
304 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000305 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000306 }
307 static inline ChildIteratorType child_begin(NodeType *N) {
308 return succ_begin(N);
309 }
310 static inline ChildIteratorType child_end(NodeType *N) {
311 return succ_end(N);
312 }
313};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000314
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000315#endif