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 | // |
| 10 | // This implements a a hazard recognizer using the instructions itineraries |
| 11 | // defined for the current target. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "exact-hazards" |
| 16 | #include "ExactHazardRecognizer.h" |
| 17 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Target/TargetInstrItineraries.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | ExactHazardRecognizer::ExactHazardRecognizer(const InstrItineraryData &LItinData) : |
| 25 | ScheduleHazardRecognizer(), ItinData(LItinData) |
| 26 | { |
| 27 | // Determine the maximum depth of any itinerary. This determines the |
| 28 | // depth of the scoreboard. We always make the scoreboard at least 1 |
| 29 | // cycle deep to avoid dealing with the boundary condition. |
| 30 | ScoreboardDepth = 1; |
| 31 | if (!ItinData.isEmpty()) { |
| 32 | for (unsigned idx = 0; ; ++idx) { |
| 33 | // If the begin stage of an itinerary has 0 cycles and units, |
| 34 | // then we have reached the end of the itineraries. |
| 35 | const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx); |
| 36 | if ((IS->Cycles == 0) && (IS->Units == 0)) |
| 37 | break; |
| 38 | |
| 39 | unsigned ItinDepth = 0; |
| 40 | for (; IS != E; ++IS) |
| 41 | ItinDepth += IS->Cycles; |
| 42 | |
| 43 | ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | Scoreboard = new unsigned[ScoreboardDepth]; |
| 48 | ScoreboardHead = 0; |
| 49 | |
| 50 | DOUT << "Using exact hazard recognizer: ScoreboardDepth = " |
| 51 | << ScoreboardDepth << '\n'; |
| 52 | } |
| 53 | |
| 54 | ExactHazardRecognizer::~ExactHazardRecognizer() { |
| 55 | delete Scoreboard; |
| 56 | } |
| 57 | |
| 58 | void ExactHazardRecognizer::Reset() { |
| 59 | memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned)); |
| 60 | ScoreboardHead = 0; |
| 61 | } |
| 62 | |
| 63 | unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) { |
| 64 | return (ScoreboardHead + offset) % ScoreboardDepth; |
| 65 | } |
| 66 | |
| 67 | void ExactHazardRecognizer::dumpScoreboard() { |
| 68 | DOUT << "Scoreboard:\n"; |
| 69 | |
| 70 | unsigned last = ScoreboardDepth - 1; |
| 71 | while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0)) |
| 72 | last--; |
| 73 | |
| 74 | for (unsigned i = 0; i <= last; i++) { |
| 75 | unsigned FUs = Scoreboard[getFutureIndex(i)]; |
| 76 | DOUT << "\t"; |
| 77 | for (int j = 31; j >= 0; j--) |
| 78 | DOUT << ((FUs & (1 << j)) ? '1' : '0'); |
| 79 | DOUT << '\n'; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) { |
| 84 | unsigned cycle = 0; |
| 85 | |
| 86 | // Use the itinerary for the underlying instruction to check for |
| 87 | // free FU's in the scoreboard at the appropriate future cycles. |
| 88 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 89 | for (const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx); |
| 90 | IS != E; ++IS) { |
| 91 | // We must find one of the stage's units free for every cycle the |
| 92 | // stage is occupied. |
| 93 | for (unsigned int i = 0; i < IS->Cycles; ++i) { |
| 94 | assert((cycle < ScoreboardDepth) && "Scoreboard depth exceeded!"); |
| 95 | |
| 96 | unsigned index = getFutureIndex(cycle); |
| 97 | unsigned freeUnits = IS->Units & ~Scoreboard[index]; |
| 98 | if (!freeUnits) { |
| 99 | DOUT << "*** Hazard in cycle " << cycle << ", "; |
| 100 | DOUT << "SU(" << SU->NodeNum << "): "; |
| 101 | DEBUG(SU->getInstr()->dump()); |
| 102 | return Hazard; |
| 103 | } |
| 104 | |
| 105 | ++cycle; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return NoHazard; |
| 110 | } |
| 111 | |
| 112 | void ExactHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 113 | unsigned cycle = 0; |
| 114 | |
| 115 | // Use the itinerary for the underlying instruction to reserve FU's |
| 116 | // in the scoreboard at the appropriate future cycles. |
| 117 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 118 | for (const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx); |
| 119 | IS != E; ++IS) { |
| 120 | // We must reserve one of the stage's units for every cycle the |
| 121 | // stage is occupied. |
| 122 | for (unsigned int i = 0; i < IS->Cycles; ++i) { |
| 123 | assert((cycle < ScoreboardDepth) && "Scoreboard depth exceeded!"); |
| 124 | |
| 125 | unsigned index = getFutureIndex(cycle); |
| 126 | unsigned freeUnits = IS->Units & ~Scoreboard[index]; |
| 127 | |
| 128 | // reduce to a single unit |
| 129 | unsigned freeUnit = 0; |
| 130 | do { |
| 131 | freeUnit = freeUnits; |
| 132 | freeUnits = freeUnit & (freeUnit - 1); |
| 133 | } while (freeUnits); |
| 134 | |
| 135 | assert(freeUnit && "No function unit available!"); |
| 136 | Scoreboard[index] |= freeUnit; |
| 137 | ++cycle; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | DEBUG(dumpScoreboard()); |
| 142 | } |
| 143 | |
| 144 | void ExactHazardRecognizer::AdvanceCycle() { |
| 145 | Scoreboard[ScoreboardHead] = 0; |
| 146 | ScoreboardHead = getFutureIndex(1); |
| 147 | } |
| 148 | |
| 149 | } /* namespace llvm */ |