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