2010-12-08 20:04:29 +00:00
|
|
|
//===----- ScoreboardHazardRecognizer.cpp - Scheduler Support -------------===//
|
2009-08-10 15:55:25 +00:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2010-12-08 20:04:29 +00:00
|
|
|
// This file implements the ScoreboardHazardRecognizer class, which
|
|
|
|
|
// encapsultes hazard-avoidance heuristics for scheduling, based on the
|
|
|
|
|
// scheduling itineraries specified for the target.
|
2009-08-10 15:55:25 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2010-12-08 20:04:29 +00:00
|
|
|
#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
|
2010-06-14 21:06:53 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2011-06-29 01:14:12 +00:00
|
|
|
#include "llvm/MC/MCInstrItineraries.h"
|
2009-08-10 15:55:25 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-11 01:44:26 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-21 05:51:33 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2009-08-22 20:08:44 +00:00
|
|
|
using namespace llvm;
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2016-04-20 00:21:24 +00:00
|
|
|
#define DEBUG_TYPE DebugType
|
2014-04-22 02:02:50 +00:00
|
|
|
|
2016-04-20 00:21:24 +00:00
|
|
|
ScoreboardHazardRecognizer::ScoreboardHazardRecognizer(
|
|
|
|
|
const InstrItineraryData *II, const ScheduleDAG *SchedDAG,
|
|
|
|
|
const char *ParentDebugType)
|
|
|
|
|
: ScheduleHazardRecognizer(), DebugType(ParentDebugType), ItinData(II),
|
|
|
|
|
DAG(SchedDAG), IssueWidth(0), IssueCount(0) {
|
2010-12-24 05:03:26 +00:00
|
|
|
|
2012-06-05 03:44:32 +00:00
|
|
|
// Determine the maximum depth of any itinerary. This determines the depth of
|
|
|
|
|
// the scoreboard. We always make the scoreboard at least 1 cycle deep to
|
|
|
|
|
// avoid dealing with the boundary condition.
|
2010-04-07 18:19:24 +00:00
|
|
|
unsigned ScoreboardDepth = 1;
|
2010-09-10 01:29:16 +00:00
|
|
|
if (ItinData && !ItinData->isEmpty()) {
|
2009-08-10 15:55:25 +00:00
|
|
|
for (unsigned idx = 0; ; ++idx) {
|
2010-09-10 01:29:16 +00:00
|
|
|
if (ItinData->isEndMarker(idx))
|
2009-08-10 15:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
2010-09-10 01:29:16 +00:00
|
|
|
const InstrStage *IS = ItinData->beginStage(idx);
|
|
|
|
|
const InstrStage *E = ItinData->endStage(idx);
|
2010-12-24 05:03:26 +00:00
|
|
|
unsigned CurCycle = 0;
|
2009-08-10 15:55:25 +00:00
|
|
|
unsigned ItinDepth = 0;
|
2010-12-24 05:03:26 +00:00
|
|
|
for (; IS != E; ++IS) {
|
|
|
|
|
unsigned StageDepth = CurCycle + IS->getCycles();
|
|
|
|
|
if (ItinDepth < StageDepth) ItinDepth = StageDepth;
|
|
|
|
|
CurCycle += IS->getNextCycles();
|
|
|
|
|
}
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2010-12-08 20:04:29 +00:00
|
|
|
// Find the next power-of-2 >= ItinDepth
|
|
|
|
|
while (ItinDepth > ScoreboardDepth) {
|
|
|
|
|
ScoreboardDepth *= 2;
|
2012-06-05 03:44:32 +00:00
|
|
|
// Don't set MaxLookAhead until we find at least one nonzero stage.
|
|
|
|
|
// This way, an itinerary with no stages has MaxLookAhead==0, which
|
|
|
|
|
// completely bypasses the scoreboard hazard logic.
|
|
|
|
|
MaxLookAhead = ScoreboardDepth;
|
2010-12-08 20:04:29 +00:00
|
|
|
}
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-07 18:19:32 +00:00
|
|
|
ReservedScoreboard.reset(ScoreboardDepth);
|
|
|
|
|
RequiredScoreboard.reset(ScoreboardDepth);
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2012-07-07 04:00:00 +00:00
|
|
|
// If MaxLookAhead is not set above, then we are not enabled.
|
2012-06-05 03:44:40 +00:00
|
|
|
if (!isEnabled())
|
2012-06-05 03:44:32 +00:00
|
|
|
DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
|
2012-06-05 03:44:40 +00:00
|
|
|
else {
|
2012-07-07 04:00:00 +00:00
|
|
|
// A nonempty itinerary must have a SchedModel.
|
2014-09-02 17:43:54 +00:00
|
|
|
IssueWidth = ItinData->SchedModel.IssueWidth;
|
2012-06-05 03:44:32 +00:00
|
|
|
DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
|
|
|
|
|
<< ScoreboardDepth << '\n');
|
2012-06-05 03:44:40 +00:00
|
|
|
}
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-08 20:04:29 +00:00
|
|
|
void ScoreboardHazardRecognizer::Reset() {
|
2010-12-24 05:03:26 +00:00
|
|
|
IssueCount = 0;
|
2010-04-07 18:19:32 +00:00
|
|
|
RequiredScoreboard.reset();
|
|
|
|
|
ReservedScoreboard.reset();
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-11 22:23:19 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 20:50:44 +00:00
|
|
|
LLVM_DUMP_METHOD void ScoreboardHazardRecognizer::Scoreboard::dump() const {
|
2010-01-04 21:26:07 +00:00
|
|
|
dbgs() << "Scoreboard:\n";
|
2010-04-07 18:19:24 +00:00
|
|
|
|
|
|
|
|
unsigned last = Depth - 1;
|
|
|
|
|
while ((last > 0) && ((*this)[last] == 0))
|
2009-08-10 15:55:25 +00:00
|
|
|
last--;
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i <= last; i++) {
|
2012-06-22 20:27:13 +00:00
|
|
|
unsigned FUs = (*this)[i];
|
2010-01-04 21:26:07 +00:00
|
|
|
dbgs() << "\t";
|
2012-06-22 20:27:13 +00:00
|
|
|
for (int j = 31; j >= 0; j--)
|
|
|
|
|
dbgs() << ((FUs & (1 << j)) ? '1' : '0');
|
2010-01-04 21:26:07 +00:00
|
|
|
dbgs() << '\n';
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-09-06 19:06:06 +00:00
|
|
|
#endif
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2010-12-24 05:03:26 +00:00
|
|
|
bool ScoreboardHazardRecognizer::atIssueLimit() const {
|
|
|
|
|
if (IssueWidth == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return IssueCount == IssueWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 07:35:02 +00:00
|
|
|
ScheduleHazardRecognizer::HazardType
|
2010-12-24 05:03:26 +00:00
|
|
|
ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
|
2010-09-10 01:29:16 +00:00
|
|
|
if (!ItinData || ItinData->isEmpty())
|
2009-09-22 16:47:52 +00:00
|
|
|
return NoHazard;
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2010-12-24 05:03:26 +00:00
|
|
|
// Note that stalls will be negative for bottom-up scheduling.
|
|
|
|
|
int cycle = Stalls;
|
2009-09-22 16:47:52 +00:00
|
|
|
|
|
|
|
|
// Use the itinerary for the underlying instruction to check for
|
|
|
|
|
// free FU's in the scoreboard at the appropriate future cycles.
|
2010-12-24 05:03:26 +00:00
|
|
|
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
|
2014-04-14 00:51:57 +00:00
|
|
|
if (!MCID) {
|
2010-12-24 05:03:26 +00:00
|
|
|
// Don't check hazards for non-machineinstr Nodes.
|
|
|
|
|
return NoHazard;
|
|
|
|
|
}
|
2011-06-28 19:10:37 +00:00
|
|
|
unsigned idx = MCID->getSchedClass();
|
2010-09-10 01:29:16 +00:00
|
|
|
for (const InstrStage *IS = ItinData->beginStage(idx),
|
|
|
|
|
*E = ItinData->endStage(idx); IS != E; ++IS) {
|
2009-09-22 16:47:52 +00:00
|
|
|
// We must find one of the stage's units free for every cycle the
|
|
|
|
|
// stage is occupied. FIXME it would be more accurate to find the
|
|
|
|
|
// same unit free in all the cycles.
|
|
|
|
|
for (unsigned int i = 0; i < IS->getCycles(); ++i) {
|
2010-12-24 05:03:26 +00:00
|
|
|
int StageCycle = cycle + (int)i;
|
|
|
|
|
if (StageCycle < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
|
|
|
|
|
assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
|
|
|
|
|
"Scoreboard depth exceeded!");
|
|
|
|
|
// This stage was stalled beyond pipeline depth, so cannot conflict.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2012-06-22 20:27:13 +00:00
|
|
|
unsigned freeUnits = IS->getUnits();
|
2010-04-07 18:19:32 +00:00
|
|
|
switch (IS->getReservationKind()) {
|
|
|
|
|
case InstrStage::Required:
|
|
|
|
|
// Required FUs conflict with both reserved and required ones
|
2010-12-24 05:03:26 +00:00
|
|
|
freeUnits &= ~ReservedScoreboard[StageCycle];
|
2016-08-17 05:10:15 +00:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-04-07 18:19:32 +00:00
|
|
|
case InstrStage::Reserved:
|
|
|
|
|
// Reserved FUs can conflict only with required ones.
|
2010-12-24 05:03:26 +00:00
|
|
|
freeUnits &= ~RequiredScoreboard[StageCycle];
|
2010-04-07 18:19:32 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-22 16:47:52 +00:00
|
|
|
if (!freeUnits) {
|
2012-05-26 11:37:37 +00:00
|
|
|
DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
|
2010-01-04 21:26:07 +00:00
|
|
|
DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
|
2010-12-24 05:03:26 +00:00
|
|
|
DEBUG(DAG->dumpNode(SU));
|
2009-09-22 16:47:52 +00:00
|
|
|
return Hazard;
|
|
|
|
|
}
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2009-09-22 16:47:52 +00:00
|
|
|
// Advance the cycle to the next stage.
|
|
|
|
|
cycle += IS->getNextCycles();
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NoHazard;
|
|
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2010-12-08 20:04:29 +00:00
|
|
|
void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
|
2010-09-10 01:29:16 +00:00
|
|
|
if (!ItinData || ItinData->isEmpty())
|
2009-09-22 16:47:52 +00:00
|
|
|
return;
|
2009-08-10 15:55:25 +00:00
|
|
|
|
2009-09-22 16:47:52 +00:00
|
|
|
// Use the itinerary for the underlying instruction to reserve FU's
|
|
|
|
|
// in the scoreboard at the appropriate future cycles.
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
|
|
|
|
|
assert(MCID && "The scheduler must filter non-machineinstrs");
|
|
|
|
|
if (DAG->TII->isZeroCost(MCID->Opcode))
|
2011-01-21 05:51:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
++IssueCount;
|
|
|
|
|
|
|
|
|
|
unsigned cycle = 0;
|
|
|
|
|
|
2011-06-28 19:10:37 +00:00
|
|
|
unsigned idx = MCID->getSchedClass();
|
2010-09-10 01:29:16 +00:00
|
|
|
for (const InstrStage *IS = ItinData->beginStage(idx),
|
|
|
|
|
*E = ItinData->endStage(idx); IS != E; ++IS) {
|
2009-09-22 16:47:52 +00:00
|
|
|
// We must reserve one of the stage's units for every cycle the
|
|
|
|
|
// stage is occupied. FIXME it would be more accurate to reserve
|
|
|
|
|
// the same unit free in all the cycles.
|
|
|
|
|
for (unsigned int i = 0; i < IS->getCycles(); ++i) {
|
2010-04-07 18:19:32 +00:00
|
|
|
assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
|
2009-09-22 16:47:52 +00:00
|
|
|
"Scoreboard depth exceeded!");
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2012-06-22 20:27:13 +00:00
|
|
|
unsigned freeUnits = IS->getUnits();
|
2010-04-07 18:19:32 +00:00
|
|
|
switch (IS->getReservationKind()) {
|
|
|
|
|
case InstrStage::Required:
|
|
|
|
|
// Required FUs conflict with both reserved and required ones
|
|
|
|
|
freeUnits &= ~ReservedScoreboard[cycle + i];
|
2016-08-17 05:10:15 +00:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-04-07 18:19:32 +00:00
|
|
|
case InstrStage::Reserved:
|
|
|
|
|
// Reserved FUs can conflict only with required ones.
|
|
|
|
|
freeUnits &= ~RequiredScoreboard[cycle + i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2009-09-22 16:47:52 +00:00
|
|
|
// reduce to a single unit
|
2012-06-22 20:27:13 +00:00
|
|
|
unsigned freeUnit = 0;
|
2009-09-22 16:47:52 +00:00
|
|
|
do {
|
|
|
|
|
freeUnit = freeUnits;
|
|
|
|
|
freeUnits = freeUnit & (freeUnit - 1);
|
|
|
|
|
} while (freeUnits);
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2010-04-07 18:19:32 +00:00
|
|
|
if (IS->getReservationKind() == InstrStage::Required)
|
|
|
|
|
RequiredScoreboard[cycle + i] |= freeUnit;
|
|
|
|
|
else
|
|
|
|
|
ReservedScoreboard[cycle + i] |= freeUnit;
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2009-09-22 16:47:52 +00:00
|
|
|
// Advance the cycle to the next stage.
|
|
|
|
|
cycle += IS->getNextCycles();
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2010-04-07 18:19:32 +00:00
|
|
|
DEBUG(ReservedScoreboard.dump());
|
|
|
|
|
DEBUG(RequiredScoreboard.dump());
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
2010-04-07 18:19:24 +00:00
|
|
|
|
2010-12-08 20:04:29 +00:00
|
|
|
void ScoreboardHazardRecognizer::AdvanceCycle() {
|
2010-12-24 05:03:26 +00:00
|
|
|
IssueCount = 0;
|
2010-04-07 18:19:32 +00:00
|
|
|
ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
|
|
|
|
|
RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
|
2009-08-10 15:55:25 +00:00
|
|
|
}
|
2010-12-08 20:04:29 +00:00
|
|
|
|
|
|
|
|
void ScoreboardHazardRecognizer::RecedeCycle() {
|
2010-12-24 05:03:26 +00:00
|
|
|
IssueCount = 0;
|
2010-12-08 20:04:29 +00:00
|
|
|
ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
|
|
|
|
|
ReservedScoreboard.recede();
|
|
|
|
|
RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
|
|
|
|
|
RequiredScoreboard.recede();
|
|
|
|
|
}
|