1 //===----- 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 26 /// the begining of blocks. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #include "llvm/CodeGen/Passes.h" 31 #include "llvm/ADT/Statistic.h" 32 #include "llvm/CodeGen/MachineFunctionPass.h" 33 #include "llvm/CodeGen/ScheduleHazardRecognizer.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include "llvm/Target/TargetInstrInfo.h" 38 #include "llvm/Target/TargetSubtargetInfo.h" 39 using namespace llvm; 40 41 #define DEBUG_TYPE "post-RA-hazard-rec" 42 43 STATISTIC(NumNoops, "Number of noops inserted"); 44 45 namespace { 46 class PostRAHazardRecognizer : public MachineFunctionPass { 47 const TargetInstrInfo *TII; 48 49 public: 50 static char ID; 51 PostRAHazardRecognizer() : MachineFunctionPass(ID) {} 52 53 void getAnalysisUsage(AnalysisUsage &AU) const override { 54 AU.setPreservesCFG(); 55 MachineFunctionPass::getAnalysisUsage(AU); 56 } 57 58 bool runOnMachineFunction(MachineFunction &Fn) override; 59 60 }; 61 char PostRAHazardRecognizer::ID = 0; 62 63 } 64 65 char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID; 66 67 INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE, 68 "Post RA hazard recognizer", false, false) 69 70 bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) { 71 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo(); 72 std::unique_ptr<ScheduleHazardRecognizer> HazardRec( 73 TII->CreateTargetPostRAHazardRecognizer(Fn)); 74 75 // Return if the target has not implemented a hazard recognizer. 76 if (!HazardRec.get()) 77 return false; 78 79 // Loop over all of the basic blocks 80 for (auto &MBB : Fn) { 81 // We do not call HazardRec->reset() here to make sure we are handling noop 82 // hazards at the start of basic blocks. 83 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); 84 I != E; ++I) { 85 MachineInstr *MI = I; 86 // If we need to emit noops prior to this instruction, then do so. 87 unsigned NumPreNoops = HazardRec->PreEmitNoops(MI); 88 for (unsigned i = 0; i != NumPreNoops; ++i) { 89 HazardRec->EmitNoop(); 90 TII->insertNoop(MBB, I); 91 ++NumNoops; 92 } 93 94 HazardRec->EmitInstruction(MI); 95 if (HazardRec->atIssueLimit()) { 96 HazardRec->AdvanceCycle(); 97 } 98 } 99 } 100 return true; 101 } 102