1//===- Type.cpp - Implement the Type class --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Type class for the IR library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Type.h"
14#include "LLVMContextImpl.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/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
32using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// Type Class Implementation
36//===----------------------------------------------------------------------===//
37
38Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
39 switch (IDNumber) {
40 case VoidTyID : return getVoidTy(C);
41 case HalfTyID : return getHalfTy(C);
42 case BFloatTyID : return getBFloatTy(C);
43 case FloatTyID : return getFloatTy(C);
44 case DoubleTyID : return getDoubleTy(C);
45 case X86_FP80TyID : return getX86_FP80Ty(C);
46 case FP128TyID : return getFP128Ty(C);
47 case PPC_FP128TyID : return getPPC_FP128Ty(C);
48 case LabelTyID : return getLabelTy(C);
49 case MetadataTyID : return getMetadataTy(C);
50 case X86_AMXTyID : return getX86_AMXTy(C);
51 case TokenTyID : return getTokenTy(C);
52 default:
53 return nullptr;
54 }
55}
56
57bool Type::isByteTy(unsigned BitWidth) const {
58 return isByteTy() && cast<ByteType>(Val: this)->getBitWidth() == BitWidth;
59}
60
61bool Type::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {
62 if (const auto *ATy = dyn_cast<ArrayType>(Val: this))
63 return ATy->getElementType()->isScalableTy(Visited);
64 if (const auto *STy = dyn_cast<StructType>(Val: this))
65 return STy->isScalableTy(Visited);
66 return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();
67}
68
69bool Type::isScalableTy() const {
70 SmallPtrSet<const Type *, 4> Visited;
71 return isScalableTy(Visited);
72}
73
74bool Type::containsNonGlobalTargetExtType(
75 SmallPtrSetImpl<const Type *> &Visited) const {
76 if (const auto *ATy = dyn_cast<ArrayType>(Val: this))
77 return ATy->getElementType()->containsNonGlobalTargetExtType(Visited);
78 if (const auto *STy = dyn_cast<StructType>(Val: this))
79 return STy->containsNonGlobalTargetExtType(Visited);
80 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
81 return !TT->hasProperty(Prop: TargetExtType::CanBeGlobal);
82 return false;
83}
84
85bool Type::containsNonGlobalTargetExtType() const {
86 SmallPtrSet<const Type *, 4> Visited;
87 return containsNonGlobalTargetExtType(Visited);
88}
89
90bool Type::containsNonLocalTargetExtType(
91 SmallPtrSetImpl<const Type *> &Visited) const {
92 if (const auto *ATy = dyn_cast<ArrayType>(Val: this))
93 return ATy->getElementType()->containsNonLocalTargetExtType(Visited);
94 if (const auto *STy = dyn_cast<StructType>(Val: this))
95 return STy->containsNonLocalTargetExtType(Visited);
96 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
97 return !TT->hasProperty(Prop: TargetExtType::CanBeLocal);
98 return false;
99}
100
101bool Type::containsNonLocalTargetExtType() const {
102 SmallPtrSet<const Type *, 4> Visited;
103 return containsNonLocalTargetExtType(Visited);
104}
105
106const fltSemantics &Type::getFltSemantics() const {
107 switch (getTypeID()) {
108 case HalfTyID: return APFloat::IEEEhalf();
109 case BFloatTyID: return APFloat::BFloat();
110 case FloatTyID: return APFloat::IEEEsingle();
111 case DoubleTyID: return APFloat::IEEEdouble();
112 case X86_FP80TyID: return APFloat::x87DoubleExtended();
113 case FP128TyID: return APFloat::IEEEquad();
114 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
115 default: llvm_unreachable("Invalid floating type");
116 }
117}
118
119bool Type::isScalableTargetExtTy() const {
120 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
121 return isa<ScalableVectorType>(Val: TT->getLayoutType());
122 return false;
123}
124
125Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
126 Type *Ty;
127 if (&S == &APFloat::IEEEhalf())
128 Ty = Type::getHalfTy(C);
129 else if (&S == &APFloat::BFloat())
130 Ty = Type::getBFloatTy(C);
131 else if (&S == &APFloat::IEEEsingle())
132 Ty = Type::getFloatTy(C);
133 else if (&S == &APFloat::IEEEdouble())
134 Ty = Type::getDoubleTy(C);
135 else if (&S == &APFloat::x87DoubleExtended())
136 Ty = Type::getX86_FP80Ty(C);
137 else if (&S == &APFloat::IEEEquad())
138 Ty = Type::getFP128Ty(C);
139 else {
140 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
141 Ty = Type::getPPC_FP128Ty(C);
142 }
143 return Ty;
144}
145
146bool Type::isRISCVVectorTupleTy() const {
147 if (!isTargetExtTy())
148 return false;
149
150 return cast<TargetExtType>(Val: this)->getName() == "riscv.vector.tuple";
151}
152
153bool Type::canLosslesslyBitCastTo(Type *Ty) const {
154 // Identity cast means no change so return true
155 if (this == Ty)
156 return true;
157
158 // They are not convertible unless they are at least first class types
159 if (!this->isFirstClassType() || !Ty->isFirstClassType())
160 return false;
161
162 // Vector -> Vector conversions are always lossless if the two vector types
163 // have the same size, otherwise not.
164 if (isa<VectorType>(Val: this) && isa<VectorType>(Val: Ty))
165 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
166
167 // 8192-bit fixed width vector types can be losslessly converted to x86amx.
168 if (((isa<FixedVectorType>(Val: this)) && Ty->isX86_AMXTy()) &&
169 getPrimitiveSizeInBits().getFixedValue() == 8192)
170 return true;
171 if ((isX86_AMXTy() && isa<FixedVectorType>(Val: Ty)) &&
172 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
173 return true;
174
175 // Conservatively assume we can't losslessly convert between pointers with
176 // different address spaces.
177 return false;
178}
179
180bool Type::isEmptyTy() const {
181 if (auto *ATy = dyn_cast<ArrayType>(Val: this)) {
182 unsigned NumElements = ATy->getNumElements();
183 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
184 }
185
186 if (auto *STy = dyn_cast<StructType>(Val: this)) {
187 unsigned NumElements = STy->getNumElements();
188 for (unsigned i = 0; i < NumElements; ++i)
189 if (!STy->getElementType(N: i)->isEmptyTy())
190 return false;
191 return true;
192 }
193
194 return false;
195}
196
197TypeSize Type::getPrimitiveSizeInBits() const {
198 switch (getTypeID()) {
199 case Type::HalfTyID:
200 return TypeSize::getFixed(ExactSize: 16);
201 case Type::BFloatTyID:
202 return TypeSize::getFixed(ExactSize: 16);
203 case Type::FloatTyID:
204 return TypeSize::getFixed(ExactSize: 32);
205 case Type::DoubleTyID:
206 return TypeSize::getFixed(ExactSize: 64);
207 case Type::X86_FP80TyID:
208 return TypeSize::getFixed(ExactSize: 80);
209 case Type::FP128TyID:
210 return TypeSize::getFixed(ExactSize: 128);
211 case Type::PPC_FP128TyID:
212 return TypeSize::getFixed(ExactSize: 128);
213 case Type::X86_AMXTyID:
214 return TypeSize::getFixed(ExactSize: 8192);
215 case Type::ByteTyID:
216 return TypeSize::getFixed(ExactSize: cast<ByteType>(Val: this)->getBitWidth());
217 case Type::IntegerTyID:
218 return TypeSize::getFixed(ExactSize: cast<IntegerType>(Val: this)->getBitWidth());
219 case Type::FixedVectorTyID:
220 case Type::ScalableVectorTyID: {
221 const VectorType *VTy = cast<VectorType>(Val: this);
222 ElementCount EC = VTy->getElementCount();
223 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
224 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
225 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
226 }
227 default:
228 return TypeSize::getFixed(ExactSize: 0);
229 }
230}
231
232unsigned Type::getScalarSizeInBits() const {
233 // It is safe to assume that the scalar types have a fixed size.
234 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
235}
236
237int Type::getFPMantissaWidth() const {
238 if (auto *VTy = dyn_cast<VectorType>(Val: this))
239 return VTy->getElementType()->getFPMantissaWidth();
240 assert(isFloatingPointTy() && "Not a floating point type!");
241 if (getTypeID() == HalfTyID) return 11;
242 if (getTypeID() == BFloatTyID) return 8;
243 if (getTypeID() == FloatTyID) return 24;
244 if (getTypeID() == DoubleTyID) return 53;
245 if (getTypeID() == X86_FP80TyID) return 64;
246 if (getTypeID() == FP128TyID) return 113;
247 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
248 return -1;
249}
250
251bool Type::isFirstClassType() const {
252 switch (getTypeID()) {
253 default:
254 return true;
255 case FunctionTyID:
256 case VoidTyID:
257 return false;
258 case StructTyID: {
259 auto *ST = cast<StructType>(Val: this);
260 return !ST->isOpaque();
261 }
262 }
263}
264
265bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
266 if (auto *ATy = dyn_cast<ArrayType>(Val: this))
267 return ATy->getElementType()->isSized(Visited);
268
269 if (auto *VTy = dyn_cast<VectorType>(Val: this))
270 return VTy->getElementType()->isSized(Visited);
271
272 if (auto *TTy = dyn_cast<TargetExtType>(Val: this))
273 return TTy->getLayoutType()->isSized(Visited);
274
275 return cast<StructType>(Val: this)->isSized(Visited);
276}
277
278//===----------------------------------------------------------------------===//
279// Primitive 'Type' data
280//===----------------------------------------------------------------------===//
281
282Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
283Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
284Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
285Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
286Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
287Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
288Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
289Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
290Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
291Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
292Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
293Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
294
295ByteType *Type::getByte1Ty(LLVMContext &C) { return &C.pImpl->Byte1Ty; }
296ByteType *Type::getByte8Ty(LLVMContext &C) { return &C.pImpl->Byte8Ty; }
297ByteType *Type::getByte16Ty(LLVMContext &C) { return &C.pImpl->Byte16Ty; }
298ByteType *Type::getByte32Ty(LLVMContext &C) { return &C.pImpl->Byte32Ty; }
299ByteType *Type::getByte64Ty(LLVMContext &C) { return &C.pImpl->Byte64Ty; }
300ByteType *Type::getByte128Ty(LLVMContext &C) { return &C.pImpl->Byte128Ty; }
301
302ByteType *Type::getByteNTy(LLVMContext &C, unsigned N) {
303 return ByteType::get(C, NumBits: N);
304}
305
306IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
307IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
308IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
309IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
310IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
311IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
312
313IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
314 return IntegerType::get(C, NumBits: N);
315}
316
317Type *Type::getIntFromByteType(Type *Ty) {
318 assert(Ty->isByteOrByteVectorTy() && "Expected a byte or byte vector type.");
319 unsigned NumBits = Ty->getScalarSizeInBits();
320 IntegerType *IntTy = IntegerType::get(C&: Ty->getContext(), NumBits);
321 if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty))
322 return VectorType::get(ElementType: IntTy, Other: VecTy);
323 return IntTy;
324}
325
326Type *Type::getByteFromIntType(Type *Ty) {
327 assert(!Ty->isPtrOrPtrVectorTy() &&
328 "Expected a non-pointer or non-pointer vector type.");
329 unsigned NumBits = Ty->getScalarSizeInBits();
330 ByteType *ByteTy = ByteType::get(C&: Ty->getContext(), NumBits);
331 if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty))
332 return VectorType::get(ElementType: ByteTy, Other: VecTy);
333 return ByteTy;
334}
335
336Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
337 return TargetExtType::get(Context&: C, Name: "wasm.externref", Types: {}, Ints: {});
338}
339
340Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
341 return TargetExtType::get(Context&: C, Name: "wasm.funcref", Types: {}, Ints: {});
342}
343
344//===----------------------------------------------------------------------===//
345// IntegerType Implementation
346//===----------------------------------------------------------------------===//
347
348IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
349 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
350 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
351
352 // Check for the built-in integer types
353 switch (NumBits) {
354 case 1: return Type::getInt1Ty(C);
355 case 8: return Type::getInt8Ty(C);
356 case 16: return Type::getInt16Ty(C);
357 case 32: return Type::getInt32Ty(C);
358 case 64: return Type::getInt64Ty(C);
359 case 128: return Type::getInt128Ty(C);
360 default:
361 break;
362 }
363
364 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
365
366 if (!Entry)
367 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
368
369 return Entry;
370}
371
372APInt IntegerType::getMask() const { return APInt::getAllOnes(numBits: getBitWidth()); }
373
374//===----------------------------------------------------------------------===//
375// ByteType Implementation
376//===----------------------------------------------------------------------===//
377
378ByteType *ByteType::get(LLVMContext &C, unsigned NumBits) {
379 assert(NumBits >= MIN_BYTE_BITS && "bitwidth too small");
380 assert(NumBits <= MAX_BYTE_BITS && "bitwidth too large");
381
382 // Check for the built-in byte types
383 switch (NumBits) {
384 case 8:
385 return Type::getByte8Ty(C);
386 case 16:
387 return Type::getByte16Ty(C);
388 case 32:
389 return Type::getByte32Ty(C);
390 case 64:
391 return Type::getByte64Ty(C);
392 case 128:
393 return Type::getByte128Ty(C);
394 default:
395 break;
396 }
397
398 ByteType *&Entry = C.pImpl->ByteTypes[NumBits];
399
400 if (!Entry)
401 Entry = new (C.pImpl->Alloc) ByteType(C, NumBits);
402
403 return Entry;
404}
405
406APInt ByteType::getMask() const { return APInt::getAllOnes(numBits: getBitWidth()); }
407
408//===----------------------------------------------------------------------===//
409// FunctionType Implementation
410//===----------------------------------------------------------------------===//
411
412FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
413 bool IsVarArgs)
414 : Type(Result->getContext(), FunctionTyID) {
415 Type **SubTys = reinterpret_cast<Type**>(this+1);
416 assert(isValidReturnType(Result) && "invalid return type for function");
417 setSubclassData(IsVarArgs);
418
419 SubTys[0] = Result;
420
421 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
422 assert(isValidArgumentType(Params[i]) &&
423 "Not a valid type for function argument!");
424 SubTys[i+1] = Params[i];
425 }
426
427 ContainedTys = SubTys;
428 NumContainedTys = Params.size() + 1; // + 1 for result type
429}
430
431// This is the factory function for the FunctionType class.
432FunctionType *FunctionType::get(Type *ReturnType,
433 ArrayRef<Type*> Params, bool isVarArg) {
434 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
435 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
436 FunctionType *FT;
437 // Since we only want to allocate a fresh function type in case none is found
438 // and we don't want to perform two lookups (one for checking if existent and
439 // one for inserting the newly allocated one), here we instead lookup based on
440 // Key and update the reference to the function type in-place to a newly
441 // allocated one if not found.
442 auto Insertion = pImpl->FunctionTypes.insert_as(V: nullptr, LookupKey: Key);
443 if (Insertion.second) {
444 // The function type was not found. Allocate one and update FunctionTypes
445 // in-place.
446 FT = (FunctionType *)pImpl->Alloc.Allocate(
447 Size: sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
448 Alignment: alignof(FunctionType));
449 new (FT) FunctionType(ReturnType, Params, isVarArg);
450 *Insertion.first = FT;
451 } else {
452 // The function type was found. Just return it.
453 FT = *Insertion.first;
454 }
455 return FT;
456}
457
458FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
459 return get(ReturnType: Result, Params: {}, isVarArg);
460}
461
462bool FunctionType::isValidReturnType(Type *RetTy) {
463 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
464 !RetTy->isMetadataTy();
465}
466
467bool FunctionType::isValidArgumentType(Type *ArgTy) {
468 return ArgTy->isFirstClassType() && !ArgTy->isLabelTy();
469}
470
471//===----------------------------------------------------------------------===//
472// StructType Implementation
473//===----------------------------------------------------------------------===//
474
475// Primitive Constructors.
476
477StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
478 bool isPacked) {
479 LLVMContextImpl *pImpl = Context.pImpl;
480 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
481
482 StructType *ST;
483 // Since we only want to allocate a fresh struct type in case none is found
484 // and we don't want to perform two lookups (one for checking if existent and
485 // one for inserting the newly allocated one), here we instead lookup based on
486 // Key and update the reference to the struct type in-place to a newly
487 // allocated one if not found.
488 auto Insertion = pImpl->AnonStructTypes.insert_as(V: nullptr, LookupKey: Key);
489 if (Insertion.second) {
490 // The struct type was not found. Allocate one and update AnonStructTypes
491 // in-place.
492 ST = new (Context.pImpl->Alloc) StructType(Context);
493 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
494 ST->setBody(Elements: ETypes, isPacked);
495 *Insertion.first = ST;
496 } else {
497 // The struct type was found. Just return it.
498 ST = *Insertion.first;
499 }
500
501 return ST;
502}
503
504bool StructType::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {
505 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
506 return true;
507
508 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
509 return false;
510
511 if (!Visited.insert(Ptr: this).second)
512 return false;
513
514 for (Type *Ty : elements()) {
515 if (Ty->isScalableTy(Visited)) {
516 const_cast<StructType *>(this)->setSubclassData(
517 getSubclassData() | SCDB_ContainsScalableVector);
518 return true;
519 }
520 }
521
522 // For structures that are opaque, return false but do not set the
523 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
524 // when it becomes non-opaque.
525 if (!isOpaque())
526 const_cast<StructType *>(this)->setSubclassData(
527 getSubclassData() | SCDB_NotContainsScalableVector);
528 return false;
529}
530
531bool StructType::containsNonGlobalTargetExtType(
532 SmallPtrSetImpl<const Type *> &Visited) const {
533 if ((getSubclassData() & SCDB_ContainsNonGlobalTargetExtType) != 0)
534 return true;
535
536 if ((getSubclassData() & SCDB_NotContainsNonGlobalTargetExtType) != 0)
537 return false;
538
539 if (!Visited.insert(Ptr: this).second)
540 return false;
541
542 for (Type *Ty : elements()) {
543 if (Ty->containsNonGlobalTargetExtType(Visited)) {
544 const_cast<StructType *>(this)->setSubclassData(
545 getSubclassData() | SCDB_ContainsNonGlobalTargetExtType);
546 return true;
547 }
548 }
549
550 // For structures that are opaque, return false but do not set the
551 // SCDB_NotContainsNonGlobalTargetExtType flag since it may gain non-global
552 // target extension types when it becomes non-opaque.
553 if (!isOpaque())
554 const_cast<StructType *>(this)->setSubclassData(
555 getSubclassData() | SCDB_NotContainsNonGlobalTargetExtType);
556 return false;
557}
558
559bool StructType::containsNonLocalTargetExtType(
560 SmallPtrSetImpl<const Type *> &Visited) const {
561 if ((getSubclassData() & SCDB_ContainsNonLocalTargetExtType) != 0)
562 return true;
563
564 if ((getSubclassData() & SCDB_NotContainsNonLocalTargetExtType) != 0)
565 return false;
566
567 if (!Visited.insert(Ptr: this).second)
568 return false;
569
570 for (Type *Ty : elements()) {
571 if (Ty->containsNonLocalTargetExtType(Visited)) {
572 const_cast<StructType *>(this)->setSubclassData(
573 getSubclassData() | SCDB_ContainsNonLocalTargetExtType);
574 return true;
575 }
576 }
577
578 // For structures that are opaque, return false but do not set the
579 // SCDB_NotContainsNonLocalTargetExtType flag since it may gain non-local
580 // target extension types when it becomes non-opaque.
581 if (!isOpaque())
582 const_cast<StructType *>(this)->setSubclassData(
583 getSubclassData() | SCDB_NotContainsNonLocalTargetExtType);
584 return false;
585}
586
587bool StructType::containsHomogeneousScalableVectorTypes() const {
588 if (getNumElements() <= 0 || !isa<ScalableVectorType>(Val: elements().front()))
589 return false;
590 return containsHomogeneousTypes();
591}
592
593bool StructType::containsHomogeneousTypes() const {
594 ArrayRef<Type *> ElementTys = elements();
595 return !ElementTys.empty() && all_equal(Range&: ElementTys);
596}
597
598void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
599 cantFail(Err: setBodyOrError(Elements, isPacked));
600}
601
602Error StructType::setBodyOrError(ArrayRef<Type *> Elements, bool isPacked) {
603 assert(isOpaque() && "Struct body already set!");
604
605 if (auto E = checkBody(Elements))
606 return E;
607
608 setSubclassData(getSubclassData() | SCDB_HasBody);
609 if (isPacked)
610 setSubclassData(getSubclassData() | SCDB_Packed);
611
612 NumContainedTys = Elements.size();
613 ContainedTys = Elements.empty()
614 ? nullptr
615 : Elements.copy(A&: getContext().pImpl->Alloc).data();
616
617 return Error::success();
618}
619
620Error StructType::checkBody(ArrayRef<Type *> Elements) {
621 SmallSetVector<Type *, 4> Worklist(Elements.begin(), Elements.end());
622 for (unsigned I = 0; I < Worklist.size(); ++I) {
623 Type *Ty = Worklist[I];
624 if (Ty == this)
625 return createStringError(S: Twine("identified structure type '") +
626 getName() + "' is recursive");
627 Worklist.insert_range(R: Ty->subtypes());
628 }
629 return Error::success();
630}
631
632void StructType::setName(StringRef Name) {
633 if (Name == getName()) return;
634
635 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
636
637 using EntryTy = StringMap<StructType *>::MapEntryTy;
638
639 // If this struct already had a name, remove its symbol table entry. Don't
640 // delete the data yet because it may be part of the new name.
641 if (SymbolTableEntry)
642 SymbolTable.remove(KeyValue: (EntryTy *)SymbolTableEntry);
643
644 // If this is just removing the name, we're done.
645 if (Name.empty()) {
646 if (SymbolTableEntry) {
647 // Delete the old string data.
648 ((EntryTy *)SymbolTableEntry)->Destroy(allocator&: SymbolTable.getAllocator());
649 SymbolTableEntry = nullptr;
650 }
651 return;
652 }
653
654 // Look up the entry for the name.
655 auto IterBool =
656 getContext().pImpl->NamedStructTypes.insert(KV: std::make_pair(x&: Name, y: this));
657
658 // While we have a name collision, try a random rename.
659 if (!IterBool.second) {
660 SmallString<64> TempStr(Name);
661 TempStr.push_back(Elt: '.');
662 raw_svector_ostream TmpStream(TempStr);
663 unsigned NameSize = Name.size();
664
665 do {
666 TempStr.resize(N: NameSize + 1);
667 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
668
669 IterBool = getContext().pImpl->NamedStructTypes.insert(
670 KV: std::make_pair(x: TmpStream.str(), y: this));
671 } while (!IterBool.second);
672 }
673
674 // Delete the old string data.
675 if (SymbolTableEntry)
676 ((EntryTy *)SymbolTableEntry)->Destroy(allocator&: SymbolTable.getAllocator());
677 SymbolTableEntry = &*IterBool.first;
678}
679
680//===----------------------------------------------------------------------===//
681// StructType Helper functions.
682
683StructType *StructType::create(LLVMContext &Context, StringRef Name) {
684 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
685 if (!Name.empty())
686 ST->setName(Name);
687 return ST;
688}
689
690StructType *StructType::get(LLVMContext &Context, bool isPacked) {
691 return get(Context, ETypes: {}, isPacked);
692}
693
694StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
695 StringRef Name, bool isPacked) {
696 StructType *ST = create(Context, Name);
697 ST->setBody(Elements, isPacked);
698 return ST;
699}
700
701StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
702 return create(Context, Elements, Name: StringRef());
703}
704
705StructType *StructType::create(LLVMContext &Context) {
706 return create(Context, Name: StringRef());
707}
708
709StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
710 bool isPacked) {
711 assert(!Elements.empty() &&
712 "This method may not be invoked with an empty list");
713 return create(Context&: Elements[0]->getContext(), Elements, Name, isPacked);
714}
715
716StructType *StructType::create(ArrayRef<Type*> Elements) {
717 assert(!Elements.empty() &&
718 "This method may not be invoked with an empty list");
719 return create(Context&: Elements[0]->getContext(), Elements, Name: StringRef());
720}
721
722bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
723 if ((getSubclassData() & SCDB_IsSized) != 0)
724 return true;
725 if (isOpaque())
726 return false;
727
728 if (Visited && !Visited->insert(Ptr: const_cast<StructType*>(this)).second)
729 return false;
730
731 // Okay, our struct is sized if all of the elements are, but if one of the
732 // elements is opaque, the struct isn't sized *yet*, but may become sized in
733 // the future, so just bail out without caching.
734 // The ONLY special case inside a struct that is considered sized is when the
735 // elements are homogeneous of a scalable vector type.
736 if (containsHomogeneousScalableVectorTypes()) {
737 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
738 SCDB_IsSized);
739 return true;
740 }
741 for (Type *Ty : elements()) {
742 // If the struct contains a scalable vector type, don't consider it sized.
743 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
744 // special case right now is a structure of homogenous scalable vector
745 // types and is handled by the if-statement before this for-loop.
746 if (Ty->isScalableTy())
747 return false;
748 if (!Ty->isSized(Visited))
749 return false;
750 }
751
752 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
753 // we find a sized type, as types can only move from opaque to sized, not the
754 // other way.
755 const_cast<StructType*>(this)->setSubclassData(
756 getSubclassData() | SCDB_IsSized);
757 return true;
758}
759
760StringRef StructType::getName() const {
761 assert(!isLiteral() && "Literal structs never have names");
762 if (!SymbolTableEntry) return StringRef();
763
764 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
765}
766
767bool StructType::isValidElementType(Type *ElemTy) {
768 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
769 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
770 !ElemTy->isTokenTy();
771}
772
773bool StructType::isLayoutIdentical(StructType *Other) const {
774 if (this == Other) return true;
775
776 if (isPacked() != Other->isPacked())
777 return false;
778
779 return elements() == Other->elements();
780}
781
782Type *StructType::getTypeAtIndex(const Value *V) const {
783 unsigned Idx = (unsigned)cast<Constant>(Val: V)->getUniqueInteger().getZExtValue();
784 assert(indexValid(Idx) && "Invalid structure index!");
785 return getElementType(N: Idx);
786}
787
788bool StructType::indexValid(const Value *V) const {
789 // Structure indexes require (vectors of) 32-bit integer constants. In the
790 // vector case all of the indices must be equal.
791 if (!V->getType()->isIntOrIntVectorTy(BitWidth: 32))
792 return false;
793 if (isa<ScalableVectorType>(Val: V->getType()))
794 return false;
795 const Constant *C = dyn_cast<Constant>(Val: V);
796 if (C && V->getType()->isVectorTy())
797 C = C->getSplatValue();
798 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(Val: C);
799 return CU && CU->getZExtValue() < getNumElements();
800}
801
802StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
803 return C.pImpl->NamedStructTypes.lookup(Key: Name);
804}
805
806//===----------------------------------------------------------------------===//
807// ArrayType Implementation
808//===----------------------------------------------------------------------===//
809
810ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
811 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
812 NumElements(NumEl) {
813 ContainedTys = &ContainedType;
814 NumContainedTys = 1;
815}
816
817ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
818 assert(isValidElementType(ElementType) && "Invalid type for array element!");
819
820 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
821 ArrayType *&Entry =
822 pImpl->ArrayTypes[std::make_pair(x&: ElementType, y&: NumElements)];
823
824 if (!Entry)
825 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
826 return Entry;
827}
828
829bool ArrayType::isValidElementType(Type *ElemTy) {
830 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
831 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
832 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy();
833}
834
835//===----------------------------------------------------------------------===//
836// VectorType Implementation
837//===----------------------------------------------------------------------===//
838
839VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
840 : Type(ElType->getContext(), TID), ContainedType(ElType),
841 ElementQuantity(EQ) {
842 ContainedTys = &ContainedType;
843 NumContainedTys = 1;
844}
845
846VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
847 if (EC.isScalable())
848 return ScalableVectorType::get(ElementType, MinNumElts: EC.getKnownMinValue());
849 else
850 return FixedVectorType::get(ElementType, NumElts: EC.getKnownMinValue());
851}
852
853bool VectorType::isValidElementType(Type *ElemTy) {
854 if (ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
855 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID ||
856 ElemTy->isByteTy())
857 return true;
858 if (auto *TTy = dyn_cast<TargetExtType>(Val: ElemTy))
859 return TTy->hasProperty(Prop: TargetExtType::CanBeVectorElement);
860 return false;
861}
862
863//===----------------------------------------------------------------------===//
864// FixedVectorType Implementation
865//===----------------------------------------------------------------------===//
866
867FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
868 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
869 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
870 "be an integer, floating point, "
871 "pointer type, or a valid target "
872 "extension type.");
873
874 auto EC = ElementCount::getFixed(MinVal: NumElts);
875
876 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
877 VectorType *&Entry = ElementType->getContext()
878 .pImpl->VectorTypes[std::make_pair(x&: ElementType, y&: EC)];
879
880 if (!Entry)
881 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
882 return cast<FixedVectorType>(Val: Entry);
883}
884
885//===----------------------------------------------------------------------===//
886// ScalableVectorType Implementation
887//===----------------------------------------------------------------------===//
888
889ScalableVectorType *ScalableVectorType::get(Type *ElementType,
890 unsigned MinNumElts) {
891 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
892 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
893 "be an integer, floating point, or "
894 "pointer type.");
895
896 auto EC = ElementCount::getScalable(MinVal: MinNumElts);
897
898 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
899 VectorType *&Entry = ElementType->getContext()
900 .pImpl->VectorTypes[std::make_pair(x&: ElementType, y&: EC)];
901
902 if (!Entry)
903 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
904 return cast<ScalableVectorType>(Val: Entry);
905}
906
907//===----------------------------------------------------------------------===//
908// PointerType Implementation
909//===----------------------------------------------------------------------===//
910
911PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
912 assert(EltTy && "Can't get a pointer to <null> type!");
913 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
914
915 // Automatically convert typed pointers to opaque pointers.
916 return get(C&: EltTy->getContext(), AddressSpace);
917}
918
919PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
920 LLVMContextImpl *CImpl = C.pImpl;
921
922 // Since AddressSpace #0 is the common case, we special case it.
923 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
924 : CImpl->PointerTypes[AddressSpace];
925
926 if (!Entry)
927 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
928 return Entry;
929}
930
931PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
932 : Type(C, PointerTyID) {
933 setSubclassData(AddrSpace);
934}
935
936PointerType *Type::getPointerTo(unsigned AddrSpace) const {
937 return PointerType::get(C&: getContext(), AddressSpace: AddrSpace);
938}
939
940bool PointerType::isValidElementType(Type *ElemTy) {
941 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
942 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
943 !ElemTy->isX86_AMXTy();
944}
945
946bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
947 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
948}
949
950//===----------------------------------------------------------------------===//
951// TargetExtType Implementation
952//===----------------------------------------------------------------------===//
953
954TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
955 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
956 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(S: Name)) {
957 NumContainedTys = Types.size();
958
959 // Parameter storage immediately follows the class in allocation.
960 Type **Params = reinterpret_cast<Type **>(this + 1);
961 ContainedTys = Params;
962 for (Type *T : Types)
963 *Params++ = T;
964
965 setSubclassData(Ints.size());
966 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
967 IntParams = IntParamSpace;
968 for (unsigned IntParam : Ints)
969 *IntParamSpace++ = IntParam;
970}
971
972TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
973 ArrayRef<Type *> Types,
974 ArrayRef<unsigned> Ints) {
975 return cantFail(ValOrErr: getOrError(Context&: C, Name, Types, Ints));
976}
977
978Expected<TargetExtType *> TargetExtType::getOrError(LLVMContext &C,
979 StringRef Name,
980 ArrayRef<Type *> Types,
981 ArrayRef<unsigned> Ints) {
982 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
983 TargetExtType *TT;
984 // Since we only want to allocate a fresh target type in case none is found
985 // and we don't want to perform two lookups (one for checking if existent and
986 // one for inserting the newly allocated one), here we instead lookup based on
987 // Key and update the reference to the target type in-place to a newly
988 // allocated one if not found.
989 auto [Iter, Inserted] = C.pImpl->TargetExtTypes.insert_as(V: nullptr, LookupKey: Key);
990 if (Inserted) {
991 // The target type was not found. Allocate one and update TargetExtTypes
992 // in-place.
993 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
994 Size: sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
995 sizeof(unsigned) * Ints.size(),
996 Alignment: alignof(TargetExtType));
997 new (TT) TargetExtType(C, Name, Types, Ints);
998 *Iter = TT;
999 return checkParams(TTy: TT);
1000 }
1001
1002 // The target type was found. Just return it.
1003 return *Iter;
1004}
1005
1006Expected<TargetExtType *> TargetExtType::checkParams(TargetExtType *TTy) {
1007 // Opaque types in the AArch64 name space.
1008 if (TTy->Name == "aarch64.svcount" &&
1009 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 0))
1010 return createStringError(
1011 Fmt: "target extension type aarch64.svcount should have no parameters");
1012
1013 // Opaque types in the RISC-V name space.
1014 if (TTy->Name == "riscv.vector.tuple" &&
1015 (TTy->getNumTypeParameters() != 1 || TTy->getNumIntParameters() != 1))
1016 return createStringError(
1017 Fmt: "target extension type riscv.vector.tuple should have one "
1018 "type parameter and one integer parameter");
1019
1020 // Opaque types in the AMDGPU name space.
1021 if (TTy->Name == "amdgcn.named.barrier" &&
1022 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 1)) {
1023 return createStringError(Fmt: "target extension type amdgcn.named.barrier "
1024 "should have no type parameters "
1025 "and one integer parameter");
1026 }
1027 if (TTy->Name == "amdgpu.stridemark" &&
1028 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() > 1)) {
1029 return createStringError(Fmt: "target extension type amdgpu.stridemark "
1030 "should have no type parameters "
1031 "and at most one integer parameter");
1032 }
1033
1034 return TTy;
1035}
1036
1037namespace {
1038struct TargetTypeInfo {
1039 Type *LayoutType;
1040 uint64_t Properties;
1041
1042 template <typename... ArgTys>
1043 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
1044 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {
1045 assert((!(this->Properties & TargetExtType::CanBeVectorElement) ||
1046 LayoutType->isSized()) &&
1047 "Vector element type must be sized");
1048 }
1049};
1050} // anonymous namespace
1051
1052static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
1053 LLVMContext &C = Ty->getContext();
1054 StringRef Name = Ty->getName();
1055 if (Name == "spirv.Image" || Name == "spirv.SignedImage")
1056 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::CanBeGlobal,
1057 TargetExtType::CanBeLocal);
1058 if (Name == "spirv.Type") {
1059 assert(Ty->getNumIntParameters() == 3 &&
1060 "Wrong number of parameters for spirv.Type");
1061
1062 auto Size = Ty->getIntParameter(i: 1);
1063 auto Alignment = Ty->getIntParameter(i: 2);
1064
1065 llvm::Type *LayoutType = nullptr;
1066 if (Size > 0 && Alignment > 0) {
1067 LayoutType =
1068 ArrayType::get(ElementType: Type::getIntNTy(C, N: Alignment), NumElements: Size * 8 / Alignment);
1069 } else {
1070 // LLVM expects variables that can be allocated to have an alignment and
1071 // size. Default to using a 32-bit int as the layout type if none are
1072 // present.
1073 LayoutType = Type::getInt32Ty(C);
1074 }
1075
1076 return TargetTypeInfo(LayoutType, TargetExtType::CanBeGlobal,
1077 TargetExtType::CanBeLocal);
1078 }
1079 if (Name == "spirv.IntegralConstant" || Name == "spirv.Literal")
1080 return TargetTypeInfo(Type::getVoidTy(C));
1081 if (Name == "spirv.Padding")
1082 return TargetTypeInfo(
1083 ArrayType::get(ElementType: Type::getInt8Ty(C), NumElements: Ty->getIntParameter(i: 0)),
1084 TargetExtType::CanBeGlobal);
1085 if (Name.starts_with(Prefix: "spirv."))
1086 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::HasZeroInit,
1087 TargetExtType::CanBeGlobal,
1088 TargetExtType::CanBeLocal);
1089
1090 // Opaque types in the AArch64 name space.
1091 if (Name == "aarch64.svcount")
1092 return TargetTypeInfo(ScalableVectorType::get(ElementType: Type::getInt1Ty(C), MinNumElts: 16),
1093 TargetExtType::HasZeroInit,
1094 TargetExtType::CanBeLocal);
1095
1096 // RISC-V vector tuple type. The layout is represented as the type that needs
1097 // the same number of vector registers(VREGS) as this tuple type, represented
1098 // as <vscale x (RVVBitsPerBlock * VREGS / 8) x i8>.
1099 if (Name == "riscv.vector.tuple") {
1100 unsigned TotalNumElts =
1101 std::max(a: cast<ScalableVectorType>(Val: Ty->getTypeParameter(i: 0))
1102 ->getMinNumElements(),
1103 b: RISCV::RVVBytesPerBlock) *
1104 Ty->getIntParameter(i: 0);
1105 return TargetTypeInfo(
1106 ScalableVectorType::get(ElementType: Type::getInt8Ty(C), MinNumElts: TotalNumElts),
1107 TargetExtType::CanBeLocal, TargetExtType::HasZeroInit);
1108 }
1109
1110 // DirectX resources
1111 if (Name == "dx.Padding")
1112 return TargetTypeInfo(
1113 ArrayType::get(ElementType: Type::getInt8Ty(C), NumElements: Ty->getIntParameter(i: 0)),
1114 TargetExtType::CanBeGlobal);
1115 if (Name.starts_with(Prefix: "dx."))
1116 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::CanBeGlobal,
1117 TargetExtType::CanBeLocal,
1118 TargetExtType::IsTokenLike);
1119
1120 // Opaque types in the AMDGPU name space.
1121 if (Name == "amdgcn.named.barrier") {
1122 return TargetTypeInfo(FixedVectorType::get(ElementType: Type::getInt32Ty(C), NumElts: 4),
1123 TargetExtType::CanBeGlobal);
1124 }
1125 if (Name == "amdgpu.stridemark")
1126 return TargetTypeInfo(Type::getVoidTy(C), TargetExtType::IsTokenLike);
1127
1128 // Type used to test vector element target extension property.
1129 // Can be removed once a public target extension type uses CanBeVectorElement.
1130 if (Name == "llvm.test.vectorelement") {
1131 return TargetTypeInfo(Type::getInt32Ty(C), TargetExtType::CanBeLocal,
1132 TargetExtType::CanBeVectorElement);
1133 }
1134
1135 // Opaque types in the WebAssembly name space.
1136 if (Name == "wasm.funcref" || Name == "wasm.externref")
1137 return TargetTypeInfo(PointerType::getUnqual(C), TargetExtType::HasZeroInit,
1138 TargetExtType::CanBeGlobal,
1139 TargetExtType::CanBeLocal);
1140
1141 return TargetTypeInfo(Type::getVoidTy(C));
1142}
1143
1144bool Type::isTokenLikeTy() const {
1145 if (isTokenTy())
1146 return true;
1147 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
1148 return TT->hasProperty(Prop: TargetExtType::Property::IsTokenLike);
1149 return false;
1150}
1151
1152Type *TargetExtType::getLayoutType() const {
1153 return getTargetTypeInfo(Ty: this).LayoutType;
1154}
1155
1156bool TargetExtType::hasProperty(Property Prop) const {
1157 uint64_t Properties = getTargetTypeInfo(Ty: this).Properties;
1158 return (Properties & Prop) == Prop;
1159}
1160