PostRAHazardRecognizer.cpp 3.4 KB

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