1*5fe3f00aSReid Kleckner //===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
2*5fe3f00aSReid Kleckner //
3*5fe3f00aSReid Kleckner // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5fe3f00aSReid Kleckner // See https://llvm.org/LICENSE.txt for license information.
5*5fe3f00aSReid Kleckner // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5fe3f00aSReid Kleckner //
7*5fe3f00aSReid Kleckner //===----------------------------------------------------------------------===//
8*5fe3f00aSReid Kleckner //
9*5fe3f00aSReid Kleckner /// \file
10*5fe3f00aSReid Kleckner /// This runs the hazard recognizer and emits noops when necessary.  This
11*5fe3f00aSReid Kleckner /// gives targets a way to run the hazard recognizer without running one of
12*5fe3f00aSReid Kleckner /// the schedulers.  Example use cases for this pass would be:
13*5fe3f00aSReid Kleckner ///
14ee34680bSTom Stellard /// - Targets that need the hazard recognizer to be run at -O0.
15ee34680bSTom Stellard /// - Targets that want to guarantee that hazards at the beginning of
16ee34680bSTom Stellard ///   scheduling regions are handled correctly.  The post-RA scheduler is
17ee34680bSTom Stellard ///   a top-down scheduler, but when there are multiple scheduling regions
18ee34680bSTom Stellard ///   in a basic block, it visits the regions in bottom-up order.  This
19ee34680bSTom Stellard ///   makes it impossible for the scheduler to gauranttee it can correctly
20ee34680bSTom Stellard ///   handle hazards at the beginning of scheduling regions.
21ee34680bSTom Stellard ///
22ee34680bSTom Stellard /// This pass traverses all the instructions in a program in top-down order.
23ee34680bSTom Stellard /// In contrast to the instruction scheduling passes, this pass never resets
24ee34680bSTom Stellard /// the hazard recognizer to ensure it can correctly handles noop hazards at
25e9dea6e6SHiroshi Inoue /// the beginning of blocks.
26ee34680bSTom Stellard //
27ee34680bSTom Stellard //===----------------------------------------------------------------------===//
28ee34680bSTom Stellard 
29ee34680bSTom Stellard #include "llvm/ADT/Statistic.h"
30ee34680bSTom Stellard #include "llvm/CodeGen/MachineFunctionPass.h"
316bda14b3SChandler Carruth #include "llvm/CodeGen/Passes.h"
32ee34680bSTom Stellard #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
333f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
34b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
3505da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
36ee34680bSTom Stellard #include "llvm/Support/Debug.h"
37ee34680bSTom Stellard #include "llvm/Support/ErrorHandling.h"
38ee34680bSTom Stellard #include "llvm/Support/raw_ostream.h"
39ee34680bSTom Stellard using namespace llvm;
40ee34680bSTom Stellard 
41ee34680bSTom Stellard #define DEBUG_TYPE "post-RA-hazard-rec"
42ee34680bSTom Stellard 
43ee34680bSTom Stellard STATISTIC(NumNoops, "Number of noops inserted");
44ee34680bSTom Stellard 
45ee34680bSTom Stellard namespace {
46ee34680bSTom Stellard   class PostRAHazardRecognizer : public MachineFunctionPass {
47ee34680bSTom Stellard 
48ee34680bSTom Stellard   public:
49ee34680bSTom Stellard     static char ID;
50ee34680bSTom Stellard     PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
51ee34680bSTom Stellard 
52ee34680bSTom Stellard     void getAnalysisUsage(AnalysisUsage &AU) const override {
53ee34680bSTom Stellard       AU.setPreservesCFG();
54ee34680bSTom Stellard       MachineFunctionPass::getAnalysisUsage(AU);
55ee34680bSTom Stellard     }
56ee34680bSTom Stellard 
57ee34680bSTom Stellard     bool runOnMachineFunction(MachineFunction &Fn) override;
58ee34680bSTom Stellard 
59ee34680bSTom Stellard   };
60ee34680bSTom Stellard   char PostRAHazardRecognizer::ID = 0;
61ee34680bSTom Stellard 
62ee34680bSTom Stellard }
63ee34680bSTom Stellard 
64ee34680bSTom Stellard char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
65ee34680bSTom Stellard 
66ee34680bSTom Stellard INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
67ee34680bSTom Stellard                 "Post RA hazard recognizer", false, false)
68ee34680bSTom Stellard 
69ee34680bSTom Stellard bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
70ee34680bSTom Stellard   const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
71ee34680bSTom Stellard   std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
72ee34680bSTom Stellard       TII->CreateTargetPostRAHazardRecognizer(Fn));
73ee34680bSTom Stellard 
74ee34680bSTom Stellard   // Return if the target has not implemented a hazard recognizer.
75ee34680bSTom Stellard   if (!HazardRec.get())
76ee34680bSTom Stellard     return false;
77ee34680bSTom Stellard 
78ee34680bSTom Stellard   // Loop over all of the basic blocks
79ee34680bSTom Stellard   for (auto &MBB : Fn) {
80ee34680bSTom Stellard     // We do not call HazardRec->reset() here to make sure we are handling noop
81ee34680bSTom Stellard     // hazards at the start of basic blocks.
82286d9488SDuncan P. N. Exon Smith     for (MachineInstr &MI : MBB) {
83ee34680bSTom Stellard       // If we need to emit noops prior to this instruction, then do so.
84286d9488SDuncan P. N. Exon Smith       unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
85ee34680bSTom Stellard       for (unsigned i = 0; i != NumPreNoops; ++i) {
86ee34680bSTom Stellard         HazardRec->EmitNoop();
87286d9488SDuncan P. N. Exon Smith         TII->insertNoop(MBB, MachineBasicBlock::iterator(MI));
88ee34680bSTom Stellard         ++NumNoops;
89ee34680bSTom Stellard       }
90ee34680bSTom Stellard 
91286d9488SDuncan P. N. Exon Smith       HazardRec->EmitInstruction(&MI);
92ee34680bSTom Stellard       if (HazardRec->atIssueLimit()) {
93ee34680bSTom Stellard         HazardRec->AdvanceCycle();
94ee34680bSTom Stellard       }
95ee34680bSTom Stellard     }
96ee34680bSTom Stellard   }
97ee34680bSTom Stellard   return true;
98ee34680bSTom Stellard }
99