blob: 0a3838617bc5605babf95e1f6fc69f95107ed688 [file] [log] [blame]
Tom Stellardee346802016-04-22 14:43:50 +00001//===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellardee346802016-04-22 14:43:50 +00006//
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
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +000025/// the beginning of blocks.
Tom Stellardee346802016-04-22 14:43:50 +000026//
27//===----------------------------------------------------------------------===//
28
Tom Stellardee346802016-04-22 14:43:50 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "llvm/CodeGen/Passes.h"
Tom Stellardee346802016-04-22 14:43:50 +000032#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000034#include "llvm/CodeGen/TargetSubtargetInfo.h"
Tom Stellardee346802016-04-22 14:43:50 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Tom Stellardee346802016-04-22 14:43:50 +000038using namespace llvm;
39
40#define DEBUG_TYPE "post-RA-hazard-rec"
41
42STATISTIC(NumNoops, "Number of noops inserted");
43
44namespace {
45 class PostRAHazardRecognizer : public MachineFunctionPass {
Tom Stellardee346802016-04-22 14:43:50 +000046
47 public:
48 static char ID;
49 PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesCFG();
53 MachineFunctionPass::getAnalysisUsage(AU);
54 }
55
56 bool runOnMachineFunction(MachineFunction &Fn) override;
57
58 };
59 char PostRAHazardRecognizer::ID = 0;
60
61}
62
63char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
64
65INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
66 "Post RA hazard recognizer", false, false)
67
68bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
69 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
70 std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
71 TII->CreateTargetPostRAHazardRecognizer(Fn));
72
73 // Return if the target has not implemented a hazard recognizer.
74 if (!HazardRec.get())
75 return false;
76
77 // Loop over all of the basic blocks
78 for (auto &MBB : Fn) {
79 // We do not call HazardRec->reset() here to make sure we are handling noop
80 // hazards at the start of basic blocks.
Duncan P. N. Exon Smith286d9482016-07-01 00:50:29 +000081 for (MachineInstr &MI : MBB) {
Tom Stellardee346802016-04-22 14:43:50 +000082 // If we need to emit noops prior to this instruction, then do so.
Duncan P. N. Exon Smith286d9482016-07-01 00:50:29 +000083 unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
Tom Stellardee346802016-04-22 14:43:50 +000084 for (unsigned i = 0; i != NumPreNoops; ++i) {
85 HazardRec->EmitNoop();
Duncan P. N. Exon Smith286d9482016-07-01 00:50:29 +000086 TII->insertNoop(MBB, MachineBasicBlock::iterator(MI));
Tom Stellardee346802016-04-22 14:43:50 +000087 ++NumNoops;
88 }
89
Duncan P. N. Exon Smith286d9482016-07-01 00:50:29 +000090 HazardRec->EmitInstruction(&MI);
Tom Stellardee346802016-04-22 14:43:50 +000091 if (HazardRec->atIssueLimit()) {
92 HazardRec->AdvanceCycle();
93 }
94 }
95 }
96 return true;
97}