LLVM 23.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
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/Analysis.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/PassManager.h"
25
26#define DEBUG_TYPE "mir-strip-debug"
27
28using namespace llvm;
29
30namespace {
31
33 OnlyDebugifiedDefault("mir-strip-debugify-only",
34 cl::desc("Should mir-strip-debug only strip debug "
35 "info from debugified modules by default"),
36 cl::init(true));
37
38bool stripDebugMachineModuleImpl(
39 Module &M, bool OnlyDebugified,
41 if (OnlyDebugified) {
42 NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
43 if (!DebugifyMD) {
44 LLVM_DEBUG(dbgs() << "Not stripping debug info"
45 " (debugify metadata not found)?\n");
46 return false;
47 }
48 }
49
50 bool Changed = false;
51 for (Function &F : M.functions()) {
52 MachineFunction *MaybeMF = GetMF(F);
53 if (!MaybeMF)
54 continue;
55 MachineFunction &MF = *MaybeMF;
56 for (MachineBasicBlock &MBB : MF) {
58 if (MI.isDebugInstr()) {
59 // FIXME: We should remove all of them. However, AArch64 emits an
60 // invalid `DBG_VALUE $lr` with only one operand instead of
61 // the usual three and has a test that depends on it's
62 // preservation. Preserve it for now.
63 if (MI.getNumOperands() > 1) {
64 LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
65 MBB.erase_instr(&MI);
66 Changed |= true;
67 continue;
68 }
69 }
70 if (MI.getDebugLoc()) {
71 LLVM_DEBUG(dbgs() << "Removing location " << MI);
72 MI.setDebugLoc(DebugLoc());
73 Changed |= true;
74 continue;
75 }
76 LLVM_DEBUG(dbgs() << "Keeping " << MI);
77 }
78 }
79 }
80
82
83 return Changed;
84}
85
86struct StripDebugMachineModule : public ModulePass {
87 bool runOnModule(Module &M) override {
89 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
90 return stripDebugMachineModuleImpl(
91 M, OnlyDebugified, [&MMI](Function &F) -> MachineFunction * {
92 return MMI.getMachineFunction(F);
93 });
94 }
95
96 StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
97 StripDebugMachineModule(bool OnlyDebugified)
98 : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
99
100 void getAnalysisUsage(AnalysisUsage &AU) const override {
103 AU.setPreservesCFG();
104 }
105
106 static char ID; // Pass identification.
107
108protected:
109 bool OnlyDebugified;
110};
111char StripDebugMachineModule::ID = 0;
112
113} // end anonymous namespace
114
115INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
116 "Machine Strip Debug Module", false, false)
117INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
118 "Machine Strip Debug Module", false, false)
119
121 return new StripDebugMachineModule(OnlyDebugified);
122}
123
128 const bool Changed = stripDebugMachineModuleImpl(
129 M, OnlyDebugifiedDefault, [&FAM](Function &F) -> MachineFunction * {
130 return &FAM.getResult<MachineFunctionAnalysis>(F).getMF();
131 });
132 if (!Changed)
133 return PreservedAnalyses::all();
134
139 return PA;
140}
MachineBasicBlock & MBB
#define DEBUG_TYPE
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define LLVM_DEBUG(...)
Definition Debug.h:114
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
This analysis create MachineFunction for given Function.
Representation of each machine instruction.
An analysis that produces MachineModuleInfo for a module.
This class contains meta information specific to a module.
LLVM_ABI 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:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A tuple of MDNodes.
Definition Metadata.h:1760
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
An efficient, type-erasing, non-owning reference to a callable.
Changed
Pass manager infrastructure for declaring and invalidating analyses.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI ModulePass * createStripDebugMachineModuleLegacyPass(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:634
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition Debugify.cpp:306
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39