PostRAHazardRecognizer.cpp 3.4 KB

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