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