Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1 | // ModuloScheduling.h -------------------------------------------*- C++ -*-===// |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 3 | // This header defines the the classes ModuloScheduling and |
| 4 | // ModuloSchedulingSet's structure |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | |
| 9 | #ifndef LLVM_CODEGEN_MODULOSCHEDULING_H |
| 10 | #define LLVM_CODEGEN_MODULOSCHEDULING_H |
| 11 | |
| 12 | #include "ModuloSchedGraph.h" |
| 13 | #include <iostream> |
Guochun Shi | 6fbe5fb | 2003-04-06 23:56:19 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame] | 16 | #define DEBUG_PRINT(x) x |
| 17 | |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 18 | // for debug information selecton |
| 19 | enum ModuloSchedDebugLevel_t { |
| 20 | ModuloSchedDebugLevel_NoDebugInfo, |
| 21 | ModuloSchedDebugLevel_PrintSchedule, |
| 22 | ModuloSchedDebugLevel_PrintScheduleProcess, |
| 23 | }; |
| 24 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 25 | class ModuloScheduling: NonCopyable { |
| 26 | private: |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 27 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 28 | typedef std::vector<ModuloSchedGraphNode*> NodeVec; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 29 | typedef std::vector<std::vector<unsigned> > Resources; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 30 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 31 | // The graph to feed in |
| 32 | ModuloSchedGraph &graph; |
| 33 | const TargetMachine ⌖ |
| 34 | |
| 35 | // The BasicBlock to be scheduled |
| 36 | BasicBlock *bb; |
| 37 | |
| 38 | // Iteration Interval |
| 39 | // FIXME: II may be a better name for its meaning |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 40 | unsigned II; |
| 41 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 42 | // The vector containing the nodes which have been scheduled |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 43 | NodeVec nodeScheduled; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 44 | |
| 45 | // The remaining unscheduled nodes |
| 46 | const NodeVec &oNodes; |
| 47 | |
| 48 | // The machine resource table |
| 49 | std::vector<std::vector<std::pair<int,int> > > resourceTable; |
| 50 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 51 | ///the schedule( with many schedule stage) |
| 52 | std::vector<std::vector<ModuloSchedGraphNode*> > schedule; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 53 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 54 | ///the kernel(core) schedule(length = II) |
| 55 | std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule; |
| 56 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 57 | typedef BasicBlock::InstListType InstListType; |
| 58 | typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 59 | |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | |
| 63 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 64 | public: |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 65 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 66 | ModuloScheduling(ModuloSchedGraph & _graph): |
| 67 | graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes()) |
| 68 | { |
| 69 | II = graph.getMII(); |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 70 | bb = graph.getBasicBlock(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 71 | instrScheduling(); |
| 72 | }; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 73 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 74 | ~ModuloScheduling() {}; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 75 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 76 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 77 | |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 78 | static bool |
| 79 | printSchedule() { |
| 80 | |
| 81 | //return ModuloScheduling::DebugLevel >= DebugLevel_PrintSchedule; |
Guochun Shi | 8f1d4ab | 2003-06-08 23:16:07 +0000 | [diff] [blame] | 82 | return true; |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 83 | |
Guochun Shi | 8f1d4ab | 2003-06-08 23:16:07 +0000 | [diff] [blame] | 84 | |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 85 | } |
| 86 | static bool |
| 87 | printScheduleProcess() { |
| 88 | |
| 89 | //return DebugLevel >= DebugLevel_PrintScheduleProcess; |
Guochun Shi | 8f1d4ab | 2003-06-08 23:16:07 +0000 | [diff] [blame] | 90 | return true; |
Guochun Shi | 139f0c2 | 2003-05-30 00:17:09 +0000 | [diff] [blame] | 91 | |
| 92 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // The method to compute schedule and instert epilogue and prologue |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 96 | void instrScheduling(); |
| 97 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 98 | // Debug functions: |
| 99 | // Dump the schedule and core schedule |
| 100 | void dumpScheduling(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 101 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 102 | // Dump the input vector of nodes |
| 103 | // sch: the input vector of nodes |
| 104 | void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 105 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 106 | // Dump the resource usage table |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 107 | void dumpResourceUsageTable(); |
| 108 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 109 | //*******************internal functions******************************* |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 110 | private: |
| 111 | //clear memory from the last round and initialize if necessary |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 112 | void clearInitMem(const TargetSchedInfo&); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 113 | |
| 114 | //compute schedule and coreSchedule with the current II |
| 115 | bool computeSchedule(); |
| 116 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 117 | BasicBlock *getSuccBB(BasicBlock *); |
| 118 | BasicBlock *getPredBB(BasicBlock *); |
| 119 | void constructPrologue(BasicBlock *prologue); |
| 120 | void constructKernel(BasicBlock *prologue, |
| 121 | BasicBlock *kernel, |
| 122 | BasicBlock *epilogue); |
| 123 | void constructEpilogue(BasicBlock *epilogue, BasicBlock *succ_bb); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 124 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 125 | // update the resource table at the startCycle |
| 126 | // vec: the resouce usage |
| 127 | // startCycle: the start cycle the resouce usage is |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 128 | void updateResourceTable(std::vector<std::vector<unsigned> > vec, |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 129 | int startCycle); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 130 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 131 | // un-do the update in the resource table in the startCycle |
| 132 | // vec: the resouce usage |
| 133 | // startCycle: the start cycle the resouce usage is |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 134 | void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec, |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 135 | int startCycle); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 136 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 137 | // return whether the resourcetable has negative element |
| 138 | // this function is called after updateResouceTable() to determine whether a |
| 139 | // node can be scheduled at certain cycle |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 140 | bool resourceTableNegative(); |
| 141 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 142 | // try to Schedule the node starting from start to end cycle(inclusive) |
| 143 | // if it can be scheduled, put it in the schedule and update nodeScheduled |
| 144 | // node: the node to be scheduled |
| 145 | // start: start cycle |
| 146 | // end : end cycle |
| 147 | // nodeScheduled: a vector storing nodes which has been scheduled |
| 148 | bool ScheduleNode(ModuloSchedGraphNode * node, unsigned start, |
| 149 | unsigned end, NodeVec &nodeScheduled); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 150 | |
| 151 | //each instruction has a memory of the latest clone instruction |
| 152 | //the clone instruction can be get using getClone() |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 153 | //this function clears the memory, i.e. getClone() after calling this function |
| 154 | //returns null |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 155 | void clearCloneMemory(); |
| 156 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 157 | //this fuction make a clone of this input Instruction and update the clone |
| 158 | //memory |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 159 | //inst: the instrution to be cloned |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 160 | Instruction *cloneInstSetMemory(Instruction *inst); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 161 | |
| 162 | //this function update each instrutions which uses ist as its operand |
| 163 | //after update, each instruction will use ist's clone as its operand |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 164 | void updateUseWithClone(Instruction * ist); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 165 | |
| 166 | }; |
| 167 | |
| 168 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 169 | class ModuloSchedulingSet: |
| 170 | NonCopyable { |
| 171 | private: |
| 172 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 173 | //the graphSet to feed in |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 174 | ModuloSchedGraphSet & graphSet; |
| 175 | |
| 176 | public: |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 177 | |
| 178 | //constructor |
| 179 | //Scheduling graph one by one |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 180 | ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) { |
| 181 | for (unsigned i = 0; i < graphSet.size(); i++) { |
| 182 | ModuloSchedGraph & graph = *(graphSet[i]); |
Guochun Shi | 8f1d4ab | 2003-06-08 23:16:07 +0000 | [diff] [blame] | 183 | if (graph.isLoop(graph.getBasicBlock())) |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 184 | ModuloScheduling ModuloScheduling(graph); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 185 | } |
| 186 | }; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 187 | |
| 188 | ~ModuloSchedulingSet() {}; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 191 | #endif |