ResetMachineFunctionPass.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/Statistic.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/CodeGen/Passes.h"
  19. #include "llvm/IR/DiagnosticInfo.h"
  20. #include "llvm/Support/Debug.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "reset-machine-function"
  23. STATISTIC(NumFunctionsReset, "Number of functions reset");
  24. namespace {
  25. class ResetMachineFunction : public MachineFunctionPass {
  26. /// Tells whether or not this pass should emit a fallback
  27. /// diagnostic when it resets a function.
  28. bool EmitFallbackDiag;
  29. /// Whether we should abort immediately instead of resetting the function.
  30. bool AbortOnFailedISel;
  31. public:
  32. static char ID; // Pass identification, replacement for typeid
  33. ResetMachineFunction(bool EmitFallbackDiag = false,
  34. bool AbortOnFailedISel = false)
  35. : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
  36. AbortOnFailedISel(AbortOnFailedISel) {}
  37. StringRef getPassName() const override { return "ResetMachineFunction"; }
  38. bool runOnMachineFunction(MachineFunction &MF) override {
  39. if (MF.getProperties().hasProperty(
  40. MachineFunctionProperties::Property::FailedISel)) {
  41. if (AbortOnFailedISel)
  42. report_fatal_error("Instruction selection failed");
  43. DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
  44. ++NumFunctionsReset;
  45. MF.reset();
  46. if (EmitFallbackDiag) {
  47. const Function &F = MF.getFunction();
  48. DiagnosticInfoISelFallback DiagFallback(F);
  49. F.getContext().diagnose(DiagFallback);
  50. }
  51. return true;
  52. }
  53. return false;
  54. }
  55. };
  56. } // end anonymous namespace
  57. char ResetMachineFunction::ID = 0;
  58. INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
  59. "Reset machine function if ISel failed", false, false)
  60. MachineFunctionPass *
  61. llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
  62. bool AbortOnFailedISel = false) {
  63. return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
  64. }