1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TargetInfo.h"
15 #include "ABIInfo.h"
16 #include "CGBlocks.h"
17 #include "CGCXXABI.h"
18 #include "CGValue.h"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/Basic/CodeGenOptions.h"
23 #include "clang/Basic/DiagnosticFrontend.h"
24 #include "clang/Basic/Builtins.h"
25 #include "clang/CodeGen/CGFunctionInfo.h"
26 #include "clang/CodeGen/SwiftCallingConv.h"
27 #include "llvm/ADT/SmallBitVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/IntrinsicsNVPTX.h"
34 #include "llvm/IR/IntrinsicsS390.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm> // std::sort
38 
39 using namespace clang;
40 using namespace CodeGen;
41 
42 // Helper for coercing an aggregate argument or return value into an integer
43 // array of the same size (including padding) and alignment.  This alternate
44 // coercion happens only for the RenderScript ABI and can be removed after
45 // runtimes that rely on it are no longer supported.
46 //
47 // RenderScript assumes that the size of the argument / return value in the IR
48 // is the same as the size of the corresponding qualified type. This helper
49 // coerces the aggregate type into an array of the same size (including
50 // padding).  This coercion is used in lieu of expansion of struct members or
51 // other canonical coercions that return a coerced-type of larger size.
52 //
53 // Ty          - The argument / return value type
54 // Context     - The associated ASTContext
55 // LLVMContext - The associated LLVMContext
56 static ABIArgInfo coerceToIntArray(QualType Ty,
57                                    ASTContext &Context,
58                                    llvm::LLVMContext &LLVMContext) {
59   // Alignment and Size are measured in bits.
60   const uint64_t Size = Context.getTypeSize(Ty);
61   const uint64_t Alignment = Context.getTypeAlign(Ty);
62   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
63   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
64   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
65 }
66 
67 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
68                                llvm::Value *Array,
69                                llvm::Value *Value,
70                                unsigned FirstIndex,
71                                unsigned LastIndex) {
72   // Alternatively, we could emit this as a loop in the source.
73   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
74     llvm::Value *Cell =
75         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
76     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
77   }
78 }
79 
80 static bool isAggregateTypeForABI(QualType T) {
81   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
82          T->isMemberFunctionPointerType();
83 }
84 
85 ABIArgInfo ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByVal,
86                                             bool Realign,
87                                             llvm::Type *Padding) const {
88   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), ByVal,
89                                  Realign, Padding);
90 }
91 
92 ABIArgInfo
93 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
94   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
95                                       /*ByVal*/ false, Realign);
96 }
97 
98 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
99                              QualType Ty) const {
100   return Address::invalid();
101 }
102 
103 bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
104   if (Ty->isPromotableIntegerType())
105     return true;
106 
107   if (const auto *EIT = Ty->getAs<ExtIntType>())
108     if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
109       return true;
110 
111   return false;
112 }
113 
114 ABIInfo::~ABIInfo() {}
115 
116 /// Does the given lowering require more than the given number of
117 /// registers when expanded?
118 ///
119 /// This is intended to be the basis of a reasonable basic implementation
120 /// of should{Pass,Return}IndirectlyForSwift.
121 ///
122 /// For most targets, a limit of four total registers is reasonable; this
123 /// limits the amount of code required in order to move around the value
124 /// in case it wasn't produced immediately prior to the call by the caller
125 /// (or wasn't produced in exactly the right registers) or isn't used
126 /// immediately within the callee.  But some targets may need to further
127 /// limit the register count due to an inability to support that many
128 /// return registers.
129 static bool occupiesMoreThan(CodeGenTypes &cgt,
130                              ArrayRef<llvm::Type*> scalarTypes,
131                              unsigned maxAllRegisters) {
132   unsigned intCount = 0, fpCount = 0;
133   for (llvm::Type *type : scalarTypes) {
134     if (type->isPointerTy()) {
135       intCount++;
136     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
137       auto ptrWidth = cgt.getTarget().getPointerWidth(0);
138       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
139     } else {
140       assert(type->isVectorTy() || type->isFloatingPointTy());
141       fpCount++;
142     }
143   }
144 
145   return (intCount + fpCount > maxAllRegisters);
146 }
147 
148 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
149                                              llvm::Type *eltTy,
150                                              unsigned numElts) const {
151   // The default implementation of this assumes that the target guarantees
152   // 128-bit SIMD support but nothing more.
153   return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
154 }
155 
156 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
157                                               CGCXXABI &CXXABI) {
158   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
159   if (!RD) {
160     if (!RT->getDecl()->canPassInRegisters())
161       return CGCXXABI::RAA_Indirect;
162     return CGCXXABI::RAA_Default;
163   }
164   return CXXABI.getRecordArgABI(RD);
165 }
166 
167 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
168                                               CGCXXABI &CXXABI) {
169   const RecordType *RT = T->getAs<RecordType>();
170   if (!RT)
171     return CGCXXABI::RAA_Default;
172   return getRecordArgABI(RT, CXXABI);
173 }
174 
175 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
176                                const ABIInfo &Info) {
177   QualType Ty = FI.getReturnType();
178 
179   if (const auto *RT = Ty->getAs<RecordType>())
180     if (!isa<CXXRecordDecl>(RT->getDecl()) &&
181         !RT->getDecl()->canPassInRegisters()) {
182       FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
183       return true;
184     }
185 
186   return CXXABI.classifyReturnType(FI);
187 }
188 
189 /// Pass transparent unions as if they were the type of the first element. Sema
190 /// should ensure that all elements of the union have the same "machine type".
191 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
192   if (const RecordType *UT = Ty->getAsUnionType()) {
193     const RecordDecl *UD = UT->getDecl();
194     if (UD->hasAttr<TransparentUnionAttr>()) {
195       assert(!UD->field_empty() && "sema created an empty transparent union");
196       return UD->field_begin()->getType();
197     }
198   }
199   return Ty;
200 }
201 
202 CGCXXABI &ABIInfo::getCXXABI() const {
203   return CGT.getCXXABI();
204 }
205 
206 ASTContext &ABIInfo::getContext() const {
207   return CGT.getContext();
208 }
209 
210 llvm::LLVMContext &ABIInfo::getVMContext() const {
211   return CGT.getLLVMContext();
212 }
213 
214 const llvm::DataLayout &ABIInfo::getDataLayout() const {
215   return CGT.getDataLayout();
216 }
217 
218 const TargetInfo &ABIInfo::getTarget() const {
219   return CGT.getTarget();
220 }
221 
222 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
223   return CGT.getCodeGenOpts();
224 }
225 
226 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
227 
228 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
229   return false;
230 }
231 
232 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
233                                                 uint64_t Members) const {
234   return false;
235 }
236 
237 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
238   raw_ostream &OS = llvm::errs();
239   OS << "(ABIArgInfo Kind=";
240   switch (TheKind) {
241   case Direct:
242     OS << "Direct Type=";
243     if (llvm::Type *Ty = getCoerceToType())
244       Ty->print(OS);
245     else
246       OS << "null";
247     break;
248   case Extend:
249     OS << "Extend";
250     break;
251   case Ignore:
252     OS << "Ignore";
253     break;
254   case InAlloca:
255     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
256     break;
257   case Indirect:
258     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
259        << " ByVal=" << getIndirectByVal()
260        << " Realign=" << getIndirectRealign();
261     break;
262   case IndirectAliased:
263     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
264        << " AadrSpace=" << getIndirectAddrSpace()
265        << " Realign=" << getIndirectRealign();
266     break;
267   case Expand:
268     OS << "Expand";
269     break;
270   case CoerceAndExpand:
271     OS << "CoerceAndExpand Type=";
272     getCoerceAndExpandType()->print(OS);
273     break;
274   }
275   OS << ")\n";
276 }
277 
278 // Dynamically round a pointer up to a multiple of the given alignment.
279 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
280                                                   llvm::Value *Ptr,
281                                                   CharUnits Align) {
282   llvm::Value *PtrAsInt = Ptr;
283   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
284   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
285   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
286         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
287   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
288            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
289   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
290                                         Ptr->getType(),
291                                         Ptr->getName() + ".aligned");
292   return PtrAsInt;
293 }
294 
295 /// Emit va_arg for a platform using the common void* representation,
296 /// where arguments are simply emitted in an array of slots on the stack.
297 ///
298 /// This version implements the core direct-value passing rules.
299 ///
300 /// \param SlotSize - The size and alignment of a stack slot.
301 ///   Each argument will be allocated to a multiple of this number of
302 ///   slots, and all the slots will be aligned to this value.
303 /// \param AllowHigherAlign - The slot alignment is not a cap;
304 ///   an argument type with an alignment greater than the slot size
305 ///   will be emitted on a higher-alignment address, potentially
306 ///   leaving one or more empty slots behind as padding.  If this
307 ///   is false, the returned address might be less-aligned than
308 ///   DirectAlign.
309 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
310                                       Address VAListAddr,
311                                       llvm::Type *DirectTy,
312                                       CharUnits DirectSize,
313                                       CharUnits DirectAlign,
314                                       CharUnits SlotSize,
315                                       bool AllowHigherAlign) {
316   // Cast the element type to i8* if necessary.  Some platforms define
317   // va_list as a struct containing an i8* instead of just an i8*.
318   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
319     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
320 
321   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
322 
323   // If the CC aligns values higher than the slot size, do so if needed.
324   Address Addr = Address::invalid();
325   if (AllowHigherAlign && DirectAlign > SlotSize) {
326     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
327                                                  DirectAlign);
328   } else {
329     Addr = Address(Ptr, SlotSize);
330   }
331 
332   // Advance the pointer past the argument, then store that back.
333   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
334   Address NextPtr =
335       CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
336   CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
337 
338   // If the argument is smaller than a slot, and this is a big-endian
339   // target, the argument will be right-adjusted in its slot.
340   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
341       !DirectTy->isStructTy()) {
342     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
343   }
344 
345   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
346   return Addr;
347 }
348 
349 /// Emit va_arg for a platform using the common void* representation,
350 /// where arguments are simply emitted in an array of slots on the stack.
351 ///
352 /// \param IsIndirect - Values of this type are passed indirectly.
353 /// \param ValueInfo - The size and alignment of this type, generally
354 ///   computed with getContext().getTypeInfoInChars(ValueTy).
355 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
356 ///   Each argument will be allocated to a multiple of this number of
357 ///   slots, and all the slots will be aligned to this value.
358 /// \param AllowHigherAlign - The slot alignment is not a cap;
359 ///   an argument type with an alignment greater than the slot size
360 ///   will be emitted on a higher-alignment address, potentially
361 ///   leaving one or more empty slots behind as padding.
362 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
363                                 QualType ValueTy, bool IsIndirect,
364                                 TypeInfoChars ValueInfo,
365                                 CharUnits SlotSizeAndAlign,
366                                 bool AllowHigherAlign) {
367   // The size and alignment of the value that was passed directly.
368   CharUnits DirectSize, DirectAlign;
369   if (IsIndirect) {
370     DirectSize = CGF.getPointerSize();
371     DirectAlign = CGF.getPointerAlign();
372   } else {
373     DirectSize = ValueInfo.Width;
374     DirectAlign = ValueInfo.Align;
375   }
376 
377   // Cast the address we've calculated to the right type.
378   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
379   if (IsIndirect)
380     DirectTy = DirectTy->getPointerTo(0);
381 
382   Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
383                                         DirectSize, DirectAlign,
384                                         SlotSizeAndAlign,
385                                         AllowHigherAlign);
386 
387   if (IsIndirect) {
388     Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.Align);
389   }
390 
391   return Addr;
392 
393 }
394 
395 static Address emitMergePHI(CodeGenFunction &CGF,
396                             Address Addr1, llvm::BasicBlock *Block1,
397                             Address Addr2, llvm::BasicBlock *Block2,
398                             const llvm::Twine &Name = "") {
399   assert(Addr1.getType() == Addr2.getType());
400   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
401   PHI->addIncoming(Addr1.getPointer(), Block1);
402   PHI->addIncoming(Addr2.getPointer(), Block2);
403   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
404   return Address(PHI, Align);
405 }
406 
407 TargetCodeGenInfo::~TargetCodeGenInfo() = default;
408 
409 // If someone can figure out a general rule for this, that would be great.
410 // It's probably just doomed to be platform-dependent, though.
411 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
412   // Verified for:
413   //   x86-64     FreeBSD, Linux, Darwin
414   //   x86-32     FreeBSD, Linux, Darwin
415   //   PowerPC    Linux, Darwin
416   //   ARM        Darwin (*not* EABI)
417   //   AArch64    Linux
418   return 32;
419 }
420 
421 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
422                                      const FunctionNoProtoType *fnType) const {
423   // The following conventions are known to require this to be false:
424   //   x86_stdcall
425   //   MIPS
426   // For everything else, we just prefer false unless we opt out.
427   return false;
428 }
429 
430 void
431 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
432                                              llvm::SmallString<24> &Opt) const {
433   // This assumes the user is passing a library name like "rt" instead of a
434   // filename like "librt.a/so", and that they don't care whether it's static or
435   // dynamic.
436   Opt = "-l";
437   Opt += Lib;
438 }
439 
440 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
441   // OpenCL kernels are called via an explicit runtime API with arguments
442   // set with clSetKernelArg(), not as normal sub-functions.
443   // Return SPIR_KERNEL by default as the kernel calling convention to
444   // ensure the fingerprint is fixed such way that each OpenCL argument
445   // gets one matching argument in the produced kernel function argument
446   // list to enable feasible implementation of clSetKernelArg() with
447   // aggregates etc. In case we would use the default C calling conv here,
448   // clSetKernelArg() might break depending on the target-specific
449   // conventions; different targets might split structs passed as values
450   // to multiple function arguments etc.
451   return llvm::CallingConv::SPIR_KERNEL;
452 }
453 
454 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
455     llvm::PointerType *T, QualType QT) const {
456   return llvm::ConstantPointerNull::get(T);
457 }
458 
459 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
460                                                    const VarDecl *D) const {
461   assert(!CGM.getLangOpts().OpenCL &&
462          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
463          "Address space agnostic languages only");
464   return D ? D->getType().getAddressSpace() : LangAS::Default;
465 }
466 
467 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
468     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
469     LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
470   // Since target may map different address spaces in AST to the same address
471   // space, an address space conversion may end up as a bitcast.
472   if (auto *C = dyn_cast<llvm::Constant>(Src))
473     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
474   // Try to preserve the source's name to make IR more readable.
475   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
476       Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
477 }
478 
479 llvm::Constant *
480 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
481                                         LangAS SrcAddr, LangAS DestAddr,
482                                         llvm::Type *DestTy) const {
483   // Since target may map different address spaces in AST to the same address
484   // space, an address space conversion may end up as a bitcast.
485   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
486 }
487 
488 llvm::SyncScope::ID
489 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
490                                       SyncScope Scope,
491                                       llvm::AtomicOrdering Ordering,
492                                       llvm::LLVMContext &Ctx) const {
493   return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
494 }
495 
496 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
497 
498 /// isEmptyField - Return true iff a the field is "empty", that is it
499 /// is an unnamed bit-field or an (array of) empty record(s).
500 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
501                          bool AllowArrays) {
502   if (FD->isUnnamedBitfield())
503     return true;
504 
505   QualType FT = FD->getType();
506 
507   // Constant arrays of empty records count as empty, strip them off.
508   // Constant arrays of zero length always count as empty.
509   bool WasArray = false;
510   if (AllowArrays)
511     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
512       if (AT->getSize() == 0)
513         return true;
514       FT = AT->getElementType();
515       // The [[no_unique_address]] special case below does not apply to
516       // arrays of C++ empty records, so we need to remember this fact.
517       WasArray = true;
518     }
519 
520   const RecordType *RT = FT->getAs<RecordType>();
521   if (!RT)
522     return false;
523 
524   // C++ record fields are never empty, at least in the Itanium ABI.
525   //
526   // FIXME: We should use a predicate for whether this behavior is true in the
527   // current ABI.
528   //
529   // The exception to the above rule are fields marked with the
530   // [[no_unique_address]] attribute (since C++20).  Those do count as empty
531   // according to the Itanium ABI.  The exception applies only to records,
532   // not arrays of records, so we must also check whether we stripped off an
533   // array type above.
534   if (isa<CXXRecordDecl>(RT->getDecl()) &&
535       (WasArray || !FD->hasAttr<NoUniqueAddressAttr>()))
536     return false;
537 
538   return isEmptyRecord(Context, FT, AllowArrays);
539 }
540 
541 /// isEmptyRecord - Return true iff a structure contains only empty
542 /// fields. Note that a structure with a flexible array member is not
543 /// considered empty.
544 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
545   const RecordType *RT = T->getAs<RecordType>();
546   if (!RT)
547     return false;
548   const RecordDecl *RD = RT->getDecl();
549   if (RD->hasFlexibleArrayMember())
550     return false;
551 
552   // If this is a C++ record, check the bases first.
553   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
554     for (const auto &I : CXXRD->bases())
555       if (!isEmptyRecord(Context, I.getType(), true))
556         return false;
557 
558   for (const auto *I : RD->fields())
559     if (!isEmptyField(Context, I, AllowArrays))
560       return false;
561   return true;
562 }
563 
564 /// isSingleElementStruct - Determine if a structure is a "single
565 /// element struct", i.e. it has exactly one non-empty field or
566 /// exactly one field which is itself a single element
567 /// struct. Structures with flexible array members are never
568 /// considered single element structs.
569 ///
570 /// \return The field declaration for the single non-empty field, if
571 /// it exists.
572 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
573   const RecordType *RT = T->getAs<RecordType>();
574   if (!RT)
575     return nullptr;
576 
577   const RecordDecl *RD = RT->getDecl();
578   if (RD->hasFlexibleArrayMember())
579     return nullptr;
580 
581   const Type *Found = nullptr;
582 
583   // If this is a C++ record, check the bases first.
584   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
585     for (const auto &I : CXXRD->bases()) {
586       // Ignore empty records.
587       if (isEmptyRecord(Context, I.getType(), true))
588         continue;
589 
590       // If we already found an element then this isn't a single-element struct.
591       if (Found)
592         return nullptr;
593 
594       // If this is non-empty and not a single element struct, the composite
595       // cannot be a single element struct.
596       Found = isSingleElementStruct(I.getType(), Context);
597       if (!Found)
598         return nullptr;
599     }
600   }
601 
602   // Check for single element.
603   for (const auto *FD : RD->fields()) {
604     QualType FT = FD->getType();
605 
606     // Ignore empty fields.
607     if (isEmptyField(Context, FD, true))
608       continue;
609 
610     // If we already found an element then this isn't a single-element
611     // struct.
612     if (Found)
613       return nullptr;
614 
615     // Treat single element arrays as the element.
616     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
617       if (AT->getSize().getZExtValue() != 1)
618         break;
619       FT = AT->getElementType();
620     }
621 
622     if (!isAggregateTypeForABI(FT)) {
623       Found = FT.getTypePtr();
624     } else {
625       Found = isSingleElementStruct(FT, Context);
626       if (!Found)
627         return nullptr;
628     }
629   }
630 
631   // We don't consider a struct a single-element struct if it has
632   // padding beyond the element type.
633   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
634     return nullptr;
635 
636   return Found;
637 }
638 
639 namespace {
640 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
641                        const ABIArgInfo &AI) {
642   // This default implementation defers to the llvm backend's va_arg
643   // instruction. It can handle only passing arguments directly
644   // (typically only handled in the backend for primitive types), or
645   // aggregates passed indirectly by pointer (NOTE: if the "byval"
646   // flag has ABI impact in the callee, this implementation cannot
647   // work.)
648 
649   // Only a few cases are covered here at the moment -- those needed
650   // by the default abi.
651   llvm::Value *Val;
652 
653   if (AI.isIndirect()) {
654     assert(!AI.getPaddingType() &&
655            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
656     assert(
657         !AI.getIndirectRealign() &&
658         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
659 
660     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
661     CharUnits TyAlignForABI = TyInfo.Align;
662 
663     llvm::Type *BaseTy =
664         llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
665     llvm::Value *Addr =
666         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
667     return Address(Addr, TyAlignForABI);
668   } else {
669     assert((AI.isDirect() || AI.isExtend()) &&
670            "Unexpected ArgInfo Kind in generic VAArg emitter!");
671 
672     assert(!AI.getInReg() &&
673            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
674     assert(!AI.getPaddingType() &&
675            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
676     assert(!AI.getDirectOffset() &&
677            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
678     assert(!AI.getCoerceToType() &&
679            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
680 
681     Address Temp = CGF.CreateMemTemp(Ty, "varet");
682     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
683     CGF.Builder.CreateStore(Val, Temp);
684     return Temp;
685   }
686 }
687 
688 /// DefaultABIInfo - The default implementation for ABI specific
689 /// details. This implementation provides information which results in
690 /// self-consistent and sensible LLVM IR generation, but does not
691 /// conform to any particular ABI.
692 class DefaultABIInfo : public ABIInfo {
693 public:
694   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
695 
696   ABIArgInfo classifyReturnType(QualType RetTy) const;
697   ABIArgInfo classifyArgumentType(QualType RetTy) const;
698 
699   void computeInfo(CGFunctionInfo &FI) const override {
700     if (!getCXXABI().classifyReturnType(FI))
701       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
702     for (auto &I : FI.arguments())
703       I.info = classifyArgumentType(I.type);
704   }
705 
706   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
707                     QualType Ty) const override {
708     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
709   }
710 };
711 
712 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
713 public:
714   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
715       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
716 };
717 
718 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
719   Ty = useFirstFieldIfTransparentUnion(Ty);
720 
721   if (isAggregateTypeForABI(Ty)) {
722     // Records with non-trivial destructors/copy-constructors should not be
723     // passed by value.
724     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
725       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
726 
727     return getNaturalAlignIndirect(Ty);
728   }
729 
730   // Treat an enum type as its underlying type.
731   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
732     Ty = EnumTy->getDecl()->getIntegerType();
733 
734   ASTContext &Context = getContext();
735   if (const auto *EIT = Ty->getAs<ExtIntType>())
736     if (EIT->getNumBits() >
737         Context.getTypeSize(Context.getTargetInfo().hasInt128Type()
738                                 ? Context.Int128Ty
739                                 : Context.LongLongTy))
740       return getNaturalAlignIndirect(Ty);
741 
742   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
743                                             : ABIArgInfo::getDirect());
744 }
745 
746 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
747   if (RetTy->isVoidType())
748     return ABIArgInfo::getIgnore();
749 
750   if (isAggregateTypeForABI(RetTy))
751     return getNaturalAlignIndirect(RetTy);
752 
753   // Treat an enum type as its underlying type.
754   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
755     RetTy = EnumTy->getDecl()->getIntegerType();
756 
757   if (const auto *EIT = RetTy->getAs<ExtIntType>())
758     if (EIT->getNumBits() >
759         getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
760                                      ? getContext().Int128Ty
761                                      : getContext().LongLongTy))
762       return getNaturalAlignIndirect(RetTy);
763 
764   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
765                                                : ABIArgInfo::getDirect());
766 }
767 
768 //===----------------------------------------------------------------------===//
769 // WebAssembly ABI Implementation
770 //
771 // This is a very simple ABI that relies a lot on DefaultABIInfo.
772 //===----------------------------------------------------------------------===//
773 
774 class WebAssemblyABIInfo final : public SwiftABIInfo {
775 public:
776   enum ABIKind {
777     MVP = 0,
778     ExperimentalMV = 1,
779   };
780 
781 private:
782   DefaultABIInfo defaultInfo;
783   ABIKind Kind;
784 
785 public:
786   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
787       : SwiftABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {}
788 
789 private:
790   ABIArgInfo classifyReturnType(QualType RetTy) const;
791   ABIArgInfo classifyArgumentType(QualType Ty) const;
792 
793   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
794   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
795   // overload them.
796   void computeInfo(CGFunctionInfo &FI) const override {
797     if (!getCXXABI().classifyReturnType(FI))
798       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
799     for (auto &Arg : FI.arguments())
800       Arg.info = classifyArgumentType(Arg.type);
801   }
802 
803   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
804                     QualType Ty) const override;
805 
806   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
807                                     bool asReturnValue) const override {
808     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
809   }
810 
811   bool isSwiftErrorInRegister() const override {
812     return false;
813   }
814 };
815 
816 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
817 public:
818   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
819                                         WebAssemblyABIInfo::ABIKind K)
820       : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {}
821 
822   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
823                            CodeGen::CodeGenModule &CGM) const override {
824     TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
825     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
826       if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
827         llvm::Function *Fn = cast<llvm::Function>(GV);
828         llvm::AttrBuilder B;
829         B.addAttribute("wasm-import-module", Attr->getImportModule());
830         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
831       }
832       if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
833         llvm::Function *Fn = cast<llvm::Function>(GV);
834         llvm::AttrBuilder B;
835         B.addAttribute("wasm-import-name", Attr->getImportName());
836         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
837       }
838       if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
839         llvm::Function *Fn = cast<llvm::Function>(GV);
840         llvm::AttrBuilder B;
841         B.addAttribute("wasm-export-name", Attr->getExportName());
842         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
843       }
844     }
845 
846     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
847       llvm::Function *Fn = cast<llvm::Function>(GV);
848       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
849         Fn->addFnAttr("no-prototype");
850     }
851   }
852 };
853 
854 /// Classify argument of given type \p Ty.
855 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
856   Ty = useFirstFieldIfTransparentUnion(Ty);
857 
858   if (isAggregateTypeForABI(Ty)) {
859     // Records with non-trivial destructors/copy-constructors should not be
860     // passed by value.
861     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
862       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
863     // Ignore empty structs/unions.
864     if (isEmptyRecord(getContext(), Ty, true))
865       return ABIArgInfo::getIgnore();
866     // Lower single-element structs to just pass a regular value. TODO: We
867     // could do reasonable-size multiple-element structs too, using getExpand(),
868     // though watch out for things like bitfields.
869     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
870       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
871     // For the experimental multivalue ABI, fully expand all other aggregates
872     if (Kind == ABIKind::ExperimentalMV) {
873       const RecordType *RT = Ty->getAs<RecordType>();
874       assert(RT);
875       bool HasBitField = false;
876       for (auto *Field : RT->getDecl()->fields()) {
877         if (Field->isBitField()) {
878           HasBitField = true;
879           break;
880         }
881       }
882       if (!HasBitField)
883         return ABIArgInfo::getExpand();
884     }
885   }
886 
887   // Otherwise just do the default thing.
888   return defaultInfo.classifyArgumentType(Ty);
889 }
890 
891 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
892   if (isAggregateTypeForABI(RetTy)) {
893     // Records with non-trivial destructors/copy-constructors should not be
894     // returned by value.
895     if (!getRecordArgABI(RetTy, getCXXABI())) {
896       // Ignore empty structs/unions.
897       if (isEmptyRecord(getContext(), RetTy, true))
898         return ABIArgInfo::getIgnore();
899       // Lower single-element structs to just return a regular value. TODO: We
900       // could do reasonable-size multiple-element structs too, using
901       // ABIArgInfo::getDirect().
902       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
903         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
904       // For the experimental multivalue ABI, return all other aggregates
905       if (Kind == ABIKind::ExperimentalMV)
906         return ABIArgInfo::getDirect();
907     }
908   }
909 
910   // Otherwise just do the default thing.
911   return defaultInfo.classifyReturnType(RetTy);
912 }
913 
914 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
915                                       QualType Ty) const {
916   bool IsIndirect = isAggregateTypeForABI(Ty) &&
917                     !isEmptyRecord(getContext(), Ty, true) &&
918                     !isSingleElementStruct(Ty, getContext());
919   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
920                           getContext().getTypeInfoInChars(Ty),
921                           CharUnits::fromQuantity(4),
922                           /*AllowHigherAlign=*/true);
923 }
924 
925 //===----------------------------------------------------------------------===//
926 // le32/PNaCl bitcode ABI Implementation
927 //
928 // This is a simplified version of the x86_32 ABI.  Arguments and return values
929 // are always passed on the stack.
930 //===----------------------------------------------------------------------===//
931 
932 class PNaClABIInfo : public ABIInfo {
933  public:
934   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
935 
936   ABIArgInfo classifyReturnType(QualType RetTy) const;
937   ABIArgInfo classifyArgumentType(QualType RetTy) const;
938 
939   void computeInfo(CGFunctionInfo &FI) const override;
940   Address EmitVAArg(CodeGenFunction &CGF,
941                     Address VAListAddr, QualType Ty) const override;
942 };
943 
944 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
945  public:
946    PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
947        : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {}
948 };
949 
950 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
951   if (!getCXXABI().classifyReturnType(FI))
952     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
953 
954   for (auto &I : FI.arguments())
955     I.info = classifyArgumentType(I.type);
956 }
957 
958 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
959                                 QualType Ty) const {
960   // The PNaCL ABI is a bit odd, in that varargs don't use normal
961   // function classification. Structs get passed directly for varargs
962   // functions, through a rewriting transform in
963   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
964   // this target to actually support a va_arg instructions with an
965   // aggregate type, unlike other targets.
966   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
967 }
968 
969 /// Classify argument of given type \p Ty.
970 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
971   if (isAggregateTypeForABI(Ty)) {
972     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
973       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
974     return getNaturalAlignIndirect(Ty);
975   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
976     // Treat an enum type as its underlying type.
977     Ty = EnumTy->getDecl()->getIntegerType();
978   } else if (Ty->isFloatingType()) {
979     // Floating-point types don't go inreg.
980     return ABIArgInfo::getDirect();
981   } else if (const auto *EIT = Ty->getAs<ExtIntType>()) {
982     // Treat extended integers as integers if <=64, otherwise pass indirectly.
983     if (EIT->getNumBits() > 64)
984       return getNaturalAlignIndirect(Ty);
985     return ABIArgInfo::getDirect();
986   }
987 
988   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
989                                             : ABIArgInfo::getDirect());
990 }
991 
992 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
993   if (RetTy->isVoidType())
994     return ABIArgInfo::getIgnore();
995 
996   // In the PNaCl ABI we always return records/structures on the stack.
997   if (isAggregateTypeForABI(RetTy))
998     return getNaturalAlignIndirect(RetTy);
999 
1000   // Treat extended integers as integers if <=64, otherwise pass indirectly.
1001   if (const auto *EIT = RetTy->getAs<ExtIntType>()) {
1002     if (EIT->getNumBits() > 64)
1003       return getNaturalAlignIndirect(RetTy);
1004     return ABIArgInfo::getDirect();
1005   }
1006 
1007   // Treat an enum type as its underlying type.
1008   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1009     RetTy = EnumTy->getDecl()->getIntegerType();
1010 
1011   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1012                                                : ABIArgInfo::getDirect());
1013 }
1014 
1015 /// IsX86_MMXType - Return true if this is an MMX type.
1016 bool IsX86_MMXType(llvm::Type *IRType) {
1017   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
1018   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
1019     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
1020     IRType->getScalarSizeInBits() != 64;
1021 }
1022 
1023 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1024                                           StringRef Constraint,
1025                                           llvm::Type* Ty) {
1026   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
1027                      .Cases("y", "&y", "^Ym", true)
1028                      .Default(false);
1029   if (IsMMXCons && Ty->isVectorTy()) {
1030     if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedSize() !=
1031         64) {
1032       // Invalid MMX constraint
1033       return nullptr;
1034     }
1035 
1036     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
1037   }
1038 
1039   // No operation needed
1040   return Ty;
1041 }
1042 
1043 /// Returns true if this type can be passed in SSE registers with the
1044 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1045 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
1046   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1047     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
1048       if (BT->getKind() == BuiltinType::LongDouble) {
1049         if (&Context.getTargetInfo().getLongDoubleFormat() ==
1050             &llvm::APFloat::x87DoubleExtended())
1051           return false;
1052       }
1053       return true;
1054     }
1055   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
1056     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
1057     // registers specially.
1058     unsigned VecSize = Context.getTypeSize(VT);
1059     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
1060       return true;
1061   }
1062   return false;
1063 }
1064 
1065 /// Returns true if this aggregate is small enough to be passed in SSE registers
1066 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1067 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
1068   return NumMembers <= 4;
1069 }
1070 
1071 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
1072 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
1073   auto AI = ABIArgInfo::getDirect(T);
1074   AI.setInReg(true);
1075   AI.setCanBeFlattened(false);
1076   return AI;
1077 }
1078 
1079 //===----------------------------------------------------------------------===//
1080 // X86-32 ABI Implementation
1081 //===----------------------------------------------------------------------===//
1082 
1083 /// Similar to llvm::CCState, but for Clang.
1084 struct CCState {
1085   CCState(CGFunctionInfo &FI)
1086       : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {}
1087 
1088   llvm::SmallBitVector IsPreassigned;
1089   unsigned CC = CallingConv::CC_C;
1090   unsigned FreeRegs = 0;
1091   unsigned FreeSSERegs = 0;
1092 };
1093 
1094 /// X86_32ABIInfo - The X86-32 ABI information.
1095 class X86_32ABIInfo : public SwiftABIInfo {
1096   enum Class {
1097     Integer,
1098     Float
1099   };
1100 
1101   static const unsigned MinABIStackAlignInBytes = 4;
1102 
1103   bool IsDarwinVectorABI;
1104   bool IsRetSmallStructInRegABI;
1105   bool IsWin32StructABI;
1106   bool IsSoftFloatABI;
1107   bool IsMCUABI;
1108   bool IsLinuxABI;
1109   unsigned DefaultNumRegisterParameters;
1110 
1111   static bool isRegisterSize(unsigned Size) {
1112     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
1113   }
1114 
1115   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1116     // FIXME: Assumes vectorcall is in use.
1117     return isX86VectorTypeForVectorCall(getContext(), Ty);
1118   }
1119 
1120   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1121                                          uint64_t NumMembers) const override {
1122     // FIXME: Assumes vectorcall is in use.
1123     return isX86VectorCallAggregateSmallEnough(NumMembers);
1124   }
1125 
1126   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
1127 
1128   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1129   /// such that the argument will be passed in memory.
1130   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
1131 
1132   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
1133 
1134   /// Return the alignment to use for the given type on the stack.
1135   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
1136 
1137   Class classify(QualType Ty) const;
1138   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
1139   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
1140 
1141   /// Updates the number of available free registers, returns
1142   /// true if any registers were allocated.
1143   bool updateFreeRegs(QualType Ty, CCState &State) const;
1144 
1145   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1146                                 bool &NeedsPadding) const;
1147   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1148 
1149   bool canExpandIndirectArgument(QualType Ty) const;
1150 
1151   /// Rewrite the function info so that all memory arguments use
1152   /// inalloca.
1153   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1154 
1155   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1156                            CharUnits &StackOffset, ABIArgInfo &Info,
1157                            QualType Type) const;
1158   void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const;
1159 
1160 public:
1161 
1162   void computeInfo(CGFunctionInfo &FI) const override;
1163   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1164                     QualType Ty) const override;
1165 
1166   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1167                 bool RetSmallStructInRegABI, bool Win32StructABI,
1168                 unsigned NumRegisterParameters, bool SoftFloatABI)
1169     : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1170       IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1171       IsWin32StructABI(Win32StructABI), IsSoftFloatABI(SoftFloatABI),
1172       IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1173       IsLinuxABI(CGT.getTarget().getTriple().isOSLinux()),
1174       DefaultNumRegisterParameters(NumRegisterParameters) {}
1175 
1176   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
1177                                     bool asReturnValue) const override {
1178     // LLVM's x86-32 lowering currently only assigns up to three
1179     // integer registers and three fp registers.  Oddly, it'll use up to
1180     // four vector registers for vectors, but those can overlap with the
1181     // scalar registers.
1182     return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1183   }
1184 
1185   bool isSwiftErrorInRegister() const override {
1186     // x86-32 lowering does not support passing swifterror in a register.
1187     return false;
1188   }
1189 };
1190 
1191 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1192 public:
1193   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1194                           bool RetSmallStructInRegABI, bool Win32StructABI,
1195                           unsigned NumRegisterParameters, bool SoftFloatABI)
1196       : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>(
1197             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1198             NumRegisterParameters, SoftFloatABI)) {}
1199 
1200   static bool isStructReturnInRegABI(
1201       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1202 
1203   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1204                            CodeGen::CodeGenModule &CGM) const override;
1205 
1206   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1207     // Darwin uses different dwarf register numbers for EH.
1208     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1209     return 4;
1210   }
1211 
1212   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1213                                llvm::Value *Address) const override;
1214 
1215   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1216                                   StringRef Constraint,
1217                                   llvm::Type* Ty) const override {
1218     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1219   }
1220 
1221   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1222                                 std::string &Constraints,
1223                                 std::vector<llvm::Type *> &ResultRegTypes,
1224                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1225                                 std::vector<LValue> &ResultRegDests,
1226                                 std::string &AsmString,
1227                                 unsigned NumOutputs) const override;
1228 
1229   llvm::Constant *
1230   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1231     unsigned Sig = (0xeb << 0) |  // jmp rel8
1232                    (0x06 << 8) |  //           .+0x08
1233                    ('v' << 16) |
1234                    ('2' << 24);
1235     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1236   }
1237 
1238   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1239     return "movl\t%ebp, %ebp"
1240            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1241   }
1242 };
1243 
1244 }
1245 
1246 /// Rewrite input constraint references after adding some output constraints.
1247 /// In the case where there is one output and one input and we add one output,
1248 /// we need to replace all operand references greater than or equal to 1:
1249 ///     mov $0, $1
1250 ///     mov eax, $1
1251 /// The result will be:
1252 ///     mov $0, $2
1253 ///     mov eax, $2
1254 static void rewriteInputConstraintReferences(unsigned FirstIn,
1255                                              unsigned NumNewOuts,
1256                                              std::string &AsmString) {
1257   std::string Buf;
1258   llvm::raw_string_ostream OS(Buf);
1259   size_t Pos = 0;
1260   while (Pos < AsmString.size()) {
1261     size_t DollarStart = AsmString.find('$', Pos);
1262     if (DollarStart == std::string::npos)
1263       DollarStart = AsmString.size();
1264     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1265     if (DollarEnd == std::string::npos)
1266       DollarEnd = AsmString.size();
1267     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1268     Pos = DollarEnd;
1269     size_t NumDollars = DollarEnd - DollarStart;
1270     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1271       // We have an operand reference.
1272       size_t DigitStart = Pos;
1273       if (AsmString[DigitStart] == '{') {
1274         OS << '{';
1275         ++DigitStart;
1276       }
1277       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1278       if (DigitEnd == std::string::npos)
1279         DigitEnd = AsmString.size();
1280       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1281       unsigned OperandIndex;
1282       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1283         if (OperandIndex >= FirstIn)
1284           OperandIndex += NumNewOuts;
1285         OS << OperandIndex;
1286       } else {
1287         OS << OperandStr;
1288       }
1289       Pos = DigitEnd;
1290     }
1291   }
1292   AsmString = std::move(OS.str());
1293 }
1294 
1295 /// Add output constraints for EAX:EDX because they are return registers.
1296 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1297     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1298     std::vector<llvm::Type *> &ResultRegTypes,
1299     std::vector<llvm::Type *> &ResultTruncRegTypes,
1300     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1301     unsigned NumOutputs) const {
1302   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1303 
1304   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1305   // larger.
1306   if (!Constraints.empty())
1307     Constraints += ',';
1308   if (RetWidth <= 32) {
1309     Constraints += "={eax}";
1310     ResultRegTypes.push_back(CGF.Int32Ty);
1311   } else {
1312     // Use the 'A' constraint for EAX:EDX.
1313     Constraints += "=A";
1314     ResultRegTypes.push_back(CGF.Int64Ty);
1315   }
1316 
1317   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1318   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1319   ResultTruncRegTypes.push_back(CoerceTy);
1320 
1321   // Coerce the integer by bitcasting the return slot pointer.
1322   ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF),
1323                                                   CoerceTy->getPointerTo()));
1324   ResultRegDests.push_back(ReturnSlot);
1325 
1326   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1327 }
1328 
1329 /// shouldReturnTypeInRegister - Determine if the given type should be
1330 /// returned in a register (for the Darwin and MCU ABI).
1331 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1332                                                ASTContext &Context) const {
1333   uint64_t Size = Context.getTypeSize(Ty);
1334 
1335   // For i386, type must be register sized.
1336   // For the MCU ABI, it only needs to be <= 8-byte
1337   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1338    return false;
1339 
1340   if (Ty->isVectorType()) {
1341     // 64- and 128- bit vectors inside structures are not returned in
1342     // registers.
1343     if (Size == 64 || Size == 128)
1344       return false;
1345 
1346     return true;
1347   }
1348 
1349   // If this is a builtin, pointer, enum, complex type, member pointer, or
1350   // member function pointer it is ok.
1351   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1352       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1353       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1354     return true;
1355 
1356   // Arrays are treated like records.
1357   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1358     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1359 
1360   // Otherwise, it must be a record type.
1361   const RecordType *RT = Ty->getAs<RecordType>();
1362   if (!RT) return false;
1363 
1364   // FIXME: Traverse bases here too.
1365 
1366   // Structure types are passed in register if all fields would be
1367   // passed in a register.
1368   for (const auto *FD : RT->getDecl()->fields()) {
1369     // Empty fields are ignored.
1370     if (isEmptyField(Context, FD, true))
1371       continue;
1372 
1373     // Check fields recursively.
1374     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1375       return false;
1376   }
1377   return true;
1378 }
1379 
1380 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1381   // Treat complex types as the element type.
1382   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1383     Ty = CTy->getElementType();
1384 
1385   // Check for a type which we know has a simple scalar argument-passing
1386   // convention without any padding.  (We're specifically looking for 32
1387   // and 64-bit integer and integer-equivalents, float, and double.)
1388   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1389       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1390     return false;
1391 
1392   uint64_t Size = Context.getTypeSize(Ty);
1393   return Size == 32 || Size == 64;
1394 }
1395 
1396 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1397                           uint64_t &Size) {
1398   for (const auto *FD : RD->fields()) {
1399     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1400     // argument is smaller than 32-bits, expanding the struct will create
1401     // alignment padding.
1402     if (!is32Or64BitBasicType(FD->getType(), Context))
1403       return false;
1404 
1405     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1406     // how to expand them yet, and the predicate for telling if a bitfield still
1407     // counts as "basic" is more complicated than what we were doing previously.
1408     if (FD->isBitField())
1409       return false;
1410 
1411     Size += Context.getTypeSize(FD->getType());
1412   }
1413   return true;
1414 }
1415 
1416 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1417                                  uint64_t &Size) {
1418   // Don't do this if there are any non-empty bases.
1419   for (const CXXBaseSpecifier &Base : RD->bases()) {
1420     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1421                               Size))
1422       return false;
1423   }
1424   if (!addFieldSizes(Context, RD, Size))
1425     return false;
1426   return true;
1427 }
1428 
1429 /// Test whether an argument type which is to be passed indirectly (on the
1430 /// stack) would have the equivalent layout if it was expanded into separate
1431 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1432 /// optimizations.
1433 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1434   // We can only expand structure types.
1435   const RecordType *RT = Ty->getAs<RecordType>();
1436   if (!RT)
1437     return false;
1438   const RecordDecl *RD = RT->getDecl();
1439   uint64_t Size = 0;
1440   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1441     if (!IsWin32StructABI) {
1442       // On non-Windows, we have to conservatively match our old bitcode
1443       // prototypes in order to be ABI-compatible at the bitcode level.
1444       if (!CXXRD->isCLike())
1445         return false;
1446     } else {
1447       // Don't do this for dynamic classes.
1448       if (CXXRD->isDynamicClass())
1449         return false;
1450     }
1451     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1452       return false;
1453   } else {
1454     if (!addFieldSizes(getContext(), RD, Size))
1455       return false;
1456   }
1457 
1458   // We can do this if there was no alignment padding.
1459   return Size == getContext().getTypeSize(Ty);
1460 }
1461 
1462 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1463   // If the return value is indirect, then the hidden argument is consuming one
1464   // integer register.
1465   if (State.FreeRegs) {
1466     --State.FreeRegs;
1467     if (!IsMCUABI)
1468       return getNaturalAlignIndirectInReg(RetTy);
1469   }
1470   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1471 }
1472 
1473 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1474                                              CCState &State) const {
1475   if (RetTy->isVoidType())
1476     return ABIArgInfo::getIgnore();
1477 
1478   const Type *Base = nullptr;
1479   uint64_t NumElts = 0;
1480   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1481        State.CC == llvm::CallingConv::X86_RegCall) &&
1482       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1483     // The LLVM struct type for such an aggregate should lower properly.
1484     return ABIArgInfo::getDirect();
1485   }
1486 
1487   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1488     // On Darwin, some vectors are returned in registers.
1489     if (IsDarwinVectorABI) {
1490       uint64_t Size = getContext().getTypeSize(RetTy);
1491 
1492       // 128-bit vectors are a special case; they are returned in
1493       // registers and we need to make sure to pick a type the LLVM
1494       // backend will like.
1495       if (Size == 128)
1496         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1497             llvm::Type::getInt64Ty(getVMContext()), 2));
1498 
1499       // Always return in register if it fits in a general purpose
1500       // register, or if it is 64 bits and has a single element.
1501       if ((Size == 8 || Size == 16 || Size == 32) ||
1502           (Size == 64 && VT->getNumElements() == 1))
1503         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1504                                                             Size));
1505 
1506       return getIndirectReturnResult(RetTy, State);
1507     }
1508 
1509     return ABIArgInfo::getDirect();
1510   }
1511 
1512   if (isAggregateTypeForABI(RetTy)) {
1513     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1514       // Structures with flexible arrays are always indirect.
1515       if (RT->getDecl()->hasFlexibleArrayMember())
1516         return getIndirectReturnResult(RetTy, State);
1517     }
1518 
1519     // If specified, structs and unions are always indirect.
1520     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1521       return getIndirectReturnResult(RetTy, State);
1522 
1523     // Ignore empty structs/unions.
1524     if (isEmptyRecord(getContext(), RetTy, true))
1525       return ABIArgInfo::getIgnore();
1526 
1527     // Small structures which are register sized are generally returned
1528     // in a register.
1529     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1530       uint64_t Size = getContext().getTypeSize(RetTy);
1531 
1532       // As a special-case, if the struct is a "single-element" struct, and
1533       // the field is of type "float" or "double", return it in a
1534       // floating-point register. (MSVC does not apply this special case.)
1535       // We apply a similar transformation for pointer types to improve the
1536       // quality of the generated IR.
1537       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1538         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1539             || SeltTy->hasPointerRepresentation())
1540           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1541 
1542       // FIXME: We should be able to narrow this integer in cases with dead
1543       // padding.
1544       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1545     }
1546 
1547     return getIndirectReturnResult(RetTy, State);
1548   }
1549 
1550   // Treat an enum type as its underlying type.
1551   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1552     RetTy = EnumTy->getDecl()->getIntegerType();
1553 
1554   if (const auto *EIT = RetTy->getAs<ExtIntType>())
1555     if (EIT->getNumBits() > 64)
1556       return getIndirectReturnResult(RetTy, State);
1557 
1558   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1559                                                : ABIArgInfo::getDirect());
1560 }
1561 
1562 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) {
1563   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1564 }
1565 
1566 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) {
1567   const RecordType *RT = Ty->getAs<RecordType>();
1568   if (!RT)
1569     return 0;
1570   const RecordDecl *RD = RT->getDecl();
1571 
1572   // If this is a C++ record, check the bases first.
1573   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1574     for (const auto &I : CXXRD->bases())
1575       if (!isRecordWithSIMDVectorType(Context, I.getType()))
1576         return false;
1577 
1578   for (const auto *i : RD->fields()) {
1579     QualType FT = i->getType();
1580 
1581     if (isSIMDVectorType(Context, FT))
1582       return true;
1583 
1584     if (isRecordWithSIMDVectorType(Context, FT))
1585       return true;
1586   }
1587 
1588   return false;
1589 }
1590 
1591 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1592                                                  unsigned Align) const {
1593   // Otherwise, if the alignment is less than or equal to the minimum ABI
1594   // alignment, just use the default; the backend will handle this.
1595   if (Align <= MinABIStackAlignInBytes)
1596     return 0; // Use default alignment.
1597 
1598   if (IsLinuxABI) {
1599     // Exclude other System V OS (e.g Darwin, PS4 and FreeBSD) since we don't
1600     // want to spend any effort dealing with the ramifications of ABI breaks.
1601     //
1602     // If the vector type is __m128/__m256/__m512, return the default alignment.
1603     if (Ty->isVectorType() && (Align == 16 || Align == 32 || Align == 64))
1604       return Align;
1605   }
1606   // On non-Darwin, the stack type alignment is always 4.
1607   if (!IsDarwinVectorABI) {
1608     // Set explicit alignment, since we may need to realign the top.
1609     return MinABIStackAlignInBytes;
1610   }
1611 
1612   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1613   if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) ||
1614                       isRecordWithSIMDVectorType(getContext(), Ty)))
1615     return 16;
1616 
1617   return MinABIStackAlignInBytes;
1618 }
1619 
1620 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1621                                             CCState &State) const {
1622   if (!ByVal) {
1623     if (State.FreeRegs) {
1624       --State.FreeRegs; // Non-byval indirects just use one pointer.
1625       if (!IsMCUABI)
1626         return getNaturalAlignIndirectInReg(Ty);
1627     }
1628     return getNaturalAlignIndirect(Ty, false);
1629   }
1630 
1631   // Compute the byval alignment.
1632   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1633   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1634   if (StackAlign == 0)
1635     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1636 
1637   // If the stack alignment is less than the type alignment, realign the
1638   // argument.
1639   bool Realign = TypeAlign > StackAlign;
1640   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1641                                  /*ByVal=*/true, Realign);
1642 }
1643 
1644 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1645   const Type *T = isSingleElementStruct(Ty, getContext());
1646   if (!T)
1647     T = Ty.getTypePtr();
1648 
1649   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1650     BuiltinType::Kind K = BT->getKind();
1651     if (K == BuiltinType::Float || K == BuiltinType::Double)
1652       return Float;
1653   }
1654   return Integer;
1655 }
1656 
1657 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1658   if (!IsSoftFloatABI) {
1659     Class C = classify(Ty);
1660     if (C == Float)
1661       return false;
1662   }
1663 
1664   unsigned Size = getContext().getTypeSize(Ty);
1665   unsigned SizeInRegs = (Size + 31) / 32;
1666 
1667   if (SizeInRegs == 0)
1668     return false;
1669 
1670   if (!IsMCUABI) {
1671     if (SizeInRegs > State.FreeRegs) {
1672       State.FreeRegs = 0;
1673       return false;
1674     }
1675   } else {
1676     // The MCU psABI allows passing parameters in-reg even if there are
1677     // earlier parameters that are passed on the stack. Also,
1678     // it does not allow passing >8-byte structs in-register,
1679     // even if there are 3 free registers available.
1680     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1681       return false;
1682   }
1683 
1684   State.FreeRegs -= SizeInRegs;
1685   return true;
1686 }
1687 
1688 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1689                                              bool &InReg,
1690                                              bool &NeedsPadding) const {
1691   // On Windows, aggregates other than HFAs are never passed in registers, and
1692   // they do not consume register slots. Homogenous floating-point aggregates
1693   // (HFAs) have already been dealt with at this point.
1694   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1695     return false;
1696 
1697   NeedsPadding = false;
1698   InReg = !IsMCUABI;
1699 
1700   if (!updateFreeRegs(Ty, State))
1701     return false;
1702 
1703   if (IsMCUABI)
1704     return true;
1705 
1706   if (State.CC == llvm::CallingConv::X86_FastCall ||
1707       State.CC == llvm::CallingConv::X86_VectorCall ||
1708       State.CC == llvm::CallingConv::X86_RegCall) {
1709     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1710       NeedsPadding = true;
1711 
1712     return false;
1713   }
1714 
1715   return true;
1716 }
1717 
1718 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1719   if (!updateFreeRegs(Ty, State))
1720     return false;
1721 
1722   if (IsMCUABI)
1723     return false;
1724 
1725   if (State.CC == llvm::CallingConv::X86_FastCall ||
1726       State.CC == llvm::CallingConv::X86_VectorCall ||
1727       State.CC == llvm::CallingConv::X86_RegCall) {
1728     if (getContext().getTypeSize(Ty) > 32)
1729       return false;
1730 
1731     return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1732         Ty->isReferenceType());
1733   }
1734 
1735   return true;
1736 }
1737 
1738 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const {
1739   // Vectorcall x86 works subtly different than in x64, so the format is
1740   // a bit different than the x64 version.  First, all vector types (not HVAs)
1741   // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers.
1742   // This differs from the x64 implementation, where the first 6 by INDEX get
1743   // registers.
1744   // In the second pass over the arguments, HVAs are passed in the remaining
1745   // vector registers if possible, or indirectly by address. The address will be
1746   // passed in ECX/EDX if available. Any other arguments are passed according to
1747   // the usual fastcall rules.
1748   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1749   for (int I = 0, E = Args.size(); I < E; ++I) {
1750     const Type *Base = nullptr;
1751     uint64_t NumElts = 0;
1752     const QualType &Ty = Args[I].type;
1753     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1754         isHomogeneousAggregate(Ty, Base, NumElts)) {
1755       if (State.FreeSSERegs >= NumElts) {
1756         State.FreeSSERegs -= NumElts;
1757         Args[I].info = ABIArgInfo::getDirectInReg();
1758         State.IsPreassigned.set(I);
1759       }
1760     }
1761   }
1762 }
1763 
1764 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1765                                                CCState &State) const {
1766   // FIXME: Set alignment on indirect arguments.
1767   bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall;
1768   bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall;
1769   bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall;
1770 
1771   Ty = useFirstFieldIfTransparentUnion(Ty);
1772   TypeInfo TI = getContext().getTypeInfo(Ty);
1773 
1774   // Check with the C++ ABI first.
1775   const RecordType *RT = Ty->getAs<RecordType>();
1776   if (RT) {
1777     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1778     if (RAA == CGCXXABI::RAA_Indirect) {
1779       return getIndirectResult(Ty, false, State);
1780     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1781       // The field index doesn't matter, we'll fix it up later.
1782       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1783     }
1784   }
1785 
1786   // Regcall uses the concept of a homogenous vector aggregate, similar
1787   // to other targets.
1788   const Type *Base = nullptr;
1789   uint64_t NumElts = 0;
1790   if ((IsRegCall || IsVectorCall) &&
1791       isHomogeneousAggregate(Ty, Base, NumElts)) {
1792     if (State.FreeSSERegs >= NumElts) {
1793       State.FreeSSERegs -= NumElts;
1794 
1795       // Vectorcall passes HVAs directly and does not flatten them, but regcall
1796       // does.
1797       if (IsVectorCall)
1798         return getDirectX86Hva();
1799 
1800       if (Ty->isBuiltinType() || Ty->isVectorType())
1801         return ABIArgInfo::getDirect();
1802       return ABIArgInfo::getExpand();
1803     }
1804     return getIndirectResult(Ty, /*ByVal=*/false, State);
1805   }
1806 
1807   if (isAggregateTypeForABI(Ty)) {
1808     // Structures with flexible arrays are always indirect.
1809     // FIXME: This should not be byval!
1810     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1811       return getIndirectResult(Ty, true, State);
1812 
1813     // Ignore empty structs/unions on non-Windows.
1814     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1815       return ABIArgInfo::getIgnore();
1816 
1817     llvm::LLVMContext &LLVMContext = getVMContext();
1818     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1819     bool NeedsPadding = false;
1820     bool InReg;
1821     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1822       unsigned SizeInRegs = (TI.Width + 31) / 32;
1823       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1824       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1825       if (InReg)
1826         return ABIArgInfo::getDirectInReg(Result);
1827       else
1828         return ABIArgInfo::getDirect(Result);
1829     }
1830     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1831 
1832     // Pass over-aligned aggregates on Windows indirectly. This behavior was
1833     // added in MSVC 2015.
1834     if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32)
1835       return getIndirectResult(Ty, /*ByVal=*/false, State);
1836 
1837     // Expand small (<= 128-bit) record types when we know that the stack layout
1838     // of those arguments will match the struct. This is important because the
1839     // LLVM backend isn't smart enough to remove byval, which inhibits many
1840     // optimizations.
1841     // Don't do this for the MCU if there are still free integer registers
1842     // (see X86_64 ABI for full explanation).
1843     if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) &&
1844         canExpandIndirectArgument(Ty))
1845       return ABIArgInfo::getExpandWithPadding(
1846           IsFastCall || IsVectorCall || IsRegCall, PaddingType);
1847 
1848     return getIndirectResult(Ty, true, State);
1849   }
1850 
1851   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1852     // On Windows, vectors are passed directly if registers are available, or
1853     // indirectly if not. This avoids the need to align argument memory. Pass
1854     // user-defined vector types larger than 512 bits indirectly for simplicity.
1855     if (IsWin32StructABI) {
1856       if (TI.Width <= 512 && State.FreeSSERegs > 0) {
1857         --State.FreeSSERegs;
1858         return ABIArgInfo::getDirectInReg();
1859       }
1860       return getIndirectResult(Ty, /*ByVal=*/false, State);
1861     }
1862 
1863     // On Darwin, some vectors are passed in memory, we handle this by passing
1864     // it as an i8/i16/i32/i64.
1865     if (IsDarwinVectorABI) {
1866       if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) ||
1867           (TI.Width == 64 && VT->getNumElements() == 1))
1868         return ABIArgInfo::getDirect(
1869             llvm::IntegerType::get(getVMContext(), TI.Width));
1870     }
1871 
1872     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1873       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1874 
1875     return ABIArgInfo::getDirect();
1876   }
1877 
1878 
1879   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1880     Ty = EnumTy->getDecl()->getIntegerType();
1881 
1882   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1883 
1884   if (isPromotableIntegerTypeForABI(Ty)) {
1885     if (InReg)
1886       return ABIArgInfo::getExtendInReg(Ty);
1887     return ABIArgInfo::getExtend(Ty);
1888   }
1889 
1890   if (const auto * EIT = Ty->getAs<ExtIntType>()) {
1891     if (EIT->getNumBits() <= 64) {
1892       if (InReg)
1893         return ABIArgInfo::getDirectInReg();
1894       return ABIArgInfo::getDirect();
1895     }
1896     return getIndirectResult(Ty, /*ByVal=*/false, State);
1897   }
1898 
1899   if (InReg)
1900     return ABIArgInfo::getDirectInReg();
1901   return ABIArgInfo::getDirect();
1902 }
1903 
1904 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1905   CCState State(FI);
1906   if (IsMCUABI)
1907     State.FreeRegs = 3;
1908   else if (State.CC == llvm::CallingConv::X86_FastCall) {
1909     State.FreeRegs = 2;
1910     State.FreeSSERegs = 3;
1911   } else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1912     State.FreeRegs = 2;
1913     State.FreeSSERegs = 6;
1914   } else if (FI.getHasRegParm())
1915     State.FreeRegs = FI.getRegParm();
1916   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1917     State.FreeRegs = 5;
1918     State.FreeSSERegs = 8;
1919   } else if (IsWin32StructABI) {
1920     // Since MSVC 2015, the first three SSE vectors have been passed in
1921     // registers. The rest are passed indirectly.
1922     State.FreeRegs = DefaultNumRegisterParameters;
1923     State.FreeSSERegs = 3;
1924   } else
1925     State.FreeRegs = DefaultNumRegisterParameters;
1926 
1927   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
1928     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1929   } else if (FI.getReturnInfo().isIndirect()) {
1930     // The C++ ABI is not aware of register usage, so we have to check if the
1931     // return value was sret and put it in a register ourselves if appropriate.
1932     if (State.FreeRegs) {
1933       --State.FreeRegs;  // The sret parameter consumes a register.
1934       if (!IsMCUABI)
1935         FI.getReturnInfo().setInReg(true);
1936     }
1937   }
1938 
1939   // The chain argument effectively gives us another free register.
1940   if (FI.isChainCall())
1941     ++State.FreeRegs;
1942 
1943   // For vectorcall, do a first pass over the arguments, assigning FP and vector
1944   // arguments to XMM registers as available.
1945   if (State.CC == llvm::CallingConv::X86_VectorCall)
1946     runVectorCallFirstPass(FI, State);
1947 
1948   bool UsedInAlloca = false;
1949   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1950   for (int I = 0, E = Args.size(); I < E; ++I) {
1951     // Skip arguments that have already been assigned.
1952     if (State.IsPreassigned.test(I))
1953       continue;
1954 
1955     Args[I].info = classifyArgumentType(Args[I].type, State);
1956     UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca);
1957   }
1958 
1959   // If we needed to use inalloca for any argument, do a second pass and rewrite
1960   // all the memory arguments to use inalloca.
1961   if (UsedInAlloca)
1962     rewriteWithInAlloca(FI);
1963 }
1964 
1965 void
1966 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1967                                    CharUnits &StackOffset, ABIArgInfo &Info,
1968                                    QualType Type) const {
1969   // Arguments are always 4-byte-aligned.
1970   CharUnits WordSize = CharUnits::fromQuantity(4);
1971   assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct");
1972 
1973   // sret pointers and indirect things will require an extra pointer
1974   // indirection, unless they are byval. Most things are byval, and will not
1975   // require this indirection.
1976   bool IsIndirect = false;
1977   if (Info.isIndirect() && !Info.getIndirectByVal())
1978     IsIndirect = true;
1979   Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect);
1980   llvm::Type *LLTy = CGT.ConvertTypeForMem(Type);
1981   if (IsIndirect)
1982     LLTy = LLTy->getPointerTo(0);
1983   FrameFields.push_back(LLTy);
1984   StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type);
1985 
1986   // Insert padding bytes to respect alignment.
1987   CharUnits FieldEnd = StackOffset;
1988   StackOffset = FieldEnd.alignTo(WordSize);
1989   if (StackOffset != FieldEnd) {
1990     CharUnits NumBytes = StackOffset - FieldEnd;
1991     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1992     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
1993     FrameFields.push_back(Ty);
1994   }
1995 }
1996 
1997 static bool isArgInAlloca(const ABIArgInfo &Info) {
1998   // Leave ignored and inreg arguments alone.
1999   switch (Info.getKind()) {
2000   case ABIArgInfo::InAlloca:
2001     return true;
2002   case ABIArgInfo::Ignore:
2003   case ABIArgInfo::IndirectAliased:
2004     return false;
2005   case ABIArgInfo::Indirect:
2006   case ABIArgInfo::Direct:
2007   case ABIArgInfo::Extend:
2008     return !Info.getInReg();
2009   case ABIArgInfo::Expand:
2010   case ABIArgInfo::CoerceAndExpand:
2011     // These are aggregate types which are never passed in registers when
2012     // inalloca is involved.
2013     return true;
2014   }
2015   llvm_unreachable("invalid enum");
2016 }
2017 
2018 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
2019   assert(IsWin32StructABI && "inalloca only supported on win32");
2020 
2021   // Build a packed struct type for all of the arguments in memory.
2022   SmallVector<llvm::Type *, 6> FrameFields;
2023 
2024   // The stack alignment is always 4.
2025   CharUnits StackAlign = CharUnits::fromQuantity(4);
2026 
2027   CharUnits StackOffset;
2028   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
2029 
2030   // Put 'this' into the struct before 'sret', if necessary.
2031   bool IsThisCall =
2032       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
2033   ABIArgInfo &Ret = FI.getReturnInfo();
2034   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
2035       isArgInAlloca(I->info)) {
2036     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2037     ++I;
2038   }
2039 
2040   // Put the sret parameter into the inalloca struct if it's in memory.
2041   if (Ret.isIndirect() && !Ret.getInReg()) {
2042     addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType());
2043     // On Windows, the hidden sret parameter is always returned in eax.
2044     Ret.setInAllocaSRet(IsWin32StructABI);
2045   }
2046 
2047   // Skip the 'this' parameter in ecx.
2048   if (IsThisCall)
2049     ++I;
2050 
2051   // Put arguments passed in memory into the struct.
2052   for (; I != E; ++I) {
2053     if (isArgInAlloca(I->info))
2054       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2055   }
2056 
2057   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
2058                                         /*isPacked=*/true),
2059                   StackAlign);
2060 }
2061 
2062 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
2063                                  Address VAListAddr, QualType Ty) const {
2064 
2065   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
2066 
2067   // x86-32 changes the alignment of certain arguments on the stack.
2068   //
2069   // Just messing with TypeInfo like this works because we never pass
2070   // anything indirectly.
2071   TypeInfo.Align = CharUnits::fromQuantity(
2072                 getTypeStackAlignInBytes(Ty, TypeInfo.Align.getQuantity()));
2073 
2074   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
2075                           TypeInfo, CharUnits::fromQuantity(4),
2076                           /*AllowHigherAlign*/ true);
2077 }
2078 
2079 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
2080     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
2081   assert(Triple.getArch() == llvm::Triple::x86);
2082 
2083   switch (Opts.getStructReturnConvention()) {
2084   case CodeGenOptions::SRCK_Default:
2085     break;
2086   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
2087     return false;
2088   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
2089     return true;
2090   }
2091 
2092   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
2093     return true;
2094 
2095   switch (Triple.getOS()) {
2096   case llvm::Triple::DragonFly:
2097   case llvm::Triple::FreeBSD:
2098   case llvm::Triple::OpenBSD:
2099   case llvm::Triple::Win32:
2100     return true;
2101   default:
2102     return false;
2103   }
2104 }
2105 
2106 static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
2107                                  CodeGen::CodeGenModule &CGM) {
2108   if (!FD->hasAttr<AnyX86InterruptAttr>())
2109     return;
2110 
2111   llvm::Function *Fn = cast<llvm::Function>(GV);
2112   Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2113   if (FD->getNumParams() == 0)
2114     return;
2115 
2116   auto PtrTy = cast<PointerType>(FD->getParamDecl(0)->getType());
2117   llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
2118   llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
2119     Fn->getContext(), ByValTy);
2120   Fn->addParamAttr(0, NewAttr);
2121 }
2122 
2123 void X86_32TargetCodeGenInfo::setTargetAttributes(
2124     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2125   if (GV->isDeclaration())
2126     return;
2127   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2128     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2129       llvm::Function *Fn = cast<llvm::Function>(GV);
2130       Fn->addFnAttr("stackrealign");
2131     }
2132 
2133     addX86InterruptAttrs(FD, GV, CGM);
2134   }
2135 }
2136 
2137 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
2138                                                CodeGen::CodeGenFunction &CGF,
2139                                                llvm::Value *Address) const {
2140   CodeGen::CGBuilderTy &Builder = CGF.Builder;
2141 
2142   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
2143 
2144   // 0-7 are the eight integer registers;  the order is different
2145   //   on Darwin (for EH), but the range is the same.
2146   // 8 is %eip.
2147   AssignToArrayRange(Builder, Address, Four8, 0, 8);
2148 
2149   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
2150     // 12-16 are st(0..4).  Not sure why we stop at 4.
2151     // These have size 16, which is sizeof(long double) on
2152     // platforms with 8-byte alignment for that type.
2153     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
2154     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
2155 
2156   } else {
2157     // 9 is %eflags, which doesn't get a size on Darwin for some
2158     // reason.
2159     Builder.CreateAlignedStore(
2160         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
2161                                CharUnits::One());
2162 
2163     // 11-16 are st(0..5).  Not sure why we stop at 5.
2164     // These have size 12, which is sizeof(long double) on
2165     // platforms with 4-byte alignment for that type.
2166     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
2167     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
2168   }
2169 
2170   return false;
2171 }
2172 
2173 //===----------------------------------------------------------------------===//
2174 // X86-64 ABI Implementation
2175 //===----------------------------------------------------------------------===//
2176 
2177 
2178 namespace {
2179 /// The AVX ABI level for X86 targets.
2180 enum class X86AVXABILevel {
2181   None,
2182   AVX,
2183   AVX512
2184 };
2185 
2186 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2187 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2188   switch (AVXLevel) {
2189   case X86AVXABILevel::AVX512:
2190     return 512;
2191   case X86AVXABILevel::AVX:
2192     return 256;
2193   case X86AVXABILevel::None:
2194     return 128;
2195   }
2196   llvm_unreachable("Unknown AVXLevel");
2197 }
2198 
2199 /// X86_64ABIInfo - The X86_64 ABI information.
2200 class X86_64ABIInfo : public SwiftABIInfo {
2201   enum Class {
2202     Integer = 0,
2203     SSE,
2204     SSEUp,
2205     X87,
2206     X87Up,
2207     ComplexX87,
2208     NoClass,
2209     Memory
2210   };
2211 
2212   /// merge - Implement the X86_64 ABI merging algorithm.
2213   ///
2214   /// Merge an accumulating classification \arg Accum with a field
2215   /// classification \arg Field.
2216   ///
2217   /// \param Accum - The accumulating classification. This should
2218   /// always be either NoClass or the result of a previous merge
2219   /// call. In addition, this should never be Memory (the caller
2220   /// should just return Memory for the aggregate).
2221   static Class merge(Class Accum, Class Field);
2222 
2223   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2224   ///
2225   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2226   /// final MEMORY or SSE classes when necessary.
2227   ///
2228   /// \param AggregateSize - The size of the current aggregate in
2229   /// the classification process.
2230   ///
2231   /// \param Lo - The classification for the parts of the type
2232   /// residing in the low word of the containing object.
2233   ///
2234   /// \param Hi - The classification for the parts of the type
2235   /// residing in the higher words of the containing object.
2236   ///
2237   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2238 
2239   /// classify - Determine the x86_64 register classes in which the
2240   /// given type T should be passed.
2241   ///
2242   /// \param Lo - The classification for the parts of the type
2243   /// residing in the low word of the containing object.
2244   ///
2245   /// \param Hi - The classification for the parts of the type
2246   /// residing in the high word of the containing object.
2247   ///
2248   /// \param OffsetBase - The bit offset of this type in the
2249   /// containing object.  Some parameters are classified different
2250   /// depending on whether they straddle an eightbyte boundary.
2251   ///
2252   /// \param isNamedArg - Whether the argument in question is a "named"
2253   /// argument, as used in AMD64-ABI 3.5.7.
2254   ///
2255   /// If a word is unused its result will be NoClass; if a type should
2256   /// be passed in Memory then at least the classification of \arg Lo
2257   /// will be Memory.
2258   ///
2259   /// The \arg Lo class will be NoClass iff the argument is ignored.
2260   ///
2261   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2262   /// also be ComplexX87.
2263   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2264                 bool isNamedArg) const;
2265 
2266   llvm::Type *GetByteVectorType(QualType Ty) const;
2267   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2268                                  unsigned IROffset, QualType SourceTy,
2269                                  unsigned SourceOffset) const;
2270   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2271                                      unsigned IROffset, QualType SourceTy,
2272                                      unsigned SourceOffset) const;
2273 
2274   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2275   /// such that the argument will be returned in memory.
2276   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2277 
2278   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2279   /// such that the argument will be passed in memory.
2280   ///
2281   /// \param freeIntRegs - The number of free integer registers remaining
2282   /// available.
2283   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2284 
2285   ABIArgInfo classifyReturnType(QualType RetTy) const;
2286 
2287   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2288                                   unsigned &neededInt, unsigned &neededSSE,
2289                                   bool isNamedArg) const;
2290 
2291   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2292                                        unsigned &NeededSSE) const;
2293 
2294   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2295                                            unsigned &NeededSSE) const;
2296 
2297   bool IsIllegalVectorType(QualType Ty) const;
2298 
2299   /// The 0.98 ABI revision clarified a lot of ambiguities,
2300   /// unfortunately in ways that were not always consistent with
2301   /// certain previous compilers.  In particular, platforms which
2302   /// required strict binary compatibility with older versions of GCC
2303   /// may need to exempt themselves.
2304   bool honorsRevision0_98() const {
2305     return !getTarget().getTriple().isOSDarwin();
2306   }
2307 
2308   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2309   /// classify it as INTEGER (for compatibility with older clang compilers).
2310   bool classifyIntegerMMXAsSSE() const {
2311     // Clang <= 3.8 did not do this.
2312     if (getContext().getLangOpts().getClangABICompat() <=
2313         LangOptions::ClangABI::Ver3_8)
2314       return false;
2315 
2316     const llvm::Triple &Triple = getTarget().getTriple();
2317     if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2318       return false;
2319     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2320       return false;
2321     return true;
2322   }
2323 
2324   // GCC classifies vectors of __int128 as memory.
2325   bool passInt128VectorsInMem() const {
2326     // Clang <= 9.0 did not do this.
2327     if (getContext().getLangOpts().getClangABICompat() <=
2328         LangOptions::ClangABI::Ver9)
2329       return false;
2330 
2331     const llvm::Triple &T = getTarget().getTriple();
2332     return T.isOSLinux() || T.isOSNetBSD();
2333   }
2334 
2335   X86AVXABILevel AVXLevel;
2336   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2337   // 64-bit hardware.
2338   bool Has64BitPointers;
2339 
2340 public:
2341   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
2342       SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2343       Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
2344   }
2345 
2346   bool isPassedUsingAVXType(QualType type) const {
2347     unsigned neededInt, neededSSE;
2348     // The freeIntRegs argument doesn't matter here.
2349     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2350                                            /*isNamedArg*/true);
2351     if (info.isDirect()) {
2352       llvm::Type *ty = info.getCoerceToType();
2353       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2354         return vectorTy->getPrimitiveSizeInBits().getFixedSize() > 128;
2355     }
2356     return false;
2357   }
2358 
2359   void computeInfo(CGFunctionInfo &FI) const override;
2360 
2361   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2362                     QualType Ty) const override;
2363   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2364                       QualType Ty) const override;
2365 
2366   bool has64BitPointers() const {
2367     return Has64BitPointers;
2368   }
2369 
2370   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
2371                                     bool asReturnValue) const override {
2372     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2373   }
2374   bool isSwiftErrorInRegister() const override {
2375     return true;
2376   }
2377 };
2378 
2379 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2380 class WinX86_64ABIInfo : public SwiftABIInfo {
2381 public:
2382   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2383       : SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2384         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2385 
2386   void computeInfo(CGFunctionInfo &FI) const override;
2387 
2388   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2389                     QualType Ty) const override;
2390 
2391   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2392     // FIXME: Assumes vectorcall is in use.
2393     return isX86VectorTypeForVectorCall(getContext(), Ty);
2394   }
2395 
2396   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2397                                          uint64_t NumMembers) const override {
2398     // FIXME: Assumes vectorcall is in use.
2399     return isX86VectorCallAggregateSmallEnough(NumMembers);
2400   }
2401 
2402   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
2403                                     bool asReturnValue) const override {
2404     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2405   }
2406 
2407   bool isSwiftErrorInRegister() const override {
2408     return true;
2409   }
2410 
2411 private:
2412   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2413                       bool IsVectorCall, bool IsRegCall) const;
2414   ABIArgInfo reclassifyHvaArgForVectorCall(QualType Ty, unsigned &FreeSSERegs,
2415                                            const ABIArgInfo &current) const;
2416 
2417   X86AVXABILevel AVXLevel;
2418 
2419   bool IsMingw64;
2420 };
2421 
2422 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2423 public:
2424   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2425       : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {}
2426 
2427   const X86_64ABIInfo &getABIInfo() const {
2428     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2429   }
2430 
2431   /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
2432   /// autoreleaseRV/retainRV and autoreleaseRV/unsafeClaimRV optimizations.
2433   bool markARCOptimizedReturnCallsAsNoTail() const override { return true; }
2434 
2435   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2436     return 7;
2437   }
2438 
2439   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2440                                llvm::Value *Address) const override {
2441     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2442 
2443     // 0-15 are the 16 integer registers.
2444     // 16 is %rip.
2445     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2446     return false;
2447   }
2448 
2449   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2450                                   StringRef Constraint,
2451                                   llvm::Type* Ty) const override {
2452     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2453   }
2454 
2455   bool isNoProtoCallVariadic(const CallArgList &args,
2456                              const FunctionNoProtoType *fnType) const override {
2457     // The default CC on x86-64 sets %al to the number of SSA
2458     // registers used, and GCC sets this when calling an unprototyped
2459     // function, so we override the default behavior.  However, don't do
2460     // that when AVX types are involved: the ABI explicitly states it is
2461     // undefined, and it doesn't work in practice because of how the ABI
2462     // defines varargs anyway.
2463     if (fnType->getCallConv() == CC_C) {
2464       bool HasAVXType = false;
2465       for (CallArgList::const_iterator
2466              it = args.begin(), ie = args.end(); it != ie; ++it) {
2467         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2468           HasAVXType = true;
2469           break;
2470         }
2471       }
2472 
2473       if (!HasAVXType)
2474         return true;
2475     }
2476 
2477     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2478   }
2479 
2480   llvm::Constant *
2481   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2482     unsigned Sig = (0xeb << 0) | // jmp rel8
2483                    (0x06 << 8) | //           .+0x08
2484                    ('v' << 16) |
2485                    ('2' << 24);
2486     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2487   }
2488 
2489   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2490                            CodeGen::CodeGenModule &CGM) const override {
2491     if (GV->isDeclaration())
2492       return;
2493     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2494       if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2495         llvm::Function *Fn = cast<llvm::Function>(GV);
2496         Fn->addFnAttr("stackrealign");
2497       }
2498 
2499       addX86InterruptAttrs(FD, GV, CGM);
2500     }
2501   }
2502 
2503   void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
2504                             const FunctionDecl *Caller,
2505                             const FunctionDecl *Callee,
2506                             const CallArgList &Args) const override;
2507 };
2508 
2509 static void initFeatureMaps(const ASTContext &Ctx,
2510                             llvm::StringMap<bool> &CallerMap,
2511                             const FunctionDecl *Caller,
2512                             llvm::StringMap<bool> &CalleeMap,
2513                             const FunctionDecl *Callee) {
2514   if (CalleeMap.empty() && CallerMap.empty()) {
2515     // The caller is potentially nullptr in the case where the call isn't in a
2516     // function.  In this case, the getFunctionFeatureMap ensures we just get
2517     // the TU level setting (since it cannot be modified by 'target'..
2518     Ctx.getFunctionFeatureMap(CallerMap, Caller);
2519     Ctx.getFunctionFeatureMap(CalleeMap, Callee);
2520   }
2521 }
2522 
2523 static bool checkAVXParamFeature(DiagnosticsEngine &Diag,
2524                                  SourceLocation CallLoc,
2525                                  const llvm::StringMap<bool> &CallerMap,
2526                                  const llvm::StringMap<bool> &CalleeMap,
2527                                  QualType Ty, StringRef Feature,
2528                                  bool IsArgument) {
2529   bool CallerHasFeat = CallerMap.lookup(Feature);
2530   bool CalleeHasFeat = CalleeMap.lookup(Feature);
2531   if (!CallerHasFeat && !CalleeHasFeat)
2532     return Diag.Report(CallLoc, diag::warn_avx_calling_convention)
2533            << IsArgument << Ty << Feature;
2534 
2535   // Mixing calling conventions here is very clearly an error.
2536   if (!CallerHasFeat || !CalleeHasFeat)
2537     return Diag.Report(CallLoc, diag::err_avx_calling_convention)
2538            << IsArgument << Ty << Feature;
2539 
2540   // Else, both caller and callee have the required feature, so there is no need
2541   // to diagnose.
2542   return false;
2543 }
2544 
2545 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx,
2546                           SourceLocation CallLoc,
2547                           const llvm::StringMap<bool> &CallerMap,
2548                           const llvm::StringMap<bool> &CalleeMap, QualType Ty,
2549                           bool IsArgument) {
2550   uint64_t Size = Ctx.getTypeSize(Ty);
2551   if (Size > 256)
2552     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty,
2553                                 "avx512f", IsArgument);
2554 
2555   if (Size > 128)
2556     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx",
2557                                 IsArgument);
2558 
2559   return false;
2560 }
2561 
2562 void X86_64TargetCodeGenInfo::checkFunctionCallABI(
2563     CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller,
2564     const FunctionDecl *Callee, const CallArgList &Args) const {
2565   llvm::StringMap<bool> CallerMap;
2566   llvm::StringMap<bool> CalleeMap;
2567   unsigned ArgIndex = 0;
2568 
2569   // We need to loop through the actual call arguments rather than the the
2570   // function's parameters, in case this variadic.
2571   for (const CallArg &Arg : Args) {
2572     // The "avx" feature changes how vectors >128 in size are passed. "avx512f"
2573     // additionally changes how vectors >256 in size are passed. Like GCC, we
2574     // warn when a function is called with an argument where this will change.
2575     // Unlike GCC, we also error when it is an obvious ABI mismatch, that is,
2576     // the caller and callee features are mismatched.
2577     // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can
2578     // change its ABI with attribute-target after this call.
2579     if (Arg.getType()->isVectorType() &&
2580         CGM.getContext().getTypeSize(Arg.getType()) > 128) {
2581       initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2582       QualType Ty = Arg.getType();
2583       // The CallArg seems to have desugared the type already, so for clearer
2584       // diagnostics, replace it with the type in the FunctionDecl if possible.
2585       if (ArgIndex < Callee->getNumParams())
2586         Ty = Callee->getParamDecl(ArgIndex)->getType();
2587 
2588       if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2589                         CalleeMap, Ty, /*IsArgument*/ true))
2590         return;
2591     }
2592     ++ArgIndex;
2593   }
2594 
2595   // Check return always, as we don't have a good way of knowing in codegen
2596   // whether this value is used, tail-called, etc.
2597   if (Callee->getReturnType()->isVectorType() &&
2598       CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) {
2599     initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2600     checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2601                   CalleeMap, Callee->getReturnType(),
2602                   /*IsArgument*/ false);
2603   }
2604 }
2605 
2606 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2607   // If the argument does not end in .lib, automatically add the suffix.
2608   // If the argument contains a space, enclose it in quotes.
2609   // This matches the behavior of MSVC.
2610   bool Quote = (Lib.find(' ') != StringRef::npos);
2611   std::string ArgStr = Quote ? "\"" : "";
2612   ArgStr += Lib;
2613   if (!Lib.endswith_insensitive(".lib") && !Lib.endswith_insensitive(".a"))
2614     ArgStr += ".lib";
2615   ArgStr += Quote ? "\"" : "";
2616   return ArgStr;
2617 }
2618 
2619 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2620 public:
2621   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2622         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2623         unsigned NumRegisterParameters)
2624     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2625         Win32StructABI, NumRegisterParameters, false) {}
2626 
2627   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2628                            CodeGen::CodeGenModule &CGM) const override;
2629 
2630   void getDependentLibraryOption(llvm::StringRef Lib,
2631                                  llvm::SmallString<24> &Opt) const override {
2632     Opt = "/DEFAULTLIB:";
2633     Opt += qualifyWindowsLibrary(Lib);
2634   }
2635 
2636   void getDetectMismatchOption(llvm::StringRef Name,
2637                                llvm::StringRef Value,
2638                                llvm::SmallString<32> &Opt) const override {
2639     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2640   }
2641 };
2642 
2643 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2644                                           CodeGen::CodeGenModule &CGM) {
2645   if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
2646 
2647     if (CGM.getCodeGenOpts().StackProbeSize != 4096)
2648       Fn->addFnAttr("stack-probe-size",
2649                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2650     if (CGM.getCodeGenOpts().NoStackArgProbe)
2651       Fn->addFnAttr("no-stack-arg-probe");
2652   }
2653 }
2654 
2655 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2656     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2657   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2658   if (GV->isDeclaration())
2659     return;
2660   addStackProbeTargetAttributes(D, GV, CGM);
2661 }
2662 
2663 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2664 public:
2665   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2666                              X86AVXABILevel AVXLevel)
2667       : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {}
2668 
2669   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2670                            CodeGen::CodeGenModule &CGM) const override;
2671 
2672   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2673     return 7;
2674   }
2675 
2676   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2677                                llvm::Value *Address) const override {
2678     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2679 
2680     // 0-15 are the 16 integer registers.
2681     // 16 is %rip.
2682     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2683     return false;
2684   }
2685 
2686   void getDependentLibraryOption(llvm::StringRef Lib,
2687                                  llvm::SmallString<24> &Opt) const override {
2688     Opt = "/DEFAULTLIB:";
2689     Opt += qualifyWindowsLibrary(Lib);
2690   }
2691 
2692   void getDetectMismatchOption(llvm::StringRef Name,
2693                                llvm::StringRef Value,
2694                                llvm::SmallString<32> &Opt) const override {
2695     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2696   }
2697 };
2698 
2699 void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2700     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2701   TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2702   if (GV->isDeclaration())
2703     return;
2704   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2705     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2706       llvm::Function *Fn = cast<llvm::Function>(GV);
2707       Fn->addFnAttr("stackrealign");
2708     }
2709 
2710     addX86InterruptAttrs(FD, GV, CGM);
2711   }
2712 
2713   addStackProbeTargetAttributes(D, GV, CGM);
2714 }
2715 }
2716 
2717 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2718                               Class &Hi) const {
2719   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2720   //
2721   // (a) If one of the classes is Memory, the whole argument is passed in
2722   //     memory.
2723   //
2724   // (b) If X87UP is not preceded by X87, the whole argument is passed in
2725   //     memory.
2726   //
2727   // (c) If the size of the aggregate exceeds two eightbytes and the first
2728   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2729   //     argument is passed in memory. NOTE: This is necessary to keep the
2730   //     ABI working for processors that don't support the __m256 type.
2731   //
2732   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2733   //
2734   // Some of these are enforced by the merging logic.  Others can arise
2735   // only with unions; for example:
2736   //   union { _Complex double; unsigned; }
2737   //
2738   // Note that clauses (b) and (c) were added in 0.98.
2739   //
2740   if (Hi == Memory)
2741     Lo = Memory;
2742   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2743     Lo = Memory;
2744   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2745     Lo = Memory;
2746   if (Hi == SSEUp && Lo != SSE)
2747     Hi = SSE;
2748 }
2749 
2750 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2751   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2752   // classified recursively so that always two fields are
2753   // considered. The resulting class is calculated according to
2754   // the classes of the fields in the eightbyte:
2755   //
2756   // (a) If both classes are equal, this is the resulting class.
2757   //
2758   // (b) If one of the classes is NO_CLASS, the resulting class is
2759   // the other class.
2760   //
2761   // (c) If one of the classes is MEMORY, the result is the MEMORY
2762   // class.
2763   //
2764   // (d) If one of the classes is INTEGER, the result is the
2765   // INTEGER.
2766   //
2767   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2768   // MEMORY is used as class.
2769   //
2770   // (f) Otherwise class SSE is used.
2771 
2772   // Accum should never be memory (we should have returned) or
2773   // ComplexX87 (because this cannot be passed in a structure).
2774   assert((Accum != Memory && Accum != ComplexX87) &&
2775          "Invalid accumulated classification during merge.");
2776   if (Accum == Field || Field == NoClass)
2777     return Accum;
2778   if (Field == Memory)
2779     return Memory;
2780   if (Accum == NoClass)
2781     return Field;
2782   if (Accum == Integer || Field == Integer)
2783     return Integer;
2784   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2785       Accum == X87 || Accum == X87Up)
2786     return Memory;
2787   return SSE;
2788 }
2789 
2790 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
2791                              Class &Lo, Class &Hi, bool isNamedArg) const {
2792   // FIXME: This code can be simplified by introducing a simple value class for
2793   // Class pairs with appropriate constructor methods for the various
2794   // situations.
2795 
2796   // FIXME: Some of the split computations are wrong; unaligned vectors
2797   // shouldn't be passed in registers for example, so there is no chance they
2798   // can straddle an eightbyte. Verify & simplify.
2799 
2800   Lo = Hi = NoClass;
2801 
2802   Class &Current = OffsetBase < 64 ? Lo : Hi;
2803   Current = Memory;
2804 
2805   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2806     BuiltinType::Kind k = BT->getKind();
2807 
2808     if (k == BuiltinType::Void) {
2809       Current = NoClass;
2810     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2811       Lo = Integer;
2812       Hi = Integer;
2813     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2814       Current = Integer;
2815     } else if (k == BuiltinType::Float || k == BuiltinType::Double ||
2816                k == BuiltinType::Float16) {
2817       Current = SSE;
2818     } else if (k == BuiltinType::LongDouble) {
2819       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2820       if (LDF == &llvm::APFloat::IEEEquad()) {
2821         Lo = SSE;
2822         Hi = SSEUp;
2823       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2824         Lo = X87;
2825         Hi = X87Up;
2826       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2827         Current = SSE;
2828       } else
2829         llvm_unreachable("unexpected long double representation!");
2830     }
2831     // FIXME: _Decimal32 and _Decimal64 are SSE.
2832     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2833     return;
2834   }
2835 
2836   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2837     // Classify the underlying integer type.
2838     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2839     return;
2840   }
2841 
2842   if (Ty->hasPointerRepresentation()) {
2843     Current = Integer;
2844     return;
2845   }
2846 
2847   if (Ty->isMemberPointerType()) {
2848     if (Ty->isMemberFunctionPointerType()) {
2849       if (Has64BitPointers) {
2850         // If Has64BitPointers, this is an {i64, i64}, so classify both
2851         // Lo and Hi now.
2852         Lo = Hi = Integer;
2853       } else {
2854         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2855         // straddles an eightbyte boundary, Hi should be classified as well.
2856         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2857         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2858         if (EB_FuncPtr != EB_ThisAdj) {
2859           Lo = Hi = Integer;
2860         } else {
2861           Current = Integer;
2862         }
2863       }
2864     } else {
2865       Current = Integer;
2866     }
2867     return;
2868   }
2869 
2870   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2871     uint64_t Size = getContext().getTypeSize(VT);
2872     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2873       // gcc passes the following as integer:
2874       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2875       // 2 bytes - <2 x char>, <1 x short>
2876       // 1 byte  - <1 x char>
2877       Current = Integer;
2878 
2879       // If this type crosses an eightbyte boundary, it should be
2880       // split.
2881       uint64_t EB_Lo = (OffsetBase) / 64;
2882       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2883       if (EB_Lo != EB_Hi)
2884         Hi = Lo;
2885     } else if (Size == 64) {
2886       QualType ElementType = VT->getElementType();
2887 
2888       // gcc passes <1 x double> in memory. :(
2889       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2890         return;
2891 
2892       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2893       // pass them as integer.  For platforms where clang is the de facto
2894       // platform compiler, we must continue to use integer.
2895       if (!classifyIntegerMMXAsSSE() &&
2896           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2897            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2898            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2899            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2900         Current = Integer;
2901       else
2902         Current = SSE;
2903 
2904       // If this type crosses an eightbyte boundary, it should be
2905       // split.
2906       if (OffsetBase && OffsetBase != 64)
2907         Hi = Lo;
2908     } else if (Size == 128 ||
2909                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2910       QualType ElementType = VT->getElementType();
2911 
2912       // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
2913       if (passInt128VectorsInMem() && Size != 128 &&
2914           (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
2915            ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
2916         return;
2917 
2918       // Arguments of 256-bits are split into four eightbyte chunks. The
2919       // least significant one belongs to class SSE and all the others to class
2920       // SSEUP. The original Lo and Hi design considers that types can't be
2921       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2922       // This design isn't correct for 256-bits, but since there're no cases
2923       // where the upper parts would need to be inspected, avoid adding
2924       // complexity and just consider Hi to match the 64-256 part.
2925       //
2926       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2927       // registers if they are "named", i.e. not part of the "..." of a
2928       // variadic function.
2929       //
2930       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2931       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2932       Lo = SSE;
2933       Hi = SSEUp;
2934     }
2935     return;
2936   }
2937 
2938   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2939     QualType ET = getContext().getCanonicalType(CT->getElementType());
2940 
2941     uint64_t Size = getContext().getTypeSize(Ty);
2942     if (ET->isIntegralOrEnumerationType()) {
2943       if (Size <= 64)
2944         Current = Integer;
2945       else if (Size <= 128)
2946         Lo = Hi = Integer;
2947     } else if (ET->isFloat16Type() || ET == getContext().FloatTy) {
2948       Current = SSE;
2949     } else if (ET == getContext().DoubleTy) {
2950       Lo = Hi = SSE;
2951     } else if (ET == getContext().LongDoubleTy) {
2952       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2953       if (LDF == &llvm::APFloat::IEEEquad())
2954         Current = Memory;
2955       else if (LDF == &llvm::APFloat::x87DoubleExtended())
2956         Current = ComplexX87;
2957       else if (LDF == &llvm::APFloat::IEEEdouble())
2958         Lo = Hi = SSE;
2959       else
2960         llvm_unreachable("unexpected long double representation!");
2961     }
2962 
2963     // If this complex type crosses an eightbyte boundary then it
2964     // should be split.
2965     uint64_t EB_Real = (OffsetBase) / 64;
2966     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2967     if (Hi == NoClass && EB_Real != EB_Imag)
2968       Hi = Lo;
2969 
2970     return;
2971   }
2972 
2973   if (const auto *EITy = Ty->getAs<ExtIntType>()) {
2974     if (EITy->getNumBits() <= 64)
2975       Current = Integer;
2976     else if (EITy->getNumBits() <= 128)
2977       Lo = Hi = Integer;
2978     // Larger values need to get passed in memory.
2979     return;
2980   }
2981 
2982   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2983     // Arrays are treated like structures.
2984 
2985     uint64_t Size = getContext().getTypeSize(Ty);
2986 
2987     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2988     // than eight eightbytes, ..., it has class MEMORY.
2989     if (Size > 512)
2990       return;
2991 
2992     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2993     // fields, it has class MEMORY.
2994     //
2995     // Only need to check alignment of array base.
2996     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2997       return;
2998 
2999     // Otherwise implement simplified merge. We could be smarter about
3000     // this, but it isn't worth it and would be harder to verify.
3001     Current = NoClass;
3002     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
3003     uint64_t ArraySize = AT->getSize().getZExtValue();
3004 
3005     // The only case a 256-bit wide vector could be used is when the array
3006     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
3007     // to work for sizes wider than 128, early check and fallback to memory.
3008     //
3009     if (Size > 128 &&
3010         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
3011       return;
3012 
3013     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
3014       Class FieldLo, FieldHi;
3015       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
3016       Lo = merge(Lo, FieldLo);
3017       Hi = merge(Hi, FieldHi);
3018       if (Lo == Memory || Hi == Memory)
3019         break;
3020     }
3021 
3022     postMerge(Size, Lo, Hi);
3023     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
3024     return;
3025   }
3026 
3027   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3028     uint64_t Size = getContext().getTypeSize(Ty);
3029 
3030     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3031     // than eight eightbytes, ..., it has class MEMORY.
3032     if (Size > 512)
3033       return;
3034 
3035     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
3036     // copy constructor or a non-trivial destructor, it is passed by invisible
3037     // reference.
3038     if (getRecordArgABI(RT, getCXXABI()))
3039       return;
3040 
3041     const RecordDecl *RD = RT->getDecl();
3042 
3043     // Assume variable sized types are passed in memory.
3044     if (RD->hasFlexibleArrayMember())
3045       return;
3046 
3047     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3048 
3049     // Reset Lo class, this will be recomputed.
3050     Current = NoClass;
3051 
3052     // If this is a C++ record, classify the bases first.
3053     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3054       for (const auto &I : CXXRD->bases()) {
3055         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3056                "Unexpected base class!");
3057         const auto *Base =
3058             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3059 
3060         // Classify this field.
3061         //
3062         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
3063         // single eightbyte, each is classified separately. Each eightbyte gets
3064         // initialized to class NO_CLASS.
3065         Class FieldLo, FieldHi;
3066         uint64_t Offset =
3067           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
3068         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
3069         Lo = merge(Lo, FieldLo);
3070         Hi = merge(Hi, FieldHi);
3071         if (Lo == Memory || Hi == Memory) {
3072           postMerge(Size, Lo, Hi);
3073           return;
3074         }
3075       }
3076     }
3077 
3078     // Classify the fields one at a time, merging the results.
3079     unsigned idx = 0;
3080     bool UseClang11Compat = getContext().getLangOpts().getClangABICompat() <=
3081                                 LangOptions::ClangABI::Ver11 ||
3082                             getContext().getTargetInfo().getTriple().isPS4();
3083     bool IsUnion = RT->isUnionType() && !UseClang11Compat;
3084 
3085     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3086            i != e; ++i, ++idx) {
3087       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3088       bool BitField = i->isBitField();
3089 
3090       // Ignore padding bit-fields.
3091       if (BitField && i->isUnnamedBitfield())
3092         continue;
3093 
3094       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
3095       // eight eightbytes, or it contains unaligned fields, it has class MEMORY.
3096       //
3097       // The only case a 256-bit or a 512-bit wide vector could be used is when
3098       // the struct contains a single 256-bit or 512-bit element. Early check
3099       // and fallback to memory.
3100       //
3101       // FIXME: Extended the Lo and Hi logic properly to work for size wider
3102       // than 128.
3103       if (Size > 128 &&
3104           ((!IsUnion && Size != getContext().getTypeSize(i->getType())) ||
3105            Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
3106         Lo = Memory;
3107         postMerge(Size, Lo, Hi);
3108         return;
3109       }
3110       // Note, skip this test for bit-fields, see below.
3111       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
3112         Lo = Memory;
3113         postMerge(Size, Lo, Hi);
3114         return;
3115       }
3116 
3117       // Classify this field.
3118       //
3119       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
3120       // exceeds a single eightbyte, each is classified
3121       // separately. Each eightbyte gets initialized to class
3122       // NO_CLASS.
3123       Class FieldLo, FieldHi;
3124 
3125       // Bit-fields require special handling, they do not force the
3126       // structure to be passed in memory even if unaligned, and
3127       // therefore they can straddle an eightbyte.
3128       if (BitField) {
3129         assert(!i->isUnnamedBitfield());
3130         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3131         uint64_t Size = i->getBitWidthValue(getContext());
3132 
3133         uint64_t EB_Lo = Offset / 64;
3134         uint64_t EB_Hi = (Offset + Size - 1) / 64;
3135 
3136         if (EB_Lo) {
3137           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
3138           FieldLo = NoClass;
3139           FieldHi = Integer;
3140         } else {
3141           FieldLo = Integer;
3142           FieldHi = EB_Hi ? Integer : NoClass;
3143         }
3144       } else
3145         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
3146       Lo = merge(Lo, FieldLo);
3147       Hi = merge(Hi, FieldHi);
3148       if (Lo == Memory || Hi == Memory)
3149         break;
3150     }
3151 
3152     postMerge(Size, Lo, Hi);
3153   }
3154 }
3155 
3156 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
3157   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3158   // place naturally.
3159   if (!isAggregateTypeForABI(Ty)) {
3160     // Treat an enum type as its underlying type.
3161     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3162       Ty = EnumTy->getDecl()->getIntegerType();
3163 
3164     if (Ty->isExtIntType())
3165       return getNaturalAlignIndirect(Ty);
3166 
3167     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3168                                               : ABIArgInfo::getDirect());
3169   }
3170 
3171   return getNaturalAlignIndirect(Ty);
3172 }
3173 
3174 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
3175   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
3176     uint64_t Size = getContext().getTypeSize(VecTy);
3177     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
3178     if (Size <= 64 || Size > LargestVector)
3179       return true;
3180     QualType EltTy = VecTy->getElementType();
3181     if (passInt128VectorsInMem() &&
3182         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
3183          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
3184       return true;
3185   }
3186 
3187   return false;
3188 }
3189 
3190 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
3191                                             unsigned freeIntRegs) const {
3192   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3193   // place naturally.
3194   //
3195   // This assumption is optimistic, as there could be free registers available
3196   // when we need to pass this argument in memory, and LLVM could try to pass
3197   // the argument in the free register. This does not seem to happen currently,
3198   // but this code would be much safer if we could mark the argument with
3199   // 'onstack'. See PR12193.
3200   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
3201       !Ty->isExtIntType()) {
3202     // Treat an enum type as its underlying type.
3203     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3204       Ty = EnumTy->getDecl()->getIntegerType();
3205 
3206     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3207                                               : ABIArgInfo::getDirect());
3208   }
3209 
3210   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3211     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3212 
3213   // Compute the byval alignment. We specify the alignment of the byval in all
3214   // cases so that the mid-level optimizer knows the alignment of the byval.
3215   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
3216 
3217   // Attempt to avoid passing indirect results using byval when possible. This
3218   // is important for good codegen.
3219   //
3220   // We do this by coercing the value into a scalar type which the backend can
3221   // handle naturally (i.e., without using byval).
3222   //
3223   // For simplicity, we currently only do this when we have exhausted all of the
3224   // free integer registers. Doing this when there are free integer registers
3225   // would require more care, as we would have to ensure that the coerced value
3226   // did not claim the unused register. That would require either reording the
3227   // arguments to the function (so that any subsequent inreg values came first),
3228   // or only doing this optimization when there were no following arguments that
3229   // might be inreg.
3230   //
3231   // We currently expect it to be rare (particularly in well written code) for
3232   // arguments to be passed on the stack when there are still free integer
3233   // registers available (this would typically imply large structs being passed
3234   // by value), so this seems like a fair tradeoff for now.
3235   //
3236   // We can revisit this if the backend grows support for 'onstack' parameter
3237   // attributes. See PR12193.
3238   if (freeIntRegs == 0) {
3239     uint64_t Size = getContext().getTypeSize(Ty);
3240 
3241     // If this type fits in an eightbyte, coerce it into the matching integral
3242     // type, which will end up on the stack (with alignment 8).
3243     if (Align == 8 && Size <= 64)
3244       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3245                                                           Size));
3246   }
3247 
3248   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
3249 }
3250 
3251 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3252 /// register. Pick an LLVM IR type that will be passed as a vector register.
3253 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3254   // Wrapper structs/arrays that only contain vectors are passed just like
3255   // vectors; strip them off if present.
3256   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3257     Ty = QualType(InnerTy, 0);
3258 
3259   llvm::Type *IRType = CGT.ConvertType(Ty);
3260   if (isa<llvm::VectorType>(IRType)) {
3261     // Don't pass vXi128 vectors in their native type, the backend can't
3262     // legalize them.
3263     if (passInt128VectorsInMem() &&
3264         cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
3265       // Use a vXi64 vector.
3266       uint64_t Size = getContext().getTypeSize(Ty);
3267       return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3268                                         Size / 64);
3269     }
3270 
3271     return IRType;
3272   }
3273 
3274   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3275     return IRType;
3276 
3277   // We couldn't find the preferred IR vector type for 'Ty'.
3278   uint64_t Size = getContext().getTypeSize(Ty);
3279   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3280 
3281 
3282   // Return a LLVM IR vector type based on the size of 'Ty'.
3283   return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3284                                     Size / 64);
3285 }
3286 
3287 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3288 /// is known to either be off the end of the specified type or being in
3289 /// alignment padding.  The user type specified is known to be at most 128 bits
3290 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3291 /// classification that put one of the two halves in the INTEGER class.
3292 ///
3293 /// It is conservatively correct to return false.
3294 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3295                                   unsigned EndBit, ASTContext &Context) {
3296   // If the bytes being queried are off the end of the type, there is no user
3297   // data hiding here.  This handles analysis of builtins, vectors and other
3298   // types that don't contain interesting padding.
3299   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3300   if (TySize <= StartBit)
3301     return true;
3302 
3303   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3304     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3305     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3306 
3307     // Check each element to see if the element overlaps with the queried range.
3308     for (unsigned i = 0; i != NumElts; ++i) {
3309       // If the element is after the span we care about, then we're done..
3310       unsigned EltOffset = i*EltSize;
3311       if (EltOffset >= EndBit) break;
3312 
3313       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3314       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3315                                  EndBit-EltOffset, Context))
3316         return false;
3317     }
3318     // If it overlaps no elements, then it is safe to process as padding.
3319     return true;
3320   }
3321 
3322   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3323     const RecordDecl *RD = RT->getDecl();
3324     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3325 
3326     // If this is a C++ record, check the bases first.
3327     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3328       for (const auto &I : CXXRD->bases()) {
3329         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3330                "Unexpected base class!");
3331         const auto *Base =
3332             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3333 
3334         // If the base is after the span we care about, ignore it.
3335         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3336         if (BaseOffset >= EndBit) continue;
3337 
3338         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3339         if (!BitsContainNoUserData(I.getType(), BaseStart,
3340                                    EndBit-BaseOffset, Context))
3341           return false;
3342       }
3343     }
3344 
3345     // Verify that no field has data that overlaps the region of interest.  Yes
3346     // this could be sped up a lot by being smarter about queried fields,
3347     // however we're only looking at structs up to 16 bytes, so we don't care
3348     // much.
3349     unsigned idx = 0;
3350     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3351          i != e; ++i, ++idx) {
3352       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3353 
3354       // If we found a field after the region we care about, then we're done.
3355       if (FieldOffset >= EndBit) break;
3356 
3357       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3358       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3359                                  Context))
3360         return false;
3361     }
3362 
3363     // If nothing in this record overlapped the area of interest, then we're
3364     // clean.
3365     return true;
3366   }
3367 
3368   return false;
3369 }
3370 
3371 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3372 /// float member at the specified offset.  For example, {int,{float}} has a
3373 /// float at offset 4.  It is conservatively correct for this routine to return
3374 /// false.
3375 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
3376                                   const llvm::DataLayout &TD) {
3377   // Base case if we find a float.
3378   if (IROffset == 0 && IRType->isFloatTy())
3379     return true;
3380 
3381   // If this is a struct, recurse into the field at the specified offset.
3382   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3383     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3384     unsigned Elt = SL->getElementContainingOffset(IROffset);
3385     IROffset -= SL->getElementOffset(Elt);
3386     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3387   }
3388 
3389   // If this is an array, recurse into the field at the specified offset.
3390   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3391     llvm::Type *EltTy = ATy->getElementType();
3392     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3393     IROffset -= IROffset/EltSize*EltSize;
3394     return ContainsFloatAtOffset(EltTy, IROffset, TD);
3395   }
3396 
3397   return false;
3398 }
3399 
3400 /// ContainsHalfAtOffset - Return true if the specified LLVM IR type has a
3401 /// half member at the specified offset.  For example, {int,{half}} has a
3402 /// half at offset 4.  It is conservatively correct for this routine to return
3403 /// false.
3404 /// FIXME: Merge with ContainsFloatAtOffset
3405 static bool ContainsHalfAtOffset(llvm::Type *IRType, unsigned IROffset,
3406                                  const llvm::DataLayout &TD) {
3407   // Base case if we find a float.
3408   if (IROffset == 0 && IRType->isHalfTy())
3409     return true;
3410 
3411   // If this is a struct, recurse into the field at the specified offset.
3412   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3413     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3414     unsigned Elt = SL->getElementContainingOffset(IROffset);
3415     IROffset -= SL->getElementOffset(Elt);
3416     return ContainsHalfAtOffset(STy->getElementType(Elt), IROffset, TD);
3417   }
3418 
3419   // If this is an array, recurse into the field at the specified offset.
3420   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3421     llvm::Type *EltTy = ATy->getElementType();
3422     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3423     IROffset -= IROffset / EltSize * EltSize;
3424     return ContainsHalfAtOffset(EltTy, IROffset, TD);
3425   }
3426 
3427   return false;
3428 }
3429 
3430 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3431 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3432 llvm::Type *X86_64ABIInfo::
3433 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3434                    QualType SourceTy, unsigned SourceOffset) const {
3435   // If the high 32 bits are not used, we have three choices. Single half,
3436   // single float or two halfs.
3437   if (BitsContainNoUserData(SourceTy, SourceOffset * 8 + 32,
3438                             SourceOffset * 8 + 64, getContext())) {
3439     if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()))
3440       return llvm::Type::getFloatTy(getVMContext());
3441     if (ContainsHalfAtOffset(IRType, IROffset + 2, getDataLayout()))
3442       return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()),
3443                                         2);
3444 
3445     return llvm::Type::getHalfTy(getVMContext());
3446   }
3447 
3448   // We want to pass as <2 x float> if the LLVM IR type contains a float at
3449   // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
3450   // case.
3451   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3452       ContainsFloatAtOffset(IRType, IROffset + 4, getDataLayout()))
3453     return llvm::FixedVectorType::get(llvm::Type::getFloatTy(getVMContext()),
3454                                       2);
3455 
3456   // We want to pass as <4 x half> if the LLVM IR type contains a half at
3457   // offset+0, +2, +4. Walk the LLVM IR type to find out if this is the case.
3458   if (ContainsHalfAtOffset(IRType, IROffset, getDataLayout()) &&
3459       ContainsHalfAtOffset(IRType, IROffset + 2, getDataLayout()) &&
3460       ContainsHalfAtOffset(IRType, IROffset + 4, getDataLayout()))
3461     return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()), 4);
3462 
3463   // We want to pass as <4 x half> if the LLVM IR type contains a mix of float
3464   // and half.
3465   // FIXME: Do we have a better representation for the mixed type?
3466   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) ||
3467       ContainsFloatAtOffset(IRType, IROffset + 4, getDataLayout()))
3468     return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()), 4);
3469 
3470   return llvm::Type::getDoubleTy(getVMContext());
3471 }
3472 
3473 
3474 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3475 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3476 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3477 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3478 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3479 /// etc).
3480 ///
3481 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3482 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3483 /// the 8-byte value references.  PrefType may be null.
3484 ///
3485 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3486 /// an offset into this that we're processing (which is always either 0 or 8).
3487 ///
3488 llvm::Type *X86_64ABIInfo::
3489 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3490                        QualType SourceTy, unsigned SourceOffset) const {
3491   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3492   // returning an 8-byte unit starting with it.  See if we can safely use it.
3493   if (IROffset == 0) {
3494     // Pointers and int64's always fill the 8-byte unit.
3495     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3496         IRType->isIntegerTy(64))
3497       return IRType;
3498 
3499     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3500     // goodness in the source type is just tail padding.  This is allowed to
3501     // kick in for struct {double,int} on the int, but not on
3502     // struct{double,int,int} because we wouldn't return the second int.  We
3503     // have to do this analysis on the source type because we can't depend on
3504     // unions being lowered a specific way etc.
3505     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3506         IRType->isIntegerTy(32) ||
3507         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3508       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3509           cast<llvm::IntegerType>(IRType)->getBitWidth();
3510 
3511       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3512                                 SourceOffset*8+64, getContext()))
3513         return IRType;
3514     }
3515   }
3516 
3517   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3518     // If this is a struct, recurse into the field at the specified offset.
3519     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3520     if (IROffset < SL->getSizeInBytes()) {
3521       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3522       IROffset -= SL->getElementOffset(FieldIdx);
3523 
3524       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3525                                     SourceTy, SourceOffset);
3526     }
3527   }
3528 
3529   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3530     llvm::Type *EltTy = ATy->getElementType();
3531     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3532     unsigned EltOffset = IROffset/EltSize*EltSize;
3533     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3534                                   SourceOffset);
3535   }
3536 
3537   // Okay, we don't have any better idea of what to pass, so we pass this in an
3538   // integer register that isn't too big to fit the rest of the struct.
3539   unsigned TySizeInBytes =
3540     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3541 
3542   assert(TySizeInBytes != SourceOffset && "Empty field?");
3543 
3544   // It is always safe to classify this as an integer type up to i64 that
3545   // isn't larger than the structure.
3546   return llvm::IntegerType::get(getVMContext(),
3547                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3548 }
3549 
3550 
3551 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3552 /// be used as elements of a two register pair to pass or return, return a
3553 /// first class aggregate to represent them.  For example, if the low part of
3554 /// a by-value argument should be passed as i32* and the high part as float,
3555 /// return {i32*, float}.
3556 static llvm::Type *
3557 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3558                            const llvm::DataLayout &TD) {
3559   // In order to correctly satisfy the ABI, we need to the high part to start
3560   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3561   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3562   // the second element at offset 8.  Check for this:
3563   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3564   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3565   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3566   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3567 
3568   // To handle this, we have to increase the size of the low part so that the
3569   // second element will start at an 8 byte offset.  We can't increase the size
3570   // of the second element because it might make us access off the end of the
3571   // struct.
3572   if (HiStart != 8) {
3573     // There are usually two sorts of types the ABI generation code can produce
3574     // for the low part of a pair that aren't 8 bytes in size: half, float or
3575     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3576     // NaCl).
3577     // Promote these to a larger type.
3578     if (Lo->isHalfTy() || Lo->isFloatTy())
3579       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3580     else {
3581       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3582              && "Invalid/unknown lo type");
3583       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3584     }
3585   }
3586 
3587   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3588 
3589   // Verify that the second element is at an 8-byte offset.
3590   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3591          "Invalid x86-64 argument pair!");
3592   return Result;
3593 }
3594 
3595 ABIArgInfo X86_64ABIInfo::
3596 classifyReturnType(QualType RetTy) const {
3597   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3598   // classification algorithm.
3599   X86_64ABIInfo::Class Lo, Hi;
3600   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3601 
3602   // Check some invariants.
3603   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3604   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3605 
3606   llvm::Type *ResType = nullptr;
3607   switch (Lo) {
3608   case NoClass:
3609     if (Hi == NoClass)
3610       return ABIArgInfo::getIgnore();
3611     // If the low part is just padding, it takes no register, leave ResType
3612     // null.
3613     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3614            "Unknown missing lo part");
3615     break;
3616 
3617   case SSEUp:
3618   case X87Up:
3619     llvm_unreachable("Invalid classification for lo word.");
3620 
3621     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3622     // hidden argument.
3623   case Memory:
3624     return getIndirectReturnResult(RetTy);
3625 
3626     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3627     // available register of the sequence %rax, %rdx is used.
3628   case Integer:
3629     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3630 
3631     // If we have a sign or zero extended integer, make sure to return Extend
3632     // so that the parameter gets the right LLVM IR attributes.
3633     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3634       // Treat an enum type as its underlying type.
3635       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3636         RetTy = EnumTy->getDecl()->getIntegerType();
3637 
3638       if (RetTy->isIntegralOrEnumerationType() &&
3639           isPromotableIntegerTypeForABI(RetTy))
3640         return ABIArgInfo::getExtend(RetTy);
3641     }
3642     break;
3643 
3644     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3645     // available SSE register of the sequence %xmm0, %xmm1 is used.
3646   case SSE:
3647     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3648     break;
3649 
3650     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3651     // returned on the X87 stack in %st0 as 80-bit x87 number.
3652   case X87:
3653     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3654     break;
3655 
3656     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3657     // part of the value is returned in %st0 and the imaginary part in
3658     // %st1.
3659   case ComplexX87:
3660     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3661     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3662                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3663     break;
3664   }
3665 
3666   llvm::Type *HighPart = nullptr;
3667   switch (Hi) {
3668     // Memory was handled previously and X87 should
3669     // never occur as a hi class.
3670   case Memory:
3671   case X87:
3672     llvm_unreachable("Invalid classification for hi word.");
3673 
3674   case ComplexX87: // Previously handled.
3675   case NoClass:
3676     break;
3677 
3678   case Integer:
3679     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3680     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3681       return ABIArgInfo::getDirect(HighPart, 8);
3682     break;
3683   case SSE:
3684     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3685     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3686       return ABIArgInfo::getDirect(HighPart, 8);
3687     break;
3688 
3689     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3690     // is passed in the next available eightbyte chunk if the last used
3691     // vector register.
3692     //
3693     // SSEUP should always be preceded by SSE, just widen.
3694   case SSEUp:
3695     assert(Lo == SSE && "Unexpected SSEUp classification.");
3696     ResType = GetByteVectorType(RetTy);
3697     break;
3698 
3699     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3700     // returned together with the previous X87 value in %st0.
3701   case X87Up:
3702     // If X87Up is preceded by X87, we don't need to do
3703     // anything. However, in some cases with unions it may not be
3704     // preceded by X87. In such situations we follow gcc and pass the
3705     // extra bits in an SSE reg.
3706     if (Lo != X87) {
3707       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3708       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3709         return ABIArgInfo::getDirect(HighPart, 8);
3710     }
3711     break;
3712   }
3713 
3714   // If a high part was specified, merge it together with the low part.  It is
3715   // known to pass in the high eightbyte of the result.  We do this by forming a
3716   // first class struct aggregate with the high and low part: {low, high}
3717   if (HighPart)
3718     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3719 
3720   return ABIArgInfo::getDirect(ResType);
3721 }
3722 
3723 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
3724   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3725   bool isNamedArg)
3726   const
3727 {
3728   Ty = useFirstFieldIfTransparentUnion(Ty);
3729 
3730   X86_64ABIInfo::Class Lo, Hi;
3731   classify(Ty, 0, Lo, Hi, isNamedArg);
3732 
3733   // Check some invariants.
3734   // FIXME: Enforce these by construction.
3735   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3736   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3737 
3738   neededInt = 0;
3739   neededSSE = 0;
3740   llvm::Type *ResType = nullptr;
3741   switch (Lo) {
3742   case NoClass:
3743     if (Hi == NoClass)
3744       return ABIArgInfo::getIgnore();
3745     // If the low part is just padding, it takes no register, leave ResType
3746     // null.
3747     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3748            "Unknown missing lo part");
3749     break;
3750 
3751     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3752     // on the stack.
3753   case Memory:
3754 
3755     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3756     // COMPLEX_X87, it is passed in memory.
3757   case X87:
3758   case ComplexX87:
3759     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3760       ++neededInt;
3761     return getIndirectResult(Ty, freeIntRegs);
3762 
3763   case SSEUp:
3764   case X87Up:
3765     llvm_unreachable("Invalid classification for lo word.");
3766 
3767     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3768     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3769     // and %r9 is used.
3770   case Integer:
3771     ++neededInt;
3772 
3773     // Pick an 8-byte type based on the preferred type.
3774     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3775 
3776     // If we have a sign or zero extended integer, make sure to return Extend
3777     // so that the parameter gets the right LLVM IR attributes.
3778     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3779       // Treat an enum type as its underlying type.
3780       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3781         Ty = EnumTy->getDecl()->getIntegerType();
3782 
3783       if (Ty->isIntegralOrEnumerationType() &&
3784           isPromotableIntegerTypeForABI(Ty))
3785         return ABIArgInfo::getExtend(Ty);
3786     }
3787 
3788     break;
3789 
3790     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3791     // available SSE register is used, the registers are taken in the
3792     // order from %xmm0 to %xmm7.
3793   case SSE: {
3794     llvm::Type *IRType = CGT.ConvertType(Ty);
3795     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3796     ++neededSSE;
3797     break;
3798   }
3799   }
3800 
3801   llvm::Type *HighPart = nullptr;
3802   switch (Hi) {
3803     // Memory was handled previously, ComplexX87 and X87 should
3804     // never occur as hi classes, and X87Up must be preceded by X87,
3805     // which is passed in memory.
3806   case Memory:
3807   case X87:
3808   case ComplexX87:
3809     llvm_unreachable("Invalid classification for hi word.");
3810 
3811   case NoClass: break;
3812 
3813   case Integer:
3814     ++neededInt;
3815     // Pick an 8-byte type based on the preferred type.
3816     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3817 
3818     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3819       return ABIArgInfo::getDirect(HighPart, 8);
3820     break;
3821 
3822     // X87Up generally doesn't occur here (long double is passed in
3823     // memory), except in situations involving unions.
3824   case X87Up:
3825   case SSE:
3826     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3827 
3828     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3829       return ABIArgInfo::getDirect(HighPart, 8);
3830 
3831     ++neededSSE;
3832     break;
3833 
3834     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3835     // eightbyte is passed in the upper half of the last used SSE
3836     // register.  This only happens when 128-bit vectors are passed.
3837   case SSEUp:
3838     assert(Lo == SSE && "Unexpected SSEUp classification");
3839     ResType = GetByteVectorType(Ty);
3840     break;
3841   }
3842 
3843   // If a high part was specified, merge it together with the low part.  It is
3844   // known to pass in the high eightbyte of the result.  We do this by forming a
3845   // first class struct aggregate with the high and low part: {low, high}
3846   if (HighPart)
3847     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3848 
3849   return ABIArgInfo::getDirect(ResType);
3850 }
3851 
3852 ABIArgInfo
3853 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3854                                              unsigned &NeededSSE) const {
3855   auto RT = Ty->getAs<RecordType>();
3856   assert(RT && "classifyRegCallStructType only valid with struct types");
3857 
3858   if (RT->getDecl()->hasFlexibleArrayMember())
3859     return getIndirectReturnResult(Ty);
3860 
3861   // Sum up bases
3862   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3863     if (CXXRD->isDynamicClass()) {
3864       NeededInt = NeededSSE = 0;
3865       return getIndirectReturnResult(Ty);
3866     }
3867 
3868     for (const auto &I : CXXRD->bases())
3869       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3870               .isIndirect()) {
3871         NeededInt = NeededSSE = 0;
3872         return getIndirectReturnResult(Ty);
3873       }
3874   }
3875 
3876   // Sum up members
3877   for (const auto *FD : RT->getDecl()->fields()) {
3878     if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3879       if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3880               .isIndirect()) {
3881         NeededInt = NeededSSE = 0;
3882         return getIndirectReturnResult(Ty);
3883       }
3884     } else {
3885       unsigned LocalNeededInt, LocalNeededSSE;
3886       if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3887                                LocalNeededSSE, true)
3888               .isIndirect()) {
3889         NeededInt = NeededSSE = 0;
3890         return getIndirectReturnResult(Ty);
3891       }
3892       NeededInt += LocalNeededInt;
3893       NeededSSE += LocalNeededSSE;
3894     }
3895   }
3896 
3897   return ABIArgInfo::getDirect();
3898 }
3899 
3900 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3901                                                     unsigned &NeededInt,
3902                                                     unsigned &NeededSSE) const {
3903 
3904   NeededInt = 0;
3905   NeededSSE = 0;
3906 
3907   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3908 }
3909 
3910 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3911 
3912   const unsigned CallingConv = FI.getCallingConvention();
3913   // It is possible to force Win64 calling convention on any x86_64 target by
3914   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3915   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3916   if (CallingConv == llvm::CallingConv::Win64) {
3917     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3918     Win64ABIInfo.computeInfo(FI);
3919     return;
3920   }
3921 
3922   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3923 
3924   // Keep track of the number of assigned registers.
3925   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3926   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3927   unsigned NeededInt, NeededSSE;
3928 
3929   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3930     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3931         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3932       FI.getReturnInfo() =
3933           classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3934       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3935         FreeIntRegs -= NeededInt;
3936         FreeSSERegs -= NeededSSE;
3937       } else {
3938         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3939       }
3940     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
3941                getContext().getCanonicalType(FI.getReturnType()
3942                                                  ->getAs<ComplexType>()
3943                                                  ->getElementType()) ==
3944                    getContext().LongDoubleTy)
3945       // Complex Long Double Type is passed in Memory when Regcall
3946       // calling convention is used.
3947       FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3948     else
3949       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3950   }
3951 
3952   // If the return value is indirect, then the hidden argument is consuming one
3953   // integer register.
3954   if (FI.getReturnInfo().isIndirect())
3955     --FreeIntRegs;
3956 
3957   // The chain argument effectively gives us another free register.
3958   if (FI.isChainCall())
3959     ++FreeIntRegs;
3960 
3961   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3962   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3963   // get assigned (in left-to-right order) for passing as follows...
3964   unsigned ArgNo = 0;
3965   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3966        it != ie; ++it, ++ArgNo) {
3967     bool IsNamedArg = ArgNo < NumRequiredArgs;
3968 
3969     if (IsRegCall && it->type->isStructureOrClassType())
3970       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3971     else
3972       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3973                                       NeededSSE, IsNamedArg);
3974 
3975     // AMD64-ABI 3.2.3p3: If there are no registers available for any
3976     // eightbyte of an argument, the whole argument is passed on the
3977     // stack. If registers have already been assigned for some
3978     // eightbytes of such an argument, the assignments get reverted.
3979     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3980       FreeIntRegs -= NeededInt;
3981       FreeSSERegs -= NeededSSE;
3982     } else {
3983       it->info = getIndirectResult(it->type, FreeIntRegs);
3984     }
3985   }
3986 }
3987 
3988 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3989                                          Address VAListAddr, QualType Ty) {
3990   Address overflow_arg_area_p =
3991       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
3992   llvm::Value *overflow_arg_area =
3993     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3994 
3995   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3996   // byte boundary if alignment needed by type exceeds 8 byte boundary.
3997   // It isn't stated explicitly in the standard, but in practice we use
3998   // alignment greater than 16 where necessary.
3999   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4000   if (Align > CharUnits::fromQuantity(8)) {
4001     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
4002                                                       Align);
4003   }
4004 
4005   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
4006   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4007   llvm::Value *Res =
4008     CGF.Builder.CreateBitCast(overflow_arg_area,
4009                               llvm::PointerType::getUnqual(LTy));
4010 
4011   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
4012   // l->overflow_arg_area + sizeof(type).
4013   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
4014   // an 8 byte boundary.
4015 
4016   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
4017   llvm::Value *Offset =
4018       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
4019   overflow_arg_area = CGF.Builder.CreateGEP(CGF.Int8Ty, overflow_arg_area,
4020                                             Offset, "overflow_arg_area.next");
4021   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
4022 
4023   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
4024   return Address(Res, Align);
4025 }
4026 
4027 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4028                                  QualType Ty) const {
4029   // Assume that va_list type is correct; should be pointer to LLVM type:
4030   // struct {
4031   //   i32 gp_offset;
4032   //   i32 fp_offset;
4033   //   i8* overflow_arg_area;
4034   //   i8* reg_save_area;
4035   // };
4036   unsigned neededInt, neededSSE;
4037 
4038   Ty = getContext().getCanonicalType(Ty);
4039   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
4040                                        /*isNamedArg*/false);
4041 
4042   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
4043   // in the registers. If not go to step 7.
4044   if (!neededInt && !neededSSE)
4045     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4046 
4047   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
4048   // general purpose registers needed to pass type and num_fp to hold
4049   // the number of floating point registers needed.
4050 
4051   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
4052   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
4053   // l->fp_offset > 304 - num_fp * 16 go to step 7.
4054   //
4055   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
4056   // register save space).
4057 
4058   llvm::Value *InRegs = nullptr;
4059   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
4060   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
4061   if (neededInt) {
4062     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
4063     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
4064     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
4065     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
4066   }
4067 
4068   if (neededSSE) {
4069     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
4070     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
4071     llvm::Value *FitsInFP =
4072       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
4073     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
4074     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
4075   }
4076 
4077   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4078   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4079   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4080   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4081 
4082   // Emit code to load the value if it was passed in registers.
4083 
4084   CGF.EmitBlock(InRegBlock);
4085 
4086   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
4087   // an offset of l->gp_offset and/or l->fp_offset. This may require
4088   // copying to a temporary location in case the parameter is passed
4089   // in different register classes or requires an alignment greater
4090   // than 8 for general purpose registers and 16 for XMM registers.
4091   //
4092   // FIXME: This really results in shameful code when we end up needing to
4093   // collect arguments from different places; often what should result in a
4094   // simple assembling of a structure from scattered addresses has many more
4095   // loads than necessary. Can we clean this up?
4096   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4097   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
4098       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
4099 
4100   Address RegAddr = Address::invalid();
4101   if (neededInt && neededSSE) {
4102     // FIXME: Cleanup.
4103     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
4104     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
4105     Address Tmp = CGF.CreateMemTemp(Ty);
4106     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4107     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
4108     llvm::Type *TyLo = ST->getElementType(0);
4109     llvm::Type *TyHi = ST->getElementType(1);
4110     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
4111            "Unexpected ABI info for mixed regs");
4112     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
4113     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
4114     llvm::Value *GPAddr =
4115         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset);
4116     llvm::Value *FPAddr =
4117         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset);
4118     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
4119     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
4120 
4121     // Copy the first element.
4122     // FIXME: Our choice of alignment here and below is probably pessimistic.
4123     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
4124         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
4125         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
4126     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4127 
4128     // Copy the second element.
4129     V = CGF.Builder.CreateAlignedLoad(
4130         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
4131         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
4132     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4133 
4134     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4135   } else if (neededInt) {
4136     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset),
4137                       CharUnits::fromQuantity(8));
4138     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4139 
4140     // Copy to a temporary if necessary to ensure the appropriate alignment.
4141     auto TInfo = getContext().getTypeInfoInChars(Ty);
4142     uint64_t TySize = TInfo.Width.getQuantity();
4143     CharUnits TyAlign = TInfo.Align;
4144 
4145     // Copy into a temporary if the type is more aligned than the
4146     // register save area.
4147     if (TyAlign.getQuantity() > 8) {
4148       Address Tmp = CGF.CreateMemTemp(Ty);
4149       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
4150       RegAddr = Tmp;
4151     }
4152 
4153   } else if (neededSSE == 1) {
4154     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset),
4155                       CharUnits::fromQuantity(16));
4156     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4157   } else {
4158     assert(neededSSE == 2 && "Invalid number of needed registers!");
4159     // SSE registers are spaced 16 bytes apart in the register save
4160     // area, we need to collect the two eightbytes together.
4161     // The ABI isn't explicit about this, but it seems reasonable
4162     // to assume that the slots are 16-byte aligned, since the stack is
4163     // naturally 16-byte aligned and the prologue is expected to store
4164     // all the SSE registers to the RSA.
4165     Address RegAddrLo = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea,
4166                                                       fp_offset),
4167                                 CharUnits::fromQuantity(16));
4168     Address RegAddrHi =
4169       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
4170                                              CharUnits::fromQuantity(16));
4171     llvm::Type *ST = AI.canHaveCoerceToType()
4172                          ? AI.getCoerceToType()
4173                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
4174     llvm::Value *V;
4175     Address Tmp = CGF.CreateMemTemp(Ty);
4176     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4177     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4178         RegAddrLo, ST->getStructElementType(0)));
4179     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4180     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4181         RegAddrHi, ST->getStructElementType(1)));
4182     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4183 
4184     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4185   }
4186 
4187   // AMD64-ABI 3.5.7p5: Step 5. Set:
4188   // l->gp_offset = l->gp_offset + num_gp * 8
4189   // l->fp_offset = l->fp_offset + num_fp * 16.
4190   if (neededInt) {
4191     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
4192     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
4193                             gp_offset_p);
4194   }
4195   if (neededSSE) {
4196     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
4197     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
4198                             fp_offset_p);
4199   }
4200   CGF.EmitBranch(ContBlock);
4201 
4202   // Emit code to load the value if it was passed in memory.
4203 
4204   CGF.EmitBlock(InMemBlock);
4205   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4206 
4207   // Return the appropriate result.
4208 
4209   CGF.EmitBlock(ContBlock);
4210   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
4211                                  "vaarg.addr");
4212   return ResAddr;
4213 }
4214 
4215 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4216                                    QualType Ty) const {
4217   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4218   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4219   uint64_t Width = getContext().getTypeSize(Ty);
4220   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4221 
4222   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4223                           CGF.getContext().getTypeInfoInChars(Ty),
4224                           CharUnits::fromQuantity(8),
4225                           /*allowHigherAlign*/ false);
4226 }
4227 
4228 ABIArgInfo WinX86_64ABIInfo::reclassifyHvaArgForVectorCall(
4229     QualType Ty, unsigned &FreeSSERegs, const ABIArgInfo &current) const {
4230   const Type *Base = nullptr;
4231   uint64_t NumElts = 0;
4232 
4233   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
4234       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
4235     FreeSSERegs -= NumElts;
4236     return getDirectX86Hva();
4237   }
4238   return current;
4239 }
4240 
4241 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
4242                                       bool IsReturnType, bool IsVectorCall,
4243                                       bool IsRegCall) const {
4244 
4245   if (Ty->isVoidType())
4246     return ABIArgInfo::getIgnore();
4247 
4248   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4249     Ty = EnumTy->getDecl()->getIntegerType();
4250 
4251   TypeInfo Info = getContext().getTypeInfo(Ty);
4252   uint64_t Width = Info.Width;
4253   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
4254 
4255   const RecordType *RT = Ty->getAs<RecordType>();
4256   if (RT) {
4257     if (!IsReturnType) {
4258       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
4259         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4260     }
4261 
4262     if (RT->getDecl()->hasFlexibleArrayMember())
4263       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4264 
4265   }
4266 
4267   const Type *Base = nullptr;
4268   uint64_t NumElts = 0;
4269   // vectorcall adds the concept of a homogenous vector aggregate, similar to
4270   // other targets.
4271   if ((IsVectorCall || IsRegCall) &&
4272       isHomogeneousAggregate(Ty, Base, NumElts)) {
4273     if (IsRegCall) {
4274       if (FreeSSERegs >= NumElts) {
4275         FreeSSERegs -= NumElts;
4276         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
4277           return ABIArgInfo::getDirect();
4278         return ABIArgInfo::getExpand();
4279       }
4280       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4281     } else if (IsVectorCall) {
4282       if (FreeSSERegs >= NumElts &&
4283           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
4284         FreeSSERegs -= NumElts;
4285         return ABIArgInfo::getDirect();
4286       } else if (IsReturnType) {
4287         return ABIArgInfo::getExpand();
4288       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
4289         // HVAs are delayed and reclassified in the 2nd step.
4290         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4291       }
4292     }
4293   }
4294 
4295   if (Ty->isMemberPointerType()) {
4296     // If the member pointer is represented by an LLVM int or ptr, pass it
4297     // directly.
4298     llvm::Type *LLTy = CGT.ConvertType(Ty);
4299     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
4300       return ABIArgInfo::getDirect();
4301   }
4302 
4303   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
4304     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4305     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4306     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4307       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4308 
4309     // Otherwise, coerce it to a small integer.
4310     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4311   }
4312 
4313   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4314     switch (BT->getKind()) {
4315     case BuiltinType::Bool:
4316       // Bool type is always extended to the ABI, other builtin types are not
4317       // extended.
4318       return ABIArgInfo::getExtend(Ty);
4319 
4320     case BuiltinType::LongDouble:
4321       // Mingw64 GCC uses the old 80 bit extended precision floating point
4322       // unit. It passes them indirectly through memory.
4323       if (IsMingw64) {
4324         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4325         if (LDF == &llvm::APFloat::x87DoubleExtended())
4326           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4327       }
4328       break;
4329 
4330     case BuiltinType::Int128:
4331     case BuiltinType::UInt128:
4332       // If it's a parameter type, the normal ABI rule is that arguments larger
4333       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4334       // even though it isn't particularly efficient.
4335       if (!IsReturnType)
4336         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4337 
4338       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4339       // Clang matches them for compatibility.
4340       return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
4341           llvm::Type::getInt64Ty(getVMContext()), 2));
4342 
4343     default:
4344       break;
4345     }
4346   }
4347 
4348   if (Ty->isExtIntType()) {
4349     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4350     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4351     // However, non-power-of-two _ExtInts will be passed as 1,2,4 or 8 bytes
4352     // anyway as long is it fits in them, so we don't have to check the power of
4353     // 2.
4354     if (Width <= 64)
4355       return ABIArgInfo::getDirect();
4356     return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4357   }
4358 
4359   return ABIArgInfo::getDirect();
4360 }
4361 
4362 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4363   const unsigned CC = FI.getCallingConvention();
4364   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4365   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4366 
4367   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4368   // classification rules.
4369   if (CC == llvm::CallingConv::X86_64_SysV) {
4370     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4371     SysVABIInfo.computeInfo(FI);
4372     return;
4373   }
4374 
4375   unsigned FreeSSERegs = 0;
4376   if (IsVectorCall) {
4377     // We can use up to 4 SSE return registers with vectorcall.
4378     FreeSSERegs = 4;
4379   } else if (IsRegCall) {
4380     // RegCall gives us 16 SSE registers.
4381     FreeSSERegs = 16;
4382   }
4383 
4384   if (!getCXXABI().classifyReturnType(FI))
4385     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4386                                   IsVectorCall, IsRegCall);
4387 
4388   if (IsVectorCall) {
4389     // We can use up to 6 SSE register parameters with vectorcall.
4390     FreeSSERegs = 6;
4391   } else if (IsRegCall) {
4392     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4393     FreeSSERegs = 16;
4394   }
4395 
4396   unsigned ArgNum = 0;
4397   unsigned ZeroSSERegs = 0;
4398   for (auto &I : FI.arguments()) {
4399     // Vectorcall in x64 only permits the first 6 arguments to be passed as
4400     // XMM/YMM registers. After the sixth argument, pretend no vector
4401     // registers are left.
4402     unsigned *MaybeFreeSSERegs =
4403         (IsVectorCall && ArgNum >= 6) ? &ZeroSSERegs : &FreeSSERegs;
4404     I.info =
4405         classify(I.type, *MaybeFreeSSERegs, false, IsVectorCall, IsRegCall);
4406     ++ArgNum;
4407   }
4408 
4409   if (IsVectorCall) {
4410     // For vectorcall, assign aggregate HVAs to any free vector registers in a
4411     // second pass.
4412     for (auto &I : FI.arguments())
4413       I.info = reclassifyHvaArgForVectorCall(I.type, FreeSSERegs, I.info);
4414   }
4415 }
4416 
4417 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4418                                     QualType Ty) const {
4419   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4420   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4421   uint64_t Width = getContext().getTypeSize(Ty);
4422   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4423 
4424   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4425                           CGF.getContext().getTypeInfoInChars(Ty),
4426                           CharUnits::fromQuantity(8),
4427                           /*allowHigherAlign*/ false);
4428 }
4429 
4430 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4431                                         llvm::Value *Address, bool Is64Bit,
4432                                         bool IsAIX) {
4433   // This is calculated from the LLVM and GCC tables and verified
4434   // against gcc output.  AFAIK all PPC ABIs use the same encoding.
4435 
4436   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4437 
4438   llvm::IntegerType *i8 = CGF.Int8Ty;
4439   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4440   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4441   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4442 
4443   // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
4444   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
4445 
4446   // 32-63: fp0-31, the 8-byte floating-point registers
4447   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4448 
4449   // 64-67 are various 4-byte or 8-byte special-purpose registers:
4450   // 64: mq
4451   // 65: lr
4452   // 66: ctr
4453   // 67: ap
4454   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
4455 
4456   // 68-76 are various 4-byte special-purpose registers:
4457   // 68-75 cr0-7
4458   // 76: xer
4459   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4460 
4461   // 77-108: v0-31, the 16-byte vector registers
4462   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4463 
4464   // 109: vrsave
4465   // 110: vscr
4466   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
4467 
4468   // AIX does not utilize the rest of the registers.
4469   if (IsAIX)
4470     return false;
4471 
4472   // 111: spe_acc
4473   // 112: spefscr
4474   // 113: sfp
4475   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
4476 
4477   if (!Is64Bit)
4478     return false;
4479 
4480   // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
4481   // or above CPU.
4482   // 64-bit only registers:
4483   // 114: tfhar
4484   // 115: tfiar
4485   // 116: texasr
4486   AssignToArrayRange(Builder, Address, Eight8, 114, 116);
4487 
4488   return false;
4489 }
4490 
4491 // AIX
4492 namespace {
4493 /// AIXABIInfo - The AIX XCOFF ABI information.
4494 class AIXABIInfo : public ABIInfo {
4495   const bool Is64Bit;
4496   const unsigned PtrByteSize;
4497   CharUnits getParamTypeAlignment(QualType Ty) const;
4498 
4499 public:
4500   AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4501       : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
4502 
4503   bool isPromotableTypeForABI(QualType Ty) const;
4504 
4505   ABIArgInfo classifyReturnType(QualType RetTy) const;
4506   ABIArgInfo classifyArgumentType(QualType Ty) const;
4507 
4508   void computeInfo(CGFunctionInfo &FI) const override {
4509     if (!getCXXABI().classifyReturnType(FI))
4510       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4511 
4512     for (auto &I : FI.arguments())
4513       I.info = classifyArgumentType(I.type);
4514   }
4515 
4516   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4517                     QualType Ty) const override;
4518 };
4519 
4520 class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
4521   const bool Is64Bit;
4522 
4523 public:
4524   AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4525       : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
4526         Is64Bit(Is64Bit) {}
4527   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4528     return 1; // r1 is the dedicated stack pointer
4529   }
4530 
4531   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4532                                llvm::Value *Address) const override;
4533 };
4534 } // namespace
4535 
4536 // Return true if the ABI requires Ty to be passed sign- or zero-
4537 // extended to 32/64 bits.
4538 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
4539   // Treat an enum type as its underlying type.
4540   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4541     Ty = EnumTy->getDecl()->getIntegerType();
4542 
4543   // Promotable integer types are required to be promoted by the ABI.
4544   if (Ty->isPromotableIntegerType())
4545     return true;
4546 
4547   if (!Is64Bit)
4548     return false;
4549 
4550   // For 64 bit mode, in addition to the usual promotable integer types, we also
4551   // need to extend all 32-bit types, since the ABI requires promotion to 64
4552   // bits.
4553   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4554     switch (BT->getKind()) {
4555     case BuiltinType::Int:
4556     case BuiltinType::UInt:
4557       return true;
4558     default:
4559       break;
4560     }
4561 
4562   return false;
4563 }
4564 
4565 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
4566   if (RetTy->isAnyComplexType())
4567     return ABIArgInfo::getDirect();
4568 
4569   if (RetTy->isVectorType())
4570     return ABIArgInfo::getDirect();
4571 
4572   if (RetTy->isVoidType())
4573     return ABIArgInfo::getIgnore();
4574 
4575   if (isAggregateTypeForABI(RetTy))
4576     return getNaturalAlignIndirect(RetTy);
4577 
4578   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4579                                         : ABIArgInfo::getDirect());
4580 }
4581 
4582 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
4583   Ty = useFirstFieldIfTransparentUnion(Ty);
4584 
4585   if (Ty->isAnyComplexType())
4586     return ABIArgInfo::getDirect();
4587 
4588   if (Ty->isVectorType())
4589     return ABIArgInfo::getDirect();
4590 
4591   if (isAggregateTypeForABI(Ty)) {
4592     // Records with non-trivial destructors/copy-constructors should not be
4593     // passed by value.
4594     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4595       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4596 
4597     CharUnits CCAlign = getParamTypeAlignment(Ty);
4598     CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
4599 
4600     return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
4601                                    /*Realign*/ TyAlign > CCAlign);
4602   }
4603 
4604   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4605                                      : ABIArgInfo::getDirect());
4606 }
4607 
4608 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
4609   // Complex types are passed just like their elements.
4610   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4611     Ty = CTy->getElementType();
4612 
4613   if (Ty->isVectorType())
4614     return CharUnits::fromQuantity(16);
4615 
4616   // If the structure contains a vector type, the alignment is 16.
4617   if (isRecordWithSIMDVectorType(getContext(), Ty))
4618     return CharUnits::fromQuantity(16);
4619 
4620   return CharUnits::fromQuantity(PtrByteSize);
4621 }
4622 
4623 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4624                               QualType Ty) const {
4625   if (Ty->isAnyComplexType())
4626     llvm::report_fatal_error("complex type is not supported on AIX yet");
4627 
4628   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4629   TypeInfo.Align = getParamTypeAlignment(Ty);
4630 
4631   CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
4632 
4633   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
4634                           SlotSize, /*AllowHigher*/ true);
4635 }
4636 
4637 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
4638     CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
4639   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
4640 }
4641 
4642 // PowerPC-32
4643 namespace {
4644 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4645 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4646   bool IsSoftFloatABI;
4647   bool IsRetSmallStructInRegABI;
4648 
4649   CharUnits getParamTypeAlignment(QualType Ty) const;
4650 
4651 public:
4652   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
4653                      bool RetSmallStructInRegABI)
4654       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
4655         IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
4656 
4657   ABIArgInfo classifyReturnType(QualType RetTy) const;
4658 
4659   void computeInfo(CGFunctionInfo &FI) const override {
4660     if (!getCXXABI().classifyReturnType(FI))
4661       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4662     for (auto &I : FI.arguments())
4663       I.info = classifyArgumentType(I.type);
4664   }
4665 
4666   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4667                     QualType Ty) const override;
4668 };
4669 
4670 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4671 public:
4672   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
4673                          bool RetSmallStructInRegABI)
4674       : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
4675             CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
4676 
4677   static bool isStructReturnInRegABI(const llvm::Triple &Triple,
4678                                      const CodeGenOptions &Opts);
4679 
4680   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4681     // This is recovered from gcc output.
4682     return 1; // r1 is the dedicated stack pointer
4683   }
4684 
4685   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4686                                llvm::Value *Address) const override;
4687 };
4688 }
4689 
4690 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4691   // Complex types are passed just like their elements.
4692   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4693     Ty = CTy->getElementType();
4694 
4695   if (Ty->isVectorType())
4696     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4697                                                                        : 4);
4698 
4699   // For single-element float/vector structs, we consider the whole type
4700   // to have the same alignment requirements as its single element.
4701   const Type *AlignTy = nullptr;
4702   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4703     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4704     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4705         (BT && BT->isFloatingPoint()))
4706       AlignTy = EltType;
4707   }
4708 
4709   if (AlignTy)
4710     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4711   return CharUnits::fromQuantity(4);
4712 }
4713 
4714 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4715   uint64_t Size;
4716 
4717   // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
4718   if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
4719       (Size = getContext().getTypeSize(RetTy)) <= 64) {
4720     // System V ABI (1995), page 3-22, specified:
4721     // > A structure or union whose size is less than or equal to 8 bytes
4722     // > shall be returned in r3 and r4, as if it were first stored in the
4723     // > 8-byte aligned memory area and then the low addressed word were
4724     // > loaded into r3 and the high-addressed word into r4.  Bits beyond
4725     // > the last member of the structure or union are not defined.
4726     //
4727     // GCC for big-endian PPC32 inserts the pad before the first member,
4728     // not "beyond the last member" of the struct.  To stay compatible
4729     // with GCC, we coerce the struct to an integer of the same size.
4730     // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
4731     if (Size == 0)
4732       return ABIArgInfo::getIgnore();
4733     else {
4734       llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
4735       return ABIArgInfo::getDirect(CoerceTy);
4736     }
4737   }
4738 
4739   return DefaultABIInfo::classifyReturnType(RetTy);
4740 }
4741 
4742 // TODO: this implementation is now likely redundant with
4743 // DefaultABIInfo::EmitVAArg.
4744 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4745                                       QualType Ty) const {
4746   if (getTarget().getTriple().isOSDarwin()) {
4747     auto TI = getContext().getTypeInfoInChars(Ty);
4748     TI.Align = getParamTypeAlignment(Ty);
4749 
4750     CharUnits SlotSize = CharUnits::fromQuantity(4);
4751     return emitVoidPtrVAArg(CGF, VAList, Ty,
4752                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4753                             /*AllowHigherAlign=*/true);
4754   }
4755 
4756   const unsigned OverflowLimit = 8;
4757   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4758     // TODO: Implement this. For now ignore.
4759     (void)CTy;
4760     return Address::invalid(); // FIXME?
4761   }
4762 
4763   // struct __va_list_tag {
4764   //   unsigned char gpr;
4765   //   unsigned char fpr;
4766   //   unsigned short reserved;
4767   //   void *overflow_arg_area;
4768   //   void *reg_save_area;
4769   // };
4770 
4771   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4772   bool isInt = !Ty->isFloatingType();
4773   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4774 
4775   // All aggregates are passed indirectly?  That doesn't seem consistent
4776   // with the argument-lowering code.
4777   bool isIndirect = isAggregateTypeForABI(Ty);
4778 
4779   CGBuilderTy &Builder = CGF.Builder;
4780 
4781   // The calling convention either uses 1-2 GPRs or 1 FPR.
4782   Address NumRegsAddr = Address::invalid();
4783   if (isInt || IsSoftFloatABI) {
4784     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4785   } else {
4786     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4787   }
4788 
4789   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4790 
4791   // "Align" the register count when TY is i64.
4792   if (isI64 || (isF64 && IsSoftFloatABI)) {
4793     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4794     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4795   }
4796 
4797   llvm::Value *CC =
4798       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4799 
4800   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4801   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4802   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4803 
4804   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4805 
4806   llvm::Type *DirectTy = CGF.ConvertType(Ty);
4807   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4808 
4809   // Case 1: consume registers.
4810   Address RegAddr = Address::invalid();
4811   {
4812     CGF.EmitBlock(UsingRegs);
4813 
4814     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4815     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4816                       CharUnits::fromQuantity(8));
4817     assert(RegAddr.getElementType() == CGF.Int8Ty);
4818 
4819     // Floating-point registers start after the general-purpose registers.
4820     if (!(isInt || IsSoftFloatABI)) {
4821       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4822                                                    CharUnits::fromQuantity(32));
4823     }
4824 
4825     // Get the address of the saved value by scaling the number of
4826     // registers we've used by the number of
4827     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4828     llvm::Value *RegOffset =
4829       Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4830     RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4831                                             RegAddr.getPointer(), RegOffset),
4832                       RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4833     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4834 
4835     // Increase the used-register count.
4836     NumRegs =
4837       Builder.CreateAdd(NumRegs,
4838                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4839     Builder.CreateStore(NumRegs, NumRegsAddr);
4840 
4841     CGF.EmitBranch(Cont);
4842   }
4843 
4844   // Case 2: consume space in the overflow area.
4845   Address MemAddr = Address::invalid();
4846   {
4847     CGF.EmitBlock(UsingOverflow);
4848 
4849     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4850 
4851     // Everything in the overflow area is rounded up to a size of at least 4.
4852     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4853 
4854     CharUnits Size;
4855     if (!isIndirect) {
4856       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4857       Size = TypeInfo.Width.alignTo(OverflowAreaAlign);
4858     } else {
4859       Size = CGF.getPointerSize();
4860     }
4861 
4862     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4863     Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
4864                          OverflowAreaAlign);
4865     // Round up address of argument to alignment
4866     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4867     if (Align > OverflowAreaAlign) {
4868       llvm::Value *Ptr = OverflowArea.getPointer();
4869       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4870                                                            Align);
4871     }
4872 
4873     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4874 
4875     // Increase the overflow area.
4876     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4877     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4878     CGF.EmitBranch(Cont);
4879   }
4880 
4881   CGF.EmitBlock(Cont);
4882 
4883   // Merge the cases with a phi.
4884   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4885                                 "vaarg.addr");
4886 
4887   // Load the pointer if the argument was passed indirectly.
4888   if (isIndirect) {
4889     Result = Address(Builder.CreateLoad(Result, "aggr"),
4890                      getContext().getTypeAlignInChars(Ty));
4891   }
4892 
4893   return Result;
4894 }
4895 
4896 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
4897     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4898   assert(Triple.isPPC32());
4899 
4900   switch (Opts.getStructReturnConvention()) {
4901   case CodeGenOptions::SRCK_Default:
4902     break;
4903   case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
4904     return false;
4905   case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
4906     return true;
4907   }
4908 
4909   if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
4910     return true;
4911 
4912   return false;
4913 }
4914 
4915 bool
4916 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4917                                                 llvm::Value *Address) const {
4918   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
4919                                      /*IsAIX*/ false);
4920 }
4921 
4922 // PowerPC-64
4923 
4924 namespace {
4925 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4926 class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
4927 public:
4928   enum ABIKind {
4929     ELFv1 = 0,
4930     ELFv2
4931   };
4932 
4933 private:
4934   static const unsigned GPRBits = 64;
4935   ABIKind Kind;
4936   bool IsSoftFloatABI;
4937 
4938 public:
4939   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind,
4940                      bool SoftFloatABI)
4941       : SwiftABIInfo(CGT), Kind(Kind), IsSoftFloatABI(SoftFloatABI) {}
4942 
4943   bool isPromotableTypeForABI(QualType Ty) const;
4944   CharUnits getParamTypeAlignment(QualType Ty) const;
4945 
4946   ABIArgInfo classifyReturnType(QualType RetTy) const;
4947   ABIArgInfo classifyArgumentType(QualType Ty) const;
4948 
4949   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4950   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4951                                          uint64_t Members) const override;
4952 
4953   // TODO: We can add more logic to computeInfo to improve performance.
4954   // Example: For aggregate arguments that fit in a register, we could
4955   // use getDirectInReg (as is done below for structs containing a single
4956   // floating-point value) to avoid pushing them to memory on function
4957   // entry.  This would require changing the logic in PPCISelLowering
4958   // when lowering the parameters in the caller and args in the callee.
4959   void computeInfo(CGFunctionInfo &FI) const override {
4960     if (!getCXXABI().classifyReturnType(FI))
4961       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4962     for (auto &I : FI.arguments()) {
4963       // We rely on the default argument classification for the most part.
4964       // One exception:  An aggregate containing a single floating-point
4965       // or vector item must be passed in a register if one is available.
4966       const Type *T = isSingleElementStruct(I.type, getContext());
4967       if (T) {
4968         const BuiltinType *BT = T->getAs<BuiltinType>();
4969         if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
4970             (BT && BT->isFloatingPoint())) {
4971           QualType QT(T, 0);
4972           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
4973           continue;
4974         }
4975       }
4976       I.info = classifyArgumentType(I.type);
4977     }
4978   }
4979 
4980   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4981                     QualType Ty) const override;
4982 
4983   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
4984                                     bool asReturnValue) const override {
4985     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4986   }
4987 
4988   bool isSwiftErrorInRegister() const override {
4989     return false;
4990   }
4991 };
4992 
4993 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
4994 
4995 public:
4996   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
4997                                PPC64_SVR4_ABIInfo::ABIKind Kind,
4998                                bool SoftFloatABI)
4999       : TargetCodeGenInfo(
5000             std::make_unique<PPC64_SVR4_ABIInfo>(CGT, Kind, SoftFloatABI)) {}
5001 
5002   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5003     // This is recovered from gcc output.
5004     return 1; // r1 is the dedicated stack pointer
5005   }
5006 
5007   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5008                                llvm::Value *Address) const override;
5009 };
5010 
5011 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5012 public:
5013   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
5014 
5015   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5016     // This is recovered from gcc output.
5017     return 1; // r1 is the dedicated stack pointer
5018   }
5019 
5020   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5021                                llvm::Value *Address) const override;
5022 };
5023 
5024 }
5025 
5026 // Return true if the ABI requires Ty to be passed sign- or zero-
5027 // extended to 64 bits.
5028 bool
5029 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
5030   // Treat an enum type as its underlying type.
5031   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5032     Ty = EnumTy->getDecl()->getIntegerType();
5033 
5034   // Promotable integer types are required to be promoted by the ABI.
5035   if (isPromotableIntegerTypeForABI(Ty))
5036     return true;
5037 
5038   // In addition to the usual promotable integer types, we also need to
5039   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
5040   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5041     switch (BT->getKind()) {
5042     case BuiltinType::Int:
5043     case BuiltinType::UInt:
5044       return true;
5045     default:
5046       break;
5047     }
5048 
5049   if (const auto *EIT = Ty->getAs<ExtIntType>())
5050     if (EIT->getNumBits() < 64)
5051       return true;
5052 
5053   return false;
5054 }
5055 
5056 /// isAlignedParamType - Determine whether a type requires 16-byte or
5057 /// higher alignment in the parameter area.  Always returns at least 8.
5058 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
5059   // Complex types are passed just like their elements.
5060   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
5061     Ty = CTy->getElementType();
5062 
5063   // Only vector types of size 16 bytes need alignment (larger types are
5064   // passed via reference, smaller types are not aligned).
5065   if (Ty->isVectorType()) {
5066     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5067   } else if (Ty->isRealFloatingType() &&
5068              &getContext().getFloatTypeSemantics(Ty) ==
5069                  &llvm::APFloat::IEEEquad()) {
5070     // According to ABI document section 'Optional Save Areas': If extended
5071     // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
5072     // format are supported, map them to a single quadword, quadword aligned.
5073     return CharUnits::fromQuantity(16);
5074   }
5075 
5076   // For single-element float/vector structs, we consider the whole type
5077   // to have the same alignment requirements as its single element.
5078   const Type *AlignAsType = nullptr;
5079   const Type *EltType = isSingleElementStruct(Ty, getContext());
5080   if (EltType) {
5081     const BuiltinType *BT = EltType->getAs<BuiltinType>();
5082     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
5083         (BT && BT->isFloatingPoint()))
5084       AlignAsType = EltType;
5085   }
5086 
5087   // Likewise for ELFv2 homogeneous aggregates.
5088   const Type *Base = nullptr;
5089   uint64_t Members = 0;
5090   if (!AlignAsType && Kind == ELFv2 &&
5091       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
5092     AlignAsType = Base;
5093 
5094   // With special case aggregates, only vector base types need alignment.
5095   if (AlignAsType) {
5096     return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
5097   }
5098 
5099   // Otherwise, we only need alignment for any aggregate type that
5100   // has an alignment requirement of >= 16 bytes.
5101   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
5102     return CharUnits::fromQuantity(16);
5103   }
5104 
5105   return CharUnits::fromQuantity(8);
5106 }
5107 
5108 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
5109 /// aggregate.  Base is set to the base element type, and Members is set
5110 /// to the number of base elements.
5111 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
5112                                      uint64_t &Members) const {
5113   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
5114     uint64_t NElements = AT->getSize().getZExtValue();
5115     if (NElements == 0)
5116       return false;
5117     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
5118       return false;
5119     Members *= NElements;
5120   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
5121     const RecordDecl *RD = RT->getDecl();
5122     if (RD->hasFlexibleArrayMember())
5123       return false;
5124 
5125     Members = 0;
5126 
5127     // If this is a C++ record, check the properties of the record such as
5128     // bases and ABI specific restrictions
5129     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5130       if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
5131         return false;
5132 
5133       for (const auto &I : CXXRD->bases()) {
5134         // Ignore empty records.
5135         if (isEmptyRecord(getContext(), I.getType(), true))
5136           continue;
5137 
5138         uint64_t FldMembers;
5139         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
5140           return false;
5141 
5142         Members += FldMembers;
5143       }
5144     }
5145 
5146     for (const auto *FD : RD->fields()) {
5147       // Ignore (non-zero arrays of) empty records.
5148       QualType FT = FD->getType();
5149       while (const ConstantArrayType *AT =
5150              getContext().getAsConstantArrayType(FT)) {
5151         if (AT->getSize().getZExtValue() == 0)
5152           return false;
5153         FT = AT->getElementType();
5154       }
5155       if (isEmptyRecord(getContext(), FT, true))
5156         continue;
5157 
5158       // For compatibility with GCC, ignore empty bitfields in C++ mode.
5159       if (getContext().getLangOpts().CPlusPlus &&
5160           FD->isZeroLengthBitField(getContext()))
5161         continue;
5162 
5163       uint64_t FldMembers;
5164       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
5165         return false;
5166 
5167       Members = (RD->isUnion() ?
5168                  std::max(Members, FldMembers) : Members + FldMembers);
5169     }
5170 
5171     if (!Base)
5172       return false;
5173 
5174     // Ensure there is no padding.
5175     if (getContext().getTypeSize(Base) * Members !=
5176         getContext().getTypeSize(Ty))
5177       return false;
5178   } else {
5179     Members = 1;
5180     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
5181       Members = 2;
5182       Ty = CT->getElementType();
5183     }
5184 
5185     // Most ABIs only support float, double, and some vector type widths.
5186     if (!isHomogeneousAggregateBaseType(Ty))
5187       return false;
5188 
5189     // The base type must be the same for all members.  Types that
5190     // agree in both total size and mode (float vs. vector) are
5191     // treated as being equivalent here.
5192     const Type *TyPtr = Ty.getTypePtr();
5193     if (!Base) {
5194       Base = TyPtr;
5195       // If it's a non-power-of-2 vector, its size is already a power-of-2,
5196       // so make sure to widen it explicitly.
5197       if (const VectorType *VT = Base->getAs<VectorType>()) {
5198         QualType EltTy = VT->getElementType();
5199         unsigned NumElements =
5200             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
5201         Base = getContext()
5202                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
5203                    .getTypePtr();
5204       }
5205     }
5206 
5207     if (Base->isVectorType() != TyPtr->isVectorType() ||
5208         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
5209       return false;
5210   }
5211   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
5212 }
5213 
5214 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5215   // Homogeneous aggregates for ELFv2 must have base types of float,
5216   // double, long double, or 128-bit vectors.
5217   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5218     if (BT->getKind() == BuiltinType::Float ||
5219         BT->getKind() == BuiltinType::Double ||
5220         BT->getKind() == BuiltinType::LongDouble ||
5221         (getContext().getTargetInfo().hasFloat128Type() &&
5222           (BT->getKind() == BuiltinType::Float128))) {
5223       if (IsSoftFloatABI)
5224         return false;
5225       return true;
5226     }
5227   }
5228   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5229     if (getContext().getTypeSize(VT) == 128)
5230       return true;
5231   }
5232   return false;
5233 }
5234 
5235 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
5236     const Type *Base, uint64_t Members) const {
5237   // Vector and fp128 types require one register, other floating point types
5238   // require one or two registers depending on their size.
5239   uint32_t NumRegs =
5240       ((getContext().getTargetInfo().hasFloat128Type() &&
5241           Base->isFloat128Type()) ||
5242         Base->isVectorType()) ? 1
5243                               : (getContext().getTypeSize(Base) + 63) / 64;
5244 
5245   // Homogeneous Aggregates may occupy at most 8 registers.
5246   return Members * NumRegs <= 8;
5247 }
5248 
5249 ABIArgInfo
5250 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
5251   Ty = useFirstFieldIfTransparentUnion(Ty);
5252 
5253   if (Ty->isAnyComplexType())
5254     return ABIArgInfo::getDirect();
5255 
5256   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
5257   // or via reference (larger than 16 bytes).
5258   if (Ty->isVectorType()) {
5259     uint64_t Size = getContext().getTypeSize(Ty);
5260     if (Size > 128)
5261       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5262     else if (Size < 128) {
5263       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5264       return ABIArgInfo::getDirect(CoerceTy);
5265     }
5266   }
5267 
5268   if (const auto *EIT = Ty->getAs<ExtIntType>())
5269     if (EIT->getNumBits() > 128)
5270       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
5271 
5272   if (isAggregateTypeForABI(Ty)) {
5273     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5274       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5275 
5276     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
5277     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5278 
5279     // ELFv2 homogeneous aggregates are passed as array types.
5280     const Type *Base = nullptr;
5281     uint64_t Members = 0;
5282     if (Kind == ELFv2 &&
5283         isHomogeneousAggregate(Ty, Base, Members)) {
5284       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5285       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5286       return ABIArgInfo::getDirect(CoerceTy);
5287     }
5288 
5289     // If an aggregate may end up fully in registers, we do not
5290     // use the ByVal method, but pass the aggregate as array.
5291     // This is usually beneficial since we avoid forcing the
5292     // back-end to store the argument to memory.
5293     uint64_t Bits = getContext().getTypeSize(Ty);
5294     if (Bits > 0 && Bits <= 8 * GPRBits) {
5295       llvm::Type *CoerceTy;
5296 
5297       // Types up to 8 bytes are passed as integer type (which will be
5298       // properly aligned in the argument save area doubleword).
5299       if (Bits <= GPRBits)
5300         CoerceTy =
5301             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5302       // Larger types are passed as arrays, with the base type selected
5303       // according to the required alignment in the save area.
5304       else {
5305         uint64_t RegBits = ABIAlign * 8;
5306         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
5307         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
5308         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
5309       }
5310 
5311       return ABIArgInfo::getDirect(CoerceTy);
5312     }
5313 
5314     // All other aggregates are passed ByVal.
5315     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5316                                    /*ByVal=*/true,
5317                                    /*Realign=*/TyAlign > ABIAlign);
5318   }
5319 
5320   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
5321                                      : ABIArgInfo::getDirect());
5322 }
5323 
5324 ABIArgInfo
5325 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
5326   if (RetTy->isVoidType())
5327     return ABIArgInfo::getIgnore();
5328 
5329   if (RetTy->isAnyComplexType())
5330     return ABIArgInfo::getDirect();
5331 
5332   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
5333   // or via reference (larger than 16 bytes).
5334   if (RetTy->isVectorType()) {
5335     uint64_t Size = getContext().getTypeSize(RetTy);
5336     if (Size > 128)
5337       return getNaturalAlignIndirect(RetTy);
5338     else if (Size < 128) {
5339       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5340       return ABIArgInfo::getDirect(CoerceTy);
5341     }
5342   }
5343 
5344   if (const auto *EIT = RetTy->getAs<ExtIntType>())
5345     if (EIT->getNumBits() > 128)
5346       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
5347 
5348   if (isAggregateTypeForABI(RetTy)) {
5349     // ELFv2 homogeneous aggregates are returned as array types.
5350     const Type *Base = nullptr;
5351     uint64_t Members = 0;
5352     if (Kind == ELFv2 &&
5353         isHomogeneousAggregate(RetTy, Base, Members)) {
5354       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5355       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5356       return ABIArgInfo::getDirect(CoerceTy);
5357     }
5358 
5359     // ELFv2 small aggregates are returned in up to two registers.
5360     uint64_t Bits = getContext().getTypeSize(RetTy);
5361     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
5362       if (Bits == 0)
5363         return ABIArgInfo::getIgnore();
5364 
5365       llvm::Type *CoerceTy;
5366       if (Bits > GPRBits) {
5367         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
5368         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
5369       } else
5370         CoerceTy =
5371             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5372       return ABIArgInfo::getDirect(CoerceTy);
5373     }
5374 
5375     // All other aggregates are returned indirectly.
5376     return getNaturalAlignIndirect(RetTy);
5377   }
5378 
5379   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
5380                                         : ABIArgInfo::getDirect());
5381 }
5382 
5383 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
5384 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5385                                       QualType Ty) const {
5386   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
5387   TypeInfo.Align = getParamTypeAlignment(Ty);
5388 
5389   CharUnits SlotSize = CharUnits::fromQuantity(8);
5390 
5391   // If we have a complex type and the base type is smaller than 8 bytes,
5392   // the ABI calls for the real and imaginary parts to be right-adjusted
5393   // in separate doublewords.  However, Clang expects us to produce a
5394   // pointer to a structure with the two parts packed tightly.  So generate
5395   // loads of the real and imaginary parts relative to the va_list pointer,
5396   // and store them to a temporary structure.
5397   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
5398     CharUnits EltSize = TypeInfo.Width / 2;
5399     if (EltSize < SlotSize) {
5400       Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
5401                                             SlotSize * 2, SlotSize,
5402                                             SlotSize, /*AllowHigher*/ true);
5403 
5404       Address RealAddr = Addr;
5405       Address ImagAddr = RealAddr;
5406       if (CGF.CGM.getDataLayout().isBigEndian()) {
5407         RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
5408                                                           SlotSize - EltSize);
5409         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
5410                                                       2 * SlotSize - EltSize);
5411       } else {
5412         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
5413       }
5414 
5415       llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
5416       RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
5417       ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
5418       llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
5419       llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
5420 
5421       Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
5422       CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
5423                              /*init*/ true);
5424       return Temp;
5425     }
5426   }
5427 
5428   // Otherwise, just use the general rule.
5429   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
5430                           TypeInfo, SlotSize, /*AllowHigher*/ true);
5431 }
5432 
5433 bool
5434 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
5435   CodeGen::CodeGenFunction &CGF,
5436   llvm::Value *Address) const {
5437   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5438                                      /*IsAIX*/ false);
5439 }
5440 
5441 bool
5442 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5443                                                 llvm::Value *Address) const {
5444   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5445                                      /*IsAIX*/ false);
5446 }
5447 
5448 //===----------------------------------------------------------------------===//
5449 // AArch64 ABI Implementation
5450 //===----------------------------------------------------------------------===//
5451 
5452 namespace {
5453 
5454 class AArch64ABIInfo : public SwiftABIInfo {
5455 public:
5456   enum ABIKind {
5457     AAPCS = 0,
5458     DarwinPCS,
5459     Win64
5460   };
5461 
5462 private:
5463   ABIKind Kind;
5464 
5465 public:
5466   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
5467     : SwiftABIInfo(CGT), Kind(Kind) {}
5468 
5469 private:
5470   ABIKind getABIKind() const { return Kind; }
5471   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5472 
5473   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5474   ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
5475                                   unsigned CallingConvention) const;
5476   ABIArgInfo coerceIllegalVector(QualType Ty) const;
5477   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5478   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5479                                          uint64_t Members) const override;
5480 
5481   bool isIllegalVectorType(QualType Ty) const;
5482 
5483   void computeInfo(CGFunctionInfo &FI) const override {
5484     if (!::classifyReturnType(getCXXABI(), FI, *this))
5485       FI.getReturnInfo() =
5486           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5487 
5488     for (auto &it : FI.arguments())
5489       it.info = classifyArgumentType(it.type, FI.isVariadic(),
5490                                      FI.getCallingConvention());
5491   }
5492 
5493   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5494                           CodeGenFunction &CGF) const;
5495 
5496   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5497                          CodeGenFunction &CGF) const;
5498 
5499   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5500                     QualType Ty) const override {
5501     llvm::Type *BaseTy = CGF.ConvertType(Ty);
5502     if (isa<llvm::ScalableVectorType>(BaseTy))
5503       llvm::report_fatal_error("Passing SVE types to variadic functions is "
5504                                "currently not supported");
5505 
5506     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5507                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5508                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5509   }
5510 
5511   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5512                       QualType Ty) const override;
5513 
5514   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5515                                     bool asReturnValue) const override {
5516     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5517   }
5518   bool isSwiftErrorInRegister() const override {
5519     return true;
5520   }
5521 
5522   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5523                                  unsigned elts) const override;
5524 
5525   bool allowBFloatArgsAndRet() const override {
5526     return getTarget().hasBFloat16Type();
5527   }
5528 };
5529 
5530 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5531 public:
5532   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5533       : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
5534 
5535   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5536     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5537   }
5538 
5539   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5540     return 31;
5541   }
5542 
5543   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5544 
5545   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5546                            CodeGen::CodeGenModule &CGM) const override {
5547     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5548     if (!FD)
5549       return;
5550 
5551     const auto *TA = FD->getAttr<TargetAttr>();
5552     if (TA == nullptr)
5553       return;
5554 
5555     ParsedTargetAttr Attr = TA->parse();
5556     if (Attr.BranchProtection.empty())
5557       return;
5558 
5559     TargetInfo::BranchProtectionInfo BPI;
5560     StringRef Error;
5561     (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
5562                                                    BPI, Error);
5563     assert(Error.empty());
5564 
5565     auto *Fn = cast<llvm::Function>(GV);
5566     static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
5567     Fn->addFnAttr("sign-return-address", SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
5568 
5569     if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
5570       Fn->addFnAttr("sign-return-address-key",
5571                     BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
5572                         ? "a_key"
5573                         : "b_key");
5574     }
5575 
5576     Fn->addFnAttr("branch-target-enforcement",
5577                   BPI.BranchTargetEnforcement ? "true" : "false");
5578   }
5579 
5580   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
5581                                 llvm::Type *Ty) const override {
5582     if (CGF.getTarget().hasFeature("ls64")) {
5583       auto *ST = dyn_cast<llvm::StructType>(Ty);
5584       if (ST && ST->getNumElements() == 1) {
5585         auto *AT = dyn_cast<llvm::ArrayType>(ST->getElementType(0));
5586         if (AT && AT->getNumElements() == 8 &&
5587             AT->getElementType()->isIntegerTy(64))
5588           return true;
5589       }
5590     }
5591     return TargetCodeGenInfo::isScalarizableAsmOperand(CGF, Ty);
5592   }
5593 };
5594 
5595 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5596 public:
5597   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5598       : AArch64TargetCodeGenInfo(CGT, K) {}
5599 
5600   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5601                            CodeGen::CodeGenModule &CGM) const override;
5602 
5603   void getDependentLibraryOption(llvm::StringRef Lib,
5604                                  llvm::SmallString<24> &Opt) const override {
5605     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5606   }
5607 
5608   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5609                                llvm::SmallString<32> &Opt) const override {
5610     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5611   }
5612 };
5613 
5614 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5615     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5616   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5617   if (GV->isDeclaration())
5618     return;
5619   addStackProbeTargetAttributes(D, GV, CGM);
5620 }
5621 }
5622 
5623 ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty) const {
5624   assert(Ty->isVectorType() && "expected vector type!");
5625 
5626   const auto *VT = Ty->castAs<VectorType>();
5627   if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
5628     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5629     assert(VT->getElementType()->castAs<BuiltinType>()->getKind() ==
5630                BuiltinType::UChar &&
5631            "unexpected builtin type for SVE predicate!");
5632     return ABIArgInfo::getDirect(llvm::ScalableVectorType::get(
5633         llvm::Type::getInt1Ty(getVMContext()), 16));
5634   }
5635 
5636   if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) {
5637     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5638 
5639     const auto *BT = VT->getElementType()->castAs<BuiltinType>();
5640     llvm::ScalableVectorType *ResType = nullptr;
5641     switch (BT->getKind()) {
5642     default:
5643       llvm_unreachable("unexpected builtin type for SVE vector!");
5644     case BuiltinType::SChar:
5645     case BuiltinType::UChar:
5646       ResType = llvm::ScalableVectorType::get(
5647           llvm::Type::getInt8Ty(getVMContext()), 16);
5648       break;
5649     case BuiltinType::Short:
5650     case BuiltinType::UShort:
5651       ResType = llvm::ScalableVectorType::get(
5652           llvm::Type::getInt16Ty(getVMContext()), 8);
5653       break;
5654     case BuiltinType::Int:
5655     case BuiltinType::UInt:
5656       ResType = llvm::ScalableVectorType::get(
5657           llvm::Type::getInt32Ty(getVMContext()), 4);
5658       break;
5659     case BuiltinType::Long:
5660     case BuiltinType::ULong:
5661       ResType = llvm::ScalableVectorType::get(
5662           llvm::Type::getInt64Ty(getVMContext()), 2);
5663       break;
5664     case BuiltinType::Half:
5665       ResType = llvm::ScalableVectorType::get(
5666           llvm::Type::getHalfTy(getVMContext()), 8);
5667       break;
5668     case BuiltinType::Float:
5669       ResType = llvm::ScalableVectorType::get(
5670           llvm::Type::getFloatTy(getVMContext()), 4);
5671       break;
5672     case BuiltinType::Double:
5673       ResType = llvm::ScalableVectorType::get(
5674           llvm::Type::getDoubleTy(getVMContext()), 2);
5675       break;
5676     case BuiltinType::BFloat16:
5677       ResType = llvm::ScalableVectorType::get(
5678           llvm::Type::getBFloatTy(getVMContext()), 8);
5679       break;
5680     }
5681     return ABIArgInfo::getDirect(ResType);
5682   }
5683 
5684   uint64_t Size = getContext().getTypeSize(Ty);
5685   // Android promotes <2 x i8> to i16, not i32
5686   if (isAndroid() && (Size <= 16)) {
5687     llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5688     return ABIArgInfo::getDirect(ResType);
5689   }
5690   if (Size <= 32) {
5691     llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5692     return ABIArgInfo::getDirect(ResType);
5693   }
5694   if (Size == 64) {
5695     auto *ResType =
5696         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5697     return ABIArgInfo::getDirect(ResType);
5698   }
5699   if (Size == 128) {
5700     auto *ResType =
5701         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5702     return ABIArgInfo::getDirect(ResType);
5703   }
5704   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5705 }
5706 
5707 ABIArgInfo
5708 AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadic,
5709                                      unsigned CallingConvention) const {
5710   Ty = useFirstFieldIfTransparentUnion(Ty);
5711 
5712   // Handle illegal vector types here.
5713   if (isIllegalVectorType(Ty))
5714     return coerceIllegalVector(Ty);
5715 
5716   if (!isAggregateTypeForABI(Ty)) {
5717     // Treat an enum type as its underlying type.
5718     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5719       Ty = EnumTy->getDecl()->getIntegerType();
5720 
5721     if (const auto *EIT = Ty->getAs<ExtIntType>())
5722       if (EIT->getNumBits() > 128)
5723         return getNaturalAlignIndirect(Ty);
5724 
5725     return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
5726                 ? ABIArgInfo::getExtend(Ty)
5727                 : ABIArgInfo::getDirect());
5728   }
5729 
5730   // Structures with either a non-trivial destructor or a non-trivial
5731   // copy constructor are always indirect.
5732   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5733     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5734                                      CGCXXABI::RAA_DirectInMemory);
5735   }
5736 
5737   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5738   // elsewhere for GNU compatibility.
5739   uint64_t Size = getContext().getTypeSize(Ty);
5740   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5741   if (IsEmpty || Size == 0) {
5742     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5743       return ABIArgInfo::getIgnore();
5744 
5745     // GNU C mode. The only argument that gets ignored is an empty one with size
5746     // 0.
5747     if (IsEmpty && Size == 0)
5748       return ABIArgInfo::getIgnore();
5749     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5750   }
5751 
5752   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5753   const Type *Base = nullptr;
5754   uint64_t Members = 0;
5755   bool IsWin64 = Kind == Win64 || CallingConvention == llvm::CallingConv::Win64;
5756   bool IsWinVariadic = IsWin64 && IsVariadic;
5757   // In variadic functions on Windows, all composite types are treated alike,
5758   // no special handling of HFAs/HVAs.
5759   if (!IsWinVariadic && isHomogeneousAggregate(Ty, Base, Members)) {
5760     if (Kind != AArch64ABIInfo::AAPCS)
5761       return ABIArgInfo::getDirect(
5762           llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5763 
5764     // For alignment adjusted HFAs, cap the argument alignment to 16, leave it
5765     // default otherwise.
5766     unsigned Align =
5767         getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
5768     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
5769     Align = (Align > BaseAlign && Align >= 16) ? 16 : 0;
5770     return ABIArgInfo::getDirect(
5771         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members), 0,
5772         nullptr, true, Align);
5773   }
5774 
5775   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5776   if (Size <= 128) {
5777     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5778     // same size and alignment.
5779     if (getTarget().isRenderScriptTarget()) {
5780       return coerceToIntArray(Ty, getContext(), getVMContext());
5781     }
5782     unsigned Alignment;
5783     if (Kind == AArch64ABIInfo::AAPCS) {
5784       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5785       Alignment = Alignment < 128 ? 64 : 128;
5786     } else {
5787       Alignment = std::max(getContext().getTypeAlign(Ty),
5788                            (unsigned)getTarget().getPointerWidth(0));
5789     }
5790     Size = llvm::alignTo(Size, Alignment);
5791 
5792     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5793     // For aggregates with 16-byte alignment, we use i128.
5794     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5795     return ABIArgInfo::getDirect(
5796         Size == Alignment ? BaseTy
5797                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5798   }
5799 
5800   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5801 }
5802 
5803 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5804                                               bool IsVariadic) const {
5805   if (RetTy->isVoidType())
5806     return ABIArgInfo::getIgnore();
5807 
5808   if (const auto *VT = RetTy->getAs<VectorType>()) {
5809     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5810         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5811       return coerceIllegalVector(RetTy);
5812   }
5813 
5814   // Large vector types should be returned via memory.
5815   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5816     return getNaturalAlignIndirect(RetTy);
5817 
5818   if (!isAggregateTypeForABI(RetTy)) {
5819     // Treat an enum type as its underlying type.
5820     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5821       RetTy = EnumTy->getDecl()->getIntegerType();
5822 
5823     if (const auto *EIT = RetTy->getAs<ExtIntType>())
5824       if (EIT->getNumBits() > 128)
5825         return getNaturalAlignIndirect(RetTy);
5826 
5827     return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
5828                 ? ABIArgInfo::getExtend(RetTy)
5829                 : ABIArgInfo::getDirect());
5830   }
5831 
5832   uint64_t Size = getContext().getTypeSize(RetTy);
5833   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5834     return ABIArgInfo::getIgnore();
5835 
5836   const Type *Base = nullptr;
5837   uint64_t Members = 0;
5838   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5839       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5840         IsVariadic))
5841     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5842     return ABIArgInfo::getDirect();
5843 
5844   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5845   if (Size <= 128) {
5846     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5847     // same size and alignment.
5848     if (getTarget().isRenderScriptTarget()) {
5849       return coerceToIntArray(RetTy, getContext(), getVMContext());
5850     }
5851 
5852     if (Size <= 64 && getDataLayout().isLittleEndian()) {
5853       // Composite types are returned in lower bits of a 64-bit register for LE,
5854       // and in higher bits for BE. However, integer types are always returned
5855       // in lower bits for both LE and BE, and they are not rounded up to
5856       // 64-bits. We can skip rounding up of composite types for LE, but not for
5857       // BE, otherwise composite types will be indistinguishable from integer
5858       // types.
5859       return ABIArgInfo::getDirect(
5860           llvm::IntegerType::get(getVMContext(), Size));
5861     }
5862 
5863     unsigned Alignment = getContext().getTypeAlign(RetTy);
5864     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5865 
5866     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5867     // For aggregates with 16-byte alignment, we use i128.
5868     if (Alignment < 128 && Size == 128) {
5869       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5870       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5871     }
5872     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5873   }
5874 
5875   return getNaturalAlignIndirect(RetTy);
5876 }
5877 
5878 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5879 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5880   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5881     // Check whether VT is a fixed-length SVE vector. These types are
5882     // represented as scalable vectors in function args/return and must be
5883     // coerced from fixed vectors.
5884     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5885         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5886       return true;
5887 
5888     // Check whether VT is legal.
5889     unsigned NumElements = VT->getNumElements();
5890     uint64_t Size = getContext().getTypeSize(VT);
5891     // NumElements should be power of 2.
5892     if (!llvm::isPowerOf2_32(NumElements))
5893       return true;
5894 
5895     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5896     // vectors for some reason.
5897     llvm::Triple Triple = getTarget().getTriple();
5898     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5899         Triple.isOSBinFormatMachO())
5900       return Size <= 32;
5901 
5902     return Size != 64 && (Size != 128 || NumElements == 1);
5903   }
5904   return false;
5905 }
5906 
5907 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5908                                                llvm::Type *eltTy,
5909                                                unsigned elts) const {
5910   if (!llvm::isPowerOf2_32(elts))
5911     return false;
5912   if (totalSize.getQuantity() != 8 &&
5913       (totalSize.getQuantity() != 16 || elts == 1))
5914     return false;
5915   return true;
5916 }
5917 
5918 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5919   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5920   // point type or a short-vector type. This is the same as the 32-bit ABI,
5921   // but with the difference that any floating-point type is allowed,
5922   // including __fp16.
5923   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5924     if (BT->isFloatingPoint())
5925       return true;
5926   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5927     unsigned VecSize = getContext().getTypeSize(VT);
5928     if (VecSize == 64 || VecSize == 128)
5929       return true;
5930   }
5931   return false;
5932 }
5933 
5934 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5935                                                        uint64_t Members) const {
5936   return Members <= 4;
5937 }
5938 
5939 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5940                                        CodeGenFunction &CGF) const {
5941   ABIArgInfo AI = classifyArgumentType(Ty, /*IsVariadic=*/true,
5942                                        CGF.CurFnInfo->getCallingConvention());
5943   bool IsIndirect = AI.isIndirect();
5944 
5945   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5946   if (IsIndirect)
5947     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5948   else if (AI.getCoerceToType())
5949     BaseTy = AI.getCoerceToType();
5950 
5951   unsigned NumRegs = 1;
5952   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5953     BaseTy = ArrTy->getElementType();
5954     NumRegs = ArrTy->getNumElements();
5955   }
5956   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5957 
5958   // The AArch64 va_list type and handling is specified in the Procedure Call
5959   // Standard, section B.4:
5960   //
5961   // struct {
5962   //   void *__stack;
5963   //   void *__gr_top;
5964   //   void *__vr_top;
5965   //   int __gr_offs;
5966   //   int __vr_offs;
5967   // };
5968 
5969   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5970   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5971   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5972   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5973 
5974   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
5975   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
5976 
5977   Address reg_offs_p = Address::invalid();
5978   llvm::Value *reg_offs = nullptr;
5979   int reg_top_index;
5980   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
5981   if (!IsFPR) {
5982     // 3 is the field number of __gr_offs
5983     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
5984     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5985     reg_top_index = 1; // field number for __gr_top
5986     RegSize = llvm::alignTo(RegSize, 8);
5987   } else {
5988     // 4 is the field number of __vr_offs.
5989     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
5990     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5991     reg_top_index = 2; // field number for __vr_top
5992     RegSize = 16 * NumRegs;
5993   }
5994 
5995   //=======================================
5996   // Find out where argument was passed
5997   //=======================================
5998 
5999   // If reg_offs >= 0 we're already using the stack for this type of
6000   // argument. We don't want to keep updating reg_offs (in case it overflows,
6001   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
6002   // whatever they get).
6003   llvm::Value *UsingStack = nullptr;
6004   UsingStack = CGF.Builder.CreateICmpSGE(
6005       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
6006 
6007   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
6008 
6009   // Otherwise, at least some kind of argument could go in these registers, the
6010   // question is whether this particular type is too big.
6011   CGF.EmitBlock(MaybeRegBlock);
6012 
6013   // Integer arguments may need to correct register alignment (for example a
6014   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
6015   // align __gr_offs to calculate the potential address.
6016   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
6017     int Align = TyAlign.getQuantity();
6018 
6019     reg_offs = CGF.Builder.CreateAdd(
6020         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
6021         "align_regoffs");
6022     reg_offs = CGF.Builder.CreateAnd(
6023         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
6024         "aligned_regoffs");
6025   }
6026 
6027   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
6028   // The fact that this is done unconditionally reflects the fact that
6029   // allocating an argument to the stack also uses up all the remaining
6030   // registers of the appropriate kind.
6031   llvm::Value *NewOffset = nullptr;
6032   NewOffset = CGF.Builder.CreateAdd(
6033       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
6034   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
6035 
6036   // Now we're in a position to decide whether this argument really was in
6037   // registers or not.
6038   llvm::Value *InRegs = nullptr;
6039   InRegs = CGF.Builder.CreateICmpSLE(
6040       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
6041 
6042   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
6043 
6044   //=======================================
6045   // Argument was in registers
6046   //=======================================
6047 
6048   // Now we emit the code for if the argument was originally passed in
6049   // registers. First start the appropriate block:
6050   CGF.EmitBlock(InRegBlock);
6051 
6052   llvm::Value *reg_top = nullptr;
6053   Address reg_top_p =
6054       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
6055   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
6056   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, reg_top, reg_offs),
6057                    CharUnits::fromQuantity(IsFPR ? 16 : 8));
6058   Address RegAddr = Address::invalid();
6059   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
6060 
6061   if (IsIndirect) {
6062     // If it's been passed indirectly (actually a struct), whatever we find from
6063     // stored registers or on the stack will actually be a struct **.
6064     MemTy = llvm::PointerType::getUnqual(MemTy);
6065   }
6066 
6067   const Type *Base = nullptr;
6068   uint64_t NumMembers = 0;
6069   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
6070   if (IsHFA && NumMembers > 1) {
6071     // Homogeneous aggregates passed in registers will have their elements split
6072     // and stored 16-bytes apart regardless of size (they're notionally in qN,
6073     // qN+1, ...). We reload and store into a temporary local variable
6074     // contiguously.
6075     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
6076     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
6077     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
6078     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
6079     Address Tmp = CGF.CreateTempAlloca(HFATy,
6080                                        std::max(TyAlign, BaseTyInfo.Align));
6081 
6082     // On big-endian platforms, the value will be right-aligned in its slot.
6083     int Offset = 0;
6084     if (CGF.CGM.getDataLayout().isBigEndian() &&
6085         BaseTyInfo.Width.getQuantity() < 16)
6086       Offset = 16 - BaseTyInfo.Width.getQuantity();
6087 
6088     for (unsigned i = 0; i < NumMembers; ++i) {
6089       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
6090       Address LoadAddr =
6091         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
6092       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
6093 
6094       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
6095 
6096       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
6097       CGF.Builder.CreateStore(Elem, StoreAddr);
6098     }
6099 
6100     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
6101   } else {
6102     // Otherwise the object is contiguous in memory.
6103 
6104     // It might be right-aligned in its slot.
6105     CharUnits SlotSize = BaseAddr.getAlignment();
6106     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
6107         (IsHFA || !isAggregateTypeForABI(Ty)) &&
6108         TySize < SlotSize) {
6109       CharUnits Offset = SlotSize - TySize;
6110       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
6111     }
6112 
6113     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
6114   }
6115 
6116   CGF.EmitBranch(ContBlock);
6117 
6118   //=======================================
6119   // Argument was on the stack
6120   //=======================================
6121   CGF.EmitBlock(OnStackBlock);
6122 
6123   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
6124   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
6125 
6126   // Again, stack arguments may need realignment. In this case both integer and
6127   // floating-point ones might be affected.
6128   if (!IsIndirect && TyAlign.getQuantity() > 8) {
6129     int Align = TyAlign.getQuantity();
6130 
6131     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
6132 
6133     OnStackPtr = CGF.Builder.CreateAdd(
6134         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
6135         "align_stack");
6136     OnStackPtr = CGF.Builder.CreateAnd(
6137         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
6138         "align_stack");
6139 
6140     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
6141   }
6142   Address OnStackAddr(OnStackPtr,
6143                       std::max(CharUnits::fromQuantity(8), TyAlign));
6144 
6145   // All stack slots are multiples of 8 bytes.
6146   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
6147   CharUnits StackSize;
6148   if (IsIndirect)
6149     StackSize = StackSlotSize;
6150   else
6151     StackSize = TySize.alignTo(StackSlotSize);
6152 
6153   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
6154   llvm::Value *NewStack = CGF.Builder.CreateInBoundsGEP(
6155       CGF.Int8Ty, OnStackPtr, StackSizeC, "new_stack");
6156 
6157   // Write the new value of __stack for the next call to va_arg
6158   CGF.Builder.CreateStore(NewStack, stack_p);
6159 
6160   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
6161       TySize < StackSlotSize) {
6162     CharUnits Offset = StackSlotSize - TySize;
6163     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
6164   }
6165 
6166   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
6167 
6168   CGF.EmitBranch(ContBlock);
6169 
6170   //=======================================
6171   // Tidy up
6172   //=======================================
6173   CGF.EmitBlock(ContBlock);
6174 
6175   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6176                                  OnStackAddr, OnStackBlock, "vaargs.addr");
6177 
6178   if (IsIndirect)
6179     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
6180                    TyAlign);
6181 
6182   return ResAddr;
6183 }
6184 
6185 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
6186                                         CodeGenFunction &CGF) const {
6187   // The backend's lowering doesn't support va_arg for aggregates or
6188   // illegal vector types.  Lower VAArg here for these cases and use
6189   // the LLVM va_arg instruction for everything else.
6190   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
6191     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
6192 
6193   uint64_t PointerSize = getTarget().getPointerWidth(0) / 8;
6194   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6195 
6196   // Empty records are ignored for parameter passing purposes.
6197   if (isEmptyRecord(getContext(), Ty, true)) {
6198     Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
6199     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6200     return Addr;
6201   }
6202 
6203   // The size of the actual thing passed, which might end up just
6204   // being a pointer for indirect types.
6205   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6206 
6207   // Arguments bigger than 16 bytes which aren't homogeneous
6208   // aggregates should be passed indirectly.
6209   bool IsIndirect = false;
6210   if (TyInfo.Width.getQuantity() > 16) {
6211     const Type *Base = nullptr;
6212     uint64_t Members = 0;
6213     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
6214   }
6215 
6216   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6217                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
6218 }
6219 
6220 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
6221                                     QualType Ty) const {
6222   bool IsIndirect = false;
6223 
6224   // Composites larger than 16 bytes are passed by reference.
6225   if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) > 128)
6226     IsIndirect = true;
6227 
6228   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6229                           CGF.getContext().getTypeInfoInChars(Ty),
6230                           CharUnits::fromQuantity(8),
6231                           /*allowHigherAlign*/ false);
6232 }
6233 
6234 //===----------------------------------------------------------------------===//
6235 // ARM ABI Implementation
6236 //===----------------------------------------------------------------------===//
6237 
6238 namespace {
6239 
6240 class ARMABIInfo : public SwiftABIInfo {
6241 public:
6242   enum ABIKind {
6243     APCS = 0,
6244     AAPCS = 1,
6245     AAPCS_VFP = 2,
6246     AAPCS16_VFP = 3,
6247   };
6248 
6249 private:
6250   ABIKind Kind;
6251   bool IsFloatABISoftFP;
6252 
6253 public:
6254   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
6255       : SwiftABIInfo(CGT), Kind(_Kind) {
6256     setCCs();
6257     IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
6258         CGT.getCodeGenOpts().FloatABI == ""; // default
6259   }
6260 
6261   bool isEABI() const {
6262     switch (getTarget().getTriple().getEnvironment()) {
6263     case llvm::Triple::Android:
6264     case llvm::Triple::EABI:
6265     case llvm::Triple::EABIHF:
6266     case llvm::Triple::GNUEABI:
6267     case llvm::Triple::GNUEABIHF:
6268     case llvm::Triple::MuslEABI:
6269     case llvm::Triple::MuslEABIHF:
6270       return true;
6271     default:
6272       return false;
6273     }
6274   }
6275 
6276   bool isEABIHF() const {
6277     switch (getTarget().getTriple().getEnvironment()) {
6278     case llvm::Triple::EABIHF:
6279     case llvm::Triple::GNUEABIHF:
6280     case llvm::Triple::MuslEABIHF:
6281       return true;
6282     default:
6283       return false;
6284     }
6285   }
6286 
6287   ABIKind getABIKind() const { return Kind; }
6288 
6289   bool allowBFloatArgsAndRet() const override {
6290     return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
6291   }
6292 
6293 private:
6294   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
6295                                 unsigned functionCallConv) const;
6296   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
6297                                   unsigned functionCallConv) const;
6298   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
6299                                           uint64_t Members) const;
6300   ABIArgInfo coerceIllegalVector(QualType Ty) const;
6301   bool isIllegalVectorType(QualType Ty) const;
6302   bool containsAnyFP16Vectors(QualType Ty) const;
6303 
6304   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
6305   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
6306                                          uint64_t Members) const override;
6307 
6308   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
6309 
6310   void computeInfo(CGFunctionInfo &FI) const override;
6311 
6312   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6313                     QualType Ty) const override;
6314 
6315   llvm::CallingConv::ID getLLVMDefaultCC() const;
6316   llvm::CallingConv::ID getABIDefaultCC() const;
6317   void setCCs();
6318 
6319   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
6320                                     bool asReturnValue) const override {
6321     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6322   }
6323   bool isSwiftErrorInRegister() const override {
6324     return true;
6325   }
6326   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
6327                                  unsigned elts) const override;
6328 };
6329 
6330 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
6331 public:
6332   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6333       : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {}
6334 
6335   const ARMABIInfo &getABIInfo() const {
6336     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
6337   }
6338 
6339   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6340     return 13;
6341   }
6342 
6343   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
6344     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
6345   }
6346 
6347   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6348                                llvm::Value *Address) const override {
6349     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6350 
6351     // 0-15 are the 16 integer registers.
6352     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
6353     return false;
6354   }
6355 
6356   unsigned getSizeOfUnwindException() const override {
6357     if (getABIInfo().isEABI()) return 88;
6358     return TargetCodeGenInfo::getSizeOfUnwindException();
6359   }
6360 
6361   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6362                            CodeGen::CodeGenModule &CGM) const override {
6363     if (GV->isDeclaration())
6364       return;
6365     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6366     if (!FD)
6367       return;
6368 
6369     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
6370     if (!Attr)
6371       return;
6372 
6373     const char *Kind;
6374     switch (Attr->getInterrupt()) {
6375     case ARMInterruptAttr::Generic: Kind = ""; break;
6376     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
6377     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
6378     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
6379     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
6380     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
6381     }
6382 
6383     llvm::Function *Fn = cast<llvm::Function>(GV);
6384 
6385     Fn->addFnAttr("interrupt", Kind);
6386 
6387     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
6388     if (ABI == ARMABIInfo::APCS)
6389       return;
6390 
6391     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
6392     // however this is not necessarily true on taking any interrupt. Instruct
6393     // the backend to perform a realignment as part of the function prologue.
6394     llvm::AttrBuilder B;
6395     B.addStackAlignmentAttr(8);
6396     Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
6397   }
6398 };
6399 
6400 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
6401 public:
6402   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6403       : ARMTargetCodeGenInfo(CGT, K) {}
6404 
6405   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6406                            CodeGen::CodeGenModule &CGM) const override;
6407 
6408   void getDependentLibraryOption(llvm::StringRef Lib,
6409                                  llvm::SmallString<24> &Opt) const override {
6410     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
6411   }
6412 
6413   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
6414                                llvm::SmallString<32> &Opt) const override {
6415     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
6416   }
6417 };
6418 
6419 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
6420     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
6421   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
6422   if (GV->isDeclaration())
6423     return;
6424   addStackProbeTargetAttributes(D, GV, CGM);
6425 }
6426 }
6427 
6428 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
6429   if (!::classifyReturnType(getCXXABI(), FI, *this))
6430     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
6431                                             FI.getCallingConvention());
6432 
6433   for (auto &I : FI.arguments())
6434     I.info = classifyArgumentType(I.type, FI.isVariadic(),
6435                                   FI.getCallingConvention());
6436 
6437 
6438   // Always honor user-specified calling convention.
6439   if (FI.getCallingConvention() != llvm::CallingConv::C)
6440     return;
6441 
6442   llvm::CallingConv::ID cc = getRuntimeCC();
6443   if (cc != llvm::CallingConv::C)
6444     FI.setEffectiveCallingConvention(cc);
6445 }
6446 
6447 /// Return the default calling convention that LLVM will use.
6448 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
6449   // The default calling convention that LLVM will infer.
6450   if (isEABIHF() || getTarget().getTriple().isWatchABI())
6451     return llvm::CallingConv::ARM_AAPCS_VFP;
6452   else if (isEABI())
6453     return llvm::CallingConv::ARM_AAPCS;
6454   else
6455     return llvm::CallingConv::ARM_APCS;
6456 }
6457 
6458 /// Return the calling convention that our ABI would like us to use
6459 /// as the C calling convention.
6460 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
6461   switch (getABIKind()) {
6462   case APCS: return llvm::CallingConv::ARM_APCS;
6463   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
6464   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6465   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6466   }
6467   llvm_unreachable("bad ABI kind");
6468 }
6469 
6470 void ARMABIInfo::setCCs() {
6471   assert(getRuntimeCC() == llvm::CallingConv::C);
6472 
6473   // Don't muddy up the IR with a ton of explicit annotations if
6474   // they'd just match what LLVM will infer from the triple.
6475   llvm::CallingConv::ID abiCC = getABIDefaultCC();
6476   if (abiCC != getLLVMDefaultCC())
6477     RuntimeCC = abiCC;
6478 }
6479 
6480 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
6481   uint64_t Size = getContext().getTypeSize(Ty);
6482   if (Size <= 32) {
6483     llvm::Type *ResType =
6484         llvm::Type::getInt32Ty(getVMContext());
6485     return ABIArgInfo::getDirect(ResType);
6486   }
6487   if (Size == 64 || Size == 128) {
6488     auto *ResType = llvm::FixedVectorType::get(
6489         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6490     return ABIArgInfo::getDirect(ResType);
6491   }
6492   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6493 }
6494 
6495 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
6496                                                     const Type *Base,
6497                                                     uint64_t Members) const {
6498   assert(Base && "Base class should be set for homogeneous aggregate");
6499   // Base can be a floating-point or a vector.
6500   if (const VectorType *VT = Base->getAs<VectorType>()) {
6501     // FP16 vectors should be converted to integer vectors
6502     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
6503       uint64_t Size = getContext().getTypeSize(VT);
6504       auto *NewVecTy = llvm::FixedVectorType::get(
6505           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6506       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
6507       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6508     }
6509   }
6510   unsigned Align = 0;
6511   if (getABIKind() == ARMABIInfo::AAPCS ||
6512       getABIKind() == ARMABIInfo::AAPCS_VFP) {
6513     // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
6514     // default otherwise.
6515     Align = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6516     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
6517     Align = (Align > BaseAlign && Align >= 8) ? 8 : 0;
6518   }
6519   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align);
6520 }
6521 
6522 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
6523                                             unsigned functionCallConv) const {
6524   // 6.1.2.1 The following argument types are VFP CPRCs:
6525   //   A single-precision floating-point type (including promoted
6526   //   half-precision types); A double-precision floating-point type;
6527   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
6528   //   with a Base Type of a single- or double-precision floating-point type,
6529   //   64-bit containerized vectors or 128-bit containerized vectors with one
6530   //   to four Elements.
6531   // Variadic functions should always marshal to the base standard.
6532   bool IsAAPCS_VFP =
6533       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
6534 
6535   Ty = useFirstFieldIfTransparentUnion(Ty);
6536 
6537   // Handle illegal vector types here.
6538   if (isIllegalVectorType(Ty))
6539     return coerceIllegalVector(Ty);
6540 
6541   if (!isAggregateTypeForABI(Ty)) {
6542     // Treat an enum type as its underlying type.
6543     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
6544       Ty = EnumTy->getDecl()->getIntegerType();
6545     }
6546 
6547     if (const auto *EIT = Ty->getAs<ExtIntType>())
6548       if (EIT->getNumBits() > 64)
6549         return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6550 
6551     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
6552                                               : ABIArgInfo::getDirect());
6553   }
6554 
6555   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6556     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6557   }
6558 
6559   // Ignore empty records.
6560   if (isEmptyRecord(getContext(), Ty, true))
6561     return ABIArgInfo::getIgnore();
6562 
6563   if (IsAAPCS_VFP) {
6564     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
6565     // into VFP registers.
6566     const Type *Base = nullptr;
6567     uint64_t Members = 0;
6568     if (isHomogeneousAggregate(Ty, Base, Members))
6569       return classifyHomogeneousAggregate(Ty, Base, Members);
6570   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6571     // WatchOS does have homogeneous aggregates. Note that we intentionally use
6572     // this convention even for a variadic function: the backend will use GPRs
6573     // if needed.
6574     const Type *Base = nullptr;
6575     uint64_t Members = 0;
6576     if (isHomogeneousAggregate(Ty, Base, Members)) {
6577       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
6578       llvm::Type *Ty =
6579         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
6580       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6581     }
6582   }
6583 
6584   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6585       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
6586     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
6587     // bigger than 128-bits, they get placed in space allocated by the caller,
6588     // and a pointer is passed.
6589     return ABIArgInfo::getIndirect(
6590         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
6591   }
6592 
6593   // Support byval for ARM.
6594   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
6595   // most 8-byte. We realign the indirect argument if type alignment is bigger
6596   // than ABI alignment.
6597   uint64_t ABIAlign = 4;
6598   uint64_t TyAlign;
6599   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6600       getABIKind() == ARMABIInfo::AAPCS) {
6601     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6602     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
6603   } else {
6604     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
6605   }
6606   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
6607     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
6608     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
6609                                    /*ByVal=*/true,
6610                                    /*Realign=*/TyAlign > ABIAlign);
6611   }
6612 
6613   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
6614   // same size and alignment.
6615   if (getTarget().isRenderScriptTarget()) {
6616     return coerceToIntArray(Ty, getContext(), getVMContext());
6617   }
6618 
6619   // Otherwise, pass by coercing to a structure of the appropriate size.
6620   llvm::Type* ElemTy;
6621   unsigned SizeRegs;
6622   // FIXME: Try to match the types of the arguments more accurately where
6623   // we can.
6624   if (TyAlign <= 4) {
6625     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6626     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6627   } else {
6628     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6629     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6630   }
6631 
6632   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6633 }
6634 
6635 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6636                               llvm::LLVMContext &VMContext) {
6637   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6638   // is called integer-like if its size is less than or equal to one word, and
6639   // the offset of each of its addressable sub-fields is zero.
6640 
6641   uint64_t Size = Context.getTypeSize(Ty);
6642 
6643   // Check that the type fits in a word.
6644   if (Size > 32)
6645     return false;
6646 
6647   // FIXME: Handle vector types!
6648   if (Ty->isVectorType())
6649     return false;
6650 
6651   // Float types are never treated as "integer like".
6652   if (Ty->isRealFloatingType())
6653     return false;
6654 
6655   // If this is a builtin or pointer type then it is ok.
6656   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6657     return true;
6658 
6659   // Small complex integer types are "integer like".
6660   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6661     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6662 
6663   // Single element and zero sized arrays should be allowed, by the definition
6664   // above, but they are not.
6665 
6666   // Otherwise, it must be a record type.
6667   const RecordType *RT = Ty->getAs<RecordType>();
6668   if (!RT) return false;
6669 
6670   // Ignore records with flexible arrays.
6671   const RecordDecl *RD = RT->getDecl();
6672   if (RD->hasFlexibleArrayMember())
6673     return false;
6674 
6675   // Check that all sub-fields are at offset 0, and are themselves "integer
6676   // like".
6677   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6678 
6679   bool HadField = false;
6680   unsigned idx = 0;
6681   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6682        i != e; ++i, ++idx) {
6683     const FieldDecl *FD = *i;
6684 
6685     // Bit-fields are not addressable, we only need to verify they are "integer
6686     // like". We still have to disallow a subsequent non-bitfield, for example:
6687     //   struct { int : 0; int x }
6688     // is non-integer like according to gcc.
6689     if (FD->isBitField()) {
6690       if (!RD->isUnion())
6691         HadField = true;
6692 
6693       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6694         return false;
6695 
6696       continue;
6697     }
6698 
6699     // Check if this field is at offset 0.
6700     if (Layout.getFieldOffset(idx) != 0)
6701       return false;
6702 
6703     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6704       return false;
6705 
6706     // Only allow at most one field in a structure. This doesn't match the
6707     // wording above, but follows gcc in situations with a field following an
6708     // empty structure.
6709     if (!RD->isUnion()) {
6710       if (HadField)
6711         return false;
6712 
6713       HadField = true;
6714     }
6715   }
6716 
6717   return true;
6718 }
6719 
6720 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6721                                           unsigned functionCallConv) const {
6722 
6723   // Variadic functions should always marshal to the base standard.
6724   bool IsAAPCS_VFP =
6725       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6726 
6727   if (RetTy->isVoidType())
6728     return ABIArgInfo::getIgnore();
6729 
6730   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6731     // Large vector types should be returned via memory.
6732     if (getContext().getTypeSize(RetTy) > 128)
6733       return getNaturalAlignIndirect(RetTy);
6734     // TODO: FP16/BF16 vectors should be converted to integer vectors
6735     // This check is similar  to isIllegalVectorType - refactor?
6736     if ((!getTarget().hasLegalHalfType() &&
6737         (VT->getElementType()->isFloat16Type() ||
6738          VT->getElementType()->isHalfType())) ||
6739         (IsFloatABISoftFP &&
6740          VT->getElementType()->isBFloat16Type()))
6741       return coerceIllegalVector(RetTy);
6742   }
6743 
6744   if (!isAggregateTypeForABI(RetTy)) {
6745     // Treat an enum type as its underlying type.
6746     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6747       RetTy = EnumTy->getDecl()->getIntegerType();
6748 
6749     if (const auto *EIT = RetTy->getAs<ExtIntType>())
6750       if (EIT->getNumBits() > 64)
6751         return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
6752 
6753     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6754                                                 : ABIArgInfo::getDirect();
6755   }
6756 
6757   // Are we following APCS?
6758   if (getABIKind() == APCS) {
6759     if (isEmptyRecord(getContext(), RetTy, false))
6760       return ABIArgInfo::getIgnore();
6761 
6762     // Complex types are all returned as packed integers.
6763     //
6764     // FIXME: Consider using 2 x vector types if the back end handles them
6765     // correctly.
6766     if (RetTy->isAnyComplexType())
6767       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6768           getVMContext(), getContext().getTypeSize(RetTy)));
6769 
6770     // Integer like structures are returned in r0.
6771     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6772       // Return in the smallest viable integer type.
6773       uint64_t Size = getContext().getTypeSize(RetTy);
6774       if (Size <= 8)
6775         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6776       if (Size <= 16)
6777         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6778       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6779     }
6780 
6781     // Otherwise return in memory.
6782     return getNaturalAlignIndirect(RetTy);
6783   }
6784 
6785   // Otherwise this is an AAPCS variant.
6786 
6787   if (isEmptyRecord(getContext(), RetTy, true))
6788     return ABIArgInfo::getIgnore();
6789 
6790   // Check for homogeneous aggregates with AAPCS-VFP.
6791   if (IsAAPCS_VFP) {
6792     const Type *Base = nullptr;
6793     uint64_t Members = 0;
6794     if (isHomogeneousAggregate(RetTy, Base, Members))
6795       return classifyHomogeneousAggregate(RetTy, Base, Members);
6796   }
6797 
6798   // Aggregates <= 4 bytes are returned in r0; other aggregates
6799   // are returned indirectly.
6800   uint64_t Size = getContext().getTypeSize(RetTy);
6801   if (Size <= 32) {
6802     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6803     // same size and alignment.
6804     if (getTarget().isRenderScriptTarget()) {
6805       return coerceToIntArray(RetTy, getContext(), getVMContext());
6806     }
6807     if (getDataLayout().isBigEndian())
6808       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6809       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6810 
6811     // Return in the smallest viable integer type.
6812     if (Size <= 8)
6813       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6814     if (Size <= 16)
6815       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6816     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6817   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6818     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6819     llvm::Type *CoerceTy =
6820         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6821     return ABIArgInfo::getDirect(CoerceTy);
6822   }
6823 
6824   return getNaturalAlignIndirect(RetTy);
6825 }
6826 
6827 /// isIllegalVector - check whether Ty is an illegal vector type.
6828 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6829   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6830     // On targets that don't support half, fp16 or bfloat, they are expanded
6831     // into float, and we don't want the ABI to depend on whether or not they
6832     // are supported in hardware. Thus return false to coerce vectors of these
6833     // types into integer vectors.
6834     // We do not depend on hasLegalHalfType for bfloat as it is a
6835     // separate IR type.
6836     if ((!getTarget().hasLegalHalfType() &&
6837         (VT->getElementType()->isFloat16Type() ||
6838          VT->getElementType()->isHalfType())) ||
6839         (IsFloatABISoftFP &&
6840          VT->getElementType()->isBFloat16Type()))
6841       return true;
6842     if (isAndroid()) {
6843       // Android shipped using Clang 3.1, which supported a slightly different
6844       // vector ABI. The primary differences were that 3-element vector types
6845       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6846       // accepts that legacy behavior for Android only.
6847       // Check whether VT is legal.
6848       unsigned NumElements = VT->getNumElements();
6849       // NumElements should be power of 2 or equal to 3.
6850       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6851         return true;
6852     } else {
6853       // Check whether VT is legal.
6854       unsigned NumElements = VT->getNumElements();
6855       uint64_t Size = getContext().getTypeSize(VT);
6856       // NumElements should be power of 2.
6857       if (!llvm::isPowerOf2_32(NumElements))
6858         return true;
6859       // Size should be greater than 32 bits.
6860       return Size <= 32;
6861     }
6862   }
6863   return false;
6864 }
6865 
6866 /// Return true if a type contains any 16-bit floating point vectors
6867 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6868   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6869     uint64_t NElements = AT->getSize().getZExtValue();
6870     if (NElements == 0)
6871       return false;
6872     return containsAnyFP16Vectors(AT->getElementType());
6873   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6874     const RecordDecl *RD = RT->getDecl();
6875 
6876     // If this is a C++ record, check the bases first.
6877     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6878       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6879             return containsAnyFP16Vectors(B.getType());
6880           }))
6881         return true;
6882 
6883     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6884           return FD && containsAnyFP16Vectors(FD->getType());
6885         }))
6886       return true;
6887 
6888     return false;
6889   } else {
6890     if (const VectorType *VT = Ty->getAs<VectorType>())
6891       return (VT->getElementType()->isFloat16Type() ||
6892               VT->getElementType()->isBFloat16Type() ||
6893               VT->getElementType()->isHalfType());
6894     return false;
6895   }
6896 }
6897 
6898 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6899                                            llvm::Type *eltTy,
6900                                            unsigned numElts) const {
6901   if (!llvm::isPowerOf2_32(numElts))
6902     return false;
6903   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6904   if (size > 64)
6905     return false;
6906   if (vectorSize.getQuantity() != 8 &&
6907       (vectorSize.getQuantity() != 16 || numElts == 1))
6908     return false;
6909   return true;
6910 }
6911 
6912 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6913   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6914   // double, or 64-bit or 128-bit vectors.
6915   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6916     if (BT->getKind() == BuiltinType::Float ||
6917         BT->getKind() == BuiltinType::Double ||
6918         BT->getKind() == BuiltinType::LongDouble)
6919       return true;
6920   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6921     unsigned VecSize = getContext().getTypeSize(VT);
6922     if (VecSize == 64 || VecSize == 128)
6923       return true;
6924   }
6925   return false;
6926 }
6927 
6928 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6929                                                    uint64_t Members) const {
6930   return Members <= 4;
6931 }
6932 
6933 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
6934                                         bool acceptHalf) const {
6935   // Give precedence to user-specified calling conventions.
6936   if (callConvention != llvm::CallingConv::C)
6937     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
6938   else
6939     return (getABIKind() == AAPCS_VFP) ||
6940            (acceptHalf && (getABIKind() == AAPCS16_VFP));
6941 }
6942 
6943 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6944                               QualType Ty) const {
6945   CharUnits SlotSize = CharUnits::fromQuantity(4);
6946 
6947   // Empty records are ignored for parameter passing purposes.
6948   if (isEmptyRecord(getContext(), Ty, true)) {
6949     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6950     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6951     return Addr;
6952   }
6953 
6954   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6955   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
6956 
6957   // Use indirect if size of the illegal vector is bigger than 16 bytes.
6958   bool IsIndirect = false;
6959   const Type *Base = nullptr;
6960   uint64_t Members = 0;
6961   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6962     IsIndirect = true;
6963 
6964   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6965   // allocated by the caller.
6966   } else if (TySize > CharUnits::fromQuantity(16) &&
6967              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6968              !isHomogeneousAggregate(Ty, Base, Members)) {
6969     IsIndirect = true;
6970 
6971   // Otherwise, bound the type's ABI alignment.
6972   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6973   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
6974   // Our callers should be prepared to handle an under-aligned address.
6975   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6976              getABIKind() == ARMABIInfo::AAPCS) {
6977     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6978     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
6979   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6980     // ARMv7k allows type alignment up to 16 bytes.
6981     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6982     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
6983   } else {
6984     TyAlignForABI = CharUnits::fromQuantity(4);
6985   }
6986 
6987   TypeInfoChars TyInfo(TySize, TyAlignForABI, false);
6988   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6989                           SlotSize, /*AllowHigherAlign*/ true);
6990 }
6991 
6992 //===----------------------------------------------------------------------===//
6993 // NVPTX ABI Implementation
6994 //===----------------------------------------------------------------------===//
6995 
6996 namespace {
6997 
6998 class NVPTXTargetCodeGenInfo;
6999 
7000 class NVPTXABIInfo : public ABIInfo {
7001   NVPTXTargetCodeGenInfo &CGInfo;
7002 
7003 public:
7004   NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
7005       : ABIInfo(CGT), CGInfo(Info) {}
7006 
7007   ABIArgInfo classifyReturnType(QualType RetTy) const;
7008   ABIArgInfo classifyArgumentType(QualType Ty) const;
7009 
7010   void computeInfo(CGFunctionInfo &FI) const override;
7011   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7012                     QualType Ty) const override;
7013   bool isUnsupportedType(QualType T) const;
7014   ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
7015 };
7016 
7017 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
7018 public:
7019   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
7020       : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
7021 
7022   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7023                            CodeGen::CodeGenModule &M) const override;
7024   bool shouldEmitStaticExternCAliases() const override;
7025 
7026   llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
7027     // On the device side, surface reference is represented as an object handle
7028     // in 64-bit integer.
7029     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7030   }
7031 
7032   llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
7033     // On the device side, texture reference is represented as an object handle
7034     // in 64-bit integer.
7035     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7036   }
7037 
7038   bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7039                                               LValue Src) const override {
7040     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7041     return true;
7042   }
7043 
7044   bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7045                                               LValue Src) const override {
7046     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7047     return true;
7048   }
7049 
7050 private:
7051   // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
7052   // resulting MDNode to the nvvm.annotations MDNode.
7053   static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
7054                               int Operand);
7055 
7056   static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7057                                            LValue Src) {
7058     llvm::Value *Handle = nullptr;
7059     llvm::Constant *C =
7060         llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
7061     // Lookup `addrspacecast` through the constant pointer if any.
7062     if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
7063       C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
7064     if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
7065       // Load the handle from the specific global variable using
7066       // `nvvm.texsurf.handle.internal` intrinsic.
7067       Handle = CGF.EmitRuntimeCall(
7068           CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
7069                                {GV->getType()}),
7070           {GV}, "texsurf_handle");
7071     } else
7072       Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
7073     CGF.EmitStoreOfScalar(Handle, Dst);
7074   }
7075 };
7076 
7077 /// Checks if the type is unsupported directly by the current target.
7078 bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
7079   ASTContext &Context = getContext();
7080   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
7081     return true;
7082   if (!Context.getTargetInfo().hasFloat128Type() &&
7083       (T->isFloat128Type() ||
7084        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
7085     return true;
7086   if (const auto *EIT = T->getAs<ExtIntType>())
7087     return EIT->getNumBits() >
7088            (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
7089   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
7090       Context.getTypeSize(T) > 64U)
7091     return true;
7092   if (const auto *AT = T->getAsArrayTypeUnsafe())
7093     return isUnsupportedType(AT->getElementType());
7094   const auto *RT = T->getAs<RecordType>();
7095   if (!RT)
7096     return false;
7097   const RecordDecl *RD = RT->getDecl();
7098 
7099   // If this is a C++ record, check the bases first.
7100   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7101     for (const CXXBaseSpecifier &I : CXXRD->bases())
7102       if (isUnsupportedType(I.getType()))
7103         return true;
7104 
7105   for (const FieldDecl *I : RD->fields())
7106     if (isUnsupportedType(I->getType()))
7107       return true;
7108   return false;
7109 }
7110 
7111 /// Coerce the given type into an array with maximum allowed size of elements.
7112 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
7113                                                    unsigned MaxSize) const {
7114   // Alignment and Size are measured in bits.
7115   const uint64_t Size = getContext().getTypeSize(Ty);
7116   const uint64_t Alignment = getContext().getTypeAlign(Ty);
7117   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
7118   llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
7119   const uint64_t NumElements = (Size + Div - 1) / Div;
7120   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
7121 }
7122 
7123 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
7124   if (RetTy->isVoidType())
7125     return ABIArgInfo::getIgnore();
7126 
7127   if (getContext().getLangOpts().OpenMP &&
7128       getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
7129     return coerceToIntArrayWithLimit(RetTy, 64);
7130 
7131   // note: this is different from default ABI
7132   if (!RetTy->isScalarType())
7133     return ABIArgInfo::getDirect();
7134 
7135   // Treat an enum type as its underlying type.
7136   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7137     RetTy = EnumTy->getDecl()->getIntegerType();
7138 
7139   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7140                                                : ABIArgInfo::getDirect());
7141 }
7142 
7143 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
7144   // Treat an enum type as its underlying type.
7145   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7146     Ty = EnumTy->getDecl()->getIntegerType();
7147 
7148   // Return aggregates type as indirect by value
7149   if (isAggregateTypeForABI(Ty)) {
7150     // Under CUDA device compilation, tex/surf builtin types are replaced with
7151     // object types and passed directly.
7152     if (getContext().getLangOpts().CUDAIsDevice) {
7153       if (Ty->isCUDADeviceBuiltinSurfaceType())
7154         return ABIArgInfo::getDirect(
7155             CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
7156       if (Ty->isCUDADeviceBuiltinTextureType())
7157         return ABIArgInfo::getDirect(
7158             CGInfo.getCUDADeviceBuiltinTextureDeviceType());
7159     }
7160     return getNaturalAlignIndirect(Ty, /* byval */ true);
7161   }
7162 
7163   if (const auto *EIT = Ty->getAs<ExtIntType>()) {
7164     if ((EIT->getNumBits() > 128) ||
7165         (!getContext().getTargetInfo().hasInt128Type() &&
7166          EIT->getNumBits() > 64))
7167       return getNaturalAlignIndirect(Ty, /* byval */ true);
7168   }
7169 
7170   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
7171                                             : ABIArgInfo::getDirect());
7172 }
7173 
7174 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
7175   if (!getCXXABI().classifyReturnType(FI))
7176     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7177   for (auto &I : FI.arguments())
7178     I.info = classifyArgumentType(I.type);
7179 
7180   // Always honor user-specified calling convention.
7181   if (FI.getCallingConvention() != llvm::CallingConv::C)
7182     return;
7183 
7184   FI.setEffectiveCallingConvention(getRuntimeCC());
7185 }
7186 
7187 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7188                                 QualType Ty) const {
7189   llvm_unreachable("NVPTX does not support varargs");
7190 }
7191 
7192 void NVPTXTargetCodeGenInfo::setTargetAttributes(
7193     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7194   if (GV->isDeclaration())
7195     return;
7196   const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
7197   if (VD) {
7198     if (M.getLangOpts().CUDA) {
7199       if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
7200         addNVVMMetadata(GV, "surface", 1);
7201       else if (VD->getType()->isCUDADeviceBuiltinTextureType())
7202         addNVVMMetadata(GV, "texture", 1);
7203       return;
7204     }
7205   }
7206 
7207   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7208   if (!FD) return;
7209 
7210   llvm::Function *F = cast<llvm::Function>(GV);
7211 
7212   // Perform special handling in OpenCL mode
7213   if (M.getLangOpts().OpenCL) {
7214     // Use OpenCL function attributes to check for kernel functions
7215     // By default, all functions are device functions
7216     if (FD->hasAttr<OpenCLKernelAttr>()) {
7217       // OpenCL __kernel functions get kernel metadata
7218       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7219       addNVVMMetadata(F, "kernel", 1);
7220       // And kernel functions are not subject to inlining
7221       F->addFnAttr(llvm::Attribute::NoInline);
7222     }
7223   }
7224 
7225   // Perform special handling in CUDA mode.
7226   if (M.getLangOpts().CUDA) {
7227     // CUDA __global__ functions get a kernel metadata entry.  Since
7228     // __global__ functions cannot be called from the device, we do not
7229     // need to set the noinline attribute.
7230     if (FD->hasAttr<CUDAGlobalAttr>()) {
7231       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7232       addNVVMMetadata(F, "kernel", 1);
7233     }
7234     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
7235       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
7236       llvm::APSInt MaxThreads(32);
7237       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
7238       if (MaxThreads > 0)
7239         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
7240 
7241       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
7242       // not specified in __launch_bounds__ or if the user specified a 0 value,
7243       // we don't have to add a PTX directive.
7244       if (Attr->getMinBlocks()) {
7245         llvm::APSInt MinBlocks(32);
7246         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
7247         if (MinBlocks > 0)
7248           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
7249           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
7250       }
7251     }
7252   }
7253 }
7254 
7255 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
7256                                              StringRef Name, int Operand) {
7257   llvm::Module *M = GV->getParent();
7258   llvm::LLVMContext &Ctx = M->getContext();
7259 
7260   // Get "nvvm.annotations" metadata node
7261   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
7262 
7263   llvm::Metadata *MDVals[] = {
7264       llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
7265       llvm::ConstantAsMetadata::get(
7266           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
7267   // Append metadata to nvvm.annotations
7268   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7269 }
7270 
7271 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
7272   return false;
7273 }
7274 }
7275 
7276 //===----------------------------------------------------------------------===//
7277 // SystemZ ABI Implementation
7278 //===----------------------------------------------------------------------===//
7279 
7280 namespace {
7281 
7282 class SystemZABIInfo : public SwiftABIInfo {
7283   bool HasVector;
7284   bool IsSoftFloatABI;
7285 
7286 public:
7287   SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
7288     : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
7289 
7290   bool isPromotableIntegerTypeForABI(QualType Ty) const;
7291   bool isCompoundType(QualType Ty) const;
7292   bool isVectorArgumentType(QualType Ty) const;
7293   bool isFPArgumentType(QualType Ty) const;
7294   QualType GetSingleElementType(QualType Ty) const;
7295 
7296   ABIArgInfo classifyReturnType(QualType RetTy) const;
7297   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
7298 
7299   void computeInfo(CGFunctionInfo &FI) const override {
7300     if (!getCXXABI().classifyReturnType(FI))
7301       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7302     for (auto &I : FI.arguments())
7303       I.info = classifyArgumentType(I.type);
7304   }
7305 
7306   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7307                     QualType Ty) const override;
7308 
7309   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
7310                                     bool asReturnValue) const override {
7311     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
7312   }
7313   bool isSwiftErrorInRegister() const override {
7314     return false;
7315   }
7316 };
7317 
7318 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
7319 public:
7320   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
7321       : TargetCodeGenInfo(
7322             std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {}
7323 
7324   llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID,
7325                           CGBuilderTy &Builder,
7326                           CodeGenModule &CGM) const override {
7327     assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
7328     // Only use TDC in constrained FP mode.
7329     if (!Builder.getIsFPConstrained())
7330       return nullptr;
7331 
7332     llvm::Type *Ty = V->getType();
7333     if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) {
7334       llvm::Module &M = CGM.getModule();
7335       auto &Ctx = M.getContext();
7336       llvm::Function *TDCFunc =
7337           llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty);
7338       unsigned TDCBits = 0;
7339       switch (BuiltinID) {
7340       case Builtin::BI__builtin_isnan:
7341         TDCBits = 0xf;
7342         break;
7343       case Builtin::BIfinite:
7344       case Builtin::BI__finite:
7345       case Builtin::BIfinitef:
7346       case Builtin::BI__finitef:
7347       case Builtin::BIfinitel:
7348       case Builtin::BI__finitel:
7349       case Builtin::BI__builtin_isfinite:
7350         TDCBits = 0xfc0;
7351         break;
7352       case Builtin::BI__builtin_isinf:
7353         TDCBits = 0x30;
7354         break;
7355       default:
7356         break;
7357       }
7358       if (TDCBits)
7359         return Builder.CreateCall(
7360             TDCFunc,
7361             {V, llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), TDCBits)});
7362     }
7363     return nullptr;
7364   }
7365 };
7366 }
7367 
7368 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
7369   // Treat an enum type as its underlying type.
7370   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7371     Ty = EnumTy->getDecl()->getIntegerType();
7372 
7373   // Promotable integer types are required to be promoted by the ABI.
7374   if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
7375     return true;
7376 
7377   if (const auto *EIT = Ty->getAs<ExtIntType>())
7378     if (EIT->getNumBits() < 64)
7379       return true;
7380 
7381   // 32-bit values must also be promoted.
7382   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7383     switch (BT->getKind()) {
7384     case BuiltinType::Int:
7385     case BuiltinType::UInt:
7386       return true;
7387     default:
7388       return false;
7389     }
7390   return false;
7391 }
7392 
7393 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
7394   return (Ty->isAnyComplexType() ||
7395           Ty->isVectorType() ||
7396           isAggregateTypeForABI(Ty));
7397 }
7398 
7399 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
7400   return (HasVector &&
7401           Ty->isVectorType() &&
7402           getContext().getTypeSize(Ty) <= 128);
7403 }
7404 
7405 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
7406   if (IsSoftFloatABI)
7407     return false;
7408 
7409   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7410     switch (BT->getKind()) {
7411     case BuiltinType::Float:
7412     case BuiltinType::Double:
7413       return true;
7414     default:
7415       return false;
7416     }
7417 
7418   return false;
7419 }
7420 
7421 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
7422   const RecordType *RT = Ty->getAs<RecordType>();
7423 
7424   if (RT && RT->isStructureOrClassType()) {
7425     const RecordDecl *RD = RT->getDecl();
7426     QualType Found;
7427 
7428     // If this is a C++ record, check the bases first.
7429     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7430       for (const auto &I : CXXRD->bases()) {
7431         QualType Base = I.getType();
7432 
7433         // Empty bases don't affect things either way.
7434         if (isEmptyRecord(getContext(), Base, true))
7435           continue;
7436 
7437         if (!Found.isNull())
7438           return Ty;
7439         Found = GetSingleElementType(Base);
7440       }
7441 
7442     // Check the fields.
7443     for (const auto *FD : RD->fields()) {
7444       // For compatibility with GCC, ignore empty bitfields in C++ mode.
7445       // Unlike isSingleElementStruct(), empty structure and array fields
7446       // do count.  So do anonymous bitfields that aren't zero-sized.
7447       if (getContext().getLangOpts().CPlusPlus &&
7448           FD->isZeroLengthBitField(getContext()))
7449         continue;
7450       // Like isSingleElementStruct(), ignore C++20 empty data members.
7451       if (FD->hasAttr<NoUniqueAddressAttr>() &&
7452           isEmptyRecord(getContext(), FD->getType(), true))
7453         continue;
7454 
7455       // Unlike isSingleElementStruct(), arrays do not count.
7456       // Nested structures still do though.
7457       if (!Found.isNull())
7458         return Ty;
7459       Found = GetSingleElementType(FD->getType());
7460     }
7461 
7462     // Unlike isSingleElementStruct(), trailing padding is allowed.
7463     // An 8-byte aligned struct s { float f; } is passed as a double.
7464     if (!Found.isNull())
7465       return Found;
7466   }
7467 
7468   return Ty;
7469 }
7470 
7471 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7472                                   QualType Ty) const {
7473   // Assume that va_list type is correct; should be pointer to LLVM type:
7474   // struct {
7475   //   i64 __gpr;
7476   //   i64 __fpr;
7477   //   i8 *__overflow_arg_area;
7478   //   i8 *__reg_save_area;
7479   // };
7480 
7481   // Every non-vector argument occupies 8 bytes and is passed by preference
7482   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
7483   // always passed on the stack.
7484   Ty = getContext().getCanonicalType(Ty);
7485   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7486   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
7487   llvm::Type *DirectTy = ArgTy;
7488   ABIArgInfo AI = classifyArgumentType(Ty);
7489   bool IsIndirect = AI.isIndirect();
7490   bool InFPRs = false;
7491   bool IsVector = false;
7492   CharUnits UnpaddedSize;
7493   CharUnits DirectAlign;
7494   if (IsIndirect) {
7495     DirectTy = llvm::PointerType::getUnqual(DirectTy);
7496     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
7497   } else {
7498     if (AI.getCoerceToType())
7499       ArgTy = AI.getCoerceToType();
7500     InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
7501     IsVector = ArgTy->isVectorTy();
7502     UnpaddedSize = TyInfo.Width;
7503     DirectAlign = TyInfo.Align;
7504   }
7505   CharUnits PaddedSize = CharUnits::fromQuantity(8);
7506   if (IsVector && UnpaddedSize > PaddedSize)
7507     PaddedSize = CharUnits::fromQuantity(16);
7508   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
7509 
7510   CharUnits Padding = (PaddedSize - UnpaddedSize);
7511 
7512   llvm::Type *IndexTy = CGF.Int64Ty;
7513   llvm::Value *PaddedSizeV =
7514     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
7515 
7516   if (IsVector) {
7517     // Work out the address of a vector argument on the stack.
7518     // Vector arguments are always passed in the high bits of a
7519     // single (8 byte) or double (16 byte) stack slot.
7520     Address OverflowArgAreaPtr =
7521         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7522     Address OverflowArgArea =
7523       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7524               TyInfo.Align);
7525     Address MemAddr =
7526       CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
7527 
7528     // Update overflow_arg_area_ptr pointer
7529     llvm::Value *NewOverflowArgArea =
7530       CGF.Builder.CreateGEP(OverflowArgArea.getElementType(),
7531                             OverflowArgArea.getPointer(), PaddedSizeV,
7532                             "overflow_arg_area");
7533     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7534 
7535     return MemAddr;
7536   }
7537 
7538   assert(PaddedSize.getQuantity() == 8);
7539 
7540   unsigned MaxRegs, RegCountField, RegSaveIndex;
7541   CharUnits RegPadding;
7542   if (InFPRs) {
7543     MaxRegs = 4; // Maximum of 4 FPR arguments
7544     RegCountField = 1; // __fpr
7545     RegSaveIndex = 16; // save offset for f0
7546     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
7547   } else {
7548     MaxRegs = 5; // Maximum of 5 GPR arguments
7549     RegCountField = 0; // __gpr
7550     RegSaveIndex = 2; // save offset for r2
7551     RegPadding = Padding; // values are passed in the low bits of a GPR
7552   }
7553 
7554   Address RegCountPtr =
7555       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
7556   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
7557   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
7558   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
7559                                                  "fits_in_regs");
7560 
7561   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
7562   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
7563   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
7564   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
7565 
7566   // Emit code to load the value if it was passed in registers.
7567   CGF.EmitBlock(InRegBlock);
7568 
7569   // Work out the address of an argument register.
7570   llvm::Value *ScaledRegCount =
7571     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
7572   llvm::Value *RegBase =
7573     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
7574                                       + RegPadding.getQuantity());
7575   llvm::Value *RegOffset =
7576     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
7577   Address RegSaveAreaPtr =
7578       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
7579   llvm::Value *RegSaveArea =
7580     CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
7581   Address RawRegAddr(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, RegOffset,
7582                                            "raw_reg_addr"),
7583                      PaddedSize);
7584   Address RegAddr =
7585     CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
7586 
7587   // Update the register count
7588   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
7589   llvm::Value *NewRegCount =
7590     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
7591   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
7592   CGF.EmitBranch(ContBlock);
7593 
7594   // Emit code to load the value if it was passed in memory.
7595   CGF.EmitBlock(InMemBlock);
7596 
7597   // Work out the address of a stack argument.
7598   Address OverflowArgAreaPtr =
7599       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7600   Address OverflowArgArea =
7601     Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7602             PaddedSize);
7603   Address RawMemAddr =
7604     CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
7605   Address MemAddr =
7606     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
7607 
7608   // Update overflow_arg_area_ptr pointer
7609   llvm::Value *NewOverflowArgArea =
7610     CGF.Builder.CreateGEP(OverflowArgArea.getElementType(),
7611                           OverflowArgArea.getPointer(), PaddedSizeV,
7612                           "overflow_arg_area");
7613   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7614   CGF.EmitBranch(ContBlock);
7615 
7616   // Return the appropriate result.
7617   CGF.EmitBlock(ContBlock);
7618   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
7619                                  MemAddr, InMemBlock, "va_arg.addr");
7620 
7621   if (IsIndirect)
7622     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
7623                       TyInfo.Align);
7624 
7625   return ResAddr;
7626 }
7627 
7628 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
7629   if (RetTy->isVoidType())
7630     return ABIArgInfo::getIgnore();
7631   if (isVectorArgumentType(RetTy))
7632     return ABIArgInfo::getDirect();
7633   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
7634     return getNaturalAlignIndirect(RetTy);
7635   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7636                                                : ABIArgInfo::getDirect());
7637 }
7638 
7639 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
7640   // Handle the generic C++ ABI.
7641   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7642     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7643 
7644   // Integers and enums are extended to full register width.
7645   if (isPromotableIntegerTypeForABI(Ty))
7646     return ABIArgInfo::getExtend(Ty);
7647 
7648   // Handle vector types and vector-like structure types.  Note that
7649   // as opposed to float-like structure types, we do not allow any
7650   // padding for vector-like structures, so verify the sizes match.
7651   uint64_t Size = getContext().getTypeSize(Ty);
7652   QualType SingleElementTy = GetSingleElementType(Ty);
7653   if (isVectorArgumentType(SingleElementTy) &&
7654       getContext().getTypeSize(SingleElementTy) == Size)
7655     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
7656 
7657   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
7658   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
7659     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7660 
7661   // Handle small structures.
7662   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7663     // Structures with flexible arrays have variable length, so really
7664     // fail the size test above.
7665     const RecordDecl *RD = RT->getDecl();
7666     if (RD->hasFlexibleArrayMember())
7667       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7668 
7669     // The structure is passed as an unextended integer, a float, or a double.
7670     llvm::Type *PassTy;
7671     if (isFPArgumentType(SingleElementTy)) {
7672       assert(Size == 32 || Size == 64);
7673       if (Size == 32)
7674         PassTy = llvm::Type::getFloatTy(getVMContext());
7675       else
7676         PassTy = llvm::Type::getDoubleTy(getVMContext());
7677     } else
7678       PassTy = llvm::IntegerType::get(getVMContext(), Size);
7679     return ABIArgInfo::getDirect(PassTy);
7680   }
7681 
7682   // Non-structure compounds are passed indirectly.
7683   if (isCompoundType(Ty))
7684     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7685 
7686   return ABIArgInfo::getDirect(nullptr);
7687 }
7688 
7689 //===----------------------------------------------------------------------===//
7690 // MSP430 ABI Implementation
7691 //===----------------------------------------------------------------------===//
7692 
7693 namespace {
7694 
7695 class MSP430ABIInfo : public DefaultABIInfo {
7696   static ABIArgInfo complexArgInfo() {
7697     ABIArgInfo Info = ABIArgInfo::getDirect();
7698     Info.setCanBeFlattened(false);
7699     return Info;
7700   }
7701 
7702 public:
7703   MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7704 
7705   ABIArgInfo classifyReturnType(QualType RetTy) const {
7706     if (RetTy->isAnyComplexType())
7707       return complexArgInfo();
7708 
7709     return DefaultABIInfo::classifyReturnType(RetTy);
7710   }
7711 
7712   ABIArgInfo classifyArgumentType(QualType RetTy) const {
7713     if (RetTy->isAnyComplexType())
7714       return complexArgInfo();
7715 
7716     return DefaultABIInfo::classifyArgumentType(RetTy);
7717   }
7718 
7719   // Just copy the original implementations because
7720   // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
7721   void computeInfo(CGFunctionInfo &FI) const override {
7722     if (!getCXXABI().classifyReturnType(FI))
7723       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7724     for (auto &I : FI.arguments())
7725       I.info = classifyArgumentType(I.type);
7726   }
7727 
7728   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7729                     QualType Ty) const override {
7730     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
7731   }
7732 };
7733 
7734 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7735 public:
7736   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7737       : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
7738   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7739                            CodeGen::CodeGenModule &M) const override;
7740 };
7741 
7742 }
7743 
7744 void MSP430TargetCodeGenInfo::setTargetAttributes(
7745     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7746   if (GV->isDeclaration())
7747     return;
7748   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7749     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7750     if (!InterruptAttr)
7751       return;
7752 
7753     // Handle 'interrupt' attribute:
7754     llvm::Function *F = cast<llvm::Function>(GV);
7755 
7756     // Step 1: Set ISR calling convention.
7757     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7758 
7759     // Step 2: Add attributes goodness.
7760     F->addFnAttr(llvm::Attribute::NoInline);
7761     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7762   }
7763 }
7764 
7765 //===----------------------------------------------------------------------===//
7766 // MIPS ABI Implementation.  This works for both little-endian and
7767 // big-endian variants.
7768 //===----------------------------------------------------------------------===//
7769 
7770 namespace {
7771 class MipsABIInfo : public ABIInfo {
7772   bool IsO32;
7773   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7774   void CoerceToIntArgs(uint64_t TySize,
7775                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7776   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7777   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7778   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7779 public:
7780   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7781     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7782     StackAlignInBytes(IsO32 ? 8 : 16) {}
7783 
7784   ABIArgInfo classifyReturnType(QualType RetTy) const;
7785   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7786   void computeInfo(CGFunctionInfo &FI) const override;
7787   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7788                     QualType Ty) const override;
7789   ABIArgInfo extendType(QualType Ty) const;
7790 };
7791 
7792 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7793   unsigned SizeOfUnwindException;
7794 public:
7795   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7796       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7797         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7798 
7799   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7800     return 29;
7801   }
7802 
7803   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7804                            CodeGen::CodeGenModule &CGM) const override {
7805     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7806     if (!FD) return;
7807     llvm::Function *Fn = cast<llvm::Function>(GV);
7808 
7809     if (FD->hasAttr<MipsLongCallAttr>())
7810       Fn->addFnAttr("long-call");
7811     else if (FD->hasAttr<MipsShortCallAttr>())
7812       Fn->addFnAttr("short-call");
7813 
7814     // Other attributes do not have a meaning for declarations.
7815     if (GV->isDeclaration())
7816       return;
7817 
7818     if (FD->hasAttr<Mips16Attr>()) {
7819       Fn->addFnAttr("mips16");
7820     }
7821     else if (FD->hasAttr<NoMips16Attr>()) {
7822       Fn->addFnAttr("nomips16");
7823     }
7824 
7825     if (FD->hasAttr<MicroMipsAttr>())
7826       Fn->addFnAttr("micromips");
7827     else if (FD->hasAttr<NoMicroMipsAttr>())
7828       Fn->addFnAttr("nomicromips");
7829 
7830     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7831     if (!Attr)
7832       return;
7833 
7834     const char *Kind;
7835     switch (Attr->getInterrupt()) {
7836     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7837     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7838     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7839     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7840     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7841     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7842     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7843     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7844     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7845     }
7846 
7847     Fn->addFnAttr("interrupt", Kind);
7848 
7849   }
7850 
7851   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7852                                llvm::Value *Address) const override;
7853 
7854   unsigned getSizeOfUnwindException() const override {
7855     return SizeOfUnwindException;
7856   }
7857 };
7858 }
7859 
7860 void MipsABIInfo::CoerceToIntArgs(
7861     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7862   llvm::IntegerType *IntTy =
7863     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7864 
7865   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7866   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7867     ArgList.push_back(IntTy);
7868 
7869   // If necessary, add one more integer type to ArgList.
7870   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7871 
7872   if (R)
7873     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7874 }
7875 
7876 // In N32/64, an aligned double precision floating point field is passed in
7877 // a register.
7878 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7879   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7880 
7881   if (IsO32) {
7882     CoerceToIntArgs(TySize, ArgList);
7883     return llvm::StructType::get(getVMContext(), ArgList);
7884   }
7885 
7886   if (Ty->isComplexType())
7887     return CGT.ConvertType(Ty);
7888 
7889   const RecordType *RT = Ty->getAs<RecordType>();
7890 
7891   // Unions/vectors are passed in integer registers.
7892   if (!RT || !RT->isStructureOrClassType()) {
7893     CoerceToIntArgs(TySize, ArgList);
7894     return llvm::StructType::get(getVMContext(), ArgList);
7895   }
7896 
7897   const RecordDecl *RD = RT->getDecl();
7898   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7899   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7900 
7901   uint64_t LastOffset = 0;
7902   unsigned idx = 0;
7903   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7904 
7905   // Iterate over fields in the struct/class and check if there are any aligned
7906   // double fields.
7907   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
7908        i != e; ++i, ++idx) {
7909     const QualType Ty = i->getType();
7910     const BuiltinType *BT = Ty->getAs<BuiltinType>();
7911 
7912     if (!BT || BT->getKind() != BuiltinType::Double)
7913       continue;
7914 
7915     uint64_t Offset = Layout.getFieldOffset(idx);
7916     if (Offset % 64) // Ignore doubles that are not aligned.
7917       continue;
7918 
7919     // Add ((Offset - LastOffset) / 64) args of type i64.
7920     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
7921       ArgList.push_back(I64);
7922 
7923     // Add double type.
7924     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
7925     LastOffset = Offset + 64;
7926   }
7927 
7928   CoerceToIntArgs(TySize - LastOffset, IntArgList);
7929   ArgList.append(IntArgList.begin(), IntArgList.end());
7930 
7931   return llvm::StructType::get(getVMContext(), ArgList);
7932 }
7933 
7934 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
7935                                         uint64_t Offset) const {
7936   if (OrigOffset + MinABIStackAlignInBytes > Offset)
7937     return nullptr;
7938 
7939   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
7940 }
7941 
7942 ABIArgInfo
7943 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
7944   Ty = useFirstFieldIfTransparentUnion(Ty);
7945 
7946   uint64_t OrigOffset = Offset;
7947   uint64_t TySize = getContext().getTypeSize(Ty);
7948   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
7949 
7950   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
7951                    (uint64_t)StackAlignInBytes);
7952   unsigned CurrOffset = llvm::alignTo(Offset, Align);
7953   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
7954 
7955   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
7956     // Ignore empty aggregates.
7957     if (TySize == 0)
7958       return ABIArgInfo::getIgnore();
7959 
7960     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
7961       Offset = OrigOffset + MinABIStackAlignInBytes;
7962       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7963     }
7964 
7965     // If we have reached here, aggregates are passed directly by coercing to
7966     // another structure type. Padding is inserted if the offset of the
7967     // aggregate is unaligned.
7968     ABIArgInfo ArgInfo =
7969         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
7970                               getPaddingType(OrigOffset, CurrOffset));
7971     ArgInfo.setInReg(true);
7972     return ArgInfo;
7973   }
7974 
7975   // Treat an enum type as its underlying type.
7976   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7977     Ty = EnumTy->getDecl()->getIntegerType();
7978 
7979   // Make sure we pass indirectly things that are too large.
7980   if (const auto *EIT = Ty->getAs<ExtIntType>())
7981     if (EIT->getNumBits() > 128 ||
7982         (EIT->getNumBits() > 64 &&
7983          !getContext().getTargetInfo().hasInt128Type()))
7984       return getNaturalAlignIndirect(Ty);
7985 
7986   // All integral types are promoted to the GPR width.
7987   if (Ty->isIntegralOrEnumerationType())
7988     return extendType(Ty);
7989 
7990   return ABIArgInfo::getDirect(
7991       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
7992 }
7993 
7994 llvm::Type*
7995 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
7996   const RecordType *RT = RetTy->getAs<RecordType>();
7997   SmallVector<llvm::Type*, 8> RTList;
7998 
7999   if (RT && RT->isStructureOrClassType()) {
8000     const RecordDecl *RD = RT->getDecl();
8001     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
8002     unsigned FieldCnt = Layout.getFieldCount();
8003 
8004     // N32/64 returns struct/classes in floating point registers if the
8005     // following conditions are met:
8006     // 1. The size of the struct/class is no larger than 128-bit.
8007     // 2. The struct/class has one or two fields all of which are floating
8008     //    point types.
8009     // 3. The offset of the first field is zero (this follows what gcc does).
8010     //
8011     // Any other composite results are returned in integer registers.
8012     //
8013     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
8014       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
8015       for (; b != e; ++b) {
8016         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
8017 
8018         if (!BT || !BT->isFloatingPoint())
8019           break;
8020 
8021         RTList.push_back(CGT.ConvertType(b->getType()));
8022       }
8023 
8024       if (b == e)
8025         return llvm::StructType::get(getVMContext(), RTList,
8026                                      RD->hasAttr<PackedAttr>());
8027 
8028       RTList.clear();
8029     }
8030   }
8031 
8032   CoerceToIntArgs(Size, RTList);
8033   return llvm::StructType::get(getVMContext(), RTList);
8034 }
8035 
8036 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
8037   uint64_t Size = getContext().getTypeSize(RetTy);
8038 
8039   if (RetTy->isVoidType())
8040     return ABIArgInfo::getIgnore();
8041 
8042   // O32 doesn't treat zero-sized structs differently from other structs.
8043   // However, N32/N64 ignores zero sized return values.
8044   if (!IsO32 && Size == 0)
8045     return ABIArgInfo::getIgnore();
8046 
8047   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
8048     if (Size <= 128) {
8049       if (RetTy->isAnyComplexType())
8050         return ABIArgInfo::getDirect();
8051 
8052       // O32 returns integer vectors in registers and N32/N64 returns all small
8053       // aggregates in registers.
8054       if (!IsO32 ||
8055           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
8056         ABIArgInfo ArgInfo =
8057             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
8058         ArgInfo.setInReg(true);
8059         return ArgInfo;
8060       }
8061     }
8062 
8063     return getNaturalAlignIndirect(RetTy);
8064   }
8065 
8066   // Treat an enum type as its underlying type.
8067   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8068     RetTy = EnumTy->getDecl()->getIntegerType();
8069 
8070   // Make sure we pass indirectly things that are too large.
8071   if (const auto *EIT = RetTy->getAs<ExtIntType>())
8072     if (EIT->getNumBits() > 128 ||
8073         (EIT->getNumBits() > 64 &&
8074          !getContext().getTargetInfo().hasInt128Type()))
8075       return getNaturalAlignIndirect(RetTy);
8076 
8077   if (isPromotableIntegerTypeForABI(RetTy))
8078     return ABIArgInfo::getExtend(RetTy);
8079 
8080   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
8081       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
8082     return ABIArgInfo::getSignExtend(RetTy);
8083 
8084   return ABIArgInfo::getDirect();
8085 }
8086 
8087 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
8088   ABIArgInfo &RetInfo = FI.getReturnInfo();
8089   if (!getCXXABI().classifyReturnType(FI))
8090     RetInfo = classifyReturnType(FI.getReturnType());
8091 
8092   // Check if a pointer to an aggregate is passed as a hidden argument.
8093   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
8094 
8095   for (auto &I : FI.arguments())
8096     I.info = classifyArgumentType(I.type, Offset);
8097 }
8098 
8099 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8100                                QualType OrigTy) const {
8101   QualType Ty = OrigTy;
8102 
8103   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
8104   // Pointers are also promoted in the same way but this only matters for N32.
8105   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
8106   unsigned PtrWidth = getTarget().getPointerWidth(0);
8107   bool DidPromote = false;
8108   if ((Ty->isIntegerType() &&
8109           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
8110       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
8111     DidPromote = true;
8112     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
8113                                             Ty->isSignedIntegerType());
8114   }
8115 
8116   auto TyInfo = getContext().getTypeInfoInChars(Ty);
8117 
8118   // The alignment of things in the argument area is never larger than
8119   // StackAlignInBytes.
8120   TyInfo.Align =
8121     std::min(TyInfo.Align, CharUnits::fromQuantity(StackAlignInBytes));
8122 
8123   // MinABIStackAlignInBytes is the size of argument slots on the stack.
8124   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
8125 
8126   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
8127                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
8128 
8129 
8130   // If there was a promotion, "unpromote" into a temporary.
8131   // TODO: can we just use a pointer into a subset of the original slot?
8132   if (DidPromote) {
8133     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
8134     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
8135 
8136     // Truncate down to the right width.
8137     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
8138                                                  : CGF.IntPtrTy);
8139     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
8140     if (OrigTy->isPointerType())
8141       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
8142 
8143     CGF.Builder.CreateStore(V, Temp);
8144     Addr = Temp;
8145   }
8146 
8147   return Addr;
8148 }
8149 
8150 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
8151   int TySize = getContext().getTypeSize(Ty);
8152 
8153   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
8154   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
8155     return ABIArgInfo::getSignExtend(Ty);
8156 
8157   return ABIArgInfo::getExtend(Ty);
8158 }
8159 
8160 bool
8161 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8162                                                llvm::Value *Address) const {
8163   // This information comes from gcc's implementation, which seems to
8164   // as canonical as it gets.
8165 
8166   // Everything on MIPS is 4 bytes.  Double-precision FP registers
8167   // are aliased to pairs of single-precision FP registers.
8168   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8169 
8170   // 0-31 are the general purpose registers, $0 - $31.
8171   // 32-63 are the floating-point registers, $f0 - $f31.
8172   // 64 and 65 are the multiply/divide registers, $hi and $lo.
8173   // 66 is the (notional, I think) register for signal-handler return.
8174   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
8175 
8176   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
8177   // They are one bit wide and ignored here.
8178 
8179   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
8180   // (coprocessor 1 is the FP unit)
8181   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
8182   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
8183   // 176-181 are the DSP accumulator registers.
8184   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
8185   return false;
8186 }
8187 
8188 //===----------------------------------------------------------------------===//
8189 // M68k ABI Implementation
8190 //===----------------------------------------------------------------------===//
8191 
8192 namespace {
8193 
8194 class M68kTargetCodeGenInfo : public TargetCodeGenInfo {
8195 public:
8196   M68kTargetCodeGenInfo(CodeGenTypes &CGT)
8197       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
8198   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8199                            CodeGen::CodeGenModule &M) const override;
8200 };
8201 
8202 } // namespace
8203 
8204 void M68kTargetCodeGenInfo::setTargetAttributes(
8205     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8206   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
8207     if (const auto *attr = FD->getAttr<M68kInterruptAttr>()) {
8208       // Handle 'interrupt' attribute:
8209       llvm::Function *F = cast<llvm::Function>(GV);
8210 
8211       // Step 1: Set ISR calling convention.
8212       F->setCallingConv(llvm::CallingConv::M68k_INTR);
8213 
8214       // Step 2: Add attributes goodness.
8215       F->addFnAttr(llvm::Attribute::NoInline);
8216 
8217       // Step 3: Emit ISR vector alias.
8218       unsigned Num = attr->getNumber() / 2;
8219       llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
8220                                 "__isr_" + Twine(Num), F);
8221     }
8222   }
8223 }
8224 
8225 //===----------------------------------------------------------------------===//
8226 // AVR ABI Implementation. Documented at
8227 // https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
8228 // https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
8229 //===----------------------------------------------------------------------===//
8230 
8231 namespace {
8232 class AVRABIInfo : public DefaultABIInfo {
8233 public:
8234   AVRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8235 
8236   ABIArgInfo classifyReturnType(QualType Ty) const {
8237     // A return struct with size less than or equal to 8 bytes is returned
8238     // directly via registers R18-R25.
8239     if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) <= 64)
8240       return ABIArgInfo::getDirect();
8241     else
8242       return DefaultABIInfo::classifyReturnType(Ty);
8243   }
8244 
8245   // Just copy the original implementation of DefaultABIInfo::computeInfo(),
8246   // since DefaultABIInfo::classify{Return,Argument}Type() are not virtual.
8247   void computeInfo(CGFunctionInfo &FI) const override {
8248     if (!getCXXABI().classifyReturnType(FI))
8249       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8250     for (auto &I : FI.arguments())
8251       I.info = classifyArgumentType(I.type);
8252   }
8253 };
8254 
8255 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
8256 public:
8257   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
8258       : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT)) {}
8259 
8260   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8261                                   const VarDecl *D) const override {
8262     // Check if a global/static variable is defined within address space 1
8263     // but not constant.
8264     LangAS AS = D->getType().getAddressSpace();
8265     if (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 1 &&
8266         !D->getType().isConstQualified())
8267       CGM.getDiags().Report(D->getLocation(),
8268                             diag::err_verify_nonconst_addrspace)
8269           << "__flash";
8270     return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
8271   }
8272 
8273   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8274                            CodeGen::CodeGenModule &CGM) const override {
8275     if (GV->isDeclaration())
8276       return;
8277     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
8278     if (!FD) return;
8279     auto *Fn = cast<llvm::Function>(GV);
8280 
8281     if (FD->getAttr<AVRInterruptAttr>())
8282       Fn->addFnAttr("interrupt");
8283 
8284     if (FD->getAttr<AVRSignalAttr>())
8285       Fn->addFnAttr("signal");
8286   }
8287 };
8288 }
8289 
8290 //===----------------------------------------------------------------------===//
8291 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
8292 // Currently subclassed only to implement custom OpenCL C function attribute
8293 // handling.
8294 //===----------------------------------------------------------------------===//
8295 
8296 namespace {
8297 
8298 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
8299 public:
8300   TCETargetCodeGenInfo(CodeGenTypes &CGT)
8301     : DefaultTargetCodeGenInfo(CGT) {}
8302 
8303   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8304                            CodeGen::CodeGenModule &M) const override;
8305 };
8306 
8307 void TCETargetCodeGenInfo::setTargetAttributes(
8308     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8309   if (GV->isDeclaration())
8310     return;
8311   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8312   if (!FD) return;
8313 
8314   llvm::Function *F = cast<llvm::Function>(GV);
8315 
8316   if (M.getLangOpts().OpenCL) {
8317     if (FD->hasAttr<OpenCLKernelAttr>()) {
8318       // OpenCL C Kernel functions are not subject to inlining
8319       F->addFnAttr(llvm::Attribute::NoInline);
8320       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
8321       if (Attr) {
8322         // Convert the reqd_work_group_size() attributes to metadata.
8323         llvm::LLVMContext &Context = F->getContext();
8324         llvm::NamedMDNode *OpenCLMetadata =
8325             M.getModule().getOrInsertNamedMetadata(
8326                 "opencl.kernel_wg_size_info");
8327 
8328         SmallVector<llvm::Metadata *, 5> Operands;
8329         Operands.push_back(llvm::ConstantAsMetadata::get(F));
8330 
8331         Operands.push_back(
8332             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8333                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8334         Operands.push_back(
8335             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8336                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8337         Operands.push_back(
8338             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8339                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8340 
8341         // Add a boolean constant operand for "required" (true) or "hint"
8342         // (false) for implementing the work_group_size_hint attr later.
8343         // Currently always true as the hint is not yet implemented.
8344         Operands.push_back(
8345             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8346         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8347       }
8348     }
8349   }
8350 }
8351 
8352 }
8353 
8354 //===----------------------------------------------------------------------===//
8355 // Hexagon ABI Implementation
8356 //===----------------------------------------------------------------------===//
8357 
8358 namespace {
8359 
8360 class HexagonABIInfo : public DefaultABIInfo {
8361 public:
8362   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8363 
8364 private:
8365   ABIArgInfo classifyReturnType(QualType RetTy) const;
8366   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8367   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8368 
8369   void computeInfo(CGFunctionInfo &FI) const override;
8370 
8371   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8372                     QualType Ty) const override;
8373   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8374                               QualType Ty) const;
8375   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8376                               QualType Ty) const;
8377   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8378                                    QualType Ty) const;
8379 };
8380 
8381 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8382 public:
8383   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8384       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8385 
8386   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8387     return 29;
8388   }
8389 
8390   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8391                            CodeGen::CodeGenModule &GCM) const override {
8392     if (GV->isDeclaration())
8393       return;
8394     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8395     if (!FD)
8396       return;
8397   }
8398 };
8399 
8400 } // namespace
8401 
8402 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8403   unsigned RegsLeft = 6;
8404   if (!getCXXABI().classifyReturnType(FI))
8405     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8406   for (auto &I : FI.arguments())
8407     I.info = classifyArgumentType(I.type, &RegsLeft);
8408 }
8409 
8410 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8411   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8412                        " through registers");
8413 
8414   if (*RegsLeft == 0)
8415     return false;
8416 
8417   if (Size <= 32) {
8418     (*RegsLeft)--;
8419     return true;
8420   }
8421 
8422   if (2 <= (*RegsLeft & (~1U))) {
8423     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8424     return true;
8425   }
8426 
8427   // Next available register was r5 but candidate was greater than 32-bits so it
8428   // has to go on the stack. However we still consume r5
8429   if (*RegsLeft == 1)
8430     *RegsLeft = 0;
8431 
8432   return false;
8433 }
8434 
8435 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8436                                                 unsigned *RegsLeft) const {
8437   if (!isAggregateTypeForABI(Ty)) {
8438     // Treat an enum type as its underlying type.
8439     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8440       Ty = EnumTy->getDecl()->getIntegerType();
8441 
8442     uint64_t Size = getContext().getTypeSize(Ty);
8443     if (Size <= 64)
8444       HexagonAdjustRegsLeft(Size, RegsLeft);
8445 
8446     if (Size > 64 && Ty->isExtIntType())
8447       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8448 
8449     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8450                                              : ABIArgInfo::getDirect();
8451   }
8452 
8453   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8454     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8455 
8456   // Ignore empty records.
8457   if (isEmptyRecord(getContext(), Ty, true))
8458     return ABIArgInfo::getIgnore();
8459 
8460   uint64_t Size = getContext().getTypeSize(Ty);
8461   unsigned Align = getContext().getTypeAlign(Ty);
8462 
8463   if (Size > 64)
8464     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8465 
8466   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8467     Align = Size <= 32 ? 32 : 64;
8468   if (Size <= Align) {
8469     // Pass in the smallest viable integer type.
8470     if (!llvm::isPowerOf2_64(Size))
8471       Size = llvm::NextPowerOf2(Size);
8472     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8473   }
8474   return DefaultABIInfo::classifyArgumentType(Ty);
8475 }
8476 
8477 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8478   if (RetTy->isVoidType())
8479     return ABIArgInfo::getIgnore();
8480 
8481   const TargetInfo &T = CGT.getTarget();
8482   uint64_t Size = getContext().getTypeSize(RetTy);
8483 
8484   if (RetTy->getAs<VectorType>()) {
8485     // HVX vectors are returned in vector registers or register pairs.
8486     if (T.hasFeature("hvx")) {
8487       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8488       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8489       if (Size == VecSize || Size == 2*VecSize)
8490         return ABIArgInfo::getDirectInReg();
8491     }
8492     // Large vector types should be returned via memory.
8493     if (Size > 64)
8494       return getNaturalAlignIndirect(RetTy);
8495   }
8496 
8497   if (!isAggregateTypeForABI(RetTy)) {
8498     // Treat an enum type as its underlying type.
8499     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8500       RetTy = EnumTy->getDecl()->getIntegerType();
8501 
8502     if (Size > 64 && RetTy->isExtIntType())
8503       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8504 
8505     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8506                                                 : ABIArgInfo::getDirect();
8507   }
8508 
8509   if (isEmptyRecord(getContext(), RetTy, true))
8510     return ABIArgInfo::getIgnore();
8511 
8512   // Aggregates <= 8 bytes are returned in registers, other aggregates
8513   // are returned indirectly.
8514   if (Size <= 64) {
8515     // Return in the smallest viable integer type.
8516     if (!llvm::isPowerOf2_64(Size))
8517       Size = llvm::NextPowerOf2(Size);
8518     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8519   }
8520   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8521 }
8522 
8523 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8524                                             Address VAListAddr,
8525                                             QualType Ty) const {
8526   // Load the overflow area pointer.
8527   Address __overflow_area_pointer_p =
8528       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8529   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8530       __overflow_area_pointer_p, "__overflow_area_pointer");
8531 
8532   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8533   if (Align > 4) {
8534     // Alignment should be a power of 2.
8535     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8536 
8537     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8538     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8539 
8540     // Add offset to the current pointer to access the argument.
8541     __overflow_area_pointer =
8542         CGF.Builder.CreateGEP(CGF.Int8Ty, __overflow_area_pointer, Offset);
8543     llvm::Value *AsInt =
8544         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8545 
8546     // Create a mask which should be "AND"ed
8547     // with (overflow_arg_area + align - 1)
8548     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8549     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8550         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8551         "__overflow_area_pointer.align");
8552   }
8553 
8554   // Get the type of the argument from memory and bitcast
8555   // overflow area pointer to the argument type.
8556   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8557   Address AddrTyped = CGF.Builder.CreateBitCast(
8558       Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)),
8559       llvm::PointerType::getUnqual(PTy));
8560 
8561   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8562   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8563 
8564   __overflow_area_pointer = CGF.Builder.CreateGEP(
8565       CGF.Int8Ty, __overflow_area_pointer,
8566       llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8567       "__overflow_area_pointer.next");
8568   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8569 
8570   return AddrTyped;
8571 }
8572 
8573 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8574                                             Address VAListAddr,
8575                                             QualType Ty) const {
8576   // FIXME: Need to handle alignment
8577   llvm::Type *BP = CGF.Int8PtrTy;
8578   llvm::Type *BPP = CGF.Int8PtrPtrTy;
8579   CGBuilderTy &Builder = CGF.Builder;
8580   Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
8581   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8582   // Handle address alignment for type alignment > 32 bits
8583   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8584   if (TyAlign > 4) {
8585     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8586     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8587     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8588     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8589     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8590   }
8591   llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
8592   Address AddrTyped = Builder.CreateBitCast(
8593       Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy);
8594 
8595   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8596   llvm::Value *NextAddr = Builder.CreateGEP(
8597       CGF.Int8Ty, Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8598   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8599 
8600   return AddrTyped;
8601 }
8602 
8603 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8604                                                  Address VAListAddr,
8605                                                  QualType Ty) const {
8606   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8607 
8608   if (ArgSize > 8)
8609     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8610 
8611   // Here we have check if the argument is in register area or
8612   // in overflow area.
8613   // If the saved register area pointer + argsize rounded up to alignment >
8614   // saved register area end pointer, argument is in overflow area.
8615   unsigned RegsLeft = 6;
8616   Ty = CGF.getContext().getCanonicalType(Ty);
8617   (void)classifyArgumentType(Ty, &RegsLeft);
8618 
8619   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8620   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8621   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8622   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8623 
8624   // Get rounded size of the argument.GCC does not allow vararg of
8625   // size < 4 bytes. We follow the same logic here.
8626   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8627   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8628 
8629   // Argument may be in saved register area
8630   CGF.EmitBlock(MaybeRegBlock);
8631 
8632   // Load the current saved register area pointer.
8633   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8634       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8635   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8636       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8637 
8638   // Load the saved register area end pointer.
8639   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8640       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8641   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8642       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8643 
8644   // If the size of argument is > 4 bytes, check if the stack
8645   // location is aligned to 8 bytes
8646   if (ArgAlign > 4) {
8647 
8648     llvm::Value *__current_saved_reg_area_pointer_int =
8649         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8650                                    CGF.Int32Ty);
8651 
8652     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8653         __current_saved_reg_area_pointer_int,
8654         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8655         "align_current_saved_reg_area_pointer");
8656 
8657     __current_saved_reg_area_pointer_int =
8658         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8659                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8660                               "align_current_saved_reg_area_pointer");
8661 
8662     __current_saved_reg_area_pointer =
8663         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8664                                    __current_saved_reg_area_pointer->getType(),
8665                                    "align_current_saved_reg_area_pointer");
8666   }
8667 
8668   llvm::Value *__new_saved_reg_area_pointer =
8669       CGF.Builder.CreateGEP(CGF.Int8Ty, __current_saved_reg_area_pointer,
8670                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8671                             "__new_saved_reg_area_pointer");
8672 
8673   llvm::Value *UsingStack = 0;
8674   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8675                                          __saved_reg_area_end_pointer);
8676 
8677   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8678 
8679   // Argument in saved register area
8680   // Implement the block where argument is in register saved area
8681   CGF.EmitBlock(InRegBlock);
8682 
8683   llvm::Type *PTy = CGF.ConvertType(Ty);
8684   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8685       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8686 
8687   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8688                           __current_saved_reg_area_pointer_p);
8689 
8690   CGF.EmitBranch(ContBlock);
8691 
8692   // Argument in overflow area
8693   // Implement the block where the argument is in overflow area.
8694   CGF.EmitBlock(OnStackBlock);
8695 
8696   // Load the overflow area pointer
8697   Address __overflow_area_pointer_p =
8698       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8699   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8700       __overflow_area_pointer_p, "__overflow_area_pointer");
8701 
8702   // Align the overflow area pointer according to the alignment of the argument
8703   if (ArgAlign > 4) {
8704     llvm::Value *__overflow_area_pointer_int =
8705         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8706 
8707     __overflow_area_pointer_int =
8708         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8709                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8710                               "align_overflow_area_pointer");
8711 
8712     __overflow_area_pointer_int =
8713         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8714                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8715                               "align_overflow_area_pointer");
8716 
8717     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8718         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8719         "align_overflow_area_pointer");
8720   }
8721 
8722   // Get the pointer for next argument in overflow area and store it
8723   // to overflow area pointer.
8724   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8725       CGF.Int8Ty, __overflow_area_pointer,
8726       llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8727       "__overflow_area_pointer.next");
8728 
8729   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8730                           __overflow_area_pointer_p);
8731 
8732   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8733                           __current_saved_reg_area_pointer_p);
8734 
8735   // Bitcast the overflow area pointer to the type of argument.
8736   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8737   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8738       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8739 
8740   CGF.EmitBranch(ContBlock);
8741 
8742   // Get the correct pointer to load the variable argument
8743   // Implement the ContBlock
8744   CGF.EmitBlock(ContBlock);
8745 
8746   llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
8747   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
8748   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
8749   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
8750 
8751   return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign));
8752 }
8753 
8754 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8755                                   QualType Ty) const {
8756 
8757   if (getTarget().getTriple().isMusl())
8758     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
8759 
8760   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
8761 }
8762 
8763 //===----------------------------------------------------------------------===//
8764 // Lanai ABI Implementation
8765 //===----------------------------------------------------------------------===//
8766 
8767 namespace {
8768 class LanaiABIInfo : public DefaultABIInfo {
8769 public:
8770   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8771 
8772   bool shouldUseInReg(QualType Ty, CCState &State) const;
8773 
8774   void computeInfo(CGFunctionInfo &FI) const override {
8775     CCState State(FI);
8776     // Lanai uses 4 registers to pass arguments unless the function has the
8777     // regparm attribute set.
8778     if (FI.getHasRegParm()) {
8779       State.FreeRegs = FI.getRegParm();
8780     } else {
8781       State.FreeRegs = 4;
8782     }
8783 
8784     if (!getCXXABI().classifyReturnType(FI))
8785       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8786     for (auto &I : FI.arguments())
8787       I.info = classifyArgumentType(I.type, State);
8788   }
8789 
8790   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
8791   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
8792 };
8793 } // end anonymous namespace
8794 
8795 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
8796   unsigned Size = getContext().getTypeSize(Ty);
8797   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
8798 
8799   if (SizeInRegs == 0)
8800     return false;
8801 
8802   if (SizeInRegs > State.FreeRegs) {
8803     State.FreeRegs = 0;
8804     return false;
8805   }
8806 
8807   State.FreeRegs -= SizeInRegs;
8808 
8809   return true;
8810 }
8811 
8812 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
8813                                            CCState &State) const {
8814   if (!ByVal) {
8815     if (State.FreeRegs) {
8816       --State.FreeRegs; // Non-byval indirects just use one pointer.
8817       return getNaturalAlignIndirectInReg(Ty);
8818     }
8819     return getNaturalAlignIndirect(Ty, false);
8820   }
8821 
8822   // Compute the byval alignment.
8823   const unsigned MinABIStackAlignInBytes = 4;
8824   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8825   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8826                                  /*Realign=*/TypeAlign >
8827                                      MinABIStackAlignInBytes);
8828 }
8829 
8830 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
8831                                               CCState &State) const {
8832   // Check with the C++ ABI first.
8833   const RecordType *RT = Ty->getAs<RecordType>();
8834   if (RT) {
8835     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8836     if (RAA == CGCXXABI::RAA_Indirect) {
8837       return getIndirectResult(Ty, /*ByVal=*/false, State);
8838     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
8839       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8840     }
8841   }
8842 
8843   if (isAggregateTypeForABI(Ty)) {
8844     // Structures with flexible arrays are always indirect.
8845     if (RT && RT->getDecl()->hasFlexibleArrayMember())
8846       return getIndirectResult(Ty, /*ByVal=*/true, State);
8847 
8848     // Ignore empty structs/unions.
8849     if (isEmptyRecord(getContext(), Ty, true))
8850       return ABIArgInfo::getIgnore();
8851 
8852     llvm::LLVMContext &LLVMContext = getVMContext();
8853     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
8854     if (SizeInRegs <= State.FreeRegs) {
8855       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
8856       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
8857       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8858       State.FreeRegs -= SizeInRegs;
8859       return ABIArgInfo::getDirectInReg(Result);
8860     } else {
8861       State.FreeRegs = 0;
8862     }
8863     return getIndirectResult(Ty, true, State);
8864   }
8865 
8866   // Treat an enum type as its underlying type.
8867   if (const auto *EnumTy = Ty->getAs<EnumType>())
8868     Ty = EnumTy->getDecl()->getIntegerType();
8869 
8870   bool InReg = shouldUseInReg(Ty, State);
8871 
8872   // Don't pass >64 bit integers in registers.
8873   if (const auto *EIT = Ty->getAs<ExtIntType>())
8874     if (EIT->getNumBits() > 64)
8875       return getIndirectResult(Ty, /*ByVal=*/true, State);
8876 
8877   if (isPromotableIntegerTypeForABI(Ty)) {
8878     if (InReg)
8879       return ABIArgInfo::getDirectInReg();
8880     return ABIArgInfo::getExtend(Ty);
8881   }
8882   if (InReg)
8883     return ABIArgInfo::getDirectInReg();
8884   return ABIArgInfo::getDirect();
8885 }
8886 
8887 namespace {
8888 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
8889 public:
8890   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8891       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
8892 };
8893 }
8894 
8895 //===----------------------------------------------------------------------===//
8896 // AMDGPU ABI Implementation
8897 //===----------------------------------------------------------------------===//
8898 
8899 namespace {
8900 
8901 class AMDGPUABIInfo final : public DefaultABIInfo {
8902 private:
8903   static const unsigned MaxNumRegsForArgsRet = 16;
8904 
8905   unsigned numRegsForType(QualType Ty) const;
8906 
8907   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
8908   bool isHomogeneousAggregateSmallEnough(const Type *Base,
8909                                          uint64_t Members) const override;
8910 
8911   // Coerce HIP scalar pointer arguments from generic pointers to global ones.
8912   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
8913                                        unsigned ToAS) const {
8914     // Single value types.
8915     if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS)
8916       return llvm::PointerType::get(
8917           cast<llvm::PointerType>(Ty)->getElementType(), ToAS);
8918     return Ty;
8919   }
8920 
8921 public:
8922   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
8923     DefaultABIInfo(CGT) {}
8924 
8925   ABIArgInfo classifyReturnType(QualType RetTy) const;
8926   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
8927   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
8928 
8929   void computeInfo(CGFunctionInfo &FI) const override;
8930   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8931                     QualType Ty) const override;
8932 };
8933 
8934 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
8935   return true;
8936 }
8937 
8938 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
8939   const Type *Base, uint64_t Members) const {
8940   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
8941 
8942   // Homogeneous Aggregates may occupy at most 16 registers.
8943   return Members * NumRegs <= MaxNumRegsForArgsRet;
8944 }
8945 
8946 /// Estimate number of registers the type will use when passed in registers.
8947 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
8948   unsigned NumRegs = 0;
8949 
8950   if (const VectorType *VT = Ty->getAs<VectorType>()) {
8951     // Compute from the number of elements. The reported size is based on the
8952     // in-memory size, which includes the padding 4th element for 3-vectors.
8953     QualType EltTy = VT->getElementType();
8954     unsigned EltSize = getContext().getTypeSize(EltTy);
8955 
8956     // 16-bit element vectors should be passed as packed.
8957     if (EltSize == 16)
8958       return (VT->getNumElements() + 1) / 2;
8959 
8960     unsigned EltNumRegs = (EltSize + 31) / 32;
8961     return EltNumRegs * VT->getNumElements();
8962   }
8963 
8964   if (const RecordType *RT = Ty->getAs<RecordType>()) {
8965     const RecordDecl *RD = RT->getDecl();
8966     assert(!RD->hasFlexibleArrayMember());
8967 
8968     for (const FieldDecl *Field : RD->fields()) {
8969       QualType FieldTy = Field->getType();
8970       NumRegs += numRegsForType(FieldTy);
8971     }
8972 
8973     return NumRegs;
8974   }
8975 
8976   return (getContext().getTypeSize(Ty) + 31) / 32;
8977 }
8978 
8979 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
8980   llvm::CallingConv::ID CC = FI.getCallingConvention();
8981 
8982   if (!getCXXABI().classifyReturnType(FI))
8983     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8984 
8985   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
8986   for (auto &Arg : FI.arguments()) {
8987     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
8988       Arg.info = classifyKernelArgumentType(Arg.type);
8989     } else {
8990       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
8991     }
8992   }
8993 }
8994 
8995 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8996                                  QualType Ty) const {
8997   llvm_unreachable("AMDGPU does not support varargs");
8998 }
8999 
9000 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
9001   if (isAggregateTypeForABI(RetTy)) {
9002     // Records with non-trivial destructors/copy-constructors should not be
9003     // returned by value.
9004     if (!getRecordArgABI(RetTy, getCXXABI())) {
9005       // Ignore empty structs/unions.
9006       if (isEmptyRecord(getContext(), RetTy, true))
9007         return ABIArgInfo::getIgnore();
9008 
9009       // Lower single-element structs to just return a regular value.
9010       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
9011         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9012 
9013       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
9014         const RecordDecl *RD = RT->getDecl();
9015         if (RD->hasFlexibleArrayMember())
9016           return DefaultABIInfo::classifyReturnType(RetTy);
9017       }
9018 
9019       // Pack aggregates <= 4 bytes into single VGPR or pair.
9020       uint64_t Size = getContext().getTypeSize(RetTy);
9021       if (Size <= 16)
9022         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9023 
9024       if (Size <= 32)
9025         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9026 
9027       if (Size <= 64) {
9028         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9029         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9030       }
9031 
9032       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
9033         return ABIArgInfo::getDirect();
9034     }
9035   }
9036 
9037   // Otherwise just do the default thing.
9038   return DefaultABIInfo::classifyReturnType(RetTy);
9039 }
9040 
9041 /// For kernels all parameters are really passed in a special buffer. It doesn't
9042 /// make sense to pass anything byval, so everything must be direct.
9043 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
9044   Ty = useFirstFieldIfTransparentUnion(Ty);
9045 
9046   // TODO: Can we omit empty structs?
9047 
9048   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9049     Ty = QualType(SeltTy, 0);
9050 
9051   llvm::Type *OrigLTy = CGT.ConvertType(Ty);
9052   llvm::Type *LTy = OrigLTy;
9053   if (getContext().getLangOpts().HIP) {
9054     LTy = coerceKernelArgumentType(
9055         OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
9056         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
9057   }
9058 
9059   // FIXME: Should also use this for OpenCL, but it requires addressing the
9060   // problem of kernels being called.
9061   //
9062   // FIXME: This doesn't apply the optimization of coercing pointers in structs
9063   // to global address space when using byref. This would require implementing a
9064   // new kind of coercion of the in-memory type when for indirect arguments.
9065   if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
9066       isAggregateTypeForABI(Ty)) {
9067     return ABIArgInfo::getIndirectAliased(
9068         getContext().getTypeAlignInChars(Ty),
9069         getContext().getTargetAddressSpace(LangAS::opencl_constant),
9070         false /*Realign*/, nullptr /*Padding*/);
9071   }
9072 
9073   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
9074   // individual elements, which confuses the Clover OpenCL backend; therefore we
9075   // have to set it to false here. Other args of getDirect() are just defaults.
9076   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
9077 }
9078 
9079 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
9080                                                unsigned &NumRegsLeft) const {
9081   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
9082 
9083   Ty = useFirstFieldIfTransparentUnion(Ty);
9084 
9085   if (isAggregateTypeForABI(Ty)) {
9086     // Records with non-trivial destructors/copy-constructors should not be
9087     // passed by value.
9088     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
9089       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9090 
9091     // Ignore empty structs/unions.
9092     if (isEmptyRecord(getContext(), Ty, true))
9093       return ABIArgInfo::getIgnore();
9094 
9095     // Lower single-element structs to just pass a regular value. TODO: We
9096     // could do reasonable-size multiple-element structs too, using getExpand(),
9097     // though watch out for things like bitfields.
9098     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9099       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9100 
9101     if (const RecordType *RT = Ty->getAs<RecordType>()) {
9102       const RecordDecl *RD = RT->getDecl();
9103       if (RD->hasFlexibleArrayMember())
9104         return DefaultABIInfo::classifyArgumentType(Ty);
9105     }
9106 
9107     // Pack aggregates <= 8 bytes into single VGPR or pair.
9108     uint64_t Size = getContext().getTypeSize(Ty);
9109     if (Size <= 64) {
9110       unsigned NumRegs = (Size + 31) / 32;
9111       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
9112 
9113       if (Size <= 16)
9114         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9115 
9116       if (Size <= 32)
9117         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9118 
9119       // XXX: Should this be i64 instead, and should the limit increase?
9120       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9121       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9122     }
9123 
9124     if (NumRegsLeft > 0) {
9125       unsigned NumRegs = numRegsForType(Ty);
9126       if (NumRegsLeft >= NumRegs) {
9127         NumRegsLeft -= NumRegs;
9128         return ABIArgInfo::getDirect();
9129       }
9130     }
9131   }
9132 
9133   // Otherwise just do the default thing.
9134   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
9135   if (!ArgInfo.isIndirect()) {
9136     unsigned NumRegs = numRegsForType(Ty);
9137     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
9138   }
9139 
9140   return ArgInfo;
9141 }
9142 
9143 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
9144 public:
9145   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
9146       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
9147   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
9148                            CodeGen::CodeGenModule &M) const override;
9149   unsigned getOpenCLKernelCallingConv() const override;
9150 
9151   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
9152       llvm::PointerType *T, QualType QT) const override;
9153 
9154   LangAS getASTAllocaAddressSpace() const override {
9155     return getLangASFromTargetAS(
9156         getABIInfo().getDataLayout().getAllocaAddrSpace());
9157   }
9158   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
9159                                   const VarDecl *D) const override;
9160   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
9161                                          SyncScope Scope,
9162                                          llvm::AtomicOrdering Ordering,
9163                                          llvm::LLVMContext &Ctx) const override;
9164   llvm::Function *
9165   createEnqueuedBlockKernel(CodeGenFunction &CGF,
9166                             llvm::Function *BlockInvokeFunc,
9167                             llvm::Value *BlockLiteral) const override;
9168   bool shouldEmitStaticExternCAliases() const override;
9169   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
9170 };
9171 }
9172 
9173 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
9174                                               llvm::GlobalValue *GV) {
9175   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
9176     return false;
9177 
9178   return D->hasAttr<OpenCLKernelAttr>() ||
9179          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
9180          (isa<VarDecl>(D) &&
9181           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
9182            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
9183            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
9184 }
9185 
9186 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
9187     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
9188   if (requiresAMDGPUProtectedVisibility(D, GV)) {
9189     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
9190     GV->setDSOLocal(true);
9191   }
9192 
9193   if (GV->isDeclaration())
9194     return;
9195   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
9196   if (!FD)
9197     return;
9198 
9199   llvm::Function *F = cast<llvm::Function>(GV);
9200 
9201   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
9202     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
9203 
9204 
9205   const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
9206                               FD->hasAttr<OpenCLKernelAttr>();
9207   const bool IsHIPKernel = M.getLangOpts().HIP &&
9208                            FD->hasAttr<CUDAGlobalAttr>();
9209   if ((IsOpenCLKernel || IsHIPKernel) &&
9210       (M.getTriple().getOS() == llvm::Triple::AMDHSA))
9211     F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
9212 
9213   if (IsHIPKernel)
9214     F->addFnAttr("uniform-work-group-size", "true");
9215 
9216 
9217   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
9218   if (ReqdWGS || FlatWGS) {
9219     unsigned Min = 0;
9220     unsigned Max = 0;
9221     if (FlatWGS) {
9222       Min = FlatWGS->getMin()
9223                 ->EvaluateKnownConstInt(M.getContext())
9224                 .getExtValue();
9225       Max = FlatWGS->getMax()
9226                 ->EvaluateKnownConstInt(M.getContext())
9227                 .getExtValue();
9228     }
9229     if (ReqdWGS && Min == 0 && Max == 0)
9230       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
9231 
9232     if (Min != 0) {
9233       assert(Min <= Max && "Min must be less than or equal Max");
9234 
9235       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
9236       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9237     } else
9238       assert(Max == 0 && "Max must be zero");
9239   } else if (IsOpenCLKernel || IsHIPKernel) {
9240     // By default, restrict the maximum size to a value specified by
9241     // --gpu-max-threads-per-block=n or its default value for HIP.
9242     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
9243     const unsigned DefaultMaxWorkGroupSize =
9244         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
9245                        : M.getLangOpts().GPUMaxThreadsPerBlock;
9246     std::string AttrVal =
9247         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
9248     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9249   }
9250 
9251   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
9252     unsigned Min =
9253         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
9254     unsigned Max = Attr->getMax() ? Attr->getMax()
9255                                         ->EvaluateKnownConstInt(M.getContext())
9256                                         .getExtValue()
9257                                   : 0;
9258 
9259     if (Min != 0) {
9260       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
9261 
9262       std::string AttrVal = llvm::utostr(Min);
9263       if (Max != 0)
9264         AttrVal = AttrVal + "," + llvm::utostr(Max);
9265       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
9266     } else
9267       assert(Max == 0 && "Max must be zero");
9268   }
9269 
9270   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
9271     unsigned NumSGPR = Attr->getNumSGPR();
9272 
9273     if (NumSGPR != 0)
9274       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
9275   }
9276 
9277   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
9278     uint32_t NumVGPR = Attr->getNumVGPR();
9279 
9280     if (NumVGPR != 0)
9281       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
9282   }
9283 
9284   if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics())
9285     F->addFnAttr("amdgpu-unsafe-fp-atomics", "true");
9286 
9287   if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
9288     F->addFnAttr("amdgpu-ieee", "false");
9289 }
9290 
9291 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9292   return llvm::CallingConv::AMDGPU_KERNEL;
9293 }
9294 
9295 // Currently LLVM assumes null pointers always have value 0,
9296 // which results in incorrectly transformed IR. Therefore, instead of
9297 // emitting null pointers in private and local address spaces, a null
9298 // pointer in generic address space is emitted which is casted to a
9299 // pointer in local or private address space.
9300 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
9301     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
9302     QualType QT) const {
9303   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
9304     return llvm::ConstantPointerNull::get(PT);
9305 
9306   auto &Ctx = CGM.getContext();
9307   auto NPT = llvm::PointerType::get(PT->getElementType(),
9308       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
9309   return llvm::ConstantExpr::getAddrSpaceCast(
9310       llvm::ConstantPointerNull::get(NPT), PT);
9311 }
9312 
9313 LangAS
9314 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
9315                                                   const VarDecl *D) const {
9316   assert(!CGM.getLangOpts().OpenCL &&
9317          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
9318          "Address space agnostic languages only");
9319   LangAS DefaultGlobalAS = getLangASFromTargetAS(
9320       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
9321   if (!D)
9322     return DefaultGlobalAS;
9323 
9324   LangAS AddrSpace = D->getType().getAddressSpace();
9325   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9326   if (AddrSpace != LangAS::Default)
9327     return AddrSpace;
9328 
9329   if (CGM.isTypeConstant(D->getType(), false)) {
9330     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9331       return ConstAS.getValue();
9332   }
9333   return DefaultGlobalAS;
9334 }
9335 
9336 llvm::SyncScope::ID
9337 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9338                                             SyncScope Scope,
9339                                             llvm::AtomicOrdering Ordering,
9340                                             llvm::LLVMContext &Ctx) const {
9341   std::string Name;
9342   switch (Scope) {
9343   case SyncScope::OpenCLWorkGroup:
9344     Name = "workgroup";
9345     break;
9346   case SyncScope::OpenCLDevice:
9347     Name = "agent";
9348     break;
9349   case SyncScope::OpenCLAllSVMDevices:
9350     Name = "";
9351     break;
9352   case SyncScope::OpenCLSubGroup:
9353     Name = "wavefront";
9354   }
9355 
9356   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9357     if (!Name.empty())
9358       Name = Twine(Twine(Name) + Twine("-")).str();
9359 
9360     Name = Twine(Twine(Name) + Twine("one-as")).str();
9361   }
9362 
9363   return Ctx.getOrInsertSyncScopeID(Name);
9364 }
9365 
9366 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9367   return false;
9368 }
9369 
9370 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9371     const FunctionType *&FT) const {
9372   FT = getABIInfo().getContext().adjustFunctionType(
9373       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9374 }
9375 
9376 //===----------------------------------------------------------------------===//
9377 // SPARC v8 ABI Implementation.
9378 // Based on the SPARC Compliance Definition version 2.4.1.
9379 //
9380 // Ensures that complex values are passed in registers.
9381 //
9382 namespace {
9383 class SparcV8ABIInfo : public DefaultABIInfo {
9384 public:
9385   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9386 
9387 private:
9388   ABIArgInfo classifyReturnType(QualType RetTy) const;
9389   void computeInfo(CGFunctionInfo &FI) const override;
9390 };
9391 } // end anonymous namespace
9392 
9393 
9394 ABIArgInfo
9395 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9396   if (Ty->isAnyComplexType()) {
9397     return ABIArgInfo::getDirect();
9398   }
9399   else {
9400     return DefaultABIInfo::classifyReturnType(Ty);
9401   }
9402 }
9403 
9404 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9405 
9406   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9407   for (auto &Arg : FI.arguments())
9408     Arg.info = classifyArgumentType(Arg.type);
9409 }
9410 
9411 namespace {
9412 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9413 public:
9414   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9415       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9416 };
9417 } // end anonymous namespace
9418 
9419 //===----------------------------------------------------------------------===//
9420 // SPARC v9 ABI Implementation.
9421 // Based on the SPARC Compliance Definition version 2.4.1.
9422 //
9423 // Function arguments a mapped to a nominal "parameter array" and promoted to
9424 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9425 // the array, structs larger than 16 bytes are passed indirectly.
9426 //
9427 // One case requires special care:
9428 //
9429 //   struct mixed {
9430 //     int i;
9431 //     float f;
9432 //   };
9433 //
9434 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9435 // parameter array, but the int is passed in an integer register, and the float
9436 // is passed in a floating point register. This is represented as two arguments
9437 // with the LLVM IR inreg attribute:
9438 //
9439 //   declare void f(i32 inreg %i, float inreg %f)
9440 //
9441 // The code generator will only allocate 4 bytes from the parameter array for
9442 // the inreg arguments. All other arguments are allocated a multiple of 8
9443 // bytes.
9444 //
9445 namespace {
9446 class SparcV9ABIInfo : public ABIInfo {
9447 public:
9448   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9449 
9450 private:
9451   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9452   void computeInfo(CGFunctionInfo &FI) const override;
9453   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9454                     QualType Ty) const override;
9455 
9456   // Coercion type builder for structs passed in registers. The coercion type
9457   // serves two purposes:
9458   //
9459   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9460   //    in registers.
9461   // 2. Expose aligned floating point elements as first-level elements, so the
9462   //    code generator knows to pass them in floating point registers.
9463   //
9464   // We also compute the InReg flag which indicates that the struct contains
9465   // aligned 32-bit floats.
9466   //
9467   struct CoerceBuilder {
9468     llvm::LLVMContext &Context;
9469     const llvm::DataLayout &DL;
9470     SmallVector<llvm::Type*, 8> Elems;
9471     uint64_t Size;
9472     bool InReg;
9473 
9474     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9475       : Context(c), DL(dl), Size(0), InReg(false) {}
9476 
9477     // Pad Elems with integers until Size is ToSize.
9478     void pad(uint64_t ToSize) {
9479       assert(ToSize >= Size && "Cannot remove elements");
9480       if (ToSize == Size)
9481         return;
9482 
9483       // Finish the current 64-bit word.
9484       uint64_t Aligned = llvm::alignTo(Size, 64);
9485       if (Aligned > Size && Aligned <= ToSize) {
9486         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9487         Size = Aligned;
9488       }
9489 
9490       // Add whole 64-bit words.
9491       while (Size + 64 <= ToSize) {
9492         Elems.push_back(llvm::Type::getInt64Ty(Context));
9493         Size += 64;
9494       }
9495 
9496       // Final in-word padding.
9497       if (Size < ToSize) {
9498         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9499         Size = ToSize;
9500       }
9501     }
9502 
9503     // Add a floating point element at Offset.
9504     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9505       // Unaligned floats are treated as integers.
9506       if (Offset % Bits)
9507         return;
9508       // The InReg flag is only required if there are any floats < 64 bits.
9509       if (Bits < 64)
9510         InReg = true;
9511       pad(Offset);
9512       Elems.push_back(Ty);
9513       Size = Offset + Bits;
9514     }
9515 
9516     // Add a struct type to the coercion type, starting at Offset (in bits).
9517     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9518       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9519       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9520         llvm::Type *ElemTy = StrTy->getElementType(i);
9521         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9522         switch (ElemTy->getTypeID()) {
9523         case llvm::Type::StructTyID:
9524           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9525           break;
9526         case llvm::Type::FloatTyID:
9527           addFloat(ElemOffset, ElemTy, 32);
9528           break;
9529         case llvm::Type::DoubleTyID:
9530           addFloat(ElemOffset, ElemTy, 64);
9531           break;
9532         case llvm::Type::FP128TyID:
9533           addFloat(ElemOffset, ElemTy, 128);
9534           break;
9535         case llvm::Type::PointerTyID:
9536           if (ElemOffset % 64 == 0) {
9537             pad(ElemOffset);
9538             Elems.push_back(ElemTy);
9539             Size += 64;
9540           }
9541           break;
9542         default:
9543           break;
9544         }
9545       }
9546     }
9547 
9548     // Check if Ty is a usable substitute for the coercion type.
9549     bool isUsableType(llvm::StructType *Ty) const {
9550       return llvm::makeArrayRef(Elems) == Ty->elements();
9551     }
9552 
9553     // Get the coercion type as a literal struct type.
9554     llvm::Type *getType() const {
9555       if (Elems.size() == 1)
9556         return Elems.front();
9557       else
9558         return llvm::StructType::get(Context, Elems);
9559     }
9560   };
9561 };
9562 } // end anonymous namespace
9563 
9564 ABIArgInfo
9565 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9566   if (Ty->isVoidType())
9567     return ABIArgInfo::getIgnore();
9568 
9569   uint64_t Size = getContext().getTypeSize(Ty);
9570 
9571   // Anything too big to fit in registers is passed with an explicit indirect
9572   // pointer / sret pointer.
9573   if (Size > SizeLimit)
9574     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9575 
9576   // Treat an enum type as its underlying type.
9577   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9578     Ty = EnumTy->getDecl()->getIntegerType();
9579 
9580   // Integer types smaller than a register are extended.
9581   if (Size < 64 && Ty->isIntegerType())
9582     return ABIArgInfo::getExtend(Ty);
9583 
9584   if (const auto *EIT = Ty->getAs<ExtIntType>())
9585     if (EIT->getNumBits() < 64)
9586       return ABIArgInfo::getExtend(Ty);
9587 
9588   // Other non-aggregates go in registers.
9589   if (!isAggregateTypeForABI(Ty))
9590     return ABIArgInfo::getDirect();
9591 
9592   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9593   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9594   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9595     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9596 
9597   // This is a small aggregate type that should be passed in registers.
9598   // Build a coercion type from the LLVM struct type.
9599   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9600   if (!StrTy)
9601     return ABIArgInfo::getDirect();
9602 
9603   CoerceBuilder CB(getVMContext(), getDataLayout());
9604   CB.addStruct(0, StrTy);
9605   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9606 
9607   // Try to use the original type for coercion.
9608   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9609 
9610   if (CB.InReg)
9611     return ABIArgInfo::getDirectInReg(CoerceTy);
9612   else
9613     return ABIArgInfo::getDirect(CoerceTy);
9614 }
9615 
9616 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9617                                   QualType Ty) const {
9618   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9619   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9620   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9621     AI.setCoerceToType(ArgTy);
9622 
9623   CharUnits SlotSize = CharUnits::fromQuantity(8);
9624 
9625   CGBuilderTy &Builder = CGF.Builder;
9626   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
9627   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9628 
9629   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9630 
9631   Address ArgAddr = Address::invalid();
9632   CharUnits Stride;
9633   switch (AI.getKind()) {
9634   case ABIArgInfo::Expand:
9635   case ABIArgInfo::CoerceAndExpand:
9636   case ABIArgInfo::InAlloca:
9637     llvm_unreachable("Unsupported ABI kind for va_arg");
9638 
9639   case ABIArgInfo::Extend: {
9640     Stride = SlotSize;
9641     CharUnits Offset = SlotSize - TypeInfo.Width;
9642     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9643     break;
9644   }
9645 
9646   case ABIArgInfo::Direct: {
9647     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9648     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9649     ArgAddr = Addr;
9650     break;
9651   }
9652 
9653   case ABIArgInfo::Indirect:
9654   case ABIArgInfo::IndirectAliased:
9655     Stride = SlotSize;
9656     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9657     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
9658                       TypeInfo.Align);
9659     break;
9660 
9661   case ABIArgInfo::Ignore:
9662     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.Align);
9663   }
9664 
9665   // Update VAList.
9666   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9667   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9668 
9669   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
9670 }
9671 
9672 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9673   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9674   for (auto &I : FI.arguments())
9675     I.info = classifyType(I.type, 16 * 8);
9676 }
9677 
9678 namespace {
9679 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9680 public:
9681   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9682       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9683 
9684   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9685     return 14;
9686   }
9687 
9688   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9689                                llvm::Value *Address) const override;
9690 };
9691 } // end anonymous namespace
9692 
9693 bool
9694 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9695                                                 llvm::Value *Address) const {
9696   // This is calculated from the LLVM and GCC tables and verified
9697   // against gcc output.  AFAIK all ABIs use the same encoding.
9698 
9699   CodeGen::CGBuilderTy &Builder = CGF.Builder;
9700 
9701   llvm::IntegerType *i8 = CGF.Int8Ty;
9702   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
9703   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
9704 
9705   // 0-31: the 8-byte general-purpose registers
9706   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
9707 
9708   // 32-63: f0-31, the 4-byte floating-point registers
9709   AssignToArrayRange(Builder, Address, Four8, 32, 63);
9710 
9711   //   Y   = 64
9712   //   PSR = 65
9713   //   WIM = 66
9714   //   TBR = 67
9715   //   PC  = 68
9716   //   NPC = 69
9717   //   FSR = 70
9718   //   CSR = 71
9719   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
9720 
9721   // 72-87: d0-15, the 8-byte floating-point registers
9722   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
9723 
9724   return false;
9725 }
9726 
9727 // ARC ABI implementation.
9728 namespace {
9729 
9730 class ARCABIInfo : public DefaultABIInfo {
9731 public:
9732   using DefaultABIInfo::DefaultABIInfo;
9733 
9734 private:
9735   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9736                     QualType Ty) const override;
9737 
9738   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
9739     if (!State.FreeRegs)
9740       return;
9741     if (Info.isIndirect() && Info.getInReg())
9742       State.FreeRegs--;
9743     else if (Info.isDirect() && Info.getInReg()) {
9744       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
9745       if (sz < State.FreeRegs)
9746         State.FreeRegs -= sz;
9747       else
9748         State.FreeRegs = 0;
9749     }
9750   }
9751 
9752   void computeInfo(CGFunctionInfo &FI) const override {
9753     CCState State(FI);
9754     // ARC uses 8 registers to pass arguments.
9755     State.FreeRegs = 8;
9756 
9757     if (!getCXXABI().classifyReturnType(FI))
9758       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9759     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
9760     for (auto &I : FI.arguments()) {
9761       I.info = classifyArgumentType(I.type, State.FreeRegs);
9762       updateState(I.info, I.type, State);
9763     }
9764   }
9765 
9766   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
9767   ABIArgInfo getIndirectByValue(QualType Ty) const;
9768   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
9769   ABIArgInfo classifyReturnType(QualType RetTy) const;
9770 };
9771 
9772 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
9773 public:
9774   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
9775       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
9776 };
9777 
9778 
9779 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
9780   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
9781                        getNaturalAlignIndirect(Ty, false);
9782 }
9783 
9784 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
9785   // Compute the byval alignment.
9786   const unsigned MinABIStackAlignInBytes = 4;
9787   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9788   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9789                                  TypeAlign > MinABIStackAlignInBytes);
9790 }
9791 
9792 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9793                               QualType Ty) const {
9794   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
9795                           getContext().getTypeInfoInChars(Ty),
9796                           CharUnits::fromQuantity(4), true);
9797 }
9798 
9799 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
9800                                             uint8_t FreeRegs) const {
9801   // Handle the generic C++ ABI.
9802   const RecordType *RT = Ty->getAs<RecordType>();
9803   if (RT) {
9804     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
9805     if (RAA == CGCXXABI::RAA_Indirect)
9806       return getIndirectByRef(Ty, FreeRegs > 0);
9807 
9808     if (RAA == CGCXXABI::RAA_DirectInMemory)
9809       return getIndirectByValue(Ty);
9810   }
9811 
9812   // Treat an enum type as its underlying type.
9813   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9814     Ty = EnumTy->getDecl()->getIntegerType();
9815 
9816   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
9817 
9818   if (isAggregateTypeForABI(Ty)) {
9819     // Structures with flexible arrays are always indirect.
9820     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9821       return getIndirectByValue(Ty);
9822 
9823     // Ignore empty structs/unions.
9824     if (isEmptyRecord(getContext(), Ty, true))
9825       return ABIArgInfo::getIgnore();
9826 
9827     llvm::LLVMContext &LLVMContext = getVMContext();
9828 
9829     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9830     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9831     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9832 
9833     return FreeRegs >= SizeInRegs ?
9834         ABIArgInfo::getDirectInReg(Result) :
9835         ABIArgInfo::getDirect(Result, 0, nullptr, false);
9836   }
9837 
9838   if (const auto *EIT = Ty->getAs<ExtIntType>())
9839     if (EIT->getNumBits() > 64)
9840       return getIndirectByValue(Ty);
9841 
9842   return isPromotableIntegerTypeForABI(Ty)
9843              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
9844                                        : ABIArgInfo::getExtend(Ty))
9845              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
9846                                        : ABIArgInfo::getDirect());
9847 }
9848 
9849 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
9850   if (RetTy->isAnyComplexType())
9851     return ABIArgInfo::getDirectInReg();
9852 
9853   // Arguments of size > 4 registers are indirect.
9854   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
9855   if (RetSize > 4)
9856     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
9857 
9858   return DefaultABIInfo::classifyReturnType(RetTy);
9859 }
9860 
9861 } // End anonymous namespace.
9862 
9863 //===----------------------------------------------------------------------===//
9864 // XCore ABI Implementation
9865 //===----------------------------------------------------------------------===//
9866 
9867 namespace {
9868 
9869 /// A SmallStringEnc instance is used to build up the TypeString by passing
9870 /// it by reference between functions that append to it.
9871 typedef llvm::SmallString<128> SmallStringEnc;
9872 
9873 /// TypeStringCache caches the meta encodings of Types.
9874 ///
9875 /// The reason for caching TypeStrings is two fold:
9876 ///   1. To cache a type's encoding for later uses;
9877 ///   2. As a means to break recursive member type inclusion.
9878 ///
9879 /// A cache Entry can have a Status of:
9880 ///   NonRecursive:   The type encoding is not recursive;
9881 ///   Recursive:      The type encoding is recursive;
9882 ///   Incomplete:     An incomplete TypeString;
9883 ///   IncompleteUsed: An incomplete TypeString that has been used in a
9884 ///                   Recursive type encoding.
9885 ///
9886 /// A NonRecursive entry will have all of its sub-members expanded as fully
9887 /// as possible. Whilst it may contain types which are recursive, the type
9888 /// itself is not recursive and thus its encoding may be safely used whenever
9889 /// the type is encountered.
9890 ///
9891 /// A Recursive entry will have all of its sub-members expanded as fully as
9892 /// possible. The type itself is recursive and it may contain other types which
9893 /// are recursive. The Recursive encoding must not be used during the expansion
9894 /// of a recursive type's recursive branch. For simplicity the code uses
9895 /// IncompleteCount to reject all usage of Recursive encodings for member types.
9896 ///
9897 /// An Incomplete entry is always a RecordType and only encodes its
9898 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
9899 /// are placed into the cache during type expansion as a means to identify and
9900 /// handle recursive inclusion of types as sub-members. If there is recursion
9901 /// the entry becomes IncompleteUsed.
9902 ///
9903 /// During the expansion of a RecordType's members:
9904 ///
9905 ///   If the cache contains a NonRecursive encoding for the member type, the
9906 ///   cached encoding is used;
9907 ///
9908 ///   If the cache contains a Recursive encoding for the member type, the
9909 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
9910 ///
9911 ///   If the member is a RecordType, an Incomplete encoding is placed into the
9912 ///   cache to break potential recursive inclusion of itself as a sub-member;
9913 ///
9914 ///   Once a member RecordType has been expanded, its temporary incomplete
9915 ///   entry is removed from the cache. If a Recursive encoding was swapped out
9916 ///   it is swapped back in;
9917 ///
9918 ///   If an incomplete entry is used to expand a sub-member, the incomplete
9919 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
9920 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
9921 ///
9922 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
9923 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
9924 ///   Else the member is part of a recursive type and thus the recursion has
9925 ///   been exited too soon for the encoding to be correct for the member.
9926 ///
9927 class TypeStringCache {
9928   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
9929   struct Entry {
9930     std::string Str;     // The encoded TypeString for the type.
9931     enum Status State;   // Information about the encoding in 'Str'.
9932     std::string Swapped; // A temporary place holder for a Recursive encoding
9933                          // during the expansion of RecordType's members.
9934   };
9935   std::map<const IdentifierInfo *, struct Entry> Map;
9936   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
9937   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
9938 public:
9939   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
9940   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
9941   bool removeIncomplete(const IdentifierInfo *ID);
9942   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
9943                      bool IsRecursive);
9944   StringRef lookupStr(const IdentifierInfo *ID);
9945 };
9946 
9947 /// TypeString encodings for enum & union fields must be order.
9948 /// FieldEncoding is a helper for this ordering process.
9949 class FieldEncoding {
9950   bool HasName;
9951   std::string Enc;
9952 public:
9953   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
9954   StringRef str() { return Enc; }
9955   bool operator<(const FieldEncoding &rhs) const {
9956     if (HasName != rhs.HasName) return HasName;
9957     return Enc < rhs.Enc;
9958   }
9959 };
9960 
9961 class XCoreABIInfo : public DefaultABIInfo {
9962 public:
9963   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9964   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9965                     QualType Ty) const override;
9966 };
9967 
9968 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
9969   mutable TypeStringCache TSC;
9970   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
9971                     const CodeGen::CodeGenModule &M) const;
9972 
9973 public:
9974   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
9975       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
9976   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
9977                           const llvm::MapVector<GlobalDecl, StringRef>
9978                               &MangledDeclNames) const override;
9979 };
9980 
9981 } // End anonymous namespace.
9982 
9983 // TODO: this implementation is likely now redundant with the default
9984 // EmitVAArg.
9985 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9986                                 QualType Ty) const {
9987   CGBuilderTy &Builder = CGF.Builder;
9988 
9989   // Get the VAList.
9990   CharUnits SlotSize = CharUnits::fromQuantity(4);
9991   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
9992 
9993   // Handle the argument.
9994   ABIArgInfo AI = classifyArgumentType(Ty);
9995   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
9996   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9997   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9998     AI.setCoerceToType(ArgTy);
9999   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
10000 
10001   Address Val = Address::invalid();
10002   CharUnits ArgSize = CharUnits::Zero();
10003   switch (AI.getKind()) {
10004   case ABIArgInfo::Expand:
10005   case ABIArgInfo::CoerceAndExpand:
10006   case ABIArgInfo::InAlloca:
10007     llvm_unreachable("Unsupported ABI kind for va_arg");
10008   case ABIArgInfo::Ignore:
10009     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
10010     ArgSize = CharUnits::Zero();
10011     break;
10012   case ABIArgInfo::Extend:
10013   case ABIArgInfo::Direct:
10014     Val = Builder.CreateBitCast(AP, ArgPtrTy);
10015     ArgSize = CharUnits::fromQuantity(
10016                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
10017     ArgSize = ArgSize.alignTo(SlotSize);
10018     break;
10019   case ABIArgInfo::Indirect:
10020   case ABIArgInfo::IndirectAliased:
10021     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
10022     Val = Address(Builder.CreateLoad(Val), TypeAlign);
10023     ArgSize = SlotSize;
10024     break;
10025   }
10026 
10027   // Increment the VAList.
10028   if (!ArgSize.isZero()) {
10029     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
10030     Builder.CreateStore(APN.getPointer(), VAListAddr);
10031   }
10032 
10033   return Val;
10034 }
10035 
10036 /// During the expansion of a RecordType, an incomplete TypeString is placed
10037 /// into the cache as a means to identify and break recursion.
10038 /// If there is a Recursive encoding in the cache, it is swapped out and will
10039 /// be reinserted by removeIncomplete().
10040 /// All other types of encoding should have been used rather than arriving here.
10041 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
10042                                     std::string StubEnc) {
10043   if (!ID)
10044     return;
10045   Entry &E = Map[ID];
10046   assert( (E.Str.empty() || E.State == Recursive) &&
10047          "Incorrectly use of addIncomplete");
10048   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
10049   E.Swapped.swap(E.Str); // swap out the Recursive
10050   E.Str.swap(StubEnc);
10051   E.State = Incomplete;
10052   ++IncompleteCount;
10053 }
10054 
10055 /// Once the RecordType has been expanded, the temporary incomplete TypeString
10056 /// must be removed from the cache.
10057 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
10058 /// Returns true if the RecordType was defined recursively.
10059 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
10060   if (!ID)
10061     return false;
10062   auto I = Map.find(ID);
10063   assert(I != Map.end() && "Entry not present");
10064   Entry &E = I->second;
10065   assert( (E.State == Incomplete ||
10066            E.State == IncompleteUsed) &&
10067          "Entry must be an incomplete type");
10068   bool IsRecursive = false;
10069   if (E.State == IncompleteUsed) {
10070     // We made use of our Incomplete encoding, thus we are recursive.
10071     IsRecursive = true;
10072     --IncompleteUsedCount;
10073   }
10074   if (E.Swapped.empty())
10075     Map.erase(I);
10076   else {
10077     // Swap the Recursive back.
10078     E.Swapped.swap(E.Str);
10079     E.Swapped.clear();
10080     E.State = Recursive;
10081   }
10082   --IncompleteCount;
10083   return IsRecursive;
10084 }
10085 
10086 /// Add the encoded TypeString to the cache only if it is NonRecursive or
10087 /// Recursive (viz: all sub-members were expanded as fully as possible).
10088 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
10089                                     bool IsRecursive) {
10090   if (!ID || IncompleteUsedCount)
10091     return; // No key or it is is an incomplete sub-type so don't add.
10092   Entry &E = Map[ID];
10093   if (IsRecursive && !E.Str.empty()) {
10094     assert(E.State==Recursive && E.Str.size() == Str.size() &&
10095            "This is not the same Recursive entry");
10096     // The parent container was not recursive after all, so we could have used
10097     // this Recursive sub-member entry after all, but we assumed the worse when
10098     // we started viz: IncompleteCount!=0.
10099     return;
10100   }
10101   assert(E.Str.empty() && "Entry already present");
10102   E.Str = Str.str();
10103   E.State = IsRecursive? Recursive : NonRecursive;
10104 }
10105 
10106 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
10107 /// are recursively expanding a type (IncompleteCount != 0) and the cached
10108 /// encoding is Recursive, return an empty StringRef.
10109 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
10110   if (!ID)
10111     return StringRef();   // We have no key.
10112   auto I = Map.find(ID);
10113   if (I == Map.end())
10114     return StringRef();   // We have no encoding.
10115   Entry &E = I->second;
10116   if (E.State == Recursive && IncompleteCount)
10117     return StringRef();   // We don't use Recursive encodings for member types.
10118 
10119   if (E.State == Incomplete) {
10120     // The incomplete type is being used to break out of recursion.
10121     E.State = IncompleteUsed;
10122     ++IncompleteUsedCount;
10123   }
10124   return E.Str;
10125 }
10126 
10127 /// The XCore ABI includes a type information section that communicates symbol
10128 /// type information to the linker. The linker uses this information to verify
10129 /// safety/correctness of things such as array bound and pointers et al.
10130 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
10131 /// This type information (TypeString) is emitted into meta data for all global
10132 /// symbols: definitions, declarations, functions & variables.
10133 ///
10134 /// The TypeString carries type, qualifier, name, size & value details.
10135 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
10136 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
10137 /// The output is tested by test/CodeGen/xcore-stringtype.c.
10138 ///
10139 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10140                           const CodeGen::CodeGenModule &CGM,
10141                           TypeStringCache &TSC);
10142 
10143 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
10144 void XCoreTargetCodeGenInfo::emitTargetMD(
10145     const Decl *D, llvm::GlobalValue *GV,
10146     const CodeGen::CodeGenModule &CGM) const {
10147   SmallStringEnc Enc;
10148   if (getTypeString(Enc, D, CGM, TSC)) {
10149     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
10150     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
10151                                 llvm::MDString::get(Ctx, Enc.str())};
10152     llvm::NamedMDNode *MD =
10153       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
10154     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
10155   }
10156 }
10157 
10158 void XCoreTargetCodeGenInfo::emitTargetMetadata(
10159     CodeGen::CodeGenModule &CGM,
10160     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
10161   // Warning, new MangledDeclNames may be appended within this loop.
10162   // We rely on MapVector insertions adding new elements to the end
10163   // of the container.
10164   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
10165     auto Val = *(MangledDeclNames.begin() + I);
10166     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
10167     if (GV) {
10168       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
10169       emitTargetMD(D, GV, CGM);
10170     }
10171   }
10172 }
10173 //===----------------------------------------------------------------------===//
10174 // SPIR ABI Implementation
10175 //===----------------------------------------------------------------------===//
10176 
10177 namespace {
10178 class SPIRABIInfo : public DefaultABIInfo {
10179 public:
10180   SPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
10181 
10182 private:
10183   void setCCs();
10184 };
10185 } // end anonymous namespace
10186 namespace {
10187 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
10188 public:
10189   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10190       : TargetCodeGenInfo(std::make_unique<SPIRABIInfo>(CGT)) {}
10191 
10192   LangAS getASTAllocaAddressSpace() const override {
10193     return getLangASFromTargetAS(
10194         getABIInfo().getDataLayout().getAllocaAddrSpace());
10195   }
10196 
10197   unsigned getOpenCLKernelCallingConv() const override;
10198 };
10199 
10200 } // End anonymous namespace.
10201 void SPIRABIInfo::setCCs() {
10202   assert(getRuntimeCC() == llvm::CallingConv::C);
10203   RuntimeCC = llvm::CallingConv::SPIR_FUNC;
10204 }
10205 
10206 namespace clang {
10207 namespace CodeGen {
10208 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
10209   DefaultABIInfo SPIRABI(CGM.getTypes());
10210   SPIRABI.computeInfo(FI);
10211 }
10212 }
10213 }
10214 
10215 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
10216   return llvm::CallingConv::SPIR_KERNEL;
10217 }
10218 
10219 static bool appendType(SmallStringEnc &Enc, QualType QType,
10220                        const CodeGen::CodeGenModule &CGM,
10221                        TypeStringCache &TSC);
10222 
10223 /// Helper function for appendRecordType().
10224 /// Builds a SmallVector containing the encoded field types in declaration
10225 /// order.
10226 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
10227                              const RecordDecl *RD,
10228                              const CodeGen::CodeGenModule &CGM,
10229                              TypeStringCache &TSC) {
10230   for (const auto *Field : RD->fields()) {
10231     SmallStringEnc Enc;
10232     Enc += "m(";
10233     Enc += Field->getName();
10234     Enc += "){";
10235     if (Field->isBitField()) {
10236       Enc += "b(";
10237       llvm::raw_svector_ostream OS(Enc);
10238       OS << Field->getBitWidthValue(CGM.getContext());
10239       Enc += ':';
10240     }
10241     if (!appendType(Enc, Field->getType(), CGM, TSC))
10242       return false;
10243     if (Field->isBitField())
10244       Enc += ')';
10245     Enc += '}';
10246     FE.emplace_back(!Field->getName().empty(), Enc);
10247   }
10248   return true;
10249 }
10250 
10251 /// Appends structure and union types to Enc and adds encoding to cache.
10252 /// Recursively calls appendType (via extractFieldType) for each field.
10253 /// Union types have their fields ordered according to the ABI.
10254 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
10255                              const CodeGen::CodeGenModule &CGM,
10256                              TypeStringCache &TSC, const IdentifierInfo *ID) {
10257   // Append the cached TypeString if we have one.
10258   StringRef TypeString = TSC.lookupStr(ID);
10259   if (!TypeString.empty()) {
10260     Enc += TypeString;
10261     return true;
10262   }
10263 
10264   // Start to emit an incomplete TypeString.
10265   size_t Start = Enc.size();
10266   Enc += (RT->isUnionType()? 'u' : 's');
10267   Enc += '(';
10268   if (ID)
10269     Enc += ID->getName();
10270   Enc += "){";
10271 
10272   // We collect all encoded fields and order as necessary.
10273   bool IsRecursive = false;
10274   const RecordDecl *RD = RT->getDecl()->getDefinition();
10275   if (RD && !RD->field_empty()) {
10276     // An incomplete TypeString stub is placed in the cache for this RecordType
10277     // so that recursive calls to this RecordType will use it whilst building a
10278     // complete TypeString for this RecordType.
10279     SmallVector<FieldEncoding, 16> FE;
10280     std::string StubEnc(Enc.substr(Start).str());
10281     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
10282     TSC.addIncomplete(ID, std::move(StubEnc));
10283     if (!extractFieldType(FE, RD, CGM, TSC)) {
10284       (void) TSC.removeIncomplete(ID);
10285       return false;
10286     }
10287     IsRecursive = TSC.removeIncomplete(ID);
10288     // The ABI requires unions to be sorted but not structures.
10289     // See FieldEncoding::operator< for sort algorithm.
10290     if (RT->isUnionType())
10291       llvm::sort(FE);
10292     // We can now complete the TypeString.
10293     unsigned E = FE.size();
10294     for (unsigned I = 0; I != E; ++I) {
10295       if (I)
10296         Enc += ',';
10297       Enc += FE[I].str();
10298     }
10299   }
10300   Enc += '}';
10301   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
10302   return true;
10303 }
10304 
10305 /// Appends enum types to Enc and adds the encoding to the cache.
10306 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
10307                            TypeStringCache &TSC,
10308                            const IdentifierInfo *ID) {
10309   // Append the cached TypeString if we have one.
10310   StringRef TypeString = TSC.lookupStr(ID);
10311   if (!TypeString.empty()) {
10312     Enc += TypeString;
10313     return true;
10314   }
10315 
10316   size_t Start = Enc.size();
10317   Enc += "e(";
10318   if (ID)
10319     Enc += ID->getName();
10320   Enc += "){";
10321 
10322   // We collect all encoded enumerations and order them alphanumerically.
10323   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
10324     SmallVector<FieldEncoding, 16> FE;
10325     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
10326          ++I) {
10327       SmallStringEnc EnumEnc;
10328       EnumEnc += "m(";
10329       EnumEnc += I->getName();
10330       EnumEnc += "){";
10331       I->getInitVal().toString(EnumEnc);
10332       EnumEnc += '}';
10333       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
10334     }
10335     llvm::sort(FE);
10336     unsigned E = FE.size();
10337     for (unsigned I = 0; I != E; ++I) {
10338       if (I)
10339         Enc += ',';
10340       Enc += FE[I].str();
10341     }
10342   }
10343   Enc += '}';
10344   TSC.addIfComplete(ID, Enc.substr(Start), false);
10345   return true;
10346 }
10347 
10348 /// Appends type's qualifier to Enc.
10349 /// This is done prior to appending the type's encoding.
10350 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10351   // Qualifiers are emitted in alphabetical order.
10352   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10353   int Lookup = 0;
10354   if (QT.isConstQualified())
10355     Lookup += 1<<0;
10356   if (QT.isRestrictQualified())
10357     Lookup += 1<<1;
10358   if (QT.isVolatileQualified())
10359     Lookup += 1<<2;
10360   Enc += Table[Lookup];
10361 }
10362 
10363 /// Appends built-in types to Enc.
10364 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10365   const char *EncType;
10366   switch (BT->getKind()) {
10367     case BuiltinType::Void:
10368       EncType = "0";
10369       break;
10370     case BuiltinType::Bool:
10371       EncType = "b";
10372       break;
10373     case BuiltinType::Char_U:
10374       EncType = "uc";
10375       break;
10376     case BuiltinType::UChar:
10377       EncType = "uc";
10378       break;
10379     case BuiltinType::SChar:
10380       EncType = "sc";
10381       break;
10382     case BuiltinType::UShort:
10383       EncType = "us";
10384       break;
10385     case BuiltinType::Short:
10386       EncType = "ss";
10387       break;
10388     case BuiltinType::UInt:
10389       EncType = "ui";
10390       break;
10391     case BuiltinType::Int:
10392       EncType = "si";
10393       break;
10394     case BuiltinType::ULong:
10395       EncType = "ul";
10396       break;
10397     case BuiltinType::Long:
10398       EncType = "sl";
10399       break;
10400     case BuiltinType::ULongLong:
10401       EncType = "ull";
10402       break;
10403     case BuiltinType::LongLong:
10404       EncType = "sll";
10405       break;
10406     case BuiltinType::Float:
10407       EncType = "ft";
10408       break;
10409     case BuiltinType::Double:
10410       EncType = "d";
10411       break;
10412     case BuiltinType::LongDouble:
10413       EncType = "ld";
10414       break;
10415     default:
10416       return false;
10417   }
10418   Enc += EncType;
10419   return true;
10420 }
10421 
10422 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10423 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10424                               const CodeGen::CodeGenModule &CGM,
10425                               TypeStringCache &TSC) {
10426   Enc += "p(";
10427   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10428     return false;
10429   Enc += ')';
10430   return true;
10431 }
10432 
10433 /// Appends array encoding to Enc before calling appendType for the element.
10434 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10435                             const ArrayType *AT,
10436                             const CodeGen::CodeGenModule &CGM,
10437                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10438   if (AT->getSizeModifier() != ArrayType::Normal)
10439     return false;
10440   Enc += "a(";
10441   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10442     CAT->getSize().toStringUnsigned(Enc);
10443   else
10444     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10445   Enc += ':';
10446   // The Qualifiers should be attached to the type rather than the array.
10447   appendQualifier(Enc, QT);
10448   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10449     return false;
10450   Enc += ')';
10451   return true;
10452 }
10453 
10454 /// Appends a function encoding to Enc, calling appendType for the return type
10455 /// and the arguments.
10456 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10457                              const CodeGen::CodeGenModule &CGM,
10458                              TypeStringCache &TSC) {
10459   Enc += "f{";
10460   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10461     return false;
10462   Enc += "}(";
10463   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10464     // N.B. we are only interested in the adjusted param types.
10465     auto I = FPT->param_type_begin();
10466     auto E = FPT->param_type_end();
10467     if (I != E) {
10468       do {
10469         if (!appendType(Enc, *I, CGM, TSC))
10470           return false;
10471         ++I;
10472         if (I != E)
10473           Enc += ',';
10474       } while (I != E);
10475       if (FPT->isVariadic())
10476         Enc += ",va";
10477     } else {
10478       if (FPT->isVariadic())
10479         Enc += "va";
10480       else
10481         Enc += '0';
10482     }
10483   }
10484   Enc += ')';
10485   return true;
10486 }
10487 
10488 /// Handles the type's qualifier before dispatching a call to handle specific
10489 /// type encodings.
10490 static bool appendType(SmallStringEnc &Enc, QualType QType,
10491                        const CodeGen::CodeGenModule &CGM,
10492                        TypeStringCache &TSC) {
10493 
10494   QualType QT = QType.getCanonicalType();
10495 
10496   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10497     // The Qualifiers should be attached to the type rather than the array.
10498     // Thus we don't call appendQualifier() here.
10499     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10500 
10501   appendQualifier(Enc, QT);
10502 
10503   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10504     return appendBuiltinType(Enc, BT);
10505 
10506   if (const PointerType *PT = QT->getAs<PointerType>())
10507     return appendPointerType(Enc, PT, CGM, TSC);
10508 
10509   if (const EnumType *ET = QT->getAs<EnumType>())
10510     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10511 
10512   if (const RecordType *RT = QT->getAsStructureType())
10513     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10514 
10515   if (const RecordType *RT = QT->getAsUnionType())
10516     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10517 
10518   if (const FunctionType *FT = QT->getAs<FunctionType>())
10519     return appendFunctionType(Enc, FT, CGM, TSC);
10520 
10521   return false;
10522 }
10523 
10524 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10525                           const CodeGen::CodeGenModule &CGM,
10526                           TypeStringCache &TSC) {
10527   if (!D)
10528     return false;
10529 
10530   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10531     if (FD->getLanguageLinkage() != CLanguageLinkage)
10532       return false;
10533     return appendType(Enc, FD->getType(), CGM, TSC);
10534   }
10535 
10536   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10537     if (VD->getLanguageLinkage() != CLanguageLinkage)
10538       return false;
10539     QualType QT = VD->getType().getCanonicalType();
10540     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10541       // Global ArrayTypes are given a size of '*' if the size is unknown.
10542       // The Qualifiers should be attached to the type rather than the array.
10543       // Thus we don't call appendQualifier() here.
10544       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10545     }
10546     return appendType(Enc, QT, CGM, TSC);
10547   }
10548   return false;
10549 }
10550 
10551 //===----------------------------------------------------------------------===//
10552 // RISCV ABI Implementation
10553 //===----------------------------------------------------------------------===//
10554 
10555 namespace {
10556 class RISCVABIInfo : public DefaultABIInfo {
10557 private:
10558   // Size of the integer ('x') registers in bits.
10559   unsigned XLen;
10560   // Size of the floating point ('f') registers in bits. Note that the target
10561   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10562   // with soft float ABI has FLen==0).
10563   unsigned FLen;
10564   static const int NumArgGPRs = 8;
10565   static const int NumArgFPRs = 8;
10566   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10567                                       llvm::Type *&Field1Ty,
10568                                       CharUnits &Field1Off,
10569                                       llvm::Type *&Field2Ty,
10570                                       CharUnits &Field2Off) const;
10571 
10572 public:
10573   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10574       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10575 
10576   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10577   // non-virtual, but computeInfo is virtual, so we overload it.
10578   void computeInfo(CGFunctionInfo &FI) const override;
10579 
10580   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10581                                   int &ArgFPRsLeft) const;
10582   ABIArgInfo classifyReturnType(QualType RetTy) const;
10583 
10584   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10585                     QualType Ty) const override;
10586 
10587   ABIArgInfo extendType(QualType Ty) const;
10588 
10589   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10590                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10591                                 CharUnits &Field2Off, int &NeededArgGPRs,
10592                                 int &NeededArgFPRs) const;
10593   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10594                                                CharUnits Field1Off,
10595                                                llvm::Type *Field2Ty,
10596                                                CharUnits Field2Off) const;
10597 };
10598 } // end anonymous namespace
10599 
10600 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10601   QualType RetTy = FI.getReturnType();
10602   if (!getCXXABI().classifyReturnType(FI))
10603     FI.getReturnInfo() = classifyReturnType(RetTy);
10604 
10605   // IsRetIndirect is true if classifyArgumentType indicated the value should
10606   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10607   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10608   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10609   // list and pass indirectly on RV32.
10610   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
10611   if (!IsRetIndirect && RetTy->isScalarType() &&
10612       getContext().getTypeSize(RetTy) > (2 * XLen)) {
10613     if (RetTy->isComplexType() && FLen) {
10614       QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
10615       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
10616     } else {
10617       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
10618       IsRetIndirect = true;
10619     }
10620   }
10621 
10622   // We must track the number of GPRs used in order to conform to the RISC-V
10623   // ABI, as integer scalars passed in registers should have signext/zeroext
10624   // when promoted, but are anyext if passed on the stack. As GPR usage is
10625   // different for variadic arguments, we must also track whether we are
10626   // examining a vararg or not.
10627   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
10628   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
10629   int NumFixedArgs = FI.getNumRequiredArgs();
10630 
10631   int ArgNum = 0;
10632   for (auto &ArgInfo : FI.arguments()) {
10633     bool IsFixed = ArgNum < NumFixedArgs;
10634     ArgInfo.info =
10635         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
10636     ArgNum++;
10637   }
10638 }
10639 
10640 // Returns true if the struct is a potential candidate for the floating point
10641 // calling convention. If this function returns true, the caller is
10642 // responsible for checking that if there is only a single field then that
10643 // field is a float.
10644 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10645                                                   llvm::Type *&Field1Ty,
10646                                                   CharUnits &Field1Off,
10647                                                   llvm::Type *&Field2Ty,
10648                                                   CharUnits &Field2Off) const {
10649   bool IsInt = Ty->isIntegralOrEnumerationType();
10650   bool IsFloat = Ty->isRealFloatingType();
10651 
10652   if (IsInt || IsFloat) {
10653     uint64_t Size = getContext().getTypeSize(Ty);
10654     if (IsInt && Size > XLen)
10655       return false;
10656     // Can't be eligible if larger than the FP registers. Half precision isn't
10657     // currently supported on RISC-V and the ABI hasn't been confirmed, so
10658     // default to the integer ABI in that case.
10659     if (IsFloat && (Size > FLen || Size < 32))
10660       return false;
10661     // Can't be eligible if an integer type was already found (int+int pairs
10662     // are not eligible).
10663     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
10664       return false;
10665     if (!Field1Ty) {
10666       Field1Ty = CGT.ConvertType(Ty);
10667       Field1Off = CurOff;
10668       return true;
10669     }
10670     if (!Field2Ty) {
10671       Field2Ty = CGT.ConvertType(Ty);
10672       Field2Off = CurOff;
10673       return true;
10674     }
10675     return false;
10676   }
10677 
10678   if (auto CTy = Ty->getAs<ComplexType>()) {
10679     if (Field1Ty)
10680       return false;
10681     QualType EltTy = CTy->getElementType();
10682     if (getContext().getTypeSize(EltTy) > FLen)
10683       return false;
10684     Field1Ty = CGT.ConvertType(EltTy);
10685     Field1Off = CurOff;
10686     Field2Ty = Field1Ty;
10687     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
10688     return true;
10689   }
10690 
10691   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
10692     uint64_t ArraySize = ATy->getSize().getZExtValue();
10693     QualType EltTy = ATy->getElementType();
10694     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
10695     for (uint64_t i = 0; i < ArraySize; ++i) {
10696       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
10697                                                 Field1Off, Field2Ty, Field2Off);
10698       if (!Ret)
10699         return false;
10700       CurOff += EltSize;
10701     }
10702     return true;
10703   }
10704 
10705   if (const auto *RTy = Ty->getAs<RecordType>()) {
10706     // Structures with either a non-trivial destructor or a non-trivial
10707     // copy constructor are not eligible for the FP calling convention.
10708     if (getRecordArgABI(Ty, CGT.getCXXABI()))
10709       return false;
10710     if (isEmptyRecord(getContext(), Ty, true))
10711       return true;
10712     const RecordDecl *RD = RTy->getDecl();
10713     // Unions aren't eligible unless they're empty (which is caught above).
10714     if (RD->isUnion())
10715       return false;
10716     int ZeroWidthBitFieldCount = 0;
10717     for (const FieldDecl *FD : RD->fields()) {
10718       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
10719       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
10720       QualType QTy = FD->getType();
10721       if (FD->isBitField()) {
10722         unsigned BitWidth = FD->getBitWidthValue(getContext());
10723         // Allow a bitfield with a type greater than XLen as long as the
10724         // bitwidth is XLen or less.
10725         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
10726           QTy = getContext().getIntTypeForBitwidth(XLen, false);
10727         if (BitWidth == 0) {
10728           ZeroWidthBitFieldCount++;
10729           continue;
10730         }
10731       }
10732 
10733       bool Ret = detectFPCCEligibleStructHelper(
10734           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
10735           Field1Ty, Field1Off, Field2Ty, Field2Off);
10736       if (!Ret)
10737         return false;
10738 
10739       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
10740       // or int+fp structs, but are ignored for a struct with an fp field and
10741       // any number of zero-width bitfields.
10742       if (Field2Ty && ZeroWidthBitFieldCount > 0)
10743         return false;
10744     }
10745     return Field1Ty != nullptr;
10746   }
10747 
10748   return false;
10749 }
10750 
10751 // Determine if a struct is eligible for passing according to the floating
10752 // point calling convention (i.e., when flattened it contains a single fp
10753 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
10754 // NeededArgGPRs are incremented appropriately.
10755 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10756                                             CharUnits &Field1Off,
10757                                             llvm::Type *&Field2Ty,
10758                                             CharUnits &Field2Off,
10759                                             int &NeededArgGPRs,
10760                                             int &NeededArgFPRs) const {
10761   Field1Ty = nullptr;
10762   Field2Ty = nullptr;
10763   NeededArgGPRs = 0;
10764   NeededArgFPRs = 0;
10765   bool IsCandidate = detectFPCCEligibleStructHelper(
10766       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
10767   // Not really a candidate if we have a single int but no float.
10768   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
10769     return false;
10770   if (!IsCandidate)
10771     return false;
10772   if (Field1Ty && Field1Ty->isFloatingPointTy())
10773     NeededArgFPRs++;
10774   else if (Field1Ty)
10775     NeededArgGPRs++;
10776   if (Field2Ty && Field2Ty->isFloatingPointTy())
10777     NeededArgFPRs++;
10778   else if (Field2Ty)
10779     NeededArgGPRs++;
10780   return true;
10781 }
10782 
10783 // Call getCoerceAndExpand for the two-element flattened struct described by
10784 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
10785 // appropriate coerceToType and unpaddedCoerceToType.
10786 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
10787     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
10788     CharUnits Field2Off) const {
10789   SmallVector<llvm::Type *, 3> CoerceElts;
10790   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
10791   if (!Field1Off.isZero())
10792     CoerceElts.push_back(llvm::ArrayType::get(
10793         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
10794 
10795   CoerceElts.push_back(Field1Ty);
10796   UnpaddedCoerceElts.push_back(Field1Ty);
10797 
10798   if (!Field2Ty) {
10799     return ABIArgInfo::getCoerceAndExpand(
10800         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
10801         UnpaddedCoerceElts[0]);
10802   }
10803 
10804   CharUnits Field2Align =
10805       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
10806   CharUnits Field1End = Field1Off +
10807       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
10808   CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
10809 
10810   CharUnits Padding = CharUnits::Zero();
10811   if (Field2Off > Field2OffNoPadNoPack)
10812     Padding = Field2Off - Field2OffNoPadNoPack;
10813   else if (Field2Off != Field2Align && Field2Off > Field1End)
10814     Padding = Field2Off - Field1End;
10815 
10816   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
10817 
10818   if (!Padding.isZero())
10819     CoerceElts.push_back(llvm::ArrayType::get(
10820         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
10821 
10822   CoerceElts.push_back(Field2Ty);
10823   UnpaddedCoerceElts.push_back(Field2Ty);
10824 
10825   auto CoerceToType =
10826       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
10827   auto UnpaddedCoerceToType =
10828       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
10829 
10830   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
10831 }
10832 
10833 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
10834                                               int &ArgGPRsLeft,
10835                                               int &ArgFPRsLeft) const {
10836   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
10837   Ty = useFirstFieldIfTransparentUnion(Ty);
10838 
10839   // Structures with either a non-trivial destructor or a non-trivial
10840   // copy constructor are always passed indirectly.
10841   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
10842     if (ArgGPRsLeft)
10843       ArgGPRsLeft -= 1;
10844     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
10845                                            CGCXXABI::RAA_DirectInMemory);
10846   }
10847 
10848   // Ignore empty structs/unions.
10849   if (isEmptyRecord(getContext(), Ty, true))
10850     return ABIArgInfo::getIgnore();
10851 
10852   uint64_t Size = getContext().getTypeSize(Ty);
10853 
10854   // Pass floating point values via FPRs if possible.
10855   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
10856       FLen >= Size && ArgFPRsLeft) {
10857     ArgFPRsLeft--;
10858     return ABIArgInfo::getDirect();
10859   }
10860 
10861   // Complex types for the hard float ABI must be passed direct rather than
10862   // using CoerceAndExpand.
10863   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
10864     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
10865     if (getContext().getTypeSize(EltTy) <= FLen) {
10866       ArgFPRsLeft -= 2;
10867       return ABIArgInfo::getDirect();
10868     }
10869   }
10870 
10871   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
10872     llvm::Type *Field1Ty = nullptr;
10873     llvm::Type *Field2Ty = nullptr;
10874     CharUnits Field1Off = CharUnits::Zero();
10875     CharUnits Field2Off = CharUnits::Zero();
10876     int NeededArgGPRs = 0;
10877     int NeededArgFPRs = 0;
10878     bool IsCandidate =
10879         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
10880                                  NeededArgGPRs, NeededArgFPRs);
10881     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
10882         NeededArgFPRs <= ArgFPRsLeft) {
10883       ArgGPRsLeft -= NeededArgGPRs;
10884       ArgFPRsLeft -= NeededArgFPRs;
10885       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
10886                                                Field2Off);
10887     }
10888   }
10889 
10890   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
10891   bool MustUseStack = false;
10892   // Determine the number of GPRs needed to pass the current argument
10893   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
10894   // register pairs, so may consume 3 registers.
10895   int NeededArgGPRs = 1;
10896   if (!IsFixed && NeededAlign == 2 * XLen)
10897     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
10898   else if (Size > XLen && Size <= 2 * XLen)
10899     NeededArgGPRs = 2;
10900 
10901   if (NeededArgGPRs > ArgGPRsLeft) {
10902     MustUseStack = true;
10903     NeededArgGPRs = ArgGPRsLeft;
10904   }
10905 
10906   ArgGPRsLeft -= NeededArgGPRs;
10907 
10908   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
10909     // Treat an enum type as its underlying type.
10910     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10911       Ty = EnumTy->getDecl()->getIntegerType();
10912 
10913     // All integral types are promoted to XLen width, unless passed on the
10914     // stack.
10915     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
10916       return extendType(Ty);
10917     }
10918 
10919     if (const auto *EIT = Ty->getAs<ExtIntType>()) {
10920       if (EIT->getNumBits() < XLen && !MustUseStack)
10921         return extendType(Ty);
10922       if (EIT->getNumBits() > 128 ||
10923           (!getContext().getTargetInfo().hasInt128Type() &&
10924            EIT->getNumBits() > 64))
10925         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10926     }
10927 
10928     return ABIArgInfo::getDirect();
10929   }
10930 
10931   // Aggregates which are <= 2*XLen will be passed in registers if possible,
10932   // so coerce to integers.
10933   if (Size <= 2 * XLen) {
10934     unsigned Alignment = getContext().getTypeAlign(Ty);
10935 
10936     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
10937     // required, and a 2-element XLen array if only XLen alignment is required.
10938     if (Size <= XLen) {
10939       return ABIArgInfo::getDirect(
10940           llvm::IntegerType::get(getVMContext(), XLen));
10941     } else if (Alignment == 2 * XLen) {
10942       return ABIArgInfo::getDirect(
10943           llvm::IntegerType::get(getVMContext(), 2 * XLen));
10944     } else {
10945       return ABIArgInfo::getDirect(llvm::ArrayType::get(
10946           llvm::IntegerType::get(getVMContext(), XLen), 2));
10947     }
10948   }
10949   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10950 }
10951 
10952 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
10953   if (RetTy->isVoidType())
10954     return ABIArgInfo::getIgnore();
10955 
10956   int ArgGPRsLeft = 2;
10957   int ArgFPRsLeft = FLen ? 2 : 0;
10958 
10959   // The rules for return and argument types are the same, so defer to
10960   // classifyArgumentType.
10961   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
10962                               ArgFPRsLeft);
10963 }
10964 
10965 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10966                                 QualType Ty) const {
10967   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
10968 
10969   // Empty records are ignored for parameter passing purposes.
10970   if (isEmptyRecord(getContext(), Ty, true)) {
10971     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
10972     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
10973     return Addr;
10974   }
10975 
10976   auto TInfo = getContext().getTypeInfoInChars(Ty);
10977 
10978   // Arguments bigger than 2*Xlen bytes are passed indirectly.
10979   bool IsIndirect = TInfo.Width > 2 * SlotSize;
10980 
10981   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo,
10982                           SlotSize, /*AllowHigherAlign=*/true);
10983 }
10984 
10985 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
10986   int TySize = getContext().getTypeSize(Ty);
10987   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
10988   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
10989     return ABIArgInfo::getSignExtend(Ty);
10990   return ABIArgInfo::getExtend(Ty);
10991 }
10992 
10993 namespace {
10994 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
10995 public:
10996   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
10997                          unsigned FLen)
10998       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
10999 
11000   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
11001                            CodeGen::CodeGenModule &CGM) const override {
11002     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
11003     if (!FD) return;
11004 
11005     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
11006     if (!Attr)
11007       return;
11008 
11009     const char *Kind;
11010     switch (Attr->getInterrupt()) {
11011     case RISCVInterruptAttr::user: Kind = "user"; break;
11012     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
11013     case RISCVInterruptAttr::machine: Kind = "machine"; break;
11014     }
11015 
11016     auto *Fn = cast<llvm::Function>(GV);
11017 
11018     Fn->addFnAttr("interrupt", Kind);
11019   }
11020 };
11021 } // namespace
11022 
11023 //===----------------------------------------------------------------------===//
11024 // VE ABI Implementation.
11025 //
11026 namespace {
11027 class VEABIInfo : public DefaultABIInfo {
11028 public:
11029   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
11030 
11031 private:
11032   ABIArgInfo classifyReturnType(QualType RetTy) const;
11033   ABIArgInfo classifyArgumentType(QualType RetTy) const;
11034   void computeInfo(CGFunctionInfo &FI) const override;
11035 };
11036 } // end anonymous namespace
11037 
11038 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
11039   if (Ty->isAnyComplexType())
11040     return ABIArgInfo::getDirect();
11041   uint64_t Size = getContext().getTypeSize(Ty);
11042   if (Size < 64 && Ty->isIntegerType())
11043     return ABIArgInfo::getExtend(Ty);
11044   return DefaultABIInfo::classifyReturnType(Ty);
11045 }
11046 
11047 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
11048   if (Ty->isAnyComplexType())
11049     return ABIArgInfo::getDirect();
11050   uint64_t Size = getContext().getTypeSize(Ty);
11051   if (Size < 64 && Ty->isIntegerType())
11052     return ABIArgInfo::getExtend(Ty);
11053   return DefaultABIInfo::classifyArgumentType(Ty);
11054 }
11055 
11056 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
11057   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
11058   for (auto &Arg : FI.arguments())
11059     Arg.info = classifyArgumentType(Arg.type);
11060 }
11061 
11062 namespace {
11063 class VETargetCodeGenInfo : public TargetCodeGenInfo {
11064 public:
11065   VETargetCodeGenInfo(CodeGenTypes &CGT)
11066       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
11067   // VE ABI requires the arguments of variadic and prototype-less functions
11068   // are passed in both registers and memory.
11069   bool isNoProtoCallVariadic(const CallArgList &args,
11070                              const FunctionNoProtoType *fnType) const override {
11071     return true;
11072   }
11073 };
11074 } // end anonymous namespace
11075 
11076 //===----------------------------------------------------------------------===//
11077 // Driver code
11078 //===----------------------------------------------------------------------===//
11079 
11080 bool CodeGenModule::supportsCOMDAT() const {
11081   return getTriple().supportsCOMDAT();
11082 }
11083 
11084 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
11085   if (TheTargetCodeGenInfo)
11086     return *TheTargetCodeGenInfo;
11087 
11088   // Helper to set the unique_ptr while still keeping the return value.
11089   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
11090     this->TheTargetCodeGenInfo.reset(P);
11091     return *P;
11092   };
11093 
11094   const llvm::Triple &Triple = getTarget().getTriple();
11095   switch (Triple.getArch()) {
11096   default:
11097     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
11098 
11099   case llvm::Triple::le32:
11100     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11101   case llvm::Triple::m68k:
11102     return SetCGInfo(new M68kTargetCodeGenInfo(Types));
11103   case llvm::Triple::mips:
11104   case llvm::Triple::mipsel:
11105     if (Triple.getOS() == llvm::Triple::NaCl)
11106       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11107     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
11108 
11109   case llvm::Triple::mips64:
11110   case llvm::Triple::mips64el:
11111     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
11112 
11113   case llvm::Triple::avr:
11114     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
11115 
11116   case llvm::Triple::aarch64:
11117   case llvm::Triple::aarch64_32:
11118   case llvm::Triple::aarch64_be: {
11119     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
11120     if (getTarget().getABI() == "darwinpcs")
11121       Kind = AArch64ABIInfo::DarwinPCS;
11122     else if (Triple.isOSWindows())
11123       return SetCGInfo(
11124           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
11125 
11126     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
11127   }
11128 
11129   case llvm::Triple::wasm32:
11130   case llvm::Triple::wasm64: {
11131     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
11132     if (getTarget().getABI() == "experimental-mv")
11133       Kind = WebAssemblyABIInfo::ExperimentalMV;
11134     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
11135   }
11136 
11137   case llvm::Triple::arm:
11138   case llvm::Triple::armeb:
11139   case llvm::Triple::thumb:
11140   case llvm::Triple::thumbeb: {
11141     if (Triple.getOS() == llvm::Triple::Win32) {
11142       return SetCGInfo(
11143           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
11144     }
11145 
11146     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
11147     StringRef ABIStr = getTarget().getABI();
11148     if (ABIStr == "apcs-gnu")
11149       Kind = ARMABIInfo::APCS;
11150     else if (ABIStr == "aapcs16")
11151       Kind = ARMABIInfo::AAPCS16_VFP;
11152     else if (CodeGenOpts.FloatABI == "hard" ||
11153              (CodeGenOpts.FloatABI != "soft" &&
11154               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
11155                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
11156                Triple.getEnvironment() == llvm::Triple::EABIHF)))
11157       Kind = ARMABIInfo::AAPCS_VFP;
11158 
11159     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
11160   }
11161 
11162   case llvm::Triple::ppc: {
11163     if (Triple.isOSAIX())
11164       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
11165 
11166     bool IsSoftFloat =
11167         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
11168     bool RetSmallStructInRegABI =
11169         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11170     return SetCGInfo(
11171         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11172   }
11173   case llvm::Triple::ppcle: {
11174     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11175     bool RetSmallStructInRegABI =
11176         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11177     return SetCGInfo(
11178         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11179   }
11180   case llvm::Triple::ppc64:
11181     if (Triple.isOSAIX())
11182       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
11183 
11184     if (Triple.isOSBinFormatELF()) {
11185       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
11186       if (getTarget().getABI() == "elfv2")
11187         Kind = PPC64_SVR4_ABIInfo::ELFv2;
11188       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11189 
11190       return SetCGInfo(
11191           new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11192     }
11193     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
11194   case llvm::Triple::ppc64le: {
11195     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
11196     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
11197     if (getTarget().getABI() == "elfv1")
11198       Kind = PPC64_SVR4_ABIInfo::ELFv1;
11199     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11200 
11201     return SetCGInfo(
11202         new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11203   }
11204 
11205   case llvm::Triple::nvptx:
11206   case llvm::Triple::nvptx64:
11207     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
11208 
11209   case llvm::Triple::msp430:
11210     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
11211 
11212   case llvm::Triple::riscv32:
11213   case llvm::Triple::riscv64: {
11214     StringRef ABIStr = getTarget().getABI();
11215     unsigned XLen = getTarget().getPointerWidth(0);
11216     unsigned ABIFLen = 0;
11217     if (ABIStr.endswith("f"))
11218       ABIFLen = 32;
11219     else if (ABIStr.endswith("d"))
11220       ABIFLen = 64;
11221     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
11222   }
11223 
11224   case llvm::Triple::systemz: {
11225     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
11226     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
11227     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
11228   }
11229 
11230   case llvm::Triple::tce:
11231   case llvm::Triple::tcele:
11232     return SetCGInfo(new TCETargetCodeGenInfo(Types));
11233 
11234   case llvm::Triple::x86: {
11235     bool IsDarwinVectorABI = Triple.isOSDarwin();
11236     bool RetSmallStructInRegABI =
11237         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11238     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
11239 
11240     if (Triple.getOS() == llvm::Triple::Win32) {
11241       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
11242           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11243           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
11244     } else {
11245       return SetCGInfo(new X86_32TargetCodeGenInfo(
11246           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11247           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
11248           CodeGenOpts.FloatABI == "soft"));
11249     }
11250   }
11251 
11252   case llvm::Triple::x86_64: {
11253     StringRef ABI = getTarget().getABI();
11254     X86AVXABILevel AVXLevel =
11255         (ABI == "avx512"
11256              ? X86AVXABILevel::AVX512
11257              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
11258 
11259     switch (Triple.getOS()) {
11260     case llvm::Triple::Win32:
11261       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
11262     default:
11263       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
11264     }
11265   }
11266   case llvm::Triple::hexagon:
11267     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
11268   case llvm::Triple::lanai:
11269     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
11270   case llvm::Triple::r600:
11271     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11272   case llvm::Triple::amdgcn:
11273     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11274   case llvm::Triple::sparc:
11275     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
11276   case llvm::Triple::sparcv9:
11277     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
11278   case llvm::Triple::xcore:
11279     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
11280   case llvm::Triple::arc:
11281     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
11282   case llvm::Triple::spir:
11283   case llvm::Triple::spir64:
11284     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
11285   case llvm::Triple::ve:
11286     return SetCGInfo(new VETargetCodeGenInfo(Types));
11287   }
11288 }
11289 
11290 /// Create an OpenCL kernel for an enqueued block.
11291 ///
11292 /// The kernel has the same function type as the block invoke function. Its
11293 /// name is the name of the block invoke function postfixed with "_kernel".
11294 /// It simply calls the block invoke function then returns.
11295 llvm::Function *
11296 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
11297                                              llvm::Function *Invoke,
11298                                              llvm::Value *BlockLiteral) const {
11299   auto *InvokeFT = Invoke->getFunctionType();
11300   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11301   for (auto &P : InvokeFT->params())
11302     ArgTys.push_back(P);
11303   auto &C = CGF.getLLVMContext();
11304   std::string Name = Invoke->getName().str() + "_kernel";
11305   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11306   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11307                                    &CGF.CGM.getModule());
11308   auto IP = CGF.Builder.saveIP();
11309   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11310   auto &Builder = CGF.Builder;
11311   Builder.SetInsertPoint(BB);
11312   llvm::SmallVector<llvm::Value *, 2> Args;
11313   for (auto &A : F->args())
11314     Args.push_back(&A);
11315   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11316   call->setCallingConv(Invoke->getCallingConv());
11317   Builder.CreateRetVoid();
11318   Builder.restoreIP(IP);
11319   return F;
11320 }
11321 
11322 /// Create an OpenCL kernel for an enqueued block.
11323 ///
11324 /// The type of the first argument (the block literal) is the struct type
11325 /// of the block literal instead of a pointer type. The first argument
11326 /// (block literal) is passed directly by value to the kernel. The kernel
11327 /// allocates the same type of struct on stack and stores the block literal
11328 /// to it and passes its pointer to the block invoke function. The kernel
11329 /// has "enqueued-block" function attribute and kernel argument metadata.
11330 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
11331     CodeGenFunction &CGF, llvm::Function *Invoke,
11332     llvm::Value *BlockLiteral) const {
11333   auto &Builder = CGF.Builder;
11334   auto &C = CGF.getLLVMContext();
11335 
11336   auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
11337   auto *InvokeFT = Invoke->getFunctionType();
11338   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11339   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
11340   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
11341   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
11342   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
11343   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
11344   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
11345 
11346   ArgTys.push_back(BlockTy);
11347   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11348   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
11349   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11350   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11351   AccessQuals.push_back(llvm::MDString::get(C, "none"));
11352   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
11353   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
11354     ArgTys.push_back(InvokeFT->getParamType(I));
11355     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
11356     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
11357     AccessQuals.push_back(llvm::MDString::get(C, "none"));
11358     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
11359     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11360     ArgNames.push_back(
11361         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
11362   }
11363   std::string Name = Invoke->getName().str() + "_kernel";
11364   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11365   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11366                                    &CGF.CGM.getModule());
11367   F->addFnAttr("enqueued-block");
11368   auto IP = CGF.Builder.saveIP();
11369   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11370   Builder.SetInsertPoint(BB);
11371   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
11372   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
11373   BlockPtr->setAlignment(BlockAlign);
11374   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
11375   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
11376   llvm::SmallVector<llvm::Value *, 2> Args;
11377   Args.push_back(Cast);
11378   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
11379     Args.push_back(I);
11380   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11381   call->setCallingConv(Invoke->getCallingConv());
11382   Builder.CreateRetVoid();
11383   Builder.restoreIP(IP);
11384 
11385   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
11386   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
11387   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
11388   F->setMetadata("kernel_arg_base_type",
11389                  llvm::MDNode::get(C, ArgBaseTypeNames));
11390   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
11391   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
11392     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
11393 
11394   return F;
11395 }
11396