blob: 11a4f877d0c1b3b1b9cde7be1cc50a569a444117 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SchedGraph.h - Scheduling Graph -------------------------*- C++ -*-===//
Chris Lattner179cdfb2002-08-09 20:08:03 +00002//
Chris Lattnercf3056d2003-10-13 03:32:08 +00003// This is a scheduling graph based on SSA graph plus extra dependence edges
4// capturing dependences due to machine resources (machine registers, CC
5// registers, and any others).
Chris Lattner179cdfb2002-08-09 20:08:03 +00006//
Chris Lattnercf3056d2003-10-13 03:32:08 +00007// This graph tries to leverage the SSA graph as much as possible, but captures
8// the extra dependences through a common interface.
Chris Lattner179cdfb2002-08-09 20:08:03 +00009//
10//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +000011
12#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
13#define LLVM_CODEGEN_SCHEDGRAPH_H
14
Tanya Lattnerb6489f32003-08-25 22:42:20 +000015#include "llvm/CodeGen/SchedGraphCommon.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000016#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/Transforms/Scalar.h"
18#include "Support/hash_map"
19#include "Support/GraphTraits.h"
20
Vikram S. Adve4a87b382001-09-30 23:37:26 +000021class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000022class ValueToDefVecMap;
23class RefVec;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000024
Tanya Lattnerb6489f32003-08-25 22:42:20 +000025class SchedGraphNode : public SchedGraphNodeCommon {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000026
Tanya Lattnerb6489f32003-08-25 22:42:20 +000027 MachineBasicBlock *MBB;
28 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000029
Tanya Lattnerb6489f32003-08-25 22:42:20 +000030
Tanya Lattnerc50ee552003-08-27 02:42:58 +000031 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
32 const TargetMachine& Target);
33 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000034
Vikram S. Adve78ef1392001-08-28 23:06:02 +000035 friend class SchedGraph; // give access for ctor and dtor
36 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000037
38public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000039
Tanya Lattnerc50ee552003-08-27 02:42:58 +000040 // Accessor methods
41 const MachineInstr* getMachineInstr() const { return MI; }
42 const MachineOpCode getOpCode() const { return MI->getOpCode(); }
43 bool isDummyNode() const { return (MI == NULL); }
44 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
45
Tanya Lattnerc50ee552003-08-27 02:42:58 +000046 void print(std::ostream &os) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000047};
48
Tanya Lattnerb6489f32003-08-25 22:42:20 +000049class SchedGraph : public SchedGraphCommon {
50 MachineBasicBlock &MBB;
51 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000052
53public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000054 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
55 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
56
Tanya Lattnerc50ee552003-08-27 02:42:58 +000057 MachineBasicBlock& getBasicBlock() const{return MBB;}
58 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000059 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
60 const_iterator onePair = find(MI);
61 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000062 }
63
Tanya Lattnerb6489f32003-08-25 22:42:20 +000064 // Debugging support
Tanya Lattnerc50ee552003-08-27 02:42:58 +000065 void dump() const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000066
Tanya Lattnerb6489f32003-08-25 22:42:20 +000067protected:
68 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
69 ~SchedGraph();
70
Vikram S. Adve78ef1392001-08-28 23:06:02 +000071 // Unordered iterators.
72 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
73 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000074 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
75 return GraphMap.begin();
76 }
77 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
78 return GraphMap.end();
79 }
80
81 unsigned size() { return GraphMap.size(); }
82 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083
Tanya Lattnerb6489f32003-08-25 22:42:20 +000084 SchedGraphNode *&operator[](const MachineInstr *MI) {
85 return GraphMap[MI];
86 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000087
88private:
89 friend class SchedGraphSet; // give access to ctor
Tanya Lattnerc50ee552003-08-27 02:42:58 +000090
Vikram S. Adve78ef1392001-08-28 23:06:02 +000091 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +000092 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000093 assert((*this)[minstr] == NULL);
94 (*this)[minstr] = node;
95 }
96
97 //
98 // Graph builder
99 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000100 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000101
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000102 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
103 std::vector<SchedGraphNode*>& memNV,
104 std::vector<SchedGraphNode*>& callNV,
105 RegToRefVecMap& regToRefVecMap,
106 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000107
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000108
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000109 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
110 std::vector<SchedGraphNode*>& memNV,
111 std::vector<SchedGraphNode*>& callNV,
112 RegToRefVecMap& regToRefVecMap,
113 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000114
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000115 void addEdgesForInstruction(const MachineInstr& minstr,
116 const ValueToDefVecMap& valueToDefVecMap,
117 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000118
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000119 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000120
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000121 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
122 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000123
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000124 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
125 MachineBasicBlock& bbMvec,
126 const TargetMachine& target);
127
128 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
129 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000130
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000131 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
132 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000133
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000134 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
135 const Value* defValue, bool refNodeIsDef,
136 bool refNodeIsDefAndUse,
137 const TargetMachine& target);
138
139 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000140
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000141};
142
143
Chris Lattner9efc4d62003-06-02 22:45:07 +0000144
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000145class SchedGraphSet {
146 const Function* function;
147 std::vector<SchedGraph*> Graphs;
148
149 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000150 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000151
152 inline void addGraph(SchedGraph* graph) {
153 assert(graph != NULL);
154 Graphs.push_back(graph);
155 }
156
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000157public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000158 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000159 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000160
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000161 //iterators
162 typedef std::vector<SchedGraph*>::const_iterator iterator;
163 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
164
165 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
166 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
167
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000168 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000169 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000170};
171
172
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000173
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174//********************** Sched Graph Iterators *****************************/
175
176// Ok to make it a template because it shd get instantiated at most twice:
177// for <SchedGraphNode, SchedGraphNode::iterator> and
178// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
179//
180template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000181class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000182protected:
183 _EdgeIter oi;
184public:
Chris Lattner455889a2002-02-12 22:39:50 +0000185 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000186
Chris Lattner455889a2002-02-12 22:39:50 +0000187 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000188
189 inline bool operator==(const _Self& x) const { return oi == x.oi; }
190 inline bool operator!=(const _Self& x) const { return !operator==(x); }
191
192 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000193 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000194 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000195
196 inline _EdgeType* getEdge() const { return *(oi); }
197
Chris Lattnercffebdc2001-09-07 21:07:10 +0000198 inline _Self &operator++() { ++oi; return *this; } // Preincrement
199 inline _Self operator++(int) { // Postincrement
200 _Self tmp(*this); ++*this; return tmp;
201 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000202
Chris Lattnercffebdc2001-09-07 21:07:10 +0000203 inline _Self &operator--() { --oi; return *this; } // Predecrement
204 inline _Self operator--(int) { // Postdecrement
205 _Self tmp = *this; --*this; return tmp;
206 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000207};
208
209template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000210class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000211protected:
212 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000213public:
Chris Lattner455889a2002-02-12 22:39:50 +0000214 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000215
Chris Lattner455889a2002-02-12 22:39:50 +0000216 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217
Chris Lattnercffebdc2001-09-07 21:07:10 +0000218 inline bool operator==(const _Self& x) const { return oi == x.oi; }
219 inline bool operator!=(const _Self& x) const { return !operator==(x); }
220
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000221 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000222 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000223
224 inline _EdgeType* getEdge() const { return *(oi); }
225
226 inline _Self &operator++() { ++oi; return *this; } // Preincrement
227 inline _Self operator++(int) { // Postincrement
228 _Self tmp(*this); ++*this; return tmp;
229 }
230
231 inline _Self &operator--() { --oi; return *this; } // Predecrement
232 inline _Self operator--(int) { // Postdecrement
233 _Self tmp = *this; --*this; return tmp;
234 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000235};
236
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000237//
238// sg_pred_iterator
239// sg_pred_const_iterator
240//
Chris Lattner455889a2002-02-12 22:39:50 +0000241typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000242 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000243typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244 sg_pred_const_iterator;
245
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000246inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000247 return sg_pred_iterator(N->beginInEdges());
248}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000249inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000250 return sg_pred_iterator(N->endInEdges());
251}
252inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
253 return sg_pred_const_iterator(N->beginInEdges());
254}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000255inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000256 return sg_pred_const_iterator(N->endInEdges());
257}
258
259
260//
261// sg_succ_iterator
262// sg_succ_const_iterator
263//
Chris Lattner455889a2002-02-12 22:39:50 +0000264typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000265 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000266typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000267 sg_succ_const_iterator;
268
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000269inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000270 return sg_succ_iterator(N->beginOutEdges());
271}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000272inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000273 return sg_succ_iterator(N->endOutEdges());
274}
275inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
276 return sg_succ_const_iterator(N->beginOutEdges());
277}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000278inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000279 return sg_succ_const_iterator(N->endOutEdges());
280}
281
Chris Lattner3ff43872001-09-28 22:56:31 +0000282// Provide specializations of GraphTraits to be able to use graph iterators on
283// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000284//
Chris Lattner3ff43872001-09-28 22:56:31 +0000285template <> struct GraphTraits<SchedGraph*> {
286 typedef SchedGraphNode NodeType;
287 typedef sg_succ_iterator ChildIteratorType;
288
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000289 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000290 static inline ChildIteratorType child_begin(NodeType *N) {
291 return succ_begin(N);
292 }
293 static inline ChildIteratorType child_end(NodeType *N) {
294 return succ_end(N);
295 }
296};
297
298template <> struct GraphTraits<const SchedGraph*> {
299 typedef const SchedGraphNode NodeType;
300 typedef sg_succ_const_iterator ChildIteratorType;
301
302 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000303 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000304 }
305 static inline ChildIteratorType child_begin(NodeType *N) {
306 return succ_begin(N);
307 }
308 static inline ChildIteratorType child_end(NodeType *N) {
309 return succ_end(N);
310 }
311};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000312
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000313#endif