1010// / tests can be debugified without affecting the output MIR.
1111// ===----------------------------------------------------------------------===//
1212
13+ #include " llvm/CodeGen/MachineStripDebug.h"
1314#include " llvm/CodeGen/MachineBasicBlock.h"
1415#include " llvm/CodeGen/MachineFunction.h"
16+ #include " llvm/CodeGen/MachineFunctionAnalysis.h"
1517#include " llvm/CodeGen/MachineModuleInfo.h"
1618#include " llvm/CodeGen/Passes.h"
19+ #include " llvm/IR/Analysis.h"
20+ #include " llvm/IR/Module.h"
21+ #include " llvm/IR/PassManager.h"
1722#include " llvm/InitializePasses.h"
1823#include " llvm/Support/CommandLine.h"
1924#include " llvm/Transforms/Utils/Debugify.h"
2328using namespace llvm ;
2429
2530namespace {
31+
2632cl::opt<bool >
2733 OnlyDebugifiedDefault (" mir-strip-debugify-only" ,
2834 cl::desc (" Should mir-strip-debug only strip debug "
2935 " info from debugified modules by default" ),
3036 cl::init(true ));
3137
32- struct 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- }
38+ bool stripDebugMachineModuleImpl (
39+ Module &M, bool OnlyDebugified,
40+ llvm::function_ref<MachineFunction *(Function &)> GetMF ) {
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 ;
4147 }
48+ }
4249
43- MachineModuleInfo &MMI =
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) {
53- for (MachineInstr &MI : llvm::make_early_inc_range (MBB.instrs ())) {
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_instr (&MI);
62- Changed |= true ;
63- continue ;
64- }
65- }
66- if (MI.getDebugLoc ()) {
67- LLVM_DEBUG (dbgs () << " Removing location " << MI);
68- MI.setDebugLoc (DebugLoc ());
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) {
57+ for (MachineInstr &MI : llvm::make_early_inc_range (MBB.instrs ())) {
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);
6966 Changed |= true ;
7067 continue ;
7168 }
72- LLVM_DEBUG (dbgs () << " Keeping " << MI);
7369 }
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);
7477 }
7578 }
79+ }
80+
81+ Changed |= stripDebugifyMetadata (M);
7682
77- Changed |= stripDebugifyMetadata (M);
83+ return Changed;
84+ }
7885
79- return Changed;
86+ struct StripDebugMachineModule : public ModulePass {
87+ bool runOnModule (Module &M) override {
88+ MachineModuleInfo &MMI =
89+ getAnalysis<MachineModuleInfoWrapperPass>().getMMI ();
90+ return stripDebugMachineModuleImpl (
91+ M, OnlyDebugified, [&MMI](Function &F) -> MachineFunction * {
92+ return MMI.getMachineFunction (F);
93+ });
8094 }
8195
8296 StripDebugMachineModule () : StripDebugMachineModule(OnlyDebugifiedDefault) {}
@@ -103,6 +117,24 @@ INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
103117INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
104118 " Machine Strip Debug Module" , false , false )
105119
106- ModulePass *llvm::createStripDebugMachineModulePass (bool OnlyDebugified) {
120+ ModulePass *llvm::createStripDebugMachineModuleLegacyPass (bool OnlyDebugified) {
107121 return new StripDebugMachineModule (OnlyDebugified);
108122}
123+
124+ PreservedAnalyses StripDebugMachineModulePass::run (Module &M,
125+ ModuleAnalysisManager &AM) {
126+ FunctionAnalysisManager &FAM =
127+ AM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
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+
135+ PreservedAnalyses PA;
136+ PA.preserve <MachineModuleAnalysis>();
137+ PA.preserve <FunctionAnalysisManagerModuleProxy>();
138+ PA.preserveSet <CFGAnalyses>();
139+ return PA;
140+ }
0 commit comments