blob: 38833a4165a3c82d246f6cdb9e2d9d04a2aa0342 [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 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"
Evan Cheng8264e272011-06-29 01:14:12 +000018#include "llvm/MC/MCInstrItineraries.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000021#include "llvm/Support/raw_ostream.h"
Andrew Trick47ff14b2011-01-21 05:51:33 +000022#include "llvm/Target/TargetInstrInfo.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000023
Bill Wendlinga2c45942009-08-22 20:08:44 +000024using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000025
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026#define DEBUG_TYPE ::llvm::ScoreboardHazardRecognizer::DebugType
27
Andrew Trick10ffc2b2010-12-24 05:03:26 +000028#ifndef NDEBUG
29const char *ScoreboardHazardRecognizer::DebugType = "";
30#endif
31
Andrew Trick00067fb2010-12-08 20:04:29 +000032ScoreboardHazardRecognizer::
Andrew Trick10ffc2b2010-12-24 05:03:26 +000033ScoreboardHazardRecognizer(const InstrItineraryData *II,
34 const ScheduleDAG *SchedDAG,
35 const char *ParentDebugType) :
36 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
37 IssueCount(0) {
38
39#ifndef NDEBUG
40 DebugType = ParentDebugType;
41#endif
42
Andrew Tricked7c96d2012-06-05 03:44:32 +000043 // Determine the maximum depth of any itinerary. This determines the depth of
44 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
45 // avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000046 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000047 if (ItinData && !ItinData->isEmpty()) {
David Goodwin6021b4d2009-08-10 15:55:25 +000048 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000049 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000050 break;
51
Evan Chengbf407072010-09-10 01:29:16 +000052 const InstrStage *IS = ItinData->beginStage(idx);
53 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000054 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000055 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000056 for (; IS != E; ++IS) {
57 unsigned StageDepth = CurCycle + IS->getCycles();
58 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
59 CurCycle += IS->getNextCycles();
60 }
David Goodwin6021b4d2009-08-10 15:55:25 +000061
Andrew Trick00067fb2010-12-08 20:04:29 +000062 // Find the next power-of-2 >= ItinDepth
63 while (ItinDepth > ScoreboardDepth) {
64 ScoreboardDepth *= 2;
Andrew Tricked7c96d2012-06-05 03:44:32 +000065 // Don't set MaxLookAhead until we find at least one nonzero stage.
66 // This way, an itinerary with no stages has MaxLookAhead==0, which
67 // completely bypasses the scoreboard hazard logic.
68 MaxLookAhead = ScoreboardDepth;
Andrew Trick00067fb2010-12-08 20:04:29 +000069 }
David Goodwin6021b4d2009-08-10 15:55:25 +000070 }
71 }
72
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000073 ReservedScoreboard.reset(ScoreboardDepth);
74 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000075
Andrew Trick87255e32012-07-07 04:00:00 +000076 // If MaxLookAhead is not set above, then we are not enabled.
Andrew Trick73d77362012-06-05 03:44:40 +000077 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000078 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000079 else {
Andrew Trick87255e32012-07-07 04:00:00 +000080 // A nonempty itinerary must have a SchedModel.
Pete Cooper11759452014-09-02 17:43:54 +000081 IssueWidth = ItinData->SchedModel.IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000082 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
83 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000084 }
David Goodwin6021b4d2009-08-10 15:55:25 +000085}
86
Andrew Trick00067fb2010-12-08 20:04:29 +000087void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000088 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000089 RequiredScoreboard.reset();
90 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000091}
92
Manman Ren19f49ac2012-09-11 22:23:19 +000093#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Andrew Trick00067fb2010-12-08 20:04:29 +000094void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000095 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000096
97 unsigned last = Depth - 1;
98 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000099 last--;
100
101 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +0000102 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +0000103 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +0000104 for (int j = 31; j >= 0; j--)
105 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +0000106 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +0000107 }
108}
Manman Ren742534c2012-09-06 19:06:06 +0000109#endif
David Goodwin6021b4d2009-08-10 15:55:25 +0000110
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000111bool ScoreboardHazardRecognizer::atIssueLimit() const {
112 if (IssueWidth == 0)
113 return false;
114
115 return IssueCount == IssueWidth;
116}
117
Evan Chengf128bdc2010-06-16 07:35:02 +0000118ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000119ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000120 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000121 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000122
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000123 // Note that stalls will be negative for bottom-up scheduling.
124 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000125
126 // Use the itinerary for the underlying instruction to check for
127 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000128
Evan Cheng6cc775f2011-06-28 19:10:37 +0000129 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
Craig Topperc0196b12014-04-14 00:51:57 +0000130 if (!MCID) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000131 // Don't check hazards for non-machineinstr Nodes.
132 return NoHazard;
133 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000134 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000135 for (const InstrStage *IS = ItinData->beginStage(idx),
136 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000137 // We must find one of the stage's units free for every cycle the
138 // stage is occupied. FIXME it would be more accurate to find the
139 // same unit free in all the cycles.
140 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000141 int StageCycle = cycle + (int)i;
142 if (StageCycle < 0)
143 continue;
144
145 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
146 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
147 "Scoreboard depth exceeded!");
148 // This stage was stalled beyond pipeline depth, so cannot conflict.
149 break;
150 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000151
Hal Finkel8db55472012-06-22 20:27:13 +0000152 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000153 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000154 case InstrStage::Required:
155 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000156 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000157 // FALLTHROUGH
158 case InstrStage::Reserved:
159 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000160 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000161 break;
162 }
163
David Goodwin74b79562009-09-22 16:47:52 +0000164 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000165 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000166 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000167 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000168 return Hazard;
169 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000170 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000171
David Goodwin74b79562009-09-22 16:47:52 +0000172 // Advance the cycle to the next stage.
173 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000174 }
175
176 return NoHazard;
177}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000178
Andrew Trick00067fb2010-12-08 20:04:29 +0000179void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000180 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000181 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000182
David Goodwin74b79562009-09-22 16:47:52 +0000183 // Use the itinerary for the underlying instruction to reserve FU's
184 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000185 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
186 assert(MCID && "The scheduler must filter non-machineinstrs");
187 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000188 return;
189
190 ++IssueCount;
191
192 unsigned cycle = 0;
193
Evan Cheng6cc775f2011-06-28 19:10:37 +0000194 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000195 for (const InstrStage *IS = ItinData->beginStage(idx),
196 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000197 // We must reserve one of the stage's units for every cycle the
198 // stage is occupied. FIXME it would be more accurate to reserve
199 // the same unit free in all the cycles.
200 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000201 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000202 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000203
Hal Finkel8db55472012-06-22 20:27:13 +0000204 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000205 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000206 case InstrStage::Required:
207 // Required FUs conflict with both reserved and required ones
208 freeUnits &= ~ReservedScoreboard[cycle + i];
209 // FALLTHROUGH
210 case InstrStage::Reserved:
211 // Reserved FUs can conflict only with required ones.
212 freeUnits &= ~RequiredScoreboard[cycle + i];
213 break;
214 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000215
David Goodwin74b79562009-09-22 16:47:52 +0000216 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000217 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000218 do {
219 freeUnit = freeUnits;
220 freeUnits = freeUnit & (freeUnit - 1);
221 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000222
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000223 if (IS->getReservationKind() == InstrStage::Required)
224 RequiredScoreboard[cycle + i] |= freeUnit;
225 else
226 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000227 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000228
David Goodwin74b79562009-09-22 16:47:52 +0000229 // Advance the cycle to the next stage.
230 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000231 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000232
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000233 DEBUG(ReservedScoreboard.dump());
234 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000235}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000236
Andrew Trick00067fb2010-12-08 20:04:29 +0000237void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000238 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000239 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
240 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000241}
Andrew Trick00067fb2010-12-08 20:04:29 +0000242
243void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000244 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000245 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
246 ReservedScoreboard.recede();
247 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
248 RequiredScoreboard.recede();
249}