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