blob: b789e2d9c52c44ad4a0827d3a6519805b3d629e9 [file] [log] [blame]
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001//===- ScoreboardHazardRecognizer.cpp - Scheduler Support -----------------===//
David Goodwin6021b4d2009-08-10 15:55:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Andrew Trick00067fb2010-12-08 20:04:29 +000010// This file implements the ScoreboardHazardRecognizer class, which
11// encapsultes hazard-avoidance heuristics for scheduling, based on the
12// scheduling itineraries specified for the target.
David Goodwin6021b4d2009-08-10 15:55:25 +000013//
14//===----------------------------------------------------------------------===//
15
Andrew Trick00067fb2010-12-08 20:04:29 +000016#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Cheng078f4ce2010-06-14 21:06:53 +000017#include "llvm/CodeGen/ScheduleDAG.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000018#include "llvm/CodeGen/TargetInstrInfo.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000019#include "llvm/MC/MCInstrDesc.h"
Evan Cheng8264e272011-06-29 01:14:12 +000020#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000021#include "llvm/Support/Compiler.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000022#include "llvm/Support/Debug.h"
David Goodwinf20236a2009-08-11 01:44:26 +000023#include "llvm/Support/raw_ostream.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000024#include <cassert>
David Goodwin6021b4d2009-08-10 15:55:25 +000025
Bill Wendlinga2c45942009-08-22 20:08:44 +000026using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000027
Mehdi Aminiea0b1e72016-04-20 00:21:24 +000028#define DEBUG_TYPE DebugType
Chandler Carruth1b9dde02014-04-22 02:02:50 +000029
Mehdi Aminiea0b1e72016-04-20 00:21:24 +000030ScoreboardHazardRecognizer::ScoreboardHazardRecognizer(
31 const InstrItineraryData *II, const ScheduleDAG *SchedDAG,
32 const char *ParentDebugType)
33 : ScheduleHazardRecognizer(), DebugType(ParentDebugType), ItinData(II),
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000034 DAG(SchedDAG) {
Don Hinton53eb6372017-09-27 21:19:56 +000035 (void)DebugType;
Andrew Tricked7c96d2012-06-05 03:44:32 +000036 // Determine the maximum depth of any itinerary. This determines the depth of
37 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
38 // avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000039 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000040 if (ItinData && !ItinData->isEmpty()) {
David Goodwin6021b4d2009-08-10 15:55:25 +000041 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000042 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000043 break;
44
Evan Chengbf407072010-09-10 01:29:16 +000045 const InstrStage *IS = ItinData->beginStage(idx);
46 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000047 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000048 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000049 for (; IS != E; ++IS) {
50 unsigned StageDepth = CurCycle + IS->getCycles();
51 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
52 CurCycle += IS->getNextCycles();
53 }
David Goodwin6021b4d2009-08-10 15:55:25 +000054
Andrew Trick00067fb2010-12-08 20:04:29 +000055 // Find the next power-of-2 >= ItinDepth
56 while (ItinDepth > ScoreboardDepth) {
57 ScoreboardDepth *= 2;
Andrew Tricked7c96d2012-06-05 03:44:32 +000058 // Don't set MaxLookAhead until we find at least one nonzero stage.
59 // This way, an itinerary with no stages has MaxLookAhead==0, which
60 // completely bypasses the scoreboard hazard logic.
61 MaxLookAhead = ScoreboardDepth;
Andrew Trick00067fb2010-12-08 20:04:29 +000062 }
David Goodwin6021b4d2009-08-10 15:55:25 +000063 }
64 }
65
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000066 ReservedScoreboard.reset(ScoreboardDepth);
67 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000068
Andrew Trick87255e32012-07-07 04:00:00 +000069 // If MaxLookAhead is not set above, then we are not enabled.
Andrew Trick73d77362012-06-05 03:44:40 +000070 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000071 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000072 else {
Andrew Trick87255e32012-07-07 04:00:00 +000073 // A nonempty itinerary must have a SchedModel.
Pete Cooper11759452014-09-02 17:43:54 +000074 IssueWidth = ItinData->SchedModel.IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000075 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
76 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000077 }
David Goodwin6021b4d2009-08-10 15:55:25 +000078}
79
Andrew Trick00067fb2010-12-08 20:04:29 +000080void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000081 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000082 RequiredScoreboard.reset();
83 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000084}
85
Aaron Ballman615eb472017-10-15 14:32:27 +000086#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000087LLVM_DUMP_METHOD void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000088 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000089
90 unsigned last = Depth - 1;
91 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000092 last--;
93
94 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +000095 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +000096 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +000097 for (int j = 31; j >= 0; j--)
98 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +000099 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +0000100 }
101}
Manman Ren742534c2012-09-06 19:06:06 +0000102#endif
David Goodwin6021b4d2009-08-10 15:55:25 +0000103
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000104bool ScoreboardHazardRecognizer::atIssueLimit() const {
105 if (IssueWidth == 0)
106 return false;
107
108 return IssueCount == IssueWidth;
109}
110
Evan Chengf128bdc2010-06-16 07:35:02 +0000111ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000112ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000113 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000114 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000115
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116 // Note that stalls will be negative for bottom-up scheduling.
117 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000118
119 // Use the itinerary for the underlying instruction to check for
120 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000121
Evan Cheng6cc775f2011-06-28 19:10:37 +0000122 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
Craig Topperc0196b12014-04-14 00:51:57 +0000123 if (!MCID) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000124 // Don't check hazards for non-machineinstr Nodes.
125 return NoHazard;
126 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000127 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000128 for (const InstrStage *IS = ItinData->beginStage(idx),
129 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000130 // We must find one of the stage's units free for every cycle the
131 // stage is occupied. FIXME it would be more accurate to find the
132 // same unit free in all the cycles.
133 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000134 int StageCycle = cycle + (int)i;
135 if (StageCycle < 0)
136 continue;
137
138 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
139 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
140 "Scoreboard depth exceeded!");
141 // This stage was stalled beyond pipeline depth, so cannot conflict.
142 break;
143 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000144
Hal Finkel8db55472012-06-22 20:27:13 +0000145 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000146 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000147 case InstrStage::Required:
148 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000149 freeUnits &= ~ReservedScoreboard[StageCycle];
Justin Bognerb03fd122016-08-17 05:10:15 +0000150 LLVM_FALLTHROUGH;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000151 case InstrStage::Reserved:
152 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000153 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000154 break;
155 }
156
David Goodwin74b79562009-09-22 16:47:52 +0000157 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000158 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000159 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000160 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000161 return Hazard;
162 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000163 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000164
David Goodwin74b79562009-09-22 16:47:52 +0000165 // Advance the cycle to the next stage.
166 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000167 }
168
169 return NoHazard;
170}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000171
Andrew Trick00067fb2010-12-08 20:04:29 +0000172void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000173 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000174 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000175
David Goodwin74b79562009-09-22 16:47:52 +0000176 // Use the itinerary for the underlying instruction to reserve FU's
177 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000178 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
179 assert(MCID && "The scheduler must filter non-machineinstrs");
180 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000181 return;
182
183 ++IssueCount;
184
185 unsigned cycle = 0;
186
Evan Cheng6cc775f2011-06-28 19:10:37 +0000187 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000188 for (const InstrStage *IS = ItinData->beginStage(idx),
189 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000190 // We must reserve one of the stage's units for every cycle the
191 // stage is occupied. FIXME it would be more accurate to reserve
192 // the same unit free in all the cycles.
193 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000194 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000195 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000196
Hal Finkel8db55472012-06-22 20:27:13 +0000197 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000198 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000199 case InstrStage::Required:
200 // Required FUs conflict with both reserved and required ones
201 freeUnits &= ~ReservedScoreboard[cycle + i];
Justin Bognerb03fd122016-08-17 05:10:15 +0000202 LLVM_FALLTHROUGH;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000203 case InstrStage::Reserved:
204 // Reserved FUs can conflict only with required ones.
205 freeUnits &= ~RequiredScoreboard[cycle + i];
206 break;
207 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000208
David Goodwin74b79562009-09-22 16:47:52 +0000209 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000210 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000211 do {
212 freeUnit = freeUnits;
213 freeUnits = freeUnit & (freeUnit - 1);
214 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000215
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000216 if (IS->getReservationKind() == InstrStage::Required)
217 RequiredScoreboard[cycle + i] |= freeUnit;
218 else
219 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000220 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000221
David Goodwin74b79562009-09-22 16:47:52 +0000222 // Advance the cycle to the next stage.
223 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000224 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000225
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000226 DEBUG(ReservedScoreboard.dump());
227 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000228}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000229
Andrew Trick00067fb2010-12-08 20:04:29 +0000230void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000231 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000232 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
233 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000234}
Andrew Trick00067fb2010-12-08 20:04:29 +0000235
236void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000237 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000238 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
239 ReservedScoreboard.recede();
240 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
241 RequiredScoreboard.recede();
242}