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