blob: e327598bd5758d9e7615c2cd6f33ef6a40b2bab9 [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; }
Brian Gaeke918cdd42004-02-12 01:34:05 +000051 const MachineOpCode getOpcode() const { return MI->getOpcode(); }
Tanya Lattnerc50ee552003-08-27 02:42:58 +000052 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
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000184//
185// sg_pred_iterator
186// sg_pred_const_iterator
187//
Chris Lattner455889a2002-02-12 22:39:50 +0000188typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000189 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000190typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000191 sg_pred_const_iterator;
192
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000193inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000194 return sg_pred_iterator(N->beginInEdges());
195}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000196inline sg_pred_iterator pred_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000197 return sg_pred_iterator(N->endInEdges());
198}
199inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
200 return sg_pred_const_iterator(N->beginInEdges());
201}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000202inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000203 return sg_pred_const_iterator(N->endInEdges());
204}
205
206
207//
208// sg_succ_iterator
209// sg_succ_const_iterator
210//
Chris Lattner455889a2002-02-12 22:39:50 +0000211typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000212 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000213typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000214 sg_succ_const_iterator;
215
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000216inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217 return sg_succ_iterator(N->beginOutEdges());
218}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000219inline sg_succ_iterator succ_end(SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000220 return sg_succ_iterator(N->endOutEdges());
221}
222inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
223 return sg_succ_const_iterator(N->beginOutEdges());
224}
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000225inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000226 return sg_succ_const_iterator(N->endOutEdges());
227}
228
Chris Lattner3ff43872001-09-28 22:56:31 +0000229// Provide specializations of GraphTraits to be able to use graph iterators on
230// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000231//
Chris Lattner3ff43872001-09-28 22:56:31 +0000232template <> struct GraphTraits<SchedGraph*> {
233 typedef SchedGraphNode NodeType;
234 typedef sg_succ_iterator ChildIteratorType;
235
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000236 static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
Chris Lattner3ff43872001-09-28 22:56:31 +0000237 static inline ChildIteratorType child_begin(NodeType *N) {
238 return succ_begin(N);
239 }
240 static inline ChildIteratorType child_end(NodeType *N) {
241 return succ_end(N);
242 }
243};
244
245template <> struct GraphTraits<const SchedGraph*> {
246 typedef const SchedGraphNode NodeType;
247 typedef sg_succ_const_iterator ChildIteratorType;
248
249 static inline NodeType *getEntryNode(const SchedGraph *SG) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000250 return (NodeType*)SG->getRoot();
Chris Lattner3ff43872001-09-28 22:56:31 +0000251 }
252 static inline ChildIteratorType child_begin(NodeType *N) {
253 return succ_begin(N);
254 }
255 static inline ChildIteratorType child_end(NodeType *N) {
256 return succ_end(N);
257 }
258};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000259
Brian Gaeked0fde302003-11-11 22:41:34 +0000260} // End llvm namespace
261
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000262#endif