2019-11-14 18:19:36 +00:00
|
|
|
//===----- PostRAHazardRecognizer.cpp - hazard recognizer -----------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// This runs the hazard recognizer and emits noops when necessary. This
|
|
|
|
/// gives targets a way to run the hazard recognizer without running one of
|
|
|
|
/// the schedulers. Example use cases for this pass would be:
|
|
|
|
///
|
2016-04-22 14:43:50 +00:00
|
|
|
/// - Targets that need the hazard recognizer to be run at -O0.
|
|
|
|
/// - Targets that want to guarantee that hazards at the beginning of
|
|
|
|
/// scheduling regions are handled correctly. The post-RA scheduler is
|
|
|
|
/// a top-down scheduler, but when there are multiple scheduling regions
|
|
|
|
/// in a basic block, it visits the regions in bottom-up order. This
|
|
|
|
/// makes it impossible for the scheduler to gauranttee it can correctly
|
|
|
|
/// handle hazards at the beginning of scheduling regions.
|
|
|
|
///
|
|
|
|
/// This pass traverses all the instructions in a program in top-down order.
|
|
|
|
/// In contrast to the instruction scheduling passes, this pass never resets
|
|
|
|
/// the hazard recognizer to ensure it can correctly handles noop hazards at
|
2017-07-13 06:48:39 +00:00
|
|
|
/// the beginning of blocks.
|
2016-04-22 14:43:50 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-06-06 11:49:48 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2016-04-22 14:43:50 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
|
2017-11-08 01:01:31 +00:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 01:07:10 +00:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 21:15:01 +00:00
|
|
|
#include "llvm/InitializePasses.h"
|
2016-04-22 14:43:50 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "post-RA-hazard-rec"
|
|
|
|
|
|
|
|
STATISTIC(NumNoops, "Number of noops inserted");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PostRAHazardRecognizer : public MachineFunctionPass {
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
PostRAHazardRecognizer() : MachineFunctionPass(ID) {}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
|
|
|
|
|
|
|
};
|
|
|
|
char PostRAHazardRecognizer::ID = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE,
|
|
|
|
"Post RA hazard recognizer", false, false)
|
|
|
|
|
|
|
|
bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
|
|
|
|
std::unique_ptr<ScheduleHazardRecognizer> HazardRec(
|
|
|
|
TII->CreateTargetPostRAHazardRecognizer(Fn));
|
|
|
|
|
|
|
|
// Return if the target has not implemented a hazard recognizer.
|
|
|
|
if (!HazardRec.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Loop over all of the basic blocks
|
|
|
|
for (auto &MBB : Fn) {
|
|
|
|
// We do not call HazardRec->reset() here to make sure we are handling noop
|
|
|
|
// hazards at the start of basic blocks.
|
2016-07-01 00:50:29 +00:00
|
|
|
for (MachineInstr &MI : MBB) {
|
2016-04-22 14:43:50 +00:00
|
|
|
// If we need to emit noops prior to this instruction, then do so.
|
2016-07-01 00:50:29 +00:00
|
|
|
unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI);
|
2016-04-22 14:43:50 +00:00
|
|
|
for (unsigned i = 0; i != NumPreNoops; ++i) {
|
|
|
|
HazardRec->EmitNoop();
|
2016-07-01 00:50:29 +00:00
|
|
|
TII->insertNoop(MBB, MachineBasicBlock::iterator(MI));
|
2016-04-22 14:43:50 +00:00
|
|
|
++NumNoops;
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:50:29 +00:00
|
|
|
HazardRec->EmitInstruction(&MI);
|
2016-04-22 14:43:50 +00:00
|
|
|
if (HazardRec->atIssueLimit()) {
|
|
|
|
HazardRec->AdvanceCycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|