File: | tools/clang/lib/CodeGen/CodeGenTypes.cpp |
Warning: | line 748, column 29 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// | ||||||||
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 is the code that handles AST -> LLVM type lowering. | ||||||||
10 | // | ||||||||
11 | //===----------------------------------------------------------------------===// | ||||||||
12 | |||||||||
13 | #include "CodeGenTypes.h" | ||||||||
14 | #include "CGCXXABI.h" | ||||||||
15 | #include "CGCall.h" | ||||||||
16 | #include "CGOpenCLRuntime.h" | ||||||||
17 | #include "CGRecordLayout.h" | ||||||||
18 | #include "TargetInfo.h" | ||||||||
19 | #include "clang/AST/ASTContext.h" | ||||||||
20 | #include "clang/AST/DeclCXX.h" | ||||||||
21 | #include "clang/AST/DeclObjC.h" | ||||||||
22 | #include "clang/AST/Expr.h" | ||||||||
23 | #include "clang/AST/RecordLayout.h" | ||||||||
24 | #include "clang/CodeGen/CGFunctionInfo.h" | ||||||||
25 | #include "llvm/IR/DataLayout.h" | ||||||||
26 | #include "llvm/IR/DerivedTypes.h" | ||||||||
27 | #include "llvm/IR/Module.h" | ||||||||
28 | using namespace clang; | ||||||||
29 | using namespace CodeGen; | ||||||||
30 | |||||||||
31 | CodeGenTypes::CodeGenTypes(CodeGenModule &cgm) | ||||||||
32 | : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()), | ||||||||
33 | Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()), | ||||||||
34 | TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) { | ||||||||
35 | SkippedLayout = false; | ||||||||
36 | } | ||||||||
37 | |||||||||
38 | CodeGenTypes::~CodeGenTypes() { | ||||||||
39 | llvm::DeleteContainerSeconds(CGRecordLayouts); | ||||||||
40 | |||||||||
41 | for (llvm::FoldingSet<CGFunctionInfo>::iterator | ||||||||
42 | I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) | ||||||||
43 | delete &*I++; | ||||||||
44 | } | ||||||||
45 | |||||||||
46 | const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const { | ||||||||
47 | return CGM.getCodeGenOpts(); | ||||||||
48 | } | ||||||||
49 | |||||||||
50 | void CodeGenTypes::addRecordTypeName(const RecordDecl *RD, | ||||||||
51 | llvm::StructType *Ty, | ||||||||
52 | StringRef suffix) { | ||||||||
53 | SmallString<256> TypeName; | ||||||||
54 | llvm::raw_svector_ostream OS(TypeName); | ||||||||
55 | OS << RD->getKindName() << '.'; | ||||||||
56 | |||||||||
57 | // Name the codegen type after the typedef name | ||||||||
58 | // if there is no tag type name available | ||||||||
59 | if (RD->getIdentifier()) { | ||||||||
60 | // FIXME: We should not have to check for a null decl context here. | ||||||||
61 | // Right now we do it because the implicit Obj-C decls don't have one. | ||||||||
62 | if (RD->getDeclContext()) | ||||||||
63 | RD->printQualifiedName(OS); | ||||||||
64 | else | ||||||||
65 | RD->printName(OS); | ||||||||
66 | } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { | ||||||||
67 | // FIXME: We should not have to check for a null decl context here. | ||||||||
68 | // Right now we do it because the implicit Obj-C decls don't have one. | ||||||||
69 | if (TDD->getDeclContext()) | ||||||||
70 | TDD->printQualifiedName(OS); | ||||||||
71 | else | ||||||||
72 | TDD->printName(OS); | ||||||||
73 | } else | ||||||||
74 | OS << "anon"; | ||||||||
75 | |||||||||
76 | if (!suffix.empty()) | ||||||||
77 | OS << suffix; | ||||||||
78 | |||||||||
79 | Ty->setName(OS.str()); | ||||||||
80 | } | ||||||||
81 | |||||||||
82 | /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from | ||||||||
83 | /// ConvertType in that it is used to convert to the memory representation for | ||||||||
84 | /// a type. For example, the scalar representation for _Bool is i1, but the | ||||||||
85 | /// memory representation is usually i8 or i32, depending on the target. | ||||||||
86 | llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) { | ||||||||
87 | llvm::Type *R = ConvertType(T); | ||||||||
88 | |||||||||
89 | // If this is a non-bool type, don't map it. | ||||||||
90 | if (!R->isIntegerTy(1)) | ||||||||
91 | return R; | ||||||||
92 | |||||||||
93 | // Otherwise, return an integer of the target-specified size. | ||||||||
94 | return llvm::IntegerType::get(getLLVMContext(), | ||||||||
95 | (unsigned)Context.getTypeSize(T)); | ||||||||
96 | } | ||||||||
97 | |||||||||
98 | |||||||||
99 | /// isRecordLayoutComplete - Return true if the specified type is already | ||||||||
100 | /// completely laid out. | ||||||||
101 | bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { | ||||||||
102 | llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = | ||||||||
103 | RecordDeclTypes.find(Ty); | ||||||||
104 | return I != RecordDeclTypes.end() && !I->second->isOpaque(); | ||||||||
105 | } | ||||||||
106 | |||||||||
107 | static bool | ||||||||
108 | isSafeToConvert(QualType T, CodeGenTypes &CGT, | ||||||||
109 | llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked); | ||||||||
110 | |||||||||
111 | |||||||||
112 | /// isSafeToConvert - Return true if it is safe to convert the specified record | ||||||||
113 | /// decl to IR and lay it out, false if doing so would cause us to get into a | ||||||||
114 | /// recursive compilation mess. | ||||||||
115 | static bool | ||||||||
116 | isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT, | ||||||||
117 | llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { | ||||||||
118 | // If we have already checked this type (maybe the same type is used by-value | ||||||||
119 | // multiple times in multiple structure fields, don't check again. | ||||||||
120 | if (!AlreadyChecked.insert(RD).second) | ||||||||
121 | return true; | ||||||||
122 | |||||||||
123 | const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr(); | ||||||||
124 | |||||||||
125 | // If this type is already laid out, converting it is a noop. | ||||||||
126 | if (CGT.isRecordLayoutComplete(Key)) return true; | ||||||||
127 | |||||||||
128 | // If this type is currently being laid out, we can't recursively compile it. | ||||||||
129 | if (CGT.isRecordBeingLaidOut(Key)) | ||||||||
130 | return false; | ||||||||
131 | |||||||||
132 | // If this type would require laying out bases that are currently being laid | ||||||||
133 | // out, don't do it. This includes virtual base classes which get laid out | ||||||||
134 | // when a class is translated, even though they aren't embedded by-value into | ||||||||
135 | // the class. | ||||||||
136 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { | ||||||||
137 | for (const auto &I : CRD->bases()) | ||||||||
138 | if (!isSafeToConvert(I.getType()->getAs<RecordType>()->getDecl(), | ||||||||
139 | CGT, AlreadyChecked)) | ||||||||
140 | return false; | ||||||||
141 | } | ||||||||
142 | |||||||||
143 | // If this type would require laying out members that are currently being laid | ||||||||
144 | // out, don't do it. | ||||||||
145 | for (const auto *I : RD->fields()) | ||||||||
146 | if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked)) | ||||||||
147 | return false; | ||||||||
148 | |||||||||
149 | // If there are no problems, lets do it. | ||||||||
150 | return true; | ||||||||
151 | } | ||||||||
152 | |||||||||
153 | /// isSafeToConvert - Return true if it is safe to convert this field type, | ||||||||
154 | /// which requires the structure elements contained by-value to all be | ||||||||
155 | /// recursively safe to convert. | ||||||||
156 | static bool | ||||||||
157 | isSafeToConvert(QualType T, CodeGenTypes &CGT, | ||||||||
158 | llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { | ||||||||
159 | // Strip off atomic type sugar. | ||||||||
160 | if (const auto *AT = T->getAs<AtomicType>()) | ||||||||
161 | T = AT->getValueType(); | ||||||||
162 | |||||||||
163 | // If this is a record, check it. | ||||||||
164 | if (const auto *RT = T->getAs<RecordType>()) | ||||||||
165 | return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked); | ||||||||
166 | |||||||||
167 | // If this is an array, check the elements, which are embedded inline. | ||||||||
168 | if (const auto *AT = CGT.getContext().getAsArrayType(T)) | ||||||||
169 | return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked); | ||||||||
170 | |||||||||
171 | // Otherwise, there is no concern about transforming this. We only care about | ||||||||
172 | // things that are contained by-value in a structure that can have another | ||||||||
173 | // structure as a member. | ||||||||
174 | return true; | ||||||||
175 | } | ||||||||
176 | |||||||||
177 | |||||||||
178 | /// isSafeToConvert - Return true if it is safe to convert the specified record | ||||||||
179 | /// decl to IR and lay it out, false if doing so would cause us to get into a | ||||||||
180 | /// recursive compilation mess. | ||||||||
181 | static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) { | ||||||||
182 | // If no structs are being laid out, we can certainly do this one. | ||||||||
183 | if (CGT.noRecordsBeingLaidOut()) return true; | ||||||||
184 | |||||||||
185 | llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked; | ||||||||
186 | return isSafeToConvert(RD, CGT, AlreadyChecked); | ||||||||
187 | } | ||||||||
188 | |||||||||
189 | /// isFuncParamTypeConvertible - Return true if the specified type in a | ||||||||
190 | /// function parameter or result position can be converted to an IR type at this | ||||||||
191 | /// point. This boils down to being whether it is complete, as well as whether | ||||||||
192 | /// we've temporarily deferred expanding the type because we're in a recursive | ||||||||
193 | /// context. | ||||||||
194 | bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) { | ||||||||
195 | // Some ABIs cannot have their member pointers represented in IR unless | ||||||||
196 | // certain circumstances have been reached. | ||||||||
197 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) | ||||||||
198 | return getCXXABI().isMemberPointerConvertible(MPT); | ||||||||
199 | |||||||||
200 | // If this isn't a tagged type, we can convert it! | ||||||||
201 | const TagType *TT = Ty->getAs<TagType>(); | ||||||||
202 | if (!TT) return true; | ||||||||
203 | |||||||||
204 | // Incomplete types cannot be converted. | ||||||||
205 | if (TT->isIncompleteType()) | ||||||||
206 | return false; | ||||||||
207 | |||||||||
208 | // If this is an enum, then it is always safe to convert. | ||||||||
209 | const RecordType *RT = dyn_cast<RecordType>(TT); | ||||||||
210 | if (!RT) return true; | ||||||||
211 | |||||||||
212 | // Otherwise, we have to be careful. If it is a struct that we're in the | ||||||||
213 | // process of expanding, then we can't convert the function type. That's ok | ||||||||
214 | // though because we must be in a pointer context under the struct, so we can | ||||||||
215 | // just convert it to a dummy type. | ||||||||
216 | // | ||||||||
217 | // We decide this by checking whether ConvertRecordDeclType returns us an | ||||||||
218 | // opaque type for a struct that we know is defined. | ||||||||
219 | return isSafeToConvert(RT->getDecl(), *this); | ||||||||
220 | } | ||||||||
221 | |||||||||
222 | |||||||||
223 | /// Code to verify a given function type is complete, i.e. the return type | ||||||||
224 | /// and all of the parameter types are complete. Also check to see if we are in | ||||||||
225 | /// a RS_StructPointer context, and if so whether any struct types have been | ||||||||
226 | /// pended. If so, we don't want to ask the ABI lowering code to handle a type | ||||||||
227 | /// that cannot be converted to an IR type. | ||||||||
228 | bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { | ||||||||
229 | if (!isFuncParamTypeConvertible(FT->getReturnType())) | ||||||||
230 | return false; | ||||||||
231 | |||||||||
232 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) | ||||||||
233 | for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) | ||||||||
234 | if (!isFuncParamTypeConvertible(FPT->getParamType(i))) | ||||||||
235 | return false; | ||||||||
236 | |||||||||
237 | return true; | ||||||||
238 | } | ||||||||
239 | |||||||||
240 | /// UpdateCompletedType - When we find the full definition for a TagDecl, | ||||||||
241 | /// replace the 'opaque' type we previously made for it if applicable. | ||||||||
242 | void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { | ||||||||
243 | // If this is an enum being completed, then we flush all non-struct types from | ||||||||
244 | // the cache. This allows function types and other things that may be derived | ||||||||
245 | // from the enum to be recomputed. | ||||||||
246 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) { | ||||||||
247 | // Only flush the cache if we've actually already converted this type. | ||||||||
248 | if (TypeCache.count(ED->getTypeForDecl())) { | ||||||||
249 | // Okay, we formed some types based on this. We speculated that the enum | ||||||||
250 | // would be lowered to i32, so we only need to flush the cache if this | ||||||||
251 | // didn't happen. | ||||||||
252 | if (!ConvertType(ED->getIntegerType())->isIntegerTy(32)) | ||||||||
253 | TypeCache.clear(); | ||||||||
254 | } | ||||||||
255 | // If necessary, provide the full definition of a type only used with a | ||||||||
256 | // declaration so far. | ||||||||
257 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) | ||||||||
258 | DI->completeType(ED); | ||||||||
259 | return; | ||||||||
260 | } | ||||||||
261 | |||||||||
262 | // If we completed a RecordDecl that we previously used and converted to an | ||||||||
263 | // anonymous type, then go ahead and complete it now. | ||||||||
264 | const RecordDecl *RD = cast<RecordDecl>(TD); | ||||||||
265 | if (RD->isDependentType()) return; | ||||||||
266 | |||||||||
267 | // Only complete it if we converted it already. If we haven't converted it | ||||||||
268 | // yet, we'll just do it lazily. | ||||||||
269 | if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr())) | ||||||||
270 | ConvertRecordDeclType(RD); | ||||||||
271 | |||||||||
272 | // If necessary, provide the full definition of a type only used with a | ||||||||
273 | // declaration so far. | ||||||||
274 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) | ||||||||
275 | DI->completeType(RD); | ||||||||
276 | } | ||||||||
277 | |||||||||
278 | void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { | ||||||||
279 | QualType T = Context.getRecordType(RD); | ||||||||
280 | T = Context.getCanonicalType(T); | ||||||||
281 | |||||||||
282 | const Type *Ty = T.getTypePtr(); | ||||||||
283 | if (RecordsWithOpaqueMemberPointers.count(Ty)) { | ||||||||
284 | TypeCache.clear(); | ||||||||
285 | RecordsWithOpaqueMemberPointers.clear(); | ||||||||
286 | } | ||||||||
287 | } | ||||||||
288 | |||||||||
289 | static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, | ||||||||
290 | const llvm::fltSemantics &format, | ||||||||
291 | bool UseNativeHalf = false) { | ||||||||
292 | if (&format == &llvm::APFloat::IEEEhalf()) { | ||||||||
293 | if (UseNativeHalf) | ||||||||
294 | return llvm::Type::getHalfTy(VMContext); | ||||||||
295 | else | ||||||||
296 | return llvm::Type::getInt16Ty(VMContext); | ||||||||
297 | } | ||||||||
298 | if (&format == &llvm::APFloat::IEEEsingle()) | ||||||||
299 | return llvm::Type::getFloatTy(VMContext); | ||||||||
300 | if (&format == &llvm::APFloat::IEEEdouble()) | ||||||||
301 | return llvm::Type::getDoubleTy(VMContext); | ||||||||
302 | if (&format == &llvm::APFloat::IEEEquad()) | ||||||||
303 | return llvm::Type::getFP128Ty(VMContext); | ||||||||
304 | if (&format == &llvm::APFloat::PPCDoubleDouble()) | ||||||||
305 | return llvm::Type::getPPC_FP128Ty(VMContext); | ||||||||
306 | if (&format == &llvm::APFloat::x87DoubleExtended()) | ||||||||
307 | return llvm::Type::getX86_FP80Ty(VMContext); | ||||||||
308 | llvm_unreachable("Unknown float format!")::llvm::llvm_unreachable_internal("Unknown float format!", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 308); | ||||||||
309 | } | ||||||||
310 | |||||||||
311 | llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) { | ||||||||
312 | assert(QFT.isCanonical())((QFT.isCanonical()) ? static_cast<void> (0) : __assert_fail ("QFT.isCanonical()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 312, __PRETTY_FUNCTION__)); | ||||||||
313 | const Type *Ty = QFT.getTypePtr(); | ||||||||
314 | const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr()); | ||||||||
315 | // First, check whether we can build the full function type. If the | ||||||||
316 | // function type depends on an incomplete type (e.g. a struct or enum), we | ||||||||
317 | // cannot lower the function type. | ||||||||
318 | if (!isFuncTypeConvertible(FT)) { | ||||||||
319 | // This function's type depends on an incomplete tag type. | ||||||||
320 | |||||||||
321 | // Force conversion of all the relevant record types, to make sure | ||||||||
322 | // we re-convert the FunctionType when appropriate. | ||||||||
323 | if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>()) | ||||||||
324 | ConvertRecordDeclType(RT->getDecl()); | ||||||||
325 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) | ||||||||
326 | for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) | ||||||||
327 | if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>()) | ||||||||
328 | ConvertRecordDeclType(RT->getDecl()); | ||||||||
329 | |||||||||
330 | SkippedLayout = true; | ||||||||
331 | |||||||||
332 | // Return a placeholder type. | ||||||||
333 | return llvm::StructType::get(getLLVMContext()); | ||||||||
334 | } | ||||||||
335 | |||||||||
336 | // While we're converting the parameter types for a function, we don't want | ||||||||
337 | // to recursively convert any pointed-to structs. Converting directly-used | ||||||||
338 | // structs is ok though. | ||||||||
339 | if (!RecordsBeingLaidOut.insert(Ty).second) { | ||||||||
340 | SkippedLayout = true; | ||||||||
341 | return llvm::StructType::get(getLLVMContext()); | ||||||||
342 | } | ||||||||
343 | |||||||||
344 | // The function type can be built; call the appropriate routines to | ||||||||
345 | // build it. | ||||||||
346 | const CGFunctionInfo *FI; | ||||||||
347 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { | ||||||||
348 | FI = &arrangeFreeFunctionType( | ||||||||
349 | CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0))); | ||||||||
350 | } else { | ||||||||
351 | const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT); | ||||||||
352 | FI = &arrangeFreeFunctionType( | ||||||||
353 | CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0))); | ||||||||
354 | } | ||||||||
355 | |||||||||
356 | llvm::Type *ResultType = nullptr; | ||||||||
357 | // If there is something higher level prodding our CGFunctionInfo, then | ||||||||
358 | // don't recurse into it again. | ||||||||
359 | if (FunctionsBeingProcessed.count(FI)) { | ||||||||
360 | |||||||||
361 | ResultType = llvm::StructType::get(getLLVMContext()); | ||||||||
362 | SkippedLayout = true; | ||||||||
363 | } else { | ||||||||
364 | |||||||||
365 | // Otherwise, we're good to go, go ahead and convert it. | ||||||||
366 | ResultType = GetFunctionType(*FI); | ||||||||
367 | } | ||||||||
368 | |||||||||
369 | RecordsBeingLaidOut.erase(Ty); | ||||||||
370 | |||||||||
371 | if (SkippedLayout) | ||||||||
372 | TypeCache.clear(); | ||||||||
373 | |||||||||
374 | if (RecordsBeingLaidOut.empty()) | ||||||||
375 | while (!DeferredRecords.empty()) | ||||||||
376 | ConvertRecordDeclType(DeferredRecords.pop_back_val()); | ||||||||
377 | return ResultType; | ||||||||
378 | } | ||||||||
379 | |||||||||
380 | /// ConvertType - Convert the specified type to its LLVM form. | ||||||||
381 | llvm::Type *CodeGenTypes::ConvertType(QualType T) { | ||||||||
382 | T = Context.getCanonicalType(T); | ||||||||
383 | |||||||||
384 | const Type *Ty = T.getTypePtr(); | ||||||||
385 | |||||||||
386 | // RecordTypes are cached and processed specially. | ||||||||
387 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) | ||||||||
388 | return ConvertRecordDeclType(RT->getDecl()); | ||||||||
389 | |||||||||
390 | // See if type is already cached. | ||||||||
391 | llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty); | ||||||||
392 | // If type is found in map then use it. Otherwise, convert type T. | ||||||||
393 | if (TCI != TypeCache.end()) | ||||||||
394 | return TCI->second; | ||||||||
395 | |||||||||
396 | // If we don't have it in the cache, convert it now. | ||||||||
397 | llvm::Type *ResultType = nullptr; | ||||||||
398 | switch (Ty->getTypeClass()) { | ||||||||
399 | case Type::Record: // Handled above. | ||||||||
400 | #define TYPE(Class, Base) | ||||||||
401 | #define ABSTRACT_TYPE(Class, Base) | ||||||||
402 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: | ||||||||
403 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | ||||||||
404 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: | ||||||||
405 | #include "clang/AST/TypeNodes.inc" | ||||||||
406 | llvm_unreachable("Non-canonical or dependent types aren't possible.")::llvm::llvm_unreachable_internal("Non-canonical or dependent types aren't possible." , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 406); | ||||||||
407 | |||||||||
408 | case Type::Builtin: { | ||||||||
409 | switch (cast<BuiltinType>(Ty)->getKind()) { | ||||||||
410 | case BuiltinType::Void: | ||||||||
411 | case BuiltinType::ObjCId: | ||||||||
412 | case BuiltinType::ObjCClass: | ||||||||
413 | case BuiltinType::ObjCSel: | ||||||||
414 | // LLVM void type can only be used as the result of a function call. Just | ||||||||
415 | // map to the same as char. | ||||||||
416 | ResultType = llvm::Type::getInt8Ty(getLLVMContext()); | ||||||||
417 | break; | ||||||||
418 | |||||||||
419 | case BuiltinType::Bool: | ||||||||
420 | // Note that we always return bool as i1 for use as a scalar type. | ||||||||
421 | ResultType = llvm::Type::getInt1Ty(getLLVMContext()); | ||||||||
422 | break; | ||||||||
423 | |||||||||
424 | case BuiltinType::Char_S: | ||||||||
425 | case BuiltinType::Char_U: | ||||||||
426 | case BuiltinType::SChar: | ||||||||
427 | case BuiltinType::UChar: | ||||||||
428 | case BuiltinType::Short: | ||||||||
429 | case BuiltinType::UShort: | ||||||||
430 | case BuiltinType::Int: | ||||||||
431 | case BuiltinType::UInt: | ||||||||
432 | case BuiltinType::Long: | ||||||||
433 | case BuiltinType::ULong: | ||||||||
434 | case BuiltinType::LongLong: | ||||||||
435 | case BuiltinType::ULongLong: | ||||||||
436 | case BuiltinType::WChar_S: | ||||||||
437 | case BuiltinType::WChar_U: | ||||||||
438 | case BuiltinType::Char8: | ||||||||
439 | case BuiltinType::Char16: | ||||||||
440 | case BuiltinType::Char32: | ||||||||
441 | case BuiltinType::ShortAccum: | ||||||||
442 | case BuiltinType::Accum: | ||||||||
443 | case BuiltinType::LongAccum: | ||||||||
444 | case BuiltinType::UShortAccum: | ||||||||
445 | case BuiltinType::UAccum: | ||||||||
446 | case BuiltinType::ULongAccum: | ||||||||
447 | case BuiltinType::ShortFract: | ||||||||
448 | case BuiltinType::Fract: | ||||||||
449 | case BuiltinType::LongFract: | ||||||||
450 | case BuiltinType::UShortFract: | ||||||||
451 | case BuiltinType::UFract: | ||||||||
452 | case BuiltinType::ULongFract: | ||||||||
453 | case BuiltinType::SatShortAccum: | ||||||||
454 | case BuiltinType::SatAccum: | ||||||||
455 | case BuiltinType::SatLongAccum: | ||||||||
456 | case BuiltinType::SatUShortAccum: | ||||||||
457 | case BuiltinType::SatUAccum: | ||||||||
458 | case BuiltinType::SatULongAccum: | ||||||||
459 | case BuiltinType::SatShortFract: | ||||||||
460 | case BuiltinType::SatFract: | ||||||||
461 | case BuiltinType::SatLongFract: | ||||||||
462 | case BuiltinType::SatUShortFract: | ||||||||
463 | case BuiltinType::SatUFract: | ||||||||
464 | case BuiltinType::SatULongFract: | ||||||||
465 | ResultType = llvm::IntegerType::get(getLLVMContext(), | ||||||||
466 | static_cast<unsigned>(Context.getTypeSize(T))); | ||||||||
467 | break; | ||||||||
468 | |||||||||
469 | case BuiltinType::Float16: | ||||||||
470 | ResultType = | ||||||||
471 | getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T), | ||||||||
472 | /* UseNativeHalf = */ true); | ||||||||
473 | break; | ||||||||
474 | |||||||||
475 | case BuiltinType::Half: | ||||||||
476 | // Half FP can either be storage-only (lowered to i16) or native. | ||||||||
477 | ResultType = getTypeForFormat( | ||||||||
478 | getLLVMContext(), Context.getFloatTypeSemantics(T), | ||||||||
479 | Context.getLangOpts().NativeHalfType || | ||||||||
480 | !Context.getTargetInfo().useFP16ConversionIntrinsics()); | ||||||||
481 | break; | ||||||||
482 | case BuiltinType::Float: | ||||||||
483 | case BuiltinType::Double: | ||||||||
484 | case BuiltinType::LongDouble: | ||||||||
485 | case BuiltinType::Float128: | ||||||||
486 | ResultType = getTypeForFormat(getLLVMContext(), | ||||||||
487 | Context.getFloatTypeSemantics(T), | ||||||||
488 | /* UseNativeHalf = */ false); | ||||||||
489 | break; | ||||||||
490 | |||||||||
491 | case BuiltinType::NullPtr: | ||||||||
492 | // Model std::nullptr_t as i8* | ||||||||
493 | ResultType = llvm::Type::getInt8PtrTy(getLLVMContext()); | ||||||||
494 | break; | ||||||||
495 | |||||||||
496 | case BuiltinType::UInt128: | ||||||||
497 | case BuiltinType::Int128: | ||||||||
498 | ResultType = llvm::IntegerType::get(getLLVMContext(), 128); | ||||||||
499 | break; | ||||||||
500 | |||||||||
501 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | ||||||||
502 | case BuiltinType::Id: | ||||||||
503 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||||
504 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | ||||||||
505 | case BuiltinType::Id: | ||||||||
506 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||||
507 | case BuiltinType::OCLSampler: | ||||||||
508 | case BuiltinType::OCLEvent: | ||||||||
509 | case BuiltinType::OCLClkEvent: | ||||||||
510 | case BuiltinType::OCLQueue: | ||||||||
511 | case BuiltinType::OCLReserveID: | ||||||||
512 | ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty); | ||||||||
513 | break; | ||||||||
514 | |||||||||
515 | // TODO: real CodeGen support for SVE types requires more infrastructure | ||||||||
516 | // to be added first. Report an error until then. | ||||||||
517 | #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: | ||||||||
518 | #include "clang/Basic/AArch64SVEACLETypes.def" | ||||||||
519 | { | ||||||||
520 | unsigned DiagID = CGM.getDiags().getCustomDiagID( | ||||||||
521 | DiagnosticsEngine::Error, | ||||||||
522 | "cannot yet generate code for SVE type '%0'"); | ||||||||
523 | auto *BT = cast<BuiltinType>(Ty); | ||||||||
524 | auto Name = BT->getName(CGM.getContext().getPrintingPolicy()); | ||||||||
525 | CGM.getDiags().Report(DiagID) << Name; | ||||||||
526 | // Return something safe. | ||||||||
527 | ResultType = llvm::IntegerType::get(getLLVMContext(), 32); | ||||||||
528 | break; | ||||||||
529 | } | ||||||||
530 | |||||||||
531 | case BuiltinType::Dependent: | ||||||||
532 | #define BUILTIN_TYPE(Id, SingletonId) | ||||||||
533 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ | ||||||||
534 | case BuiltinType::Id: | ||||||||
535 | #include "clang/AST/BuiltinTypes.def" | ||||||||
536 | llvm_unreachable("Unexpected placeholder builtin type!")::llvm::llvm_unreachable_internal("Unexpected placeholder builtin type!" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 536); | ||||||||
537 | } | ||||||||
538 | break; | ||||||||
539 | } | ||||||||
540 | case Type::Auto: | ||||||||
541 | case Type::DeducedTemplateSpecialization: | ||||||||
542 | llvm_unreachable("Unexpected undeduced type!")::llvm::llvm_unreachable_internal("Unexpected undeduced type!" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 542); | ||||||||
543 | case Type::Complex: { | ||||||||
544 | llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType()); | ||||||||
545 | ResultType = llvm::StructType::get(EltTy, EltTy); | ||||||||
546 | break; | ||||||||
547 | } | ||||||||
548 | case Type::LValueReference: | ||||||||
549 | case Type::RValueReference: { | ||||||||
550 | const ReferenceType *RTy = cast<ReferenceType>(Ty); | ||||||||
551 | QualType ETy = RTy->getPointeeType(); | ||||||||
552 | llvm::Type *PointeeType = ConvertTypeForMem(ETy); | ||||||||
553 | unsigned AS = Context.getTargetAddressSpace(ETy); | ||||||||
554 | ResultType = llvm::PointerType::get(PointeeType, AS); | ||||||||
555 | break; | ||||||||
556 | } | ||||||||
557 | case Type::Pointer: { | ||||||||
558 | const PointerType *PTy = cast<PointerType>(Ty); | ||||||||
559 | QualType ETy = PTy->getPointeeType(); | ||||||||
560 | llvm::Type *PointeeType = ConvertTypeForMem(ETy); | ||||||||
561 | if (PointeeType->isVoidTy()) | ||||||||
562 | PointeeType = llvm::Type::getInt8Ty(getLLVMContext()); | ||||||||
563 | unsigned AS = Context.getTargetAddressSpace(ETy); | ||||||||
564 | ResultType = llvm::PointerType::get(PointeeType, AS); | ||||||||
565 | break; | ||||||||
566 | } | ||||||||
567 | |||||||||
568 | case Type::VariableArray: { | ||||||||
569 | const VariableArrayType *A = cast<VariableArrayType>(Ty); | ||||||||
570 | assert(A->getIndexTypeCVRQualifiers() == 0 &&((A->getIndexTypeCVRQualifiers() == 0 && "FIXME: We only handle trivial array types so far!" ) ? static_cast<void> (0) : __assert_fail ("A->getIndexTypeCVRQualifiers() == 0 && \"FIXME: We only handle trivial array types so far!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 571, __PRETTY_FUNCTION__)) | ||||||||
571 | "FIXME: We only handle trivial array types so far!")((A->getIndexTypeCVRQualifiers() == 0 && "FIXME: We only handle trivial array types so far!" ) ? static_cast<void> (0) : __assert_fail ("A->getIndexTypeCVRQualifiers() == 0 && \"FIXME: We only handle trivial array types so far!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 571, __PRETTY_FUNCTION__)); | ||||||||
572 | // VLAs resolve to the innermost element type; this matches | ||||||||
573 | // the return of alloca, and there isn't any obviously better choice. | ||||||||
574 | ResultType = ConvertTypeForMem(A->getElementType()); | ||||||||
575 | break; | ||||||||
576 | } | ||||||||
577 | case Type::IncompleteArray: { | ||||||||
578 | const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); | ||||||||
579 | assert(A->getIndexTypeCVRQualifiers() == 0 &&((A->getIndexTypeCVRQualifiers() == 0 && "FIXME: We only handle trivial array types so far!" ) ? static_cast<void> (0) : __assert_fail ("A->getIndexTypeCVRQualifiers() == 0 && \"FIXME: We only handle trivial array types so far!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 580, __PRETTY_FUNCTION__)) | ||||||||
580 | "FIXME: We only handle trivial array types so far!")((A->getIndexTypeCVRQualifiers() == 0 && "FIXME: We only handle trivial array types so far!" ) ? static_cast<void> (0) : __assert_fail ("A->getIndexTypeCVRQualifiers() == 0 && \"FIXME: We only handle trivial array types so far!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 580, __PRETTY_FUNCTION__)); | ||||||||
581 | // int X[] -> [0 x int], unless the element type is not sized. If it is | ||||||||
582 | // unsized (e.g. an incomplete struct) just use [0 x i8]. | ||||||||
583 | ResultType = ConvertTypeForMem(A->getElementType()); | ||||||||
584 | if (!ResultType->isSized()) { | ||||||||
585 | SkippedLayout = true; | ||||||||
586 | ResultType = llvm::Type::getInt8Ty(getLLVMContext()); | ||||||||
587 | } | ||||||||
588 | ResultType = llvm::ArrayType::get(ResultType, 0); | ||||||||
589 | break; | ||||||||
590 | } | ||||||||
591 | case Type::ConstantArray: { | ||||||||
592 | const ConstantArrayType *A = cast<ConstantArrayType>(Ty); | ||||||||
593 | llvm::Type *EltTy = ConvertTypeForMem(A->getElementType()); | ||||||||
594 | |||||||||
595 | // Lower arrays of undefined struct type to arrays of i8 just to have a | ||||||||
596 | // concrete type. | ||||||||
597 | if (!EltTy->isSized()) { | ||||||||
598 | SkippedLayout = true; | ||||||||
599 | EltTy = llvm::Type::getInt8Ty(getLLVMContext()); | ||||||||
600 | } | ||||||||
601 | |||||||||
602 | ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue()); | ||||||||
603 | break; | ||||||||
604 | } | ||||||||
605 | case Type::ExtVector: | ||||||||
606 | case Type::Vector: { | ||||||||
607 | const VectorType *VT = cast<VectorType>(Ty); | ||||||||
608 | ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()), | ||||||||
609 | VT->getNumElements()); | ||||||||
610 | break; | ||||||||
611 | } | ||||||||
612 | case Type::FunctionNoProto: | ||||||||
613 | case Type::FunctionProto: | ||||||||
614 | ResultType = ConvertFunctionTypeInternal(T); | ||||||||
615 | break; | ||||||||
616 | case Type::ObjCObject: | ||||||||
617 | ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType()); | ||||||||
618 | break; | ||||||||
619 | |||||||||
620 | case Type::ObjCInterface: { | ||||||||
621 | // Objective-C interfaces are always opaque (outside of the | ||||||||
622 | // runtime, which can do whatever it likes); we never refine | ||||||||
623 | // these. | ||||||||
624 | llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; | ||||||||
625 | if (!T) | ||||||||
626 | T = llvm::StructType::create(getLLVMContext()); | ||||||||
627 | ResultType = T; | ||||||||
628 | break; | ||||||||
629 | } | ||||||||
630 | |||||||||
631 | case Type::ObjCObjectPointer: { | ||||||||
632 | // Protocol qualifications do not influence the LLVM type, we just return a | ||||||||
633 | // pointer to the underlying interface type. We don't need to worry about | ||||||||
634 | // recursive conversion. | ||||||||
635 | llvm::Type *T = | ||||||||
636 | ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); | ||||||||
637 | ResultType = T->getPointerTo(); | ||||||||
638 | break; | ||||||||
639 | } | ||||||||
640 | |||||||||
641 | case Type::Enum: { | ||||||||
642 | const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); | ||||||||
643 | if (ED->isCompleteDefinition() || ED->isFixed()) | ||||||||
644 | return ConvertType(ED->getIntegerType()); | ||||||||
645 | // Return a placeholder 'i32' type. This can be changed later when the | ||||||||
646 | // type is defined (see UpdateCompletedType), but is likely to be the | ||||||||
647 | // "right" answer. | ||||||||
648 | ResultType = llvm::Type::getInt32Ty(getLLVMContext()); | ||||||||
649 | break; | ||||||||
650 | } | ||||||||
651 | |||||||||
652 | case Type::BlockPointer: { | ||||||||
653 | const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); | ||||||||
654 | llvm::Type *PointeeType = CGM.getLangOpts().OpenCL | ||||||||
655 | ? CGM.getGenericBlockLiteralType() | ||||||||
656 | : ConvertTypeForMem(FTy); | ||||||||
657 | unsigned AS = Context.getTargetAddressSpace(FTy); | ||||||||
658 | ResultType = llvm::PointerType::get(PointeeType, AS); | ||||||||
659 | break; | ||||||||
660 | } | ||||||||
661 | |||||||||
662 | case Type::MemberPointer: { | ||||||||
663 | auto *MPTy = cast<MemberPointerType>(Ty); | ||||||||
664 | if (!getCXXABI().isMemberPointerConvertible(MPTy)) { | ||||||||
665 | RecordsWithOpaqueMemberPointers.insert(MPTy->getClass()); | ||||||||
666 | ResultType = llvm::StructType::create(getLLVMContext()); | ||||||||
667 | } else { | ||||||||
668 | ResultType = getCXXABI().ConvertMemberPointerType(MPTy); | ||||||||
669 | } | ||||||||
670 | break; | ||||||||
671 | } | ||||||||
672 | |||||||||
673 | case Type::Atomic: { | ||||||||
674 | QualType valueType = cast<AtomicType>(Ty)->getValueType(); | ||||||||
675 | ResultType = ConvertTypeForMem(valueType); | ||||||||
676 | |||||||||
677 | // Pad out to the inflated size if necessary. | ||||||||
678 | uint64_t valueSize = Context.getTypeSize(valueType); | ||||||||
679 | uint64_t atomicSize = Context.getTypeSize(Ty); | ||||||||
680 | if (valueSize != atomicSize) { | ||||||||
681 | assert(valueSize < atomicSize)((valueSize < atomicSize) ? static_cast<void> (0) : __assert_fail ("valueSize < atomicSize", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 681, __PRETTY_FUNCTION__)); | ||||||||
682 | llvm::Type *elts[] = { | ||||||||
683 | ResultType, | ||||||||
684 | llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8) | ||||||||
685 | }; | ||||||||
686 | ResultType = llvm::StructType::get(getLLVMContext(), | ||||||||
687 | llvm::makeArrayRef(elts)); | ||||||||
688 | } | ||||||||
689 | break; | ||||||||
690 | } | ||||||||
691 | case Type::Pipe: { | ||||||||
692 | ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty)); | ||||||||
693 | break; | ||||||||
694 | } | ||||||||
695 | } | ||||||||
696 | |||||||||
697 | assert(ResultType && "Didn't convert a type?")((ResultType && "Didn't convert a type?") ? static_cast <void> (0) : __assert_fail ("ResultType && \"Didn't convert a type?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 697, __PRETTY_FUNCTION__)); | ||||||||
698 | |||||||||
699 | TypeCache[Ty] = ResultType; | ||||||||
700 | return ResultType; | ||||||||
701 | } | ||||||||
702 | |||||||||
703 | bool CodeGenModule::isPaddedAtomicType(QualType type) { | ||||||||
704 | return isPaddedAtomicType(type->castAs<AtomicType>()); | ||||||||
705 | } | ||||||||
706 | |||||||||
707 | bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { | ||||||||
708 | return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType()); | ||||||||
709 | } | ||||||||
710 | |||||||||
711 | /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. | ||||||||
712 | llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { | ||||||||
713 | // TagDecl's are not necessarily unique, instead use the (clang) | ||||||||
714 | // type connected to the decl. | ||||||||
715 | const Type *Key = Context.getTagDeclType(RD).getTypePtr(); | ||||||||
716 | |||||||||
717 | llvm::StructType *&Entry = RecordDeclTypes[Key]; | ||||||||
718 | |||||||||
719 | // If we don't have a StructType at all yet, create the forward declaration. | ||||||||
720 | if (!Entry) { | ||||||||
721 | Entry = llvm::StructType::create(getLLVMContext()); | ||||||||
722 | addRecordTypeName(RD, Entry, ""); | ||||||||
723 | } | ||||||||
724 | llvm::StructType *Ty = Entry; | ||||||||
725 | |||||||||
726 | // If this is still a forward declaration, or the LLVM type is already | ||||||||
727 | // complete, there's nothing more to do. | ||||||||
728 | RD = RD->getDefinition(); | ||||||||
729 | if (!RD
| ||||||||
730 | return Ty; | ||||||||
731 | |||||||||
732 | // If converting this type would cause us to infinitely loop, don't do it! | ||||||||
733 | if (!isSafeToConvert(RD, *this)) { | ||||||||
734 | DeferredRecords.push_back(RD); | ||||||||
735 | return Ty; | ||||||||
736 | } | ||||||||
737 | |||||||||
738 | // Okay, this is a definition of a type. Compile the implementation now. | ||||||||
739 | bool InsertResult = RecordsBeingLaidOut.insert(Key).second; | ||||||||
740 | (void)InsertResult; | ||||||||
741 | assert(InsertResult && "Recursively compiling a struct?")((InsertResult && "Recursively compiling a struct?") ? static_cast<void> (0) : __assert_fail ("InsertResult && \"Recursively compiling a struct?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 741, __PRETTY_FUNCTION__)); | ||||||||
742 | |||||||||
743 | // Force conversion of non-virtual base classes recursively. | ||||||||
744 | if (const CXXRecordDecl *CRD
| ||||||||
745 | for (const auto &I : CRD->bases()) { | ||||||||
746 | if (I.isVirtual()) continue; | ||||||||
747 | |||||||||
748 | ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl()); | ||||||||
| |||||||||
749 | } | ||||||||
750 | } | ||||||||
751 | |||||||||
752 | // Layout fields. | ||||||||
753 | CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty); | ||||||||
754 | CGRecordLayouts[Key] = Layout; | ||||||||
755 | |||||||||
756 | // We're done laying out this struct. | ||||||||
757 | bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult; | ||||||||
758 | assert(EraseResult && "struct not in RecordsBeingLaidOut set?")((EraseResult && "struct not in RecordsBeingLaidOut set?" ) ? static_cast<void> (0) : __assert_fail ("EraseResult && \"struct not in RecordsBeingLaidOut set?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 758, __PRETTY_FUNCTION__)); | ||||||||
759 | |||||||||
760 | // If this struct blocked a FunctionType conversion, then recompute whatever | ||||||||
761 | // was derived from that. | ||||||||
762 | // FIXME: This is hugely overconservative. | ||||||||
763 | if (SkippedLayout) | ||||||||
764 | TypeCache.clear(); | ||||||||
765 | |||||||||
766 | // If we're done converting the outer-most record, then convert any deferred | ||||||||
767 | // structs as well. | ||||||||
768 | if (RecordsBeingLaidOut.empty()) | ||||||||
769 | while (!DeferredRecords.empty()) | ||||||||
770 | ConvertRecordDeclType(DeferredRecords.pop_back_val()); | ||||||||
771 | |||||||||
772 | return Ty; | ||||||||
773 | } | ||||||||
774 | |||||||||
775 | /// getCGRecordLayout - Return record layout info for the given record decl. | ||||||||
776 | const CGRecordLayout & | ||||||||
777 | CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { | ||||||||
778 | const Type *Key = Context.getTagDeclType(RD).getTypePtr(); | ||||||||
779 | |||||||||
780 | const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key); | ||||||||
781 | if (!Layout) { | ||||||||
782 | // Compute the type information. | ||||||||
783 | ConvertRecordDeclType(RD); | ||||||||
784 | |||||||||
785 | // Now try again. | ||||||||
786 | Layout = CGRecordLayouts.lookup(Key); | ||||||||
787 | } | ||||||||
788 | |||||||||
789 | assert(Layout && "Unable to find record layout information for type")((Layout && "Unable to find record layout information for type" ) ? static_cast<void> (0) : __assert_fail ("Layout && \"Unable to find record layout information for type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 789, __PRETTY_FUNCTION__)); | ||||||||
790 | return *Layout; | ||||||||
791 | } | ||||||||
792 | |||||||||
793 | bool CodeGenTypes::isPointerZeroInitializable(QualType T) { | ||||||||
794 | assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type")(((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type") ? static_cast<void> (0) : __assert_fail ("(T->isAnyPointerType() || T->isBlockPointerType()) && \"Invalid type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenTypes.cpp" , 794, __PRETTY_FUNCTION__)); | ||||||||
| |||||||||
795 | return isZeroInitializable(T); | ||||||||
796 | } | ||||||||
797 | |||||||||
798 | bool CodeGenTypes::isZeroInitializable(QualType T) { | ||||||||
799 | if (T->getAs<PointerType>()) | ||||||||
800 | return Context.getTargetNullPointerValue(T) == 0; | ||||||||
801 | |||||||||
802 | if (const auto *AT = Context.getAsArrayType(T)) { | ||||||||
803 | if (isa<IncompleteArrayType>(AT)) | ||||||||
804 | return true; | ||||||||
805 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) | ||||||||
806 | if (Context.getConstantArrayElementCount(CAT) == 0) | ||||||||
807 | return true; | ||||||||
808 | T = Context.getBaseElementType(T); | ||||||||
809 | } | ||||||||
810 | |||||||||
811 | // Records are non-zero-initializable if they contain any | ||||||||
812 | // non-zero-initializable subobjects. | ||||||||
813 | if (const RecordType *RT = T->getAs<RecordType>()) { | ||||||||
814 | const RecordDecl *RD = RT->getDecl(); | ||||||||
815 | return isZeroInitializable(RD); | ||||||||
816 | } | ||||||||
817 | |||||||||
818 | // We have to ask the ABI about member pointers. | ||||||||
819 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) | ||||||||
820 | return getCXXABI().isZeroInitializable(MPT); | ||||||||
821 | |||||||||
822 | // Everything else is okay. | ||||||||
823 | return true; | ||||||||
824 | } | ||||||||
825 | |||||||||
826 | bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) { | ||||||||
827 | return getCGRecordLayout(RD).isZeroInitializable(); | ||||||||
828 | } |
1 | //===- llvm/DerivedTypes.h - Classes for handling data types ----*- C++ -*-===// |
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 contains the declarations of classes that represent "derived |
10 | // types". These are things like "arrays of x" or "structure of x, y, z" or |
11 | // "function returning x taking (y,z) as parameters", etc... |
12 | // |
13 | // The implementations of these classes live in the Type.cpp file. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #ifndef LLVM_IR_DERIVEDTYPES_H |
18 | #define LLVM_IR_DERIVEDTYPES_H |
19 | |
20 | #include "llvm/ADT/ArrayRef.h" |
21 | #include "llvm/ADT/STLExtras.h" |
22 | #include "llvm/ADT/StringRef.h" |
23 | #include "llvm/IR/Type.h" |
24 | #include "llvm/Support/Casting.h" |
25 | #include "llvm/Support/Compiler.h" |
26 | #include "llvm/Support/ScalableSize.h" |
27 | #include <cassert> |
28 | #include <cstdint> |
29 | |
30 | namespace llvm { |
31 | |
32 | class Value; |
33 | class APInt; |
34 | class LLVMContext; |
35 | |
36 | /// Class to represent integer types. Note that this class is also used to |
37 | /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and |
38 | /// Int64Ty. |
39 | /// Integer representation type |
40 | class IntegerType : public Type { |
41 | friend class LLVMContextImpl; |
42 | |
43 | protected: |
44 | explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){ |
45 | setSubclassData(NumBits); |
46 | } |
47 | |
48 | public: |
49 | /// This enum is just used to hold constants we need for IntegerType. |
50 | enum { |
51 | MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified |
52 | MAX_INT_BITS = (1<<24)-1 ///< Maximum number of bits that can be specified |
53 | ///< Note that bit width is stored in the Type classes SubclassData field |
54 | ///< which has 24 bits. This yields a maximum bit width of 16,777,215 |
55 | ///< bits. |
56 | }; |
57 | |
58 | /// This static method is the primary way of constructing an IntegerType. |
59 | /// If an IntegerType with the same NumBits value was previously instantiated, |
60 | /// that instance will be returned. Otherwise a new one will be created. Only |
61 | /// one instance with a given NumBits value is ever created. |
62 | /// Get or create an IntegerType instance. |
63 | static IntegerType *get(LLVMContext &C, unsigned NumBits); |
64 | |
65 | /// Returns type twice as wide the input type. |
66 | IntegerType *getExtendedType() const { |
67 | return Type::getIntNTy(getContext(), 2 * getScalarSizeInBits()); |
68 | } |
69 | |
70 | /// Get the number of bits in this IntegerType |
71 | unsigned getBitWidth() const { return getSubclassData(); } |
72 | |
73 | /// Return a bitmask with ones set for all of the bits that can be set by an |
74 | /// unsigned version of this type. This is 0xFF for i8, 0xFFFF for i16, etc. |
75 | uint64_t getBitMask() const { |
76 | return ~uint64_t(0UL) >> (64-getBitWidth()); |
77 | } |
78 | |
79 | /// Return a uint64_t with just the most significant bit set (the sign bit, if |
80 | /// the value is treated as a signed number). |
81 | uint64_t getSignBit() const { |
82 | return 1ULL << (getBitWidth()-1); |
83 | } |
84 | |
85 | /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc. |
86 | /// @returns a bit mask with ones set for all the bits of this type. |
87 | /// Get a bit mask for this type. |
88 | APInt getMask() const; |
89 | |
90 | /// This method determines if the width of this IntegerType is a power-of-2 |
91 | /// in terms of 8 bit bytes. |
92 | /// @returns true if this is a power-of-2 byte width. |
93 | /// Is this a power-of-2 byte-width IntegerType ? |
94 | bool isPowerOf2ByteWidth() const; |
95 | |
96 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
97 | static bool classof(const Type *T) { |
98 | return T->getTypeID() == IntegerTyID; |
99 | } |
100 | }; |
101 | |
102 | unsigned Type::getIntegerBitWidth() const { |
103 | return cast<IntegerType>(this)->getBitWidth(); |
104 | } |
105 | |
106 | /// Class to represent function types |
107 | /// |
108 | class FunctionType : public Type { |
109 | FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs); |
110 | |
111 | public: |
112 | FunctionType(const FunctionType &) = delete; |
113 | FunctionType &operator=(const FunctionType &) = delete; |
114 | |
115 | /// This static method is the primary way of constructing a FunctionType. |
116 | static FunctionType *get(Type *Result, |
117 | ArrayRef<Type*> Params, bool isVarArg); |
118 | |
119 | /// Create a FunctionType taking no parameters. |
120 | static FunctionType *get(Type *Result, bool isVarArg); |
121 | |
122 | /// Return true if the specified type is valid as a return type. |
123 | static bool isValidReturnType(Type *RetTy); |
124 | |
125 | /// Return true if the specified type is valid as an argument type. |
126 | static bool isValidArgumentType(Type *ArgTy); |
127 | |
128 | bool isVarArg() const { return getSubclassData()!=0; } |
129 | Type *getReturnType() const { return ContainedTys[0]; } |
130 | |
131 | using param_iterator = Type::subtype_iterator; |
132 | |
133 | param_iterator param_begin() const { return ContainedTys + 1; } |
134 | param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } |
135 | ArrayRef<Type *> params() const { |
136 | return makeArrayRef(param_begin(), param_end()); |
137 | } |
138 | |
139 | /// Parameter type accessors. |
140 | Type *getParamType(unsigned i) const { return ContainedTys[i+1]; } |
141 | |
142 | /// Return the number of fixed parameters this function type requires. |
143 | /// This does not consider varargs. |
144 | unsigned getNumParams() const { return NumContainedTys - 1; } |
145 | |
146 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
147 | static bool classof(const Type *T) { |
148 | return T->getTypeID() == FunctionTyID; |
149 | } |
150 | }; |
151 | static_assert(alignof(FunctionType) >= alignof(Type *), |
152 | "Alignment sufficient for objects appended to FunctionType"); |
153 | |
154 | bool Type::isFunctionVarArg() const { |
155 | return cast<FunctionType>(this)->isVarArg(); |
156 | } |
157 | |
158 | Type *Type::getFunctionParamType(unsigned i) const { |
159 | return cast<FunctionType>(this)->getParamType(i); |
160 | } |
161 | |
162 | unsigned Type::getFunctionNumParams() const { |
163 | return cast<FunctionType>(this)->getNumParams(); |
164 | } |
165 | |
166 | /// A handy container for a FunctionType+Callee-pointer pair, which can be |
167 | /// passed around as a single entity. This assists in replacing the use of |
168 | /// PointerType::getElementType() to access the function's type, since that's |
169 | /// slated for removal as part of the [opaque pointer types] project. |
170 | class FunctionCallee { |
171 | public: |
172 | // Allow implicit conversion from types which have a getFunctionType member |
173 | // (e.g. Function and InlineAsm). |
174 | template <typename T, typename U = decltype(&T::getFunctionType)> |
175 | FunctionCallee(T *Fn) |
176 | : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {} |
177 | |
178 | FunctionCallee(FunctionType *FnTy, Value *Callee) |
179 | : FnTy(FnTy), Callee(Callee) { |
180 | assert((FnTy == nullptr) == (Callee == nullptr))(((FnTy == nullptr) == (Callee == nullptr)) ? static_cast< void> (0) : __assert_fail ("(FnTy == nullptr) == (Callee == nullptr)" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 180, __PRETTY_FUNCTION__)); |
181 | } |
182 | |
183 | FunctionCallee(std::nullptr_t) {} |
184 | |
185 | FunctionCallee() = default; |
186 | |
187 | FunctionType *getFunctionType() { return FnTy; } |
188 | |
189 | Value *getCallee() { return Callee; } |
190 | |
191 | explicit operator bool() { return Callee; } |
192 | |
193 | private: |
194 | FunctionType *FnTy = nullptr; |
195 | Value *Callee = nullptr; |
196 | }; |
197 | |
198 | /// Common super class of ArrayType, StructType and VectorType. |
199 | class CompositeType : public Type { |
200 | protected: |
201 | explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {} |
202 | |
203 | public: |
204 | /// Given an index value into the type, return the type of the element. |
205 | Type *getTypeAtIndex(const Value *V) const; |
206 | Type *getTypeAtIndex(unsigned Idx) const; |
207 | bool indexValid(const Value *V) const; |
208 | bool indexValid(unsigned Idx) const; |
209 | |
210 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
211 | static bool classof(const Type *T) { |
212 | return T->getTypeID() == ArrayTyID || |
213 | T->getTypeID() == StructTyID || |
214 | T->getTypeID() == VectorTyID; |
215 | } |
216 | }; |
217 | |
218 | /// Class to represent struct types. There are two different kinds of struct |
219 | /// types: Literal structs and Identified structs. |
220 | /// |
221 | /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must |
222 | /// always have a body when created. You can get one of these by using one of |
223 | /// the StructType::get() forms. |
224 | /// |
225 | /// Identified structs (e.g. %foo or %42) may optionally have a name and are not |
226 | /// uniqued. The names for identified structs are managed at the LLVMContext |
227 | /// level, so there can only be a single identified struct with a given name in |
228 | /// a particular LLVMContext. Identified structs may also optionally be opaque |
229 | /// (have no body specified). You get one of these by using one of the |
230 | /// StructType::create() forms. |
231 | /// |
232 | /// Independent of what kind of struct you have, the body of a struct type are |
233 | /// laid out in memory consecutively with the elements directly one after the |
234 | /// other (if the struct is packed) or (if not packed) with padding between the |
235 | /// elements as defined by DataLayout (which is required to match what the code |
236 | /// generator for a target expects). |
237 | /// |
238 | class StructType : public CompositeType { |
239 | StructType(LLVMContext &C) : CompositeType(C, StructTyID) {} |
240 | |
241 | enum { |
242 | /// This is the contents of the SubClassData field. |
243 | SCDB_HasBody = 1, |
244 | SCDB_Packed = 2, |
245 | SCDB_IsLiteral = 4, |
246 | SCDB_IsSized = 8 |
247 | }; |
248 | |
249 | /// For a named struct that actually has a name, this is a pointer to the |
250 | /// symbol table entry (maintained by LLVMContext) for the struct. |
251 | /// This is null if the type is an literal struct or if it is a identified |
252 | /// type that has an empty name. |
253 | void *SymbolTableEntry = nullptr; |
254 | |
255 | public: |
256 | StructType(const StructType &) = delete; |
257 | StructType &operator=(const StructType &) = delete; |
258 | |
259 | /// This creates an identified struct. |
260 | static StructType *create(LLVMContext &Context, StringRef Name); |
261 | static StructType *create(LLVMContext &Context); |
262 | |
263 | static StructType *create(ArrayRef<Type *> Elements, StringRef Name, |
264 | bool isPacked = false); |
265 | static StructType *create(ArrayRef<Type *> Elements); |
266 | static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements, |
267 | StringRef Name, bool isPacked = false); |
268 | static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements); |
269 | template <class... Tys> |
270 | static typename std::enable_if<are_base_of<Type, Tys...>::value, |
271 | StructType *>::type |
272 | create(StringRef Name, Type *elt1, Tys *... elts) { |
273 | assert(elt1 && "Cannot create a struct type with no elements with this")((elt1 && "Cannot create a struct type with no elements with this" ) ? static_cast<void> (0) : __assert_fail ("elt1 && \"Cannot create a struct type with no elements with this\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 273, __PRETTY_FUNCTION__)); |
274 | SmallVector<llvm::Type *, 8> StructFields({elt1, elts...}); |
275 | return create(StructFields, Name); |
276 | } |
277 | |
278 | /// This static method is the primary way to create a literal StructType. |
279 | static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements, |
280 | bool isPacked = false); |
281 | |
282 | /// Create an empty structure type. |
283 | static StructType *get(LLVMContext &Context, bool isPacked = false); |
284 | |
285 | /// This static method is a convenience method for creating structure types by |
286 | /// specifying the elements as arguments. Note that this method always returns |
287 | /// a non-packed struct, and requires at least one element type. |
288 | template <class... Tys> |
289 | static typename std::enable_if<are_base_of<Type, Tys...>::value, |
290 | StructType *>::type |
291 | get(Type *elt1, Tys *... elts) { |
292 | assert(elt1 && "Cannot create a struct type with no elements with this")((elt1 && "Cannot create a struct type with no elements with this" ) ? static_cast<void> (0) : __assert_fail ("elt1 && \"Cannot create a struct type with no elements with this\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 292, __PRETTY_FUNCTION__)); |
293 | LLVMContext &Ctx = elt1->getContext(); |
294 | SmallVector<llvm::Type *, 8> StructFields({elt1, elts...}); |
295 | return llvm::StructType::get(Ctx, StructFields); |
296 | } |
297 | |
298 | bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } |
299 | |
300 | /// Return true if this type is uniqued by structural equivalence, false if it |
301 | /// is a struct definition. |
302 | bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; } |
303 | |
304 | /// Return true if this is a type with an identity that has no body specified |
305 | /// yet. These prints as 'opaque' in .ll files. |
306 | bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; } |
307 | |
308 | /// isSized - Return true if this is a sized type. |
309 | bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const; |
310 | |
311 | /// Return true if this is a named struct that has a non-empty name. |
312 | bool hasName() const { return SymbolTableEntry != nullptr; } |
313 | |
314 | /// Return the name for this struct type if it has an identity. |
315 | /// This may return an empty string for an unnamed struct type. Do not call |
316 | /// this on an literal type. |
317 | StringRef getName() const; |
318 | |
319 | /// Change the name of this type to the specified name, or to a name with a |
320 | /// suffix if there is a collision. Do not call this on an literal type. |
321 | void setName(StringRef Name); |
322 | |
323 | /// Specify a body for an opaque identified type. |
324 | void setBody(ArrayRef<Type*> Elements, bool isPacked = false); |
325 | |
326 | template <typename... Tys> |
327 | typename std::enable_if<are_base_of<Type, Tys...>::value, void>::type |
328 | setBody(Type *elt1, Tys *... elts) { |
329 | assert(elt1 && "Cannot create a struct type with no elements with this")((elt1 && "Cannot create a struct type with no elements with this" ) ? static_cast<void> (0) : __assert_fail ("elt1 && \"Cannot create a struct type with no elements with this\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 329, __PRETTY_FUNCTION__)); |
330 | SmallVector<llvm::Type *, 8> StructFields({elt1, elts...}); |
331 | setBody(StructFields); |
332 | } |
333 | |
334 | /// Return true if the specified type is valid as a element type. |
335 | static bool isValidElementType(Type *ElemTy); |
336 | |
337 | // Iterator access to the elements. |
338 | using element_iterator = Type::subtype_iterator; |
339 | |
340 | element_iterator element_begin() const { return ContainedTys; } |
341 | element_iterator element_end() const { return &ContainedTys[NumContainedTys];} |
342 | ArrayRef<Type *> const elements() const { |
343 | return makeArrayRef(element_begin(), element_end()); |
344 | } |
345 | |
346 | /// Return true if this is layout identical to the specified struct. |
347 | bool isLayoutIdentical(StructType *Other) const; |
348 | |
349 | /// Random access to the elements |
350 | unsigned getNumElements() const { return NumContainedTys; } |
351 | Type *getElementType(unsigned N) const { |
352 | assert(N < NumContainedTys && "Element number out of range!")((N < NumContainedTys && "Element number out of range!" ) ? static_cast<void> (0) : __assert_fail ("N < NumContainedTys && \"Element number out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 352, __PRETTY_FUNCTION__)); |
353 | return ContainedTys[N]; |
354 | } |
355 | |
356 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
357 | static bool classof(const Type *T) { |
358 | return T->getTypeID() == StructTyID; |
359 | } |
360 | }; |
361 | |
362 | StringRef Type::getStructName() const { |
363 | return cast<StructType>(this)->getName(); |
364 | } |
365 | |
366 | unsigned Type::getStructNumElements() const { |
367 | return cast<StructType>(this)->getNumElements(); |
368 | } |
369 | |
370 | Type *Type::getStructElementType(unsigned N) const { |
371 | return cast<StructType>(this)->getElementType(N); |
372 | } |
373 | |
374 | /// This is the superclass of the array and vector type classes. Both of these |
375 | /// represent "arrays" in memory. The array type represents a specifically sized |
376 | /// array, and the vector type represents a specifically sized array that allows |
377 | /// for use of SIMD instructions. SequentialType holds the common features of |
378 | /// both, which stem from the fact that both lay their components out in memory |
379 | /// identically. |
380 | class SequentialType : public CompositeType { |
381 | Type *ContainedType; ///< Storage for the single contained type. |
382 | uint64_t NumElements; |
383 | |
384 | protected: |
385 | SequentialType(TypeID TID, Type *ElType, uint64_t NumElements) |
386 | : CompositeType(ElType->getContext(), TID), ContainedType(ElType), |
387 | NumElements(NumElements) { |
388 | ContainedTys = &ContainedType; |
389 | NumContainedTys = 1; |
390 | } |
391 | |
392 | public: |
393 | SequentialType(const SequentialType &) = delete; |
394 | SequentialType &operator=(const SequentialType &) = delete; |
395 | |
396 | /// For scalable vectors, this will return the minimum number of elements |
397 | /// in the vector. |
398 | uint64_t getNumElements() const { return NumElements; } |
399 | Type *getElementType() const { return ContainedType; } |
400 | |
401 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
402 | static bool classof(const Type *T) { |
403 | return T->getTypeID() == ArrayTyID || T->getTypeID() == VectorTyID; |
404 | } |
405 | }; |
406 | |
407 | /// Class to represent array types. |
408 | class ArrayType : public SequentialType { |
409 | ArrayType(Type *ElType, uint64_t NumEl); |
410 | |
411 | public: |
412 | ArrayType(const ArrayType &) = delete; |
413 | ArrayType &operator=(const ArrayType &) = delete; |
414 | |
415 | /// This static method is the primary way to construct an ArrayType |
416 | static ArrayType *get(Type *ElementType, uint64_t NumElements); |
417 | |
418 | /// Return true if the specified type is valid as a element type. |
419 | static bool isValidElementType(Type *ElemTy); |
420 | |
421 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
422 | static bool classof(const Type *T) { |
423 | return T->getTypeID() == ArrayTyID; |
424 | } |
425 | }; |
426 | |
427 | uint64_t Type::getArrayNumElements() const { |
428 | return cast<ArrayType>(this)->getNumElements(); |
429 | } |
430 | |
431 | /// Class to represent vector types. |
432 | class VectorType : public SequentialType { |
433 | /// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the |
434 | /// minimum number of elements of type Ty contained within the vector, and |
435 | /// 'vscale x' indicates that the total element count is an integer multiple |
436 | /// of 'n', where the multiple is either guaranteed to be one, or is |
437 | /// statically unknown at compile time. |
438 | /// |
439 | /// If the multiple is known to be 1, then the extra term is discarded in |
440 | /// textual IR: |
441 | /// |
442 | /// <4 x i32> - a vector containing 4 i32s |
443 | /// <vscale x 4 x i32> - a vector containing an unknown integer multiple |
444 | /// of 4 i32s |
445 | |
446 | VectorType(Type *ElType, unsigned NumEl, bool Scalable = false); |
447 | VectorType(Type *ElType, ElementCount EC); |
448 | |
449 | // If true, the total number of elements is an unknown multiple of the |
450 | // minimum 'NumElements' from SequentialType. Otherwise the total number |
451 | // of elements is exactly equal to 'NumElements'. |
452 | bool Scalable; |
453 | |
454 | public: |
455 | VectorType(const VectorType &) = delete; |
456 | VectorType &operator=(const VectorType &) = delete; |
457 | |
458 | /// This static method is the primary way to construct an VectorType. |
459 | static VectorType *get(Type *ElementType, ElementCount EC); |
460 | static VectorType *get(Type *ElementType, unsigned NumElements, |
461 | bool Scalable = false) { |
462 | return VectorType::get(ElementType, {NumElements, Scalable}); |
463 | } |
464 | |
465 | /// This static method gets a VectorType with the same number of elements as |
466 | /// the input type, and the element type is an integer type of the same width |
467 | /// as the input element type. |
468 | static VectorType *getInteger(VectorType *VTy) { |
469 | unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); |
470 | assert(EltBits && "Element size must be of a non-zero size")((EltBits && "Element size must be of a non-zero size" ) ? static_cast<void> (0) : __assert_fail ("EltBits && \"Element size must be of a non-zero size\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 470, __PRETTY_FUNCTION__)); |
471 | Type *EltTy = IntegerType::get(VTy->getContext(), EltBits); |
472 | return VectorType::get(EltTy, VTy->getElementCount()); |
473 | } |
474 | |
475 | /// This static method is like getInteger except that the element types are |
476 | /// twice as wide as the elements in the input type. |
477 | static VectorType *getExtendedElementVectorType(VectorType *VTy) { |
478 | assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.")((VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints." ) ? static_cast<void> (0) : __assert_fail ("VTy->isIntOrIntVectorTy() && \"VTy expected to be a vector of ints.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 478, __PRETTY_FUNCTION__)); |
479 | auto *EltTy = cast<IntegerType>(VTy->getElementType()); |
480 | return VectorType::get(EltTy->getExtendedType(), VTy->getElementCount()); |
481 | } |
482 | |
483 | // This static method gets a VectorType with the same number of elements as |
484 | // the input type, and the element type is an integer or float type which |
485 | // is half as wide as the elements in the input type. |
486 | static VectorType *getTruncatedElementVectorType(VectorType *VTy) { |
487 | Type *EltTy; |
488 | if (VTy->getElementType()->isFloatingPointTy()) { |
489 | switch(VTy->getElementType()->getTypeID()) { |
490 | case DoubleTyID: |
491 | EltTy = Type::getFloatTy(VTy->getContext()); |
492 | break; |
493 | case FloatTyID: |
494 | EltTy = Type::getHalfTy(VTy->getContext()); |
495 | break; |
496 | default: |
497 | llvm_unreachable("Cannot create narrower fp vector element type")::llvm::llvm_unreachable_internal("Cannot create narrower fp vector element type" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 497); |
498 | } |
499 | } else { |
500 | unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); |
501 | assert((EltBits & 1) == 0 &&(((EltBits & 1) == 0 && "Cannot truncate vector element with odd bit-width" ) ? static_cast<void> (0) : __assert_fail ("(EltBits & 1) == 0 && \"Cannot truncate vector element with odd bit-width\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 502, __PRETTY_FUNCTION__)) |
502 | "Cannot truncate vector element with odd bit-width")(((EltBits & 1) == 0 && "Cannot truncate vector element with odd bit-width" ) ? static_cast<void> (0) : __assert_fail ("(EltBits & 1) == 0 && \"Cannot truncate vector element with odd bit-width\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 502, __PRETTY_FUNCTION__)); |
503 | EltTy = IntegerType::get(VTy->getContext(), EltBits / 2); |
504 | } |
505 | return VectorType::get(EltTy, VTy->getElementCount()); |
506 | } |
507 | |
508 | // This static method returns a VectorType with a smaller number of elements |
509 | // of a larger type than the input element type. For example, a <16 x i8> |
510 | // subdivided twice would return <4 x i32> |
511 | static VectorType *getSubdividedVectorType(VectorType *VTy, int NumSubdivs) { |
512 | for (int i = 0; i < NumSubdivs; ++i) { |
513 | VTy = VectorType::getDoubleElementsVectorType(VTy); |
514 | VTy = VectorType::getTruncatedElementVectorType(VTy); |
515 | } |
516 | return VTy; |
517 | } |
518 | |
519 | /// This static method returns a VectorType with half as many elements as the |
520 | /// input type and the same element type. |
521 | static VectorType *getHalfElementsVectorType(VectorType *VTy) { |
522 | auto EltCnt = VTy->getElementCount(); |
523 | assert ((EltCnt.Min & 1) == 0 &&(((EltCnt.Min & 1) == 0 && "Cannot halve vector with odd number of elements." ) ? static_cast<void> (0) : __assert_fail ("(EltCnt.Min & 1) == 0 && \"Cannot halve vector with odd number of elements.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 524, __PRETTY_FUNCTION__)) |
524 | "Cannot halve vector with odd number of elements.")(((EltCnt.Min & 1) == 0 && "Cannot halve vector with odd number of elements." ) ? static_cast<void> (0) : __assert_fail ("(EltCnt.Min & 1) == 0 && \"Cannot halve vector with odd number of elements.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 524, __PRETTY_FUNCTION__)); |
525 | return VectorType::get(VTy->getElementType(), EltCnt/2); |
526 | } |
527 | |
528 | /// This static method returns a VectorType with twice as many elements as the |
529 | /// input type and the same element type. |
530 | static VectorType *getDoubleElementsVectorType(VectorType *VTy) { |
531 | auto EltCnt = VTy->getElementCount(); |
532 | assert((VTy->getNumElements() * 2ull) <= UINT_MAX &&(((VTy->getNumElements() * 2ull) <= (2147483647 *2U +1U ) && "Too many elements in vector") ? static_cast< void> (0) : __assert_fail ("(VTy->getNumElements() * 2ull) <= UINT_MAX && \"Too many elements in vector\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 533, __PRETTY_FUNCTION__)) |
533 | "Too many elements in vector")(((VTy->getNumElements() * 2ull) <= (2147483647 *2U +1U ) && "Too many elements in vector") ? static_cast< void> (0) : __assert_fail ("(VTy->getNumElements() * 2ull) <= UINT_MAX && \"Too many elements in vector\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 533, __PRETTY_FUNCTION__)); |
534 | return VectorType::get(VTy->getElementType(), EltCnt*2); |
535 | } |
536 | |
537 | /// Return true if the specified type is valid as a element type. |
538 | static bool isValidElementType(Type *ElemTy); |
539 | |
540 | /// Return an ElementCount instance to represent the (possibly scalable) |
541 | /// number of elements in the vector. |
542 | ElementCount getElementCount() const { |
543 | uint64_t MinimumEltCnt = getNumElements(); |
544 | assert(MinimumEltCnt <= UINT_MAX && "Too many elements in vector")((MinimumEltCnt <= (2147483647 *2U +1U) && "Too many elements in vector" ) ? static_cast<void> (0) : __assert_fail ("MinimumEltCnt <= UINT_MAX && \"Too many elements in vector\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 544, __PRETTY_FUNCTION__)); |
545 | return { (unsigned)MinimumEltCnt, Scalable }; |
546 | } |
547 | |
548 | /// Returns whether or not this is a scalable vector (meaning the total |
549 | /// element count is a multiple of the minimum). |
550 | bool isScalable() const { |
551 | return Scalable; |
552 | } |
553 | |
554 | /// Return the minimum number of bits in the Vector type. |
555 | /// Returns zero when the vector is a vector of pointers. |
556 | unsigned getBitWidth() const { |
557 | return getNumElements() * getElementType()->getPrimitiveSizeInBits(); |
558 | } |
559 | |
560 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
561 | static bool classof(const Type *T) { |
562 | return T->getTypeID() == VectorTyID; |
563 | } |
564 | }; |
565 | |
566 | unsigned Type::getVectorNumElements() const { |
567 | return cast<VectorType>(this)->getNumElements(); |
568 | } |
569 | |
570 | bool Type::getVectorIsScalable() const { |
571 | return cast<VectorType>(this)->isScalable(); |
572 | } |
573 | |
574 | /// Class to represent pointers. |
575 | class PointerType : public Type { |
576 | explicit PointerType(Type *ElType, unsigned AddrSpace); |
577 | |
578 | Type *PointeeTy; |
579 | |
580 | public: |
581 | PointerType(const PointerType &) = delete; |
582 | PointerType &operator=(const PointerType &) = delete; |
583 | |
584 | /// This constructs a pointer to an object of the specified type in a numbered |
585 | /// address space. |
586 | static PointerType *get(Type *ElementType, unsigned AddressSpace); |
587 | |
588 | /// This constructs a pointer to an object of the specified type in the |
589 | /// generic address space (address space zero). |
590 | static PointerType *getUnqual(Type *ElementType) { |
591 | return PointerType::get(ElementType, 0); |
592 | } |
593 | |
594 | Type *getElementType() const { return PointeeTy; } |
595 | |
596 | /// Return true if the specified type is valid as a element type. |
597 | static bool isValidElementType(Type *ElemTy); |
598 | |
599 | /// Return true if we can load or store from a pointer to this type. |
600 | static bool isLoadableOrStorableType(Type *ElemTy); |
601 | |
602 | /// Return the address space of the Pointer type. |
603 | inline unsigned getAddressSpace() const { return getSubclassData(); } |
604 | |
605 | /// Implement support type inquiry through isa, cast, and dyn_cast. |
606 | static bool classof(const Type *T) { |
607 | return T->getTypeID() == PointerTyID; |
608 | } |
609 | }; |
610 | |
611 | Type *Type::getExtendedType() const { |
612 | assert(((isIntOrIntVectorTy() && "Original type expected to be a vector of integers or a scalar integer." ) ? static_cast<void> (0) : __assert_fail ("isIntOrIntVectorTy() && \"Original type expected to be a vector of integers or a scalar integer.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 614, __PRETTY_FUNCTION__)) |
613 | isIntOrIntVectorTy() &&((isIntOrIntVectorTy() && "Original type expected to be a vector of integers or a scalar integer." ) ? static_cast<void> (0) : __assert_fail ("isIntOrIntVectorTy() && \"Original type expected to be a vector of integers or a scalar integer.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 614, __PRETTY_FUNCTION__)) |
614 | "Original type expected to be a vector of integers or a scalar integer.")((isIntOrIntVectorTy() && "Original type expected to be a vector of integers or a scalar integer." ) ? static_cast<void> (0) : __assert_fail ("isIntOrIntVectorTy() && \"Original type expected to be a vector of integers or a scalar integer.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/include/llvm/IR/DerivedTypes.h" , 614, __PRETTY_FUNCTION__)); |
615 | if (auto *VTy = dyn_cast<VectorType>(this)) |
616 | return VectorType::getExtendedElementVectorType( |
617 | const_cast<VectorType *>(VTy)); |
618 | return cast<IntegerType>(this)->getExtendedType(); |
619 | } |
620 | |
621 | unsigned Type::getPointerAddressSpace() const { |
622 | return cast<PointerType>(getScalarType())->getAddressSpace(); |
623 | } |
624 | |
625 | } // end namespace llvm |
626 | |
627 | #endif // LLVM_IR_DERIVEDTYPES_H |