2001-09-18 12:52:03 +00:00
|
|
|
// $Id$ -*-C++-*-
|
|
|
|
//***************************************************************************
|
|
|
|
// File:
|
|
|
|
// SchedPriorities.h
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Encapsulate heuristics for instruction scheduling.
|
|
|
|
//
|
|
|
|
// Strategy:
|
|
|
|
// Priority ordering rules:
|
|
|
|
// (1) Max delay, which is the order of the heap S.candsAsHeap.
|
|
|
|
// (2) Instruction that frees up a register.
|
|
|
|
// (3) Instruction that has the maximum number of dependent instructions.
|
|
|
|
// Note that rules 2 and 3 are only used if issue conflicts prevent
|
|
|
|
// choosing a higher priority instruction by rule 1.
|
|
|
|
//
|
|
|
|
// History:
|
|
|
|
// 7/30/01 - Vikram Adve - Created
|
|
|
|
//**************************************************************************/
|
2001-08-28 23:06:49 +00:00
|
|
|
|
2001-09-14 16:56:32 +00:00
|
|
|
#include "SchedPriorities.h"
|
2002-02-04 20:02:16 +00:00
|
|
|
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/PostOrderIterator.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
2001-08-28 23:06:49 +00:00
|
|
|
|
2002-04-08 22:03:57 +00:00
|
|
|
SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
|
2002-02-04 20:02:16 +00:00
|
|
|
MethodLiveVarInfo &LVI)
|
|
|
|
: curTime(0), graph(G), methodLiveVarInfo(LVI),
|
|
|
|
nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
|
|
|
|
earliestForNode(G->getNumNodes(), 0),
|
2001-08-28 23:06:49 +00:00
|
|
|
earliestReadyTime(0),
|
2002-02-04 20:02:16 +00:00
|
|
|
nextToTry(candsAsHeap.begin()) {
|
2001-08-28 23:06:49 +00:00
|
|
|
computeDelays(graph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SchedPriorities::initialize()
|
|
|
|
{
|
|
|
|
initializeReadyHeap(graph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SchedPriorities::computeDelays(const SchedGraph* graph)
|
|
|
|
{
|
2001-09-28 22:56:31 +00:00
|
|
|
po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
|
2001-08-28 23:06:49 +00:00
|
|
|
for ( ; poIter != poEnd; ++poIter)
|
|
|
|
{
|
|
|
|
const SchedGraphNode* node = *poIter;
|
|
|
|
cycles_t nodeDelay;
|
|
|
|
if (node->beginOutEdges() == node->endOutEdges())
|
|
|
|
nodeDelay = node->getLatency();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Iterate over the out-edges of the node to compute delay
|
|
|
|
nodeDelay = 0;
|
|
|
|
for (SchedGraphNode::const_iterator E=node->beginOutEdges();
|
|
|
|
E != node->endOutEdges(); ++E)
|
|
|
|
{
|
|
|
|
cycles_t sinkDelay = getNodeDelayRef((*E)->getSink());
|
2002-01-20 22:54:45 +00:00
|
|
|
nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
|
2001-08-28 23:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
getNodeDelayRef(node) = nodeDelay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
|
|
|
|
{
|
|
|
|
const SchedGraphNode* graphRoot = graph->getRoot();
|
|
|
|
assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
|
|
|
|
|
|
|
|
// Insert immediate successors of dummy root, which are the actual roots
|
|
|
|
sg_succ_const_iterator SEnd = succ_end(graphRoot);
|
|
|
|
for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
|
|
|
|
this->insertReady(*S);
|
|
|
|
|
|
|
|
#undef TEST_HEAP_CONVERSION
|
|
|
|
#ifdef TEST_HEAP_CONVERSION
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << "Before heap conversion:\n";
|
2001-08-28 23:06:49 +00:00
|
|
|
copy(candsAsHeap.begin(), candsAsHeap.end(),
|
2002-01-20 22:54:45 +00:00
|
|
|
ostream_iterator<NodeDelayPair*>(cerr,"\n"));
|
2001-08-28 23:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
candsAsHeap.makeHeap();
|
|
|
|
|
2002-03-24 03:46:15 +00:00
|
|
|
nextToTry = candsAsHeap.begin();
|
|
|
|
|
2001-08-28 23:06:49 +00:00
|
|
|
#ifdef TEST_HEAP_CONVERSION
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << "After heap conversion:\n";
|
2001-08-28 23:06:49 +00:00
|
|
|
copy(candsAsHeap.begin(), candsAsHeap.end(),
|
2002-01-20 22:54:45 +00:00
|
|
|
ostream_iterator<NodeDelayPair*>(cerr,"\n"));
|
2001-08-28 23:06:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
void
|
|
|
|
SchedPriorities::insertReady(const SchedGraphNode* node)
|
|
|
|
{
|
|
|
|
candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
|
|
|
|
candsAsSet.insert(node);
|
|
|
|
mcands.clear(); // ensure reset choices is called before any more choices
|
|
|
|
earliestReadyTime = std::min(earliestReadyTime,
|
|
|
|
earliestForNode[node->getNodeId()]);
|
|
|
|
|
|
|
|
if (SchedDebugLevel >= Sched_PrintSchedTrace)
|
|
|
|
{
|
2002-03-24 03:46:15 +00:00
|
|
|
cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
|
|
|
|
<< earliestForNode[node->getNodeId()] << "; "
|
|
|
|
<< " Delay = " <<(long)getNodeDelayRef(node) << "; Instruction: \n";
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " " << *node->getMachineInstr() << "\n";
|
|
|
|
}
|
|
|
|
}
|
2001-08-28 23:06:49 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
|
|
|
|
const SchedGraphNode* node)
|
|
|
|
{
|
|
|
|
candsAsHeap.removeNode(node);
|
|
|
|
candsAsSet.erase(node);
|
|
|
|
mcands.clear(); // ensure reset choices is called before any more choices
|
|
|
|
|
|
|
|
if (earliestReadyTime == getEarliestForNodeRef(node))
|
|
|
|
{// earliestReadyTime may have been due to this node, so recompute it
|
|
|
|
earliestReadyTime = HUGE_LATENCY;
|
|
|
|
for (NodeHeap::const_iterator I=candsAsHeap.begin();
|
|
|
|
I != candsAsHeap.end(); ++I)
|
|
|
|
if (candsAsHeap.getNode(I))
|
2002-01-20 22:54:45 +00:00
|
|
|
earliestReadyTime = std::min(earliestReadyTime,
|
2001-08-28 23:06:49 +00:00
|
|
|
getEarliestForNodeRef(candsAsHeap.getNode(I)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now update ready times for successors
|
|
|
|
for (SchedGraphNode::const_iterator E=node->beginOutEdges();
|
|
|
|
E != node->endOutEdges(); ++E)
|
|
|
|
{
|
|
|
|
cycles_t& etime = getEarliestForNodeRef((*E)->getSink());
|
2002-01-20 22:54:45 +00:00
|
|
|
etime = std::max(etime, curTime + (*E)->getMinDelay());
|
2001-08-28 23:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Priority ordering rules:
|
|
|
|
// (1) Max delay, which is the order of the heap S.candsAsHeap.
|
|
|
|
// (2) Instruction that frees up a register.
|
|
|
|
// (3) Instruction that has the maximum number of dependent instructions.
|
|
|
|
// Note that rules 2 and 3 are only used if issue conflicts prevent
|
|
|
|
// choosing a higher priority instruction by rule 1.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
inline int
|
2002-01-20 22:54:45 +00:00
|
|
|
SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
|
2001-08-28 23:06:49 +00:00
|
|
|
{
|
|
|
|
return (mcands.size() == 1)? 0 // only one choice exists so take it
|
|
|
|
: -1; // -1 indicates multiple choices
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int
|
2002-01-20 22:54:45 +00:00
|
|
|
SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
|
2001-08-28 23:06:49 +00:00
|
|
|
{
|
|
|
|
assert(mcands.size() >= 1 && "Should have at least one candidate here.");
|
|
|
|
for (unsigned i=0, N = mcands.size(); i < N; i++)
|
|
|
|
if (instructionHasLastUse(methodLiveVarInfo,
|
|
|
|
candsAsHeap.getNode(mcands[i])))
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int
|
2002-01-20 22:54:45 +00:00
|
|
|
SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
|
2001-08-28 23:06:49 +00:00
|
|
|
{
|
|
|
|
assert(mcands.size() >= 1 && "Should have at least one candidate here.");
|
|
|
|
int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
|
|
|
|
int indexWithMaxUses = 0;
|
|
|
|
for (unsigned i=1, N = mcands.size(); i < N; i++)
|
|
|
|
{
|
|
|
|
int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
|
|
|
|
if (numUses > maxUses)
|
|
|
|
{
|
|
|
|
maxUses = numUses;
|
|
|
|
indexWithMaxUses = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indexWithMaxUses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SchedGraphNode*
|
|
|
|
SchedPriorities::getNextHighest(const SchedulingManager& S,
|
|
|
|
cycles_t curTime)
|
|
|
|
{
|
|
|
|
int nextIdx = -1;
|
|
|
|
const SchedGraphNode* nextChoice = NULL;
|
|
|
|
|
|
|
|
if (mcands.size() == 0)
|
|
|
|
findSetWithMaxDelay(mcands, S);
|
|
|
|
|
|
|
|
while (nextIdx < 0 && mcands.size() > 0)
|
|
|
|
{
|
|
|
|
nextIdx = chooseByRule1(mcands); // rule 1
|
|
|
|
|
|
|
|
if (nextIdx == -1)
|
|
|
|
nextIdx = chooseByRule2(mcands); // rule 2
|
|
|
|
|
|
|
|
if (nextIdx == -1)
|
|
|
|
nextIdx = chooseByRule3(mcands); // rule 3
|
|
|
|
|
|
|
|
if (nextIdx == -1)
|
|
|
|
nextIdx = 0; // default to first choice by delays
|
|
|
|
|
|
|
|
// We have found the next best candidate. Check if it ready in
|
|
|
|
// the current cycle, and if it is feasible.
|
|
|
|
// If not, remove it from mcands and continue. Refill mcands if
|
|
|
|
// it becomes empty.
|
|
|
|
nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
|
|
|
|
if (getEarliestForNodeRef(nextChoice) > curTime
|
2001-09-07 21:22:28 +00:00
|
|
|
|| ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
|
2001-08-28 23:06:49 +00:00
|
|
|
{
|
|
|
|
mcands.erase(mcands.begin() + nextIdx);
|
|
|
|
nextIdx = -1;
|
|
|
|
if (mcands.size() == 0)
|
|
|
|
findSetWithMaxDelay(mcands, S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextIdx >= 0)
|
|
|
|
{
|
|
|
|
mcands.erase(mcands.begin() + nextIdx);
|
|
|
|
return nextChoice;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-01-20 22:54:45 +00:00
|
|
|
SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
|
2001-08-28 23:06:49 +00:00
|
|
|
const SchedulingManager& S)
|
|
|
|
{
|
|
|
|
if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
|
|
|
|
{ // out of choices at current maximum delay;
|
|
|
|
// put nodes with next highest delay in mcands
|
|
|
|
candIndex next = nextToTry;
|
|
|
|
cycles_t maxDelay = candsAsHeap.getDelay(next);
|
|
|
|
for (; next != candsAsHeap.end()
|
|
|
|
&& candsAsHeap.getDelay(next) == maxDelay; ++next)
|
|
|
|
mcands.push_back(next);
|
|
|
|
|
|
|
|
nextToTry = next;
|
|
|
|
|
|
|
|
if (SchedDebugLevel >= Sched_PrintSchedTrace)
|
|
|
|
{
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " Cycle " << (long)getTime() << ": "
|
|
|
|
<< "Next highest delay = " << (long)maxDelay << " : "
|
2001-08-28 23:06:49 +00:00
|
|
|
<< mcands.size() << " Nodes with this delay: ";
|
|
|
|
for (unsigned i=0; i < mcands.size(); i++)
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
|
|
|
|
cerr << "\n";
|
2001-08-28 23:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
SchedPriorities::instructionHasLastUse(MethodLiveVarInfo& methodLiveVarInfo,
|
2002-02-05 02:51:01 +00:00
|
|
|
const SchedGraphNode* graphNode) {
|
2002-02-05 06:02:59 +00:00
|
|
|
const MachineInstr *MI = graphNode->getMachineInstr();
|
2001-08-28 23:06:49 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::hash_map<const MachineInstr*, bool>::const_iterator
|
2002-02-05 06:02:59 +00:00
|
|
|
ui = lastUseMap.find(MI);
|
2001-08-28 23:06:49 +00:00
|
|
|
if (ui != lastUseMap.end())
|
2002-01-20 22:54:45 +00:00
|
|
|
return ui->second;
|
2001-08-28 23:06:49 +00:00
|
|
|
|
|
|
|
// else check if instruction is a last use and save it in the hash_map
|
|
|
|
bool hasLastUse = false;
|
2001-11-12 14:18:01 +00:00
|
|
|
const BasicBlock* bb = graphNode->getBB();
|
2002-02-05 06:02:59 +00:00
|
|
|
const ValueSet &LVs = methodLiveVarInfo.getLiveVarSetBeforeMInst(MI, bb);
|
2001-08-28 23:06:49 +00:00
|
|
|
|
2002-02-05 06:02:59 +00:00
|
|
|
for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if (!LVs.count(*OI)) {
|
2002-02-05 02:51:01 +00:00
|
|
|
hasLastUse = true;
|
|
|
|
break;
|
|
|
|
}
|
2002-02-05 04:20:12 +00:00
|
|
|
|
2002-02-05 06:02:59 +00:00
|
|
|
return lastUseMap[MI] = hasLastUse;
|
2001-08-28 23:06:49 +00:00
|
|
|
}
|
|
|
|
|