1 //===- PPC.cpp ------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ABIInfoImpl.h"
10 #include "TargetInfo.h"
11 #include "clang/Basic/DiagnosticFrontend.h"
13 using namespace clang
;
14 using namespace clang::CodeGen
;
16 static RValue
complexTempStructure(CodeGenFunction
&CGF
, Address VAListAddr
,
17 QualType Ty
, CharUnits SlotSize
,
18 CharUnits EltSize
, const ComplexType
*CTy
) {
20 emitVoidPtrDirectVAArg(CGF
, VAListAddr
, CGF
.Int8Ty
, SlotSize
* 2,
21 SlotSize
, SlotSize
, /*AllowHigher*/ true);
23 Address RealAddr
= Addr
;
24 Address ImagAddr
= RealAddr
;
25 if (CGF
.CGM
.getDataLayout().isBigEndian()) {
27 CGF
.Builder
.CreateConstInBoundsByteGEP(RealAddr
, SlotSize
- EltSize
);
28 ImagAddr
= CGF
.Builder
.CreateConstInBoundsByteGEP(ImagAddr
,
29 2 * SlotSize
- EltSize
);
31 ImagAddr
= CGF
.Builder
.CreateConstInBoundsByteGEP(RealAddr
, SlotSize
);
34 llvm::Type
*EltTy
= CGF
.ConvertTypeForMem(CTy
->getElementType());
35 RealAddr
= RealAddr
.withElementType(EltTy
);
36 ImagAddr
= ImagAddr
.withElementType(EltTy
);
37 llvm::Value
*Real
= CGF
.Builder
.CreateLoad(RealAddr
, ".vareal");
38 llvm::Value
*Imag
= CGF
.Builder
.CreateLoad(ImagAddr
, ".vaimag");
40 return RValue::getComplex(Real
, Imag
);
43 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
44 llvm::Value
*Address
, bool Is64Bit
,
46 // This is calculated from the LLVM and GCC tables and verified
47 // against gcc output. AFAIK all PPC ABIs use the same encoding.
49 CodeGen::CGBuilderTy
&Builder
= CGF
.Builder
;
51 llvm::IntegerType
*i8
= CGF
.Int8Ty
;
52 llvm::Value
*Four8
= llvm::ConstantInt::get(i8
, 4);
53 llvm::Value
*Eight8
= llvm::ConstantInt::get(i8
, 8);
54 llvm::Value
*Sixteen8
= llvm::ConstantInt::get(i8
, 16);
56 // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
57 AssignToArrayRange(Builder
, Address
, Is64Bit
? Eight8
: Four8
, 0, 31);
59 // 32-63: fp0-31, the 8-byte floating-point registers
60 AssignToArrayRange(Builder
, Address
, Eight8
, 32, 63);
62 // 64-67 are various 4-byte or 8-byte special-purpose registers:
67 AssignToArrayRange(Builder
, Address
, Is64Bit
? Eight8
: Four8
, 64, 67);
69 // 68-76 are various 4-byte special-purpose registers:
72 AssignToArrayRange(Builder
, Address
, Four8
, 68, 76);
74 // 77-108: v0-31, the 16-byte vector registers
75 AssignToArrayRange(Builder
, Address
, Sixteen8
, 77, 108);
79 AssignToArrayRange(Builder
, Address
, Is64Bit
? Eight8
: Four8
, 109, 110);
81 // AIX does not utilize the rest of the registers.
88 AssignToArrayRange(Builder
, Address
, Is64Bit
? Eight8
: Four8
, 111, 113);
93 // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
95 // 64-bit only registers:
99 AssignToArrayRange(Builder
, Address
, Eight8
, 114, 116);
106 /// AIXABIInfo - The AIX XCOFF ABI information.
107 class AIXABIInfo
: public ABIInfo
{
109 const unsigned PtrByteSize
;
110 CharUnits
getParamTypeAlignment(QualType Ty
) const;
113 AIXABIInfo(CodeGen::CodeGenTypes
&CGT
, bool Is64Bit
)
114 : ABIInfo(CGT
), Is64Bit(Is64Bit
), PtrByteSize(Is64Bit
? 8 : 4) {}
116 bool isPromotableTypeForABI(QualType Ty
) const;
118 ABIArgInfo
classifyReturnType(QualType RetTy
) const;
119 ABIArgInfo
classifyArgumentType(QualType Ty
) const;
121 void computeInfo(CGFunctionInfo
&FI
) const override
{
122 if (!getCXXABI().classifyReturnType(FI
))
123 FI
.getReturnInfo() = classifyReturnType(FI
.getReturnType());
125 for (auto &I
: FI
.arguments())
126 I
.info
= classifyArgumentType(I
.type
);
129 RValue
EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
, QualType Ty
,
130 AggValueSlot Slot
) const override
;
133 class AIXTargetCodeGenInfo
: public TargetCodeGenInfo
{
137 AIXTargetCodeGenInfo(CodeGen::CodeGenTypes
&CGT
, bool Is64Bit
)
138 : TargetCodeGenInfo(std::make_unique
<AIXABIInfo
>(CGT
, Is64Bit
)),
140 int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const override
{
141 return 1; // r1 is the dedicated stack pointer
144 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
145 llvm::Value
*Address
) const override
;
147 void setTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
148 CodeGen::CodeGenModule
&M
) const override
;
152 // Return true if the ABI requires Ty to be passed sign- or zero-
153 // extended to 32/64 bits.
154 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty
) const {
155 // Treat an enum type as its underlying type.
156 if (const EnumType
*EnumTy
= Ty
->getAs
<EnumType
>())
157 Ty
= EnumTy
->getDecl()->getIntegerType();
159 // Promotable integer types are required to be promoted by the ABI.
160 if (getContext().isPromotableIntegerType(Ty
))
166 // For 64 bit mode, in addition to the usual promotable integer types, we also
167 // need to extend all 32-bit types, since the ABI requires promotion to 64
169 if (const BuiltinType
*BT
= Ty
->getAs
<BuiltinType
>())
170 switch (BT
->getKind()) {
171 case BuiltinType::Int
:
172 case BuiltinType::UInt
:
181 ABIArgInfo
AIXABIInfo::classifyReturnType(QualType RetTy
) const {
182 if (RetTy
->isAnyComplexType())
183 return ABIArgInfo::getDirect();
185 if (RetTy
->isVectorType())
186 return ABIArgInfo::getDirect();
188 if (RetTy
->isVoidType())
189 return ABIArgInfo::getIgnore();
191 if (isAggregateTypeForABI(RetTy
))
192 return getNaturalAlignIndirect(RetTy
);
194 return (isPromotableTypeForABI(RetTy
) ? ABIArgInfo::getExtend(RetTy
)
195 : ABIArgInfo::getDirect());
198 ABIArgInfo
AIXABIInfo::classifyArgumentType(QualType Ty
) const {
199 Ty
= useFirstFieldIfTransparentUnion(Ty
);
201 if (Ty
->isAnyComplexType())
202 return ABIArgInfo::getDirect();
204 if (Ty
->isVectorType())
205 return ABIArgInfo::getDirect();
207 if (isAggregateTypeForABI(Ty
)) {
208 // Records with non-trivial destructors/copy-constructors should not be
210 if (CGCXXABI::RecordArgABI RAA
= getRecordArgABI(Ty
, getCXXABI()))
211 return getNaturalAlignIndirect(Ty
, RAA
== CGCXXABI::RAA_DirectInMemory
);
213 CharUnits CCAlign
= getParamTypeAlignment(Ty
);
214 CharUnits TyAlign
= getContext().getTypeAlignInChars(Ty
);
216 return ABIArgInfo::getIndirect(CCAlign
, /*ByVal*/ true,
217 /*Realign*/ TyAlign
> CCAlign
);
220 return (isPromotableTypeForABI(Ty
)
221 ? ABIArgInfo::getExtend(Ty
, CGT
.ConvertType(Ty
))
222 : ABIArgInfo::getDirect());
225 CharUnits
AIXABIInfo::getParamTypeAlignment(QualType Ty
) const {
226 // Complex types are passed just like their elements.
227 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>())
228 Ty
= CTy
->getElementType();
230 if (Ty
->isVectorType())
231 return CharUnits::fromQuantity(16);
233 // If the structure contains a vector type, the alignment is 16.
234 if (isRecordWithSIMDVectorType(getContext(), Ty
))
235 return CharUnits::fromQuantity(16);
237 return CharUnits::fromQuantity(PtrByteSize
);
240 RValue
AIXABIInfo::EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
241 QualType Ty
, AggValueSlot Slot
) const {
243 auto TypeInfo
= getContext().getTypeInfoInChars(Ty
);
244 TypeInfo
.Align
= getParamTypeAlignment(Ty
);
246 CharUnits SlotSize
= CharUnits::fromQuantity(PtrByteSize
);
248 // If we have a complex type and the base type is smaller than the register
249 // size, the ABI calls for the real and imaginary parts to be right-adjusted
250 // in separate words in 32bit mode or doublewords in 64bit mode. However,
251 // Clang expects us to produce a pointer to a structure with the two parts
252 // packed tightly. So generate loads of the real and imaginary parts relative
253 // to the va_list pointer, and store them to a temporary structure. We do the
254 // same as the PPC64ABI here.
255 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>()) {
256 CharUnits EltSize
= TypeInfo
.Width
/ 2;
257 if (EltSize
< SlotSize
)
258 return complexTempStructure(CGF
, VAListAddr
, Ty
, SlotSize
, EltSize
, CTy
);
261 return emitVoidPtrVAArg(CGF
, VAListAddr
, Ty
, /*Indirect*/ false, TypeInfo
,
262 SlotSize
, /*AllowHigher*/ true, Slot
);
265 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
266 CodeGen::CodeGenFunction
&CGF
, llvm::Value
*Address
) const {
267 return PPC_initDwarfEHRegSizeTable(CGF
, Address
, Is64Bit
, /*IsAIX*/ true);
270 void AIXTargetCodeGenInfo::setTargetAttributes(
271 const Decl
*D
, llvm::GlobalValue
*GV
, CodeGen::CodeGenModule
&M
) const {
272 if (!isa
<llvm::GlobalVariable
>(GV
))
275 auto *GVar
= cast
<llvm::GlobalVariable
>(GV
);
276 auto GVId
= GV
->getName();
278 // Is this a global variable specified by the user as toc-data?
279 bool UserSpecifiedTOC
=
280 llvm::binary_search(M
.getCodeGenOpts().TocDataVarsUserSpecified
, GVId
);
281 // Assumes the same variable cannot be in both TocVarsUserSpecified and
283 if (UserSpecifiedTOC
||
284 ((M
.getCodeGenOpts().AllTocData
) &&
285 !llvm::binary_search(M
.getCodeGenOpts().NoTocDataVars
, GVId
))) {
286 const unsigned long PointerSize
=
287 GV
->getParent()->getDataLayout().getPointerSizeInBits() / 8;
288 auto *VarD
= dyn_cast
<VarDecl
>(D
);
289 assert(VarD
&& "Invalid declaration of global variable.");
291 ASTContext
&Context
= D
->getASTContext();
292 unsigned Alignment
= Context
.toBits(Context
.getDeclAlign(D
)) / 8;
293 const auto *Ty
= VarD
->getType().getTypePtr();
294 const RecordDecl
*RDecl
=
295 Ty
->isRecordType() ? Ty
->getAs
<RecordType
>()->getDecl() : nullptr;
297 bool EmitDiagnostic
= UserSpecifiedTOC
&& GV
->hasExternalLinkage();
298 auto reportUnsupportedWarning
= [&](bool ShouldEmitWarning
, StringRef Msg
) {
299 if (ShouldEmitWarning
)
300 M
.getDiags().Report(D
->getLocation(), diag::warn_toc_unsupported_type
)
303 if (!Ty
|| Ty
->isIncompleteType())
304 reportUnsupportedWarning(EmitDiagnostic
, "of incomplete type");
305 else if (RDecl
&& RDecl
->hasFlexibleArrayMember())
306 reportUnsupportedWarning(EmitDiagnostic
,
307 "it contains a flexible array member");
308 else if (VarD
->getTLSKind() != VarDecl::TLS_None
)
309 reportUnsupportedWarning(EmitDiagnostic
, "of thread local storage");
310 else if (PointerSize
< Context
.getTypeInfo(VarD
->getType()).Width
/ 8)
311 reportUnsupportedWarning(EmitDiagnostic
,
312 "variable is larger than a pointer");
313 else if (PointerSize
< Alignment
)
314 reportUnsupportedWarning(EmitDiagnostic
,
315 "variable is aligned wider than a pointer");
316 else if (D
->hasAttr
<SectionAttr
>())
317 reportUnsupportedWarning(EmitDiagnostic
,
318 "variable has a section attribute");
319 else if (GV
->hasExternalLinkage() ||
320 (M
.getCodeGenOpts().AllTocData
&& !GV
->hasLocalLinkage()))
321 GVar
->addAttribute("toc-data");
327 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
328 class PPC32_SVR4_ABIInfo
: public DefaultABIInfo
{
330 bool IsRetSmallStructInRegABI
;
332 CharUnits
getParamTypeAlignment(QualType Ty
) const;
335 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes
&CGT
, bool SoftFloatABI
,
336 bool RetSmallStructInRegABI
)
337 : DefaultABIInfo(CGT
), IsSoftFloatABI(SoftFloatABI
),
338 IsRetSmallStructInRegABI(RetSmallStructInRegABI
) {}
340 ABIArgInfo
classifyReturnType(QualType RetTy
) const;
342 void computeInfo(CGFunctionInfo
&FI
) const override
{
343 if (!getCXXABI().classifyReturnType(FI
))
344 FI
.getReturnInfo() = classifyReturnType(FI
.getReturnType());
345 for (auto &I
: FI
.arguments())
346 I
.info
= classifyArgumentType(I
.type
);
349 RValue
EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
, QualType Ty
,
350 AggValueSlot Slot
) const override
;
353 class PPC32TargetCodeGenInfo
: public TargetCodeGenInfo
{
355 PPC32TargetCodeGenInfo(CodeGenTypes
&CGT
, bool SoftFloatABI
,
356 bool RetSmallStructInRegABI
)
357 : TargetCodeGenInfo(std::make_unique
<PPC32_SVR4_ABIInfo
>(
358 CGT
, SoftFloatABI
, RetSmallStructInRegABI
)) {}
360 static bool isStructReturnInRegABI(const llvm::Triple
&Triple
,
361 const CodeGenOptions
&Opts
);
363 int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const override
{
364 // This is recovered from gcc output.
365 return 1; // r1 is the dedicated stack pointer
368 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
369 llvm::Value
*Address
) const override
;
373 CharUnits
PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty
) const {
374 // Complex types are passed just like their elements.
375 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>())
376 Ty
= CTy
->getElementType();
378 if (Ty
->isVectorType())
379 return CharUnits::fromQuantity(getContext().getTypeSize(Ty
) == 128 ? 16
382 // For single-element float/vector structs, we consider the whole type
383 // to have the same alignment requirements as its single element.
384 const Type
*AlignTy
= nullptr;
385 if (const Type
*EltType
= isSingleElementStruct(Ty
, getContext())) {
386 const BuiltinType
*BT
= EltType
->getAs
<BuiltinType
>();
387 if ((EltType
->isVectorType() && getContext().getTypeSize(EltType
) == 128) ||
388 (BT
&& BT
->isFloatingPoint()))
393 return CharUnits::fromQuantity(AlignTy
->isVectorType() ? 16 : 4);
394 return CharUnits::fromQuantity(4);
397 ABIArgInfo
PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy
) const {
400 // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
401 if (isAggregateTypeForABI(RetTy
) && IsRetSmallStructInRegABI
&&
402 (Size
= getContext().getTypeSize(RetTy
)) <= 64) {
403 // System V ABI (1995), page 3-22, specified:
404 // > A structure or union whose size is less than or equal to 8 bytes
405 // > shall be returned in r3 and r4, as if it were first stored in the
406 // > 8-byte aligned memory area and then the low addressed word were
407 // > loaded into r3 and the high-addressed word into r4. Bits beyond
408 // > the last member of the structure or union are not defined.
410 // GCC for big-endian PPC32 inserts the pad before the first member,
411 // not "beyond the last member" of the struct. To stay compatible
412 // with GCC, we coerce the struct to an integer of the same size.
413 // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
415 return ABIArgInfo::getIgnore();
417 llvm::Type
*CoerceTy
= llvm::Type::getIntNTy(getVMContext(), Size
);
418 return ABIArgInfo::getDirect(CoerceTy
);
422 return DefaultABIInfo::classifyReturnType(RetTy
);
425 // TODO: this implementation is now likely redundant with
426 // DefaultABIInfo::EmitVAArg.
427 RValue
PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction
&CGF
, Address VAList
,
428 QualType Ty
, AggValueSlot Slot
) const {
429 if (getTarget().getTriple().isOSDarwin()) {
430 auto TI
= getContext().getTypeInfoInChars(Ty
);
431 TI
.Align
= getParamTypeAlignment(Ty
);
433 CharUnits SlotSize
= CharUnits::fromQuantity(4);
434 return emitVoidPtrVAArg(CGF
, VAList
, Ty
,
435 classifyArgumentType(Ty
).isIndirect(), TI
, SlotSize
,
436 /*AllowHigherAlign=*/true, Slot
);
439 const unsigned OverflowLimit
= 8;
440 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>()) {
441 // TODO: Implement this. For now ignore.
443 return RValue::getAggregate(Address::invalid()); // FIXME?
446 // struct __va_list_tag {
447 // unsigned char gpr;
448 // unsigned char fpr;
449 // unsigned short reserved;
450 // void *overflow_arg_area;
451 // void *reg_save_area;
454 bool isI64
= Ty
->isIntegerType() && getContext().getTypeSize(Ty
) == 64;
455 bool isInt
= !Ty
->isFloatingType();
456 bool isF64
= Ty
->isFloatingType() && getContext().getTypeSize(Ty
) == 64;
458 // All aggregates are passed indirectly? That doesn't seem consistent
459 // with the argument-lowering code.
460 bool isIndirect
= isAggregateTypeForABI(Ty
);
462 CGBuilderTy
&Builder
= CGF
.Builder
;
464 // The calling convention either uses 1-2 GPRs or 1 FPR.
465 Address NumRegsAddr
= Address::invalid();
466 if (isInt
|| IsSoftFloatABI
) {
467 NumRegsAddr
= Builder
.CreateStructGEP(VAList
, 0, "gpr");
469 NumRegsAddr
= Builder
.CreateStructGEP(VAList
, 1, "fpr");
472 llvm::Value
*NumRegs
= Builder
.CreateLoad(NumRegsAddr
, "numUsedRegs");
474 // "Align" the register count when TY is i64.
475 if (isI64
|| (isF64
&& IsSoftFloatABI
)) {
476 NumRegs
= Builder
.CreateAdd(NumRegs
, Builder
.getInt8(1));
477 NumRegs
= Builder
.CreateAnd(NumRegs
, Builder
.getInt8((uint8_t) ~1U));
481 Builder
.CreateICmpULT(NumRegs
, Builder
.getInt8(OverflowLimit
), "cond");
483 llvm::BasicBlock
*UsingRegs
= CGF
.createBasicBlock("using_regs");
484 llvm::BasicBlock
*UsingOverflow
= CGF
.createBasicBlock("using_overflow");
485 llvm::BasicBlock
*Cont
= CGF
.createBasicBlock("cont");
487 Builder
.CreateCondBr(CC
, UsingRegs
, UsingOverflow
);
489 llvm::Type
*DirectTy
= CGF
.ConvertType(Ty
), *ElementTy
= DirectTy
;
491 DirectTy
= CGF
.UnqualPtrTy
;
493 // Case 1: consume registers.
494 Address RegAddr
= Address::invalid();
496 CGF
.EmitBlock(UsingRegs
);
498 Address RegSaveAreaPtr
= Builder
.CreateStructGEP(VAList
, 4);
499 RegAddr
= Address(Builder
.CreateLoad(RegSaveAreaPtr
), CGF
.Int8Ty
,
500 CharUnits::fromQuantity(8));
501 assert(RegAddr
.getElementType() == CGF
.Int8Ty
);
503 // Floating-point registers start after the general-purpose registers.
504 if (!(isInt
|| IsSoftFloatABI
)) {
505 RegAddr
= Builder
.CreateConstInBoundsByteGEP(RegAddr
,
506 CharUnits::fromQuantity(32));
509 // Get the address of the saved value by scaling the number of
510 // registers we've used by the number of
511 CharUnits RegSize
= CharUnits::fromQuantity((isInt
|| IsSoftFloatABI
) ? 4 : 8);
512 llvm::Value
*RegOffset
=
513 Builder
.CreateMul(NumRegs
, Builder
.getInt8(RegSize
.getQuantity()));
514 RegAddr
= Address(Builder
.CreateInBoundsGEP(
515 CGF
.Int8Ty
, RegAddr
.emitRawPointer(CGF
), RegOffset
),
517 RegAddr
.getAlignment().alignmentOfArrayElement(RegSize
));
519 // Increase the used-register count.
521 Builder
.CreateAdd(NumRegs
,
522 Builder
.getInt8((isI64
|| (isF64
&& IsSoftFloatABI
)) ? 2 : 1));
523 Builder
.CreateStore(NumRegs
, NumRegsAddr
);
525 CGF
.EmitBranch(Cont
);
528 // Case 2: consume space in the overflow area.
529 Address MemAddr
= Address::invalid();
531 CGF
.EmitBlock(UsingOverflow
);
533 Builder
.CreateStore(Builder
.getInt8(OverflowLimit
), NumRegsAddr
);
535 // Everything in the overflow area is rounded up to a size of at least 4.
536 CharUnits OverflowAreaAlign
= CharUnits::fromQuantity(4);
540 auto TypeInfo
= CGF
.getContext().getTypeInfoInChars(Ty
);
541 Size
= TypeInfo
.Width
.alignTo(OverflowAreaAlign
);
543 Size
= CGF
.getPointerSize();
546 Address OverflowAreaAddr
= Builder
.CreateStructGEP(VAList
, 3);
547 Address OverflowArea
=
548 Address(Builder
.CreateLoad(OverflowAreaAddr
, "argp.cur"), CGF
.Int8Ty
,
550 // Round up address of argument to alignment
551 CharUnits Align
= CGF
.getContext().getTypeAlignInChars(Ty
);
552 if (Align
> OverflowAreaAlign
) {
553 llvm::Value
*Ptr
= OverflowArea
.emitRawPointer(CGF
);
554 OverflowArea
= Address(emitRoundPointerUpToAlignment(CGF
, Ptr
, Align
),
555 OverflowArea
.getElementType(), Align
);
558 MemAddr
= OverflowArea
.withElementType(DirectTy
);
560 // Increase the overflow area.
561 OverflowArea
= Builder
.CreateConstInBoundsByteGEP(OverflowArea
, Size
);
562 Builder
.CreateStore(OverflowArea
.emitRawPointer(CGF
), OverflowAreaAddr
);
563 CGF
.EmitBranch(Cont
);
568 // Merge the cases with a phi.
569 Address Result
= emitMergePHI(CGF
, RegAddr
, UsingRegs
, MemAddr
, UsingOverflow
,
572 // Load the pointer if the argument was passed indirectly.
574 Result
= Address(Builder
.CreateLoad(Result
, "aggr"), ElementTy
,
575 getContext().getTypeAlignInChars(Ty
));
578 return CGF
.EmitLoadOfAnyValue(CGF
.MakeAddrLValue(Result
, Ty
), Slot
);
581 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
582 const llvm::Triple
&Triple
, const CodeGenOptions
&Opts
) {
583 assert(Triple
.isPPC32());
585 switch (Opts
.getStructReturnConvention()) {
586 case CodeGenOptions::SRCK_Default
:
588 case CodeGenOptions::SRCK_OnStack
: // -maix-struct-return
590 case CodeGenOptions::SRCK_InRegs
: // -msvr4-struct-return
594 if (Triple
.isOSBinFormatELF() && !Triple
.isOSLinux())
601 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
602 llvm::Value
*Address
) const {
603 return PPC_initDwarfEHRegSizeTable(CGF
, Address
, /*Is64Bit*/ false,
611 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
612 class PPC64_SVR4_ABIInfo
: public ABIInfo
{
613 static const unsigned GPRBits
= 64;
614 PPC64_SVR4_ABIKind Kind
;
618 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes
&CGT
, PPC64_SVR4_ABIKind Kind
,
620 : ABIInfo(CGT
), Kind(Kind
), IsSoftFloatABI(SoftFloatABI
) {}
622 bool isPromotableTypeForABI(QualType Ty
) const;
623 CharUnits
getParamTypeAlignment(QualType Ty
) const;
625 ABIArgInfo
classifyReturnType(QualType RetTy
) const;
626 ABIArgInfo
classifyArgumentType(QualType Ty
) const;
628 bool isHomogeneousAggregateBaseType(QualType Ty
) const override
;
629 bool isHomogeneousAggregateSmallEnough(const Type
*Ty
,
630 uint64_t Members
) const override
;
632 // TODO: We can add more logic to computeInfo to improve performance.
633 // Example: For aggregate arguments that fit in a register, we could
634 // use getDirectInReg (as is done below for structs containing a single
635 // floating-point value) to avoid pushing them to memory on function
636 // entry. This would require changing the logic in PPCISelLowering
637 // when lowering the parameters in the caller and args in the callee.
638 void computeInfo(CGFunctionInfo
&FI
) const override
{
639 if (!getCXXABI().classifyReturnType(FI
))
640 FI
.getReturnInfo() = classifyReturnType(FI
.getReturnType());
641 for (auto &I
: FI
.arguments()) {
642 // We rely on the default argument classification for the most part.
643 // One exception: An aggregate containing a single floating-point
644 // or vector item must be passed in a register if one is available.
645 const Type
*T
= isSingleElementStruct(I
.type
, getContext());
647 const BuiltinType
*BT
= T
->getAs
<BuiltinType
>();
648 if ((T
->isVectorType() && getContext().getTypeSize(T
) == 128) ||
649 (BT
&& BT
->isFloatingPoint())) {
651 I
.info
= ABIArgInfo::getDirectInReg(CGT
.ConvertType(QT
));
655 I
.info
= classifyArgumentType(I
.type
);
659 RValue
EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
, QualType Ty
,
660 AggValueSlot Slot
) const override
;
663 class PPC64_SVR4_TargetCodeGenInfo
: public TargetCodeGenInfo
{
666 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes
&CGT
, PPC64_SVR4_ABIKind Kind
,
669 std::make_unique
<PPC64_SVR4_ABIInfo
>(CGT
, Kind
, SoftFloatABI
)) {
671 std::make_unique
<SwiftABIInfo
>(CGT
, /*SwiftErrorInRegister=*/false);
674 int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const override
{
675 // This is recovered from gcc output.
676 return 1; // r1 is the dedicated stack pointer
679 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
680 llvm::Value
*Address
) const override
;
681 void emitTargetMetadata(CodeGen::CodeGenModule
&CGM
,
682 const llvm::MapVector
<GlobalDecl
, StringRef
>
683 &MangledDeclNames
) const override
;
686 class PPC64TargetCodeGenInfo
: public TargetCodeGenInfo
{
688 PPC64TargetCodeGenInfo(CodeGenTypes
&CGT
)
689 : TargetCodeGenInfo(std::make_unique
<DefaultABIInfo
>(CGT
)) {}
691 int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const override
{
692 // This is recovered from gcc output.
693 return 1; // r1 is the dedicated stack pointer
696 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
697 llvm::Value
*Address
) const override
;
701 // Return true if the ABI requires Ty to be passed sign- or zero-
702 // extended to 64 bits.
704 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty
) const {
705 // Treat an enum type as its underlying type.
706 if (const EnumType
*EnumTy
= Ty
->getAs
<EnumType
>())
707 Ty
= EnumTy
->getDecl()->getIntegerType();
709 // Promotable integer types are required to be promoted by the ABI.
710 if (isPromotableIntegerTypeForABI(Ty
))
713 // In addition to the usual promotable integer types, we also need to
714 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
715 if (const BuiltinType
*BT
= Ty
->getAs
<BuiltinType
>())
716 switch (BT
->getKind()) {
717 case BuiltinType::Int
:
718 case BuiltinType::UInt
:
724 if (const auto *EIT
= Ty
->getAs
<BitIntType
>())
725 if (EIT
->getNumBits() < 64)
731 /// isAlignedParamType - Determine whether a type requires 16-byte or
732 /// higher alignment in the parameter area. Always returns at least 8.
733 CharUnits
PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty
) const {
734 // Complex types are passed just like their elements.
735 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>())
736 Ty
= CTy
->getElementType();
738 auto FloatUsesVector
= [this](QualType Ty
){
739 return Ty
->isRealFloatingType() && &getContext().getFloatTypeSemantics(
740 Ty
) == &llvm::APFloat::IEEEquad();
743 // Only vector types of size 16 bytes need alignment (larger types are
744 // passed via reference, smaller types are not aligned).
745 if (Ty
->isVectorType()) {
746 return CharUnits::fromQuantity(getContext().getTypeSize(Ty
) == 128 ? 16 : 8);
747 } else if (FloatUsesVector(Ty
)) {
748 // According to ABI document section 'Optional Save Areas': If extended
749 // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
750 // format are supported, map them to a single quadword, quadword aligned.
751 return CharUnits::fromQuantity(16);
754 // For single-element float/vector structs, we consider the whole type
755 // to have the same alignment requirements as its single element.
756 const Type
*AlignAsType
= nullptr;
757 const Type
*EltType
= isSingleElementStruct(Ty
, getContext());
759 const BuiltinType
*BT
= EltType
->getAs
<BuiltinType
>();
760 if ((EltType
->isVectorType() && getContext().getTypeSize(EltType
) == 128) ||
761 (BT
&& BT
->isFloatingPoint()))
762 AlignAsType
= EltType
;
765 // Likewise for ELFv2 homogeneous aggregates.
766 const Type
*Base
= nullptr;
767 uint64_t Members
= 0;
768 if (!AlignAsType
&& Kind
== PPC64_SVR4_ABIKind::ELFv2
&&
769 isAggregateTypeForABI(Ty
) && isHomogeneousAggregate(Ty
, Base
, Members
))
772 // With special case aggregates, only vector base types need alignment.
774 bool UsesVector
= AlignAsType
->isVectorType() ||
775 FloatUsesVector(QualType(AlignAsType
, 0));
776 return CharUnits::fromQuantity(UsesVector
? 16 : 8);
779 // Otherwise, we only need alignment for any aggregate type that
780 // has an alignment requirement of >= 16 bytes.
781 if (isAggregateTypeForABI(Ty
) && getContext().getTypeAlign(Ty
) >= 128) {
782 return CharUnits::fromQuantity(16);
785 return CharUnits::fromQuantity(8);
788 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty
) const {
789 // Homogeneous aggregates for ELFv2 must have base types of float,
790 // double, long double, or 128-bit vectors.
791 if (const BuiltinType
*BT
= Ty
->getAs
<BuiltinType
>()) {
792 if (BT
->getKind() == BuiltinType::Float
||
793 BT
->getKind() == BuiltinType::Double
||
794 BT
->getKind() == BuiltinType::LongDouble
||
795 BT
->getKind() == BuiltinType::Ibm128
||
796 (getContext().getTargetInfo().hasFloat128Type() &&
797 (BT
->getKind() == BuiltinType::Float128
))) {
803 if (const VectorType
*VT
= Ty
->getAs
<VectorType
>()) {
804 if (getContext().getTypeSize(VT
) == 128)
810 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
811 const Type
*Base
, uint64_t Members
) const {
812 // Vector and fp128 types require one register, other floating point types
813 // require one or two registers depending on their size.
815 ((getContext().getTargetInfo().hasFloat128Type() &&
816 Base
->isFloat128Type()) ||
817 Base
->isVectorType()) ? 1
818 : (getContext().getTypeSize(Base
) + 63) / 64;
820 // Homogeneous Aggregates may occupy at most 8 registers.
821 return Members
* NumRegs
<= 8;
825 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty
) const {
826 Ty
= useFirstFieldIfTransparentUnion(Ty
);
828 if (Ty
->isAnyComplexType())
829 return ABIArgInfo::getDirect();
831 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
832 // or via reference (larger than 16 bytes).
833 if (Ty
->isVectorType()) {
834 uint64_t Size
= getContext().getTypeSize(Ty
);
836 return getNaturalAlignIndirect(Ty
, /*ByVal=*/false);
837 else if (Size
< 128) {
838 llvm::Type
*CoerceTy
= llvm::IntegerType::get(getVMContext(), Size
);
839 return ABIArgInfo::getDirect(CoerceTy
);
843 if (const auto *EIT
= Ty
->getAs
<BitIntType
>())
844 if (EIT
->getNumBits() > 128)
845 return getNaturalAlignIndirect(Ty
, /*ByVal=*/true);
847 if (isAggregateTypeForABI(Ty
)) {
848 if (CGCXXABI::RecordArgABI RAA
= getRecordArgABI(Ty
, getCXXABI()))
849 return getNaturalAlignIndirect(Ty
, RAA
== CGCXXABI::RAA_DirectInMemory
);
851 uint64_t ABIAlign
= getParamTypeAlignment(Ty
).getQuantity();
852 uint64_t TyAlign
= getContext().getTypeAlignInChars(Ty
).getQuantity();
854 // ELFv2 homogeneous aggregates are passed as array types.
855 const Type
*Base
= nullptr;
856 uint64_t Members
= 0;
857 if (Kind
== PPC64_SVR4_ABIKind::ELFv2
&&
858 isHomogeneousAggregate(Ty
, Base
, Members
)) {
859 llvm::Type
*BaseTy
= CGT
.ConvertType(QualType(Base
, 0));
860 llvm::Type
*CoerceTy
= llvm::ArrayType::get(BaseTy
, Members
);
861 return ABIArgInfo::getDirect(CoerceTy
);
864 // If an aggregate may end up fully in registers, we do not
865 // use the ByVal method, but pass the aggregate as array.
866 // This is usually beneficial since we avoid forcing the
867 // back-end to store the argument to memory.
868 uint64_t Bits
= getContext().getTypeSize(Ty
);
869 if (Bits
> 0 && Bits
<= 8 * GPRBits
) {
870 llvm::Type
*CoerceTy
;
872 // Types up to 8 bytes are passed as integer type (which will be
873 // properly aligned in the argument save area doubleword).
876 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits
, 8));
877 // Larger types are passed as arrays, with the base type selected
878 // according to the required alignment in the save area.
880 uint64_t RegBits
= ABIAlign
* 8;
881 uint64_t NumRegs
= llvm::alignTo(Bits
, RegBits
) / RegBits
;
882 llvm::Type
*RegTy
= llvm::IntegerType::get(getVMContext(), RegBits
);
883 CoerceTy
= llvm::ArrayType::get(RegTy
, NumRegs
);
886 return ABIArgInfo::getDirect(CoerceTy
);
889 // All other aggregates are passed ByVal.
890 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign
),
892 /*Realign=*/TyAlign
> ABIAlign
);
895 return (isPromotableTypeForABI(Ty
)
896 ? ABIArgInfo::getExtend(Ty
, CGT
.ConvertType(Ty
))
897 : ABIArgInfo::getDirect());
901 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy
) const {
902 if (RetTy
->isVoidType())
903 return ABIArgInfo::getIgnore();
905 if (RetTy
->isAnyComplexType())
906 return ABIArgInfo::getDirect();
908 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
909 // or via reference (larger than 16 bytes).
910 if (RetTy
->isVectorType()) {
911 uint64_t Size
= getContext().getTypeSize(RetTy
);
913 return getNaturalAlignIndirect(RetTy
);
914 else if (Size
< 128) {
915 llvm::Type
*CoerceTy
= llvm::IntegerType::get(getVMContext(), Size
);
916 return ABIArgInfo::getDirect(CoerceTy
);
920 if (const auto *EIT
= RetTy
->getAs
<BitIntType
>())
921 if (EIT
->getNumBits() > 128)
922 return getNaturalAlignIndirect(RetTy
, /*ByVal=*/false);
924 if (isAggregateTypeForABI(RetTy
)) {
925 // ELFv2 homogeneous aggregates are returned as array types.
926 const Type
*Base
= nullptr;
927 uint64_t Members
= 0;
928 if (Kind
== PPC64_SVR4_ABIKind::ELFv2
&&
929 isHomogeneousAggregate(RetTy
, Base
, Members
)) {
930 llvm::Type
*BaseTy
= CGT
.ConvertType(QualType(Base
, 0));
931 llvm::Type
*CoerceTy
= llvm::ArrayType::get(BaseTy
, Members
);
932 return ABIArgInfo::getDirect(CoerceTy
);
935 // ELFv2 small aggregates are returned in up to two registers.
936 uint64_t Bits
= getContext().getTypeSize(RetTy
);
937 if (Kind
== PPC64_SVR4_ABIKind::ELFv2
&& Bits
<= 2 * GPRBits
) {
939 return ABIArgInfo::getIgnore();
941 llvm::Type
*CoerceTy
;
942 if (Bits
> GPRBits
) {
943 CoerceTy
= llvm::IntegerType::get(getVMContext(), GPRBits
);
944 CoerceTy
= llvm::StructType::get(CoerceTy
, CoerceTy
);
947 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits
, 8));
948 return ABIArgInfo::getDirect(CoerceTy
);
951 // All other aggregates are returned indirectly.
952 return getNaturalAlignIndirect(RetTy
);
955 return (isPromotableTypeForABI(RetTy
) ? ABIArgInfo::getExtend(RetTy
)
956 : ABIArgInfo::getDirect());
959 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
960 RValue
PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
961 QualType Ty
, AggValueSlot Slot
) const {
962 auto TypeInfo
= getContext().getTypeInfoInChars(Ty
);
963 TypeInfo
.Align
= getParamTypeAlignment(Ty
);
965 CharUnits SlotSize
= CharUnits::fromQuantity(8);
967 // If we have a complex type and the base type is smaller than 8 bytes,
968 // the ABI calls for the real and imaginary parts to be right-adjusted
969 // in separate doublewords. However, Clang expects us to produce a
970 // pointer to a structure with the two parts packed tightly. So generate
971 // loads of the real and imaginary parts relative to the va_list pointer,
972 // and store them to a temporary structure.
973 if (const ComplexType
*CTy
= Ty
->getAs
<ComplexType
>()) {
974 CharUnits EltSize
= TypeInfo
.Width
/ 2;
975 if (EltSize
< SlotSize
)
976 return complexTempStructure(CGF
, VAListAddr
, Ty
, SlotSize
, EltSize
, CTy
);
979 // Otherwise, just use the general rule.
981 // The PPC64 ABI passes some arguments in integer registers, even to variadic
982 // functions. To allow va_list to use the simple "void*" representation,
983 // variadic calls allocate space in the argument area for the integer argument
984 // registers, and variadic functions spill their integer argument registers to
985 // this area in their prologues. When aggregates smaller than a register are
986 // passed this way, they are passed in the least significant bits of the
987 // register, which means that after spilling on big-endian targets they will
988 // be right-aligned in their argument slot. This is uncommon; for a variety of
989 // reasons, other big-endian targets don't end up right-aligning aggregate
990 // types this way, and so right-alignment only applies to fundamental types.
991 // So on PPC64, we must force the use of right-alignment even for aggregates.
992 return emitVoidPtrVAArg(CGF
, VAListAddr
, Ty
, /*Indirect*/ false, TypeInfo
,
993 SlotSize
, /*AllowHigher*/ true, Slot
,
994 /*ForceRightAdjust*/ true);
998 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
999 CodeGen::CodeGenFunction
&CGF
,
1000 llvm::Value
*Address
) const {
1001 return PPC_initDwarfEHRegSizeTable(CGF
, Address
, /*Is64Bit*/ true,
1005 void PPC64_SVR4_TargetCodeGenInfo::emitTargetMetadata(
1006 CodeGen::CodeGenModule
&CGM
,
1007 const llvm::MapVector
<GlobalDecl
, StringRef
> &MangledDeclNames
) const {
1008 if (CGM
.getTypes().isLongDoubleReferenced()) {
1009 llvm::LLVMContext
&Ctx
= CGM
.getLLVMContext();
1010 const auto *flt
= &CGM
.getTarget().getLongDoubleFormat();
1011 if (flt
== &llvm::APFloat::PPCDoubleDouble())
1012 CGM
.getModule().addModuleFlag(llvm::Module::Error
, "float-abi",
1013 llvm::MDString::get(Ctx
, "doubledouble"));
1014 else if (flt
== &llvm::APFloat::IEEEquad())
1015 CGM
.getModule().addModuleFlag(llvm::Module::Error
, "float-abi",
1016 llvm::MDString::get(Ctx
, "ieeequad"));
1017 else if (flt
== &llvm::APFloat::IEEEdouble())
1018 CGM
.getModule().addModuleFlag(llvm::Module::Error
, "float-abi",
1019 llvm::MDString::get(Ctx
, "ieeedouble"));
1024 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
1025 llvm::Value
*Address
) const {
1026 return PPC_initDwarfEHRegSizeTable(CGF
, Address
, /*Is64Bit*/ true,
1030 std::unique_ptr
<TargetCodeGenInfo
>
1031 CodeGen::createAIXTargetCodeGenInfo(CodeGenModule
&CGM
, bool Is64Bit
) {
1032 return std::make_unique
<AIXTargetCodeGenInfo
>(CGM
.getTypes(), Is64Bit
);
1035 std::unique_ptr
<TargetCodeGenInfo
>
1036 CodeGen::createPPC32TargetCodeGenInfo(CodeGenModule
&CGM
, bool SoftFloatABI
) {
1037 bool RetSmallStructInRegABI
= PPC32TargetCodeGenInfo::isStructReturnInRegABI(
1038 CGM
.getTriple(), CGM
.getCodeGenOpts());
1039 return std::make_unique
<PPC32TargetCodeGenInfo
>(CGM
.getTypes(), SoftFloatABI
,
1040 RetSmallStructInRegABI
);
1043 std::unique_ptr
<TargetCodeGenInfo
>
1044 CodeGen::createPPC64TargetCodeGenInfo(CodeGenModule
&CGM
) {
1045 return std::make_unique
<PPC64TargetCodeGenInfo
>(CGM
.getTypes());
1048 std::unique_ptr
<TargetCodeGenInfo
> CodeGen::createPPC64_SVR4_TargetCodeGenInfo(
1049 CodeGenModule
&CGM
, PPC64_SVR4_ABIKind Kind
, bool SoftFloatABI
) {
1050 return std::make_unique
<PPC64_SVR4_TargetCodeGenInfo
>(CGM
.getTypes(), Kind
,