Bug Summary

File:llvm/lib/CodeGen/MachineLoopInfo.cpp
Warning:line 144, column 33
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 MachineLoopInfo.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/lib/CodeGen -I include -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/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-14/lib/clang/14.0.0/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/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -O3 -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 -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -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-2022-01-19-134126-35450-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/lib/CodeGen/MachineLoopInfo.cpp
1//===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
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// This file defines the MachineLoopInfo class that is used to identify natural
10// loops and determine the loop depth of various nodes of the CFG. Note that
11// the loops identified may actually be several natural loops that share the
12// same header node... not just a single natural loop.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/MachineLoopInfo.h"
17#include "llvm/Analysis/LoopInfoImpl.h"
18#include "llvm/CodeGen/MachineDominators.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/Passes.h"
21#include "llvm/CodeGen/TargetInstrInfo.h"
22#include "llvm/CodeGen/TargetSubtargetInfo.h"
23#include "llvm/Config/llvm-config.h"
24#include "llvm/InitializePasses.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
27
28using namespace llvm;
29
30// Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
31template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
32template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
33
34char MachineLoopInfo::ID = 0;
35MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
36 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
37}
38INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",static void *initializeMachineLoopInfoPassOnce(PassRegistry &
Registry) {
39 "Machine Natural Loop Construction", true, true)static void *initializeMachineLoopInfoPassOnce(PassRegistry &
Registry) {
40INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)initializeMachineDominatorTreePass(Registry);
41INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",PassInfo *PI = new PassInfo( "Machine Natural Loop Construction"
, "machine-loops", &MachineLoopInfo::ID, PassInfo::NormalCtor_t
(callDefaultCtor<MachineLoopInfo>), true, true); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeMachineLoopInfoPassFlag; void llvm::initializeMachineLoopInfoPass
(PassRegistry &Registry) { llvm::call_once(InitializeMachineLoopInfoPassFlag
, initializeMachineLoopInfoPassOnce, std::ref(Registry)); }
42 "Machine Natural Loop Construction", true, true)PassInfo *PI = new PassInfo( "Machine Natural Loop Construction"
, "machine-loops", &MachineLoopInfo::ID, PassInfo::NormalCtor_t
(callDefaultCtor<MachineLoopInfo>), true, true); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeMachineLoopInfoPassFlag; void llvm::initializeMachineLoopInfoPass
(PassRegistry &Registry) { llvm::call_once(InitializeMachineLoopInfoPassFlag
, initializeMachineLoopInfoPassOnce, std::ref(Registry)); }
43
44char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
45
46bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
47 calculate(getAnalysis<MachineDominatorTree>());
48 return false;
49}
50
51void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
52 releaseMemory();
53 LI.analyze(MDT.getBase());
54}
55
56void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesAll();
58 AU.addRequired<MachineDominatorTree>();
59 MachineFunctionPass::getAnalysisUsage(AU);
60}
61
62MachineBasicBlock *MachineLoop::getTopBlock() {
63 MachineBasicBlock *TopMBB = getHeader();
64 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
65 if (TopMBB->getIterator() != Begin) {
66 MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
67 while (contains(PriorMBB)) {
68 TopMBB = PriorMBB;
69 if (TopMBB->getIterator() == Begin)
70 break;
71 PriorMBB = &*std::prev(TopMBB->getIterator());
72 }
73 }
74 return TopMBB;
75}
76
77MachineBasicBlock *MachineLoop::getBottomBlock() {
78 MachineBasicBlock *BotMBB = getHeader();
79 MachineFunction::iterator End = BotMBB->getParent()->end();
80 if (BotMBB->getIterator() != std::prev(End)) {
81 MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
82 while (contains(NextMBB)) {
83 BotMBB = NextMBB;
84 if (BotMBB == &*std::next(BotMBB->getIterator()))
85 break;
86 NextMBB = &*std::next(BotMBB->getIterator());
87 }
88 }
89 return BotMBB;
90}
91
92MachineBasicBlock *MachineLoop::findLoopControlBlock() {
93 if (MachineBasicBlock *Latch = getLoopLatch()) {
94 if (isLoopExiting(Latch))
95 return Latch;
96 else
97 return getExitingBlock();
98 }
99 return nullptr;
100}
101
102DebugLoc MachineLoop::getStartLoc() const {
103 // Try the pre-header first.
104 if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
105 if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
106 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
107 return DL;
108
109 // If we have no pre-header or there are no instructions with debug
110 // info in it, try the header.
111 if (MachineBasicBlock *HeadMBB = getHeader())
112 if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
113 return HeadBB->getTerminator()->getDebugLoc();
114
115 return DebugLoc();
116}
117
118MachineBasicBlock *
119MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
120 bool FindMultiLoopPreheader) const {
121 if (MachineBasicBlock *PB = L->getLoopPreheader())
1
Assuming 'PB' is null
2
Taking false branch
122 return PB;
123
124 if (!SpeculativePreheader)
3
Assuming 'SpeculativePreheader' is true
4
Taking false branch
125 return nullptr;
126
127 MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
128 if (HB->pred_size() != 2 || HB->hasAddressTaken())
5
Assuming the condition is false
6
Assuming the condition is false
7
Taking false branch
129 return nullptr;
130 // Find the predecessor of the header that is not the latch block.
131 MachineBasicBlock *Preheader = nullptr;
8
'Preheader' initialized to a null pointer value
132 for (MachineBasicBlock *P : HB->predecessors()) {
133 if (P == LB)
134 continue;
135 // Sanity.
136 if (Preheader)
137 return nullptr;
138 Preheader = P;
139 }
140
141 // Check if the preheader candidate is a successor of any other loop
142 // headers. We want to avoid having two loop setups in the same block.
143 if (!FindMultiLoopPreheader) {
9
Assuming 'FindMultiLoopPreheader' is false
10
Taking true branch
144 for (MachineBasicBlock *S : Preheader->successors()) {
11
Called C++ object pointer is null
145 if (S == HB)
146 continue;
147 MachineLoop *T = getLoopFor(S);
148 if (T && T->getHeader() == S)
149 return nullptr;
150 }
151 }
152 return Preheader;
153}
154
155bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
156 MachineFunction *MF = I.getParent()->getParent();
157 MachineRegisterInfo *MRI = &MF->getRegInfo();
158 const TargetSubtargetInfo &ST = MF->getSubtarget();
159 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
160 const TargetInstrInfo *TII = ST.getInstrInfo();
161
162 // The instruction is loop invariant if all of its operands are.
163 for (const MachineOperand &MO : I.operands()) {
164 if (!MO.isReg())
165 continue;
166
167 Register Reg = MO.getReg();
168 if (Reg == 0) continue;
169
170 // An instruction that uses or defines a physical register can't e.g. be
171 // hoisted, so mark this as not invariant.
172 if (Register::isPhysicalRegister(Reg)) {
173 if (MO.isUse()) {
174 // If the physreg has no defs anywhere, it's just an ambient register
175 // and we can freely move its uses. Alternatively, if it's allocatable,
176 // it could get allocated to something with a def during allocation.
177 // However, if the physreg is known to always be caller saved/restored
178 // then this use is safe to hoist.
179 if (!MRI->isConstantPhysReg(Reg) &&
180 !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
181 !TII->isIgnorableUse(MO))
182 return false;
183 // Otherwise it's safe to move.
184 continue;
185 } else if (!MO.isDead()) {
186 // A def that isn't dead can't be moved.
187 return false;
188 } else if (getHeader()->isLiveIn(Reg)) {
189 // If the reg is live into the loop, we can't hoist an instruction
190 // which would clobber it.
191 return false;
192 }
193 }
194
195 if (!MO.isUse())
196 continue;
197
198 assert(MRI->getVRegDef(Reg) &&(static_cast <bool> (MRI->getVRegDef(Reg) &&
"Machine instr not mapped for this vreg?!") ? void (0) : __assert_fail
("MRI->getVRegDef(Reg) && \"Machine instr not mapped for this vreg?!\""
, "llvm/lib/CodeGen/MachineLoopInfo.cpp", 199, __extension__ __PRETTY_FUNCTION__
))
199 "Machine instr not mapped for this vreg?!")(static_cast <bool> (MRI->getVRegDef(Reg) &&
"Machine instr not mapped for this vreg?!") ? void (0) : __assert_fail
("MRI->getVRegDef(Reg) && \"Machine instr not mapped for this vreg?!\""
, "llvm/lib/CodeGen/MachineLoopInfo.cpp", 199, __extension__ __PRETTY_FUNCTION__
))
;
200
201 // If the loop contains the definition of an operand, then the instruction
202 // isn't loop invariant.
203 if (contains(MRI->getVRegDef(Reg)))
204 return false;
205 }
206
207 // If we got this far, the instruction is loop invariant!
208 return true;
209}
210
211#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
212LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineLoop::dump() const {
213 print(dbgs());
214}
215#endif