1 | //===- SystemZ.cpp --------------------------------------------------------===// |
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 | #include "ABIInfoImpl.h" |
10 | #include "TargetInfo.h" |
11 | #include "clang/Basic/Builtins.h" |
12 | #include "llvm/IR/IntrinsicsS390.h" |
13 | |
14 | using namespace clang; |
15 | using namespace clang::CodeGen; |
16 | |
17 | //===----------------------------------------------------------------------===// |
18 | // SystemZ ABI Implementation |
19 | //===----------------------------------------------------------------------===// |
20 | |
21 | namespace { |
22 | |
23 | class SystemZABIInfo : public ABIInfo { |
24 | bool HasVector; |
25 | bool IsSoftFloatABI; |
26 | |
27 | public: |
28 | SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF) |
29 | : ABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {} |
30 | |
31 | bool isPromotableIntegerTypeForABI(QualType Ty) const; |
32 | bool isCompoundType(QualType Ty) const; |
33 | bool isVectorArgumentType(QualType Ty) const; |
34 | bool isFPArgumentType(QualType Ty) const; |
35 | QualType GetSingleElementType(QualType Ty) const; |
36 | |
37 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
38 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
39 | |
40 | void computeInfo(CGFunctionInfo &FI) const override; |
41 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
42 | QualType Ty) const override; |
43 | }; |
44 | |
45 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
46 | ASTContext &Ctx; |
47 | |
48 | // These are used for speeding up the search for a visible vector ABI. |
49 | mutable bool HasVisibleVecABIFlag = false; |
50 | mutable std::set<const Type *> SeenTypes; |
51 | |
52 | // Returns true (the first time) if Ty is, or is found to include, a vector |
53 | // type that exposes the vector ABI. This is any vector >=16 bytes which |
54 | // with vector support are aligned to only 8 bytes. When IsParam is true, |
55 | // the type belongs to a value as passed between functions. If it is a |
56 | // vector <=16 bytes it will be passed in a vector register (if supported). |
57 | bool isVectorTypeBased(const Type *Ty, bool IsParam) const; |
58 | |
59 | public: |
60 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI) |
61 | : TargetCodeGenInfo( |
62 | std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)), |
63 | Ctx(CGT.getContext()) { |
64 | SwiftInfo = |
65 | std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false); |
66 | } |
67 | |
68 | // The vector ABI is different when the vector facility is present and when |
69 | // a module e.g. defines an externally visible vector variable, a flag |
70 | // indicating a visible vector ABI is added. Eventually this will result in |
71 | // a GNU attribute indicating the vector ABI of the module. Ty is the type |
72 | // of a variable or function parameter that is globally visible. |
73 | void handleExternallyVisibleObjABI(const Type *Ty, CodeGen::CodeGenModule &M, |
74 | bool IsParam) const { |
75 | if (!HasVisibleVecABIFlag && isVectorTypeBased(Ty, IsParam)) { |
76 | M.getModule().addModuleFlag(Behavior: llvm::Module::Warning, |
77 | Key: "s390x-visible-vector-ABI" , Val: 1); |
78 | HasVisibleVecABIFlag = true; |
79 | } |
80 | } |
81 | |
82 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
83 | CodeGen::CodeGenModule &M) const override { |
84 | if (!D) |
85 | return; |
86 | |
87 | // Check if the vector ABI becomes visible by an externally visible |
88 | // variable or function. |
89 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
90 | if (VD->isExternallyVisible()) |
91 | handleExternallyVisibleObjABI(Ty: VD->getType().getTypePtr(), M, |
92 | /*IsParam*/false); |
93 | } |
94 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
95 | if (FD->isExternallyVisible()) |
96 | handleExternallyVisibleObjABI(Ty: FD->getType().getTypePtr(), M, |
97 | /*IsParam*/false); |
98 | } |
99 | } |
100 | |
101 | llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID, |
102 | CGBuilderTy &Builder, |
103 | CodeGenModule &CGM) const override { |
104 | assert(V->getType()->isFloatingPointTy() && "V should have an FP type." ); |
105 | // Only use TDC in constrained FP mode. |
106 | if (!Builder.getIsFPConstrained()) |
107 | return nullptr; |
108 | |
109 | llvm::Type *Ty = V->getType(); |
110 | if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) { |
111 | llvm::Module &M = CGM.getModule(); |
112 | auto &Ctx = M.getContext(); |
113 | llvm::Function *TDCFunc = |
114 | llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty); |
115 | unsigned TDCBits = 0; |
116 | switch (BuiltinID) { |
117 | case Builtin::BI__builtin_isnan: |
118 | TDCBits = 0xf; |
119 | break; |
120 | case Builtin::BIfinite: |
121 | case Builtin::BI__finite: |
122 | case Builtin::BIfinitef: |
123 | case Builtin::BI__finitef: |
124 | case Builtin::BIfinitel: |
125 | case Builtin::BI__finitel: |
126 | case Builtin::BI__builtin_isfinite: |
127 | TDCBits = 0xfc0; |
128 | break; |
129 | case Builtin::BI__builtin_isinf: |
130 | TDCBits = 0x30; |
131 | break; |
132 | default: |
133 | break; |
134 | } |
135 | if (TDCBits) |
136 | return Builder.CreateCall( |
137 | TDCFunc, |
138 | {V, llvm::ConstantInt::get(Ty: llvm::Type::getInt64Ty(C&: Ctx), V: TDCBits)}); |
139 | } |
140 | return nullptr; |
141 | } |
142 | }; |
143 | } |
144 | |
145 | bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const { |
146 | // Treat an enum type as its underlying type. |
147 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
148 | Ty = EnumTy->getDecl()->getIntegerType(); |
149 | |
150 | // Promotable integer types are required to be promoted by the ABI. |
151 | if (ABIInfo::isPromotableIntegerTypeForABI(Ty)) |
152 | return true; |
153 | |
154 | if (const auto *EIT = Ty->getAs<BitIntType>()) |
155 | if (EIT->getNumBits() < 64) |
156 | return true; |
157 | |
158 | // 32-bit values must also be promoted. |
159 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
160 | switch (BT->getKind()) { |
161 | case BuiltinType::Int: |
162 | case BuiltinType::UInt: |
163 | return true; |
164 | default: |
165 | return false; |
166 | } |
167 | return false; |
168 | } |
169 | |
170 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
171 | return (Ty->isAnyComplexType() || |
172 | Ty->isVectorType() || |
173 | isAggregateTypeForABI(T: Ty)); |
174 | } |
175 | |
176 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
177 | return (HasVector && |
178 | Ty->isVectorType() && |
179 | getContext().getTypeSize(T: Ty) <= 128); |
180 | } |
181 | |
182 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
183 | if (IsSoftFloatABI) |
184 | return false; |
185 | |
186 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
187 | switch (BT->getKind()) { |
188 | case BuiltinType::Float: |
189 | case BuiltinType::Double: |
190 | return true; |
191 | default: |
192 | return false; |
193 | } |
194 | |
195 | return false; |
196 | } |
197 | |
198 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
199 | const RecordType *RT = Ty->getAs<RecordType>(); |
200 | |
201 | if (RT && RT->isStructureOrClassType()) { |
202 | const RecordDecl *RD = RT->getDecl(); |
203 | QualType Found; |
204 | |
205 | // If this is a C++ record, check the bases first. |
206 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) |
207 | if (CXXRD->hasDefinition()) |
208 | for (const auto &I : CXXRD->bases()) { |
209 | QualType Base = I.getType(); |
210 | |
211 | // Empty bases don't affect things either way. |
212 | if (isEmptyRecord(Context&: getContext(), T: Base, AllowArrays: true)) |
213 | continue; |
214 | |
215 | if (!Found.isNull()) |
216 | return Ty; |
217 | Found = GetSingleElementType(Ty: Base); |
218 | } |
219 | |
220 | // Check the fields. |
221 | for (const auto *FD : RD->fields()) { |
222 | // Unlike isSingleElementStruct(), empty structure and array fields |
223 | // do count. So do anonymous bitfields that aren't zero-sized. |
224 | |
225 | // Like isSingleElementStruct(), ignore C++20 empty data members. |
226 | if (FD->hasAttr<NoUniqueAddressAttr>() && |
227 | isEmptyRecord(getContext(), FD->getType(), true)) |
228 | continue; |
229 | |
230 | // Unlike isSingleElementStruct(), arrays do not count. |
231 | // Nested structures still do though. |
232 | if (!Found.isNull()) |
233 | return Ty; |
234 | Found = GetSingleElementType(Ty: FD->getType()); |
235 | } |
236 | |
237 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
238 | // An 8-byte aligned struct s { float f; } is passed as a double. |
239 | if (!Found.isNull()) |
240 | return Found; |
241 | } |
242 | |
243 | return Ty; |
244 | } |
245 | |
246 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
247 | QualType Ty) const { |
248 | // Assume that va_list type is correct; should be pointer to LLVM type: |
249 | // struct { |
250 | // i64 __gpr; |
251 | // i64 __fpr; |
252 | // i8 *__overflow_arg_area; |
253 | // i8 *__reg_save_area; |
254 | // }; |
255 | |
256 | // Every non-vector argument occupies 8 bytes and is passed by preference |
257 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
258 | // always passed on the stack. |
259 | const SystemZTargetCodeGenInfo &SZCGI = |
260 | static_cast<const SystemZTargetCodeGenInfo &>( |
261 | CGT.getCGM().getTargetCodeGenInfo()); |
262 | Ty = getContext().getCanonicalType(T: Ty); |
263 | auto TyInfo = getContext().getTypeInfoInChars(T: Ty); |
264 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(T: Ty); |
265 | llvm::Type *DirectTy = ArgTy; |
266 | ABIArgInfo AI = classifyArgumentType(ArgTy: Ty); |
267 | bool IsIndirect = AI.isIndirect(); |
268 | bool InFPRs = false; |
269 | bool IsVector = false; |
270 | CharUnits UnpaddedSize; |
271 | CharUnits DirectAlign; |
272 | SZCGI.handleExternallyVisibleObjABI(Ty: Ty.getTypePtr(), M&: CGT.getCGM(), |
273 | /*IsParam*/true); |
274 | if (IsIndirect) { |
275 | DirectTy = llvm::PointerType::getUnqual(ElementType: DirectTy); |
276 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(Quantity: 8); |
277 | } else { |
278 | if (AI.getCoerceToType()) |
279 | ArgTy = AI.getCoerceToType(); |
280 | InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy())); |
281 | IsVector = ArgTy->isVectorTy(); |
282 | UnpaddedSize = TyInfo.Width; |
283 | DirectAlign = TyInfo.Align; |
284 | } |
285 | CharUnits PaddedSize = CharUnits::fromQuantity(Quantity: 8); |
286 | if (IsVector && UnpaddedSize > PaddedSize) |
287 | PaddedSize = CharUnits::fromQuantity(Quantity: 16); |
288 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size." ); |
289 | |
290 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
291 | |
292 | llvm::Type *IndexTy = CGF.Int64Ty; |
293 | llvm::Value *PaddedSizeV = |
294 | llvm::ConstantInt::get(Ty: IndexTy, V: PaddedSize.getQuantity()); |
295 | |
296 | if (IsVector) { |
297 | // Work out the address of a vector argument on the stack. |
298 | // Vector arguments are always passed in the high bits of a |
299 | // single (8 byte) or double (16 byte) stack slot. |
300 | Address OverflowArgAreaPtr = |
301 | CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: 2, Name: "overflow_arg_area_ptr" ); |
302 | Address OverflowArgArea = |
303 | Address(CGF.Builder.CreateLoad(Addr: OverflowArgAreaPtr, Name: "overflow_arg_area" ), |
304 | CGF.Int8Ty, TyInfo.Align); |
305 | Address MemAddr = OverflowArgArea.withElementType(ElemTy: DirectTy); |
306 | |
307 | // Update overflow_arg_area_ptr pointer |
308 | llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP( |
309 | Ty: OverflowArgArea.getElementType(), Ptr: OverflowArgArea.emitRawPointer(CGF), |
310 | IdxList: PaddedSizeV, Name: "overflow_arg_area" ); |
311 | CGF.Builder.CreateStore(Val: NewOverflowArgArea, Addr: OverflowArgAreaPtr); |
312 | |
313 | return MemAddr; |
314 | } |
315 | |
316 | assert(PaddedSize.getQuantity() == 8); |
317 | |
318 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
319 | CharUnits RegPadding; |
320 | if (InFPRs) { |
321 | MaxRegs = 4; // Maximum of 4 FPR arguments |
322 | RegCountField = 1; // __fpr |
323 | RegSaveIndex = 16; // save offset for f0 |
324 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
325 | } else { |
326 | MaxRegs = 5; // Maximum of 5 GPR arguments |
327 | RegCountField = 0; // __gpr |
328 | RegSaveIndex = 2; // save offset for r2 |
329 | RegPadding = Padding; // values are passed in the low bits of a GPR |
330 | } |
331 | |
332 | Address RegCountPtr = |
333 | CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: RegCountField, Name: "reg_count_ptr" ); |
334 | llvm::Value *RegCount = CGF.Builder.CreateLoad(Addr: RegCountPtr, Name: "reg_count" ); |
335 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(Ty: IndexTy, V: MaxRegs); |
336 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(LHS: RegCount, RHS: MaxRegsV, |
337 | Name: "fits_in_regs" ); |
338 | |
339 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock(name: "vaarg.in_reg" ); |
340 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock(name: "vaarg.in_mem" ); |
341 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock(name: "vaarg.end" ); |
342 | CGF.Builder.CreateCondBr(Cond: InRegs, True: InRegBlock, False: InMemBlock); |
343 | |
344 | // Emit code to load the value if it was passed in registers. |
345 | CGF.EmitBlock(BB: InRegBlock); |
346 | |
347 | // Work out the address of an argument register. |
348 | llvm::Value *ScaledRegCount = |
349 | CGF.Builder.CreateMul(LHS: RegCount, RHS: PaddedSizeV, Name: "scaled_reg_count" ); |
350 | llvm::Value *RegBase = |
351 | llvm::ConstantInt::get(Ty: IndexTy, V: RegSaveIndex * PaddedSize.getQuantity() |
352 | + RegPadding.getQuantity()); |
353 | llvm::Value *RegOffset = |
354 | CGF.Builder.CreateAdd(LHS: ScaledRegCount, RHS: RegBase, Name: "reg_offset" ); |
355 | Address RegSaveAreaPtr = |
356 | CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: 3, Name: "reg_save_area_ptr" ); |
357 | llvm::Value *RegSaveArea = |
358 | CGF.Builder.CreateLoad(Addr: RegSaveAreaPtr, Name: "reg_save_area" ); |
359 | Address RawRegAddr( |
360 | CGF.Builder.CreateGEP(Ty: CGF.Int8Ty, Ptr: RegSaveArea, IdxList: RegOffset, Name: "raw_reg_addr" ), |
361 | CGF.Int8Ty, PaddedSize); |
362 | Address RegAddr = RawRegAddr.withElementType(ElemTy: DirectTy); |
363 | |
364 | // Update the register count |
365 | llvm::Value *One = llvm::ConstantInt::get(Ty: IndexTy, V: 1); |
366 | llvm::Value *NewRegCount = |
367 | CGF.Builder.CreateAdd(LHS: RegCount, RHS: One, Name: "reg_count" ); |
368 | CGF.Builder.CreateStore(Val: NewRegCount, Addr: RegCountPtr); |
369 | CGF.EmitBranch(Block: ContBlock); |
370 | |
371 | // Emit code to load the value if it was passed in memory. |
372 | CGF.EmitBlock(BB: InMemBlock); |
373 | |
374 | // Work out the address of a stack argument. |
375 | Address OverflowArgAreaPtr = |
376 | CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: 2, Name: "overflow_arg_area_ptr" ); |
377 | Address OverflowArgArea = |
378 | Address(CGF.Builder.CreateLoad(Addr: OverflowArgAreaPtr, Name: "overflow_arg_area" ), |
379 | CGF.Int8Ty, PaddedSize); |
380 | Address RawMemAddr = |
381 | CGF.Builder.CreateConstByteGEP(Addr: OverflowArgArea, Offset: Padding, Name: "raw_mem_addr" ); |
382 | Address MemAddr = RawMemAddr.withElementType(ElemTy: DirectTy); |
383 | |
384 | // Update overflow_arg_area_ptr pointer |
385 | llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP( |
386 | Ty: OverflowArgArea.getElementType(), Ptr: OverflowArgArea.emitRawPointer(CGF), |
387 | IdxList: PaddedSizeV, Name: "overflow_arg_area" ); |
388 | CGF.Builder.CreateStore(Val: NewOverflowArgArea, Addr: OverflowArgAreaPtr); |
389 | CGF.EmitBranch(Block: ContBlock); |
390 | |
391 | // Return the appropriate result. |
392 | CGF.EmitBlock(BB: ContBlock); |
393 | Address ResAddr = emitMergePHI(CGF, Addr1: RegAddr, Block1: InRegBlock, Addr2: MemAddr, Block2: InMemBlock, |
394 | Name: "va_arg.addr" ); |
395 | |
396 | if (IsIndirect) |
397 | ResAddr = Address(CGF.Builder.CreateLoad(Addr: ResAddr, Name: "indirect_arg" ), ArgTy, |
398 | TyInfo.Align); |
399 | |
400 | return ResAddr; |
401 | } |
402 | |
403 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
404 | if (RetTy->isVoidType()) |
405 | return ABIArgInfo::getIgnore(); |
406 | if (isVectorArgumentType(Ty: RetTy)) |
407 | return ABIArgInfo::getDirect(); |
408 | if (isCompoundType(Ty: RetTy) || getContext().getTypeSize(T: RetTy) > 64) |
409 | return getNaturalAlignIndirect(Ty: RetTy); |
410 | return (isPromotableIntegerTypeForABI(Ty: RetTy) ? ABIArgInfo::getExtend(Ty: RetTy) |
411 | : ABIArgInfo::getDirect()); |
412 | } |
413 | |
414 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
415 | // Handle the generic C++ ABI. |
416 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(T: Ty, CXXABI&: getCXXABI())) |
417 | return getNaturalAlignIndirect(Ty, ByVal: RAA == CGCXXABI::RAA_DirectInMemory); |
418 | |
419 | // Integers and enums are extended to full register width. |
420 | if (isPromotableIntegerTypeForABI(Ty)) |
421 | return ABIArgInfo::getExtend(Ty); |
422 | |
423 | // Handle vector types and vector-like structure types. Note that |
424 | // as opposed to float-like structure types, we do not allow any |
425 | // padding for vector-like structures, so verify the sizes match. |
426 | uint64_t Size = getContext().getTypeSize(T: Ty); |
427 | QualType SingleElementTy = GetSingleElementType(Ty); |
428 | if (isVectorArgumentType(Ty: SingleElementTy) && |
429 | getContext().getTypeSize(T: SingleElementTy) == Size) |
430 | return ABIArgInfo::getDirect(T: CGT.ConvertType(T: SingleElementTy)); |
431 | |
432 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
433 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
434 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
435 | |
436 | // Handle small structures. |
437 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
438 | // Structures with flexible arrays have variable length, so really |
439 | // fail the size test above. |
440 | const RecordDecl *RD = RT->getDecl(); |
441 | if (RD->hasFlexibleArrayMember()) |
442 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
443 | |
444 | // The structure is passed as an unextended integer, a float, or a double. |
445 | llvm::Type *PassTy; |
446 | if (isFPArgumentType(Ty: SingleElementTy)) { |
447 | assert(Size == 32 || Size == 64); |
448 | if (Size == 32) |
449 | PassTy = llvm::Type::getFloatTy(C&: getVMContext()); |
450 | else |
451 | PassTy = llvm::Type::getDoubleTy(C&: getVMContext()); |
452 | } else |
453 | PassTy = llvm::IntegerType::get(C&: getVMContext(), NumBits: Size); |
454 | return ABIArgInfo::getDirect(T: PassTy); |
455 | } |
456 | |
457 | // Non-structure compounds are passed indirectly. |
458 | if (isCompoundType(Ty)) |
459 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
460 | |
461 | return ABIArgInfo::getDirect(T: nullptr); |
462 | } |
463 | |
464 | void SystemZABIInfo::computeInfo(CGFunctionInfo &FI) const { |
465 | const SystemZTargetCodeGenInfo &SZCGI = |
466 | static_cast<const SystemZTargetCodeGenInfo &>( |
467 | CGT.getCGM().getTargetCodeGenInfo()); |
468 | if (!getCXXABI().classifyReturnType(FI)) |
469 | FI.getReturnInfo() = classifyReturnType(RetTy: FI.getReturnType()); |
470 | unsigned Idx = 0; |
471 | for (auto &I : FI.arguments()) { |
472 | I.info = classifyArgumentType(Ty: I.type); |
473 | if (FI.isVariadic() && Idx++ >= FI.getNumRequiredArgs()) |
474 | // Check if a vararg vector argument is passed, in which case the |
475 | // vector ABI becomes visible as the va_list could be passed on to |
476 | // other functions. |
477 | SZCGI.handleExternallyVisibleObjABI(Ty: I.type.getTypePtr(), M&: CGT.getCGM(), |
478 | /*IsParam*/true); |
479 | } |
480 | } |
481 | |
482 | bool SystemZTargetCodeGenInfo::isVectorTypeBased(const Type *Ty, |
483 | bool IsParam) const { |
484 | if (!SeenTypes.insert(Ty).second) |
485 | return false; |
486 | |
487 | if (IsParam) { |
488 | // A narrow (<16 bytes) vector will as a parameter also expose the ABI as |
489 | // it will be passed in a vector register. A wide (>16 bytes) vector will |
490 | // be passed via "hidden" pointer where any extra alignment is not |
491 | // required (per GCC). |
492 | const Type *SingleEltTy = getABIInfo<SystemZABIInfo>() |
493 | .GetSingleElementType(QualType(Ty, 0)) |
494 | .getTypePtr(); |
495 | bool SingleVecEltStruct = SingleEltTy != Ty && SingleEltTy->isVectorType() && |
496 | Ctx.getTypeSize(T: SingleEltTy) == Ctx.getTypeSize(T: Ty); |
497 | if (Ty->isVectorType() || SingleVecEltStruct) |
498 | return Ctx.getTypeSize(T: Ty) / 8 <= 16; |
499 | } |
500 | |
501 | // Assume pointers are dereferenced. |
502 | while (Ty->isPointerType() || Ty->isArrayType()) |
503 | Ty = Ty->getPointeeOrArrayElementType(); |
504 | |
505 | // Vectors >= 16 bytes expose the ABI through alignment requirements. |
506 | if (Ty->isVectorType() && Ctx.getTypeSize(T: Ty) / 8 >= 16) |
507 | return true; |
508 | |
509 | if (const auto *RecordTy = Ty->getAs<RecordType>()) { |
510 | const RecordDecl *RD = RecordTy->getDecl(); |
511 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) |
512 | if (CXXRD->hasDefinition()) |
513 | for (const auto &I : CXXRD->bases()) |
514 | if (isVectorTypeBased(Ty: I.getType().getTypePtr(), /*IsParam*/false)) |
515 | return true; |
516 | for (const auto *FD : RD->fields()) |
517 | if (isVectorTypeBased(Ty: FD->getType().getTypePtr(), /*IsParam*/false)) |
518 | return true; |
519 | } |
520 | |
521 | if (const auto *FT = Ty->getAs<FunctionType>()) |
522 | if (isVectorTypeBased(Ty: FT->getReturnType().getTypePtr(), /*IsParam*/true)) |
523 | return true; |
524 | if (const FunctionProtoType *Proto = Ty->getAs<FunctionProtoType>()) |
525 | for (const auto &ParamType : Proto->getParamTypes()) |
526 | if (isVectorTypeBased(Ty: ParamType.getTypePtr(), /*IsParam*/true)) |
527 | return true; |
528 | |
529 | return false; |
530 | } |
531 | |
532 | std::unique_ptr<TargetCodeGenInfo> |
533 | CodeGen::createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector, |
534 | bool SoftFloatABI) { |
535 | return std::make_unique<SystemZTargetCodeGenInfo>(args&: CGM.getTypes(), args&: HasVector, |
536 | args&: SoftFloatABI); |
537 | } |
538 | |