blob: f9d4a9746e4162b099580043d944f5ad9ad443c9 [file] [log] [blame]
Tom Stellardfbbc6212016-04-22 14:43:50 +00001//===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// This runs the hazard recognizer and emits noops when necessary. This
12/// gives targets a way to run the hazard recognizer without running one of
13/// the schedulers. Example use cases for this pass would be:
14///
15/// - Targets that need the hazard recognizer to be run at -O0.
16/// - Targets that want to guarantee that hazards at the beginning of
17/// scheduling regions are handled correctly. The post-RA scheduler is
18/// a top-down scheduler, but when there are multiple scheduling regions
19/// in a basic block, it visits the regions in bottom-up order. This
20/// makes it impossible for the scheduler to gauranttee it can correctly
21/// handle hazards at the beginning of scheduling regions.
22///
23/// This pass traverses all the instructions in a program in top-down order.
24/// In contrast to the instruction scheduling passes, this pass never resets
25/// the hazard recognizer to ensure it can correctly handles noop hazards at
Hiroshi Inoueff281e52017-07-13 06:48:39 +000026/// the beginning of blocks.
Tom Stellardfbbc6212016-04-22 14:43:50 +000027//
28//===----------------------------------------------------------------------===//
29
Tom Stellardfbbc6212016-04-22 14:43:50 +000030#include "llvm/ADT/Statistic.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000032#include "llvm/CodeGen/Passes.h"
Tom Stellardfbbc6212016-04-22 14:43:50 +000033#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
David Blaikie48319232017-11-08 01:01:31 +000034#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000035#include "llvm/CodeGen/TargetSubtargetInfo.h"
Tom Stellardfbbc6212016-04-22 14:43:50 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Tom Stellardfbbc6212016-04-22 14:43:50 +000039using namespace llvm;
40
41#define DEBUG_TYPE "post-RA-hazard-rec"
42
43STATISTIC(NumNoops, "Number of noops inserted");
44
45namespace {
46 class PostRAHazardRecognizer : public MachineFunctionPass {
Tom Stellardfbbc6212016-04-22 14:43:50 +000047
48 public:
49 static char ID;
50 PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
51
52 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.setPreservesCFG();
54 MachineFunctionPass::getAnalysisUsage(AU);
55 }
56
57 bool runOnMachineFunction(MachineFunction &Fn) override;
58
59 };
60 char PostRAHazardRecognizer::ID = 0;
61
62}
63
64char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
65
66INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
67 "Post RA hazard recognizer", false, false)
68
69bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
70 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
71 std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
72 TII->CreateTargetPostRAHazardRecognizer(Fn));
73
74 // Return if the target has not implemented a hazard recognizer.
75 if (!HazardRec.get())
76 return false;
77
78 // Loop over all of the basic blocks
79 for (auto &MBB : Fn) {
80 // We do not call HazardRec->reset() here to make sure we are handling noop
81 // hazards at the start of basic blocks.
Duncan P. N. Exon Smithed5a96f2016-07-01 00:50:29 +000082 for (MachineInstr &MI : MBB) {
Tom Stellardfbbc6212016-04-22 14:43:50 +000083 // If we need to emit noops prior to this instruction, then do so.
Duncan P. N. Exon Smithed5a96f2016-07-01 00:50:29 +000084 unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
Tom Stellardfbbc6212016-04-22 14:43:50 +000085 for (unsigned i = 0; i != NumPreNoops; ++i) {
86 HazardRec->EmitNoop();
Duncan P. N. Exon Smithed5a96f2016-07-01 00:50:29 +000087 TII->insertNoop(MBB, MachineBasicBlock::iterator(MI));
Tom Stellardfbbc6212016-04-22 14:43:50 +000088 ++NumNoops;
89 }
90
Duncan P. N. Exon Smithed5a96f2016-07-01 00:50:29 +000091 HazardRec->EmitInstruction(&MI);
Tom Stellardfbbc6212016-04-22 14:43:50 +000092 if (HazardRec->atIssueLimit()) {
93 HazardRec->AdvanceCycle();
94 }
95 }
96 }
97 return true;
98}