blob: 5aee9b25f6939f0817baa4c0d6eccee680f480d0 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Vikram S. Adve4a87b382001-09-30 23:37:26 +000030class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000031class ValueToDefVecMap;
32class RefVec;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000033
Tanya Lattnerb6489f32003-08-25 22:42:20 +000034class SchedGraphNode : public SchedGraphNodeCommon {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000035
Tanya Lattnerb6489f32003-08-25 22:42:20 +000036 MachineBasicBlock *MBB;
37 const MachineInstr *MI;
Chris Lattner9efc4d62003-06-02 22:45:07 +000038
Tanya Lattnerb6489f32003-08-25 22:42:20 +000039
Tanya Lattnerc50ee552003-08-27 02:42:58 +000040 SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
41 const TargetMachine& Target);
42 ~SchedGraphNode();
Tanya Lattnerb6489f32003-08-25 22:42:20 +000043
Vikram S. Adve78ef1392001-08-28 23:06:02 +000044 friend class SchedGraph; // give access for ctor and dtor
45 friend class SchedGraphEdge; // give access for adding edges
Tanya Lattnerb6489f32003-08-25 22:42:20 +000046
47public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000048
Tanya Lattnerc50ee552003-08-27 02:42:58 +000049 // Accessor methods
50 const MachineInstr* getMachineInstr() const { return MI; }
51 const MachineOpCode getOpCode() const { return MI->getOpCode(); }
52 bool isDummyNode() const { return (MI == NULL); }
53 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
54
Tanya Lattnerc50ee552003-08-27 02:42:58 +000055 void print(std::ostream &os) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000056};
57
Tanya Lattnerb6489f32003-08-25 22:42:20 +000058class SchedGraph : public SchedGraphCommon {
59 MachineBasicBlock &MBB;
60 hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000061
62public:
Tanya Lattnerb6489f32003-08-25 22:42:20 +000063 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
64 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
65
Tanya Lattnerc50ee552003-08-27 02:42:58 +000066 MachineBasicBlock& getBasicBlock() const{return MBB;}
67 const unsigned int getNumNodes() const { return GraphMap.size()+2; }
Tanya Lattnerb6489f32003-08-25 22:42:20 +000068 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
69 const_iterator onePair = find(MI);
70 return (onePair != end())? onePair->second : NULL;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000071 }
72
Tanya Lattnerb6489f32003-08-25 22:42:20 +000073 // Debugging support
Tanya Lattnerc50ee552003-08-27 02:42:58 +000074 void dump() const;
Vikram S. Advef0b6d792001-09-18 12:49:26 +000075
Tanya Lattnerb6489f32003-08-25 22:42:20 +000076protected:
77 SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
78 ~SchedGraph();
79
Vikram S. Adve78ef1392001-08-28 23:06:02 +000080 // Unordered iterators.
81 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
82 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +000083 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
84 return GraphMap.begin();
85 }
86 hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
87 return GraphMap.end();
88 }
89
90 unsigned size() { return GraphMap.size(); }
91 iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000092
Tanya Lattnerb6489f32003-08-25 22:42:20 +000093 SchedGraphNode *&operator[](const MachineInstr *MI) {
94 return GraphMap[MI];
95 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000096
97private:
98 friend class SchedGraphSet; // give access to ctor
Tanya Lattnerc50ee552003-08-27 02:42:58 +000099
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000100 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000101 SchedGraphNode* node) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000102 assert((*this)[minstr] == NULL);
103 (*this)[minstr] = node;
104 }
105
106 //
107 // Graph builder
108 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000109 void buildGraph(const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000110
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000111 void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
112 std::vector<SchedGraphNode*>& memNV,
113 std::vector<SchedGraphNode*>& callNV,
114 RegToRefVecMap& regToRefVecMap,
115 ValueToDefVecMap& valueToDefVecMap);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000116
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000117
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000118 void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
119 std::vector<SchedGraphNode*>& memNV,
120 std::vector<SchedGraphNode*>& callNV,
121 RegToRefVecMap& regToRefVecMap,
122 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000123
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000124 void addEdgesForInstruction(const MachineInstr& minstr,
125 const ValueToDefVecMap& valueToDefVecMap,
126 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000127
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128 void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000129
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000130 void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
131 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000132
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000133 void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
134 MachineBasicBlock& bbMvec,
135 const TargetMachine& target);
136
137 void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
138 const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000139
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000140 void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
141 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000142
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000143 void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
144 const Value* defValue, bool refNodeIsDef,
145 bool refNodeIsDefAndUse,
146 const TargetMachine& target);
147
148 void addDummyEdges();
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000149
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000150};
151
152
Chris Lattner9efc4d62003-06-02 22:45:07 +0000153
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000154class SchedGraphSet {
155 const Function* function;
156 std::vector<SchedGraph*> Graphs;
157
158 // Graph builder
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000159 void buildGraphsForMethod(const Function *F, const TargetMachine& target);
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000160
161 inline void addGraph(SchedGraph* graph) {
162 assert(graph != NULL);
163 Graphs.push_back(graph);
164 }
165
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000166public:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000167 SchedGraphSet(const Function *function, const TargetMachine& target);
Chris Lattner9efc4d62003-06-02 22:45:07 +0000168 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000169
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000170 //iterators
171 typedef std::vector<SchedGraph*>::const_iterator iterator;
172 typedef std::vector<SchedGraph*>::const_iterator const_iterator;
173
174 std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
175 std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
176
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000177 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000178 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179};
180
181
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000182
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000183//********************** Sched Graph Iterators *****************************/
184
185// Ok to make it a template because it shd get instantiated at most twice:
186// for <SchedGraphNode, SchedGraphNode::iterator> and
187// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
188//
189template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000190class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000191protected:
192 _EdgeIter oi;
193public:
Chris Lattner455889a2002-02-12 22:39:50 +0000194 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000195
Chris Lattner455889a2002-02-12 22:39:50 +0000196 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000197
198 inline bool operator==(const _Self& x) const { return oi == x.oi; }
199 inline bool operator!=(const _Self& x) const { return !operator==(x); }
200
201 // operator*() differs for pred or succ iterator
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000202 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000203 inline _NodeType* operator->() const { return operator*(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000204
205 inline _EdgeType* getEdge() const { return *(oi); }
206
Chris Lattnercffebdc2001-09-07 21:07:10 +0000207 inline _Self &operator++() { ++oi; return *this; } // Preincrement
208 inline _Self operator++(int) { // Postincrement
209 _Self tmp(*this); ++*this; return tmp;
210 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000211
Chris Lattnercffebdc2001-09-07 21:07:10 +0000212 inline _Self &operator--() { --oi; return *this; } // Predecrement
213 inline _Self operator--(int) { // Postdecrement
214 _Self tmp = *this; --*this; return tmp;
215 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000216};
217
218template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000219class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000220protected:
221 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000222public:
Chris Lattner455889a2002-02-12 22:39:50 +0000223 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000224
Chris Lattner455889a2002-02-12 22:39:50 +0000225 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000226
Chris Lattnercffebdc2001-09-07 21:07:10 +0000227 inline bool operator==(const _Self& x) const { return oi == x.oi; }
228 inline bool operator!=(const _Self& x) const { return !operator==(x); }
229
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000230 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000231 inline _NodeType* operator->() const { return operator*(); }
Chris Lattnercffebdc2001-09-07 21:07:10 +0000232
233 inline _EdgeType* getEdge() const { return *(oi); }
234
235 inline _Self &operator++() { ++oi; return *this; } // Preincrement
236 inline _Self operator++(int) { // Postincrement
237 _Self tmp(*this); ++*this; return tmp;
238 }
239
240 inline _Self &operator--() { --oi; return *this; } // Predecrement
241 inline _Self operator--(int) { // Postdecrement
242 _Self tmp = *this; --*this; return tmp;
243 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244};
245
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000246//
247// sg_pred_iterator
248// sg_pred_const_iterator
249//
Chris Lattner455889a2002-02-12 22:39:50 +0000250typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000251 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000252typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000253 sg_pred_const_iterator;
254
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000255inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000256 return sg_pred_iterator(N->beginInEdges());
257}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000258inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000259 return sg_pred_iterator(N->endInEdges());
260}
261inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
262 return sg_pred_const_iterator(N->beginInEdges());
263}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000264inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000265 return sg_pred_const_iterator(N->endInEdges());
266}
267
268
269//
270// sg_succ_iterator
271// sg_succ_const_iterator
272//
Chris Lattner455889a2002-02-12 22:39:50 +0000273typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000274 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000275typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000276 sg_succ_const_iterator;
277
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000278inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000279 return sg_succ_iterator(N->beginOutEdges());
280}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000281inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000282 return sg_succ_iterator(N->endOutEdges());
283}
284inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
285 return sg_succ_const_iterator(N->beginOutEdges());
286}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000287inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000288 return sg_succ_const_iterator(N->endOutEdges());
289}
290
Chris Lattner3ff43872001-09-28 22:56:31 +0000291// Provide specializations of GraphTraits to be able to use graph iterators on
292// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000293//
Chris Lattner3ff43872001-09-28 22:56:31 +0000294template <> struct GraphTraits<SchedGraph*> {
295 typedef SchedGraphNode NodeType;
296 typedef sg_succ_iterator ChildIteratorType;
297
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000298 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000299 static inline ChildIteratorType child_begin(NodeType *N) {
300 return succ_begin(N);
301 }
302 static inline ChildIteratorType child_end(NodeType *N) {
303 return succ_end(N);
304 }
305};
306
307template <> struct GraphTraits<const SchedGraph*> {
308 typedef const SchedGraphNode NodeType;
309 typedef sg_succ_const_iterator ChildIteratorType;
310
311 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000312 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000313 }
314 static inline ChildIteratorType child_begin(NodeType *N) {
315 return succ_begin(N);
316 }
317 static inline ChildIteratorType child_end(NodeType *N) {
318 return succ_end(N);
319 }
320};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000321
Brian Gaeked0fde302003-11-11 22:41:34 +0000322} // End llvm namespace
323
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000324#endif