LLVM  13.0.0git
MachineCheckDebugify.cpp
Go to the documentation of this file.
1 //===- MachineCheckDebugify.cpp - Check 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 checks debug info after mir-debugify (+ pass-to-test). Currently
10 /// it simply checks the integrity of line info in DILocation and
11 /// DILocalVariable which mir-debugifiy generated before.
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/InitializePasses.h"
21 
22 #define DEBUG_TYPE "mir-check-debugify"
23 
24 using namespace llvm;
25 
26 namespace {
27 
28 struct CheckDebugMachineModule : public ModulePass {
29  bool runOnModule(Module &M) override {
30  MachineModuleInfo &MMI =
31  getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
32 
33  NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
34  if (!NMD) {
35  errs() << "WARNING: Please run mir-debugify to generate "
36  "llvm.mir.debugify metadata first.\n";
37  return false;
38  }
39 
40  auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
41  return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
42  ->getZExtValue();
43  };
44  assert(NMD->getNumOperands() == 2 &&
45  "llvm.mir.debugify should have exactly 2 operands!");
46  unsigned NumLines = getDebugifyOperand(0);
47  unsigned NumVars = getDebugifyOperand(1);
48  BitVector MissingLines{NumLines, true};
49  BitVector MissingVars{NumVars, true};
50 
51  for (Function &F : M.functions()) {
53  if (!MF)
54  continue;
55  for (MachineBasicBlock &MBB : *MF) {
56  // Find missing lines.
57  // TODO: Avoid meta instructions other than dbg_val.
58  for (MachineInstr &MI : MBB) {
59  if (MI.isDebugValue())
60  continue;
61  const DebugLoc DL = MI.getDebugLoc();
62  if (DL && DL.getLine() != 0) {
63  MissingLines.reset(DL.getLine() - 1);
64  continue;
65  }
66 
67  if (!DL) {
68  errs() << "WARNING: Instruction with empty DebugLoc in function ";
69  errs() << F.getName() << " --";
70  MI.print(errs());
71  }
72  }
73 
74  // Find missing variables.
75  // TODO: Handle DBG_INSTR_REF which is under an experimental option now.
76  for (MachineInstr &MI : MBB) {
77  if (!MI.isDebugValue())
78  continue;
79  const DILocalVariable *LocalVar = MI.getDebugVariable();
80  unsigned Var = ~0U;
81 
82  (void)to_integer(LocalVar->getName(), Var, 10);
83  assert(Var <= NumVars && "Unexpected name for DILocalVariable");
84  MissingVars.reset(Var - 1);
85  }
86  }
87  }
88 
89  bool Fail = false;
90  for (unsigned Idx : MissingLines.set_bits()) {
91  errs() << "WARNING: Missing line " << Idx + 1 << "\n";
92  Fail = true;
93  }
94 
95  for (unsigned Idx : MissingVars.set_bits()) {
96  errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
97  Fail = true;
98  }
99  errs() << "Machine IR debug info check: ";
100  errs() << (Fail ? "FAIL" : "PASS") << "\n";
101 
102  return false;
103  }
104 
105  CheckDebugMachineModule() : ModulePass(ID) {}
106 
107  void getAnalysisUsage(AnalysisUsage &AU) const override {
110  AU.setPreservesCFG();
111  }
112 
113  static char ID; // Pass identification.
114 };
116 
117 } // end anonymous namespace
118 
119 INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
120  "Machine Check Debug Module", false, false)
121 INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
123 
125  return new CheckDebugMachineModule();
126 }
llvm::createCheckDebugMachineModulePass
ModulePass * createCheckDebugMachineModulePass()
Creates MIR Check Debug pass.
Definition: MachineCheckDebugify.cpp:124
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:102
llvm
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:1386
llvm::NamedMDNode::getNumOperands
unsigned getNumOperands() const
Definition: Metadata.cpp:1114
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:61
llvm::lltok::LocalVar
@ LocalVar
Definition: LLToken.h:471
Fail
#define Fail
Definition: AArch64Disassembler.cpp:249
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE, "Machine Check Debug Module", false, false) INITIALIZE_PASS_END(CheckDebugMachineModule
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
F
#define F(x, y, z)
Definition: MD5.cpp:56
CommandLine.h
llvm::DILocalVariable
Local variable.
Definition: DebugInfoMetadata.h:3050
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::BitVector
Definition: BitVector.h:74
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::Pass::print
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:125
Passes.h
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1102
Check
static bool Check(DecodeStatus &Out, DecodeStatus In)
Definition: AArch64Disassembler.cpp:230
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:279
DebugInfo.h
Debug
bool Debug
Definition: PassBuilderBindings.cpp:51
MachineFunctionPass.h
llvm::NamedMDNode::getOperand
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1118
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:230
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
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
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
DEBUG_TYPE
#define DEBUG_TYPE
Definition: MachineCheckDebugify.cpp:22
Machine
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38