ResetMachineFunctionPass.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
  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. /// \file
  10. /// This file implements a pass that will conditionally reset a machine
  11. /// function as if it was just created. This is used to provide a fallback
  12. /// mechanism when GlobalISel fails, thus the condition for the reset to
  13. /// happen is that the MachineFunction has the FailedISel property.
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/ADT/ScopeExit.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/CodeGen/StackProtector.h"
  21. #include "llvm/CodeGen/Passes.h"
  22. #include "llvm/IR/DiagnosticInfo.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. 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. // No matter what happened, whether we successfully selected the function
  47. // or not, nothing is going to use the vreg types after us. Make sure they
  48. // disappear.
  49. auto ClearVRegTypesOnReturn =
  50. make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
  51. if (MF.getProperties().hasProperty(
  52. MachineFunctionProperties::Property::FailedISel)) {
  53. if (AbortOnFailedISel)
  54. report_fatal_error("Instruction selection failed");
  55. LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
  56. ++NumFunctionsReset;
  57. MF.reset();
  58. if (EmitFallbackDiag) {
  59. const Function &F = MF.getFunction();
  60. DiagnosticInfoISelFallback DiagFallback(F);
  61. F.getContext().diagnose(DiagFallback);
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. };
  68. } // end anonymous namespace
  69. char ResetMachineFunction::ID = 0;
  70. INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
  71. "Reset machine function if ISel failed", false, false)
  72. MachineFunctionPass *
  73. llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
  74. bool AbortOnFailedISel = false) {
  75. return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
  76. }