Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1 | //===- ModuloSchedGraph.h - Represent a collection of data structures ----*- C++ -*-===// |
| 2 | // |
| 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 |
| 10 | |
| 11 | #include "Support/HashExtras.h" |
| 12 | #include "Support/GraphTraits.h" |
| 13 | #include "../InstrSched/SchedGraphCommon.h" |
| 14 | #include "llvm/Instruction.h" |
| 15 | #include "llvm/Target/TargetMachine.h" |
Guochun Shi | 6fbe5fb | 2003-04-06 23:56:19 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetInstrInfo.h" |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 17 | #include <iostream> |
| 18 | using std::pair; |
| 19 | |
| 20 | //for debug information selecton |
| 21 | enum ModuloSchedDebugLevel_t{ |
| 22 | ModuloSched_NoDebugInfo, |
| 23 | ModuloSched_Disable, |
| 24 | ModuloSched_PrintSchedule, |
| 25 | ModuloSched_PrintScheduleProcess, |
| 26 | }; |
| 27 | |
| 28 | //===============------------------------------------------------------------------------ |
| 29 | ///ModuloSchedGraphNode - Implement a data structure based on the SchedGraphNodeCommon |
| 30 | ///this class stores informtion needed later to order the nodes in modulo scheduling |
| 31 | /// |
| 32 | class ModuloSchedGraphNode: public SchedGraphNodeCommon { |
| 33 | private: |
| 34 | //the corresponding instruction |
| 35 | const Instruction* inst; |
| 36 | |
| 37 | //whether this node's property(ASAP,ALAP, ...) has been computed |
| 38 | bool propertyComputed; |
| 39 | |
| 40 | //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 |
| 48 | int ASAP, ALAP, depth, height, mov; |
| 49 | int schTime; |
| 50 | int earlyStart,lateStart; |
| 51 | |
| 52 | public: |
| 53 | |
| 54 | //get the instruction |
| 55 | const Instruction* getInst() const { return inst;} |
| 56 | |
| 57 | //get the instruction op-code name |
| 58 | const char* getInstOpcodeName() const{ return inst->getOpcodeName();} |
| 59 | |
| 60 | //get the instruction op-code |
| 61 | const unsigned getInstOpcode() const { return inst->getOpcode();} |
| 62 | |
| 63 | //return whether the node is NULL |
| 64 | bool isNullNode() const{ return(inst== NULL);} |
| 65 | |
| 66 | //return whether the property of the node has been computed |
| 67 | bool getPropertyComputed() {return propertyComputed;} |
| 68 | |
| 69 | //set the propertyComputed |
| 70 | void setPropertyComputed(bool _propertyComputed) {propertyComputed = _propertyComputed;} |
| 71 | |
| 72 | //get the corresponding property |
| 73 | int getASAP(){ return ASAP;} |
| 74 | int getALAP(){ return ALAP;} |
| 75 | int getMov() { return mov;} |
| 76 | int getDepth(){return depth;} |
| 77 | int getHeight(){return height;} |
| 78 | int getSchTime(){return schTime;} |
| 79 | int getEarlyStart(){return earlyStart;} |
| 80 | int getLateStart() { return lateStart;} |
| 81 | void setEarlyStart(int _earlyStart) {earlyStart= _earlyStart;} |
| 82 | void setLateStart(int _lateStart) {lateStart= _lateStart;} |
| 83 | void setSchTime(int _time){schTime=_time;} |
| 84 | |
| 85 | private: |
| 86 | friend class ModuloSchedGraph; |
| 87 | friend class SchedGraphNode; |
| 88 | |
| 89 | //constructor: |
| 90 | //nodeId: the node id, unique within the each BasicBlock |
| 91 | //_bb: which BasicBlock the corresponding instruction belongs to |
| 92 | //_inst: the corresponding instruction |
| 93 | //indexInBB: the corresponding instruction's index in the BasicBlock |
| 94 | //target: the targetMachine |
| 95 | ModuloSchedGraphNode(unsigned int _nodeId, |
| 96 | const BasicBlock* _bb, |
| 97 | const Instruction* _inst, |
| 98 | int indexInBB, |
| 99 | const TargetMachine& target); |
| 100 | |
| 101 | |
| 102 | friend std::ostream& operator<<(std::ostream& os,const ModuloSchedGraphNode& edge); |
| 103 | |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | //FIXME: these two value should not be used |
| 111 | #define MAXNODE 100 |
| 112 | #define MAXCC 100 |
| 113 | |
| 114 | |
| 115 | |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | /// ModuloSchedGraph- the data structure to store dependence between nodes |
| 118 | /// it catches data dependence and constrol dependence |
| 119 | /// |
| 120 | /// |
| 121 | |
| 122 | class ModuloSchedGraph: |
| 123 | public SchedGraphCommon, |
| 124 | protected hash_map<const Instruction*, ModuloSchedGraphNode*> |
| 125 | { |
| 126 | |
| 127 | private: |
| 128 | //iteration Interval |
| 129 | int MII; |
| 130 | |
| 131 | //target machine |
| 132 | const TargetMachine& target; |
| 133 | |
| 134 | //the circuits in the dependence graph |
| 135 | unsigned circuits[MAXCC][MAXNODE]; |
| 136 | |
| 137 | //the order nodes |
| 138 | std::vector<ModuloSchedGraphNode*> oNodes; |
| 139 | |
| 140 | typedef std::vector<ModuloSchedGraphNode*> NodeVec; |
| 141 | |
| 142 | //the function to compute properties |
| 143 | void computeNodeASAP(const BasicBlock* bb); |
| 144 | void computeNodeALAP(const BasicBlock* bb); |
| 145 | void computeNodeMov(const BasicBlock* bb); |
| 146 | void computeNodeDepth(const BasicBlock* bb); |
| 147 | void computeNodeHeight(const BasicBlock* bb); |
| 148 | |
| 149 | //the function to compute node property |
| 150 | void computeNodeProperty(const BasicBlock* bb); |
| 151 | |
| 152 | //the function to sort nodes |
| 153 | void orderNodes(); |
| 154 | |
| 155 | //add the resource usage |
| 156 | void addResourceUsage(std::vector<pair<int,int> >&, int); |
| 157 | |
| 158 | //debug functions: |
| 159 | //dump circuits |
| 160 | void dumpCircuits(); |
| 161 | //dump the input set of nodes |
| 162 | void dumpSet(std::vector<ModuloSchedGraphNode*> set); |
| 163 | //dump the input resource usage table |
| 164 | void dumpResourceUsage(std::vector<pair<int,int> >&); |
| 165 | |
| 166 | public: |
| 167 | //help functions |
| 168 | |
| 169 | //get the maxium the delay between two nodes |
| 170 | SchedGraphEdge* getMaxDelayEdge(unsigned srcId, unsigned sinkId); |
| 171 | |
| 172 | //FIXME: |
| 173 | //get the predessor Set of the set |
| 174 | NodeVec predSet(NodeVec set, unsigned, unsigned); |
| 175 | NodeVec predSet(NodeVec set); |
| 176 | |
| 177 | //get the predessor set of the node |
| 178 | NodeVec predSet(ModuloSchedGraphNode* node, unsigned,unsigned); |
| 179 | NodeVec predSet(ModuloSchedGraphNode* node); |
| 180 | |
| 181 | //get the successor set of the set |
| 182 | NodeVec succSet(NodeVec set, unsigned, unsigned); |
| 183 | NodeVec succSet(NodeVec set); |
| 184 | |
| 185 | //get the succssor set of the node |
| 186 | NodeVec succSet(ModuloSchedGraphNode* node,unsigned, unsigned); |
| 187 | NodeVec succSet(ModuloSchedGraphNode* node); |
| 188 | |
| 189 | //return the uniton of the two vectors |
| 190 | NodeVec vectorUnion(NodeVec set1,NodeVec set2 ); |
| 191 | |
| 192 | //return the consjuction of the two vectors |
| 193 | NodeVec vectorConj(NodeVec set1,NodeVec set2 ); |
| 194 | |
| 195 | //return all nodes in set1 but not set2 |
| 196 | NodeVec vectorSub(NodeVec set1, NodeVec set2); |
| 197 | |
| 198 | typedef hash_map<const Instruction*, ModuloSchedGraphNode*> map_base; |
| 199 | public: |
| 200 | using map_base::iterator; |
| 201 | using map_base::const_iterator; |
| 202 | |
| 203 | public: |
| 204 | |
| 205 | //get target machine |
| 206 | const TargetMachine& getTarget(){return target;} |
| 207 | |
| 208 | //get the iteration interval |
| 209 | const int getMII(){return MII;} |
| 210 | |
| 211 | //get the ordered nodes |
| 212 | const NodeVec& getONodes(){return oNodes;} |
| 213 | |
| 214 | //get the number of nodes (including the root and leaf) |
| 215 | //note: actually root and leaf is not used |
| 216 | const unsigned int getNumNodes() const {return size()+2;} |
| 217 | |
| 218 | //return wether the BasicBlock 'bb' contains a loop |
| 219 | bool isLoop (const BasicBlock* bb); |
| 220 | |
| 221 | //return this basibBlock contains a loop |
| 222 | bool isLoop (); |
| 223 | |
| 224 | |
| 225 | //return the node for the input instruction |
| 226 | ModuloSchedGraphNode* getGraphNodeForInst(const Instruction* inst) const{ |
| 227 | const_iterator onePair = this->find(inst); |
| 228 | return (onePair != this->end())?(*onePair).second: NULL; |
| 229 | } |
| 230 | |
| 231 | //Debugging support |
| 232 | //dump the graph |
| 233 | void dump() const; |
| 234 | //dump the basicBlock |
| 235 | void dump(const BasicBlock* bb); |
| 236 | //dump the basicBlock into 'os' stream |
| 237 | void dump(const BasicBlock* bb, std::ostream& os); |
| 238 | //dump the node property |
| 239 | void dumpNodeProperty() const ; |
| 240 | |
| 241 | private: |
| 242 | friend class ModuloSchedGraphSet; //give access to ctor |
| 243 | |
| 244 | public: |
| 245 | /*ctr*/ |
| 246 | ModuloSchedGraph(const BasicBlock* bb, const TargetMachine& _target) |
| 247 | :SchedGraphCommon(bb), target(_target){ |
| 248 | buildGraph(target); |
| 249 | } |
| 250 | |
| 251 | /*dtr*/ |
| 252 | ~ModuloSchedGraph(){ |
| 253 | for(const_iterator I=begin(); I!=end(); ++I) |
| 254 | delete I->second; |
| 255 | } |
| 256 | |
| 257 | //unorder iterators |
| 258 | //return values are pair<const Instruction*, ModuloSchedGraphNode*> |
| 259 | using map_base::begin; |
| 260 | using map_base::end; |
| 261 | |
| 262 | |
| 263 | |
| 264 | inline void noteModuloSchedGraphNodeForInst(const Instruction* inst, |
| 265 | ModuloSchedGraphNode* node) |
| 266 | { |
| 267 | assert((*this)[inst] ==NULL); |
| 268 | (*this)[inst]=node; |
| 269 | } |
| 270 | |
| 271 | //Graph builder |
| 272 | |
| 273 | ModuloSchedGraphNode* getNode (const unsigned nodeId) const; |
| 274 | |
| 275 | //build the graph from the basicBlock |
| 276 | void buildGraph (const TargetMachine& target); |
| 277 | |
| 278 | //Build nodes for BasicBlock |
| 279 | void buildNodesforBB (const TargetMachine& target, |
| 280 | const BasicBlock* bb, |
| 281 | NodeVec& memNode, |
| 282 | RegToRefVecMap& regToRefVecMap, |
| 283 | ValueToDefVecMap& valueToDefVecMap); |
| 284 | |
| 285 | //find definitiona and use information for all nodes |
| 286 | void findDefUseInfoAtInstr (const TargetMachine& target, |
| 287 | ModuloSchedGraphNode* node, |
| 288 | NodeVec& memNode, |
| 289 | RegToRefVecMap& regToRefVecMap, |
| 290 | ValueToDefVecMap& valueToDefVecMap); |
| 291 | |
| 292 | //add def-use edge |
| 293 | void addDefUseEdges (const BasicBlock* bb); |
| 294 | |
| 295 | //add control dependence edges |
| 296 | void addCDEdges (const BasicBlock* bb); |
| 297 | |
| 298 | //add memory dependence dges |
| 299 | void addMemEdges (const BasicBlock* bb); |
| 300 | |
| 301 | //add dummy edges |
| 302 | void addDummyEdges(); |
| 303 | |
| 304 | //computer source restrictoin II |
| 305 | int computeResII (const BasicBlock* bb); |
| 306 | |
| 307 | //computer recurrence II |
| 308 | int computeRecII (const BasicBlock* bb); |
| 309 | }; |
| 310 | |
| 311 | ///==================================- |
| 312 | //gragh set |
| 313 | |
| 314 | class ModuloSchedGraphSet: |
| 315 | public std::vector<ModuloSchedGraph*> |
| 316 | { |
| 317 | private: |
| 318 | const Function* method; |
| 319 | |
| 320 | public: |
| 321 | typedef std::vector<ModuloSchedGraph*> baseVector; |
| 322 | using baseVector::iterator; |
| 323 | using baseVector::const_iterator; |
| 324 | |
| 325 | public: |
| 326 | /*ctor*/ ModuloSchedGraphSet (const Function* function, const TargetMachine& target); |
| 327 | |
| 328 | /*dtor*/ ~ModuloSchedGraphSet (); |
| 329 | |
| 330 | //iterators |
| 331 | using baseVector::begin; |
| 332 | using baseVector::end; |
| 333 | |
| 334 | |
| 335 | //Debugging support |
| 336 | void dump() const; |
| 337 | |
| 338 | private: |
| 339 | inline void addGraph(ModuloSchedGraph* graph){ |
| 340 | assert(graph !=NULL); |
| 341 | this->push_back(graph); |
| 342 | } |
| 343 | |
| 344 | //Graph builder |
| 345 | void buildGraphsForMethod (const Function *F, const TargetMachine& target); |
| 346 | }; |
| 347 | |
| 348 | #endif |