15fe3f00aSReid Kleckner //===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
25fe3f00aSReid Kleckner //
35fe3f00aSReid Kleckner // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45fe3f00aSReid Kleckner // See https://llvm.org/LICENSE.txt for license information.
55fe3f00aSReid Kleckner // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65fe3f00aSReid Kleckner //
75fe3f00aSReid Kleckner //===----------------------------------------------------------------------===//
85fe3f00aSReid Kleckner //
95fe3f00aSReid Kleckner /// \file
105fe3f00aSReid Kleckner /// This runs the hazard recognizer and emits noops when necessary. This
115fe3f00aSReid Kleckner /// gives targets a way to run the hazard recognizer without running one of
125fe3f00aSReid Kleckner /// the schedulers. Example use cases for this pass would be:
135fe3f00aSReid 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"
31ee34680bSTom Stellard #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
323f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
33b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
3405da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
35989f1c72Sserge-sans-paille #include "llvm/Pass.h"
36ee34680bSTom Stellard using namespace llvm;
37ee34680bSTom Stellard
38ee34680bSTom Stellard #define DEBUG_TYPE "post-RA-hazard-rec"
39ee34680bSTom Stellard
40ee34680bSTom Stellard STATISTIC(NumNoops, "Number of noops inserted");
41ee34680bSTom Stellard
42ee34680bSTom Stellard namespace {
43ee34680bSTom Stellard class PostRAHazardRecognizer : public MachineFunctionPass {
44ee34680bSTom Stellard
45ee34680bSTom Stellard public:
46ee34680bSTom Stellard static char ID;
PostRAHazardRecognizer()47ee34680bSTom Stellard PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
48ee34680bSTom Stellard
getAnalysisUsage(AnalysisUsage & AU) const49ee34680bSTom Stellard void getAnalysisUsage(AnalysisUsage &AU) const override {
50ee34680bSTom Stellard AU.setPreservesCFG();
51ee34680bSTom Stellard MachineFunctionPass::getAnalysisUsage(AU);
52ee34680bSTom Stellard }
53ee34680bSTom Stellard
54ee34680bSTom Stellard bool runOnMachineFunction(MachineFunction &Fn) override;
55ee34680bSTom Stellard
56ee34680bSTom Stellard };
57ee34680bSTom Stellard char PostRAHazardRecognizer::ID = 0;
58ee34680bSTom Stellard
59ee34680bSTom Stellard }
60ee34680bSTom Stellard
61ee34680bSTom Stellard char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
62ee34680bSTom Stellard
63ee34680bSTom Stellard INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
64ee34680bSTom Stellard "Post RA hazard recognizer", false, false)
65ee34680bSTom Stellard
runOnMachineFunction(MachineFunction & Fn)66ee34680bSTom Stellard bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
67ee34680bSTom Stellard const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
68ee34680bSTom Stellard std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
69ee34680bSTom Stellard TII->CreateTargetPostRAHazardRecognizer(Fn));
70ee34680bSTom Stellard
71ee34680bSTom Stellard // Return if the target has not implemented a hazard recognizer.
72*1eada2adSKazu Hirata if (!HazardRec)
73ee34680bSTom Stellard return false;
74ee34680bSTom Stellard
75ee34680bSTom Stellard // Loop over all of the basic blocks
76074d1e25SJay Foad bool Changed = false;
77ee34680bSTom Stellard for (auto &MBB : Fn) {
78ee34680bSTom Stellard // We do not call HazardRec->reset() here to make sure we are handling noop
79ee34680bSTom Stellard // hazards at the start of basic blocks.
80286d9488SDuncan P. N. Exon Smith for (MachineInstr &MI : MBB) {
81ee34680bSTom Stellard // If we need to emit noops prior to this instruction, then do so.
82286d9488SDuncan P. N. Exon Smith unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
8337d90789SAustin Kerbow HazardRec->EmitNoops(NumPreNoops);
8437d90789SAustin Kerbow TII->insertNoops(MBB, MachineBasicBlock::iterator(MI), NumPreNoops);
8537d90789SAustin Kerbow NumNoops += NumPreNoops;
86074d1e25SJay Foad if (NumPreNoops)
87074d1e25SJay Foad Changed = true;
88ee34680bSTom Stellard
89286d9488SDuncan P. N. Exon Smith HazardRec->EmitInstruction(&MI);
90ee34680bSTom Stellard if (HazardRec->atIssueLimit()) {
91ee34680bSTom Stellard HazardRec->AdvanceCycle();
92ee34680bSTom Stellard }
93ee34680bSTom Stellard }
94ee34680bSTom Stellard }
95074d1e25SJay Foad return Changed;
96ee34680bSTom Stellard }
97