blob: 4e67f40d5722cb463c1a756a732c626bc622762e [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. Adve78ef1392001-08-28 23:06:02 +000023
Vikram S. Adve4a87b382001-09-30 23:37:26 +000024class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000025class ValueToDefVecMap;
26class RefVec;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000027
Tanya Lattnerb6489f32003-08-25 22:42:20 +000028class SchedGraphNode : public SchedGraphNodeCommon {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000029
Vikram S. Adve5b43af92001-11-11 01:23:27 +000030 int origIndexInBB; // original position of machine instr in BB
Tanya Lattnerb6489f32003-08-25 22:42:20 +000031 MachineBasicBlock *MBB;
32 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000033
Tanya Lattnerb6489f32003-08-25 22:42:20 +000034
Tanya Lattnerc50ee552003-08-27 02:42:58 +000035 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
36 const TargetMachine& Target);
37 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000038
Vikram S. Adve78ef1392001-08-28 23:06:02 +000039 friend class SchedGraph; // give access for ctor and dtor
40 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000041
42public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000043
Tanya Lattnerc50ee552003-08-27 02:42:58 +000044 // Accessor methods
45 const MachineInstr* getMachineInstr() const { return MI; }
46 const MachineOpCode getOpCode() const { return MI->getOpCode(); }
47 bool isDummyNode() const { return (MI == NULL); }
48 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
49
50 int getOrigIndexInBB() const { return origIndexInBB; }
51 void print(std::ostream &os) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000052};
53
Tanya Lattnerb6489f32003-08-25 22:42:20 +000054class SchedGraph : public SchedGraphCommon {
55 MachineBasicBlock &MBB;
56 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000057
58public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000059 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
60 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
61
Tanya Lattnerc50ee552003-08-27 02:42:58 +000062 MachineBasicBlock& getBasicBlock() const{return MBB;}
63 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000064 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
65 const_iterator onePair = find(MI);
66 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000067 }
68
Tanya Lattnerb6489f32003-08-25 22:42:20 +000069 // Debugging support
Tanya Lattnerc50ee552003-08-27 02:42:58 +000070 void dump() const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000071
Tanya Lattnerb6489f32003-08-25 22:42:20 +000072protected:
73 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
74 ~SchedGraph();
75
Vikram S. Adve78ef1392001-08-28 23:06:02 +000076 // Unordered iterators.
77 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
78 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000079 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
80 return GraphMap.begin();
81 }
82 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
83 return GraphMap.end();
84 }
85
86 unsigned size() { return GraphMap.size(); }
87 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000088
Tanya Lattnerb6489f32003-08-25 22:42:20 +000089 SchedGraphNode *&operator[](const MachineInstr *MI) {
90 return GraphMap[MI];
91 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000092
93private:
94 friend class SchedGraphSet; // give access to ctor
Tanya Lattnerc50ee552003-08-27 02:42:58 +000095
Vikram S. Adve78ef1392001-08-28 23:06:02 +000096 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +000097 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000098 assert((*this)[minstr] == NULL);
99 (*this)[minstr] = node;
100 }
101
102 //
103 // Graph builder
104 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000105 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000107 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
108 std::vector<SchedGraphNode*>& memNV,
109 std::vector<SchedGraphNode*>& callNV,
110 RegToRefVecMap& regToRefVecMap,
111 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000112
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000113
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000114 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
115 std::vector<SchedGraphNode*>& memNV,
116 std::vector<SchedGraphNode*>& callNV,
117 RegToRefVecMap& regToRefVecMap,
118 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000119
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000120 void addEdgesForInstruction(const MachineInstr& minstr,
121 const ValueToDefVecMap& valueToDefVecMap,
122 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000123
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000124 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000126 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
127 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000128
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000129 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
130 MachineBasicBlock& bbMvec,
131 const TargetMachine& target);
132
133 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
134 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000135
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000136 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
137 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000138
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000139 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
140 const Value* defValue, bool refNodeIsDef,
141 bool refNodeIsDefAndUse,
142 const TargetMachine& target);
143
144 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000145
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000146};
147
148
Chris Lattner9efc4d62003-06-02 22:45:07 +0000149
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000150class SchedGraphSet {
151 const Function* function;
152 std::vector<SchedGraph*> Graphs;
153
154 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000155 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000156
157 inline void addGraph(SchedGraph* graph) {
158 assert(graph != NULL);
159 Graphs.push_back(graph);
160 }
161
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000163 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000164 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000165
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000166 //iterators
167 typedef std::vector<SchedGraph*>::const_iterator iterator;
168 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
169
170 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
171 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
172
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000173 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000174 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000175};
176
177
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000178
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179//********************** Sched Graph Iterators *****************************/
180
181// Ok to make it a template because it shd get instantiated at most twice:
182// for <SchedGraphNode, SchedGraphNode::iterator> and
183// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
184//
185template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000186class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000187protected:
188 _EdgeIter oi;
189public:
Chris Lattner455889a2002-02-12 22:39:50 +0000190 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000191
Chris Lattner455889a2002-02-12 22:39:50 +0000192 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000193
194 inline bool operator==(const _Self& x) const { return oi == x.oi; }
195 inline bool operator!=(const _Self& x) const { return !operator==(x); }
196
197 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000198 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000199 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000200
201 inline _EdgeType* getEdge() const { return *(oi); }
202
Chris Lattnercffebdc2001-09-07 21:07:10 +0000203 inline _Self &operator++() { ++oi; return *this; } // Preincrement
204 inline _Self operator++(int) { // Postincrement
205 _Self tmp(*this); ++*this; return tmp;
206 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000207
Chris Lattnercffebdc2001-09-07 21:07:10 +0000208 inline _Self &operator--() { --oi; return *this; } // Predecrement
209 inline _Self operator--(int) { // Postdecrement
210 _Self tmp = *this; --*this; return tmp;
211 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000212};
213
214template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000215class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000216protected:
217 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000218public:
Chris Lattner455889a2002-02-12 22:39:50 +0000219 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000220
Chris Lattner455889a2002-02-12 22:39:50 +0000221 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000222
Chris Lattnercffebdc2001-09-07 21:07:10 +0000223 inline bool operator==(const _Self& x) const { return oi == x.oi; }
224 inline bool operator!=(const _Self& x) const { return !operator==(x); }
225
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000226 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000227 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000228
229 inline _EdgeType* getEdge() const { return *(oi); }
230
231 inline _Self &operator++() { ++oi; return *this; } // Preincrement
232 inline _Self operator++(int) { // Postincrement
233 _Self tmp(*this); ++*this; return tmp;
234 }
235
236 inline _Self &operator--() { --oi; return *this; } // Predecrement
237 inline _Self operator--(int) { // Postdecrement
238 _Self tmp = *this; --*this; return tmp;
239 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000240};
241
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000242//
243// sg_pred_iterator
244// sg_pred_const_iterator
245//
Chris Lattner455889a2002-02-12 22:39:50 +0000246typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000247 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000248typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000249 sg_pred_const_iterator;
250
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000251inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000252 return sg_pred_iterator(N->beginInEdges());
253}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000254inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000255 return sg_pred_iterator(N->endInEdges());
256}
257inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
258 return sg_pred_const_iterator(N->beginInEdges());
259}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000260inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000261 return sg_pred_const_iterator(N->endInEdges());
262}
263
264
265//
266// sg_succ_iterator
267// sg_succ_const_iterator
268//
Chris Lattner455889a2002-02-12 22:39:50 +0000269typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000270 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000271typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000272 sg_succ_const_iterator;
273
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000274inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000275 return sg_succ_iterator(N->beginOutEdges());
276}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000277inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000278 return sg_succ_iterator(N->endOutEdges());
279}
280inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
281 return sg_succ_const_iterator(N->beginOutEdges());
282}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000283inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000284 return sg_succ_const_iterator(N->endOutEdges());
285}
286
Chris Lattner3ff43872001-09-28 22:56:31 +0000287// Provide specializations of GraphTraits to be able to use graph iterators on
288// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000289//
Chris Lattner3ff43872001-09-28 22:56:31 +0000290template <> struct GraphTraits<SchedGraph*> {
291 typedef SchedGraphNode NodeType;
292 typedef sg_succ_iterator ChildIteratorType;
293
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000294 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000295 static inline ChildIteratorType child_begin(NodeType *N) {
296 return succ_begin(N);
297 }
298 static inline ChildIteratorType child_end(NodeType *N) {
299 return succ_end(N);
300 }
301};
302
303template <> struct GraphTraits<const SchedGraph*> {
304 typedef const SchedGraphNode NodeType;
305 typedef sg_succ_const_iterator ChildIteratorType;
306
307 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000308 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000309 }
310 static inline ChildIteratorType child_begin(NodeType *N) {
311 return succ_begin(N);
312 }
313 static inline ChildIteratorType child_end(NodeType *N) {
314 return succ_end(N);
315 }
316};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000317
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000318#endif