blob: 2abd448fcdc1f109663d0fe60579ef361e80bad0 [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/GraphTraits.h"
Misha Brukman2c821cc2003-04-10 19:19:23 +000015#include "Support/hash_map"
Misha Brukman8baa01c2003-04-09 21:51:34 +000016#include "../InstrSched/SchedGraphCommon.h"
Guochun Shif1c154f2003-03-27 17:57:44 +000017#include <iostream>
Guochun Shif1c154f2003-03-27 17:57:44 +000018
Misha Brukman8baa01c2003-04-09 21:51:34 +000019//===----------------------------------------------------------------------===//
20// ModuloSchedGraphNode - Implement a data structure based on the
21// SchedGraphNodeCommon this class stores informtion needed later to order the
22// nodes in modulo scheduling
23//
24class ModuloSchedGraphNode:public SchedGraphNodeCommon {
Guochun Shif1c154f2003-03-27 17:57:44 +000025private:
Misha Brukman8baa01c2003-04-09 21:51:34 +000026 // the corresponding instruction
27 const Instruction *inst;
Guochun Shif1c154f2003-03-27 17:57:44 +000028
Misha Brukman8baa01c2003-04-09 21:51:34 +000029 // whether this node's property(ASAP,ALAP, ...) has been computed
Guochun Shif1c154f2003-03-27 17:57:44 +000030 bool propertyComputed;
31
Misha Brukman8baa01c2003-04-09 21:51:34 +000032 // ASAP: the earliest time the node could be scheduled
33 // ALAP: the latest time the node couldbe scheduled
34 // depth: the depth of the node
35 // height: the height of the node
36 // mov: the mobility function, computed as ALAP - ASAP
37 // scheTime: the scheduled time if this node has been scheduled
38 // earlyStart: the earliest time to be tried to schedule the node
39 // lateStart: the latest time to be tried to schedule the node
Guochun Shif1c154f2003-03-27 17:57:44 +000040 int ASAP, ALAP, depth, height, mov;
41 int schTime;
Misha Brukman8baa01c2003-04-09 21:51:34 +000042 int earlyStart, lateStart;
Guochun Shif1c154f2003-03-27 17:57:44 +000043
44public:
Misha Brukman8baa01c2003-04-09 21:51:34 +000045
Guochun Shif1c154f2003-03-27 17:57:44 +000046 //get the instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +000047 const Instruction *getInst() const {
48 return inst;
49 }
Guochun Shif1c154f2003-03-27 17:57:44 +000050 //get the instruction op-code name
Misha Brukman8baa01c2003-04-09 21:51:34 +000051 const char *getInstOpcodeName() const {
52 return inst->getOpcodeName();
53 }
54 //get the instruction op-code
55 const unsigned getInstOpcode() const {
56 return inst->getOpcode();
57 }
Guochun Shi099b0642003-06-02 17:48:56 +000058
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
Guochun Shif3252612003-06-10 19:09:00 +0000122
Misha Brukman8baa01c2003-04-09 21:51:34 +0000123 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 Shi099b0642003-06-02 17:48:56 +0000141
142 BasicBlock* bb;
143
Guochun Shif1c154f2003-03-27 17:57:44 +0000144 //iteration Interval
145 int MII;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000146
Guochun Shif1c154f2003-03-27 17:57:44 +0000147 //target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000148 const TargetMachine & target;
Guochun Shif1c154f2003-03-27 17:57:44 +0000149
150 //the circuits in the dependence graph
151 unsigned circuits[MAXCC][MAXNODE];
152
153 //the order nodes
154 std::vector<ModuloSchedGraphNode*> oNodes;
155
156 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
157
158 //the function to compute properties
Guochun Shi099b0642003-06-02 17:48:56 +0000159 void computeNodeASAP(const BasicBlock * in_bb);
160 void computeNodeALAP(const BasicBlock * in_bb);
161 void computeNodeMov(const BasicBlock * in_bb);
162 void computeNodeDepth(const BasicBlock * in_bb);
163 void computeNodeHeight(const BasicBlock * in_bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000164
165 //the function to compute node property
Guochun Shi099b0642003-06-02 17:48:56 +0000166 void computeNodeProperty(const BasicBlock * in_bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000167
168 //the function to sort nodes
169 void orderNodes();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000170
Guochun Shif1c154f2003-03-27 17:57:44 +0000171 //add the resource usage
Misha Brukman2c821cc2003-04-10 19:19:23 +0000172void addResourceUsage(std::vector<std::pair<int,int> > &, int);
Guochun Shif1c154f2003-03-27 17:57:44 +0000173
174 //debug functions:
175 //dump circuits
176 void dumpCircuits();
177 //dump the input set of nodes
178 void dumpSet(std::vector<ModuloSchedGraphNode*> set);
179 //dump the input resource usage table
Misha Brukman2c821cc2003-04-10 19:19:23 +0000180 void dumpResourceUsage(std::vector<std::pair<int,int> > &);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000181
182public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000183 //help functions
Misha Brukman8baa01c2003-04-09 21:51:34 +0000184
Guochun Shif1c154f2003-03-27 17:57:44 +0000185 //get the maxium the delay between two nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000186 SchedGraphEdge *getMaxDelayEdge(unsigned srcId, unsigned sinkId);
Guochun Shif1c154f2003-03-27 17:57:44 +0000187
188 //FIXME:
189 //get the predessor Set of the set
190 NodeVec predSet(NodeVec set, unsigned, unsigned);
191 NodeVec predSet(NodeVec set);
192
193 //get the predessor set of the node
Misha Brukman2c821cc2003-04-10 19:19:23 +0000194 NodeVec predSet(ModuloSchedGraphNode *node, unsigned, unsigned);
195 NodeVec predSet(ModuloSchedGraphNode *node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000196
197 //get the successor set of the set
198 NodeVec succSet(NodeVec set, unsigned, unsigned);
199 NodeVec succSet(NodeVec set);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000200
Guochun Shif1c154f2003-03-27 17:57:44 +0000201 //get the succssor set of the node
Misha Brukman2c821cc2003-04-10 19:19:23 +0000202 NodeVec succSet(ModuloSchedGraphNode *node, unsigned, unsigned);
203 NodeVec succSet(ModuloSchedGraphNode *node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000204
205 //return the uniton of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000206 NodeVec vectorUnion(NodeVec set1, NodeVec set2);
Guochun Shif1c154f2003-03-27 17:57:44 +0000207
208 //return the consjuction of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000209 NodeVec vectorConj(NodeVec set1, NodeVec set2);
210
Guochun Shif1c154f2003-03-27 17:57:44 +0000211 //return all nodes in set1 but not set2
212 NodeVec vectorSub(NodeVec set1, NodeVec set2);
213
Misha Brukman8baa01c2003-04-09 21:51:34 +0000214 typedef hash_map<const Instruction*,ModuloSchedGraphNode*> map_base;
215
Guochun Shif1c154f2003-03-27 17:57:44 +0000216public:
217 using map_base::iterator;
218 using map_base::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000219
Guochun Shif1c154f2003-03-27 17:57:44 +0000220public:
221
222 //get target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000223 const TargetMachine & getTarget() {
224 return target;
225 }
Guochun Shi099b0642003-06-02 17:48:56 +0000226
227 //get the basic block
228 BasicBlock* getBasicBlock() const {
229 return bb;
230 }
231
232
Guochun Shif1c154f2003-03-27 17:57:44 +0000233 //get the iteration interval
Misha Brukman8baa01c2003-04-09 21:51:34 +0000234 const int getMII() {
235 return MII;
236 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000237
238 //get the ordered nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000239 const NodeVec & getONodes() {
240 return oNodes;
241 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000242
243 //get the number of nodes (including the root and leaf)
244 //note: actually root and leaf is not used
Misha Brukman8baa01c2003-04-09 21:51:34 +0000245 const unsigned int getNumNodes() const {
246 return size() + 2;
247 }
Misha Brukman63e04f32003-04-22 23:00:08 +0000248
Guochun Shif1c154f2003-03-27 17:57:44 +0000249 //return wether the BasicBlock 'bb' contains a loop
Misha Brukman63e04f32003-04-22 23:00:08 +0000250 bool isLoop(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000251
Guochun Shif1c154f2003-03-27 17:57:44 +0000252 //return the node for the input instruction
Misha Brukman63e04f32003-04-22 23:00:08 +0000253 ModuloSchedGraphNode *getGraphNodeForInst(const Instruction *inst) const {
Guochun Shif1c154f2003-03-27 17:57:44 +0000254 const_iterator onePair = this->find(inst);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000255 return (onePair != this->end()) ? (*onePair).second : NULL;
Guochun Shif1c154f2003-03-27 17:57:44 +0000256 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000257
258 // Debugging support
259 //dump the graph
260 void dump() const;
261
262 // dump the basicBlock
Misha Brukman63e04f32003-04-22 23:00:08 +0000263 void dump(const BasicBlock *bb);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000264
Guochun Shif1c154f2003-03-27 17:57:44 +0000265 //dump the basicBlock into 'os' stream
Misha Brukman63e04f32003-04-22 23:00:08 +0000266 void dump(const BasicBlock *bb, std::ostream &os);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000267
Guochun Shif1c154f2003-03-27 17:57:44 +0000268 //dump the node property
Misha Brukman8baa01c2003-04-09 21:51:34 +0000269 void dumpNodeProperty() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000270
Misha Brukman8baa01c2003-04-09 21:51:34 +0000271private:
272 friend class ModuloSchedGraphSet; //give access to ctor
273
274public:
Guochun Shi099b0642003-06-02 17:48:56 +0000275 ModuloSchedGraph(BasicBlock * in_bb,
276 const TargetMachine & in_target)
277 :SchedGraphCommon(), bb(in_bb),target(in_target)
Misha Brukman2c821cc2003-04-10 19:19:23 +0000278 {
Guochun Shif1c154f2003-03-27 17:57:44 +0000279 buildGraph(target);
280 }
281
Misha Brukman8baa01c2003-04-09 21:51:34 +0000282 ~ModuloSchedGraph() {
283 for (const_iterator I = begin(); I != end(); ++I)
Guochun Shif1c154f2003-03-27 17:57:44 +0000284 delete I->second;
285 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000286
Misha Brukman2c821cc2003-04-10 19:19:23 +0000287 // Unorder iterators
288 // return values are pair<const Instruction*, ModuloSchedGraphNode*>
Guochun Shif1c154f2003-03-27 17:57:44 +0000289 using map_base::begin;
290 using map_base::end;
291
Guochun Shi8f1d4ab2003-06-08 23:16:07 +0000292 void addHash(const Instruction *inst,
293 ModuloSchedGraphNode *node){
294
Misha Brukman8baa01c2003-04-09 21:51:34 +0000295 assert((*this)[inst] == NULL);
296 (*this)[inst] = node;
Guochun Shi8f1d4ab2003-06-08 23:16:07 +0000297
Misha Brukman8baa01c2003-04-09 21:51:34 +0000298 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000299
Misha Brukman63e04f32003-04-22 23:00:08 +0000300 // Graph builder
Misha Brukman8baa01c2003-04-09 21:51:34 +0000301 ModuloSchedGraphNode *getNode(const unsigned nodeId) const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000302
Misha Brukman63e04f32003-04-22 23:00:08 +0000303 // Build the graph from the basicBlock
304 void buildGraph(const TargetMachine &target);
Guochun Shif1c154f2003-03-27 17:57:44 +0000305
Misha Brukman63e04f32003-04-22 23:00:08 +0000306 // Build nodes for BasicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000307 void buildNodesforBB(const TargetMachine &target,
Guochun Shi8f1d4ab2003-06-08 23:16:07 +0000308 const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000309
310 //find definitiona and use information for all nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000311 void findDefUseInfoAtInstr(const TargetMachine &target,
312 ModuloSchedGraphNode *node,
313 NodeVec &memNode,
314 RegToRefVecMap &regToRefVecMap,
315 ValueToDefVecMap &valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000316
317 //add def-use edge
Misha Brukman8baa01c2003-04-09 21:51:34 +0000318 void addDefUseEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000319
320 //add control dependence edges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000321 void addCDEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000322
323 //add memory dependence dges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000324 void addMemEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000325
Guochun Shif1c154f2003-03-27 17:57:44 +0000326 //computer source restrictoin II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000327 int computeResII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000328
329 //computer recurrence II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000330 int computeRecII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000331};
332
Misha Brukman8baa01c2003-04-09 21:51:34 +0000333//==================================-
334// Graph set
Guochun Shif1c154f2003-03-27 17:57:44 +0000335
Misha Brukman8baa01c2003-04-09 21:51:34 +0000336class ModuloSchedGraphSet : public std::vector<ModuloSchedGraph*> {
Guochun Shif1c154f2003-03-27 17:57:44 +0000337private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000338 const Function *method;
339
Guochun Shif1c154f2003-03-27 17:57:44 +0000340public:
341 typedef std::vector<ModuloSchedGraph*> baseVector;
342 using baseVector::iterator;
343 using baseVector::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000344
Guochun Shif1c154f2003-03-27 17:57:44 +0000345public:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000346 ModuloSchedGraphSet(const Function *function, const TargetMachine &target);
347 ~ModuloSchedGraphSet();
348
349 // Iterators
Guochun Shif1c154f2003-03-27 17:57:44 +0000350 using baseVector::begin;
351 using baseVector::end;
352
Misha Brukman2c821cc2003-04-10 19:19:23 +0000353 // Debugging support
354 void dump() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000355
Guochun Shif1c154f2003-03-27 17:57:44 +0000356private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000357 void addGraph(ModuloSchedGraph *graph) {
358 assert(graph != NULL);
Guochun Shif1c154f2003-03-27 17:57:44 +0000359 this->push_back(graph);
360 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000361
Misha Brukman8baa01c2003-04-09 21:51:34 +0000362 // Graph builder
363 void buildGraphsForMethod(const Function *F,
364 const TargetMachine &target);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000365};
Misha Brukman8baa01c2003-04-09 21:51:34 +0000366
367#endif