MachineStripDebug.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/MachineFunctionPass.h"
  13. #include "llvm/CodeGen/MachineModuleInfo.h"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/IR/DebugInfo.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 (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
  47. I != E;) {
  48. if (I->isDebugInstr()) {
  49. // FIXME: We should remove all of them. However, AArch64 emits an
  50. // invalid `DBG_VALUE $lr` with only one operand instead of
  51. // the usual three and has a test that depends on it's
  52. // preservation. Preserve it for now.
  53. if (I->getNumOperands() > 1) {
  54. LLVM_DEBUG(dbgs() << "Removing debug instruction " << *I);
  55. I = MBB.erase(I);
  56. Changed |= true;
  57. continue;
  58. }
  59. }
  60. if (I->getDebugLoc()) {
  61. LLVM_DEBUG(dbgs() << "Removing location " << *I);
  62. I->setDebugLoc(DebugLoc());
  63. Changed |= true;
  64. ++I;
  65. continue;
  66. }
  67. LLVM_DEBUG(dbgs() << "Keeping " << *I);
  68. ++I;
  69. }
  70. }
  71. }
  72. Changed |= stripDebugifyMetadata(M);
  73. return Changed;
  74. }
  75. StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
  76. StripDebugMachineModule(bool OnlyDebugified)
  77. : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
  78. void getAnalysisUsage(AnalysisUsage &AU) const override {
  79. AU.addRequired<MachineModuleInfoWrapperPass>();
  80. AU.addPreserved<MachineModuleInfoWrapperPass>();
  81. AU.setPreservesCFG();
  82. }
  83. static char ID; // Pass identification.
  84. protected:
  85. bool OnlyDebugified;
  86. };
  87. char StripDebugMachineModule::ID = 0;
  88. } // end anonymous namespace
  89. INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
  90. "Machine Strip Debug Module", false, false)
  91. INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
  92. "Machine Strip Debug Module", false, false)
  93. ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
  94. return new StripDebugMachineModule(OnlyDebugified);
  95. }