blob: af5f2892c2f07f2f1a56321e96688b4037688308 [file] [log] [blame]
David Goodwind94a4e52009-08-10 15:55:25 +00001//===----- 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 Gohmanf451cb82010-02-10 16:03:48 +000010// This implements a hazard recognizer using the instructions itineraries
David Goodwind94a4e52009-08-10 15:55:25 +000011// defined for the current target.
12//
13//===----------------------------------------------------------------------===//
14
David Goodwin4de099d2009-11-03 20:57:50 +000015#define DEBUG_TYPE "post-RA-sched"
David Goodwind94a4e52009-08-10 15:55:25 +000016#include "ExactHazardRecognizer.h"
17#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000020#include "llvm/Support/raw_ostream.h"
David Goodwind94a4e52009-08-10 15:55:25 +000021#include "llvm/Target/TargetInstrItineraries.h"
22
Bill Wendlingfb98a432009-08-22 20:08:44 +000023using namespace llvm;
David Goodwind94a4e52009-08-10 15:55:25 +000024
Evan Cheng449c25b2009-10-16 05:18:39 +000025ExactHazardRecognizer::
26ExactHazardRecognizer(const InstrItineraryData &LItinData) :
David Goodwind94a4e52009-08-10 15:55:25 +000027 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 Korobeynikov12989482010-04-07 18:19:24 +000032 unsigned ScoreboardDepth = 1;
David Goodwind94a4e52009-08-10 15:55:25 +000033 if (!ItinData.isEmpty()) {
34 for (unsigned idx = 0; ; ++idx) {
David Goodwin1f528952009-09-24 20:22:50 +000035 if (ItinData.isEndMarker(idx))
David Goodwind94a4e52009-08-10 15:55:25 +000036 break;
37
David Goodwin1f528952009-09-24 20:22:50 +000038 const InstrStage *IS = ItinData.beginStage(idx);
39 const InstrStage *E = ItinData.endStage(idx);
David Goodwind94a4e52009-08-10 15:55:25 +000040 unsigned ItinDepth = 0;
41 for (; IS != E; ++IS)
David Goodwin1a8f36e2009-08-12 18:31:53 +000042 ItinDepth += IS->getCycles();
David Goodwind94a4e52009-08-10 15:55:25 +000043
44 ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
45 }
46 }
47
Anton Korobeynikov96085a32010-04-07 18:19:32 +000048 ReservedScoreboard.reset(ScoreboardDepth);
49 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwind94a4e52009-08-10 15:55:25 +000050
David Greene73242dd2010-01-04 21:26:07 +000051 DEBUG(dbgs() << "Using exact hazard recognizer: ScoreboardDepth = "
Bill Wendlingfb98a432009-08-22 20:08:44 +000052 << ScoreboardDepth << '\n');
David Goodwind94a4e52009-08-10 15:55:25 +000053}
54
David Goodwind94a4e52009-08-10 15:55:25 +000055void ExactHazardRecognizer::Reset() {
Anton Korobeynikov96085a32010-04-07 18:19:32 +000056 RequiredScoreboard.reset();
57 ReservedScoreboard.reset();
David Goodwind94a4e52009-08-10 15:55:25 +000058}
59
Anton Korobeynikov12989482010-04-07 18:19:24 +000060void ExactHazardRecognizer::ScoreBoard::dump() const {
David Greene73242dd2010-01-04 21:26:07 +000061 dbgs() << "Scoreboard:\n";
Anton Korobeynikov12989482010-04-07 18:19:24 +000062
63 unsigned last = Depth - 1;
64 while ((last > 0) && ((*this)[last] == 0))
David Goodwind94a4e52009-08-10 15:55:25 +000065 last--;
66
67 for (unsigned i = 0; i <= last; i++) {
Anton Korobeynikov12989482010-04-07 18:19:24 +000068 unsigned FUs = (*this)[i];
David Greene73242dd2010-01-04 21:26:07 +000069 dbgs() << "\t";
David Goodwind94a4e52009-08-10 15:55:25 +000070 for (int j = 31; j >= 0; j--)
David Greene73242dd2010-01-04 21:26:07 +000071 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
72 dbgs() << '\n';
David Goodwind94a4e52009-08-10 15:55:25 +000073 }
74}
75
76ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +000077 if (ItinData.isEmpty())
78 return NoHazard;
David Goodwind94a4e52009-08-10 15:55:25 +000079
David Goodwin047ae2f2009-09-22 16:47:52 +000080 unsigned cycle = 0;
81
82 // Use the itinerary for the underlying instruction to check for
83 // free FU's in the scoreboard at the appropriate future cycles.
84 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
85 for (const InstrStage *IS = ItinData.beginStage(idx),
86 *E = ItinData.endStage(idx); IS != E; ++IS) {
87 // We must find one of the stage's units free for every cycle the
88 // stage is occupied. FIXME it would be more accurate to find the
89 // same unit free in all the cycles.
90 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov96085a32010-04-07 18:19:32 +000091 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin047ae2f2009-09-22 16:47:52 +000092 "Scoreboard depth exceeded!");
Anton Korobeynikov12989482010-04-07 18:19:24 +000093
Anton Korobeynikov96085a32010-04-07 18:19:32 +000094 unsigned freeUnits = IS->getUnits();
95 switch (IS->getReservationKind()) {
96 default:
97 assert(0 && "Invalid FU reservation");
98 case InstrStage::Required:
99 // Required FUs conflict with both reserved and required ones
100 freeUnits &= ~ReservedScoreboard[cycle + i];
101 // FALLTHROUGH
102 case InstrStage::Reserved:
103 // Reserved FUs can conflict only with required ones.
104 freeUnits &= ~RequiredScoreboard[cycle + i];
105 break;
106 }
107
David Goodwin047ae2f2009-09-22 16:47:52 +0000108 if (!freeUnits) {
David Greene73242dd2010-01-04 21:26:07 +0000109 DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
110 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
David Goodwin047ae2f2009-09-22 16:47:52 +0000111 DEBUG(SU->getInstr()->dump());
112 return Hazard;
113 }
David Goodwind94a4e52009-08-10 15:55:25 +0000114 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000115
David Goodwin047ae2f2009-09-22 16:47:52 +0000116 // Advance the cycle to the next stage.
117 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000118 }
119
120 return NoHazard;
121}
Anton Korobeynikov12989482010-04-07 18:19:24 +0000122
David Goodwind94a4e52009-08-10 15:55:25 +0000123void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +0000124 if (ItinData.isEmpty())
125 return;
David Goodwind94a4e52009-08-10 15:55:25 +0000126
David Goodwin047ae2f2009-09-22 16:47:52 +0000127 unsigned cycle = 0;
David Goodwind94a4e52009-08-10 15:55:25 +0000128
David Goodwin047ae2f2009-09-22 16:47:52 +0000129 // Use the itinerary for the underlying instruction to reserve FU's
130 // in the scoreboard at the appropriate future cycles.
131 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000132 for (const InstrStage *IS = ItinData.beginStage(idx),
David Goodwin047ae2f2009-09-22 16:47:52 +0000133 *E = ItinData.endStage(idx); IS != E; ++IS) {
134 // We must reserve one of the stage's units for every cycle the
135 // stage is occupied. FIXME it would be more accurate to reserve
136 // the same unit free in all the cycles.
137 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000138 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin047ae2f2009-09-22 16:47:52 +0000139 "Scoreboard depth exceeded!");
Anton Korobeynikov12989482010-04-07 18:19:24 +0000140
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000141 unsigned freeUnits = IS->getUnits();
142 switch (IS->getReservationKind()) {
143 default:
144 assert(0 && "Invalid FU reservation");
145 case InstrStage::Required:
146 // Required FUs conflict with both reserved and required ones
147 freeUnits &= ~ReservedScoreboard[cycle + i];
148 // FALLTHROUGH
149 case InstrStage::Reserved:
150 // Reserved FUs can conflict only with required ones.
151 freeUnits &= ~RequiredScoreboard[cycle + i];
152 break;
153 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000154
David Goodwin047ae2f2009-09-22 16:47:52 +0000155 // reduce to a single unit
156 unsigned freeUnit = 0;
157 do {
158 freeUnit = freeUnits;
159 freeUnits = freeUnit & (freeUnit - 1);
160 } while (freeUnits);
Anton Korobeynikov12989482010-04-07 18:19:24 +0000161
David Goodwin047ae2f2009-09-22 16:47:52 +0000162 assert(freeUnit && "No function unit available!");
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000163 if (IS->getReservationKind() == InstrStage::Required)
164 RequiredScoreboard[cycle + i] |= freeUnit;
165 else
166 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwind94a4e52009-08-10 15:55:25 +0000167 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000168
David Goodwin047ae2f2009-09-22 16:47:52 +0000169 // Advance the cycle to the next stage.
170 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000171 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000172
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000173 DEBUG(ReservedScoreboard.dump());
174 DEBUG(RequiredScoreboard.dump());
David Goodwind94a4e52009-08-10 15:55:25 +0000175}
Anton Korobeynikov12989482010-04-07 18:19:24 +0000176
David Goodwind94a4e52009-08-10 15:55:25 +0000177void ExactHazardRecognizer::AdvanceCycle() {
Anton Korobeynikov96085a32010-04-07 18:19:32 +0000178 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
179 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwind94a4e52009-08-10 15:55:25 +0000180}