2016-10-20 16:27:16 +08:00
|
|
|
//-- SystemZMachineScheduler.cpp - SystemZ Scheduler Interface -*- C++ -*---==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-10-20 16:27:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// -------------------------- Post RA scheduling ---------------------------- //
|
|
|
|
// SystemZPostRASchedStrategy is a scheduling strategy which is plugged into
|
|
|
|
// the MachineScheduler. It has a sorted Available set of SUs and a pickNode()
|
|
|
|
// implementation that looks to optimize decoder grouping and balance the
|
2017-08-17 16:33:44 +08:00
|
|
|
// usage of processor resources. Scheduler states are saved for the end
|
|
|
|
// region of each MBB, so that a successor block can learn from it.
|
2016-10-20 16:27:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SystemZMachineScheduler.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-07-12 06:08:28 +08:00
|
|
|
#define DEBUG_TYPE "machine-scheduler"
|
2016-10-20 16:27:16 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Print the set of SUs
|
|
|
|
void SystemZPostRASchedStrategy::SUSet::
|
2017-06-22 07:02:57 +08:00
|
|
|
dump(SystemZHazardRecognizer &HazardRec) const {
|
2016-10-20 16:27:16 +08:00
|
|
|
dbgs() << "{";
|
|
|
|
for (auto &SU : *this) {
|
|
|
|
HazardRec.dumpSU(SU, dbgs());
|
|
|
|
if (SU != *rbegin())
|
|
|
|
dbgs() << ", ";
|
|
|
|
}
|
|
|
|
dbgs() << "}\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-17 16:33:44 +08:00
|
|
|
// Try to find a single predecessor that would be interesting for the
|
|
|
|
// scheduler in the top-most region of MBB.
|
|
|
|
static MachineBasicBlock *getSingleSchedPred(MachineBasicBlock *MBB,
|
|
|
|
const MachineLoop *Loop) {
|
|
|
|
MachineBasicBlock *PredMBB = nullptr;
|
|
|
|
if (MBB->pred_size() == 1)
|
|
|
|
PredMBB = *MBB->pred_begin();
|
|
|
|
|
|
|
|
// The loop header has two predecessors, return the latch, but not for a
|
|
|
|
// single block loop.
|
|
|
|
if (MBB->pred_size() == 2 && Loop != nullptr && Loop->getHeader() == MBB) {
|
|
|
|
for (auto I = MBB->pred_begin(); I != MBB->pred_end(); ++I)
|
|
|
|
if (Loop->contains(*I))
|
|
|
|
PredMBB = (*I == MBB ? nullptr : *I);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert ((PredMBB == nullptr || !Loop || Loop->contains(PredMBB))
|
|
|
|
&& "Loop MBB should not consider predecessor outside of loop.");
|
|
|
|
|
|
|
|
return PredMBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZPostRASchedStrategy::
|
|
|
|
advanceTo(MachineBasicBlock::iterator NextBegin) {
|
|
|
|
MachineBasicBlock::iterator LastEmittedMI = HazardRec->getLastEmittedMI();
|
|
|
|
MachineBasicBlock::iterator I =
|
|
|
|
((LastEmittedMI != nullptr && LastEmittedMI->getParent() == MBB) ?
|
|
|
|
std::next(LastEmittedMI) : MBB->begin());
|
|
|
|
|
|
|
|
for (; I != NextBegin; ++I) {
|
2018-05-09 10:42:00 +08:00
|
|
|
if (I->isPosition() || I->isDebugInstr())
|
2017-08-17 16:33:44 +08:00
|
|
|
continue;
|
|
|
|
HazardRec->emitInstruction(&*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:39:00 +08:00
|
|
|
void SystemZPostRASchedStrategy::initialize(ScheduleDAGMI *dag) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(HazardRec->dumpState(););
|
2018-03-07 16:39:00 +08:00
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:44 +08:00
|
|
|
void SystemZPostRASchedStrategy::enterMBB(MachineBasicBlock *NextMBB) {
|
|
|
|
assert ((SchedStates.find(NextMBB) == SchedStates.end()) &&
|
|
|
|
"Entering MBB twice?");
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Entering " << printMBBReference(*NextMBB));
|
2017-08-17 16:33:44 +08:00
|
|
|
|
|
|
|
MBB = NextMBB;
|
2018-03-07 16:39:00 +08:00
|
|
|
|
2017-08-17 16:33:44 +08:00
|
|
|
/// Create a HazardRec for MBB, save it in SchedStates and set HazardRec to
|
|
|
|
/// point to it.
|
|
|
|
HazardRec = SchedStates[MBB] = new SystemZHazardRecognizer(TII, &SchedModel);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(const MachineLoop *Loop = MLI->getLoopFor(MBB);
|
|
|
|
if (Loop && Loop->getHeader() == MBB) dbgs() << " (Loop header)";
|
|
|
|
dbgs() << ":\n";);
|
2017-08-17 16:33:44 +08:00
|
|
|
|
|
|
|
// Try to take over the state from a single predecessor, if it has been
|
|
|
|
// scheduled. If this is not possible, we are done.
|
|
|
|
MachineBasicBlock *SinglePredMBB =
|
|
|
|
getSingleSchedPred(MBB, MLI->getLoopFor(MBB));
|
|
|
|
if (SinglePredMBB == nullptr ||
|
|
|
|
SchedStates.find(SinglePredMBB) == SchedStates.end())
|
|
|
|
return;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Continued scheduling from "
|
|
|
|
<< printMBBReference(*SinglePredMBB) << "\n";);
|
2017-08-17 16:33:44 +08:00
|
|
|
|
|
|
|
HazardRec->copyState(SchedStates[SinglePredMBB]);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(HazardRec->dumpState(););
|
2017-08-17 16:33:44 +08:00
|
|
|
|
|
|
|
// Emit incoming terminator(s). Be optimistic and assume that branch
|
|
|
|
// prediction will generally do "the right thing".
|
|
|
|
for (MachineBasicBlock::iterator I = SinglePredMBB->getFirstTerminator();
|
|
|
|
I != SinglePredMBB->end(); I++) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Emitting incoming branch: "; I->dump(););
|
2017-08-17 16:33:44 +08:00
|
|
|
bool TakenBranch = (I->isBranch() &&
|
|
|
|
(TII->getBranchInfo(*I).Target->isReg() || // Relative branch
|
|
|
|
TII->getBranchInfo(*I).Target->getMBB() == MBB));
|
|
|
|
HazardRec->emitInstruction(&*I, TakenBranch);
|
|
|
|
if (TakenBranch)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZPostRASchedStrategy::leaveMBB() {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Leaving " << printMBBReference(*MBB) << "\n";);
|
2017-08-17 16:33:44 +08:00
|
|
|
|
|
|
|
// Advance to first terminator. The successor block will handle terminators
|
|
|
|
// dependent on CFG layout (T/NT branch etc).
|
|
|
|
advanceTo(MBB->getFirstTerminator());
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:27:16 +08:00
|
|
|
SystemZPostRASchedStrategy::
|
|
|
|
SystemZPostRASchedStrategy(const MachineSchedContext *C)
|
2017-08-17 16:33:44 +08:00
|
|
|
: MLI(C->MLI),
|
|
|
|
TII(static_cast<const SystemZInstrInfo *>
|
2018-07-31 03:41:25 +08:00
|
|
|
(C->MF->getSubtarget().getInstrInfo())),
|
2017-08-17 16:33:44 +08:00
|
|
|
MBB(nullptr), HazardRec(nullptr) {
|
|
|
|
const TargetSubtargetInfo *ST = &C->MF->getSubtarget();
|
2018-04-09 03:56:04 +08:00
|
|
|
SchedModel.init(ST);
|
2017-08-17 16:33:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SystemZPostRASchedStrategy::~SystemZPostRASchedStrategy() {
|
|
|
|
// Delete hazard recognizers kept around for each MBB.
|
|
|
|
for (auto I : SchedStates) {
|
|
|
|
SystemZHazardRecognizer *hazrec = I.second;
|
|
|
|
delete hazrec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZPostRASchedStrategy::initPolicy(MachineBasicBlock::iterator Begin,
|
|
|
|
MachineBasicBlock::iterator End,
|
|
|
|
unsigned NumRegionInstrs) {
|
|
|
|
// Don't emit the terminators.
|
|
|
|
if (Begin->isTerminator())
|
|
|
|
return;
|
2016-10-20 16:27:16 +08:00
|
|
|
|
2017-08-17 16:33:44 +08:00
|
|
|
// Emit any instructions before start of region.
|
|
|
|
advanceTo(Begin);
|
2016-10-20 16:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the next node to schedule.
|
|
|
|
SUnit *SystemZPostRASchedStrategy::pickNode(bool &IsTopNode) {
|
|
|
|
// Only scheduling top-down.
|
|
|
|
IsTopNode = true;
|
|
|
|
|
|
|
|
if (Available.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If only one choice, return it.
|
|
|
|
if (Available.size() == 1) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Only one: ";
|
|
|
|
HazardRec->dumpSU(*Available.begin(), dbgs()); dbgs() << "\n";);
|
2016-10-20 16:27:16 +08:00
|
|
|
return *Available.begin();
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:00:42 +08:00
|
|
|
// All nodes that are possible to schedule are stored in the Available set.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Available: "; Available.dump(*HazardRec););
|
2016-10-20 16:27:16 +08:00
|
|
|
|
|
|
|
Candidate Best;
|
|
|
|
for (auto *SU : Available) {
|
|
|
|
|
|
|
|
// SU is the next candidate to be compared against current Best.
|
2017-08-17 16:33:44 +08:00
|
|
|
Candidate c(SU, *HazardRec);
|
2016-10-20 16:27:16 +08:00
|
|
|
|
|
|
|
// Remeber which SU is the best candidate.
|
|
|
|
if (Best.SU == nullptr || c < Best) {
|
|
|
|
Best = c;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Best so far: ";);
|
2018-03-07 16:39:00 +08:00
|
|
|
} else
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Tried : ";);
|
|
|
|
LLVM_DEBUG(HazardRec->dumpSU(c.SU, dbgs()); c.dumpCosts();
|
|
|
|
dbgs() << " Height:" << c.SU->getHeight(); dbgs() << "\n";);
|
2016-10-20 16:27:16 +08:00
|
|
|
|
|
|
|
// Once we know we have seen all SUs that affect grouping or use unbuffered
|
|
|
|
// resources, we can stop iterating if Best looks good.
|
|
|
|
if (!SU->isScheduleHigh && Best.noCost())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (Best.SU != nullptr);
|
|
|
|
return Best.SU;
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemZPostRASchedStrategy::Candidate::
|
|
|
|
Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec) : Candidate() {
|
|
|
|
SU = SU_;
|
|
|
|
|
|
|
|
// Check the grouping cost. For a node that must begin / end a
|
|
|
|
// group, it is positive if it would do so prematurely, or negative
|
|
|
|
// if it would fit naturally into the schedule.
|
|
|
|
GroupingCost = HazardRec.groupingCost(SU);
|
|
|
|
|
2018-03-07 16:39:00 +08:00
|
|
|
// Check the resources cost for this SU.
|
2016-10-20 16:27:16 +08:00
|
|
|
ResourcesCost = HazardRec.resourcesCost(SU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemZPostRASchedStrategy::Candidate::
|
|
|
|
operator<(const Candidate &other) {
|
|
|
|
|
|
|
|
// Check decoder grouping.
|
|
|
|
if (GroupingCost < other.GroupingCost)
|
|
|
|
return true;
|
|
|
|
if (GroupingCost > other.GroupingCost)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Compare the use of resources.
|
|
|
|
if (ResourcesCost < other.ResourcesCost)
|
|
|
|
return true;
|
|
|
|
if (ResourcesCost > other.ResourcesCost)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Higher SU is otherwise generally better.
|
|
|
|
if (SU->getHeight() > other.SU->getHeight())
|
|
|
|
return true;
|
|
|
|
if (SU->getHeight() < other.SU->getHeight())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If all same, fall back to original order.
|
|
|
|
if (SU->NodeNum < other.SU->NodeNum)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZPostRASchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "** Scheduling SU(" << SU->NodeNum << ") ";
|
|
|
|
if (Available.size() == 1) dbgs() << "(only one) ";
|
|
|
|
Candidate c(SU, *HazardRec); c.dumpCosts(); dbgs() << "\n";);
|
2016-10-20 16:27:16 +08:00
|
|
|
|
|
|
|
// Remove SU from Available set and update HazardRec.
|
|
|
|
Available.erase(SU);
|
2017-08-17 16:33:44 +08:00
|
|
|
HazardRec->EmitInstruction(SU);
|
2016-10-20 16:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZPostRASchedStrategy::releaseTopNode(SUnit *SU) {
|
|
|
|
// Set isScheduleHigh flag on all SUs that we want to consider first in
|
|
|
|
// pickNode().
|
2017-08-17 16:33:44 +08:00
|
|
|
const MCSchedClassDesc *SC = HazardRec->getSchedClass(SU);
|
2016-10-20 16:27:16 +08:00
|
|
|
bool AffectsGrouping = (SC->isValid() && (SC->BeginGroup || SC->EndGroup));
|
|
|
|
SU->isScheduleHigh = (AffectsGrouping || SU->isUnbuffered);
|
|
|
|
|
|
|
|
// Put all released SUs in the Available set.
|
|
|
|
Available.insert(SU);
|
|
|
|
}
|