blob: 52908edefc880cc580d4ad2e44a4beb62c78c02e [file] [log] [blame]
David Goodwin5ab4fd42009-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 Gohmandf1a7ff2010-02-10 16:03:48 +000010// This implements a hazard recognizer using the instructions itineraries
David Goodwin5ab4fd42009-08-10 15:55:25 +000011// defined for the current target.
12//
13//===----------------------------------------------------------------------===//
14
David Goodwin996892b2009-11-03 20:57:50 +000015#define DEBUG_TYPE "post-RA-sched"
David Goodwin5ab4fd42009-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 Goodwin0eeb4512009-08-11 01:44:26 +000020#include "llvm/Support/raw_ostream.h"
David Goodwin5ab4fd42009-08-10 15:55:25 +000021#include "llvm/Target/TargetInstrItineraries.h"
22
Bill Wendling4c1dfcc2009-08-22 20:08:44 +000023using namespace llvm;
David Goodwin5ab4fd42009-08-10 15:55:25 +000024
Evan Chenge7a67052009-10-16 05:18:39 +000025ExactHazardRecognizer::
26ExactHazardRecognizer(const InstrItineraryData &LItinData) :
David Goodwin5ab4fd42009-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 Korobeynikov60fb8022010-04-07 18:19:24 +000032 unsigned ScoreboardDepth = 1;
David Goodwin5ab4fd42009-08-10 15:55:25 +000033 if (!ItinData.isEmpty()) {
34 for (unsigned idx = 0; ; ++idx) {
David Goodwined174992009-09-24 20:22:50 +000035 if (ItinData.isEndMarker(idx))
David Goodwin5ab4fd42009-08-10 15:55:25 +000036 break;
37
David Goodwined174992009-09-24 20:22:50 +000038 const InstrStage *IS = ItinData.beginStage(idx);
39 const InstrStage *E = ItinData.endStage(idx);
David Goodwin5ab4fd42009-08-10 15:55:25 +000040 unsigned ItinDepth = 0;
41 for (; IS != E; ++IS)
David Goodwin4b6e4982009-08-12 18:31:53 +000042 ItinDepth += IS->getCycles();
David Goodwin5ab4fd42009-08-10 15:55:25 +000043
44 ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
45 }
46 }
47
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000048 Scoreboard.reset(ScoreboardDepth);
David Goodwin5ab4fd42009-08-10 15:55:25 +000049
David Greene5fb66142010-01-04 21:26:07 +000050 DEBUG(dbgs() << "Using exact hazard recognizer: ScoreboardDepth = "
Bill Wendling4c1dfcc2009-08-22 20:08:44 +000051 << ScoreboardDepth << '\n');
David Goodwin5ab4fd42009-08-10 15:55:25 +000052}
53
David Goodwin5ab4fd42009-08-10 15:55:25 +000054void ExactHazardRecognizer::Reset() {
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000055 Scoreboard.reset();
David Goodwin5ab4fd42009-08-10 15:55:25 +000056}
57
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000058void ExactHazardRecognizer::ScoreBoard::dump() const {
David Greene5fb66142010-01-04 21:26:07 +000059 dbgs() << "Scoreboard:\n";
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000060
61 unsigned last = Depth - 1;
62 while ((last > 0) && ((*this)[last] == 0))
David Goodwin5ab4fd42009-08-10 15:55:25 +000063 last--;
64
65 for (unsigned i = 0; i <= last; i++) {
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000066 unsigned FUs = (*this)[i];
David Greene5fb66142010-01-04 21:26:07 +000067 dbgs() << "\t";
David Goodwin5ab4fd42009-08-10 15:55:25 +000068 for (int j = 31; j >= 0; j--)
David Greene5fb66142010-01-04 21:26:07 +000069 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
70 dbgs() << '\n';
David Goodwin5ab4fd42009-08-10 15:55:25 +000071 }
72}
73
74ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
David Goodwincc747922009-09-22 16:47:52 +000075 if (ItinData.isEmpty())
76 return NoHazard;
David Goodwin5ab4fd42009-08-10 15:55:25 +000077
David Goodwincc747922009-09-22 16:47:52 +000078 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 Korobeynikov60fb8022010-04-07 18:19:24 +000089 assert(((cycle + i) < Scoreboard.getDepth()) &&
David Goodwincc747922009-09-22 16:47:52 +000090 "Scoreboard depth exceeded!");
Anton Korobeynikov60fb8022010-04-07 18:19:24 +000091
92 unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i];
David Goodwincc747922009-09-22 16:47:52 +000093 if (!freeUnits) {
David Greene5fb66142010-01-04 21:26:07 +000094 DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
95 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
David Goodwincc747922009-09-22 16:47:52 +000096 DEBUG(SU->getInstr()->dump());
97 return Hazard;
98 }
David Goodwin5ab4fd42009-08-10 15:55:25 +000099 }
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000100
David Goodwincc747922009-09-22 16:47:52 +0000101 // Advance the cycle to the next stage.
102 cycle += IS->getNextCycles();
David Goodwin5ab4fd42009-08-10 15:55:25 +0000103 }
104
105 return NoHazard;
106}
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000107
David Goodwin5ab4fd42009-08-10 15:55:25 +0000108void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
David Goodwincc747922009-09-22 16:47:52 +0000109 if (ItinData.isEmpty())
110 return;
David Goodwin5ab4fd42009-08-10 15:55:25 +0000111
David Goodwincc747922009-09-22 16:47:52 +0000112 unsigned cycle = 0;
David Goodwin5ab4fd42009-08-10 15:55:25 +0000113
David Goodwincc747922009-09-22 16:47:52 +0000114 // 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 Korobeynikov60fb8022010-04-07 18:19:24 +0000123 assert(((cycle + i) < Scoreboard.getDepth()) &&
David Goodwincc747922009-09-22 16:47:52 +0000124 "Scoreboard depth exceeded!");
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000125
126 unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i];
127
David Goodwincc747922009-09-22 16:47:52 +0000128 // reduce to a single unit
129 unsigned freeUnit = 0;
130 do {
131 freeUnit = freeUnits;
132 freeUnits = freeUnit & (freeUnit - 1);
133 } while (freeUnits);
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000134
David Goodwincc747922009-09-22 16:47:52 +0000135 assert(freeUnit && "No function unit available!");
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000136 Scoreboard[cycle + i] |= freeUnit;
David Goodwin5ab4fd42009-08-10 15:55:25 +0000137 }
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000138
David Goodwincc747922009-09-22 16:47:52 +0000139 // Advance the cycle to the next stage.
140 cycle += IS->getNextCycles();
David Goodwin5ab4fd42009-08-10 15:55:25 +0000141 }
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000142
143 DEBUG(Scoreboard.dump());
David Goodwin5ab4fd42009-08-10 15:55:25 +0000144}
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000145
David Goodwin5ab4fd42009-08-10 15:55:25 +0000146void ExactHazardRecognizer::AdvanceCycle() {
Anton Korobeynikov60fb8022010-04-07 18:19:24 +0000147 Scoreboard[0] = 0;
148 Scoreboard.advance();
David Goodwin5ab4fd42009-08-10 15:55:25 +0000149}