LLVM 17.0.0git
MachineStripDebug.cpp
Go to the documentation of this file.
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
16#include "llvm/CodeGen/Passes.h"
20
21#define DEBUG_TYPE "mir-strip-debug"
22
23using namespace llvm;
24
25namespace {
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
32struct 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
44 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
45
46 bool Changed = false;
47 for (Function &F : M.functions()) {
48 MachineFunction *MaybeMF = MMI.getMachineFunction(F);
49 if (!MaybeMF)
50 continue;
51 MachineFunction &MF = *MaybeMF;
52 for (MachineBasicBlock &MBB : MF) {
54 if (MI.isDebugInstr()) {
55 // FIXME: We should remove all of them. However, AArch64 emits an
56 // invalid `DBG_VALUE $lr` with only one operand instead of
57 // the usual three and has a test that depends on it's
58 // preservation. Preserve it for now.
59 if (MI.getNumOperands() > 1) {
60 LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
61 MBB.erase(&MI);
62 Changed |= true;
63 continue;
64 }
65 }
66 if (MI.getDebugLoc()) {
67 LLVM_DEBUG(dbgs() << "Removing location " << MI);
68 MI.setDebugLoc(DebugLoc());
69 Changed |= true;
70 continue;
71 }
72 LLVM_DEBUG(dbgs() << "Keeping " << MI);
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 {
89 AU.setPreservesCFG();
90 }
91
92 static char ID; // Pass identification.
93
94protected:
95 bool OnlyDebugified;
96};
97char StripDebugMachineModule::ID = 0;
98
99} // end anonymous namespace
100
101INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
102 "Machine Strip Debug Module", false, false)
103INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
104 "Machine Strip Debug Module", false, false)
105
107 return new StripDebugMachineModule(OnlyDebugified);
108}
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define DEBUG_TYPE
bool Debug
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
A debug info location.
Definition: DebugLoc.h:33
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
Representation of each machine instruction.
Definition: MachineInstr.h:68
This class contains meta information specific to a module.
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1587
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createStripDebugMachineModulePass(bool OnlyDebugified)
Creates MIR Strip Debug pass.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:721
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:245
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163