blob: 78718447e5f74d412774012c77ab73609cf2ae89 [file] [log] [blame]
Misha Brukman8baa01c2003-04-09 21:51:34 +00001//===- ModuloSchedGraph.h - Modulo Scheduling Graph and Set -*- C++ -*-----===//
Guochun Shif1c154f2003-03-27 17:57:44 +00002//
3// This header defines the primative classes that make up a data structure
4// graph.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef LLVM_CODEGEN_MODULO_SCHED_GRAPH_H
9#define LLVM_CODEGEN_MODULO_SCHED_GRAPH_H
Misha Brukman8baa01c2003-04-09 21:51:34 +000010
Guochun Shif1c154f2003-03-27 17:57:44 +000011#include "llvm/Instruction.h"
12#include "llvm/Target/TargetMachine.h"
Guochun Shi6fbe5fb2003-04-06 23:56:19 +000013#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman8baa01c2003-04-09 21:51:34 +000014#include "Support/HashExtras.h"
15#include "Support/GraphTraits.h"
Misha Brukman2c821cc2003-04-10 19:19:23 +000016#include "Support/hash_map"
Misha Brukman8baa01c2003-04-09 21:51:34 +000017#include "../InstrSched/SchedGraphCommon.h"
Guochun Shif1c154f2003-03-27 17:57:44 +000018#include <iostream>
Guochun Shif1c154f2003-03-27 17:57:44 +000019
Misha Brukman8baa01c2003-04-09 21:51:34 +000020//===----------------------------------------------------------------------===//
21// ModuloSchedGraphNode - Implement a data structure based on the
22// SchedGraphNodeCommon this class stores informtion needed later to order the
23// nodes in modulo scheduling
24//
25class ModuloSchedGraphNode:public SchedGraphNodeCommon {
Guochun Shif1c154f2003-03-27 17:57:44 +000026private:
Misha Brukman8baa01c2003-04-09 21:51:34 +000027 // the corresponding instruction
28 const Instruction *inst;
Guochun Shif1c154f2003-03-27 17:57:44 +000029
Misha Brukman8baa01c2003-04-09 21:51:34 +000030 // whether this node's property(ASAP,ALAP, ...) has been computed
Guochun Shif1c154f2003-03-27 17:57:44 +000031 bool propertyComputed;
32
Misha Brukman8baa01c2003-04-09 21:51:34 +000033 // ASAP: the earliest time the node could be scheduled
34 // ALAP: the latest time the node couldbe scheduled
35 // depth: the depth of the node
36 // height: the height of the node
37 // mov: the mobility function, computed as ALAP - ASAP
38 // scheTime: the scheduled time if this node has been scheduled
39 // earlyStart: the earliest time to be tried to schedule the node
40 // lateStart: the latest time to be tried to schedule the node
Guochun Shif1c154f2003-03-27 17:57:44 +000041 int ASAP, ALAP, depth, height, mov;
42 int schTime;
Misha Brukman8baa01c2003-04-09 21:51:34 +000043 int earlyStart, lateStart;
Guochun Shif1c154f2003-03-27 17:57:44 +000044
45public:
Misha Brukman8baa01c2003-04-09 21:51:34 +000046
Guochun Shif1c154f2003-03-27 17:57:44 +000047 //get the instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +000048 const Instruction *getInst() const {
49 return inst;
50 }
Guochun Shif1c154f2003-03-27 17:57:44 +000051 //get the instruction op-code name
Misha Brukman8baa01c2003-04-09 21:51:34 +000052 const char *getInstOpcodeName() const {
53 return inst->getOpcodeName();
54 }
55 //get the instruction op-code
56 const unsigned getInstOpcode() const {
57 return inst->getOpcode();
58 }
Guochun Shif1c154f2003-03-27 17:57:44 +000059 //return whether the node is NULL
Misha Brukman8baa01c2003-04-09 21:51:34 +000060 bool isNullNode() const {
61 return (inst == NULL);
62 }
Guochun Shif1c154f2003-03-27 17:57:44 +000063 //return whether the property of the node has been computed
Misha Brukman8baa01c2003-04-09 21:51:34 +000064 bool getPropertyComputed() {
65 return propertyComputed;
66 }
Guochun Shif1c154f2003-03-27 17:57:44 +000067 //set the propertyComputed
Misha Brukman8baa01c2003-04-09 21:51:34 +000068 void setPropertyComputed(bool _propertyComputed) {
69 propertyComputed = _propertyComputed;
70 }
71
Guochun Shif1c154f2003-03-27 17:57:44 +000072 //get the corresponding property
Misha Brukman8baa01c2003-04-09 21:51:34 +000073 int getASAP() {
74 return ASAP;
75 }
76 int getALAP() {
77 return ALAP;
78 }
79 int getMov() {
80 return mov;
81 }
82 int getDepth() {
83 return depth;
84 }
85 int getHeight() {
86 return height;
87 }
88 int getSchTime() {
89 return schTime;
90 }
91 int getEarlyStart() {
92 return earlyStart;
93 }
94 int getLateStart() {
95 return lateStart;
96 }
97 void setEarlyStart(int _earlyStart) {
98 earlyStart = _earlyStart;
99 }
100 void setLateStart(int _lateStart) {
101 lateStart = _lateStart;
102 }
103 void setSchTime(int _time) {
104 schTime = _time;
105 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000106
Misha Brukman8baa01c2003-04-09 21:51:34 +0000107private:
Guochun Shif1c154f2003-03-27 17:57:44 +0000108 friend class ModuloSchedGraph;
109 friend class SchedGraphNode;
110
111 //constructor:
112 //nodeId: the node id, unique within the each BasicBlock
113 //_bb: which BasicBlock the corresponding instruction belongs to
114 //_inst: the corresponding instruction
115 //indexInBB: the corresponding instruction's index in the BasicBlock
116 //target: the targetMachine
117 ModuloSchedGraphNode(unsigned int _nodeId,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000118 const BasicBlock * _bb,
119 const Instruction * _inst,
120 int indexInBB, const TargetMachine &target);
121
122
123 friend std::ostream & operator<<(std::ostream & os,
124 const ModuloSchedGraphNode & edge);
125
Guochun Shif1c154f2003-03-27 17:57:44 +0000126};
127
Guochun Shif1c154f2003-03-27 17:57:44 +0000128//FIXME: these two value should not be used
129#define MAXNODE 100
130#define MAXCC 100
131
Guochun Shif1c154f2003-03-27 17:57:44 +0000132//===----------------------------------------------------------------------===//
133/// ModuloSchedGraph- the data structure to store dependence between nodes
134/// it catches data dependence and constrol dependence
135///
Misha Brukman8baa01c2003-04-09 21:51:34 +0000136class ModuloSchedGraph :
Guochun Shif1c154f2003-03-27 17:57:44 +0000137 public SchedGraphCommon,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000138 protected hash_map<const Instruction*,ModuloSchedGraphNode*> {
139
140private:
Guochun Shif1c154f2003-03-27 17:57:44 +0000141 //iteration Interval
142 int MII;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000143
Guochun Shif1c154f2003-03-27 17:57:44 +0000144 //target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000145 const TargetMachine & target;
Guochun Shif1c154f2003-03-27 17:57:44 +0000146
147 //the circuits in the dependence graph
148 unsigned circuits[MAXCC][MAXNODE];
149
150 //the order nodes
151 std::vector<ModuloSchedGraphNode*> oNodes;
152
153 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
154
155 //the function to compute properties
Misha Brukman2c821cc2003-04-10 19:19:23 +0000156 void computeNodeASAP(const BasicBlock *bb);
157 void computeNodeALAP(const BasicBlock *bb);
158 void computeNodeMov(const BasicBlock *bb);
159 void computeNodeDepth(const BasicBlock *bb);
160 void computeNodeHeight(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000161
162 //the function to compute node property
Misha Brukman2c821cc2003-04-10 19:19:23 +0000163 void computeNodeProperty(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000164
165 //the function to sort nodes
166 void orderNodes();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000167
Guochun Shif1c154f2003-03-27 17:57:44 +0000168 //add the resource usage
Misha Brukman2c821cc2003-04-10 19:19:23 +0000169void addResourceUsage(std::vector<std::pair<int,int> > &, int);
Guochun Shif1c154f2003-03-27 17:57:44 +0000170
171 //debug functions:
172 //dump circuits
173 void dumpCircuits();
174 //dump the input set of nodes
175 void dumpSet(std::vector<ModuloSchedGraphNode*> set);
176 //dump the input resource usage table
Misha Brukman2c821cc2003-04-10 19:19:23 +0000177 void dumpResourceUsage(std::vector<std::pair<int,int> > &);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000178
179public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000180 //help functions
Misha Brukman8baa01c2003-04-09 21:51:34 +0000181
Guochun Shif1c154f2003-03-27 17:57:44 +0000182 //get the maxium the delay between two nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000183 SchedGraphEdge *getMaxDelayEdge(unsigned srcId, unsigned sinkId);
Guochun Shif1c154f2003-03-27 17:57:44 +0000184
185 //FIXME:
186 //get the predessor Set of the set
187 NodeVec predSet(NodeVec set, unsigned, unsigned);
188 NodeVec predSet(NodeVec set);
189
190 //get the predessor set of the node
Misha Brukman2c821cc2003-04-10 19:19:23 +0000191 NodeVec predSet(ModuloSchedGraphNode *node, unsigned, unsigned);
192 NodeVec predSet(ModuloSchedGraphNode *node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000193
194 //get the successor set of the set
195 NodeVec succSet(NodeVec set, unsigned, unsigned);
196 NodeVec succSet(NodeVec set);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000197
Guochun Shif1c154f2003-03-27 17:57:44 +0000198 //get the succssor set of the node
Misha Brukman2c821cc2003-04-10 19:19:23 +0000199 NodeVec succSet(ModuloSchedGraphNode *node, unsigned, unsigned);
200 NodeVec succSet(ModuloSchedGraphNode *node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000201
202 //return the uniton of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000203 NodeVec vectorUnion(NodeVec set1, NodeVec set2);
Guochun Shif1c154f2003-03-27 17:57:44 +0000204
205 //return the consjuction of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000206 NodeVec vectorConj(NodeVec set1, NodeVec set2);
207
Guochun Shif1c154f2003-03-27 17:57:44 +0000208 //return all nodes in set1 but not set2
209 NodeVec vectorSub(NodeVec set1, NodeVec set2);
210
Misha Brukman8baa01c2003-04-09 21:51:34 +0000211 typedef hash_map<const Instruction*,ModuloSchedGraphNode*> map_base;
212
Guochun Shif1c154f2003-03-27 17:57:44 +0000213public:
214 using map_base::iterator;
215 using map_base::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000216
Guochun Shif1c154f2003-03-27 17:57:44 +0000217public:
218
219 //get target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000220 const TargetMachine & getTarget() {
221 return target;
222 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000223 //get the iteration interval
Misha Brukman8baa01c2003-04-09 21:51:34 +0000224 const int getMII() {
225 return MII;
226 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000227
228 //get the ordered nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000229 const NodeVec & getONodes() {
230 return oNodes;
231 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000232
233 //get the number of nodes (including the root and leaf)
234 //note: actually root and leaf is not used
Misha Brukman8baa01c2003-04-09 21:51:34 +0000235 const unsigned int getNumNodes() const {
236 return size() + 2;
237 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000238 //return wether the BasicBlock 'bb' contains a loop
Misha Brukman8baa01c2003-04-09 21:51:34 +0000239 bool isLoop(const BasicBlock * bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000240
241 //return this basibBlock contains a loop
Misha Brukman8baa01c2003-04-09 21:51:34 +0000242 bool isLoop();
243
Guochun Shif1c154f2003-03-27 17:57:44 +0000244 //return the node for the input instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +0000245 ModuloSchedGraphNode *getGraphNodeForInst(const Instruction * inst) const {
Guochun Shif1c154f2003-03-27 17:57:44 +0000246 const_iterator onePair = this->find(inst);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000247 return (onePair != this->end()) ? (*onePair).second : NULL;
Guochun Shif1c154f2003-03-27 17:57:44 +0000248 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000249
250 // Debugging support
251 //dump the graph
252 void dump() const;
253
254 // dump the basicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000255 void dump(const BasicBlock * bb);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000256
Guochun Shif1c154f2003-03-27 17:57:44 +0000257 //dump the basicBlock into 'os' stream
Misha Brukman8baa01c2003-04-09 21:51:34 +0000258 void dump(const BasicBlock * bb, std::ostream & os);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000259
Guochun Shif1c154f2003-03-27 17:57:44 +0000260 //dump the node property
Misha Brukman8baa01c2003-04-09 21:51:34 +0000261 void dumpNodeProperty() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000262
Misha Brukman8baa01c2003-04-09 21:51:34 +0000263private:
264 friend class ModuloSchedGraphSet; //give access to ctor
265
266public:
267 ModuloSchedGraph(const BasicBlock *bb, const TargetMachine &_target)
Misha Brukman2c821cc2003-04-10 19:19:23 +0000268 :SchedGraphCommon(bb), target(_target)
269 {
Guochun Shif1c154f2003-03-27 17:57:44 +0000270 buildGraph(target);
271 }
272
Misha Brukman8baa01c2003-04-09 21:51:34 +0000273 ~ModuloSchedGraph() {
274 for (const_iterator I = begin(); I != end(); ++I)
Guochun Shif1c154f2003-03-27 17:57:44 +0000275 delete I->second;
276 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000277
Misha Brukman2c821cc2003-04-10 19:19:23 +0000278 // Unorder iterators
279 // return values are pair<const Instruction*, ModuloSchedGraphNode*>
Guochun Shif1c154f2003-03-27 17:57:44 +0000280 using map_base::begin;
281 using map_base::end;
282
Misha Brukman8baa01c2003-04-09 21:51:34 +0000283 void noteModuloSchedGraphNodeForInst(const Instruction *inst,
284 ModuloSchedGraphNode *node)
285 {
286 assert((*this)[inst] == NULL);
287 (*this)[inst] = node;
288 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000289
Guochun Shif1c154f2003-03-27 17:57:44 +0000290 //Graph builder
Misha Brukman8baa01c2003-04-09 21:51:34 +0000291
292 ModuloSchedGraphNode *getNode(const unsigned nodeId) const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000293
294 //build the graph from the basicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000295 void buildGraph(const TargetMachine & target);
Guochun Shif1c154f2003-03-27 17:57:44 +0000296
297 //Build nodes for BasicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000298 void buildNodesforBB(const TargetMachine &target,
299 const BasicBlock *bb,
300 NodeVec &memNode,
301 RegToRefVecMap &regToRefVecMap,
302 ValueToDefVecMap &valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000303
304 //find definitiona and use information for all nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000305 void findDefUseInfoAtInstr(const TargetMachine &target,
306 ModuloSchedGraphNode *node,
307 NodeVec &memNode,
308 RegToRefVecMap &regToRefVecMap,
309 ValueToDefVecMap &valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000310
311 //add def-use edge
Misha Brukman8baa01c2003-04-09 21:51:34 +0000312 void addDefUseEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000313
314 //add control dependence edges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000315 void addCDEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000316
317 //add memory dependence dges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000318 void addMemEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000319
320 //add dummy edges
321 void addDummyEdges();
322
323 //computer source restrictoin II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000324 int computeResII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000325
326 //computer recurrence II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000327 int computeRecII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000328};
329
Misha Brukman8baa01c2003-04-09 21:51:34 +0000330//==================================-
331// Graph set
Guochun Shif1c154f2003-03-27 17:57:44 +0000332
Misha Brukman8baa01c2003-04-09 21:51:34 +0000333class ModuloSchedGraphSet : public std::vector<ModuloSchedGraph*> {
Guochun Shif1c154f2003-03-27 17:57:44 +0000334private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000335 const Function *method;
336
Guochun Shif1c154f2003-03-27 17:57:44 +0000337public:
338 typedef std::vector<ModuloSchedGraph*> baseVector;
339 using baseVector::iterator;
340 using baseVector::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000341
Guochun Shif1c154f2003-03-27 17:57:44 +0000342public:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000343 ModuloSchedGraphSet(const Function *function, const TargetMachine &target);
344 ~ModuloSchedGraphSet();
345
346 // Iterators
Guochun Shif1c154f2003-03-27 17:57:44 +0000347 using baseVector::begin;
348 using baseVector::end;
349
Misha Brukman2c821cc2003-04-10 19:19:23 +0000350 // Debugging support
351 void dump() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000352
Guochun Shif1c154f2003-03-27 17:57:44 +0000353private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000354 void addGraph(ModuloSchedGraph *graph) {
355 assert(graph != NULL);
Guochun Shif1c154f2003-03-27 17:57:44 +0000356 this->push_back(graph);
357 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000358
Misha Brukman8baa01c2003-04-09 21:51:34 +0000359 // Graph builder
360 void buildGraphsForMethod(const Function *F,
361 const TargetMachine &target);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000362};
Misha Brukman8baa01c2003-04-09 21:51:34 +0000363
364#endif