ResetMachineFunctionPass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
  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. /// \file
  9. /// This file implements a pass that will conditionally reset a machine
  10. /// function as if it was just created. This is used to provide a fallback
  11. /// mechanism when GlobalISel fails, thus the condition for the reset to
  12. /// happen is that the MachineFunction has the FailedISel property.
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/ScopeExit.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/CodeGen/StackProtector.h"
  21. #include "llvm/IR/DiagnosticInfo.h"
  22. #include "llvm/InitializePasses.h"
  23. #include "llvm/Support/Debug.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "reset-machine-function"
  26. STATISTIC(NumFunctionsReset, "Number of functions reset");
  27. STATISTIC(NumFunctionsVisited, "Number of functions visited");
  28. namespace {
  29. class ResetMachineFunction : public MachineFunctionPass {
  30. /// Tells whether or not this pass should emit a fallback
  31. /// diagnostic when it resets a function.
  32. bool EmitFallbackDiag;
  33. /// Whether we should abort immediately instead of resetting the function.
  34. bool AbortOnFailedISel;
  35. public:
  36. static char ID; // Pass identification, replacement for typeid
  37. ResetMachineFunction(bool EmitFallbackDiag = false,
  38. bool AbortOnFailedISel = false)
  39. : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
  40. AbortOnFailedISel(AbortOnFailedISel) {}
  41. StringRef getPassName() const override { return "ResetMachineFunction"; }
  42. void getAnalysisUsage(AnalysisUsage &AU) const override {
  43. AU.addPreserved<StackProtector>();
  44. MachineFunctionPass::getAnalysisUsage(AU);
  45. }
  46. bool runOnMachineFunction(MachineFunction &MF) override {
  47. ++NumFunctionsVisited;
  48. // No matter what happened, whether we successfully selected the function
  49. // or not, nothing is going to use the vreg types after us. Make sure they
  50. // disappear.
  51. auto ClearVRegTypesOnReturn =
  52. make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
  53. if (MF.getProperties().hasProperty(
  54. MachineFunctionProperties::Property::FailedISel)) {
  55. if (AbortOnFailedISel)
  56. report_fatal_error("Instruction selection failed");
  57. LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
  58. ++NumFunctionsReset;
  59. MF.reset();
  60. if (EmitFallbackDiag) {
  61. const Function &F = MF.getFunction();
  62. DiagnosticInfoISelFallback DiagFallback(F);
  63. F.getContext().diagnose(DiagFallback);
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. };
  70. } // end anonymous namespace
  71. char ResetMachineFunction::ID = 0;
  72. INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
  73. "Reset machine function if ISel failed", false, false)
  74. MachineFunctionPass *
  75. llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
  76. bool AbortOnFailedISel = false) {
  77. return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
  78. }