LLVM  14.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 
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/InitializePasses.h"
20 
21 #define DEBUG_TYPE "mir-strip-debug"
22 
23 using namespace llvm;
24 
25 namespace {
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 
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  }
41  }
42 
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) {
54  I != E;) {
55  if (I->isDebugInstr()) {
56  // FIXME: We should remove all of them. However, AArch64 emits an
57  // invalid `DBG_VALUE $lr` with only one operand instead of
58  // the usual three and has a test that depends on it's
59  // preservation. Preserve it for now.
60  if (I->getNumOperands() > 1) {
61  LLVM_DEBUG(dbgs() << "Removing debug instruction " << *I);
62  I = MBB.erase(I);
63  Changed |= true;
64  continue;
65  }
66  }
67  if (I->getDebugLoc()) {
68  LLVM_DEBUG(dbgs() << "Removing location " << *I);
69  I->setDebugLoc(DebugLoc());
70  Changed |= true;
71  ++I;
72  continue;
73  }
74  LLVM_DEBUG(dbgs() << "Keeping " << *I);
75  ++I;
76  }
77  }
78  }
79 
80  Changed |= stripDebugifyMetadata(M);
81 
82  return Changed;
83  }
84 
85  StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
86  StripDebugMachineModule(bool OnlyDebugified)
87  : ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
88 
89  void getAnalysisUsage(AnalysisUsage &AU) const override {
92  AU.setPreservesCFG();
93  }
94 
95  static char ID; // Pass identification.
96 
97 protected:
98  bool OnlyDebugified;
99 };
101 
102 } // end anonymous namespace
103 
104 INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
105  "Machine Strip Debug Module", false, false)
106 INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
108 
110  return new StripDebugMachineModule(OnlyDebugified);
111 }
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::NamedMDNode
A tuple of MDNodes.
Definition: Metadata.h:1421
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
Debugify.h
llvm::Function
Definition: Function.h:62
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MachineBasicBlock::erase
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
Definition: MachineBasicBlock.cpp:1304
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:142
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:78
llvm::stripDebugifyMetadata
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:239
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
Passes.h
llvm::cl::opt< bool >
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:279
DebugInfo.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
Debug
bool Debug
Definition: PassBuilderBindings.cpp:51
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
DEBUG_TYPE
#define DEBUG_TYPE
Definition: MachineStripDebug.cpp:21
MachineFunctionPass.h
MachineModuleInfo.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::createStripDebugMachineModulePass
ModulePass * createStripDebugMachineModulePass(bool OnlyDebugified)
Creates MIR Strip Debug pass.
Definition: MachineStripDebug.cpp:109
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineModuleInfo::getMachineFunction
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
Definition: MachineModuleInfo.cpp:286
Machine
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:366
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE, "Machine Strip Debug Module", false, false) INITIALIZE_PASS_END(StripDebugMachineModule
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::cl::desc
Definition: CommandLine.h:412
llvm::MachineInstrBundleIterator< MachineInstr >
InitializePasses.h
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37