From 2706983c48d001b042896c4302c19a197b802fb6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 20 Apr 2006 17:15:44 +0000 Subject: This target is no longer built. The ,v files now live in the reoptimizer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27885 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../ModuloSchedulingSuperBlock.cpp | 3155 -------------------- 1 file changed, 3155 deletions(-) delete mode 100644 lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp (limited to 'lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp') diff --git a/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp b/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp deleted file mode 100644 index 8b3185155e..0000000000 --- a/lib/Target/SparcV9/ModuloScheduling/ModuloSchedulingSuperBlock.cpp +++ /dev/null @@ -1,3155 +0,0 @@ -//===-- ModuloSchedulingSuperBlock.cpp - ModuloScheduling--------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This ModuloScheduling pass is based on the Swing Modulo Scheduling -// algorithm, but has been extended to support SuperBlocks (multiple -// basic block, single entry, multipl exit loops). -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "ModuloSchedSB" - -#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" -#include "../SparcV9Internals.h" -#include "../SparcV9TmpInstr.h" -#include -#include -#include -#include - -using namespace llvm; -/// Create ModuloSchedulingSBPass -/// -FunctionPass *llvm::createModuloSchedulingSBPass(TargetMachine & targ) { - DEBUG(std::cerr << "Created ModuloSchedulingSBPass\n"); - 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 -static void WriteGraphToFileSB(std::ostream &O, const std::string &GraphName, - const GraphType >) { - std::string Filename = GraphName + ".dot"; - O << "Writing '" << Filename << "'..."; - std::ofstream F(Filename.c_str()); - - if (F.good()) - WriteGraph(F, GT); - else - O << " error opening file for writing!"; - O << "\n"; -}; - -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<> 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 : 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) { - bool Changed = false; - - //Get MachineFunction - MachineFunction &MF = MachineFunction::get(&F); - - //Get Loop Info & Dependence Anaysis info - LoopInfo &LI = getAnalysis(); - DependenceAnalyzer &DA = getAnalysis(); - - //Worklist of superblocks - std::vector > Worklist; - FindSuperBlocks(F, LI, Worklist); - - DEBUG(if(Worklist.size() == 0) std::cerr << "No superblocks in function to ModuloSchedule\n"); - - //Loop over worklist and ModuloSchedule each SuperBlock - for(std::vector >::iterator SB = Worklist.begin(), - SBE = Worklist.end(); SB != SBE; ++SB) { - - //Print out Superblock - DEBUG(std::cerr << "ModuloScheduling SB: \n"; - for(std::vector::const_iterator BI = SB->begin(), - BE = SB->end(); BI != BE; ++BI) { - (*BI)->print(std::cerr);}); - - if(!CreateDefMap(*SB)) { - defaultInst = 0; - defMap.clear(); - continue; - } - - MSchedGraphSB *MSG = new MSchedGraphSB(*SB, target, indVarInstrs[*SB], DA, - machineTollvm[*SB]); - - //Write Graph out to file - 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::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 >::iterator I = partialOrder.begin(), - E = partialOrder.end(); I !=E; ++I) { - std::cerr << "Start set in PO\n"; - for(std::set::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::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(); - - } - return Changed; - } - - void ModuloSchedulingSBPass::FindSuperBlocks(Function &F, LoopInfo &LI, - std::vector > &Worklist) { - - //Get MachineFunction - MachineFunction &MF = MachineFunction::get(&F); - - //Map of LLVM BB to machine BB - std::map bbMap; - - for (MachineFunction::iterator BI = MF.begin(); BI != MF.end(); ++BI) { - BasicBlock *llvmBB = (BasicBlock*) BI->getBasicBlock(); - assert(!bbMap.count(llvmBB) && "LLVM BB already in map!"); - bbMap[llvmBB] = &*BI; - } - - //Iterate over the loops, and find super blocks - for(LoopInfo::iterator LB = LI.begin(), LE = LI.end(); LB != LE; ++LB) { - Loop *L = *LB; - ++NumLoops; - - //If loop is not single entry, try the next one - if(!L->getLoopPreheader()) - continue; - - //Check size of this loop, we don't want SBB loops - if(L->getBlocks().size() == 1) - continue; - - //Check if this loop contains no sub loops - if(L->getSubLoops().size() == 0) { - - std::vector superBlock; - - //Get Loop Headers - BasicBlock *header = L->getHeader(); - - //Follow the header and make sure each BB only has one entry and is valid - BasicBlock *current = header; - assert(bbMap.count(current) && "LLVM BB must have corresponding Machine BB\n"); - MachineBasicBlock *currentMBB = bbMap[header]; - bool done = false; - bool success = true; - unsigned offset = 0; - std::map indexMap; - - while(!done) { - //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()) { - 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; - - //Loop over all the blocks in the superblock - for(std::vector::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 &superBlock, std::map &bbMap, - std::map &indexMap) { - //See if we can get induction var instructions - std::set 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(((BasicBlock*) (superBlock[superBlock.size()-1])->getBasicBlock())->getTerminator()); - std::set 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 stack; - - //Add branch - indVar.insert(b); - - if(Instruction *I = dyn_cast(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::iterator N = indVar.begin(), NE = indVar.end(); - N != NE; ++N) { - std::cerr << **N << "\n"; - }); - - //Create map of machine instr to llvm instr - std::map mllvm; - for(std::vector::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 mIndVar; - for(std::set::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(*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 &indVar, - std::vector &stack, - std::map &bbMap, - const BasicBlock *last, std::set &llvmSuperBlock) { - - stack.push_back(I); - - //If this is a phi node, check if its the canonical indvar - if(PHINode *PN = dyn_cast(I)) { - if(llvmSuperBlock.count(PN->getParent())) { - if (Instruction *Inc = - dyn_cast(PN->getIncomingValueForBlock(last))) - if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN) - if (ConstantInt *CI = dyn_cast(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(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, - std::map &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) - return false; - - //Get Target machine instruction info - const TargetInstrInfo *TMI = target.getInstrInfo(); - - unsigned count = 0; - for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) { - //Get opcode to check instruction type - MachineOpCode OC = I->getOpcode(); - - //Look for calls - if(TMI->isCall(OC)) { - ++BBWithCalls; - return false; - } - - //Look for conditional move - if(OC == V9::MOVRZr || OC == V9::MOVRZi || OC == V9::MOVRLEZr || OC == V9::MOVRLEZi - || OC == V9::MOVRLZr || OC == V9::MOVRLZi || OC == V9::MOVRNZr || OC == V9::MOVRNZi - || OC == V9::MOVRGZr || OC == V9::MOVRGZi || OC == V9::MOVRGEZr - || OC == V9::MOVRGEZi || OC == V9::MOVLEr || OC == V9::MOVLEi || OC == V9::MOVLEUr - || OC == V9::MOVLEUi || OC == V9::MOVFLEr || OC == V9::MOVFLEi - || OC == V9::MOVNEr || OC == V9::MOVNEi || OC == V9::MOVNEGr || OC == V9::MOVNEGi - || OC == V9::MOVFNEr || OC == V9::MOVFNEi) { - ++BBWithCondMov; - return false; - } - - indexMap[I] = count + offset; - - if(TMI->isNop(OC)) - continue; - - ++count; - } - - offset += count; - - return true; - } -} - -bool ModuloSchedulingSBPass::CreateDefMap(std::vector &SB) { - defaultInst = 0; - - for(std::vector::iterator BI = SB.begin(), - BE = SB.end(); BI != BE; ++BI) { - - for(MachineBasicBlock::const_iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I) { - 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 - if(defMap.count(V) && isa(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 - if(!defaultInst && mOp.getType() == MachineOperand::MO_VirtualRegister) { - Value *V = mOp.getVRegValue(); - if(!isa(V) && !isa(V) && !isa(V) && !isa(V)) - defaultInst = (Instruction*) V; - } - } - } - } - - if(!defaultInst) - return false; - - 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 &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 resourceUsageCount; - - for(std::vector::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 > 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::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 > >::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 &blocked, - std::map > &B) { - - //Unblock u - DEBUG(std::cerr << "Unblocking: " << *u << "\n"); - blocked.erase(u); - - //std::set 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 &SCC, std::map &newNodes) { - - int totalDelay = 0; - int totalDistance = 0; - std::vector recc; - MSchedGraphSBNode *start = 0; - MSchedGraphSBNode *end = 0; - - //Loop over recurrence, get delay and distance - for(std::vector::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 &stack, - std::set &blocked, std::vector &SCC, - MSchedGraphSBNode *s, std::map > &B, - int II, std::map &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 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::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::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 &stack, std::map &newNodes) { - std::vector 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::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 newNodes; - - //copy the graph - MSchedGraphSB *MSG = new MSchedGraphSB(*g, newNodes); - - DEBUG(std::cerr << "Finding All Circuits\n"); - - //Set of blocked nodes - std::set blocked; - - //Stack holding current circuit - std::vector stack; - - //Map for B Lists - std::map > 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 Visited; - std::vector 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 SCCI = scc_begin(GI->second), - E = scc_end(GI->second); SCCI != E; ++SCCI) { - std::vector &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::iterator N = Vk.begin(), NE = Vk.end(); - N != NE; ++N) { std::cerr << *((*N)->getInst()); }); - - //Iterate over all nodes in this scc - for(std::vector::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 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::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 visitedNodes; - - //Now Loop over map and calculate the node attributes - for(std::map::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::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::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 << ")\n"); - - return maxPredValue; -} - - -int ModuloSchedulingSBPass::calculateALAP(MSchedGraphSBNode *node, int MII, - int maxASAP, MSchedGraphSBNode *srcNode) { - - DEBUG(std::cerr << "Calculating ALAP for " << *node << "\n"); - - MSNodeSBAttributes &attributes = nodeToAttributesMap.find(node)->second; - - if(attributes.ALAP != -1) - return attributes.ALAP; - - if(node->hasSuccessors()) { - - //Trying to deal with the issue where the node has successors, but - //we are ignoring all of the edges to them. So this is my hack for - //now.. there is probably a more elegant way of doing this (FIXME) - bool processedOneEdge = false; - - //FIXME, set to something high to start - int minSuccValue = 9999999; - - //Iterate over all of the predecessors and fine max - for(MSchedGraphSBNode::succ_iterator P = node->succ_begin(), - E = node->succ_end(); P != E; ++P) { - - //Only process if we are not ignoring the edge - if(!ignoreEdge(node, *P)) { - processedOneEdge = true; - int succALAP = -1; - succALAP = calculateALAP(*P, MII, maxASAP, node); - - assert(succALAP != -1 && "Successors ALAP should have been caclulated"); - - int iteDiff = P.getEdge().getIteDiff(); - - int currentSuccValue = succALAP - node->getLatency() + iteDiff * MII; - - DEBUG(std::cerr << "succ ALAP: " << succALAP << ", iteDiff: " << iteDiff << ", SuccLatency: " << (*P)->getLatency() << ", Current ALAP succ: " << currentSuccValue << "\n"); - - minSuccValue = std::min(minSuccValue, currentSuccValue); - } - } - - if(processedOneEdge) - attributes.ALAP = minSuccValue; - - else - attributes.ALAP = maxASAP; - } - else - attributes.ALAP = maxASAP; - - DEBUG(std::cerr << "ALAP: " << attributes.ALAP << " (" << *node << ")\n"); - - if(attributes.ALAP < 0) - attributes.ALAP = 0; - - return attributes.ALAP; -} - -int ModuloSchedulingSBPass::findMaxASAP() { - int maxASAP = 0; - - for(std::map::iterator I = nodeToAttributesMap.begin(), - E = nodeToAttributesMap.end(); I != E; ++I) - maxASAP = std::max(maxASAP, I->second.ASAP); - return maxASAP; -} - - -int ModuloSchedulingSBPass::calculateHeight(MSchedGraphSBNode *node,MSchedGraphSBNode *srcNode) { - - MSNodeSBAttributes &attributes = nodeToAttributesMap.find(node)->second; - - if(attributes.height != -1) - return attributes.height; - - int maxHeight = 0; - - //Iterate over all of the predecessors and find max - for(MSchedGraphSBNode::succ_iterator P = node->succ_begin(), - E = node->succ_end(); P != E; ++P) { - - - if(!ignoreEdge(node, *P)) { - int succHeight = calculateHeight(*P, node); - - assert(succHeight != -1 && "Successors Height should have been caclulated"); - - int currentHeight = succHeight + node->getLatency(); - maxHeight = std::max(maxHeight, currentHeight); - } - } - attributes.height = maxHeight; - DEBUG(std::cerr << "Height: " << attributes.height << " (" << *node << ")\n"); - return maxHeight; -} - - -int ModuloSchedulingSBPass::calculateDepth(MSchedGraphSBNode *node, - MSchedGraphSBNode *destNode) { - - MSNodeSBAttributes &attributes = nodeToAttributesMap.find(node)->second; - - if(attributes.depth != -1) - return attributes.depth; - - int maxDepth = 0; - - //Iterate over all of the predecessors and fine max - for(MSchedGraphSBNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) { - - if(!ignoreEdge(*P, node)) { - int predDepth = -1; - predDepth = calculateDepth(*P, node); - - assert(predDepth != -1 && "Predecessors ASAP should have been caclulated"); - - int currentDepth = predDepth + (*P)->getLatency(); - maxDepth = std::max(maxDepth, currentDepth); - } - } - attributes.depth = maxDepth; - - DEBUG(std::cerr << "Depth: " << attributes.depth << " (" << *node << "*)\n"); - return maxDepth; -} - -void ModuloSchedulingSBPass::computePartialOrder() { - - TIME_REGION(X, "calculatePartialOrder"); - - DEBUG(std::cerr << "Computing Partial Order\n"); - - //Steps to add a recurrence to the partial order 1) Find reccurrence - //with the highest RecMII. Add it to the partial order. 2) For each - //recurrence with decreasing RecMII, add it to the partial order - //along with any nodes that connect this recurrence to recurrences - //already in the partial order - for(std::set > >::reverse_iterator - I = recurrenceList.rbegin(), E=recurrenceList.rend(); I !=E; ++I) { - - std::set new_recurrence; - - //Loop through recurrence and remove any nodes already in the partial order - for(std::vector::const_iterator N = I->second.begin(), - NE = I->second.end(); N != NE; ++N) { - - bool found = false; - for(std::vector >::iterator PO = partialOrder.begin(), - PE = partialOrder.end(); PO != PE; ++PO) { - if(PO->count(*N)) - found = true; - } - - //Check if its a branch, and remove to handle special - if(!found) { - new_recurrence.insert(*N); - } - - } - - - if(new_recurrence.size() > 0) { - - std::vector path; - std::set nodesToAdd; - - //Dump recc we are dealing with (minus nodes already in PO) - DEBUG(std::cerr << "Recc: "); - DEBUG(for(std::set::iterator R = new_recurrence.begin(), RE = new_recurrence.end(); R != RE; ++R) { std::cerr << **R ; }); - - //Add nodes that connect this recurrence to recurrences in the partial path - for(std::set::iterator N = new_recurrence.begin(), - NE = new_recurrence.end(); N != NE; ++N) - searchPath(*N, path, nodesToAdd, new_recurrence); - - //Add nodes to this recurrence if they are not already in the partial order - for(std::set::iterator N = nodesToAdd.begin(), NE = nodesToAdd.end(); - N != NE; ++N) { - bool found = false; - for(std::vector >::iterator PO = partialOrder.begin(), - PE = partialOrder.end(); PO != PE; ++PO) { - if(PO->count(*N)) - found = true; - } - if(!found) { - assert("FOUND CONNECTOR"); - new_recurrence.insert(*N); - } - } - - partialOrder.push_back(new_recurrence); - } - } - - //Add any nodes that are not already in the partial order - //Add them in a set, one set per connected component - std::set lastNodes; - std::set noPredNodes; - for(std::map::iterator I = nodeToAttributesMap.begin(), - E = nodeToAttributesMap.end(); I != E; ++I) { - - bool found = false; - - //Check if its already in our partial order, if not add it to the final vector - for(std::vector >::iterator PO = partialOrder.begin(), - PE = partialOrder.end(); PO != PE; ++PO) { - if(PO->count(I->first)) - found = true; - } - if(!found) - lastNodes.insert(I->first); - } - - //For each node w/out preds, see if there is a path to one of the - //recurrences, and if so add them to that current recc - /*for(std::set::iterator N = noPredNodes.begin(), NE = noPredNodes.end(); - N != NE; ++N) { - DEBUG(std::cerr << "No Pred Path from: " << **N << "\n"); - for(std::vector >::iterator PO = partialOrder.begin(), - PE = partialOrder.end(); PO != PE; ++PO) { - std::vector path; - pathToRecc(*N, path, *PO, lastNodes); - } - }*/ - - - //Break up remaining nodes that are not in the partial order - ///into their connected compoenents - while(lastNodes.size() > 0) { - std::set ccSet; - connectedComponentSet(*(lastNodes.begin()),ccSet, lastNodes); - if(ccSet.size() > 0) - partialOrder.push_back(ccSet); - } - -} - -void ModuloSchedulingSBPass::connectedComponentSet(MSchedGraphSBNode *node, std::set &ccSet, std::set &lastNodes) { - - //Add to final set - if( !ccSet.count(node) && lastNodes.count(node)) { - lastNodes.erase(node); - ccSet.insert(node); - } - else - return; - - //Loop over successors and recurse if we have not seen this node before - for(MSchedGraphSBNode::succ_iterator node_succ = node->succ_begin(), end=node->succ_end(); node_succ != end; ++node_succ) { - connectedComponentSet(*node_succ, ccSet, lastNodes); - } - -} - -void ModuloSchedulingSBPass::searchPath(MSchedGraphSBNode *node, - std::vector &path, - std::set &nodesToAdd, - std::set &new_reccurrence) { - //Push node onto the path - path.push_back(node); - - //Loop over all successors and see if there is a path from this node to - //a recurrence in the partial order, if so.. add all nodes to be added to recc - for(MSchedGraphSBNode::succ_iterator S = node->succ_begin(), SE = node->succ_end(); S != SE; - ++S) { - - //Check if we should ignore this edge first - if(ignoreEdge(node,*S)) - continue; - - //check if successor is in this recurrence, we will get to it eventually - if(new_reccurrence.count(*S)) - continue; - - //If this node exists in a recurrence already in the partial - //order, then add all nodes in the path to the set of nodes to add - //Check if its already in our partial order, if not add it to the - //final vector - bool found = false; - for(std::vector >::iterator PO = partialOrder.begin(), - PE = partialOrder.end(); PO != PE; ++PO) { - - if(PO->count(*S)) { - found = true; - break; - } - } - - if(!found) { - nodesToAdd.insert(*S); - searchPath(*S, path, nodesToAdd, new_reccurrence); - } - } - - //Pop Node off the path - path.pop_back(); -} - -void dumpIntersection(std::set &IntersectCurrent) { - std::cerr << "Intersection ("; - for(std::set::iterator I = IntersectCurrent.begin(), E = IntersectCurrent.end(); I != E; ++I) - std::cerr << **I << ", "; - std::cerr << ")\n"; -} - -void ModuloSchedulingSBPass::orderNodes() { - - TIME_REGION(X, "orderNodes"); - - int BOTTOM_UP = 0; - int TOP_DOWN = 1; - - //Set default order - int order = BOTTOM_UP; - - //Loop over and find all pred nodes and schedule them first - /*for(std::vector >::iterator CurrentSet = partialOrder.begin(), E= partialOrder.end(); CurrentSet != E; ++CurrentSet) { - for(std::set::iterator N = CurrentSet->begin(), NE = CurrentSet->end(); N != NE; ++N) - if((*N)->isPredicate()) { - FinalNodeOrder.push_back(*N); - CurrentSet->erase(*N); - } - }*/ - - - - //Loop over all the sets and place them in the final node order - for(std::vector >::iterator CurrentSet = partialOrder.begin(), E= partialOrder.end(); CurrentSet != E; ++CurrentSet) { - - DEBUG(std::cerr << "Processing set in S\n"); - DEBUG(dumpIntersection(*CurrentSet)); - - //Result of intersection - std::set IntersectCurrent; - - predIntersect(*CurrentSet, IntersectCurrent); - - //If the intersection of predecessor and current set is not empty - //sort nodes bottom up - if(IntersectCurrent.size() != 0) { - DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is NOT empty\n"); - order = BOTTOM_UP; - } - //If empty, use successors - else { - DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is empty\n"); - - succIntersect(*CurrentSet, IntersectCurrent); - - //sort top-down - if(IntersectCurrent.size() != 0) { - DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is NOT empty\n"); - order = TOP_DOWN; - } - else { - DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is empty\n"); - //Find node with max ASAP in current Set - MSchedGraphSBNode *node; - int maxASAP = 0; - DEBUG(std::cerr << "Using current set of size " << CurrentSet->size() << "to find max ASAP\n"); - for(std::set::iterator J = CurrentSet->begin(), JE = CurrentSet->end(); J != JE; ++J) { - //Get node attributes - MSNodeSBAttributes nodeAttr= nodeToAttributesMap.find(*J)->second; - //assert(nodeAttr != nodeToAttributesMap.end() && "Node not in attributes map!"); - - if(maxASAP <= nodeAttr.ASAP) { - maxASAP = nodeAttr.ASAP; - node = *J; - } - } - assert(node != 0 && "In node ordering node should not be null"); - IntersectCurrent.insert(node); - order = BOTTOM_UP; - } - } - - //Repeat until all nodes are put into the final order from current set - while(IntersectCurrent.size() > 0) { - - if(order == TOP_DOWN) { - DEBUG(std::cerr << "Order is TOP DOWN\n"); - - while(IntersectCurrent.size() > 0) { - DEBUG(std::cerr << "Intersection is not empty, so find heighest height\n"); - - int MOB = 0; - int height = 0; - MSchedGraphSBNode *highestHeightNode = *(IntersectCurrent.begin()); - - //Find node in intersection with highest heigh and lowest MOB - for(std::set::iterator I = IntersectCurrent.begin(), - E = IntersectCurrent.end(); I != E; ++I) { - - //Get current nodes properties - MSNodeSBAttributes nodeAttr= nodeToAttributesMap.find(*I)->second; - - if(height < nodeAttr.height) { - highestHeightNode = *I; - height = nodeAttr.height; - MOB = nodeAttr.MOB; - } - else if(height == nodeAttr.height) { - if(MOB > nodeAttr.height) { - highestHeightNode = *I; - height = nodeAttr.height; - MOB = nodeAttr.MOB; - } - } - } - - //Append our node with greatest height to the NodeOrder - if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestHeightNode) == FinalNodeOrder.end()) { - DEBUG(std::cerr << "Adding node to Final Order: " << *highestHeightNode << "\n"); - FinalNodeOrder.push_back(highestHeightNode); - } - - //Remove V from IntersectOrder - IntersectCurrent.erase(std::find(IntersectCurrent.begin(), - IntersectCurrent.end(), highestHeightNode)); - - - //Intersect V's successors with CurrentSet - for(MSchedGraphSBNode::succ_iterator P = highestHeightNode->succ_begin(), - E = highestHeightNode->succ_end(); P != E; ++P) { - //if(lower_bound(CurrentSet->begin(), - // CurrentSet->end(), *P) != CurrentSet->end()) { - if(std::find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) { - if(ignoreEdge(highestHeightNode, *P)) - continue; - //If not already in Intersect, add - if(!IntersectCurrent.count(*P)) - IntersectCurrent.insert(*P); - } - } - } //End while loop over Intersect Size - - //Change direction - order = BOTTOM_UP; - - //Reset Intersect to reflect changes in OrderNodes - IntersectCurrent.clear(); - predIntersect(*CurrentSet, IntersectCurrent); - - } //End If TOP_DOWN - - //Begin if BOTTOM_UP - else { - DEBUG(std::cerr << "Order is BOTTOM UP\n"); - while(IntersectCurrent.size() > 0) { - DEBUG(std::cerr << "Intersection of size " << IntersectCurrent.size() << ", finding highest depth\n"); - - //dump intersection - DEBUG(dumpIntersection(IntersectCurrent)); - //Get node with highest depth, if a tie, use one with lowest - //MOB - int MOB = 0; - int depth = 0; - MSchedGraphSBNode *highestDepthNode = *(IntersectCurrent.begin()); - - for(std::set::iterator I = IntersectCurrent.begin(), - E = IntersectCurrent.end(); I != E; ++I) { - //Find node attribute in graph - MSNodeSBAttributes nodeAttr= nodeToAttributesMap.find(*I)->second; - - if(depth < nodeAttr.depth) { - highestDepthNode = *I; - depth = nodeAttr.depth; - MOB = nodeAttr.MOB; - } - else if(depth == nodeAttr.depth) { - if(MOB > nodeAttr.MOB) { - highestDepthNode = *I; - depth = nodeAttr.depth; - MOB = nodeAttr.MOB; - } - } - } - - - - //Append highest depth node to the NodeOrder - if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestDepthNode) == FinalNodeOrder.end()) { - DEBUG(std::cerr << "Adding node to Final Order: " << *highestDepthNode << "\n"); - FinalNodeOrder.push_back(highestDepthNode); - } - //Remove heightestDepthNode from IntersectOrder - IntersectCurrent.erase(highestDepthNode); - - - //Intersect heightDepthNode's pred with CurrentSet - for(MSchedGraphSBNode::pred_iterator P = highestDepthNode->pred_begin(), - E = highestDepthNode->pred_end(); P != E; ++P) { - if(CurrentSet->count(*P)) { - if(ignoreEdge(*P, highestDepthNode)) - continue; - - //If not already in Intersect, add - if(!IntersectCurrent.count(*P)) - IntersectCurrent.insert(*P); - } - } - - } //End while loop over Intersect Size - - //Change order - order = TOP_DOWN; - - //Reset IntersectCurrent to reflect changes in OrderNodes - IntersectCurrent.clear(); - succIntersect(*CurrentSet, IntersectCurrent); - } //End if BOTTOM_DOWN - - DEBUG(std::cerr << "Current Intersection Size: " << IntersectCurrent.size() << "\n"); - } - //End Wrapping while loop - DEBUG(std::cerr << "Ending Size of Current Set: " << CurrentSet->size() << "\n"); - }//End for over all sets of nodes - - //FIXME: As the algorithm stands it will NEVER add an instruction such as ba (with no - //data dependencies) to the final order. We add this manually. It will always be - //in the last set of S since its not part of a recurrence - //Loop over all the sets and place them in the final node order - std::vector > ::reverse_iterator LastSet = partialOrder.rbegin(); - for(std::set::iterator CurrentNode = LastSet->begin(), LastNode = LastSet->end(); - CurrentNode != LastNode; ++CurrentNode) { - if((*CurrentNode)->getInst()->getOpcode() == V9::BA) - FinalNodeOrder.push_back(*CurrentNode); - } - //Return final Order - //return FinalNodeOrder; -} - - -void ModuloSchedulingSBPass::predIntersect(std::set &CurrentSet, std::set &IntersectResult) { - - for(unsigned j=0; j < FinalNodeOrder.size(); ++j) { - for(MSchedGraphSBNode::pred_iterator P = FinalNodeOrder[j]->pred_begin(), - E = FinalNodeOrder[j]->pred_end(); P != E; ++P) { - - //Check if we are supposed to ignore this edge or not - if(ignoreEdge(*P,FinalNodeOrder[j])) - continue; - - if(CurrentSet.count(*P)) - if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end()) - IntersectResult.insert(*P); - } - } -} - -void ModuloSchedulingSBPass::succIntersect(std::set &CurrentSet, std::set &IntersectResult) { - - for(unsigned j=0; j < FinalNodeOrder.size(); ++j) { - for(MSchedGraphSBNode::succ_iterator P = FinalNodeOrder[j]->succ_begin(), - E = FinalNodeOrder[j]->succ_end(); P != E; ++P) { - - //Check if we are supposed to ignore this edge or not - if(ignoreEdge(FinalNodeOrder[j],*P)) - continue; - - if(CurrentSet.count(*P)) - if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end()) - IntersectResult.insert(*P); - } - } -} - - - -bool ModuloSchedulingSBPass::computeSchedule(std::vector &SB, MSchedGraphSB *MSG) { - - TIME_REGION(X, "computeSchedule"); - - bool success = false; - - //FIXME: Should be set to max II of the original loop - //Cap II in order to prevent infinite loop - int capII = MSG->totalDelay(); - - while(!success) { - - //Keep track of branches, but do not insert into the schedule - std::vector branches; - - //Loop over the final node order and process each node - for(std::vector::iterator I = FinalNodeOrder.begin(), - E = FinalNodeOrder.end(); I != E; ++I) { - - //CalculateEarly and Late start - bool initialLSVal = false; - bool initialESVal = false; - int EarlyStart = 0; - int LateStart = 0; - bool hasSucc = false; - bool hasPred = false; - bool sched; - - if((*I)->isBranch()) - if((*I)->hasPredecessors()) - sched = true; - else - sched = false; - else - sched = true; - - if(sched) { - //Loop over nodes in the schedule and determine if they are predecessors - //or successors of the node we are trying to schedule - for(MSScheduleSB::schedule_iterator nodesByCycle = schedule.begin(), nodesByCycleEnd = schedule.end(); - nodesByCycle != nodesByCycleEnd; ++nodesByCycle) { - - //For this cycle, get the vector of nodes schedule and loop over it - for(std::vector::iterator schedNode = nodesByCycle->second.begin(), SNE = nodesByCycle->second.end(); schedNode != SNE; ++schedNode) { - - if((*I)->isPredecessor(*schedNode)) { - int diff = (*I)->getInEdge(*schedNode).getIteDiff(); - int ES_Temp = nodesByCycle->first + (*schedNode)->getLatency() - diff * II; - DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n"); - DEBUG(std::cerr << "Temp EarlyStart: " << ES_Temp << " Prev EarlyStart: " << EarlyStart << "\n"); - if(initialESVal) - EarlyStart = std::max(EarlyStart, ES_Temp); - else { - EarlyStart = ES_Temp; - initialESVal = true; - } - hasPred = true; - } - if((*I)->isSuccessor(*schedNode)) { - int diff = (*schedNode)->getInEdge(*I).getIteDiff(); - int LS_Temp = nodesByCycle->first - (*I)->getLatency() + diff * II; - DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n"); - DEBUG(std::cerr << "Temp LateStart: " << LS_Temp << " Prev LateStart: " << LateStart << "\n"); - if(initialLSVal) - LateStart = std::min(LateStart, LS_Temp); - else { - LateStart = LS_Temp; - initialLSVal = true; - } - hasSucc = true; - } - } - } - } - else { - branches.push_back(*I); - continue; - } - - //Check if the node has no pred or successors and set Early Start to its ASAP - if(!hasSucc && !hasPred) - EarlyStart = nodeToAttributesMap.find(*I)->second.ASAP; - - DEBUG(std::cerr << "Has Successors: " << hasSucc << ", Has Pred: " << hasPred << "\n"); - DEBUG(std::cerr << "EarlyStart: " << EarlyStart << ", LateStart: " << LateStart << "\n"); - - //Now, try to schedule this node depending upon its pred and successor in the schedule - //already - if(!hasSucc && hasPred) - success = scheduleNode(*I, EarlyStart, (EarlyStart + II -1)); - else if(!hasPred && hasSucc) - success = scheduleNode(*I, LateStart, (LateStart - II +1)); - else if(hasPred && hasSucc) { - if(EarlyStart > LateStart) { - success = false; - //LateStart = EarlyStart; - DEBUG(std::cerr << "Early Start can not be later then the late start cycle, schedule fails\n"); - } - else - success = scheduleNode(*I, EarlyStart, std::min(LateStart, (EarlyStart + II -1))); - } - else - success = scheduleNode(*I, EarlyStart, EarlyStart + II - 1); - - if(!success) { - ++II; - schedule.clear(); - break; - } - - } - - if(success) { - DEBUG(std::cerr << "Constructing Schedule Kernel\n"); - success = schedule.constructKernel(II, branches, indVarInstrs[SB]); - DEBUG(std::cerr << "Done Constructing Schedule Kernel\n"); - if(!success) { - ++II; - schedule.clear(); - } - DEBUG(std::cerr << "Final II: " << II << "\n"); - - } - - if(II >= capII) { - DEBUG(std::cerr << "Maximum II reached, giving up\n"); - return false; - } - - assert(II < capII && "The II should not exceed the original loop number of cycles"); - } - return true; -} - - -bool ModuloSchedulingSBPass::scheduleNode(MSchedGraphSBNode *node, - int start, int end) { - bool success = false; - - DEBUG(std::cerr << *node << " (Start Cycle: " << start << ", End Cycle: " << end << ")\n"); - - //Make sure start and end are not negative - //if(start < 0) { - //start = 0; - - //} - //if(end < 0) - //end = 0; - - bool forward = true; - if(start > end) - forward = false; - - bool increaseSC = true; - int cycle = start ; - - - while(incre