LLVM 23.0.0git
PostRAHazardRecognizer.cpp
Go to the documentation of this file.
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
30#include "llvm/ADT/Statistic.h"
37#include "llvm/Pass.h"
38using namespace llvm;
39
40#define DEBUG_TYPE "post-RA-hazard-rec"
41
42STATISTIC(NumNoops, "Number of noops inserted");
43
44namespace {
45struct PostRAHazardRecognizer {
46 bool run(MachineFunction &MF, MachineLoopInfo *MLI);
47};
48
49class PostRAHazardRecognizerLegacy : public MachineFunctionPass {
50
51public:
52 static char ID;
53 PostRAHazardRecognizerLegacy() : MachineFunctionPass(ID) {}
54
55 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 AU.setPreservesCFG();
57 AU.addRequired<MachineLoopInfoWrapperPass>();
59 }
60
61 bool runOnMachineFunction(MachineFunction &Fn) override {
62 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
63 return PostRAHazardRecognizer().run(Fn, &MLI);
64 }
65};
66char PostRAHazardRecognizerLegacy::ID = 0;
67
68} // namespace
69
70char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizerLegacy::ID;
71
72INITIALIZE_PASS_BEGIN(PostRAHazardRecognizerLegacy, DEBUG_TYPE,
73 "Post RA hazard recognizer", false, false)
75INITIALIZE_PASS_END(PostRAHazardRecognizerLegacy, DEBUG_TYPE,
76 "Post RA hazard recognizer", false, false)
77
81 MachineLoopInfo *MLI = &MFAM.getResult<MachineLoopAnalysis>(MF);
82 if (!PostRAHazardRecognizer().run(MF, MLI))
84
86 PA.preserveSet<CFGAnalyses>();
87 return PA;
88}
89
90bool PostRAHazardRecognizer::run(MachineFunction &Fn, MachineLoopInfo *MLI) {
92 std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
93 TII->CreateTargetPostRAHazardRecognizer(Fn, MLI));
94
95 // Return if the target has not implemented a hazard recognizer.
96 if (!HazardRec)
97 return false;
98
99 // Loop over all of the basic blocks
100 bool Changed = false;
101 for (auto &MBB : Fn) {
102 // We do not call HazardRec->reset() here to make sure we are handling noop
103 // hazards at the start of basic blocks.
104 for (MachineInstr &MI : MBB) {
105 // If we need to emit noops prior to this instruction, then do so.
106 unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
107 HazardRec->EmitNoops(NumPreNoops);
108 TII->insertNoops(MBB, MachineBasicBlock::iterator(MI), NumPreNoops);
109 NumNoops += NumPreNoops;
110 if (NumPreNoops)
111 Changed = true;
112
113 HazardRec->EmitInstruction(&MI);
114 if (HazardRec->atIssueLimit()) {
115 HazardRec->AdvanceCycle();
116 }
117 }
118 }
119 return Changed;
120}
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
Analysis pass that exposes the MachineLoopInfo for a machine function.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
Changed
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI char & PostRAHazardRecognizerID
PostRAHazardRecognizer - This pass runs the post-ra hazard recognizer.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.