ResetMachineFunctionPass.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. MF.initTargetMachineFunctionInfo(MF.getSubtarget());
  61. if (EmitFallbackDiag) {
  62. const Function &F = MF.getFunction();
  63. DiagnosticInfoISelFallback DiagFallback(F);
  64. F.getContext().diagnose(DiagFallback);
  65. }
  66. return true;
  67. }
  68. return false;
  69. }
  70. };
  71. } // end anonymous namespace
  72. char ResetMachineFunction::ID = 0;
  73. INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
  74. "Reset machine function if ISel failed", false, false)
  75. MachineFunctionPass *
  76. llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
  77. bool AbortOnFailedISel = false) {
  78. return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
  79. }