ResetMachineFunctionPass.cpp 3.4 KB

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