2007-07-14 01:31:29 +08:00
|
|
|
//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
|
2007-07-14 01:13:54 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-14 01:13:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements a top-down list scheduler, using standard algorithms.
|
|
|
|
// The basic approach uses a priority queue of available nodes to schedule.
|
|
|
|
// One at a time, nodes are taken from the priority queue (thus in priority
|
|
|
|
// order), checked for legality to schedule, and emitted if legal.
|
|
|
|
//
|
|
|
|
// Nodes may not be legal to schedule either due to structural hazards (e.g.
|
|
|
|
// pipeline or resource constraints) or because an input to the instruction has
|
|
|
|
// not completed execution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "post-RA-sched"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/CodeGen/ScheduleDAGInstrs.h"
|
|
|
|
#include "llvm/CodeGen/LatencyPriorityQueue.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2007-07-14 01:13:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-01-15 03:00:06 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-07-14 01:13:54 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-07-14 01:13:54 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
STATISTIC(NumStalls, "Number of pipeline stalls");
|
|
|
|
|
2007-07-14 01:13:54 +08:00
|
|
|
namespace {
|
2008-11-20 07:18:57 +08:00
|
|
|
class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
|
2007-07-14 01:13:54 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
2008-11-20 07:18:57 +08:00
|
|
|
PostRAScheduler() : MachineFunctionPass(&ID) {}
|
|
|
|
private:
|
|
|
|
MachineFunction *MF;
|
|
|
|
const TargetMachine *TM;
|
|
|
|
public:
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Post RA top-down list latency scheduler (STUB)";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
};
|
|
|
|
char PostRAScheduler::ID = 0;
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
|
|
|
|
public:
|
|
|
|
SchedulePostRATDList(MachineBasicBlock *mbb, const TargetMachine &tm)
|
|
|
|
: ScheduleDAGInstrs(mbb, tm) {}
|
2007-07-14 01:13:54 +08:00
|
|
|
private:
|
|
|
|
MachineFunction *MF;
|
|
|
|
const TargetMachine *TM;
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
/// AvailableQueue - The priority queue to use for the available SUnits.
|
|
|
|
///
|
|
|
|
LatencyPriorityQueue AvailableQueue;
|
|
|
|
|
|
|
|
/// PendingQueue - This contains all of the instructions whose operands have
|
|
|
|
/// been issued, but their results are not ready yet (due to the latency of
|
|
|
|
/// the operation). Once the operands becomes available, the instruction is
|
|
|
|
/// added to the AvailableQueue.
|
|
|
|
std::vector<SUnit*> PendingQueue;
|
|
|
|
|
2007-07-14 01:13:54 +08:00
|
|
|
public:
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Post RA top-down list latency scheduler (STUB)";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
void Schedule();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
|
|
|
|
void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
|
|
|
|
void ListScheduleTopDown();
|
2007-07-14 01:13:54 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
DOUT << "PostRAScheduler\n";
|
2007-07-14 01:13:54 +08:00
|
|
|
MF = &Fn;
|
|
|
|
TM = &MF->getTarget();
|
|
|
|
|
|
|
|
// Loop over all of the basic blocks
|
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
2008-11-20 07:18:57 +08:00
|
|
|
MBB != MBBe; ++MBB) {
|
|
|
|
|
|
|
|
SchedulePostRATDList Scheduler(MBB, *TM);
|
|
|
|
|
|
|
|
Scheduler.Run();
|
|
|
|
|
|
|
|
Scheduler.EmitSchedule();
|
|
|
|
}
|
2007-07-14 01:13:54 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
/// Schedule - Schedule the DAG using list scheduling.
|
|
|
|
void SchedulePostRATDList::Schedule() {
|
|
|
|
DOUT << "********** List Scheduling **********\n";
|
|
|
|
|
|
|
|
// Build scheduling units.
|
|
|
|
BuildSchedUnits();
|
|
|
|
|
|
|
|
AvailableQueue.initNodes(SUnits);
|
|
|
|
|
|
|
|
ListScheduleTopDown();
|
|
|
|
|
|
|
|
AvailableQueue.releaseState();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-Down Scheduling
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
|
|
|
|
/// the PendingQueue if the count reaches zero. Also update its cycle bound.
|
|
|
|
void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
|
|
|
|
--SuccSU->NumPredsLeft;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (SuccSU->NumPredsLeft < 0) {
|
|
|
|
cerr << "*** Scheduling failed! ***\n";
|
|
|
|
SuccSU->dump(this);
|
|
|
|
cerr << " has been released too many times!\n";
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Compute how many cycles it will be before this actually becomes
|
|
|
|
// available. This is the max of the start time of all predecessors plus
|
|
|
|
// their latencies.
|
|
|
|
// If this is a token edge, we don't need to wait for the latency of the
|
|
|
|
// preceeding instruction (e.g. a long-latency load) unless there is also
|
|
|
|
// some other data dependence.
|
|
|
|
unsigned PredDoneCycle = SU->Cycle;
|
|
|
|
if (!isChain)
|
|
|
|
PredDoneCycle += SU->Latency;
|
|
|
|
else if (SU->Latency)
|
|
|
|
PredDoneCycle += 1;
|
|
|
|
SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
|
|
|
|
|
|
|
|
if (SuccSU->NumPredsLeft == 0) {
|
|
|
|
PendingQueue.push_back(SuccSU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
|
|
|
|
/// count of its successors. If a successor pending count is zero, add it to
|
|
|
|
/// the Available queue.
|
|
|
|
void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
|
|
|
|
DOUT << "*** Scheduling [" << CurCycle << "]: ";
|
|
|
|
DEBUG(SU->dump(this));
|
|
|
|
|
|
|
|
Sequence.push_back(SU);
|
|
|
|
SU->Cycle = CurCycle;
|
|
|
|
|
|
|
|
// Top down: release successors.
|
|
|
|
for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
|
|
|
|
I != E; ++I)
|
|
|
|
ReleaseSucc(SU, I->Dep, I->isCtrl);
|
|
|
|
|
|
|
|
SU->isScheduled = true;
|
|
|
|
AvailableQueue.ScheduledNode(SU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ListScheduleTopDown - The main loop of list scheduling for top-down
|
|
|
|
/// schedulers.
|
|
|
|
void SchedulePostRATDList::ListScheduleTopDown() {
|
|
|
|
unsigned CurCycle = 0;
|
|
|
|
|
|
|
|
// All leaves to Available queue.
|
|
|
|
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
|
|
|
|
// It is available if it has no predecessors.
|
|
|
|
if (SUnits[i].Preds.empty()) {
|
|
|
|
AvailableQueue.push(&SUnits[i]);
|
|
|
|
SUnits[i].isAvailable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// While Available queue is not empty, grab the node with the highest
|
|
|
|
// priority. If it is not ready put it back. Schedule the node.
|
|
|
|
Sequence.reserve(SUnits.size());
|
|
|
|
while (!AvailableQueue.empty() || !PendingQueue.empty()) {
|
|
|
|
// Check to see if any of the pending instructions are ready to issue. If
|
|
|
|
// so, add them to the available queue.
|
|
|
|
for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
|
|
|
|
if (PendingQueue[i]->CycleBound == CurCycle) {
|
|
|
|
AvailableQueue.push(PendingQueue[i]);
|
|
|
|
PendingQueue[i]->isAvailable = true;
|
|
|
|
PendingQueue[i] = PendingQueue.back();
|
|
|
|
PendingQueue.pop_back();
|
|
|
|
--i; --e;
|
|
|
|
} else {
|
|
|
|
assert(PendingQueue[i]->CycleBound > CurCycle && "Negative latency?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no instructions available, don't try to issue anything, and
|
|
|
|
// don't advance the hazard recognizer.
|
|
|
|
if (AvailableQueue.empty()) {
|
|
|
|
++CurCycle;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SUnit *FoundSUnit = AvailableQueue.pop();
|
|
|
|
|
|
|
|
// If we found a node to schedule, do it now.
|
|
|
|
if (FoundSUnit) {
|
|
|
|
ScheduleNodeTopDown(FoundSUnit, CurCycle);
|
|
|
|
|
|
|
|
// If this is a pseudo-op node, we don't want to increment the current
|
|
|
|
// cycle.
|
|
|
|
if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
|
|
|
|
++CurCycle;
|
|
|
|
} else {
|
|
|
|
// Otherwise, we have a pipeline stall, but no other problem, just advance
|
|
|
|
// the current cycle and try again.
|
|
|
|
DOUT << "*** Advancing cycle, no work to do\n";
|
|
|
|
++NumStalls;
|
|
|
|
++CurCycle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify that all SUnits were scheduled.
|
|
|
|
bool AnyNotSched = false;
|
|
|
|
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
|
|
|
|
if (SUnits[i].NumPredsLeft != 0) {
|
|
|
|
if (!AnyNotSched)
|
|
|
|
cerr << "*** List scheduling failed! ***\n";
|
|
|
|
SUnits[i].dump(this);
|
|
|
|
cerr << "has not been scheduled!\n";
|
|
|
|
AnyNotSched = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!AnyNotSched);
|
|
|
|
#endif
|
|
|
|
}
|
2007-07-14 01:13:54 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public Constructor Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
FunctionPass *llvm::createPostRAScheduler() {
|
2008-11-20 07:18:57 +08:00
|
|
|
return new PostRAScheduler();
|
2007-07-14 01:13:54 +08:00
|
|
|
}
|