Skip to content
ExactHazardRecognizer.cpp 4.61 KiB
Newer Older
//===----- ExactHazardRecognizer.cpp - hazard recognizer -------- ---------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This implements a hazard recognizer using the instructions itineraries
// defined for the current target.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "post-RA-sched"
#include "ExactHazardRecognizer.h"
#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
David Goodwin's avatar
David Goodwin committed
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrItineraries.h"

Evan Cheng's avatar
Evan Cheng committed
ExactHazardRecognizer::
ExactHazardRecognizer(const InstrItineraryData &LItinData) :
  ScheduleHazardRecognizer(), ItinData(LItinData) 
{
  // Determine the maximum depth of any itinerary. This determines the
  // depth of the scoreboard. We always make the scoreboard at least 1
  // cycle deep to avoid dealing with the boundary condition.
  if (!ItinData.isEmpty()) {
    for (unsigned idx = 0; ; ++idx) {
      if (ItinData.isEndMarker(idx))
      const InstrStage *IS = ItinData.beginStage(idx);
      const InstrStage *E = ItinData.endStage(idx);
      unsigned ItinDepth = 0;
      for (; IS != E; ++IS)

      ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
    }
  }

David Greene's avatar
 
David Greene committed
  DEBUG(dbgs() << "Using exact hazard recognizer: ScoreboardDepth = " 
               << ScoreboardDepth << '\n');
void ExactHazardRecognizer::ScoreBoard::dump() const {
David Greene's avatar
 
David Greene committed
  dbgs() << "Scoreboard:\n";

  unsigned last = Depth - 1;
  while ((last > 0) && ((*this)[last] == 0))
    last--;

  for (unsigned i = 0; i <= last; i++) {
David Greene's avatar
 
David Greene committed
    dbgs() << "\t";
David Greene's avatar
 
David Greene committed
      dbgs() << ((FUs & (1 << j)) ? '1' : '0');
    dbgs() << '\n';
  }
}

ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
David Goodwin's avatar
David Goodwin committed
  if (ItinData.isEmpty())
    return NoHazard;

  unsigned cycle = 0;

  // Use the itinerary for the underlying instruction to check for
  // free FU's in the scoreboard at the appropriate future cycles.
  unsigned idx = SU->getInstr()->getDesc().getSchedClass();
  for (const InstrStage *IS = ItinData.beginStage(idx),
         *E = ItinData.endStage(idx); IS != E; ++IS) {
    // We must find one of the stage's units free for every cycle the
    // stage is occupied. FIXME it would be more accurate to find the
    // same unit free in all the cycles.
    for (unsigned int i = 0; i < IS->getCycles(); ++i) {
David Goodwin's avatar
David Goodwin committed
             "Scoreboard depth exceeded!");

      unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i];
David Goodwin's avatar
David Goodwin committed
      if (!freeUnits) {
David Greene's avatar
 
David Greene committed
        DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
        DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
David Goodwin's avatar
David Goodwin committed
        DEBUG(SU->getInstr()->dump());
        return Hazard;
      }
David Goodwin's avatar
David Goodwin committed
    // Advance the cycle to the next stage.
    cycle += IS->getNextCycles();
void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
David Goodwin's avatar
David Goodwin committed
  if (ItinData.isEmpty())
    return;

  unsigned cycle = 0;

  // Use the itinerary for the underlying instruction to reserve FU's
  // in the scoreboard at the appropriate future cycles.
  unsigned idx = SU->getInstr()->getDesc().getSchedClass();
  for (const InstrStage *IS = ItinData.beginStage(idx), 
         *E = ItinData.endStage(idx); IS != E; ++IS) {
    // We must reserve one of the stage's units for every cycle the
    // stage is occupied. FIXME it would be more accurate to reserve
    // the same unit free in all the cycles.
    for (unsigned int i = 0; i < IS->getCycles(); ++i) {
David Goodwin's avatar
David Goodwin committed
             "Scoreboard depth exceeded!");
David Goodwin's avatar
David Goodwin committed
      // reduce to a single unit
      unsigned freeUnit = 0;
      do {
        freeUnit = freeUnits;
        freeUnits = freeUnit & (freeUnit - 1);
      } while (freeUnits);
David Goodwin's avatar
David Goodwin committed
      assert(freeUnit && "No function unit available!");
David Goodwin's avatar
David Goodwin committed
    // Advance the cycle to the next stage.
    cycle += IS->getNextCycles();
void ExactHazardRecognizer::AdvanceCycle() {