Bug Summary

File:lib/IR/Type.cpp
Warning:line 440, column 5
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 Type.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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/lib/IR -I /build/llvm-toolchain-snapshot-8~svn345461/lib/IR -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/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/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.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++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/lib/IR -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp -faddrsig
1//===- Type.cpp - Implement the Type class --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Type class for the IR library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/Type.h"
15#include "LLVMContextImpl.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Constant.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Value.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/Support/raw_ostream.h"
30#include <cassert>
31#include <utility>
32
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// Type Class Implementation
37//===----------------------------------------------------------------------===//
38
39Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
40 switch (IDNumber) {
41 case VoidTyID : return getVoidTy(C);
42 case HalfTyID : return getHalfTy(C);
43 case FloatTyID : return getFloatTy(C);
44 case DoubleTyID : return getDoubleTy(C);
45 case X86_FP80TyID : return getX86_FP80Ty(C);
46 case FP128TyID : return getFP128Ty(C);
47 case PPC_FP128TyID : return getPPC_FP128Ty(C);
48 case LabelTyID : return getLabelTy(C);
49 case MetadataTyID : return getMetadataTy(C);
50 case X86_MMXTyID : return getX86_MMXTy(C);
51 case TokenTyID : return getTokenTy(C);
52 default:
53 return nullptr;
54 }
55}
56
57bool Type::isIntegerTy(unsigned Bitwidth) const {
58 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
59}
60
61bool Type::canLosslesslyBitCastTo(Type *Ty) const {
62 // Identity cast means no change so return true
63 if (this == Ty)
64 return true;
65
66 // They are not convertible unless they are at least first class types
67 if (!this->isFirstClassType() || !Ty->isFirstClassType())
68 return false;
69
70 // Vector -> Vector conversions are always lossless if the two vector types
71 // have the same size, otherwise not. Also, 64-bit vector types can be
72 // converted to x86mmx.
73 if (auto *thisPTy = dyn_cast<VectorType>(this)) {
74 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
75 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
76 if (Ty->getTypeID() == Type::X86_MMXTyID &&
77 thisPTy->getBitWidth() == 64)
78 return true;
79 }
80
81 if (this->getTypeID() == Type::X86_MMXTyID)
82 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
83 if (thatPTy->getBitWidth() == 64)
84 return true;
85
86 // At this point we have only various mismatches of the first class types
87 // remaining and ptr->ptr. Just select the lossless conversions. Everything
88 // else is not lossless. Conservatively assume we can't losslessly convert
89 // between pointers with different address spaces.
90 if (auto *PTy = dyn_cast<PointerType>(this)) {
91 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
92 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
93 return false;
94 }
95 return false; // Other types have no identity values
96}
97
98bool Type::isEmptyTy() const {
99 if (auto *ATy = dyn_cast<ArrayType>(this)) {
100 unsigned NumElements = ATy->getNumElements();
101 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
102 }
103
104 if (auto *STy = dyn_cast<StructType>(this)) {
105 unsigned NumElements = STy->getNumElements();
106 for (unsigned i = 0; i < NumElements; ++i)
107 if (!STy->getElementType(i)->isEmptyTy())
108 return false;
109 return true;
110 }
111
112 return false;
113}
114
115unsigned Type::getPrimitiveSizeInBits() const {
116 switch (getTypeID()) {
117 case Type::HalfTyID: return 16;
118 case Type::FloatTyID: return 32;
119 case Type::DoubleTyID: return 64;
120 case Type::X86_FP80TyID: return 80;
121 case Type::FP128TyID: return 128;
122 case Type::PPC_FP128TyID: return 128;
123 case Type::X86_MMXTyID: return 64;
124 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
125 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
126 default: return 0;
127 }
128}
129
130unsigned Type::getScalarSizeInBits() const {
131 return getScalarType()->getPrimitiveSizeInBits();
132}
133
134int Type::getFPMantissaWidth() const {
135 if (auto *VTy = dyn_cast<VectorType>(this))
136 return VTy->getElementType()->getFPMantissaWidth();
137 assert(isFloatingPointTy() && "Not a floating point type!")((isFloatingPointTy() && "Not a floating point type!"
) ? static_cast<void> (0) : __assert_fail ("isFloatingPointTy() && \"Not a floating point type!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 137, __PRETTY_FUNCTION__))
;
138 if (getTypeID() == HalfTyID) return 11;
139 if (getTypeID() == FloatTyID) return 24;
140 if (getTypeID() == DoubleTyID) return 53;
141 if (getTypeID() == X86_FP80TyID) return 64;
142 if (getTypeID() == FP128TyID) return 113;
143 assert(getTypeID() == PPC_FP128TyID && "unknown fp type")((getTypeID() == PPC_FP128TyID && "unknown fp type") ?
static_cast<void> (0) : __assert_fail ("getTypeID() == PPC_FP128TyID && \"unknown fp type\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 143, __PRETTY_FUNCTION__))
;
144 return -1;
145}
146
147bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
148 if (auto *ATy = dyn_cast<ArrayType>(this))
149 return ATy->getElementType()->isSized(Visited);
150
151 if (auto *VTy = dyn_cast<VectorType>(this))
152 return VTy->getElementType()->isSized(Visited);
153
154 return cast<StructType>(this)->isSized(Visited);
155}
156
157//===----------------------------------------------------------------------===//
158// Primitive 'Type' data
159//===----------------------------------------------------------------------===//
160
161Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
162Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
163Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
164Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
165Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
166Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
167Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
168Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
169Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
170Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
171Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
172
173IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
174IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
175IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
176IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
177IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
178IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
179
180IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
181 return IntegerType::get(C, N);
182}
183
184PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
185 return getHalfTy(C)->getPointerTo(AS);
186}
187
188PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
189 return getFloatTy(C)->getPointerTo(AS);
190}
191
192PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
193 return getDoubleTy(C)->getPointerTo(AS);
194}
195
196PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
197 return getX86_FP80Ty(C)->getPointerTo(AS);
198}
199
200PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
201 return getFP128Ty(C)->getPointerTo(AS);
202}
203
204PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
205 return getPPC_FP128Ty(C)->getPointerTo(AS);
206}
207
208PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
209 return getX86_MMXTy(C)->getPointerTo(AS);
210}
211
212PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
213 return getIntNTy(C, N)->getPointerTo(AS);
214}
215
216PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
217 return getInt1Ty(C)->getPointerTo(AS);
218}
219
220PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
221 return getInt8Ty(C)->getPointerTo(AS);
222}
223
224PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
225 return getInt16Ty(C)->getPointerTo(AS);
226}
227
228PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
229 return getInt32Ty(C)->getPointerTo(AS);
230}
231
232PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
233 return getInt64Ty(C)->getPointerTo(AS);
234}
235
236//===----------------------------------------------------------------------===//
237// IntegerType Implementation
238//===----------------------------------------------------------------------===//
239
240IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
241 assert(NumBits >= MIN_INT_BITS && "bitwidth too small")((NumBits >= MIN_INT_BITS && "bitwidth too small")
? static_cast<void> (0) : __assert_fail ("NumBits >= MIN_INT_BITS && \"bitwidth too small\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 241, __PRETTY_FUNCTION__))
;
242 assert(NumBits <= MAX_INT_BITS && "bitwidth too large")((NumBits <= MAX_INT_BITS && "bitwidth too large")
? static_cast<void> (0) : __assert_fail ("NumBits <= MAX_INT_BITS && \"bitwidth too large\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 242, __PRETTY_FUNCTION__))
;
243
244 // Check for the built-in integer types
245 switch (NumBits) {
246 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
247 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
248 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
249 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
250 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
251 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
252 default:
253 break;
254 }
255
256 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
257
258 if (!Entry)
259 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
260
261 return Entry;
262}
263
264bool IntegerType::isPowerOf2ByteWidth() const {
265 unsigned BitWidth = getBitWidth();
266 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
267}
268
269APInt IntegerType::getMask() const {
270 return APInt::getAllOnesValue(getBitWidth());
271}
272
273//===----------------------------------------------------------------------===//
274// FunctionType Implementation
275//===----------------------------------------------------------------------===//
276
277FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
278 bool IsVarArgs)
279 : Type(Result->getContext(), FunctionTyID) {
280 Type **SubTys = reinterpret_cast<Type**>(this+1);
281 assert(isValidReturnType(Result) && "invalid return type for function")((isValidReturnType(Result) && "invalid return type for function"
) ? static_cast<void> (0) : __assert_fail ("isValidReturnType(Result) && \"invalid return type for function\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 281, __PRETTY_FUNCTION__))
;
282 setSubclassData(IsVarArgs);
283
284 SubTys[0] = Result;
285
286 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
287 assert(isValidArgumentType(Params[i]) &&((isValidArgumentType(Params[i]) && "Not a valid type for function argument!"
) ? static_cast<void> (0) : __assert_fail ("isValidArgumentType(Params[i]) && \"Not a valid type for function argument!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 288, __PRETTY_FUNCTION__))
288 "Not a valid type for function argument!")((isValidArgumentType(Params[i]) && "Not a valid type for function argument!"
) ? static_cast<void> (0) : __assert_fail ("isValidArgumentType(Params[i]) && \"Not a valid type for function argument!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 288, __PRETTY_FUNCTION__))
;
289 SubTys[i+1] = Params[i];
290 }
291
292 ContainedTys = SubTys;
293 NumContainedTys = Params.size() + 1; // + 1 for result type
294}
295
296// This is the factory function for the FunctionType class.
297FunctionType *FunctionType::get(Type *ReturnType,
298 ArrayRef<Type*> Params, bool isVarArg) {
299 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
300 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
301 FunctionType *FT;
302 // Since we only want to allocate a fresh function type in case none is found
303 // and we don't want to perform two lookups (one for checking if existent and
304 // one for inserting the newly allocated one), here we instead lookup based on
305 // Key and update the reference to the function type in-place to a newly
306 // allocated one if not found.
307 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
308 if (Insertion.second) {
309 // The function type was not found. Allocate one and update FunctionTypes
310 // in-place.
311 FT = (FunctionType *)pImpl->TypeAllocator.Allocate(
312 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
313 alignof(FunctionType));
314 new (FT) FunctionType(ReturnType, Params, isVarArg);
315 *Insertion.first = FT;
316 } else {
317 // The function type was found. Just return it.
318 FT = *Insertion.first;
319 }
320 return FT;
321}
322
323FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
324 return get(Result, None, isVarArg);
325}
326
327bool FunctionType::isValidReturnType(Type *RetTy) {
328 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
329 !RetTy->isMetadataTy();
330}
331
332bool FunctionType::isValidArgumentType(Type *ArgTy) {
333 return ArgTy->isFirstClassType();
334}
335
336//===----------------------------------------------------------------------===//
337// StructType Implementation
338//===----------------------------------------------------------------------===//
339
340// Primitive Constructors.
341
342StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
343 bool isPacked) {
344 LLVMContextImpl *pImpl = Context.pImpl;
345 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
346
347 StructType *ST;
348 // Since we only want to allocate a fresh struct type in case none is found
349 // and we don't want to perform two lookups (one for checking if existent and
350 // one for inserting the newly allocated one), here we instead lookup based on
351 // Key and update the reference to the struct type in-place to a newly
352 // allocated one if not found.
353 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
354 if (Insertion.second) {
355 // The struct type was not found. Allocate one and update AnonStructTypes
356 // in-place.
357 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
358 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
359 ST->setBody(ETypes, isPacked);
360 *Insertion.first = ST;
361 } else {
362 // The struct type was found. Just return it.
363 ST = *Insertion.first;
364 }
365
366 return ST;
367}
368
369void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
370 assert(isOpaque() && "Struct body already set!")((isOpaque() && "Struct body already set!") ? static_cast
<void> (0) : __assert_fail ("isOpaque() && \"Struct body already set!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 370, __PRETTY_FUNCTION__))
;
371
372 setSubclassData(getSubclassData() | SCDB_HasBody);
373 if (isPacked)
374 setSubclassData(getSubclassData() | SCDB_Packed);
375
376 NumContainedTys = Elements.size();
377
378 if (Elements.empty()) {
379 ContainedTys = nullptr;
380 return;
381 }
382
383 ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data();
384}
385
386void StructType::setName(StringRef Name) {
387 if (Name == getName()) return;
388
389 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
390
391 using EntryTy = StringMap<StructType *>::MapEntryTy;
392
393 // If this struct already had a name, remove its symbol table entry. Don't
394 // delete the data yet because it may be part of the new name.
395 if (SymbolTableEntry)
396 SymbolTable.remove((EntryTy *)SymbolTableEntry);
397
398 // If this is just removing the name, we're done.
399 if (Name.empty()) {
400 if (SymbolTableEntry) {
401 // Delete the old string data.
402 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
403 SymbolTableEntry = nullptr;
404 }
405 return;
406 }
407
408 // Look up the entry for the name.
409 auto IterBool =
410 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
411
412 // While we have a name collision, try a random rename.
413 if (!IterBool.second) {
414 SmallString<64> TempStr(Name);
415 TempStr.push_back('.');
416 raw_svector_ostream TmpStream(TempStr);
417 unsigned NameSize = Name.size();
418
419 do {
420 TempStr.resize(NameSize + 1);
421 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
422
423 IterBool = getContext().pImpl->NamedStructTypes.insert(
424 std::make_pair(TmpStream.str(), this));
425 } while (!IterBool.second);
426 }
427
428 // Delete the old string data.
429 if (SymbolTableEntry)
430 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
431 SymbolTableEntry = &*IterBool.first;
432}
433
434//===----------------------------------------------------------------------===//
435// StructType Helper functions.
436
437StructType *StructType::create(LLVMContext &Context, StringRef Name) {
438 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
3
'ST' initialized to a null pointer value
439 if (!Name.empty())
4
Assuming the condition is true
5
Taking true branch
440 ST->setName(Name);
6
Called C++ object pointer is null
441 return ST;
442}
443
444StructType *StructType::get(LLVMContext &Context, bool isPacked) {
445 return get(Context, None, isPacked);
446}
447
448StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
449 StringRef Name, bool isPacked) {
450 StructType *ST = create(Context, Name);
2
Calling 'StructType::create'
451 ST->setBody(Elements, isPacked);
452 return ST;
453}
454
455StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
456 return create(Context, Elements, StringRef());
457}
458
459StructType *StructType::create(LLVMContext &Context) {
460 return create(Context, StringRef());
461}
462
463StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
464 bool isPacked) {
465 assert(!Elements.empty() &&((!Elements.empty() && "This method may not be invoked with an empty list"
) ? static_cast<void> (0) : __assert_fail ("!Elements.empty() && \"This method may not be invoked with an empty list\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 466, __PRETTY_FUNCTION__))
466 "This method may not be invoked with an empty list")((!Elements.empty() && "This method may not be invoked with an empty list"
) ? static_cast<void> (0) : __assert_fail ("!Elements.empty() && \"This method may not be invoked with an empty list\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 466, __PRETTY_FUNCTION__))
;
467 return create(Elements[0]->getContext(), Elements, Name, isPacked);
468}
469
470StructType *StructType::create(ArrayRef<Type*> Elements) {
471 assert(!Elements.empty() &&((!Elements.empty() && "This method may not be invoked with an empty list"
) ? static_cast<void> (0) : __assert_fail ("!Elements.empty() && \"This method may not be invoked with an empty list\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 472, __PRETTY_FUNCTION__))
472 "This method may not be invoked with an empty list")((!Elements.empty() && "This method may not be invoked with an empty list"
) ? static_cast<void> (0) : __assert_fail ("!Elements.empty() && \"This method may not be invoked with an empty list\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 472, __PRETTY_FUNCTION__))
;
473 return create(Elements[0]->getContext(), Elements, StringRef());
1
Calling 'StructType::create'
474}
475
476bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
477 if ((getSubclassData() & SCDB_IsSized) != 0)
478 return true;
479 if (isOpaque())
480 return false;
481
482 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
483 return false;
484
485 // Okay, our struct is sized if all of the elements are, but if one of the
486 // elements is opaque, the struct isn't sized *yet*, but may become sized in
487 // the future, so just bail out without caching.
488 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
489 if (!(*I)->isSized(Visited))
490 return false;
491
492 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
493 // we find a sized type, as types can only move from opaque to sized, not the
494 // other way.
495 const_cast<StructType*>(this)->setSubclassData(
496 getSubclassData() | SCDB_IsSized);
497 return true;
498}
499
500StringRef StructType::getName() const {
501 assert(!isLiteral() && "Literal structs never have names")((!isLiteral() && "Literal structs never have names")
? static_cast<void> (0) : __assert_fail ("!isLiteral() && \"Literal structs never have names\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 501, __PRETTY_FUNCTION__))
;
502 if (!SymbolTableEntry) return StringRef();
503
504 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
505}
506
507bool StructType::isValidElementType(Type *ElemTy) {
508 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
509 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
510 !ElemTy->isTokenTy();
511}
512
513bool StructType::isLayoutIdentical(StructType *Other) const {
514 if (this == Other) return true;
515
516 if (isPacked() != Other->isPacked())
517 return false;
518
519 return elements() == Other->elements();
520}
521
522StructType *Module::getTypeByName(StringRef Name) const {
523 return getContext().pImpl->NamedStructTypes.lookup(Name);
524}
525
526//===----------------------------------------------------------------------===//
527// CompositeType Implementation
528//===----------------------------------------------------------------------===//
529
530Type *CompositeType::getTypeAtIndex(const Value *V) const {
531 if (auto *STy = dyn_cast<StructType>(this)) {
532 unsigned Idx =
533 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
534 assert(indexValid(Idx) && "Invalid structure index!")((indexValid(Idx) && "Invalid structure index!") ? static_cast
<void> (0) : __assert_fail ("indexValid(Idx) && \"Invalid structure index!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 534, __PRETTY_FUNCTION__))
;
535 return STy->getElementType(Idx);
536 }
537
538 return cast<SequentialType>(this)->getElementType();
539}
540
541Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
542 if (auto *STy = dyn_cast<StructType>(this)) {
543 assert(indexValid(Idx) && "Invalid structure index!")((indexValid(Idx) && "Invalid structure index!") ? static_cast
<void> (0) : __assert_fail ("indexValid(Idx) && \"Invalid structure index!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 543, __PRETTY_FUNCTION__))
;
544 return STy->getElementType(Idx);
545 }
546
547 return cast<SequentialType>(this)->getElementType();
548}
549
550bool CompositeType::indexValid(const Value *V) const {
551 if (auto *STy = dyn_cast<StructType>(this)) {
552 // Structure indexes require (vectors of) 32-bit integer constants. In the
553 // vector case all of the indices must be equal.
554 if (!V->getType()->isIntOrIntVectorTy(32))
555 return false;
556 const Constant *C = dyn_cast<Constant>(V);
557 if (C && V->getType()->isVectorTy())
558 C = C->getSplatValue();
559 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
560 return CU && CU->getZExtValue() < STy->getNumElements();
561 }
562
563 // Sequential types can be indexed by any integer.
564 return V->getType()->isIntOrIntVectorTy();
565}
566
567bool CompositeType::indexValid(unsigned Idx) const {
568 if (auto *STy = dyn_cast<StructType>(this))
569 return Idx < STy->getNumElements();
570 // Sequential types can be indexed by any integer.
571 return true;
572}
573
574//===----------------------------------------------------------------------===//
575// ArrayType Implementation
576//===----------------------------------------------------------------------===//
577
578ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
579 : SequentialType(ArrayTyID, ElType, NumEl) {}
580
581ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
582 assert(isValidElementType(ElementType) && "Invalid type for array element!")((isValidElementType(ElementType) && "Invalid type for array element!"
) ? static_cast<void> (0) : __assert_fail ("isValidElementType(ElementType) && \"Invalid type for array element!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 582, __PRETTY_FUNCTION__))
;
583
584 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
585 ArrayType *&Entry =
586 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
587
588 if (!Entry)
589 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
590 return Entry;
591}
592
593bool ArrayType::isValidElementType(Type *ElemTy) {
594 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
595 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
596 !ElemTy->isTokenTy();
597}
598
599//===----------------------------------------------------------------------===//
600// VectorType Implementation
601//===----------------------------------------------------------------------===//
602
603VectorType::VectorType(Type *ElType, unsigned NumEl)
604 : SequentialType(VectorTyID, ElType, NumEl) {}
605
606VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
607 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0")((NumElements > 0 && "#Elements of a VectorType must be greater than 0"
) ? static_cast<void> (0) : __assert_fail ("NumElements > 0 && \"#Elements of a VectorType must be greater than 0\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 607, __PRETTY_FUNCTION__))
;
608 assert(isValidElementType(ElementType) && "Element type of a VectorType must "((isValidElementType(ElementType) && "Element type of a VectorType must "
"be an integer, floating point, or " "pointer type.") ? static_cast
<void> (0) : __assert_fail ("isValidElementType(ElementType) && \"Element type of a VectorType must \" \"be an integer, floating point, or \" \"pointer type.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 610, __PRETTY_FUNCTION__))
609 "be an integer, floating point, or "((isValidElementType(ElementType) && "Element type of a VectorType must "
"be an integer, floating point, or " "pointer type.") ? static_cast
<void> (0) : __assert_fail ("isValidElementType(ElementType) && \"Element type of a VectorType must \" \"be an integer, floating point, or \" \"pointer type.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 610, __PRETTY_FUNCTION__))
610 "pointer type.")((isValidElementType(ElementType) && "Element type of a VectorType must "
"be an integer, floating point, or " "pointer type.") ? static_cast
<void> (0) : __assert_fail ("isValidElementType(ElementType) && \"Element type of a VectorType must \" \"be an integer, floating point, or \" \"pointer type.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 610, __PRETTY_FUNCTION__))
;
611
612 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
613 VectorType *&Entry = ElementType->getContext().pImpl
614 ->VectorTypes[std::make_pair(ElementType, NumElements)];
615
616 if (!Entry)
617 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
618 return Entry;
619}
620
621bool VectorType::isValidElementType(Type *ElemTy) {
622 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
623 ElemTy->isPointerTy();
624}
625
626//===----------------------------------------------------------------------===//
627// PointerType Implementation
628//===----------------------------------------------------------------------===//
629
630PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
631 assert(EltTy && "Can't get a pointer to <null> type!")((EltTy && "Can't get a pointer to <null> type!"
) ? static_cast<void> (0) : __assert_fail ("EltTy && \"Can't get a pointer to <null> type!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 631, __PRETTY_FUNCTION__))
;
632 assert(isValidElementType(EltTy) && "Invalid type for pointer element!")((isValidElementType(EltTy) && "Invalid type for pointer element!"
) ? static_cast<void> (0) : __assert_fail ("isValidElementType(EltTy) && \"Invalid type for pointer element!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/lib/IR/Type.cpp"
, 632, __PRETTY_FUNCTION__))
;
633
634 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
635
636 // Since AddressSpace #0 is the common case, we special case it.
637 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
638 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
639
640 if (!Entry)
641 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
642 return Entry;
643}
644
645PointerType::PointerType(Type *E, unsigned AddrSpace)
646 : Type(E->getContext(), PointerTyID), PointeeTy(E) {
647 ContainedTys = &PointeeTy;
648 NumContainedTys = 1;
649 setSubclassData(AddrSpace);
650}
651
652PointerType *Type::getPointerTo(unsigned addrs) const {
653 return PointerType::get(const_cast<Type*>(this), addrs);
654}
655
656bool PointerType::isValidElementType(Type *ElemTy) {
657 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
658 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
659}
660
661bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
662 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
663}