1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
13 //===----------------------------------------------------------------------===//
15 #include "TargetInfo.h"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/CodeGen/CGFunctionInfo.h"
22 #include "clang/Frontend/CodeGenOptions.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm> // std::sort
30 using namespace clang;
31 using namespace CodeGen;
33 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
38 // Alternatively, we could emit this as a loop in the source.
39 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
41 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
42 Builder.CreateStore(Value, Cell);
46 static bool isAggregateTypeForABI(QualType T) {
47 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
48 T->isMemberFunctionPointerType();
51 ABIInfo::~ABIInfo() {}
53 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
55 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
57 return CGCXXABI::RAA_Default;
58 return CXXABI.getRecordArgABI(RD);
61 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
63 const RecordType *RT = T->getAs<RecordType>();
65 return CGCXXABI::RAA_Default;
66 return getRecordArgABI(RT, CXXABI);
69 /// Pass transparent unions as if they were the type of the first element. Sema
70 /// should ensure that all elements of the union have the same "machine type".
71 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
72 if (const RecordType *UT = Ty->getAsUnionType()) {
73 const RecordDecl *UD = UT->getDecl();
74 if (UD->hasAttr<TransparentUnionAttr>()) {
75 assert(!UD->field_empty() && "sema created an empty transparent union");
76 return UD->field_begin()->getType();
82 CGCXXABI &ABIInfo::getCXXABI() const {
83 return CGT.getCXXABI();
86 ASTContext &ABIInfo::getContext() const {
87 return CGT.getContext();
90 llvm::LLVMContext &ABIInfo::getVMContext() const {
91 return CGT.getLLVMContext();
94 const llvm::DataLayout &ABIInfo::getDataLayout() const {
95 return CGT.getDataLayout();
98 const TargetInfo &ABIInfo::getTarget() const {
99 return CGT.getTarget();
102 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
106 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
107 uint64_t Members) const {
111 bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
115 void ABIArgInfo::dump() const {
116 raw_ostream &OS = llvm::errs();
117 OS << "(ABIArgInfo Kind=";
120 OS << "Direct Type=";
121 if (llvm::Type *Ty = getCoerceToType())
133 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
136 OS << "Indirect Align=" << getIndirectAlign()
137 << " ByVal=" << getIndirectByVal()
138 << " Realign=" << getIndirectRealign();
147 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
149 // If someone can figure out a general rule for this, that would be great.
150 // It's probably just doomed to be platform-dependent, though.
151 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
153 // x86-64 FreeBSD, Linux, Darwin
154 // x86-32 FreeBSD, Linux, Darwin
155 // PowerPC Linux, Darwin
156 // ARM Darwin (*not* EABI)
161 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
162 const FunctionNoProtoType *fnType) const {
163 // The following conventions are known to require this to be false:
166 // For everything else, we just prefer false unless we opt out.
171 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
172 llvm::SmallString<24> &Opt) const {
173 // This assumes the user is passing a library name like "rt" instead of a
174 // filename like "librt.a/so", and that they don't care whether it's static or
180 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
182 /// isEmptyField - Return true iff a the field is "empty", that is it
183 /// is an unnamed bit-field or an (array of) empty record(s).
184 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
186 if (FD->isUnnamedBitfield())
189 QualType FT = FD->getType();
191 // Constant arrays of empty records count as empty, strip them off.
192 // Constant arrays of zero length always count as empty.
194 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
195 if (AT->getSize() == 0)
197 FT = AT->getElementType();
200 const RecordType *RT = FT->getAs<RecordType>();
204 // C++ record fields are never empty, at least in the Itanium ABI.
206 // FIXME: We should use a predicate for whether this behavior is true in the
208 if (isa<CXXRecordDecl>(RT->getDecl()))
211 return isEmptyRecord(Context, FT, AllowArrays);
214 /// isEmptyRecord - Return true iff a structure contains only empty
215 /// fields. Note that a structure with a flexible array member is not
216 /// considered empty.
217 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
218 const RecordType *RT = T->getAs<RecordType>();
221 const RecordDecl *RD = RT->getDecl();
222 if (RD->hasFlexibleArrayMember())
225 // If this is a C++ record, check the bases first.
226 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
227 for (const auto &I : CXXRD->bases())
228 if (!isEmptyRecord(Context, I.getType(), true))
231 for (const auto *I : RD->fields())
232 if (!isEmptyField(Context, I, AllowArrays))
237 /// isSingleElementStruct - Determine if a structure is a "single
238 /// element struct", i.e. it has exactly one non-empty field or
239 /// exactly one field which is itself a single element
240 /// struct. Structures with flexible array members are never
241 /// considered single element structs.
243 /// \return The field declaration for the single non-empty field, if
245 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
246 const RecordType *RT = T->getAs<RecordType>();
250 const RecordDecl *RD = RT->getDecl();
251 if (RD->hasFlexibleArrayMember())
254 const Type *Found = nullptr;
256 // If this is a C++ record, check the bases first.
257 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
258 for (const auto &I : CXXRD->bases()) {
259 // Ignore empty records.
260 if (isEmptyRecord(Context, I.getType(), true))
263 // If we already found an element then this isn't a single-element struct.
267 // If this is non-empty and not a single element struct, the composite
268 // cannot be a single element struct.
269 Found = isSingleElementStruct(I.getType(), Context);
275 // Check for single element.
276 for (const auto *FD : RD->fields()) {
277 QualType FT = FD->getType();
279 // Ignore empty fields.
280 if (isEmptyField(Context, FD, true))
283 // If we already found an element then this isn't a single-element
288 // Treat single element arrays as the element.
289 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
290 if (AT->getSize().getZExtValue() != 1)
292 FT = AT->getElementType();
295 if (!isAggregateTypeForABI(FT)) {
296 Found = FT.getTypePtr();
298 Found = isSingleElementStruct(FT, Context);
304 // We don't consider a struct a single-element struct if it has
305 // padding beyond the element type.
306 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
312 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
313 // Treat complex types as the element type.
314 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
315 Ty = CTy->getElementType();
317 // Check for a type which we know has a simple scalar argument-passing
318 // convention without any padding. (We're specifically looking for 32
319 // and 64-bit integer and integer-equivalents, float, and double.)
320 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
321 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
324 uint64_t Size = Context.getTypeSize(Ty);
325 return Size == 32 || Size == 64;
328 /// canExpandIndirectArgument - Test whether an argument type which is to be
329 /// passed indirectly (on the stack) would have the equivalent layout if it was
330 /// expanded into separate arguments. If so, we prefer to do the latter to avoid
331 /// inhibiting optimizations.
333 // FIXME: This predicate is missing many cases, currently it just follows
334 // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
335 // should probably make this smarter, or better yet make the LLVM backend
336 // capable of handling it.
337 static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
338 // We can only expand structure types.
339 const RecordType *RT = Ty->getAs<RecordType>();
343 // We can only expand (C) structures.
345 // FIXME: This needs to be generalized to handle classes as well.
346 const RecordDecl *RD = RT->getDecl();
350 // We try to expand CLike CXXRecordDecl.
351 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
352 if (!CXXRD->isCLike())
358 for (const auto *FD : RD->fields()) {
359 if (!is32Or64BitBasicType(FD->getType(), Context))
362 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
363 // how to expand them yet, and the predicate for telling if a bitfield still
364 // counts as "basic" is more complicated than what we were doing previously.
365 if (FD->isBitField())
368 Size += Context.getTypeSize(FD->getType());
371 // Make sure there are not any holes in the struct.
372 if (Size != Context.getTypeSize(Ty))
379 /// DefaultABIInfo - The default implementation for ABI specific
380 /// details. This implementation provides information which results in
381 /// self-consistent and sensible LLVM IR generation, but does not
382 /// conform to any particular ABI.
383 class DefaultABIInfo : public ABIInfo {
385 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
387 ABIArgInfo classifyReturnType(QualType RetTy) const;
388 ABIArgInfo classifyArgumentType(QualType RetTy) const;
390 void computeInfo(CGFunctionInfo &FI) const override {
391 if (!getCXXABI().classifyReturnType(FI))
392 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
393 for (auto &I : FI.arguments())
394 I.info = classifyArgumentType(I.type);
397 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
398 CodeGenFunction &CGF) const override;
401 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
403 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
404 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
407 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
408 CodeGenFunction &CGF) const {
412 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
413 Ty = useFirstFieldIfTransparentUnion(Ty);
415 if (isAggregateTypeForABI(Ty)) {
416 // Records with non-trivial destructors/copy-constructors should not be
418 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
419 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
421 return ABIArgInfo::getIndirect(0);
424 // Treat an enum type as its underlying type.
425 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
426 Ty = EnumTy->getDecl()->getIntegerType();
428 return (Ty->isPromotableIntegerType() ?
429 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
432 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
433 if (RetTy->isVoidType())
434 return ABIArgInfo::getIgnore();
436 if (isAggregateTypeForABI(RetTy))
437 return ABIArgInfo::getIndirect(0);
439 // Treat an enum type as its underlying type.
440 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
441 RetTy = EnumTy->getDecl()->getIntegerType();
443 return (RetTy->isPromotableIntegerType() ?
444 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
447 //===----------------------------------------------------------------------===//
448 // le32/PNaCl bitcode ABI Implementation
450 // This is a simplified version of the x86_32 ABI. Arguments and return values
451 // are always passed on the stack.
452 //===----------------------------------------------------------------------===//
454 class PNaClABIInfo : public ABIInfo {
456 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
458 ABIArgInfo classifyReturnType(QualType RetTy) const;
459 ABIArgInfo classifyArgumentType(QualType RetTy) const;
461 void computeInfo(CGFunctionInfo &FI) const override;
462 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
463 CodeGenFunction &CGF) const override;
466 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
468 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
469 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
472 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
473 if (!getCXXABI().classifyReturnType(FI))
474 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
476 for (auto &I : FI.arguments())
477 I.info = classifyArgumentType(I.type);
480 llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
481 CodeGenFunction &CGF) const {
485 /// \brief Classify argument of given type \p Ty.
486 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
487 if (isAggregateTypeForABI(Ty)) {
488 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
489 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
490 return ABIArgInfo::getIndirect(0);
491 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
492 // Treat an enum type as its underlying type.
493 Ty = EnumTy->getDecl()->getIntegerType();
494 } else if (Ty->isFloatingType()) {
495 // Floating-point types don't go inreg.
496 return ABIArgInfo::getDirect();
499 return (Ty->isPromotableIntegerType() ?
500 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
503 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
504 if (RetTy->isVoidType())
505 return ABIArgInfo::getIgnore();
507 // In the PNaCl ABI we always return records/structures on the stack.
508 if (isAggregateTypeForABI(RetTy))
509 return ABIArgInfo::getIndirect(0);
511 // Treat an enum type as its underlying type.
512 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
513 RetTy = EnumTy->getDecl()->getIntegerType();
515 return (RetTy->isPromotableIntegerType() ?
516 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
519 /// IsX86_MMXType - Return true if this is an MMX type.
520 bool IsX86_MMXType(llvm::Type *IRType) {
521 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
522 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
523 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
524 IRType->getScalarSizeInBits() != 64;
527 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
528 StringRef Constraint,
530 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
531 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
532 // Invalid MMX constraint
536 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
539 // No operation needed
543 /// Returns true if this type can be passed in SSE registers with the
544 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
545 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
546 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
547 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
549 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
550 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
551 // registers specially.
552 unsigned VecSize = Context.getTypeSize(VT);
553 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
559 /// Returns true if this aggregate is small enough to be passed in SSE registers
560 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
561 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
562 return NumMembers <= 4;
565 //===----------------------------------------------------------------------===//
566 // X86-32 ABI Implementation
567 //===----------------------------------------------------------------------===//
569 /// \brief Similar to llvm::CCState, but for Clang.
571 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
575 unsigned FreeSSERegs;
578 /// X86_32ABIInfo - The X86-32 ABI information.
579 class X86_32ABIInfo : public ABIInfo {
585 static const unsigned MinABIStackAlignInBytes = 4;
587 bool IsDarwinVectorABI;
588 bool IsSmallStructInRegABI;
589 bool IsWin32StructABI;
590 unsigned DefaultNumRegisterParameters;
592 static bool isRegisterSize(unsigned Size) {
593 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
596 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
597 // FIXME: Assumes vectorcall is in use.
598 return isX86VectorTypeForVectorCall(getContext(), Ty);
601 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
602 uint64_t NumMembers) const override {
603 // FIXME: Assumes vectorcall is in use.
604 return isX86VectorCallAggregateSmallEnough(NumMembers);
607 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
609 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
610 /// such that the argument will be passed in memory.
611 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
613 ABIArgInfo getIndirectReturnResult(CCState &State) const;
615 /// \brief Return the alignment to use for the given type on the stack.
616 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
618 Class classify(QualType Ty) const;
619 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
620 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
621 bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const;
623 /// \brief Rewrite the function info so that all memory arguments use
625 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
627 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
628 unsigned &StackOffset, ABIArgInfo &Info,
629 QualType Type) const;
633 void computeInfo(CGFunctionInfo &FI) const override;
634 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
635 CodeGenFunction &CGF) const override;
637 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
639 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
640 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
643 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
645 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
646 bool d, bool p, bool w, unsigned r)
647 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
649 static bool isStructReturnInRegABI(
650 const llvm::Triple &Triple, const CodeGenOptions &Opts);
652 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
653 CodeGen::CodeGenModule &CGM) const override;
655 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
656 // Darwin uses different dwarf register numbers for EH.
657 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
661 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
662 llvm::Value *Address) const override;
664 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
665 StringRef Constraint,
666 llvm::Type* Ty) const override {
667 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
670 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
671 std::string &Constraints,
672 std::vector<llvm::Type *> &ResultRegTypes,
673 std::vector<llvm::Type *> &ResultTruncRegTypes,
674 std::vector<LValue> &ResultRegDests,
675 std::string &AsmString,
676 unsigned NumOutputs) const override;
679 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
680 unsigned Sig = (0xeb << 0) | // jmp rel8
681 (0x06 << 8) | // .+0x08
684 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
690 /// Rewrite input constraint references after adding some output constraints.
691 /// In the case where there is one output and one input and we add one output,
692 /// we need to replace all operand references greater than or equal to 1:
695 /// The result will be:
698 static void rewriteInputConstraintReferences(unsigned FirstIn,
700 std::string &AsmString) {
702 llvm::raw_string_ostream OS(Buf);
704 while (Pos < AsmString.size()) {
705 size_t DollarStart = AsmString.find('$', Pos);
706 if (DollarStart == std::string::npos)
707 DollarStart = AsmString.size();
708 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
709 if (DollarEnd == std::string::npos)
710 DollarEnd = AsmString.size();
711 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
713 size_t NumDollars = DollarEnd - DollarStart;
714 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
715 // We have an operand reference.
716 size_t DigitStart = Pos;
717 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
718 if (DigitEnd == std::string::npos)
719 DigitEnd = AsmString.size();
720 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
721 unsigned OperandIndex;
722 if (!OperandStr.getAsInteger(10, OperandIndex)) {
723 if (OperandIndex >= FirstIn)
724 OperandIndex += NumNewOuts;
732 AsmString = std::move(OS.str());
735 /// Add output constraints for EAX:EDX because they are return registers.
736 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
737 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
738 std::vector<llvm::Type *> &ResultRegTypes,
739 std::vector<llvm::Type *> &ResultTruncRegTypes,
740 std::vector<LValue> &ResultRegDests, std::string &AsmString,
741 unsigned NumOutputs) const {
742 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
744 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
746 if (!Constraints.empty())
748 if (RetWidth <= 32) {
749 Constraints += "={eax}";
750 ResultRegTypes.push_back(CGF.Int32Ty);
752 // Use the 'A' constraint for EAX:EDX.
754 ResultRegTypes.push_back(CGF.Int64Ty);
757 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
758 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
759 ResultTruncRegTypes.push_back(CoerceTy);
761 // Coerce the integer by bitcasting the return slot pointer.
762 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
763 CoerceTy->getPointerTo()));
764 ResultRegDests.push_back(ReturnSlot);
766 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
769 /// shouldReturnTypeInRegister - Determine if the given type should be
770 /// passed in a register (for the Darwin ABI).
771 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
772 ASTContext &Context) const {
773 uint64_t Size = Context.getTypeSize(Ty);
775 // Type must be register sized.
776 if (!isRegisterSize(Size))
779 if (Ty->isVectorType()) {
780 // 64- and 128- bit vectors inside structures are not returned in
782 if (Size == 64 || Size == 128)
788 // If this is a builtin, pointer, enum, complex type, member pointer, or
789 // member function pointer it is ok.
790 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
791 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
792 Ty->isBlockPointerType() || Ty->isMemberPointerType())
795 // Arrays are treated like records.
796 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
797 return shouldReturnTypeInRegister(AT->getElementType(), Context);
799 // Otherwise, it must be a record type.
800 const RecordType *RT = Ty->getAs<RecordType>();
801 if (!RT) return false;
803 // FIXME: Traverse bases here too.
805 // Structure types are passed in register if all fields would be
806 // passed in a register.
807 for (const auto *FD : RT->getDecl()->fields()) {
808 // Empty fields are ignored.
809 if (isEmptyField(Context, FD, true))
812 // Check fields recursively.
813 if (!shouldReturnTypeInRegister(FD->getType(), Context))
819 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const {
820 // If the return value is indirect, then the hidden argument is consuming one
822 if (State.FreeRegs) {
824 return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false);
826 return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false);
829 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
830 CCState &State) const {
831 if (RetTy->isVoidType())
832 return ABIArgInfo::getIgnore();
834 const Type *Base = nullptr;
835 uint64_t NumElts = 0;
836 if (State.CC == llvm::CallingConv::X86_VectorCall &&
837 isHomogeneousAggregate(RetTy, Base, NumElts)) {
838 // The LLVM struct type for such an aggregate should lower properly.
839 return ABIArgInfo::getDirect();
842 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
843 // On Darwin, some vectors are returned in registers.
844 if (IsDarwinVectorABI) {
845 uint64_t Size = getContext().getTypeSize(RetTy);
847 // 128-bit vectors are a special case; they are returned in
848 // registers and we need to make sure to pick a type the LLVM
849 // backend will like.
851 return ABIArgInfo::getDirect(llvm::VectorType::get(
852 llvm::Type::getInt64Ty(getVMContext()), 2));
854 // Always return in register if it fits in a general purpose
855 // register, or if it is 64 bits and has a single element.
856 if ((Size == 8 || Size == 16 || Size == 32) ||
857 (Size == 64 && VT->getNumElements() == 1))
858 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
861 return getIndirectReturnResult(State);
864 return ABIArgInfo::getDirect();
867 if (isAggregateTypeForABI(RetTy)) {
868 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
869 // Structures with flexible arrays are always indirect.
870 if (RT->getDecl()->hasFlexibleArrayMember())
871 return getIndirectReturnResult(State);
874 // If specified, structs and unions are always indirect.
875 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
876 return getIndirectReturnResult(State);
878 // Small structures which are register sized are generally returned
880 if (shouldReturnTypeInRegister(RetTy, getContext())) {
881 uint64_t Size = getContext().getTypeSize(RetTy);
883 // As a special-case, if the struct is a "single-element" struct, and
884 // the field is of type "float" or "double", return it in a
885 // floating-point register. (MSVC does not apply this special case.)
886 // We apply a similar transformation for pointer types to improve the
887 // quality of the generated IR.
888 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
889 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
890 || SeltTy->hasPointerRepresentation())
891 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
893 // FIXME: We should be able to narrow this integer in cases with dead
895 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
898 return getIndirectReturnResult(State);
901 // Treat an enum type as its underlying type.
902 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
903 RetTy = EnumTy->getDecl()->getIntegerType();
905 return (RetTy->isPromotableIntegerType() ?
906 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
909 static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
910 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
913 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
914 const RecordType *RT = Ty->getAs<RecordType>();
917 const RecordDecl *RD = RT->getDecl();
919 // If this is a C++ record, check the bases first.
920 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
921 for (const auto &I : CXXRD->bases())
922 if (!isRecordWithSSEVectorType(Context, I.getType()))
925 for (const auto *i : RD->fields()) {
926 QualType FT = i->getType();
928 if (isSSEVectorType(Context, FT))
931 if (isRecordWithSSEVectorType(Context, FT))
938 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
939 unsigned Align) const {
940 // Otherwise, if the alignment is less than or equal to the minimum ABI
941 // alignment, just use the default; the backend will handle this.
942 if (Align <= MinABIStackAlignInBytes)
943 return 0; // Use default alignment.
945 // On non-Darwin, the stack type alignment is always 4.
946 if (!IsDarwinVectorABI) {
947 // Set explicit alignment, since we may need to realign the top.
948 return MinABIStackAlignInBytes;
951 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
952 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
953 isRecordWithSSEVectorType(getContext(), Ty)))
956 return MinABIStackAlignInBytes;
959 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
960 CCState &State) const {
962 if (State.FreeRegs) {
963 --State.FreeRegs; // Non-byval indirects just use one pointer.
964 return ABIArgInfo::getIndirectInReg(0, false);
966 return ABIArgInfo::getIndirect(0, false);
969 // Compute the byval alignment.
970 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
971 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
973 return ABIArgInfo::getIndirect(4, /*ByVal=*/true);
975 // If the stack alignment is less than the type alignment, realign the
977 bool Realign = TypeAlign > StackAlign;
978 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign);
981 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
982 const Type *T = isSingleElementStruct(Ty, getContext());
986 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
987 BuiltinType::Kind K = BT->getKind();
988 if (K == BuiltinType::Float || K == BuiltinType::Double)
994 bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State,
995 bool &NeedsPadding) const {
996 NeedsPadding = false;
997 Class C = classify(Ty);
1001 unsigned Size = getContext().getTypeSize(Ty);
1002 unsigned SizeInRegs = (Size + 31) / 32;
1004 if (SizeInRegs == 0)
1007 if (SizeInRegs > State.FreeRegs) {
1012 State.FreeRegs -= SizeInRegs;
1014 if (State.CC == llvm::CallingConv::X86_FastCall ||
1015 State.CC == llvm::CallingConv::X86_VectorCall) {
1019 if (Ty->isIntegralOrEnumerationType())
1022 if (Ty->isPointerType())
1025 if (Ty->isReferenceType())
1029 NeedsPadding = true;
1037 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1038 CCState &State) const {
1039 // FIXME: Set alignment on indirect arguments.
1041 Ty = useFirstFieldIfTransparentUnion(Ty);
1043 // Check with the C++ ABI first.
1044 const RecordType *RT = Ty->getAs<RecordType>();
1046 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1047 if (RAA == CGCXXABI::RAA_Indirect) {
1048 return getIndirectResult(Ty, false, State);
1049 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1050 // The field index doesn't matter, we'll fix it up later.
1051 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1055 // vectorcall adds the concept of a homogenous vector aggregate, similar
1056 // to other targets.
1057 const Type *Base = nullptr;
1058 uint64_t NumElts = 0;
1059 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1060 isHomogeneousAggregate(Ty, Base, NumElts)) {
1061 if (State.FreeSSERegs >= NumElts) {
1062 State.FreeSSERegs -= NumElts;
1063 if (Ty->isBuiltinType() || Ty->isVectorType())
1064 return ABIArgInfo::getDirect();
1065 return ABIArgInfo::getExpand();
1067 return getIndirectResult(Ty, /*ByVal=*/false, State);
1070 if (isAggregateTypeForABI(Ty)) {
1072 // Structs are always byval on win32, regardless of what they contain.
1073 if (IsWin32StructABI)
1074 return getIndirectResult(Ty, true, State);
1076 // Structures with flexible arrays are always indirect.
1077 if (RT->getDecl()->hasFlexibleArrayMember())
1078 return getIndirectResult(Ty, true, State);
1081 // Ignore empty structs/unions.
1082 if (isEmptyRecord(getContext(), Ty, true))
1083 return ABIArgInfo::getIgnore();
1085 llvm::LLVMContext &LLVMContext = getVMContext();
1086 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1088 if (shouldUseInReg(Ty, State, NeedsPadding)) {
1089 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
1090 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1091 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1092 return ABIArgInfo::getDirectInReg(Result);
1094 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1096 // Expand small (<= 128-bit) record types when we know that the stack layout
1097 // of those arguments will match the struct. This is important because the
1098 // LLVM backend isn't smart enough to remove byval, which inhibits many
1100 if (getContext().getTypeSize(Ty) <= 4*32 &&
1101 canExpandIndirectArgument(Ty, getContext()))
1102 return ABIArgInfo::getExpandWithPadding(
1103 State.CC == llvm::CallingConv::X86_FastCall ||
1104 State.CC == llvm::CallingConv::X86_VectorCall,
1107 return getIndirectResult(Ty, true, State);
1110 if (const VectorType *VT = Ty->getAs<VectorType>()) {
1111 // On Darwin, some vectors are passed in memory, we handle this by passing
1112 // it as an i8/i16/i32/i64.
1113 if (IsDarwinVectorABI) {
1114 uint64_t Size = getContext().getTypeSize(Ty);
1115 if ((Size == 8 || Size == 16 || Size == 32) ||
1116 (Size == 64 && VT->getNumElements() == 1))
1117 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1121 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1122 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1124 return ABIArgInfo::getDirect();
1128 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1129 Ty = EnumTy->getDecl()->getIntegerType();
1132 bool InReg = shouldUseInReg(Ty, State, NeedsPadding);
1134 if (Ty->isPromotableIntegerType()) {
1136 return ABIArgInfo::getExtendInReg();
1137 return ABIArgInfo::getExtend();
1140 return ABIArgInfo::getDirectInReg();
1141 return ABIArgInfo::getDirect();
1144 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1145 CCState State(FI.getCallingConvention());
1146 if (State.CC == llvm::CallingConv::X86_FastCall)
1148 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1150 State.FreeSSERegs = 6;
1151 } else if (FI.getHasRegParm())
1152 State.FreeRegs = FI.getRegParm();
1154 State.FreeRegs = DefaultNumRegisterParameters;
1156 if (!getCXXABI().classifyReturnType(FI)) {
1157 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1158 } else if (FI.getReturnInfo().isIndirect()) {
1159 // The C++ ABI is not aware of register usage, so we have to check if the
1160 // return value was sret and put it in a register ourselves if appropriate.
1161 if (State.FreeRegs) {
1162 --State.FreeRegs; // The sret parameter consumes a register.
1163 FI.getReturnInfo().setInReg(true);
1167 // The chain argument effectively gives us another free register.
1168 if (FI.isChainCall())
1171 bool UsedInAlloca = false;
1172 for (auto &I : FI.arguments()) {
1173 I.info = classifyArgumentType(I.type, State);
1174 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1177 // If we needed to use inalloca for any argument, do a second pass and rewrite
1178 // all the memory arguments to use inalloca.
1180 rewriteWithInAlloca(FI);
1184 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1185 unsigned &StackOffset,
1186 ABIArgInfo &Info, QualType Type) const {
1187 assert(StackOffset % 4U == 0 && "unaligned inalloca struct");
1188 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1189 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1190 StackOffset += getContext().getTypeSizeInChars(Type).getQuantity();
1192 // Insert padding bytes to respect alignment. For x86_32, each argument is 4
1194 if (StackOffset % 4U) {
1195 unsigned OldOffset = StackOffset;
1196 StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U);
1197 unsigned NumBytes = StackOffset - OldOffset;
1199 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1200 Ty = llvm::ArrayType::get(Ty, NumBytes);
1201 FrameFields.push_back(Ty);
1205 static bool isArgInAlloca(const ABIArgInfo &Info) {
1206 // Leave ignored and inreg arguments alone.
1207 switch (Info.getKind()) {
1208 case ABIArgInfo::InAlloca:
1210 case ABIArgInfo::Indirect:
1211 assert(Info.getIndirectByVal());
1213 case ABIArgInfo::Ignore:
1215 case ABIArgInfo::Direct:
1216 case ABIArgInfo::Extend:
1217 case ABIArgInfo::Expand:
1218 if (Info.getInReg())
1222 llvm_unreachable("invalid enum");
1225 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1226 assert(IsWin32StructABI && "inalloca only supported on win32");
1228 // Build a packed struct type for all of the arguments in memory.
1229 SmallVector<llvm::Type *, 6> FrameFields;
1231 unsigned StackOffset = 0;
1232 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1234 // Put 'this' into the struct before 'sret', if necessary.
1236 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1237 ABIArgInfo &Ret = FI.getReturnInfo();
1238 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1239 isArgInAlloca(I->info)) {
1240 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1244 // Put the sret parameter into the inalloca struct if it's in memory.
1245 if (Ret.isIndirect() && !Ret.getInReg()) {
1246 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1247 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1248 // On Windows, the hidden sret parameter is always returned in eax.
1249 Ret.setInAllocaSRet(IsWin32StructABI);
1252 // Skip the 'this' parameter in ecx.
1256 // Put arguments passed in memory into the struct.
1257 for (; I != E; ++I) {
1258 if (isArgInAlloca(I->info))
1259 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1262 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1263 /*isPacked=*/true));
1266 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1267 CodeGenFunction &CGF) const {
1268 llvm::Type *BPP = CGF.Int8PtrPtrTy;
1270 CGBuilderTy &Builder = CGF.Builder;
1271 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1273 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1275 // Compute if the address needs to be aligned
1276 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
1277 Align = getTypeStackAlignInBytes(Ty, Align);
1278 Align = std::max(Align, 4U);
1280 // addr = (addr + align - 1) & -align;
1281 llvm::Value *Offset =
1282 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
1283 Addr = CGF.Builder.CreateGEP(Addr, Offset);
1284 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
1286 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1287 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1293 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1294 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1297 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
1298 llvm::Value *NextAddr =
1299 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
1301 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1306 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1307 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1308 assert(Triple.getArch() == llvm::Triple::x86);
1310 switch (Opts.getStructReturnConvention()) {
1311 case CodeGenOptions::SRCK_Default:
1313 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1315 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1319 if (Triple.isOSDarwin())
1322 switch (Triple.getOS()) {
1323 case llvm::Triple::DragonFly:
1324 case llvm::Triple::FreeBSD:
1325 case llvm::Triple::OpenBSD:
1326 case llvm::Triple::Bitrig:
1327 case llvm::Triple::Win32:
1334 void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
1335 llvm::GlobalValue *GV,
1336 CodeGen::CodeGenModule &CGM) const {
1337 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1338 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1339 // Get the LLVM function.
1340 llvm::Function *Fn = cast<llvm::Function>(GV);
1342 // Now add the 'alignstack' attribute with a value of 16.
1343 llvm::AttrBuilder B;
1344 B.addStackAlignmentAttr(16);
1345 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1346 llvm::AttributeSet::get(CGM.getLLVMContext(),
1347 llvm::AttributeSet::FunctionIndex,
1353 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1354 CodeGen::CodeGenFunction &CGF,
1355 llvm::Value *Address) const {
1356 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1358 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
1360 // 0-7 are the eight integer registers; the order is different
1361 // on Darwin (for EH), but the range is the same.
1363 AssignToArrayRange(Builder, Address, Four8, 0, 8);
1365 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
1366 // 12-16 are st(0..4). Not sure why we stop at 4.
1367 // These have size 16, which is sizeof(long double) on
1368 // platforms with 8-byte alignment for that type.
1369 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
1370 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
1373 // 9 is %eflags, which doesn't get a size on Darwin for some
1375 Builder.CreateStore(
1376 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9));
1378 // 11-16 are st(0..5). Not sure why we stop at 5.
1379 // These have size 12, which is sizeof(long double) on
1380 // platforms with 4-byte alignment for that type.
1381 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1382 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1388 //===----------------------------------------------------------------------===//
1389 // X86-64 ABI Implementation
1390 //===----------------------------------------------------------------------===//
1394 /// The AVX ABI level for X86 targets.
1395 enum class X86AVXABILevel {
1401 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1402 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1404 case X86AVXABILevel::AVX512:
1406 case X86AVXABILevel::AVX:
1408 case X86AVXABILevel::None:
1411 llvm_unreachable("Unknown AVXLevel");
1414 /// X86_64ABIInfo - The X86_64 ABI information.
1415 class X86_64ABIInfo : public ABIInfo {
1427 /// merge - Implement the X86_64 ABI merging algorithm.
1429 /// Merge an accumulating classification \arg Accum with a field
1430 /// classification \arg Field.
1432 /// \param Accum - The accumulating classification. This should
1433 /// always be either NoClass or the result of a previous merge
1434 /// call. In addition, this should never be Memory (the caller
1435 /// should just return Memory for the aggregate).
1436 static Class merge(Class Accum, Class Field);
1438 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1440 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1441 /// final MEMORY or SSE classes when necessary.
1443 /// \param AggregateSize - The size of the current aggregate in
1444 /// the classification process.
1446 /// \param Lo - The classification for the parts of the type
1447 /// residing in the low word of the containing object.
1449 /// \param Hi - The classification for the parts of the type
1450 /// residing in the higher words of the containing object.
1452 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1454 /// classify - Determine the x86_64 register classes in which the
1455 /// given type T should be passed.
1457 /// \param Lo - The classification for the parts of the type
1458 /// residing in the low word of the containing object.
1460 /// \param Hi - The classification for the parts of the type
1461 /// residing in the high word of the containing object.
1463 /// \param OffsetBase - The bit offset of this type in the
1464 /// containing object. Some parameters are classified different
1465 /// depending on whether they straddle an eightbyte boundary.
1467 /// \param isNamedArg - Whether the argument in question is a "named"
1468 /// argument, as used in AMD64-ABI 3.5.7.
1470 /// If a word is unused its result will be NoClass; if a type should
1471 /// be passed in Memory then at least the classification of \arg Lo
1474 /// The \arg Lo class will be NoClass iff the argument is ignored.
1476 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1477 /// also be ComplexX87.
1478 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1479 bool isNamedArg) const;
1481 llvm::Type *GetByteVectorType(QualType Ty) const;
1482 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1483 unsigned IROffset, QualType SourceTy,
1484 unsigned SourceOffset) const;
1485 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1486 unsigned IROffset, QualType SourceTy,
1487 unsigned SourceOffset) const;
1489 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1490 /// such that the argument will be returned in memory.
1491 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
1493 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1494 /// such that the argument will be passed in memory.
1496 /// \param freeIntRegs - The number of free integer registers remaining
1498 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1500 ABIArgInfo classifyReturnType(QualType RetTy) const;
1502 ABIArgInfo classifyArgumentType(QualType Ty,
1503 unsigned freeIntRegs,
1504 unsigned &neededInt,
1505 unsigned &neededSSE,
1506 bool isNamedArg) const;
1508 bool IsIllegalVectorType(QualType Ty) const;
1510 /// The 0.98 ABI revision clarified a lot of ambiguities,
1511 /// unfortunately in ways that were not always consistent with
1512 /// certain previous compilers. In particular, platforms which
1513 /// required strict binary compatibility with older versions of GCC
1514 /// may need to exempt themselves.
1515 bool honorsRevision0_98() const {
1516 return !getTarget().getTriple().isOSDarwin();
1519 X86AVXABILevel AVXLevel;
1520 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1522 bool Has64BitPointers;
1525 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
1526 ABIInfo(CGT), AVXLevel(AVXLevel),
1527 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
1530 bool isPassedUsingAVXType(QualType type) const {
1531 unsigned neededInt, neededSSE;
1532 // The freeIntRegs argument doesn't matter here.
1533 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1534 /*isNamedArg*/true);
1535 if (info.isDirect()) {
1536 llvm::Type *ty = info.getCoerceToType();
1537 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1538 return (vectorTy->getBitWidth() > 128);
1543 void computeInfo(CGFunctionInfo &FI) const override;
1545 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1546 CodeGenFunction &CGF) const override;
1548 bool has64BitPointers() const {
1549 return Has64BitPointers;
1553 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1554 class WinX86_64ABIInfo : public ABIInfo {
1556 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
1557 bool IsReturnType) const;
1560 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1562 void computeInfo(CGFunctionInfo &FI) const override;
1564 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1565 CodeGenFunction &CGF) const override;
1567 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1568 // FIXME: Assumes vectorcall is in use.
1569 return isX86VectorTypeForVectorCall(getContext(), Ty);
1572 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1573 uint64_t NumMembers) const override {
1574 // FIXME: Assumes vectorcall is in use.
1575 return isX86VectorCallAggregateSmallEnough(NumMembers);
1579 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1581 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
1582 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
1584 const X86_64ABIInfo &getABIInfo() const {
1585 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1588 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1592 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1593 llvm::Value *Address) const override {
1594 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1596 // 0-15 are the 16 integer registers.
1598 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1602 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1603 StringRef Constraint,
1604 llvm::Type* Ty) const override {
1605 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1608 bool isNoProtoCallVariadic(const CallArgList &args,
1609 const FunctionNoProtoType *fnType) const override {
1610 // The default CC on x86-64 sets %al to the number of SSA
1611 // registers used, and GCC sets this when calling an unprototyped
1612 // function, so we override the default behavior. However, don't do
1613 // that when AVX types are involved: the ABI explicitly states it is
1614 // undefined, and it doesn't work in practice because of how the ABI
1615 // defines varargs anyway.
1616 if (fnType->getCallConv() == CC_C) {
1617 bool HasAVXType = false;
1618 for (CallArgList::const_iterator
1619 it = args.begin(), ie = args.end(); it != ie; ++it) {
1620 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1630 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
1634 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1636 if (getABIInfo().has64BitPointers())
1637 Sig = (0xeb << 0) | // jmp rel8
1638 (0x0a << 8) | // .+0x0c
1642 Sig = (0xeb << 0) | // jmp rel8
1643 (0x06 << 8) | // .+0x08
1646 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1650 class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
1652 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
1653 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
1655 void getDependentLibraryOption(llvm::StringRef Lib,
1656 llvm::SmallString<24> &Opt) const override {
1662 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1663 // If the argument does not end in .lib, automatically add the suffix.
1664 // If the argument contains a space, enclose it in quotes.
1665 // This matches the behavior of MSVC.
1666 bool Quote = (Lib.find(" ") != StringRef::npos);
1667 std::string ArgStr = Quote ? "\"" : "";
1669 if (!Lib.endswith_lower(".lib"))
1671 ArgStr += Quote ? "\"" : "";
1675 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1677 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1678 bool d, bool p, bool w, unsigned RegParms)
1679 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
1681 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1682 CodeGen::CodeGenModule &CGM) const override;
1684 void getDependentLibraryOption(llvm::StringRef Lib,
1685 llvm::SmallString<24> &Opt) const override {
1686 Opt = "/DEFAULTLIB:";
1687 Opt += qualifyWindowsLibrary(Lib);
1690 void getDetectMismatchOption(llvm::StringRef Name,
1691 llvm::StringRef Value,
1692 llvm::SmallString<32> &Opt) const override {
1693 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1697 static void addStackProbeSizeTargetAttribute(const Decl *D,
1698 llvm::GlobalValue *GV,
1699 CodeGen::CodeGenModule &CGM) {
1700 if (isa<FunctionDecl>(D)) {
1701 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
1702 llvm::Function *Fn = cast<llvm::Function>(GV);
1704 Fn->addFnAttr("stack-probe-size",
1705 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
1710 void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
1711 llvm::GlobalValue *GV,
1712 CodeGen::CodeGenModule &CGM) const {
1713 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
1715 addStackProbeSizeTargetAttribute(D, GV, CGM);
1718 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1720 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1721 X86AVXABILevel AVXLevel)
1722 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1724 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1725 CodeGen::CodeGenModule &CGM) const override;
1727 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1731 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1732 llvm::Value *Address) const override {
1733 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1735 // 0-15 are the 16 integer registers.
1737 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1741 void getDependentLibraryOption(llvm::StringRef Lib,
1742 llvm::SmallString<24> &Opt) const override {
1743 Opt = "/DEFAULTLIB:";
1744 Opt += qualifyWindowsLibrary(Lib);
1747 void getDetectMismatchOption(llvm::StringRef Name,
1748 llvm::StringRef Value,
1749 llvm::SmallString<32> &Opt) const override {
1750 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1754 void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
1755 llvm::GlobalValue *GV,
1756 CodeGen::CodeGenModule &CGM) const {
1757 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
1759 addStackProbeSizeTargetAttribute(D, GV, CGM);
1763 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1765 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1767 // (a) If one of the classes is Memory, the whole argument is passed in
1770 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1773 // (c) If the size of the aggregate exceeds two eightbytes and the first
1774 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1775 // argument is passed in memory. NOTE: This is necessary to keep the
1776 // ABI working for processors that don't support the __m256 type.
1778 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1780 // Some of these are enforced by the merging logic. Others can arise
1781 // only with unions; for example:
1782 // union { _Complex double; unsigned; }
1784 // Note that clauses (b) and (c) were added in 0.98.
1788 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1790 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1792 if (Hi == SSEUp && Lo != SSE)
1796 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1797 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1798 // classified recursively so that always two fields are
1799 // considered. The resulting class is calculated according to
1800 // the classes of the fields in the eightbyte:
1802 // (a) If both classes are equal, this is the resulting class.
1804 // (b) If one of the classes is NO_CLASS, the resulting class is
1807 // (c) If one of the classes is MEMORY, the result is the MEMORY
1810 // (d) If one of the classes is INTEGER, the result is the
1813 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1814 // MEMORY is used as class.
1816 // (f) Otherwise class SSE is used.
1818 // Accum should never be memory (we should have returned) or
1819 // ComplexX87 (because this cannot be passed in a structure).
1820 assert((Accum != Memory && Accum != ComplexX87) &&
1821 "Invalid accumulated classification during merge.");
1822 if (Accum == Field || Field == NoClass)
1824 if (Field == Memory)
1826 if (Accum == NoClass)
1828 if (Accum == Integer || Field == Integer)
1830 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1831 Accum == X87 || Accum == X87Up)
1836 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1837 Class &Lo, Class &Hi, bool isNamedArg) const {
1838 // FIXME: This code can be simplified by introducing a simple value class for
1839 // Class pairs with appropriate constructor methods for the various
1842 // FIXME: Some of the split computations are wrong; unaligned vectors
1843 // shouldn't be passed in registers for example, so there is no chance they
1844 // can straddle an eightbyte. Verify & simplify.
1848 Class &Current = OffsetBase < 64 ? Lo : Hi;
1851 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1852 BuiltinType::Kind k = BT->getKind();
1854 if (k == BuiltinType::Void) {
1856 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1859 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1861 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1862 (k == BuiltinType::LongDouble &&
1863 getTarget().getTriple().isOSNaCl())) {
1865 } else if (k == BuiltinType::LongDouble) {
1869 // FIXME: _Decimal32 and _Decimal64 are SSE.
1870 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
1874 if (const EnumType *ET = Ty->getAs<EnumType>()) {
1875 // Classify the underlying integer type.
1876 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
1880 if (Ty->hasPointerRepresentation()) {
1885 if (Ty->isMemberPointerType()) {
1886 if (Ty->isMemberFunctionPointerType()) {
1887 if (Has64BitPointers) {
1888 // If Has64BitPointers, this is an {i64, i64}, so classify both
1892 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
1893 // straddles an eightbyte boundary, Hi should be classified as well.
1894 uint64_t EB_FuncPtr = (OffsetBase) / 64;
1895 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
1896 if (EB_FuncPtr != EB_ThisAdj) {
1908 if (const VectorType *VT = Ty->getAs<VectorType>()) {
1909 uint64_t Size = getContext().getTypeSize(VT);
1911 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1912 // float> as integer.
1915 // If this type crosses an eightbyte boundary, it should be
1917 uint64_t EB_Real = (OffsetBase) / 64;
1918 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1919 if (EB_Real != EB_Imag)
1921 } else if (Size == 64) {
1922 // gcc passes <1 x double> in memory. :(
1923 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1926 // gcc passes <1 x long long> as INTEGER.
1927 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
1928 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1929 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1930 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1935 // If this type crosses an eightbyte boundary, it should be
1937 if (OffsetBase && OffsetBase != 64)
1939 } else if (Size == 128 ||
1940 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
1941 // Arguments of 256-bits are split into four eightbyte chunks. The
1942 // least significant one belongs to class SSE and all the others to class
1943 // SSEUP. The original Lo and Hi design considers that types can't be
1944 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1945 // This design isn't correct for 256-bits, but since there're no cases
1946 // where the upper parts would need to be inspected, avoid adding
1947 // complexity and just consider Hi to match the 64-256 part.
1949 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1950 // registers if they are "named", i.e. not part of the "..." of a
1951 // variadic function.
1953 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
1954 // split into eight eightbyte chunks, one SSE and seven SSEUP.
1961 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1962 QualType ET = getContext().getCanonicalType(CT->getElementType());
1964 uint64_t Size = getContext().getTypeSize(Ty);
1965 if (ET->isIntegralOrEnumerationType()) {
1968 else if (Size <= 128)
1970 } else if (ET == getContext().FloatTy)
1972 else if (ET == getContext().DoubleTy ||
1973 (ET == getContext().LongDoubleTy &&
1974 getTarget().getTriple().isOSNaCl()))
1976 else if (ET == getContext().LongDoubleTy)
1977 Current = ComplexX87;
1979 // If this complex type crosses an eightbyte boundary then it
1981 uint64_t EB_Real = (OffsetBase) / 64;
1982 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1983 if (Hi == NoClass && EB_Real != EB_Imag)
1989 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1990 // Arrays are treated like structures.
1992 uint64_t Size = getContext().getTypeSize(Ty);
1994 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1995 // than four eightbytes, ..., it has class MEMORY.
1999 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2000 // fields, it has class MEMORY.
2002 // Only need to check alignment of array base.
2003 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2006 // Otherwise implement simplified merge. We could be smarter about
2007 // this, but it isn't worth it and would be harder to verify.
2009 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
2010 uint64_t ArraySize = AT->getSize().getZExtValue();
2012 // The only case a 256-bit wide vector could be used is when the array
2013 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2014 // to work for sizes wider than 128, early check and fallback to memory.
2015 if (Size > 128 && EltSize != 256)
2018 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2019 Class FieldLo, FieldHi;
2020 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
2021 Lo = merge(Lo, FieldLo);
2022 Hi = merge(Hi, FieldHi);
2023 if (Lo == Memory || Hi == Memory)
2027 postMerge(Size, Lo, Hi);
2028 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
2032 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2033 uint64_t Size = getContext().getTypeSize(Ty);
2035 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2036 // than four eightbytes, ..., it has class MEMORY.
2040 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2041 // copy constructor or a non-trivial destructor, it is passed by invisible
2043 if (getRecordArgABI(RT, getCXXABI()))
2046 const RecordDecl *RD = RT->getDecl();
2048 // Assume variable sized types are passed in memory.
2049 if (RD->hasFlexibleArrayMember())
2052 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2054 // Reset Lo class, this will be recomputed.
2057 // If this is a C++ record, classify the bases first.
2058 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2059 for (const auto &I : CXXRD->bases()) {
2060 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2061 "Unexpected base class!");
2062 const CXXRecordDecl *Base =
2063 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2065 // Classify this field.
2067 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2068 // single eightbyte, each is classified separately. Each eightbyte gets
2069 // initialized to class NO_CLASS.
2070 Class FieldLo, FieldHi;
2072 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
2073 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
2074 Lo = merge(Lo, FieldLo);
2075 Hi = merge(Hi, FieldHi);
2076 if (Lo == Memory || Hi == Memory) {
2077 postMerge(Size, Lo, Hi);
2083 // Classify the fields one at a time, merging the results.
2085 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2086 i != e; ++i, ++idx) {
2087 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2088 bool BitField = i->isBitField();
2090 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2091 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
2093 // The only case a 256-bit wide vector could be used is when the struct
2094 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2095 // to work for sizes wider than 128, early check and fallback to memory.
2097 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2099 postMerge(Size, Lo, Hi);
2102 // Note, skip this test for bit-fields, see below.
2103 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
2105 postMerge(Size, Lo, Hi);
2109 // Classify this field.
2111 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2112 // exceeds a single eightbyte, each is classified
2113 // separately. Each eightbyte gets initialized to class
2115 Class FieldLo, FieldHi;
2117 // Bit-fields require special handling, they do not force the
2118 // structure to be passed in memory even if unaligned, and
2119 // therefore they can straddle an eightbyte.
2121 // Ignore padding bit-fields.
2122 if (i->isUnnamedBitfield())
2125 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2126 uint64_t Size = i->getBitWidthValue(getContext());
2128 uint64_t EB_Lo = Offset / 64;
2129 uint64_t EB_Hi = (Offset + Size - 1) / 64;
2132 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2137 FieldHi = EB_Hi ? Integer : NoClass;
2140 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
2141 Lo = merge(Lo, FieldLo);
2142 Hi = merge(Hi, FieldHi);
2143 if (Lo == Memory || Hi == Memory)
2147 postMerge(Size, Lo, Hi);
2151 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
2152 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2154 if (!isAggregateTypeForABI(Ty)) {
2155 // Treat an enum type as its underlying type.
2156 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2157 Ty = EnumTy->getDecl()->getIntegerType();
2159 return (Ty->isPromotableIntegerType() ?
2160 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2163 return ABIArgInfo::getIndirect(0);
2166 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2167 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2168 uint64_t Size = getContext().getTypeSize(VecTy);
2169 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
2170 if (Size <= 64 || Size > LargestVector)
2177 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2178 unsigned freeIntRegs) const {
2179 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2182 // This assumption is optimistic, as there could be free registers available
2183 // when we need to pass this argument in memory, and LLVM could try to pass
2184 // the argument in the free register. This does not seem to happen currently,
2185 // but this code would be much safer if we could mark the argument with
2186 // 'onstack'. See PR12193.
2187 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
2188 // Treat an enum type as its underlying type.
2189 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2190 Ty = EnumTy->getDecl()->getIntegerType();
2192 return (Ty->isPromotableIntegerType() ?
2193 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2196 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
2197 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2199 // Compute the byval alignment. We specify the alignment of the byval in all
2200 // cases so that the mid-level optimizer knows the alignment of the byval.
2201 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
2203 // Attempt to avoid passing indirect results using byval when possible. This
2204 // is important for good codegen.
2206 // We do this by coercing the value into a scalar type which the backend can
2207 // handle naturally (i.e., without using byval).
2209 // For simplicity, we currently only do this when we have exhausted all of the
2210 // free integer registers. Doing this when there are free integer registers
2211 // would require more care, as we would have to ensure that the coerced value
2212 // did not claim the unused register. That would require either reording the
2213 // arguments to the function (so that any subsequent inreg values came first),
2214 // or only doing this optimization when there were no following arguments that
2217 // We currently expect it to be rare (particularly in well written code) for
2218 // arguments to be passed on the stack when there are still free integer
2219 // registers available (this would typically imply large structs being passed
2220 // by value), so this seems like a fair tradeoff for now.
2222 // We can revisit this if the backend grows support for 'onstack' parameter
2223 // attributes. See PR12193.
2224 if (freeIntRegs == 0) {
2225 uint64_t Size = getContext().getTypeSize(Ty);
2227 // If this type fits in an eightbyte, coerce it into the matching integral
2228 // type, which will end up on the stack (with alignment 8).
2229 if (Align == 8 && Size <= 64)
2230 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2234 return ABIArgInfo::getIndirect(Align);
2237 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
2238 /// register. Pick an LLVM IR type that will be passed as a vector register.
2239 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
2240 // Wrapper structs/arrays that only contain vectors are passed just like
2241 // vectors; strip them off if present.
2242 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2243 Ty = QualType(InnerTy, 0);
2245 llvm::Type *IRType = CGT.ConvertType(Ty);
2246 if(isa<llvm::VectorType>(IRType))
2249 // We couldn't find the preferred IR vector type for 'Ty'.
2250 uint64_t Size = getContext().getTypeSize(Ty);
2251 assert((Size == 128 || Size == 256) && "Invalid type found!");
2253 // Return a LLVM IR vector type based on the size of 'Ty'.
2254 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2258 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
2259 /// is known to either be off the end of the specified type or being in
2260 /// alignment padding. The user type specified is known to be at most 128 bits
2261 /// in size, and have passed through X86_64ABIInfo::classify with a successful
2262 /// classification that put one of the two halves in the INTEGER class.
2264 /// It is conservatively correct to return false.
2265 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2266 unsigned EndBit, ASTContext &Context) {
2267 // If the bytes being queried are off the end of the type, there is no user
2268 // data hiding here. This handles analysis of builtins, vectors and other
2269 // types that don't contain interesting padding.
2270 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2271 if (TySize <= StartBit)
2274 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2275 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2276 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2278 // Check each element to see if the element overlaps with the queried range.
2279 for (unsigned i = 0; i != NumElts; ++i) {
2280 // If the element is after the span we care about, then we're done..
2281 unsigned EltOffset = i*EltSize;
2282 if (EltOffset >= EndBit) break;
2284 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2285 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2286 EndBit-EltOffset, Context))
2289 // If it overlaps no elements, then it is safe to process as padding.
2293 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2294 const RecordDecl *RD = RT->getDecl();
2295 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2297 // If this is a C++ record, check the bases first.
2298 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2299 for (const auto &I : CXXRD->bases()) {
2300 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2301 "Unexpected base class!");
2302 const CXXRecordDecl *Base =
2303 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2305 // If the base is after the span we care about, ignore it.
2306 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
2307 if (BaseOffset >= EndBit) continue;
2309 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
2310 if (!BitsContainNoUserData(I.getType(), BaseStart,
2311 EndBit-BaseOffset, Context))
2316 // Verify that no field has data that overlaps the region of interest. Yes
2317 // this could be sped up a lot by being smarter about queried fields,
2318 // however we're only looking at structs up to 16 bytes, so we don't care
2321 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2322 i != e; ++i, ++idx) {
2323 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
2325 // If we found a field after the region we care about, then we're done.
2326 if (FieldOffset >= EndBit) break;
2328 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2329 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2334 // If nothing in this record overlapped the area of interest, then we're
2342 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2343 /// float member at the specified offset. For example, {int,{float}} has a
2344 /// float at offset 4. It is conservatively correct for this routine to return
2346 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
2347 const llvm::DataLayout &TD) {
2348 // Base case if we find a float.
2349 if (IROffset == 0 && IRType->isFloatTy())
2352 // If this is a struct, recurse into the field at the specified offset.
2353 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
2354 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2355 unsigned Elt = SL->getElementContainingOffset(IROffset);
2356 IROffset -= SL->getElementOffset(Elt);
2357 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2360 // If this is an array, recurse into the field at the specified offset.
2361 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2362 llvm::Type *EltTy = ATy->getElementType();
2363 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2364 IROffset -= IROffset/EltSize*EltSize;
2365 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2372 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2373 /// low 8 bytes of an XMM register, corresponding to the SSE class.
2374 llvm::Type *X86_64ABIInfo::
2375 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
2376 QualType SourceTy, unsigned SourceOffset) const {
2377 // The only three choices we have are either double, <2 x float>, or float. We
2378 // pass as float if the last 4 bytes is just padding. This happens for
2379 // structs that contain 3 floats.
2380 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2381 SourceOffset*8+64, getContext()))
2382 return llvm::Type::getFloatTy(getVMContext());
2384 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2385 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2387 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2388 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
2389 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
2391 return llvm::Type::getDoubleTy(getVMContext());
2395 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2396 /// an 8-byte GPR. This means that we either have a scalar or we are talking
2397 /// about the high or low part of an up-to-16-byte struct. This routine picks
2398 /// the best LLVM IR type to represent this, which may be i64 or may be anything
2399 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2402 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2403 /// the source type. IROffset is an offset in bytes into the LLVM IR type that
2404 /// the 8-byte value references. PrefType may be null.
2406 /// SourceTy is the source-level type for the entire argument. SourceOffset is
2407 /// an offset into this that we're processing (which is always either 0 or 8).
2409 llvm::Type *X86_64ABIInfo::
2410 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
2411 QualType SourceTy, unsigned SourceOffset) const {
2412 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2413 // returning an 8-byte unit starting with it. See if we can safely use it.
2414 if (IROffset == 0) {
2415 // Pointers and int64's always fill the 8-byte unit.
2416 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2417 IRType->isIntegerTy(64))
2420 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2421 // goodness in the source type is just tail padding. This is allowed to
2422 // kick in for struct {double,int} on the int, but not on
2423 // struct{double,int,int} because we wouldn't return the second int. We
2424 // have to do this analysis on the source type because we can't depend on
2425 // unions being lowered a specific way etc.
2426 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
2427 IRType->isIntegerTy(32) ||
2428 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2429 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2430 cast<llvm::IntegerType>(IRType)->getBitWidth();
2432 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2433 SourceOffset*8+64, getContext()))
2438 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
2439 // If this is a struct, recurse into the field at the specified offset.
2440 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
2441 if (IROffset < SL->getSizeInBytes()) {
2442 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2443 IROffset -= SL->getElementOffset(FieldIdx);
2445 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2446 SourceTy, SourceOffset);
2450 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2451 llvm::Type *EltTy = ATy->getElementType();
2452 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
2453 unsigned EltOffset = IROffset/EltSize*EltSize;
2454 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2458 // Okay, we don't have any better idea of what to pass, so we pass this in an
2459 // integer register that isn't too big to fit the rest of the struct.
2460 unsigned TySizeInBytes =
2461 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
2463 assert(TySizeInBytes != SourceOffset && "Empty field?");
2465 // It is always safe to classify this as an integer type up to i64 that
2466 // isn't larger than the structure.
2467 return llvm::IntegerType::get(getVMContext(),
2468 std::min(TySizeInBytes-SourceOffset, 8U)*8);
2472 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2473 /// be used as elements of a two register pair to pass or return, return a
2474 /// first class aggregate to represent them. For example, if the low part of
2475 /// a by-value argument should be passed as i32* and the high part as float,
2476 /// return {i32*, float}.
2478 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
2479 const llvm::DataLayout &TD) {
2480 // In order to correctly satisfy the ABI, we need to the high part to start
2481 // at offset 8. If the high and low parts we inferred are both 4-byte types
2482 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2483 // the second element at offset 8. Check for this:
2484 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2485 unsigned HiAlign = TD.getABITypeAlignment(Hi);
2486 unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign);
2487 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
2489 // To handle this, we have to increase the size of the low part so that the
2490 // second element will start at an 8 byte offset. We can't increase the size
2491 // of the second element because it might make us access off the end of the
2494 // There are usually two sorts of types the ABI generation code can produce
2495 // for the low part of a pair that aren't 8 bytes in size: float or
2496 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
2498 // Promote these to a larger type.
2499 if (Lo->isFloatTy())
2500 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2502 assert((Lo->isIntegerTy() || Lo->isPointerTy())
2503 && "Invalid/unknown lo type");
2504 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2508 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
2511 // Verify that the second element is at an 8-byte offset.
2512 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2513 "Invalid x86-64 argument pair!");
2517 ABIArgInfo X86_64ABIInfo::
2518 classifyReturnType(QualType RetTy) const {
2519 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2520 // classification algorithm.
2521 X86_64ABIInfo::Class Lo, Hi;
2522 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
2524 // Check some invariants.
2525 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2526 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2528 llvm::Type *ResType = nullptr;
2532 return ABIArgInfo::getIgnore();
2533 // If the low part is just padding, it takes no register, leave ResType
2535 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2536 "Unknown missing lo part");
2541 llvm_unreachable("Invalid classification for lo word.");
2543 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2546 return getIndirectReturnResult(RetTy);
2548 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2549 // available register of the sequence %rax, %rdx is used.
2551 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2553 // If we have a sign or zero extended integer, make sure to return Extend
2554 // so that the parameter gets the right LLVM IR attributes.
2555 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2556 // Treat an enum type as its underlying type.
2557 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2558 RetTy = EnumTy->getDecl()->getIntegerType();
2560 if (RetTy->isIntegralOrEnumerationType() &&
2561 RetTy->isPromotableIntegerType())
2562 return ABIArgInfo::getExtend();
2566 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2567 // available SSE register of the sequence %xmm0, %xmm1 is used.
2569 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2572 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2573 // returned on the X87 stack in %st0 as 80-bit x87 number.
2575 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
2578 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2579 // part of the value is returned in %st0 and the imaginary part in
2582 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
2583 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
2584 llvm::Type::getX86_FP80Ty(getVMContext()),
2589 llvm::Type *HighPart = nullptr;
2591 // Memory was handled previously and X87 should
2592 // never occur as a hi class.
2595 llvm_unreachable("Invalid classification for hi word.");
2597 case ComplexX87: // Previously handled.
2602 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2603 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2604 return ABIArgInfo::getDirect(HighPart, 8);
2607 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2608 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2609 return ABIArgInfo::getDirect(HighPart, 8);
2612 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
2613 // is passed in the next available eightbyte chunk if the last used
2616 // SSEUP should always be preceded by SSE, just widen.
2618 assert(Lo == SSE && "Unexpected SSEUp classification.");
2619 ResType = GetByteVectorType(RetTy);
2622 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2623 // returned together with the previous X87 value in %st0.
2625 // If X87Up is preceded by X87, we don't need to do
2626 // anything. However, in some cases with unions it may not be
2627 // preceded by X87. In such situations we follow gcc and pass the
2628 // extra bits in an SSE reg.
2630 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2631 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2632 return ABIArgInfo::getDirect(HighPart, 8);
2637 // If a high part was specified, merge it together with the low part. It is
2638 // known to pass in the high eightbyte of the result. We do this by forming a
2639 // first class struct aggregate with the high and low part: {low, high}
2641 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2643 return ABIArgInfo::getDirect(ResType);
2646 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2647 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2651 Ty = useFirstFieldIfTransparentUnion(Ty);
2653 X86_64ABIInfo::Class Lo, Hi;
2654 classify(Ty, 0, Lo, Hi, isNamedArg);
2656 // Check some invariants.
2657 // FIXME: Enforce these by construction.
2658 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2659 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2663 llvm::Type *ResType = nullptr;
2667 return ABIArgInfo::getIgnore();
2668 // If the low part is just padding, it takes no register, leave ResType
2670 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2671 "Unknown missing lo part");
2674 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2678 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2679 // COMPLEX_X87, it is passed in memory.
2682 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
2684 return getIndirectResult(Ty, freeIntRegs);
2688 llvm_unreachable("Invalid classification for lo word.");
2690 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2691 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2696 // Pick an 8-byte type based on the preferred type.
2697 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2699 // If we have a sign or zero extended integer, make sure to return Extend
2700 // so that the parameter gets the right LLVM IR attributes.
2701 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2702 // Treat an enum type as its underlying type.
2703 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2704 Ty = EnumTy->getDecl()->getIntegerType();
2706 if (Ty->isIntegralOrEnumerationType() &&
2707 Ty->isPromotableIntegerType())
2708 return ABIArgInfo::getExtend();
2713 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2714 // available SSE register is used, the registers are taken in the
2715 // order from %xmm0 to %xmm7.
2717 llvm::Type *IRType = CGT.ConvertType(Ty);
2718 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
2724 llvm::Type *HighPart = nullptr;
2726 // Memory was handled previously, ComplexX87 and X87 should
2727 // never occur as hi classes, and X87Up must be preceded by X87,
2728 // which is passed in memory.
2732 llvm_unreachable("Invalid classification for hi word.");
2734 case NoClass: break;
2738 // Pick an 8-byte type based on the preferred type.
2739 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2741 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2742 return ABIArgInfo::getDirect(HighPart, 8);
2745 // X87Up generally doesn't occur here (long double is passed in
2746 // memory), except in situations involving unions.
2749 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2751 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2752 return ABIArgInfo::getDirect(HighPart, 8);
2757 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2758 // eightbyte is passed in the upper half of the last used SSE
2759 // register. This only happens when 128-bit vectors are passed.
2761 assert(Lo == SSE && "Unexpected SSEUp classification");
2762 ResType = GetByteVectorType(Ty);
2766 // If a high part was specified, merge it together with the low part. It is
2767 // known to pass in the high eightbyte of the result. We do this by forming a
2768 // first class struct aggregate with the high and low part: {low, high}
2770 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2772 return ABIArgInfo::getDirect(ResType);
2775 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2777 if (!getCXXABI().classifyReturnType(FI))
2778 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2780 // Keep track of the number of assigned registers.
2781 unsigned freeIntRegs = 6, freeSSERegs = 8;
2783 // If the return value is indirect, then the hidden argument is consuming one
2784 // integer register.
2785 if (FI.getReturnInfo().isIndirect())
2788 // The chain argument effectively gives us another free register.
2789 if (FI.isChainCall())
2792 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
2793 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2794 // get assigned (in left-to-right order) for passing as follows...
2796 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2797 it != ie; ++it, ++ArgNo) {
2798 bool IsNamedArg = ArgNo < NumRequiredArgs;
2800 unsigned neededInt, neededSSE;
2801 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2802 neededSSE, IsNamedArg);
2804 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2805 // eightbyte of an argument, the whole argument is passed on the
2806 // stack. If registers have already been assigned for some
2807 // eightbytes of such an argument, the assignments get reverted.
2808 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2809 freeIntRegs -= neededInt;
2810 freeSSERegs -= neededSSE;
2812 it->info = getIndirectResult(it->type, freeIntRegs);
2817 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2819 CodeGenFunction &CGF) {
2820 llvm::Value *overflow_arg_area_p = CGF.Builder.CreateStructGEP(
2821 nullptr, VAListAddr, 2, "overflow_arg_area_p");
2822 llvm::Value *overflow_arg_area =
2823 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2825 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2826 // byte boundary if alignment needed by type exceeds 8 byte boundary.
2827 // It isn't stated explicitly in the standard, but in practice we use
2828 // alignment greater than 16 where necessary.
2829 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2831 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
2832 llvm::Value *Offset =
2833 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2834 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2835 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
2837 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2839 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2840 overflow_arg_area->getType(),
2841 "overflow_arg_area.align");
2844 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
2845 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2847 CGF.Builder.CreateBitCast(overflow_arg_area,
2848 llvm::PointerType::getUnqual(LTy));
2850 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2851 // l->overflow_arg_area + sizeof(type).
2852 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2853 // an 8 byte boundary.
2855 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
2856 llvm::Value *Offset =
2857 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
2858 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2859 "overflow_arg_area.next");
2860 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2862 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2866 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2867 CodeGenFunction &CGF) const {
2868 // Assume that va_list type is correct; should be pointer to LLVM type:
2872 // i8* overflow_arg_area;
2873 // i8* reg_save_area;
2875 unsigned neededInt, neededSSE;
2877 Ty = CGF.getContext().getCanonicalType(Ty);
2878 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2879 /*isNamedArg*/false);
2881 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2882 // in the registers. If not go to step 7.
2883 if (!neededInt && !neededSSE)
2884 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2886 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2887 // general purpose registers needed to pass type and num_fp to hold
2888 // the number of floating point registers needed.
2890 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2891 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2892 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2894 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2895 // register save space).
2897 llvm::Value *InRegs = nullptr;
2898 llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr;
2899 llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr;
2902 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "gp_offset_p");
2903 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
2904 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2905 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2910 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 1, "fp_offset_p");
2911 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2912 llvm::Value *FitsInFP =
2913 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2914 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2915 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2918 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2919 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2920 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2921 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2923 // Emit code to load the value if it was passed in registers.
2925 CGF.EmitBlock(InRegBlock);
2927 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2928 // an offset of l->gp_offset and/or l->fp_offset. This may require
2929 // copying to a temporary location in case the parameter is passed
2930 // in different register classes or requires an alignment greater
2931 // than 8 for general purpose registers and 16 for XMM registers.
2933 // FIXME: This really results in shameful code when we end up needing to
2934 // collect arguments from different places; often what should result in a
2935 // simple assembling of a structure from scattered addresses has many more
2936 // loads than necessary. Can we clean this up?
2937 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2938 llvm::Value *RegAddr = CGF.Builder.CreateLoad(
2939 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3), "reg_save_area");
2940 if (neededInt && neededSSE) {
2942 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
2943 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2944 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2945 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
2946 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2947 llvm::Type *TyLo = ST->getElementType(0);
2948 llvm::Type *TyHi = ST->getElementType(1);
2949 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2950 "Unexpected ABI info for mixed regs");
2951 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2952 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2953 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2954 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2955 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
2956 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
2958 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2959 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0));
2960 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2961 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1));
2963 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2964 llvm::PointerType::getUnqual(LTy));
2965 } else if (neededInt) {
2966 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2967 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2968 llvm::PointerType::getUnqual(LTy));
2970 // Copy to a temporary if necessary to ensure the appropriate alignment.
2971 std::pair<CharUnits, CharUnits> SizeAlign =
2972 CGF.getContext().getTypeInfoInChars(Ty);
2973 uint64_t TySize = SizeAlign.first.getQuantity();
2974 unsigned TyAlign = SizeAlign.second.getQuantity();
2976 llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2977 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2980 } else if (neededSSE == 1) {
2981 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2982 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2983 llvm::PointerType::getUnqual(LTy));
2985 assert(neededSSE == 2 && "Invalid number of needed registers!");
2986 // SSE registers are spaced 16 bytes apart in the register save
2987 // area, we need to collect the two eightbytes together.
2988 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2989 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
2990 llvm::Type *DoubleTy = CGF.DoubleTy;
2991 llvm::Type *DblPtrTy =
2992 llvm::PointerType::getUnqual(DoubleTy);
2993 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
2994 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2995 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
2996 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2998 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0));
2999 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
3001 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1));
3002 RegAddr = CGF.Builder.CreateBitCast(Tmp,
3003 llvm::PointerType::getUnqual(LTy));
3006 // AMD64-ABI 3.5.7p5: Step 5. Set:
3007 // l->gp_offset = l->gp_offset + num_gp * 8
3008 // l->fp_offset = l->fp_offset + num_fp * 16.
3010 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
3011 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3015 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
3016 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3019 CGF.EmitBranch(ContBlock);
3021 // Emit code to load the value if it was passed in memory.
3023 CGF.EmitBlock(InMemBlock);
3024 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
3026 // Return the appropriate result.
3028 CGF.EmitBlock(ContBlock);
3029 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
3031 ResAddr->addIncoming(RegAddr, InRegBlock);
3032 ResAddr->addIncoming(MemAddr, InMemBlock);
3036 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3037 bool IsReturnType) const {
3039 if (Ty->isVoidType())
3040 return ABIArgInfo::getIgnore();
3042 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3043 Ty = EnumTy->getDecl()->getIntegerType();
3045 TypeInfo Info = getContext().getTypeInfo(Ty);
3046 uint64_t Width = Info.Width;
3047 unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity();
3049 const RecordType *RT = Ty->getAs<RecordType>();
3051 if (!IsReturnType) {
3052 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
3053 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3056 if (RT->getDecl()->hasFlexibleArrayMember())
3057 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3059 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
3060 if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment())
3061 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3065 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3067 const Type *Base = nullptr;
3068 uint64_t NumElts = 0;
3069 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
3070 if (FreeSSERegs >= NumElts) {
3071 FreeSSERegs -= NumElts;
3072 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3073 return ABIArgInfo::getDirect();
3074 return ABIArgInfo::getExpand();
3076 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3080 if (Ty->isMemberPointerType()) {
3081 // If the member pointer is represented by an LLVM int or ptr, pass it
3083 llvm::Type *LLTy = CGT.ConvertType(Ty);
3084 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3085 return ABIArgInfo::getDirect();
3088 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
3089 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3090 // not 1, 2, 4, or 8 bytes, must be passed by reference."
3091 if (Width > 64 || !llvm::isPowerOf2_64(Width))
3092 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3094 // Otherwise, coerce it to a small integer.
3095 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
3098 // Bool type is always extended to the ABI, other builtin types are not
3100 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3101 if (BT && BT->getKind() == BuiltinType::Bool)
3102 return ABIArgInfo::getExtend();
3104 return ABIArgInfo::getDirect();
3107 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3109 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
3111 // We can use up to 4 SSE return registers with vectorcall.
3112 unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3113 if (!getCXXABI().classifyReturnType(FI))
3114 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3116 // We can use up to 6 SSE register parameters with vectorcall.
3117 FreeSSERegs = IsVectorCall ? 6 : 0;
3118 for (auto &I : FI.arguments())
3119 I.info = classify(I.type, FreeSSERegs, false);
3122 llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3123 CodeGenFunction &CGF) const {
3124 llvm::Type *BPP = CGF.Int8PtrPtrTy;
3126 CGBuilderTy &Builder = CGF.Builder;
3127 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3129 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3131 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3132 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3135 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
3136 llvm::Value *NextAddr =
3137 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3139 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3146 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3147 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
3149 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
3151 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3152 CodeGenFunction &CGF) const override;
3155 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3157 PPC32TargetCodeGenInfo(CodeGenTypes &CGT)
3158 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {}
3160 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3161 // This is recovered from gcc output.
3162 return 1; // r1 is the dedicated stack pointer
3165 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3166 llvm::Value *Address) const override;
3171 llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
3173 CodeGenFunction &CGF) const {
3174 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3175 // TODO: Implement this. For now ignore.
3180 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
3182 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
3183 llvm::Type *CharPtr = CGF.Int8PtrTy;
3184 llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy;
3186 CGBuilderTy &Builder = CGF.Builder;
3187 llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr");
3188 llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty);
3189 llvm::Value *FPRPtrAsInt =
3190 Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1));
3191 llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr);
3192 llvm::Value *OverflowAreaPtrAsInt =
3193 Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3));
3194 llvm::Value *OverflowAreaPtr =
3195 Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr);
3196 llvm::Value *RegsaveAreaPtrAsInt =
3197 Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4));
3198 llvm::Value *RegsaveAreaPtr =
3199 Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr);
3200 llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr");
3201 // Align GPR when TY is i64.
3203 llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1));
3204 llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1));
3205 llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1));
3206 GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR);
3208 llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr");
3209 llvm::Value *OverflowArea =
3210 Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area");
3211 llvm::Value *OverflowAreaAsInt =
3212 Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty);
3213 llvm::Value *RegsaveArea =
3214 Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area");
3215 llvm::Value *RegsaveAreaAsInt =
3216 Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty);
3219 Builder.CreateICmpULT(isInt ? GPR : FPR, Builder.getInt8(8), "cond");
3221 llvm::Value *RegConstant =
3222 Builder.CreateMul(isInt ? GPR : FPR, Builder.getInt8(isInt ? 4 : 8));
3224 llvm::Value *OurReg = Builder.CreateAdd(
3225 RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty));
3227 if (Ty->isFloatingType())
3228 OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32));
3230 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3231 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3232 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3234 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3236 CGF.EmitBlock(UsingRegs);
3238 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3239 llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy);
3240 // Increase the GPR/FPR indexes.
3242 GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1));
3243 Builder.CreateStore(GPR, GPRPtr);
3245 FPR = Builder.CreateAdd(FPR, Builder.getInt8(1));
3246 Builder.CreateStore(FPR, FPRPtr);
3248 CGF.EmitBranch(Cont);
3250 CGF.EmitBlock(UsingOverflow);
3252 // Increase the overflow area.
3253 llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy);
3255 Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8));
3256 Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr),
3258 CGF.EmitBranch(Cont);
3260 CGF.EmitBlock(Cont);
3262 llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr");
3263 Result->addIncoming(Result1, UsingRegs);
3264 Result->addIncoming(Result2, UsingOverflow);
3266 if (Ty->isAggregateType()) {
3267 llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr");
3268 return Builder.CreateLoad(AGGPtr, false, "aggr");
3275 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3276 llvm::Value *Address) const {
3277 // This is calculated from the LLVM and GCC tables and verified
3278 // against gcc output. AFAIK all ABIs use the same encoding.
3280 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3282 llvm::IntegerType *i8 = CGF.Int8Ty;
3283 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3284 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3285 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3287 // 0-31: r0-31, the 4-byte general-purpose registers
3288 AssignToArrayRange(Builder, Address, Four8, 0, 31);
3290 // 32-63: fp0-31, the 8-byte floating-point registers
3291 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
3293 // 64-76 are various 4-byte special-purpose registers:
3300 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3302 // 77-108: v0-31, the 16-byte vector registers
3303 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3310 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3318 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
3319 class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
3327 static const unsigned GPRBits = 64;
3331 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
3332 // will be passed in a QPX register.
3333 bool IsQPXVectorTy(const Type *Ty) const {
3337 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3338 unsigned NumElements = VT->getNumElements();
3339 if (NumElements == 1)
3342 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
3343 if (getContext().getTypeSize(Ty) <= 256)
3345 } else if (VT->getElementType()->
3346 isSpecificBuiltinType(BuiltinType::Float)) {
3347 if (getContext().getTypeSize(Ty) <= 128)
3355 bool IsQPXVectorTy(QualType Ty) const {
3356 return IsQPXVectorTy(Ty.getTypePtr());
3360 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX)
3361 : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {}
3363 bool isPromotableTypeForABI(QualType Ty) const;
3364 bool isAlignedParamType(QualType Ty, bool &Align32) const;
3366 ABIArgInfo classifyReturnType(QualType RetTy) const;
3367 ABIArgInfo classifyArgumentType(QualType Ty) const;
3369 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3370 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3371 uint64_t Members) const override;
3373 // TODO: We can add more logic to computeInfo to improve performance.
3374 // Example: For aggregate arguments that fit in a register, we could
3375 // use getDirectInReg (as is done below for structs containing a single
3376 // floating-point value) to avoid pushing them to memory on function
3377 // entry. This would require changing the logic in PPCISelLowering
3378 // when lowering the parameters in the caller and args in the callee.
3379 void computeInfo(CGFunctionInfo &FI) const override {
3380 if (!getCXXABI().classifyReturnType(FI))
3381 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3382 for (auto &I : FI.arguments()) {
3383 // We rely on the default argument classification for the most part.
3384 // One exception: An aggregate containing a single floating-point
3385 // or vector item must be passed in a register if one is available.
3386 const Type *T = isSingleElementStruct(I.type, getContext());
3388 const BuiltinType *BT = T->getAs<BuiltinType>();
3389 if (IsQPXVectorTy(T) ||
3390 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
3391 (BT && BT->isFloatingPoint())) {
3393 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
3397 I.info = classifyArgumentType(I.type);
3401 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3402 CodeGenFunction &CGF) const override;
3405 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
3408 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
3409 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX)
3410 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {}
3412 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3413 // This is recovered from gcc output.
3414 return 1; // r1 is the dedicated stack pointer
3417 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3418 llvm::Value *Address) const override;
3421 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3423 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3425 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3426 // This is recovered from gcc output.
3427 return 1; // r1 is the dedicated stack pointer
3430 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3431 llvm::Value *Address) const override;
3436 // Return true if the ABI requires Ty to be passed sign- or zero-
3437 // extended to 64 bits.
3439 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
3440 // Treat an enum type as its underlying type.
3441 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3442 Ty = EnumTy->getDecl()->getIntegerType();
3444 // Promotable integer types are required to be promoted by the ABI.
3445 if (Ty->isPromotableIntegerType())
3448 // In addition to the usual promotable integer types, we also need to
3449 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
3450 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3451 switch (BT->getKind()) {
3452 case BuiltinType::Int:
3453 case BuiltinType::UInt:
3462 /// isAlignedParamType - Determine whether a type requires 16-byte
3463 /// alignment in the parameter area.
3465 PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const {
3468 // Complex types are passed just like their elements.
3469 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
3470 Ty = CTy->getElementType();
3472 // Only vector types of size 16 bytes need alignment (larger types are
3473 // passed via reference, smaller types are not aligned).
3474 if (IsQPXVectorTy(Ty)) {
3475 if (getContext().getTypeSize(Ty) > 128)
3479 } else if (Ty->isVectorType()) {
3480 return getContext().getTypeSize(Ty) == 128;
3483 // For single-element float/vector structs, we consider the whole type
3484 // to have the same alignment requirements as its single element.
3485 const Type *AlignAsType = nullptr;
3486 const Type *EltType = isSingleElementStruct(Ty, getContext());
3488 const BuiltinType *BT = EltType->getAs<BuiltinType>();
3489 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
3490 getContext().getTypeSize(EltType) == 128) ||
3491 (BT && BT->isFloatingPoint()))
3492 AlignAsType = EltType;
3495 // Likewise for ELFv2 homogeneous aggregates.
3496 const Type *Base = nullptr;
3497 uint64_t Members = 0;
3498 if (!AlignAsType && Kind == ELFv2 &&
3499 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
3502 // With special case aggregates, only vector base types need alignment.
3503 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
3504 if (getContext().getTypeSize(AlignAsType) > 128)
3508 } else if (AlignAsType) {
3509 return AlignAsType->isVectorType();
3512 // Otherwise, we only need alignment for any aggregate type that
3513 // has an alignment requirement of >= 16 bytes.
3514 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
3515 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
3523 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
3524 /// aggregate. Base is set to the base element type, and Members is set
3525 /// to the number of base elements.
3526 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
3527 uint64_t &Members) const {
3528 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3529 uint64_t NElements = AT->getSize().getZExtValue();
3532 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
3534 Members *= NElements;
3535 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3536 const RecordDecl *RD = RT->getDecl();
3537 if (RD->hasFlexibleArrayMember())
3542 // If this is a C++ record, check the bases first.
3543 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3544 for (const auto &I : CXXRD->bases()) {
3545 // Ignore empty records.
3546 if (isEmptyRecord(getContext(), I.getType(), true))
3549 uint64_t FldMembers;
3550 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
3553 Members += FldMembers;
3557 for (const auto *FD : RD->fields()) {
3558 // Ignore (non-zero arrays of) empty records.
3559 QualType FT = FD->getType();
3560 while (const ConstantArrayType *AT =
3561 getContext().getAsConstantArrayType(FT)) {
3562 if (AT->getSize().getZExtValue() == 0)
3564 FT = AT->getElementType();
3566 if (isEmptyRecord(getContext(), FT, true))
3569 // For compatibility with GCC, ignore empty bitfields in C++ mode.
3570 if (getContext().getLangOpts().CPlusPlus &&
3571 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
3574 uint64_t FldMembers;
3575 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
3578 Members = (RD->isUnion() ?
3579 std::max(Members, FldMembers) : Members + FldMembers);
3585 // Ensure there is no padding.
3586 if (getContext().getTypeSize(Base) * Members !=
3587 getContext().getTypeSize(Ty))
3591 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3593 Ty = CT->getElementType();
3596 // Most ABIs only support float, double, and some vector type widths.
3597 if (!isHomogeneousAggregateBaseType(Ty))
3600 // The base type must be the same for all members. Types that
3601 // agree in both total size and mode (float vs. vector) are
3602 // treated as being equivalent here.
3603 const Type *TyPtr = Ty.getTypePtr();
3607 if (Base->isVectorType() != TyPtr->isVectorType() ||
3608 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
3611 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
3614 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
3615 // Homogeneous aggregates for ELFv2 must have base types of float,
3616 // double, long double, or 128-bit vectors.
3617 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3618 if (BT->getKind() == BuiltinType::Float ||
3619 BT->getKind() == BuiltinType::Double ||
3620 BT->getKind() == BuiltinType::LongDouble)
3623 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3624 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
3630 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
3631 const Type *Base, uint64_t Members) const {
3632 // Vector types require one register, floating point types require one
3633 // or two registers depending on their size.
3635 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
3637 // Homogeneous Aggregates may occupy at most 8 registers.
3638 return Members * NumRegs <= 8;
3642 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
3643 Ty = useFirstFieldIfTransparentUnion(Ty);
3645 if (Ty->isAnyComplexType())
3646 return ABIArgInfo::getDirect();
3648 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
3649 // or via reference (larger than 16 bytes).
3650 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
3651 uint64_t Size = getContext().getTypeSize(Ty);
3653 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3654 else if (Size < 128) {
3655 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
3656 return ABIArgInfo::getDirect(CoerceTy);
3660 if (isAggregateTypeForABI(Ty)) {
3661 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3662 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3665 uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ?
3666 (Align32 ? 32 : 16) : 8;
3667 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3669 // ELFv2 homogeneous aggregates are passed as array types.
3670 const Type *Base = nullptr;
3671 uint64_t Members = 0;
3672 if (Kind == ELFv2 &&
3673 isHomogeneousAggregate(Ty, Base, Members)) {
3674 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
3675 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
3676 return ABIArgInfo::getDirect(CoerceTy);
3679 // If an aggregate may end up fully in registers, we do not
3680 // use the ByVal method, but pass the aggregate as array.
3681 // This is usually beneficial since we avoid forcing the
3682 // back-end to store the argument to memory.
3683 uint64_t Bits = getContext().getTypeSize(Ty);
3684 if (Bits > 0 && Bits <= 8 * GPRBits) {
3685 llvm::Type *CoerceTy;
3687 // Types up to 8 bytes are passed as integer type (which will be
3688 // properly aligned in the argument save area doubleword).
3689 if (Bits <= GPRBits)
3690 CoerceTy = llvm::IntegerType::get(getVMContext(),
3691 llvm::RoundUpToAlignment(Bits, 8));
3692 // Larger types are passed as arrays, with the base type selected
3693 // according to the required alignment in the save area.
3695 uint64_t RegBits = ABIAlign * 8;
3696 uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits;
3697 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
3698 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
3701 return ABIArgInfo::getDirect(CoerceTy);
3704 // All other aggregates are passed ByVal.
3705 return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true,
3706 /*Realign=*/TyAlign > ABIAlign);
3709 return (isPromotableTypeForABI(Ty) ?
3710 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3714 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
3715 if (RetTy->isVoidType())
3716 return ABIArgInfo::getIgnore();
3718 if (RetTy->isAnyComplexType())
3719 return ABIArgInfo::getDirect();
3721 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
3722 // or via reference (larger than 16 bytes).
3723 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
3724 uint64_t Size = getContext().getTypeSize(RetTy);
3726 return ABIArgInfo::getIndirect(0);
3727 else if (Size < 128) {
3728 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
3729 return ABIArgInfo::getDirect(CoerceTy);
3733 if (isAggregateTypeForABI(RetTy)) {
3734 // ELFv2 homogeneous aggregates are returned as array types.
3735 const Type *Base = nullptr;
3736 uint64_t Members = 0;
3737 if (Kind == ELFv2 &&
3738 isHomogeneousAggregate(RetTy, Base, Members)) {
3739 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
3740 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
3741 return ABIArgInfo::getDirect(CoerceTy);
3744 // ELFv2 small aggregates are returned in up to two registers.
3745 uint64_t Bits = getContext().getTypeSize(RetTy);
3746 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
3748 return ABIArgInfo::getIgnore();
3750 llvm::Type *CoerceTy;
3751 if (Bits > GPRBits) {
3752 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
3753 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
3755 CoerceTy = llvm::IntegerType::get(getVMContext(),
3756 llvm::RoundUpToAlignment(Bits, 8));
3757 return ABIArgInfo::getDirect(CoerceTy);
3760 // All other aggregates are returned indirectly.
3761 return ABIArgInfo::getIndirect(0);
3764 return (isPromotableTypeForABI(RetTy) ?
3765 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3768 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
3769 llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
3771 CodeGenFunction &CGF) const {
3772 llvm::Type *BP = CGF.Int8PtrTy;
3773 llvm::Type *BPP = CGF.Int8PtrPtrTy;
3775 CGBuilderTy &Builder = CGF.Builder;
3776 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3777 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3779 // Handle types that require 16-byte alignment in the parameter save area.
3781 if (isAlignedParamType(Ty, Align32)) {
3782 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3783 AddrAsInt = Builder.CreateAdd(AddrAsInt,
3784 Builder.getInt64(Align32 ? 31 : 15));
3785 AddrAsInt = Builder.CreateAnd(AddrAsInt,
3786 Builder.getInt64(Align32 ? -32 : -16));
3787 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
3790 // Update the va_list pointer. The pointer should be bumped by the
3791 // size of the object. We can trust getTypeSize() except for a complex
3792 // type whose base type is smaller than a doubleword. For these, the
3793 // size of the object is 16 bytes; see below for further explanation.
3794 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
3796 unsigned CplxBaseSize = 0;
3798 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3799 BaseTy = CTy->getElementType();
3800 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
3801 if (CplxBaseSize < 8)
3805 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
3806 llvm::Value *NextAddr =
3807 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
3809 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3811 // If we have a complex type and the base type is smaller than 8 bytes,
3812 // the ABI calls for the real and imaginary parts to be right-adjusted
3813 // in separate doublewords. However, Clang expects us to produce a
3814 // pointer to a structure with the two parts packed tightly. So generate
3815 // loads of the real and imaginary parts relative to the va_list pointer,
3816 // and store them to a temporary structure.
3817 if (CplxBaseSize && CplxBaseSize < 8) {
3818 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3819 llvm::Value *ImagAddr = RealAddr;
3820 if (CGF.CGM.getDataLayout().isBigEndian()) {
3822 Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
3824 Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
3826 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8));
3828 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
3829 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
3830 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
3831 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
3832 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
3833 llvm::AllocaInst *Ptr =
3834 CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), "vacplx");
3835 llvm::Value *RealPtr =
3836 Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 0, ".real");
3837 llvm::Value *ImagPtr =
3838 Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 1, ".imag");
3839 Builder.CreateStore(Real, RealPtr, false);
3840 Builder.CreateStore(Imag, ImagPtr, false);
3844 // If the argument is smaller than 8 bytes, it is right-adjusted in
3845 // its doubleword slot. Adjust the pointer to pick it up from the
3847 if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) {
3848 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
3849 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
3850 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3853 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3854 return Builder.CreateBitCast(Addr, PTy);
3858 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3859 llvm::Value *Address) {
3860 // This is calculated from the LLVM and GCC tables and verified
3861 // against gcc output. AFAIK all ABIs use the same encoding.
3863 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3865 llvm::IntegerType *i8 = CGF.Int8Ty;
3866 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3867 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3868 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3870 // 0-31: r0-31, the 8-byte general-purpose registers
3871 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
3873 // 32-63: fp0-31, the 8-byte floating-point registers
3874 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
3876 // 64-76 are various 4-byte special-purpose registers:
3883 AssignToArrayRange(Builder, Address, Four8, 64, 76);
3885 // 77-108: v0-31, the 16-byte vector registers
3886 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3893 AssignToArrayRange(Builder, Address, Four8, 109, 113);
3899 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3900 CodeGen::CodeGenFunction &CGF,
3901 llvm::Value *Address) const {
3903 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3907 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3908 llvm::Value *Address) const {
3910 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3913 //===----------------------------------------------------------------------===//
3914 // AArch64 ABI Implementation
3915 //===----------------------------------------------------------------------===//
3919 class AArch64ABIInfo : public ABIInfo {
3930 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
3933 ABIKind getABIKind() const { return Kind; }
3934 bool isDarwinPCS() const { return Kind == DarwinPCS; }
3936 ABIArgInfo classifyReturnType(QualType RetTy) const;
3937 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3938 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3939 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3940 uint64_t Members) const override;
3942 bool isIllegalVectorType(QualType Ty) const;
3944 void computeInfo(CGFunctionInfo &FI) const override {
3945 if (!getCXXABI().classifyReturnType(FI))
3946 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3948 for (auto &it : FI.arguments())
3949 it.info = classifyArgumentType(it.type);
3952 llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty,
3953 CodeGenFunction &CGF) const;
3955 llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty,
3956 CodeGenFunction &CGF) const;
3958 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3959 CodeGenFunction &CGF) const override {
3960 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
3961 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
3965 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3967 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
3968 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
3970 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
3971 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
3974 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3978 bool doesReturnSlotInterfereWithArgs() const override { return false; }
3982 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
3983 Ty = useFirstFieldIfTransparentUnion(Ty);
3985 // Handle illegal vector types here.
3986 if (isIllegalVectorType(Ty)) {
3987 uint64_t Size = getContext().getTypeSize(Ty);
3989 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
3990 return ABIArgInfo::getDirect(ResType);
3993 llvm::Type *ResType =
3994 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
3995 return ABIArgInfo::getDirect(ResType);
3998 llvm::Type *ResType =
3999 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
4000 return ABIArgInfo::getDirect(ResType);
4002 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4005 if (!isAggregateTypeForABI(Ty)) {
4006 // Treat an enum type as its underlying type.
4007 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4008 Ty = EnumTy->getDecl()->getIntegerType();
4010 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4011 ? ABIArgInfo::getExtend()
4012 : ABIArgInfo::getDirect());
4015 // Structures with either a non-trivial destructor or a non-trivial
4016 // copy constructor are always indirect.
4017 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
4018 return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA ==
4019 CGCXXABI::RAA_DirectInMemory);
4022 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4023 // elsewhere for GNU compatibility.
4024 if (isEmptyRecord(getContext(), Ty, true)) {
4025 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4026 return ABIArgInfo::getIgnore();
4028 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4031 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
4032 const Type *Base = nullptr;
4033 uint64_t Members = 0;
4034 if (isHomogeneousAggregate(Ty, Base, Members)) {
4035 return ABIArgInfo::getDirect(
4036 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
4039 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4040 uint64_t Size = getContext().getTypeSize(Ty);
4042 unsigned Alignment = getContext().getTypeAlign(Ty);
4043 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
4045 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4046 // For aggregates with 16-byte alignment, we use i128.
4047 if (Alignment < 128 && Size == 128) {
4048 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4049 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4051 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4054 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4057 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
4058 if (RetTy->isVoidType())
4059 return ABIArgInfo::getIgnore();
4061 // Large vector types should be returned via memory.
4062 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
4063 return ABIArgInfo::getIndirect(0);
4065 if (!isAggregateTypeForABI(RetTy)) {
4066 // Treat an enum type as its underlying type.
4067 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4068 RetTy = EnumTy->getDecl()->getIntegerType();
4070 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4071 ? ABIArgInfo::getExtend()
4072 : ABIArgInfo::getDirect());
4075 if (isEmptyRecord(getContext(), RetTy, true))
4076 return ABIArgInfo::getIgnore();
4078 const Type *Base = nullptr;
4079 uint64_t Members = 0;
4080 if (isHomogeneousAggregate(RetTy, Base, Members))
4081 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4082 return ABIArgInfo::getDirect();
4084 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
4085 uint64_t Size = getContext().getTypeSize(RetTy);
4087 unsigned Alignment = getContext().getTypeAlign(RetTy);
4088 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
4090 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4091 // For aggregates with 16-byte alignment, we use i128.
4092 if (Alignment < 128 && Size == 128) {
4093 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4094 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4096 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4099 return ABIArgInfo::getIndirect(0);
4102 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
4103 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
4104 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4105 // Check whether VT is legal.
4106 unsigned NumElements = VT->getNumElements();
4107 uint64_t Size = getContext().getTypeSize(VT);
4108 // NumElements should be power of 2 between 1 and 16.
4109 if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16)
4111 return Size != 64 && (Size != 128 || NumElements == 1);
4116 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4117 // Homogeneous aggregates for AAPCS64 must have base types of a floating
4118 // point type or a short-vector type. This is the same as the 32-bit ABI,
4119 // but with the difference that any floating-point type is allowed,
4120 // including __fp16.
4121 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4122 if (BT->isFloatingPoint())
4124 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4125 unsigned VecSize = getContext().getTypeSize(VT);
4126 if (VecSize == 64 || VecSize == 128)
4132 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4133 uint64_t Members) const {
4134 return Members <= 4;
4137 llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr,
4139 CodeGenFunction &CGF) const {
4140 ABIArgInfo AI = classifyArgumentType(Ty);
4141 bool IsIndirect = AI.isIndirect();
4143 llvm::Type *BaseTy = CGF.ConvertType(Ty);
4145 BaseTy = llvm::PointerType::getUnqual(BaseTy);
4146 else if (AI.getCoerceToType())
4147 BaseTy = AI.getCoerceToType();
4149 unsigned NumRegs = 1;
4150 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4151 BaseTy = ArrTy->getElementType();
4152 NumRegs = ArrTy->getNumElements();
4154 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4156 // The AArch64 va_list type and handling is specified in the Procedure Call
4157 // Standard, section B.4:
4167 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4168 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4169 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4170 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4171 auto &Ctx = CGF.getContext();
4173 llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr;
4175 int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8;
4177 // 3 is the field number of __gr_offs
4179 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "gr_offs_p");
4180 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4181 reg_top_index = 1; // field number for __gr_top
4182 RegSize = llvm::RoundUpToAlignment(RegSize, 8);
4184 // 4 is the field number of __vr_offs.
4186 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 4, "vr_offs_p");
4187 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4188 reg_top_index = 2; // field number for __vr_top
4189 RegSize = 16 * NumRegs;
4192 //=======================================
4193 // Find out where argument was passed
4194 //=======================================
4196 // If reg_offs >= 0 we're already using the stack for this type of
4197 // argument. We don't want to keep updating reg_offs (in case it overflows,
4198 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4199 // whatever they get).
4200 llvm::Value *UsingStack = nullptr;
4201 UsingStack = CGF.Builder.CreateICmpSGE(
4202 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4204 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4206 // Otherwise, at least some kind of argument could go in these registers, the
4207 // question is whether this particular type is too big.
4208 CGF.EmitBlock(MaybeRegBlock);
4210 // Integer arguments may need to correct register alignment (for example a
4211 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4212 // align __gr_offs to calculate the potential address.
4213 if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) {
4214 int Align = Ctx.getTypeAlign(Ty) / 8;
4216 reg_offs = CGF.Builder.CreateAdd(
4217 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4219 reg_offs = CGF.Builder.CreateAnd(
4220 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4224 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
4225 llvm::Value *NewOffset = nullptr;
4226 NewOffset = CGF.Builder.CreateAdd(
4227 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4228 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4230 // Now we're in a position to decide whether this argument really was in
4231 // registers or not.
4232 llvm::Value *InRegs = nullptr;
4233 InRegs = CGF.Builder.CreateICmpSLE(
4234 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4236 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4238 //=======================================
4239 // Argument was in registers
4240 //=======================================
4242 // Now we emit the code for if the argument was originally passed in
4243 // registers. First start the appropriate block:
4244 CGF.EmitBlock(InRegBlock);
4246 llvm::Value *reg_top_p = nullptr, *reg_top = nullptr;
4247 reg_top_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, reg_top_index,
4249 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4250 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4251 llvm::Value *RegAddr = nullptr;
4252 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4255 // If it's been passed indirectly (actually a struct), whatever we find from
4256 // stored registers or on the stack will actually be a struct **.
4257 MemTy = llvm::PointerType::getUnqual(MemTy);
4260 const Type *Base = nullptr;
4261 uint64_t NumMembers = 0;
4262 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
4263 if (IsHFA && NumMembers > 1) {
4264 // Homogeneous aggregates passed in registers will have their elements split
4265 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4266 // qN+1, ...). We reload and store into a temporary local variable
4268 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
4269 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4270 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4271 llvm::AllocaInst *Tmp = CGF.CreateTempAlloca(HFATy);
4274 if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128)
4275 Offset = 16 - Ctx.getTypeSize(Base) / 8;
4276 for (unsigned i = 0; i < NumMembers; ++i) {
4277 llvm::Value *BaseOffset =
4278 llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset);
4279 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4280 LoadAddr = CGF.Builder.CreateBitCast(
4281 LoadAddr, llvm::PointerType::getUnqual(BaseTy));
4282 llvm::Value *StoreAddr =
4283 CGF.Builder.CreateStructGEP(Tmp->getAllocatedType(), Tmp, i);
4285 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4286 CGF.Builder.CreateStore(Elem, StoreAddr);
4289 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4291 // Otherwise the object is contiguous in memory
4292 unsigned BeAlign = reg_top_index == 2 ? 16 : 8;
4293 if (CGF.CGM.getDataLayout().isBigEndian() &&
4294 (IsHFA || !isAggregateTypeForABI(Ty)) &&
4295 Ctx.getTypeSize(Ty) < (BeAlign * 8)) {
4296 int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8;
4297 BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty);
4299 BaseAddr = CGF.Builder.CreateAdd(
4300 BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be");
4302 BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy);
4305 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4308 CGF.EmitBranch(ContBlock);
4310 //=======================================
4311 // Argument was on the stack
4312 //=======================================
4313 CGF.EmitBlock(OnStackBlock);
4315 llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr;
4316 stack_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "stack_p");
4317 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4319 // Again, stack arguments may need realigmnent. In this case both integer and
4320 // floating-point ones might be affected.
4321 if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) {
4322 int Align = Ctx.getTypeAlign(Ty) / 8;
4324 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4326 OnStackAddr = CGF.Builder.CreateAdd(
4327 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4329 OnStackAddr = CGF.Builder.CreateAnd(
4330 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4333 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4340 StackSize = Ctx.getTypeSize(Ty) / 8;
4342 // All stack slots are 8 bytes
4343 StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4345 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4346 llvm::Value *NewStack =
4347 CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack");
4349 // Write the new value of __stack for the next call to va_arg
4350 CGF.Builder.CreateStore(NewStack, stack_p);
4352 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
4353 Ctx.getTypeSize(Ty) < 64) {
4354 int Offset = 8 - Ctx.getTypeSize(Ty) / 8;
4355 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4357 OnStackAddr = CGF.Builder.CreateAdd(
4358 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be");
4360 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4363 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4365 CGF.EmitBranch(ContBlock);
4367 //=======================================
4369 //=======================================
4370 CGF.EmitBlock(ContBlock);
4372 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4373 ResAddr->addIncoming(RegAddr, InRegBlock);
4374 ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4377 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4382 llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr,
4384 CodeGenFunction &CGF) const {
4385 // We do not support va_arg for aggregates or illegal vector types.
4386 // Lower VAArg here for these cases and use the LLVM va_arg instruction for
4388 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
4391 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
4392 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
4394 const Type *Base = nullptr;
4395 uint64_t Members = 0;
4396 bool isHA = isHomogeneousAggregate(Ty, Base, Members);
4398 bool isIndirect = false;
4399 // Arguments bigger than 16 bytes which aren't homogeneous aggregates should
4400 // be passed indirectly.
4401 if (Size > 16 && !isHA) {
4407 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
4408 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
4410 CGBuilderTy &Builder = CGF.Builder;
4411 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4412 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4414 if (isEmptyRecord(getContext(), Ty, true)) {
4415 // These are ignored for parameter passing purposes.
4416 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4417 return Builder.CreateBitCast(Addr, PTy);
4420 const uint64_t MinABIAlign = 8;
4421 if (Align > MinABIAlign) {
4422 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
4423 Addr = Builder.CreateGEP(Addr, Offset);
4424 llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
4425 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1));
4426 llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask);
4427 Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align");
4430 uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign);
4431 llvm::Value *NextAddr = Builder.CreateGEP(
4432 Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
4433 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4436 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
4437 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4438 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4443 //===----------------------------------------------------------------------===//
4444 // ARM ABI Implementation
4445 //===----------------------------------------------------------------------===//
4449 class ARMABIInfo : public ABIInfo {
4461 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
4465 bool isEABI() const {
4466 switch (getTarget().getTriple().getEnvironment()) {
4467 case llvm::Triple::Android:
4468 case llvm::Triple::EABI:
4469 case llvm::Triple::EABIHF:
4470 case llvm::Triple::GNUEABI:
4471 case llvm::Triple::GNUEABIHF:
4478 bool isEABIHF() const {
4479 switch (getTarget().getTriple().getEnvironment()) {
4480 case llvm::Triple::EABIHF:
4481 case llvm::Triple::GNUEABIHF:
4488 ABIKind getABIKind() const { return Kind; }
4491 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
4492 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
4493 bool isIllegalVectorType(QualType Ty) const;
4495 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4496 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4497 uint64_t Members) const override;
4499 void computeInfo(CGFunctionInfo &FI) const override;
4501 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4502 CodeGenFunction &CGF) const override;
4504 llvm::CallingConv::ID getLLVMDefaultCC() const;
4505 llvm::CallingConv::ID getABIDefaultCC() const;
4509 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
4511 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4512 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
4514 const ARMABIInfo &getABIInfo() const {
4515 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
4518 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4522 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
4523 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
4526 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4527 llvm::Value *Address) const override {
4528 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
4530 // 0-15 are the 16 integer registers.
4531 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
4535 unsigned getSizeOfUnwindException() const override {
4536 if (getABIInfo().isEABI()) return 88;
4537 return TargetCodeGenInfo::getSizeOfUnwindException();
4540 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4541 CodeGen::CodeGenModule &CGM) const override {
4542 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4546 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
4551 switch (Attr->getInterrupt()) {
4552 case ARMInterruptAttr::Generic: Kind = ""; break;
4553 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
4554 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
4555 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
4556 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
4557 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
4560 llvm::Function *Fn = cast<llvm::Function>(GV);
4562 Fn->addFnAttr("interrupt", Kind);
4564 if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS)
4567 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
4568 // however this is not necessarily true on taking any interrupt. Instruct
4569 // the backend to perform a realignment as part of the function prologue.
4570 llvm::AttrBuilder B;
4571 B.addStackAlignmentAttr(8);
4572 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
4573 llvm::AttributeSet::get(CGM.getLLVMContext(),
4574 llvm::AttributeSet::FunctionIndex,
4579 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
4580 void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV,
4581 CodeGen::CodeGenModule &CGM) const;
4584 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4585 : ARMTargetCodeGenInfo(CGT, K) {}
4587 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4588 CodeGen::CodeGenModule &CGM) const override;
4591 void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute(
4592 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
4593 if (!isa<FunctionDecl>(D))
4595 if (CGM.getCodeGenOpts().StackProbeSize == 4096)
4598 llvm::Function *F = cast<llvm::Function>(GV);
4599 F->addFnAttr("stack-probe-size",
4600 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
4603 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
4604 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
4605 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
4606 addStackProbeSizeTargetAttribute(D, GV, CGM);
4610 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
4611 if (!getCXXABI().classifyReturnType(FI))
4612 FI.getReturnInfo() =
4613 classifyReturnType(FI.getReturnType(), FI.isVariadic());
4615 for (auto &I : FI.arguments())
4616 I.info = classifyArgumentType(I.type, FI.isVariadic());
4618 // Always honor user-specified calling convention.
4619 if (FI.getCallingConvention() != llvm::CallingConv::C)
4622 llvm::CallingConv::ID cc = getRuntimeCC();
4623 if (cc != llvm::CallingConv::C)
4624 FI.setEffectiveCallingConvention(cc);
4627 /// Return the default calling convention that LLVM will use.
4628 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
4629 // The default calling convention that LLVM will infer.
4631 return llvm::CallingConv::ARM_AAPCS_VFP;
4633 return llvm::CallingConv::ARM_AAPCS;
4635 return llvm::CallingConv::ARM_APCS;
4638 /// Return the calling convention that our ABI would like us to use
4639 /// as the C calling convention.
4640 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
4641 switch (getABIKind()) {
4642 case APCS: return llvm::CallingConv::ARM_APCS;
4643 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
4644 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
4646 llvm_unreachable("bad ABI kind");
4649 void ARMABIInfo::setCCs() {
4650 assert(getRuntimeCC() == llvm::CallingConv::C);
4652 // Don't muddy up the IR with a ton of explicit annotations if
4653 // they'd just match what LLVM will infer from the triple.
4654 llvm::CallingConv::ID abiCC = getABIDefaultCC();
4655 if (abiCC != getLLVMDefaultCC())
4658 BuiltinCC = (getABIKind() == APCS ?
4659 llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS);
4662 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
4663 bool isVariadic) const {
4664 // 6.1.2.1 The following argument types are VFP CPRCs:
4665 // A single-precision floating-point type (including promoted
4666 // half-precision types); A double-precision floating-point type;
4667 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
4668 // with a Base Type of a single- or double-precision floating-point type,
4669 // 64-bit containerized vectors or 128-bit containerized vectors with one
4670 // to four Elements.
4671 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
4673 Ty = useFirstFieldIfTransparentUnion(Ty);
4675 // Handle illegal vector types here.
4676 if (isIllegalVectorType(Ty)) {
4677 uint64_t Size = getContext().getTypeSize(Ty);
4679 llvm::Type *ResType =
4680 llvm::Type::getInt32Ty(getVMContext());
4681 return ABIArgInfo::getDirect(ResType);
4684 llvm::Type *ResType = llvm::VectorType::get(
4685 llvm::Type::getInt32Ty(getVMContext()), 2);
4686 return ABIArgInfo::getDirect(ResType);
4689 llvm::Type *ResType = llvm::VectorType::get(
4690 llvm::Type::getInt32Ty(getVMContext()), 4);
4691 return ABIArgInfo::getDirect(ResType);
4693 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4696 if (!isAggregateTypeForABI(Ty)) {
4697 // Treat an enum type as its underlying type.
4698 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
4699 Ty = EnumTy->getDecl()->getIntegerType();
4702 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
4703 : ABIArgInfo::getDirect());
4706 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
4707 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4710 // Ignore empty records.
4711 if (isEmptyRecord(getContext(), Ty, true))
4712 return ABIArgInfo::getIgnore();
4714 if (IsEffectivelyAAPCS_VFP) {
4715 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
4716 // into VFP registers.
4717 const Type *Base = nullptr;
4718 uint64_t Members = 0;
4719 if (isHomogeneousAggregate(Ty, Base, Members)) {
4720 assert(Base && "Base class should be set for homogeneous aggregate");
4721 // Base can be a floating-point or a vector.
4722 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
4726 // Support byval for ARM.
4727 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
4728 // most 8-byte. We realign the indirect argument if type alignment is bigger
4729 // than ABI alignment.
4730 uint64_t ABIAlign = 4;
4731 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
4732 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
4733 getABIKind() == ARMABIInfo::AAPCS)
4734 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
4736 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
4737 return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true,
4738 /*Realign=*/TyAlign > ABIAlign);
4741 // Otherwise, pass by coercing to a structure of the appropriate size.
4744 // FIXME: Try to match the types of the arguments more accurately where
4746 if (getContext().getTypeAlign(Ty) <= 32) {
4747 ElemTy = llvm::Type::getInt32Ty(getVMContext());
4748 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
4750 ElemTy = llvm::Type::getInt64Ty(getVMContext());
4751 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
4754 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
4757 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
4758 llvm::LLVMContext &VMContext) {
4759 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
4760 // is called integer-like if its size is less than or equal to one word, and
4761 // the offset of each of its addressable sub-fields is zero.
4763 uint64_t Size = Context.getTypeSize(Ty);
4765 // Check that the type fits in a word.
4769 // FIXME: Handle vector types!
4770 if (Ty->isVectorType())
4773 // Float types are never treated as "integer like".
4774 if (Ty->isRealFloatingType())
4777 // If this is a builtin or pointer type then it is ok.
4778 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
4781 // Small complex integer types are "integer like".
4782 if (const ComplexType *CT = Ty->getAs<ComplexType>())
4783 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
4785 // Single element and zero sized arrays should be allowed, by the definition
4786 // above, but they are not.
4788 // Otherwise, it must be a record type.
4789 const RecordType *RT = Ty->getAs<RecordType>();
4790 if (!RT) return false;
4792 // Ignore records with flexible arrays.
4793 const RecordDecl *RD = RT->getDecl();
4794 if (RD->hasFlexibleArrayMember())
4797 // Check that all sub-fields are at offset 0, and are themselves "integer
4799 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
4801 bool HadField = false;
4803 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4804 i != e; ++i, ++idx) {
4805 const FieldDecl *FD = *i;
4807 // Bit-fields are not addressable, we only need to verify they are "integer
4808 // like". We still have to disallow a subsequent non-bitfield, for example:
4809 // struct { int : 0; int x }
4810 // is non-integer like according to gcc.
4811 if (FD->isBitField()) {
4815 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
4821 // Check if this field is at offset 0.
4822 if (Layout.getFieldOffset(idx) != 0)
4825 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
4828 // Only allow at most one field in a structure. This doesn't match the
4829 // wording above, but follows gcc in situations with a field following an
4831 if (!RD->isUnion()) {
4842 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
4843 bool isVariadic) const {
4844 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
4846 if (RetTy->isVoidType())
4847 return ABIArgInfo::getIgnore();
4849 // Large vector types should be returned via memory.
4850 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
4851 return ABIArgInfo::getIndirect(0);
4854 if (!isAggregateTypeForABI(RetTy)) {
4855 // Treat an enum type as its underlying type.
4856 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4857 RetTy = EnumTy->getDecl()->getIntegerType();
4859 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
4860 : ABIArgInfo::getDirect();
4863 // Are we following APCS?
4864 if (getABIKind() == APCS) {
4865 if (isEmptyRecord(getContext(), RetTy, false))
4866 return ABIArgInfo::getIgnore();
4868 // Complex types are all returned as packed integers.
4870 // FIXME: Consider using 2 x vector types if the back end handles them
4872 if (RetTy->isAnyComplexType())
4873 return ABIArgInfo::getDirect(llvm::IntegerType::get(
4874 getVMContext(), getContext().getTypeSize(RetTy)));
4876 // Integer like structures are returned in r0.
4877 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
4878 // Return in the smallest viable integer type.
4879 uint64_t Size = getContext().getTypeSize(RetTy);
4881 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4883 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4884 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4887 // Otherwise return in memory.
4888 return ABIArgInfo::getIndirect(0);
4891 // Otherwise this is an AAPCS variant.
4893 if (isEmptyRecord(getContext(), RetTy, true))
4894 return ABIArgInfo::getIgnore();
4896 // Check for homogeneous aggregates with AAPCS-VFP.
4897 if (IsEffectivelyAAPCS_VFP) {
4898 const Type *Base = nullptr;
4900 if (isHomogeneousAggregate(RetTy, Base, Members)) {
4901 assert(Base && "Base class should be set for homogeneous aggregate");
4902 // Homogeneous Aggregates are returned directly.
4903 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
4907 // Aggregates <= 4 bytes are returned in r0; other aggregates
4908 // are returned indirectly.
4909 uint64_t Size = getContext().getTypeSize(RetTy);
4911 if (getDataLayout().isBigEndian())
4912 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
4913 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4915 // Return in the smallest viable integer type.
4917 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4919 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4920 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4923 return ABIArgInfo::getIndirect(0);
4926 /// isIllegalVector - check whether Ty is an illegal vector type.
4927 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
4928 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4929 // Check whether VT is legal.
4930 unsigned NumElements = VT->getNumElements();
4931 uint64_t Size = getContext().getTypeSize(VT);
4932 // NumElements should be power of 2.
4933 if ((NumElements & (NumElements - 1)) != 0)
4935 // Size should be greater than 32 bits.
4941 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4942 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
4943 // double, or 64-bit or 128-bit vectors.
4944 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4945 if (BT->getKind() == BuiltinType::Float ||
4946 BT->getKind() == BuiltinType::Double ||
4947 BT->getKind() == BuiltinType::LongDouble)
4949 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4950 unsigned VecSize = getContext().getTypeSize(VT);
4951 if (VecSize == 64 || VecSize == 128)
4957 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4958 uint64_t Members) const {
4959 return Members <= 4;
4962 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4963 CodeGenFunction &CGF) const {
4964 llvm::Type *BP = CGF.Int8PtrTy;
4965 llvm::Type *BPP = CGF.Int8PtrPtrTy;
4967 CGBuilderTy &Builder = CGF.Builder;
4968 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4969 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4971 if (isEmptyRecord(getContext(), Ty, true)) {
4972 // These are ignored for parameter passing purposes.
4973 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4974 return Builder.CreateBitCast(Addr, PTy);
4977 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
4978 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
4979 bool IsIndirect = false;
4981 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
4982 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
4983 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
4984 getABIKind() == ARMABIInfo::AAPCS)
4985 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
4988 // Use indirect if size of the illegal vector is bigger than 16 bytes.
4989 if (isIllegalVectorType(Ty) && Size > 16) {
4995 // Handle address alignment for ABI alignment > 4 bytes.
4997 assert((TyAlign & (TyAlign - 1)) == 0 &&
4998 "Alignment is not power of 2!");
4999 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
5000 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
5001 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
5002 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
5006 llvm::RoundUpToAlignment(Size, 4);
5007 llvm::Value *NextAddr =
5008 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5010 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5013 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
5014 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
5015 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
5016 // may not be correctly aligned for the vector type. We create an aligned
5017 // temporary space and copy the content over from ap.cur to the temporary
5018 // space. This is necessary if the natural alignment of the type is greater
5019 // than the ABI alignment.
5020 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
5021 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
5022 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
5024 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
5025 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
5026 Builder.CreateMemCpy(Dst, Src,
5027 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
5029 Addr = AlignedTemp; //The content is in aligned location.
5032 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5033 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5038 //===----------------------------------------------------------------------===//
5039 // NVPTX ABI Implementation
5040 //===----------------------------------------------------------------------===//
5044 class NVPTXABIInfo : public ABIInfo {
5046 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5048 ABIArgInfo classifyReturnType(QualType RetTy) const;
5049 ABIArgInfo classifyArgumentType(QualType Ty) const;
5051 void computeInfo(CGFunctionInfo &FI) const override;
5052 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5053 CodeGenFunction &CFG) const override;
5056 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
5058 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5059 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
5061 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5062 CodeGen::CodeGenModule &M) const override;
5064 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5065 // resulting MDNode to the nvvm.annotations MDNode.
5066 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
5069 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
5070 if (RetTy->isVoidType())
5071 return ABIArgInfo::getIgnore();
5073 // note: this is different from default ABI
5074 if (!RetTy->isScalarType())
5075 return ABIArgInfo::getDirect();
5077 // Treat an enum type as its underlying type.
5078 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5079 RetTy = EnumTy->getDecl()->getIntegerType();
5081 return (RetTy->isPromotableIntegerType() ?
5082 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5085 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
5086 // Treat an enum type as its underlying type.
5087 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5088 Ty = EnumTy->getDecl()->getIntegerType();
5090 // Return aggregates type as indirect by value
5091 if (isAggregateTypeForABI(Ty))
5092 return ABIArgInfo::getIndirect(0, /* byval */ true);
5094 return (Ty->isPromotableIntegerType() ?
5095 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5098 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
5099 if (!getCXXABI().classifyReturnType(FI))
5100 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5101 for (auto &I : FI.arguments())
5102 I.info = classifyArgumentType(I.type);
5104 // Always honor user-specified calling convention.
5105 if (FI.getCallingConvention() != llvm::CallingConv::C)
5108 FI.setEffectiveCallingConvention(getRuntimeCC());
5111 llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5112 CodeGenFunction &CFG) const {
5113 llvm_unreachable("NVPTX does not support varargs");
5116 void NVPTXTargetCodeGenInfo::
5117 setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5118 CodeGen::CodeGenModule &M) const{
5119 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5122 llvm::Function *F = cast<llvm::Function>(GV);
5124 // Perform special handling in OpenCL mode
5125 if (M.getLangOpts().OpenCL) {
5126 // Use OpenCL function attributes to check for kernel functions
5127 // By default, all functions are device functions
5128 if (FD->hasAttr<OpenCLKernelAttr>()) {
5129 // OpenCL __kernel functions get kernel metadata
5130 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5131 addNVVMMetadata(F, "kernel", 1);
5132 // And kernel functions are not subject to inlining
5133 F->addFnAttr(llvm::Attribute::NoInline);
5137 // Perform special handling in CUDA mode.
5138 if (M.getLangOpts().CUDA) {
5139 // CUDA __global__ functions get a kernel metadata entry. Since
5140 // __global__ functions cannot be called from the device, we do not
5141 // need to set the noinline attribute.
5142 if (FD->hasAttr<CUDAGlobalAttr>()) {
5143 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5144 addNVVMMetadata(F, "kernel", 1);
5146 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
5147 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
5148 llvm::APSInt MaxThreads(32);
5149 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
5151 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
5153 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
5154 // not specified in __launch_bounds__ or if the user specified a 0 value,
5155 // we don't have to add a PTX directive.
5156 if (Attr->getMinBlocks()) {
5157 llvm::APSInt MinBlocks(32);
5158 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
5160 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5161 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
5167 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5169 llvm::Module *M = F->getParent();
5170 llvm::LLVMContext &Ctx = M->getContext();
5172 // Get "nvvm.annotations" metadata node
5173 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5175 llvm::Metadata *MDVals[] = {
5176 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5177 llvm::ConstantAsMetadata::get(
5178 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
5179 // Append metadata to nvvm.annotations
5180 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5184 //===----------------------------------------------------------------------===//
5185 // SystemZ ABI Implementation
5186 //===----------------------------------------------------------------------===//
5190 class SystemZABIInfo : public ABIInfo {
5194 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
5195 : ABIInfo(CGT), HasVector(HV) {}
5197 bool isPromotableIntegerType(QualType Ty) const;
5198 bool isCompoundType(QualType Ty) const;
5199 bool isVectorArgumentType(QualType Ty) const;
5200 bool isFPArgumentType(QualType Ty) const;
5201 QualType GetSingleElementType(QualType Ty) const;
5203 ABIArgInfo classifyReturnType(QualType RetTy) const;
5204 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5206 void computeInfo(CGFunctionInfo &FI) const override {
5207 if (!getCXXABI().classifyReturnType(FI))
5208 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5209 for (auto &I : FI.arguments())
5210 I.info = classifyArgumentType(I.type);
5213 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5214 CodeGenFunction &CGF) const override;
5217 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5219 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
5220 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
5225 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5226 // Treat an enum type as its underlying type.
5227 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5228 Ty = EnumTy->getDecl()->getIntegerType();
5230 // Promotable integer types are required to be promoted by the ABI.
5231 if (Ty->isPromotableIntegerType())
5234 // 32-bit values must also be promoted.
5235 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5236 switch (BT->getKind()) {
5237 case BuiltinType::Int:
5238 case BuiltinType::UInt:
5246 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
5247 return (Ty->isAnyComplexType() ||
5248 Ty->isVectorType() ||
5249 isAggregateTypeForABI(Ty));
5252 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
5253 return (HasVector &&
5254 Ty->isVectorType() &&
5255 getContext().getTypeSize(Ty) <= 128);
5258 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5259 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5260 switch (BT->getKind()) {
5261 case BuiltinType::Float:
5262 case BuiltinType::Double:
5271 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
5272 if (const RecordType *RT = Ty->getAsStructureType()) {
5273 const RecordDecl *RD = RT->getDecl();
5276 // If this is a C++ record, check the bases first.
5277 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
5278 for (const auto &I : CXXRD->bases()) {
5279 QualType Base = I.getType();
5281 // Empty bases don't affect things either way.
5282 if (isEmptyRecord(getContext(), Base, true))
5285 if (!Found.isNull())
5287 Found = GetSingleElementType(Base);
5290 // Check the fields.
5291 for (const auto *FD : RD->fields()) {
5292 // For compatibility with GCC, ignore empty bitfields in C++ mode.
5293 // Unlike isSingleElementStruct(), empty structure and array fields
5294 // do count. So do anonymous bitfields that aren't zero-sized.
5295 if (getContext().getLangOpts().CPlusPlus &&
5296 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5299 // Unlike isSingleElementStruct(), arrays do not count.
5300 // Nested structures still do though.
5301 if (!Found.isNull())
5303 Found = GetSingleElementType(FD->getType());
5306 // Unlike isSingleElementStruct(), trailing padding is allowed.
5307 // An 8-byte aligned struct s { float f; } is passed as a double.
5308 if (!Found.isNull())
5315 llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5316 CodeGenFunction &CGF) const {
5317 // Assume that va_list type is correct; should be pointer to LLVM type:
5321 // i8 *__overflow_arg_area;
5322 // i8 *__reg_save_area;
5325 // Every non-vector argument occupies 8 bytes and is passed by preference
5326 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
5327 // always passed on the stack.
5328 Ty = CGF.getContext().getCanonicalType(Ty);
5329 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
5330 llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy);
5331 ABIArgInfo AI = classifyArgumentType(Ty);
5332 bool IsIndirect = AI.isIndirect();
5333 bool InFPRs = false;
5334 bool IsVector = false;
5335 unsigned UnpaddedBitSize;
5337 APTy = llvm::PointerType::getUnqual(APTy);
5338 UnpaddedBitSize = 64;
5340 if (AI.getCoerceToType())
5341 ArgTy = AI.getCoerceToType();
5342 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
5343 IsVector = ArgTy->isVectorTy();
5344 UnpaddedBitSize = getContext().getTypeSize(Ty);
5346 unsigned PaddedBitSize = (IsVector && UnpaddedBitSize > 64) ? 128 : 64;
5347 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
5349 unsigned PaddedSize = PaddedBitSize / 8;
5350 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
5352 llvm::Type *IndexTy = CGF.Int64Ty;
5353 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
5356 // Work out the address of a vector argument on the stack.
5357 // Vector arguments are always passed in the high bits of a
5358 // single (8 byte) or double (16 byte) stack slot.
5359 llvm::Value *OverflowArgAreaPtr =
5360 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 2,
5361 "overflow_arg_area_ptr");
5362 llvm::Value *OverflowArgArea =
5363 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
5364 llvm::Value *MemAddr =
5365 CGF.Builder.CreateBitCast(OverflowArgArea, APTy, "mem_addr");
5367 // Update overflow_arg_area_ptr pointer
5368 llvm::Value *NewOverflowArgArea =
5369 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
5370 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5375 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
5377 MaxRegs = 4; // Maximum of 4 FPR arguments
5378 RegCountField = 1; // __fpr
5379 RegSaveIndex = 16; // save offset for f0
5380 RegPadding = 0; // floats are passed in the high bits of an FPR
5382 MaxRegs = 5; // Maximum of 5 GPR arguments
5383 RegCountField = 0; // __gpr
5384 RegSaveIndex = 2; // save offset for r2
5385 RegPadding = Padding; // values are passed in the low bits of a GPR
5388 llvm::Value *RegCountPtr = CGF.Builder.CreateStructGEP(
5389 nullptr, VAListAddr, RegCountField, "reg_count_ptr");
5390 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
5391 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
5392 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
5395 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5396 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
5397 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5398 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
5400 // Emit code to load the value if it was passed in registers.
5401 CGF.EmitBlock(InRegBlock);
5403 // Work out the address of an argument register.
5404 llvm::Value *ScaledRegCount =
5405 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
5406 llvm::Value *RegBase =
5407 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
5408 llvm::Value *RegOffset =
5409 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
5410 llvm::Value *RegSaveAreaPtr =
5411 CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "reg_save_area_ptr");
5412 llvm::Value *RegSaveArea =
5413 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
5414 llvm::Value *RawRegAddr =
5415 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
5416 llvm::Value *RegAddr =
5417 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
5419 // Update the register count
5420 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
5421 llvm::Value *NewRegCount =
5422 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
5423 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
5424 CGF.EmitBranch(ContBlock);
5426 // Emit code to load the value if it was passed in memory.
5427 CGF.EmitBlock(InMemBlock);
5429 // Work out the address of a stack argument.
5430 llvm::Value *OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
5431 nullptr, VAListAddr, 2, "overflow_arg_area_ptr");
5432 llvm::Value *OverflowArgArea =
5433 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
5434 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
5435 llvm::Value *RawMemAddr =
5436 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
5437 llvm::Value *MemAddr =
5438 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
5440 // Update overflow_arg_area_ptr pointer
5441 llvm::Value *NewOverflowArgArea =
5442 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
5443 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5444 CGF.EmitBranch(ContBlock);
5446 // Return the appropriate result.
5447 CGF.EmitBlock(ContBlock);
5448 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
5449 ResAddr->addIncoming(RegAddr, InRegBlock);
5450 ResAddr->addIncoming(MemAddr, InMemBlock);
5453 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
5458 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
5459 if (RetTy->isVoidType())
5460 return ABIArgInfo::getIgnore();
5461 if (isVectorArgumentType(RetTy))
5462 return ABIArgInfo::getDirect();
5463 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
5464 return ABIArgInfo::getIndirect(0);
5465 return (isPromotableIntegerType(RetTy) ?
5466 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5469 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
5470 // Handle the generic C++ ABI.
5471 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5472 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
5474 // Integers and enums are extended to full register width.
5475 if (isPromotableIntegerType(Ty))
5476 return ABIArgInfo::getExtend();
5478 // Handle vector types and vector-like structure types. Note that
5479 // as opposed to float-like structure types, we do not allow any
5480 // padding for vector-like structures, so verify the sizes match.
5481 uint64_t Size = getContext().getTypeSize(Ty);
5482 QualType SingleElementTy = GetSingleElementType(Ty);
5483 if (isVectorArgumentType(SingleElementTy) &&
5484 getContext().getTypeSize(SingleElementTy) == Size)
5485 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
5487 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
5488 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
5489 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5491 // Handle small structures.
5492 if (const RecordType *RT = Ty->getAs<RecordType>()) {
5493 // Structures with flexible arrays have variable length, so really
5494 // fail the size test above.
5495 const RecordDecl *RD = RT->getDecl();
5496 if (RD->hasFlexibleArrayMember())
5497 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5499 // The structure is passed as an unextended integer, a float, or a double.
5501 if (isFPArgumentType(SingleElementTy)) {
5502 assert(Size == 32 || Size == 64);
5504 PassTy = llvm::Type::getFloatTy(getVMContext());
5506 PassTy = llvm::Type::getDoubleTy(getVMContext());
5508 PassTy = llvm::IntegerType::get(getVMContext(), Size);
5509 return ABIArgInfo::getDirect(PassTy);
5512 // Non-structure compounds are passed indirectly.
5513 if (isCompoundType(Ty))
5514 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5516 return ABIArgInfo::getDirect(nullptr);
5519 //===----------------------------------------------------------------------===//
5520 // MSP430 ABI Implementation
5521 //===----------------------------------------------------------------------===//
5525 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
5527 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
5528 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
5529 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5530 CodeGen::CodeGenModule &M) const override;
5535 void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
5536 llvm::GlobalValue *GV,
5537 CodeGen::CodeGenModule &M) const {
5538 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5539 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
5540 // Handle 'interrupt' attribute:
5541 llvm::Function *F = cast<llvm::Function>(GV);
5543 // Step 1: Set ISR calling convention.
5544 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
5546 // Step 2: Add attributes goodness.
5547 F->addFnAttr(llvm::Attribute::NoInline);
5549 // Step 3: Emit ISR vector alias.
5550 unsigned Num = attr->getNumber() / 2;
5551 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
5552 "__isr_" + Twine(Num), F);
5557 //===----------------------------------------------------------------------===//
5558 // MIPS ABI Implementation. This works for both little-endian and
5559 // big-endian variants.
5560 //===----------------------------------------------------------------------===//
5563 class MipsABIInfo : public ABIInfo {
5565 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
5566 void CoerceToIntArgs(uint64_t TySize,
5567 SmallVectorImpl<llvm::Type *> &ArgList) const;
5568 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
5569 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
5570 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
5572 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
5573 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
5574 StackAlignInBytes(IsO32 ? 8 : 16) {}
5576 ABIArgInfo classifyReturnType(QualType RetTy) const;
5577 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
5578 void computeInfo(CGFunctionInfo &FI) const override;
5579 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5580 CodeGenFunction &CGF) const override;
5581 bool shouldSignExtUnsignedType(QualType Ty) const override;
5584 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
5585 unsigned SizeOfUnwindException;
5587 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
5588 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
5589 SizeOfUnwindException(IsO32 ? 24 : 32) {}
5591 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
5595 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5596 CodeGen::CodeGenModule &CGM) const override {
5597 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5599 llvm::Function *Fn = cast<llvm::Function>(GV);
5600 if (FD->hasAttr<Mips16Attr>()) {
5601 Fn->addFnAttr("mips16");
5603 else if (FD->hasAttr<NoMips16Attr>()) {
5604 Fn->addFnAttr("nomips16");
5608 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5609 llvm::Value *Address) const override;
5611 unsigned getSizeOfUnwindException() const override {
5612 return SizeOfUnwindException;
5617 void MipsABIInfo::CoerceToIntArgs(
5618 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
5619 llvm::IntegerType *IntTy =
5620 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
5622 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
5623 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
5624 ArgList.push_back(IntTy);
5626 // If necessary, add one more integer type to ArgList.
5627 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
5630 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
5633 // In N32/64, an aligned double precision floating point field is passed in
5635 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
5636 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
5639 CoerceToIntArgs(TySize, ArgList);
5640 return llvm::StructType::get(getVMContext(), ArgList);
5643 if (Ty->isComplexType())
5644 return CGT.ConvertType(Ty);
5646 const RecordType *RT = Ty->getAs<RecordType>();
5648 // Unions/vectors are passed in integer registers.
5649 if (!RT || !RT->isStructureOrClassType()) {
5650 CoerceToIntArgs(TySize, ArgList);
5651 return llvm::StructType::get(getVMContext(), ArgList);
5654 const RecordDecl *RD = RT->getDecl();
5655 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
5656 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
5658 uint64_t LastOffset = 0;
5660 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
5662 // Iterate over fields in the struct/class and check if there are any aligned
5664 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5665 i != e; ++i, ++idx) {
5666 const QualType Ty = i->getType();
5667 const BuiltinType *BT = Ty->getAs<BuiltinType>();
5669 if (!BT || BT->getKind() != BuiltinType::Double)
5672 uint64_t Offset = Layout.getFieldOffset(idx);
5673 if (Offset % 64) // Ignore doubles that are not aligned.
5676 // Add ((Offset - LastOffset) / 64) args of type i64.
5677 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
5678 ArgList.push_back(I64);
5681 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
5682 LastOffset = Offset + 64;
5685 CoerceToIntArgs(TySize - LastOffset, IntArgList);
5686 ArgList.append(IntArgList.begin(), IntArgList.end());
5688 return llvm::StructType::get(getVMContext(), ArgList);
5691 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
5692 uint64_t Offset) const {
5693 if (OrigOffset + MinABIStackAlignInBytes > Offset)
5696 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
5700 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
5701 Ty = useFirstFieldIfTransparentUnion(Ty);
5703 uint64_t OrigOffset = Offset;
5704 uint64_t TySize = getContext().getTypeSize(Ty);
5705 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
5707 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
5708 (uint64_t)StackAlignInBytes);
5709 unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align);
5710 Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
5712 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
5713 // Ignore empty aggregates.
5715 return ABIArgInfo::getIgnore();
5717 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5718 Offset = OrigOffset + MinABIStackAlignInBytes;
5719 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
5722 // If we have reached here, aggregates are passed directly by coercing to
5723 // another structure type. Padding is inserted if the offset of the
5724 // aggregate is unaligned.
5725 ABIArgInfo ArgInfo =
5726 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
5727 getPaddingType(OrigOffset, CurrOffset));
5728 ArgInfo.setInReg(true);
5732 // Treat an enum type as its underlying type.
5733 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5734 Ty = EnumTy->getDecl()->getIntegerType();
5736 // All integral types are promoted to the GPR width.
5737 if (Ty->isIntegralOrEnumerationType())
5738 return ABIArgInfo::getExtend();
5740 return ABIArgInfo::getDirect(
5741 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
5745 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
5746 const RecordType *RT = RetTy->getAs<RecordType>();
5747 SmallVector<llvm::Type*, 8> RTList;
5749 if (RT && RT->isStructureOrClassType()) {
5750 const RecordDecl *RD = RT->getDecl();
5751 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
5752 unsigned FieldCnt = Layout.getFieldCount();
5754 // N32/64 returns struct/classes in floating point registers if the
5755 // following conditions are met:
5756 // 1. The size of the struct/class is no larger than 128-bit.
5757 // 2. The struct/class has one or two fields all of which are floating
5759 // 3. The offset of the first field is zero (this follows what gcc does).
5761 // Any other composite results are returned in integer registers.
5763 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
5764 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
5765 for (; b != e; ++b) {
5766 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
5768 if (!BT || !BT->isFloatingPoint())
5771 RTList.push_back(CGT.ConvertType(b->getType()));
5775 return llvm::StructType::get(getVMContext(), RTList,
5776 RD->hasAttr<PackedAttr>());
5782 CoerceToIntArgs(Size, RTList);
5783 return llvm::StructType::get(getVMContext(), RTList);
5786 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
5787 uint64_t Size = getContext().getTypeSize(RetTy);
5789 if (RetTy->isVoidType())
5790 return ABIArgInfo::getIgnore();
5792 // O32 doesn't treat zero-sized structs differently from other structs.
5793 // However, N32/N64 ignores zero sized return values.
5794 if (!IsO32 && Size == 0)
5795 return ABIArgInfo::getIgnore();
5797 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
5799 if (RetTy->isAnyComplexType())
5800 return ABIArgInfo::getDirect();
5802 // O32 returns integer vectors in registers and N32/N64 returns all small
5803 // aggregates in registers.
5805 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
5806 ABIArgInfo ArgInfo =
5807 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
5808 ArgInfo.setInReg(true);
5813 return ABIArgInfo::getIndirect(0);
5816 // Treat an enum type as its underlying type.
5817 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5818 RetTy = EnumTy->getDecl()->getIntegerType();
5820 return (RetTy->isPromotableIntegerType() ?
5821 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5824 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
5825 ABIArgInfo &RetInfo = FI.getReturnInfo();
5826 if (!getCXXABI().classifyReturnType(FI))
5827 RetInfo = classifyReturnType(FI.getReturnType());
5829 // Check if a pointer to an aggregate is passed as a hidden argument.
5830 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
5832 for (auto &I : FI.arguments())
5833 I.info = classifyArgumentType(I.type, Offset);
5836 llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5837 CodeGenFunction &CGF) const {
5838 llvm::Type *BP = CGF.Int8PtrTy;
5839 llvm::Type *BPP = CGF.Int8PtrPtrTy;
5841 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
5842 // Pointers are also promoted in the same way but this only matters for N32.
5843 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
5844 unsigned PtrWidth = getTarget().getPointerWidth(0);
5845 if ((Ty->isIntegerType() &&
5846 CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) ||
5847 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
5848 Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits,
5849 Ty->isSignedIntegerType());
5852 CGBuilderTy &Builder = CGF.Builder;
5853 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5854 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5856 std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes);
5857 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5858 llvm::Value *AddrTyped;
5859 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
5861 if (TypeAlign > MinABIStackAlignInBytes) {
5862 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
5863 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
5864 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
5865 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
5866 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
5867 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
5870 AddrTyped = Builder.CreateBitCast(Addr, PTy);
5872 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
5873 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
5874 unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty);
5875 uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign);
5876 llvm::Value *NextAddr =
5877 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
5879 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5884 bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
5885 int TySize = getContext().getTypeSize(Ty);
5887 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
5888 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
5895 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5896 llvm::Value *Address) const {
5897 // This information comes from gcc's implementation, which seems to
5898 // as canonical as it gets.
5900 // Everything on MIPS is 4 bytes. Double-precision FP registers
5901 // are aliased to pairs of single-precision FP registers.
5902 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
5904 // 0-31 are the general purpose registers, $0 - $31.
5905 // 32-63 are the floating-point registers, $f0 - $f31.
5906 // 64 and 65 are the multiply/divide registers, $hi and $lo.
5907 // 66 is the (notional, I think) register for signal-handler return.
5908 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
5910 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
5911 // They are one bit wide and ignored here.
5913 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
5914 // (coprocessor 1 is the FP unit)
5915 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
5916 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
5917 // 176-181 are the DSP accumulator registers.
5918 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
5922 //===----------------------------------------------------------------------===//
5923 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
5924 // Currently subclassed only to implement custom OpenCL C function attribute
5926 //===----------------------------------------------------------------------===//
5930 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5932 TCETargetCodeGenInfo(CodeGenTypes &CGT)
5933 : DefaultTargetCodeGenInfo(CGT) {}
5935 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5936 CodeGen::CodeGenModule &M) const override;
5939 void TCETargetCodeGenInfo::setTargetAttributes(
5940 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
5941 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
5944 llvm::Function *F = cast<llvm::Function>(GV);
5946 if (M.getLangOpts().OpenCL) {
5947 if (FD->hasAttr<OpenCLKernelAttr>()) {
5948 // OpenCL C Kernel functions are not subject to inlining
5949 F->addFnAttr(llvm::Attribute::NoInline);
5950 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
5952 // Convert the reqd_work_group_size() attributes to metadata.
5953 llvm::LLVMContext &Context = F->getContext();
5954 llvm::NamedMDNode *OpenCLMetadata =
5955 M.getModule().getOrInsertNamedMetadata(
5956 "opencl.kernel_wg_size_info");
5958 SmallVector<llvm::Metadata *, 5> Operands;
5959 Operands.push_back(llvm::ConstantAsMetadata::get(F));
5962 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5963 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
5965 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5966 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
5968 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
5969 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
5971 // Add a boolean constant operand for "required" (true) or "hint"
5972 // (false) for implementing the work_group_size_hint attr later.
5973 // Currently always true as the hint is not yet implemented.
5975 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
5976 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
5984 //===----------------------------------------------------------------------===//
5985 // Hexagon ABI Implementation
5986 //===----------------------------------------------------------------------===//
5990 class HexagonABIInfo : public ABIInfo {
5994 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5998 ABIArgInfo classifyReturnType(QualType RetTy) const;
5999 ABIArgInfo classifyArgumentType(QualType RetTy) const;
6001 void computeInfo(CGFunctionInfo &FI) const override;
6003 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6004 CodeGenFunction &CGF) const override;
6007 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
6009 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
6010 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
6012 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6019 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
6020 if (!getCXXABI().classifyReturnType(FI))
6021 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6022 for (auto &I : FI.arguments())
6023 I.info = classifyArgumentType(I.type);
6026 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6027 if (!isAggregateTypeForABI(Ty)) {
6028 // Treat an enum type as its underlying type.
6029 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6030 Ty = EnumTy->getDecl()->getIntegerType();
6032 return (Ty->isPromotableIntegerType() ?
6033 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6036 // Ignore empty records.
6037 if (isEmptyRecord(getContext(), Ty, true))
6038 return ABIArgInfo::getIgnore();
6040 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6041 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
6043 uint64_t Size = getContext().getTypeSize(Ty);
6045 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
6046 // Pass in the smallest viable integer type.
6048 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6050 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6052 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6054 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6057 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6058 if (RetTy->isVoidType())
6059 return ABIArgInfo::getIgnore();
6061 // Large vector types should be returned via memory.
6062 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
6063 return ABIArgInfo::getIndirect(0);
6065 if (!isAggregateTypeForABI(RetTy)) {
6066 // Treat an enum type as its underlying type.
6067 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6068 RetTy = EnumTy->getDecl()->getIntegerType();
6070 return (RetTy->isPromotableIntegerType() ?
6071 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6074 if (isEmptyRecord(getContext(), RetTy, true))
6075 return ABIArgInfo::getIgnore();
6077 // Aggregates <= 8 bytes are returned in r0; other aggregates
6078 // are returned indirectly.
6079 uint64_t Size = getContext().getTypeSize(RetTy);
6081 // Return in the smallest viable integer type.
6083 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6085 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6087 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6088 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6091 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
6094 llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6095 CodeGenFunction &CGF) const {
6096 // FIXME: Need to handle alignment
6097 llvm::Type *BPP = CGF.Int8PtrPtrTy;
6099 CGBuilderTy &Builder = CGF.Builder;
6100 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
6102 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
6104 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
6105 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
6108 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
6109 llvm::Value *NextAddr =
6110 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
6112 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
6117 //===----------------------------------------------------------------------===//
6118 // AMDGPU ABI Implementation
6119 //===----------------------------------------------------------------------===//
6123 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6125 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6126 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
6127 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6128 CodeGen::CodeGenModule &M) const override;
6133 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
6135 llvm::GlobalValue *GV,
6136 CodeGen::CodeGenModule &M) const {
6137 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
6141 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6142 llvm::Function *F = cast<llvm::Function>(GV);
6143 uint32_t NumVGPR = Attr->getNumVGPR();
6145 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6148 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6149 llvm::Function *F = cast<llvm::Function>(GV);
6150 unsigned NumSGPR = Attr->getNumSGPR();
6152 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6157 //===----------------------------------------------------------------------===//
6158 // SPARC v9 ABI Implementation.
6159 // Based on the SPARC Compliance Definition version 2.4.1.
6161 // Function arguments a mapped to a nominal "parameter array" and promoted to
6162 // registers depending on their type. Each argument occupies 8 or 16 bytes in
6163 // the array, structs larger than 16 bytes are passed indirectly.
6165 // One case requires special care:
6172 // When a struct mixed is passed by value, it only occupies 8 bytes in the
6173 // parameter array, but the int is passed in an integer register, and the float
6174 // is passed in a floating point register. This is represented as two arguments
6175 // with the LLVM IR inreg attribute:
6177 // declare void f(i32 inreg %i, float inreg %f)
6179 // The code generator will only allocate 4 bytes from the parameter array for
6180 // the inreg arguments. All other arguments are allocated a multiple of 8
6184 class SparcV9ABIInfo : public ABIInfo {
6186 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6189 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
6190 void computeInfo(CGFunctionInfo &FI) const override;
6191 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6192 CodeGenFunction &CGF) const override;
6194 // Coercion type builder for structs passed in registers. The coercion type
6195 // serves two purposes:
6197 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
6199 // 2. Expose aligned floating point elements as first-level elements, so the
6200 // code generator knows to pass them in floating point registers.
6202 // We also compute the InReg flag which indicates that the struct contains
6203 // aligned 32-bit floats.
6205 struct CoerceBuilder {
6206 llvm::LLVMContext &Context;
6207 const llvm::DataLayout &DL;
6208 SmallVector<llvm::Type*, 8> Elems;
6212 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
6213 : Context(c), DL(dl), Size(0), InReg(false) {}
6215 // Pad Elems with integers until Size is ToSize.
6216 void pad(uint64_t ToSize) {
6217 assert(ToSize >= Size && "Cannot remove elements");
6221 // Finish the current 64-bit word.
6222 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
6223 if (Aligned > Size && Aligned <= ToSize) {
6224 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
6228 // Add whole 64-bit words.
6229 while (Size + 64 <= ToSize) {
6230 Elems.push_back(llvm::Type::getInt64Ty(Context));
6234 // Final in-word padding.
6235 if (Size < ToSize) {
6236 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
6241 // Add a floating point element at Offset.
6242 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
6243 // Unaligned floats are treated as integers.
6246 // The InReg flag is only required if there are any floats < 64 bits.
6250 Elems.push_back(Ty);
6251 Size = Offset + Bits;
6254 // Add a struct type to the coercion type, starting at Offset (in bits).
6255 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
6256 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
6257 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
6258 llvm::Type *ElemTy = StrTy->getElementType(i);
6259 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
6260 switch (ElemTy->getTypeID()) {
6261 case llvm::Type::StructTyID:
6262 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
6264 case llvm::Type::FloatTyID:
6265 addFloat(ElemOffset, ElemTy, 32);
6267 case llvm::Type::DoubleTyID:
6268 addFloat(ElemOffset, ElemTy, 64);
6270 case llvm::Type::FP128TyID:
6271 addFloat(ElemOffset, ElemTy, 128);
6273 case llvm::Type::PointerTyID:
6274 if (ElemOffset % 64 == 0) {
6276 Elems.push_back(ElemTy);
6286 // Check if Ty is a usable substitute for the coercion type.
6287 bool isUsableType(llvm::StructType *Ty) const {
6288 return llvm::makeArrayRef(Elems) == Ty->elements();
6291 // Get the coercion type as a literal struct type.
6292 llvm::Type *getType() const {
6293 if (Elems.size() == 1)
6294 return Elems.front();
6296 return llvm::StructType::get(Context, Elems);
6300 } // end anonymous namespace
6303 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
6304 if (Ty->isVoidType())
6305 return ABIArgInfo::getIgnore();
6307 uint64_t Size = getContext().getTypeSize(Ty);
6309 // Anything too big to fit in registers is passed with an explicit indirect
6310 // pointer / sret pointer.
6311 if (Size > SizeLimit)
6312 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
6314 // Treat an enum type as its underlying type.
6315 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6316 Ty = EnumTy->getDecl()->getIntegerType();
6318 // Integer types smaller than a register are extended.
6319 if (Size < 64 && Ty->isIntegerType())
6320 return ABIArgInfo::getExtend();
6322 // Other non-aggregates go in registers.
6323 if (!isAggregateTypeForABI(Ty))
6324 return ABIArgInfo::getDirect();
6326 // If a C++ object has either a non-trivial copy constructor or a non-trivial
6327 // destructor, it is passed with an explicit indirect pointer / sret pointer.
6328 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6329 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
6331 // This is a small aggregate type that should be passed in registers.
6332 // Build a coercion type from the LLVM struct type.
6333 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
6335 return ABIArgInfo::getDirect();
6337 CoerceBuilder CB(getVMContext(), getDataLayout());
6338 CB.addStruct(0, StrTy);
6339 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
6341 // Try to use the original type for coercion.
6342 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
6345 return ABIArgInfo::getDirectInReg(CoerceTy);
6347 return ABIArgInfo::getDirect(CoerceTy);
6350 llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6351 CodeGenFunction &CGF) const {
6352 ABIArgInfo AI = classifyType(Ty, 16 * 8);
6353 llvm::Type *ArgTy = CGT.ConvertType(Ty);
6354 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6355 AI.setCoerceToType(ArgTy);
6357 llvm::Type *BPP = CGF.Int8PtrPtrTy;
6358 CGBuilderTy &Builder = CGF.Builder;
6359 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
6360 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
6361 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6362 llvm::Value *ArgAddr;
6365 switch (AI.getKind()) {
6366 case ABIArgInfo::Expand:
6367 case ABIArgInfo::InAlloca:
6368 llvm_unreachable("Unsupported ABI kind for va_arg");
6370 case ABIArgInfo::Extend:
6373 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
6377 case ABIArgInfo::Direct:
6378 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
6382 case ABIArgInfo::Indirect:
6384 ArgAddr = Builder.CreateBitCast(Addr,
6385 llvm::PointerType::getUnqual(ArgPtrTy),
6387 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
6390 case ABIArgInfo::Ignore:
6391 return llvm::UndefValue::get(ArgPtrTy);
6395 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
6396 Builder.CreateStore(Addr, VAListAddrAsBPP);
6398 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
6401 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6402 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
6403 for (auto &I : FI.arguments())
6404 I.info = classifyType(I.type, 16 * 8);
6408 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
6410 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
6411 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
6413 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6417 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6418 llvm::Value *Address) const override;
6420 } // end anonymous namespace
6423 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6424 llvm::Value *Address) const {
6425 // This is calculated from the LLVM and GCC tables and verified
6426 // against gcc output. AFAIK all ABIs use the same encoding.
6428 CodeGen::CGBuilderTy &Builder = CGF.Builder;
6430 llvm::IntegerType *i8 = CGF.Int8Ty;
6431 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
6432 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
6434 // 0-31: the 8-byte general-purpose registers
6435 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
6437 // 32-63: f0-31, the 4-byte floating-point registers
6438 AssignToArrayRange(Builder, Address, Four8, 32, 63);
6448 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
6450 // 72-87: d0-15, the 8-byte floating-point registers
6451 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
6457 //===----------------------------------------------------------------------===//
6458 // XCore ABI Implementation
6459 //===----------------------------------------------------------------------===//
6463 /// A SmallStringEnc instance is used to build up the TypeString by passing
6464 /// it by reference between functions that append to it.
6465 typedef llvm::SmallString<128> SmallStringEnc;
6467 /// TypeStringCache caches the meta encodings of Types.
6469 /// The reason for caching TypeStrings is two fold:
6470 /// 1. To cache a type's encoding for later uses;
6471 /// 2. As a means to break recursive member type inclusion.
6473 /// A cache Entry can have a Status of:
6474 /// NonRecursive: The type encoding is not recursive;
6475 /// Recursive: The type encoding is recursive;
6476 /// Incomplete: An incomplete TypeString;
6477 /// IncompleteUsed: An incomplete TypeString that has been used in a
6478 /// Recursive type encoding.
6480 /// A NonRecursive entry will have all of its sub-members expanded as fully
6481 /// as possible. Whilst it may contain types which are recursive, the type
6482 /// itself is not recursive and thus its encoding may be safely used whenever
6483 /// the type is encountered.
6485 /// A Recursive entry will have all of its sub-members expanded as fully as
6486 /// possible. The type itself is recursive and it may contain other types which
6487 /// are recursive. The Recursive encoding must not be used during the expansion
6488 /// of a recursive type's recursive branch. For simplicity the code uses
6489 /// IncompleteCount to reject all usage of Recursive encodings for member types.
6491 /// An Incomplete entry is always a RecordType and only encodes its
6492 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
6493 /// are placed into the cache during type expansion as a means to identify and
6494 /// handle recursive inclusion of types as sub-members. If there is recursion
6495 /// the entry becomes IncompleteUsed.
6497 /// During the expansion of a RecordType's members:
6499 /// If the cache contains a NonRecursive encoding for the member type, the
6500 /// cached encoding is used;
6502 /// If the cache contains a Recursive encoding for the member type, the
6503 /// cached encoding is 'Swapped' out, as it may be incorrect, and...
6505 /// If the member is a RecordType, an Incomplete encoding is placed into the
6506 /// cache to break potential recursive inclusion of itself as a sub-member;
6508 /// Once a member RecordType has been expanded, its temporary incomplete
6509 /// entry is removed from the cache. If a Recursive encoding was swapped out
6510 /// it is swapped back in;
6512 /// If an incomplete entry is used to expand a sub-member, the incomplete
6513 /// entry is marked as IncompleteUsed. The cache keeps count of how many
6514 /// IncompleteUsed entries it currently contains in IncompleteUsedCount;
6516 /// If a member's encoding is found to be a NonRecursive or Recursive viz:
6517 /// IncompleteUsedCount==0, the member's encoding is added to the cache.
6518 /// Else the member is part of a recursive type and thus the recursion has
6519 /// been exited too soon for the encoding to be correct for the member.
6521 class TypeStringCache {
6522 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
6524 std::string Str; // The encoded TypeString for the type.
6525 enum Status State; // Information about the encoding in 'Str'.
6526 std::string Swapped; // A temporary place holder for a Recursive encoding
6527 // during the expansion of RecordType's members.
6529 std::map<const IdentifierInfo *, struct Entry> Map;
6530 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
6531 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
6533 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {};
6534 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
6535 bool removeIncomplete(const IdentifierInfo *ID);
6536 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
6538 StringRef lookupStr(const IdentifierInfo *ID);
6541 /// TypeString encodings for enum & union fields must be order.
6542 /// FieldEncoding is a helper for this ordering process.
6543 class FieldEncoding {
6547 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {};
6548 StringRef str() {return Enc.c_str();};
6549 bool operator<(const FieldEncoding &rhs) const {
6550 if (HasName != rhs.HasName) return HasName;
6551 return Enc < rhs.Enc;
6555 class XCoreABIInfo : public DefaultABIInfo {
6557 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6558 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6559 CodeGenFunction &CGF) const override;
6562 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
6563 mutable TypeStringCache TSC;
6565 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
6566 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
6567 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
6568 CodeGen::CodeGenModule &M) const override;
6571 } // End anonymous namespace.
6573 llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
6574 CodeGenFunction &CGF) const {
6575 CGBuilderTy &Builder = CGF.Builder;
6578 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr,
6580 llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP);
6582 // Handle the argument.
6583 ABIArgInfo AI = classifyArgumentType(Ty);
6584 llvm::Type *ArgTy = CGT.ConvertType(Ty);
6585 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6586 AI.setCoerceToType(ArgTy);
6587 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6589 uint64_t ArgSize = 0;
6590 switch (AI.getKind()) {
6591 case ABIArgInfo::Expand:
6592 case ABIArgInfo::InAlloca:
6593 llvm_unreachable("Unsupported ABI kind for va_arg");
6594 case ABIArgInfo::Ignore:
6595 Val = llvm::UndefValue::get(ArgPtrTy);
6598 case ABIArgInfo::Extend:
6599 case ABIArgInfo::Direct:
6600 Val = Builder.CreatePointerCast(AP, ArgPtrTy);
6601 ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
6605 case ABIArgInfo::Indirect:
6606 llvm::Value *ArgAddr;
6607 ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy));
6608 ArgAddr = Builder.CreateLoad(ArgAddr);
6609 Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy);
6614 // Increment the VAList.
6616 llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize);
6617 Builder.CreateStore(APN, VAListAddrAsBPP);
6622 /// During the expansion of a RecordType, an incomplete TypeString is placed
6623 /// into the cache as a means to identify and break recursion.
6624 /// If there is a Recursive encoding in the cache, it is swapped out and will
6625 /// be reinserted by removeIncomplete().
6626 /// All other types of encoding should have been used rather than arriving here.
6627 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
6628 std::string StubEnc) {
6632 assert( (E.Str.empty() || E.State == Recursive) &&
6633 "Incorrectly use of addIncomplete");
6634 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
6635 E.Swapped.swap(E.Str); // swap out the Recursive
6636 E.Str.swap(StubEnc);
6637 E.State = Incomplete;
6641 /// Once the RecordType has been expanded, the temporary incomplete TypeString
6642 /// must be removed from the cache.
6643 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
6644 /// Returns true if the RecordType was defined recursively.
6645 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
6648 auto I = Map.find(ID);
6649 assert(I != Map.end() && "Entry not present");
6650 Entry &E = I->second;
6651 assert( (E.State == Incomplete ||
6652 E.State == IncompleteUsed) &&
6653 "Entry must be an incomplete type");
6654 bool IsRecursive = false;
6655 if (E.State == IncompleteUsed) {
6656 // We made use of our Incomplete encoding, thus we are recursive.
6658 --IncompleteUsedCount;
6660 if (E.Swapped.empty())
6663 // Swap the Recursive back.
6664 E.Swapped.swap(E.Str);
6666 E.State = Recursive;
6672 /// Add the encoded TypeString to the cache only if it is NonRecursive or
6673 /// Recursive (viz: all sub-members were expanded as fully as possible).
6674 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
6676 if (!ID || IncompleteUsedCount)
6677 return; // No key or it is is an incomplete sub-type so don't add.
6679 if (IsRecursive && !E.Str.empty()) {
6680 assert(E.State==Recursive && E.Str.size() == Str.size() &&
6681 "This is not the same Recursive entry");
6682 // The parent container was not recursive after all, so we could have used
6683 // this Recursive sub-member entry after all, but we assumed the worse when
6684 // we started viz: IncompleteCount!=0.
6687 assert(E.Str.empty() && "Entry already present");
6689 E.State = IsRecursive? Recursive : NonRecursive;
6692 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
6693 /// are recursively expanding a type (IncompleteCount != 0) and the cached
6694 /// encoding is Recursive, return an empty StringRef.
6695 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
6697 return StringRef(); // We have no key.
6698 auto I = Map.find(ID);
6700 return StringRef(); // We have no encoding.
6701 Entry &E = I->second;
6702 if (E.State == Recursive && IncompleteCount)
6703 return StringRef(); // We don't use Recursive encodings for member types.
6705 if (E.State == Incomplete) {
6706 // The incomplete type is being used to break out of recursion.
6707 E.State = IncompleteUsed;
6708 ++IncompleteUsedCount;
6710 return E.Str.c_str();
6713 /// The XCore ABI includes a type information section that communicates symbol
6714 /// type information to the linker. The linker uses this information to verify
6715 /// safety/correctness of things such as array bound and pointers et al.
6716 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
6717 /// This type information (TypeString) is emitted into meta data for all global
6718 /// symbols: definitions, declarations, functions & variables.
6720 /// The TypeString carries type, qualifier, name, size & value details.
6721 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
6722 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
6723 /// The output is tested by test/CodeGen/xcore-stringtype.c.
6725 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
6726 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
6728 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
6729 void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
6730 CodeGen::CodeGenModule &CGM) const {
6732 if (getTypeString(Enc, D, CGM, TSC)) {
6733 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
6734 llvm::SmallVector<llvm::Metadata *, 2> MDVals;
6735 MDVals.push_back(llvm::ConstantAsMetadata::get(GV));
6736 MDVals.push_back(llvm::MDString::get(Ctx, Enc.str()));
6737 llvm::NamedMDNode *MD =
6738 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
6739 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6743 static bool appendType(SmallStringEnc &Enc, QualType QType,
6744 const CodeGen::CodeGenModule &CGM,
6745 TypeStringCache &TSC);
6747 /// Helper function for appendRecordType().
6748 /// Builds a SmallVector containing the encoded field types in declaration
6750 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
6751 const RecordDecl *RD,
6752 const CodeGen::CodeGenModule &CGM,
6753 TypeStringCache &TSC) {
6754 for (const auto *Field : RD->fields()) {
6757 Enc += Field->getName();
6759 if (Field->isBitField()) {
6761 llvm::raw_svector_ostream OS(Enc);
6763 OS << Field->getBitWidthValue(CGM.getContext());
6767 if (!appendType(Enc, Field->getType(), CGM, TSC))
6769 if (Field->isBitField())
6772 FE.emplace_back(!Field->getName().empty(), Enc);
6777 /// Appends structure and union types to Enc and adds encoding to cache.
6778 /// Recursively calls appendType (via extractFieldType) for each field.
6779 /// Union types have their fields ordered according to the ABI.
6780 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
6781 const CodeGen::CodeGenModule &CGM,
6782 TypeStringCache &TSC, const IdentifierInfo *ID) {
6783 // Append the cached TypeString if we have one.
6784 StringRef TypeString = TSC.lookupStr(ID);
6785 if (!TypeString.empty()) {
6790 // Start to emit an incomplete TypeString.
6791 size_t Start = Enc.size();
6792 Enc += (RT->isUnionType()? 'u' : 's');
6795 Enc += ID->getName();
6798 // We collect all encoded fields and order as necessary.
6799 bool IsRecursive = false;
6800 const RecordDecl *RD = RT->getDecl()->getDefinition();
6801 if (RD && !RD->field_empty()) {
6802 // An incomplete TypeString stub is placed in the cache for this RecordType
6803 // so that recursive calls to this RecordType will use it whilst building a
6804 // complete TypeString for this RecordType.
6805 SmallVector<FieldEncoding, 16> FE;
6806 std::string StubEnc(Enc.substr(Start).str());
6807 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
6808 TSC.addIncomplete(ID, std::move(StubEnc));
6809 if (!extractFieldType(FE, RD, CGM, TSC)) {
6810 (void) TSC.removeIncomplete(ID);
6813 IsRecursive = TSC.removeIncomplete(ID);
6814 // The ABI requires unions to be sorted but not structures.
6815 // See FieldEncoding::operator< for sort algorithm.
6816 if (RT->isUnionType())
6817 std::sort(FE.begin(), FE.end());
6818 // We can now complete the TypeString.
6819 unsigned E = FE.size();
6820 for (unsigned I = 0; I != E; ++I) {
6827 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
6831 /// Appends enum types to Enc and adds the encoding to the cache.
6832 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
6833 TypeStringCache &TSC,
6834 const IdentifierInfo *ID) {
6835 // Append the cached TypeString if we have one.
6836 StringRef TypeString = TSC.lookupStr(ID);
6837 if (!TypeString.empty()) {
6842 size_t Start = Enc.size();
6845 Enc += ID->getName();
6848 // We collect all encoded enumerations and order them alphanumerically.
6849 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
6850 SmallVector<FieldEncoding, 16> FE;
6851 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
6853 SmallStringEnc EnumEnc;
6855 EnumEnc += I->getName();
6857 I->getInitVal().toString(EnumEnc);
6859 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
6861 std::sort(FE.begin(), FE.end());
6862 unsigned E = FE.size();
6863 for (unsigned I = 0; I != E; ++I) {
6870 TSC.addIfComplete(ID, Enc.substr(Start), false);
6874 /// Appends type's qualifier to Enc.
6875 /// This is done prior to appending the type's encoding.
6876 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
6877 // Qualifiers are emitted in alphabetical order.
6878 static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"};
6880 if (QT.isConstQualified())
6882 if (QT.isRestrictQualified())
6884 if (QT.isVolatileQualified())
6886 Enc += Table[Lookup];
6889 /// Appends built-in types to Enc.
6890 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
6891 const char *EncType;
6892 switch (BT->getKind()) {
6893 case BuiltinType::Void:
6896 case BuiltinType::Bool:
6899 case BuiltinType::Char_U:
6902 case BuiltinType::UChar:
6905 case BuiltinType::SChar:
6908 case BuiltinType::UShort:
6911 case BuiltinType::Short:
6914 case BuiltinType::UInt:
6917 case BuiltinType::Int:
6920 case BuiltinType::ULong:
6923 case BuiltinType::Long:
6926 case BuiltinType::ULongLong:
6929 case BuiltinType::LongLong:
6932 case BuiltinType::Float:
6935 case BuiltinType::Double:
6938 case BuiltinType::LongDouble:
6948 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
6949 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
6950 const CodeGen::CodeGenModule &CGM,
6951 TypeStringCache &TSC) {
6953 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
6959 /// Appends array encoding to Enc before calling appendType for the element.
6960 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
6961 const ArrayType *AT,
6962 const CodeGen::CodeGenModule &CGM,
6963 TypeStringCache &TSC, StringRef NoSizeEnc) {
6964 if (AT->getSizeModifier() != ArrayType::Normal)
6967 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
6968 CAT->getSize().toStringUnsigned(Enc);
6970 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
6972 // The Qualifiers should be attached to the type rather than the array.
6973 appendQualifier(Enc, QT);
6974 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
6980 /// Appends a function encoding to Enc, calling appendType for the return type
6981 /// and the arguments.
6982 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
6983 const CodeGen::CodeGenModule &CGM,
6984 TypeStringCache &TSC) {
6986 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
6989 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
6990 // N.B. we are only interested in the adjusted param types.
6991 auto I = FPT->param_type_begin();
6992 auto E = FPT->param_type_end();
6995 if (!appendType(Enc, *I, CGM, TSC))
7001 if (FPT->isVariadic())
7004 if (FPT->isVariadic())
7014 /// Handles the type's qualifier before dispatching a call to handle specific
7016 static bool appendType(SmallStringEnc &Enc, QualType QType,
7017 const CodeGen::CodeGenModule &CGM,
7018 TypeStringCache &TSC) {
7020 QualType QT = QType.getCanonicalType();
7022 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7023 // The Qualifiers should be attached to the type rather than the array.
7024 // Thus we don't call appendQualifier() here.
7025 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7027 appendQualifier(Enc, QT);
7029 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7030 return appendBuiltinType(Enc, BT);
7032 if (const PointerType *PT = QT->getAs<PointerType>())
7033 return appendPointerType(Enc, PT, CGM, TSC);
7035 if (const EnumType *ET = QT->getAs<EnumType>())
7036 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7038 if (const RecordType *RT = QT->getAsStructureType())
7039 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7041 if (const RecordType *RT = QT->getAsUnionType())
7042 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7044 if (const FunctionType *FT = QT->getAs<FunctionType>())
7045 return appendFunctionType(Enc, FT, CGM, TSC);
7050 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7051 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7055 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7056 if (FD->getLanguageLinkage() != CLanguageLinkage)
7058 return appendType(Enc, FD->getType(), CGM, TSC);
7061 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7062 if (VD->getLanguageLinkage() != CLanguageLinkage)
7064 QualType QT = VD->getType().getCanonicalType();
7065 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7066 // Global ArrayTypes are given a size of '*' if the size is unknown.
7067 // The Qualifiers should be attached to the type rather than the array.
7068 // Thus we don't call appendQualifier() here.
7069 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
7071 return appendType(Enc, QT, CGM, TSC);
7077 //===----------------------------------------------------------------------===//
7079 //===----------------------------------------------------------------------===//
7081 const llvm::Triple &CodeGenModule::getTriple() const {
7082 return getTarget().getTriple();
7085 bool CodeGenModule::supportsCOMDAT() const {
7086 return !getTriple().isOSBinFormatMachO();
7089 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
7090 if (TheTargetCodeGenInfo)
7091 return *TheTargetCodeGenInfo;
7093 const llvm::Triple &Triple = getTarget().getTriple();
7094 switch (Triple.getArch()) {
7096 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
7098 case llvm::Triple::le32:
7099 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
7100 case llvm::Triple::mips:
7101 case llvm::Triple::mipsel:
7102 if (Triple.getOS() == llvm::Triple::NaCl)
7103 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
7104 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
7106 case llvm::Triple::mips64:
7107 case llvm::Triple::mips64el:
7108 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
7110 case llvm::Triple::aarch64:
7111 case llvm::Triple::aarch64_be: {
7112 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
7113 if (getTarget().getABI() == "darwinpcs")
7114 Kind = AArch64ABIInfo::DarwinPCS;
7116 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind));
7119 case llvm::Triple::arm:
7120 case llvm::Triple::armeb:
7121 case llvm::Triple::thumb:
7122 case llvm::Triple::thumbeb:
7124 if (Triple.getOS() == llvm::Triple::Win32) {
7125 TheTargetCodeGenInfo =
7126 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP);
7127 return *TheTargetCodeGenInfo;
7130 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
7131 if (getTarget().getABI() == "apcs-gnu")
7132 Kind = ARMABIInfo::APCS;
7133 else if (CodeGenOpts.FloatABI == "hard" ||
7134 (CodeGenOpts.FloatABI != "soft" &&
7135 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
7136 Kind = ARMABIInfo::AAPCS_VFP;
7138 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
7141 case llvm::Triple::ppc:
7142 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
7143 case llvm::Triple::ppc64:
7144 if (Triple.isOSBinFormatELF()) {
7145 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
7146 if (getTarget().getABI() == "elfv2")
7147 Kind = PPC64_SVR4_ABIInfo::ELFv2;
7148 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
7150 return *(TheTargetCodeGenInfo =
7151 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
7153 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
7154 case llvm::Triple::ppc64le: {
7155 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
7156 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
7157 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
7158 Kind = PPC64_SVR4_ABIInfo::ELFv1;
7159 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
7161 return *(TheTargetCodeGenInfo =
7162 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
7165 case llvm::Triple::nvptx:
7166 case llvm::Triple::nvptx64:
7167 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
7169 case llvm::Triple::msp430:
7170 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
7172 case llvm::Triple::systemz: {
7173 bool HasVector = getTarget().getABI() == "vector";
7174 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types,
7178 case llvm::Triple::tce:
7179 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
7181 case llvm::Triple::x86: {
7182 bool IsDarwinVectorABI = Triple.isOSDarwin();
7183 bool IsSmallStructInRegABI =
7184 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
7185 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
7187 if (Triple.getOS() == llvm::Triple::Win32) {
7188 return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo(
7189 Types, IsDarwinVectorABI, IsSmallStructInRegABI,
7190 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
7192 return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo(
7193 Types, IsDarwinVectorABI, IsSmallStructInRegABI,
7194 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
7198 case llvm::Triple::x86_64: {
7199 StringRef ABI = getTarget().getABI();
7200 X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 :
7201 ABI == "avx" ? X86AVXABILevel::AVX :
7202 X86AVXABILevel::None);
7204 switch (Triple.getOS()) {
7205 case llvm::Triple::Win32:
7206 return *(TheTargetCodeGenInfo =
7207 new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
7208 case llvm::Triple::PS4:
7209 return *(TheTargetCodeGenInfo =
7210 new PS4TargetCodeGenInfo(Types, AVXLevel));
7212 return *(TheTargetCodeGenInfo =
7213 new X86_64TargetCodeGenInfo(Types, AVXLevel));
7216 case llvm::Triple::hexagon:
7217 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
7218 case llvm::Triple::r600:
7219 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
7220 case llvm::Triple::amdgcn:
7221 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
7222 case llvm::Triple::sparcv9:
7223 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
7224 case llvm::Triple::xcore:
7225 return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types));