Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1 | //===- ModuloSchedGraph.cpp - Graph datastructure for Modulo Scheduling ---===// |
| 2 | // |
| 3 | // |
| 4 | //===----------------------------------------------------------------------===// |
| 5 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 6 | #include "llvm/CodeGen/InstrSelection.h" |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 7 | #include "llvm/Function.h" |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 8 | #include "llvm/Instructions.h" |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 9 | #include "llvm/Type.h" |
| 10 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 11 | #include "llvm/CodeGen/MachineInstr.h" |
Guochun Shi | 6fbe5fb | 2003-04-06 23:56:19 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetSchedInfo.h" |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 13 | #include "Support/StringExtras.h" |
| 14 | #include "Support/STLExtras.h" |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 15 | #include "Support/hash_map" |
| 16 | #include "Support/Statistic.h" |
| 17 | #include "ModuloScheduling.h" |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 18 | #include "ModuloSchedGraph.h" |
| 19 | #include <algorithm> |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 20 | #include <ostream> |
| 21 | #include <vector> |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 22 | #include <math.h> |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 23 | |
| 24 | #define UNIDELAY 1 |
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 | //*********************** Internal Data Structures *************************/ |
| 27 | |
| 28 | // The following two types need to be classes, not typedefs, so we can use |
| 29 | // opaque declarations in SchedGraph.h |
| 30 | // |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 31 | struct RefVec:public std::vector<std::pair<ModuloSchedGraphNode*,int> > { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 32 | typedef std::vector<std::pair<ModuloSchedGraphNode*, |
| 33 | int> >::iterator iterator; |
| 34 | typedef std::vector<std::pair<ModuloSchedGraphNode*, |
| 35 | int> >::const_iterator const_iterator; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 38 | struct RegToRefVecMap:public hash_map<int,RefVec> { |
| 39 | typedef hash_map<int,RefVec>::iterator iterator; |
| 40 | typedef hash_map<int,RefVec>::const_iterator const_iterator; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 43 | struct ValueToDefVecMap:public hash_map<const Instruction*,RefVec> { |
| 44 | typedef hash_map<const Instruction*, RefVec>::iterator iterator; |
| 45 | typedef hash_map<const Instruction*, |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 46 | RefVec>::const_iterator const_iterator; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 49 | // class Modulo SchedGraphNode |
| 50 | |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 51 | ModuloSchedGraphNode::ModuloSchedGraphNode(unsigned int in_nodeId, |
| 52 | const BasicBlock * in_bb, |
| 53 | const Instruction * in_inst, |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 54 | int indexInBB, |
| 55 | const TargetMachine & target) |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 56 | :SchedGraphNodeCommon(in_nodeId, indexInBB), inst(in_inst) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 57 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 58 | if (inst) { |
| 59 | //FIXME: find the latency |
| 60 | //currently setthe latency to zero |
| 61 | latency = 0; |
| 62 | } |
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 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 65 | //class ModuloScheGraph |
| 66 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 67 | void ModuloSchedGraph::addDummyEdges() |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 68 | { |
| 69 | assert(graphRoot->outEdges.size() == 0); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 70 | |
| 71 | for (const_iterator I = begin(); I != end(); ++I) { |
| 72 | ModuloSchedGraphNode *node = (ModuloSchedGraphNode *) ((*I).second); |
| 73 | assert(node != graphRoot && node != graphLeaf); |
| 74 | if (node->beginInEdges() == node->endInEdges()) |
| 75 | (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep, |
| 76 | SchedGraphEdge::NonDataDep, 0); |
| 77 | if (node->beginOutEdges() == node->endOutEdges()) |
| 78 | (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep, |
| 79 | SchedGraphEdge::NonDataDep, 0); |
| 80 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 83 | bool isDefinition(const Instruction *I) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 84 | { |
| 85 | //if(TerminatorInst::classof(I)||FreeInst::classof(I) || StoreInst::classof(I) || CallInst::classof(I)) |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 86 | if (!I->hasName()) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 87 | return false; |
| 88 | else |
| 89 | return true; |
| 90 | } |
| 91 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 92 | void ModuloSchedGraph::addDefUseEdges(const BasicBlock *bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 93 | { |
| 94 | //collect def instructions, store them in vector |
Guochun Shi | 6fbe5fb | 2003-04-06 23:56:19 +0000 | [diff] [blame] | 95 | // const TargetInstrInfo& mii = target.getInstrInfo(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 96 | const TargetInstrInfo & mii = target.getInstrInfo(); |
| 97 | |
| 98 | typedef std::vector < ModuloSchedGraphNode * >DefVec; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 99 | DefVec defVec; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 100 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 101 | //find those def instructions |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 102 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); I != E; ++I) { |
| 103 | if (I->getType() != Type::VoidTy) { |
| 104 | defVec.push_back(this->getGraphNodeForInst(I)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | for (unsigned int i = 0; i < defVec.size(); i++) { |
| 109 | for (Value::use_const_iterator I = defVec[i]->getInst()->use_begin(); |
| 110 | I != defVec[i]->getInst()->use_end(); I++) { |
Misha Brukman | 63e04f3 | 2003-04-22 23:00:08 +0000 | [diff] [blame] | 111 | //for each use of a def, add a flow edge from the def instruction to the |
| 112 | //ref instruction |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 113 | |
| 114 | const Instruction *value = defVec[i]->getInst(); |
| 115 | Instruction *inst = (Instruction *) (*I); |
| 116 | ModuloSchedGraphNode *node = NULL; |
| 117 | |
| 118 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); |
| 119 | I != E; ++I) |
| 120 | if ((const Instruction *) I == inst) { |
| 121 | node = (*this)[inst]; |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | assert(inst != NULL && " Use not an Instruction ?"); |
| 126 | |
| 127 | if (node == NULL) //inst is not an instruction in this block |
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 | } else { |
| 130 | // Add a flow edge from the def instruction to the ref instruction |
| 131 | |
| 132 | // self loop will not happen in SSA form |
| 133 | assert(defVec[i] != node && "same node?"); |
| 134 | |
| 135 | // This is a true dependence, so the delay is equal to the delay of the |
| 136 | // pred node. |
| 137 | int delay = 0; |
| 138 | MachineCodeForInstruction & tempMvec = |
| 139 | MachineCodeForInstruction::get(value); |
| 140 | for (unsigned j = 0; j < tempMvec.size(); j++) { |
| 141 | MachineInstr *temp = tempMvec[j]; |
| 142 | //delay +=mii.minLatency(temp->getOpCode()); |
| 143 | delay = std::max(delay, mii.minLatency(temp->getOpCode())); |
| 144 | } |
| 145 | |
| 146 | SchedGraphEdge *trueEdge = |
| 147 | new SchedGraphEdge(defVec[i], node, value, |
| 148 | SchedGraphEdge::TrueDep, delay); |
| 149 | |
| 150 | // if the ref instruction is before the def instrution |
| 151 | // then the def instruction must be a phi instruction |
| 152 | // add an anti-dependence edge to from the ref instruction to the def |
| 153 | // instruction |
| 154 | if (node->getOrigIndexInBB() < defVec[i]->getOrigIndexInBB()) { |
| 155 | assert(PHINode::classof(inst) |
| 156 | && "the ref instruction befre def is not PHINode?"); |
| 157 | trueEdge->setIteDiff(1); |
| 158 | } |
| 159 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 160 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 161 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 166 | void ModuloSchedGraph::addCDEdges(const BasicBlock * bb) { |
| 167 | // find the last instruction in the basic block |
| 168 | // see if it is an branch instruction. |
| 169 | // If yes, then add an edge from each node expcept the last node to the last |
| 170 | // node |
| 171 | const Instruction *inst = &(bb->back()); |
| 172 | ModuloSchedGraphNode *lastNode = (*this)[inst]; |
| 173 | if (TerminatorInst::classof(inst)) |
| 174 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); I != E; |
| 175 | I++) { |
| 176 | if (inst != I) { |
| 177 | ModuloSchedGraphNode *node = (*this)[I]; |
| 178 | //use latency of 0 |
| 179 | (void) new SchedGraphEdge(node, lastNode, SchedGraphEdge::CtrlDep, |
| 180 | SchedGraphEdge::NonDataDep, 0); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 181 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 182 | |
| 183 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 186 | static const int SG_LOAD_REF = 0; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 187 | static const int SG_STORE_REF = 1; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 188 | static const int SG_CALL_REF = 2; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 189 | |
| 190 | static const unsigned int SG_DepOrderArray[][3] = { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 191 | {SchedGraphEdge::NonDataDep, |
| 192 | SchedGraphEdge::AntiDep, |
| 193 | SchedGraphEdge::AntiDep}, |
| 194 | {SchedGraphEdge::TrueDep, |
| 195 | SchedGraphEdge::OutputDep, |
| 196 | SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep}, |
| 197 | {SchedGraphEdge::TrueDep, |
| 198 | SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep, |
| 199 | SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep |
| 200 | | SchedGraphEdge::OutputDep} |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | |
| 204 | // Add a dependence edge between every pair of machine load/store/call |
| 205 | // instructions, where at least one is a store or a call. |
| 206 | // Use latency 1 just to ensure that memory operations are ordered; |
| 207 | // latency does not otherwise matter (true dependences enforce that). |
| 208 | // |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 209 | void ModuloSchedGraph::addMemEdges(const BasicBlock * bb) { |
| 210 | |
| 211 | std::vector<ModuloSchedGraphNode*> memNodeVec; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 212 | |
| 213 | //construct the memNodeVec |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 214 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); I != E; ++I) { |
| 215 | if (LoadInst::classof(I) || StoreInst::classof(I) |
| 216 | || CallInst::classof(I)) { |
| 217 | ModuloSchedGraphNode *node = (*this)[(const Instruction *) I]; |
| 218 | memNodeVec.push_back(node); |
| 219 | } |
| 220 | } |
| 221 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 222 | // Instructions in memNodeVec are in execution order within the basic block, |
| 223 | // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>. |
| 224 | // |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 225 | for (unsigned im = 0, NM = memNodeVec.size(); im < NM; im++) { |
| 226 | const Instruction *fromInst = memNodeVec[im]->getInst(); |
| 227 | int fromType = CallInst::classof(fromInst) ? SG_CALL_REF |
| 228 | : LoadInst::classof(fromInst) ? SG_LOAD_REF : SG_STORE_REF; |
| 229 | for (unsigned jm = im + 1; jm < NM; jm++) { |
| 230 | const Instruction *toInst = memNodeVec[jm]->getInst(); |
| 231 | int toType = CallInst::classof(toInst) ? SG_CALL_REF |
| 232 | : LoadInst::classof(toInst) ? SG_LOAD_REF : SG_STORE_REF; |
| 233 | if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF) { |
| 234 | (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm], |
| 235 | SchedGraphEdge::MemoryDep, |
| 236 | SG_DepOrderArray[fromType][toType], 1); |
| 237 | |
| 238 | SchedGraphEdge *edge = |
| 239 | new SchedGraphEdge(memNodeVec[jm], memNodeVec[im], |
| 240 | SchedGraphEdge::MemoryDep, |
| 241 | SG_DepOrderArray[toType][fromType], 1); |
| 242 | edge->setIteDiff(1); |
| 243 | |
| 244 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 245 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 251 | void ModuloSchedGraph::buildNodesforBB(const TargetMachine &target, |
| 252 | const BasicBlock *bb, |
| 253 | std::vector<ModuloSchedGraphNode*> &memNode, |
| 254 | RegToRefVecMap ®ToRefVecMap, |
| 255 | ValueToDefVecMap &valueToDefVecMap) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 256 | { |
Guochun Shi | 6fbe5fb | 2003-04-06 23:56:19 +0000 | [diff] [blame] | 257 | //const TargetInstrInfo& mii=target.getInstrInfo(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 258 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 259 | //Build graph nodes for each LLVM instruction and gather def/use info. |
| 260 | //Do both together in a single pass over all machine instructions. |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 261 | |
| 262 | int i = 0; |
| 263 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); I != E; |
| 264 | ++I) { |
| 265 | ModuloSchedGraphNode *node = |
| 266 | new ModuloSchedGraphNode(getNumNodes(), bb, I, i, target); |
| 267 | i++; |
| 268 | this->noteModuloSchedGraphNodeForInst(I, node); |
| 269 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 270 | |
| 271 | //this function finds some info about instruction in basic block for later use |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 272 | //findDefUseInfoAtInstr(target, node, |
| 273 | //memNode,regToRefVecMap,valueToDefVecMap); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 277 | bool ModuloSchedGraph::isLoop(const BasicBlock *bb) { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 278 | //only if the last instruction in the basicblock is branch instruction and |
| 279 | //there is at least an option to branch itself |
| 280 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 281 | const Instruction *inst = &(bb->back()); |
| 282 | if (BranchInst::classof(inst)) { |
| 283 | for (unsigned i = 0; i < ((BranchInst *) inst)->getNumSuccessors(); |
| 284 | i++) { |
| 285 | BasicBlock *sb = ((BranchInst *) inst)->getSuccessor(i); |
| 286 | if (sb == bb) |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 291 | return false; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 292 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 293 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 294 | |
| 295 | bool ModuloSchedGraph::isLoop() { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 296 | //only if the last instruction in the basicblock is branch instruction and |
| 297 | //there is at least an option to branch itself |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 298 | |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 299 | assert(this->bb&& "the basicblock is not empty"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 300 | const Instruction *inst = &(bb->back()); |
| 301 | if (BranchInst::classof(inst)) |
| 302 | for (unsigned i = 0; i < ((BranchInst *) inst)->getNumSuccessors(); |
| 303 | i++) { |
| 304 | BasicBlock *sb = ((BranchInst *) inst)->getSuccessor(i); |
| 305 | if (sb == bb) |
| 306 | return true; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 307 | } |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 308 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 309 | return false; |
| 310 | |
| 311 | } |
| 312 | |
| 313 | void ModuloSchedGraph::computeNodeASAP(const BasicBlock *bb) { |
| 314 | |
| 315 | //FIXME: now assume the only backward edges come from the edges from other |
| 316 | //nodes to the phi Node so i will ignore all edges to the phi node; after |
| 317 | //this, there shall be no recurrence. |
| 318 | |
| 319 | unsigned numNodes = bb->size(); |
| 320 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 321 | ModuloSchedGraphNode *node = getNode(i); |
| 322 | node->setPropertyComputed(false); |
| 323 | } |
| 324 | |
| 325 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 326 | ModuloSchedGraphNode *node = getNode(i); |
| 327 | node->ASAP = 0; |
| 328 | if (i == 2 || node->getNumInEdges() == 0) { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 329 | node->setPropertyComputed(true); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 330 | continue; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 331 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 332 | for (ModuloSchedGraphNode::const_iterator I = node->beginInEdges(), E = |
| 333 | node->endInEdges(); I != E; I++) { |
| 334 | SchedGraphEdge *edge = *I; |
| 335 | ModuloSchedGraphNode *pred = |
| 336 | (ModuloSchedGraphNode *) (edge->getSrc()); |
| 337 | assert(pred->getPropertyComputed() |
| 338 | && "pred node property is not computed!"); |
| 339 | int temp = |
| 340 | pred->ASAP + edge->getMinDelay() - |
| 341 | edge->getIteDiff() * this->MII; |
| 342 | node->ASAP = std::max(node->ASAP, temp); |
| 343 | } |
| 344 | node->setPropertyComputed(true); |
| 345 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 348 | void ModuloSchedGraph::computeNodeALAP(const BasicBlock *bb) { |
| 349 | unsigned numNodes = bb->size(); |
| 350 | int maxASAP = 0; |
| 351 | for (unsigned i = numNodes + 1; i >= 2; i--) { |
| 352 | ModuloSchedGraphNode *node = getNode(i); |
| 353 | node->setPropertyComputed(false); |
| 354 | //cerr<< " maxASAP= " <<maxASAP<<" node->ASAP= "<< node->ASAP<<"\n"; |
| 355 | maxASAP = std::max(maxASAP, node->ASAP); |
| 356 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 357 | |
| 358 | //cerr<<"maxASAP is "<<maxASAP<<"\n"; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 359 | |
| 360 | for (unsigned i = numNodes + 1; i >= 2; i--) { |
| 361 | ModuloSchedGraphNode *node = getNode(i); |
| 362 | node->ALAP = maxASAP; |
| 363 | for (ModuloSchedGraphNode::const_iterator I = |
| 364 | node->beginOutEdges(), E = node->endOutEdges(); I != E; I++) { |
| 365 | SchedGraphEdge *edge = *I; |
| 366 | ModuloSchedGraphNode *succ = |
| 367 | (ModuloSchedGraphNode *) (edge->getSink()); |
| 368 | if (PHINode::classof(succ->getInst())) |
| 369 | continue; |
| 370 | assert(succ->getPropertyComputed() |
| 371 | && "succ node property is not computed!"); |
| 372 | int temp = |
| 373 | succ->ALAP - edge->getMinDelay() + |
| 374 | edge->getIteDiff() * this->MII; |
| 375 | node->ALAP = std::min(node->ALAP, temp); |
| 376 | } |
| 377 | node->setPropertyComputed(true); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void ModuloSchedGraph::computeNodeMov(const BasicBlock *bb) |
| 382 | { |
| 383 | unsigned numNodes = bb->size(); |
| 384 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 385 | ModuloSchedGraphNode *node = getNode(i); |
| 386 | node->mov = node->ALAP - node->ASAP; |
| 387 | assert(node->mov >= 0 |
| 388 | && "move freedom for this node is less than zero? "); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | void ModuloSchedGraph::computeNodeDepth(const BasicBlock * bb) |
| 394 | { |
| 395 | |
| 396 | unsigned numNodes = bb->size(); |
| 397 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 398 | ModuloSchedGraphNode *node = getNode(i); |
| 399 | node->setPropertyComputed(false); |
| 400 | } |
| 401 | |
| 402 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 403 | ModuloSchedGraphNode *node = getNode(i); |
| 404 | node->depth = 0; |
| 405 | if (i == 2 || node->getNumInEdges() == 0) { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 406 | node->setPropertyComputed(true); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 407 | continue; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 408 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 409 | for (ModuloSchedGraphNode::const_iterator I = node->beginInEdges(), E = |
| 410 | node->endInEdges(); I != E; I++) { |
| 411 | SchedGraphEdge *edge = *I; |
| 412 | ModuloSchedGraphNode *pred = |
| 413 | (ModuloSchedGraphNode *) (edge->getSrc()); |
| 414 | assert(pred->getPropertyComputed() |
| 415 | && "pred node property is not computed!"); |
| 416 | int temp = pred->depth + edge->getMinDelay(); |
| 417 | node->depth = std::max(node->depth, temp); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 418 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 419 | node->setPropertyComputed(true); |
| 420 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 421 | |
| 422 | } |
| 423 | |
| 424 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 425 | void ModuloSchedGraph::computeNodeHeight(const BasicBlock *bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 426 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 427 | unsigned numNodes = bb->size(); |
| 428 | for (unsigned i = numNodes + 1; i >= 2; i--) { |
| 429 | ModuloSchedGraphNode *node = getNode(i); |
| 430 | node->setPropertyComputed(false); |
| 431 | } |
| 432 | |
| 433 | for (unsigned i = numNodes + 1; i >= 2; i--) { |
| 434 | ModuloSchedGraphNode *node = getNode(i); |
| 435 | node->height = 0; |
| 436 | for (ModuloSchedGraphNode::const_iterator I = |
| 437 | node->beginOutEdges(), E = node->endOutEdges(); I != E; ++I) { |
| 438 | SchedGraphEdge *edge = *I; |
| 439 | ModuloSchedGraphNode *succ = |
| 440 | (ModuloSchedGraphNode *) (edge->getSink()); |
| 441 | if (PHINode::classof(succ->getInst())) |
| 442 | continue; |
| 443 | assert(succ->getPropertyComputed() |
| 444 | && "succ node property is not computed!"); |
| 445 | node->height = std::max(node->height, succ->height + edge->getMinDelay()); |
| 446 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 447 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 448 | node->setPropertyComputed(true); |
| 449 | |
| 450 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 451 | |
| 452 | } |
| 453 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 454 | void ModuloSchedGraph::computeNodeProperty(const BasicBlock * bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 455 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 456 | //FIXME: now assume the only backward edges come from the edges from other |
| 457 | //nodes to the phi Node so i will ignore all edges to the phi node; after |
| 458 | //this, there shall be no recurrence. |
| 459 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 460 | this->computeNodeASAP(bb); |
| 461 | this->computeNodeALAP(bb); |
| 462 | this->computeNodeMov(bb); |
| 463 | this->computeNodeDepth(bb); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 464 | this->computeNodeHeight(bb); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | //do not consider the backedge in these two functions: |
| 469 | // i.e. don't consider the edge with destination in phi node |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 470 | std::vector<ModuloSchedGraphNode*> |
| 471 | ModuloSchedGraph::predSet(std::vector<ModuloSchedGraphNode*> set, |
| 472 | unsigned backEdgeSrc, |
| 473 | unsigned |
| 474 | backEdgeSink) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 475 | { |
| 476 | std::vector<ModuloSchedGraphNode*> predS; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 477 | for (unsigned i = 0; i < set.size(); i++) { |
| 478 | ModuloSchedGraphNode *node = set[i]; |
| 479 | for (ModuloSchedGraphNode::const_iterator I = node->beginInEdges(), E = |
| 480 | node->endInEdges(); I != E; I++) { |
| 481 | SchedGraphEdge *edge = *I; |
| 482 | if (edge->getSrc()->getNodeId() == backEdgeSrc |
| 483 | && edge->getSink()->getNodeId() == backEdgeSink) |
| 484 | continue; |
| 485 | ModuloSchedGraphNode *pred = |
| 486 | (ModuloSchedGraphNode *) (edge->getSrc()); |
| 487 | //if pred is not in the predSet, push it in vector |
| 488 | bool alreadyInset = false; |
| 489 | for (unsigned j = 0; j < predS.size(); ++j) |
| 490 | if (predS[j]->getNodeId() == pred->getNodeId()) { |
| 491 | alreadyInset = true; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | for (unsigned j = 0; j < set.size(); ++j) |
| 496 | if (set[j]->getNodeId() == pred->getNodeId()) { |
| 497 | alreadyInset = true; |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | if (!alreadyInset) |
| 502 | predS.push_back(pred); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 503 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 504 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 505 | return predS; |
| 506 | } |
| 507 | |
| 508 | ModuloSchedGraph::NodeVec ModuloSchedGraph::predSet(NodeVec set) |
| 509 | { |
| 510 | //node number increases from 2 |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 511 | return predSet(set, 0, 0); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 514 | std::vector <ModuloSchedGraphNode*> |
| 515 | ModuloSchedGraph::predSet(ModuloSchedGraphNode *_node, |
| 516 | unsigned backEdgeSrc, unsigned backEdgeSink) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 517 | { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 518 | std::vector<ModuloSchedGraphNode*> set; |
| 519 | set.push_back(_node); |
| 520 | return predSet(set, backEdgeSrc, backEdgeSink); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 523 | std::vector <ModuloSchedGraphNode*> |
| 524 | ModuloSchedGraph::predSet(ModuloSchedGraphNode * _node) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 525 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 526 | return predSet(_node, 0, 0); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 529 | std::vector<ModuloSchedGraphNode*> |
| 530 | ModuloSchedGraph::succSet(std::vector<ModuloSchedGraphNode*> set, |
| 531 | unsigned src, unsigned sink) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 532 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 533 | std::vector<ModuloSchedGraphNode*> succS; |
| 534 | for (unsigned i = 0; i < set.size(); i++) { |
| 535 | ModuloSchedGraphNode *node = set[i]; |
| 536 | for (ModuloSchedGraphNode::const_iterator I = |
| 537 | node->beginOutEdges(), E = node->endOutEdges(); I != E; I++) { |
| 538 | SchedGraphEdge *edge = *I; |
| 539 | if (edge->getSrc()->getNodeId() == src |
| 540 | && edge->getSink()->getNodeId() == sink) |
| 541 | continue; |
| 542 | ModuloSchedGraphNode *succ = |
| 543 | (ModuloSchedGraphNode *) (edge->getSink()); |
| 544 | //if pred is not in the predSet, push it in vector |
| 545 | bool alreadyInset = false; |
| 546 | for (unsigned j = 0; j < succS.size(); j++) |
| 547 | if (succS[j]->getNodeId() == succ->getNodeId()) { |
| 548 | alreadyInset = true; |
| 549 | break; |
| 550 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 551 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 552 | for (unsigned j = 0; j < set.size(); j++) |
| 553 | if (set[j]->getNodeId() == succ->getNodeId()) { |
| 554 | alreadyInset = true; |
| 555 | break; |
| 556 | } |
| 557 | if (!alreadyInset) |
| 558 | succS.push_back(succ); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 559 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 560 | } |
| 561 | return succS; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | ModuloSchedGraph::NodeVec ModuloSchedGraph::succSet(NodeVec set) |
| 565 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 566 | return succSet(set, 0, 0); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 570 | std::vector<ModuloSchedGraphNode*> |
| 571 | ModuloSchedGraph::succSet(ModuloSchedGraphNode *_node, |
| 572 | unsigned src, unsigned sink) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 573 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 574 | std::vector<ModuloSchedGraphNode*>set; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 575 | set.push_back(_node); |
| 576 | return succSet(set, src, sink); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 579 | std::vector<ModuloSchedGraphNode*> |
| 580 | ModuloSchedGraph::succSet(ModuloSchedGraphNode * _node) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 581 | { |
| 582 | return succSet(_node, 0, 0); |
| 583 | } |
| 584 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 585 | SchedGraphEdge *ModuloSchedGraph::getMaxDelayEdge(unsigned srcId, |
| 586 | unsigned sinkId) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 587 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 588 | ModuloSchedGraphNode *node = getNode(srcId); |
| 589 | SchedGraphEdge *maxDelayEdge = NULL; |
| 590 | int maxDelay = -1; |
| 591 | for (ModuloSchedGraphNode::const_iterator I = node->beginOutEdges(), E = |
| 592 | node->endOutEdges(); I != E; I++) { |
| 593 | SchedGraphEdge *edge = *I; |
| 594 | if (edge->getSink()->getNodeId() == sinkId) |
| 595 | if (edge->getMinDelay() > maxDelay) { |
| 596 | maxDelayEdge = edge; |
| 597 | maxDelay = edge->getMinDelay(); |
| 598 | } |
| 599 | } |
| 600 | assert(maxDelayEdge != NULL && "no edge between the srcId and sinkId?"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 601 | return maxDelayEdge; |
| 602 | } |
| 603 | |
| 604 | void ModuloSchedGraph::dumpCircuits() |
| 605 | { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 606 | DEBUG_PRINT(std::cerr << "dumping circuits for graph:\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 607 | int j = -1; |
| 608 | while (circuits[++j][0] != 0) { |
| 609 | int k = -1; |
| 610 | while (circuits[j][++k] != 0) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 611 | DEBUG_PRINT(std::cerr << circuits[j][k] << "\t"); |
| 612 | DEBUG_PRINT(std::cerr << "\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 616 | void ModuloSchedGraph::dumpSet(std::vector < ModuloSchedGraphNode * >set) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 617 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 618 | for (unsigned i = 0; i < set.size(); i++) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 619 | DEBUG_PRINT(std::cerr << set[i]->getNodeId() << "\t"); |
| 620 | DEBUG_PRINT(std::cerr << "\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 623 | std::vector<ModuloSchedGraphNode*> |
| 624 | ModuloSchedGraph::vectorUnion(std::vector<ModuloSchedGraphNode*> set1, |
| 625 | std::vector<ModuloSchedGraphNode*> set2) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 626 | { |
| 627 | std::vector<ModuloSchedGraphNode*> unionVec; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 628 | for (unsigned i = 0; i < set1.size(); i++) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 629 | unionVec.push_back(set1[i]); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 630 | for (unsigned j = 0; j < set2.size(); j++) { |
| 631 | bool inset = false; |
| 632 | for (unsigned i = 0; i < unionVec.size(); i++) |
| 633 | if (set2[j] == unionVec[i]) |
| 634 | inset = true; |
| 635 | if (!inset) |
| 636 | unionVec.push_back(set2[j]); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 637 | } |
| 638 | return unionVec; |
| 639 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 640 | |
| 641 | std::vector<ModuloSchedGraphNode*> |
| 642 | ModuloSchedGraph::vectorConj(std::vector<ModuloSchedGraphNode*> set1, |
| 643 | std::vector<ModuloSchedGraphNode*> set2) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 644 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 645 | std::vector<ModuloSchedGraphNode*> conjVec; |
| 646 | for (unsigned i = 0; i < set1.size(); i++) |
| 647 | for (unsigned j = 0; j < set2.size(); j++) |
| 648 | if (set1[i] == set2[j]) |
| 649 | conjVec.push_back(set1[i]); |
| 650 | return conjVec; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 653 | ModuloSchedGraph::NodeVec ModuloSchedGraph::vectorSub(NodeVec set1, |
| 654 | NodeVec set2) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 655 | { |
| 656 | NodeVec newVec; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 657 | for (NodeVec::iterator I = set1.begin(); I != set1.end(); I++) { |
| 658 | bool inset = false; |
| 659 | for (NodeVec::iterator II = set2.begin(); II != set2.end(); II++) |
| 660 | if ((*I)->getNodeId() == (*II)->getNodeId()) { |
| 661 | inset = true; |
| 662 | break; |
| 663 | } |
| 664 | if (!inset) |
| 665 | newVec.push_back(*I); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 666 | } |
| 667 | return newVec; |
| 668 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 669 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 670 | void ModuloSchedGraph::orderNodes() { |
| 671 | oNodes.clear(); |
| 672 | |
| 673 | std::vector < ModuloSchedGraphNode * >set; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 674 | unsigned numNodes = bb->size(); |
| 675 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 676 | // first order all the sets |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 677 | int j = -1; |
| 678 | int totalDelay = -1; |
| 679 | int preDelay = -1; |
| 680 | while (circuits[++j][0] != 0) { |
| 681 | int k = -1; |
| 682 | preDelay = totalDelay; |
| 683 | |
| 684 | while (circuits[j][++k] != 0) { |
| 685 | ModuloSchedGraphNode *node = getNode(circuits[j][k]); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 686 | unsigned nextNodeId; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 687 | nextNodeId = |
| 688 | circuits[j][k + 1] != 0 ? circuits[j][k + 1] : circuits[j][0]; |
| 689 | SchedGraphEdge *edge = getMaxDelayEdge(circuits[j][k], nextNodeId); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 690 | totalDelay += edge->getMinDelay(); |
| 691 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 692 | if (preDelay != -1 && totalDelay > preDelay) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 693 | // swap circuits[j][] and cuicuits[j-1][] |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 694 | unsigned temp[MAXNODE]; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 695 | for (int k = 0; k < MAXNODE; k++) { |
| 696 | temp[k] = circuits[j - 1][k]; |
| 697 | circuits[j - 1][k] = circuits[j][k]; |
| 698 | circuits[j][k] = temp[k]; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 699 | } |
| 700 | //restart |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 701 | j = -1; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 705 | // build the first set |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 706 | int backEdgeSrc; |
| 707 | int backEdgeSink; |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 708 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 709 | DEBUG_PRINT(std::cerr << "building the first set" << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 710 | int setSeq = -1; |
| 711 | int k = -1; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 712 | setSeq++; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 713 | while (circuits[setSeq][++k] != 0) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 714 | set.push_back(getNode(circuits[setSeq][k])); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 715 | if (circuits[setSeq][0] != 0) { |
| 716 | backEdgeSrc = circuits[setSeq][k - 1]; |
| 717 | backEdgeSink = circuits[setSeq][0]; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 718 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 719 | if (ModuloScheduling::printScheduleProcess()) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 720 | DEBUG_PRINT(std::cerr << "the first set is:"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 721 | dumpSet(set); |
| 722 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 723 | // implement the ordering algorithm |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 724 | enum OrderSeq { bottom_up, top_down }; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 725 | OrderSeq order; |
| 726 | std::vector<ModuloSchedGraphNode*> R; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 727 | while (!set.empty()) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 728 | std::vector<ModuloSchedGraphNode*> pset = predSet(oNodes); |
| 729 | std::vector<ModuloSchedGraphNode*> sset = succSet(oNodes); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 730 | |
| 731 | if (!pset.empty() && !vectorConj(pset, set).empty()) { |
| 732 | R = vectorConj(pset, set); |
| 733 | order = bottom_up; |
| 734 | } else if (!sset.empty() && !vectorConj(sset, set).empty()) { |
| 735 | R = vectorConj(sset, set); |
| 736 | order = top_down; |
| 737 | } else { |
| 738 | int maxASAP = -1; |
| 739 | int position = -1; |
| 740 | for (unsigned i = 0; i < set.size(); i++) { |
| 741 | int temp = set[i]->getASAP(); |
| 742 | if (temp > maxASAP) { |
| 743 | maxASAP = temp; |
| 744 | position = i; |
| 745 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 746 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 747 | R.push_back(set[position]); |
| 748 | order = bottom_up; |
| 749 | } |
| 750 | |
| 751 | while (!R.empty()) { |
| 752 | if (order == top_down) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 753 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 754 | DEBUG_PRINT(std::cerr << "in top_down round\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 755 | while (!R.empty()) { |
| 756 | int maxHeight = -1; |
| 757 | NodeVec::iterator chosenI; |
| 758 | for (NodeVec::iterator I = R.begin(); I != R.end(); I++) { |
| 759 | int temp = (*I)->height; |
| 760 | if ((temp > maxHeight) |
| 761 | || (temp == maxHeight && (*I)->mov <= (*chosenI)->mov)) { |
| 762 | |
| 763 | if ((temp > maxHeight) |
| 764 | || (temp == maxHeight && (*I)->mov < (*chosenI)->mov)) { |
| 765 | maxHeight = temp; |
| 766 | chosenI = I; |
| 767 | continue; |
| 768 | } |
| 769 | //possible case: instruction A and B has the same height and mov, |
| 770 | //but A has dependence to B e.g B is the branch instruction in the |
| 771 | //end, or A is the phi instruction at the beginning |
| 772 | if ((*I)->mov == (*chosenI)->mov) |
| 773 | for (ModuloSchedGraphNode::const_iterator oe = |
| 774 | (*I)->beginOutEdges(), end = (*I)->endOutEdges(); |
| 775 | oe != end; oe++) { |
| 776 | if ((*oe)->getSink() == (*chosenI)) { |
| 777 | maxHeight = temp; |
| 778 | chosenI = I; |
| 779 | continue; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | ModuloSchedGraphNode *mu = *chosenI; |
| 785 | oNodes.push_back(mu); |
| 786 | R.erase(chosenI); |
| 787 | std::vector<ModuloSchedGraphNode*> succ_mu = |
| 788 | succSet(mu, backEdgeSrc, backEdgeSink); |
| 789 | std::vector<ModuloSchedGraphNode*> comm = |
| 790 | vectorConj(succ_mu, set); |
| 791 | comm = vectorSub(comm, oNodes); |
| 792 | R = vectorUnion(comm, R); |
| 793 | } |
| 794 | order = bottom_up; |
| 795 | R = vectorConj(predSet(oNodes), set); |
| 796 | } else { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 797 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 798 | DEBUG_PRINT(std::cerr << "in bottom up round\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 799 | while (!R.empty()) { |
| 800 | int maxDepth = -1; |
| 801 | NodeVec::iterator chosenI; |
| 802 | for (NodeVec::iterator I = R.begin(); I != R.end(); I++) { |
| 803 | int temp = (*I)->depth; |
| 804 | if ((temp > maxDepth) |
| 805 | || (temp == maxDepth && (*I)->mov < (*chosenI)->mov)) { |
| 806 | maxDepth = temp; |
| 807 | chosenI = I; |
| 808 | } |
| 809 | } |
| 810 | ModuloSchedGraphNode *mu = *chosenI; |
| 811 | oNodes.push_back(mu); |
| 812 | R.erase(chosenI); |
| 813 | std::vector<ModuloSchedGraphNode*> pred_mu = |
| 814 | predSet(mu, backEdgeSrc, backEdgeSink); |
| 815 | std::vector<ModuloSchedGraphNode*> comm = |
| 816 | vectorConj(pred_mu, set); |
| 817 | comm = vectorSub(comm, oNodes); |
| 818 | R = vectorUnion(comm, R); |
| 819 | } |
| 820 | order = top_down; |
| 821 | R = vectorConj(succSet(oNodes), set); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 822 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 823 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 824 | if (ModuloScheduling::printScheduleProcess()) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 825 | DEBUG_PRINT(std::cerr << "order finished\n"); |
| 826 | DEBUG_PRINT(std::cerr << "dumping the ordered nodes:\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 827 | dumpSet(oNodes); |
| 828 | dumpCircuits(); |
| 829 | } |
| 830 | //create a new set |
| 831 | //FIXME: the nodes between onodes and this circuit should also be include in |
| 832 | //this set |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 833 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 834 | DEBUG_PRINT(std::cerr << "building the next set\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 835 | set.clear(); |
| 836 | int k = -1; |
| 837 | setSeq++; |
| 838 | while (circuits[setSeq][++k] != 0) |
| 839 | set.push_back(getNode(circuits[setSeq][k])); |
| 840 | if (circuits[setSeq][0] != 0) { |
| 841 | backEdgeSrc = circuits[setSeq][k - 1]; |
| 842 | backEdgeSink = circuits[setSeq][0]; |
| 843 | } |
| 844 | if (set.empty()) { |
| 845 | //no circuits any more |
| 846 | //collect all other nodes |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 847 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 848 | DEBUG_PRINT(std::cerr << "no circuits any more, collect the rest nodes\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 849 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 850 | bool inset = false; |
| 851 | for (unsigned j = 0; j < oNodes.size(); j++) |
| 852 | if (oNodes[j]->getNodeId() == i) { |
| 853 | inset = true; |
| 854 | break; |
| 855 | } |
| 856 | if (!inset) |
| 857 | set.push_back(getNode(i)); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 858 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 859 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 860 | if (ModuloScheduling::printScheduleProcess()) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 861 | DEBUG_PRINT(std::cerr << "next set is:\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 862 | dumpSet(set); |
| 863 | } |
| 864 | } //while(!set.empty()) |
| 865 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
| 869 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 870 | void ModuloSchedGraph::buildGraph(const TargetMachine & target) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 871 | { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 872 | |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 873 | assert(this->bb && "The basicBlock is NULL?"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 874 | |
| 875 | // Use this data structure to note all machine operands that compute |
| 876 | // ordinary LLVM values. These must be computed defs (i.e., instructions). |
| 877 | // Note that there may be multiple machine instructions that define |
| 878 | // each Value. |
| 879 | ValueToDefVecMap valueToDefVecMap; |
| 880 | |
| 881 | // Use this data structure to note all memory instructions. |
| 882 | // We use this to add memory dependence edges without a second full walk. |
| 883 | // |
| 884 | // vector<const Instruction*> memVec; |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 885 | std::vector<ModuloSchedGraphNode*> memNodeVec; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 886 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 887 | // Use this data structure to note any uses or definitions of |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 888 | // machine registers so we can add edges for those later without |
| 889 | // extra passes over the nodes. |
| 890 | // The vector holds an ordered list of references to the machine reg, |
| 891 | // ordered according to control-flow order. This only works for a |
| 892 | // single basic block, hence the assertion. Each reference is identified |
| 893 | // by the pair: <node, operand-number>. |
| 894 | // |
| 895 | RegToRefVecMap regToRefVecMap; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 896 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 897 | // Make a dummy root node. We'll add edges to the real roots later. |
| 898 | graphRoot = new ModuloSchedGraphNode(0, NULL, NULL, -1, target); |
| 899 | graphLeaf = new ModuloSchedGraphNode(1, NULL, NULL, -1, target); |
| 900 | |
| 901 | //---------------------------------------------------------------- |
| 902 | // First add nodes for all the LLVM instructions in the basic block because |
| 903 | // this greatly simplifies identifying which edges to add. Do this one VM |
| 904 | // instruction at a time since the ModuloSchedGraphNode needs that. |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 905 | // Also, remember the load/store instructions to add memory deps later. |
| 906 | //---------------------------------------------------------------- |
| 907 | |
| 908 | //FIXME:if there is call instruction, then we shall quit somewhere |
| 909 | // currently we leave call instruction and continue construct graph |
| 910 | |
| 911 | //dump only the blocks which are from loops |
| 912 | |
| 913 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 914 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 915 | this->dump(bb); |
| 916 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 917 | if (!isLoop(bb)) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 918 | DEBUG_PRINT(std::cerr << " dumping non-loop BB:\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 919 | dump(bb); |
| 920 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 921 | if (isLoop(bb)) { |
| 922 | buildNodesforBB(target, bb, memNodeVec, regToRefVecMap, |
| 923 | valueToDefVecMap); |
| 924 | |
| 925 | this->addDefUseEdges(bb); |
| 926 | this->addCDEdges(bb); |
| 927 | this->addMemEdges(bb); |
| 928 | |
| 929 | //this->dump(); |
| 930 | |
| 931 | int ResII = this->computeResII(bb); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 932 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 933 | DEBUG_PRINT(std::cerr << "ResII is " << ResII << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 934 | int RecII = this->computeRecII(bb); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 935 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 936 | DEBUG_PRINT(std::cerr << "RecII is " << RecII << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 937 | |
| 938 | this->MII = std::max(ResII, RecII); |
| 939 | |
| 940 | this->computeNodeProperty(bb); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 941 | if (ModuloScheduling::printScheduleProcess()) |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 942 | this->dumpNodeProperty(); |
| 943 | |
| 944 | this->orderNodes(); |
| 945 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 946 | if (ModuloScheduling::printScheduleProcess()) |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 947 | this->dump(); |
| 948 | //this->instrScheduling(); |
| 949 | |
| 950 | //this->dumpScheduling(); |
| 951 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 954 | ModuloSchedGraphNode *ModuloSchedGraph::getNode(const unsigned nodeId) const |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 955 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 956 | for (const_iterator I = begin(), E = end(); I != E; I++) |
| 957 | if ((*I).second->getNodeId() == nodeId) |
| 958 | return (ModuloSchedGraphNode *) (*I).second; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 959 | return NULL; |
| 960 | } |
| 961 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 962 | int ModuloSchedGraph::computeRecII(const BasicBlock *bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 963 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 964 | int RecII = 0; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 965 | |
| 966 | //os<<"begining computerRecII()"<<"\n"; |
| 967 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 968 | //FIXME: only deal with circuits starting at the first node: the phi node |
| 969 | //nodeId=2; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 970 | |
| 971 | //search all elementary circuits in the dependance graph |
| 972 | //assume maximum number of nodes is MAXNODE |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 973 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 974 | unsigned path[MAXNODE]; |
| 975 | unsigned stack[MAXNODE][MAXNODE]; |
| 976 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 977 | for (int j = 0; j < MAXNODE; j++) { |
| 978 | path[j] = 0; |
| 979 | for (int k = 0; k < MAXNODE; k++) |
| 980 | stack[j][k] = 0; |
| 981 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 982 | //in our graph, the node number starts at 2 |
| 983 | |
| 984 | //number of nodes, because each instruction will result in one node |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 985 | const unsigned numNodes = bb->size(); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 986 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 987 | int i = 0; |
| 988 | path[i] = 2; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 989 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 990 | ModuloSchedGraphNode *initNode = getNode(path[0]); |
| 991 | unsigned initNodeId = initNode->getNodeId(); |
| 992 | ModuloSchedGraphNode *currentNode = initNode; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 993 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 994 | while (currentNode != NULL) { |
| 995 | unsigned currentNodeId = currentNode->getNodeId(); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 996 | // DEBUG_PRINT(std::cerr<<"current node is "<<currentNodeId<<"\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 997 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 998 | ModuloSchedGraphNode *nextNode = NULL; |
| 999 | for (ModuloSchedGraphNode::const_iterator I = |
| 1000 | currentNode->beginOutEdges(), E = currentNode->endOutEdges(); |
| 1001 | I != E; I++) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1002 | //DEBUG_PRINT(std::cerr <<" searching in outgoint edges of node |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1003 | //"<<currentNodeId<<"\n"; |
| 1004 | unsigned nodeId = ((SchedGraphEdge *) * I)->getSink()->getNodeId(); |
| 1005 | bool inpath = false, instack = false; |
| 1006 | int k; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1007 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1008 | //DEBUG_PRINT(std::cerr<<"nodeId is "<<nodeId<<"\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1009 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1010 | k = -1; |
| 1011 | while (path[++k] != 0) |
| 1012 | if (nodeId == path[k]) { |
| 1013 | inpath = true; |
| 1014 | break; |
| 1015 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1016 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1017 | k = -1; |
| 1018 | while (stack[i][++k] != 0) |
| 1019 | if (nodeId == stack[i][k]) { |
| 1020 | instack = true; |
| 1021 | break; |
| 1022 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1023 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1024 | if (nodeId > currentNodeId && !inpath && !instack) { |
| 1025 | nextNode = |
| 1026 | (ModuloSchedGraphNode *) ((SchedGraphEdge *) * I)->getSink(); |
| 1027 | break; |
| 1028 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1029 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (nextNode != NULL) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1032 | //DEBUG_PRINT(std::cerr<<"find the next Node "<<nextNode->getNodeId()<<"\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1033 | |
| 1034 | int j = 0; |
| 1035 | while (stack[i][j] != 0) |
| 1036 | j++; |
| 1037 | stack[i][j] = nextNode->getNodeId(); |
| 1038 | |
| 1039 | i++; |
| 1040 | path[i] = nextNode->getNodeId(); |
| 1041 | currentNode = nextNode; |
| 1042 | } else { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1043 | //DEBUG_PRINT(std::cerr<<"no expansion any more"<<"\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1044 | //confirmCircuit(); |
| 1045 | for (ModuloSchedGraphNode::const_iterator I = |
| 1046 | currentNode->beginOutEdges(), E = currentNode->endOutEdges(); |
| 1047 | I != E; I++) { |
| 1048 | unsigned nodeId = ((SchedGraphEdge *) * I)->getSink()->getNodeId(); |
| 1049 | if (nodeId == initNodeId) { |
| 1050 | |
| 1051 | int j = -1; |
| 1052 | while (circuits[++j][0] != 0); |
| 1053 | for (int k = 0; k < MAXNODE; k++) |
| 1054 | circuits[j][k] = path[k]; |
| 1055 | |
| 1056 | } |
| 1057 | } |
| 1058 | //remove this node in the path and clear the corresponding entries in the |
| 1059 | //stack |
| 1060 | path[i] = 0; |
| 1061 | int j = 0; |
| 1062 | for (j = 0; j < MAXNODE; j++) |
| 1063 | stack[i][j] = 0; |
| 1064 | i--; |
| 1065 | currentNode = getNode(path[i]); |
| 1066 | } |
| 1067 | if (i == 0) { |
| 1068 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1069 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1070 | DEBUG_PRINT(std::cerr << "circuits found are:\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1071 | int j = -1; |
| 1072 | while (circuits[++j][0] != 0) { |
| 1073 | int k = -1; |
| 1074 | while (circuits[j][++k] != 0) |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1075 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1076 | DEBUG_PRINT(std::cerr << circuits[j][k] << "\t"); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1077 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1078 | DEBUG_PRINT(std::cerr << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1079 | |
| 1080 | //for this circuit, compute the sum of all edge delay |
| 1081 | int sumDelay = 0; |
| 1082 | k = -1; |
| 1083 | while (circuits[j][++k] != 0) { |
| 1084 | //ModuloSchedGraphNode* node =getNode(circuits[j][k]); |
| 1085 | unsigned nextNodeId; |
| 1086 | nextNodeId = |
| 1087 | circuits[j][k + 1] != |
| 1088 | 0 ? circuits[j][k + 1] : circuits[j][0]; |
| 1089 | |
| 1090 | /* |
| 1091 | for(ModuloSchedGraphNode::const_iterator I=node->beginOutEdges(), |
| 1092 | E=node->endOutEdges();I !=E; I++) |
| 1093 | { |
| 1094 | SchedGraphEdge* edge= *I; |
| 1095 | if(edge->getSink()->getNodeId() == nextNodeId) |
| 1096 | {sumDelay += edge->getMinDelay();break;} |
| 1097 | } |
| 1098 | */ |
| 1099 | |
| 1100 | sumDelay += |
| 1101 | getMaxDelayEdge(circuits[j][k], nextNodeId)->getMinDelay(); |
| 1102 | |
| 1103 | } |
| 1104 | // assume we have distance 1, in this case the sumDelay is RecII |
| 1105 | // this is correct for SSA form only |
| 1106 | // |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1107 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1108 | DEBUG_PRINT(std::cerr << "The total Delay in the circuit is " << sumDelay |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1109 | << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1110 | |
| 1111 | RecII = RecII > sumDelay ? RecII : sumDelay; |
| 1112 | |
| 1113 | } |
| 1114 | return RecII; |
| 1115 | } |
| 1116 | |
| 1117 | } |
| 1118 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1119 | return -1; |
| 1120 | } |
| 1121 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1122 | void ModuloSchedGraph::addResourceUsage(std::vector<std::pair<int,int> > &ruVec, |
| 1123 | int rid) |
| 1124 | { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1125 | //DEBUG_PRINT(std::cerr<<"\nadding a resouce , current resouceUsage vector size is |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1126 | //"<<ruVec.size()<<"\n"; |
| 1127 | bool alreadyExists = false; |
| 1128 | for (unsigned i = 0; i < ruVec.size(); i++) { |
| 1129 | if (rid == ruVec[i].first) { |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1130 | ruVec[i].second++; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1131 | alreadyExists = true; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | } |
| 1134 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1135 | if (!alreadyExists) |
| 1136 | ruVec.push_back(std::make_pair(rid, 1)); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1137 | //DEBUG_PRINT(std::cerr<<"current resouceUsage vector size is "<<ruVec.size()<<"\n"; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1138 | |
| 1139 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1140 | void ModuloSchedGraph::dumpResourceUsage(std::vector<std::pair<int,int> > &ru) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1141 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1142 | TargetSchedInfo & msi = (TargetSchedInfo &) target.getSchedInfo(); |
| 1143 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1144 | std::vector<std::pair<int,int> > resourceNumVector = msi.resourceNumVector; |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1145 | DEBUG_PRINT(std::cerr << "resourceID\t" << "resourceNum\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1146 | for (unsigned i = 0; i < resourceNumVector.size(); i++) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1147 | DEBUG_PRINT(std::cerr << resourceNumVector[i]. |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1148 | first << "\t" << resourceNumVector[i].second << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1149 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1150 | DEBUG_PRINT(std::cerr << " maxNumIssueTotal(issue slot in one cycle) = " << msi. |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1151 | maxNumIssueTotal << "\n"); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1152 | DEBUG_PRINT(std::cerr << "resourceID\t resourceUsage\t ResourceNum\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1153 | for (unsigned i = 0; i < ru.size(); i++) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1154 | DEBUG_PRINT(std::cerr << ru[i].first << "\t" << ru[i].second); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1155 | const unsigned resNum = msi.getCPUResourceNum(ru[i].first); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1156 | DEBUG_PRINT(std::cerr << "\t" << resNum << "\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1160 | int ModuloSchedGraph::computeResII(const BasicBlock * bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1161 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1162 | |
| 1163 | const TargetInstrInfo & mii = target.getInstrInfo(); |
| 1164 | const TargetSchedInfo & msi = target.getSchedInfo(); |
| 1165 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1166 | int ResII; |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1167 | std::vector<std::pair<int,int> > resourceUsage; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1168 | //pair<int resourceid, int resourceUsageTimes_in_the_whole_block> |
| 1169 | |
| 1170 | //FIXME: number of functional units the target machine can provide should be |
| 1171 | //get from Target here I temporary hardcode it |
| 1172 | |
| 1173 | for (BasicBlock::const_iterator I = bb->begin(), E = bb->end(); I != E; |
| 1174 | I++) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1175 | if (ModuloScheduling::printScheduleProcess()) { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1176 | DEBUG_PRINT(std::cerr << "machine instruction for llvm instruction( node " << |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1177 | getGraphNodeForInst(I)->getNodeId() << ")\n"); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1178 | DEBUG_PRINT(std::cerr << "\t" << *I); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1179 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1180 | MachineCodeForInstruction & tempMvec = |
| 1181 | MachineCodeForInstruction::get(I); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1182 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1183 | DEBUG_PRINT(std::cerr << "size =" << tempMvec.size() << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1184 | for (unsigned i = 0; i < tempMvec.size(); i++) { |
| 1185 | MachineInstr *minstr = tempMvec[i]; |
| 1186 | |
| 1187 | unsigned minDelay = mii.minLatency(minstr->getOpCode()); |
| 1188 | InstrRUsage rUsage = msi.getInstrRUsage(minstr->getOpCode()); |
| 1189 | InstrClassRUsage classRUsage = |
| 1190 | msi.getClassRUsage(mii.getSchedClass(minstr->getOpCode())); |
| 1191 | unsigned totCycles = classRUsage.totCycles; |
| 1192 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1193 | std::vector<std::vector<resourceId_t> > resources=rUsage.resourcesByCycle; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1194 | assert(totCycles == resources.size()); |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1195 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1196 | DEBUG_PRINT(std::cerr << "resources Usage for this Instr(totCycles=" |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1197 | << totCycles << ",mindLatency=" |
| 1198 | << mii.minLatency(minstr->getOpCode()) << "): " << *minstr |
| 1199 | << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1200 | for (unsigned j = 0; j < resources.size(); j++) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1201 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1202 | DEBUG_PRINT(std::cerr << "cycle " << j << ": "); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1203 | for (unsigned k = 0; k < resources[j].size(); k++) { |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1204 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1205 | DEBUG_PRINT(std::cerr << "\t" << resources[j][k]); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1206 | addResourceUsage(resourceUsage, resources[j][k]); |
| 1207 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1208 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1209 | DEBUG_PRINT(std::cerr << "\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1213 | if (ModuloScheduling::printScheduleProcess()) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1214 | this->dumpResourceUsage(resourceUsage); |
| 1215 | |
| 1216 | //compute ResII |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1217 | ResII = 0; |
| 1218 | int issueSlots = msi.maxNumIssueTotal; |
| 1219 | for (unsigned i = 0; i < resourceUsage.size(); i++) { |
| 1220 | int resourceNum = msi.getCPUResourceNum(resourceUsage[i].first); |
| 1221 | int useNum = resourceUsage[i].second; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1222 | double tempII; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1223 | if (resourceNum <= issueSlots) |
| 1224 | tempII = ceil(1.0 * useNum / resourceNum); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1225 | else |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1226 | tempII = ceil(1.0 * useNum / issueSlots); |
| 1227 | ResII = std::max((int) tempII, ResII); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1228 | } |
| 1229 | return ResII; |
| 1230 | } |
| 1231 | |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1232 | ModuloSchedGraphSet::ModuloSchedGraphSet(const Function *function, |
| 1233 | const TargetMachine &target) |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1234 | : method(function) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1235 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1236 | buildGraphsForMethod(method, target); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | ModuloSchedGraphSet::~ModuloSchedGraphSet() |
| 1241 | { |
| 1242 | //delete all the graphs |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1243 | for (iterator I = begin(), E = end(); I != E; ++I) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1244 | delete *I; |
| 1245 | } |
| 1246 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1247 | void ModuloSchedGraphSet::dump() const |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1248 | { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1249 | DEBUG_PRINT(std::cerr << " ====== ModuloSched graphs for function `" << |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1250 | method->getName() << "' =========\n\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1251 | for (const_iterator I = begin(); I != end(); ++I) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1252 | (*I)->dump(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1253 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1254 | DEBUG_PRINT(std::cerr << "\n=========End graphs for function `" << method->getName() |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1255 | << "' ==========\n\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1258 | void ModuloSchedGraph::dump(const BasicBlock * bb) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1259 | { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1260 | DEBUG_PRINT(std::cerr << "dumping basic block:"); |
| 1261 | DEBUG_PRINT(std::cerr << (bb->hasName()? bb->getName() : "block") |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1262 | << " (" << bb << ")" << "\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1265 | void ModuloSchedGraph::dump(const BasicBlock * bb, std::ostream & os) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1266 | { |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1267 | os << "dumping basic block:"; |
| 1268 | os << (bb->hasName()? bb->getName() : "block") |
| 1269 | << " (" << bb << ")" << "\n"; |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1272 | void ModuloSchedGraph::dump() const |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1273 | { |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1274 | DEBUG_PRINT(std::cerr << " ModuloSchedGraph for basic Blocks:"); |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 1275 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1276 | DEBUG_PRINT(std::cerr << (bb->hasName()? bb->getName() : "block") |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 1277 | << " (" << bb << ")" << ""); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1278 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1279 | DEBUG_PRINT(std::cerr << "\n\n Actual Root nodes : "); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1280 | for (unsigned i = 0, N = graphRoot->outEdges.size(); i < N; i++) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1281 | DEBUG_PRINT(std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId() |
Misha Brukman | 2c821cc | 2003-04-10 19:19:23 +0000 | [diff] [blame] | 1282 | << ((i == N - 1) ? "" : ", ")); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1283 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1284 | DEBUG_PRINT(std::cerr << "\n Graph Nodes:\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1285 | //for (const_iterator I=begin(); I != end(); ++I) |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1286 | //DEBUG_PRINT(std::cerr << "\n" << *I->second; |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 1287 | unsigned numNodes = bb->size(); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1288 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 1289 | ModuloSchedGraphNode *node = getNode(i); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1290 | DEBUG_PRINT(std::cerr << "\n" << *node); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1291 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1292 | |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1293 | DEBUG_PRINT(std::cerr << "\n"); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1294 | } |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1295 | |
| 1296 | void ModuloSchedGraph::dumpNodeProperty() const |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1297 | { |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 1298 | |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1299 | unsigned numNodes = bb->size(); |
| 1300 | for (unsigned i = 2; i < numNodes + 2; i++) { |
| 1301 | ModuloSchedGraphNode *node = getNode(i); |
Guochun Shi | 3328052 | 2003-06-08 20:40:47 +0000 | [diff] [blame^] | 1302 | DEBUG_PRINT(std::cerr << "NodeId " << node->getNodeId() << "\t"); |
| 1303 | DEBUG_PRINT(std::cerr << "ASAP " << node->getASAP() << "\t"); |
| 1304 | DEBUG_PRINT(std::cerr << "ALAP " << node->getALAP() << "\t"); |
| 1305 | DEBUG_PRINT(std::cerr << "mov " << node->getMov() << "\t"); |
| 1306 | DEBUG_PRINT(std::cerr << "depth " << node->getDepth() << "\t"); |
| 1307 | DEBUG_PRINT(std::cerr << "height " << node->getHeight() << "\t\n"); |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1308 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Misha Brukman | 63e04f3 | 2003-04-22 23:00:08 +0000 | [diff] [blame] | 1311 | void ModuloSchedGraphSet::buildGraphsForMethod(const Function *F, |
| 1312 | const TargetMachine &target) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1313 | { |
Guochun Shi | 099b064 | 2003-06-02 17:48:56 +0000 | [diff] [blame] | 1314 | for (Function::const_iterator BI = F->begin(); BI != F->end(); ++BI){ |
| 1315 | const BasicBlock* local_bb; |
| 1316 | local_bb=BI; |
| 1317 | addGraph(new ModuloSchedGraph((BasicBlock*)local_bb, target)); |
| 1318 | } |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Misha Brukman | 63e04f3 | 2003-04-22 23:00:08 +0000 | [diff] [blame] | 1321 | std::ostream& operator<<(std::ostream &os, |
| 1322 | const ModuloSchedGraphNode &node) |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1323 | { |
| 1324 | os << std::string(8, ' ') |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1325 | << "Node " << node.nodeId << " : " |
| 1326 | << "latency = " << node.latency << "\n" << std::string(12, ' '); |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (node.getInst() == NULL) |
| 1329 | os << "(Dummy node)\n"; |
Misha Brukman | 8baa01c | 2003-04-09 21:51:34 +0000 | [diff] [blame] | 1330 | else { |
| 1331 | os << *node.getInst() << "\n" << std::string(12, ' '); |
| 1332 | os << node.inEdges.size() << " Incoming Edges:\n"; |
| 1333 | for (unsigned i = 0, N = node.inEdges.size(); i < N; i++) |
| 1334 | os << std::string(16, ' ') << *node.inEdges[i]; |
| 1335 | |
| 1336 | os << std::string(12, ' ') << node.outEdges.size() |
| 1337 | << " Outgoing Edges:\n"; |
| 1338 | for (unsigned i = 0, N = node.outEdges.size(); i < N; i++) |
| 1339 | os << std::string(16, ' ') << *node.outEdges[i]; |
| 1340 | } |
| 1341 | |
Guochun Shi | f1c154f | 2003-03-27 17:57:44 +0000 | [diff] [blame] | 1342 | return os; |
| 1343 | } |