Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 1 | //===----- ScoreboardHazardRecognizer.cpp - Scheduler Support -------------===// |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 2 | // |
| 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 Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 10 | // 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 Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE ::llvm::ScoreboardHazardRecognizer::DebugType |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ScoreboardHazardRecognizer.h" |
Evan Cheng | 078f4ce | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
Evan Cheng | 8264e27 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInstrItineraries.h" |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | f20236a | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 47ff14b | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetInstrInfo.h" |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 24 | |
Bill Wendling | a2c4594 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 25 | using namespace llvm; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 26 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 27 | #ifndef NDEBUG |
| 28 | const char *ScoreboardHazardRecognizer::DebugType = ""; |
| 29 | #endif |
| 30 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 31 | ScoreboardHazardRecognizer:: |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 32 | ScoreboardHazardRecognizer(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 Trick | ed7c96d | 2012-06-05 03:44:32 +0000 | [diff] [blame^] | 42 | // 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 Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 45 | unsigned ScoreboardDepth = 1; |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 46 | if (ItinData && !ItinData->isEmpty()) { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 47 | IssueWidth = ItinData->IssueWidth; |
| 48 | |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 49 | for (unsigned idx = 0; ; ++idx) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 50 | if (ItinData->isEndMarker(idx)) |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 51 | break; |
| 52 | |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 53 | const InstrStage *IS = ItinData->beginStage(idx); |
| 54 | const InstrStage *E = ItinData->endStage(idx); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 55 | unsigned CurCycle = 0; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | unsigned ItinDepth = 0; |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 57 | for (; IS != E; ++IS) { |
| 58 | unsigned StageDepth = CurCycle + IS->getCycles(); |
| 59 | if (ItinDepth < StageDepth) ItinDepth = StageDepth; |
| 60 | CurCycle += IS->getNextCycles(); |
| 61 | } |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 62 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 63 | // Find the next power-of-2 >= ItinDepth |
| 64 | while (ItinDepth > ScoreboardDepth) { |
| 65 | ScoreboardDepth *= 2; |
Andrew Trick | ed7c96d | 2012-06-05 03:44:32 +0000 | [diff] [blame^] | 66 | // Don't set MaxLookAhead until we find at least one nonzero stage. |
| 67 | // This way, an itinerary with no stages has MaxLookAhead==0, which |
| 68 | // completely bypasses the scoreboard hazard logic. |
| 69 | MaxLookAhead = ScoreboardDepth; |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 70 | } |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 74 | ReservedScoreboard.reset(ScoreboardDepth); |
| 75 | RequiredScoreboard.reset(ScoreboardDepth); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 76 | |
Andrew Trick | ed7c96d | 2012-06-05 03:44:32 +0000 | [diff] [blame^] | 77 | if (!MaxLookAhead) |
| 78 | DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n"); |
| 79 | else |
| 80 | DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = " |
| 81 | << ScoreboardDepth << '\n'); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 84 | void ScoreboardHazardRecognizer::Reset() { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 85 | IssueCount = 0; |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 86 | RequiredScoreboard.reset(); |
| 87 | ReservedScoreboard.reset(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 90 | void ScoreboardHazardRecognizer::Scoreboard::dump() const { |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 91 | dbgs() << "Scoreboard:\n"; |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 92 | |
| 93 | unsigned last = Depth - 1; |
| 94 | while ((last > 0) && ((*this)[last] == 0)) |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 95 | last--; |
| 96 | |
| 97 | for (unsigned i = 0; i <= last; i++) { |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 98 | unsigned FUs = (*this)[i]; |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 99 | dbgs() << "\t"; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 100 | for (int j = 31; j >= 0; j--) |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 101 | dbgs() << ((FUs & (1 << j)) ? '1' : '0'); |
| 102 | dbgs() << '\n'; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 106 | bool ScoreboardHazardRecognizer::atIssueLimit() const { |
| 107 | if (IssueWidth == 0) |
| 108 | return false; |
| 109 | |
| 110 | return IssueCount == IssueWidth; |
| 111 | } |
| 112 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 113 | ScheduleHazardRecognizer::HazardType |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 114 | ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 115 | if (!ItinData || ItinData->isEmpty()) |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 116 | return NoHazard; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 117 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 118 | // Note that stalls will be negative for bottom-up scheduling. |
| 119 | int cycle = Stalls; |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 120 | |
| 121 | // Use the itinerary for the underlying instruction to check for |
| 122 | // free FU's in the scoreboard at the appropriate future cycles. |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 123 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 124 | const MCInstrDesc *MCID = DAG->getInstrDesc(SU); |
| 125 | if (MCID == NULL) { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 126 | // Don't check hazards for non-machineinstr Nodes. |
| 127 | return NoHazard; |
| 128 | } |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 129 | unsigned idx = MCID->getSchedClass(); |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 130 | for (const InstrStage *IS = ItinData->beginStage(idx), |
| 131 | *E = ItinData->endStage(idx); IS != E; ++IS) { |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 132 | // We must find one of the stage's units free for every cycle the |
| 133 | // stage is occupied. FIXME it would be more accurate to find the |
| 134 | // same unit free in all the cycles. |
| 135 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 136 | int StageCycle = cycle + (int)i; |
| 137 | if (StageCycle < 0) |
| 138 | continue; |
| 139 | |
| 140 | if (StageCycle >= (int)RequiredScoreboard.getDepth()) { |
| 141 | assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() && |
| 142 | "Scoreboard depth exceeded!"); |
| 143 | // This stage was stalled beyond pipeline depth, so cannot conflict. |
| 144 | break; |
| 145 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 146 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 147 | unsigned freeUnits = IS->getUnits(); |
| 148 | switch (IS->getReservationKind()) { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 149 | case InstrStage::Required: |
| 150 | // Required FUs conflict with both reserved and required ones |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 151 | freeUnits &= ~ReservedScoreboard[StageCycle]; |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 152 | // FALLTHROUGH |
| 153 | case InstrStage::Reserved: |
| 154 | // Reserved FUs can conflict only with required ones. |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 155 | freeUnits &= ~RequiredScoreboard[StageCycle]; |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 159 | if (!freeUnits) { |
Benjamin Kramer | 484f424 | 2012-05-26 11:37:37 +0000 | [diff] [blame] | 160 | DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", "); |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 161 | DEBUG(dbgs() << "SU(" << SU->NodeNum << "): "); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 162 | DEBUG(DAG->dumpNode(SU)); |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 163 | return Hazard; |
| 164 | } |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 165 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 166 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 167 | // Advance the cycle to the next stage. |
| 168 | cycle += IS->getNextCycles(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return NoHazard; |
| 172 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 173 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 174 | void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 175 | if (!ItinData || ItinData->isEmpty()) |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 176 | return; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 177 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 178 | // Use the itinerary for the underlying instruction to reserve FU's |
| 179 | // in the scoreboard at the appropriate future cycles. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 180 | const MCInstrDesc *MCID = DAG->getInstrDesc(SU); |
| 181 | assert(MCID && "The scheduler must filter non-machineinstrs"); |
| 182 | if (DAG->TII->isZeroCost(MCID->Opcode)) |
Andrew Trick | 47ff14b | 2011-01-21 05:51:33 +0000 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | ++IssueCount; |
| 186 | |
| 187 | unsigned cycle = 0; |
| 188 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 189 | unsigned idx = MCID->getSchedClass(); |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 190 | for (const InstrStage *IS = ItinData->beginStage(idx), |
| 191 | *E = ItinData->endStage(idx); IS != E; ++IS) { |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 192 | // We must reserve one of the stage's units for every cycle the |
| 193 | // stage is occupied. FIXME it would be more accurate to reserve |
| 194 | // the same unit free in all the cycles. |
| 195 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 196 | assert(((cycle + i) < RequiredScoreboard.getDepth()) && |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 197 | "Scoreboard depth exceeded!"); |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 198 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 199 | unsigned freeUnits = IS->getUnits(); |
| 200 | switch (IS->getReservationKind()) { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 201 | case InstrStage::Required: |
| 202 | // Required FUs conflict with both reserved and required ones |
| 203 | freeUnits &= ~ReservedScoreboard[cycle + i]; |
| 204 | // FALLTHROUGH |
| 205 | case InstrStage::Reserved: |
| 206 | // Reserved FUs can conflict only with required ones. |
| 207 | freeUnits &= ~RequiredScoreboard[cycle + i]; |
| 208 | break; |
| 209 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 210 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 211 | // reduce to a single unit |
| 212 | unsigned freeUnit = 0; |
| 213 | do { |
| 214 | freeUnit = freeUnits; |
| 215 | freeUnits = freeUnit & (freeUnit - 1); |
| 216 | } while (freeUnits); |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 217 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 218 | if (IS->getReservationKind() == InstrStage::Required) |
| 219 | RequiredScoreboard[cycle + i] |= freeUnit; |
| 220 | else |
| 221 | ReservedScoreboard[cycle + i] |= freeUnit; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 222 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 223 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 224 | // Advance the cycle to the next stage. |
| 225 | cycle += IS->getNextCycles(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 226 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 227 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 228 | DEBUG(ReservedScoreboard.dump()); |
| 229 | DEBUG(RequiredScoreboard.dump()); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 230 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 231 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 232 | void ScoreboardHazardRecognizer::AdvanceCycle() { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 233 | IssueCount = 0; |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 234 | ReservedScoreboard[0] = 0; ReservedScoreboard.advance(); |
| 235 | RequiredScoreboard[0] = 0; RequiredScoreboard.advance(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 236 | } |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 237 | |
| 238 | void ScoreboardHazardRecognizer::RecedeCycle() { |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 239 | IssueCount = 0; |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 240 | ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0; |
| 241 | ReservedScoreboard.recede(); |
| 242 | RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0; |
| 243 | RequiredScoreboard.recede(); |
| 244 | } |