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