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