blob: ebbb49971b49b2b2a79f302804588e9438a4c4ad [file] [log] [blame]
Misha Brukman8baa01c2003-04-09 21:51:34 +00001//===- ModuloSchedGraph.cpp - Graph datastructure for Modulo Scheduling ---===//
2//
3//
4//===----------------------------------------------------------------------===//
5
Guochun Shif1c154f2003-03-27 17:57:44 +00006#include "llvm/CodeGen/InstrSelection.h"
Guochun Shif1c154f2003-03-27 17:57:44 +00007#include "llvm/Function.h"
Misha Brukman8baa01c2003-04-09 21:51:34 +00008#include "llvm/Instructions.h"
Guochun Shif1c154f2003-03-27 17:57:44 +00009#include "llvm/Type.h"
10#include "llvm/CodeGen/MachineCodeForInstruction.h"
11#include "llvm/CodeGen/MachineInstr.h"
Guochun Shi6fbe5fb2003-04-06 23:56:19 +000012#include "llvm/Target/TargetSchedInfo.h"
Misha Brukman8baa01c2003-04-09 21:51:34 +000013#include "Support/StringExtras.h"
14#include "Support/STLExtras.h"
Misha Brukman2c821cc2003-04-10 19:19:23 +000015#include "Support/hash_map"
16#include "Support/Statistic.h"
17#include "ModuloScheduling.h"
Misha Brukman8baa01c2003-04-09 21:51:34 +000018#include "ModuloSchedGraph.h"
19#include <algorithm>
Misha Brukman2c821cc2003-04-10 19:19:23 +000020#include <ostream>
21#include <vector>
Misha Brukman2c821cc2003-04-10 19:19:23 +000022// FIXME: Should be using #include <cmath>
Misha Brukman8baa01c2003-04-09 21:51:34 +000023#include <math.h>
24//#include <swig.h>
Guochun Shif1c154f2003-03-27 17:57:44 +000025
26#define UNIDELAY 1
Guochun Shif1c154f2003-03-27 17:57:44 +000027
Guochun Shif1c154f2003-03-27 17:57:44 +000028//*********************** 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 Brukman2c821cc2003-04-10 19:19:23 +000033struct RefVec:public std::vector<std::pair<ModuloSchedGraphNode*,int> > {
Misha Brukman8baa01c2003-04-09 21:51:34 +000034 typedef std::vector<std::pair<ModuloSchedGraphNode*,
35 int> >::iterator iterator;
36 typedef std::vector<std::pair<ModuloSchedGraphNode*,
37 int> >::const_iterator const_iterator;
Guochun Shif1c154f2003-03-27 17:57:44 +000038};
39
Misha Brukman2c821cc2003-04-10 19:19:23 +000040struct 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 Shif1c154f2003-03-27 17:57:44 +000043};
44
Misha Brukman2c821cc2003-04-10 19:19:23 +000045struct ValueToDefVecMap:public hash_map<const Instruction*,RefVec> {
46 typedef hash_map<const Instruction*, RefVec>::iterator iterator;
47 typedef hash_map<const Instruction*,
Misha Brukman8baa01c2003-04-09 21:51:34 +000048 RefVec>::const_iterator const_iterator;
Guochun Shif1c154f2003-03-27 17:57:44 +000049};
50
Guochun Shif1c154f2003-03-27 17:57:44 +000051// class Modulo SchedGraphNode
52
Guochun Shi099b0642003-06-02 17:48:56 +000053ModuloSchedGraphNode::ModuloSchedGraphNode(unsigned int in_nodeId,
54 const BasicBlock * in_bb,
55 const Instruction * in_inst,
Misha Brukman8baa01c2003-04-09 21:51:34 +000056 int indexInBB,
57 const TargetMachine & target)
Guochun Shi099b0642003-06-02 17:48:56 +000058:SchedGraphNodeCommon(in_nodeId, indexInBB), inst(in_inst)
Guochun Shif1c154f2003-03-27 17:57:44 +000059{
Misha Brukman8baa01c2003-04-09 21:51:34 +000060 if (inst) {
61 //FIXME: find the latency
62 //currently setthe latency to zero
63 latency = 0;
64 }
Guochun Shif1c154f2003-03-27 17:57:44 +000065}
Misha Brukman8baa01c2003-04-09 21:51:34 +000066
Guochun Shif1c154f2003-03-27 17:57:44 +000067//class ModuloScheGraph
68
Misha Brukman8baa01c2003-04-09 21:51:34 +000069void ModuloSchedGraph::addDummyEdges()
Guochun Shif1c154f2003-03-27 17:57:44 +000070{
71 assert(graphRoot->outEdges.size() == 0);
Misha Brukman8baa01c2003-04-09 21:51:34 +000072
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 Shif1c154f2003-03-27 17:57:44 +000083}
84
Misha Brukman8baa01c2003-04-09 21:51:34 +000085bool isDefinition(const Instruction *I)
Guochun Shif1c154f2003-03-27 17:57:44 +000086{
87 //if(TerminatorInst::classof(I)||FreeInst::classof(I) || StoreInst::classof(I) || CallInst::classof(I))
Misha Brukman8baa01c2003-04-09 21:51:34 +000088 if (!I->hasName())
Guochun Shif1c154f2003-03-27 17:57:44 +000089 return false;
90 else
91 return true;
92}
93
Misha Brukman8baa01c2003-04-09 21:51:34 +000094void ModuloSchedGraph::addDefUseEdges(const BasicBlock *bb)
Guochun Shif1c154f2003-03-27 17:57:44 +000095{
96 //collect def instructions, store them in vector
Guochun Shi6fbe5fb2003-04-06 23:56:19 +000097 // const TargetInstrInfo& mii = target.getInstrInfo();
Misha Brukman8baa01c2003-04-09 21:51:34 +000098 const TargetInstrInfo & mii = target.getInstrInfo();
99
100 typedef std::vector < ModuloSchedGraphNode * >DefVec;
Guochun Shif1c154f2003-03-27 17:57:44 +0000101 DefVec defVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000102
Guochun Shif1c154f2003-03-27 17:57:44 +0000103 //find those def instructions
Misha Brukman8baa01c2003-04-09 21:51:34 +0000104 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 Brukman63e04f32003-04-22 23:00:08 +0000113 //for each use of a def, add a flow edge from the def instruction to the
114 //ref instruction
Misha Brukman8baa01c2003-04-09 21:51:34 +0000115
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 Shif1c154f2003-03-27 17:57:44 +0000130 {
Misha Brukman8baa01c2003-04-09 21:51:34 +0000131 } 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 Shif1c154f2003-03-27 17:57:44 +0000162 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000163
Misha Brukman8baa01c2003-04-09 21:51:34 +0000164 }
165 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000166}
167
Misha Brukman8baa01c2003-04-09 21:51:34 +0000168void 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 Shif1c154f2003-03-27 17:57:44 +0000183 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000184
185 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000186}
187
Misha Brukman8baa01c2003-04-09 21:51:34 +0000188static const int SG_LOAD_REF = 0;
Guochun Shif1c154f2003-03-27 17:57:44 +0000189static const int SG_STORE_REF = 1;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000190static const int SG_CALL_REF = 2;
Guochun Shif1c154f2003-03-27 17:57:44 +0000191
192static const unsigned int SG_DepOrderArray[][3] = {
Misha Brukman8baa01c2003-04-09 21:51:34 +0000193 {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 Shif1c154f2003-03-27 17:57:44 +0000203};
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 Brukman8baa01c2003-04-09 21:51:34 +0000211void ModuloSchedGraph::addMemEdges(const BasicBlock * bb) {
212
213 std::vector<ModuloSchedGraphNode*> memNodeVec;
Guochun Shif1c154f2003-03-27 17:57:44 +0000214
215 //construct the memNodeVec
Misha Brukman8baa01c2003-04-09 21:51:34 +0000216 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 Shif1c154f2003-03-27 17:57:44 +0000224 // 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 Brukman8baa01c2003-04-09 21:51:34 +0000227 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 Shif1c154f2003-03-27 17:57:44 +0000247 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000248 }
249}
Guochun Shif1c154f2003-03-27 17:57:44 +0000250
251
252
Misha Brukman8baa01c2003-04-09 21:51:34 +0000253void ModuloSchedGraph::buildNodesforBB(const TargetMachine &target,
254 const BasicBlock *bb,
255 std::vector<ModuloSchedGraphNode*> &memNode,
256 RegToRefVecMap &regToRefVecMap,
257 ValueToDefVecMap &valueToDefVecMap)
Guochun Shif1c154f2003-03-27 17:57:44 +0000258{
Guochun Shi6fbe5fb2003-04-06 23:56:19 +0000259 //const TargetInstrInfo& mii=target.getInstrInfo();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000260
Guochun Shif1c154f2003-03-27 17:57:44 +0000261 //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 Brukman8baa01c2003-04-09 21:51:34 +0000263
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 Shif1c154f2003-03-27 17:57:44 +0000272
273 //this function finds some info about instruction in basic block for later use
Misha Brukman8baa01c2003-04-09 21:51:34 +0000274 //findDefUseInfoAtInstr(target, node,
275 //memNode,regToRefVecMap,valueToDefVecMap);
Guochun Shif1c154f2003-03-27 17:57:44 +0000276}
277
278
Misha Brukman8baa01c2003-04-09 21:51:34 +0000279bool ModuloSchedGraph::isLoop(const BasicBlock *bb) {
Guochun Shif1c154f2003-03-27 17:57:44 +0000280 //only if the last instruction in the basicblock is branch instruction and
281 //there is at least an option to branch itself
282
Misha Brukman8baa01c2003-04-09 21:51:34 +0000283 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 Shif1c154f2003-03-27 17:57:44 +0000293 return false;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000294
Guochun Shif1c154f2003-03-27 17:57:44 +0000295}
Misha Brukman8baa01c2003-04-09 21:51:34 +0000296
297bool ModuloSchedGraph::isLoop() {
Guochun Shif1c154f2003-03-27 17:57:44 +0000298 //only if the last instruction in the basicblock is branch instruction and
299 //there is at least an option to branch itself
Guochun Shif1c154f2003-03-27 17:57:44 +0000300
Guochun Shi099b0642003-06-02 17:48:56 +0000301 assert(this->bb&& "the basicblock is not empty");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000302 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 Shif1c154f2003-03-27 17:57:44 +0000309 }
Guochun Shi099b0642003-06-02 17:48:56 +0000310
Misha Brukman8baa01c2003-04-09 21:51:34 +0000311 return false;
312
313}
314
315void 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 Shif1c154f2003-03-27 17:57:44 +0000331 node->setPropertyComputed(true);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000332 continue;
Guochun Shif1c154f2003-03-27 17:57:44 +0000333 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000334 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 Shif1c154f2003-03-27 17:57:44 +0000348}
349
Misha Brukman8baa01c2003-04-09 21:51:34 +0000350void 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 Shif1c154f2003-03-27 17:57:44 +0000359
360 //cerr<<"maxASAP is "<<maxASAP<<"\n";
Misha Brukman8baa01c2003-04-09 21:51:34 +0000361
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
383void 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
395void 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 Shif1c154f2003-03-27 17:57:44 +0000408 node->setPropertyComputed(true);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000409 continue;
Guochun Shif1c154f2003-03-27 17:57:44 +0000410 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000411 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 Shif1c154f2003-03-27 17:57:44 +0000420 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000421 node->setPropertyComputed(true);
422 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000423
424}
425
426
Misha Brukman8baa01c2003-04-09 21:51:34 +0000427void ModuloSchedGraph::computeNodeHeight(const BasicBlock *bb)
Guochun Shif1c154f2003-03-27 17:57:44 +0000428{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000429 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 Shif1c154f2003-03-27 17:57:44 +0000449 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000450 node->setPropertyComputed(true);
451
452 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000453
454}
455
Misha Brukman8baa01c2003-04-09 21:51:34 +0000456void ModuloSchedGraph::computeNodeProperty(const BasicBlock * bb)
Guochun Shif1c154f2003-03-27 17:57:44 +0000457{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000458 //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 Shif1c154f2003-03-27 17:57:44 +0000462 this->computeNodeASAP(bb);
463 this->computeNodeALAP(bb);
464 this->computeNodeMov(bb);
465 this->computeNodeDepth(bb);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000466 this->computeNodeHeight(bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000467}
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 Brukman8baa01c2003-04-09 21:51:34 +0000472std::vector<ModuloSchedGraphNode*>
473ModuloSchedGraph::predSet(std::vector<ModuloSchedGraphNode*> set,
474 unsigned backEdgeSrc,
475 unsigned
476 backEdgeSink)
Guochun Shif1c154f2003-03-27 17:57:44 +0000477{
478 std::vector<ModuloSchedGraphNode*> predS;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000479 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 Shif1c154f2003-03-27 17:57:44 +0000505 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000506 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000507 return predS;
508}
509
510ModuloSchedGraph::NodeVec ModuloSchedGraph::predSet(NodeVec set)
511{
512 //node number increases from 2
Misha Brukman8baa01c2003-04-09 21:51:34 +0000513 return predSet(set, 0, 0);
Guochun Shif1c154f2003-03-27 17:57:44 +0000514}
515
Misha Brukman8baa01c2003-04-09 21:51:34 +0000516std::vector <ModuloSchedGraphNode*>
517ModuloSchedGraph::predSet(ModuloSchedGraphNode *_node,
518 unsigned backEdgeSrc, unsigned backEdgeSink)
Guochun Shif1c154f2003-03-27 17:57:44 +0000519{
Guochun Shif1c154f2003-03-27 17:57:44 +0000520 std::vector<ModuloSchedGraphNode*> set;
521 set.push_back(_node);
522 return predSet(set, backEdgeSrc, backEdgeSink);
Guochun Shif1c154f2003-03-27 17:57:44 +0000523}
524
Misha Brukman8baa01c2003-04-09 21:51:34 +0000525std::vector <ModuloSchedGraphNode*>
526ModuloSchedGraph::predSet(ModuloSchedGraphNode * _node)
Guochun Shif1c154f2003-03-27 17:57:44 +0000527{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000528 return predSet(_node, 0, 0);
Guochun Shif1c154f2003-03-27 17:57:44 +0000529}
530
Misha Brukman8baa01c2003-04-09 21:51:34 +0000531std::vector<ModuloSchedGraphNode*>
532ModuloSchedGraph::succSet(std::vector<ModuloSchedGraphNode*> set,
533 unsigned src, unsigned sink)
Guochun Shif1c154f2003-03-27 17:57:44 +0000534{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000535 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 Shif1c154f2003-03-27 17:57:44 +0000553
Misha Brukman8baa01c2003-04-09 21:51:34 +0000554 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 Shif1c154f2003-03-27 17:57:44 +0000561 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000562 }
563 return succS;
Guochun Shif1c154f2003-03-27 17:57:44 +0000564}
565
566ModuloSchedGraph::NodeVec ModuloSchedGraph::succSet(NodeVec set)
567{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000568 return succSet(set, 0, 0);
Guochun Shif1c154f2003-03-27 17:57:44 +0000569}
570
571
Misha Brukman8baa01c2003-04-09 21:51:34 +0000572std::vector<ModuloSchedGraphNode*>
573ModuloSchedGraph::succSet(ModuloSchedGraphNode *_node,
574 unsigned src, unsigned sink)
Guochun Shif1c154f2003-03-27 17:57:44 +0000575{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000576 std::vector<ModuloSchedGraphNode*>set;
Guochun Shif1c154f2003-03-27 17:57:44 +0000577 set.push_back(_node);
578 return succSet(set, src, sink);
Guochun Shif1c154f2003-03-27 17:57:44 +0000579}
580
Misha Brukman8baa01c2003-04-09 21:51:34 +0000581std::vector<ModuloSchedGraphNode*>
582ModuloSchedGraph::succSet(ModuloSchedGraphNode * _node)
Guochun Shif1c154f2003-03-27 17:57:44 +0000583{
584 return succSet(_node, 0, 0);
585}
586
Misha Brukman8baa01c2003-04-09 21:51:34 +0000587SchedGraphEdge *ModuloSchedGraph::getMaxDelayEdge(unsigned srcId,
588 unsigned sinkId)
Guochun Shif1c154f2003-03-27 17:57:44 +0000589{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000590 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 Shif1c154f2003-03-27 17:57:44 +0000603 return maxDelayEdge;
604}
605
606void ModuloSchedGraph::dumpCircuits()
607{
Misha Brukman2c821cc2003-04-10 19:19:23 +0000608 DEBUG(std::cerr << "dumping circuits for graph:\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000609 int j = -1;
610 while (circuits[++j][0] != 0) {
611 int k = -1;
612 while (circuits[j][++k] != 0)
Misha Brukman2c821cc2003-04-10 19:19:23 +0000613 DEBUG(std::cerr << circuits[j][k] << "\t");
614 DEBUG(std::cerr << "\n");
Guochun Shif1c154f2003-03-27 17:57:44 +0000615 }
616}
617
Misha Brukman8baa01c2003-04-09 21:51:34 +0000618void ModuloSchedGraph::dumpSet(std::vector < ModuloSchedGraphNode * >set)
Guochun Shif1c154f2003-03-27 17:57:44 +0000619{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000620 for (unsigned i = 0; i < set.size(); i++)
Misha Brukman2c821cc2003-04-10 19:19:23 +0000621 DEBUG(std::cerr << set[i]->getNodeId() << "\t");
622 DEBUG(std::cerr << "\n");
Guochun Shif1c154f2003-03-27 17:57:44 +0000623}
624
Misha Brukman8baa01c2003-04-09 21:51:34 +0000625std::vector<ModuloSchedGraphNode*>
626ModuloSchedGraph::vectorUnion(std::vector<ModuloSchedGraphNode*> set1,
627 std::vector<ModuloSchedGraphNode*> set2)
Guochun Shif1c154f2003-03-27 17:57:44 +0000628{
629 std::vector<ModuloSchedGraphNode*> unionVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000630 for (unsigned i = 0; i < set1.size(); i++)
Guochun Shif1c154f2003-03-27 17:57:44 +0000631 unionVec.push_back(set1[i]);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000632 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 Shif1c154f2003-03-27 17:57:44 +0000639 }
640 return unionVec;
641}
Misha Brukman8baa01c2003-04-09 21:51:34 +0000642
643std::vector<ModuloSchedGraphNode*>
644ModuloSchedGraph::vectorConj(std::vector<ModuloSchedGraphNode*> set1,
645 std::vector<ModuloSchedGraphNode*> set2)
Guochun Shif1c154f2003-03-27 17:57:44 +0000646{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000647 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 Shif1c154f2003-03-27 17:57:44 +0000653}
654
Misha Brukman8baa01c2003-04-09 21:51:34 +0000655ModuloSchedGraph::NodeVec ModuloSchedGraph::vectorSub(NodeVec set1,
656 NodeVec set2)
Guochun Shif1c154f2003-03-27 17:57:44 +0000657{
658 NodeVec newVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000659 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 Shif1c154f2003-03-27 17:57:44 +0000668 }
669 return newVec;
670}
Guochun Shif1c154f2003-03-27 17:57:44 +0000671
Misha Brukman8baa01c2003-04-09 21:51:34 +0000672void ModuloSchedGraph::orderNodes() {
673 oNodes.clear();
674
675 std::vector < ModuloSchedGraphNode * >set;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000676 unsigned numNodes = bb->size();
677
Misha Brukman2c821cc2003-04-10 19:19:23 +0000678 // first order all the sets
Misha Brukman8baa01c2003-04-09 21:51:34 +0000679 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 Shif1c154f2003-03-27 17:57:44 +0000688 unsigned nextNodeId;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000689 nextNodeId =
690 circuits[j][k + 1] != 0 ? circuits[j][k + 1] : circuits[j][0];
691 SchedGraphEdge *edge = getMaxDelayEdge(circuits[j][k], nextNodeId);
Guochun Shif1c154f2003-03-27 17:57:44 +0000692 totalDelay += edge->getMinDelay();
693 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000694 if (preDelay != -1 && totalDelay > preDelay) {
Misha Brukman2c821cc2003-04-10 19:19:23 +0000695 // swap circuits[j][] and cuicuits[j-1][]
Guochun Shif1c154f2003-03-27 17:57:44 +0000696 unsigned temp[MAXNODE];
Misha Brukman8baa01c2003-04-09 21:51:34 +0000697 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 Shif1c154f2003-03-27 17:57:44 +0000701 }
702 //restart
Misha Brukman8baa01c2003-04-09 21:51:34 +0000703 j = -1;
Guochun Shif1c154f2003-03-27 17:57:44 +0000704 }
705 }
706
Misha Brukman2c821cc2003-04-10 19:19:23 +0000707 // build the first set
Guochun Shif1c154f2003-03-27 17:57:44 +0000708 int backEdgeSrc;
709 int backEdgeSink;
Misha Brukman2c821cc2003-04-10 19:19:23 +0000710 if (ModuloScheduling::printScheduleProcess())
711 DEBUG(std::cerr << "building the first set" << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000712 int setSeq = -1;
713 int k = -1;
Guochun Shif1c154f2003-03-27 17:57:44 +0000714 setSeq++;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000715 while (circuits[setSeq][++k] != 0)
Guochun Shif1c154f2003-03-27 17:57:44 +0000716 set.push_back(getNode(circuits[setSeq][k]));
Misha Brukman8baa01c2003-04-09 21:51:34 +0000717 if (circuits[setSeq][0] != 0) {
718 backEdgeSrc = circuits[setSeq][k - 1];
719 backEdgeSink = circuits[setSeq][0];
Guochun Shif1c154f2003-03-27 17:57:44 +0000720 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000721 if (ModuloScheduling::printScheduleProcess()) {
722 DEBUG(std::cerr << "the first set is:");
Guochun Shif1c154f2003-03-27 17:57:44 +0000723 dumpSet(set);
724 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000725 // implement the ordering algorithm
Misha Brukman8baa01c2003-04-09 21:51:34 +0000726 enum OrderSeq { bottom_up, top_down };
Guochun Shif1c154f2003-03-27 17:57:44 +0000727 OrderSeq order;
728 std::vector<ModuloSchedGraphNode*> R;
Misha Brukman8baa01c2003-04-09 21:51:34 +0000729 while (!set.empty()) {
Misha Brukman2c821cc2003-04-10 19:19:23 +0000730 std::vector<ModuloSchedGraphNode*> pset = predSet(oNodes);
731 std::vector<ModuloSchedGraphNode*> sset = succSet(oNodes);
Misha Brukman8baa01c2003-04-09 21:51:34 +0000732
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 Shif1c154f2003-03-27 17:57:44 +0000748 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000749 R.push_back(set[position]);
750 order = bottom_up;
751 }
752
753 while (!R.empty()) {
754 if (order == top_down) {
Misha Brukman2c821cc2003-04-10 19:19:23 +0000755 if (ModuloScheduling::printScheduleProcess())
756 DEBUG(std::cerr << "in top_down round\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000757 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 Brukman2c821cc2003-04-10 19:19:23 +0000799 if (ModuloScheduling::printScheduleProcess())
800 DEBUG(std::cerr << "in bottom up round\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000801 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 Shif1c154f2003-03-27 17:57:44 +0000824 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000825 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000826 if (ModuloScheduling::printScheduleProcess()) {
827 DEBUG(std::cerr << "order finished\n");
828 DEBUG(std::cerr << "dumping the ordered nodes:\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000829 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 Brukman2c821cc2003-04-10 19:19:23 +0000835 if (ModuloScheduling::printScheduleProcess())
836 DEBUG(std::cerr << "building the next set\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000837 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 Brukman2c821cc2003-04-10 19:19:23 +0000849 if (ModuloScheduling::printScheduleProcess())
850 DEBUG(std::cerr << "no circuits any more, collect the rest nodes\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000851 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 Shif1c154f2003-03-27 17:57:44 +0000860 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000861 }
Misha Brukman2c821cc2003-04-10 19:19:23 +0000862 if (ModuloScheduling::printScheduleProcess()) {
863 DEBUG(std::cerr << "next set is:\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000864 dumpSet(set);
865 }
866 } //while(!set.empty())
867
Guochun Shif1c154f2003-03-27 17:57:44 +0000868}
869
870
871
Misha Brukman8baa01c2003-04-09 21:51:34 +0000872void ModuloSchedGraph::buildGraph(const TargetMachine & target)
Guochun Shif1c154f2003-03-27 17:57:44 +0000873{
Guochun Shif1c154f2003-03-27 17:57:44 +0000874
Guochun Shi099b0642003-06-02 17:48:56 +0000875 assert(this->bb && "The basicBlock is NULL?");
Guochun Shif1c154f2003-03-27 17:57:44 +0000876
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 Brukman2c821cc2003-04-10 19:19:23 +0000887 std::vector<ModuloSchedGraphNode*> memNodeVec;
Guochun Shif1c154f2003-03-27 17:57:44 +0000888
Misha Brukman8baa01c2003-04-09 21:51:34 +0000889 // Use this data structure to note any uses or definitions of
Guochun Shif1c154f2003-03-27 17:57:44 +0000890 // 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 Shif1c154f2003-03-27 17:57:44 +0000898
Misha Brukman8baa01c2003-04-09 21:51:34 +0000899 // 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 Shif1c154f2003-03-27 17:57:44 +0000907 // 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 Brukman2c821cc2003-04-10 19:19:23 +0000916 if (ModuloScheduling::printScheduleProcess())
Guochun Shif1c154f2003-03-27 17:57:44 +0000917 this->dump(bb);
918
Misha Brukman8baa01c2003-04-09 21:51:34 +0000919 if (!isLoop(bb)) {
Misha Brukman2c821cc2003-04-10 19:19:23 +0000920 DEBUG(std::cerr << " dumping non-loop BB:\n");
Guochun Shif1c154f2003-03-27 17:57:44 +0000921 dump(bb);
922 }
Misha Brukman8baa01c2003-04-09 21:51:34 +0000923 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 Brukman2c821cc2003-04-10 19:19:23 +0000934 if (ModuloScheduling::printScheduleProcess())
935 DEBUG(std::cerr << "ResII is " << ResII << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000936 int RecII = this->computeRecII(bb);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000937 if (ModuloScheduling::printScheduleProcess())
938 DEBUG(std::cerr << "RecII is " << RecII << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +0000939
940 this->MII = std::max(ResII, RecII);
941
942 this->computeNodeProperty(bb);
Misha Brukman2c821cc2003-04-10 19:19:23 +0000943 if (ModuloScheduling::printScheduleProcess())
Misha Brukman8baa01c2003-04-09 21:51:34 +0000944 this->dumpNodeProperty();
945
946 this->orderNodes();
947
Misha Brukman2c821cc2003-04-10 19:19:23 +0000948 if (ModuloScheduling::printScheduleProcess())
Misha Brukman8baa01c2003-04-09 21:51:34 +0000949 this->dump();
950 //this->instrScheduling();
951
952 //this->dumpScheduling();
953 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000954}
955
Misha Brukman8baa01c2003-04-09 21:51:34 +0000956ModuloSchedGraphNode *ModuloSchedGraph::getNode(const unsigned nodeId) const
Guochun Shif1c154f2003-03-27 17:57:44 +0000957{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000958 for (const_iterator I = begin(), E = end(); I != E; I++)
959 if ((*I).second->getNodeId() == nodeId)
960 return (ModuloSchedGraphNode *) (*I).second;
Guochun Shif1c154f2003-03-27 17:57:44 +0000961 return NULL;
962}
963
Misha Brukman8baa01c2003-04-09 21:51:34 +0000964int ModuloSchedGraph::computeRecII(const BasicBlock *bb)
Guochun Shif1c154f2003-03-27 17:57:44 +0000965{
Misha Brukman8baa01c2003-04-09 21:51:34 +0000966 int RecII = 0;
Guochun Shif1c154f2003-03-27 17:57:44 +0000967
968 //os<<"begining computerRecII()"<<"\n";
969
Misha Brukman8baa01c2003-04-09 21:51:34 +0000970 //FIXME: only deal with circuits starting at the first node: the phi node
971 //nodeId=2;
Guochun Shif1c154f2003-03-27 17:57:44 +0000972
973 //search all elementary circuits in the dependance graph
974 //assume maximum number of nodes is MAXNODE
Misha Brukman8baa01c2003-04-09 21:51:34 +0000975
Guochun Shif1c154f2003-03-27 17:57:44 +0000976 unsigned path[MAXNODE];
977 unsigned stack[MAXNODE][MAXNODE];
978
Misha Brukman8baa01c2003-04-09 21:51:34 +0000979 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 Shif1c154f2003-03-27 17:57:44 +0000984 //in our graph, the node number starts at 2
985
986 //number of nodes, because each instruction will result in one node
Misha Brukman8baa01c2003-04-09 21:51:34 +0000987 const unsigned numNodes = bb->size();
Guochun Shif1c154f2003-03-27 17:57:44 +0000988
Misha Brukman8baa01c2003-04-09 21:51:34 +0000989 int i = 0;
990 path[i] = 2;
Guochun Shif1c154f2003-03-27 17:57:44 +0000991
Misha Brukman8baa01c2003-04-09 21:51:34 +0000992 ModuloSchedGraphNode *initNode = getNode(path[0]);
993 unsigned initNodeId = initNode->getNodeId();
994 ModuloSchedGraphNode *currentNode = initNode;
Guochun Shif1c154f2003-03-27 17:57:44 +0000995
Misha Brukman8baa01c2003-04-09 21:51:34 +0000996 while (currentNode != NULL) {
997 unsigned currentNodeId = currentNode->getNodeId();
Misha Brukman2c821cc2003-04-10 19:19:23 +0000998 // DEBUG(std::cerr<<"current node is "<<currentNodeId<<"\n");
Guochun Shif1c154f2003-03-27 17:57:44 +0000999
Misha Brukman8baa01c2003-04-09 21:51:34 +00001000 ModuloSchedGraphNode *nextNode = NULL;
1001 for (ModuloSchedGraphNode::const_iterator I =
1002 currentNode->beginOutEdges(), E = currentNode->endOutEdges();
1003 I != E; I++) {
Misha Brukman2c821cc2003-04-10 19:19:23 +00001004 //DEBUG(std::cerr <<" searching in outgoint edges of node
Misha Brukman8baa01c2003-04-09 21:51:34 +00001005 //"<<currentNodeId<<"\n";
1006 unsigned nodeId = ((SchedGraphEdge *) * I)->getSink()->getNodeId();
1007 bool inpath = false, instack = false;
1008 int k;
Guochun Shif1c154f2003-03-27 17:57:44 +00001009
Misha Brukman2c821cc2003-04-10 19:19:23 +00001010 //DEBUG(std::cerr<<"nodeId is "<<nodeId<<"\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001011
Misha Brukman8baa01c2003-04-09 21:51:34 +00001012 k = -1;
1013 while (path[++k] != 0)
1014 if (nodeId == path[k]) {
1015 inpath = true;
1016 break;
1017 }
Guochun Shif1c154f2003-03-27 17:57:44 +00001018
Misha Brukman8baa01c2003-04-09 21:51:34 +00001019 k = -1;
1020 while (stack[i][++k] != 0)
1021 if (nodeId == stack[i][k]) {
1022 instack = true;
1023 break;
1024 }
Guochun Shif1c154f2003-03-27 17:57:44 +00001025
Misha Brukman8baa01c2003-04-09 21:51:34 +00001026 if (nodeId > currentNodeId && !inpath && !instack) {
1027 nextNode =
1028 (ModuloSchedGraphNode *) ((SchedGraphEdge *) * I)->getSink();
1029 break;
1030 }
Guochun Shif1c154f2003-03-27 17:57:44 +00001031 }
Misha Brukman8baa01c2003-04-09 21:51:34 +00001032
1033 if (nextNode != NULL) {
Misha Brukman2c821cc2003-04-10 19:19:23 +00001034 //DEBUG(std::cerr<<"find the next Node "<<nextNode->getNodeId()<<"\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001035
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 Brukman2c821cc2003-04-10 19:19:23 +00001045 //DEBUG(std::cerr<<"no expansion any more"<<"\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001046 //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 Brukman2c821cc2003-04-10 19:19:23 +00001071 if (ModuloScheduling::printScheduleProcess())
1072 DEBUG(std::cerr << "circuits found are:\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001073 int j = -1;
1074 while (circuits[++j][0] != 0) {
1075 int k = -1;
1076 while (circuits[j][++k] != 0)
Misha Brukman2c821cc2003-04-10 19:19:23 +00001077 if (ModuloScheduling::printScheduleProcess())
1078 DEBUG(std::cerr << circuits[j][k] << "\t");
1079 if (ModuloScheduling::printScheduleProcess())
1080 DEBUG(std::cerr << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001081
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 Brukman2c821cc2003-04-10 19:19:23 +00001109 if (ModuloScheduling::printScheduleProcess())
1110 DEBUG(std::cerr << "The total Delay in the circuit is " << sumDelay
1111 << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001112
1113 RecII = RecII > sumDelay ? RecII : sumDelay;
1114
1115 }
1116 return RecII;
1117 }
1118
1119 }
1120
Guochun Shif1c154f2003-03-27 17:57:44 +00001121 return -1;
1122}
1123
Misha Brukman8baa01c2003-04-09 21:51:34 +00001124void ModuloSchedGraph::addResourceUsage(std::vector<std::pair<int,int> > &ruVec,
1125 int rid)
1126{
Misha Brukman2c821cc2003-04-10 19:19:23 +00001127 //DEBUG(std::cerr<<"\nadding a resouce , current resouceUsage vector size is
Misha Brukman8baa01c2003-04-09 21:51:34 +00001128 //"<<ruVec.size()<<"\n";
1129 bool alreadyExists = false;
1130 for (unsigned i = 0; i < ruVec.size(); i++) {
1131 if (rid == ruVec[i].first) {
Guochun Shif1c154f2003-03-27 17:57:44 +00001132 ruVec[i].second++;
Misha Brukman8baa01c2003-04-09 21:51:34 +00001133 alreadyExists = true;
Guochun Shif1c154f2003-03-27 17:57:44 +00001134 break;
1135 }
1136 }
Misha Brukman8baa01c2003-04-09 21:51:34 +00001137 if (!alreadyExists)
1138 ruVec.push_back(std::make_pair(rid, 1));
Misha Brukman2c821cc2003-04-10 19:19:23 +00001139 //DEBUG(std::cerr<<"current resouceUsage vector size is "<<ruVec.size()<<"\n";
Guochun Shif1c154f2003-03-27 17:57:44 +00001140
1141}
Misha Brukman8baa01c2003-04-09 21:51:34 +00001142void ModuloSchedGraph::dumpResourceUsage(std::vector<std::pair<int,int> > &ru)
Guochun Shif1c154f2003-03-27 17:57:44 +00001143{
Misha Brukman8baa01c2003-04-09 21:51:34 +00001144 TargetSchedInfo & msi = (TargetSchedInfo &) target.getSchedInfo();
1145
Misha Brukman2c821cc2003-04-10 19:19:23 +00001146 std::vector<std::pair<int,int> > resourceNumVector = msi.resourceNumVector;
1147 DEBUG(std::cerr << "resourceID\t" << "resourceNum\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001148 for (unsigned i = 0; i < resourceNumVector.size(); i++)
Misha Brukman2c821cc2003-04-10 19:19:23 +00001149 DEBUG(std::cerr << resourceNumVector[i].
1150 first << "\t" << resourceNumVector[i].second << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001151
Misha Brukman2c821cc2003-04-10 19:19:23 +00001152 DEBUG(std::cerr << " maxNumIssueTotal(issue slot in one cycle) = " << msi.
1153 maxNumIssueTotal << "\n");
1154 DEBUG(std::cerr << "resourceID\t resourceUsage\t ResourceNum\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001155 for (unsigned i = 0; i < ru.size(); i++) {
Misha Brukman2c821cc2003-04-10 19:19:23 +00001156 DEBUG(std::cerr << ru[i].first << "\t" << ru[i].second);
Misha Brukman8baa01c2003-04-09 21:51:34 +00001157 const unsigned resNum = msi.getCPUResourceNum(ru[i].first);
Misha Brukman2c821cc2003-04-10 19:19:23 +00001158 DEBUG(std::cerr << "\t" << resNum << "\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001159 }
1160}
1161
Misha Brukman8baa01c2003-04-09 21:51:34 +00001162int ModuloSchedGraph::computeResII(const BasicBlock * bb)
Guochun Shif1c154f2003-03-27 17:57:44 +00001163{
Misha Brukman8baa01c2003-04-09 21:51:34 +00001164
1165 const TargetInstrInfo & mii = target.getInstrInfo();
1166 const TargetSchedInfo & msi = target.getSchedInfo();
1167
Guochun Shif1c154f2003-03-27 17:57:44 +00001168 int ResII;
Misha Brukman2c821cc2003-04-10 19:19:23 +00001169 std::vector<std::pair<int,int> > resourceUsage;
Misha Brukman8baa01c2003-04-09 21:51:34 +00001170 //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 Brukman2c821cc2003-04-10 19:19:23 +00001177 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 Shif1c154f2003-03-27 17:57:44 +00001181 }
Misha Brukman8baa01c2003-04-09 21:51:34 +00001182 MachineCodeForInstruction & tempMvec =
1183 MachineCodeForInstruction::get(I);
Misha Brukman2c821cc2003-04-10 19:19:23 +00001184 if (ModuloScheduling::printScheduleProcess())
1185 DEBUG(std::cerr << "size =" << tempMvec.size() << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001186 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 Brukman2c821cc2003-04-10 19:19:23 +00001195 std::vector<std::vector<resourceId_t> > resources=rUsage.resourcesByCycle;
Misha Brukman8baa01c2003-04-09 21:51:34 +00001196 assert(totCycles == resources.size());
Misha Brukman2c821cc2003-04-10 19:19:23 +00001197 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 Brukman8baa01c2003-04-09 21:51:34 +00001202 for (unsigned j = 0; j < resources.size(); j++) {
Misha Brukman2c821cc2003-04-10 19:19:23 +00001203 if (ModuloScheduling::printScheduleProcess())
1204 DEBUG(std::cerr << "cycle " << j << ": ");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001205 for (unsigned k = 0; k < resources[j].size(); k++) {
Misha Brukman2c821cc2003-04-10 19:19:23 +00001206 if (ModuloScheduling::printScheduleProcess())
1207 DEBUG(std::cerr << "\t" << resources[j][k]);
Misha Brukman8baa01c2003-04-09 21:51:34 +00001208 addResourceUsage(resourceUsage, resources[j][k]);
1209 }
Misha Brukman2c821cc2003-04-10 19:19:23 +00001210 if (ModuloScheduling::printScheduleProcess())
1211 DEBUG(std::cerr << "\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001212 }
1213 }
1214 }
Misha Brukman2c821cc2003-04-10 19:19:23 +00001215 if (ModuloScheduling::printScheduleProcess())
Guochun Shif1c154f2003-03-27 17:57:44 +00001216 this->dumpResourceUsage(resourceUsage);
1217
1218 //compute ResII
Misha Brukman8baa01c2003-04-09 21:51:34 +00001219 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 Shif1c154f2003-03-27 17:57:44 +00001224 double tempII;
Misha Brukman8baa01c2003-04-09 21:51:34 +00001225 if (resourceNum <= issueSlots)
1226 tempII = ceil(1.0 * useNum / resourceNum);
Guochun Shif1c154f2003-03-27 17:57:44 +00001227 else
Misha Brukman8baa01c2003-04-09 21:51:34 +00001228 tempII = ceil(1.0 * useNum / issueSlots);
1229 ResII = std::max((int) tempII, ResII);
Guochun Shif1c154f2003-03-27 17:57:44 +00001230 }
1231 return ResII;
1232}
1233
Misha Brukman2c821cc2003-04-10 19:19:23 +00001234ModuloSchedGraphSet::ModuloSchedGraphSet(const Function *function,
1235 const TargetMachine &target)
Misha Brukman8baa01c2003-04-09 21:51:34 +00001236: method(function)
Guochun Shif1c154f2003-03-27 17:57:44 +00001237{
Misha Brukman8baa01c2003-04-09 21:51:34 +00001238 buildGraphsForMethod(method, target);
Guochun Shif1c154f2003-03-27 17:57:44 +00001239}
1240
1241
1242ModuloSchedGraphSet::~ModuloSchedGraphSet()
1243{
1244 //delete all the graphs
Misha Brukman8baa01c2003-04-09 21:51:34 +00001245 for (iterator I = begin(), E = end(); I != E; ++I)
Guochun Shif1c154f2003-03-27 17:57:44 +00001246 delete *I;
1247}
1248
Misha Brukman8baa01c2003-04-09 21:51:34 +00001249void ModuloSchedGraphSet::dump() const
Guochun Shif1c154f2003-03-27 17:57:44 +00001250{
Misha Brukman2c821cc2003-04-10 19:19:23 +00001251 DEBUG(std::cerr << " ====== ModuloSched graphs for function `" <<
1252 method->getName() << "' =========\n\n");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001253 for (const_iterator I = begin(); I != end(); ++I)
Guochun Shif1c154f2003-03-27 17:57:44 +00001254 (*I)->dump();
Misha Brukman8baa01c2003-04-09 21:51:34 +00001255
Misha Brukman2c821cc2003-04-10 19:19:23 +00001256 DEBUG(std::cerr << "\n=========End graphs for function `" << method->getName()
1257 << "' ==========\n\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001258}
1259
Misha Brukman8baa01c2003-04-09 21:51:34 +00001260void ModuloSchedGraph::dump(const BasicBlock * bb)
Guochun Shif1c154f2003-03-27 17:57:44 +00001261{
Misha Brukman2c821cc2003-04-10 19:19:23 +00001262 DEBUG(std::cerr << "dumping basic block:");
1263 DEBUG(std::cerr << (bb->hasName()? bb->getName() : "block")
1264 << " (" << bb << ")" << "\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001265}
1266
Misha Brukman8baa01c2003-04-09 21:51:34 +00001267void ModuloSchedGraph::dump(const BasicBlock * bb, std::ostream & os)
Guochun Shif1c154f2003-03-27 17:57:44 +00001268{
Misha Brukman8baa01c2003-04-09 21:51:34 +00001269 os << "dumping basic block:";
1270 os << (bb->hasName()? bb->getName() : "block")
1271 << " (" << bb << ")" << "\n";
Guochun Shif1c154f2003-03-27 17:57:44 +00001272}
1273
Misha Brukman8baa01c2003-04-09 21:51:34 +00001274void ModuloSchedGraph::dump() const
Guochun Shif1c154f2003-03-27 17:57:44 +00001275{
Misha Brukman2c821cc2003-04-10 19:19:23 +00001276 DEBUG(std::cerr << " ModuloSchedGraph for basic Blocks:");
Guochun Shi099b0642003-06-02 17:48:56 +00001277
1278 DEBUG(std::cerr << (bb->hasName()? bb->getName() : "block")
1279 << " (" << bb << ")" << "");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001280
Misha Brukman2c821cc2003-04-10 19:19:23 +00001281 DEBUG(std::cerr << "\n\n Actual Root nodes : ");
Misha Brukman8baa01c2003-04-09 21:51:34 +00001282 for (unsigned i = 0, N = graphRoot->outEdges.size(); i < N; i++)
Misha Brukman2c821cc2003-04-10 19:19:23 +00001283 DEBUG(std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
1284 << ((i == N - 1) ? "" : ", "));
Guochun Shif1c154f2003-03-27 17:57:44 +00001285
Misha Brukman2c821cc2003-04-10 19:19:23 +00001286 DEBUG(std::cerr << "\n Graph Nodes:\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001287 //for (const_iterator I=begin(); I != end(); ++I)
Misha Brukman2c821cc2003-04-10 19:19:23 +00001288 //DEBUG(std::cerr << "\n" << *I->second;
Guochun Shi099b0642003-06-02 17:48:56 +00001289 unsigned numNodes = bb->size();
Misha Brukman8baa01c2003-04-09 21:51:34 +00001290 for (unsigned i = 2; i < numNodes + 2; i++) {
1291 ModuloSchedGraphNode *node = getNode(i);
Misha Brukman2c821cc2003-04-10 19:19:23 +00001292 DEBUG(std::cerr << "\n" << *node);
Misha Brukman8baa01c2003-04-09 21:51:34 +00001293 }
Guochun Shif1c154f2003-03-27 17:57:44 +00001294
Misha Brukman2c821cc2003-04-10 19:19:23 +00001295 DEBUG(std::cerr << "\n");
Guochun Shif1c154f2003-03-27 17:57:44 +00001296}
Misha Brukman8baa01c2003-04-09 21:51:34 +00001297
1298void ModuloSchedGraph::dumpNodeProperty() const
Guochun Shif1c154f2003-03-27 17:57:44 +00001299{
Guochun Shi099b0642003-06-02 17:48:56 +00001300
Misha Brukman8baa01c2003-04-09 21:51:34 +00001301 unsigned numNodes = bb->size();
1302 for (unsigned i = 2; i < numNodes + 2; i++) {
1303 ModuloSchedGraphNode *node = getNode(i);
Misha Brukman2c821cc2003-04-10 19:19:23 +00001304 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 Brukman8baa01c2003-04-09 21:51:34 +00001310 }
Guochun Shif1c154f2003-03-27 17:57:44 +00001311}
1312
Misha Brukman63e04f32003-04-22 23:00:08 +00001313void ModuloSchedGraphSet::buildGraphsForMethod(const Function *F,
1314 const TargetMachine &target)
Guochun Shif1c154f2003-03-27 17:57:44 +00001315{
Guochun Shi099b0642003-06-02 17:48:56 +00001316 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 Shif1c154f2003-03-27 17:57:44 +00001321}
1322
Misha Brukman63e04f32003-04-22 23:00:08 +00001323std::ostream& operator<<(std::ostream &os,
1324 const ModuloSchedGraphNode &node)
Guochun Shif1c154f2003-03-27 17:57:44 +00001325{
1326 os << std::string(8, ' ')
Misha Brukman8baa01c2003-04-09 21:51:34 +00001327 << "Node " << node.nodeId << " : "
1328 << "latency = " << node.latency << "\n" << std::string(12, ' ');
Guochun Shif1c154f2003-03-27 17:57:44 +00001329
1330 if (node.getInst() == NULL)
1331 os << "(Dummy node)\n";
Misha Brukman8baa01c2003-04-09 21:51:34 +00001332 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 Shif1c154f2003-03-27 17:57:44 +00001344 return os;
1345}