Bug Summary

File:build/source/llvm/lib/CodeGen/MachineDebugify.cpp
Warning:line 112, column 16
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MachineDebugify.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen -I /build/source/llvm/lib/CodeGen -I include -I /build/source/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1683717183 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-05-10-133810-16478-1 -x c++ /build/source/llvm/lib/CodeGen/MachineDebugify.cpp
1//===- MachineDebugify.cpp - Attach synthetic debug info to everything ----===//
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 pass attaches synthetic debug info to everything. It can be used
10/// to create targeted tests for debug info preservation, or test for CodeGen
11/// differences with vs. without debug info.
12///
13/// This isn't intended to have feature parity with Debugify.
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/TargetInstrInfo.h"
23#include "llvm/CodeGen/TargetSubtargetInfo.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/InitializePasses.h"
26#include "llvm/Transforms/Utils/Debugify.h"
27
28#define DEBUG_TYPE"mir-debugify" "mir-debugify"
29
30using namespace llvm;
31
32namespace {
33bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
34 DIBuilder &DIB, Function &F) {
35 MachineFunction *MaybeMF = MMI.getMachineFunction(F);
36 if (!MaybeMF)
2
Assuming 'MaybeMF' is non-null
3
Taking false branch
37 return false;
38 MachineFunction &MF = *MaybeMF;
39 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
40
41 DISubprogram *SP = F.getSubprogram();
42 assert(SP && "IR Debugify just created it?")(static_cast <bool> (SP && "IR Debugify just created it?"
) ? void (0) : __assert_fail ("SP && \"IR Debugify just created it?\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 42, __extension__ __PRETTY_FUNCTION__
))
;
4
Assuming 'SP' is non-null
5
'?' condition is true
43
44 Module &M = *F.getParent();
45 LLVMContext &Ctx = M.getContext();
46
47 unsigned NextLine = SP->getLine();
48 for (MachineBasicBlock &MBB : MF) {
49 for (MachineInstr &MI : MBB) {
50 // This will likely emit line numbers beyond the end of the imagined
51 // source function and into subsequent ones. We don't do anything about
52 // that as it doesn't really matter to the compiler where the line is in
53 // the imaginary source code.
54 MI.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
55 }
56 }
57
58 // Find local variables defined by debugify. No attempt is made to match up
59 // MIR-level regs to the 'correct' IR-level variables: there isn't a simple
60 // way to do that, and it isn't necessary to find interesting CodeGen bugs.
61 // Instead, simply keep track of one variable per line. Later, we can insert
62 // DBG_VALUE insts that point to these local variables. Emitting DBG_VALUEs
63 // which cover a wide range of lines can help stress the debug info passes:
64 // if we can't do that, fall back to using the local variable which precedes
65 // all the others.
66 Function *DbgValF = M.getFunction("llvm.dbg.value");
67 DbgValueInst *EarliestDVI = nullptr;
6
'EarliestDVI' initialized to a null pointer value
68 DenseMap<unsigned, DILocalVariable *> Line2Var;
69 DIExpression *Expr = nullptr;
70 if (DbgValF) {
7
Assuming 'DbgValF' is null
8
Taking false branch
71 for (const Use &U : DbgValF->uses()) {
72 auto *DVI = dyn_cast<DbgValueInst>(U.getUser());
73 if (!DVI || DVI->getFunction() != &F)
74 continue;
75 unsigned Line = DVI->getDebugLoc().getLine();
76 assert(Line != 0 && "debugify should not insert line 0 locations")(static_cast <bool> (Line != 0 && "debugify should not insert line 0 locations"
) ? void (0) : __assert_fail ("Line != 0 && \"debugify should not insert line 0 locations\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 76, __extension__ __PRETTY_FUNCTION__
))
;
77 Line2Var[Line] = DVI->getVariable();
78 if (!EarliestDVI || Line < EarliestDVI->getDebugLoc().getLine())
79 EarliestDVI = DVI;
80 Expr = DVI->getExpression();
81 }
82 }
83 if (Line2Var.empty())
9
Assuming the condition is false
10
Taking false branch
84 return true;
85
86 // Now, try to insert a DBG_VALUE instruction after each real instruction.
87 // Do this by introducing debug uses of each register definition. If that is
88 // not possible (e.g. we have a phi or a meta instruction), emit a constant.
89 uint64_t NextImm = 0;
90 SmallSet<DILocalVariable *, 16> VarSet;
91 const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
92 for (MachineBasicBlock &MBB : MF) {
93 MachineBasicBlock::iterator FirstNonPHIIt = MBB.getFirstNonPHI();
94 for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
11
Loop condition is true. Entering loop body
95 MachineInstr &MI = *I;
96 ++I;
97
98 // `I` may point to a DBG_VALUE created in the previous loop iteration.
99 if (MI.isDebugInstr())
12
Taking false branch
100 continue;
101
102 // It's not allowed to insert DBG_VALUEs after a terminator.
103 if (MI.isTerminator())
13
Assuming the condition is false
104 continue;
105
106 // Find a suitable insertion point for the DBG_VALUE.
107 auto InsertBeforeIt = MI.isPHI() ? FirstNonPHIIt : I;
14
Taking false branch
15
'?' condition is true
108
109 // Find a suitable local variable for the DBG_VALUE.
110 unsigned Line = MI.getDebugLoc().getLine();
111 if (!Line2Var.count(Line))
16
Assuming the condition is true
17
Taking true branch
112 Line = EarliestDVI->getDebugLoc().getLine();
18
Called C++ object pointer is null
113 DILocalVariable *LocalVar = Line2Var[Line];
114 assert(LocalVar && "No variable for current line?")(static_cast <bool> (LocalVar && "No variable for current line?"
) ? void (0) : __assert_fail ("LocalVar && \"No variable for current line?\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 114, __extension__ __PRETTY_FUNCTION__
))
;
115 VarSet.insert(LocalVar);
116
117 // Emit DBG_VALUEs for register definitions.
118 SmallVector<MachineOperand *, 4> RegDefs;
119 for (MachineOperand &MO : MI.operands())
120 if (MO.isReg() && MO.isDef() && MO.getReg())
121 RegDefs.push_back(&MO);
122 for (MachineOperand *MO : RegDefs)
123 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
124 /*IsIndirect=*/false, *MO, LocalVar, Expr);
125
126 // OK, failing that, emit a constant DBG_VALUE.
127 if (RegDefs.empty()) {
128 auto ImmOp = MachineOperand::CreateImm(NextImm++);
129 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
130 /*IsIndirect=*/false, ImmOp, LocalVar, Expr);
131 }
132 }
133 }
134
135 // Here we save the number of lines and variables into "llvm.mir.debugify".
136 // It is useful for mir-check-debugify.
137 NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
138 IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
139 if (!NMD) {
140 NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
141 auto addDebugifyOperand = [&](unsigned N) {
142 NMD->addOperand(MDNode::get(
143 Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
144 };
145 // Add number of lines.
146 addDebugifyOperand(NextLine - 1);
147 // Add number of variables.
148 addDebugifyOperand(VarSet.size());
149 } else {
150 assert(NMD->getNumOperands() == 2 &&(static_cast <bool> (NMD->getNumOperands() == 2 &&
"llvm.mir.debugify should have exactly 2 operands!") ? void (
0) : __assert_fail ("NMD->getNumOperands() == 2 && \"llvm.mir.debugify should have exactly 2 operands!\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 151, __extension__ __PRETTY_FUNCTION__
))
151 "llvm.mir.debugify should have exactly 2 operands!")(static_cast <bool> (NMD->getNumOperands() == 2 &&
"llvm.mir.debugify should have exactly 2 operands!") ? void (
0) : __assert_fail ("NMD->getNumOperands() == 2 && \"llvm.mir.debugify should have exactly 2 operands!\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 151, __extension__ __PRETTY_FUNCTION__
))
;
152 auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
153 NMD->setOperand(Idx, MDNode::get(Ctx, ValueAsMetadata::getConstant(
154 ConstantInt::get(Int32Ty, N))));
155 };
156 auto getDebugifyOperand = [&](unsigned Idx) {
157 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
158 ->getZExtValue();
159 };
160 // Set number of lines.
161 setDebugifyOperand(0, NextLine - 1);
162 // Set number of variables.
163 auto OldNumVars = getDebugifyOperand(1);
164 setDebugifyOperand(1, OldNumVars + VarSet.size());
165 }
166
167 return true;
168}
169
170/// ModulePass for attaching synthetic debug info to everything, used with the
171/// legacy module pass manager.
172struct DebugifyMachineModule : public ModulePass {
173 bool runOnModule(Module &M) override {
174 // We will insert new debugify metadata, so erasing the old one.
175 assert(!M.getNamedMetadata("llvm.mir.debugify") &&(static_cast <bool> (!M.getNamedMetadata("llvm.mir.debugify"
) && "llvm.mir.debugify metadata already exists! Strip it first"
) ? void (0) : __assert_fail ("!M.getNamedMetadata(\"llvm.mir.debugify\") && \"llvm.mir.debugify metadata already exists! Strip it first\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 176, __extension__ __PRETTY_FUNCTION__
))
176 "llvm.mir.debugify metadata already exists! Strip it first")(static_cast <bool> (!M.getNamedMetadata("llvm.mir.debugify"
) && "llvm.mir.debugify metadata already exists! Strip it first"
) ? void (0) : __assert_fail ("!M.getNamedMetadata(\"llvm.mir.debugify\") && \"llvm.mir.debugify metadata already exists! Strip it first\""
, "llvm/lib/CodeGen/MachineDebugify.cpp", 176, __extension__ __PRETTY_FUNCTION__
))
;
177 MachineModuleInfo &MMI =
178 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
179 return applyDebugifyMetadata(
180 M, M.functions(),
181 "ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
182 return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
1
Calling 'applyDebugifyMetadataToMachineFunction'
183 });
184 }
185
186 DebugifyMachineModule() : ModulePass(ID) {}
187
188 void getAnalysisUsage(AnalysisUsage &AU) const override {
189 AU.addRequired<MachineModuleInfoWrapperPass>();
190 AU.addPreserved<MachineModuleInfoWrapperPass>();
191 AU.setPreservesCFG();
192 }
193
194 static char ID; // Pass identification.
195};
196char DebugifyMachineModule::ID = 0;
197
198} // end anonymous namespace
199
200INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE,static void *initializeDebugifyMachineModulePassOnce(PassRegistry
&Registry) {
201 "Machine Debugify Module", false, false)static void *initializeDebugifyMachineModulePassOnce(PassRegistry
&Registry) {
202INITIALIZE_PASS_END(DebugifyMachineModule, DEBUG_TYPE,PassInfo *PI = new PassInfo( "Machine Debugify Module", "mir-debugify"
, &DebugifyMachineModule::ID, PassInfo::NormalCtor_t(callDefaultCtor
<DebugifyMachineModule>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeDebugifyMachineModulePassFlag
; void llvm::initializeDebugifyMachineModulePass(PassRegistry
&Registry) { llvm::call_once(InitializeDebugifyMachineModulePassFlag
, initializeDebugifyMachineModulePassOnce, std::ref(Registry)
); }
203 "Machine Debugify Module", false, false)PassInfo *PI = new PassInfo( "Machine Debugify Module", "mir-debugify"
, &DebugifyMachineModule::ID, PassInfo::NormalCtor_t(callDefaultCtor
<DebugifyMachineModule>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeDebugifyMachineModulePassFlag
; void llvm::initializeDebugifyMachineModulePass(PassRegistry
&Registry) { llvm::call_once(InitializeDebugifyMachineModulePassFlag
, initializeDebugifyMachineModulePassOnce, std::ref(Registry)
); }
204
205ModulePass *llvm::createDebugifyMachineModulePass() {
206 return new DebugifyMachineModule();
207}