1249259Sdim//===-- Type.cpp - Implement the Type class -------------------------------===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file implements the Type class for the IR library.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14249259Sdim#include "llvm/IR/Type.h"
15249259Sdim#include "LLVMContextImpl.h"
16249259Sdim#include "llvm/ADT/SmallString.h"
17249259Sdim#include "llvm/IR/Module.h"
18249259Sdim#include <algorithm>
19249259Sdim#include <cstdarg>
20249259Sdimusing namespace llvm;
21249259Sdim
22249259Sdim//===----------------------------------------------------------------------===//
23249259Sdim//                         Type Class Implementation
24249259Sdim//===----------------------------------------------------------------------===//
25249259Sdim
26249259SdimType *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
27249259Sdim  switch (IDNumber) {
28249259Sdim  case VoidTyID      : return getVoidTy(C);
29249259Sdim  case HalfTyID      : return getHalfTy(C);
30249259Sdim  case FloatTyID     : return getFloatTy(C);
31249259Sdim  case DoubleTyID    : return getDoubleTy(C);
32249259Sdim  case X86_FP80TyID  : return getX86_FP80Ty(C);
33249259Sdim  case FP128TyID     : return getFP128Ty(C);
34249259Sdim  case PPC_FP128TyID : return getPPC_FP128Ty(C);
35249259Sdim  case LabelTyID     : return getLabelTy(C);
36249259Sdim  case MetadataTyID  : return getMetadataTy(C);
37249259Sdim  case X86_MMXTyID   : return getX86_MMXTy(C);
38249259Sdim  default:
39249259Sdim    return 0;
40249259Sdim  }
41249259Sdim}
42249259Sdim
43249259Sdim/// getScalarType - If this is a vector type, return the element type,
44249259Sdim/// otherwise return this.
45249259SdimType *Type::getScalarType() {
46249259Sdim  if (VectorType *VTy = dyn_cast<VectorType>(this))
47249259Sdim    return VTy->getElementType();
48249259Sdim  return this;
49249259Sdim}
50249259Sdim
51249259Sdimconst Type *Type::getScalarType() const {
52249259Sdim  if (const VectorType *VTy = dyn_cast<VectorType>(this))
53249259Sdim    return VTy->getElementType();
54249259Sdim  return this;
55249259Sdim}
56249259Sdim
57249259Sdim/// isIntegerTy - Return true if this is an IntegerType of the specified width.
58249259Sdimbool Type::isIntegerTy(unsigned Bitwidth) const {
59249259Sdim  return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
60249259Sdim}
61249259Sdim
62249259Sdim// canLosslesslyBitCastTo - Return true if this type can be converted to
63249259Sdim// 'Ty' without any reinterpretation of bits.  For example, i8* to i32*.
64249259Sdim//
65249259Sdimbool Type::canLosslesslyBitCastTo(Type *Ty) const {
66249259Sdim  // Identity cast means no change so return true
67249259Sdim  if (this == Ty)
68249259Sdim    return true;
69249259Sdim
70249259Sdim  // They are not convertible unless they are at least first class types
71249259Sdim  if (!this->isFirstClassType() || !Ty->isFirstClassType())
72249259Sdim    return false;
73249259Sdim
74249259Sdim  // Vector -> Vector conversions are always lossless if the two vector types
75249259Sdim  // have the same size, otherwise not.  Also, 64-bit vector types can be
76249259Sdim  // converted to x86mmx.
77249259Sdim  if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
78249259Sdim    if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
79249259Sdim      return thisPTy->getBitWidth() == thatPTy->getBitWidth();
80249259Sdim    if (Ty->getTypeID() == Type::X86_MMXTyID &&
81249259Sdim        thisPTy->getBitWidth() == 64)
82249259Sdim      return true;
83249259Sdim  }
84249259Sdim
85249259Sdim  if (this->getTypeID() == Type::X86_MMXTyID)
86249259Sdim    if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
87249259Sdim      if (thatPTy->getBitWidth() == 64)
88249259Sdim        return true;
89249259Sdim
90249259Sdim  // At this point we have only various mismatches of the first class types
91249259Sdim  // remaining and ptr->ptr. Just select the lossless conversions. Everything
92249259Sdim  // else is not lossless.
93249259Sdim  if (this->isPointerTy())
94249259Sdim    return Ty->isPointerTy();
95249259Sdim  return false;  // Other types have no identity values
96249259Sdim}
97249259Sdim
98249259Sdimbool Type::isEmptyTy() const {
99249259Sdim  const ArrayType *ATy = dyn_cast<ArrayType>(this);
100249259Sdim  if (ATy) {
101249259Sdim    unsigned NumElements = ATy->getNumElements();
102249259Sdim    return NumElements == 0 || ATy->getElementType()->isEmptyTy();
103249259Sdim  }
104249259Sdim
105249259Sdim  const StructType *STy = dyn_cast<StructType>(this);
106249259Sdim  if (STy) {
107249259Sdim    unsigned NumElements = STy->getNumElements();
108249259Sdim    for (unsigned i = 0; i < NumElements; ++i)
109249259Sdim      if (!STy->getElementType(i)->isEmptyTy())
110249259Sdim        return false;
111249259Sdim    return true;
112249259Sdim  }
113249259Sdim
114249259Sdim  return false;
115249259Sdim}
116249259Sdim
117249259Sdimunsigned Type::getPrimitiveSizeInBits() const {
118249259Sdim  switch (getTypeID()) {
119249259Sdim  case Type::HalfTyID: return 16;
120249259Sdim  case Type::FloatTyID: return 32;
121249259Sdim  case Type::DoubleTyID: return 64;
122249259Sdim  case Type::X86_FP80TyID: return 80;
123249259Sdim  case Type::FP128TyID: return 128;
124249259Sdim  case Type::PPC_FP128TyID: return 128;
125249259Sdim  case Type::X86_MMXTyID: return 64;
126249259Sdim  case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
127249259Sdim  case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
128249259Sdim  default: return 0;
129249259Sdim  }
130249259Sdim}
131249259Sdim
132249259Sdim/// getScalarSizeInBits - If this is a vector type, return the
133249259Sdim/// getPrimitiveSizeInBits value for the element type. Otherwise return the
134249259Sdim/// getPrimitiveSizeInBits value for this type.
135249259Sdimunsigned Type::getScalarSizeInBits() {
136249259Sdim  return getScalarType()->getPrimitiveSizeInBits();
137249259Sdim}
138249259Sdim
139249259Sdim/// getFPMantissaWidth - Return the width of the mantissa of this type.  This
140249259Sdim/// is only valid on floating point types.  If the FP type does not
141249259Sdim/// have a stable mantissa (e.g. ppc long double), this method returns -1.
142249259Sdimint Type::getFPMantissaWidth() const {
143249259Sdim  if (const VectorType *VTy = dyn_cast<VectorType>(this))
144249259Sdim    return VTy->getElementType()->getFPMantissaWidth();
145249259Sdim  assert(isFloatingPointTy() && "Not a floating point type!");
146249259Sdim  if (getTypeID() == HalfTyID) return 11;
147249259Sdim  if (getTypeID() == FloatTyID) return 24;
148249259Sdim  if (getTypeID() == DoubleTyID) return 53;
149249259Sdim  if (getTypeID() == X86_FP80TyID) return 64;
150249259Sdim  if (getTypeID() == FP128TyID) return 113;
151249259Sdim  assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
152249259Sdim  return -1;
153249259Sdim}
154249259Sdim
155249259Sdim/// isSizedDerivedType - Derived types like structures and arrays are sized
156249259Sdim/// iff all of the members of the type are sized as well.  Since asking for
157249259Sdim/// their size is relatively uncommon, move this operation out of line.
158249259Sdimbool Type::isSizedDerivedType() const {
159249259Sdim  if (this->isIntegerTy())
160249259Sdim    return true;
161249259Sdim
162249259Sdim  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
163249259Sdim    return ATy->getElementType()->isSized();
164249259Sdim
165249259Sdim  if (const VectorType *VTy = dyn_cast<VectorType>(this))
166249259Sdim    return VTy->getElementType()->isSized();
167249259Sdim
168249259Sdim  if (!this->isStructTy())
169249259Sdim    return false;
170249259Sdim
171249259Sdim  return cast<StructType>(this)->isSized();
172249259Sdim}
173249259Sdim
174249259Sdim//===----------------------------------------------------------------------===//
175249259Sdim//                         Subclass Helper Methods
176249259Sdim//===----------------------------------------------------------------------===//
177249259Sdim
178249259Sdimunsigned Type::getIntegerBitWidth() const {
179249259Sdim  return cast<IntegerType>(this)->getBitWidth();
180249259Sdim}
181249259Sdim
182249259Sdimbool Type::isFunctionVarArg() const {
183249259Sdim  return cast<FunctionType>(this)->isVarArg();
184249259Sdim}
185249259Sdim
186249259SdimType *Type::getFunctionParamType(unsigned i) const {
187249259Sdim  return cast<FunctionType>(this)->getParamType(i);
188249259Sdim}
189249259Sdim
190249259Sdimunsigned Type::getFunctionNumParams() const {
191249259Sdim  return cast<FunctionType>(this)->getNumParams();
192249259Sdim}
193249259Sdim
194249259SdimStringRef Type::getStructName() const {
195249259Sdim  return cast<StructType>(this)->getName();
196249259Sdim}
197249259Sdim
198249259Sdimunsigned Type::getStructNumElements() const {
199249259Sdim  return cast<StructType>(this)->getNumElements();
200249259Sdim}
201249259Sdim
202249259SdimType *Type::getStructElementType(unsigned N) const {
203249259Sdim  return cast<StructType>(this)->getElementType(N);
204249259Sdim}
205249259Sdim
206249259SdimType *Type::getSequentialElementType() const {
207249259Sdim  return cast<SequentialType>(this)->getElementType();
208249259Sdim}
209249259Sdim
210249259Sdimuint64_t Type::getArrayNumElements() const {
211249259Sdim  return cast<ArrayType>(this)->getNumElements();
212249259Sdim}
213249259Sdim
214249259Sdimunsigned Type::getVectorNumElements() const {
215249259Sdim  return cast<VectorType>(this)->getNumElements();
216249259Sdim}
217249259Sdim
218249259Sdimunsigned Type::getPointerAddressSpace() const {
219249259Sdim  return cast<PointerType>(getScalarType())->getAddressSpace();
220249259Sdim}
221249259Sdim
222249259Sdim
223249259Sdim//===----------------------------------------------------------------------===//
224249259Sdim//                          Primitive 'Type' data
225249259Sdim//===----------------------------------------------------------------------===//
226249259Sdim
227249259SdimType *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
228249259SdimType *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
229249259SdimType *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
230249259SdimType *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
231249259SdimType *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
232249259SdimType *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
233249259SdimType *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
234249259SdimType *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
235249259SdimType *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
236249259SdimType *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
237249259Sdim
238249259SdimIntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
239249259SdimIntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
240249259SdimIntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
241249259SdimIntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
242249259SdimIntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
243249259Sdim
244249259SdimIntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
245249259Sdim  return IntegerType::get(C, N);
246249259Sdim}
247249259Sdim
248249259SdimPointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
249249259Sdim  return getHalfTy(C)->getPointerTo(AS);
250249259Sdim}
251249259Sdim
252249259SdimPointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
253249259Sdim  return getFloatTy(C)->getPointerTo(AS);
254249259Sdim}
255249259Sdim
256249259SdimPointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
257249259Sdim  return getDoubleTy(C)->getPointerTo(AS);
258249259Sdim}
259249259Sdim
260249259SdimPointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
261249259Sdim  return getX86_FP80Ty(C)->getPointerTo(AS);
262249259Sdim}
263249259Sdim
264249259SdimPointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
265249259Sdim  return getFP128Ty(C)->getPointerTo(AS);
266249259Sdim}
267249259Sdim
268249259SdimPointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
269249259Sdim  return getPPC_FP128Ty(C)->getPointerTo(AS);
270249259Sdim}
271249259Sdim
272249259SdimPointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
273249259Sdim  return getX86_MMXTy(C)->getPointerTo(AS);
274249259Sdim}
275249259Sdim
276249259SdimPointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
277249259Sdim  return getIntNTy(C, N)->getPointerTo(AS);
278249259Sdim}
279249259Sdim
280249259SdimPointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
281249259Sdim  return getInt1Ty(C)->getPointerTo(AS);
282249259Sdim}
283249259Sdim
284249259SdimPointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
285249259Sdim  return getInt8Ty(C)->getPointerTo(AS);
286249259Sdim}
287249259Sdim
288249259SdimPointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
289249259Sdim  return getInt16Ty(C)->getPointerTo(AS);
290249259Sdim}
291249259Sdim
292249259SdimPointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
293249259Sdim  return getInt32Ty(C)->getPointerTo(AS);
294249259Sdim}
295249259Sdim
296249259SdimPointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
297249259Sdim  return getInt64Ty(C)->getPointerTo(AS);
298249259Sdim}
299249259Sdim
300249259Sdim
301249259Sdim//===----------------------------------------------------------------------===//
302249259Sdim//                       IntegerType Implementation
303249259Sdim//===----------------------------------------------------------------------===//
304249259Sdim
305249259SdimIntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
306249259Sdim  assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
307249259Sdim  assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
308249259Sdim
309249259Sdim  // Check for the built-in integer types
310249259Sdim  switch (NumBits) {
311249259Sdim  case  1: return cast<IntegerType>(Type::getInt1Ty(C));
312249259Sdim  case  8: return cast<IntegerType>(Type::getInt8Ty(C));
313249259Sdim  case 16: return cast<IntegerType>(Type::getInt16Ty(C));
314249259Sdim  case 32: return cast<IntegerType>(Type::getInt32Ty(C));
315249259Sdim  case 64: return cast<IntegerType>(Type::getInt64Ty(C));
316249259Sdim  default:
317249259Sdim    break;
318249259Sdim  }
319249259Sdim
320249259Sdim  IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
321249259Sdim
322249259Sdim  if (Entry == 0)
323249259Sdim    Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
324249259Sdim
325249259Sdim  return Entry;
326249259Sdim}
327249259Sdim
328249259Sdimbool IntegerType::isPowerOf2ByteWidth() const {
329249259Sdim  unsigned BitWidth = getBitWidth();
330249259Sdim  return (BitWidth > 7) && isPowerOf2_32(BitWidth);
331249259Sdim}
332249259Sdim
333249259SdimAPInt IntegerType::getMask() const {
334249259Sdim  return APInt::getAllOnesValue(getBitWidth());
335249259Sdim}
336249259Sdim
337249259Sdim//===----------------------------------------------------------------------===//
338249259Sdim//                       FunctionType Implementation
339249259Sdim//===----------------------------------------------------------------------===//
340249259Sdim
341249259SdimFunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
342249259Sdim                           bool IsVarArgs)
343249259Sdim  : Type(Result->getContext(), FunctionTyID) {
344249259Sdim  Type **SubTys = reinterpret_cast<Type**>(this+1);
345249259Sdim  assert(isValidReturnType(Result) && "invalid return type for function");
346249259Sdim  setSubclassData(IsVarArgs);
347249259Sdim
348249259Sdim  SubTys[0] = const_cast<Type*>(Result);
349249259Sdim
350249259Sdim  for (unsigned i = 0, e = Params.size(); i != e; ++i) {
351249259Sdim    assert(isValidArgumentType(Params[i]) &&
352249259Sdim           "Not a valid type for function argument!");
353249259Sdim    SubTys[i+1] = Params[i];
354249259Sdim  }
355249259Sdim
356249259Sdim  ContainedTys = SubTys;
357249259Sdim  NumContainedTys = Params.size() + 1; // + 1 for result type
358249259Sdim}
359249259Sdim
360249259Sdim// FunctionType::get - The factory function for the FunctionType class.
361249259SdimFunctionType *FunctionType::get(Type *ReturnType,
362249259Sdim                                ArrayRef<Type*> Params, bool isVarArg) {
363249259Sdim  LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
364249259Sdim  FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
365249259Sdim  LLVMContextImpl::FunctionTypeMap::iterator I =
366249259Sdim    pImpl->FunctionTypes.find_as(Key);
367249259Sdim  FunctionType *FT;
368249259Sdim
369249259Sdim  if (I == pImpl->FunctionTypes.end()) {
370249259Sdim    FT = (FunctionType*) pImpl->TypeAllocator.
371249259Sdim      Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
372249259Sdim               AlignOf<FunctionType>::Alignment);
373249259Sdim    new (FT) FunctionType(ReturnType, Params, isVarArg);
374249259Sdim    pImpl->FunctionTypes[FT] = true;
375249259Sdim  } else {
376249259Sdim    FT = I->first;
377249259Sdim  }
378249259Sdim
379249259Sdim  return FT;
380249259Sdim}
381249259Sdim
382249259SdimFunctionType *FunctionType::get(Type *Result, bool isVarArg) {
383252723Sdim  return get(Result, None, isVarArg);
384249259Sdim}
385249259Sdim
386249259Sdim/// isValidReturnType - Return true if the specified type is valid as a return
387249259Sdim/// type.
388249259Sdimbool FunctionType::isValidReturnType(Type *RetTy) {
389249259Sdim  return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
390249259Sdim  !RetTy->isMetadataTy();
391249259Sdim}
392249259Sdim
393249259Sdim/// isValidArgumentType - Return true if the specified type is valid as an
394249259Sdim/// argument type.
395249259Sdimbool FunctionType::isValidArgumentType(Type *ArgTy) {
396249259Sdim  return ArgTy->isFirstClassType();
397249259Sdim}
398249259Sdim
399249259Sdim//===----------------------------------------------------------------------===//
400249259Sdim//                       StructType Implementation
401249259Sdim//===----------------------------------------------------------------------===//
402249259Sdim
403249259Sdim// Primitive Constructors.
404249259Sdim
405249259SdimStructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
406249259Sdim                            bool isPacked) {
407249259Sdim  LLVMContextImpl *pImpl = Context.pImpl;
408249259Sdim  AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
409249259Sdim  LLVMContextImpl::StructTypeMap::iterator I =
410249259Sdim    pImpl->AnonStructTypes.find_as(Key);
411249259Sdim  StructType *ST;
412249259Sdim
413249259Sdim  if (I == pImpl->AnonStructTypes.end()) {
414249259Sdim    // Value not found.  Create a new type!
415249259Sdim    ST = new (Context.pImpl->TypeAllocator) StructType(Context);
416249259Sdim    ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
417249259Sdim    ST->setBody(ETypes, isPacked);
418249259Sdim    Context.pImpl->AnonStructTypes[ST] = true;
419249259Sdim  } else {
420249259Sdim    ST = I->first;
421249259Sdim  }
422249259Sdim
423249259Sdim  return ST;
424249259Sdim}
425249259Sdim
426249259Sdimvoid StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
427249259Sdim  assert(isOpaque() && "Struct body already set!");
428249259Sdim
429249259Sdim  setSubclassData(getSubclassData() | SCDB_HasBody);
430249259Sdim  if (isPacked)
431249259Sdim    setSubclassData(getSubclassData() | SCDB_Packed);
432249259Sdim
433249259Sdim  unsigned NumElements = Elements.size();
434249259Sdim  Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
435249259Sdim  memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
436249259Sdim
437249259Sdim  ContainedTys = Elts;
438249259Sdim  NumContainedTys = NumElements;
439249259Sdim}
440249259Sdim
441249259Sdimvoid StructType::setName(StringRef Name) {
442249259Sdim  if (Name == getName()) return;
443249259Sdim
444249259Sdim  StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
445249259Sdim  typedef StringMap<StructType *>::MapEntryTy EntryTy;
446249259Sdim
447249259Sdim  // If this struct already had a name, remove its symbol table entry. Don't
448249259Sdim  // delete the data yet because it may be part of the new name.
449249259Sdim  if (SymbolTableEntry)
450249259Sdim    SymbolTable.remove((EntryTy *)SymbolTableEntry);
451249259Sdim
452249259Sdim  // If this is just removing the name, we're done.
453249259Sdim  if (Name.empty()) {
454249259Sdim    if (SymbolTableEntry) {
455249259Sdim      // Delete the old string data.
456249259Sdim      ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
457249259Sdim      SymbolTableEntry = 0;
458249259Sdim    }
459249259Sdim    return;
460249259Sdim  }
461249259Sdim
462249259Sdim  // Look up the entry for the name.
463249259Sdim  EntryTy *Entry = &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name);
464249259Sdim
465249259Sdim  // While we have a name collision, try a random rename.
466249259Sdim  if (Entry->getValue()) {
467249259Sdim    SmallString<64> TempStr(Name);
468249259Sdim    TempStr.push_back('.');
469249259Sdim    raw_svector_ostream TmpStream(TempStr);
470249259Sdim    unsigned NameSize = Name.size();
471249259Sdim
472249259Sdim    do {
473249259Sdim      TempStr.resize(NameSize + 1);
474249259Sdim      TmpStream.resync();
475249259Sdim      TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
476249259Sdim
477249259Sdim      Entry = &getContext().pImpl->
478249259Sdim                 NamedStructTypes.GetOrCreateValue(TmpStream.str());
479249259Sdim    } while (Entry->getValue());
480249259Sdim  }
481249259Sdim
482249259Sdim  // Okay, we found an entry that isn't used.  It's us!
483249259Sdim  Entry->setValue(this);
484249259Sdim
485249259Sdim  // Delete the old string data.
486249259Sdim  if (SymbolTableEntry)
487249259Sdim    ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
488249259Sdim  SymbolTableEntry = Entry;
489249259Sdim}
490249259Sdim
491249259Sdim//===----------------------------------------------------------------------===//
492249259Sdim// StructType Helper functions.
493249259Sdim
494249259SdimStructType *StructType::create(LLVMContext &Context, StringRef Name) {
495249259Sdim  StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
496249259Sdim  if (!Name.empty())
497249259Sdim    ST->setName(Name);
498249259Sdim  return ST;
499249259Sdim}
500249259Sdim
501249259SdimStructType *StructType::get(LLVMContext &Context, bool isPacked) {
502252723Sdim  return get(Context, None, isPacked);
503249259Sdim}
504249259Sdim
505249259SdimStructType *StructType::get(Type *type, ...) {
506249259Sdim  assert(type != 0 && "Cannot create a struct type with no elements with this");
507249259Sdim  LLVMContext &Ctx = type->getContext();
508249259Sdim  va_list ap;
509249259Sdim  SmallVector<llvm::Type*, 8> StructFields;
510249259Sdim  va_start(ap, type);
511249259Sdim  while (type) {
512249259Sdim    StructFields.push_back(type);
513249259Sdim    type = va_arg(ap, llvm::Type*);
514249259Sdim  }
515249259Sdim  return llvm::StructType::get(Ctx, StructFields);
516249259Sdim}
517249259Sdim
518249259SdimStructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
519249259Sdim                               StringRef Name, bool isPacked) {
520249259Sdim  StructType *ST = create(Context, Name);
521249259Sdim  ST->setBody(Elements, isPacked);
522249259Sdim  return ST;
523249259Sdim}
524249259Sdim
525249259SdimStructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
526249259Sdim  return create(Context, Elements, StringRef());
527249259Sdim}
528249259Sdim
529249259SdimStructType *StructType::create(LLVMContext &Context) {
530249259Sdim  return create(Context, StringRef());
531249259Sdim}
532249259Sdim
533249259SdimStructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
534249259Sdim                               bool isPacked) {
535249259Sdim  assert(!Elements.empty() &&
536249259Sdim         "This method may not be invoked with an empty list");
537249259Sdim  return create(Elements[0]->getContext(), Elements, Name, isPacked);
538249259Sdim}
539249259Sdim
540249259SdimStructType *StructType::create(ArrayRef<Type*> Elements) {
541249259Sdim  assert(!Elements.empty() &&
542249259Sdim         "This method may not be invoked with an empty list");
543249259Sdim  return create(Elements[0]->getContext(), Elements, StringRef());
544249259Sdim}
545249259Sdim
546249259SdimStructType *StructType::create(StringRef Name, Type *type, ...) {
547249259Sdim  assert(type != 0 && "Cannot create a struct type with no elements with this");
548249259Sdim  LLVMContext &Ctx = type->getContext();
549249259Sdim  va_list ap;
550249259Sdim  SmallVector<llvm::Type*, 8> StructFields;
551249259Sdim  va_start(ap, type);
552249259Sdim  while (type) {
553249259Sdim    StructFields.push_back(type);
554249259Sdim    type = va_arg(ap, llvm::Type*);
555249259Sdim  }
556249259Sdim  return llvm::StructType::create(Ctx, StructFields, Name);
557249259Sdim}
558249259Sdim
559249259Sdimbool StructType::isSized() const {
560249259Sdim  if ((getSubclassData() & SCDB_IsSized) != 0)
561249259Sdim    return true;
562249259Sdim  if (isOpaque())
563249259Sdim    return false;
564249259Sdim
565249259Sdim  // Okay, our struct is sized if all of the elements are, but if one of the
566249259Sdim  // elements is opaque, the struct isn't sized *yet*, but may become sized in
567249259Sdim  // the future, so just bail out without caching.
568249259Sdim  for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
569249259Sdim    if (!(*I)->isSized())
570249259Sdim      return false;
571249259Sdim
572249259Sdim  // Here we cheat a bit and cast away const-ness. The goal is to memoize when
573249259Sdim  // we find a sized type, as types can only move from opaque to sized, not the
574249259Sdim  // other way.
575249259Sdim  const_cast<StructType*>(this)->setSubclassData(
576249259Sdim    getSubclassData() | SCDB_IsSized);
577249259Sdim  return true;
578249259Sdim}
579249259Sdim
580249259SdimStringRef StructType::getName() const {
581249259Sdim  assert(!isLiteral() && "Literal structs never have names");
582249259Sdim  if (SymbolTableEntry == 0) return StringRef();
583249259Sdim
584249259Sdim  return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
585249259Sdim}
586249259Sdim
587249259Sdimvoid StructType::setBody(Type *type, ...) {
588249259Sdim  assert(type != 0 && "Cannot create a struct type with no elements with this");
589249259Sdim  va_list ap;
590249259Sdim  SmallVector<llvm::Type*, 8> StructFields;
591249259Sdim  va_start(ap, type);
592249259Sdim  while (type) {
593249259Sdim    StructFields.push_back(type);
594249259Sdim    type = va_arg(ap, llvm::Type*);
595249259Sdim  }
596249259Sdim  setBody(StructFields);
597249259Sdim}
598249259Sdim
599249259Sdimbool StructType::isValidElementType(Type *ElemTy) {
600249259Sdim  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
601249259Sdim         !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
602249259Sdim}
603249259Sdim
604249259Sdim/// isLayoutIdentical - Return true if this is layout identical to the
605249259Sdim/// specified struct.
606249259Sdimbool StructType::isLayoutIdentical(StructType *Other) const {
607249259Sdim  if (this == Other) return true;
608249259Sdim
609249259Sdim  if (isPacked() != Other->isPacked() ||
610249259Sdim      getNumElements() != Other->getNumElements())
611249259Sdim    return false;
612249259Sdim
613249259Sdim  return std::equal(element_begin(), element_end(), Other->element_begin());
614249259Sdim}
615249259Sdim
616249259Sdim/// getTypeByName - Return the type with the specified name, or null if there
617249259Sdim/// is none by that name.
618249259SdimStructType *Module::getTypeByName(StringRef Name) const {
619263509Sdim  return getContext().pImpl->NamedStructTypes.lookup(Name);
620249259Sdim}
621249259Sdim
622249259Sdim
623249259Sdim//===----------------------------------------------------------------------===//
624249259Sdim//                       CompositeType Implementation
625249259Sdim//===----------------------------------------------------------------------===//
626249259Sdim
627249259SdimType *CompositeType::getTypeAtIndex(const Value *V) {
628249259Sdim  if (StructType *STy = dyn_cast<StructType>(this)) {
629249259Sdim    unsigned Idx =
630249259Sdim      (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
631249259Sdim    assert(indexValid(Idx) && "Invalid structure index!");
632249259Sdim    return STy->getElementType(Idx);
633249259Sdim  }
634249259Sdim
635249259Sdim  return cast<SequentialType>(this)->getElementType();
636249259Sdim}
637249259SdimType *CompositeType::getTypeAtIndex(unsigned Idx) {
638249259Sdim  if (StructType *STy = dyn_cast<StructType>(this)) {
639249259Sdim    assert(indexValid(Idx) && "Invalid structure index!");
640249259Sdim    return STy->getElementType(Idx);
641249259Sdim  }
642249259Sdim
643249259Sdim  return cast<SequentialType>(this)->getElementType();
644249259Sdim}
645249259Sdimbool CompositeType::indexValid(const Value *V) const {
646249259Sdim  if (const StructType *STy = dyn_cast<StructType>(this)) {
647249259Sdim    // Structure indexes require (vectors of) 32-bit integer constants.  In the
648249259Sdim    // vector case all of the indices must be equal.
649249259Sdim    if (!V->getType()->getScalarType()->isIntegerTy(32))
650249259Sdim      return false;
651249259Sdim    const Constant *C = dyn_cast<Constant>(V);
652249259Sdim    if (C && V->getType()->isVectorTy())
653249259Sdim      C = C->getSplatValue();
654249259Sdim    const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
655249259Sdim    return CU && CU->getZExtValue() < STy->getNumElements();
656249259Sdim  }
657249259Sdim
658249259Sdim  // Sequential types can be indexed by any integer.
659249259Sdim  return V->getType()->isIntOrIntVectorTy();
660249259Sdim}
661249259Sdim
662249259Sdimbool CompositeType::indexValid(unsigned Idx) const {
663249259Sdim  if (const StructType *STy = dyn_cast<StructType>(this))
664249259Sdim    return Idx < STy->getNumElements();
665249259Sdim  // Sequential types can be indexed by any integer.
666249259Sdim  return true;
667249259Sdim}
668249259Sdim
669249259Sdim
670249259Sdim//===----------------------------------------------------------------------===//
671249259Sdim//                           ArrayType Implementation
672249259Sdim//===----------------------------------------------------------------------===//
673249259Sdim
674249259SdimArrayType::ArrayType(Type *ElType, uint64_t NumEl)
675249259Sdim  : SequentialType(ArrayTyID, ElType) {
676249259Sdim  NumElements = NumEl;
677249259Sdim}
678249259Sdim
679249259SdimArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
680249259Sdim  Type *ElementType = const_cast<Type*>(elementType);
681249259Sdim  assert(isValidElementType(ElementType) && "Invalid type for array element!");
682249259Sdim
683249259Sdim  LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
684249259Sdim  ArrayType *&Entry =
685249259Sdim    pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
686249259Sdim
687249259Sdim  if (Entry == 0)
688249259Sdim    Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
689249259Sdim  return Entry;
690249259Sdim}
691249259Sdim
692249259Sdimbool ArrayType::isValidElementType(Type *ElemTy) {
693249259Sdim  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
694249259Sdim         !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
695249259Sdim}
696249259Sdim
697249259Sdim//===----------------------------------------------------------------------===//
698249259Sdim//                          VectorType Implementation
699249259Sdim//===----------------------------------------------------------------------===//
700249259Sdim
701249259SdimVectorType::VectorType(Type *ElType, unsigned NumEl)
702249259Sdim  : SequentialType(VectorTyID, ElType) {
703249259Sdim  NumElements = NumEl;
704249259Sdim}
705249259Sdim
706249259SdimVectorType *VectorType::get(Type *elementType, unsigned NumElements) {
707249259Sdim  Type *ElementType = const_cast<Type*>(elementType);
708249259Sdim  assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
709249259Sdim  assert(isValidElementType(ElementType) &&
710249259Sdim         "Elements of a VectorType must be a primitive type");
711249259Sdim
712249259Sdim  LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
713249259Sdim  VectorType *&Entry = ElementType->getContext().pImpl
714249259Sdim    ->VectorTypes[std::make_pair(ElementType, NumElements)];
715249259Sdim
716249259Sdim  if (Entry == 0)
717249259Sdim    Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
718249259Sdim  return Entry;
719249259Sdim}
720249259Sdim
721249259Sdimbool VectorType::isValidElementType(Type *ElemTy) {
722249259Sdim  return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
723249259Sdim    ElemTy->isPointerTy();
724249259Sdim}
725249259Sdim
726249259Sdim//===----------------------------------------------------------------------===//
727249259Sdim//                         PointerType Implementation
728249259Sdim//===----------------------------------------------------------------------===//
729249259Sdim
730249259SdimPointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
731249259Sdim  assert(EltTy && "Can't get a pointer to <null> type!");
732249259Sdim  assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
733249259Sdim
734249259Sdim  LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
735249259Sdim
736249259Sdim  // Since AddressSpace #0 is the common case, we special case it.
737249259Sdim  PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
738249259Sdim     : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
739249259Sdim
740249259Sdim  if (Entry == 0)
741249259Sdim    Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
742249259Sdim  return Entry;
743249259Sdim}
744249259Sdim
745249259Sdim
746249259SdimPointerType::PointerType(Type *E, unsigned AddrSpace)
747249259Sdim  : SequentialType(PointerTyID, E) {
748249259Sdim#ifndef NDEBUG
749249259Sdim  const unsigned oldNCT = NumContainedTys;
750249259Sdim#endif
751249259Sdim  setSubclassData(AddrSpace);
752249259Sdim  // Check for miscompile. PR11652.
753249259Sdim  assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
754249259Sdim}
755249259Sdim
756249259SdimPointerType *Type::getPointerTo(unsigned addrs) {
757249259Sdim  return PointerType::get(this, addrs);
758249259Sdim}
759249259Sdim
760249259Sdimbool PointerType::isValidElementType(Type *ElemTy) {
761249259Sdim  return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
762249259Sdim         !ElemTy->isMetadataTy();
763249259Sdim}
764