Bug Summary

File:llvm/lib/CodeGen/MachineLoopInfo.cpp
Warning:line 146, column 31
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -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 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/CodeGen -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/CodeGen -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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/Passes.h"
20#include "llvm/Config/llvm-config.h"
21#include "llvm/InitializePasses.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26// Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
27template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
28template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
29
30char MachineLoopInfo::ID = 0;
31MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
32 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
33}
34INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",static void *initializeMachineLoopInfoPassOnce(PassRegistry &
Registry) {
35 "Machine Natural Loop Construction", true, true)static void *initializeMachineLoopInfoPassOnce(PassRegistry &
Registry) {
36INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)initializeMachineDominatorTreePass(Registry);
37INITIALIZE_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)); }
38 "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)); }
39
40char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
41
42bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
43 calculate(getAnalysis<MachineDominatorTree>());
44 return false;
45}
46
47void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
48 releaseMemory();
49 LI.analyze(MDT.getBase());
50}
51
52void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 AU.addRequired<MachineDominatorTree>();
55 MachineFunctionPass::getAnalysisUsage(AU);
56}
57
58MachineBasicBlock *MachineLoop::getTopBlock() {
59 MachineBasicBlock *TopMBB = getHeader();
60 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
61 if (TopMBB->getIterator() != Begin) {
62 MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
63 while (contains(PriorMBB)) {
64 TopMBB = PriorMBB;
65 if (TopMBB->getIterator() == Begin)
66 break;
67 PriorMBB = &*std::prev(TopMBB->getIterator());
68 }
69 }
70 return TopMBB;
71}
72
73MachineBasicBlock *MachineLoop::getBottomBlock() {
74 MachineBasicBlock *BotMBB = getHeader();
75 MachineFunction::iterator End = BotMBB->getParent()->end();
76 if (BotMBB->getIterator() != std::prev(End)) {
77 MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
78 while (contains(NextMBB)) {
79 BotMBB = NextMBB;
80 if (BotMBB == &*std::next(BotMBB->getIterator()))
81 break;
82 NextMBB = &*std::next(BotMBB->getIterator());
83 }
84 }
85 return BotMBB;
86}
87
88MachineBasicBlock *MachineLoop::findLoopControlBlock() {
89 if (MachineBasicBlock *Latch = getLoopLatch()) {
90 if (isLoopExiting(Latch))
91 return Latch;
92 else
93 return getExitingBlock();
94 }
95 return nullptr;
96}
97
98DebugLoc MachineLoop::getStartLoc() const {
99 // Try the pre-header first.
100 if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
101 if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
102 if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
103 return DL;
104
105 // If we have no pre-header or there are no instructions with debug
106 // info in it, try the header.
107 if (MachineBasicBlock *HeadMBB = getHeader())
108 if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
109 return HeadBB->getTerminator()->getDebugLoc();
110
111 return DebugLoc();
112}
113
114bool MachineLoop::hasStaticProfInfo() const {
115 return llvm::any_of(blocks(), [](const MachineBasicBlock *MBB){
116 const BasicBlock *BB = MBB->getBasicBlock();
117 return BB && BB->getTerminator()->hasMetadata(LLVMContext::MD_prof);
118 });
119}
120
121MachineBasicBlock *
122MachineLoopInfo::findLoopPreheader(MachineLoop *L,
123 bool SpeculativePreheader) const {
124 if (MachineBasicBlock *PB = L->getLoopPreheader())
1
Assuming 'PB' is null
2
Taking false branch
125 return PB;
126
127 if (!SpeculativePreheader)
3
Assuming 'SpeculativePreheader' is true
4
Taking false branch
128 return nullptr;
129
130 MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
131 if (HB->pred_size() != 2 || HB->hasAddressTaken())
5
Assuming the condition is false
6
Assuming the condition is false
7
Taking false branch
132 return nullptr;
133 // Find the predecessor of the header that is not the latch block.
134 MachineBasicBlock *Preheader = nullptr;
8
'Preheader' initialized to a null pointer value
135 for (MachineBasicBlock *P : HB->predecessors()) {
136 if (P == LB)
137 continue;
138 // Sanity.
139 if (Preheader)
140 return nullptr;
141 Preheader = P;
142 }
143
144 // Check if the preheader candidate is a successor of any other loop
145 // headers. We want to avoid having two loop setups in the same block.
146 for (MachineBasicBlock *S : Preheader->successors()) {
9
Called C++ object pointer is null
147 if (S == HB)
148 continue;
149 MachineLoop *T = getLoopFor(S);
150 if (T && T->getHeader() == S)
151 return nullptr;
152 }
153 return Preheader;
154}
155
156#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
157LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MachineLoop::dump() const {
158 print(dbgs());
159}
160#endif