[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
[llvm-project.git] / llvm / lib / IR / Type.cpp
blob277985b6b00a31d7e3db949aa3324957e96f67b2
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/SetVector.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/Value.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/TypeSize.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/TargetParser/RISCVTargetParser.h"
30 #include <cassert>
31 #include <utility>
33 using namespace llvm;
35 //===----------------------------------------------------------------------===//
36 // Type Class Implementation
37 //===----------------------------------------------------------------------===//
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 BFloatTyID : return getBFloatTy(C);
44 case FloatTyID : return getFloatTy(C);
45 case DoubleTyID : return getDoubleTy(C);
46 case X86_FP80TyID : return getX86_FP80Ty(C);
47 case FP128TyID : return getFP128Ty(C);
48 case PPC_FP128TyID : return getPPC_FP128Ty(C);
49 case LabelTyID : return getLabelTy(C);
50 case MetadataTyID : return getMetadataTy(C);
51 case X86_AMXTyID : return getX86_AMXTy(C);
52 case TokenTyID : return getTokenTy(C);
53 default:
54 return nullptr;
58 bool Type::isIntegerTy(unsigned Bitwidth) const {
59 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
62 bool Type::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {
63 if (const auto *ATy = dyn_cast<ArrayType>(this))
64 return ATy->getElementType()->isScalableTy(Visited);
65 if (const auto *STy = dyn_cast<StructType>(this))
66 return STy->isScalableTy(Visited);
67 return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();
70 bool Type::isScalableTy() const {
71 SmallPtrSet<const Type *, 4> Visited;
72 return isScalableTy(Visited);
75 bool Type::containsNonGlobalTargetExtType(
76 SmallPtrSetImpl<const Type *> &Visited) const {
77 if (const auto *ATy = dyn_cast<ArrayType>(this))
78 return ATy->getElementType()->containsNonGlobalTargetExtType(Visited);
79 if (const auto *STy = dyn_cast<StructType>(this))
80 return STy->containsNonGlobalTargetExtType(Visited);
81 if (auto *TT = dyn_cast<TargetExtType>(this))
82 return !TT->hasProperty(TargetExtType::CanBeGlobal);
83 return false;
86 bool Type::containsNonGlobalTargetExtType() const {
87 SmallPtrSet<const Type *, 4> Visited;
88 return containsNonGlobalTargetExtType(Visited);
91 bool Type::containsNonLocalTargetExtType(
92 SmallPtrSetImpl<const Type *> &Visited) const {
93 if (const auto *ATy = dyn_cast<ArrayType>(this))
94 return ATy->getElementType()->containsNonLocalTargetExtType(Visited);
95 if (const auto *STy = dyn_cast<StructType>(this))
96 return STy->containsNonLocalTargetExtType(Visited);
97 if (auto *TT = dyn_cast<TargetExtType>(this))
98 return !TT->hasProperty(TargetExtType::CanBeLocal);
99 return false;
102 bool Type::containsNonLocalTargetExtType() const {
103 SmallPtrSet<const Type *, 4> Visited;
104 return containsNonLocalTargetExtType(Visited);
107 const fltSemantics &Type::getFltSemantics() const {
108 switch (getTypeID()) {
109 case HalfTyID: return APFloat::IEEEhalf();
110 case BFloatTyID: return APFloat::BFloat();
111 case FloatTyID: return APFloat::IEEEsingle();
112 case DoubleTyID: return APFloat::IEEEdouble();
113 case X86_FP80TyID: return APFloat::x87DoubleExtended();
114 case FP128TyID: return APFloat::IEEEquad();
115 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
116 default: llvm_unreachable("Invalid floating type");
120 bool Type::isIEEE() const {
121 return APFloat::getZero(getFltSemantics()).isIEEE();
124 bool Type::isScalableTargetExtTy() const {
125 if (auto *TT = dyn_cast<TargetExtType>(this))
126 return isa<ScalableVectorType>(TT->getLayoutType());
127 return false;
130 Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
131 Type *Ty;
132 if (&S == &APFloat::IEEEhalf())
133 Ty = Type::getHalfTy(C);
134 else if (&S == &APFloat::BFloat())
135 Ty = Type::getBFloatTy(C);
136 else if (&S == &APFloat::IEEEsingle())
137 Ty = Type::getFloatTy(C);
138 else if (&S == &APFloat::IEEEdouble())
139 Ty = Type::getDoubleTy(C);
140 else if (&S == &APFloat::x87DoubleExtended())
141 Ty = Type::getX86_FP80Ty(C);
142 else if (&S == &APFloat::IEEEquad())
143 Ty = Type::getFP128Ty(C);
144 else {
145 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
146 Ty = Type::getPPC_FP128Ty(C);
148 return Ty;
151 bool Type::isRISCVVectorTupleTy() const {
152 if (!isTargetExtTy())
153 return false;
155 return cast<TargetExtType>(this)->getName() == "riscv.vector.tuple";
158 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
159 // Identity cast means no change so return true
160 if (this == Ty)
161 return true;
163 // They are not convertible unless they are at least first class types
164 if (!this->isFirstClassType() || !Ty->isFirstClassType())
165 return false;
167 // Vector -> Vector conversions are always lossless if the two vector types
168 // have the same size, otherwise not.
169 if (isa<VectorType>(this) && isa<VectorType>(Ty))
170 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
172 // 8192-bit fixed width vector types can be losslessly converted to x86amx.
173 if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
174 getPrimitiveSizeInBits().getFixedValue() == 8192)
175 return true;
176 if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
177 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
178 return true;
180 // Conservatively assume we can't losslessly convert between pointers with
181 // different address spaces.
182 return false;
185 bool Type::isEmptyTy() const {
186 if (auto *ATy = dyn_cast<ArrayType>(this)) {
187 unsigned NumElements = ATy->getNumElements();
188 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
191 if (auto *STy = dyn_cast<StructType>(this)) {
192 unsigned NumElements = STy->getNumElements();
193 for (unsigned i = 0; i < NumElements; ++i)
194 if (!STy->getElementType(i)->isEmptyTy())
195 return false;
196 return true;
199 return false;
202 TypeSize Type::getPrimitiveSizeInBits() const {
203 switch (getTypeID()) {
204 case Type::HalfTyID:
205 return TypeSize::getFixed(16);
206 case Type::BFloatTyID:
207 return TypeSize::getFixed(16);
208 case Type::FloatTyID:
209 return TypeSize::getFixed(32);
210 case Type::DoubleTyID:
211 return TypeSize::getFixed(64);
212 case Type::X86_FP80TyID:
213 return TypeSize::getFixed(80);
214 case Type::FP128TyID:
215 return TypeSize::getFixed(128);
216 case Type::PPC_FP128TyID:
217 return TypeSize::getFixed(128);
218 case Type::X86_AMXTyID:
219 return TypeSize::getFixed(8192);
220 case Type::IntegerTyID:
221 return TypeSize::getFixed(cast<IntegerType>(this)->getBitWidth());
222 case Type::FixedVectorTyID:
223 case Type::ScalableVectorTyID: {
224 const VectorType *VTy = cast<VectorType>(this);
225 ElementCount EC = VTy->getElementCount();
226 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
227 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
228 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
230 default:
231 return TypeSize::getFixed(0);
235 unsigned Type::getScalarSizeInBits() const {
236 // It is safe to assume that the scalar types have a fixed size.
237 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
240 int Type::getFPMantissaWidth() const {
241 if (auto *VTy = dyn_cast<VectorType>(this))
242 return VTy->getElementType()->getFPMantissaWidth();
243 assert(isFloatingPointTy() && "Not a floating point type!");
244 if (getTypeID() == HalfTyID) return 11;
245 if (getTypeID() == BFloatTyID) return 8;
246 if (getTypeID() == FloatTyID) return 24;
247 if (getTypeID() == DoubleTyID) return 53;
248 if (getTypeID() == X86_FP80TyID) return 64;
249 if (getTypeID() == FP128TyID) return 113;
250 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
251 return -1;
254 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
255 if (auto *ATy = dyn_cast<ArrayType>(this))
256 return ATy->getElementType()->isSized(Visited);
258 if (auto *VTy = dyn_cast<VectorType>(this))
259 return VTy->getElementType()->isSized(Visited);
261 if (auto *TTy = dyn_cast<TargetExtType>(this))
262 return TTy->getLayoutType()->isSized(Visited);
264 return cast<StructType>(this)->isSized(Visited);
267 //===----------------------------------------------------------------------===//
268 // Primitive 'Type' data
269 //===----------------------------------------------------------------------===//
271 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
272 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
273 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
274 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
275 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
276 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
277 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
278 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
279 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
280 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
281 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
282 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
284 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
285 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
286 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
287 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
288 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
289 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
291 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
292 return IntegerType::get(C, N);
295 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
296 // opaque pointer in addrspace(10)
297 static PointerType *Ty = PointerType::get(C, 10);
298 return Ty;
301 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
302 // opaque pointer in addrspace(20)
303 static PointerType *Ty = PointerType::get(C, 20);
304 return Ty;
307 //===----------------------------------------------------------------------===//
308 // IntegerType Implementation
309 //===----------------------------------------------------------------------===//
311 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
312 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
313 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
315 // Check for the built-in integer types
316 switch (NumBits) {
317 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
318 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
319 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
320 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
321 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
322 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
323 default:
324 break;
327 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
329 if (!Entry)
330 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
332 return Entry;
335 APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }
337 //===----------------------------------------------------------------------===//
338 // FunctionType Implementation
339 //===----------------------------------------------------------------------===//
341 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
342 bool IsVarArgs)
343 : Type(Result->getContext(), FunctionTyID) {
344 Type **SubTys = reinterpret_cast<Type**>(this+1);
345 assert(isValidReturnType(Result) && "invalid return type for function");
346 setSubclassData(IsVarArgs);
348 SubTys[0] = Result;
350 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
351 assert(isValidArgumentType(Params[i]) &&
352 "Not a valid type for function argument!");
353 SubTys[i+1] = Params[i];
356 ContainedTys = SubTys;
357 NumContainedTys = Params.size() + 1; // + 1 for result type
360 // This is the factory function for the FunctionType class.
361 FunctionType *FunctionType::get(Type *ReturnType,
362 ArrayRef<Type*> Params, bool isVarArg) {
363 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
364 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
365 FunctionType *FT;
366 // Since we only want to allocate a fresh function type in case none is found
367 // and we don't want to perform two lookups (one for checking if existent and
368 // one for inserting the newly allocated one), here we instead lookup based on
369 // Key and update the reference to the function type in-place to a newly
370 // allocated one if not found.
371 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
372 if (Insertion.second) {
373 // The function type was not found. Allocate one and update FunctionTypes
374 // in-place.
375 FT = (FunctionType *)pImpl->Alloc.Allocate(
376 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
377 alignof(FunctionType));
378 new (FT) FunctionType(ReturnType, Params, isVarArg);
379 *Insertion.first = FT;
380 } else {
381 // The function type was found. Just return it.
382 FT = *Insertion.first;
384 return FT;
387 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
388 return get(Result, {}, isVarArg);
391 bool FunctionType::isValidReturnType(Type *RetTy) {
392 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
393 !RetTy->isMetadataTy();
396 bool FunctionType::isValidArgumentType(Type *ArgTy) {
397 return ArgTy->isFirstClassType();
400 //===----------------------------------------------------------------------===//
401 // StructType Implementation
402 //===----------------------------------------------------------------------===//
404 // Primitive Constructors.
406 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
407 bool isPacked) {
408 LLVMContextImpl *pImpl = Context.pImpl;
409 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
411 StructType *ST;
412 // Since we only want to allocate a fresh struct type in case none is found
413 // and we don't want to perform two lookups (one for checking if existent and
414 // one for inserting the newly allocated one), here we instead lookup based on
415 // Key and update the reference to the struct type in-place to a newly
416 // allocated one if not found.
417 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
418 if (Insertion.second) {
419 // The struct type was not found. Allocate one and update AnonStructTypes
420 // in-place.
421 ST = new (Context.pImpl->Alloc) StructType(Context);
422 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
423 ST->setBody(ETypes, isPacked);
424 *Insertion.first = ST;
425 } else {
426 // The struct type was found. Just return it.
427 ST = *Insertion.first;
430 return ST;
433 bool StructType::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {
434 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
435 return true;
437 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
438 return false;
440 if (!Visited.insert(this).second)
441 return false;
443 for (Type *Ty : elements()) {
444 if (Ty->isScalableTy(Visited)) {
445 const_cast<StructType *>(this)->setSubclassData(
446 getSubclassData() | SCDB_ContainsScalableVector);
447 return true;
451 // For structures that are opaque, return false but do not set the
452 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
453 // when it becomes non-opaque.
454 if (!isOpaque())
455 const_cast<StructType *>(this)->setSubclassData(
456 getSubclassData() | SCDB_NotContainsScalableVector);
457 return false;
460 bool StructType::containsNonGlobalTargetExtType(
461 SmallPtrSetImpl<const Type *> &Visited) const {
462 if ((getSubclassData() & SCDB_ContainsNonGlobalTargetExtType) != 0)
463 return true;
465 if ((getSubclassData() & SCDB_NotContainsNonGlobalTargetExtType) != 0)
466 return false;
468 if (!Visited.insert(this).second)
469 return false;
471 for (Type *Ty : elements()) {
472 if (Ty->containsNonGlobalTargetExtType(Visited)) {
473 const_cast<StructType *>(this)->setSubclassData(
474 getSubclassData() | SCDB_ContainsNonGlobalTargetExtType);
475 return true;
479 // For structures that are opaque, return false but do not set the
480 // SCDB_NotContainsNonGlobalTargetExtType flag since it may gain non-global
481 // target extension types when it becomes non-opaque.
482 if (!isOpaque())
483 const_cast<StructType *>(this)->setSubclassData(
484 getSubclassData() | SCDB_NotContainsNonGlobalTargetExtType);
485 return false;
488 bool StructType::containsNonLocalTargetExtType(
489 SmallPtrSetImpl<const Type *> &Visited) const {
490 if ((getSubclassData() & SCDB_ContainsNonLocalTargetExtType) != 0)
491 return true;
493 if ((getSubclassData() & SCDB_NotContainsNonLocalTargetExtType) != 0)
494 return false;
496 if (!Visited.insert(this).second)
497 return false;
499 for (Type *Ty : elements()) {
500 if (Ty->containsNonLocalTargetExtType(Visited)) {
501 const_cast<StructType *>(this)->setSubclassData(
502 getSubclassData() | SCDB_ContainsNonLocalTargetExtType);
503 return true;
507 // For structures that are opaque, return false but do not set the
508 // SCDB_NotContainsNonLocalTargetExtType flag since it may gain non-local
509 // target extension types when it becomes non-opaque.
510 if (!isOpaque())
511 const_cast<StructType *>(this)->setSubclassData(
512 getSubclassData() | SCDB_NotContainsNonLocalTargetExtType);
513 return false;
516 bool StructType::containsHomogeneousScalableVectorTypes() const {
517 if (getNumElements() <= 0 || !isa<ScalableVectorType>(elements().front()))
518 return false;
519 return containsHomogeneousTypes();
522 bool StructType::containsHomogeneousTypes() const {
523 ArrayRef<Type *> ElementTys = elements();
524 return !ElementTys.empty() && all_equal(ElementTys);
527 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
528 cantFail(setBodyOrError(Elements, isPacked));
531 Error StructType::setBodyOrError(ArrayRef<Type *> Elements, bool isPacked) {
532 assert(isOpaque() && "Struct body already set!");
534 if (auto E = checkBody(Elements))
535 return E;
537 setSubclassData(getSubclassData() | SCDB_HasBody);
538 if (isPacked)
539 setSubclassData(getSubclassData() | SCDB_Packed);
541 NumContainedTys = Elements.size();
542 ContainedTys = Elements.empty()
543 ? nullptr
544 : Elements.copy(getContext().pImpl->Alloc).data();
546 return Error::success();
549 Error StructType::checkBody(ArrayRef<Type *> Elements) {
550 SmallSetVector<Type *, 4> Worklist(Elements.begin(), Elements.end());
551 for (unsigned I = 0; I < Worklist.size(); ++I) {
552 Type *Ty = Worklist[I];
553 if (Ty == this)
554 return createStringError(Twine("identified structure type '") +
555 getName() + "' is recursive");
556 Worklist.insert(Ty->subtype_begin(), Ty->subtype_end());
558 return Error::success();
561 void StructType::setName(StringRef Name) {
562 if (Name == getName()) return;
564 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
566 using EntryTy = StringMap<StructType *>::MapEntryTy;
568 // If this struct already had a name, remove its symbol table entry. Don't
569 // delete the data yet because it may be part of the new name.
570 if (SymbolTableEntry)
571 SymbolTable.remove((EntryTy *)SymbolTableEntry);
573 // If this is just removing the name, we're done.
574 if (Name.empty()) {
575 if (SymbolTableEntry) {
576 // Delete the old string data.
577 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
578 SymbolTableEntry = nullptr;
580 return;
583 // Look up the entry for the name.
584 auto IterBool =
585 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
587 // While we have a name collision, try a random rename.
588 if (!IterBool.second) {
589 SmallString<64> TempStr(Name);
590 TempStr.push_back('.');
591 raw_svector_ostream TmpStream(TempStr);
592 unsigned NameSize = Name.size();
594 do {
595 TempStr.resize(NameSize + 1);
596 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
598 IterBool = getContext().pImpl->NamedStructTypes.insert(
599 std::make_pair(TmpStream.str(), this));
600 } while (!IterBool.second);
603 // Delete the old string data.
604 if (SymbolTableEntry)
605 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
606 SymbolTableEntry = &*IterBool.first;
609 //===----------------------------------------------------------------------===//
610 // StructType Helper functions.
612 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
613 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
614 if (!Name.empty())
615 ST->setName(Name);
616 return ST;
619 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
620 return get(Context, {}, isPacked);
623 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
624 StringRef Name, bool isPacked) {
625 StructType *ST = create(Context, Name);
626 ST->setBody(Elements, isPacked);
627 return ST;
630 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
631 return create(Context, Elements, StringRef());
634 StructType *StructType::create(LLVMContext &Context) {
635 return create(Context, StringRef());
638 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
639 bool isPacked) {
640 assert(!Elements.empty() &&
641 "This method may not be invoked with an empty list");
642 return create(Elements[0]->getContext(), Elements, Name, isPacked);
645 StructType *StructType::create(ArrayRef<Type*> Elements) {
646 assert(!Elements.empty() &&
647 "This method may not be invoked with an empty list");
648 return create(Elements[0]->getContext(), Elements, StringRef());
651 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
652 if ((getSubclassData() & SCDB_IsSized) != 0)
653 return true;
654 if (isOpaque())
655 return false;
657 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
658 return false;
660 // Okay, our struct is sized if all of the elements are, but if one of the
661 // elements is opaque, the struct isn't sized *yet*, but may become sized in
662 // the future, so just bail out without caching.
663 // The ONLY special case inside a struct that is considered sized is when the
664 // elements are homogeneous of a scalable vector type.
665 if (containsHomogeneousScalableVectorTypes()) {
666 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
667 SCDB_IsSized);
668 return true;
670 for (Type *Ty : elements()) {
671 // If the struct contains a scalable vector type, don't consider it sized.
672 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
673 // special case right now is a structure of homogenous scalable vector
674 // types and is handled by the if-statement before this for-loop.
675 if (Ty->isScalableTy())
676 return false;
677 if (!Ty->isSized(Visited))
678 return false;
681 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
682 // we find a sized type, as types can only move from opaque to sized, not the
683 // other way.
684 const_cast<StructType*>(this)->setSubclassData(
685 getSubclassData() | SCDB_IsSized);
686 return true;
689 StringRef StructType::getName() const {
690 assert(!isLiteral() && "Literal structs never have names");
691 if (!SymbolTableEntry) return StringRef();
693 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
696 bool StructType::isValidElementType(Type *ElemTy) {
697 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
698 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
699 !ElemTy->isTokenTy();
702 bool StructType::isLayoutIdentical(StructType *Other) const {
703 if (this == Other) return true;
705 if (isPacked() != Other->isPacked())
706 return false;
708 return elements() == Other->elements();
711 Type *StructType::getTypeAtIndex(const Value *V) const {
712 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
713 assert(indexValid(Idx) && "Invalid structure index!");
714 return getElementType(Idx);
717 bool StructType::indexValid(const Value *V) const {
718 // Structure indexes require (vectors of) 32-bit integer constants. In the
719 // vector case all of the indices must be equal.
720 if (!V->getType()->isIntOrIntVectorTy(32))
721 return false;
722 if (isa<ScalableVectorType>(V->getType()))
723 return false;
724 const Constant *C = dyn_cast<Constant>(V);
725 if (C && V->getType()->isVectorTy())
726 C = C->getSplatValue();
727 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
728 return CU && CU->getZExtValue() < getNumElements();
731 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
732 return C.pImpl->NamedStructTypes.lookup(Name);
735 //===----------------------------------------------------------------------===//
736 // ArrayType Implementation
737 //===----------------------------------------------------------------------===//
739 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
740 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
741 NumElements(NumEl) {
742 ContainedTys = &ContainedType;
743 NumContainedTys = 1;
746 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
747 assert(isValidElementType(ElementType) && "Invalid type for array element!");
749 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
750 ArrayType *&Entry =
751 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
753 if (!Entry)
754 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
755 return Entry;
758 bool ArrayType::isValidElementType(Type *ElemTy) {
759 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
760 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
761 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy();
764 //===----------------------------------------------------------------------===//
765 // VectorType Implementation
766 //===----------------------------------------------------------------------===//
768 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
769 : Type(ElType->getContext(), TID), ContainedType(ElType),
770 ElementQuantity(EQ) {
771 ContainedTys = &ContainedType;
772 NumContainedTys = 1;
775 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
776 if (EC.isScalable())
777 return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
778 else
779 return FixedVectorType::get(ElementType, EC.getKnownMinValue());
782 bool VectorType::isValidElementType(Type *ElemTy) {
783 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
784 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID;
787 //===----------------------------------------------------------------------===//
788 // FixedVectorType Implementation
789 //===----------------------------------------------------------------------===//
791 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
792 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
793 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
794 "be an integer, floating point, or "
795 "pointer type.");
797 auto EC = ElementCount::getFixed(NumElts);
799 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
800 VectorType *&Entry = ElementType->getContext()
801 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
803 if (!Entry)
804 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
805 return cast<FixedVectorType>(Entry);
808 //===----------------------------------------------------------------------===//
809 // ScalableVectorType Implementation
810 //===----------------------------------------------------------------------===//
812 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
813 unsigned MinNumElts) {
814 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
815 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
816 "be an integer, floating point, or "
817 "pointer type.");
819 auto EC = ElementCount::getScalable(MinNumElts);
821 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
822 VectorType *&Entry = ElementType->getContext()
823 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
825 if (!Entry)
826 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
827 return cast<ScalableVectorType>(Entry);
830 //===----------------------------------------------------------------------===//
831 // PointerType Implementation
832 //===----------------------------------------------------------------------===//
834 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
835 assert(EltTy && "Can't get a pointer to <null> type!");
836 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
838 // Automatically convert typed pointers to opaque pointers.
839 return get(EltTy->getContext(), AddressSpace);
842 PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
843 LLVMContextImpl *CImpl = C.pImpl;
845 // Since AddressSpace #0 is the common case, we special case it.
846 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
847 : CImpl->PointerTypes[AddressSpace];
849 if (!Entry)
850 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
851 return Entry;
854 PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
855 : Type(C, PointerTyID) {
856 setSubclassData(AddrSpace);
859 PointerType *Type::getPointerTo(unsigned AddrSpace) const {
860 return PointerType::get(getContext(), AddrSpace);
863 bool PointerType::isValidElementType(Type *ElemTy) {
864 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
865 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
866 !ElemTy->isX86_AMXTy();
869 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
870 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
873 //===----------------------------------------------------------------------===//
874 // TargetExtType Implementation
875 //===----------------------------------------------------------------------===//
877 TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
878 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
879 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {
880 NumContainedTys = Types.size();
882 // Parameter storage immediately follows the class in allocation.
883 Type **Params = reinterpret_cast<Type **>(this + 1);
884 ContainedTys = Params;
885 for (Type *T : Types)
886 *Params++ = T;
888 setSubclassData(Ints.size());
889 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
890 IntParams = IntParamSpace;
891 for (unsigned IntParam : Ints)
892 *IntParamSpace++ = IntParam;
895 TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
896 ArrayRef<Type *> Types,
897 ArrayRef<unsigned> Ints) {
898 return cantFail(getOrError(C, Name, Types, Ints));
901 Expected<TargetExtType *> TargetExtType::getOrError(LLVMContext &C,
902 StringRef Name,
903 ArrayRef<Type *> Types,
904 ArrayRef<unsigned> Ints) {
905 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
906 TargetExtType *TT;
907 // Since we only want to allocate a fresh target type in case none is found
908 // and we don't want to perform two lookups (one for checking if existent and
909 // one for inserting the newly allocated one), here we instead lookup based on
910 // Key and update the reference to the target type in-place to a newly
911 // allocated one if not found.
912 auto [Iter, Inserted] = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);
913 if (Inserted) {
914 // The target type was not found. Allocate one and update TargetExtTypes
915 // in-place.
916 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
917 sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
918 sizeof(unsigned) * Ints.size(),
919 alignof(TargetExtType));
920 new (TT) TargetExtType(C, Name, Types, Ints);
921 *Iter = TT;
922 return checkParams(TT);
925 // The target type was found. Just return it.
926 return *Iter;
929 Expected<TargetExtType *> TargetExtType::checkParams(TargetExtType *TTy) {
930 // Opaque types in the AArch64 name space.
931 if (TTy->Name == "aarch64.svcount" &&
932 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 0))
933 return createStringError(
934 "target extension type aarch64.svcount should have no parameters");
936 // Opaque types in the RISC-V name space.
937 if (TTy->Name == "riscv.vector.tuple" &&
938 (TTy->getNumTypeParameters() != 1 || TTy->getNumIntParameters() != 1))
939 return createStringError(
940 "target extension type riscv.vector.tuple should have one "
941 "type parameter and one integer parameter");
943 // Opaque types in the AMDGPU name space.
944 if (TTy->Name == "amdgcn.named.barrier" &&
945 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 1)) {
946 return createStringError("target extension type amdgcn.named.barrier "
947 "should have no type parameters "
948 "and one integer parameter");
951 return TTy;
954 namespace {
955 struct TargetTypeInfo {
956 Type *LayoutType;
957 uint64_t Properties;
959 template <typename... ArgTys>
960 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
961 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {}
963 } // anonymous namespace
965 static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
966 LLVMContext &C = Ty->getContext();
967 StringRef Name = Ty->getName();
968 if (Name == "spirv.Image")
969 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal,
970 TargetExtType::CanBeLocal);
971 if (Name.starts_with("spirv."))
972 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,
973 TargetExtType::CanBeGlobal,
974 TargetExtType::CanBeLocal);
976 // Opaque types in the AArch64 name space.
977 if (Name == "aarch64.svcount")
978 return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),
979 TargetExtType::HasZeroInit,
980 TargetExtType::CanBeLocal);
982 // RISC-V vector tuple type. The layout is represented as the type that needs
983 // the same number of vector registers(VREGS) as this tuple type, represented
984 // as <vscale x (RVVBitsPerBlock * VREGS / 8) x i8>.
985 if (Name == "riscv.vector.tuple") {
986 unsigned TotalNumElts =
987 std::max(cast<ScalableVectorType>(Ty->getTypeParameter(0))
988 ->getMinNumElements(),
989 RISCV::RVVBitsPerBlock / 8) *
990 Ty->getIntParameter(0);
991 return TargetTypeInfo(
992 ScalableVectorType::get(Type::getInt8Ty(C), TotalNumElts),
993 TargetExtType::CanBeLocal, TargetExtType::HasZeroInit);
996 // DirectX resources
997 if (Name.starts_with("dx."))
998 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal,
999 TargetExtType::CanBeLocal);
1001 // Opaque types in the AMDGPU name space.
1002 if (Name == "amdgcn.named.barrier") {
1003 return TargetTypeInfo(FixedVectorType::get(Type::getInt32Ty(C), 4),
1004 TargetExtType::CanBeGlobal);
1007 return TargetTypeInfo(Type::getVoidTy(C));
1010 Type *TargetExtType::getLayoutType() const {
1011 return getTargetTypeInfo(this).LayoutType;
1014 bool TargetExtType::hasProperty(Property Prop) const {
1015 uint64_t Properties = getTargetTypeInfo(this).Properties;
1016 return (Properties & Prop) == Prop;