blob: 50cc0520e6f35f1c834d391e67e83cded00e7e46 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SchedGraph.h - Scheduling Graph -------------------------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner179cdfb2002-08-09 20:08:03 +00009//
Chris Lattnercf3056d2003-10-13 03:32:08 +000010// This is a scheduling graph based on SSA graph plus extra dependence edges
11// capturing dependences due to machine resources (machine registers, CC
12// registers, and any others).
Chris Lattner179cdfb2002-08-09 20:08:03 +000013//
Chris Lattnercf3056d2003-10-13 03:32:08 +000014// This graph tries to leverage the SSA graph as much as possible, but captures
15// the extra dependences through a common interface.
Chris Lattner179cdfb2002-08-09 20:08:03 +000016//
17//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +000018
19#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
20#define LLVM_CODEGEN_SCHEDGRAPH_H
21
Tanya Lattnerb6489f32003-08-25 22:42:20 +000022#include "llvm/CodeGen/SchedGraphCommon.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000023#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/Transforms/Scalar.h"
25#include "Support/hash_map"
26#include "Support/GraphTraits.h"
27
Vikram S. Adve4a87b382001-09-30 23:37:26 +000028class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000029class ValueToDefVecMap;
30class RefVec;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000031
Tanya Lattnerb6489f32003-08-25 22:42:20 +000032class SchedGraphNode : public SchedGraphNodeCommon {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000033
Tanya Lattnerb6489f32003-08-25 22:42:20 +000034 MachineBasicBlock *MBB;
35 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000036
Tanya Lattnerb6489f32003-08-25 22:42:20 +000037
Tanya Lattnerc50ee552003-08-27 02:42:58 +000038 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
39 const TargetMachine& Target);
40 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000041
Vikram S. Adve78ef1392001-08-28 23:06:02 +000042 friend class SchedGraph; // give access for ctor and dtor
43 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000044
45public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000046
Tanya Lattnerc50ee552003-08-27 02:42:58 +000047 // Accessor methods
48 const MachineInstr* getMachineInstr() const { return MI; }
49 const MachineOpCode getOpCode() const { return MI->getOpCode(); }
50 bool isDummyNode() const { return (MI == NULL); }
51 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
52
Tanya Lattnerc50ee552003-08-27 02:42:58 +000053 void print(std::ostream &os) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000054};
55
Tanya Lattnerb6489f32003-08-25 22:42:20 +000056class SchedGraph : public SchedGraphCommon {
57 MachineBasicBlock &MBB;
58 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000059
60public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000061 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
62 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
63
Tanya Lattnerc50ee552003-08-27 02:42:58 +000064 MachineBasicBlock& getBasicBlock() const{return MBB;}
65 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000066 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
67 const_iterator onePair = find(MI);
68 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000069 }
70
Tanya Lattnerb6489f32003-08-25 22:42:20 +000071 // Debugging support
Tanya Lattnerc50ee552003-08-27 02:42:58 +000072 void dump() const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000073
Tanya Lattnerb6489f32003-08-25 22:42:20 +000074protected:
75 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
76 ~SchedGraph();
77
Vikram S. Adve78ef1392001-08-28 23:06:02 +000078 // Unordered iterators.
79 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
80 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000081 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
82 return GraphMap.begin();
83 }
84 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
85 return GraphMap.end();
86 }
87
88 unsigned size() { return GraphMap.size(); }
89 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000090
Tanya Lattnerb6489f32003-08-25 22:42:20 +000091 SchedGraphNode *&operator[](const MachineInstr *MI) {
92 return GraphMap[MI];
93 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000094
95private:
96 friend class SchedGraphSet; // give access to ctor
Tanya Lattnerc50ee552003-08-27 02:42:58 +000097
Vikram S. Adve78ef1392001-08-28 23:06:02 +000098 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +000099 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000100 assert((*this)[minstr] == NULL);
101 (*this)[minstr] = node;
102 }
103
104 //
105 // Graph builder
106 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000107 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000108
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000109 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
110 std::vector<SchedGraphNode*>& memNV,
111 std::vector<SchedGraphNode*>& callNV,
112 RegToRefVecMap& regToRefVecMap,
113 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000114
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000115
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000116 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
117 std::vector<SchedGraphNode*>& memNV,
118 std::vector<SchedGraphNode*>& callNV,
119 RegToRefVecMap& regToRefVecMap,
120 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000121
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000122 void addEdgesForInstruction(const MachineInstr& minstr,
123 const ValueToDefVecMap& valueToDefVecMap,
124 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000126 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000127
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
129 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000130
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000131 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
132 MachineBasicBlock& bbMvec,
133 const TargetMachine& target);
134
135 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
136 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000137
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000138 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
139 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000140
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000141 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
142 const Value* defValue, bool refNodeIsDef,
143 bool refNodeIsDefAndUse,
144 const TargetMachine& target);
145
146 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000147
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000148};
149
150
Chris Lattner9efc4d62003-06-02 22:45:07 +0000151
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000152class SchedGraphSet {
153 const Function* function;
154 std::vector<SchedGraph*> Graphs;
155
156 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000157 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000158
159 inline void addGraph(SchedGraph* graph) {
160 assert(graph != NULL);
161 Graphs.push_back(graph);
162 }
163
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000164public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000165 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000166 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000167
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000168 //iterators
169 typedef std::vector<SchedGraph*>::const_iterator iterator;
170 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
171
172 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
173 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
174
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000175 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000176 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000177};
178
179
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000180
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000181//********************** Sched Graph Iterators *****************************/
182
183// Ok to make it a template because it shd get instantiated at most twice:
184// for <SchedGraphNode, SchedGraphNode::iterator> and
185// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
186//
187template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000188class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000189protected:
190 _EdgeIter oi;
191public:
Chris Lattner455889a2002-02-12 22:39:50 +0000192 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000193
Chris Lattner455889a2002-02-12 22:39:50 +0000194 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000195
196 inline bool operator==(const _Self& x) const { return oi == x.oi; }
197 inline bool operator!=(const _Self& x) const { return !operator==(x); }
198
199 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000200 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000201 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000202
203 inline _EdgeType* getEdge() const { return *(oi); }
204
Chris Lattnercffebdc2001-09-07 21:07:10 +0000205 inline _Self &operator++() { ++oi; return *this; } // Preincrement
206 inline _Self operator++(int) { // Postincrement
207 _Self tmp(*this); ++*this; return tmp;
208 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000209
Chris Lattnercffebdc2001-09-07 21:07:10 +0000210 inline _Self &operator--() { --oi; return *this; } // Predecrement
211 inline _Self operator--(int) { // Postdecrement
212 _Self tmp = *this; --*this; return tmp;
213 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000214};
215
216template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000217class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000218protected:
219 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000220public:
Chris Lattner455889a2002-02-12 22:39:50 +0000221 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000222
Chris Lattner455889a2002-02-12 22:39:50 +0000223 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000224
Chris Lattnercffebdc2001-09-07 21:07:10 +0000225 inline bool operator==(const _Self& x) const { return oi == x.oi; }
226 inline bool operator!=(const _Self& x) const { return !operator==(x); }
227
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000228 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000229 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000230
231 inline _EdgeType* getEdge() const { return *(oi); }
232
233 inline _Self &operator++() { ++oi; return *this; } // Preincrement
234 inline _Self operator++(int) { // Postincrement
235 _Self tmp(*this); ++*this; return tmp;
236 }
237
238 inline _Self &operator--() { --oi; return *this; } // Predecrement
239 inline _Self operator--(int) { // Postdecrement
240 _Self tmp = *this; --*this; return tmp;
241 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000242};
243
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244//
245// sg_pred_iterator
246// sg_pred_const_iterator
247//
Chris Lattner455889a2002-02-12 22:39:50 +0000248typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000249 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000250typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000251 sg_pred_const_iterator;
252
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000253inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000254 return sg_pred_iterator(N->beginInEdges());
255}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000256inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000257 return sg_pred_iterator(N->endInEdges());
258}
259inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
260 return sg_pred_const_iterator(N->beginInEdges());
261}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000262inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000263 return sg_pred_const_iterator(N->endInEdges());
264}
265
266
267//
268// sg_succ_iterator
269// sg_succ_const_iterator
270//
Chris Lattner455889a2002-02-12 22:39:50 +0000271typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000272 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000273typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000274 sg_succ_const_iterator;
275
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000276inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000277 return sg_succ_iterator(N->beginOutEdges());
278}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000279inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000280 return sg_succ_iterator(N->endOutEdges());
281}
282inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
283 return sg_succ_const_iterator(N->beginOutEdges());
284}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000285inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000286 return sg_succ_const_iterator(N->endOutEdges());
287}
288
Chris Lattner3ff43872001-09-28 22:56:31 +0000289// Provide specializations of GraphTraits to be able to use graph iterators on
290// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000291//
Chris Lattner3ff43872001-09-28 22:56:31 +0000292template <> struct GraphTraits<SchedGraph*> {
293 typedef SchedGraphNode NodeType;
294 typedef sg_succ_iterator ChildIteratorType;
295
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000296 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000297 static inline ChildIteratorType child_begin(NodeType *N) {
298 return succ_begin(N);
299 }
300 static inline ChildIteratorType child_end(NodeType *N) {
301 return succ_end(N);
302 }
303};
304
305template <> struct GraphTraits<const SchedGraph*> {
306 typedef const SchedGraphNode NodeType;
307 typedef sg_succ_const_iterator ChildIteratorType;
308
309 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000310 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000311 }
312 static inline ChildIteratorType child_begin(NodeType *N) {
313 return succ_begin(N);
314 }
315 static inline ChildIteratorType child_end(NodeType *N) {
316 return succ_end(N);
317 }
318};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000319
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000320#endif