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
13*989f1c72Sserge-sans-paille #include "llvm/CodeGen/MachineBasicBlock.h"
14*989f1c72Sserge-sans-paille #include "llvm/CodeGen/MachineFunction.h"
15a79b2fc4SDaniel Sanders #include "llvm/CodeGen/MachineModuleInfo.h"
16a79b2fc4SDaniel Sanders #include "llvm/CodeGen/Passes.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 {
runOnModule__anon09a4cf720111::StripDebugMachineModule33a79b2fc4SDaniel 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()) {
4814ad8dc0SDaniel Sanders MachineFunction *MaybeMF = MMI.getMachineFunction(F);
4914ad8dc0SDaniel Sanders if (!MaybeMF)
5014ad8dc0SDaniel Sanders continue;
5114ad8dc0SDaniel Sanders MachineFunction &MF = *MaybeMF;
52a79b2fc4SDaniel Sanders for (MachineBasicBlock &MBB : MF) {
537f00806aSKazu Hirata for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
547f00806aSKazu Hirata if (MI.isDebugInstr()) {
55a79b2fc4SDaniel Sanders // FIXME: We should remove all of them. However, AArch64 emits an
56a79b2fc4SDaniel Sanders // invalid `DBG_VALUE $lr` with only one operand instead of
57a79b2fc4SDaniel Sanders // the usual three and has a test that depends on it's
58a79b2fc4SDaniel Sanders // preservation. Preserve it for now.
597f00806aSKazu Hirata if (MI.getNumOperands() > 1) {
607f00806aSKazu Hirata LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
617f00806aSKazu Hirata MBB.erase(&MI);
62a79b2fc4SDaniel Sanders Changed |= true;
63a79b2fc4SDaniel Sanders continue;
64a79b2fc4SDaniel Sanders }
65a79b2fc4SDaniel Sanders }
667f00806aSKazu Hirata if (MI.getDebugLoc()) {
677f00806aSKazu Hirata LLVM_DEBUG(dbgs() << "Removing location " << MI);
687f00806aSKazu Hirata MI.setDebugLoc(DebugLoc());
69a79b2fc4SDaniel Sanders Changed |= true;
70a79b2fc4SDaniel Sanders continue;
71a79b2fc4SDaniel Sanders }
727f00806aSKazu Hirata LLVM_DEBUG(dbgs() << "Keeping " << MI);
73a79b2fc4SDaniel Sanders }
74a79b2fc4SDaniel Sanders }
75a79b2fc4SDaniel Sanders }
76a79b2fc4SDaniel Sanders
77122a6bfbSVedant Kumar Changed |= stripDebugifyMetadata(M);
78a79b2fc4SDaniel Sanders
79a79b2fc4SDaniel Sanders return Changed;
80a79b2fc4SDaniel Sanders }
81a79b2fc4SDaniel Sanders
StripDebugMachineModule__anon09a4cf720111::StripDebugMachineModule82dfca98d6SDaniel Sanders StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
StripDebugMachineModule__anon09a4cf720111::StripDebugMachineModule83dfca98d6SDaniel Sanders StripDebugMachineModule(bool OnlyDebugified)
84dfca98d6SDaniel Sanders : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
85a79b2fc4SDaniel Sanders
getAnalysisUsage__anon09a4cf720111::StripDebugMachineModule86a79b2fc4SDaniel 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
createStripDebugMachineModulePass(bool OnlyDebugified)106f71350f0SDaniel Sanders ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
107dfca98d6SDaniel Sanders return new StripDebugMachineModule(OnlyDebugified);
108a79b2fc4SDaniel Sanders }
109