Type.cpp 23 KB

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