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