1*af732203SDimitry Andric //===- MachineCheckDebugify.cpp - Check debug info ------------------------===//
2*af732203SDimitry Andric //
3*af732203SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*af732203SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*af732203SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*af732203SDimitry Andric //
7*af732203SDimitry Andric //===----------------------------------------------------------------------===//
8*af732203SDimitry Andric ///
9*af732203SDimitry Andric /// \file This checks debug info after mir-debugify (+ pass-to-test). Currently
10*af732203SDimitry Andric /// it simply checks the integrity of line info in DILocation and
11*af732203SDimitry Andric /// DILocalVariable which mir-debugifiy generated before.
12*af732203SDimitry Andric //===----------------------------------------------------------------------===//
13*af732203SDimitry Andric 
14*af732203SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
15*af732203SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
16*af732203SDimitry Andric #include "llvm/CodeGen/Passes.h"
17*af732203SDimitry Andric #include "llvm/IR/DebugInfo.h"
18*af732203SDimitry Andric #include "llvm/InitializePasses.h"
19*af732203SDimitry Andric #include "llvm/Support/CommandLine.h"
20*af732203SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h"
21*af732203SDimitry Andric 
22*af732203SDimitry Andric #define DEBUG_TYPE "mir-check-debugify"
23*af732203SDimitry Andric 
24*af732203SDimitry Andric using namespace llvm;
25*af732203SDimitry Andric 
26*af732203SDimitry Andric namespace {
27*af732203SDimitry Andric 
28*af732203SDimitry Andric struct CheckDebugMachineModule : public ModulePass {
runOnModule__anon253ac1ea0111::CheckDebugMachineModule29*af732203SDimitry Andric   bool runOnModule(Module &M) override {
30*af732203SDimitry Andric     MachineModuleInfo &MMI =
31*af732203SDimitry Andric         getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
32*af732203SDimitry Andric 
33*af732203SDimitry Andric     NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
34*af732203SDimitry Andric     if (!NMD) {
35*af732203SDimitry Andric       errs() << "WARNING: Please run mir-debugify to generate "
36*af732203SDimitry Andric                 "llvm.mir.debugify metadata first.\n";
37*af732203SDimitry Andric       return false;
38*af732203SDimitry Andric     }
39*af732203SDimitry Andric 
40*af732203SDimitry Andric     auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
41*af732203SDimitry Andric       return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
42*af732203SDimitry Andric           ->getZExtValue();
43*af732203SDimitry Andric     };
44*af732203SDimitry Andric     assert(NMD->getNumOperands() == 2 &&
45*af732203SDimitry Andric            "llvm.mir.debugify should have exactly 2 operands!");
46*af732203SDimitry Andric     unsigned NumLines = getDebugifyOperand(0);
47*af732203SDimitry Andric     unsigned NumVars = getDebugifyOperand(1);
48*af732203SDimitry Andric     BitVector MissingLines{NumLines, true};
49*af732203SDimitry Andric     BitVector MissingVars{NumVars, true};
50*af732203SDimitry Andric 
51*af732203SDimitry Andric     for (Function &F : M.functions()) {
52*af732203SDimitry Andric       MachineFunction *MF = MMI.getMachineFunction(F);
53*af732203SDimitry Andric       if (!MF)
54*af732203SDimitry Andric         continue;
55*af732203SDimitry Andric       for (MachineBasicBlock &MBB : *MF) {
56*af732203SDimitry Andric         // Find missing lines.
57*af732203SDimitry Andric         // TODO: Avoid meta instructions other than dbg_val.
58*af732203SDimitry Andric         for (MachineInstr &MI : MBB) {
59*af732203SDimitry Andric           if (MI.isDebugValue())
60*af732203SDimitry Andric             continue;
61*af732203SDimitry Andric           const DebugLoc DL = MI.getDebugLoc();
62*af732203SDimitry Andric           if (DL && DL.getLine() != 0) {
63*af732203SDimitry Andric             MissingLines.reset(DL.getLine() - 1);
64*af732203SDimitry Andric             continue;
65*af732203SDimitry Andric           }
66*af732203SDimitry Andric 
67*af732203SDimitry Andric           if (!DL) {
68*af732203SDimitry Andric             errs() << "WARNING: Instruction with empty DebugLoc in function ";
69*af732203SDimitry Andric             errs() << F.getName() << " --";
70*af732203SDimitry Andric             MI.print(errs());
71*af732203SDimitry Andric           }
72*af732203SDimitry Andric         }
73*af732203SDimitry Andric 
74*af732203SDimitry Andric         // Find missing variables.
75*af732203SDimitry Andric         // TODO: Handle DBG_INSTR_REF which is under an experimental option now.
76*af732203SDimitry Andric         for (MachineInstr &MI : MBB) {
77*af732203SDimitry Andric           if (!MI.isDebugValue())
78*af732203SDimitry Andric             continue;
79*af732203SDimitry Andric           const DILocalVariable *LocalVar = MI.getDebugVariable();
80*af732203SDimitry Andric           unsigned Var = ~0U;
81*af732203SDimitry Andric 
82*af732203SDimitry Andric           (void)to_integer(LocalVar->getName(), Var, 10);
83*af732203SDimitry Andric           assert(Var <= NumVars && "Unexpected name for DILocalVariable");
84*af732203SDimitry Andric           MissingVars.reset(Var - 1);
85*af732203SDimitry Andric         }
86*af732203SDimitry Andric       }
87*af732203SDimitry Andric     }
88*af732203SDimitry Andric 
89*af732203SDimitry Andric     bool Fail = false;
90*af732203SDimitry Andric     for (unsigned Idx : MissingLines.set_bits()) {
91*af732203SDimitry Andric       errs() << "WARNING: Missing line " << Idx + 1 << "\n";
92*af732203SDimitry Andric       Fail = true;
93*af732203SDimitry Andric     }
94*af732203SDimitry Andric 
95*af732203SDimitry Andric     for (unsigned Idx : MissingVars.set_bits()) {
96*af732203SDimitry Andric       errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
97*af732203SDimitry Andric       Fail = true;
98*af732203SDimitry Andric     }
99*af732203SDimitry Andric     errs() << "Machine IR debug info check: ";
100*af732203SDimitry Andric     errs() << (Fail ? "FAIL" : "PASS") << "\n";
101*af732203SDimitry Andric 
102*af732203SDimitry Andric     return false;
103*af732203SDimitry Andric   }
104*af732203SDimitry Andric 
CheckDebugMachineModule__anon253ac1ea0111::CheckDebugMachineModule105*af732203SDimitry Andric   CheckDebugMachineModule() : ModulePass(ID) {}
106*af732203SDimitry Andric 
getAnalysisUsage__anon253ac1ea0111::CheckDebugMachineModule107*af732203SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
108*af732203SDimitry Andric     AU.addRequired<MachineModuleInfoWrapperPass>();
109*af732203SDimitry Andric     AU.addPreserved<MachineModuleInfoWrapperPass>();
110*af732203SDimitry Andric     AU.setPreservesCFG();
111*af732203SDimitry Andric   }
112*af732203SDimitry Andric 
113*af732203SDimitry Andric   static char ID; // Pass identification.
114*af732203SDimitry Andric };
115*af732203SDimitry Andric char CheckDebugMachineModule::ID = 0;
116*af732203SDimitry Andric 
117*af732203SDimitry Andric } // end anonymous namespace
118*af732203SDimitry Andric 
119*af732203SDimitry Andric INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
120*af732203SDimitry Andric                       "Machine Check Debug Module", false, false)
121*af732203SDimitry Andric INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
122*af732203SDimitry Andric                     "Machine Check Debug Module", false, false)
123*af732203SDimitry Andric 
createCheckDebugMachineModulePass()124*af732203SDimitry Andric ModulePass *llvm::createCheckDebugMachineModulePass() {
125*af732203SDimitry Andric   return new CheckDebugMachineModule();
126*af732203SDimitry Andric }
127