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 | |
29 | using namespace clang; |
30 | using namespace CodeGen; |
31 | |
32 | CodeGenTypes::CodeGenTypes(CodeGenModule &cgm) |
33 | : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()), |
34 | Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()), |
35 | TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) { |
36 | SkippedLayout = false; |
37 | LongDoubleReferenced = false; |
38 | } |
39 | |
40 | CodeGenTypes::~CodeGenTypes() { |
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 | // FIXME: We probably want to make more tweaks to the printing policy. For |
58 | // example, we should probably enable PrintCanonicalTypes and |
59 | // FullyQualifiedNames. |
60 | PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy(); |
61 | Policy.SuppressInlineNamespace = false; |
62 | |
63 | // Name the codegen type after the typedef name |
64 | // if there is no tag type name available |
65 | if (RD->getIdentifier()) { |
66 | // FIXME: We should not have to check for a null decl context here. |
67 | // Right now we do it because the implicit Obj-C decls don't have one. |
68 | if (RD->getDeclContext()) |
69 | RD->printQualifiedName(OS, Policy); |
70 | else |
71 | RD->printName(OS, Policy); |
72 | } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { |
73 | // FIXME: We should not have to check for a null decl context here. |
74 | // Right now we do it because the implicit Obj-C decls don't have one. |
75 | if (TDD->getDeclContext()) |
76 | TDD->printQualifiedName(OS, Policy); |
77 | else |
78 | TDD->printName(OS); |
79 | } else |
80 | OS << "anon" ; |
81 | |
82 | if (!suffix.empty()) |
83 | OS << suffix; |
84 | |
85 | Ty->setName(OS.str()); |
86 | } |
87 | |
88 | /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from |
89 | /// ConvertType in that it is used to convert to the memory representation for |
90 | /// a type. For example, the scalar representation for _Bool is i1, but the |
91 | /// memory representation is usually i8 or i32, depending on the target. |
92 | llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) { |
93 | if (T->isConstantMatrixType()) { |
94 | const Type *Ty = Context.getCanonicalType(T).getTypePtr(); |
95 | const ConstantMatrixType *MT = cast<ConstantMatrixType>(Val: Ty); |
96 | return llvm::ArrayType::get(ElementType: ConvertType(T: MT->getElementType()), |
97 | NumElements: MT->getNumRows() * MT->getNumColumns()); |
98 | } |
99 | |
100 | llvm::Type *R = ConvertType(T); |
101 | |
102 | // Check for the boolean vector case. |
103 | if (T->isExtVectorBoolType()) { |
104 | auto *FixedVT = cast<llvm::FixedVectorType>(Val: R); |
105 | // Pad to at least one byte. |
106 | uint64_t BytePadded = std::max<uint64_t>(a: FixedVT->getNumElements(), b: 8); |
107 | return llvm::IntegerType::get(C&: FixedVT->getContext(), NumBits: BytePadded); |
108 | } |
109 | |
110 | // If this is a bool type, or a bit-precise integer type in a bitfield |
111 | // representation, map this integer to the target-specified size. |
112 | if ((ForBitField && T->isBitIntType()) || |
113 | (!T->isBitIntType() && R->isIntegerTy(Bitwidth: 1))) |
114 | return llvm::IntegerType::get(C&: getLLVMContext(), |
115 | NumBits: (unsigned)Context.getTypeSize(T)); |
116 | |
117 | // Else, don't map it. |
118 | return R; |
119 | } |
120 | |
121 | /// isRecordLayoutComplete - Return true if the specified type is already |
122 | /// completely laid out. |
123 | bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { |
124 | llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = |
125 | RecordDeclTypes.find(Val: Ty); |
126 | return I != RecordDeclTypes.end() && !I->second->isOpaque(); |
127 | } |
128 | |
129 | /// isFuncParamTypeConvertible - Return true if the specified type in a |
130 | /// function parameter or result position can be converted to an IR type at this |
131 | /// point. This boils down to being whether it is complete. |
132 | bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) { |
133 | // Some ABIs cannot have their member pointers represented in IR unless |
134 | // certain circumstances have been reached. |
135 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) |
136 | return getCXXABI().isMemberPointerConvertible(MPT); |
137 | |
138 | // If this isn't a tagged type, we can convert it! |
139 | const TagType *TT = Ty->getAs<TagType>(); |
140 | if (!TT) return true; |
141 | |
142 | // Incomplete types cannot be converted. |
143 | return !TT->isIncompleteType(); |
144 | } |
145 | |
146 | |
147 | /// Code to verify a given function type is complete, i.e. the return type |
148 | /// and all of the parameter types are complete. Also check to see if we are in |
149 | /// a RS_StructPointer context, and if so whether any struct types have been |
150 | /// pended. If so, we don't want to ask the ABI lowering code to handle a type |
151 | /// that cannot be converted to an IR type. |
152 | bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { |
153 | if (!isFuncParamTypeConvertible(Ty: FT->getReturnType())) |
154 | return false; |
155 | |
156 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT)) |
157 | for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) |
158 | if (!isFuncParamTypeConvertible(Ty: FPT->getParamType(i))) |
159 | return false; |
160 | |
161 | return true; |
162 | } |
163 | |
164 | /// UpdateCompletedType - When we find the full definition for a TagDecl, |
165 | /// replace the 'opaque' type we previously made for it if applicable. |
166 | void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { |
167 | // If this is an enum being completed, then we flush all non-struct types from |
168 | // the cache. This allows function types and other things that may be derived |
169 | // from the enum to be recomputed. |
170 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(Val: TD)) { |
171 | // Only flush the cache if we've actually already converted this type. |
172 | if (TypeCache.count(Val: ED->getTypeForDecl())) { |
173 | // Okay, we formed some types based on this. We speculated that the enum |
174 | // would be lowered to i32, so we only need to flush the cache if this |
175 | // didn't happen. |
176 | if (!ConvertType(T: ED->getIntegerType())->isIntegerTy(Bitwidth: 32)) |
177 | TypeCache.clear(); |
178 | } |
179 | // If necessary, provide the full definition of a type only used with a |
180 | // declaration so far. |
181 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
182 | DI->completeType(ED); |
183 | return; |
184 | } |
185 | |
186 | // If we completed a RecordDecl that we previously used and converted to an |
187 | // anonymous type, then go ahead and complete it now. |
188 | const RecordDecl *RD = cast<RecordDecl>(Val: TD); |
189 | if (RD->isDependentType()) return; |
190 | |
191 | // Only complete it if we converted it already. If we haven't converted it |
192 | // yet, we'll just do it lazily. |
193 | if (RecordDeclTypes.count(Val: Context.getTagDeclType(RD).getTypePtr())) |
194 | ConvertRecordDeclType(TD: RD); |
195 | |
196 | // If necessary, provide the full definition of a type only used with a |
197 | // declaration so far. |
198 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
199 | DI->completeType(RD); |
200 | } |
201 | |
202 | void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
203 | QualType T = Context.getRecordType(RD); |
204 | T = Context.getCanonicalType(T); |
205 | |
206 | const Type *Ty = T.getTypePtr(); |
207 | if (RecordsWithOpaqueMemberPointers.count(Val: Ty)) { |
208 | TypeCache.clear(); |
209 | RecordsWithOpaqueMemberPointers.clear(); |
210 | } |
211 | } |
212 | |
213 | static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, |
214 | const llvm::fltSemantics &format, |
215 | bool UseNativeHalf = false) { |
216 | if (&format == &llvm::APFloat::IEEEhalf()) { |
217 | if (UseNativeHalf) |
218 | return llvm::Type::getHalfTy(C&: VMContext); |
219 | else |
220 | return llvm::Type::getInt16Ty(C&: VMContext); |
221 | } |
222 | if (&format == &llvm::APFloat::BFloat()) |
223 | return llvm::Type::getBFloatTy(C&: VMContext); |
224 | if (&format == &llvm::APFloat::IEEEsingle()) |
225 | return llvm::Type::getFloatTy(C&: VMContext); |
226 | if (&format == &llvm::APFloat::IEEEdouble()) |
227 | return llvm::Type::getDoubleTy(C&: VMContext); |
228 | if (&format == &llvm::APFloat::IEEEquad()) |
229 | return llvm::Type::getFP128Ty(C&: VMContext); |
230 | if (&format == &llvm::APFloat::PPCDoubleDouble()) |
231 | return llvm::Type::getPPC_FP128Ty(C&: VMContext); |
232 | if (&format == &llvm::APFloat::x87DoubleExtended()) |
233 | return llvm::Type::getX86_FP80Ty(C&: VMContext); |
234 | llvm_unreachable("Unknown float format!" ); |
235 | } |
236 | |
237 | llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) { |
238 | assert(QFT.isCanonical()); |
239 | const FunctionType *FT = cast<FunctionType>(Val: QFT.getTypePtr()); |
240 | // First, check whether we can build the full function type. If the |
241 | // function type depends on an incomplete type (e.g. a struct or enum), we |
242 | // cannot lower the function type. |
243 | if (!isFuncTypeConvertible(FT)) { |
244 | // This function's type depends on an incomplete tag type. |
245 | |
246 | // Force conversion of all the relevant record types, to make sure |
247 | // we re-convert the FunctionType when appropriate. |
248 | if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>()) |
249 | ConvertRecordDeclType(TD: RT->getDecl()); |
250 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT)) |
251 | for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) |
252 | if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>()) |
253 | ConvertRecordDeclType(TD: RT->getDecl()); |
254 | |
255 | SkippedLayout = true; |
256 | |
257 | // Return a placeholder type. |
258 | return llvm::StructType::get(Context&: getLLVMContext()); |
259 | } |
260 | |
261 | // The function type can be built; call the appropriate routines to |
262 | // build it. |
263 | const CGFunctionInfo *FI; |
264 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT)) { |
265 | FI = &arrangeFreeFunctionType( |
266 | Ty: CanQual<FunctionProtoType>::CreateUnsafe(Other: QualType(FPT, 0))); |
267 | } else { |
268 | const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(Val: FT); |
269 | FI = &arrangeFreeFunctionType( |
270 | Ty: CanQual<FunctionNoProtoType>::CreateUnsafe(Other: QualType(FNPT, 0))); |
271 | } |
272 | |
273 | llvm::Type *ResultType = nullptr; |
274 | // If there is something higher level prodding our CGFunctionInfo, then |
275 | // don't recurse into it again. |
276 | if (FunctionsBeingProcessed.count(Ptr: FI)) { |
277 | |
278 | ResultType = llvm::StructType::get(Context&: getLLVMContext()); |
279 | SkippedLayout = true; |
280 | } else { |
281 | |
282 | // Otherwise, we're good to go, go ahead and convert it. |
283 | ResultType = GetFunctionType(Info: *FI); |
284 | } |
285 | |
286 | return ResultType; |
287 | } |
288 | |
289 | /// ConvertType - Convert the specified type to its LLVM form. |
290 | llvm::Type *CodeGenTypes::ConvertType(QualType T) { |
291 | T = Context.getCanonicalType(T); |
292 | |
293 | const Type *Ty = T.getTypePtr(); |
294 | |
295 | // For the device-side compilation, CUDA device builtin surface/texture types |
296 | // may be represented in different types. |
297 | if (Context.getLangOpts().CUDAIsDevice) { |
298 | if (T->isCUDADeviceBuiltinSurfaceType()) { |
299 | if (auto *Ty = CGM.getTargetCodeGenInfo() |
300 | .getCUDADeviceBuiltinSurfaceDeviceType()) |
301 | return Ty; |
302 | } else if (T->isCUDADeviceBuiltinTextureType()) { |
303 | if (auto *Ty = CGM.getTargetCodeGenInfo() |
304 | .getCUDADeviceBuiltinTextureDeviceType()) |
305 | return Ty; |
306 | } |
307 | } |
308 | |
309 | // RecordTypes are cached and processed specially. |
310 | if (const RecordType *RT = dyn_cast<RecordType>(Val: Ty)) |
311 | return ConvertRecordDeclType(TD: RT->getDecl()); |
312 | |
313 | llvm::Type *CachedType = nullptr; |
314 | auto TCI = TypeCache.find(Val: Ty); |
315 | if (TCI != TypeCache.end()) |
316 | CachedType = TCI->second; |
317 | // With expensive checks, check that the type we compute matches the |
318 | // cached type. |
319 | #ifndef EXPENSIVE_CHECKS |
320 | if (CachedType) |
321 | return CachedType; |
322 | #endif |
323 | |
324 | // If we don't have it in the cache, convert it now. |
325 | llvm::Type *ResultType = nullptr; |
326 | switch (Ty->getTypeClass()) { |
327 | case Type::Record: // Handled above. |
328 | #define TYPE(Class, Base) |
329 | #define ABSTRACT_TYPE(Class, Base) |
330 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
331 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
332 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
333 | #include "clang/AST/TypeNodes.inc" |
334 | llvm_unreachable("Non-canonical or dependent types aren't possible." ); |
335 | |
336 | case Type::Builtin: { |
337 | switch (cast<BuiltinType>(Ty)->getKind()) { |
338 | case BuiltinType::Void: |
339 | case BuiltinType::ObjCId: |
340 | case BuiltinType::ObjCClass: |
341 | case BuiltinType::ObjCSel: |
342 | // LLVM void type can only be used as the result of a function call. Just |
343 | // map to the same as char. |
344 | ResultType = llvm::Type::getInt8Ty(C&: getLLVMContext()); |
345 | break; |
346 | |
347 | case BuiltinType::Bool: |
348 | // Note that we always return bool as i1 for use as a scalar type. |
349 | ResultType = llvm::Type::getInt1Ty(C&: getLLVMContext()); |
350 | break; |
351 | |
352 | case BuiltinType::Char_S: |
353 | case BuiltinType::Char_U: |
354 | case BuiltinType::SChar: |
355 | case BuiltinType::UChar: |
356 | case BuiltinType::Short: |
357 | case BuiltinType::UShort: |
358 | case BuiltinType::Int: |
359 | case BuiltinType::UInt: |
360 | case BuiltinType::Long: |
361 | case BuiltinType::ULong: |
362 | case BuiltinType::LongLong: |
363 | case BuiltinType::ULongLong: |
364 | case BuiltinType::WChar_S: |
365 | case BuiltinType::WChar_U: |
366 | case BuiltinType::Char8: |
367 | case BuiltinType::Char16: |
368 | case BuiltinType::Char32: |
369 | case BuiltinType::ShortAccum: |
370 | case BuiltinType::Accum: |
371 | case BuiltinType::LongAccum: |
372 | case BuiltinType::UShortAccum: |
373 | case BuiltinType::UAccum: |
374 | case BuiltinType::ULongAccum: |
375 | case BuiltinType::ShortFract: |
376 | case BuiltinType::Fract: |
377 | case BuiltinType::LongFract: |
378 | case BuiltinType::UShortFract: |
379 | case BuiltinType::UFract: |
380 | case BuiltinType::ULongFract: |
381 | case BuiltinType::SatShortAccum: |
382 | case BuiltinType::SatAccum: |
383 | case BuiltinType::SatLongAccum: |
384 | case BuiltinType::SatUShortAccum: |
385 | case BuiltinType::SatUAccum: |
386 | case BuiltinType::SatULongAccum: |
387 | case BuiltinType::SatShortFract: |
388 | case BuiltinType::SatFract: |
389 | case BuiltinType::SatLongFract: |
390 | case BuiltinType::SatUShortFract: |
391 | case BuiltinType::SatUFract: |
392 | case BuiltinType::SatULongFract: |
393 | ResultType = llvm::IntegerType::get(C&: getLLVMContext(), |
394 | NumBits: static_cast<unsigned>(Context.getTypeSize(T))); |
395 | break; |
396 | |
397 | case BuiltinType::Float16: |
398 | ResultType = |
399 | getTypeForFormat(VMContext&: getLLVMContext(), format: Context.getFloatTypeSemantics(T), |
400 | /* UseNativeHalf = */ true); |
401 | break; |
402 | |
403 | case BuiltinType::Half: |
404 | // Half FP can either be storage-only (lowered to i16) or native. |
405 | ResultType = getTypeForFormat( |
406 | VMContext&: getLLVMContext(), format: Context.getFloatTypeSemantics(T), |
407 | UseNativeHalf: Context.getLangOpts().NativeHalfType || |
408 | !Context.getTargetInfo().useFP16ConversionIntrinsics()); |
409 | break; |
410 | case BuiltinType::LongDouble: |
411 | LongDoubleReferenced = true; |
412 | LLVM_FALLTHROUGH; |
413 | case BuiltinType::BFloat16: |
414 | case BuiltinType::Float: |
415 | case BuiltinType::Double: |
416 | case BuiltinType::Float128: |
417 | case BuiltinType::Ibm128: |
418 | ResultType = getTypeForFormat(VMContext&: getLLVMContext(), |
419 | format: Context.getFloatTypeSemantics(T), |
420 | /* UseNativeHalf = */ false); |
421 | break; |
422 | |
423 | case BuiltinType::NullPtr: |
424 | // Model std::nullptr_t as i8* |
425 | ResultType = llvm::PointerType::getUnqual(C&: getLLVMContext()); |
426 | break; |
427 | |
428 | case BuiltinType::UInt128: |
429 | case BuiltinType::Int128: |
430 | ResultType = llvm::IntegerType::get(C&: getLLVMContext(), NumBits: 128); |
431 | break; |
432 | |
433 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
434 | case BuiltinType::Id: |
435 | #include "clang/Basic/OpenCLImageTypes.def" |
436 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
437 | case BuiltinType::Id: |
438 | #include "clang/Basic/OpenCLExtensionTypes.def" |
439 | case BuiltinType::OCLSampler: |
440 | case BuiltinType::OCLEvent: |
441 | case BuiltinType::OCLClkEvent: |
442 | case BuiltinType::OCLQueue: |
443 | case BuiltinType::OCLReserveID: |
444 | ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(T: Ty); |
445 | break; |
446 | case BuiltinType::SveInt8: |
447 | case BuiltinType::SveUint8: |
448 | case BuiltinType::SveInt8x2: |
449 | case BuiltinType::SveUint8x2: |
450 | case BuiltinType::SveInt8x3: |
451 | case BuiltinType::SveUint8x3: |
452 | case BuiltinType::SveInt8x4: |
453 | case BuiltinType::SveUint8x4: |
454 | case BuiltinType::SveInt16: |
455 | case BuiltinType::SveUint16: |
456 | case BuiltinType::SveInt16x2: |
457 | case BuiltinType::SveUint16x2: |
458 | case BuiltinType::SveInt16x3: |
459 | case BuiltinType::SveUint16x3: |
460 | case BuiltinType::SveInt16x4: |
461 | case BuiltinType::SveUint16x4: |
462 | case BuiltinType::SveInt32: |
463 | case BuiltinType::SveUint32: |
464 | case BuiltinType::SveInt32x2: |
465 | case BuiltinType::SveUint32x2: |
466 | case BuiltinType::SveInt32x3: |
467 | case BuiltinType::SveUint32x3: |
468 | case BuiltinType::SveInt32x4: |
469 | case BuiltinType::SveUint32x4: |
470 | case BuiltinType::SveInt64: |
471 | case BuiltinType::SveUint64: |
472 | case BuiltinType::SveInt64x2: |
473 | case BuiltinType::SveUint64x2: |
474 | case BuiltinType::SveInt64x3: |
475 | case BuiltinType::SveUint64x3: |
476 | case BuiltinType::SveInt64x4: |
477 | case BuiltinType::SveUint64x4: |
478 | case BuiltinType::SveBool: |
479 | case BuiltinType::SveBoolx2: |
480 | case BuiltinType::SveBoolx4: |
481 | case BuiltinType::SveFloat16: |
482 | case BuiltinType::SveFloat16x2: |
483 | case BuiltinType::SveFloat16x3: |
484 | case BuiltinType::SveFloat16x4: |
485 | case BuiltinType::SveFloat32: |
486 | case BuiltinType::SveFloat32x2: |
487 | case BuiltinType::SveFloat32x3: |
488 | case BuiltinType::SveFloat32x4: |
489 | case BuiltinType::SveFloat64: |
490 | case BuiltinType::SveFloat64x2: |
491 | case BuiltinType::SveFloat64x3: |
492 | case BuiltinType::SveFloat64x4: |
493 | case BuiltinType::SveBFloat16: |
494 | case BuiltinType::SveBFloat16x2: |
495 | case BuiltinType::SveBFloat16x3: |
496 | case BuiltinType::SveBFloat16x4: { |
497 | ASTContext::BuiltinVectorTypeInfo Info = |
498 | Context.getBuiltinVectorTypeInfo(VecTy: cast<BuiltinType>(Ty)); |
499 | return llvm::ScalableVectorType::get(ConvertType(T: Info.ElementType), |
500 | Info.EC.getKnownMinValue() * |
501 | Info.NumVectors); |
502 | } |
503 | case BuiltinType::SveCount: |
504 | return llvm::TargetExtType::get(Context&: getLLVMContext(), Name: "aarch64.svcount" ); |
505 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
506 | case BuiltinType::Id: \ |
507 | ResultType = \ |
508 | llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \ |
509 | break; |
510 | #include "clang/Basic/PPCTypes.def" |
511 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
512 | #include "clang/Basic/RISCVVTypes.def" |
513 | { |
514 | ASTContext::BuiltinVectorTypeInfo Info = |
515 | Context.getBuiltinVectorTypeInfo(VecTy: cast<BuiltinType>(Ty)); |
516 | // Tuple types are expressed as aggregregate types of the same scalable |
517 | // vector type (e.g. vint32m1x2_t is two vint32m1_t, which is {<vscale x |
518 | // 2 x i32>, <vscale x 2 x i32>}). |
519 | if (Info.NumVectors != 1) { |
520 | llvm::Type *EltTy = llvm::ScalableVectorType::get( |
521 | ConvertType(T: Info.ElementType), Info.EC.getKnownMinValue()); |
522 | llvm::SmallVector<llvm::Type *, 4> EltTys(Info.NumVectors, EltTy); |
523 | return llvm::StructType::get(getLLVMContext(), EltTys); |
524 | } |
525 | return llvm::ScalableVectorType::get(ConvertType(T: Info.ElementType), |
526 | Info.EC.getKnownMinValue() * |
527 | Info.NumVectors); |
528 | } |
529 | #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \ |
530 | case BuiltinType::Id: { \ |
531 | if (BuiltinType::Id == BuiltinType::WasmExternRef) \ |
532 | ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \ |
533 | else \ |
534 | llvm_unreachable("Unexpected wasm reference builtin type!"); \ |
535 | } break; |
536 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
537 | case BuiltinType::Dependent: |
538 | #define BUILTIN_TYPE(Id, SingletonId) |
539 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
540 | case BuiltinType::Id: |
541 | #include "clang/AST/BuiltinTypes.def" |
542 | llvm_unreachable("Unexpected placeholder builtin type!" ); |
543 | } |
544 | break; |
545 | } |
546 | case Type::Auto: |
547 | case Type::DeducedTemplateSpecialization: |
548 | llvm_unreachable("Unexpected undeduced type!" ); |
549 | case Type::Complex: { |
550 | llvm::Type *EltTy = ConvertType(T: cast<ComplexType>(Ty)->getElementType()); |
551 | ResultType = llvm::StructType::get(EltTy, EltTy); |
552 | break; |
553 | } |
554 | case Type::LValueReference: |
555 | case Type::RValueReference: { |
556 | const ReferenceType *RTy = cast<ReferenceType>(Ty); |
557 | QualType ETy = RTy->getPointeeType(); |
558 | unsigned AS = getTargetAddressSpace(T: ETy); |
559 | ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS); |
560 | break; |
561 | } |
562 | case Type::Pointer: { |
563 | const PointerType *PTy = cast<PointerType>(Ty); |
564 | QualType ETy = PTy->getPointeeType(); |
565 | unsigned AS = getTargetAddressSpace(T: ETy); |
566 | ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS); |
567 | break; |
568 | } |
569 | |
570 | case Type::VariableArray: { |
571 | const VariableArrayType *A = cast<VariableArrayType>(Ty); |
572 | assert(A->getIndexTypeCVRQualifiers() == 0 && |
573 | "FIXME: We only handle trivial array types so far!" ); |
574 | // VLAs resolve to the innermost element type; this matches |
575 | // the return of alloca, and there isn't any obviously better choice. |
576 | ResultType = ConvertTypeForMem(T: A->getElementType()); |
577 | break; |
578 | } |
579 | case Type::IncompleteArray: { |
580 | const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); |
581 | assert(A->getIndexTypeCVRQualifiers() == 0 && |
582 | "FIXME: We only handle trivial array types so far!" ); |
583 | // int X[] -> [0 x int], unless the element type is not sized. If it is |
584 | // unsized (e.g. an incomplete struct) just use [0 x i8]. |
585 | ResultType = ConvertTypeForMem(T: A->getElementType()); |
586 | if (!ResultType->isSized()) { |
587 | SkippedLayout = true; |
588 | ResultType = llvm::Type::getInt8Ty(C&: getLLVMContext()); |
589 | } |
590 | ResultType = llvm::ArrayType::get(ElementType: ResultType, NumElements: 0); |
591 | break; |
592 | } |
593 | case Type::ArrayParameter: |
594 | case Type::ConstantArray: { |
595 | const ConstantArrayType *A = cast<ConstantArrayType>(Ty); |
596 | llvm::Type *EltTy = ConvertTypeForMem(T: A->getElementType()); |
597 | |
598 | // Lower arrays of undefined struct type to arrays of i8 just to have a |
599 | // concrete type. |
600 | if (!EltTy->isSized()) { |
601 | SkippedLayout = true; |
602 | EltTy = llvm::Type::getInt8Ty(C&: getLLVMContext()); |
603 | } |
604 | |
605 | ResultType = llvm::ArrayType::get(ElementType: EltTy, NumElements: A->getZExtSize()); |
606 | break; |
607 | } |
608 | case Type::ExtVector: |
609 | case Type::Vector: { |
610 | const auto *VT = cast<VectorType>(Ty); |
611 | // An ext_vector_type of Bool is really a vector of bits. |
612 | llvm::Type *IRElemTy = VT->isExtVectorBoolType() |
613 | ? llvm::Type::getInt1Ty(C&: getLLVMContext()) |
614 | : ConvertType(T: VT->getElementType()); |
615 | ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements()); |
616 | break; |
617 | } |
618 | case Type::ConstantMatrix: { |
619 | const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty); |
620 | ResultType = |
621 | llvm::FixedVectorType::get(ConvertType(T: MT->getElementType()), |
622 | MT->getNumRows() * MT->getNumColumns()); |
623 | break; |
624 | } |
625 | case Type::FunctionNoProto: |
626 | case Type::FunctionProto: |
627 | ResultType = ConvertFunctionTypeInternal(QFT: T); |
628 | break; |
629 | case Type::ObjCObject: |
630 | ResultType = ConvertType(T: cast<ObjCObjectType>(Ty)->getBaseType()); |
631 | break; |
632 | |
633 | case Type::ObjCInterface: { |
634 | // Objective-C interfaces are always opaque (outside of the |
635 | // runtime, which can do whatever it likes); we never refine |
636 | // these. |
637 | llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; |
638 | if (!T) |
639 | T = llvm::StructType::create(Context&: getLLVMContext()); |
640 | ResultType = T; |
641 | break; |
642 | } |
643 | |
644 | case Type::ObjCObjectPointer: |
645 | ResultType = llvm::PointerType::getUnqual(C&: getLLVMContext()); |
646 | break; |
647 | |
648 | case Type::Enum: { |
649 | const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); |
650 | if (ED->isCompleteDefinition() || ED->isFixed()) |
651 | return ConvertType(T: ED->getIntegerType()); |
652 | // Return a placeholder 'i32' type. This can be changed later when the |
653 | // type is defined (see UpdateCompletedType), but is likely to be the |
654 | // "right" answer. |
655 | ResultType = llvm::Type::getInt32Ty(C&: getLLVMContext()); |
656 | break; |
657 | } |
658 | |
659 | case Type::BlockPointer: { |
660 | // Block pointers lower to function type. For function type, |
661 | // getTargetAddressSpace() returns default address space for |
662 | // function pointer i.e. program address space. Therefore, for block |
663 | // pointers, it is important to pass the pointee AST address space when |
664 | // calling getTargetAddressSpace(), to ensure that we get the LLVM IR |
665 | // address space for data pointers and not function pointers. |
666 | const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); |
667 | unsigned AS = Context.getTargetAddressSpace(AS: FTy.getAddressSpace()); |
668 | ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS); |
669 | break; |
670 | } |
671 | |
672 | case Type::MemberPointer: { |
673 | auto *MPTy = cast<MemberPointerType>(Ty); |
674 | if (!getCXXABI().isMemberPointerConvertible(MPT: MPTy)) { |
675 | auto *C = MPTy->getClass(); |
676 | auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr}); |
677 | if (Insertion.second) |
678 | Insertion.first->second = llvm::StructType::create(Context&: getLLVMContext()); |
679 | ResultType = Insertion.first->second; |
680 | } else { |
681 | ResultType = getCXXABI().ConvertMemberPointerType(MPT: MPTy); |
682 | } |
683 | break; |
684 | } |
685 | |
686 | case Type::Atomic: { |
687 | QualType valueType = cast<AtomicType>(Ty)->getValueType(); |
688 | ResultType = ConvertTypeForMem(T: valueType); |
689 | |
690 | // Pad out to the inflated size if necessary. |
691 | uint64_t valueSize = Context.getTypeSize(T: valueType); |
692 | uint64_t atomicSize = Context.getTypeSize(T: Ty); |
693 | if (valueSize != atomicSize) { |
694 | assert(valueSize < atomicSize); |
695 | llvm::Type *elts[] = { |
696 | ResultType, |
697 | llvm::ArrayType::get(ElementType: CGM.Int8Ty, NumElements: (atomicSize - valueSize) / 8) |
698 | }; |
699 | ResultType = |
700 | llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts)); |
701 | } |
702 | break; |
703 | } |
704 | case Type::Pipe: { |
705 | ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty)); |
706 | break; |
707 | } |
708 | case Type::BitInt: { |
709 | const auto &EIT = cast<BitIntType>(Ty); |
710 | ResultType = llvm::Type::getIntNTy(C&: getLLVMContext(), N: EIT->getNumBits()); |
711 | break; |
712 | } |
713 | } |
714 | |
715 | assert(ResultType && "Didn't convert a type?" ); |
716 | assert((!CachedType || CachedType == ResultType) && |
717 | "Cached type doesn't match computed type" ); |
718 | |
719 | TypeCache[Ty] = ResultType; |
720 | return ResultType; |
721 | } |
722 | |
723 | bool CodeGenModule::isPaddedAtomicType(QualType type) { |
724 | return isPaddedAtomicType(type: type->castAs<AtomicType>()); |
725 | } |
726 | |
727 | bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { |
728 | return Context.getTypeSize(type) != Context.getTypeSize(T: type->getValueType()); |
729 | } |
730 | |
731 | /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. |
732 | llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { |
733 | // TagDecl's are not necessarily unique, instead use the (clang) |
734 | // type connected to the decl. |
735 | const Type *Key = Context.getTagDeclType(RD).getTypePtr(); |
736 | |
737 | llvm::StructType *&Entry = RecordDeclTypes[Key]; |
738 | |
739 | // If we don't have a StructType at all yet, create the forward declaration. |
740 | if (!Entry) { |
741 | Entry = llvm::StructType::create(Context&: getLLVMContext()); |
742 | addRecordTypeName(RD, Ty: Entry, suffix: "" ); |
743 | } |
744 | llvm::StructType *Ty = Entry; |
745 | |
746 | // If this is still a forward declaration, or the LLVM type is already |
747 | // complete, there's nothing more to do. |
748 | RD = RD->getDefinition(); |
749 | if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque()) |
750 | return Ty; |
751 | |
752 | // Force conversion of non-virtual base classes recursively. |
753 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
754 | for (const auto &I : CRD->bases()) { |
755 | if (I.isVirtual()) continue; |
756 | ConvertRecordDeclType(RD: I.getType()->castAs<RecordType>()->getDecl()); |
757 | } |
758 | } |
759 | |
760 | // Layout fields. |
761 | std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(D: RD, Ty); |
762 | CGRecordLayouts[Key] = std::move(Layout); |
763 | |
764 | // If this struct blocked a FunctionType conversion, then recompute whatever |
765 | // was derived from that. |
766 | // FIXME: This is hugely overconservative. |
767 | if (SkippedLayout) |
768 | TypeCache.clear(); |
769 | |
770 | return Ty; |
771 | } |
772 | |
773 | /// getCGRecordLayout - Return record layout info for the given record decl. |
774 | const CGRecordLayout & |
775 | CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { |
776 | const Type *Key = Context.getTagDeclType(RD).getTypePtr(); |
777 | |
778 | auto I = CGRecordLayouts.find(Val: Key); |
779 | if (I != CGRecordLayouts.end()) |
780 | return *I->second; |
781 | // Compute the type information. |
782 | ConvertRecordDeclType(RD); |
783 | |
784 | // Now try again. |
785 | I = CGRecordLayouts.find(Val: Key); |
786 | |
787 | assert(I != CGRecordLayouts.end() && |
788 | "Unable to find record layout information for type" ); |
789 | return *I->second; |
790 | } |
791 | |
792 | bool CodeGenTypes::isPointerZeroInitializable(QualType T) { |
793 | assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type" ); |
794 | return isZeroInitializable(T); |
795 | } |
796 | |
797 | bool CodeGenTypes::isZeroInitializable(QualType T) { |
798 | if (T->getAs<PointerType>()) |
799 | return Context.getTargetNullPointerValue(QT: T) == 0; |
800 | |
801 | if (const auto *AT = Context.getAsArrayType(T)) { |
802 | if (isa<IncompleteArrayType>(Val: AT)) |
803 | return true; |
804 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
805 | if (Context.getConstantArrayElementCount(CA: CAT) == 0) |
806 | return true; |
807 | T = Context.getBaseElementType(QT: T); |
808 | } |
809 | |
810 | // Records are non-zero-initializable if they contain any |
811 | // non-zero-initializable subobjects. |
812 | if (const RecordType *RT = T->getAs<RecordType>()) { |
813 | const RecordDecl *RD = RT->getDecl(); |
814 | return isZeroInitializable(RD); |
815 | } |
816 | |
817 | // We have to ask the ABI about member pointers. |
818 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) |
819 | return getCXXABI().isZeroInitializable(MPT); |
820 | |
821 | // Everything else is okay. |
822 | return true; |
823 | } |
824 | |
825 | bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) { |
826 | return getCGRecordLayout(RD).isZeroInitializable(); |
827 | } |
828 | |
829 | unsigned CodeGenTypes::getTargetAddressSpace(QualType T) const { |
830 | // Return the address space for the type. If the type is a |
831 | // function type without an address space qualifier, the |
832 | // program address space is used. Otherwise, the target picks |
833 | // the best address space based on the type information |
834 | return T->isFunctionType() && !T.hasAddressSpace() |
835 | ? getDataLayout().getProgramAddressSpace() |
836 | : getContext().getTargetAddressSpace(AS: T.getAddressSpace()); |
837 | } |
838 | |