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