[InstCombine] Signed saturation patterns
[llvm-complete.git] / lib / IR / Type.cpp
blob3eab5042b54248125b1f13220a1badb42ab32430
1 //===- Type.cpp - Implement the Type class --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Type class for the IR library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Type.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/TypeSize.h"
30 #include <cassert>
31 #include <utility>
33 using namespace llvm;
35 //===----------------------------------------------------------------------===//
36 // Type Class Implementation
37 //===----------------------------------------------------------------------===//
39 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
40 switch (IDNumber) {
41 case VoidTyID : return getVoidTy(C);
42 case HalfTyID : return getHalfTy(C);
43 case FloatTyID : return getFloatTy(C);
44 case DoubleTyID : return getDoubleTy(C);
45 case X86_FP80TyID : return getX86_FP80Ty(C);
46 case FP128TyID : return getFP128Ty(C);
47 case PPC_FP128TyID : return getPPC_FP128Ty(C);
48 case LabelTyID : return getLabelTy(C);
49 case MetadataTyID : return getMetadataTy(C);
50 case X86_MMXTyID : return getX86_MMXTy(C);
51 case TokenTyID : return getTokenTy(C);
52 default:
53 return nullptr;
57 bool Type::isIntegerTy(unsigned Bitwidth) const {
58 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
61 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
62 // Identity cast means no change so return true
63 if (this == Ty)
64 return true;
66 // They are not convertible unless they are at least first class types
67 if (!this->isFirstClassType() || !Ty->isFirstClassType())
68 return false;
70 // Vector -> Vector conversions are always lossless if the two vector types
71 // have the same size, otherwise not. Also, 64-bit vector types can be
72 // converted to x86mmx.
73 if (auto *thisPTy = dyn_cast<VectorType>(this)) {
74 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
75 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
76 if (Ty->getTypeID() == Type::X86_MMXTyID &&
77 thisPTy->getBitWidth() == 64)
78 return true;
81 if (this->getTypeID() == Type::X86_MMXTyID)
82 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
83 if (thatPTy->getBitWidth() == 64)
84 return true;
86 // At this point we have only various mismatches of the first class types
87 // remaining and ptr->ptr. Just select the lossless conversions. Everything
88 // else is not lossless. Conservatively assume we can't losslessly convert
89 // between pointers with different address spaces.
90 if (auto *PTy = dyn_cast<PointerType>(this)) {
91 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
92 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
93 return false;
95 return false; // Other types have no identity values
98 bool Type::isEmptyTy() const {
99 if (auto *ATy = dyn_cast<ArrayType>(this)) {
100 unsigned NumElements = ATy->getNumElements();
101 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
104 if (auto *STy = dyn_cast<StructType>(this)) {
105 unsigned NumElements = STy->getNumElements();
106 for (unsigned i = 0; i < NumElements; ++i)
107 if (!STy->getElementType(i)->isEmptyTy())
108 return false;
109 return true;
112 return false;
115 TypeSize Type::getPrimitiveSizeInBits() const {
116 switch (getTypeID()) {
117 case Type::HalfTyID: return TypeSize::Fixed(16);
118 case Type::FloatTyID: return TypeSize::Fixed(32);
119 case Type::DoubleTyID: return TypeSize::Fixed(64);
120 case Type::X86_FP80TyID: return TypeSize::Fixed(80);
121 case Type::FP128TyID: return TypeSize::Fixed(128);
122 case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
123 case Type::X86_MMXTyID: return TypeSize::Fixed(64);
124 case Type::IntegerTyID:
125 return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
126 case Type::VectorTyID: {
127 const VectorType *VTy = cast<VectorType>(this);
128 return TypeSize(VTy->getBitWidth(), VTy->isScalable());
130 default: return TypeSize::Fixed(0);
134 unsigned Type::getScalarSizeInBits() const {
135 return getScalarType()->getPrimitiveSizeInBits();
138 int Type::getFPMantissaWidth() const {
139 if (auto *VTy = dyn_cast<VectorType>(this))
140 return VTy->getElementType()->getFPMantissaWidth();
141 assert(isFloatingPointTy() && "Not a floating point type!");
142 if (getTypeID() == HalfTyID) return 11;
143 if (getTypeID() == FloatTyID) return 24;
144 if (getTypeID() == DoubleTyID) return 53;
145 if (getTypeID() == X86_FP80TyID) return 64;
146 if (getTypeID() == FP128TyID) return 113;
147 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
148 return -1;
151 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
152 if (auto *ATy = dyn_cast<ArrayType>(this))
153 return ATy->getElementType()->isSized(Visited);
155 if (auto *VTy = dyn_cast<VectorType>(this))
156 return VTy->getElementType()->isSized(Visited);
158 return cast<StructType>(this)->isSized(Visited);
161 //===----------------------------------------------------------------------===//
162 // Primitive 'Type' data
163 //===----------------------------------------------------------------------===//
165 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
166 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
167 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
168 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
169 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
170 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
171 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
172 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
173 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
174 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
175 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
177 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
178 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
179 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
180 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
181 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
182 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
184 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
185 return IntegerType::get(C, N);
188 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
189 return getHalfTy(C)->getPointerTo(AS);
192 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
193 return getFloatTy(C)->getPointerTo(AS);
196 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
197 return getDoubleTy(C)->getPointerTo(AS);
200 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
201 return getX86_FP80Ty(C)->getPointerTo(AS);
204 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
205 return getFP128Ty(C)->getPointerTo(AS);
208 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
209 return getPPC_FP128Ty(C)->getPointerTo(AS);
212 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
213 return getX86_MMXTy(C)->getPointerTo(AS);
216 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
217 return getIntNTy(C, N)->getPointerTo(AS);
220 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
221 return getInt1Ty(C)->getPointerTo(AS);
224 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
225 return getInt8Ty(C)->getPointerTo(AS);
228 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
229 return getInt16Ty(C)->getPointerTo(AS);
232 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
233 return getInt32Ty(C)->getPointerTo(AS);
236 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
237 return getInt64Ty(C)->getPointerTo(AS);
240 //===----------------------------------------------------------------------===//
241 // IntegerType Implementation
242 //===----------------------------------------------------------------------===//
244 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
245 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
246 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
248 // Check for the built-in integer types
249 switch (NumBits) {
250 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
251 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
252 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
253 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
254 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
255 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
256 default:
257 break;
260 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
262 if (!Entry)
263 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
265 return Entry;
268 bool IntegerType::isPowerOf2ByteWidth() const {
269 unsigned BitWidth = getBitWidth();
270 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
273 APInt IntegerType::getMask() const {
274 return APInt::getAllOnesValue(getBitWidth());
277 //===----------------------------------------------------------------------===//
278 // FunctionType Implementation
279 //===----------------------------------------------------------------------===//
281 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
282 bool IsVarArgs)
283 : Type(Result->getContext(), FunctionTyID) {
284 Type **SubTys = reinterpret_cast<Type**>(this+1);
285 assert(isValidReturnType(Result) && "invalid return type for function");
286 setSubclassData(IsVarArgs);
288 SubTys[0] = Result;
290 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
291 assert(isValidArgumentType(Params[i]) &&
292 "Not a valid type for function argument!");
293 SubTys[i+1] = Params[i];
296 ContainedTys = SubTys;
297 NumContainedTys = Params.size() + 1; // + 1 for result type
300 // This is the factory function for the FunctionType class.
301 FunctionType *FunctionType::get(Type *ReturnType,
302 ArrayRef<Type*> Params, bool isVarArg) {
303 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
304 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
305 FunctionType *FT;
306 // Since we only want to allocate a fresh function type in case none is found
307 // and we don't want to perform two lookups (one for checking if existent and
308 // one for inserting the newly allocated one), here we instead lookup based on
309 // Key and update the reference to the function type in-place to a newly
310 // allocated one if not found.
311 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
312 if (Insertion.second) {
313 // The function type was not found. Allocate one and update FunctionTypes
314 // in-place.
315 FT = (FunctionType *)pImpl->Alloc.Allocate(
316 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
317 alignof(FunctionType));
318 new (FT) FunctionType(ReturnType, Params, isVarArg);
319 *Insertion.first = FT;
320 } else {
321 // The function type was found. Just return it.
322 FT = *Insertion.first;
324 return FT;
327 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
328 return get(Result, None, isVarArg);
331 bool FunctionType::isValidReturnType(Type *RetTy) {
332 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
333 !RetTy->isMetadataTy();
336 bool FunctionType::isValidArgumentType(Type *ArgTy) {
337 return ArgTy->isFirstClassType();
340 //===----------------------------------------------------------------------===//
341 // StructType Implementation
342 //===----------------------------------------------------------------------===//
344 // Primitive Constructors.
346 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
347 bool isPacked) {
348 LLVMContextImpl *pImpl = Context.pImpl;
349 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
351 StructType *ST;
352 // Since we only want to allocate a fresh struct type in case none is found
353 // and we don't want to perform two lookups (one for checking if existent and
354 // one for inserting the newly allocated one), here we instead lookup based on
355 // Key and update the reference to the struct type in-place to a newly
356 // allocated one if not found.
357 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
358 if (Insertion.second) {
359 // The struct type was not found. Allocate one and update AnonStructTypes
360 // in-place.
361 ST = new (Context.pImpl->Alloc) StructType(Context);
362 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
363 ST->setBody(ETypes, isPacked);
364 *Insertion.first = ST;
365 } else {
366 // The struct type was found. Just return it.
367 ST = *Insertion.first;
370 return ST;
373 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
374 assert(isOpaque() && "Struct body already set!");
376 setSubclassData(getSubclassData() | SCDB_HasBody);
377 if (isPacked)
378 setSubclassData(getSubclassData() | SCDB_Packed);
380 NumContainedTys = Elements.size();
382 if (Elements.empty()) {
383 ContainedTys = nullptr;
384 return;
387 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
390 void StructType::setName(StringRef Name) {
391 if (Name == getName()) return;
393 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
395 using EntryTy = StringMap<StructType *>::MapEntryTy;
397 // If this struct already had a name, remove its symbol table entry. Don't
398 // delete the data yet because it may be part of the new name.
399 if (SymbolTableEntry)
400 SymbolTable.remove((EntryTy *)SymbolTableEntry);
402 // If this is just removing the name, we're done.
403 if (Name.empty()) {
404 if (SymbolTableEntry) {
405 // Delete the old string data.
406 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
407 SymbolTableEntry = nullptr;
409 return;
412 // Look up the entry for the name.
413 auto IterBool =
414 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
416 // While we have a name collision, try a random rename.
417 if (!IterBool.second) {
418 SmallString<64> TempStr(Name);
419 TempStr.push_back('.');
420 raw_svector_ostream TmpStream(TempStr);
421 unsigned NameSize = Name.size();
423 do {
424 TempStr.resize(NameSize + 1);
425 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
427 IterBool = getContext().pImpl->NamedStructTypes.insert(
428 std::make_pair(TmpStream.str(), this));
429 } while (!IterBool.second);
432 // Delete the old string data.
433 if (SymbolTableEntry)
434 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
435 SymbolTableEntry = &*IterBool.first;
438 //===----------------------------------------------------------------------===//
439 // StructType Helper functions.
441 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
442 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
443 if (!Name.empty())
444 ST->setName(Name);
445 return ST;
448 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
449 return get(Context, None, isPacked);
452 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
453 StringRef Name, bool isPacked) {
454 StructType *ST = create(Context, Name);
455 ST->setBody(Elements, isPacked);
456 return ST;
459 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
460 return create(Context, Elements, StringRef());
463 StructType *StructType::create(LLVMContext &Context) {
464 return create(Context, StringRef());
467 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
468 bool isPacked) {
469 assert(!Elements.empty() &&
470 "This method may not be invoked with an empty list");
471 return create(Elements[0]->getContext(), Elements, Name, isPacked);
474 StructType *StructType::create(ArrayRef<Type*> Elements) {
475 assert(!Elements.empty() &&
476 "This method may not be invoked with an empty list");
477 return create(Elements[0]->getContext(), Elements, StringRef());
480 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
481 if ((getSubclassData() & SCDB_IsSized) != 0)
482 return true;
483 if (isOpaque())
484 return false;
486 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
487 return false;
489 // Okay, our struct is sized if all of the elements are, but if one of the
490 // elements is opaque, the struct isn't sized *yet*, but may become sized in
491 // the future, so just bail out without caching.
492 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
493 if (!(*I)->isSized(Visited))
494 return false;
496 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
497 // we find a sized type, as types can only move from opaque to sized, not the
498 // other way.
499 const_cast<StructType*>(this)->setSubclassData(
500 getSubclassData() | SCDB_IsSized);
501 return true;
504 StringRef StructType::getName() const {
505 assert(!isLiteral() && "Literal structs never have names");
506 if (!SymbolTableEntry) return StringRef();
508 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
511 bool StructType::isValidElementType(Type *ElemTy) {
512 if (auto *VTy = dyn_cast<VectorType>(ElemTy))
513 return !VTy->isScalable();
514 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
515 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
516 !ElemTy->isTokenTy();
519 bool StructType::isLayoutIdentical(StructType *Other) const {
520 if (this == Other) return true;
522 if (isPacked() != Other->isPacked())
523 return false;
525 return elements() == Other->elements();
528 StructType *Module::getTypeByName(StringRef Name) const {
529 return getContext().pImpl->NamedStructTypes.lookup(Name);
532 //===----------------------------------------------------------------------===//
533 // CompositeType Implementation
534 //===----------------------------------------------------------------------===//
536 Type *CompositeType::getTypeAtIndex(const Value *V) const {
537 if (auto *STy = dyn_cast<StructType>(this)) {
538 unsigned Idx =
539 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
540 assert(indexValid(Idx) && "Invalid structure index!");
541 return STy->getElementType(Idx);
544 return cast<SequentialType>(this)->getElementType();
547 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
548 if (auto *STy = dyn_cast<StructType>(this)) {
549 assert(indexValid(Idx) && "Invalid structure index!");
550 return STy->getElementType(Idx);
553 return cast<SequentialType>(this)->getElementType();
556 bool CompositeType::indexValid(const Value *V) const {
557 if (auto *STy = dyn_cast<StructType>(this)) {
558 // Structure indexes require (vectors of) 32-bit integer constants. In the
559 // vector case all of the indices must be equal.
560 if (!V->getType()->isIntOrIntVectorTy(32))
561 return false;
562 const Constant *C = dyn_cast<Constant>(V);
563 if (C && V->getType()->isVectorTy())
564 C = C->getSplatValue();
565 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
566 return CU && CU->getZExtValue() < STy->getNumElements();
569 // Sequential types can be indexed by any integer.
570 return V->getType()->isIntOrIntVectorTy();
573 bool CompositeType::indexValid(unsigned Idx) const {
574 if (auto *STy = dyn_cast<StructType>(this))
575 return Idx < STy->getNumElements();
576 // Sequential types can be indexed by any integer.
577 return true;
580 //===----------------------------------------------------------------------===//
581 // ArrayType Implementation
582 //===----------------------------------------------------------------------===//
584 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
585 : SequentialType(ArrayTyID, ElType, NumEl) {}
587 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
588 assert(isValidElementType(ElementType) && "Invalid type for array element!");
590 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
591 ArrayType *&Entry =
592 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
594 if (!Entry)
595 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
596 return Entry;
599 bool ArrayType::isValidElementType(Type *ElemTy) {
600 if (auto *VTy = dyn_cast<VectorType>(ElemTy))
601 return !VTy->isScalable();
602 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
603 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
604 !ElemTy->isTokenTy();
607 //===----------------------------------------------------------------------===//
608 // VectorType Implementation
609 //===----------------------------------------------------------------------===//
611 VectorType::VectorType(Type *ElType, ElementCount EC)
612 : SequentialType(VectorTyID, ElType, EC.Min), Scalable(EC.Scalable) {}
614 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
615 assert(EC.Min > 0 && "#Elements of a VectorType must be greater than 0");
616 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
617 "be an integer, floating point, or "
618 "pointer type.");
620 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
621 VectorType *&Entry = ElementType->getContext().pImpl
622 ->VectorTypes[std::make_pair(ElementType, EC)];
623 if (!Entry)
624 Entry = new (pImpl->Alloc) VectorType(ElementType, EC);
625 return Entry;
628 bool VectorType::isValidElementType(Type *ElemTy) {
629 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
630 ElemTy->isPointerTy();
633 //===----------------------------------------------------------------------===//
634 // PointerType Implementation
635 //===----------------------------------------------------------------------===//
637 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
638 assert(EltTy && "Can't get a pointer to <null> type!");
639 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
641 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
643 // Since AddressSpace #0 is the common case, we special case it.
644 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
645 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
647 if (!Entry)
648 Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace);
649 return Entry;
652 PointerType::PointerType(Type *E, unsigned AddrSpace)
653 : Type(E->getContext(), PointerTyID), PointeeTy(E) {
654 ContainedTys = &PointeeTy;
655 NumContainedTys = 1;
656 setSubclassData(AddrSpace);
659 PointerType *Type::getPointerTo(unsigned addrs) const {
660 return PointerType::get(const_cast<Type*>(this), addrs);
663 bool PointerType::isValidElementType(Type *ElemTy) {
664 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
665 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
668 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
669 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();