LLVM 22.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"
36#include "llvm/Pass.h"
37using namespace llvm;
38
39#define DEBUG_TYPE "post-RA-hazard-rec"
40
41STATISTIC(NumNoops, "Number of noops inserted");
42
43namespace {
44struct PostRAHazardRecognizer {
45 bool run(MachineFunction &MF);
46};
47
48class PostRAHazardRecognizerLegacy : public MachineFunctionPass {
49
50public:
51 static char ID;
52 PostRAHazardRecognizerLegacy() : MachineFunctionPass(ID) {}
53
54 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 AU.setPreservesCFG();
57 }
58
59 bool runOnMachineFunction(MachineFunction &Fn) override {
60 return PostRAHazardRecognizer().run(Fn);
61 }
62};
63char PostRAHazardRecognizerLegacy::ID = 0;
64
65} // namespace
66
67char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizerLegacy::ID;
68
69INITIALIZE_PASS(PostRAHazardRecognizerLegacy, DEBUG_TYPE,
70 "Post RA hazard recognizer", false, false)
71
75 if (!PostRAHazardRecognizer().run(MF))
77
79 PA.preserveSet<CFGAnalyses>();
80 return PA;
81}
82
83bool PostRAHazardRecognizer::run(MachineFunction &Fn) {
84 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
85 std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
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}
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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
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
ScheduleHazardRecognizer * CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, const ScheduleDAG *DAG) const override
Allocate and return a hazard recognizer to use for this target when scheduling the machine instructio...
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.
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
virtual const TargetInstrInfo * getInstrInfo() const
Changed
This is an optimization pass for GlobalISel generic memory operations.
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.