Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/IR/Operator.cpp
Warning:line 71, column 37
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 Operator.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/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/IR -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/IR -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/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-16/lib/clang/16.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-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -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 -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -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-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/IR/Operator.cpp
1//===-- Operator.cpp - Implement the LLVM operators -----------------------===//
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 implements the non-inline methods for the LLVM Operator classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Operator.h"
14#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/GetElementPtrTypeIterator.h"
16#include "llvm/IR/Instructions.h"
17
18#include "ConstantsContext.h"
19
20namespace llvm {
21bool Operator::hasPoisonGeneratingFlags() const {
22 switch (getOpcode()) {
23 case Instruction::Add:
24 case Instruction::Sub:
25 case Instruction::Mul:
26 case Instruction::Shl: {
27 auto *OBO = cast<OverflowingBinaryOperator>(this);
28 return OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap();
29 }
30 case Instruction::UDiv:
31 case Instruction::SDiv:
32 case Instruction::AShr:
33 case Instruction::LShr:
34 return cast<PossiblyExactOperator>(this)->isExact();
35 case Instruction::GetElementPtr: {
36 auto *GEP = cast<GEPOperator>(this);
37 // Note: inrange exists on constexpr only
38 return GEP->isInBounds() || GEP->getInRangeIndex() != None;
39 }
40 default:
41 if (const auto *FP = dyn_cast<FPMathOperator>(this))
42 return FP->hasNoNaNs() || FP->hasNoInfs();
43 return false;
44 }
45}
46
47Type *GEPOperator::getSourceElementType() const {
48 if (auto *I = dyn_cast<GetElementPtrInst>(this))
49 return I->getSourceElementType();
50 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
51}
52
53Type *GEPOperator::getResultElementType() const {
54 if (auto *I = dyn_cast<GetElementPtrInst>(this))
55 return I->getResultElementType();
56 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
57}
58
59Align GEPOperator::getMaxPreservedAlignment(const DataLayout &DL) const {
60 /// compute the worse possible offset for every level of the GEP et accumulate
61 /// the minimum alignment into Result.
62
63 Align Result = Align(llvm::Value::MaximumAlignment);
64 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
1
Loop condition is true. Entering loop body
65 GTI != GTE; ++GTI) {
66 int64_t Offset = 1;
67 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
2
Assuming the object is not a 'CastReturnType'
3
'OpC' initialized to a null pointer value
68
69 if (StructType *STy = GTI.getStructTypeOrNull()) {
4
Assuming 'STy' is non-null
5
Taking true branch
70 const StructLayout *SL = DL.getStructLayout(STy);
71 Offset = SL->getElementOffset(OpC->getZExtValue());
6
Called C++ object pointer is null
72 } else {
73 assert(GTI.isSequential() && "should be sequencial")(static_cast <bool> (GTI.isSequential() && "should be sequencial"
) ? void (0) : __assert_fail ("GTI.isSequential() && \"should be sequencial\""
, "llvm/lib/IR/Operator.cpp", 73, __extension__ __PRETTY_FUNCTION__
))
;
74 /// If the index isn't know we take 1 because it is the index that will
75 /// give the worse alignment of the offset.
76 int64_t ElemCount = 1;
77 if (OpC)
78 ElemCount = OpC->getZExtValue();
79 Offset = DL.getTypeAllocSize(GTI.getIndexedType()) * ElemCount;
80 }
81 Result = Align(MinAlign(Offset, Result.value()));
82 }
83 return Result;
84}
85
86bool GEPOperator::accumulateConstantOffset(
87 const DataLayout &DL, APInt &Offset,
88 function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
89 assert(Offset.getBitWidth() ==(static_cast <bool> (Offset.getBitWidth() == DL.getIndexSizeInBits
(getPointerAddressSpace()) && "The offset bit width does not match DL specification."
) ? void (0) : __assert_fail ("Offset.getBitWidth() == DL.getIndexSizeInBits(getPointerAddressSpace()) && \"The offset bit width does not match DL specification.\""
, "llvm/lib/IR/Operator.cpp", 91, __extension__ __PRETTY_FUNCTION__
))
90 DL.getIndexSizeInBits(getPointerAddressSpace()) &&(static_cast <bool> (Offset.getBitWidth() == DL.getIndexSizeInBits
(getPointerAddressSpace()) && "The offset bit width does not match DL specification."
) ? void (0) : __assert_fail ("Offset.getBitWidth() == DL.getIndexSizeInBits(getPointerAddressSpace()) && \"The offset bit width does not match DL specification.\""
, "llvm/lib/IR/Operator.cpp", 91, __extension__ __PRETTY_FUNCTION__
))
91 "The offset bit width does not match DL specification.")(static_cast <bool> (Offset.getBitWidth() == DL.getIndexSizeInBits
(getPointerAddressSpace()) && "The offset bit width does not match DL specification."
) ? void (0) : __assert_fail ("Offset.getBitWidth() == DL.getIndexSizeInBits(getPointerAddressSpace()) && \"The offset bit width does not match DL specification.\""
, "llvm/lib/IR/Operator.cpp", 91, __extension__ __PRETTY_FUNCTION__
))
;
92 SmallVector<const Value *> Index(llvm::drop_begin(operand_values()));
93 return GEPOperator::accumulateConstantOffset(getSourceElementType(), Index,
94 DL, Offset, ExternalAnalysis);
95}
96
97bool GEPOperator::accumulateConstantOffset(
98 Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
99 APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
100 bool UsedExternalAnalysis = false;
101 auto AccumulateOffset = [&](APInt Index, uint64_t Size) -> bool {
102 Index = Index.sextOrTrunc(Offset.getBitWidth());
103 APInt IndexedSize = APInt(Offset.getBitWidth(), Size);
104 // For array or vector indices, scale the index by the size of the type.
105 if (!UsedExternalAnalysis) {
106 Offset += Index * IndexedSize;
107 } else {
108 // External Analysis can return a result higher/lower than the value
109 // represents. We need to detect overflow/underflow.
110 bool Overflow = false;
111 APInt OffsetPlus = Index.smul_ov(IndexedSize, Overflow);
112 if (Overflow)
113 return false;
114 Offset = Offset.sadd_ov(OffsetPlus, Overflow);
115 if (Overflow)
116 return false;
117 }
118 return true;
119 };
120 auto begin = generic_gep_type_iterator<decltype(Index.begin())>::begin(
121 SourceType, Index.begin());
122 auto end = generic_gep_type_iterator<decltype(Index.end())>::end(Index.end());
123 for (auto GTI = begin, GTE = end; GTI != GTE; ++GTI) {
124 // Scalable vectors are multiplied by a runtime constant.
125 bool ScalableType = false;
126 if (isa<ScalableVectorType>(GTI.getIndexedType()))
127 ScalableType = true;
128
129 Value *V = GTI.getOperand();
130 StructType *STy = GTI.getStructTypeOrNull();
131 // Handle ConstantInt if possible.
132 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
133 if (ConstOffset->isZero())
134 continue;
135 // if the type is scalable and the constant is not zero (vscale * n * 0 =
136 // 0) bailout.
137 if (ScalableType)
138 return false;
139 // Handle a struct index, which adds its field offset to the pointer.
140 if (STy) {
141 unsigned ElementIdx = ConstOffset->getZExtValue();
142 const StructLayout *SL = DL.getStructLayout(STy);
143 // Element offset is in bytes.
144 if (!AccumulateOffset(
145 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
146 1))
147 return false;
148 continue;
149 }
150 if (!AccumulateOffset(ConstOffset->getValue(),
151 DL.getTypeAllocSize(GTI.getIndexedType())))
152 return false;
153 continue;
154 }
155
156 // The operand is not constant, check if an external analysis was provided.
157 // External analsis is not applicable to a struct type.
158 if (!ExternalAnalysis || STy || ScalableType)
159 return false;
160 APInt AnalysisIndex;
161 if (!ExternalAnalysis(*V, AnalysisIndex))
162 return false;
163 UsedExternalAnalysis = true;
164 if (!AccumulateOffset(AnalysisIndex,
165 DL.getTypeAllocSize(GTI.getIndexedType())))
166 return false;
167 }
168 return true;
169}
170
171bool GEPOperator::collectOffset(
172 const DataLayout &DL, unsigned BitWidth,
173 MapVector<Value *, APInt> &VariableOffsets,
174 APInt &ConstantOffset) const {
175 assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&(static_cast <bool> (BitWidth == DL.getIndexSizeInBits(
getPointerAddressSpace()) && "The offset bit width does not match DL specification."
) ? void (0) : __assert_fail ("BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) && \"The offset bit width does not match DL specification.\""
, "llvm/lib/IR/Operator.cpp", 176, __extension__ __PRETTY_FUNCTION__
))
176 "The offset bit width does not match DL specification.")(static_cast <bool> (BitWidth == DL.getIndexSizeInBits(
getPointerAddressSpace()) && "The offset bit width does not match DL specification."
) ? void (0) : __assert_fail ("BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) && \"The offset bit width does not match DL specification.\""
, "llvm/lib/IR/Operator.cpp", 176, __extension__ __PRETTY_FUNCTION__
))
;
177
178 auto CollectConstantOffset = [&](APInt Index, uint64_t Size) {
179 Index = Index.sextOrTrunc(BitWidth);
180 APInt IndexedSize = APInt(BitWidth, Size);
181 ConstantOffset += Index * IndexedSize;
182 };
183
184 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
185 GTI != GTE; ++GTI) {
186 // Scalable vectors are multiplied by a runtime constant.
187 bool ScalableType = isa<ScalableVectorType>(GTI.getIndexedType());
188
189 Value *V = GTI.getOperand();
190 StructType *STy = GTI.getStructTypeOrNull();
191 // Handle ConstantInt if possible.
192 if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
193 if (ConstOffset->isZero())
194 continue;
195 // If the type is scalable and the constant is not zero (vscale * n * 0 =
196 // 0) bailout.
197 // TODO: If the runtime value is accessible at any point before DWARF
198 // emission, then we could potentially keep a forward reference to it
199 // in the debug value to be filled in later.
200 if (ScalableType)
201 return false;
202 // Handle a struct index, which adds its field offset to the pointer.
203 if (STy) {
204 unsigned ElementIdx = ConstOffset->getZExtValue();
205 const StructLayout *SL = DL.getStructLayout(STy);
206 // Element offset is in bytes.
207 CollectConstantOffset(APInt(BitWidth, SL->getElementOffset(ElementIdx)),
208 1);
209 continue;
210 }
211 CollectConstantOffset(ConstOffset->getValue(),
212 DL.getTypeAllocSize(GTI.getIndexedType()));
213 continue;
214 }
215
216 if (STy || ScalableType)
217 return false;
218 APInt IndexedSize =
219 APInt(BitWidth, DL.getTypeAllocSize(GTI.getIndexedType()));
220 // Insert an initial offset of 0 for V iff none exists already, then
221 // increment the offset by IndexedSize.
222 if (!IndexedSize.isZero()) {
223 VariableOffsets.insert({V, APInt(BitWidth, 0)});
224 VariableOffsets[V] += IndexedSize;
225 }
226 }
227 return true;
228}
229
230void FastMathFlags::print(raw_ostream &O) const {
231 if (all())
232 O << " fast";
233 else {
234 if (allowReassoc())
235 O << " reassoc";
236 if (noNaNs())
237 O << " nnan";
238 if (noInfs())
239 O << " ninf";
240 if (noSignedZeros())
241 O << " nsz";
242 if (allowReciprocal())
243 O << " arcp";
244 if (allowContract())
245 O << " contract";
246 if (approxFunc())
247 O << " afn";
248 }
249}
250} // namespace llvm