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