• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Type.cpp - Implement the Type class -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Type class for the IR library.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/Type.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/Module.h"
18 #include <algorithm>
19 #include <cstdarg>
20 using namespace llvm;
21 
22 //===----------------------------------------------------------------------===//
23 //                         Type Class Implementation
24 //===----------------------------------------------------------------------===//
25 
getPrimitiveType(LLVMContext & C,TypeID IDNumber)26 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
27   switch (IDNumber) {
28   case VoidTyID      : return getVoidTy(C);
29   case HalfTyID      : return getHalfTy(C);
30   case FloatTyID     : return getFloatTy(C);
31   case DoubleTyID    : return getDoubleTy(C);
32   case X86_FP80TyID  : return getX86_FP80Ty(C);
33   case FP128TyID     : return getFP128Ty(C);
34   case PPC_FP128TyID : return getPPC_FP128Ty(C);
35   case LabelTyID     : return getLabelTy(C);
36   case MetadataTyID  : return getMetadataTy(C);
37   case X86_MMXTyID   : return getX86_MMXTy(C);
38   case TokenTyID     : return getTokenTy(C);
39   default:
40     return nullptr;
41   }
42 }
43 
getScalarType() const44 Type *Type::getScalarType() const {
45   if (auto *VTy = dyn_cast<VectorType>(this))
46     return VTy->getElementType();
47   return const_cast<Type*>(this);
48 }
49 
isIntegerTy(unsigned Bitwidth) const50 bool Type::isIntegerTy(unsigned Bitwidth) const {
51   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
52 }
53 
canLosslesslyBitCastTo(Type * Ty) const54 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
55   // Identity cast means no change so return true
56   if (this == Ty)
57     return true;
58 
59   // They are not convertible unless they are at least first class types
60   if (!this->isFirstClassType() || !Ty->isFirstClassType())
61     return false;
62 
63   // Vector -> Vector conversions are always lossless if the two vector types
64   // have the same size, otherwise not.  Also, 64-bit vector types can be
65   // converted to x86mmx.
66   if (auto *thisPTy = dyn_cast<VectorType>(this)) {
67     if (auto *thatPTy = dyn_cast<VectorType>(Ty))
68       return thisPTy->getBitWidth() == thatPTy->getBitWidth();
69     if (Ty->getTypeID() == Type::X86_MMXTyID &&
70         thisPTy->getBitWidth() == 64)
71       return true;
72   }
73 
74   if (this->getTypeID() == Type::X86_MMXTyID)
75     if (auto *thatPTy = dyn_cast<VectorType>(Ty))
76       if (thatPTy->getBitWidth() == 64)
77         return true;
78 
79   // At this point we have only various mismatches of the first class types
80   // remaining and ptr->ptr. Just select the lossless conversions. Everything
81   // else is not lossless. Conservatively assume we can't losslessly convert
82   // between pointers with different address spaces.
83   if (auto *PTy = dyn_cast<PointerType>(this)) {
84     if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
85       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
86     return false;
87   }
88   return false;  // Other types have no identity values
89 }
90 
isEmptyTy() const91 bool Type::isEmptyTy() const {
92   if (auto *ATy = dyn_cast<ArrayType>(this)) {
93     unsigned NumElements = ATy->getNumElements();
94     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
95   }
96 
97   if (auto *STy = dyn_cast<StructType>(this)) {
98     unsigned NumElements = STy->getNumElements();
99     for (unsigned i = 0; i < NumElements; ++i)
100       if (!STy->getElementType(i)->isEmptyTy())
101         return false;
102     return true;
103   }
104 
105   return false;
106 }
107 
getPrimitiveSizeInBits() const108 unsigned Type::getPrimitiveSizeInBits() const {
109   switch (getTypeID()) {
110   case Type::HalfTyID: return 16;
111   case Type::FloatTyID: return 32;
112   case Type::DoubleTyID: return 64;
113   case Type::X86_FP80TyID: return 80;
114   case Type::FP128TyID: return 128;
115   case Type::PPC_FP128TyID: return 128;
116   case Type::X86_MMXTyID: return 64;
117   case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
118   case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
119   default: return 0;
120   }
121 }
122 
getScalarSizeInBits() const123 unsigned Type::getScalarSizeInBits() const {
124   return getScalarType()->getPrimitiveSizeInBits();
125 }
126 
getFPMantissaWidth() const127 int Type::getFPMantissaWidth() const {
128   if (auto *VTy = dyn_cast<VectorType>(this))
129     return VTy->getElementType()->getFPMantissaWidth();
130   assert(isFloatingPointTy() && "Not a floating point type!");
131   if (getTypeID() == HalfTyID) return 11;
132   if (getTypeID() == FloatTyID) return 24;
133   if (getTypeID() == DoubleTyID) return 53;
134   if (getTypeID() == X86_FP80TyID) return 64;
135   if (getTypeID() == FP128TyID) return 113;
136   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
137   return -1;
138 }
139 
isSizedDerivedType(SmallPtrSetImpl<Type * > * Visited) const140 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
141   if (auto *ATy = dyn_cast<ArrayType>(this))
142     return ATy->getElementType()->isSized(Visited);
143 
144   if (auto *VTy = dyn_cast<VectorType>(this))
145     return VTy->getElementType()->isSized(Visited);
146 
147   return cast<StructType>(this)->isSized(Visited);
148 }
149 
150 //===----------------------------------------------------------------------===//
151 //                          Primitive 'Type' data
152 //===----------------------------------------------------------------------===//
153 
getVoidTy(LLVMContext & C)154 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
getLabelTy(LLVMContext & C)155 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
getHalfTy(LLVMContext & C)156 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
getFloatTy(LLVMContext & C)157 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
getDoubleTy(LLVMContext & C)158 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
getMetadataTy(LLVMContext & C)159 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
getTokenTy(LLVMContext & C)160 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
getX86_FP80Ty(LLVMContext & C)161 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
getFP128Ty(LLVMContext & C)162 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
getPPC_FP128Ty(LLVMContext & C)163 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
getX86_MMXTy(LLVMContext & C)164 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
165 
getInt1Ty(LLVMContext & C)166 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
getInt8Ty(LLVMContext & C)167 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
getInt16Ty(LLVMContext & C)168 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
getInt32Ty(LLVMContext & C)169 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
getInt64Ty(LLVMContext & C)170 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
getInt128Ty(LLVMContext & C)171 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
172 
getIntNTy(LLVMContext & C,unsigned N)173 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
174   return IntegerType::get(C, N);
175 }
176 
getHalfPtrTy(LLVMContext & C,unsigned AS)177 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
178   return getHalfTy(C)->getPointerTo(AS);
179 }
180 
getFloatPtrTy(LLVMContext & C,unsigned AS)181 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
182   return getFloatTy(C)->getPointerTo(AS);
183 }
184 
getDoublePtrTy(LLVMContext & C,unsigned AS)185 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
186   return getDoubleTy(C)->getPointerTo(AS);
187 }
188 
getX86_FP80PtrTy(LLVMContext & C,unsigned AS)189 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
190   return getX86_FP80Ty(C)->getPointerTo(AS);
191 }
192 
getFP128PtrTy(LLVMContext & C,unsigned AS)193 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
194   return getFP128Ty(C)->getPointerTo(AS);
195 }
196 
getPPC_FP128PtrTy(LLVMContext & C,unsigned AS)197 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
198   return getPPC_FP128Ty(C)->getPointerTo(AS);
199 }
200 
getX86_MMXPtrTy(LLVMContext & C,unsigned AS)201 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
202   return getX86_MMXTy(C)->getPointerTo(AS);
203 }
204 
getIntNPtrTy(LLVMContext & C,unsigned N,unsigned AS)205 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
206   return getIntNTy(C, N)->getPointerTo(AS);
207 }
208 
getInt1PtrTy(LLVMContext & C,unsigned AS)209 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
210   return getInt1Ty(C)->getPointerTo(AS);
211 }
212 
getInt8PtrTy(LLVMContext & C,unsigned AS)213 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
214   return getInt8Ty(C)->getPointerTo(AS);
215 }
216 
getInt16PtrTy(LLVMContext & C,unsigned AS)217 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
218   return getInt16Ty(C)->getPointerTo(AS);
219 }
220 
getInt32PtrTy(LLVMContext & C,unsigned AS)221 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
222   return getInt32Ty(C)->getPointerTo(AS);
223 }
224 
getInt64PtrTy(LLVMContext & C,unsigned AS)225 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
226   return getInt64Ty(C)->getPointerTo(AS);
227 }
228 
229 
230 //===----------------------------------------------------------------------===//
231 //                       IntegerType Implementation
232 //===----------------------------------------------------------------------===//
233 
get(LLVMContext & C,unsigned NumBits)234 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
235   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
236   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
237 
238   // Check for the built-in integer types
239   switch (NumBits) {
240   case   1: return cast<IntegerType>(Type::getInt1Ty(C));
241   case   8: return cast<IntegerType>(Type::getInt8Ty(C));
242   case  16: return cast<IntegerType>(Type::getInt16Ty(C));
243   case  32: return cast<IntegerType>(Type::getInt32Ty(C));
244   case  64: return cast<IntegerType>(Type::getInt64Ty(C));
245   case 128: return cast<IntegerType>(Type::getInt128Ty(C));
246   default:
247     break;
248   }
249 
250   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
251 
252   if (!Entry)
253     Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
254 
255   return Entry;
256 }
257 
isPowerOf2ByteWidth() const258 bool IntegerType::isPowerOf2ByteWidth() const {
259   unsigned BitWidth = getBitWidth();
260   return (BitWidth > 7) && isPowerOf2_32(BitWidth);
261 }
262 
getMask() const263 APInt IntegerType::getMask() const {
264   return APInt::getAllOnesValue(getBitWidth());
265 }
266 
267 //===----------------------------------------------------------------------===//
268 //                       FunctionType Implementation
269 //===----------------------------------------------------------------------===//
270 
FunctionType(Type * Result,ArrayRef<Type * > Params,bool IsVarArgs)271 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
272                            bool IsVarArgs)
273   : Type(Result->getContext(), FunctionTyID) {
274   Type **SubTys = reinterpret_cast<Type**>(this+1);
275   assert(isValidReturnType(Result) && "invalid return type for function");
276   setSubclassData(IsVarArgs);
277 
278   SubTys[0] = Result;
279 
280   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
281     assert(isValidArgumentType(Params[i]) &&
282            "Not a valid type for function argument!");
283     SubTys[i+1] = Params[i];
284   }
285 
286   ContainedTys = SubTys;
287   NumContainedTys = Params.size() + 1; // + 1 for result type
288 }
289 
290 // This is the factory function for the FunctionType class.
get(Type * ReturnType,ArrayRef<Type * > Params,bool isVarArg)291 FunctionType *FunctionType::get(Type *ReturnType,
292                                 ArrayRef<Type*> Params, bool isVarArg) {
293   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
294   FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
295   auto I = pImpl->FunctionTypes.find_as(Key);
296   FunctionType *FT;
297 
298   if (I == pImpl->FunctionTypes.end()) {
299     FT = (FunctionType*) pImpl->TypeAllocator.
300       Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
301                AlignOf<FunctionType>::Alignment);
302     new (FT) FunctionType(ReturnType, Params, isVarArg);
303     pImpl->FunctionTypes.insert(FT);
304   } else {
305     FT = *I;
306   }
307 
308   return FT;
309 }
310 
get(Type * Result,bool isVarArg)311 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
312   return get(Result, None, isVarArg);
313 }
314 
isValidReturnType(Type * RetTy)315 bool FunctionType::isValidReturnType(Type *RetTy) {
316   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
317   !RetTy->isMetadataTy();
318 }
319 
isValidArgumentType(Type * ArgTy)320 bool FunctionType::isValidArgumentType(Type *ArgTy) {
321   return ArgTy->isFirstClassType();
322 }
323 
324 //===----------------------------------------------------------------------===//
325 //                       StructType Implementation
326 //===----------------------------------------------------------------------===//
327 
328 // Primitive Constructors.
329 
get(LLVMContext & Context,ArrayRef<Type * > ETypes,bool isPacked)330 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
331                             bool isPacked) {
332   LLVMContextImpl *pImpl = Context.pImpl;
333   AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
334   auto I = pImpl->AnonStructTypes.find_as(Key);
335   StructType *ST;
336 
337   if (I == pImpl->AnonStructTypes.end()) {
338     // Value not found.  Create a new type!
339     ST = new (Context.pImpl->TypeAllocator) StructType(Context);
340     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
341     ST->setBody(ETypes, isPacked);
342     Context.pImpl->AnonStructTypes.insert(ST);
343   } else {
344     ST = *I;
345   }
346 
347   return ST;
348 }
349 
setBody(ArrayRef<Type * > Elements,bool isPacked)350 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
351   assert(isOpaque() && "Struct body already set!");
352 
353   setSubclassData(getSubclassData() | SCDB_HasBody);
354   if (isPacked)
355     setSubclassData(getSubclassData() | SCDB_Packed);
356 
357   NumContainedTys = Elements.size();
358 
359   if (Elements.empty()) {
360     ContainedTys = nullptr;
361     return;
362   }
363 
364   ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data();
365 }
366 
setName(StringRef Name)367 void StructType::setName(StringRef Name) {
368   if (Name == getName()) return;
369 
370   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
371   typedef StringMap<StructType *>::MapEntryTy EntryTy;
372 
373   // If this struct already had a name, remove its symbol table entry. Don't
374   // delete the data yet because it may be part of the new name.
375   if (SymbolTableEntry)
376     SymbolTable.remove((EntryTy *)SymbolTableEntry);
377 
378   // If this is just removing the name, we're done.
379   if (Name.empty()) {
380     if (SymbolTableEntry) {
381       // Delete the old string data.
382       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
383       SymbolTableEntry = nullptr;
384     }
385     return;
386   }
387 
388   // Look up the entry for the name.
389   auto IterBool =
390       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
391 
392   // While we have a name collision, try a random rename.
393   if (!IterBool.second) {
394     SmallString<64> TempStr(Name);
395     TempStr.push_back('.');
396     raw_svector_ostream TmpStream(TempStr);
397     unsigned NameSize = Name.size();
398 
399     do {
400       TempStr.resize(NameSize + 1);
401       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
402 
403       IterBool = getContext().pImpl->NamedStructTypes.insert(
404           std::make_pair(TmpStream.str(), this));
405     } while (!IterBool.second);
406   }
407 
408   // Delete the old string data.
409   if (SymbolTableEntry)
410     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
411   SymbolTableEntry = &*IterBool.first;
412 }
413 
414 //===----------------------------------------------------------------------===//
415 // StructType Helper functions.
416 
create(LLVMContext & Context,StringRef Name)417 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
418   StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
419   if (!Name.empty())
420     ST->setName(Name);
421   return ST;
422 }
423 
get(LLVMContext & Context,bool isPacked)424 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
425   return get(Context, None, isPacked);
426 }
427 
get(Type * type,...)428 StructType *StructType::get(Type *type, ...) {
429   assert(type && "Cannot create a struct type with no elements with this");
430   LLVMContext &Ctx = type->getContext();
431   va_list ap;
432   SmallVector<llvm::Type*, 8> StructFields;
433   va_start(ap, type);
434   while (type) {
435     StructFields.push_back(type);
436     type = va_arg(ap, llvm::Type*);
437   }
438   auto *Ret = llvm::StructType::get(Ctx, StructFields);
439   va_end(ap);
440   return Ret;
441 }
442 
create(LLVMContext & Context,ArrayRef<Type * > Elements,StringRef Name,bool isPacked)443 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
444                                StringRef Name, bool isPacked) {
445   StructType *ST = create(Context, Name);
446   ST->setBody(Elements, isPacked);
447   return ST;
448 }
449 
create(LLVMContext & Context,ArrayRef<Type * > Elements)450 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
451   return create(Context, Elements, StringRef());
452 }
453 
create(LLVMContext & Context)454 StructType *StructType::create(LLVMContext &Context) {
455   return create(Context, StringRef());
456 }
457 
create(ArrayRef<Type * > Elements,StringRef Name,bool isPacked)458 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
459                                bool isPacked) {
460   assert(!Elements.empty() &&
461          "This method may not be invoked with an empty list");
462   return create(Elements[0]->getContext(), Elements, Name, isPacked);
463 }
464 
create(ArrayRef<Type * > Elements)465 StructType *StructType::create(ArrayRef<Type*> Elements) {
466   assert(!Elements.empty() &&
467          "This method may not be invoked with an empty list");
468   return create(Elements[0]->getContext(), Elements, StringRef());
469 }
470 
create(StringRef Name,Type * type,...)471 StructType *StructType::create(StringRef Name, Type *type, ...) {
472   assert(type && "Cannot create a struct type with no elements with this");
473   LLVMContext &Ctx = type->getContext();
474   va_list ap;
475   SmallVector<llvm::Type*, 8> StructFields;
476   va_start(ap, type);
477   while (type) {
478     StructFields.push_back(type);
479     type = va_arg(ap, llvm::Type*);
480   }
481   auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
482   va_end(ap);
483   return Ret;
484 }
485 
isSized(SmallPtrSetImpl<Type * > * Visited) const486 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
487   if ((getSubclassData() & SCDB_IsSized) != 0)
488     return true;
489   if (isOpaque())
490     return false;
491 
492   if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
493     return false;
494 
495   // Okay, our struct is sized if all of the elements are, but if one of the
496   // elements is opaque, the struct isn't sized *yet*, but may become sized in
497   // the future, so just bail out without caching.
498   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
499     if (!(*I)->isSized(Visited))
500       return false;
501 
502   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
503   // we find a sized type, as types can only move from opaque to sized, not the
504   // other way.
505   const_cast<StructType*>(this)->setSubclassData(
506     getSubclassData() | SCDB_IsSized);
507   return true;
508 }
509 
getName() const510 StringRef StructType::getName() const {
511   assert(!isLiteral() && "Literal structs never have names");
512   if (!SymbolTableEntry) return StringRef();
513 
514   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
515 }
516 
setBody(Type * type,...)517 void StructType::setBody(Type *type, ...) {
518   assert(type && "Cannot create a struct type with no elements with this");
519   va_list ap;
520   SmallVector<llvm::Type*, 8> StructFields;
521   va_start(ap, type);
522   while (type) {
523     StructFields.push_back(type);
524     type = va_arg(ap, llvm::Type*);
525   }
526   setBody(StructFields);
527   va_end(ap);
528 }
529 
isValidElementType(Type * ElemTy)530 bool StructType::isValidElementType(Type *ElemTy) {
531   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
532          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
533          !ElemTy->isTokenTy();
534 }
535 
isLayoutIdentical(StructType * Other) const536 bool StructType::isLayoutIdentical(StructType *Other) const {
537   if (this == Other) return true;
538 
539   if (isPacked() != Other->isPacked())
540     return false;
541 
542   return elements() == Other->elements();
543 }
544 
getTypeByName(StringRef Name) const545 StructType *Module::getTypeByName(StringRef Name) const {
546   return getContext().pImpl->NamedStructTypes.lookup(Name);
547 }
548 
549 
550 //===----------------------------------------------------------------------===//
551 //                       CompositeType Implementation
552 //===----------------------------------------------------------------------===//
553 
getTypeAtIndex(const Value * V) const554 Type *CompositeType::getTypeAtIndex(const Value *V) const {
555   if (auto *STy = dyn_cast<StructType>(this)) {
556     unsigned Idx =
557       (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
558     assert(indexValid(Idx) && "Invalid structure index!");
559     return STy->getElementType(Idx);
560   }
561 
562   return cast<SequentialType>(this)->getElementType();
563 }
564 
getTypeAtIndex(unsigned Idx) const565 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
566   if (auto *STy = dyn_cast<StructType>(this)) {
567     assert(indexValid(Idx) && "Invalid structure index!");
568     return STy->getElementType(Idx);
569   }
570 
571   return cast<SequentialType>(this)->getElementType();
572 }
573 
indexValid(const Value * V) const574 bool CompositeType::indexValid(const Value *V) const {
575   if (auto *STy = dyn_cast<StructType>(this)) {
576     // Structure indexes require (vectors of) 32-bit integer constants.  In the
577     // vector case all of the indices must be equal.
578     if (!V->getType()->getScalarType()->isIntegerTy(32))
579       return false;
580     const Constant *C = dyn_cast<Constant>(V);
581     if (C && V->getType()->isVectorTy())
582       C = C->getSplatValue();
583     const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
584     return CU && CU->getZExtValue() < STy->getNumElements();
585   }
586 
587   // Sequential types can be indexed by any integer.
588   return V->getType()->isIntOrIntVectorTy();
589 }
590 
indexValid(unsigned Idx) const591 bool CompositeType::indexValid(unsigned Idx) const {
592   if (auto *STy = dyn_cast<StructType>(this))
593     return Idx < STy->getNumElements();
594   // Sequential types can be indexed by any integer.
595   return true;
596 }
597 
598 
599 //===----------------------------------------------------------------------===//
600 //                           ArrayType Implementation
601 //===----------------------------------------------------------------------===//
602 
ArrayType(Type * ElType,uint64_t NumEl)603 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
604   : SequentialType(ArrayTyID, ElType) {
605   NumElements = NumEl;
606 }
607 
get(Type * ElementType,uint64_t NumElements)608 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
609   assert(isValidElementType(ElementType) && "Invalid type for array element!");
610 
611   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
612   ArrayType *&Entry =
613     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
614 
615   if (!Entry)
616     Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
617   return Entry;
618 }
619 
isValidElementType(Type * ElemTy)620 bool ArrayType::isValidElementType(Type *ElemTy) {
621   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
622          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
623          !ElemTy->isTokenTy();
624 }
625 
626 //===----------------------------------------------------------------------===//
627 //                          VectorType Implementation
628 //===----------------------------------------------------------------------===//
629 
VectorType(Type * ElType,unsigned NumEl)630 VectorType::VectorType(Type *ElType, unsigned NumEl)
631   : SequentialType(VectorTyID, ElType) {
632   NumElements = NumEl;
633 }
634 
get(Type * ElementType,unsigned NumElements)635 VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
636   assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
637   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
638                                             "be an integer, floating point, or "
639                                             "pointer type.");
640 
641   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
642   VectorType *&Entry = ElementType->getContext().pImpl
643     ->VectorTypes[std::make_pair(ElementType, NumElements)];
644 
645   if (!Entry)
646     Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
647   return Entry;
648 }
649 
isValidElementType(Type * ElemTy)650 bool VectorType::isValidElementType(Type *ElemTy) {
651   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
652     ElemTy->isPointerTy();
653 }
654 
655 //===----------------------------------------------------------------------===//
656 //                         PointerType Implementation
657 //===----------------------------------------------------------------------===//
658 
get(Type * EltTy,unsigned AddressSpace)659 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
660   assert(EltTy && "Can't get a pointer to <null> type!");
661   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
662 
663   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
664 
665   // Since AddressSpace #0 is the common case, we special case it.
666   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
667      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
668 
669   if (!Entry)
670     Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
671   return Entry;
672 }
673 
674 
PointerType(Type * E,unsigned AddrSpace)675 PointerType::PointerType(Type *E, unsigned AddrSpace)
676   : SequentialType(PointerTyID, E) {
677 #ifndef NDEBUG
678   const unsigned oldNCT = NumContainedTys;
679 #endif
680   setSubclassData(AddrSpace);
681   // Check for miscompile. PR11652.
682   assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
683 }
684 
getPointerTo(unsigned addrs) const685 PointerType *Type::getPointerTo(unsigned addrs) const {
686   return PointerType::get(const_cast<Type*>(this), addrs);
687 }
688 
isValidElementType(Type * ElemTy)689 bool PointerType::isValidElementType(Type *ElemTy) {
690   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
691          !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
692 }
693 
isLoadableOrStorableType(Type * ElemTy)694 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
695   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
696 }
697