LLVM API Documentation

SimpleHazardRecognizer.h

Go to the documentation of this file.
00001 //=- llvm/CodeGen/SimpleHazardRecognizer.h - Scheduling Support -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SimpleHazardRecognizer class, which
00011 // implements hazard-avoidance heuristics for scheduling, based on the
00012 // scheduling itineraries specified for the target.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_SIMPLEHAZARDRECOGNIZER_H
00017 #define LLVM_CODEGEN_SIMPLEHAZARDRECOGNIZER_H
00018 
00019 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
00020 #include "llvm/CodeGen/ScheduleDAG.h"
00021 #include "llvm/Target/TargetMachine.h"
00022 #include "llvm/Target/TargetInstrInfo.h"
00023 
00024 namespace llvm {
00025   /// SimpleHazardRecognizer - A *very* simple hazard recognizer. It uses
00026   /// a coarse classification and attempts to avoid that instructions of
00027   /// a given class aren't grouped too densely together.
00028   class SimpleHazardRecognizer : public ScheduleHazardRecognizer {
00029     /// Class - A simple classification for SUnits.
00030     enum Class {
00031       Other, Load, Store
00032     };
00033 
00034     /// Window - The Class values of the most recently issued
00035     /// instructions.
00036     Class Window[8];
00037 
00038     /// getClass - Classify the given SUnit.
00039     Class getClass(const SUnit *SU) {
00040       const MachineInstr *MI = SU->getInstr();
00041       const TargetInstrDesc &TID = MI->getDesc();
00042       if (TID.mayLoad())
00043         return Load;
00044       if (TID.mayStore())
00045         return Store;
00046       return Other;
00047     }
00048 
00049     /// Step - Rotate the existing entries in Window and insert the
00050     /// given class value in position as the most recent.
00051     void Step(Class C) {
00052       std::copy(Window+1, array_endof(Window), Window);
00053       Window[array_lengthof(Window)-1] = C;
00054     }
00055 
00056   public:
00057     SimpleHazardRecognizer() : Window() {
00058       Reset();
00059     }
00060 
00061     virtual HazardType getHazardType(SUnit *SU) {
00062       Class C = getClass(SU);
00063       if (C == Other)
00064         return NoHazard;
00065       unsigned Score = 0;
00066       for (unsigned i = 0; i != array_lengthof(Window); ++i)
00067         if (Window[i] == C)
00068           Score += i + 1;
00069       if (Score > array_lengthof(Window) * 2)
00070         return Hazard;
00071       return NoHazard;
00072     }
00073 
00074     virtual void Reset() {
00075       for (unsigned i = 0; i != array_lengthof(Window); ++i)
00076         Window[i] = Other;
00077     }
00078 
00079     virtual void EmitInstruction(SUnit *SU) {
00080       Step(getClass(SU));
00081     }
00082 
00083     virtual void AdvanceCycle() {
00084       Step(Other);
00085     }
00086   };
00087 }
00088 
00089 #endif