2008-11-15 00:23:40 +00:00
|
|
|
//===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LatencyPriorityQueue class, which is a
|
|
|
|
// SchedulingPriorityQueue that schedules using latency information to
|
|
|
|
// reduce the length of the critical path through the basic block.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "scheduler"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/CodeGen/LatencyPriorityQueue.h"
|
2008-11-15 00:23:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
Various bits of framework needed for precise machine-level selection
DAG scheduling during isel. Most new functionality is currently
guarded by -enable-sched-cycles and -enable-sched-hazard.
Added InstrItineraryData::IssueWidth field, currently derived from
ARM itineraries, but could be initialized differently on other targets.
Added ScheduleHazardRecognizer::MaxLookAhead to indicate whether it is
active, and if so how many cycles of state it holds.
Added SchedulingPriorityQueue::HasReadyFilter to allowing gating entry
into the scheduler's available queue.
ScoreboardHazardRecognizer now accesses the ScheduleDAG in order to
get information about it's SUnits, provides RecedeCycle for bottom-up
scheduling, correctly computes scoreboard depth, tracks IssueCount, and
considers potential stall cycles when checking for hazards.
ScheduleDAGRRList now models machine cycles and hazards (under
flags). It tracks MinAvailableCycle, drives the hazard recognizer and
priority queue's ready filter, manages a new PendingQueue, properly
accounts for stall cycles, etc.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122541 91177308-0d34-0410-b5e6-96231b3b80d8
2010-12-24 05:03:26 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-15 00:23:40 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
|
2008-12-16 03:35:01 +00:00
|
|
|
// The isScheduleHigh flag allows nodes with wraparound dependencies that
|
|
|
|
// cannot easily be modeled as edges with latencies to be scheduled as
|
|
|
|
// soon as possible in a top-down schedule.
|
|
|
|
if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
|
|
|
|
return false;
|
|
|
|
if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
|
|
|
|
return true;
|
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
unsigned LHSNum = LHS->NodeNum;
|
|
|
|
unsigned RHSNum = RHS->NodeNum;
|
|
|
|
|
|
|
|
// The most important heuristic is scheduling the critical path.
|
|
|
|
unsigned LHSLatency = PQ->getLatency(LHSNum);
|
|
|
|
unsigned RHSLatency = PQ->getLatency(RHSNum);
|
|
|
|
if (LHSLatency < RHSLatency) return true;
|
|
|
|
if (LHSLatency > RHSLatency) return false;
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
// After that, if two nodes have identical latencies, look to see if one will
|
|
|
|
// unblock more other nodes than the other.
|
|
|
|
unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
|
|
|
|
unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
|
|
|
|
if (LHSBlocked < RHSBlocked) return true;
|
|
|
|
if (LHSBlocked > RHSBlocked) return false;
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
// Finally, just to provide a stable ordering, use the node number as a
|
|
|
|
// deciding factor.
|
|
|
|
return LHSNum < RHSNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
|
|
|
|
/// of SU, return it, otherwise return null.
|
|
|
|
SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
|
|
|
|
SUnit *OnlyAvailablePred = 0;
|
|
|
|
for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
|
|
|
|
I != E; ++I) {
|
2008-12-09 22:54:47 +00:00
|
|
|
SUnit &Pred = *I->getSUnit();
|
2008-11-15 00:23:40 +00:00
|
|
|
if (!Pred.isScheduled) {
|
|
|
|
// We found an available, but not scheduled, predecessor. If it's the
|
|
|
|
// only one we have found, keep track of it... otherwise give up.
|
|
|
|
if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
|
|
|
|
return 0;
|
|
|
|
OnlyAvailablePred = &Pred;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
return OnlyAvailablePred;
|
|
|
|
}
|
|
|
|
|
2010-05-26 01:10:55 +00:00
|
|
|
void LatencyPriorityQueue::push(SUnit *SU) {
|
2008-11-15 00:23:40 +00:00
|
|
|
// Look at all of the successors of this node. Count the number of nodes that
|
|
|
|
// this node is the sole unscheduled node for.
|
|
|
|
unsigned NumNodesBlocking = 0;
|
|
|
|
for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
|
2009-11-03 20:57:50 +00:00
|
|
|
I != E; ++I) {
|
2008-12-09 22:54:47 +00:00
|
|
|
if (getSingleUnscheduledPred(I->getSUnit()) == SU)
|
2008-11-15 00:23:40 +00:00
|
|
|
++NumNodesBlocking;
|
2009-11-03 20:57:50 +00:00
|
|
|
}
|
2008-11-15 00:23:40 +00:00
|
|
|
NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2010-05-26 18:52:00 +00:00
|
|
|
Queue.push_back(SU);
|
2008-11-15 00:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ScheduledNode - As nodes are scheduled, we look to see if there are any
|
|
|
|
// successor nodes that have a single unscheduled predecessor. If so, that
|
|
|
|
// single predecessor has a higher priority, since scheduling it will make
|
|
|
|
// the node available.
|
|
|
|
void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
|
|
|
|
for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
|
2009-11-03 20:57:50 +00:00
|
|
|
I != E; ++I) {
|
2008-12-09 22:54:47 +00:00
|
|
|
AdjustPriorityOfUnscheduledPreds(I->getSUnit());
|
2009-11-03 20:57:50 +00:00
|
|
|
}
|
2008-11-15 00:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
|
|
|
|
/// scheduled. If SU is not itself available, then there is at least one
|
|
|
|
/// predecessor node that has not been scheduled yet. If SU has exactly ONE
|
|
|
|
/// unscheduled predecessor, we want to increase its priority: it getting
|
|
|
|
/// scheduled will make this node available, so it is better than some other
|
|
|
|
/// node of the same priority that will not make a node available.
|
|
|
|
void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
|
2008-11-17 16:37:30 +00:00
|
|
|
if (SU->isAvailable) return; // All preds scheduled.
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
|
|
|
|
if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
|
2010-12-24 04:28:06 +00:00
|
|
|
|
2008-11-15 00:23:40 +00:00
|
|
|
// Okay, we found a single predecessor that is available, but not scheduled.
|
|
|
|
// Since it is available, it must be in the priority queue. First remove it.
|
|
|
|
remove(OnlyAvailablePred);
|
|
|
|
|
|
|
|
// Reinsert the node into the priority queue, which recomputes its
|
|
|
|
// NumNodesSolelyBlocking value.
|
|
|
|
push(OnlyAvailablePred);
|
|
|
|
}
|
2010-05-26 18:52:00 +00:00
|
|
|
|
|
|
|
SUnit *LatencyPriorityQueue::pop() {
|
|
|
|
if (empty()) return NULL;
|
|
|
|
std::vector<SUnit *>::iterator Best = Queue.begin();
|
2010-05-30 13:14:21 +00:00
|
|
|
for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
|
2010-05-26 18:52:00 +00:00
|
|
|
E = Queue.end(); I != E; ++I)
|
|
|
|
if (Picker(*Best, *I))
|
|
|
|
Best = I;
|
|
|
|
SUnit *V = *Best;
|
|
|
|
if (Best != prior(Queue.end()))
|
|
|
|
std::swap(*Best, Queue.back());
|
|
|
|
Queue.pop_back();
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LatencyPriorityQueue::remove(SUnit *SU) {
|
|
|
|
assert(!Queue.empty() && "Queue is empty!");
|
|
|
|
std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(), SU);
|
|
|
|
if (I != prior(Queue.end()))
|
|
|
|
std::swap(*I, Queue.back());
|
|
|
|
Queue.pop_back();
|
|
|
|
}
|
Various bits of framework needed for precise machine-level selection
DAG scheduling during isel. Most new functionality is currently
guarded by -enable-sched-cycles and -enable-sched-hazard.
Added InstrItineraryData::IssueWidth field, currently derived from
ARM itineraries, but could be initialized differently on other targets.
Added ScheduleHazardRecognizer::MaxLookAhead to indicate whether it is
active, and if so how many cycles of state it holds.
Added SchedulingPriorityQueue::HasReadyFilter to allowing gating entry
into the scheduler's available queue.
ScoreboardHazardRecognizer now accesses the ScheduleDAG in order to
get information about it's SUnits, provides RecedeCycle for bottom-up
scheduling, correctly computes scoreboard depth, tracks IssueCount, and
considers potential stall cycles when checking for hazards.
ScheduleDAGRRList now models machine cycles and hazards (under
flags). It tracks MinAvailableCycle, drives the hazard recognizer and
priority queue's ready filter, manages a new PendingQueue, properly
accounts for stall cycles, etc.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122541 91177308-0d34-0410-b5e6-96231b3b80d8
2010-12-24 05:03:26 +00:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {}
|
|
|
|
#else
|
|
|
|
void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
|
|
|
|
LatencyPriorityQueue q = *this;
|
|
|
|
while (!q.empty()) {
|
|
|
|
SUnit *su = q.pop();
|
|
|
|
dbgs() << "Height " << su->getHeight() << ": ";
|
|
|
|
su->dump(DAG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|