Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / IR / Type.cpp
blob006278d16484c1cad372ef20d20c7fa47694031c
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.
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"
27 #include <cassert>
28 #include <utility>
30 using namespace llvm;
32 //===----------------------------------------------------------------------===//
33 // Type Class Implementation
34 //===----------------------------------------------------------------------===//
36 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
37 switch (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);
51 default:
52 return nullptr;
56 bool Type::isIntegerTy(unsigned Bitwidth) const {
57 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
60 bool Type::isScalableTy() const {
61 if (const auto *ATy = dyn_cast<ArrayType>(this))
62 return ATy->getElementType()->isScalableTy();
63 if (const auto *STy = dyn_cast<StructType>(this)) {
64 SmallPtrSet<Type *, 4> Visited;
65 return STy->containsScalableVectorType(&Visited);
67 return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();
70 const fltSemantics &Type::getFltSemantics() const {
71 switch (getTypeID()) {
72 case HalfTyID: return APFloat::IEEEhalf();
73 case BFloatTyID: return APFloat::BFloat();
74 case FloatTyID: return APFloat::IEEEsingle();
75 case DoubleTyID: return APFloat::IEEEdouble();
76 case X86_FP80TyID: return APFloat::x87DoubleExtended();
77 case FP128TyID: return APFloat::IEEEquad();
78 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
79 default: llvm_unreachable("Invalid floating type");
83 bool Type::isIEEE() const {
84 return APFloat::getZero(getFltSemantics()).isIEEE();
87 bool Type::isScalableTargetExtTy() const {
88 if (auto *TT = dyn_cast<TargetExtType>(this))
89 return isa<ScalableVectorType>(TT->getLayoutType());
90 return false;
93 Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
94 Type *Ty;
95 if (&S == &APFloat::IEEEhalf())
96 Ty = Type::getHalfTy(C);
97 else if (&S == &APFloat::BFloat())
98 Ty = Type::getBFloatTy(C);
99 else if (&S == &APFloat::IEEEsingle())
100 Ty = Type::getFloatTy(C);
101 else if (&S == &APFloat::IEEEdouble())
102 Ty = Type::getDoubleTy(C);
103 else if (&S == &APFloat::x87DoubleExtended())
104 Ty = Type::getX86_FP80Ty(C);
105 else if (&S == &APFloat::IEEEquad())
106 Ty = Type::getFP128Ty(C);
107 else {
108 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
109 Ty = Type::getPPC_FP128Ty(C);
111 return Ty;
114 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
115 // Identity cast means no change so return true
116 if (this == Ty)
117 return true;
119 // They are not convertible unless they are at least first class types
120 if (!this->isFirstClassType() || !Ty->isFirstClassType())
121 return false;
123 // Vector -> Vector conversions are always lossless if the two vector types
124 // have the same size, otherwise not.
125 if (isa<VectorType>(this) && isa<VectorType>(Ty))
126 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
128 // 64-bit fixed width vector types can be losslessly converted to x86mmx.
129 if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
130 getPrimitiveSizeInBits().getFixedValue() == 64)
131 return true;
132 if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
133 Ty->getPrimitiveSizeInBits().getFixedValue() == 64)
134 return true;
136 // 8192-bit fixed width vector types can be losslessly converted to x86amx.
137 if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
138 getPrimitiveSizeInBits().getFixedValue() == 8192)
139 return true;
140 if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
141 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
142 return true;
144 // At this point we have only various mismatches of the first class types
145 // remaining and ptr->ptr. Just select the lossless conversions. Everything
146 // else is not lossless. Conservatively assume we can't losslessly convert
147 // between pointers with different address spaces.
148 if (auto *PTy = dyn_cast<PointerType>(this)) {
149 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
150 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
151 return false;
153 return false; // Other types have no identity values
156 bool Type::isEmptyTy() const {
157 if (auto *ATy = dyn_cast<ArrayType>(this)) {
158 unsigned NumElements = ATy->getNumElements();
159 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
162 if (auto *STy = dyn_cast<StructType>(this)) {
163 unsigned NumElements = STy->getNumElements();
164 for (unsigned i = 0; i < NumElements; ++i)
165 if (!STy->getElementType(i)->isEmptyTy())
166 return false;
167 return true;
170 return false;
173 TypeSize Type::getPrimitiveSizeInBits() const {
174 switch (getTypeID()) {
175 case Type::HalfTyID: return TypeSize::Fixed(16);
176 case Type::BFloatTyID: return TypeSize::Fixed(16);
177 case Type::FloatTyID: return TypeSize::Fixed(32);
178 case Type::DoubleTyID: return TypeSize::Fixed(64);
179 case Type::X86_FP80TyID: return TypeSize::Fixed(80);
180 case Type::FP128TyID: return TypeSize::Fixed(128);
181 case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
182 case Type::X86_MMXTyID: return TypeSize::Fixed(64);
183 case Type::X86_AMXTyID: return TypeSize::Fixed(8192);
184 case Type::IntegerTyID:
185 return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
186 case Type::FixedVectorTyID:
187 case Type::ScalableVectorTyID: {
188 const VectorType *VTy = cast<VectorType>(this);
189 ElementCount EC = VTy->getElementCount();
190 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
191 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
192 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
194 default: return TypeSize::Fixed(0);
198 unsigned Type::getScalarSizeInBits() const {
199 // It is safe to assume that the scalar types have a fixed size.
200 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
203 int Type::getFPMantissaWidth() const {
204 if (auto *VTy = dyn_cast<VectorType>(this))
205 return VTy->getElementType()->getFPMantissaWidth();
206 assert(isFloatingPointTy() && "Not a floating point type!");
207 if (getTypeID() == HalfTyID) return 11;
208 if (getTypeID() == BFloatTyID) return 8;
209 if (getTypeID() == FloatTyID) return 24;
210 if (getTypeID() == DoubleTyID) return 53;
211 if (getTypeID() == X86_FP80TyID) return 64;
212 if (getTypeID() == FP128TyID) return 113;
213 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
214 return -1;
217 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
218 if (auto *ATy = dyn_cast<ArrayType>(this))
219 return ATy->getElementType()->isSized(Visited);
221 if (auto *VTy = dyn_cast<VectorType>(this))
222 return VTy->getElementType()->isSized(Visited);
224 if (auto *TTy = dyn_cast<TargetExtType>(this))
225 return TTy->getLayoutType()->isSized(Visited);
227 return cast<StructType>(this)->isSized(Visited);
230 //===----------------------------------------------------------------------===//
231 // Primitive 'Type' data
232 //===----------------------------------------------------------------------===//
234 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
235 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
236 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
237 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
238 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
239 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
240 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
241 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
242 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
243 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
244 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
245 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
246 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
248 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
249 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
250 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
251 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
252 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
253 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
255 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
256 return IntegerType::get(C, N);
259 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
260 return PointerType::get(C, AS);
263 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
264 // opaque pointer in addrspace(10)
265 static PointerType *Ty = PointerType::get(C, 10);
266 return Ty;
269 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
270 // opaque pointer in addrspace(20)
271 static PointerType *Ty = PointerType::get(C, 20);
272 return Ty;
275 //===----------------------------------------------------------------------===//
276 // IntegerType Implementation
277 //===----------------------------------------------------------------------===//
279 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
280 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
281 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
283 // Check for the built-in integer types
284 switch (NumBits) {
285 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
286 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
287 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
288 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
289 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
290 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
291 default:
292 break;
295 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
297 if (!Entry)
298 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
300 return Entry;
303 APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }
305 //===----------------------------------------------------------------------===//
306 // FunctionType Implementation
307 //===----------------------------------------------------------------------===//
309 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
310 bool IsVarArgs)
311 : Type(Result->getContext(), FunctionTyID) {
312 Type **SubTys = reinterpret_cast<Type**>(this+1);
313 assert(isValidReturnType(Result) && "invalid return type for function");
314 setSubclassData(IsVarArgs);
316 SubTys[0] = Result;
318 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
319 assert(isValidArgumentType(Params[i]) &&
320 "Not a valid type for function argument!");
321 SubTys[i+1] = Params[i];
324 ContainedTys = SubTys;
325 NumContainedTys = Params.size() + 1; // + 1 for result type
328 // This is the factory function for the FunctionType class.
329 FunctionType *FunctionType::get(Type *ReturnType,
330 ArrayRef<Type*> Params, bool isVarArg) {
331 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
332 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
333 FunctionType *FT;
334 // Since we only want to allocate a fresh function type in case none is found
335 // and we don't want to perform two lookups (one for checking if existent and
336 // one for inserting the newly allocated one), here we instead lookup based on
337 // Key and update the reference to the function type in-place to a newly
338 // allocated one if not found.
339 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
340 if (Insertion.second) {
341 // The function type was not found. Allocate one and update FunctionTypes
342 // in-place.
343 FT = (FunctionType *)pImpl->Alloc.Allocate(
344 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
345 alignof(FunctionType));
346 new (FT) FunctionType(ReturnType, Params, isVarArg);
347 *Insertion.first = FT;
348 } else {
349 // The function type was found. Just return it.
350 FT = *Insertion.first;
352 return FT;
355 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
356 return get(Result, std::nullopt, isVarArg);
359 bool FunctionType::isValidReturnType(Type *RetTy) {
360 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
361 !RetTy->isMetadataTy();
364 bool FunctionType::isValidArgumentType(Type *ArgTy) {
365 return ArgTy->isFirstClassType();
368 //===----------------------------------------------------------------------===//
369 // StructType Implementation
370 //===----------------------------------------------------------------------===//
372 // Primitive Constructors.
374 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
375 bool isPacked) {
376 LLVMContextImpl *pImpl = Context.pImpl;
377 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
379 StructType *ST;
380 // Since we only want to allocate a fresh struct type in case none is found
381 // and we don't want to perform two lookups (one for checking if existent and
382 // one for inserting the newly allocated one), here we instead lookup based on
383 // Key and update the reference to the struct type in-place to a newly
384 // allocated one if not found.
385 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
386 if (Insertion.second) {
387 // The struct type was not found. Allocate one and update AnonStructTypes
388 // in-place.
389 ST = new (Context.pImpl->Alloc) StructType(Context);
390 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
391 ST->setBody(ETypes, isPacked);
392 *Insertion.first = ST;
393 } else {
394 // The struct type was found. Just return it.
395 ST = *Insertion.first;
398 return ST;
401 bool StructType::containsScalableVectorType(
402 SmallPtrSetImpl<Type *> *Visited) const {
403 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
404 return true;
406 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
407 return false;
409 if (Visited && !Visited->insert(const_cast<StructType *>(this)).second)
410 return false;
412 for (Type *Ty : elements()) {
413 if (isa<ScalableVectorType>(Ty)) {
414 const_cast<StructType *>(this)->setSubclassData(
415 getSubclassData() | SCDB_ContainsScalableVector);
416 return true;
418 if (auto *STy = dyn_cast<StructType>(Ty)) {
419 if (STy->containsScalableVectorType(Visited)) {
420 const_cast<StructType *>(this)->setSubclassData(
421 getSubclassData() | SCDB_ContainsScalableVector);
422 return true;
427 // For structures that are opaque, return false but do not set the
428 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
429 // when it becomes non-opaque.
430 if (!isOpaque())
431 const_cast<StructType *>(this)->setSubclassData(
432 getSubclassData() | SCDB_NotContainsScalableVector);
433 return false;
436 bool StructType::containsHomogeneousScalableVectorTypes() const {
437 Type *FirstTy = getNumElements() > 0 ? elements()[0] : nullptr;
438 if (!FirstTy || !isa<ScalableVectorType>(FirstTy))
439 return false;
440 for (Type *Ty : elements())
441 if (Ty != FirstTy)
442 return false;
443 return true;
446 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
447 assert(isOpaque() && "Struct body already set!");
449 setSubclassData(getSubclassData() | SCDB_HasBody);
450 if (isPacked)
451 setSubclassData(getSubclassData() | SCDB_Packed);
453 NumContainedTys = Elements.size();
455 if (Elements.empty()) {
456 ContainedTys = nullptr;
457 return;
460 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
463 void StructType::setName(StringRef Name) {
464 if (Name == getName()) return;
466 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
468 using EntryTy = StringMap<StructType *>::MapEntryTy;
470 // If this struct already had a name, remove its symbol table entry. Don't
471 // delete the data yet because it may be part of the new name.
472 if (SymbolTableEntry)
473 SymbolTable.remove((EntryTy *)SymbolTableEntry);
475 // If this is just removing the name, we're done.
476 if (Name.empty()) {
477 if (SymbolTableEntry) {
478 // Delete the old string data.
479 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
480 SymbolTableEntry = nullptr;
482 return;
485 // Look up the entry for the name.
486 auto IterBool =
487 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
489 // While we have a name collision, try a random rename.
490 if (!IterBool.second) {
491 SmallString<64> TempStr(Name);
492 TempStr.push_back('.');
493 raw_svector_ostream TmpStream(TempStr);
494 unsigned NameSize = Name.size();
496 do {
497 TempStr.resize(NameSize + 1);
498 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
500 IterBool = getContext().pImpl->NamedStructTypes.insert(
501 std::make_pair(TmpStream.str(), this));
502 } while (!IterBool.second);
505 // Delete the old string data.
506 if (SymbolTableEntry)
507 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
508 SymbolTableEntry = &*IterBool.first;
511 //===----------------------------------------------------------------------===//
512 // StructType Helper functions.
514 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
515 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
516 if (!Name.empty())
517 ST->setName(Name);
518 return ST;
521 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
522 return get(Context, std::nullopt, isPacked);
525 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
526 StringRef Name, bool isPacked) {
527 StructType *ST = create(Context, Name);
528 ST->setBody(Elements, isPacked);
529 return ST;
532 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
533 return create(Context, Elements, StringRef());
536 StructType *StructType::create(LLVMContext &Context) {
537 return create(Context, StringRef());
540 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
541 bool isPacked) {
542 assert(!Elements.empty() &&
543 "This method may not be invoked with an empty list");
544 return create(Elements[0]->getContext(), Elements, Name, isPacked);
547 StructType *StructType::create(ArrayRef<Type*> Elements) {
548 assert(!Elements.empty() &&
549 "This method may not be invoked with an empty list");
550 return create(Elements[0]->getContext(), Elements, StringRef());
553 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
554 if ((getSubclassData() & SCDB_IsSized) != 0)
555 return true;
556 if (isOpaque())
557 return false;
559 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
560 return false;
562 // Okay, our struct is sized if all of the elements are, but if one of the
563 // elements is opaque, the struct isn't sized *yet*, but may become sized in
564 // the future, so just bail out without caching.
565 // The ONLY special case inside a struct that is considered sized is when the
566 // elements are homogeneous of a scalable vector type.
567 if (containsHomogeneousScalableVectorTypes()) {
568 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
569 SCDB_IsSized);
570 return true;
572 for (Type *Ty : elements()) {
573 // If the struct contains a scalable vector type, don't consider it sized.
574 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
575 // special case right now is a structure of homogenous scalable vector
576 // types and is handled by the if-statement before this for-loop.
577 if (Ty->isScalableTy())
578 return false;
579 if (!Ty->isSized(Visited))
580 return false;
583 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
584 // we find a sized type, as types can only move from opaque to sized, not the
585 // other way.
586 const_cast<StructType*>(this)->setSubclassData(
587 getSubclassData() | SCDB_IsSized);
588 return true;
591 StringRef StructType::getName() const {
592 assert(!isLiteral() && "Literal structs never have names");
593 if (!SymbolTableEntry) return StringRef();
595 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
598 bool StructType::isValidElementType(Type *ElemTy) {
599 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
600 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
601 !ElemTy->isTokenTy();
604 bool StructType::isLayoutIdentical(StructType *Other) const {
605 if (this == Other) return true;
607 if (isPacked() != Other->isPacked())
608 return false;
610 return elements() == Other->elements();
613 Type *StructType::getTypeAtIndex(const Value *V) const {
614 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
615 assert(indexValid(Idx) && "Invalid structure index!");
616 return getElementType(Idx);
619 bool StructType::indexValid(const Value *V) const {
620 // Structure indexes require (vectors of) 32-bit integer constants. In the
621 // vector case all of the indices must be equal.
622 if (!V->getType()->isIntOrIntVectorTy(32))
623 return false;
624 if (isa<ScalableVectorType>(V->getType()))
625 return false;
626 const Constant *C = dyn_cast<Constant>(V);
627 if (C && V->getType()->isVectorTy())
628 C = C->getSplatValue();
629 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
630 return CU && CU->getZExtValue() < getNumElements();
633 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
634 return C.pImpl->NamedStructTypes.lookup(Name);
637 //===----------------------------------------------------------------------===//
638 // ArrayType Implementation
639 //===----------------------------------------------------------------------===//
641 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
642 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
643 NumElements(NumEl) {
644 ContainedTys = &ContainedType;
645 NumContainedTys = 1;
648 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
649 assert(isValidElementType(ElementType) && "Invalid type for array element!");
651 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
652 ArrayType *&Entry =
653 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
655 if (!Entry)
656 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
657 return Entry;
660 bool ArrayType::isValidElementType(Type *ElemTy) {
661 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
662 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
663 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy();
666 //===----------------------------------------------------------------------===//
667 // VectorType Implementation
668 //===----------------------------------------------------------------------===//
670 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
671 : Type(ElType->getContext(), TID), ContainedType(ElType),
672 ElementQuantity(EQ) {
673 ContainedTys = &ContainedType;
674 NumContainedTys = 1;
677 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
678 if (EC.isScalable())
679 return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
680 else
681 return FixedVectorType::get(ElementType, EC.getKnownMinValue());
684 bool VectorType::isValidElementType(Type *ElemTy) {
685 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
686 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID;
689 //===----------------------------------------------------------------------===//
690 // FixedVectorType Implementation
691 //===----------------------------------------------------------------------===//
693 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
694 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
695 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
696 "be an integer, floating point, or "
697 "pointer type.");
699 auto EC = ElementCount::getFixed(NumElts);
701 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
702 VectorType *&Entry = ElementType->getContext()
703 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
705 if (!Entry)
706 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
707 return cast<FixedVectorType>(Entry);
710 //===----------------------------------------------------------------------===//
711 // ScalableVectorType Implementation
712 //===----------------------------------------------------------------------===//
714 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
715 unsigned MinNumElts) {
716 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
717 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
718 "be an integer, floating point, or "
719 "pointer type.");
721 auto EC = ElementCount::getScalable(MinNumElts);
723 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
724 VectorType *&Entry = ElementType->getContext()
725 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
727 if (!Entry)
728 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
729 return cast<ScalableVectorType>(Entry);
732 //===----------------------------------------------------------------------===//
733 // PointerType Implementation
734 //===----------------------------------------------------------------------===//
736 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
737 assert(EltTy && "Can't get a pointer to <null> type!");
738 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
740 // Automatically convert typed pointers to opaque pointers.
741 return get(EltTy->getContext(), AddressSpace);
744 PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
745 LLVMContextImpl *CImpl = C.pImpl;
747 // Since AddressSpace #0 is the common case, we special case it.
748 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
749 : CImpl->PointerTypes[AddressSpace];
751 if (!Entry)
752 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
753 return Entry;
756 PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
757 : Type(C, PointerTyID) {
758 setSubclassData(AddrSpace);
761 PointerType *Type::getPointerTo(unsigned AddrSpace) const {
762 return PointerType::get(const_cast<Type*>(this), AddrSpace);
765 bool PointerType::isValidElementType(Type *ElemTy) {
766 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
767 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
768 !ElemTy->isX86_AMXTy();
771 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
772 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
775 //===----------------------------------------------------------------------===//
776 // TargetExtType Implementation
777 //===----------------------------------------------------------------------===//
779 TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
780 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
781 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {
782 NumContainedTys = Types.size();
784 // Parameter storage immediately follows the class in allocation.
785 Type **Params = reinterpret_cast<Type **>(this + 1);
786 ContainedTys = Params;
787 for (Type *T : Types)
788 *Params++ = T;
790 setSubclassData(Ints.size());
791 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
792 IntParams = IntParamSpace;
793 for (unsigned IntParam : Ints)
794 *IntParamSpace++ = IntParam;
797 TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
798 ArrayRef<Type *> Types,
799 ArrayRef<unsigned> Ints) {
800 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
801 TargetExtType *TT;
802 // Since we only want to allocate a fresh target type in case none is found
803 // and we don't want to perform two lookups (one for checking if existent and
804 // one for inserting the newly allocated one), here we instead lookup based on
805 // Key and update the reference to the target type in-place to a newly
806 // allocated one if not found.
807 auto Insertion = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);
808 if (Insertion.second) {
809 // The target type was not found. Allocate one and update TargetExtTypes
810 // in-place.
811 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
812 sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
813 sizeof(unsigned) * Ints.size(),
814 alignof(TargetExtType));
815 new (TT) TargetExtType(C, Name, Types, Ints);
816 *Insertion.first = TT;
817 } else {
818 // The target type was found. Just return it.
819 TT = *Insertion.first;
821 return TT;
824 namespace {
825 struct TargetTypeInfo {
826 Type *LayoutType;
827 uint64_t Properties;
829 template <typename... ArgTys>
830 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
831 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {}
833 } // anonymous namespace
835 static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
836 LLVMContext &C = Ty->getContext();
837 StringRef Name = Ty->getName();
838 if (Name.startswith("spirv."))
839 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,
840 TargetExtType::CanBeGlobal);
842 // Opaque types in the AArch64 name space.
843 if (Name == "aarch64.svcount")
844 return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),
845 TargetExtType::HasZeroInit);
847 return TargetTypeInfo(Type::getVoidTy(C));
850 Type *TargetExtType::getLayoutType() const {
851 return getTargetTypeInfo(this).LayoutType;
854 bool TargetExtType::hasProperty(Property Prop) const {
855 uint64_t Properties = getTargetTypeInfo(this).Properties;
856 return (Properties & Prop) == Prop;