blob: 441aad179a74100044f1576ad7f13f2f175b87fb [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
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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000034 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
35 const TargetMachine& Target);
36 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000037
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:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000042
Tanya Lattnerc50ee552003-08-27 02:42:58 +000043 // Accessor methods
44 const MachineInstr* getMachineInstr() const { return MI; }
45 const MachineOpCode getOpCode() const { return MI->getOpCode(); }
46 bool isDummyNode() const { return (MI == NULL); }
47 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
48
49 int getOrigIndexInBB() const { return origIndexInBB; }
50 void print(std::ostream &os) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000051};
52
Tanya Lattnerb6489f32003-08-25 22:42:20 +000053class SchedGraph : public SchedGraphCommon {
54 MachineBasicBlock &MBB;
55 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000056
57public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000058 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
59 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
60
Tanya Lattnerc50ee552003-08-27 02:42:58 +000061 MachineBasicBlock& getBasicBlock() const{return MBB;}
62 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000063 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
64 const_iterator onePair = find(MI);
65 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000066 }
67
Tanya Lattnerb6489f32003-08-25 22:42:20 +000068 // Debugging support
Tanya Lattnerc50ee552003-08-27 02:42:58 +000069 void dump() const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000070
Tanya Lattnerb6489f32003-08-25 22:42:20 +000071protected:
72 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
73 ~SchedGraph();
74
Vikram S. Adve78ef1392001-08-28 23:06:02 +000075 // Unordered iterators.
76 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
77 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000078 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
79 return GraphMap.begin();
80 }
81 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
82 return GraphMap.end();
83 }
84
85 unsigned size() { return GraphMap.size(); }
86 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000087
Tanya Lattnerb6489f32003-08-25 22:42:20 +000088 SchedGraphNode *&operator[](const MachineInstr *MI) {
89 return GraphMap[MI];
90 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000091
92private:
93 friend class SchedGraphSet; // give access to ctor
Tanya Lattnerc50ee552003-08-27 02:42:58 +000094
Vikram S. Adve78ef1392001-08-28 23:06:02 +000095 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +000096 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000097 assert((*this)[minstr] == NULL);
98 (*this)[minstr] = node;
99 }
100
101 //
102 // Graph builder
103 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000104 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000105
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000106 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
107 std::vector<SchedGraphNode*>& memNV,
108 std::vector<SchedGraphNode*>& callNV,
109 RegToRefVecMap& regToRefVecMap,
110 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000111
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000112
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000113 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
114 std::vector<SchedGraphNode*>& memNV,
115 std::vector<SchedGraphNode*>& callNV,
116 RegToRefVecMap& regToRefVecMap,
117 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000118
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000119 void addEdgesForInstruction(const MachineInstr& minstr,
120 const ValueToDefVecMap& valueToDefVecMap,
121 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000123 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000124
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000125 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
126 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000127
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
129 MachineBasicBlock& bbMvec,
130 const TargetMachine& target);
131
132 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
133 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000134
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000135 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
136 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000137
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000138 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
139 const Value* defValue, bool refNodeIsDef,
140 bool refNodeIsDefAndUse,
141 const TargetMachine& target);
142
143 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000144
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000145};
146
147
Chris Lattner9efc4d62003-06-02 22:45:07 +0000148
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000149class SchedGraphSet {
150 const Function* function;
151 std::vector<SchedGraph*> Graphs;
152
153 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000154 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000155
156 inline void addGraph(SchedGraph* graph) {
157 assert(graph != NULL);
158 Graphs.push_back(graph);
159 }
160
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000161public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000162 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000163 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000164
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000165 //iterators
166 typedef std::vector<SchedGraph*>::const_iterator iterator;
167 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
168
169 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
170 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
171
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000172 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000173 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174};
175
176
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000177
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000178//********************** Sched Graph Iterators *****************************/
179
180// Ok to make it a template because it shd get instantiated at most twice:
181// for <SchedGraphNode, SchedGraphNode::iterator> and
182// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
183//
184template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000185class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000186protected:
187 _EdgeIter oi;
188public:
Chris Lattner455889a2002-02-12 22:39:50 +0000189 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000190
Chris Lattner455889a2002-02-12 22:39:50 +0000191 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000192
193 inline bool operator==(const _Self& x) const { return oi == x.oi; }
194 inline bool operator!=(const _Self& x) const { return !operator==(x); }
195
196 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000197 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000198 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000199
200 inline _EdgeType* getEdge() const { return *(oi); }
201
Chris Lattnercffebdc2001-09-07 21:07:10 +0000202 inline _Self &operator++() { ++oi; return *this; } // Preincrement
203 inline _Self operator++(int) { // Postincrement
204 _Self tmp(*this); ++*this; return tmp;
205 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000206
Chris Lattnercffebdc2001-09-07 21:07:10 +0000207 inline _Self &operator--() { --oi; return *this; } // Predecrement
208 inline _Self operator--(int) { // Postdecrement
209 _Self tmp = *this; --*this; return tmp;
210 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000211};
212
213template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000214class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000215protected:
216 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217public:
Chris Lattner455889a2002-02-12 22:39:50 +0000218 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000219
Chris Lattner455889a2002-02-12 22:39:50 +0000220 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000221
Chris Lattnercffebdc2001-09-07 21:07:10 +0000222 inline bool operator==(const _Self& x) const { return oi == x.oi; }
223 inline bool operator!=(const _Self& x) const { return !operator==(x); }
224
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000225 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000226 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000227
228 inline _EdgeType* getEdge() const { return *(oi); }
229
230 inline _Self &operator++() { ++oi; return *this; } // Preincrement
231 inline _Self operator++(int) { // Postincrement
232 _Self tmp(*this); ++*this; return tmp;
233 }
234
235 inline _Self &operator--() { --oi; return *this; } // Predecrement
236 inline _Self operator--(int) { // Postdecrement
237 _Self tmp = *this; --*this; return tmp;
238 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000239};
240
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000241//
242// sg_pred_iterator
243// sg_pred_const_iterator
244//
Chris Lattner455889a2002-02-12 22:39:50 +0000245typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000246 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000247typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000248 sg_pred_const_iterator;
249
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000250inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000251 return sg_pred_iterator(N->beginInEdges());
252}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000253inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000254 return sg_pred_iterator(N->endInEdges());
255}
256inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
257 return sg_pred_const_iterator(N->beginInEdges());
258}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000259inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000260 return sg_pred_const_iterator(N->endInEdges());
261}
262
263
264//
265// sg_succ_iterator
266// sg_succ_const_iterator
267//
Chris Lattner455889a2002-02-12 22:39:50 +0000268typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000269 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000270typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000271 sg_succ_const_iterator;
272
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000273inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000274 return sg_succ_iterator(N->beginOutEdges());
275}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000276inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000277 return sg_succ_iterator(N->endOutEdges());
278}
279inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
280 return sg_succ_const_iterator(N->beginOutEdges());
281}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000282inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000283 return sg_succ_const_iterator(N->endOutEdges());
284}
285
Chris Lattner3ff43872001-09-28 22:56:31 +0000286// Provide specializations of GraphTraits to be able to use graph iterators on
287// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000288//
Chris Lattner3ff43872001-09-28 22:56:31 +0000289template <> struct GraphTraits<SchedGraph*> {
290 typedef SchedGraphNode NodeType;
291 typedef sg_succ_iterator ChildIteratorType;
292
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000293 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000294 static inline ChildIteratorType child_begin(NodeType *N) {
295 return succ_begin(N);
296 }
297 static inline ChildIteratorType child_end(NodeType *N) {
298 return succ_end(N);
299 }
300};
301
302template <> struct GraphTraits<const SchedGraph*> {
303 typedef const SchedGraphNode NodeType;
304 typedef sg_succ_const_iterator ChildIteratorType;
305
306 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000307 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000308 }
309 static inline ChildIteratorType child_begin(NodeType *N) {
310 return succ_begin(N);
311 }
312 static inline ChildIteratorType child_end(NodeType *N) {
313 return succ_end(N);
314 }
315};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000316
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000317#endif