David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1 | //===----- ExactHazardRecognizer.cpp - hazard recognizer -------- ---------===// |
| 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 | // |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 10 | // This implements a hazard recognizer using the instructions itineraries |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 11 | // defined for the current target. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "post-RA-sched" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 16 | #include "ExactHazardRecognizer.h" |
| 17 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrItineraries.h" |
| 22 | |
Bill Wendling | fb98a43 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 23 | using namespace llvm; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 24 | |
Evan Cheng | 449c25b | 2009-10-16 05:18:39 +0000 | [diff] [blame] | 25 | ExactHazardRecognizer:: |
| 26 | ExactHazardRecognizer(const InstrItineraryData &LItinData) : |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 27 | ScheduleHazardRecognizer(), ItinData(LItinData) |
| 28 | { |
| 29 | // Determine the maximum depth of any itinerary. This determines the |
| 30 | // depth of the scoreboard. We always make the scoreboard at least 1 |
| 31 | // cycle deep to avoid dealing with the boundary condition. |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 32 | unsigned ScoreboardDepth = 1; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 33 | if (!ItinData.isEmpty()) { |
| 34 | for (unsigned idx = 0; ; ++idx) { |
David Goodwin | 1f52895 | 2009-09-24 20:22:50 +0000 | [diff] [blame] | 35 | if (ItinData.isEndMarker(idx)) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 36 | break; |
| 37 | |
David Goodwin | 1f52895 | 2009-09-24 20:22:50 +0000 | [diff] [blame] | 38 | const InstrStage *IS = ItinData.beginStage(idx); |
| 39 | const InstrStage *E = ItinData.endStage(idx); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 40 | unsigned ItinDepth = 0; |
| 41 | for (; IS != E; ++IS) |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 42 | ItinDepth += IS->getCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 43 | |
| 44 | ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth); |
| 45 | } |
| 46 | } |
| 47 | |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 48 | Scoreboard.reset(ScoreboardDepth); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 49 | |
David Greene | 73242dd | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 50 | DEBUG(dbgs() << "Using exact hazard recognizer: ScoreboardDepth = " |
Bill Wendling | fb98a43 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 51 | << ScoreboardDepth << '\n'); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 54 | void ExactHazardRecognizer::Reset() { |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 55 | Scoreboard.reset(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 58 | void ExactHazardRecognizer::ScoreBoard::dump() const { |
David Greene | 73242dd | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 59 | dbgs() << "Scoreboard:\n"; |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 60 | |
| 61 | unsigned last = Depth - 1; |
| 62 | while ((last > 0) && ((*this)[last] == 0)) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 63 | last--; |
| 64 | |
| 65 | for (unsigned i = 0; i <= last; i++) { |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 66 | unsigned FUs = (*this)[i]; |
David Greene | 73242dd | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 67 | dbgs() << "\t"; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 68 | for (int j = 31; j >= 0; j--) |
David Greene | 73242dd | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 69 | dbgs() << ((FUs & (1 << j)) ? '1' : '0'); |
| 70 | dbgs() << '\n'; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) { |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 75 | if (ItinData.isEmpty()) |
| 76 | return NoHazard; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 77 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 78 | unsigned cycle = 0; |
| 79 | |
| 80 | // Use the itinerary for the underlying instruction to check for |
| 81 | // free FU's in the scoreboard at the appropriate future cycles. |
| 82 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 83 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 84 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
| 85 | // We must find one of the stage's units free for every cycle the |
| 86 | // stage is occupied. FIXME it would be more accurate to find the |
| 87 | // same unit free in all the cycles. |
| 88 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 89 | assert(((cycle + i) < Scoreboard.getDepth()) && |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 90 | "Scoreboard depth exceeded!"); |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 91 | |
| 92 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i]; |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 93 | if (!freeUnits) { |
David Greene | 73242dd | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 94 | DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", "); |
| 95 | DEBUG(dbgs() << "SU(" << SU->NodeNum << "): "); |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 96 | DEBUG(SU->getInstr()->dump()); |
| 97 | return Hazard; |
| 98 | } |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 99 | } |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 100 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 101 | // Advance the cycle to the next stage. |
| 102 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return NoHazard; |
| 106 | } |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 107 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 108 | void ExactHazardRecognizer::EmitInstruction(SUnit *SU) { |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 109 | if (ItinData.isEmpty()) |
| 110 | return; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 111 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 112 | unsigned cycle = 0; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 113 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 114 | // Use the itinerary for the underlying instruction to reserve FU's |
| 115 | // in the scoreboard at the appropriate future cycles. |
| 116 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 117 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 118 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
| 119 | // We must reserve one of the stage's units for every cycle the |
| 120 | // stage is occupied. FIXME it would be more accurate to reserve |
| 121 | // the same unit free in all the cycles. |
| 122 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 123 | assert(((cycle + i) < Scoreboard.getDepth()) && |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 124 | "Scoreboard depth exceeded!"); |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 125 | |
| 126 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i]; |
| 127 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 128 | // reduce to a single unit |
| 129 | unsigned freeUnit = 0; |
| 130 | do { |
| 131 | freeUnit = freeUnits; |
| 132 | freeUnits = freeUnit & (freeUnit - 1); |
| 133 | } while (freeUnits); |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 134 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 135 | assert(freeUnit && "No function unit available!"); |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 136 | Scoreboard[cycle + i] |= freeUnit; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 137 | } |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 138 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 139 | // Advance the cycle to the next stage. |
| 140 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 141 | } |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 142 | |
| 143 | DEBUG(Scoreboard.dump()); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 144 | } |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 145 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 146 | void ExactHazardRecognizer::AdvanceCycle() { |
Anton Korobeynikov | 1298948 | 2010-04-07 18:19:24 +0000 | [diff] [blame^] | 147 | Scoreboard[0] = 0; |
| 148 | Scoreboard.advance(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 149 | } |