13ca95b02SDimitry Andric //===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric //
103ca95b02SDimitry Andric /// \file
113ca95b02SDimitry Andric /// This runs the hazard recognizer and emits noops when necessary. This
123ca95b02SDimitry Andric /// gives targets a way to run the hazard recognizer without running one of
133ca95b02SDimitry Andric /// the schedulers. Example use cases for this pass would be:
143ca95b02SDimitry Andric ///
153ca95b02SDimitry Andric /// - Targets that need the hazard recognizer to be run at -O0.
163ca95b02SDimitry Andric /// - Targets that want to guarantee that hazards at the beginning of
173ca95b02SDimitry Andric /// scheduling regions are handled correctly. The post-RA scheduler is
183ca95b02SDimitry Andric /// a top-down scheduler, but when there are multiple scheduling regions
193ca95b02SDimitry Andric /// in a basic block, it visits the regions in bottom-up order. This
203ca95b02SDimitry Andric /// makes it impossible for the scheduler to gauranttee it can correctly
213ca95b02SDimitry Andric /// handle hazards at the beginning of scheduling regions.
223ca95b02SDimitry Andric ///
233ca95b02SDimitry Andric /// This pass traverses all the instructions in a program in top-down order.
243ca95b02SDimitry Andric /// In contrast to the instruction scheduling passes, this pass never resets
253ca95b02SDimitry Andric /// the hazard recognizer to ensure it can correctly handles noop hazards at
26c4394386SDimitry Andric /// the beginning of blocks.
273ca95b02SDimitry Andric //
283ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
293ca95b02SDimitry Andric
303ca95b02SDimitry Andric #include "llvm/ADT/Statistic.h"
313ca95b02SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
32db17bf38SDimitry Andric #include "llvm/CodeGen/Passes.h"
333ca95b02SDimitry Andric #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
34*2cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
35*2cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
363ca95b02SDimitry Andric #include "llvm/Support/Debug.h"
373ca95b02SDimitry Andric #include "llvm/Support/ErrorHandling.h"
383ca95b02SDimitry Andric #include "llvm/Support/raw_ostream.h"
393ca95b02SDimitry Andric using namespace llvm;
403ca95b02SDimitry Andric
413ca95b02SDimitry Andric #define DEBUG_TYPE "post-RA-hazard-rec"
423ca95b02SDimitry Andric
433ca95b02SDimitry Andric STATISTIC(NumNoops, "Number of noops inserted");
443ca95b02SDimitry Andric
453ca95b02SDimitry Andric namespace {
463ca95b02SDimitry Andric class PostRAHazardRecognizer : public MachineFunctionPass {
473ca95b02SDimitry Andric
483ca95b02SDimitry Andric public:
493ca95b02SDimitry Andric static char ID;
PostRAHazardRecognizer()503ca95b02SDimitry Andric PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
513ca95b02SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const523ca95b02SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
533ca95b02SDimitry Andric AU.setPreservesCFG();
543ca95b02SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
553ca95b02SDimitry Andric }
563ca95b02SDimitry Andric
573ca95b02SDimitry Andric bool runOnMachineFunction(MachineFunction &Fn) override;
583ca95b02SDimitry Andric
593ca95b02SDimitry Andric };
603ca95b02SDimitry Andric char PostRAHazardRecognizer::ID = 0;
613ca95b02SDimitry Andric
623ca95b02SDimitry Andric }
633ca95b02SDimitry Andric
643ca95b02SDimitry Andric char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
653ca95b02SDimitry Andric
663ca95b02SDimitry Andric INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
673ca95b02SDimitry Andric "Post RA hazard recognizer", false, false)
683ca95b02SDimitry Andric
runOnMachineFunction(MachineFunction & Fn)693ca95b02SDimitry Andric bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
703ca95b02SDimitry Andric const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
713ca95b02SDimitry Andric std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
723ca95b02SDimitry Andric TII->CreateTargetPostRAHazardRecognizer(Fn));
733ca95b02SDimitry Andric
743ca95b02SDimitry Andric // Return if the target has not implemented a hazard recognizer.
753ca95b02SDimitry Andric if (!HazardRec.get())
763ca95b02SDimitry Andric return false;
773ca95b02SDimitry Andric
783ca95b02SDimitry Andric // Loop over all of the basic blocks
793ca95b02SDimitry Andric for (auto &MBB : Fn) {
803ca95b02SDimitry Andric // We do not call HazardRec->reset() here to make sure we are handling noop
813ca95b02SDimitry Andric // hazards at the start of basic blocks.
823ca95b02SDimitry Andric for (MachineInstr &MI : MBB) {
833ca95b02SDimitry Andric // If we need to emit noops prior to this instruction, then do so.
843ca95b02SDimitry Andric unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
853ca95b02SDimitry Andric for (unsigned i = 0; i != NumPreNoops; ++i) {
863ca95b02SDimitry Andric HazardRec->EmitNoop();
873ca95b02SDimitry Andric TII->insertNoop(MBB, MachineBasicBlock::iterator(MI));
883ca95b02SDimitry Andric ++NumNoops;
893ca95b02SDimitry Andric }
903ca95b02SDimitry Andric
913ca95b02SDimitry Andric HazardRec->EmitInstruction(&MI);
923ca95b02SDimitry Andric if (HazardRec->atIssueLimit()) {
933ca95b02SDimitry Andric HazardRec->AdvanceCycle();
943ca95b02SDimitry Andric }
953ca95b02SDimitry Andric }
963ca95b02SDimitry Andric }
973ca95b02SDimitry Andric return true;
983ca95b02SDimitry Andric }
99