1 //===- Type.cpp - Implement the Type class --------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Type class for the IR library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Type.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/Constant.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/TypeSize.h"
26 #include "llvm/Support/raw_ostream.h"
32 //===----------------------------------------------------------------------===//
33 // Type Class Implementation
34 //===----------------------------------------------------------------------===//
36 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
38 case VoidTyID : return getVoidTy(C);
39 case HalfTyID : return getHalfTy(C);
40 case BFloatTyID : return getBFloatTy(C);
41 case FloatTyID : return getFloatTy(C);
42 case DoubleTyID : return getDoubleTy(C);
43 case X86_FP80TyID : return getX86_FP80Ty(C);
44 case FP128TyID : return getFP128Ty(C);
45 case PPC_FP128TyID : return getPPC_FP128Ty(C);
46 case LabelTyID : return getLabelTy(C);
47 case MetadataTyID : return getMetadataTy(C);
48 case X86_MMXTyID : return getX86_MMXTy(C);
49 case X86_AMXTyID : return getX86_AMXTy(C);
50 case TokenTyID : return getTokenTy(C);
56 bool Type::isIntegerTy(unsigned Bitwidth) const {
57 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
60 bool Type::isScalableTy() const {
61 if (const auto *STy = dyn_cast<StructType>(this)) {
62 SmallPtrSet<Type *, 4> Visited;
63 return STy->containsScalableVectorType(&Visited);
65 return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();
68 const fltSemantics &Type::getFltSemantics() const {
69 switch (getTypeID()) {
70 case HalfTyID: return APFloat::IEEEhalf();
71 case BFloatTyID: return APFloat::BFloat();
72 case FloatTyID: return APFloat::IEEEsingle();
73 case DoubleTyID: return APFloat::IEEEdouble();
74 case X86_FP80TyID: return APFloat::x87DoubleExtended();
75 case FP128TyID: return APFloat::IEEEquad();
76 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
77 default: llvm_unreachable("Invalid floating type");
81 bool Type::isIEEE() const {
82 return APFloat::getZero(getFltSemantics()).isIEEE();
85 bool Type::isScalableTargetExtTy() const {
86 if (auto *TT = dyn_cast<TargetExtType>(this))
87 return isa<ScalableVectorType>(TT->getLayoutType());
91 Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
93 if (&S == &APFloat::IEEEhalf())
94 Ty = Type::getHalfTy(C);
95 else if (&S == &APFloat::BFloat())
96 Ty = Type::getBFloatTy(C);
97 else if (&S == &APFloat::IEEEsingle())
98 Ty = Type::getFloatTy(C);
99 else if (&S == &APFloat::IEEEdouble())
100 Ty = Type::getDoubleTy(C);
101 else if (&S == &APFloat::x87DoubleExtended())
102 Ty = Type::getX86_FP80Ty(C);
103 else if (&S == &APFloat::IEEEquad())
104 Ty = Type::getFP128Ty(C);
106 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
107 Ty = Type::getPPC_FP128Ty(C);
112 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
113 // Identity cast means no change so return true
117 // They are not convertible unless they are at least first class types
118 if (!this->isFirstClassType() || !Ty->isFirstClassType())
121 // Vector -> Vector conversions are always lossless if the two vector types
122 // have the same size, otherwise not.
123 if (isa<VectorType>(this) && isa<VectorType>(Ty))
124 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
126 // 64-bit fixed width vector types can be losslessly converted to x86mmx.
127 if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
128 getPrimitiveSizeInBits().getFixedValue() == 64)
130 if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
131 Ty->getPrimitiveSizeInBits().getFixedValue() == 64)
134 // 8192-bit fixed width vector types can be losslessly converted to x86amx.
135 if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
136 getPrimitiveSizeInBits().getFixedValue() == 8192)
138 if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
139 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
142 // At this point we have only various mismatches of the first class types
143 // remaining and ptr->ptr. Just select the lossless conversions. Everything
144 // else is not lossless. Conservatively assume we can't losslessly convert
145 // between pointers with different address spaces.
146 if (auto *PTy = dyn_cast<PointerType>(this)) {
147 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
148 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
151 return false; // Other types have no identity values
154 bool Type::isEmptyTy() const {
155 if (auto *ATy = dyn_cast<ArrayType>(this)) {
156 unsigned NumElements = ATy->getNumElements();
157 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
160 if (auto *STy = dyn_cast<StructType>(this)) {
161 unsigned NumElements = STy->getNumElements();
162 for (unsigned i = 0; i < NumElements; ++i)
163 if (!STy->getElementType(i)->isEmptyTy())
171 TypeSize Type::getPrimitiveSizeInBits() const {
172 switch (getTypeID()) {
173 case Type::HalfTyID: return TypeSize::Fixed(16);
174 case Type::BFloatTyID: return TypeSize::Fixed(16);
175 case Type::FloatTyID: return TypeSize::Fixed(32);
176 case Type::DoubleTyID: return TypeSize::Fixed(64);
177 case Type::X86_FP80TyID: return TypeSize::Fixed(80);
178 case Type::FP128TyID: return TypeSize::Fixed(128);
179 case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
180 case Type::X86_MMXTyID: return TypeSize::Fixed(64);
181 case Type::X86_AMXTyID: return TypeSize::Fixed(8192);
182 case Type::IntegerTyID:
183 return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
184 case Type::FixedVectorTyID:
185 case Type::ScalableVectorTyID: {
186 const VectorType *VTy = cast<VectorType>(this);
187 ElementCount EC = VTy->getElementCount();
188 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
189 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
190 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
192 default: return TypeSize::Fixed(0);
196 unsigned Type::getScalarSizeInBits() const {
197 // It is safe to assume that the scalar types have a fixed size.
198 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
201 int Type::getFPMantissaWidth() const {
202 if (auto *VTy = dyn_cast<VectorType>(this))
203 return VTy->getElementType()->getFPMantissaWidth();
204 assert(isFloatingPointTy() && "Not a floating point type!");
205 if (getTypeID() == HalfTyID) return 11;
206 if (getTypeID() == BFloatTyID) return 8;
207 if (getTypeID() == FloatTyID) return 24;
208 if (getTypeID() == DoubleTyID) return 53;
209 if (getTypeID() == X86_FP80TyID) return 64;
210 if (getTypeID() == FP128TyID) return 113;
211 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
215 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
216 if (auto *ATy = dyn_cast<ArrayType>(this))
217 return ATy->getElementType()->isSized(Visited);
219 if (auto *VTy = dyn_cast<VectorType>(this))
220 return VTy->getElementType()->isSized(Visited);
222 if (auto *TTy = dyn_cast<TargetExtType>(this))
223 return TTy->getLayoutType()->isSized(Visited);
225 return cast<StructType>(this)->isSized(Visited);
228 //===----------------------------------------------------------------------===//
229 // Primitive 'Type' data
230 //===----------------------------------------------------------------------===//
232 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
233 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
234 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
235 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
236 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
237 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
238 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
239 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
240 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
241 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
242 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
243 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
244 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
246 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
247 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
248 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
249 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
250 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
251 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
253 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
254 return IntegerType::get(C, N);
257 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
258 return getHalfTy(C)->getPointerTo(AS);
261 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
262 return getBFloatTy(C)->getPointerTo(AS);
265 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
266 return getFloatTy(C)->getPointerTo(AS);
269 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
270 return getDoubleTy(C)->getPointerTo(AS);
273 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
274 return getX86_FP80Ty(C)->getPointerTo(AS);
277 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
278 return getFP128Ty(C)->getPointerTo(AS);
281 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
282 return getPPC_FP128Ty(C)->getPointerTo(AS);
285 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
286 return getX86_MMXTy(C)->getPointerTo(AS);
289 PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) {
290 return getX86_AMXTy(C)->getPointerTo(AS);
293 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
294 return getIntNTy(C, N)->getPointerTo(AS);
297 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
298 return getInt1Ty(C)->getPointerTo(AS);
301 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
302 return getInt8Ty(C)->getPointerTo(AS);
305 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
306 return getInt16Ty(C)->getPointerTo(AS);
309 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
310 return getInt32Ty(C)->getPointerTo(AS);
313 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
314 return getInt64Ty(C)->getPointerTo(AS);
317 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
318 // opaque pointer in addrspace(10)
319 static PointerType *Ty = PointerType::get(C, 10);
323 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
324 // opaque pointer in addrspace(20)
325 static PointerType *Ty = PointerType::get(C, 20);
329 //===----------------------------------------------------------------------===//
330 // IntegerType Implementation
331 //===----------------------------------------------------------------------===//
333 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
334 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
335 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
337 // Check for the built-in integer types
339 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
340 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
341 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
342 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
343 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
344 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
349 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
352 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
357 APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }
359 //===----------------------------------------------------------------------===//
360 // FunctionType Implementation
361 //===----------------------------------------------------------------------===//
363 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
365 : Type(Result->getContext(), FunctionTyID) {
366 Type **SubTys = reinterpret_cast<Type**>(this+1);
367 assert(isValidReturnType(Result) && "invalid return type for function");
368 setSubclassData(IsVarArgs);
372 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
373 assert(isValidArgumentType(Params[i]) &&
374 "Not a valid type for function argument!");
375 SubTys[i+1] = Params[i];
378 ContainedTys = SubTys;
379 NumContainedTys = Params.size() + 1; // + 1 for result type
382 // This is the factory function for the FunctionType class.
383 FunctionType *FunctionType::get(Type *ReturnType,
384 ArrayRef<Type*> Params, bool isVarArg) {
385 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
386 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
388 // Since we only want to allocate a fresh function type in case none is found
389 // and we don't want to perform two lookups (one for checking if existent and
390 // one for inserting the newly allocated one), here we instead lookup based on
391 // Key and update the reference to the function type in-place to a newly
392 // allocated one if not found.
393 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
394 if (Insertion.second) {
395 // The function type was not found. Allocate one and update FunctionTypes
397 FT = (FunctionType *)pImpl->Alloc.Allocate(
398 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
399 alignof(FunctionType));
400 new (FT) FunctionType(ReturnType, Params, isVarArg);
401 *Insertion.first = FT;
403 // The function type was found. Just return it.
404 FT = *Insertion.first;
409 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
410 return get(Result, std::nullopt, isVarArg);
413 bool FunctionType::isValidReturnType(Type *RetTy) {
414 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
415 !RetTy->isMetadataTy();
418 bool FunctionType::isValidArgumentType(Type *ArgTy) {
419 return ArgTy->isFirstClassType();
422 //===----------------------------------------------------------------------===//
423 // StructType Implementation
424 //===----------------------------------------------------------------------===//
426 // Primitive Constructors.
428 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
430 LLVMContextImpl *pImpl = Context.pImpl;
431 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
434 // Since we only want to allocate a fresh struct type in case none is found
435 // and we don't want to perform two lookups (one for checking if existent and
436 // one for inserting the newly allocated one), here we instead lookup based on
437 // Key and update the reference to the struct type in-place to a newly
438 // allocated one if not found.
439 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
440 if (Insertion.second) {
441 // The struct type was not found. Allocate one and update AnonStructTypes
443 ST = new (Context.pImpl->Alloc) StructType(Context);
444 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
445 ST->setBody(ETypes, isPacked);
446 *Insertion.first = ST;
448 // The struct type was found. Just return it.
449 ST = *Insertion.first;
455 bool StructType::containsScalableVectorType(
456 SmallPtrSetImpl<Type *> *Visited) const {
457 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
460 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
463 if (Visited && !Visited->insert(const_cast<StructType *>(this)).second)
466 for (Type *Ty : elements()) {
467 if (isa<ScalableVectorType>(Ty)) {
468 const_cast<StructType *>(this)->setSubclassData(
469 getSubclassData() | SCDB_ContainsScalableVector);
472 if (auto *STy = dyn_cast<StructType>(Ty)) {
473 if (STy->containsScalableVectorType(Visited)) {
474 const_cast<StructType *>(this)->setSubclassData(
475 getSubclassData() | SCDB_ContainsScalableVector);
481 // For structures that are opaque, return false but do not set the
482 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
483 // when it becomes non-opaque.
485 const_cast<StructType *>(this)->setSubclassData(
486 getSubclassData() | SCDB_NotContainsScalableVector);
490 bool StructType::containsHomogeneousScalableVectorTypes() const {
491 Type *FirstTy = getNumElements() > 0 ? elements()[0] : nullptr;
492 if (!FirstTy || !isa<ScalableVectorType>(FirstTy))
494 for (Type *Ty : elements())
500 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
501 assert(isOpaque() && "Struct body already set!");
503 setSubclassData(getSubclassData() | SCDB_HasBody);
505 setSubclassData(getSubclassData() | SCDB_Packed);
507 NumContainedTys = Elements.size();
509 if (Elements.empty()) {
510 ContainedTys = nullptr;
514 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
517 void StructType::setName(StringRef Name) {
518 if (Name == getName()) return;
520 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
522 using EntryTy = StringMap<StructType *>::MapEntryTy;
524 // If this struct already had a name, remove its symbol table entry. Don't
525 // delete the data yet because it may be part of the new name.
526 if (SymbolTableEntry)
527 SymbolTable.remove((EntryTy *)SymbolTableEntry);
529 // If this is just removing the name, we're done.
531 if (SymbolTableEntry) {
532 // Delete the old string data.
533 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
534 SymbolTableEntry = nullptr;
539 // Look up the entry for the name.
541 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
543 // While we have a name collision, try a random rename.
544 if (!IterBool.second) {
545 SmallString<64> TempStr(Name);
546 TempStr.push_back('.');
547 raw_svector_ostream TmpStream(TempStr);
548 unsigned NameSize = Name.size();
551 TempStr.resize(NameSize + 1);
552 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
554 IterBool = getContext().pImpl->NamedStructTypes.insert(
555 std::make_pair(TmpStream.str(), this));
556 } while (!IterBool.second);
559 // Delete the old string data.
560 if (SymbolTableEntry)
561 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
562 SymbolTableEntry = &*IterBool.first;
565 //===----------------------------------------------------------------------===//
566 // StructType Helper functions.
568 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
569 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
575 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
576 return get(Context, std::nullopt, isPacked);
579 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
580 StringRef Name, bool isPacked) {
581 StructType *ST = create(Context, Name);
582 ST->setBody(Elements, isPacked);
586 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
587 return create(Context, Elements, StringRef());
590 StructType *StructType::create(LLVMContext &Context) {
591 return create(Context, StringRef());
594 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
596 assert(!Elements.empty() &&
597 "This method may not be invoked with an empty list");
598 return create(Elements[0]->getContext(), Elements, Name, isPacked);
601 StructType *StructType::create(ArrayRef<Type*> Elements) {
602 assert(!Elements.empty() &&
603 "This method may not be invoked with an empty list");
604 return create(Elements[0]->getContext(), Elements, StringRef());
607 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
608 if ((getSubclassData() & SCDB_IsSized) != 0)
613 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
616 // Okay, our struct is sized if all of the elements are, but if one of the
617 // elements is opaque, the struct isn't sized *yet*, but may become sized in
618 // the future, so just bail out without caching.
619 // The ONLY special case inside a struct that is considered sized is when the
620 // elements are homogeneous of a scalable vector type.
621 if (containsHomogeneousScalableVectorTypes()) {
622 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
626 for (Type *Ty : elements()) {
627 // If the struct contains a scalable vector type, don't consider it sized.
628 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
629 // special case right now is a structure of homogenous scalable vector
630 // types and is handled by the if-statement before this for-loop.
631 if (Ty->isScalableTy())
633 if (!Ty->isSized(Visited))
637 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
638 // we find a sized type, as types can only move from opaque to sized, not the
640 const_cast<StructType*>(this)->setSubclassData(
641 getSubclassData() | SCDB_IsSized);
645 StringRef StructType::getName() const {
646 assert(!isLiteral() && "Literal structs never have names");
647 if (!SymbolTableEntry) return StringRef();
649 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
652 bool StructType::isValidElementType(Type *ElemTy) {
653 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
654 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
655 !ElemTy->isTokenTy();
658 bool StructType::isLayoutIdentical(StructType *Other) const {
659 if (this == Other) return true;
661 if (isPacked() != Other->isPacked())
664 return elements() == Other->elements();
667 Type *StructType::getTypeAtIndex(const Value *V) const {
668 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
669 assert(indexValid(Idx) && "Invalid structure index!");
670 return getElementType(Idx);
673 bool StructType::indexValid(const Value *V) const {
674 // Structure indexes require (vectors of) 32-bit integer constants. In the
675 // vector case all of the indices must be equal.
676 if (!V->getType()->isIntOrIntVectorTy(32))
678 if (isa<ScalableVectorType>(V->getType()))
680 const Constant *C = dyn_cast<Constant>(V);
681 if (C && V->getType()->isVectorTy())
682 C = C->getSplatValue();
683 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
684 return CU && CU->getZExtValue() < getNumElements();
687 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
688 return C.pImpl->NamedStructTypes.lookup(Name);
691 //===----------------------------------------------------------------------===//
692 // ArrayType Implementation
693 //===----------------------------------------------------------------------===//
695 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
696 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
698 ContainedTys = &ContainedType;
702 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
703 assert(isValidElementType(ElementType) && "Invalid type for array element!");
705 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
707 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
710 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
714 bool ArrayType::isValidElementType(Type *ElemTy) {
715 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
716 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
717 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy() &&
718 !isa<ScalableVectorType>(ElemTy);
721 //===----------------------------------------------------------------------===//
722 // VectorType Implementation
723 //===----------------------------------------------------------------------===//
725 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
726 : Type(ElType->getContext(), TID), ContainedType(ElType),
727 ElementQuantity(EQ) {
728 ContainedTys = &ContainedType;
732 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
734 return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
736 return FixedVectorType::get(ElementType, EC.getKnownMinValue());
739 bool VectorType::isValidElementType(Type *ElemTy) {
740 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
741 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID;
744 //===----------------------------------------------------------------------===//
745 // FixedVectorType Implementation
746 //===----------------------------------------------------------------------===//
748 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
749 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
750 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
751 "be an integer, floating point, or "
754 auto EC = ElementCount::getFixed(NumElts);
756 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
757 VectorType *&Entry = ElementType->getContext()
758 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
761 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
762 return cast<FixedVectorType>(Entry);
765 //===----------------------------------------------------------------------===//
766 // ScalableVectorType Implementation
767 //===----------------------------------------------------------------------===//
769 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
770 unsigned MinNumElts) {
771 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
772 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
773 "be an integer, floating point, or "
776 auto EC = ElementCount::getScalable(MinNumElts);
778 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
779 VectorType *&Entry = ElementType->getContext()
780 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
783 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
784 return cast<ScalableVectorType>(Entry);
787 //===----------------------------------------------------------------------===//
788 // PointerType Implementation
789 //===----------------------------------------------------------------------===//
791 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
792 assert(EltTy && "Can't get a pointer to <null> type!");
793 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
795 // Automatically convert typed pointers to opaque pointers.
796 return get(EltTy->getContext(), AddressSpace);
799 PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
800 LLVMContextImpl *CImpl = C.pImpl;
802 // Since AddressSpace #0 is the common case, we special case it.
803 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
804 : CImpl->PointerTypes[AddressSpace];
807 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
811 PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
812 : Type(C, PointerTyID) {
813 setSubclassData(AddrSpace);
816 PointerType *Type::getPointerTo(unsigned AddrSpace) const {
817 return PointerType::get(const_cast<Type*>(this), AddrSpace);
820 bool PointerType::isValidElementType(Type *ElemTy) {
821 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
822 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
823 !ElemTy->isX86_AMXTy();
826 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
827 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
830 //===----------------------------------------------------------------------===//
831 // TargetExtType Implementation
832 //===----------------------------------------------------------------------===//
834 TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
835 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
836 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {
837 NumContainedTys = Types.size();
839 // Parameter storage immediately follows the class in allocation.
840 Type **Params = reinterpret_cast<Type **>(this + 1);
841 ContainedTys = Params;
842 for (Type *T : Types)
845 setSubclassData(Ints.size());
846 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
847 IntParams = IntParamSpace;
848 for (unsigned IntParam : Ints)
849 *IntParamSpace++ = IntParam;
852 TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
853 ArrayRef<Type *> Types,
854 ArrayRef<unsigned> Ints) {
855 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
857 // Since we only want to allocate a fresh target type in case none is found
858 // and we don't want to perform two lookups (one for checking if existent and
859 // one for inserting the newly allocated one), here we instead lookup based on
860 // Key and update the reference to the target type in-place to a newly
861 // allocated one if not found.
862 auto Insertion = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);
863 if (Insertion.second) {
864 // The target type was not found. Allocate one and update TargetExtTypes
866 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
867 sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
868 sizeof(unsigned) * Ints.size(),
869 alignof(TargetExtType));
870 new (TT) TargetExtType(C, Name, Types, Ints);
871 *Insertion.first = TT;
873 // The target type was found. Just return it.
874 TT = *Insertion.first;
880 struct TargetTypeInfo {
884 template <typename... ArgTys>
885 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
886 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {}
888 } // anonymous namespace
890 static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
891 LLVMContext &C = Ty->getContext();
892 StringRef Name = Ty->getName();
893 if (Name.startswith("spirv."))
894 return TargetTypeInfo(Type::getInt8PtrTy(C, 0), TargetExtType::HasZeroInit,
895 TargetExtType::CanBeGlobal);
897 // Opaque types in the AArch64 name space.
898 if (Name == "aarch64.svcount")
899 return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16));
901 return TargetTypeInfo(Type::getVoidTy(C));
904 Type *TargetExtType::getLayoutType() const {
905 return getTargetTypeInfo(this).LayoutType;
908 bool TargetExtType::hasProperty(Property Prop) const {
909 uint64_t Properties = getTargetTypeInfo(this).Properties;
910 return (Properties & Prop) == Prop;