PostRAHazardRecognizer.cpp 3.5 KB

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