aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp')
-rw-r--r--lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp2968
1 files changed, 2922 insertions, 46 deletions
diff --git a/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp b/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp
index bc18027ff4..80594c3451 100644
--- a/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp
+++ b/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp
@@ -17,12 +17,16 @@
#include "DependenceAnalyzer.h"
#include "ModuloSchedulingSuperBlock.h"
+#include "llvm/Constants.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/GraphWriter.h"
+#include "llvm/Support/Timer.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/SCCIterator.h"
#include "llvm/Instructions.h"
#include "../MachineCodeForInstruction.h"
#include "../SparcV9RegisterInfo.h"
@@ -30,6 +34,8 @@
#include "../SparcV9TmpInstr.h"
#include <fstream>
#include <sstream>
+#include <cmath>
+#include <utility>
using namespace llvm;
/// Create ModuloSchedulingSBPass
@@ -39,9 +45,18 @@ FunctionPass *llvm::createModuloSchedulingSBPass(TargetMachine & targ) {
return new ModuloSchedulingSBPass(targ);
}
+
+#if 1
+#define TIME_REGION(VARNAME, DESC) \
+ NamedRegionTimer VARNAME(DESC)
+#else
+#define TIME_REGION(VARNAME, DESC)
+#endif
+
+
//Graph Traits for printing out the dependence graph
template<typename GraphType>
-static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
+static void WriteGraphToFileSB(std::ostream &O, const std::string &GraphName,
const GraphType &GT) {
std::string Filename = GraphName + ".dot";
O << "Writing '" << Filename << "'...";
@@ -58,8 +73,82 @@ namespace llvm {
Statistic<> NumLoops("moduloschedSB-numLoops", "Total Number of Loops");
Statistic<> NumSB("moduloschedSB-numSuperBlocks", "Total Number of SuperBlocks");
Statistic<> BBWithCalls("modulosched-BBCalls", "Basic Blocks rejected due to calls");
- Statistic<> BBWithCondMov("modulosched-loopCondMov", "Basic Blocks rejected due to conditional moves");
+ Statistic<> BBWithCondMov("modulosched-loopCondMov",
+ "Basic Blocks rejected due to conditional moves");
+ Statistic<> SBResourceConstraint("modulosched-resourceConstraint",
+ "Loops constrained by resources");
+ Statistic<> SBRecurrenceConstraint("modulosched-recurrenceConstraint",
+ "Loops constrained by recurrences");
+ Statistic<> SBFinalIISum("modulosched-finalIISum", "Sum of all final II");
+ Statistic<> SBIISum("modulosched-IISum", "Sum of all theoretical II");
+ Statistic<> SBMSLoops("modulosched-schedLoops", "Number of loops successfully modulo-scheduled");
+ Statistic<> SBNoSched("modulosched-noSched", "No schedule");
+ Statistic<> SBSameStage("modulosched-sameStage", "Max stage is 0");
+ Statistic<> SBBLoops("modulosched-SBBLoops", "Number single basic block loops");
+ Statistic<> SBInvalid("modulosched-SBInvalid", "Number invalid superblock loops");
+ Statistic<> SBValid("modulosched-SBValid", "Number valid superblock loops");
+ Statistic<> SBSize("modulosched-SBSize", "Total size of all valid superblocks");
+
+ template<>
+ struct DOTGraphTraits<MSchedGraphSB*> : public DefaultDOTGraphTraits {
+ static std::string getGraphName(MSchedGraphSB *F) {
+ return "Dependence Graph";
+ }
+
+ static std::string getNodeLabel(MSchedGraphSBNode *Node, MSchedGraphSB *Graph) {
+ if(!Node->isPredicate()) {
+ if (Node->getInst()) {
+ std::stringstream ss;
+ ss << *(Node->getInst());
+ return ss.str(); //((MachineInstr*)Node->getInst());
+ }
+ else
+ return "No Inst";
+ }
+ else
+ return "Pred Node";
+ }
+ static std::string getEdgeSourceLabel(MSchedGraphSBNode *Node,
+ MSchedGraphSBNode::succ_iterator I) {
+ //Label each edge with the type of dependence
+ std::string edgelabel = "";
+ switch (I.getEdge().getDepOrderType()) {
+
+ case MSchedGraphSBEdge::TrueDep:
+ edgelabel = "True";
+ break;
+
+ case MSchedGraphSBEdge::AntiDep:
+ edgelabel = "Anti";
+ break;
+
+ case MSchedGraphSBEdge::OutputDep:
+ edgelabel = "Output";
+ break;
+
+ case MSchedGraphSBEdge::NonDataDep:
+ edgelabel = "Pred";
+ break;
+
+ default:
+ edgelabel = "Unknown";
+ break;
+ }
+
+ //FIXME
+ int iteDiff = I.getEdge().getIteDiff();
+ std::string intStr = "(IteDiff: ";
+ intStr += itostr(iteDiff);
+
+ intStr += ")";
+ edgelabel += intStr;
+
+ return edgelabel;
+ }
+ };
+
bool ModuloSchedulingSBPass::runOnFunction(Function &F) {
+ alarm(100);
bool Changed = false;
//Get MachineFunction
@@ -91,14 +180,97 @@ namespace llvm {
continue;
}
- MSchedGraph *MSG = new MSchedGraph(*SB, target, indVarInstrs[*SB], DA,
+ MSchedGraphSB *MSG = new MSchedGraphSB(*SB, target, indVarInstrs[*SB], DA,
machineTollvm[*SB]);
//Write Graph out to file
- DEBUG(WriteGraphToFile(std::cerr, F.getName(), MSG));
- DEBUG(MSG->print(std::cerr));
+ DEBUG(WriteGraphToFileSB(std::cerr, F.getName(), MSG));
+
+ //Calculate Resource II
+ int ResMII = calculateResMII(*SB);
+ //Calculate Recurrence II
+ int RecMII = calculateRecMII(MSG, ResMII);
+
+ DEBUG(std::cerr << "Number of reccurrences found: " << recurrenceList.size() << "\n");
+
+ //Our starting initiation interval is the maximum of RecMII and ResMII
+ if(RecMII < ResMII)
+ ++SBRecurrenceConstraint;
+ else
+ ++SBResourceConstraint;
+
+ II = std::max(RecMII, ResMII);
+ int mII = II;
+
+
+ //Print out II, RecMII, and ResMII
+ DEBUG(std::cerr << "II starts out as " << II << " ( RecMII=" << RecMII << " and ResMII=" << ResMII << ")\n");
+
+ //Calculate Node Properties
+ calculateNodeAttributes(MSG, ResMII);
+
+ //Dump node properties if in debug mode
+ DEBUG(for(std::map<MSchedGraphSBNode*, MSNodeSBAttributes>::iterator I = nodeToAttributesMap.begin(),
+ E = nodeToAttributesMap.end(); I !=E; ++I) {
+ 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";
+ });
+
+
+ //Put nodes in order to schedule them
+ computePartialOrder();
+
+ //Dump out partial order
+ DEBUG(for(std::vector<std::set<MSchedGraphSBNode*> >::iterator I = partialOrder.begin(),
+ E = partialOrder.end(); I !=E; ++I) {
+ std::cerr << "Start set in PO\n";
+ for(std::set<MSchedGraphSBNode*>::iterator J = I->begin(), JE = I->end(); J != JE; ++J)
+ std::cerr << "PO:" << **J << "\n";
+ });
+
+ //Place nodes in final order
+ orderNodes();
+
+ //Dump out order of nodes
+ DEBUG(for(std::vector<MSchedGraphSBNode*>::iterator I = FinalNodeOrder.begin(), E = FinalNodeOrder.end(); I != E; ++I) {
+ std::cerr << "FO:" << **I << "\n";
+ });
+
+
+ //Finally schedule nodes
+ bool haveSched = computeSchedule(*SB, MSG);
+
+ //Print out final schedule
+ DEBUG(schedule.print(std::cerr));
+
+ //Final scheduling step is to reconstruct the loop only if we actual have
+ //stage > 0
+ if(haveSched) {
+ //schedule.printSchedule(std::cerr);
+ reconstructLoop(*SB);
+ ++SBMSLoops;
+ //Changed = true;
+ SBIISum += mII;
+ SBFinalIISum += II;
+
+ if(schedule.getMaxStage() == 0)
+ ++SBSameStage;
+ }
+ else
+ ++SBNoSched;
+
+ //Clear out our maps for the next basic block that is processed
+ nodeToAttributesMap.clear();
+ partialOrder.clear();
+ recurrenceList.clear();
+ FinalNodeOrder.clear();
+ schedule.clear();
+ defMap.clear();
+
}
+ alarm(0);
return Changed;
}
@@ -145,63 +317,211 @@ namespace llvm {
MachineBasicBlock *currentMBB = bbMap[header];
bool done = false;
bool success = true;
+ unsigned offset = 0;
+ std::map<const MachineInstr*, unsigned> indexMap;
while(!done) {
-
- if(MachineBBisValid(currentMBB)) {
-
- //Loop over successors of this BB, they should be in the loop block
- //and be valid
- BasicBlock *next = 0;
- for(succ_iterator I = succ_begin(current), E = succ_end(current);
- I != E; ++I) {
- if(L->contains(*I)) {
- if(!next)
- next = *I;
- else {
- done = true;
- success = false;
- break;
- }
- }
- }
- if(success) {
- superBlock.push_back(currentMBB);
- if(next == header)
- done = true;
- else if(!next->getSinglePredecessor()) {
+ //Loop over successors of this BB, they should be in the
+ //loop block and be valid
+ BasicBlock *next = 0;
+ for(succ_iterator I = succ_begin(current), E = succ_end(current);
+ I != E; ++I) {
+ if(L->contains(*I)) {
+ if(!next)
+ next = *I;
+ else {
done = true;
success = false;
- }
- else {
- //Check that the next BB only has one entry
- current = next;
- assert(bbMap.count(current) && "LLVM BB must have corresponding Machine BB");
- currentMBB = bbMap[current];
+ break;
}
}
}
- else {
- done = true;
- success = false;
+
+ if(success) {
+ superBlock.push_back(currentMBB);
+ if(next == header)
+ done = true;
+ else if(!next->getSinglePredecessor()) {
+ done = true;
+ success = false;
+ }
+ else {
+ //Check that the next BB only has one entry
+ current = next;
+ assert(bbMap.count(current) && "LLVM BB must have corresponding Machine BB");
+ currentMBB = bbMap[current];
+ }
}
}
+
+
+
+
if(success) {
++NumSB;
- Worklist.push_back(superBlock);
+
+ //Loop over all the blocks in the superblock
+ for(std::vector<const MachineBasicBlock*>::iterator currentMBB = superBlock.begin(), MBBEnd = superBlock.end(); currentMBB != MBBEnd; ++currentMBB) {
+ if(!MachineBBisValid(*currentMBB, indexMap, offset)) {
+ success = false;
+ break;
+ }
+ }
+ }
+
+ if(success) {
+ if(getIndVar(superBlock, bbMap, indexMap)) {
+ ++SBValid;
+ Worklist.push_back(superBlock);
+ SBSize += superBlock.size();
+ }
+ else
+ ++SBInvalid;
}
+ }
+ }
+ }
+
+
+ bool ModuloSchedulingSBPass::getIndVar(std::vector<const MachineBasicBlock*> &superBlock, std::map<BasicBlock*, MachineBasicBlock*> &bbMap,
+ std::map<const MachineInstr*, unsigned> &indexMap) {
+ //See if we can get induction var instructions
+ std::set<const BasicBlock*> llvmSuperBlock;
+
+ for(unsigned i =0; i < superBlock.size(); ++i)
+ llvmSuperBlock.insert(superBlock[i]->getBasicBlock());
+
+ //Get Target machine instruction info
+ const TargetInstrInfo *TMI = target.getInstrInfo();
+
+ //Get the loop back branch
+ BranchInst *b = dyn_cast<BranchInst>(((BasicBlock*) (superBlock[superBlock.size()-1])->getBasicBlock())->getTerminator());
+ std::set<Instruction*> indVar;
+
+ if(b->isConditional()) {
+ //Get the condition for the branch
+ Value *cond = b->getCondition();
+
+ DEBUG(std::cerr << "Condition: " << *cond << "\n");
+
+ //List of instructions associated with induction variable
+ std::vector<Instruction*> stack;
+
+ //Add branch
+ indVar.insert(b);
+
+ if(Instruction *I = dyn_cast<Instruction>(cond))
+ if(bbMap.count(I->getParent())) {
+ if (!assocIndVar(I, indVar, stack, bbMap, superBlock[(superBlock.size()-1)]->getBasicBlock(), llvmSuperBlock))
+ return false;
+ }
+ else
+ return false;
+ else
+ return false;
+ }
+ else {
+ indVar.insert(b);
+ }
+
+ //Dump out instructions associate with indvar for debug reasons
+ DEBUG(for(std::set<Instruction*>::iterator N = indVar.begin(), NE = indVar.end();
+ N != NE; ++N) {
+ std::cerr << **N << "\n";
+ });
+
+ //Create map of machine instr to llvm instr
+ std::map<MachineInstr*, Instruction*> mllvm;
+ for(std::vector<const MachineBasicBlock*>::iterator MBB = superBlock.begin(), MBE = superBlock.end(); MBB != MBE; ++MBB) {
+ BasicBlock *BB = (BasicBlock*) (*MBB)->getBasicBlock();
+ for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
+ MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(I);
+ for (unsigned j = 0; j < tempMvec.size(); j++) {
+ mllvm[tempMvec[j]] = I;
+ }
+ }
+ }
+ //Convert list of LLVM Instructions to list of Machine instructions
+ std::map<const MachineInstr*, unsigned> mIndVar;
+ for(std::set<Instruction*>::iterator N = indVar.begin(),
+ NE = indVar.end(); N != NE; ++N) {
+
+ //If we have a load, we can't handle this loop because
+ //there is no way to preserve dependences between loads
+ //and stores
+ if(isa<LoadInst>(*N))
+ return false;
+
+ MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(*N);
+ for (unsigned j = 0; j < tempMvec.size(); j++) {
+ MachineOpCode OC = (tempMvec[j])->getOpcode();
+ if(TMI->isNop(OC))
+ continue;
+ if(!indexMap.count(tempMvec[j]))
+ continue;
+ mIndVar[(MachineInstr*) tempMvec[j]] = indexMap[(MachineInstr*) tempMvec[j]];
+ DEBUG(std::cerr << *(tempMvec[j]) << " at index " << indexMap[(MachineInstr*) tempMvec[j]] << "\n");
+ }
}
+
+ //Put into a map for future access
+ indVarInstrs[superBlock] = mIndVar;
+ machineTollvm[superBlock] = mllvm;
+
+ return true;
+
+ }
+ bool ModuloSchedulingSBPass::assocIndVar(Instruction *I,
+ std::set<Instruction*> &indVar,
+ std::vector<Instruction*> &stack,
+ std::map<BasicBlock*, MachineBasicBlock*> &bbMap,
+ const BasicBlock *last, std::set<const BasicBlock*> &llvmSuperBlock) {
+
+ stack.push_back(I);
+
+ //If this is a phi node, check if its the canonical indvar
+ if(PHINode *PN = dyn_cast<PHINode>(I)) {
+ if(llvmSuperBlock.count(PN->getParent())) {
+ if (Instruction *Inc =
+ dyn_cast<Instruction>(PN->getIncomingValueForBlock(last)))
+ if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
+ if (CI->equalsInt(1)) {
+ //We have found the indvar, so add the stack, and inc instruction to the set
+ indVar.insert(stack.begin(), stack.end());
+ indVar.insert(Inc);
+ stack.pop_back();
+ return true;
+ }
+ return false;
+ }
}
+ else {
+ //Loop over each of the instructions operands, check if they are an instruction and in this BB
+ for(unsigned i = 0; i < I->getNumOperands(); ++i) {
+ if(Instruction *N = dyn_cast<Instruction>(I->getOperand(i))) {
+ if(bbMap.count(N->getParent()))
+ if(!assocIndVar(N, indVar, stack, bbMap, last, llvmSuperBlock))
+ return false;
+ }
+ }
+ }
+
+ stack.pop_back();
+ return true;
}
+
/// This function checks if a Machine Basic Block is valid for modulo
/// scheduling. This means that it has no control flow (if/else or
/// calls) in the block. Currently ModuloScheduling only works on
/// single basic block loops.
- bool ModuloSchedulingSBPass::MachineBBisValid(const MachineBasicBlock *BI) {
+ bool ModuloSchedulingSBPass::MachineBBisValid(const MachineBasicBlock *BI,
+ std::map<const MachineInstr*, unsigned> &indexMap,
+ unsigned &offset) {
//Check size of our basic block.. make sure we have more then just the terminator in it
if(BI->getBasicBlock()->size() == 1)
@@ -210,9 +530,6 @@ namespace llvm {
//Get Target machine instruction info
const TargetInstrInfo *TMI = target.getInstrInfo();
- //Check each instruction and look for calls, keep map to get index later
- std::map<const MachineInstr*, unsigned> indexMap;
-
unsigned count = 0;
for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
//Get opcode to check instruction type
@@ -236,7 +553,7 @@ namespace llvm {
return false;
}
- indexMap[I] = count;
+ indexMap[I] = count + offset;
if(TMI->isNop(OC))
continue;
@@ -244,6 +561,8 @@ namespace llvm {
++count;
}
+ offset += count;
+
return true;
}
}
@@ -258,10 +577,16 @@ bool ModuloSchedulingSBPass::CreateDefMap(std::vector<const MachineBasicBlock*>
for(unsigned opNum = 0; opNum < I->getNumOperands(); ++opNum) {
const MachineOperand &mOp = I->getOperand(opNum);
if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef()) {
-
+ Value *V = mOp.getVRegValue();
//assert if this is the second def we have seen
- assert(!defMap.count(mOp.getVRegValue()) && "Def already in the map");
- defMap[mOp.getVRegValue()] = (MachineInstr*) &*I;
+ if(defMap.count(V) && isa<PHINode>(V))
+ DEBUG(std::cerr << "FIXME: Dup def for phi!\n");
+ else {
+ //assert(!defMap.count(V) && "Def already in the map");
+ if(defMap.count(V))
+ return false;
+ defMap[V] = (MachineInstr*) &*I;
+ }
}
//See if we can use this Value* as our defaultInst
@@ -280,3 +605,2554 @@ bool ModuloSchedulingSBPass::CreateDefMap(std::vector<const MachineBasicBlock*>
return true;
}
+
+
+//ResMII is calculated by determining the usage count for each resource
+//and using the maximum.
+//FIXME: In future there should be a way to get alternative resources
+//for each instruction
+int ModuloSchedulingSBPass::calculateResMII(std::vector<const MachineBasicBlock*> &superBlock) {
+
+ TIME_REGION(X, "calculateResMII");
+
+ const TargetInstrInfo *mii = target.getInstrInfo();
+ const TargetSchedInfo *msi = target.getSchedInfo();
+
+ int ResMII = 0;
+
+ //Map to keep track of usage count of each resource
+ std::map<unsigned, unsigned> resourceUsageCount;
+
+ for(std::vector<const MachineBasicBlock*>::iterator BI = superBlock.begin(), BE = superBlock.end(); BI != BE; ++BI) {
+ for(MachineBasicBlock::const_iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I) {
+
+ //Get resource usage for this instruction
+ InstrRUsage rUsage = msi->getInstrRUsage(I->getOpcode());
+ std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle;
+
+ //Loop over resources in each cycle and increments their usage count
+ for(unsigned i=0; i < resources.size(); ++i)
+ for(unsigned j=0; j < resources[i].size(); ++j) {
+ if(!resourceUsageCount.count(resources[i][j])) {
+ resourceUsageCount[resources[i][j]] = 1;
+ }
+ else {
+ resourceUsageCount[resources[i][j]] = resourceUsageCount[resources[i][j]] + 1;
+ }
+ }
+ }
+ }
+
+ //Find maximum usage count
+
+ //Get max number of instructions that can be issued at once. (FIXME)
+ int issueSlots = msi->maxNumIssueTotal;
+
+ for(std::map<unsigned,unsigned>::iterator RB = resourceUsageCount.begin(), RE = resourceUsageCount.end(); RB != RE; ++RB) {
+
+ //Get the total number of the resources in our cpu
+ int resourceNum = CPUResource::getCPUResource(RB->first)->maxNumUsers;
+
+ //Get total usage count for this resources
+ unsigned usageCount = RB->second;
+
+ //Divide the usage count by either the max number we can issue or the number of
+ //resources (whichever is its upper bound)
+ double finalUsageCount;
+ DEBUG(std::cerr << "Resource Num: " << RB->first << " Usage: " << usageCount << " TotalNum: " << resourceNum << "\n");
+
+ if( resourceNum <= issueSlots)
+ finalUsageCount = ceil(1.0 * usageCount / resourceNum);
+ else
+ finalUsageCount = ceil(1.0 * usageCount / issueSlots);
+
+
+ //Only keep track of the max
+ ResMII = std::max( (int) finalUsageCount, ResMII);
+
+ }
+
+ return ResMII;
+
+}
+
+/// calculateRecMII - Calculates the value of the highest recurrence
+/// By value we mean the total latency/distance
+int ModuloSchedulingSBPass::calculateRecMII(MSchedGraphSB *graph, int MII) {
+
+ TIME_REGION(X, "calculateRecMII");
+
+ findAllCircuits(graph, MII);
+ int RecMII = 0;
+
+ for(std::set<std::pair<int, std::vector<MSchedGraphSBNode*> > >::iterator I = recurrenceList.begin(), E=recurrenceList.end(); I !=E; ++I) {
+ RecMII = std::max(RecMII, I->first);
+ }
+
+ return MII;
+}
+
+int CircCountSB;
+
+void ModuloSchedulingSBPass::unblock(MSchedGraphSBNode *u, std::set<MSchedGraphSBNode*> &blocked,
+ std::map<MSchedGraphSBNode*, std::set<MSchedGraphSBNode*> > &B) {
+
+ //Unblock u
+ DEBUG(std::cerr << "Unblocking: " << *u << "\n");
+ blocked.erase(u);
+
+ //std::set<MSchedGraphSBNode*> toErase;
+ while (!B[u].empty()) {
+ MSchedGraphSBNode *W = *B[u].begin();
+ B[u].erase(W);
+ //toErase.insert(*W);
+ DEBUG(std::cerr << "Removed: " << *W << "from B-List\n");
+ if(blocked.count(W))
+ unblock(W, blocked, B);
+ }
+
+}
+
+void ModuloSchedulingSBPass::addSCC(std::vector<MSchedGraphSBNode*> &SCC, std::map<MSchedGraphSBNode*, MSchedGraphSBNode*> &newNodes) {
+
+ int totalDelay = 0;
+ int totalDistance = 0;
+ std::vector<MSchedGraphSBNode*> recc;
+ MSchedGraphSBNode *start = 0;
+ MSchedGraphSBNode *end = 0;
+
+ //Loop over recurrence, get delay and distance
+ for(std::vector<MSchedGraphSBNode*>::iterator N = SCC.begin(), NE = SCC.end(); N != NE; ++N) {
+ DEBUG(std::cerr << **N << "\n");
+ totalDelay += (*N)->getLatency();
+
+ for(unsigned i = 0; i < (*N)->succ_size(); ++i) {
+ MSchedGraphSBEdge *edge = (*N)->getSuccessor(i);
+ if(find(SCC.begin(), SCC.end(), edge->getDest()) != SCC.end()) {
+ totalDistance += edge->getIteDiff();
+ if(edge->getIteDiff() > 0)
+ if(!start && !end) {
+ start = *N;
+ end = edge->getDest();
+ }
+
+ }
+ }
+
+
+ //Get the original node
+ recc.push_back(newNodes[*N]);
+
+
+ }
+
+ DEBUG(std::cerr << "End Recc\n");
+
+
+ assert( (start && end) && "Must have start and end node to ignore edge for SCC");
+
+ if(start && end) {
+ //Insert reccurrence into the list
+ DEBUG(std::cerr << "Ignore Edge from!!: " << *start << " to " << *end << "\n");
+ edgesToIgnore.insert(std::make_pair(newNodes[start], (newNodes[end])->getInEdgeNum(newNodes[start])));
+ }
+
+ int lastII = totalDelay / totalDistance;
+
+
+ recurrenceList.insert(std::make_pair(lastII, recc));
+
+}
+
+bool ModuloSchedulingSBPass::circuit(MSchedGraphSBNode *v, std::vector<MSchedGraphSBNode*> &stack,
+ std::set<MSchedGraphSBNode*> &blocked, std::vector<MSchedGraphSBNode*> &SCC,
+ MSchedGraphSBNode *s, std::map<MSchedGraphSBNode*, std::set<MSchedGraphSBNode*> > &B,
+ int II, std::map<MSchedGraphSBNode*, MSchedGraphSBNode*> &newNodes) {
+ bool f = false;
+
+ DEBUG(std::cerr << "Finding Circuits Starting with: ( " << v << ")"<< *v << "\n");
+
+ //Push node onto the stack
+ stack.push_back(v);
+
+ //block this node
+ blocked.insert(v);
+
+ //Loop over all successors of node v that are in the scc, create Adjaceny list
+ std::set<MSchedGraphSBNode*> AkV;
+ for(MSchedGraphSBNode::succ_iterator I = v->succ_begin(), E = v->succ_end(); I != E; ++I) {
+ if((std::find(SCC.begin(), SCC.end(), *I) != SCC.end())) {
+ AkV.insert(*I);
+ }
+ }
+
+ for(std::set<MSchedGraphSBNode*>::iterator I = AkV.begin(), E = AkV.end(); I != E; ++I) {
+ if(*I == s) {
+ //We have a circuit, so add it to our list
+ addRecc(stack, newNodes);
+ f = true;
+ }
+ else if(!blocked.count(*I)) {
+ if(circuit(*I, stack, blocked, SCC, s, B, II, newNodes))
+ f = true;
+ }
+ else
+ DEBUG(std::cerr << "Blocked: " << **I << "\n");
+ }
+
+
+ if(f) {
+ unblock(v, blocked, B);
+ }
+ else {
+ for(std::set<MSchedGraphSBNode*>::iterator I = AkV.begin(), E = AkV.end(); I != E; ++I)
+ B[*I].insert(v);
+
+ }
+
+ //Pop v
+ stack.pop_back();
+
+ return f;
+
+}
+
+void ModuloSchedulingSBPass::addRecc(std::vector<MSchedGraphSBNode*> &stack, std::map<MSchedGraphSBNode*, MSchedGraphSBNode*> &newNodes) {
+ std::vector<MSchedGraphSBNode*> recc;
+ //Dump recurrence for now
+ DEBUG(std::cerr << "Starting Recc\n");
+
+ int totalDelay = 0;
+ int totalDistance = 0;
+ MSchedGraphSBNode *lastN = 0;
+ MSchedGraphSBNode *start = 0;
+ MSchedGraphSBNode *end = 0;
+
+ //Loop over recurrence, get delay and distance
+ for(std::vector<MSchedGraphSBNode*>::iterator N = stack.begin(), NE = stack.end(); N != NE; ++N) {
+ DEBUG(std::cerr << **N << "\n");
+ totalDelay += (*N)->getLatency();
+ if(lastN) {
+ int iteDiff = (*N)->getInEdge(lastN).getIteDiff();
+ totalDistance += iteDiff;
+
+ if(iteDiff > 0) {
+ start = lastN;
+ end = *N;
+ }
+ }
+ //Get the original node
+ lastN = *N;
+ recc.push_back(newNodes[*N]);
+
+
+ }
+
+ //Get the loop edge
+ totalDistance += lastN->getIteDiff(*stack.begin());
+
+ DEBUG(std::cerr << "End Recc\n");
+ CircCountSB++;
+
+ if(start && end) {
+ //Insert reccurrence into the list
+ DEBUG(std::cerr << "Ignore Edge from!!: " << *start << " to " << *end << "\n");
+ edgesToIgnore.insert(std::make_pair(newNodes[start], (newNodes[end])->getInEdgeNum(newNodes[start])));
+ }
+ else {
+ //Insert reccurrence into the list
+ DEBUG(std::cerr << "Ignore Edge from: " << *lastN << " to " << **stack.begin() << "\n");
+ edgesToIgnore.insert(std::make_pair(newNodes[lastN], newNodes[(*stack.begin())]->getInEdgeNum(newNodes[lastN])));
+
+ }
+ //Adjust II until we get close to the inequality delay - II*distance <= 0
+ int RecMII = II; //Starting value
+ int value = totalDelay-(RecMII * totalDistance);
+ int lastII = II;
+ while(value < 0) {
+
+ lastII = RecMII;
+ RecMII--;
+ value = totalDelay-(RecMII * totalDistance);
+ }
+
+ recurrenceList.insert(std::make_pair(lastII, recc));
+
+}
+
+
+void ModuloSchedulingSBPass::findAllCircuits(MSchedGraphSB *g, int II) {
+
+ CircCountSB = 0;
+
+ //Keep old to new node mapping information
+ std::map<MSchedGraphSBNode*, MSchedGraphSBNode*> newNodes;
+
+ //copy the graph
+ MSchedGraphSB *MSG = new MSchedGraphSB(*g, newNodes);
+
+ DEBUG(std::cerr << "Finding All Circuits\n");
+
+ //Set of blocked nodes
+ std::set<MSchedGraphSBNode*> blocked;
+
+ //Stack holding current circuit
+ std::vector<MSchedGraphSBNode*> stack;
+
+ //Map for B Lists
+ std::map<MSchedGraphSBNode*, std::set<MSchedGraphSBNode*> > B;
+
+ //current node
+ MSchedGraphSBNode *s;
+
+
+ //Iterate over the graph until its down to one node or empty
+ while(MSG->size() > 1) {
+
+ //Write Graph out to file
+ //WriteGraphToFile(std::cerr, "Graph" + utostr(MSG->size()), MSG);
+
+ DEBUG(std::cerr << "Graph Size: " << MSG->size() << "\n");
+ DEBUG(std::cerr << "Finding strong component Vk with least vertex\n");
+
+ //Iterate over all the SCCs in the graph
+ std::set<MSchedGraphSBNode*> Visited;
+ std::vector<MSchedGraphSBNode*> Vk;
+ MSchedGraphSBNode* s = 0;
+ int numEdges = 0;
+
+ //Find scc with the least vertex
+ for (MSchedGraphSB::iterator GI = MSG->begin(), E = MSG->end(); GI != E; ++GI)
+ if (Visited.insert(GI->second).second) {
+ for (scc_iterator<MSchedGraphSBNode*> SCCI = scc_begin(GI->second),
+ E = scc_end(GI->second); SCCI != E; ++SCCI) {
+ std::vector<MSchedGraphSBNode*> &nextSCC = *SCCI;
+
+ if (Visited.insert(nextSCC[0]).second) {
+ Visited.insert(nextSCC.begin()+1, nextSCC.end());
+
+ if(nextSCC.size() > 1) {
+ DEBUG(std::cerr << "SCC size: " << nextSCC.size() << "\n");
+
+ for(unsigned i = 0; i < nextSCC.size(); ++i) {
+ //Loop over successor and see if in scc, then count edge
+ MSchedGraphSBNode *node = nextSCC[i];
+ for(MSchedGraphSBNode::succ_iterator S = node->succ_begin(), SE = node->succ_end(); S != SE; ++S) {
+ if(find(nextSCC.begin(), nextSCC.end(), *S) != nextSCC.end())
+ numEdges++;
+ }
+ }
+ DEBUG(std::cerr << "Num Edges: " << numEdges << "\n");
+ }
+
+ //Ignore self loops
+ if(nextSCC.size() > 1) {
+
+ //Get least vertex in Vk
+ if(!s) {
+ s = nextSCC[0];
+ Vk = nextSCC;
+ }
+
+ for(unsigned i = 0; i < nextSCC.size(); ++i) {
+ if(nextSCC[i] < s) {
+ s = nextSCC[i];
+ Vk = nextSCC;
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+
+ //Process SCC
+ DEBUG(for(std::vector<MSchedGraphSBNode*>::iterator N = Vk.begin(), NE = Vk.end();
+ N != NE; ++N) { std::cerr << *((*N)->getInst()); });
+
+ //Iterate over all nodes in this scc
+ for(std::vector<MSchedGraphSBNode*>::iterator N = Vk.begin(), NE = Vk.end();
+ N != NE; ++N) {
+ blocked.erase(*N);
+ B[*N].clear();
+ }
+ if(Vk.size() > 1) {
+ if(numEdges < 98)
+ circuit(s, stack, blocked, Vk, s, B, II, newNodes);
+ else
+ addSCC(Vk, newNodes);
+
+
+ //Delete nodes from the graph
+ //Find all nodes up to s and delete them
+ std::vector<MSchedGraphSBNode*> nodesToRemove;
+ nodesToRemove.push_back(s);
+ for(MSchedGraphSB::iterator N = MSG->begin(), NE = MSG->end(); N != NE; ++N) {
+ if(N->second < s )
+ nodesToRemove.push_back(N->second);
+ }
+ for(std::vector<MSchedGraphSBNode*>::iterator N = nodesToRemove.begin(), NE = nodesToRemove.end(); N != NE; ++N) {
+ DEBUG(std::cerr << "Deleting Node: " << **N << "\n");
+ MSG->deleteNode(*N);
+ }
+ }
+ else
+ break;
+ }
+ DEBUG(std::cerr << "Num Circuits found: " << CircCountSB << "\n");
+}
+/// calculateNodeAttributes - The following properties are calculated for
+/// each node in the dependence graph: ASAP, ALAP, Depth, Height, and
+/// MOB.
+void ModuloSchedulingSBPass::calculateNodeAttributes(MSchedGraphSB *graph, int MII) {
+
+ TIME_REGION(X, "calculateNodeAttributes");
+
+ assert(nodeToAttributesMap.empty() && "Node attribute map was not cleared");
+
+ //Loop over the nodes and add them to the map
+ for(MSchedGraphSB::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
+
+ DEBUG(std::cerr << "Inserting node into attribute map: " << *I->second << "\n");
+
+ //Assert if its already in the map
+ assert(nodeToAttributesMap.count(I->second) == 0 &&
+ "Node attributes are already in the map");
+
+ //Put into the map with default attribute values
+ nodeToAttributesMap[I->second] = MSNodeSBAttributes();
+ }
+
+ //Create set to deal with reccurrences
+ std::set<MSchedGraphSBNode*> visitedNodes;
+
+ //Now Loop over map and calculate the node attributes
+ for(std::map<MSchedGraphSBNode*, MSNodeSBAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
+ calculateASAP(I->first, MII, (MSchedGraphSBNode*) 0);
+ visitedNodes.clear();
+ }
+
+ int maxASAP = findMaxASAP();
+ //Calculate ALAP which depends on ASAP being totally calculated
+ for(std::map<MSchedGraphSBNode*, MSNodeSBAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
+ calculateALAP(I->first, MII, maxASAP, (MSchedGraphSBNode*) 0);
+ visitedNodes.clear();
+ }
+
+ //Calculate MOB which depends on ASAP being totally calculated, also do depth and height
+ for(std::map<MSchedGraphSBNode*, MSNodeSBAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
+ (I->second).MOB = std::max(0,(I->second).ALAP - (I->second).ASAP);
+
+ DEBUG(std::cerr << "MOB: " << (I->second).MOB << " (" << *(I->first) << ")\n");
+ calculateDepth(I->first, (MSchedGraphSBNode*) 0);
+ calculateHeight(I->first, (MSchedGraphSBNode*) 0);
+ }
+
+
+}
+
+/// ignoreEdge - Checks to see if this edge of a recurrence should be ignored or not
+bool ModuloSchedulingSBPass::ignoreEdge(MSchedGraphSBNode *srcNode, MSchedGraphSBNode *destNode) {
+ if(destNode == 0 || srcNode ==0)
+ return false;
+
+ bool findEdge = edgesToIgnore.count(std::make_pair(srcNode, destNode->getInEdgeNum(srcNode)));
+
+ DEBUG(std::cerr << "Ignoring edge? from: " << *srcNode << " to " << *destNode << "\n");
+
+ return findEdge;
+}
+
+
+/// calculateASAP - Calculates the
+int ModuloSchedulingSBPass::calculateASAP(MSchedGraphSBNode *node, int MII, MSchedGraphSBNode *destNode) {
+
+ DEBUG(std::cerr << "Calculating ASAP for " << *node << "\n");
+
+ //Get current node attributes
+ MSNodeSBAttributes &attributes = nodeToAttributesMap.find(node)->second;
+
+ if(attributes.ASAP != -1)
+ return attributes.ASAP;
+
+ int maxPredValue = 0;
+
+ //Iterate over all of the predecessors and find max
+ for(MSchedGraphSBNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
+
+ //Only process if we are not ignoring the edge
+ if(!ignoreEdge(*P, node)) {
+ int predASAP = -1;
+ predASAP = calculateASAP(*P, MII, node);
+
+ assert(predASAP != -1 && "ASAP has not been calculated");
+ int iteDiff = node->getInEdge(*P).getIteDiff();
+
+ int currentPredValue = predASAP + (*P)->getLatency() - (iteDiff * MII);
+ DEBUG(std::cerr << "pred ASAP: " << predASAP << ", iteDiff: " << iteDiff << ", PredLatency: " << (*P)->getLatency() << ", Current ASAP pred: " << currentPredValue << "\n");
+ maxPredValue = std::max(maxPredValue, currentPredValue);
+ }
+ }
+
+ attributes.ASAP = maxPredValue;
+
+ DEBUG(std::cerr << "ASAP: " << attributes.ASAP << " (" << *node <&l