2
0

Type.cpp 26 KB

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