blob: 2cd84d670aaa7a96234e73d1fa8c695574dbb4f3 [file] [log] [blame]
Andrew Trick00067fb2010-12-08 20:04:29 +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 Trick10ffc2b2010-12-24 05:03:26 +000016#define DEBUG_TYPE ::llvm::ScoreboardHazardRecognizer::DebugType
Andrew Trick00067fb2010-12-08 20:04:29 +000017#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Cheng078f4ce2010-06-14 21:06:53 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Evan Cheng8264e272011-06-29 01:14:12 +000019#include "llvm/MC/MCInstrItineraries.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000022#include "llvm/Support/raw_ostream.h"
Andrew Trick47ff14b2011-01-21 05:51:33 +000023#include "llvm/Target/TargetInstrInfo.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000024
Bill Wendlinga2c45942009-08-22 20:08:44 +000025using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000026
Andrew Trick10ffc2b2010-12-24 05:03:26 +000027#ifndef NDEBUG
28const char *ScoreboardHazardRecognizer::DebugType = "";
29#endif
30
Andrew Trick00067fb2010-12-08 20:04:29 +000031ScoreboardHazardRecognizer::
Andrew Trick10ffc2b2010-12-24 05:03:26 +000032ScoreboardHazardRecognizer(const InstrItineraryData *II,
33 const ScheduleDAG *SchedDAG,
34 const char *ParentDebugType) :
35 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
36 IssueCount(0) {
37
38#ifndef NDEBUG
39 DebugType = ParentDebugType;
40#endif
41
Andrew Tricked7c96d2012-06-05 03:44:32 +000042 // Determine the maximum depth of any itinerary. This determines the depth of
43 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
44 // avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000045 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000046 if (ItinData && !ItinData->isEmpty()) {
David Goodwin6021b4d2009-08-10 15:55:25 +000047 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000048 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000049 break;
50
Evan Chengbf407072010-09-10 01:29:16 +000051 const InstrStage *IS = ItinData->beginStage(idx);
52 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000053 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000054 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055 for (; IS != E; ++IS) {
56 unsigned StageDepth = CurCycle + IS->getCycles();
57 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
58 CurCycle += IS->getNextCycles();
59 }
David Goodwin6021b4d2009-08-10 15:55:25 +000060
Andrew Trick00067fb2010-12-08 20:04:29 +000061 // Find the next power-of-2 >= ItinDepth
62 while (ItinDepth > ScoreboardDepth) {
63 ScoreboardDepth *= 2;
Andrew Tricked7c96d2012-06-05 03:44:32 +000064 // Don't set MaxLookAhead until we find at least one nonzero stage.
65 // This way, an itinerary with no stages has MaxLookAhead==0, which
66 // completely bypasses the scoreboard hazard logic.
67 MaxLookAhead = ScoreboardDepth;
Andrew Trick00067fb2010-12-08 20:04:29 +000068 }
David Goodwin6021b4d2009-08-10 15:55:25 +000069 }
70 }
71
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000072 ReservedScoreboard.reset(ScoreboardDepth);
73 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000074
Andrew Trick87255e32012-07-07 04:00:00 +000075 // If MaxLookAhead is not set above, then we are not enabled.
Andrew Trick73d77362012-06-05 03:44:40 +000076 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000077 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000078 else {
Andrew Trick87255e32012-07-07 04:00:00 +000079 // A nonempty itinerary must have a SchedModel.
80 IssueWidth = ItinData->SchedModel->IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000081 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
82 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000083 }
David Goodwin6021b4d2009-08-10 15:55:25 +000084}
85
Andrew Trick00067fb2010-12-08 20:04:29 +000086void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000087 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000088 RequiredScoreboard.reset();
89 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000090}
91
Manman Ren19f49ac2012-09-11 22:23:19 +000092#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick00067fb2010-12-08 20:04:29 +000093void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000094 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000095
96 unsigned last = Depth - 1;
97 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000098 last--;
99
100 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +0000101 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +0000102 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +0000103 for (int j = 31; j >= 0; j--)
104 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +0000105 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +0000106 }
107}
Manman Ren742534c2012-09-06 19:06:06 +0000108#endif
David Goodwin6021b4d2009-08-10 15:55:25 +0000109
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000110bool ScoreboardHazardRecognizer::atIssueLimit() const {
111 if (IssueWidth == 0)
112 return false;
113
114 return IssueCount == IssueWidth;
115}
116
Evan Chengf128bdc2010-06-16 07:35:02 +0000117ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000118ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000119 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000120 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000121
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000122 // Note that stalls will be negative for bottom-up scheduling.
123 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000124
125 // Use the itinerary for the underlying instruction to check for
126 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000127
Evan Cheng6cc775f2011-06-28 19:10:37 +0000128 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
129 if (MCID == NULL) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000130 // Don't check hazards for non-machineinstr Nodes.
131 return NoHazard;
132 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000133 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000134 for (const InstrStage *IS = ItinData->beginStage(idx),
135 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000136 // We must find one of the stage's units free for every cycle the
137 // stage is occupied. FIXME it would be more accurate to find the
138 // same unit free in all the cycles.
139 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000140 int StageCycle = cycle + (int)i;
141 if (StageCycle < 0)
142 continue;
143
144 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
145 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
146 "Scoreboard depth exceeded!");
147 // This stage was stalled beyond pipeline depth, so cannot conflict.
148 break;
149 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000150
Hal Finkel8db55472012-06-22 20:27:13 +0000151 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000152 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000153 case InstrStage::Required:
154 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000155 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000156 // FALLTHROUGH
157 case InstrStage::Reserved:
158 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000159 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000160 break;
161 }
162
David Goodwin74b79562009-09-22 16:47:52 +0000163 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000164 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000165 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000166 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000167 return Hazard;
168 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000169 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000170
David Goodwin74b79562009-09-22 16:47:52 +0000171 // Advance the cycle to the next stage.
172 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000173 }
174
175 return NoHazard;
176}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000177
Andrew Trick00067fb2010-12-08 20:04:29 +0000178void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000179 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000180 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000181
David Goodwin74b79562009-09-22 16:47:52 +0000182 // Use the itinerary for the underlying instruction to reserve FU's
183 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000184 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
185 assert(MCID && "The scheduler must filter non-machineinstrs");
186 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000187 return;
188
189 ++IssueCount;
190
191 unsigned cycle = 0;
192
Evan Cheng6cc775f2011-06-28 19:10:37 +0000193 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000194 for (const InstrStage *IS = ItinData->beginStage(idx),
195 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000196 // We must reserve one of the stage's units for every cycle the
197 // stage is occupied. FIXME it would be more accurate to reserve
198 // the same unit free in all the cycles.
199 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000200 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000201 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000202
Hal Finkel8db55472012-06-22 20:27:13 +0000203 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000204 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000205 case InstrStage::Required:
206 // Required FUs conflict with both reserved and required ones
207 freeUnits &= ~ReservedScoreboard[cycle + i];
208 // FALLTHROUGH
209 case InstrStage::Reserved:
210 // Reserved FUs can conflict only with required ones.
211 freeUnits &= ~RequiredScoreboard[cycle + i];
212 break;
213 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000214
David Goodwin74b79562009-09-22 16:47:52 +0000215 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000216 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000217 do {
218 freeUnit = freeUnits;
219 freeUnits = freeUnit & (freeUnit - 1);
220 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000221
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000222 if (IS->getReservationKind() == InstrStage::Required)
223 RequiredScoreboard[cycle + i] |= freeUnit;
224 else
225 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000226 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000227
David Goodwin74b79562009-09-22 16:47:52 +0000228 // Advance the cycle to the next stage.
229 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000230 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000231
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000232 DEBUG(ReservedScoreboard.dump());
233 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000234}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000235
Andrew Trick00067fb2010-12-08 20:04:29 +0000236void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000237 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000238 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
239 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000240}
Andrew Trick00067fb2010-12-08 20:04:29 +0000241
242void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000243 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000244 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
245 ReservedScoreboard.recede();
246 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
247 RequiredScoreboard.recede();
248}