blob: 266c74c9995ed542180b3a3d1e2cf1126e058d41 [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//
10// This implements a a hazard recognizer using the instructions itineraries
11// 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.
32 ScoreboardDepth = 1;
33 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
48 Scoreboard = new unsigned[ScoreboardDepth];
49 ScoreboardHead = 0;
50
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
55ExactHazardRecognizer::~ExactHazardRecognizer() {
Duncan Sands70327da2009-09-04 11:59:43 +000056 delete [] Scoreboard;
David Goodwind94a4e52009-08-10 15:55:25 +000057}
58
59void ExactHazardRecognizer::Reset() {
60 memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned));
61 ScoreboardHead = 0;
62}
63
64unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) {
65 return (ScoreboardHead + offset) % ScoreboardDepth;
66}
67
68void ExactHazardRecognizer::dumpScoreboard() {
David Greene73242dd2010-01-04 21:26:07 +000069 dbgs() << "Scoreboard:\n";
David Goodwind94a4e52009-08-10 15:55:25 +000070
71 unsigned last = ScoreboardDepth - 1;
72 while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0))
73 last--;
74
75 for (unsigned i = 0; i <= last; i++) {
76 unsigned FUs = Scoreboard[getFutureIndex(i)];
David Greene73242dd2010-01-04 21:26:07 +000077 dbgs() << "\t";
David Goodwind94a4e52009-08-10 15:55:25 +000078 for (int j = 31; j >= 0; j--)
David Greene73242dd2010-01-04 21:26:07 +000079 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
80 dbgs() << '\n';
David Goodwind94a4e52009-08-10 15:55:25 +000081 }
82}
83
84ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +000085 if (ItinData.isEmpty())
86 return NoHazard;
David Goodwind94a4e52009-08-10 15:55:25 +000087
David Goodwin047ae2f2009-09-22 16:47:52 +000088 unsigned cycle = 0;
89
90 // Use the itinerary for the underlying instruction to check for
91 // free FU's in the scoreboard at the appropriate future cycles.
92 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
93 for (const InstrStage *IS = ItinData.beginStage(idx),
94 *E = ItinData.endStage(idx); IS != E; ++IS) {
95 // We must find one of the stage's units free for every cycle the
96 // stage is occupied. FIXME it would be more accurate to find the
97 // same unit free in all the cycles.
98 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
99 assert(((cycle + i) < ScoreboardDepth) &&
100 "Scoreboard depth exceeded!");
David Goodwin4f7228f2009-09-03 22:48:51 +0000101
David Goodwin047ae2f2009-09-22 16:47:52 +0000102 unsigned index = getFutureIndex(cycle + i);
103 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
104 if (!freeUnits) {
David Greene73242dd2010-01-04 21:26:07 +0000105 DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
106 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
David Goodwin047ae2f2009-09-22 16:47:52 +0000107 DEBUG(SU->getInstr()->dump());
108 return Hazard;
109 }
David Goodwind94a4e52009-08-10 15:55:25 +0000110 }
David Goodwin047ae2f2009-09-22 16:47:52 +0000111
112 // Advance the cycle to the next stage.
113 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000114 }
115
116 return NoHazard;
117}
118
119void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +0000120 if (ItinData.isEmpty())
121 return;
David Goodwind94a4e52009-08-10 15:55:25 +0000122
David Goodwin047ae2f2009-09-22 16:47:52 +0000123 unsigned cycle = 0;
David Goodwind94a4e52009-08-10 15:55:25 +0000124
David Goodwin047ae2f2009-09-22 16:47:52 +0000125 // Use the itinerary for the underlying instruction to reserve FU's
126 // in the scoreboard at the appropriate future cycles.
127 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
128 for (const InstrStage *IS = ItinData.beginStage(idx),
129 *E = ItinData.endStage(idx); IS != E; ++IS) {
130 // We must reserve one of the stage's units for every cycle the
131 // stage is occupied. FIXME it would be more accurate to reserve
132 // the same unit free in all the cycles.
133 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
134 assert(((cycle + i) < ScoreboardDepth) &&
135 "Scoreboard depth exceeded!");
136
137 unsigned index = getFutureIndex(cycle + i);
138 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
139
140 // reduce to a single unit
141 unsigned freeUnit = 0;
142 do {
143 freeUnit = freeUnits;
144 freeUnits = freeUnit & (freeUnit - 1);
145 } while (freeUnits);
146
147 assert(freeUnit && "No function unit available!");
148 Scoreboard[index] |= freeUnit;
David Goodwind94a4e52009-08-10 15:55:25 +0000149 }
David Goodwin047ae2f2009-09-22 16:47:52 +0000150
151 // Advance the cycle to the next stage.
152 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000153 }
David Goodwin047ae2f2009-09-22 16:47:52 +0000154
155 DEBUG(dumpScoreboard());
David Goodwind94a4e52009-08-10 15:55:25 +0000156}
157
158void ExactHazardRecognizer::AdvanceCycle() {
159 Scoreboard[ScoreboardHead] = 0;
160 ScoreboardHead = getFutureIndex(1);
161}