blob: 0947d32cb31d64368ac56236c2bb1ccbf3cdc7d2 [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"
16#include "../InstrSched/SchedGraphCommon.h"
Guochun Shif1c154f2003-03-27 17:57:44 +000017#include <iostream>
Guochun Shif1c154f2003-03-27 17:57:44 +000018
19//for debug information selecton
Misha Brukman8baa01c2003-04-09 21:51:34 +000020enum ModuloSchedDebugLevel_t {
Guochun Shif1c154f2003-03-27 17:57:44 +000021 ModuloSched_NoDebugInfo,
22 ModuloSched_Disable,
23 ModuloSched_PrintSchedule,
24 ModuloSched_PrintScheduleProcess,
25};
26
Misha Brukman8baa01c2003-04-09 21:51:34 +000027//===----------------------------------------------------------------------===//
28// ModuloSchedGraphNode - Implement a data structure based on the
29// SchedGraphNodeCommon this class stores informtion needed later to order the
30// nodes in modulo scheduling
31//
32class ModuloSchedGraphNode:public SchedGraphNodeCommon {
Guochun Shif1c154f2003-03-27 17:57:44 +000033private:
Misha Brukman8baa01c2003-04-09 21:51:34 +000034 // the corresponding instruction
35 const Instruction *inst;
Guochun Shif1c154f2003-03-27 17:57:44 +000036
Misha Brukman8baa01c2003-04-09 21:51:34 +000037 // whether this node's property(ASAP,ALAP, ...) has been computed
Guochun Shif1c154f2003-03-27 17:57:44 +000038 bool propertyComputed;
39
Misha Brukman8baa01c2003-04-09 21:51:34 +000040 // ASAP: the earliest time the node could be scheduled
41 // ALAP: the latest time the node couldbe scheduled
42 // depth: the depth of the node
43 // height: the height of the node
44 // mov: the mobility function, computed as ALAP - ASAP
45 // scheTime: the scheduled time if this node has been scheduled
46 // earlyStart: the earliest time to be tried to schedule the node
47 // lateStart: the latest time to be tried to schedule the node
Guochun Shif1c154f2003-03-27 17:57:44 +000048 int ASAP, ALAP, depth, height, mov;
49 int schTime;
Misha Brukman8baa01c2003-04-09 21:51:34 +000050 int earlyStart, lateStart;
Guochun Shif1c154f2003-03-27 17:57:44 +000051
52public:
Misha Brukman8baa01c2003-04-09 21:51:34 +000053
Guochun Shif1c154f2003-03-27 17:57:44 +000054 //get the instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +000055 const Instruction *getInst() const {
56 return inst;
57 }
Guochun Shif1c154f2003-03-27 17:57:44 +000058 //get the instruction op-code name
Misha Brukman8baa01c2003-04-09 21:51:34 +000059 const char *getInstOpcodeName() const {
60 return inst->getOpcodeName();
61 }
62 //get the instruction op-code
63 const unsigned getInstOpcode() const {
64 return inst->getOpcode();
65 }
Guochun Shif1c154f2003-03-27 17:57:44 +000066 //return whether the node is NULL
Misha Brukman8baa01c2003-04-09 21:51:34 +000067 bool isNullNode() const {
68 return (inst == NULL);
69 }
Guochun Shif1c154f2003-03-27 17:57:44 +000070 //return whether the property of the node has been computed
Misha Brukman8baa01c2003-04-09 21:51:34 +000071 bool getPropertyComputed() {
72 return propertyComputed;
73 }
Guochun Shif1c154f2003-03-27 17:57:44 +000074 //set the propertyComputed
Misha Brukman8baa01c2003-04-09 21:51:34 +000075 void setPropertyComputed(bool _propertyComputed) {
76 propertyComputed = _propertyComputed;
77 }
78
Guochun Shif1c154f2003-03-27 17:57:44 +000079 //get the corresponding property
Misha Brukman8baa01c2003-04-09 21:51:34 +000080 int getASAP() {
81 return ASAP;
82 }
83 int getALAP() {
84 return ALAP;
85 }
86 int getMov() {
87 return mov;
88 }
89 int getDepth() {
90 return depth;
91 }
92 int getHeight() {
93 return height;
94 }
95 int getSchTime() {
96 return schTime;
97 }
98 int getEarlyStart() {
99 return earlyStart;
100 }
101 int getLateStart() {
102 return lateStart;
103 }
104 void setEarlyStart(int _earlyStart) {
105 earlyStart = _earlyStart;
106 }
107 void setLateStart(int _lateStart) {
108 lateStart = _lateStart;
109 }
110 void setSchTime(int _time) {
111 schTime = _time;
112 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000113
Misha Brukman8baa01c2003-04-09 21:51:34 +0000114private:
Guochun Shif1c154f2003-03-27 17:57:44 +0000115 friend class ModuloSchedGraph;
116 friend class SchedGraphNode;
117
118 //constructor:
119 //nodeId: the node id, unique within the each BasicBlock
120 //_bb: which BasicBlock the corresponding instruction belongs to
121 //_inst: the corresponding instruction
122 //indexInBB: the corresponding instruction's index in the BasicBlock
123 //target: the targetMachine
124 ModuloSchedGraphNode(unsigned int _nodeId,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000125 const BasicBlock * _bb,
126 const Instruction * _inst,
127 int indexInBB, const TargetMachine &target);
128
129
130 friend std::ostream & operator<<(std::ostream & os,
131 const ModuloSchedGraphNode & edge);
132
Guochun Shif1c154f2003-03-27 17:57:44 +0000133};
134
Guochun Shif1c154f2003-03-27 17:57:44 +0000135//FIXME: these two value should not be used
136#define MAXNODE 100
137#define MAXCC 100
138
Guochun Shif1c154f2003-03-27 17:57:44 +0000139//===----------------------------------------------------------------------===//
140/// ModuloSchedGraph- the data structure to store dependence between nodes
141/// it catches data dependence and constrol dependence
142///
Misha Brukman8baa01c2003-04-09 21:51:34 +0000143class ModuloSchedGraph :
Guochun Shif1c154f2003-03-27 17:57:44 +0000144 public SchedGraphCommon,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000145 protected hash_map<const Instruction*,ModuloSchedGraphNode*> {
146
147private:
Guochun Shif1c154f2003-03-27 17:57:44 +0000148 //iteration Interval
149 int MII;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000150
Guochun Shif1c154f2003-03-27 17:57:44 +0000151 //target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000152 const TargetMachine & target;
Guochun Shif1c154f2003-03-27 17:57:44 +0000153
154 //the circuits in the dependence graph
155 unsigned circuits[MAXCC][MAXNODE];
156
157 //the order nodes
158 std::vector<ModuloSchedGraphNode*> oNodes;
159
160 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
161
162 //the function to compute properties
Misha Brukman8baa01c2003-04-09 21:51:34 +0000163 void computeNodeASAP(const BasicBlock * bb);
164 void computeNodeALAP(const BasicBlock * bb);
165 void computeNodeMov(const BasicBlock * bb);
166 void computeNodeDepth(const BasicBlock * bb);
167 void computeNodeHeight(const BasicBlock * bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000168
169 //the function to compute node property
Misha Brukman8baa01c2003-04-09 21:51:34 +0000170 void computeNodeProperty(const BasicBlock * bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000171
172 //the function to sort nodes
173 void orderNodes();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000174
Guochun Shif1c154f2003-03-27 17:57:44 +0000175 //add the resource usage
Misha Brukman8baa01c2003-04-09 21:51:34 +0000176void addResourceUsage(std::vector<pair<int,int>>&, int);
Guochun Shif1c154f2003-03-27 17:57:44 +0000177
178 //debug functions:
179 //dump circuits
180 void dumpCircuits();
181 //dump the input set of nodes
182 void dumpSet(std::vector<ModuloSchedGraphNode*> set);
183 //dump the input resource usage table
Misha Brukman8baa01c2003-04-09 21:51:34 +0000184 void dumpResourceUsage(std::vector<pair<int,int>> &);
185
186public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000187 //help functions
Misha Brukman8baa01c2003-04-09 21:51:34 +0000188
Guochun Shif1c154f2003-03-27 17:57:44 +0000189 //get the maxium the delay between two nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000190 SchedGraphEdge *getMaxDelayEdge(unsigned srcId, unsigned sinkId);
Guochun Shif1c154f2003-03-27 17:57:44 +0000191
192 //FIXME:
193 //get the predessor Set of the set
194 NodeVec predSet(NodeVec set, unsigned, unsigned);
195 NodeVec predSet(NodeVec set);
196
197 //get the predessor set of the node
Misha Brukman8baa01c2003-04-09 21:51:34 +0000198 NodeVec predSet(ModuloSchedGraphNode * node, unsigned, unsigned);
199 NodeVec predSet(ModuloSchedGraphNode * node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000200
201 //get the successor set of the set
202 NodeVec succSet(NodeVec set, unsigned, unsigned);
203 NodeVec succSet(NodeVec set);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000204
Guochun Shif1c154f2003-03-27 17:57:44 +0000205 //get the succssor set of the node
Misha Brukman8baa01c2003-04-09 21:51:34 +0000206 NodeVec succSet(ModuloSchedGraphNode * node, unsigned, unsigned);
207 NodeVec succSet(ModuloSchedGraphNode * node);
Guochun Shif1c154f2003-03-27 17:57:44 +0000208
209 //return the uniton of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000210 NodeVec vectorUnion(NodeVec set1, NodeVec set2);
Guochun Shif1c154f2003-03-27 17:57:44 +0000211
212 //return the consjuction of the two vectors
Misha Brukman8baa01c2003-04-09 21:51:34 +0000213 NodeVec vectorConj(NodeVec set1, NodeVec set2);
214
Guochun Shif1c154f2003-03-27 17:57:44 +0000215 //return all nodes in set1 but not set2
216 NodeVec vectorSub(NodeVec set1, NodeVec set2);
217
Misha Brukman8baa01c2003-04-09 21:51:34 +0000218 typedef hash_map<const Instruction*,ModuloSchedGraphNode*> map_base;
219
Guochun Shif1c154f2003-03-27 17:57:44 +0000220public:
221 using map_base::iterator;
222 using map_base::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000223
Guochun Shif1c154f2003-03-27 17:57:44 +0000224public:
225
226 //get target machine
Misha Brukman8baa01c2003-04-09 21:51:34 +0000227 const TargetMachine & getTarget() {
228 return target;
229 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000230 //get the iteration interval
Misha Brukman8baa01c2003-04-09 21:51:34 +0000231 const int getMII() {
232 return MII;
233 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000234
235 //get the ordered nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000236 const NodeVec & getONodes() {
237 return oNodes;
238 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000239
240 //get the number of nodes (including the root and leaf)
241 //note: actually root and leaf is not used
Misha Brukman8baa01c2003-04-09 21:51:34 +0000242 const unsigned int getNumNodes() const {
243 return size() + 2;
244 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000245 //return wether the BasicBlock 'bb' contains a loop
Misha Brukman8baa01c2003-04-09 21:51:34 +0000246 bool isLoop(const BasicBlock * bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000247
248 //return this basibBlock contains a loop
Misha Brukman8baa01c2003-04-09 21:51:34 +0000249 bool isLoop();
250
Guochun Shif1c154f2003-03-27 17:57:44 +0000251
252 //return the node for the input instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +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 Brukman8baa01c2003-04-09 21:51:34 +0000257 //Debugging support//dump the graph void dump() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000258 //dump the basicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000259 void dump(const BasicBlock * bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000260 //dump the basicBlock into 'os' stream
Misha Brukman8baa01c2003-04-09 21:51:34 +0000261 void dump(const BasicBlock * bb, std::ostream & os);
Guochun Shif1c154f2003-03-27 17:57:44 +0000262 //dump the node property
Misha Brukman8baa01c2003-04-09 21:51:34 +0000263 void dumpNodeProperty() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000264
Misha Brukman8baa01c2003-04-09 21:51:34 +0000265private:
266 friend class ModuloSchedGraphSet; //give access to ctor
267
268public:
269 ModuloSchedGraph(const BasicBlock *bb, const TargetMachine &_target)
270 :SchedGraphCommon(bb), target(_target) {
Guochun Shif1c154f2003-03-27 17:57:44 +0000271 buildGraph(target);
272 }
273
Misha Brukman8baa01c2003-04-09 21:51:34 +0000274 ~ModuloSchedGraph() {
275 for (const_iterator I = begin(); I != end(); ++I)
Guochun Shif1c154f2003-03-27 17:57:44 +0000276 delete I->second;
277 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000278
Guochun Shif1c154f2003-03-27 17:57:44 +0000279 //unorder iterators
280 //return values are pair<const Instruction*, ModuloSchedGraphNode*>
281 using map_base::begin;
282 using map_base::end;
283
Misha Brukman8baa01c2003-04-09 21:51:34 +0000284 void noteModuloSchedGraphNodeForInst(const Instruction *inst,
285 ModuloSchedGraphNode *node)
286 {
287 assert((*this)[inst] == NULL);
288 (*this)[inst] = node;
289 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000290
Guochun Shif1c154f2003-03-27 17:57:44 +0000291 //Graph builder
Misha Brukman8baa01c2003-04-09 21:51:34 +0000292
293 ModuloSchedGraphNode *getNode(const unsigned nodeId) const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000294
295 //build the graph from the basicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000296 void buildGraph(const TargetMachine & target);
Guochun Shif1c154f2003-03-27 17:57:44 +0000297
298 //Build nodes for BasicBlock
Misha Brukman8baa01c2003-04-09 21:51:34 +0000299 void buildNodesforBB(const TargetMachine &target,
300 const BasicBlock *bb,
301 NodeVec &memNode,
302 RegToRefVecMap &regToRefVecMap,
303 ValueToDefVecMap &valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000304
305 //find definitiona and use information for all nodes
Misha Brukman8baa01c2003-04-09 21:51:34 +0000306 void findDefUseInfoAtInstr(const TargetMachine &target,
307 ModuloSchedGraphNode *node,
308 NodeVec &memNode,
309 RegToRefVecMap &regToRefVecMap,
310 ValueToDefVecMap &valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000311
312 //add def-use edge
Misha Brukman8baa01c2003-04-09 21:51:34 +0000313 void addDefUseEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000314
315 //add control dependence edges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000316 void addCDEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000317
318 //add memory dependence dges
Misha Brukman8baa01c2003-04-09 21:51:34 +0000319 void addMemEdges(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000320
321 //add dummy edges
322 void addDummyEdges();
323
324 //computer source restrictoin II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000325 int computeResII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000326
327 //computer recurrence II
Misha Brukman8baa01c2003-04-09 21:51:34 +0000328 int computeRecII(const BasicBlock *bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000329};
330
Misha Brukman8baa01c2003-04-09 21:51:34 +0000331//==================================-
332// Graph set
Guochun Shif1c154f2003-03-27 17:57:44 +0000333
Misha Brukman8baa01c2003-04-09 21:51:34 +0000334class ModuloSchedGraphSet : public std::vector<ModuloSchedGraph*> {
Guochun Shif1c154f2003-03-27 17:57:44 +0000335private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000336 const Function *method;
337
Guochun Shif1c154f2003-03-27 17:57:44 +0000338public:
339 typedef std::vector<ModuloSchedGraph*> baseVector;
340 using baseVector::iterator;
341 using baseVector::const_iterator;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000342
Guochun Shif1c154f2003-03-27 17:57:44 +0000343public:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000344 ModuloSchedGraphSet(const Function *function, const TargetMachine &target);
345 ~ModuloSchedGraphSet();
346
347 // Iterators
Guochun Shif1c154f2003-03-27 17:57:44 +0000348 using baseVector::begin;
349 using baseVector::end;
350
Misha Brukman8baa01c2003-04-09 21:51:34 +0000351// Debugging support
352void dump() const;
Guochun Shif1c154f2003-03-27 17:57:44 +0000353
Guochun Shif1c154f2003-03-27 17:57:44 +0000354private:
Misha Brukman8baa01c2003-04-09 21:51:34 +0000355 void addGraph(ModuloSchedGraph *graph) {
356 assert(graph != NULL);
Guochun Shif1c154f2003-03-27 17:57:44 +0000357 this->push_back(graph);
358 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000359
Misha Brukman8baa01c2003-04-09 21:51:34 +0000360 // Graph builder
361 void buildGraphsForMethod(const Function *F,
362 const TargetMachine &target);
363}
364
365#endif