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"
18*dfca98d6SDaniel Sanders #include "llvm/Support/CommandLine.h"
19a79b2fc4SDaniel Sanders 
20a79b2fc4SDaniel Sanders #define DEBUG_TYPE "mir-strip-debug"
21a79b2fc4SDaniel Sanders 
22a79b2fc4SDaniel Sanders using namespace llvm;
23a79b2fc4SDaniel Sanders 
24a79b2fc4SDaniel Sanders namespace {
25*dfca98d6SDaniel Sanders cl::opt<bool>
26*dfca98d6SDaniel Sanders     OnlyDebugifiedDefault("mir-strip-debugify-only",
27*dfca98d6SDaniel Sanders                           cl::desc("Should mir-strip-debug only strip debug "
28*dfca98d6SDaniel Sanders                                    "info from debugified modules by default"),
29*dfca98d6SDaniel Sanders                           cl::init(true));
30a79b2fc4SDaniel Sanders 
31a79b2fc4SDaniel Sanders struct StripDebugMachineModule : public ModulePass {
32a79b2fc4SDaniel Sanders   bool runOnModule(Module &M) override {
33*dfca98d6SDaniel Sanders     if (OnlyDebugified) {
34*dfca98d6SDaniel Sanders       NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
35*dfca98d6SDaniel Sanders       if (!DebugifyMD) {
36*dfca98d6SDaniel Sanders         LLVM_DEBUG(dbgs() << "Not stripping debug info"
37*dfca98d6SDaniel Sanders                              " (debugify metadata not found)?\n");
38*dfca98d6SDaniel Sanders         return false;
39*dfca98d6SDaniel Sanders       }
40*dfca98d6SDaniel Sanders     }
41*dfca98d6SDaniel Sanders 
42a79b2fc4SDaniel Sanders     MachineModuleInfo &MMI =
43a79b2fc4SDaniel Sanders         getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
44a79b2fc4SDaniel Sanders 
45a79b2fc4SDaniel Sanders     bool Changed = false;
46a79b2fc4SDaniel Sanders     for (Function &F : M.functions()) {
47a79b2fc4SDaniel Sanders       MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
48a79b2fc4SDaniel Sanders       for (MachineBasicBlock &MBB : MF) {
49a79b2fc4SDaniel Sanders         for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
50a79b2fc4SDaniel Sanders              I != E;) {
51a79b2fc4SDaniel Sanders           if (I->isDebugInstr()) {
52a79b2fc4SDaniel Sanders             // FIXME: We should remove all of them. However, AArch64 emits an
53a79b2fc4SDaniel Sanders             //        invalid `DBG_VALUE $lr` with only one operand instead of
54a79b2fc4SDaniel Sanders             //        the usual three and has a test that depends on it's
55a79b2fc4SDaniel Sanders             //        preservation. Preserve it for now.
56a79b2fc4SDaniel Sanders             if (I->getNumOperands() > 1) {
57a79b2fc4SDaniel Sanders               LLVM_DEBUG(dbgs() << "Removing debug instruction " << *I);
58a79b2fc4SDaniel Sanders               I = MBB.erase(I);
59a79b2fc4SDaniel Sanders               Changed |= true;
60a79b2fc4SDaniel Sanders               continue;
61a79b2fc4SDaniel Sanders             }
62a79b2fc4SDaniel Sanders           }
63a79b2fc4SDaniel Sanders           if (I->getDebugLoc()) {
64a79b2fc4SDaniel Sanders             LLVM_DEBUG(dbgs() << "Removing location " << *I);
65a79b2fc4SDaniel Sanders             I->setDebugLoc(DebugLoc());
66a79b2fc4SDaniel Sanders             Changed |= true;
67a79b2fc4SDaniel Sanders             ++I;
68a79b2fc4SDaniel Sanders             continue;
69a79b2fc4SDaniel Sanders           }
70a79b2fc4SDaniel Sanders           LLVM_DEBUG(dbgs() << "Keeping " << *I);
71a79b2fc4SDaniel Sanders           ++I;
72a79b2fc4SDaniel Sanders         }
73a79b2fc4SDaniel Sanders       }
74a79b2fc4SDaniel Sanders     }
75a79b2fc4SDaniel Sanders 
76a79b2fc4SDaniel Sanders     Changed |= StripDebugInfo(M);
77a79b2fc4SDaniel Sanders 
78a79b2fc4SDaniel Sanders     NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
79a79b2fc4SDaniel Sanders     if (NMD) {
80a79b2fc4SDaniel Sanders       NMD->eraseFromParent();
81a79b2fc4SDaniel Sanders       Changed |= true;
82a79b2fc4SDaniel Sanders     }
83a79b2fc4SDaniel Sanders 
84a79b2fc4SDaniel Sanders     NMD = M.getModuleFlagsMetadata();
85a79b2fc4SDaniel Sanders     if (NMD) {
86a79b2fc4SDaniel Sanders       // There must be an easier way to remove an operand from a NamedMDNode.
87a79b2fc4SDaniel Sanders       SmallVector<MDNode *, 4> Flags;
88a79b2fc4SDaniel Sanders       for (MDNode *Flag : NMD->operands())
89a79b2fc4SDaniel Sanders         Flags.push_back(Flag);
90a79b2fc4SDaniel Sanders       NMD->clearOperands();
91a79b2fc4SDaniel Sanders       for (MDNode *Flag : Flags) {
92a79b2fc4SDaniel Sanders         MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1));
93a79b2fc4SDaniel Sanders         if (Key->getString() == "Debug Info Version") {
94a79b2fc4SDaniel Sanders           Changed |= true;
95a79b2fc4SDaniel Sanders           continue;
96a79b2fc4SDaniel Sanders         }
97a79b2fc4SDaniel Sanders         NMD->addOperand(Flag);
98a79b2fc4SDaniel Sanders       }
99a79b2fc4SDaniel Sanders       // If we left it empty we might as well remove it.
100a79b2fc4SDaniel Sanders       if (NMD->getNumOperands() == 0)
101a79b2fc4SDaniel Sanders         NMD->eraseFromParent();
102a79b2fc4SDaniel Sanders     }
103a79b2fc4SDaniel Sanders 
104a79b2fc4SDaniel Sanders     return Changed;
105a79b2fc4SDaniel Sanders   }
106a79b2fc4SDaniel Sanders 
107*dfca98d6SDaniel Sanders   StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
108*dfca98d6SDaniel Sanders   StripDebugMachineModule(bool OnlyDebugified)
109*dfca98d6SDaniel Sanders       : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
110a79b2fc4SDaniel Sanders 
111a79b2fc4SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
112a79b2fc4SDaniel Sanders     AU.addRequired<MachineModuleInfoWrapperPass>();
113a79b2fc4SDaniel Sanders     AU.addPreserved<MachineModuleInfoWrapperPass>();
114a79b2fc4SDaniel Sanders   }
115a79b2fc4SDaniel Sanders 
116a79b2fc4SDaniel Sanders   static char ID; // Pass identification.
117*dfca98d6SDaniel Sanders 
118*dfca98d6SDaniel Sanders protected:
119*dfca98d6SDaniel Sanders   bool OnlyDebugified;
120a79b2fc4SDaniel Sanders };
121a79b2fc4SDaniel Sanders char StripDebugMachineModule::ID = 0;
122a79b2fc4SDaniel Sanders 
123a79b2fc4SDaniel Sanders } // end anonymous namespace
124a79b2fc4SDaniel Sanders 
125a79b2fc4SDaniel Sanders INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
126a79b2fc4SDaniel Sanders                       "Machine Strip Debug Module", false, false)
127a79b2fc4SDaniel Sanders INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
128a79b2fc4SDaniel Sanders                     "Machine Strip Debug Module", false, false)
129a79b2fc4SDaniel Sanders 
130*dfca98d6SDaniel Sanders ModulePass *createStripDebugMachineModulePass(bool OnlyDebugified) {
131*dfca98d6SDaniel Sanders   return new StripDebugMachineModule(OnlyDebugified);
132a79b2fc4SDaniel Sanders }
133