xref: /openbsd/gnu/llvm/llvm/lib/IR/Type.cpp (revision d415bd75)
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/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>
29 
30 using namespace llvm;
31 
32 //===----------------------------------------------------------------------===//
33 //                         Type Class Implementation
34 //===----------------------------------------------------------------------===//
35 
getPrimitiveType(LLVMContext & C,TypeID IDNumber)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;
53   }
54 }
55 
isIntegerTy(unsigned Bitwidth) const56 bool Type::isIntegerTy(unsigned Bitwidth) const {
57   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
58 }
59 
isOpaquePointerTy() const60 bool Type::isOpaquePointerTy() const {
61   if (auto *PTy = dyn_cast<PointerType>(this))
62     return PTy->isOpaque();
63   return false;
64 }
65 
getFltSemantics() const66 const fltSemantics &Type::getFltSemantics() const {
67   switch (getTypeID()) {
68   case HalfTyID: return APFloat::IEEEhalf();
69   case BFloatTyID: return APFloat::BFloat();
70   case FloatTyID: return APFloat::IEEEsingle();
71   case DoubleTyID: return APFloat::IEEEdouble();
72   case X86_FP80TyID: return APFloat::x87DoubleExtended();
73   case FP128TyID: return APFloat::IEEEquad();
74   case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
75   default: llvm_unreachable("Invalid floating type");
76   }
77 }
78 
isIEEE() const79 bool Type::isIEEE() const {
80   return APFloat::getZero(getFltSemantics()).isIEEE();
81 }
82 
getFloatingPointTy(LLVMContext & C,const fltSemantics & S)83 Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
84   Type *Ty;
85   if (&S == &APFloat::IEEEhalf())
86     Ty = Type::getHalfTy(C);
87   else if (&S == &APFloat::BFloat())
88     Ty = Type::getBFloatTy(C);
89   else if (&S == &APFloat::IEEEsingle())
90     Ty = Type::getFloatTy(C);
91   else if (&S == &APFloat::IEEEdouble())
92     Ty = Type::getDoubleTy(C);
93   else if (&S == &APFloat::x87DoubleExtended())
94     Ty = Type::getX86_FP80Ty(C);
95   else if (&S == &APFloat::IEEEquad())
96     Ty = Type::getFP128Ty(C);
97   else {
98     assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
99     Ty = Type::getPPC_FP128Ty(C);
100   }
101   return Ty;
102 }
103 
canLosslesslyBitCastTo(Type * Ty) const104 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
105   // Identity cast means no change so return true
106   if (this == Ty)
107     return true;
108 
109   // They are not convertible unless they are at least first class types
110   if (!this->isFirstClassType() || !Ty->isFirstClassType())
111     return false;
112 
113   // Vector -> Vector conversions are always lossless if the two vector types
114   // have the same size, otherwise not.
115   if (isa<VectorType>(this) && isa<VectorType>(Ty))
116     return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
117 
118   //  64-bit fixed width vector types can be losslessly converted to x86mmx.
119   if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
120       getPrimitiveSizeInBits().getFixedValue() == 64)
121     return true;
122   if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
123       Ty->getPrimitiveSizeInBits().getFixedValue() == 64)
124     return true;
125 
126   //  8192-bit fixed width vector types can be losslessly converted to x86amx.
127   if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
128       getPrimitiveSizeInBits().getFixedValue() == 8192)
129     return true;
130   if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
131       Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
132     return true;
133 
134   // At this point we have only various mismatches of the first class types
135   // remaining and ptr->ptr. Just select the lossless conversions. Everything
136   // else is not lossless. Conservatively assume we can't losslessly convert
137   // between pointers with different address spaces.
138   if (auto *PTy = dyn_cast<PointerType>(this)) {
139     if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
140       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
141     return false;
142   }
143   return false;  // Other types have no identity values
144 }
145 
isEmptyTy() const146 bool Type::isEmptyTy() const {
147   if (auto *ATy = dyn_cast<ArrayType>(this)) {
148     unsigned NumElements = ATy->getNumElements();
149     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
150   }
151 
152   if (auto *STy = dyn_cast<StructType>(this)) {
153     unsigned NumElements = STy->getNumElements();
154     for (unsigned i = 0; i < NumElements; ++i)
155       if (!STy->getElementType(i)->isEmptyTy())
156         return false;
157     return true;
158   }
159 
160   return false;
161 }
162 
getPrimitiveSizeInBits() const163 TypeSize Type::getPrimitiveSizeInBits() const {
164   switch (getTypeID()) {
165   case Type::HalfTyID: return TypeSize::Fixed(16);
166   case Type::BFloatTyID: return TypeSize::Fixed(16);
167   case Type::FloatTyID: return TypeSize::Fixed(32);
168   case Type::DoubleTyID: return TypeSize::Fixed(64);
169   case Type::X86_FP80TyID: return TypeSize::Fixed(80);
170   case Type::FP128TyID: return TypeSize::Fixed(128);
171   case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
172   case Type::X86_MMXTyID: return TypeSize::Fixed(64);
173   case Type::X86_AMXTyID: return TypeSize::Fixed(8192);
174   case Type::IntegerTyID:
175     return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
176   case Type::FixedVectorTyID:
177   case Type::ScalableVectorTyID: {
178     const VectorType *VTy = cast<VectorType>(this);
179     ElementCount EC = VTy->getElementCount();
180     TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
181     assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
182     return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
183   }
184   default: return TypeSize::Fixed(0);
185   }
186 }
187 
getScalarSizeInBits() const188 unsigned Type::getScalarSizeInBits() const {
189   // It is safe to assume that the scalar types have a fixed size.
190   return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
191 }
192 
getFPMantissaWidth() const193 int Type::getFPMantissaWidth() const {
194   if (auto *VTy = dyn_cast<VectorType>(this))
195     return VTy->getElementType()->getFPMantissaWidth();
196   assert(isFloatingPointTy() && "Not a floating point type!");
197   if (getTypeID() == HalfTyID) return 11;
198   if (getTypeID() == BFloatTyID) return 8;
199   if (getTypeID() == FloatTyID) return 24;
200   if (getTypeID() == DoubleTyID) return 53;
201   if (getTypeID() == X86_FP80TyID) return 64;
202   if (getTypeID() == FP128TyID) return 113;
203   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
204   return -1;
205 }
206 
isSizedDerivedType(SmallPtrSetImpl<Type * > * Visited) const207 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
208   if (auto *ATy = dyn_cast<ArrayType>(this))
209     return ATy->getElementType()->isSized(Visited);
210 
211   if (auto *VTy = dyn_cast<VectorType>(this))
212     return VTy->getElementType()->isSized(Visited);
213 
214   if (auto *TTy = dyn_cast<TargetExtType>(this))
215     return TTy->getLayoutType()->isSized(Visited);
216 
217   return cast<StructType>(this)->isSized(Visited);
218 }
219 
220 //===----------------------------------------------------------------------===//
221 //                          Primitive 'Type' data
222 //===----------------------------------------------------------------------===//
223 
getVoidTy(LLVMContext & C)224 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
getLabelTy(LLVMContext & C)225 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
getHalfTy(LLVMContext & C)226 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
getBFloatTy(LLVMContext & C)227 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
getFloatTy(LLVMContext & C)228 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
getDoubleTy(LLVMContext & C)229 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
getMetadataTy(LLVMContext & C)230 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
getTokenTy(LLVMContext & C)231 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
getX86_FP80Ty(LLVMContext & C)232 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
getFP128Ty(LLVMContext & C)233 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
getPPC_FP128Ty(LLVMContext & C)234 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
getX86_MMXTy(LLVMContext & C)235 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
getX86_AMXTy(LLVMContext & C)236 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
237 
getInt1Ty(LLVMContext & C)238 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
getInt8Ty(LLVMContext & C)239 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
getInt16Ty(LLVMContext & C)240 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
getInt32Ty(LLVMContext & C)241 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
getInt64Ty(LLVMContext & C)242 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
getInt128Ty(LLVMContext & C)243 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
244 
getIntNTy(LLVMContext & C,unsigned N)245 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
246   return IntegerType::get(C, N);
247 }
248 
getHalfPtrTy(LLVMContext & C,unsigned AS)249 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
250   return getHalfTy(C)->getPointerTo(AS);
251 }
252 
getBFloatPtrTy(LLVMContext & C,unsigned AS)253 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
254   return getBFloatTy(C)->getPointerTo(AS);
255 }
256 
getFloatPtrTy(LLVMContext & C,unsigned AS)257 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
258   return getFloatTy(C)->getPointerTo(AS);
259 }
260 
getDoublePtrTy(LLVMContext & C,unsigned AS)261 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
262   return getDoubleTy(C)->getPointerTo(AS);
263 }
264 
getX86_FP80PtrTy(LLVMContext & C,unsigned AS)265 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
266   return getX86_FP80Ty(C)->getPointerTo(AS);
267 }
268 
getFP128PtrTy(LLVMContext & C,unsigned AS)269 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
270   return getFP128Ty(C)->getPointerTo(AS);
271 }
272 
getPPC_FP128PtrTy(LLVMContext & C,unsigned AS)273 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
274   return getPPC_FP128Ty(C)->getPointerTo(AS);
275 }
276 
getX86_MMXPtrTy(LLVMContext & C,unsigned AS)277 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
278   return getX86_MMXTy(C)->getPointerTo(AS);
279 }
280 
getX86_AMXPtrTy(LLVMContext & C,unsigned AS)281 PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) {
282   return getX86_AMXTy(C)->getPointerTo(AS);
283 }
284 
getIntNPtrTy(LLVMContext & C,unsigned N,unsigned AS)285 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
286   return getIntNTy(C, N)->getPointerTo(AS);
287 }
288 
getInt1PtrTy(LLVMContext & C,unsigned AS)289 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
290   return getInt1Ty(C)->getPointerTo(AS);
291 }
292 
getInt8PtrTy(LLVMContext & C,unsigned AS)293 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
294   return getInt8Ty(C)->getPointerTo(AS);
295 }
296 
getInt16PtrTy(LLVMContext & C,unsigned AS)297 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
298   return getInt16Ty(C)->getPointerTo(AS);
299 }
300 
getInt32PtrTy(LLVMContext & C,unsigned AS)301 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
302   return getInt32Ty(C)->getPointerTo(AS);
303 }
304 
getInt64PtrTy(LLVMContext & C,unsigned AS)305 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
306   return getInt64Ty(C)->getPointerTo(AS);
307 }
308 
309 //===----------------------------------------------------------------------===//
310 //                       IntegerType Implementation
311 //===----------------------------------------------------------------------===//
312 
get(LLVMContext & C,unsigned NumBits)313 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
314   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
315   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
316 
317   // Check for the built-in integer types
318   switch (NumBits) {
319   case   1: return cast<IntegerType>(Type::getInt1Ty(C));
320   case   8: return cast<IntegerType>(Type::getInt8Ty(C));
321   case  16: return cast<IntegerType>(Type::getInt16Ty(C));
322   case  32: return cast<IntegerType>(Type::getInt32Ty(C));
323   case  64: return cast<IntegerType>(Type::getInt64Ty(C));
324   case 128: return cast<IntegerType>(Type::getInt128Ty(C));
325   default:
326     break;
327   }
328 
329   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
330 
331   if (!Entry)
332     Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
333 
334   return Entry;
335 }
336 
getMask() const337 APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }
338 
339 //===----------------------------------------------------------------------===//
340 //                       FunctionType Implementation
341 //===----------------------------------------------------------------------===//
342 
FunctionType(Type * Result,ArrayRef<Type * > Params,bool IsVarArgs)343 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
344                            bool IsVarArgs)
345   : Type(Result->getContext(), FunctionTyID) {
346   Type **SubTys = reinterpret_cast<Type**>(this+1);
347   assert(isValidReturnType(Result) && "invalid return type for function");
348   setSubclassData(IsVarArgs);
349 
350   SubTys[0] = Result;
351 
352   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
353     assert(isValidArgumentType(Params[i]) &&
354            "Not a valid type for function argument!");
355     SubTys[i+1] = Params[i];
356   }
357 
358   ContainedTys = SubTys;
359   NumContainedTys = Params.size() + 1; // + 1 for result type
360 }
361 
362 // This is the factory function for the FunctionType class.
get(Type * ReturnType,ArrayRef<Type * > Params,bool isVarArg)363 FunctionType *FunctionType::get(Type *ReturnType,
364                                 ArrayRef<Type*> Params, bool isVarArg) {
365   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
366   const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
367   FunctionType *FT;
368   // Since we only want to allocate a fresh function type in case none is found
369   // and we don't want to perform two lookups (one for checking if existent and
370   // one for inserting the newly allocated one), here we instead lookup based on
371   // Key and update the reference to the function type in-place to a newly
372   // allocated one if not found.
373   auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
374   if (Insertion.second) {
375     // The function type was not found. Allocate one and update FunctionTypes
376     // in-place.
377     FT = (FunctionType *)pImpl->Alloc.Allocate(
378         sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
379         alignof(FunctionType));
380     new (FT) FunctionType(ReturnType, Params, isVarArg);
381     *Insertion.first = FT;
382   } else {
383     // The function type was found. Just return it.
384     FT = *Insertion.first;
385   }
386   return FT;
387 }
388 
get(Type * Result,bool isVarArg)389 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
390   return get(Result, std::nullopt, isVarArg);
391 }
392 
isValidReturnType(Type * RetTy)393 bool FunctionType::isValidReturnType(Type *RetTy) {
394   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
395   !RetTy->isMetadataTy();
396 }
397 
isValidArgumentType(Type * ArgTy)398 bool FunctionType::isValidArgumentType(Type *ArgTy) {
399   return ArgTy->isFirstClassType();
400 }
401 
402 //===----------------------------------------------------------------------===//
403 //                       StructType Implementation
404 //===----------------------------------------------------------------------===//
405 
406 // Primitive Constructors.
407 
get(LLVMContext & Context,ArrayRef<Type * > ETypes,bool isPacked)408 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
409                             bool isPacked) {
410   LLVMContextImpl *pImpl = Context.pImpl;
411   const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
412 
413   StructType *ST;
414   // Since we only want to allocate a fresh struct type in case none is found
415   // and we don't want to perform two lookups (one for checking if existent and
416   // one for inserting the newly allocated one), here we instead lookup based on
417   // Key and update the reference to the struct type in-place to a newly
418   // allocated one if not found.
419   auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
420   if (Insertion.second) {
421     // The struct type was not found. Allocate one and update AnonStructTypes
422     // in-place.
423     ST = new (Context.pImpl->Alloc) StructType(Context);
424     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
425     ST->setBody(ETypes, isPacked);
426     *Insertion.first = ST;
427   } else {
428     // The struct type was found. Just return it.
429     ST = *Insertion.first;
430   }
431 
432   return ST;
433 }
434 
containsScalableVectorType() const435 bool StructType::containsScalableVectorType() const {
436   for (Type *Ty : elements()) {
437     if (isa<ScalableVectorType>(Ty))
438       return true;
439     if (auto *STy = dyn_cast<StructType>(Ty))
440       if (STy->containsScalableVectorType())
441         return true;
442   }
443 
444   return false;
445 }
446 
setBody(ArrayRef<Type * > Elements,bool isPacked)447 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
448   assert(isOpaque() && "Struct body already set!");
449 
450   setSubclassData(getSubclassData() | SCDB_HasBody);
451   if (isPacked)
452     setSubclassData(getSubclassData() | SCDB_Packed);
453 
454   NumContainedTys = Elements.size();
455 
456   if (Elements.empty()) {
457     ContainedTys = nullptr;
458     return;
459   }
460 
461   ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
462 }
463 
setName(StringRef Name)464 void StructType::setName(StringRef Name) {
465   if (Name == getName()) return;
466 
467   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
468 
469   using EntryTy = StringMap<StructType *>::MapEntryTy;
470 
471   // If this struct already had a name, remove its symbol table entry. Don't
472   // delete the data yet because it may be part of the new name.
473   if (SymbolTableEntry)
474     SymbolTable.remove((EntryTy *)SymbolTableEntry);
475 
476   // If this is just removing the name, we're done.
477   if (Name.empty()) {
478     if (SymbolTableEntry) {
479       // Delete the old string data.
480       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
481       SymbolTableEntry = nullptr;
482     }
483     return;
484   }
485 
486   // Look up the entry for the name.
487   auto IterBool =
488       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
489 
490   // While we have a name collision, try a random rename.
491   if (!IterBool.second) {
492     SmallString<64> TempStr(Name);
493     TempStr.push_back('.');
494     raw_svector_ostream TmpStream(TempStr);
495     unsigned NameSize = Name.size();
496 
497     do {
498       TempStr.resize(NameSize + 1);
499       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
500 
501       IterBool = getContext().pImpl->NamedStructTypes.insert(
502           std::make_pair(TmpStream.str(), this));
503     } while (!IterBool.second);
504   }
505 
506   // Delete the old string data.
507   if (SymbolTableEntry)
508     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
509   SymbolTableEntry = &*IterBool.first;
510 }
511 
512 //===----------------------------------------------------------------------===//
513 // StructType Helper functions.
514 
create(LLVMContext & Context,StringRef Name)515 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
516   StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
517   if (!Name.empty())
518     ST->setName(Name);
519   return ST;
520 }
521 
get(LLVMContext & Context,bool isPacked)522 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
523   return get(Context, std::nullopt, isPacked);
524 }
525 
create(LLVMContext & Context,ArrayRef<Type * > Elements,StringRef Name,bool isPacked)526 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
527                                StringRef Name, bool isPacked) {
528   StructType *ST = create(Context, Name);
529   ST->setBody(Elements, isPacked);
530   return ST;
531 }
532 
create(LLVMContext & Context,ArrayRef<Type * > Elements)533 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
534   return create(Context, Elements, StringRef());
535 }
536 
create(LLVMContext & Context)537 StructType *StructType::create(LLVMContext &Context) {
538   return create(Context, StringRef());
539 }
540 
create(ArrayRef<Type * > Elements,StringRef Name,bool isPacked)541 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
542                                bool isPacked) {
543   assert(!Elements.empty() &&
544          "This method may not be invoked with an empty list");
545   return create(Elements[0]->getContext(), Elements, Name, isPacked);
546 }
547 
create(ArrayRef<Type * > Elements)548 StructType *StructType::create(ArrayRef<Type*> Elements) {
549   assert(!Elements.empty() &&
550          "This method may not be invoked with an empty list");
551   return create(Elements[0]->getContext(), Elements, StringRef());
552 }
553 
isSized(SmallPtrSetImpl<Type * > * Visited) const554 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
555   if ((getSubclassData() & SCDB_IsSized) != 0)
556     return true;
557   if (isOpaque())
558     return false;
559 
560   if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
561     return false;
562 
563   // Okay, our struct is sized if all of the elements are, but if one of the
564   // elements is opaque, the struct isn't sized *yet*, but may become sized in
565   // the future, so just bail out without caching.
566   for (Type *Ty : elements()) {
567     // If the struct contains a scalable vector type, don't consider it sized.
568     // This prevents it from being used in loads/stores/allocas/GEPs.
569     if (isa<ScalableVectorType>(Ty))
570       return false;
571     if (!Ty->isSized(Visited))
572       return false;
573   }
574 
575   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
576   // we find a sized type, as types can only move from opaque to sized, not the
577   // other way.
578   const_cast<StructType*>(this)->setSubclassData(
579     getSubclassData() | SCDB_IsSized);
580   return true;
581 }
582 
getName() const583 StringRef StructType::getName() const {
584   assert(!isLiteral() && "Literal structs never have names");
585   if (!SymbolTableEntry) return StringRef();
586 
587   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
588 }
589 
isValidElementType(Type * ElemTy)590 bool StructType::isValidElementType(Type *ElemTy) {
591   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
592          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
593          !ElemTy->isTokenTy();
594 }
595 
isLayoutIdentical(StructType * Other) const596 bool StructType::isLayoutIdentical(StructType *Other) const {
597   if (this == Other) return true;
598 
599   if (isPacked() != Other->isPacked())
600     return false;
601 
602   return elements() == Other->elements();
603 }
604 
getTypeAtIndex(const Value * V) const605 Type *StructType::getTypeAtIndex(const Value *V) const {
606   unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
607   assert(indexValid(Idx) && "Invalid structure index!");
608   return getElementType(Idx);
609 }
610 
indexValid(const Value * V) const611 bool StructType::indexValid(const Value *V) const {
612   // Structure indexes require (vectors of) 32-bit integer constants.  In the
613   // vector case all of the indices must be equal.
614   if (!V->getType()->isIntOrIntVectorTy(32))
615     return false;
616   if (isa<ScalableVectorType>(V->getType()))
617     return false;
618   const Constant *C = dyn_cast<Constant>(V);
619   if (C && V->getType()->isVectorTy())
620     C = C->getSplatValue();
621   const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
622   return CU && CU->getZExtValue() < getNumElements();
623 }
624 
getTypeByName(LLVMContext & C,StringRef Name)625 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
626   return C.pImpl->NamedStructTypes.lookup(Name);
627 }
628 
629 //===----------------------------------------------------------------------===//
630 //                           ArrayType Implementation
631 //===----------------------------------------------------------------------===//
632 
ArrayType(Type * ElType,uint64_t NumEl)633 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
634     : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
635       NumElements(NumEl) {
636   ContainedTys = &ContainedType;
637   NumContainedTys = 1;
638 }
639 
get(Type * ElementType,uint64_t NumElements)640 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
641   assert(isValidElementType(ElementType) && "Invalid type for array element!");
642 
643   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
644   ArrayType *&Entry =
645     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
646 
647   if (!Entry)
648     Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
649   return Entry;
650 }
651 
isValidElementType(Type * ElemTy)652 bool ArrayType::isValidElementType(Type *ElemTy) {
653   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
654          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
655          !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy() &&
656          !isa<ScalableVectorType>(ElemTy);
657 }
658 
659 //===----------------------------------------------------------------------===//
660 //                          VectorType Implementation
661 //===----------------------------------------------------------------------===//
662 
VectorType(Type * ElType,unsigned EQ,Type::TypeID TID)663 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
664     : Type(ElType->getContext(), TID), ContainedType(ElType),
665       ElementQuantity(EQ) {
666   ContainedTys = &ContainedType;
667   NumContainedTys = 1;
668 }
669 
get(Type * ElementType,ElementCount EC)670 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
671   if (EC.isScalable())
672     return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
673   else
674     return FixedVectorType::get(ElementType, EC.getKnownMinValue());
675 }
676 
isValidElementType(Type * ElemTy)677 bool VectorType::isValidElementType(Type *ElemTy) {
678   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
679          ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID;
680 }
681 
682 //===----------------------------------------------------------------------===//
683 //                        FixedVectorType Implementation
684 //===----------------------------------------------------------------------===//
685 
get(Type * ElementType,unsigned NumElts)686 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
687   assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
688   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
689                                             "be an integer, floating point, or "
690                                             "pointer type.");
691 
692   auto EC = ElementCount::getFixed(NumElts);
693 
694   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
695   VectorType *&Entry = ElementType->getContext()
696                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
697 
698   if (!Entry)
699     Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
700   return cast<FixedVectorType>(Entry);
701 }
702 
703 //===----------------------------------------------------------------------===//
704 //                       ScalableVectorType Implementation
705 //===----------------------------------------------------------------------===//
706 
get(Type * ElementType,unsigned MinNumElts)707 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
708                                             unsigned MinNumElts) {
709   assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
710   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
711                                             "be an integer, floating point, or "
712                                             "pointer type.");
713 
714   auto EC = ElementCount::getScalable(MinNumElts);
715 
716   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
717   VectorType *&Entry = ElementType->getContext()
718                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
719 
720   if (!Entry)
721     Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
722   return cast<ScalableVectorType>(Entry);
723 }
724 
725 //===----------------------------------------------------------------------===//
726 //                         PointerType Implementation
727 //===----------------------------------------------------------------------===//
728 
get(Type * EltTy,unsigned AddressSpace)729 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
730   assert(EltTy && "Can't get a pointer to <null> type!");
731   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
732 
733   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
734 
735   // Automatically convert typed pointers to opaque pointers.
736   if (CImpl->getOpaquePointers())
737     return get(EltTy->getContext(), AddressSpace);
738 
739   // Since AddressSpace #0 is the common case, we special case it.
740   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
741      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
742 
743   if (!Entry)
744     Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace);
745   return Entry;
746 }
747 
get(LLVMContext & C,unsigned AddressSpace)748 PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
749   LLVMContextImpl *CImpl = C.pImpl;
750   assert(CImpl->getOpaquePointers() &&
751          "Can only create opaque pointers in opaque pointer mode");
752 
753   // Since AddressSpace #0 is the common case, we special case it.
754   PointerType *&Entry =
755       AddressSpace == 0
756           ? CImpl->PointerTypes[nullptr]
757           : CImpl->ASPointerTypes[std::make_pair(nullptr, AddressSpace)];
758 
759   if (!Entry)
760     Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
761   return Entry;
762 }
763 
PointerType(Type * E,unsigned AddrSpace)764 PointerType::PointerType(Type *E, unsigned AddrSpace)
765   : Type(E->getContext(), PointerTyID), PointeeTy(E) {
766   ContainedTys = &PointeeTy;
767   NumContainedTys = 1;
768   setSubclassData(AddrSpace);
769 }
770 
PointerType(LLVMContext & C,unsigned AddrSpace)771 PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
772     : Type(C, PointerTyID), PointeeTy(nullptr) {
773   setSubclassData(AddrSpace);
774 }
775 
getPointerTo(unsigned AddrSpace) const776 PointerType *Type::getPointerTo(unsigned AddrSpace) const {
777   return PointerType::get(const_cast<Type*>(this), AddrSpace);
778 }
779 
isValidElementType(Type * ElemTy)780 bool PointerType::isValidElementType(Type *ElemTy) {
781   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
782          !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
783          !ElemTy->isX86_AMXTy();
784 }
785 
isLoadableOrStorableType(Type * ElemTy)786 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
787   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
788 }
789 
790 //===----------------------------------------------------------------------===//
791 //                       TargetExtType Implementation
792 //===----------------------------------------------------------------------===//
793 
TargetExtType(LLVMContext & C,StringRef Name,ArrayRef<Type * > Types,ArrayRef<unsigned> Ints)794 TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
795                              ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
796     : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {
797   NumContainedTys = Types.size();
798 
799   // Parameter storage immediately follows the class in allocation.
800   Type **Params = reinterpret_cast<Type **>(this + 1);
801   ContainedTys = Params;
802   for (Type *T : Types)
803     *Params++ = T;
804 
805   setSubclassData(Ints.size());
806   unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
807   IntParams = IntParamSpace;
808   for (unsigned IntParam : Ints)
809     *IntParamSpace++ = IntParam;
810 }
811 
get(LLVMContext & C,StringRef Name,ArrayRef<Type * > Types,ArrayRef<unsigned> Ints)812 TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
813                                   ArrayRef<Type *> Types,
814                                   ArrayRef<unsigned> Ints) {
815   const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
816   TargetExtType *TT;
817   // Since we only want to allocate a fresh target type in case none is found
818   // and we don't want to perform two lookups (one for checking if existent and
819   // one for inserting the newly allocated one), here we instead lookup based on
820   // Key and update the reference to the target type in-place to a newly
821   // allocated one if not found.
822   auto Insertion = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);
823   if (Insertion.second) {
824     // The target type was not found. Allocate one and update TargetExtTypes
825     // in-place.
826     TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
827         sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
828             sizeof(unsigned) * Ints.size(),
829         alignof(TargetExtType));
830     new (TT) TargetExtType(C, Name, Types, Ints);
831     *Insertion.first = TT;
832   } else {
833     // The target type was found. Just return it.
834     TT = *Insertion.first;
835   }
836   return TT;
837 }
838 
839 namespace {
840 struct TargetTypeInfo {
841   Type *LayoutType;
842   uint64_t Properties;
843 
844   template <typename... ArgTys>
TargetTypeInfo__anond34a23290111::TargetTypeInfo845   TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
846       : LayoutType(LayoutType), Properties((0 | ... | Properties)) {}
847 };
848 } // anonymous namespace
849 
getTargetTypeInfo(const TargetExtType * Ty)850 static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
851   LLVMContext &C = Ty->getContext();
852   StringRef Name = Ty->getName();
853   if (Name.startswith("spirv.")) {
854     return TargetTypeInfo(Type::getInt8PtrTy(C, 0), TargetExtType::HasZeroInit,
855                           TargetExtType::CanBeGlobal);
856   }
857   return TargetTypeInfo(Type::getVoidTy(C));
858 }
859 
getLayoutType() const860 Type *TargetExtType::getLayoutType() const {
861   return getTargetTypeInfo(this).LayoutType;
862 }
863 
hasProperty(Property Prop) const864 bool TargetExtType::hasProperty(Property Prop) const {
865   uint64_t Properties = getTargetTypeInfo(this).Properties;
866   return (Properties & Prop) == Prop;
867 }
868