MachineStripDebug.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- MachineStripDebug.cpp - Strip debug info ---------------------------===//
  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. ///
  9. /// \file This removes debug info from everything. It can be used to ensure
  10. /// tests can be debugified without affecting the output MIR.
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineBasicBlock.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/CodeGen/MachineModuleInfo.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Transforms/Utils/Debugify.h"
  19. #define DEBUG_TYPE "mir-strip-debug"
  20. using namespace llvm;
  21. namespace {
  22. cl::opt<bool>
  23. OnlyDebugifiedDefault("mir-strip-debugify-only",
  24. cl::desc("Should mir-strip-debug only strip debug "
  25. "info from debugified modules by default"),
  26. cl::init(true));
  27. struct StripDebugMachineModule : public ModulePass {
  28. bool runOnModule(Module &M) override {
  29. if (OnlyDebugified) {
  30. NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
  31. if (!DebugifyMD) {
  32. LLVM_DEBUG(dbgs() << "Not stripping debug info"
  33. " (debugify metadata not found)?\n");
  34. return false;
  35. }
  36. }
  37. MachineModuleInfo &MMI =
  38. getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
  39. bool Changed = false;
  40. for (Function &F : M.functions()) {
  41. MachineFunction *MaybeMF = MMI.getMachineFunction(F);
  42. if (!MaybeMF)
  43. continue;
  44. MachineFunction &MF = *MaybeMF;
  45. for (MachineBasicBlock &MBB : MF) {
  46. for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
  47. if (MI.isDebugInstr()) {
  48. // FIXME: We should remove all of them. However, AArch64 emits an
  49. // invalid `DBG_VALUE $lr` with only one operand instead of
  50. // the usual three and has a test that depends on it's
  51. // preservation. Preserve it for now.
  52. if (MI.getNumOperands() > 1) {
  53. LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
  54. MBB.erase(&MI);
  55. Changed |= true;
  56. continue;
  57. }
  58. }
  59. if (MI.getDebugLoc()) {
  60. LLVM_DEBUG(dbgs() << "Removing location " << MI);
  61. MI.setDebugLoc(DebugLoc());
  62. Changed |= true;
  63. continue;
  64. }
  65. LLVM_DEBUG(dbgs() << "Keeping " << MI);
  66. }
  67. }
  68. }
  69. Changed |= stripDebugifyMetadata(M);
  70. return Changed;
  71. }
  72. StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
  73. StripDebugMachineModule(bool OnlyDebugified)
  74. : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
  75. void getAnalysisUsage(AnalysisUsage &AU) const override {
  76. AU.addRequired<MachineModuleInfoWrapperPass>();
  77. AU.addPreserved<MachineModuleInfoWrapperPass>();
  78. AU.setPreservesCFG();
  79. }
  80. static char ID; // Pass identification.
  81. protected:
  82. bool OnlyDebugified;
  83. };
  84. char StripDebugMachineModule::ID = 0;
  85. } // end anonymous namespace
  86. INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
  87. "Machine Strip Debug Module", false, false)
  88. INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
  89. "Machine Strip Debug Module", false, false)
  90. ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
  91. return new StripDebugMachineModule(OnlyDebugified);
  92. }