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