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