1 //===-- ModuloScheduling.cpp - ModuloScheduling ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "ModuloSched"
16 #include "ModuloScheduling.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/CFG.h"
20 #include "llvm/Target/TargetSchedInfo.h"
21 #include "Support/Debug.h"
22 #include "Support/GraphWriter.h"
23 #include "Support/StringExtras.h"
33 /// Create ModuloSchedulingPass
35 FunctionPass *llvm::createModuloSchedulingPass(TargetMachine & targ) {
36 DEBUG(std::cerr << "Created ModuloSchedulingPass\n");
37 return new ModuloSchedulingPass(targ);
40 template<typename GraphType>
41 static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
42 const GraphType >) {
43 std::string Filename = GraphName + ".dot";
44 O << "Writing '" << Filename << "'...";
45 std::ofstream F(Filename.c_str());
50 O << " error opening file for writing!";
57 struct DOTGraphTraits<MSchedGraph*> : public DefaultDOTGraphTraits {
58 static std::string getGraphName(MSchedGraph *F) {
59 return "Dependence Graph";
62 static std::string getNodeLabel(MSchedGraphNode *Node, MSchedGraph *Graph) {
63 if (Node->getInst()) {
65 ss << *(Node->getInst());
66 return ss.str(); //((MachineInstr*)Node->getInst());
71 static std::string getEdgeSourceLabel(MSchedGraphNode *Node,
72 MSchedGraphNode::succ_iterator I) {
73 //Label each edge with the type of dependence
74 std::string edgelabel = "";
75 switch (I.getEdge().getDepOrderType()) {
77 case MSchedGraphEdge::TrueDep:
81 case MSchedGraphEdge::AntiDep:
85 case MSchedGraphEdge::OutputDep:
90 edgelabel = "Unknown";
95 int iteDiff = I.getEdge().getIteDiff();
96 std::string intStr = "(IteDiff: ";
97 intStr += itostr(iteDiff);
110 /// ModuloScheduling::runOnFunction - main transformation entry point
111 bool ModuloSchedulingPass::runOnFunction(Function &F) {
112 bool Changed = false;
114 DEBUG(std::cerr << "Creating ModuloSchedGraph for each BasicBlock in" + F.getName() + "\n");
116 //Get MachineFunction
117 MachineFunction &MF = MachineFunction::get(&F);
119 //Iterate over BasicBlocks and do ModuloScheduling if they are valid
120 for (MachineFunction::const_iterator BI = MF.begin(); BI != MF.end(); ++BI) {
121 if(MachineBBisValid(BI)) {
122 MSchedGraph *MSG = new MSchedGraph(BI, target);
124 //Write Graph out to file
125 DEBUG(WriteGraphToFile(std::cerr, F.getName(), MSG));
127 //Print out BB for debugging
128 DEBUG(BI->print(std::cerr));
130 //Calculate Resource II
131 int ResMII = calculateResMII(BI);
133 //Calculate Recurrence II
134 int RecMII = calculateRecMII(MSG, ResMII);
136 II = std::max(RecMII, ResMII);
138 DEBUG(std::cerr << "II starts out as " << II << "\n");
140 //Calculate Node Properties
141 calculateNodeAttributes(MSG, ResMII);
143 //Dump node properties if in debug mode
144 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I !=E; ++I) {
145 DEBUG(std::cerr << "Node: " << *(I->first) << " ASAP: " << I->second.ASAP << " ALAP: " << I->second.ALAP << " MOB: " << I->second.MOB << " Depth: " << I->second.depth << " Height: " << I->second.height << "\n");
148 //Put nodes in order to schedule them
149 computePartialOrder();
151 //Dump out partial order
152 for(std::vector<std::vector<MSchedGraphNode*> >::iterator I = partialOrder.begin(), E = partialOrder.end(); I !=E; ++I) {
153 DEBUG(std::cerr << "Start set in PO\n");
154 for(std::vector<MSchedGraphNode*>::iterator J = I->begin(), JE = I->end(); J != JE; ++J)
155 DEBUG(std::cerr << "PO:" << **J << "\n");
160 //Dump out order of nodes
161 for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(), E = FinalNodeOrder.end(); I != E; ++I)
162 DEBUG(std::cerr << "FO:" << **I << "\n");
165 //Finally schedule nodes
169 //Dump out final schedule
170 //std::cerr << "FINALSCHEDULE\n";
171 //Dump out current schedule
172 /*for(std::map<unsigned, std::vector<std::pair<unsigned, MSchedGraphNode*> > >::iterator J = schedule.begin(),
173 JE = schedule.end(); J != JE; ++J) {
174 std::cerr << "Cycle " << J->first << ":\n";
175 for(std::vector<std::pair<unsigned, MSchedGraphNode*> >::iterator VI = J->second.begin(), VE = J->second.end(); VI != VE; ++VI)
176 std::cerr << "Resource ID: " << VI->first << " by node " << *(VI->second) << "\n";
178 std::cerr << "END FINAL SCHEDULE\n";
180 DEBUG(std::cerr << "II ends up as " << II << "\n");
184 nodeToAttributesMap.clear();
185 partialOrder.clear();
186 recurrenceList.clear();
187 FinalNodeOrder.clear();
198 bool ModuloSchedulingPass::MachineBBisValid(const MachineBasicBlock *BI) {
200 //Valid basic blocks must be loops and can not have if/else statements or calls.
203 //Check first if its a valid loop
204 for(succ_const_iterator I = succ_begin(BI->getBasicBlock()),
205 E = succ_end(BI->getBasicBlock()); I != E; ++I) {
206 if (*I == BI->getBasicBlock()) // has single block loop
211 DEBUG(std::cerr << "Basic Block is not a loop\n");
215 DEBUG(std::cerr << "Basic Block is a loop\n");
217 //Get Target machine instruction info
218 /*const TargetInstrInfo& TMI = targ.getInstrInfo();
220 //Check each instruction and look for calls or if/else statements
222 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
223 //Get opcode to check instruction type
224 MachineOpCode OC = I->getOpcode();
225 if(TMI.isControlFlow(OC) && (count+1 < BI->size()))
233 //ResMII is calculated by determining the usage count for each resource
234 //and using the maximum.
235 //FIXME: In future there should be a way to get alternative resources
236 //for each instruction
237 int ModuloSchedulingPass::calculateResMII(const MachineBasicBlock *BI) {
239 const TargetInstrInfo & mii = target.getInstrInfo();
240 const TargetSchedInfo & msi = target.getSchedInfo();
244 //Map to keep track of usage count of each resource
245 std::map<unsigned, unsigned> resourceUsageCount;
247 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
249 //Get resource usage for this instruction
250 InstrRUsage rUsage = msi.getInstrRUsage(I->getOpcode());
251 std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle;
253 //Loop over resources in each cycle and increments their usage count
254 for(unsigned i=0; i < resources.size(); ++i)
255 for(unsigned j=0; j < resources[i].size(); ++j) {
256 if( resourceUsageCount.find(resources[i][j]) == resourceUsageCount.end()) {
257 resourceUsageCount[resources[i][j]] = 1;
260 resourceUsageCount[resources[i][j]] = resourceUsageCount[resources[i][j]] + 1;
265 //Find maximum usage count
267 //Get max number of instructions that can be issued at once. (FIXME)
268 int issueSlots = 1; // msi.maxNumIssueTotal;
270 for(std::map<unsigned,unsigned>::iterator RB = resourceUsageCount.begin(), RE = resourceUsageCount.end(); RB != RE; ++RB) {
271 //Get the total number of the resources in our cpu
272 //int resourceNum = msi.getCPUResourceNum(RB->first);
274 //Get total usage count for this resources
275 unsigned usageCount = RB->second;
277 //Divide the usage count by either the max number we can issue or the number of
278 //resources (whichever is its upper bound)
279 double finalUsageCount;
280 //if( resourceNum <= issueSlots)
281 //finalUsageCount = ceil(1.0 * usageCount / resourceNum);
283 finalUsageCount = ceil(1.0 * usageCount / issueSlots);
286 DEBUG(std::cerr << "Resource ID: " << RB->first << " (usage=" << usageCount << ", resourceNum=X" << ", issueSlots=" << issueSlots << ", finalUsage=" << finalUsageCount << ")\n");
288 //Only keep track of the max
289 ResMII = std::max( (int) finalUsageCount, ResMII);
293 DEBUG(std::cerr << "Final Resource MII: " << ResMII << "\n");
299 int ModuloSchedulingPass::calculateRecMII(MSchedGraph *graph, int MII) {
300 std::vector<MSchedGraphNode*> vNodes;
301 //Loop over all nodes in the graph
302 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
303 findAllReccurrences(I->second, vNodes, MII);
309 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator I = recurrenceList.begin(), E=recurrenceList.end(); I !=E; ++I) {
310 std::cerr << "Recurrence: \n";
311 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
312 std::cerr << **N << "\n";
314 RecMII = std::max(RecMII, I->first);
315 std::cerr << "End Recurrence with RecMII: " << I->first << "\n";
317 DEBUG(std::cerr << "RecMII: " << RecMII << "\n");
322 void ModuloSchedulingPass::calculateNodeAttributes(MSchedGraph *graph, int MII) {
324 //Loop over the nodes and add them to the map
325 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
326 //Assert if its already in the map
327 assert(nodeToAttributesMap.find(I->second) == nodeToAttributesMap.end() && "Node attributes are already in the map");
329 //Put into the map with default attribute values
330 nodeToAttributesMap[I->second] = MSNodeAttributes();
333 //Create set to deal with reccurrences
334 std::set<MSchedGraphNode*> visitedNodes;
336 //Now Loop over map and calculate the node attributes
337 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
338 calculateASAP(I->first, MII, (MSchedGraphNode*) 0);
339 visitedNodes.clear();
342 int maxASAP = findMaxASAP();
343 //Calculate ALAP which depends on ASAP being totally calculated
344 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
345 calculateALAP(I->first, MII, maxASAP, (MSchedGraphNode*) 0);
346 visitedNodes.clear();
349 //Calculate MOB which depends on ASAP being totally calculated, also do depth and height
350 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
351 (I->second).MOB = std::max(0,(I->second).ALAP - (I->second).ASAP);
353 DEBUG(std::cerr << "MOB: " << (I->second).MOB << " (" << *(I->first) << ")\n");
354 calculateDepth(I->first, (MSchedGraphNode*) 0);
355 calculateHeight(I->first, (MSchedGraphNode*) 0);
361 bool ModuloSchedulingPass::ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode) {
362 if(destNode == 0 || srcNode ==0)
365 bool findEdge = edgesToIgnore.count(std::make_pair(srcNode, destNode->getInEdgeNum(srcNode)));
366 DEBUG(std::cerr << "Ignore Edge from " << *srcNode << " to " << *destNode << "? " << findEdge << "\n");
370 int ModuloSchedulingPass::calculateASAP(MSchedGraphNode *node, int MII, MSchedGraphNode *destNode) {
372 DEBUG(std::cerr << "Calculating ASAP for " << *node << "\n");
374 //Get current node attributes
375 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
377 if(attributes.ASAP != -1)
378 return attributes.ASAP;
380 int maxPredValue = 0;
382 //Iterate over all of the predecessors and find max
383 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
385 //Only process if we are not ignoring the edge
386 if(!ignoreEdge(*P, node)) {
388 predASAP = calculateASAP(*P, MII, node);
390 assert(predASAP != -1 && "ASAP has not been calculated");
391 int iteDiff = node->getInEdge(*P).getIteDiff();
393 int currentPredValue = predASAP + (*P)->getLatency() - (iteDiff * MII);
394 DEBUG(std::cerr << "pred ASAP: " << predASAP << ", iteDiff: " << iteDiff << ", PredLatency: " << (*P)->getLatency() << ", Current ASAP pred: " << currentPredValue << "\n");
395 maxPredValue = std::max(maxPredValue, currentPredValue);
399 attributes.ASAP = maxPredValue;
401 DEBUG(std::cerr << "ASAP: " << attributes.ASAP << " (" << *node << ")\n");
407 int ModuloSchedulingPass::calculateALAP(MSchedGraphNode *node, int MII,
408 int maxASAP, MSchedGraphNode *srcNode) {
410 DEBUG(std::cerr << "Calculating ALAP for " << *node << "\n");
412 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
414 if(attributes.ALAP != -1)
415 return attributes.ALAP;
417 if(node->hasSuccessors()) {
419 //Trying to deal with the issue where the node has successors, but
420 //we are ignoring all of the edges to them. So this is my hack for
421 //now.. there is probably a more elegant way of doing this (FIXME)
422 bool processedOneEdge = false;
424 //FIXME, set to something high to start
425 int minSuccValue = 9999999;
427 //Iterate over all of the predecessors and fine max
428 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
429 E = node->succ_end(); P != E; ++P) {
431 //Only process if we are not ignoring the edge
432 if(!ignoreEdge(node, *P)) {
433 processedOneEdge = true;
435 succALAP = calculateALAP(*P, MII, maxASAP, node);
437 assert(succALAP != -1 && "Successors ALAP should have been caclulated");
439 int iteDiff = P.getEdge().getIteDiff();
441 int currentSuccValue = succALAP - node->getLatency() + iteDiff * MII;
443 DEBUG(std::cerr << "succ ALAP: " << succALAP << ", iteDiff: " << iteDiff << ", SuccLatency: " << (*P)->getLatency() << ", Current ALAP succ: " << currentSuccValue << "\n");
445 minSuccValue = std::min(minSuccValue, currentSuccValue);
450 attributes.ALAP = minSuccValue;
453 attributes.ALAP = maxASAP;
456 attributes.ALAP = maxASAP;
458 DEBUG(std::cerr << "ALAP: " << attributes.ALAP << " (" << *node << ")\n");
460 if(attributes.ALAP < 0)
463 return attributes.ALAP;
466 int ModuloSchedulingPass::findMaxASAP() {
469 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
470 E = nodeToAttributesMap.end(); I != E; ++I)
471 maxASAP = std::max(maxASAP, I->second.ASAP);
476 int ModuloSchedulingPass::calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode) {
478 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
480 if(attributes.height != -1)
481 return attributes.height;
485 //Iterate over all of the predecessors and find max
486 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
487 E = node->succ_end(); P != E; ++P) {
490 if(!ignoreEdge(node, *P)) {
491 int succHeight = calculateHeight(*P, node);
493 assert(succHeight != -1 && "Successors Height should have been caclulated");
495 int currentHeight = succHeight + node->getLatency();
496 maxHeight = std::max(maxHeight, currentHeight);
499 attributes.height = maxHeight;
500 DEBUG(std::cerr << "Height: " << attributes.height << " (" << *node << ")\n");
505 int ModuloSchedulingPass::calculateDepth(MSchedGraphNode *node,
506 MSchedGraphNode *destNode) {
508 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
510 if(attributes.depth != -1)
511 return attributes.depth;
515 //Iterate over all of the predecessors and fine max
516 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
518 if(!ignoreEdge(*P, node)) {
520 predDepth = calculateDepth(*P, node);
522 assert(predDepth != -1 && "Predecessors ASAP should have been caclulated");
524 int currentDepth = predDepth + (*P)->getLatency();
525 maxDepth = std::max(maxDepth, currentDepth);
528 attributes.depth = maxDepth;
530 DEBUG(std::cerr << "Depth: " << attributes.depth << " (" << *node << "*)\n");
536 void ModuloSchedulingPass::addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode *srcBENode, MSchedGraphNode *destBENode) {
537 //Check to make sure that this recurrence is unique
541 //Loop over all recurrences already in our list
542 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator R = recurrenceList.begin(), RE = recurrenceList.end(); R != RE; ++R) {
544 bool all_same = true;
546 if(R->second.size() == recurrence.size()) {
548 for(std::vector<MSchedGraphNode*>::const_iterator node = R->second.begin(), end = R->second.end(); node != end; ++node) {
549 if(find(recurrence.begin(), recurrence.end(), *node) == recurrence.end()) {
550 all_same = all_same && false;
554 all_same = all_same && true;
564 //if(srcBENode == 0 || destBENode == 0) {
565 srcBENode = recurrence.back();
566 destBENode = recurrence.front();
568 DEBUG(std::cerr << "Back Edge to Remove: " << *srcBENode << " to " << *destBENode << "\n");
569 edgesToIgnore.insert(std::make_pair(srcBENode, destBENode->getInEdgeNum(srcBENode)));
570 recurrenceList.insert(std::make_pair(II, recurrence));
575 void ModuloSchedulingPass::findAllReccurrences(MSchedGraphNode *node,
576 std::vector<MSchedGraphNode*> &visitedNodes,
579 if(find(visitedNodes.begin(), visitedNodes.end(), node) != visitedNodes.end()) {
580 std::vector<MSchedGraphNode*> recurrence;
584 int RecMII = II; //Starting value
585 MSchedGraphNode *last = node;
586 MSchedGraphNode *srcBackEdge;
587 MSchedGraphNode *destBackEdge;
591 for(std::vector<MSchedGraphNode*>::iterator I = visitedNodes.begin(), E = visitedNodes.end();
599 delay = delay + (*I)->getLatency();
602 int diff = (*I)->getInEdge(last).getIteDiff();
610 recurrence.push_back(*I);
616 //Get final distance calc
617 distance += node->getInEdge(last).getIteDiff();
620 //Adjust II until we get close to the inequality delay - II*distance <= 0
622 int value = delay-(RecMII * distance);
628 value = delay-(RecMII * distance);
632 DEBUG(std::cerr << "Final II for this recurrence: " << lastII << "\n");
633 addReccurrence(recurrence, lastII, srcBackEdge, destBackEdge);
634 assert(distance != 0 && "Recurrence distance should not be zero");
638 for(MSchedGraphNode::succ_iterator I = node->succ_begin(), E = node->succ_end(); I != E; ++I) {
639 visitedNodes.push_back(node);
640 findAllReccurrences(*I, visitedNodes, II);
641 visitedNodes.pop_back();
649 void ModuloSchedulingPass::computePartialOrder() {
652 //Loop over all recurrences and add to our partial order
653 //be sure to remove nodes that are already in the partial order in
654 //a different recurrence and don't add empty recurrences.
655 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::reverse_iterator I = recurrenceList.rbegin(), E=recurrenceList.rend(); I !=E; ++I) {
657 //Add nodes that connect this recurrence to the previous recurrence
659 //If this is the first recurrence in the partial order, add all predecessors
660 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
665 std::vector<MSchedGraphNode*> new_recurrence;
666 //Loop through recurrence and remove any nodes already in the partial order
667 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
669 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
670 if(find(PO->begin(), PO->end(), *N) != PO->end())
674 new_recurrence.push_back(*N);
676 if(partialOrder.size() == 0)
677 //For each predecessors, add it to this recurrence ONLY if it is not already in it
678 for(MSchedGraphNode::pred_iterator P = (*N)->pred_begin(),
679 PE = (*N)->pred_end(); P != PE; ++P) {
681 //Check if we are supposed to ignore this edge or not
682 if(!ignoreEdge(*P, *N))
683 //Check if already in this recurrence
684 if(find(I->second.begin(), I->second.end(), *P) == I->second.end()) {
685 //Also need to check if in partial order
686 bool predFound = false;
687 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PEND = partialOrder.end(); PO != PEND; ++PO) {
688 if(find(PO->begin(), PO->end(), *P) != PO->end())
693 if(find(new_recurrence.begin(), new_recurrence.end(), *P) == new_recurrence.end())
694 new_recurrence.push_back(*P);
702 if(new_recurrence.size() > 0)
703 partialOrder.push_back(new_recurrence);
706 //Add any nodes that are not already in the partial order
707 std::vector<MSchedGraphNode*> lastNodes;
708 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
710 //Check if its already in our partial order, if not add it to the final vector
711 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
712 if(find(PO->begin(), PO->end(), I->first) != PO->end())
716 lastNodes.push_back(I->first);
719 if(lastNodes.size() > 0)
720 partialOrder.push_back(lastNodes);
725 void ModuloSchedulingPass::predIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult) {
727 //Sort CurrentSet so we can use lowerbound
728 sort(CurrentSet.begin(), CurrentSet.end());
730 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
731 for(MSchedGraphNode::pred_iterator P = FinalNodeOrder[j]->pred_begin(),
732 E = FinalNodeOrder[j]->pred_end(); P != E; ++P) {
734 //Check if we are supposed to ignore this edge or not
735 if(ignoreEdge(*P,FinalNodeOrder[j]))
738 if(find(CurrentSet.begin(),
739 CurrentSet.end(), *P) != CurrentSet.end())
740 if(find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
741 IntersectResult.push_back(*P);
746 void ModuloSchedulingPass::succIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult) {
748 //Sort CurrentSet so we can use lowerbound
749 sort(CurrentSet.begin(), CurrentSet.end());
751 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
752 for(MSchedGraphNode::succ_iterator P = FinalNodeOrder[j]->succ_begin(),
753 E = FinalNodeOrder[j]->succ_end(); P != E; ++P) {
755 //Check if we are supposed to ignore this edge or not
756 if(ignoreEdge(FinalNodeOrder[j],*P))
759 if(find(CurrentSet.begin(),
760 CurrentSet.end(), *P) != CurrentSet.end())
761 if(find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
762 IntersectResult.push_back(*P);
767 void dumpIntersection(std::vector<MSchedGraphNode*> &IntersectCurrent) {
768 std::cerr << "Intersection (";
769 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(), E = IntersectCurrent.end(); I != E; ++I)
770 std::cerr << **I << ", ";
776 void ModuloSchedulingPass::orderNodes() {
782 int order = BOTTOM_UP;
785 //Loop over all the sets and place them in the final node order
786 for(std::vector<std::vector<MSchedGraphNode*> >::iterator CurrentSet = partialOrder.begin(), E= partialOrder.end(); CurrentSet != E; ++CurrentSet) {
788 DEBUG(std::cerr << "Processing set in S\n");
789 dumpIntersection(*CurrentSet);
790 //Result of intersection
791 std::vector<MSchedGraphNode*> IntersectCurrent;
793 predIntersect(*CurrentSet, IntersectCurrent);
795 //If the intersection of predecessor and current set is not empty
796 //sort nodes bottom up
797 if(IntersectCurrent.size() != 0) {
798 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is NOT empty\n");
801 //If empty, use successors
803 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is empty\n");
805 succIntersect(*CurrentSet, IntersectCurrent);
808 if(IntersectCurrent.size() != 0) {
809 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is NOT empty\n");
813 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is empty\n");
814 //Find node with max ASAP in current Set
815 MSchedGraphNode *node;
817 DEBUG(std::cerr << "Using current set of size " << CurrentSet->size() << "to find max ASAP\n");
818 for(unsigned j=0; j < CurrentSet->size(); ++j) {
819 //Get node attributes
820 MSNodeAttributes nodeAttr= nodeToAttributesMap.find((*CurrentSet)[j])->second;
821 //assert(nodeAttr != nodeToAttributesMap.end() && "Node not in attributes map!");
822 DEBUG(std::cerr << "CurrentSet index " << j << "has ASAP: " << nodeAttr.ASAP << "\n");
823 if(maxASAP < nodeAttr.ASAP) {
824 maxASAP = nodeAttr.ASAP;
825 node = (*CurrentSet)[j];
828 assert(node != 0 && "In node ordering node should not be null");
829 IntersectCurrent.push_back(node);
834 //Repeat until all nodes are put into the final order from current set
835 while(IntersectCurrent.size() > 0) {
837 if(order == TOP_DOWN) {
838 DEBUG(std::cerr << "Order is TOP DOWN\n");
840 while(IntersectCurrent.size() > 0) {
841 DEBUG(std::cerr << "Intersection is not empty, so find heighest height\n");
845 MSchedGraphNode *highestHeightNode = IntersectCurrent[0];
847 //Find node in intersection with highest heigh and lowest MOB
848 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
849 E = IntersectCurrent.end(); I != E; ++I) {
851 //Get current nodes properties
852 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
854 if(height < nodeAttr.height) {
855 highestHeightNode = *I;
856 height = nodeAttr.height;
859 else if(height == nodeAttr.height) {
860 if(MOB > nodeAttr.height) {
861 highestHeightNode = *I;
862 height = nodeAttr.height;
868 //Append our node with greatest height to the NodeOrder
869 if(find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestHeightNode) == FinalNodeOrder.end()) {
870 DEBUG(std::cerr << "Adding node to Final Order: " << *highestHeightNode << "\n");
871 FinalNodeOrder.push_back(highestHeightNode);
874 //Remove V from IntersectOrder
875 IntersectCurrent.erase(find(IntersectCurrent.begin(),
876 IntersectCurrent.end(), highestHeightNode));
879 //Intersect V's successors with CurrentSet
880 for(MSchedGraphNode::succ_iterator P = highestHeightNode->succ_begin(),
881 E = highestHeightNode->succ_end(); P != E; ++P) {
882 //if(lower_bound(CurrentSet->begin(),
883 // CurrentSet->end(), *P) != CurrentSet->end()) {
884 if(find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) {
885 if(ignoreEdge(highestHeightNode, *P))
887 //If not already in Intersect, add
888 if(find(IntersectCurrent.begin(), IntersectCurrent.end(), *P) == IntersectCurrent.end())
889 IntersectCurrent.push_back(*P);
892 } //End while loop over Intersect Size
897 //Reset Intersect to reflect changes in OrderNodes
898 IntersectCurrent.clear();
899 predIntersect(*CurrentSet, IntersectCurrent);
905 DEBUG(std::cerr << "Order is BOTTOM UP\n");
906 while(IntersectCurrent.size() > 0) {
907 DEBUG(std::cerr << "Intersection of size " << IntersectCurrent.size() << ", finding highest depth\n");
910 DEBUG(dumpIntersection(IntersectCurrent));
911 //Get node with highest depth, if a tie, use one with lowest
915 MSchedGraphNode *highestDepthNode = IntersectCurrent[0];
917 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
918 E = IntersectCurrent.end(); I != E; ++I) {
919 //Find node attribute in graph
920 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
922 if(depth < nodeAttr.depth) {
923 highestDepthNode = *I;
924 depth = nodeAttr.depth;
927 else if(depth == nodeAttr.depth) {
928 if(MOB > nodeAttr.MOB) {
929 highestDepthNode = *I;
930 depth = nodeAttr.depth;
938 //Append highest depth node to the NodeOrder
939 if(find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestDepthNode) == FinalNodeOrder.end()) {
940 DEBUG(std::cerr << "Adding node to Final Order: " << *highestDepthNode << "\n");
941 FinalNodeOrder.push_back(highestDepthNode);
943 //Remove heightestDepthNode from IntersectOrder
944 IntersectCurrent.erase(find(IntersectCurrent.begin(),
945 IntersectCurrent.end(),highestDepthNode));
948 //Intersect heightDepthNode's pred with CurrentSet
949 for(MSchedGraphNode::pred_iterator P = highestDepthNode->pred_begin(),
950 E = highestDepthNode->pred_end(); P != E; ++P) {
951 //if(lower_bound(CurrentSet->begin(),
952 // CurrentSet->end(), *P) != CurrentSet->end()) {
953 if(find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) {
955 if(ignoreEdge(*P, highestDepthNode))
958 //If not already in Intersect, add
959 if(find(IntersectCurrent.begin(),
960 IntersectCurrent.end(), *P) == IntersectCurrent.end())
961 IntersectCurrent.push_back(*P);
965 } //End while loop over Intersect Size
970 //Reset IntersectCurrent to reflect changes in OrderNodes
971 IntersectCurrent.clear();
972 succIntersect(*CurrentSet, IntersectCurrent);
973 } //End if BOTTOM_DOWN
976 //End Wrapping while loop
978 }//End for over all sets of nodes
981 //return FinalNodeOrder;
984 void ModuloSchedulingPass::computeSchedule() {
986 bool success = false;
990 //Loop over the final node order and process each node
991 for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(),
992 E = FinalNodeOrder.end(); I != E; ++I) {
994 //CalculateEarly and Late start
996 int LateStart = 99999; //Set to something higher then we would ever expect (FIXME)
997 bool hasSucc = false;
998 bool hasPred = false;
999 std::set<MSchedGraphNode*> seenNodes;
1001 for(std::map<unsigned, std::vector<std::pair<unsigned, std::vector<MSchedGraphNode*> > > >::iterator J = schedule.begin(),
1002 JE = schedule.end(); J != JE; ++J) {
1004 //For each resource with nodes scheduled, loop over the nodes and see if they
1005 //are a predecessor or successor of this current node we are trying
1007 for(std::vector<std::pair<unsigned, std::vector<MSchedGraphNode*> > >::iterator schedNodeVec = J->second.begin(), SNE = J->second.end(); schedNodeVec != SNE; ++schedNodeVec) {
1009 for(std::vector<MSchedGraphNode*>::iterator schedNode = schedNodeVec->second.begin(), schedNodeEnd = schedNodeVec->second.end(); schedNode != schedNodeEnd; ++schedNode) {
1010 if((*I)->isPredecessor(*schedNode) && !seenNodes.count(*schedNode)) {
1011 if(!ignoreEdge(*schedNode, *I)) {
1012 int diff = (*I)->getInEdge(*schedNode).getIteDiff();
1013 int ES_Temp = J->first + (*schedNode)->getLatency() - diff * II;
1014 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << J->first << "\n");
1015 DEBUG(std::cerr << "Temp EarlyStart: " << ES_Temp << " Prev EarlyStart: " << EarlyStart << "\n");
1016 EarlyStart = std::max(EarlyStart, ES_Temp);
1020 if((*I)->isSuccessor(*schedNode) && !seenNodes.count(*schedNode)) {
1021 if(!ignoreEdge(*I,*schedNode)) {
1022 int diff = (*schedNode)->getInEdge(*I).getIteDiff();
1023 int LS_Temp = J->first - (*I)->getLatency() + diff * II;
1024 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << J->first << "\n");
1025 DEBUG(std::cerr << "Temp LateStart: " << LS_Temp << " Prev LateStart: " << LateStart << "\n");
1026 LateStart = std::min(LateStart, LS_Temp);
1030 seenNodes.insert(*schedNode);
1036 DEBUG(std::cerr << "Has Successors: " << hasSucc << ", Has Pred: " << hasPred << "\n");
1037 DEBUG(std::cerr << "EarlyStart: " << EarlyStart << ", LateStart: " << LateStart << "\n");
1039 //Check if the node has no pred or successors and set Early Start to its ASAP
1040 if(!hasSucc && !hasPred)
1041 EarlyStart = nodeToAttributesMap.find(*I)->second.ASAP;
1043 //Now, try to schedule this node depending upon its pred and successor in the schedule
1045 if(!hasSucc && hasPred)
1046 success = scheduleNode(*I, EarlyStart, (EarlyStart + II -1));
1047 else if(!hasPred && hasSucc)
1048 success = scheduleNode(*I, LateStart, (LateStart - II +1));
1049 else if(hasPred && hasSucc)
1050 success = scheduleNode(*I, EarlyStart, std::min(LateStart, (EarlyStart + II -1)));
1052 success = scheduleNode(*I, EarlyStart, EarlyStart + II - 1);
1065 bool ModuloSchedulingPass::scheduleNode(MSchedGraphNode *node,
1066 int start, int end) {
1067 bool success = false;
1069 DEBUG(std::cerr << *node << " (Start Cycle: " << start << ", End Cycle: " << end << ")\n");
1071 /*std::cerr << "CURRENT SCHEDULE\n";
1072 //Dump out current schedule
1073 for(std::map<unsigned, std::vector<std::pair<unsigned, MSchedGraphNode*> > >::iterator J = schedule.begin(),
1074 JE = schedule.end(); J != JE; ++J) {
1075 std::cerr << "Cycle " << J->first << ":\n";
1076 for(std::vector<std::pair<unsigned, MSchedGraphNode*> >::iterator VI = J->second.begin(), VE = J->second.end(); VI != VE; ++VI)
1077 std::cerr << "Resource ID: " << VI->first << " by node " << *(VI->second) << "\n";
1079 std::cerr << "END CURRENT SCHEDULE\n";
1082 //Make sure start and end are not negative
1088 bool forward = true;
1092 const TargetSchedInfo & msi = target.getSchedInfo();
1094 bool increaseSC = true;
1103 //Get the resource used by this instruction
1104 //Get resource usage for this instruction
1105 InstrRUsage rUsage = msi.getInstrRUsage(node->getInst()->getOpcode());
1106 std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle;
1108 //Loop over each resource and see if we can put it into the schedule
1109 for(unsigned r=0; r < resources.size(); ++r) {
1110 unsigned intermediateCycle = cycle + r;
1112 for(unsigned j=0; j < resources[r].size(); ++j) {
1113 //Put it into the schedule
1114 DEBUG(std::cerr << "Attempting to put resource " << resources[r][j] << " in schedule at cycle: " << intermediateCycle << "\n");
1116 //Check if resource is free at this cycle
1117 std::vector<std::pair<unsigned, std::vector<MSchedGraphNode*> > > resourceForCycle = schedule[intermediateCycle];
1119 //Vector of nodes using this resource
1120 std::vector<MSchedGraphNode*> *nodesUsingResource;
1122 for(std::vector<std::pair<unsigned, std::vector<MSchedGraphNode*> > >::iterator I = resourceForCycle.begin(), E= resourceForCycle.end(); I != E; ++I) {
1124 if(I->first == resources[r][j]) {
1125 //Get the number of available for this resource
1126 unsigned numResource = CPUResource::getCPUResource(resources[r][j])->maxNumUsers;
1127 nodesUsingResource = &(I->second);
1129 //Check that there are enough of this resource, otherwise
1130 //we need to increase/decrease the cycle
1131 if(I->second.size() >= numResource) {
1132 DEBUG(std::cerr << "No open spot for this resource in this cycle\n");
1138 //safe to put into schedule
1145 DEBUG(std::cerr << "Found spot in schedule\n");
1146 //Add node to resource vector
1147 if(nodesUsingResource == 0) {
1148 nodesUsingResource = new std::vector<MSchedGraphNode*>;
1149 resourceForCycle.push_back(std::make_pair(resources[r][j], *nodesUsingResource));
1152 nodesUsingResource->push_back(node);
1154 schedule[intermediateCycle] = resourceForCycle;
1158 /*for(unsigned x = 0; x < r; ++x) {
1159 unsigned removeCycle = x + start;
1160 for(unsigned j=0; j < resources[x].size(); ++j) {
1161 std::vector<std::pair<unsigned, MSchedGraphNode*> > resourceForCycle = schedule[removeCycle];
1162 for(std::vector<std::pair<unsigned,MSchedGraphNode*> >::iterator I = resourceForCycle.begin(), E= resourceForCycle.end(); I != E; ++I) {
1163 if(I->first == resources[x][j]) {
1165 resourceForCycle.erase(I);
1169 schedule[removeCycle] = resourceForCycle;
1179 //Increment cycle to try again
1182 DEBUG(std::cerr << "Increase cycle: " << cycle << "\n");
1188 DEBUG(std::cerr << "Decrease cycle: " << cycle << "\n");