LLVM  3.7.0
Type.cpp
Go to the documentation of this file.
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 
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  default:
39  return nullptr;
40  }
41 }
42 
43 /// getScalarType - If this is a vector type, return the element type,
44 /// otherwise return this.
46  if (VectorType *VTy = dyn_cast<VectorType>(this))
47  return VTy->getElementType();
48  return this;
49 }
50 
51 const Type *Type::getScalarType() const {
52  if (const VectorType *VTy = dyn_cast<VectorType>(this))
53  return VTy->getElementType();
54  return this;
55 }
56 
57 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
58 bool Type::isIntegerTy(unsigned Bitwidth) const {
59  return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
60 }
61 
62 // canLosslesslyBitCastTo - Return true if this type can be converted to
63 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*.
64 //
66  // Identity cast means no change so return true
67  if (this == Ty)
68  return true;
69 
70  // They are not convertible unless they are at least first class types
71  if (!this->isFirstClassType() || !Ty->isFirstClassType())
72  return false;
73 
74  // Vector -> Vector conversions are always lossless if the two vector types
75  // have the same size, otherwise not. Also, 64-bit vector types can be
76  // converted to x86mmx.
77  if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
78  if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
79  return thisPTy->getBitWidth() == thatPTy->getBitWidth();
80  if (Ty->getTypeID() == Type::X86_MMXTyID &&
81  thisPTy->getBitWidth() == 64)
82  return true;
83  }
84 
85  if (this->getTypeID() == Type::X86_MMXTyID)
86  if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
87  if (thatPTy->getBitWidth() == 64)
88  return true;
89 
90  // At this point we have only various mismatches of the first class types
91  // remaining and ptr->ptr. Just select the lossless conversions. Everything
92  // else is not lossless. Conservatively assume we can't losslessly convert
93  // between pointers with different address spaces.
94  if (const PointerType *PTy = dyn_cast<PointerType>(this)) {
95  if (const PointerType *OtherPTy = dyn_cast<PointerType>(Ty))
96  return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
97  return false;
98  }
99  return false; // Other types have no identity values
100 }
101 
102 bool Type::isEmptyTy() const {
103  const ArrayType *ATy = dyn_cast<ArrayType>(this);
104  if (ATy) {
105  unsigned NumElements = ATy->getNumElements();
106  return NumElements == 0 || ATy->getElementType()->isEmptyTy();
107  }
108 
109  const StructType *STy = dyn_cast<StructType>(this);
110  if (STy) {
111  unsigned NumElements = STy->getNumElements();
112  for (unsigned i = 0; i < NumElements; ++i)
113  if (!STy->getElementType(i)->isEmptyTy())
114  return false;
115  return true;
116  }
117 
118  return false;
119 }
120 
122  switch (getTypeID()) {
123  case Type::HalfTyID: return 16;
124  case Type::FloatTyID: return 32;
125  case Type::DoubleTyID: return 64;
126  case Type::X86_FP80TyID: return 80;
127  case Type::FP128TyID: return 128;
128  case Type::PPC_FP128TyID: return 128;
129  case Type::X86_MMXTyID: return 64;
130  case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
131  case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
132  default: return 0;
133  }
134 }
135 
136 /// getScalarSizeInBits - If this is a vector type, return the
137 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
138 /// getPrimitiveSizeInBits value for this type.
139 unsigned Type::getScalarSizeInBits() const {
141 }
142 
143 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
144 /// is only valid on floating point types. If the FP type does not
145 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
147  if (const VectorType *VTy = dyn_cast<VectorType>(this))
148  return VTy->getElementType()->getFPMantissaWidth();
149  assert(isFloatingPointTy() && "Not a floating point type!");
150  if (getTypeID() == HalfTyID) return 11;
151  if (getTypeID() == FloatTyID) return 24;
152  if (getTypeID() == DoubleTyID) return 53;
153  if (getTypeID() == X86_FP80TyID) return 64;
154  if (getTypeID() == FP128TyID) return 113;
155  assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
156  return -1;
157 }
158 
159 /// isSizedDerivedType - Derived types like structures and arrays are sized
160 /// iff all of the members of the type are sized as well. Since asking for
161 /// their size is relatively uncommon, move this operation out of line.
162 bool Type::isSizedDerivedType(SmallPtrSetImpl<const Type*> *Visited) const {
163  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
164  return ATy->getElementType()->isSized(Visited);
165 
166  if (const VectorType *VTy = dyn_cast<VectorType>(this))
167  return VTy->getElementType()->isSized(Visited);
168 
169  return cast<StructType>(this)->isSized(Visited);
170 }
171 
172 //===----------------------------------------------------------------------===//
173 // Subclass Helper Methods
174 //===----------------------------------------------------------------------===//
175 
176 unsigned Type::getIntegerBitWidth() const {
177  return cast<IntegerType>(this)->getBitWidth();
178 }
179 
181  return cast<FunctionType>(this)->isVarArg();
182 }
183 
184 Type *Type::getFunctionParamType(unsigned i) const {
185  return cast<FunctionType>(this)->getParamType(i);
186 }
187 
188 unsigned Type::getFunctionNumParams() const {
189  return cast<FunctionType>(this)->getNumParams();
190 }
191 
193  return cast<StructType>(this)->getName();
194 }
195 
196 unsigned Type::getStructNumElements() const {
197  return cast<StructType>(this)->getNumElements();
198 }
199 
201  return cast<StructType>(this)->getElementType(N);
202 }
203 
205  return cast<SequentialType>(this)->getElementType();
206 }
207 
208 uint64_t Type::getArrayNumElements() const {
209  return cast<ArrayType>(this)->getNumElements();
210 }
211 
212 unsigned Type::getVectorNumElements() const {
213  return cast<VectorType>(this)->getNumElements();
214 }
215 
217  return cast<PointerType>(getScalarType())->getAddressSpace();
218 }
219 
220 
221 //===----------------------------------------------------------------------===//
222 // Primitive 'Type' data
223 //===----------------------------------------------------------------------===//
224 
235 
242 
244  return IntegerType::get(C, N);
245 }
246 
248  return getHalfTy(C)->getPointerTo(AS);
249 }
250 
252  return getFloatTy(C)->getPointerTo(AS);
253 }
254 
256  return getDoubleTy(C)->getPointerTo(AS);
257 }
258 
260  return getX86_FP80Ty(C)->getPointerTo(AS);
261 }
262 
264  return getFP128Ty(C)->getPointerTo(AS);
265 }
266 
268  return getPPC_FP128Ty(C)->getPointerTo(AS);
269 }
270 
272  return getX86_MMXTy(C)->getPointerTo(AS);
273 }
274 
275 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
276  return getIntNTy(C, N)->getPointerTo(AS);
277 }
278 
280  return getInt1Ty(C)->getPointerTo(AS);
281 }
282 
284  return getInt8Ty(C)->getPointerTo(AS);
285 }
286 
288  return getInt16Ty(C)->getPointerTo(AS);
289 }
290 
292  return getInt32Ty(C)->getPointerTo(AS);
293 }
294 
296  return getInt64Ty(C)->getPointerTo(AS);
297 }
298 
299 
300 //===----------------------------------------------------------------------===//
301 // IntegerType Implementation
302 //===----------------------------------------------------------------------===//
303 
304 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
305  assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
306  assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
307 
308  // Check for the built-in integer types
309  switch (NumBits) {
310  case 1: return cast<IntegerType>(Type::getInt1Ty(C));
311  case 8: return cast<IntegerType>(Type::getInt8Ty(C));
312  case 16: return cast<IntegerType>(Type::getInt16Ty(C));
313  case 32: return cast<IntegerType>(Type::getInt32Ty(C));
314  case 64: return cast<IntegerType>(Type::getInt64Ty(C));
315  case 128: return cast<IntegerType>(Type::getInt128Ty(C));
316  default:
317  break;
318  }
319 
320  IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
321 
322  if (!Entry)
323  Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
324 
325  return Entry;
326 }
327 
329  unsigned BitWidth = getBitWidth();
330  return (BitWidth > 7) && isPowerOf2_32(BitWidth);
331 }
332 
335 }
336 
337 //===----------------------------------------------------------------------===//
338 // FunctionType Implementation
339 //===----------------------------------------------------------------------===//
340 
341 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
342  bool IsVarArgs)
343  : Type(Result->getContext(), FunctionTyID) {
344  Type **SubTys = reinterpret_cast<Type**>(this+1);
345  assert(isValidReturnType(Result) && "invalid return type for function");
346  setSubclassData(IsVarArgs);
347 
348  SubTys[0] = const_cast<Type*>(Result);
349 
350  for (unsigned i = 0, e = Params.size(); i != e; ++i) {
351  assert(isValidArgumentType(Params[i]) &&
352  "Not a valid type for function argument!");
353  SubTys[i+1] = Params[i];
354  }
355 
356  ContainedTys = SubTys;
357  NumContainedTys = Params.size() + 1; // + 1 for result type
358 }
359 
360 // FunctionType::get - The factory function for the FunctionType class.
362  ArrayRef<Type*> Params, bool isVarArg) {
363  LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
364  FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
365  auto I = pImpl->FunctionTypes.find_as(Key);
366  FunctionType *FT;
367 
368  if (I == pImpl->FunctionTypes.end()) {
369  FT = (FunctionType*) pImpl->TypeAllocator.
370  Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
372  new (FT) FunctionType(ReturnType, Params, isVarArg);
373  pImpl->FunctionTypes.insert(FT);
374  } else {
375  FT = *I;
376  }
377 
378  return FT;
379 }
380 
381 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
382  return get(Result, None, isVarArg);
383 }
384 
385 /// isValidReturnType - Return true if the specified type is valid as a return
386 /// type.
388  return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
389  !RetTy->isMetadataTy();
390 }
391 
392 /// isValidArgumentType - Return true if the specified type is valid as an
393 /// argument type.
395  return ArgTy->isFirstClassType();
396 }
397 
398 //===----------------------------------------------------------------------===//
399 // StructType Implementation
400 //===----------------------------------------------------------------------===//
401 
402 // Primitive Constructors.
403 
405  bool isPacked) {
406  LLVMContextImpl *pImpl = Context.pImpl;
407  AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
408  auto I = pImpl->AnonStructTypes.find_as(Key);
409  StructType *ST;
410 
411  if (I == pImpl->AnonStructTypes.end()) {
412  // Value not found. Create a new type!
413  ST = new (Context.pImpl->TypeAllocator) StructType(Context);
414  ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
415  ST->setBody(ETypes, isPacked);
416  Context.pImpl->AnonStructTypes.insert(ST);
417  } else {
418  ST = *I;
419  }
420 
421  return ST;
422 }
423 
424 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
425  assert(isOpaque() && "Struct body already set!");
426 
427  setSubclassData(getSubclassData() | SCDB_HasBody);
428  if (isPacked)
429  setSubclassData(getSubclassData() | SCDB_Packed);
430 
431  unsigned NumElements = Elements.size();
432  Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
433  memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
434 
435  ContainedTys = Elts;
436  NumContainedTys = NumElements;
437 }
438 
440  if (Name == getName()) return;
441 
443  typedef StringMap<StructType *>::MapEntryTy EntryTy;
444 
445  // If this struct already had a name, remove its symbol table entry. Don't
446  // delete the data yet because it may be part of the new name.
447  if (SymbolTableEntry)
448  SymbolTable.remove((EntryTy *)SymbolTableEntry);
449 
450  // If this is just removing the name, we're done.
451  if (Name.empty()) {
452  if (SymbolTableEntry) {
453  // Delete the old string data.
454  ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
455  SymbolTableEntry = nullptr;
456  }
457  return;
458  }
459 
460  // Look up the entry for the name.
461  auto IterBool =
462  getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
463 
464  // While we have a name collision, try a random rename.
465  if (!IterBool.second) {
466  SmallString<64> TempStr(Name);
467  TempStr.push_back('.');
468  raw_svector_ostream TmpStream(TempStr);
469  unsigned NameSize = Name.size();
470 
471  do {
472  TempStr.resize(NameSize + 1);
473  TmpStream.resync();
474  TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
475 
476  IterBool = getContext().pImpl->NamedStructTypes.insert(
477  std::make_pair(TmpStream.str(), this));
478  } while (!IterBool.second);
479  }
480 
481  // Delete the old string data.
482  if (SymbolTableEntry)
483  ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
484  SymbolTableEntry = &*IterBool.first;
485 }
486 
487 //===----------------------------------------------------------------------===//
488 // StructType Helper functions.
489 
491  StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
492  if (!Name.empty())
493  ST->setName(Name);
494  return ST;
495 }
496 
497 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
498  return get(Context, None, isPacked);
499 }
500 
502  assert(type && "Cannot create a struct type with no elements with this");
503  LLVMContext &Ctx = type->getContext();
504  va_list ap;
505  SmallVector<llvm::Type*, 8> StructFields;
506  va_start(ap, type);
507  while (type) {
508  StructFields.push_back(type);
509  type = va_arg(ap, llvm::Type*);
510  }
511  auto *Ret = llvm::StructType::get(Ctx, StructFields);
512  va_end(ap);
513  return Ret;
514 }
515 
517  StringRef Name, bool isPacked) {
518  StructType *ST = create(Context, Name);
519  ST->setBody(Elements, isPacked);
520  return ST;
521 }
522 
524  return create(Context, Elements, StringRef());
525 }
526 
528  return create(Context, StringRef());
529 }
530 
532  bool isPacked) {
533  assert(!Elements.empty() &&
534  "This method may not be invoked with an empty list");
535  return create(Elements[0]->getContext(), Elements, Name, isPacked);
536 }
537 
539  assert(!Elements.empty() &&
540  "This method may not be invoked with an empty list");
541  return create(Elements[0]->getContext(), Elements, StringRef());
542 }
543 
545  assert(type && "Cannot create a struct type with no elements with this");
546  LLVMContext &Ctx = type->getContext();
547  va_list ap;
548  SmallVector<llvm::Type*, 8> StructFields;
549  va_start(ap, type);
550  while (type) {
551  StructFields.push_back(type);
552  type = va_arg(ap, llvm::Type*);
553  }
554  auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
555  va_end(ap);
556  return Ret;
557 }
558 
560  if ((getSubclassData() & SCDB_IsSized) != 0)
561  return true;
562  if (isOpaque())
563  return false;
564 
565  if (Visited && !Visited->insert(this).second)
566  return false;
567 
568  // Okay, our struct is sized if all of the elements are, but if one of the
569  // elements is opaque, the struct isn't sized *yet*, but may become sized in
570  // the future, so just bail out without caching.
571  for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
572  if (!(*I)->isSized(Visited))
573  return false;
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 
584  assert(!isLiteral() && "Literal structs never have names");
585  if (!SymbolTableEntry) return StringRef();
586 
587  return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
588 }
589 
590 void StructType::setBody(Type *type, ...) {
591  assert(type && "Cannot create a struct type with no elements with this");
592  va_list ap;
593  SmallVector<llvm::Type*, 8> StructFields;
594  va_start(ap, type);
595  while (type) {
596  StructFields.push_back(type);
597  type = va_arg(ap, llvm::Type*);
598  }
599  setBody(StructFields);
600  va_end(ap);
601 }
602 
604  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
605  !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
606 }
607 
608 /// isLayoutIdentical - Return true if this is layout identical to the
609 /// specified struct.
611  if (this == Other) return true;
612 
613  if (isPacked() != Other->isPacked() ||
614  getNumElements() != Other->getNumElements())
615  return false;
616 
617  if (!getNumElements())
618  return true;
619 
620  return std::equal(element_begin(), element_end(), Other->element_begin());
621 }
622 
623 /// getTypeByName - Return the type with the specified name, or null if there
624 /// is none by that name.
626  return getContext().pImpl->NamedStructTypes.lookup(Name);
627 }
628 
629 
630 //===----------------------------------------------------------------------===//
631 // CompositeType Implementation
632 //===----------------------------------------------------------------------===//
633 
635  if (StructType *STy = dyn_cast<StructType>(this)) {
636  unsigned Idx =
637  (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
638  assert(indexValid(Idx) && "Invalid structure index!");
639  return STy->getElementType(Idx);
640  }
641 
642  return cast<SequentialType>(this)->getElementType();
643 }
645  if (StructType *STy = dyn_cast<StructType>(this)) {
646  assert(indexValid(Idx) && "Invalid structure index!");
647  return STy->getElementType(Idx);
648  }
649 
650  return cast<SequentialType>(this)->getElementType();
651 }
652 bool CompositeType::indexValid(const Value *V) const {
653  if (const StructType *STy = dyn_cast<StructType>(this)) {
654  // Structure indexes require (vectors of) 32-bit integer constants. In the
655  // vector case all of the indices must be equal.
656  if (!V->getType()->getScalarType()->isIntegerTy(32))
657  return false;
658  const Constant *C = dyn_cast<Constant>(V);
659  if (C && V->getType()->isVectorTy())
660  C = C->getSplatValue();
661  const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
662  return CU && CU->getZExtValue() < STy->getNumElements();
663  }
664 
665  // Sequential types can be indexed by any integer.
666  return V->getType()->isIntOrIntVectorTy();
667 }
668 
669 bool CompositeType::indexValid(unsigned Idx) const {
670  if (const StructType *STy = dyn_cast<StructType>(this))
671  return Idx < STy->getNumElements();
672  // Sequential types can be indexed by any integer.
673  return true;
674 }
675 
676 
677 //===----------------------------------------------------------------------===//
678 // ArrayType Implementation
679 //===----------------------------------------------------------------------===//
680 
681 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
682  : SequentialType(ArrayTyID, ElType) {
683  NumElements = NumEl;
684 }
685 
686 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
687  Type *ElementType = const_cast<Type*>(elementType);
688  assert(isValidElementType(ElementType) && "Invalid type for array element!");
689 
690  LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
691  ArrayType *&Entry =
692  pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
693 
694  if (!Entry)
695  Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
696  return Entry;
697 }
698 
700  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
701  !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
702 }
703 
704 //===----------------------------------------------------------------------===//
705 // VectorType Implementation
706 //===----------------------------------------------------------------------===//
707 
708 VectorType::VectorType(Type *ElType, unsigned NumEl)
709  : SequentialType(VectorTyID, ElType) {
710  NumElements = NumEl;
711 }
712 
713 VectorType *VectorType::get(Type *elementType, unsigned NumElements) {
714  Type *ElementType = const_cast<Type*>(elementType);
715  assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
716  assert(isValidElementType(ElementType) && "Element type of a VectorType must "
717  "be an integer, floating point, or "
718  "pointer type.");
719 
720  LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
721  VectorType *&Entry = ElementType->getContext().pImpl
722  ->VectorTypes[std::make_pair(ElementType, NumElements)];
723 
724  if (!Entry)
725  Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
726  return Entry;
727 }
728 
730  return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
731  ElemTy->isPointerTy();
732 }
733 
734 //===----------------------------------------------------------------------===//
735 // PointerType Implementation
736 //===----------------------------------------------------------------------===//
737 
739  assert(EltTy && "Can't get a pointer to <null> type!");
740  assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
741 
742  LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
743 
744  // Since AddressSpace #0 is the common case, we special case it.
745  PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
746  : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
747 
748  if (!Entry)
749  Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
750  return Entry;
751 }
752 
753 
754 PointerType::PointerType(Type *E, unsigned AddrSpace)
755  : SequentialType(PointerTyID, E) {
756 #ifndef NDEBUG
757  const unsigned oldNCT = NumContainedTys;
758 #endif
759  setSubclassData(AddrSpace);
760  // Check for miscompile. PR11652.
761  assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
762 }
763 
764 PointerType *Type::getPointerTo(unsigned addrs) {
765  return PointerType::get(this, addrs);
766 }
767 
769  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
770  !ElemTy->isMetadataTy();
771 }
772 
774  return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
775 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
DenseMap< unsigned, IntegerType * > IntegerTypes
void push_back(const T &Elt)
Definition: SmallVector.h:222
7: Labels
Definition: Type.h:63
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:229
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
unsigned getStructNumElements() const
Definition: Type.cpp:196
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:453
Type * getSequentialElementType() const
Definition: Type.cpp:204
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
AlignOf - A templated class that contains an enum value representing the alignment of the template ar...
Definition: AlignOf.h:46
bool isOpaque() const
isOpaque - Return true if this is a type with an identity that has no body specified yet...
Definition: DerivedTypes.h:250
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:28
APInt getMask() const
For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
Definition: Type.cpp:333
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:356
2: 32-bit floating point type
Definition: Type.h:58
Constant * getSplatValue() const
getSplatValue - If this is a splat vector constant, meaning that all of the elements have the same va...
Definition: Constants.cpp:1418
static PointerType * getInt32PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:291
bool indexValid(const Value *V) const
Definition: Type.cpp:652
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
Definition: Type.cpp:738
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if this is a sized type.
Definition: Type.cpp:559
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
int getFPMantissaWidth() const
getFPMantissaWidth - Return the width of the mantissa of this type.
Definition: Type.cpp:146
4: 80-bit floating point type (X87)
Definition: Type.h:60
static bool isValidReturnType(Type *RetTy)
isValidReturnType - Return true if the specified type is valid as a return type.
Definition: Type.cpp:387
static bool isLoadableOrStorableType(Type *ElemTy)
Return true if we can load or store from a pointer to this type.
Definition: Type.cpp:773
1: 16-bit floating point type
Definition: Type.h:57
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
DenseMap< std::pair< Type *, unsigned >, PointerType * > ASPointerTypes
static Type * getMetadataTy(LLVMContext &C)
Definition: Type.cpp:230
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: Type.cpp:216
static bool isValidArgumentType(Type *ArgTy)
isValidArgumentType - Return true if the specified type is valid as an argument type.
Definition: Type.cpp:394
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:238
static Type * getX86_MMXTy(LLVMContext &C)
Definition: Type.cpp:234
static PointerType * getX86_MMXPtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:271
StructType * getTypeByName(StringRef Name) const
Return the type with the specified name, or null if there is none by that name.
Definition: Type.cpp:625
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:295
static Type * getX86_FP80Ty(LLVMContext &C)
Definition: Type.cpp:231
Type *const * ContainedTys
ContainedTys - A pointer to the array of Types contained by this Type.
Definition: Type.h:118
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
element_iterator element_end() const
Definition: DerivedTypes.h:280
Minimum number of bits that can be specified.
Definition: DerivedTypes.h:47
bool isPacked() const
Definition: DerivedTypes.h:242
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:278
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:228
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
bool canLosslesslyBitCastTo(Type *Ty) const
canLosslesslyBitCastTo - Return true if this type could be converted with a lossless BitCast to type ...
Definition: Type.cpp:65
DenseMap< std::pair< Type *, uint64_t >, ArrayType * > ArrayTypes
bool isLiteral() const
isLiteral - Return true if this type is uniqued by structural equivalence, false if it is a struct de...
Definition: DerivedTypes.h:246
static StringRef getName(Value *V)
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Definition: Type.h:268
StringRef getStructName() const
Definition: Type.cpp:192
element_iterator element_begin() const
Definition: DerivedTypes.h:279
static PointerType * getInt16PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:287
Type * getFunctionParamType(unsigned i) const
Definition: Type.cpp:184
static Type * getPPC_FP128Ty(LLVMContext &C)
Definition: Type.cpp:233
BumpPtrAllocator TypeAllocator
TypeAllocator - All dynamically allocated types are allocated from this.
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:117
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
static Type * getLabelTy(LLVMContext &C)
Definition: Type.cpp:226
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
ArrayType - Class to represent array types.
Definition: DerivedTypes.h:336
static bool isValidElementType(Type *ElemTy)
isValidElementType - Return true if the specified type is valid as a element type.
Definition: Type.cpp:729
static PointerType * getDoublePtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:255
bool isFirstClassType() const
isFirstClassType - Return true if the type is "first class", meaning it is a valid type for a Value...
Definition: Type.h:242
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
FunctionType::get - This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:361
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
TypeID getTypeID() const
getTypeID - Return the type id for the type.
Definition: Type.h:134
bool isFloatingPointTy() const
isFloatingPointTy - Return true if this is one of the six floating point types
Definition: Type.h:159
unsigned getSubclassData() const
Definition: Type.h:101
DenseMap< std::pair< Type *, unsigned >, VectorType * > VectorTypes
Type * getElementType() const
Definition: DerivedTypes.h:323
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
10: Arbitrary bit width integers
Definition: Type.h:69
unsigned getFunctionNumParams() const
Definition: Type.cpp:188
0: type with no size
Definition: Type.h:56
bool isIntOrIntVectorTy() const
isIntOrIntVectorTy - Return true if this is an integer type or a vector of integer types...
Definition: Type.h:201
static IntegerType * getInt128Ty(LLVMContext &C)
Definition: Type.cpp:241
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
void resync()
This is called when the SmallVector we're appending to is changed outside of the raw_svector_ostream'...
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Definition: Type.h:226
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:291
This is an important base class in LLVM.
Definition: Constant.h:41
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:208
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
bool isLayoutIdentical(StructType *Other) const
isLayoutIdentical - Return true if this is layout identical to the specified struct.
Definition: Type.cpp:610
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:225
Type * getTypeAtIndex(const Value *V)
getTypeAtIndex - Given an index value into the type, return the type of the element.
Definition: Type.cpp:634
uint64_t getNumElements() const
Definition: DerivedTypes.h:352
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
Class to represent integer types.
Definition: DerivedTypes.h:37
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
static PointerType * getPPC_FP128PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:267
static bool isValidElementType(Type *ElemTy)
isValidElementType - Return true if the specified type is valid as a element type.
Definition: Type.cpp:603
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
static PointerType * getFloatPtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:251
PointerType * getPointerTo(unsigned AddrSpace=0)
getPointerTo - Return a pointer to the current type.
Definition: Type.cpp:764
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:283
void setBody(ArrayRef< Type * > Elements, bool isPacked=false)
setBody - Specify a body for an opaque identified type.
Definition: Type.cpp:424
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:43
static Type * getFP128Ty(LLVMContext &C)
Definition: Type.cpp:232
static PointerType * getX86_FP80PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:259
SequentialType - This is the superclass of the array, pointer and vector type classes.
Definition: DerivedTypes.h:310
static Type * getHalfTy(LLVMContext &C)
Definition: Type.cpp:227
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:147
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:279
unsigned getIntegerBitWidth() const
Definition: Type.cpp:176
static bool isValidElementType(Type *ElemTy)
isValidElementType - Return true if the specified type is valid as a element type.
Definition: Type.cpp:768
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
bool isFunctionTy() const
isFunctionTy - True if this is an instance of FunctionType.
Definition: Type.h:205
15: SIMD 'packed' format, or other vector type
Definition: Type.h:74
unsigned getVectorNumElements() const
Definition: Type.cpp:212
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
StructType::get - This static method is the primary way to create a literal StructType.
Definition: Type.cpp:404
bool isFunctionVarArg() const
Definition: Type.cpp:180
unsigned getScalarSizeInBits() const LLVM_READONLY
getScalarSizeInBits - If this is a vector type, return the getPrimitiveSizeInBits value for the eleme...
Definition: Type.cpp:139
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
AllocatorTy & getAllocator()
Definition: StringMap.h:241
AddressSpace
Definition: NVPTXBaseInfo.h:22
FunctionTypeSet FunctionTypes
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:243
static PointerType * getHalfPtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:247
Maximum number of bits that can be specified.
Definition: DerivedTypes.h:48
static Type * getPrimitiveType(LLVMContext &C, TypeID IDNumber)
getPrimitiveType - Return a type based on an identifier.
Definition: Type.cpp:26
static PointerType * getFP128PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:263
8: Metadata
Definition: Type.h:64
Symbol info for RuntimeDyld.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:214
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
Class for arbitrary precision integers.
Definition: APInt.h:73
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
StringRef getName() const
getName - Return the name for this struct type if it has an identity.
Definition: Type.cpp:583
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
iterator find_as(const LookupKeyT &Val)
Alternative version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseSet.h:136
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
Definition: Type.cpp:51
StringRef str()
Flushes the stream contents to the target vector and return a StringRef for the vector contents...
StructTypeSet AnonStructTypes
void setName(StringRef Name)
setName - Change the name of this type to the specified name, or to a name with a suffix if there is ...
Definition: Type.cpp:439
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
StringMap< StructType * > NamedStructTypes
static bool isValidElementType(Type *ElemTy)
isValidElementType - Return true if the specified type is valid as a element type.
Definition: Type.cpp:699
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ArrayType::get - This static method is the primary way to construct an ArrayType. ...
Definition: Type.cpp:686
bool isPowerOf2ByteWidth() const
This method determines if the width of this IntegerType is a power-of-2 in terms of 8 bit bytes...
Definition: Type.cpp:328
uint64_t getArrayNumElements() const
Definition: Type.cpp:208
IntegerType(LLVMContext &C, unsigned NumBits)
Definition: DerivedTypes.h:41
static PointerType * getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS=0)
Definition: Type.cpp:275
bool isVarArg() const
Definition: DerivedTypes.h:120
3: 64-bit floating point type
Definition: Type.h:59
void setSubclassData(unsigned val)
Definition: Type.h:103
bool isEmptyTy() const
isEmptyTy - Return true if this type is empty, that is, it has no elements or all its elements are em...
Definition: Type.cpp:102
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:121
bool isLabelTy() const
isLabelTy - Return true if this is 'label'.
Definition: Type.h:186
LLVM Value Representation.
Definition: Value.h:69
static VectorType * get(Type *ElementType, unsigned NumElements)
VectorType::get - This static method is the primary way to construct an VectorType.
Definition: Type.cpp:713
static StructType * create(LLVMContext &Context, StringRef Name)
StructType::create - This creates an identified struct.
Definition: Type.cpp:490
Type * getStructElementType(unsigned N) const
Definition: Type.cpp:200
bool isPowerOf2_32(uint32_t Value)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
Definition: MathExtras.h:354
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
DenseMap< Type *, PointerType * > PointerTypes
iterator end()
Definition: DenseSet.h:123
9: MMX vectors (64 bits, X86 specific)
Definition: Type.h:65
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:290
const T * data() const
Definition: ArrayRef.h:131
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
unsigned NumContainedTys
NumContainedTys - Keeps track of how many Type*'s there are in the ContainedTys list.
Definition: Type.h:111
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:265
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:137
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:61
bool isMetadataTy() const
isMetadataTy - Return true if this is 'metadata'.
Definition: Type.h:189
void resize(size_type N)
Definition: SmallVector.h:376