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 //===----------------------------------------------------------------------===//
10 // This ModuloScheduling pass is based on the Swing Modulo Scheduling
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "ModuloSched"
17 #include "ModuloScheduling.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Target/TargetSchedInfo.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/GraphWriter.h"
26 #include "llvm/ADT/StringExtras.h"
33 #include "../MachineCodeForInstruction.h"
34 #include "../SparcV9TmpInstr.h"
35 #include "../SparcV9Internals.h"
36 #include "../SparcV9RegisterInfo.h"
39 /// Create ModuloSchedulingPass
41 FunctionPass *llvm::createModuloSchedulingPass(TargetMachine & targ) {
42 DEBUG(std::cerr << "Created ModuloSchedulingPass\n");
43 return new ModuloSchedulingPass(targ);
47 //Graph Traits for printing out the dependence graph
48 template<typename GraphType>
49 static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
50 const GraphType >) {
51 std::string Filename = GraphName + ".dot";
52 O << "Writing '" << Filename << "'...";
53 std::ofstream F(Filename.c_str());
58 O << " error opening file for writing!";
62 //Graph Traits for printing out the dependence graph
66 struct DOTGraphTraits<MSchedGraph*> : public DefaultDOTGraphTraits {
67 static std::string getGraphName(MSchedGraph *F) {
68 return "Dependence Graph";
71 static std::string getNodeLabel(MSchedGraphNode *Node, MSchedGraph *Graph) {
72 if (Node->getInst()) {
74 ss << *(Node->getInst());
75 return ss.str(); //((MachineInstr*)Node->getInst());
80 static std::string getEdgeSourceLabel(MSchedGraphNode *Node,
81 MSchedGraphNode::succ_iterator I) {
82 //Label each edge with the type of dependence
83 std::string edgelabel = "";
84 switch (I.getEdge().getDepOrderType()) {
86 case MSchedGraphEdge::TrueDep:
90 case MSchedGraphEdge::AntiDep:
94 case MSchedGraphEdge::OutputDep:
99 edgelabel = "Unknown";
104 int iteDiff = I.getEdge().getIteDiff();
105 std::string intStr = "(IteDiff: ";
106 intStr += itostr(iteDiff);
116 /// ModuloScheduling::runOnFunction - main transformation entry point
117 /// The Swing Modulo Schedule algorithm has three basic steps:
118 /// 1) Computation and Analysis of the dependence graph
119 /// 2) Ordering of the nodes
122 bool ModuloSchedulingPass::runOnFunction(Function &F) {
124 bool Changed = false;
126 DEBUG(std::cerr << "Creating ModuloSchedGraph for each valid BasicBlock in " + F.getName() + "\n");
128 //Get MachineFunction
129 MachineFunction &MF = MachineFunction::get(&F);
133 std::vector<MachineBasicBlock*> Worklist;
135 //Iterate over BasicBlocks and put them into our worklist if they are valid
136 for (MachineFunction::iterator BI = MF.begin(); BI != MF.end(); ++BI)
137 if(MachineBBisValid(BI))
138 Worklist.push_back(&*BI);
140 DEBUG(if(Worklist.size() == 0) std::cerr << "No single basic block loops in function to ModuloSchedule\n");
142 //Iterate over the worklist and perform scheduling
143 for(std::vector<MachineBasicBlock*>::iterator BI = Worklist.begin(),
144 BE = Worklist.end(); BI != BE; ++BI) {
146 MSchedGraph *MSG = new MSchedGraph(*BI, target);
148 //Write Graph out to file
149 DEBUG(WriteGraphToFile(std::cerr, F.getName(), MSG));
151 //Print out BB for debugging
152 DEBUG(std::cerr << "ModuloScheduling BB: \n"; (*BI)->print(std::cerr));
154 //Calculate Resource II
155 int ResMII = calculateResMII(*BI);
157 //Calculate Recurrence II
158 int RecMII = calculateRecMII(MSG, ResMII);
160 //Our starting initiation interval is the maximum of RecMII and ResMII
161 II = std::max(RecMII, ResMII);
163 //Print out II, RecMII, and ResMII
164 DEBUG(std::cerr << "II starts out as " << II << " ( RecMII=" << RecMII << " and ResMII=" << ResMII << ")\n");
166 //Dump node properties if in debug mode
167 DEBUG(for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
168 E = nodeToAttributesMap.end(); I !=E; ++I) {
169 std::cerr << "Node: " << *(I->first) << " ASAP: " << I->second.ASAP << " ALAP: "
170 << I->second.ALAP << " MOB: " << I->second.MOB << " Depth: " << I->second.depth
171 << " Height: " << I->second.height << "\n";
174 //Calculate Node Properties
175 calculateNodeAttributes(MSG, ResMII);
177 //Dump node properties if in debug mode
178 DEBUG(for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
179 E = nodeToAttributesMap.end(); I !=E; ++I) {
180 std::cerr << "Node: " << *(I->first) << " ASAP: " << I->second.ASAP << " ALAP: "
181 << I->second.ALAP << " MOB: " << I->second.MOB << " Depth: " << I->second.depth
182 << " Height: " << I->second.height << "\n";
185 //Put nodes in order to schedule them
186 computePartialOrder();
188 //Dump out partial order
189 DEBUG(for(std::vector<std::set<MSchedGraphNode*> >::iterator I = partialOrder.begin(),
190 E = partialOrder.end(); I !=E; ++I) {
191 std::cerr << "Start set in PO\n";
192 for(std::set<MSchedGraphNode*>::iterator J = I->begin(), JE = I->end(); J != JE; ++J)
193 std::cerr << "PO:" << **J << "\n";
196 //Place nodes in final order
199 //Dump out order of nodes
200 DEBUG(for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(), E = FinalNodeOrder.end(); I != E; ++I) {
201 std::cerr << "FO:" << **I << "\n";
204 //Finally schedule nodes
207 //Print out final schedule
208 DEBUG(schedule.print(std::cerr));
211 //Final scheduling step is to reconstruct the loop only if we actual have
213 if(schedule.getMaxStage() != 0)
214 reconstructLoop(*BI);
216 DEBUG(std::cerr << "Max stage is 0, so no change in loop\n");
218 //Clear out our maps for the next basic block that is processed
219 nodeToAttributesMap.clear();
220 partialOrder.clear();
221 recurrenceList.clear();
222 FinalNodeOrder.clear();
225 //Clean up. Nuke old MachineBB and llvmBB
226 //BasicBlock *llvmBB = (BasicBlock*) (*BI)->getBasicBlock();
227 //Function *parent = (Function*) llvmBB->getParent();
228 //Should't std::find work??
229 //parent->getBasicBlockList().erase(std::find(parent->getBasicBlockList().begin(), parent->getBasicBlockList().end(), *llvmBB));
230 //parent->getBasicBlockList().erase(llvmBB);
241 /// This function checks if a Machine Basic Block is valid for modulo
242 /// scheduling. This means that it has no control flow (if/else or
243 /// calls) in the block. Currently ModuloScheduling only works on
244 /// single basic block loops.
245 bool ModuloSchedulingPass::MachineBBisValid(const MachineBasicBlock *BI) {
249 //Check first if its a valid loop
250 for(succ_const_iterator I = succ_begin(BI->getBasicBlock()),
251 E = succ_end(BI->getBasicBlock()); I != E; ++I) {
252 if (*I == BI->getBasicBlock()) // has single block loop
259 //Get Target machine instruction info
260 const TargetInstrInfo *TMI = target.getInstrInfo();
262 //Check each instruction and look for calls
263 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
264 //Get opcode to check instruction type
265 MachineOpCode OC = I->getOpcode();
273 //ResMII is calculated by determining the usage count for each resource
274 //and using the maximum.
275 //FIXME: In future there should be a way to get alternative resources
276 //for each instruction
277 int ModuloSchedulingPass::calculateResMII(const MachineBasicBlock *BI) {
279 const TargetInstrInfo *mii = target.getInstrInfo();
280 const TargetSchedInfo *msi = target.getSchedInfo();
284 //Map to keep track of usage count of each resource
285 std::map<unsigned, unsigned> resourceUsageCount;
287 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
289 //Get resource usage for this instruction
290 InstrRUsage rUsage = msi->getInstrRUsage(I->getOpcode());
291 std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle;
293 //Loop over resources in each cycle and increments their usage count
294 for(unsigned i=0; i < resources.size(); ++i)
295 for(unsigned j=0; j < resources[i].size(); ++j) {
296 if( resourceUsageCount.find(resources[i][j]) == resourceUsageCount.end()) {
297 resourceUsageCount[resources[i][j]] = 1;
300 resourceUsageCount[resources[i][j]] = resourceUsageCount[resources[i][j]] + 1;
305 //Find maximum usage count
307 //Get max number of instructions that can be issued at once. (FIXME)
308 int issueSlots = msi->maxNumIssueTotal;
310 for(std::map<unsigned,unsigned>::iterator RB = resourceUsageCount.begin(), RE = resourceUsageCount.end(); RB != RE; ++RB) {
312 //Get the total number of the resources in our cpu
313 int resourceNum = CPUResource::getCPUResource(RB->first)->maxNumUsers;
315 //Get total usage count for this resources
316 unsigned usageCount = RB->second;
318 //Divide the usage count by either the max number we can issue or the number of
319 //resources (whichever is its upper bound)
320 double finalUsageCount;
321 if( resourceNum <= issueSlots)
322 finalUsageCount = ceil(1.0 * usageCount / resourceNum);
324 finalUsageCount = ceil(1.0 * usageCount / issueSlots);
327 //Only keep track of the max
328 ResMII = std::max( (int) finalUsageCount, ResMII);
336 /// calculateRecMII - Calculates the value of the highest recurrence
337 /// By value we mean the total latency
338 int ModuloSchedulingPass::calculateRecMII(MSchedGraph *graph, int MII) {
339 std::vector<MSchedGraphNode*> vNodes;
340 //Loop over all nodes in the graph
341 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
342 findAllReccurrences(I->second, vNodes, MII);
348 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator I = recurrenceList.begin(), E=recurrenceList.end(); I !=E; ++I) {
349 DEBUG(for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
350 std::cerr << **N << "\n";
352 RecMII = std::max(RecMII, I->first);
358 /// calculateNodeAttributes - The following properties are calculated for
359 /// each node in the dependence graph: ASAP, ALAP, Depth, Height, and
361 void ModuloSchedulingPass::calculateNodeAttributes(MSchedGraph *graph, int MII) {
363 assert(nodeToAttributesMap.empty() && "Node attribute map was not cleared");
365 //Loop over the nodes and add them to the map
366 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
368 DEBUG(std::cerr << "Inserting node into attribute map: " << *I->second << "\n");
370 //Assert if its already in the map
371 assert(nodeToAttributesMap.count(I->second) == 0 &&
372 "Node attributes are already in the map");
374 //Put into the map with default attribute values
375 nodeToAttributesMap[I->second] = MSNodeAttributes();
378 //Create set to deal with reccurrences
379 std::set<MSchedGraphNode*> visitedNodes;
381 //Now Loop over map and calculate the node attributes
382 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
383 calculateASAP(I->first, MII, (MSchedGraphNode*) 0);
384 visitedNodes.clear();
387 int maxASAP = findMaxASAP();
388 //Calculate ALAP which depends on ASAP being totally calculated
389 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
390 calculateALAP(I->first, MII, maxASAP, (MSchedGraphNode*) 0);
391 visitedNodes.clear();
394 //Calculate MOB which depends on ASAP being totally calculated, also do depth and height
395 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
396 (I->second).MOB = std::max(0,(I->second).ALAP - (I->second).ASAP);
398 DEBUG(std::cerr << "MOB: " << (I->second).MOB << " (" << *(I->first) << ")\n");
399 calculateDepth(I->first, (MSchedGraphNode*) 0);
400 calculateHeight(I->first, (MSchedGraphNode*) 0);
406 /// ignoreEdge - Checks to see if this edge of a recurrence should be ignored or not
407 bool ModuloSchedulingPass::ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode) {
408 if(destNode == 0 || srcNode ==0)
411 bool findEdge = edgesToIgnore.count(std::make_pair(srcNode, destNode->getInEdgeNum(srcNode)));
417 /// calculateASAP - Calculates the
418 int ModuloSchedulingPass::calculateASAP(MSchedGraphNode *node, int MII, MSchedGraphNode *destNode) {
420 DEBUG(std::cerr << "Calculating ASAP for " << *node << "\n");
422 //Get current node attributes
423 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
425 if(attributes.ASAP != -1)
426 return attributes.ASAP;
428 int maxPredValue = 0;
430 //Iterate over all of the predecessors and find max
431 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
433 //Only process if we are not ignoring the edge
434 if(!ignoreEdge(*P, node)) {
436 predASAP = calculateASAP(*P, MII, node);
438 assert(predASAP != -1 && "ASAP has not been calculated");
439 int iteDiff = node->getInEdge(*P).getIteDiff();
441 int currentPredValue = predASAP + (*P)->getLatency() - (iteDiff * MII);
442 DEBUG(std::cerr << "pred ASAP: " << predASAP << ", iteDiff: " << iteDiff << ", PredLatency: " << (*P)->getLatency() << ", Current ASAP pred: " << currentPredValue << "\n");
443 maxPredValue = std::max(maxPredValue, currentPredValue);
447 attributes.ASAP = maxPredValue;
449 DEBUG(std::cerr << "ASAP: " << attributes.ASAP << " (" << *node << ")\n");
455 int ModuloSchedulingPass::calculateALAP(MSchedGraphNode *node, int MII,
456 int maxASAP, MSchedGraphNode *srcNode) {
458 DEBUG(std::cerr << "Calculating ALAP for " << *node << "\n");
460 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
462 if(attributes.ALAP != -1)
463 return attributes.ALAP;
465 if(node->hasSuccessors()) {
467 //Trying to deal with the issue where the node has successors, but
468 //we are ignoring all of the edges to them. So this is my hack for
469 //now.. there is probably a more elegant way of doing this (FIXME)
470 bool processedOneEdge = false;
472 //FIXME, set to something high to start
473 int minSuccValue = 9999999;
475 //Iterate over all of the predecessors and fine max
476 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
477 E = node->succ_end(); P != E; ++P) {
479 //Only process if we are not ignoring the edge
480 if(!ignoreEdge(node, *P)) {
481 processedOneEdge = true;
483 succALAP = calculateALAP(*P, MII, maxASAP, node);
485 assert(succALAP != -1 && "Successors ALAP should have been caclulated");
487 int iteDiff = P.getEdge().getIteDiff();
489 int currentSuccValue = succALAP - node->getLatency() + iteDiff * MII;
491 DEBUG(std::cerr << "succ ALAP: " << succALAP << ", iteDiff: " << iteDiff << ", SuccLatency: " << (*P)->getLatency() << ", Current ALAP succ: " << currentSuccValue << "\n");
493 minSuccValue = std::min(minSuccValue, currentSuccValue);
498 attributes.ALAP = minSuccValue;
501 attributes.ALAP = maxASAP;
504 attributes.ALAP = maxASAP;
506 DEBUG(std::cerr << "ALAP: " << attributes.ALAP << " (" << *node << ")\n");
508 if(attributes.ALAP < 0)
511 return attributes.ALAP;
514 int ModuloSchedulingPass::findMaxASAP() {
517 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
518 E = nodeToAttributesMap.end(); I != E; ++I)
519 maxASAP = std::max(maxASAP, I->second.ASAP);
524 int ModuloSchedulingPass::calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode) {
526 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
528 if(attributes.height != -1)
529 return attributes.height;
533 //Iterate over all of the predecessors and find max
534 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
535 E = node->succ_end(); P != E; ++P) {
538 if(!ignoreEdge(node, *P)) {
539 int succHeight = calculateHeight(*P, node);
541 assert(succHeight != -1 && "Successors Height should have been caclulated");
543 int currentHeight = succHeight + node->getLatency();
544 maxHeight = std::max(maxHeight, currentHeight);
547 attributes.height = maxHeight;
548 DEBUG(std::cerr << "Height: " << attributes.height << " (" << *node << ")\n");
553 int ModuloSchedulingPass::calculateDepth(MSchedGraphNode *node,
554 MSchedGraphNode *destNode) {
556 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
558 if(attributes.depth != -1)
559 return attributes.depth;
563 //Iterate over all of the predecessors and fine max
564 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
566 if(!ignoreEdge(*P, node)) {
568 predDepth = calculateDepth(*P, node);
570 assert(predDepth != -1 && "Predecessors ASAP should have been caclulated");
572 int currentDepth = predDepth + (*P)->getLatency();
573 maxDepth = std::max(maxDepth, currentDepth);
576 attributes.depth = maxDepth;
578 DEBUG(std::cerr << "Depth: " << attributes.depth << " (" << *node << "*)\n");
584 void ModuloSchedulingPass::addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode *srcBENode, MSchedGraphNode *destBENode) {
585 //Check to make sure that this recurrence is unique
589 //Loop over all recurrences already in our list
590 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator R = recurrenceList.begin(), RE = recurrenceList.end(); R != RE; ++R) {
592 bool all_same = true;
594 if(R->second.size() == recurrence.size()) {
596 for(std::vector<MSchedGraphNode*>::const_iterator node = R->second.begin(), end = R->second.end(); node != end; ++node) {
597 if(std::find(recurrence.begin(), recurrence.end(), *node) == recurrence.end()) {
598 all_same = all_same && false;
602 all_same = all_same && true;
612 srcBENode = recurrence.back();
613 destBENode = recurrence.front();
616 if(destBENode->getInEdge(srcBENode).getIteDiff() == 0) {
617 //DEBUG(std::cerr << "NOT A BACKEDGE\n");
618 //find actual backedge HACK HACK
619 for(unsigned i=0; i< recurrence.size()-1; ++i) {
620 if(recurrence[i+1]->getInEdge(recurrence[i]).getIteDiff() == 1) {
621 srcBENode = recurrence[i];
622 destBENode = recurrence[i+1];
629 DEBUG(std::cerr << "Back Edge to Remove: " << *srcBENode << " to " << *destBENode << "\n");
630 edgesToIgnore.insert(std::make_pair(srcBENode, destBENode->getInEdgeNum(srcBENode)));
631 recurrenceList.insert(std::make_pair(II, recurrence));
636 void ModuloSchedulingPass::findAllReccurrences(MSchedGraphNode *node,
637 std::vector<MSchedGraphNode*> &visitedNodes,
640 if(std::find(visitedNodes.begin(), visitedNodes.end(), node) != visitedNodes.end()) {
641 std::vector<MSchedGraphNode*> recurrence;
645 int RecMII = II; //Starting value
646 MSchedGraphNode *last = node;
647 MSchedGraphNode *srcBackEdge = 0;
648 MSchedGraphNode *destBackEdge = 0;
652 for(std::vector<MSchedGraphNode*>::iterator I = visitedNodes.begin(), E = visitedNodes.end();
660 delay = delay + (*I)->getLatency();
663 int diff = (*I)->getInEdge(last).getIteDiff();
671 recurrence.push_back(*I);
677 //Get final distance calc
678 distance += node->getInEdge(last).getIteDiff();
681 //Adjust II until we get close to the inequality delay - II*distance <= 0
683 int value = delay-(RecMII * distance);
689 value = delay-(RecMII * distance);
693 DEBUG(std::cerr << "Final II for this recurrence: " << lastII << "\n");
694 addReccurrence(recurrence, lastII, srcBackEdge, destBackEdge);
695 assert(distance != 0 && "Recurrence distance should not be zero");
699 for(MSchedGraphNode::succ_iterator I = node->succ_begin(), E = node->succ_end(); I != E; ++I) {
700 visitedNodes.push_back(node);
701 findAllReccurrences(*I, visitedNodes, II);
702 visitedNodes.pop_back();
710 void ModuloSchedulingPass::computePartialOrder() {
713 //Loop over all recurrences and add to our partial order
714 //be sure to remove nodes that are already in the partial order in
715 //a different recurrence and don't add empty recurrences.
716 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::reverse_iterator I = recurrenceList.rbegin(), E=recurrenceList.rend(); I !=E; ++I) {
718 //Add nodes that connect this recurrence to the previous recurrence
720 //If this is the first recurrence in the partial order, add all predecessors
721 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
726 std::set<MSchedGraphNode*> new_recurrence;
727 //Loop through recurrence and remove any nodes already in the partial order
728 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
730 for(std::vector<std::set<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
735 new_recurrence.insert(*N);
737 if(partialOrder.size() == 0)
738 //For each predecessors, add it to this recurrence ONLY if it is not already in it
739 for(MSchedGraphNode::pred_iterator P = (*N)->pred_begin(),
740 PE = (*N)->pred_end(); P != PE; ++P) {
742 //Check if we are supposed to ignore this edge or not
743 if(!ignoreEdge(*P, *N))
744 //Check if already in this recurrence
745 if(std::find(I->second.begin(), I->second.end(), *P) == I->second.end()) {
746 //Also need to check if in partial order
747 bool predFound = false;
748 for(std::vector<std::set<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PEND = partialOrder.end(); PO != PEND; ++PO) {
754 if(!new_recurrence.count(*P))
755 new_recurrence.insert(*P);
763 if(new_recurrence.size() > 0)
764 partialOrder.push_back(new_recurrence);
767 //Add any nodes that are not already in the partial order
768 //Add them in a set, one set per connected component
769 std::set<MSchedGraphNode*> lastNodes;
770 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
772 //Check if its already in our partial order, if not add it to the final vector
773 for(std::vector<std::set<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
774 if(PO->count(I->first))
778 lastNodes.insert(I->first);
781 //Break up remaining nodes that are not in the partial order
782 //into their connected compoenents
783 while(lastNodes.size() > 0) {
784 std::set<MSchedGraphNode*> ccSet;
785 connectedComponentSet(*(lastNodes.begin()),ccSet, lastNodes);
787 partialOrder.push_back(ccSet);
789 //if(lastNodes.size() > 0)
790 //partialOrder.push_back(lastNodes);
795 void ModuloSchedulingPass::connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes) {
798 if( !ccSet.count(node) && lastNodes.count(node)) {
799 lastNodes.erase(node);
805 //Loop over successors and recurse if we have not seen this node before
806 for(MSchedGraphNode::succ_iterator node_succ = node->succ_begin(), end=node->succ_end(); node_succ != end; ++node_succ) {
807 connectedComponentSet(*node_succ, ccSet, lastNodes);
812 void ModuloSchedulingPass::predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult) {
814 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
815 for(MSchedGraphNode::pred_iterator P = FinalNodeOrder[j]->pred_begin(),
816 E = FinalNodeOrder[j]->pred_end(); P != E; ++P) {
818 //Check if we are supposed to ignore this edge or not
819 if(ignoreEdge(*P,FinalNodeOrder[j]))
822 if(CurrentSet.count(*P))
823 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
824 IntersectResult.insert(*P);
833 void ModuloSchedulingPass::succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult) {
835 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
836 for(MSchedGraphNode::succ_iterator P = FinalNodeOrder[j]->succ_begin(),
837 E = FinalNodeOrder[j]->succ_end(); P != E; ++P) {
839 //Check if we are supposed to ignore this edge or not
840 if(ignoreEdge(FinalNodeOrder[j],*P))
843 if(CurrentSet.count(*P))
844 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
845 IntersectResult.insert(*P);
850 void dumpIntersection(std::set<MSchedGraphNode*> &IntersectCurrent) {
851 std::cerr << "Intersection (";
852 for(std::set<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(), E = IntersectCurrent.end(); I != E; ++I)
853 std::cerr << **I << ", ";
859 void ModuloSchedulingPass::orderNodes() {
865 int order = BOTTOM_UP;
868 //Loop over all the sets and place them in the final node order
869 for(std::vector<std::set<MSchedGraphNode*> >::iterator CurrentSet = partialOrder.begin(), E= partialOrder.end(); CurrentSet != E; ++CurrentSet) {
871 DEBUG(std::cerr << "Processing set in S\n");
872 DEBUG(dumpIntersection(*CurrentSet));
874 //Result of intersection
875 std::set<MSchedGraphNode*> IntersectCurrent;
877 predIntersect(*CurrentSet, IntersectCurrent);
879 //If the intersection of predecessor and current set is not empty
880 //sort nodes bottom up
881 if(IntersectCurrent.size() != 0) {
882 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is NOT empty\n");
885 //If empty, use successors
887 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is empty\n");
889 succIntersect(*CurrentSet, IntersectCurrent);
892 if(IntersectCurrent.size() != 0) {
893 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is NOT empty\n");
897 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is empty\n");
898 //Find node with max ASAP in current Set
899 MSchedGraphNode *node;
901 DEBUG(std::cerr << "Using current set of size " << CurrentSet->size() << "to find max ASAP\n");
902 for(std::set<MSchedGraphNode*>::iterator J = CurrentSet->begin(), JE = CurrentSet->end(); J != JE; ++J) {
903 //Get node attributes
904 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*J)->second;
905 //assert(nodeAttr != nodeToAttributesMap.end() && "Node not in attributes map!");
907 if(maxASAP <= nodeAttr.ASAP) {
908 maxASAP = nodeAttr.ASAP;
912 assert(node != 0 && "In node ordering node should not be null");
913 IntersectCurrent.insert(node);
918 //Repeat until all nodes are put into the final order from current set
919 while(IntersectCurrent.size() > 0) {
921 if(order == TOP_DOWN) {
922 DEBUG(std::cerr << "Order is TOP DOWN\n");
924 while(IntersectCurrent.size() > 0) {
925 DEBUG(std::cerr << "Intersection is not empty, so find heighest height\n");
929 MSchedGraphNode *highestHeightNode = *(IntersectCurrent.begin());
931 //Find node in intersection with highest heigh and lowest MOB
932 for(std::set<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
933 E = IntersectCurrent.end(); I != E; ++I) {
935 //Get current nodes properties
936 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
938 if(height < nodeAttr.height) {
939 highestHeightNode = *I;
940 height = nodeAttr.height;
943 else if(height == nodeAttr.height) {
944 if(MOB > nodeAttr.height) {
945 highestHeightNode = *I;
946 height = nodeAttr.height;
952 //Append our node with greatest height to the NodeOrder
953 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestHeightNode) == FinalNodeOrder.end()) {
954 DEBUG(std::cerr << "Adding node to Final Order: " << *highestHeightNode << "\n");
955 FinalNodeOrder.push_back(highestHeightNode);
958 //Remove V from IntersectOrder
959 IntersectCurrent.erase(std::find(IntersectCurrent.begin(),
960 IntersectCurrent.end(), highestHeightNode));
963 //Intersect V's successors with CurrentSet
964 for(MSchedGraphNode::succ_iterator P = highestHeightNode->succ_begin(),
965 E = highestHeightNode->succ_end(); P != E; ++P) {
966 //if(lower_bound(CurrentSet->begin(),
967 // CurrentSet->end(), *P) != CurrentSet->end()) {
968 if(std::find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) {
969 if(ignoreEdge(highestHeightNode, *P))
971 //If not already in Intersect, add
972 if(!IntersectCurrent.count(*P))
973 IntersectCurrent.insert(*P);
976 } //End while loop over Intersect Size
981 //Reset Intersect to reflect changes in OrderNodes
982 IntersectCurrent.clear();
983 predIntersect(*CurrentSet, IntersectCurrent);
989 DEBUG(std::cerr << "Order is BOTTOM UP\n");
990 while(IntersectCurrent.size() > 0) {
991 DEBUG(std::cerr << "Intersection of size " << IntersectCurrent.size() << ", finding highest depth\n");
994 DEBUG(dumpIntersection(IntersectCurrent));
995 //Get node with highest depth, if a tie, use one with lowest
999 MSchedGraphNode *highestDepthNode = *(IntersectCurrent.begin());
1001 for(std::set<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
1002 E = IntersectCurrent.end(); I != E; ++I) {
1003 //Find node attribute in graph
1004 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
1006 if(depth < nodeAttr.depth) {
1007 highestDepthNode = *I;
1008 depth = nodeAttr.depth;
1011 else if(depth == nodeAttr.depth) {
1012 if(MOB > nodeAttr.MOB) {
1013 highestDepthNode = *I;
1014 depth = nodeAttr.depth;
1022 //Append highest depth node to the NodeOrder
1023 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestDepthNode) == FinalNodeOrder.end()) {
1024 DEBUG(std::cerr << "Adding node to Final Order: " << *highestDepthNode << "\n");
1025 FinalNodeOrder.push_back(highestDepthNode);
1027 //Remove heightestDepthNode from IntersectOrder
1028 IntersectCurrent.erase(highestDepthNode);
1031 //Intersect heightDepthNode's pred with CurrentSet
1032 for(MSchedGraphNode::pred_iterator P = highestDepthNode->pred_begin(),
1033 E = highestDepthNode->pred_end(); P != E; ++P) {
1034 if(CurrentSet->count(*P)) {
1035 if(ignoreEdge(*P, highestDepthNode))
1038 //If not already in Intersect, add
1039 if(!IntersectCurrent.count(*P))
1040 IntersectCurrent.insert(*P);
1044 } //End while loop over Intersect Size
1049 //Reset IntersectCurrent to reflect changes in OrderNodes
1050 IntersectCurrent.clear();
1051 succIntersect(*CurrentSet, IntersectCurrent);
1052 } //End if BOTTOM_DOWN
1054 DEBUG(std::cerr << "Current Intersection Size: " << IntersectCurrent.size() << "\n");
1056 //End Wrapping while loop
1057 DEBUG(std::cerr << "Ending Size of Current Set: " << CurrentSet->size() << "\n");
1058 }//End for over all sets of nodes
1060 //FIXME: As the algorithm stands it will NEVER add an instruction such as ba (with no
1061 //data dependencies) to the final order. We add this manually. It will always be
1062 //in the last set of S since its not part of a recurrence
1063 //Loop over all the sets and place them in the final node order
1064 std::vector<std::set<MSchedGraphNode*> > ::reverse_iterator LastSet = partialOrder.rbegin();
1065 for(std::set<MSchedGraphNode*>::iterator CurrentNode = LastSet->begin(), LastNode = LastSet->end();
1066 CurrentNode != LastNode; ++CurrentNode) {
1067 if((*CurrentNode)->getInst()->getOpcode() == V9::BA)
1068 FinalNodeOrder.push_back(*CurrentNode);
1070 //Return final Order
1071 //return FinalNodeOrder;
1074 void ModuloSchedulingPass::computeSchedule() {
1076 bool success = false;
1078 //FIXME: Should be set to max II of the original loop
1079 //Cap II in order to prevent infinite loop
1084 //Loop over the final node order and process each node
1085 for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(),
1086 E = FinalNodeOrder.end(); I != E; ++I) {
1088 //CalculateEarly and Late start
1089 int EarlyStart = -1;
1090 int LateStart = 99999; //Set to something higher then we would ever expect (FIXME)
1091 bool hasSucc = false;
1092 bool hasPred = false;
1094 if(!(*I)->isBranch()) {
1095 //Loop over nodes in the schedule and determine if they are predecessors
1096 //or successors of the node we are trying to schedule
1097 for(MSSchedule::schedule_iterator nodesByCycle = schedule.begin(), nodesByCycleEnd = schedule.end();
1098 nodesByCycle != nodesByCycleEnd; ++nodesByCycle) {
1100 //For this cycle, get the vector of nodes schedule and loop over it
1101 for(std::vector<MSchedGraphNode*>::iterator schedNode = nodesByCycle->second.begin(), SNE = nodesByCycle->second.end(); schedNode != SNE; ++schedNode) {
1103 if((*I)->isPredecessor(*schedNode)) {
1104 if(!ignoreEdge(*schedNode, *I)) {
1105 int diff = (*I)->getInEdge(*schedNode).getIteDiff();
1106 int ES_Temp = nodesByCycle->first + (*schedNode)->getLatency() - diff * II;
1107 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n");
1108 DEBUG(std::cerr << "Temp EarlyStart: " << ES_Temp << " Prev EarlyStart: " << EarlyStart << "\n");
1109 EarlyStart = std::max(EarlyStart, ES_Temp);
1113 if((*I)->isSuccessor(*schedNode)) {
1114 if(!ignoreEdge(*I,*schedNode)) {
1115 int diff = (*schedNode)->getInEdge(*I).getIteDiff();
1116 int LS_Temp = nodesByCycle->first - (*I)->getLatency() + diff * II;
1117 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n");
1118 DEBUG(std::cerr << "Temp LateStart: " << LS_Temp << " Prev LateStart: " << LateStart << "\n");
1119 LateStart = std::min(LateStart, LS_Temp);
1127 //WARNING: HACK! FIXME!!!!
1128 if((*I)->getInst()->getOpcode() == V9::BA) {
1135 assert( (EarlyStart >= 0) && (LateStart >=0) && "EarlyStart and LateStart must be greater then 0");
1142 DEBUG(std::cerr << "Has Successors: " << hasSucc << ", Has Pred: " << hasPred << "\n");
1143 DEBUG(std::cerr << "EarlyStart: " << EarlyStart << ", LateStart: " << LateStart << "\n");
1145 //Check if the node has no pred or successors and set Early Start to its ASAP
1146 if(!hasSucc && !hasPred)
1147 EarlyStart = nodeToAttributesMap.find(*I)->second.ASAP;
1149 //Now, try to schedule this node depending upon its pred and successor in the schedule
1151 if(!hasSucc && hasPred)
1152 success = scheduleNode(*I, EarlyStart, (EarlyStart + II -1));
1153 else if(!hasPred && hasSucc)
1154 success = scheduleNode(*I, LateStart, (LateStart - II +1));
1155 else if(hasPred && hasSucc)
1156 success = scheduleNode(*I, EarlyStart, std::min(LateStart, (EarlyStart + II -1)));
1158 success = scheduleNode(*I, EarlyStart, EarlyStart + II - 1);
1169 DEBUG(std::cerr << "Constructing Schedule Kernel\n");
1170 success = schedule.constructKernel(II);
1171 DEBUG(std::cerr << "Done Constructing Schedule Kernel\n");
1178 assert(II < capII && "The II should not exceed the original loop number of cycles");
1183 bool ModuloSchedulingPass::scheduleNode(MSchedGraphNode *node,
1184 int start, int end) {
1185 bool success = false;
1187 DEBUG(std::cerr << *node << " (Start Cycle: " << start << ", End Cycle: " << end << ")\n");
1189 //Make sure start and end are not negative
1197 bool forward = true;
1201 bool increaseSC = true;
1209 increaseSC = schedule.insert(node, cycle);
1214 //Increment cycle to try again
1217 DEBUG(std::cerr << "Increase cycle: " << cycle << "\n");
1223 DEBUG(std::cerr << "Decrease cycle: " << cycle << "\n");
1232 void ModuloSchedulingPass::writePrologues(std::vector<MachineBasicBlock *> &prologues, MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_prologues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation) {
1234 //Keep a map to easily know whats in the kernel
1235 std::map<int, std::set<const MachineInstr*> > inKernel;
1236 int maxStageCount = 0;
1238 MSchedGraphNode *branch = 0;
1239 MSchedGraphNode *BAbranch = 0;
1241 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1242 maxStageCount = std::max(maxStageCount, I->second);
1244 //Ignore the branch, we will handle this separately
1245 if(I->first->isBranch()) {
1246 if (I->first->getInst()->getOpcode() != V9::BA)
1249 BAbranch = I->first;
1254 //Put int the map so we know what instructions in each stage are in the kernel
1255 DEBUG(std::cerr << "Inserting instruction " << *(I->first->getInst()) << " into map at stage " << I->second << "\n");
1256 inKernel[I->second].insert(I->first->getInst());
1259 //Get target information to look at machine operands
1260 const TargetInstrInfo *mii = target.getInstrInfo();
1262 //Now write the prologues
1263 for(int i = 0; i < maxStageCount; ++i) {
1264 BasicBlock *llvmBB = new BasicBlock("PROLOGUE", (Function*) (origBB->getBasicBlock()->getParent()));
1265 MachineBasicBlock *machineBB = new MachineBasicBlock(llvmBB);
1267 DEBUG(std::cerr << "i=" << i << "\n");
1268 for(int j = 0; j <= i; ++j) {
1269 for(MachineBasicBlock::const_iterator MI = origBB->begin(), ME = origBB->end(); ME != MI; ++MI) {
1270 if(inKernel[j].count(&*MI)) {
1271 MachineInstr *instClone = MI->clone();
1272 machineBB->push_back(instClone);
1274 DEBUG(std::cerr << "Cloning: " << *MI << "\n");
1278 //After cloning, we may need to save the value that this instruction defines
1279 for(unsigned opNum=0; opNum < MI->getNumOperands(); ++opNum) {
1280 //get machine operand
1281 const MachineOperand &mOp = instClone->getOperand(opNum);
1282 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef()) {
1284 //Check if this is a value we should save
1285 if(valuesToSave.count(mOp.getVRegValue())) {
1286 //Save copy in tmpInstruction
1287 tmp = new TmpInstruction(mOp.getVRegValue());
1289 //Get machine code for this instruction
1290 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1291 tempMvec.addTemp((Value*) tmp);
1293 DEBUG(std::cerr << "Value: " << *(mOp.getVRegValue()) << " New Value: " << *tmp << " Stage: " << i << "\n");
1295 newValues[mOp.getVRegValue()][i]= tmp;
1296 newValLocation[tmp] = machineBB;
1298 DEBUG(std::cerr << "Machine Instr Operands: " << *(mOp.getVRegValue()) << ", 0, " << *tmp << "\n");
1300 //Create machine instruction and put int machineBB
1301 MachineInstr *saveValue = BuildMI(machineBB, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1303 DEBUG(std::cerr << "Created new machine instr: " << *saveValue << "\n");
1307 //We may also need to update the value that we use if its from an earlier prologue
1309 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1310 if(newValues.count(mOp.getVRegValue()))
1311 if(newValues[mOp.getVRegValue()].count(j-1)) {
1312 DEBUG(std::cerr << "Replaced this value: " << mOp.getVRegValue() << " With:" << (newValues[mOp.getVRegValue()][i-1]) << "\n");
1313 //Update the operand with the right value
1314 instClone->getOperand(opNum).setValueReg(newValues[mOp.getVRegValue()][i-1]);
1324 //Stick in branch at the end
1325 machineBB->push_back(branch->getInst()->clone());
1328 BuildMI(machineBB, V9::NOP, 0);
1330 //Stick in branch at the end
1331 machineBB->push_back(BAbranch->getInst()->clone());
1334 BuildMI(machineBB, V9::NOP, 0);
1336 (((MachineBasicBlock*)origBB)->getParent())->getBasicBlockList().push_back(machineBB);
1337 prologues.push_back(machineBB);
1338 llvm_prologues.push_back(llvmBB);
1342 void ModuloSchedulingPass::writeEpilogues(std::vector<MachineBasicBlock *> &epilogues, const MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_epilogues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues,std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs ) {
1344 std::map<int, std::set<const MachineInstr*> > inKernel;
1346 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1348 //Ignore the branch, we will handle this separately
1349 if(I->first->isBranch())
1352 //Put int the map so we know what instructions in each stage are in the kernel
1353 inKernel[I->second].insert(I->first->getInst());
1356 std::map<Value*, Value*> valPHIs;
1358 //some debug stuff, will remove later
1359 DEBUG(for(std::map<Value*, std::map<int, Value*> >::iterator V = newValues.begin(), E = newValues.end(); V !=E; ++V) {
1360 std::cerr << "Old Value: " << *(V->first) << "\n";
1361 for(std::map<int, Value*>::iterator I = V->second.begin(), IE = V->second.end(); I != IE; ++I)
1362 std::cerr << "Stage: " << I->first << " Value: " << *(I->second) << "\n";
1365 //some debug stuff, will remove later
1366 DEBUG(for(std::map<Value*, std::map<int, Value*> >::iterator V = kernelPHIs.begin(), E = kernelPHIs.end(); V !=E; ++V) {
1367 std::cerr << "Old Value: " << *(V->first) << "\n";
1368 for(std::map<int, Value*>::iterator I = V->second.begin(), IE = V->second.end(); I != IE; ++I)
1369 std::cerr << "Stage: " << I->first << " Value: " << *(I->second) << "\n";
1372 //Now write the epilogues
1373 for(int i = schedule.getMaxStage()-1; i >= 0; --i) {
1374 BasicBlock *llvmBB = new BasicBlock("EPILOGUE", (Function*) (origBB->getBasicBlock()->getParent()));
1375 MachineBasicBlock *machineBB = new MachineBasicBlock(llvmBB);
1377 DEBUG(std::cerr << " Epilogue #: " << i << "\n");
1380 std::map<Value*, int> inEpilogue;
1382 for(MachineBasicBlock::const_iterator MI = origBB->begin(), ME = origBB->end(); ME != MI; ++MI) {
1383 for(int j=schedule.getMaxStage(); j > i; --j) {
1384 if(inKernel[j].count(&*MI)) {
1385 DEBUG(std::cerr << "Cloning instruction " << *MI << "\n");
1386 MachineInstr *clone = MI->clone();
1388 //Update operands that need to use the result from the phi
1389 for(unsigned opNum=0; opNum < clone->getNumOperands(); ++opNum) {
1390 //get machine operand
1391 const MachineOperand &mOp = clone->getOperand(opNum);
1393 if((mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse())) {
1395 DEBUG(std::cerr << "Writing PHI for " << *(mOp.getVRegValue()) << "\n");
1397 //If this is the last instructions for the max iterations ago, don't update operands
1398 if(inEpilogue.count(mOp.getVRegValue()))
1399 if(inEpilogue[mOp.getVRegValue()] == i)
1402 //Quickly write appropriate phis for this operand
1403 if(newValues.count(mOp.getVRegValue())) {
1404 if(newValues[mOp.getVRegValue()].count(i)) {
1405 Instruction *tmp = new TmpInstruction(newValues[mOp.getVRegValue()][i]);
1407 //Get machine code for this instruction
1408 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1409 tempMvec.addTemp((Value*) tmp);
1411 MachineInstr *saveValue = BuildMI(machineBB, V9::PHI, 3).addReg(newValues[mOp.getVRegValue()][i]).addReg(kernelPHIs[mOp.getVRegValue()][i]).addRegDef(tmp);
1412 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1413 valPHIs[mOp.getVRegValue()] = tmp;
1417 if(valPHIs.count(mOp.getVRegValue())) {
1418 //Update the operand in the cloned instruction
1419 clone->getOperand(opNum).setValueReg(valPHIs[mOp.getVRegValue()]);
1422 else if((mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef())) {
1423 inEpilogue[mOp.getVRegValue()] = i;
1426 machineBB->push_back(clone);
1431 (((MachineBasicBlock*)origBB)->getParent())->getBasicBlockList().push_back(machineBB);
1432 epilogues.push_back(machineBB);
1433 llvm_epilogues.push_back(llvmBB);
1435 DEBUG(std::cerr << "EPILOGUE #" << i << "\n");
1436 DEBUG(machineBB->print(std::cerr));
1440 void ModuloSchedulingPass::writeKernel(BasicBlock *llvmBB, MachineBasicBlock *machineBB, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs) {
1442 //Keep track of operands that are read and saved from a previous iteration. The new clone
1443 //instruction will use the result of the phi instead.
1444 std::map<Value*, Value*> finalPHIValue;
1445 std::map<Value*, Value*> kernelValue;
1447 //Create TmpInstructions for the final phis
1448 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1450 DEBUG(std::cerr << "Stage: " << I->second << " Inst: " << *(I->first->getInst()) << "\n";);
1453 const MachineInstr *inst = I->first->getInst();
1454 MachineInstr *instClone = inst->clone();
1456 //Insert into machine basic block
1457 machineBB->push_back(instClone);
1459 if(I->first->isBranch()) {
1461 BuildMI(machineBB, V9::NOP, 0);
1464 //Loop over Machine Operands
1465 for(unsigned i=0; i < inst->getNumOperands(); ++i) {
1466 //get machine operand
1467 const MachineOperand &mOp = inst->getOperand(i);
1469 if(I->second != 0) {
1470 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1472 //Check to see where this operand is defined if this instruction is from max stage
1473 if(I->second == schedule.getMaxStage()) {
1474 DEBUG(std::cerr << "VREG: " << *(mOp.getVRegValue()) << "\n");
1477 //If its in the value saved, we need to create a temp instruction and use that instead
1478 if(valuesToSave.count(mOp.getVRegValue())) {
1479 TmpInstruction *tmp = new TmpInstruction(mOp.getVRegValue());
1481 //Get machine code for this instruction
1482 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1483 tempMvec.addTemp((Value*) tmp);
1485 //Update the operand in the cloned instruction
1486 instClone->getOperand(i).setValueReg(tmp);
1488 //save this as our final phi
1489 finalPHIValue[mOp.getVRegValue()] = tmp;
1490 newValLocation[tmp] = machineBB;
1494 if(I->second != schedule.getMaxStage()) {
1495 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef()) {
1496 if(valuesToSave.count(mOp.getVRegValue())) {
1498 TmpInstruction *tmp = new TmpInstruction(mOp.getVRegValue());
1500 //Get machine code for this instruction
1501 MachineCodeForInstruction & tempVec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1502 tempVec.addTemp((Value*) tmp);
1504 //Create new machine instr and put in MBB
1505 MachineInstr *saveValue = BuildMI(machineBB, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1507 //Save for future cleanup
1508 kernelValue[mOp.getVRegValue()] = tmp;
1509 newValLocation[tmp] = machineBB;
1510 kernelPHIs[mOp.getVRegValue()][schedule.getMaxStage()-1] = tmp;
1518 DEBUG(std::cerr << "KERNEL before PHIs\n");
1519 DEBUG(machineBB->print(std::cerr));
1522 //Loop over each value we need to generate phis for
1523 for(std::map<Value*, std::map<int, Value*> >::iterator V = newValues.begin(),
1524 E = newValues.end(); V != E; ++V) {
1527 DEBUG(std::cerr << "Writing phi for" << *(V->first));
1528 DEBUG(std::cerr << "\nMap of Value* for this phi\n");
1529 DEBUG(for(std::map<int, Value*>::iterator I = V->second.begin(),
1530 IE = V->second.end(); I != IE; ++I) {
1531 std::cerr << "Stage: " << I->first;
1532 std::cerr << " Value: " << *(I->second) << "\n";
1535 //If we only have one current iteration live, its safe to set lastPhi = to kernel value
1536 if(V->second.size() == 1) {
1537 assert(kernelValue[V->first] != 0 && "Kernel value* must exist to create phi");
1538 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(),V9::PHI, 3).addReg(V->second.begin()->second).addReg(kernelValue[V->first]).addRegDef(finalPHIValue[V->first]);
1539 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1540 kernelPHIs[V->first][schedule.getMaxStage()-1] = kernelValue[V->first];
1544 //Keep track of last phi created.
1545 Instruction *lastPhi = 0;
1548 //Loop over the the map backwards to generate phis
1549 for(std::map<int, Value*>::reverse_iterator I = V->second.rbegin(), IE = V->second.rend();
1552 if(count < (V->second).size()) {
1554 lastPhi = new TmpInstruction(I->second);
1556 //Get machine code for this instruction
1557 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) V->first);
1558 tempMvec.addTemp((Value*) lastPhi);
1560 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(kernelValue[V->first]).addReg(I->second).addRegDef(lastPhi);
1561 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1562 newValLocation[lastPhi] = machineBB;
1565 Instruction *tmp = new TmpInstruction(I->second);
1567 //Get machine code for this instruction
1568 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) V->first);
1569 tempMvec.addTemp((Value*) tmp);
1572 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(lastPhi).addReg(I->second).addRegDef(tmp);
1573 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1575 kernelPHIs[V->first][I->first] = lastPhi;
1576 newValLocation[lastPhi] = machineBB;
1581 //The resulting value must be the Value* we created earlier
1582 assert(lastPhi != 0 && "Last phi is NULL!\n");
1583 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(lastPhi).addReg(I->second).addRegDef(finalPHIValue[V->first]);
1584 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1585 kernelPHIs[V->first][I->first] = finalPHIValue[V->first];
1594 DEBUG(std::cerr << "KERNEL after PHIs\n");
1595 DEBUG(machineBB->print(std::cerr));
1599 void ModuloSchedulingPass::removePHIs(const MachineBasicBlock *origBB, std::vector<MachineBasicBlock *> &prologues, std::vector<MachineBasicBlock *> &epilogues, MachineBasicBlock *kernelBB, std::map<Value*, MachineBasicBlock*> &newValLocation) {
1601 //Worklist to delete things
1602 std::vector<std::pair<MachineBasicBlock*, MachineBasicBlock::iterator> > worklist;
1604 //Worklist of TmpInstructions that need to be added to a MCFI
1605 std::vector<Instruction*> addToMCFI;
1607 //Worklist to add OR instructions to end of kernel so not to invalidate the iterator
1608 //std::vector<std::pair<Instruction*, Value*> > newORs;
1610 const TargetInstrInfo *TMI = target.getInstrInfo();
1612 //Start with the kernel and for each phi insert a copy for the phi def and for each arg
1613 for(MachineBasicBlock::iterator I = kernelBB->begin(), E = kernelBB->end(); I != E; ++I) {
1615 //Get op code and check if its a phi
1616 if(I->getOpcode() == V9::PHI) {
1618 DEBUG(std::cerr << "Replacing PHI: " << *I << "\n");
1619 Instruction *tmp = 0;
1621 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1623 const MachineOperand &mOp = I->getOperand(i);
1624 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Should be a Value*\n");
1627 tmp = new TmpInstruction(mOp.getVRegValue());
1628 addToMCFI.push_back(tmp);
1631 //Now for all our arguments we read, OR to the new TmpInstruction that we created
1633 DEBUG(std::cerr << "Use: " << mOp << "\n");
1634 //Place a copy at the end of its BB but before the branches
1635 assert(newValLocation.count(mOp.getVRegValue()) && "We must know where this value is located\n");
1636 //Reverse iterate to find the branches, we can safely assume no instructions have been
1637 //put in the nop positions
1638 for(MachineBasicBlock::iterator inst = --(newValLocation[mOp.getVRegValue()])->end(), endBB = (newValLocation[mOp.getVRegValue()])->begin(); inst != endBB; --inst) {
1639 MachineOpCode opc = inst->getOpcode();
1640 if(TMI->isBranch(opc) || TMI->isNop(opc))
1643 BuildMI(*(newValLocation[mOp.getVRegValue()]), ++inst, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1651 //Remove the phi and replace it with an OR
1652 DEBUG(std::cerr << "Def: " << mOp << "\n");
1653 //newORs.push_back(std::make_pair(tmp, mOp.getVRegValue()));
1654 BuildMI(*kernelBB, I, V9::ORr, 3).addReg(tmp).addImm(0).addRegDef(mOp.getVRegValue());
1655 worklist.push_back(std::make_pair(kernelBB, I));
1663 //We found an instruction that we can add to its mcfi
1664 if(addToMCFI.size() > 0) {
1665 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1666 const MachineOperand &mOp = I->getOperand(i);
1667 if(mOp.getType() == MachineOperand::MO_VirtualRegister) {
1668 if(!isa<TmpInstruction>(mOp.getVRegValue()) && !isa<PHINode>(mOp.getVRegValue())) {
1669 //Get machine code for this instruction
1670 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1671 for(unsigned x = 0; x < addToMCFI.size(); ++x) {
1672 tempMvec.addTemp(addToMCFI[x]);
1684 //for(std::vector<std::pair<Instruction*, Value*> >::reverse_iterator I = newORs.rbegin(), IE = newORs.rend(); I != IE; ++I)
1685 //BuildMI(*kernelBB, kernelBB->begin(), V9::ORr, 3).addReg(I->first).addImm(0).addRegDef(I->second);
1687 //Remove phis from epilogue
1688 for(std::vector<MachineBasicBlock*>::iterator MB = epilogues.begin(), ME = epilogues.end(); MB != ME; ++MB) {
1689 for(MachineBasicBlock::iterator I = (*MB)->begin(), E = (*MB)->end(); I != E; ++I) {
1690 //Get op code and check if its a phi
1691 if(I->getOpcode() == V9::PHI) {
1692 Instruction *tmp = 0;
1694 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1696 const MachineOperand &mOp = I->getOperand(i);
1697 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Should be a Value*\n");
1700 tmp = new TmpInstruction(mOp.getVRegValue());
1701 addToMCFI.push_back(tmp);
1704 //Now for all our arguments we read, OR to the new TmpInstruction that we created
1706 DEBUG(std::cerr << "Use: " << mOp << "\n");
1707 //Place a copy at the end of its BB but before the branches
1708 assert(newValLocation.count(mOp.getVRegValue()) && "We must know where this value is located\n");
1709 //Reverse iterate to find the branches, we can safely assume no instructions have been
1710 //put in the nop positions
1711 for(MachineBasicBlock::iterator inst = --(newValLocation[mOp.getVRegValue()])->end(), endBB = (newValLocation[mOp.getVRegValue()])->begin(); inst != endBB; --inst) {
1712 MachineOpCode opc = inst->getOpcode();
1713 if(TMI->isBranch(opc) || TMI->isNop(opc))
1716 BuildMI(*(newValLocation[mOp.getVRegValue()]), ++inst, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1724 //Remove the phi and replace it with an OR
1725 DEBUG(std::cerr << "Def: " << mOp << "\n");
1726 BuildMI(**MB, I, V9::ORr, 3).addReg(tmp).addImm(0).addRegDef(mOp.getVRegValue());
1727 worklist.push_back(std::make_pair(*MB,I));
1734 //We found an instruction that we can add to its mcfi
1735 if(addToMCFI.size() > 0) {
1736 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1737 const MachineOperand &mOp = I->getOperand(i);
1738 if(mOp.getType() == MachineOperand::MO_VirtualRegister) {
1740 if(!isa<TmpInstruction>(mOp.getVRegValue()) && !isa<PHINode>(mOp.getVRegValue())) {
1741 //Get machine code for this instruction
1742 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get((Instruction*) mOp.getVRegValue());
1743 for(unsigned x = 0; x < addToMCFI.size(); ++x) {
1744 tempMvec.addTemp(addToMCFI[x]);
1757 for(std::vector<std::pair<MachineBasicBlock*, MachineBasicBlock::iterator> >::iterator I = worklist.begin(), E = worklist.end(); I != E; ++I) {
1759 DEBUG(std::cerr << "Deleting PHI " << *I->second << "\n");
1760 I->first->erase(I->second);
1765 assert((addToMCFI.size() == 0) && "We should have added all TmpInstructions to some MachineCodeForInstruction");
1769 void ModuloSchedulingPass::reconstructLoop(MachineBasicBlock *BB) {
1771 DEBUG(std::cerr << "Reconstructing Loop\n");
1773 //First find the value *'s that we need to "save"
1774 std::map<const Value*, std::pair<const MSchedGraphNode*, int> > valuesToSave;
1776 //Keep track of instructions we have already seen and their stage because
1777 //we don't want to "save" values if they are used in the kernel immediately
1778 std::map<const MachineInstr*, int> lastInstrs;
1780 //Loop over kernel and only look at instructions from a stage > 0
1781 //Look at its operands and save values *'s that are read
1782 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1785 //For this instruction, get the Value*'s that it reads and put them into the set.
1786 //Assert if there is an operand of another type that we need to save
1787 const MachineInstr *inst = I->first->getInst();
1788 lastInstrs[inst] = I->second;
1790 for(unsigned i=0; i < inst->getNumOperands(); ++i) {
1791 //get machine operand
1792 const MachineOperand &mOp = inst->getOperand(i);
1794 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1795 //find the value in the map
1796 if (const Value* srcI = mOp.getVRegValue()) {
1798 //Before we declare this Value* one that we should save
1799 //make sure its def is not of the same stage as this instruction
1800 //because it will be consumed before its used
1801 Instruction *defInst = (Instruction*) srcI;
1803 //Should we save this value?
1806 //Get Machine code for this instruction, and loop backwards over the array
1808 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(defInst);
1809 for (int j = tempMvec.size()-1; j >= 0; j--) {
1810 MachineInstr *temp = tempMvec[j];
1812 //Loop over instructions
1813 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
1814 MachineOperand &mDefOp = temp->getOperand(opNum);
1816 if (mDefOp.getType() == MachineOperand::MO_VirtualRegister && mDefOp.isDef()) {
1817 const Value* defVReg = mDefOp.getVRegValue();
1818 if(defVReg == srcI) {
1819 //Check if instruction has been seen already and is of same stage
1820 if(lastInstrs.count(temp)) {
1821 if(lastInstrs[temp] == I->second)
1829 valuesToSave[srcI] = std::make_pair(I->first, i);
1833 if(mOp.getType() != MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1834 assert("Our assumption is wrong. We have another type of register that needs to be saved\n");
1840 //The new loop will consist of one or more prologues, the kernel, and one or more epilogues.
1842 //Map to keep track of old to new values
1843 std::map<Value*, std::map<int, Value*> > newValues;
1845 //Map to keep track of old to new values in kernel
1846 std::map<Value*, std::map<int, Value*> > kernelPHIs;
1848 //Another map to keep track of what machine basic blocks these new value*s are in since
1849 //they have no llvm instruction equivalent
1850 std::map<Value*, MachineBasicBlock*> newValLocation;
1852 std::vector<MachineBasicBlock*> prologues;
1853 std::vector<BasicBlock*> llvm_prologues;
1857 writePrologues(prologues, BB, llvm_prologues, valuesToSave, newValues, newValLocation);
1859 //Print out epilogues and prologue
1860 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = prologues.begin(), E = prologues.end();
1862 std::cerr << "PROLOGUE\n";
1863 (*I)->print(std::cerr);
1866 BasicBlock *llvmKernelBB = new BasicBlock("Kernel", (Function*) (BB->getBasicBlock()->getParent()));
1867 MachineBasicBlock *machineKernelBB = new MachineBasicBlock(llvmKernelBB);
1868 (((MachineBasicBlock*)BB)->getParent())->getBasicBlockList().push_back(machineKernelBB);
1869 writeKernel(llvmKernelBB, machineKernelBB, valuesToSave, newValues, newValLocation, kernelPHIs);
1872 std::vector<MachineBasicBlock*> epilogues;
1873 std::vector<BasicBlock*> llvm_epilogues;
1876 writeEpilogues(epilogues, BB, llvm_epilogues, valuesToSave, newValues, newValLocation, kernelPHIs);
1879 const TargetInstrInfo *TMI = target.getInstrInfo();
1881 //Fix up machineBB and llvmBB branches
1882 for(unsigned I = 0; I < prologues.size(); ++I) {
1884 MachineInstr *branch = 0;
1885 MachineInstr *branch2 = 0;
1887 //Find terminator since getFirstTerminator does not work!
1888 for(MachineBasicBlock::reverse_iterator mInst = prologues[I]->rbegin(), mInstEnd = prologues[I]->rend(); mInst != mInstEnd; ++mInst) {
1889 MachineOpCode OC = mInst->getOpcode();
1890 if(TMI->isBranch(OC)) {
1891 if(mInst->getOpcode() == V9::BA)
1895 DEBUG(std::cerr << *mInst << "\n");
1896 if(branch !=0 && branch2 !=0)
1902 for(unsigned opNum = 0; opNum < branch->getNumOperands(); ++opNum) {
1903 MachineOperand &mOp = branch->getOperand(opNum);
1904 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1905 //Check if we are branching to the kernel, if not branch to epilogue
1906 if(mOp.getVRegValue() == BB->getBasicBlock()) {
1907 if(I == prologues.size()-1)
1908 mOp.setValueReg(llvmKernelBB);
1910 mOp.setValueReg(llvm_prologues[I+1]);
1913 mOp.setValueReg(llvm_epilogues[(llvm_epilogues.size()-1-I)]);
1918 for(unsigned opNum = 0; opNum < branch2->getNumOperands(); ++opNum) {
1919 MachineOperand &mOp = branch2->getOperand(opNum);
1920 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1921 //Check if we are branching to the kernel, if not branch to epilogue
1922 if(mOp.getVRegValue() == BB->getBasicBlock()) {
1923 if(I == prologues.size()-1)
1924 mOp.setValueReg(llvmKernelBB);
1926 mOp.setValueReg(llvm_prologues[I+1]);
1929 mOp.setValueReg(llvm_epilogues[(llvm_epilogues.size()-1-I)]);
1933 //Update llvm basic block with our new branch instr
1934 DEBUG(std::cerr << BB->getBasicBlock()->getTerminator() << "\n");
1935 const BranchInst *branchVal = dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
1936 //TmpInstruction *tmp = new TmpInstruction(branchVal->getCondition());
1938 //Add TmpInstruction to original branches MCFI
1939 //MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(branchVal);
1940 //tempMvec.addTemp((Value*) tmp);
1942 if(I == prologues.size()-1) {
1943 TerminatorInst *newBranch = new BranchInst(llvmKernelBB,
1944 llvm_epilogues[(llvm_epilogues.size()-1-I)],
1945 branchVal->getCondition(),
1949 TerminatorInst *newBranch = new BranchInst(llvm_prologues[I+1],
1950 llvm_epilogues[(llvm_epilogues.size()-1-I)],
1951 branchVal->getCondition(),
1954 assert(branch != 0 && "There must be a terminator for this machine basic block!\n");
1958 //Fix up kernel machine branches
1959 MachineInstr *branch = 0;
1960 MachineInstr *BAbranch = 0;
1962 for(MachineBasicBlock::reverse_iterator mInst = machineKernelBB->rbegin(), mInstEnd = machineKernelBB->rend(); mInst != mInstEnd; ++mInst) {
1963 MachineOpCode OC = mInst->getOpcode();
1964 if(TMI->isBranch(OC)) {
1965 if(mInst->getOpcode() == V9::BA) {
1975 assert(branch != 0 && "There must be a terminator for the kernel machine basic block!\n");
1977 //Update kernel self loop branch
1978 for(unsigned opNum = 0; opNum < branch->getNumOperands(); ++opNum) {
1979 MachineOperand &mOp = branch->getOperand(opNum);
1981 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1982 mOp.setValueReg(llvmKernelBB);
1986 Value *origBAVal = 0;
1988 //Update kernel BA branch
1989 for(unsigned opNum = 0; opNum < BAbranch->getNumOperands(); ++opNum) {
1990 MachineOperand &mOp = BAbranch->getOperand(opNum);
1991 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1992 origBAVal = mOp.getVRegValue();
1993 if(llvm_epilogues.size() > 0)
1994 mOp.setValueReg(llvm_epilogues[0]);
1999 assert((origBAVal != 0) && "Could not find original branch always value");
2001 //Update kernelLLVM branches
2002 const BranchInst *branchVal = dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
2003 //TmpInstruction *tmp = new TmpInstruction(branchVal->getCondition());
2005 //Add TmpInstruction to original branches MCFI
2006 //MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(branchVal);
2007 //tempMvec.addTemp((Value*) tmp);
2009 assert(llvm_epilogues.size() != 0 && "We must have epilogues!");
2011 TerminatorInst *newBranch = new BranchInst(llvmKernelBB,
2013 branchVal->getCondition(),
2017 //Lastly add unconditional branches for the epilogues
2018 for(unsigned I = 0; I < epilogues.size(); ++I) {
2020 //Now since we don't have fall throughs, add a unconditional branch to the next prologue
2021 if(I != epilogues.size()-1) {
2022 BuildMI(epilogues[I], V9::BA, 1).addPCDisp(llvm_epilogues[I+1]);
2023 //Add unconditional branch to end of epilogue
2024 TerminatorInst *newBranch = new BranchInst(llvm_epilogues[I+1],
2029 BuildMI(epilogues[I], V9::BA, 1).addPCDisp(origBAVal);
2032 //Update last epilogue exit branch
2033 BranchInst *branchVal = (BranchInst*) dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
2034 //Find where we are supposed to branch to
2035 BasicBlock *nextBlock = 0;
2036 for(unsigned j=0; j <branchVal->getNumSuccessors(); ++j) {
2037 if(branchVal->getSuccessor(j) != BB->getBasicBlock())
2038 nextBlock = branchVal->getSuccessor(j);
2041 assert((nextBlock != 0) && "Next block should not be null!");
2042 TerminatorInst *newBranch = new BranchInst(nextBlock, llvm_epilogues[I]);
2045 BuildMI(epilogues[I], V9::NOP, 0);
2049 //FIX UP Machine BB entry!!
2050 //We are looking at the predecesor of our loop basic block and we want to change its ba instruction
2053 //Find all llvm basic blocks that branch to the loop entry and change to our first prologue.
2054 const BasicBlock *llvmBB = BB->getBasicBlock();
2056 std::vector<const BasicBlock*>Preds (pred_begin(llvmBB), pred_end(llvmBB));
2058 //for(pred_const_iterator P = pred_begin(llvmBB), PE = pred_end(llvmBB); P != PE; ++PE) {
2059 for(std::vector<const BasicBlock*>::iterator P = Preds.begin(), PE = Preds.end(); P != PE; ++P) {
2063 DEBUG(std::cerr << "Found our entry BB\n");
2064 //Get the Terminator instruction for this basic block and print it out
2065 DEBUG(std::cerr << *((*P)->getTerminator()) << "\n");
2066 //Update the terminator
2067 TerminatorInst *term = ((BasicBlock*)*P)->getTerminator();
2068 for(unsigned i=0; i < term->getNumSuccessors(); ++i) {
2069 if(term->getSuccessor(i) == llvmBB) {
2070 DEBUG(std::cerr << "Replacing successor bb\n");
2071 if(llvm_prologues.size() > 0) {
2072 term->setSuccessor(i, llvm_prologues[0]);
2073 //Also update its corresponding machine instruction
2074 MachineCodeForInstruction & tempMvec =
2075 MachineCodeForInstruction::get(term);
2076 for (unsigned j = 0; j < tempMvec.size(); j++) {
2077 MachineInstr *temp = tempMvec[j];
2078 MachineOpCode opc = temp->getOpcode();
2079 if(TMI->isBranch(opc)) {
2080 DEBUG(std::cerr << *temp << "\n");
2082 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
2083 MachineOperand &mOp = temp->getOperand(opNum);
2084 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
2085 mOp.setValueReg(llvm_prologues[0]);
2092 term->setSuccessor(i, llvmKernelBB);
2093 //Also update its corresponding machine instruction
2094 MachineCodeForInstruction & tempMvec =
2095 MachineCodeForInstruction::get(term);
2096 for (unsigned j = 0; j < tempMvec.size(); j++) {
2097 MachineInstr *temp = tempMvec[j];
2098 MachineOpCode opc = temp->getOpcode();
2099 if(TMI->isBranch(opc)) {
2100 DEBUG(std::cerr << *temp << "\n");
2102 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
2103 MachineOperand &mOp = temp->getOperand(opNum);
2104 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
2105 mOp.setValueReg(llvmKernelBB);
2117 removePHIs(BB, prologues, epilogues, machineKernelBB, newValLocation);
2121 //Print out epilogues and prologue
2122 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = prologues.begin(), E = prologues.end();
2124 std::cerr << "PROLOGUE\n";
2125 (*I)->print(std::cerr);
2128 DEBUG(std::cerr << "KERNEL\n");
2129 DEBUG(machineKernelBB->print(std::cerr));
2131 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = epilogues.begin(), E = epilogues.end();
2133 std::cerr << "EPILOGUE\n";
2134 (*I)->print(std::cerr);
2138 DEBUG(std::cerr << "New Machine Function" << "\n");
2139 DEBUG(std::cerr << BB->getParent() << "\n");
2141 //BB->getParent()->getBasicBlockList().erase(BB);