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