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       Current = SSE;
2817     } else if (k == BuiltinType::LongDouble) {
2818       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2819       if (LDF == &llvm::APFloat::IEEEquad()) {
2820         Lo = SSE;
2821         Hi = SSEUp;
2822       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2823         Lo = X87;
2824         Hi = X87Up;
2825       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2826         Current = SSE;
2827       } else
2828         llvm_unreachable("unexpected long double representation!");
2829     }
2830     // FIXME: _Decimal32 and _Decimal64 are SSE.
2831     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2832     return;
2833   }
2834 
2835   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2836     // Classify the underlying integer type.
2837     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2838     return;
2839   }
2840 
2841   if (Ty->hasPointerRepresentation()) {
2842     Current = Integer;
2843     return;
2844   }
2845 
2846   if (Ty->isMemberPointerType()) {
2847     if (Ty->isMemberFunctionPointerType()) {
2848       if (Has64BitPointers) {
2849         // If Has64BitPointers, this is an {i64, i64}, so classify both
2850         // Lo and Hi now.
2851         Lo = Hi = Integer;
2852       } else {
2853         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2854         // straddles an eightbyte boundary, Hi should be classified as well.
2855         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2856         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2857         if (EB_FuncPtr != EB_ThisAdj) {
2858           Lo = Hi = Integer;
2859         } else {
2860           Current = Integer;
2861         }
2862       }
2863     } else {
2864       Current = Integer;
2865     }
2866     return;
2867   }
2868 
2869   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2870     uint64_t Size = getContext().getTypeSize(VT);
2871     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2872       // gcc passes the following as integer:
2873       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2874       // 2 bytes - <2 x char>, <1 x short>
2875       // 1 byte  - <1 x char>
2876       Current = Integer;
2877 
2878       // If this type crosses an eightbyte boundary, it should be
2879       // split.
2880       uint64_t EB_Lo = (OffsetBase) / 64;
2881       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2882       if (EB_Lo != EB_Hi)
2883         Hi = Lo;
2884     } else if (Size == 64) {
2885       QualType ElementType = VT->getElementType();
2886 
2887       // gcc passes <1 x double> in memory. :(
2888       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2889         return;
2890 
2891       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2892       // pass them as integer.  For platforms where clang is the de facto
2893       // platform compiler, we must continue to use integer.
2894       if (!classifyIntegerMMXAsSSE() &&
2895           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2896            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2897            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2898            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2899         Current = Integer;
2900       else
2901         Current = SSE;
2902 
2903       // If this type crosses an eightbyte boundary, it should be
2904       // split.
2905       if (OffsetBase && OffsetBase != 64)
2906         Hi = Lo;
2907     } else if (Size == 128 ||
2908                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2909       QualType ElementType = VT->getElementType();
2910 
2911       // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
2912       if (passInt128VectorsInMem() && Size != 128 &&
2913           (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
2914            ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
2915         return;
2916 
2917       // Arguments of 256-bits are split into four eightbyte chunks. The
2918       // least significant one belongs to class SSE and all the others to class
2919       // SSEUP. The original Lo and Hi design considers that types can't be
2920       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2921       // This design isn't correct for 256-bits, but since there're no cases
2922       // where the upper parts would need to be inspected, avoid adding
2923       // complexity and just consider Hi to match the 64-256 part.
2924       //
2925       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2926       // registers if they are "named", i.e. not part of the "..." of a
2927       // variadic function.
2928       //
2929       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2930       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2931       Lo = SSE;
2932       Hi = SSEUp;
2933     }
2934     return;
2935   }
2936 
2937   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2938     QualType ET = getContext().getCanonicalType(CT->getElementType());
2939 
2940     uint64_t Size = getContext().getTypeSize(Ty);
2941     if (ET->isIntegralOrEnumerationType()) {
2942       if (Size <= 64)
2943         Current = Integer;
2944       else if (Size <= 128)
2945         Lo = Hi = Integer;
2946     } else if (ET == getContext().FloatTy) {
2947       Current = SSE;
2948     } else if (ET == getContext().DoubleTy) {
2949       Lo = Hi = SSE;
2950     } else if (ET == getContext().LongDoubleTy) {
2951       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2952       if (LDF == &llvm::APFloat::IEEEquad())
2953         Current = Memory;
2954       else if (LDF == &llvm::APFloat::x87DoubleExtended())
2955         Current = ComplexX87;
2956       else if (LDF == &llvm::APFloat::IEEEdouble())
2957         Lo = Hi = SSE;
2958       else
2959         llvm_unreachable("unexpected long double representation!");
2960     }
2961 
2962     // If this complex type crosses an eightbyte boundary then it
2963     // should be split.
2964     uint64_t EB_Real = (OffsetBase) / 64;
2965     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2966     if (Hi == NoClass && EB_Real != EB_Imag)
2967       Hi = Lo;
2968 
2969     return;
2970   }
2971 
2972   if (const auto *EITy = Ty->getAs<ExtIntType>()) {
2973     if (EITy->getNumBits() <= 64)
2974       Current = Integer;
2975     else if (EITy->getNumBits() <= 128)
2976       Lo = Hi = Integer;
2977     // Larger values need to get passed in memory.
2978     return;
2979   }
2980 
2981   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2982     // Arrays are treated like structures.
2983 
2984     uint64_t Size = getContext().getTypeSize(Ty);
2985 
2986     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2987     // than eight eightbytes, ..., it has class MEMORY.
2988     if (Size > 512)
2989       return;
2990 
2991     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2992     // fields, it has class MEMORY.
2993     //
2994     // Only need to check alignment of array base.
2995     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2996       return;
2997 
2998     // Otherwise implement simplified merge. We could be smarter about
2999     // this, but it isn't worth it and would be harder to verify.
3000     Current = NoClass;
3001     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
3002     uint64_t ArraySize = AT->getSize().getZExtValue();
3003 
3004     // The only case a 256-bit wide vector could be used is when the array
3005     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
3006     // to work for sizes wider than 128, early check and fallback to memory.
3007     //
3008     if (Size > 128 &&
3009         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
3010       return;
3011 
3012     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
3013       Class FieldLo, FieldHi;
3014       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
3015       Lo = merge(Lo, FieldLo);
3016       Hi = merge(Hi, FieldHi);
3017       if (Lo == Memory || Hi == Memory)
3018         break;
3019     }
3020 
3021     postMerge(Size, Lo, Hi);
3022     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
3023     return;
3024   }
3025 
3026   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3027     uint64_t Size = getContext().getTypeSize(Ty);
3028 
3029     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3030     // than eight eightbytes, ..., it has class MEMORY.
3031     if (Size > 512)
3032       return;
3033 
3034     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
3035     // copy constructor or a non-trivial destructor, it is passed by invisible
3036     // reference.
3037     if (getRecordArgABI(RT, getCXXABI()))
3038       return;
3039 
3040     const RecordDecl *RD = RT->getDecl();
3041 
3042     // Assume variable sized types are passed in memory.
3043     if (RD->hasFlexibleArrayMember())
3044       return;
3045 
3046     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3047 
3048     // Reset Lo class, this will be recomputed.
3049     Current = NoClass;
3050 
3051     // If this is a C++ record, classify the bases first.
3052     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3053       for (const auto &I : CXXRD->bases()) {
3054         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3055                "Unexpected base class!");
3056         const auto *Base =
3057             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3058 
3059         // Classify this field.
3060         //
3061         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
3062         // single eightbyte, each is classified separately. Each eightbyte gets
3063         // initialized to class NO_CLASS.
3064         Class FieldLo, FieldHi;
3065         uint64_t Offset =
3066           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
3067         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
3068         Lo = merge(Lo, FieldLo);
3069         Hi = merge(Hi, FieldHi);
3070         if (Lo == Memory || Hi == Memory) {
3071           postMerge(Size, Lo, Hi);
3072           return;
3073         }
3074       }
3075     }
3076 
3077     // Classify the fields one at a time, merging the results.
3078     unsigned idx = 0;
3079     bool UseClang11Compat = getContext().getLangOpts().getClangABICompat() <=
3080                                 LangOptions::ClangABI::Ver11 ||
3081                             getContext().getTargetInfo().getTriple().isPS4();
3082     bool IsUnion = RT->isUnionType() && !UseClang11Compat;
3083 
3084     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3085            i != e; ++i, ++idx) {
3086       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3087       bool BitField = i->isBitField();
3088 
3089       // Ignore padding bit-fields.
3090       if (BitField && i->isUnnamedBitfield())
3091         continue;
3092 
3093       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
3094       // eight eightbytes, or it contains unaligned fields, it has class MEMORY.
3095       //
3096       // The only case a 256-bit or a 512-bit wide vector could be used is when
3097       // the struct contains a single 256-bit or 512-bit element. Early check
3098       // and fallback to memory.
3099       //
3100       // FIXME: Extended the Lo and Hi logic properly to work for size wider
3101       // than 128.
3102       if (Size > 128 &&
3103           ((!IsUnion && Size != getContext().getTypeSize(i->getType())) ||
3104            Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
3105         Lo = Memory;
3106         postMerge(Size, Lo, Hi);
3107         return;
3108       }
3109       // Note, skip this test for bit-fields, see below.
3110       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
3111         Lo = Memory;
3112         postMerge(Size, Lo, Hi);
3113         return;
3114       }
3115 
3116       // Classify this field.
3117       //
3118       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
3119       // exceeds a single eightbyte, each is classified
3120       // separately. Each eightbyte gets initialized to class
3121       // NO_CLASS.
3122       Class FieldLo, FieldHi;
3123 
3124       // Bit-fields require special handling, they do not force the
3125       // structure to be passed in memory even if unaligned, and
3126       // therefore they can straddle an eightbyte.
3127       if (BitField) {
3128         assert(!i->isUnnamedBitfield());
3129         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3130         uint64_t Size = i->getBitWidthValue(getContext());
3131 
3132         uint64_t EB_Lo = Offset / 64;
3133         uint64_t EB_Hi = (Offset + Size - 1) / 64;
3134 
3135         if (EB_Lo) {
3136           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
3137           FieldLo = NoClass;
3138           FieldHi = Integer;
3139         } else {
3140           FieldLo = Integer;
3141           FieldHi = EB_Hi ? Integer : NoClass;
3142         }
3143       } else
3144         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
3145       Lo = merge(Lo, FieldLo);
3146       Hi = merge(Hi, FieldHi);
3147       if (Lo == Memory || Hi == Memory)
3148         break;
3149     }
3150 
3151     postMerge(Size, Lo, Hi);
3152   }
3153 }
3154 
3155 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
3156   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3157   // place naturally.
3158   if (!isAggregateTypeForABI(Ty)) {
3159     // Treat an enum type as its underlying type.
3160     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3161       Ty = EnumTy->getDecl()->getIntegerType();
3162 
3163     if (Ty->isExtIntType())
3164       return getNaturalAlignIndirect(Ty);
3165 
3166     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3167                                               : ABIArgInfo::getDirect());
3168   }
3169 
3170   return getNaturalAlignIndirect(Ty);
3171 }
3172 
3173 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
3174   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
3175     uint64_t Size = getContext().getTypeSize(VecTy);
3176     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
3177     if (Size <= 64 || Size > LargestVector)
3178       return true;
3179     QualType EltTy = VecTy->getElementType();
3180     if (passInt128VectorsInMem() &&
3181         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
3182          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
3183       return true;
3184   }
3185 
3186   return false;
3187 }
3188 
3189 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
3190                                             unsigned freeIntRegs) const {
3191   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3192   // place naturally.
3193   //
3194   // This assumption is optimistic, as there could be free registers available
3195   // when we need to pass this argument in memory, and LLVM could try to pass
3196   // the argument in the free register. This does not seem to happen currently,
3197   // but this code would be much safer if we could mark the argument with
3198   // 'onstack'. See PR12193.
3199   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
3200       !Ty->isExtIntType()) {
3201     // Treat an enum type as its underlying type.
3202     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3203       Ty = EnumTy->getDecl()->getIntegerType();
3204 
3205     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3206                                               : ABIArgInfo::getDirect());
3207   }
3208 
3209   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3210     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3211 
3212   // Compute the byval alignment. We specify the alignment of the byval in all
3213   // cases so that the mid-level optimizer knows the alignment of the byval.
3214   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
3215 
3216   // Attempt to avoid passing indirect results using byval when possible. This
3217   // is important for good codegen.
3218   //
3219   // We do this by coercing the value into a scalar type which the backend can
3220   // handle naturally (i.e., without using byval).
3221   //
3222   // For simplicity, we currently only do this when we have exhausted all of the
3223   // free integer registers. Doing this when there are free integer registers
3224   // would require more care, as we would have to ensure that the coerced value
3225   // did not claim the unused register. That would require either reording the
3226   // arguments to the function (so that any subsequent inreg values came first),
3227   // or only doing this optimization when there were no following arguments that
3228   // might be inreg.
3229   //
3230   // We currently expect it to be rare (particularly in well written code) for
3231   // arguments to be passed on the stack when there are still free integer
3232   // registers available (this would typically imply large structs being passed
3233   // by value), so this seems like a fair tradeoff for now.
3234   //
3235   // We can revisit this if the backend grows support for 'onstack' parameter
3236   // attributes. See PR12193.
3237   if (freeIntRegs == 0) {
3238     uint64_t Size = getContext().getTypeSize(Ty);
3239 
3240     // If this type fits in an eightbyte, coerce it into the matching integral
3241     // type, which will end up on the stack (with alignment 8).
3242     if (Align == 8 && Size <= 64)
3243       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3244                                                           Size));
3245   }
3246 
3247   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
3248 }
3249 
3250 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3251 /// register. Pick an LLVM IR type that will be passed as a vector register.
3252 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3253   // Wrapper structs/arrays that only contain vectors are passed just like
3254   // vectors; strip them off if present.
3255   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3256     Ty = QualType(InnerTy, 0);
3257 
3258   llvm::Type *IRType = CGT.ConvertType(Ty);
3259   if (isa<llvm::VectorType>(IRType)) {
3260     // Don't pass vXi128 vectors in their native type, the backend can't
3261     // legalize them.
3262     if (passInt128VectorsInMem() &&
3263         cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
3264       // Use a vXi64 vector.
3265       uint64_t Size = getContext().getTypeSize(Ty);
3266       return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3267                                         Size / 64);
3268     }
3269 
3270     return IRType;
3271   }
3272 
3273   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3274     return IRType;
3275 
3276   // We couldn't find the preferred IR vector type for 'Ty'.
3277   uint64_t Size = getContext().getTypeSize(Ty);
3278   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3279 
3280 
3281   // Return a LLVM IR vector type based on the size of 'Ty'.
3282   return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3283                                     Size / 64);
3284 }
3285 
3286 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3287 /// is known to either be off the end of the specified type or being in
3288 /// alignment padding.  The user type specified is known to be at most 128 bits
3289 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3290 /// classification that put one of the two halves in the INTEGER class.
3291 ///
3292 /// It is conservatively correct to return false.
3293 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3294                                   unsigned EndBit, ASTContext &Context) {
3295   // If the bytes being queried are off the end of the type, there is no user
3296   // data hiding here.  This handles analysis of builtins, vectors and other
3297   // types that don't contain interesting padding.
3298   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3299   if (TySize <= StartBit)
3300     return true;
3301 
3302   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3303     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3304     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3305 
3306     // Check each element to see if the element overlaps with the queried range.
3307     for (unsigned i = 0; i != NumElts; ++i) {
3308       // If the element is after the span we care about, then we're done..
3309       unsigned EltOffset = i*EltSize;
3310       if (EltOffset >= EndBit) break;
3311 
3312       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3313       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3314                                  EndBit-EltOffset, Context))
3315         return false;
3316     }
3317     // If it overlaps no elements, then it is safe to process as padding.
3318     return true;
3319   }
3320 
3321   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3322     const RecordDecl *RD = RT->getDecl();
3323     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3324 
3325     // If this is a C++ record, check the bases first.
3326     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3327       for (const auto &I : CXXRD->bases()) {
3328         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3329                "Unexpected base class!");
3330         const auto *Base =
3331             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3332 
3333         // If the base is after the span we care about, ignore it.
3334         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3335         if (BaseOffset >= EndBit) continue;
3336 
3337         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3338         if (!BitsContainNoUserData(I.getType(), BaseStart,
3339                                    EndBit-BaseOffset, Context))
3340           return false;
3341       }
3342     }
3343 
3344     // Verify that no field has data that overlaps the region of interest.  Yes
3345     // this could be sped up a lot by being smarter about queried fields,
3346     // however we're only looking at structs up to 16 bytes, so we don't care
3347     // much.
3348     unsigned idx = 0;
3349     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3350          i != e; ++i, ++idx) {
3351       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3352 
3353       // If we found a field after the region we care about, then we're done.
3354       if (FieldOffset >= EndBit) break;
3355 
3356       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3357       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3358                                  Context))
3359         return false;
3360     }
3361 
3362     // If nothing in this record overlapped the area of interest, then we're
3363     // clean.
3364     return true;
3365   }
3366 
3367   return false;
3368 }
3369 
3370 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3371 /// float member at the specified offset.  For example, {int,{float}} has a
3372 /// float at offset 4.  It is conservatively correct for this routine to return
3373 /// false.
3374 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
3375                                   const llvm::DataLayout &TD) {
3376   // Base case if we find a float.
3377   if (IROffset == 0 && IRType->isFloatTy())
3378     return true;
3379 
3380   // If this is a struct, recurse into the field at the specified offset.
3381   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3382     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3383     unsigned Elt = SL->getElementContainingOffset(IROffset);
3384     IROffset -= SL->getElementOffset(Elt);
3385     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3386   }
3387 
3388   // If this is an array, recurse into the field at the specified offset.
3389   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3390     llvm::Type *EltTy = ATy->getElementType();
3391     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3392     IROffset -= IROffset/EltSize*EltSize;
3393     return ContainsFloatAtOffset(EltTy, IROffset, TD);
3394   }
3395 
3396   return false;
3397 }
3398 
3399 
3400 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3401 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3402 llvm::Type *X86_64ABIInfo::
3403 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3404                    QualType SourceTy, unsigned SourceOffset) const {
3405   // The only three choices we have are either double, <2 x float>, or float. We
3406   // pass as float if the last 4 bytes is just padding.  This happens for
3407   // structs that contain 3 floats.
3408   if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3409                             SourceOffset*8+64, getContext()))
3410     return llvm::Type::getFloatTy(getVMContext());
3411 
3412   // We want to pass as <2 x float> if the LLVM IR type contains a float at
3413   // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
3414   // case.
3415   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3416       ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
3417     return llvm::FixedVectorType::get(llvm::Type::getFloatTy(getVMContext()),
3418                                       2);
3419 
3420   return llvm::Type::getDoubleTy(getVMContext());
3421 }
3422 
3423 
3424 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3425 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3426 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3427 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3428 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3429 /// etc).
3430 ///
3431 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3432 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3433 /// the 8-byte value references.  PrefType may be null.
3434 ///
3435 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3436 /// an offset into this that we're processing (which is always either 0 or 8).
3437 ///
3438 llvm::Type *X86_64ABIInfo::
3439 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3440                        QualType SourceTy, unsigned SourceOffset) const {
3441   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3442   // returning an 8-byte unit starting with it.  See if we can safely use it.
3443   if (IROffset == 0) {
3444     // Pointers and int64's always fill the 8-byte unit.
3445     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3446         IRType->isIntegerTy(64))
3447       return IRType;
3448 
3449     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3450     // goodness in the source type is just tail padding.  This is allowed to
3451     // kick in for struct {double,int} on the int, but not on
3452     // struct{double,int,int} because we wouldn't return the second int.  We
3453     // have to do this analysis on the source type because we can't depend on
3454     // unions being lowered a specific way etc.
3455     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3456         IRType->isIntegerTy(32) ||
3457         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3458       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3459           cast<llvm::IntegerType>(IRType)->getBitWidth();
3460 
3461       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3462                                 SourceOffset*8+64, getContext()))
3463         return IRType;
3464     }
3465   }
3466 
3467   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3468     // If this is a struct, recurse into the field at the specified offset.
3469     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3470     if (IROffset < SL->getSizeInBytes()) {
3471       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3472       IROffset -= SL->getElementOffset(FieldIdx);
3473 
3474       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3475                                     SourceTy, SourceOffset);
3476     }
3477   }
3478 
3479   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3480     llvm::Type *EltTy = ATy->getElementType();
3481     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3482     unsigned EltOffset = IROffset/EltSize*EltSize;
3483     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3484                                   SourceOffset);
3485   }
3486 
3487   // Okay, we don't have any better idea of what to pass, so we pass this in an
3488   // integer register that isn't too big to fit the rest of the struct.
3489   unsigned TySizeInBytes =
3490     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3491 
3492   assert(TySizeInBytes != SourceOffset && "Empty field?");
3493 
3494   // It is always safe to classify this as an integer type up to i64 that
3495   // isn't larger than the structure.
3496   return llvm::IntegerType::get(getVMContext(),
3497                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3498 }
3499 
3500 
3501 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3502 /// be used as elements of a two register pair to pass or return, return a
3503 /// first class aggregate to represent them.  For example, if the low part of
3504 /// a by-value argument should be passed as i32* and the high part as float,
3505 /// return {i32*, float}.
3506 static llvm::Type *
3507 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3508                            const llvm::DataLayout &TD) {
3509   // In order to correctly satisfy the ABI, we need to the high part to start
3510   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3511   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3512   // the second element at offset 8.  Check for this:
3513   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3514   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3515   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3516   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3517 
3518   // To handle this, we have to increase the size of the low part so that the
3519   // second element will start at an 8 byte offset.  We can't increase the size
3520   // of the second element because it might make us access off the end of the
3521   // struct.
3522   if (HiStart != 8) {
3523     // There are usually two sorts of types the ABI generation code can produce
3524     // for the low part of a pair that aren't 8 bytes in size: float or
3525     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3526     // NaCl).
3527     // Promote these to a larger type.
3528     if (Lo->isFloatTy())
3529       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3530     else {
3531       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3532              && "Invalid/unknown lo type");
3533       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3534     }
3535   }
3536 
3537   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3538 
3539   // Verify that the second element is at an 8-byte offset.
3540   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3541          "Invalid x86-64 argument pair!");
3542   return Result;
3543 }
3544 
3545 ABIArgInfo X86_64ABIInfo::
3546 classifyReturnType(QualType RetTy) const {
3547   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3548   // classification algorithm.
3549   X86_64ABIInfo::Class Lo, Hi;
3550   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3551 
3552   // Check some invariants.
3553   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3554   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3555 
3556   llvm::Type *ResType = nullptr;
3557   switch (Lo) {
3558   case NoClass:
3559     if (Hi == NoClass)
3560       return ABIArgInfo::getIgnore();
3561     // If the low part is just padding, it takes no register, leave ResType
3562     // null.
3563     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3564            "Unknown missing lo part");
3565     break;
3566 
3567   case SSEUp:
3568   case X87Up:
3569     llvm_unreachable("Invalid classification for lo word.");
3570 
3571     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3572     // hidden argument.
3573   case Memory:
3574     return getIndirectReturnResult(RetTy);
3575 
3576     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3577     // available register of the sequence %rax, %rdx is used.
3578   case Integer:
3579     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3580 
3581     // If we have a sign or zero extended integer, make sure to return Extend
3582     // so that the parameter gets the right LLVM IR attributes.
3583     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3584       // Treat an enum type as its underlying type.
3585       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3586         RetTy = EnumTy->getDecl()->getIntegerType();
3587 
3588       if (RetTy->isIntegralOrEnumerationType() &&
3589           isPromotableIntegerTypeForABI(RetTy))
3590         return ABIArgInfo::getExtend(RetTy);
3591     }
3592     break;
3593 
3594     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3595     // available SSE register of the sequence %xmm0, %xmm1 is used.
3596   case SSE:
3597     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3598     break;
3599 
3600     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3601     // returned on the X87 stack in %st0 as 80-bit x87 number.
3602   case X87:
3603     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3604     break;
3605 
3606     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3607     // part of the value is returned in %st0 and the imaginary part in
3608     // %st1.
3609   case ComplexX87:
3610     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3611     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3612                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3613     break;
3614   }
3615 
3616   llvm::Type *HighPart = nullptr;
3617   switch (Hi) {
3618     // Memory was handled previously and X87 should
3619     // never occur as a hi class.
3620   case Memory:
3621   case X87:
3622     llvm_unreachable("Invalid classification for hi word.");
3623 
3624   case ComplexX87: // Previously handled.
3625   case NoClass:
3626     break;
3627 
3628   case Integer:
3629     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3630     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3631       return ABIArgInfo::getDirect(HighPart, 8);
3632     break;
3633   case SSE:
3634     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3635     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3636       return ABIArgInfo::getDirect(HighPart, 8);
3637     break;
3638 
3639     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3640     // is passed in the next available eightbyte chunk if the last used
3641     // vector register.
3642     //
3643     // SSEUP should always be preceded by SSE, just widen.
3644   case SSEUp:
3645     assert(Lo == SSE && "Unexpected SSEUp classification.");
3646     ResType = GetByteVectorType(RetTy);
3647     break;
3648 
3649     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3650     // returned together with the previous X87 value in %st0.
3651   case X87Up:
3652     // If X87Up is preceded by X87, we don't need to do
3653     // anything. However, in some cases with unions it may not be
3654     // preceded by X87. In such situations we follow gcc and pass the
3655     // extra bits in an SSE reg.
3656     if (Lo != X87) {
3657       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3658       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3659         return ABIArgInfo::getDirect(HighPart, 8);
3660     }
3661     break;
3662   }
3663 
3664   // If a high part was specified, merge it together with the low part.  It is
3665   // known to pass in the high eightbyte of the result.  We do this by forming a
3666   // first class struct aggregate with the high and low part: {low, high}
3667   if (HighPart)
3668     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3669 
3670   return ABIArgInfo::getDirect(ResType);
3671 }
3672 
3673 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
3674   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3675   bool isNamedArg)
3676   const
3677 {
3678   Ty = useFirstFieldIfTransparentUnion(Ty);
3679 
3680   X86_64ABIInfo::Class Lo, Hi;
3681   classify(Ty, 0, Lo, Hi, isNamedArg);
3682 
3683   // Check some invariants.
3684   // FIXME: Enforce these by construction.
3685   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3686   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3687 
3688   neededInt = 0;
3689   neededSSE = 0;
3690   llvm::Type *ResType = nullptr;
3691   switch (Lo) {
3692   case NoClass:
3693     if (Hi == NoClass)
3694       return ABIArgInfo::getIgnore();
3695     // If the low part is just padding, it takes no register, leave ResType
3696     // null.
3697     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3698            "Unknown missing lo part");
3699     break;
3700 
3701     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3702     // on the stack.
3703   case Memory:
3704 
3705     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3706     // COMPLEX_X87, it is passed in memory.
3707   case X87:
3708   case ComplexX87:
3709     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3710       ++neededInt;
3711     return getIndirectResult(Ty, freeIntRegs);
3712 
3713   case SSEUp:
3714   case X87Up:
3715     llvm_unreachable("Invalid classification for lo word.");
3716 
3717     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3718     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3719     // and %r9 is used.
3720   case Integer:
3721     ++neededInt;
3722 
3723     // Pick an 8-byte type based on the preferred type.
3724     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3725 
3726     // If we have a sign or zero extended integer, make sure to return Extend
3727     // so that the parameter gets the right LLVM IR attributes.
3728     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3729       // Treat an enum type as its underlying type.
3730       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3731         Ty = EnumTy->getDecl()->getIntegerType();
3732 
3733       if (Ty->isIntegralOrEnumerationType() &&
3734           isPromotableIntegerTypeForABI(Ty))
3735         return ABIArgInfo::getExtend(Ty);
3736     }
3737 
3738     break;
3739 
3740     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3741     // available SSE register is used, the registers are taken in the
3742     // order from %xmm0 to %xmm7.
3743   case SSE: {
3744     llvm::Type *IRType = CGT.ConvertType(Ty);
3745     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3746     ++neededSSE;
3747     break;
3748   }
3749   }
3750 
3751   llvm::Type *HighPart = nullptr;
3752   switch (Hi) {
3753     // Memory was handled previously, ComplexX87 and X87 should
3754     // never occur as hi classes, and X87Up must be preceded by X87,
3755     // which is passed in memory.
3756   case Memory:
3757   case X87:
3758   case ComplexX87:
3759     llvm_unreachable("Invalid classification for hi word.");
3760 
3761   case NoClass: break;
3762 
3763   case Integer:
3764     ++neededInt;
3765     // Pick an 8-byte type based on the preferred type.
3766     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3767 
3768     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3769       return ABIArgInfo::getDirect(HighPart, 8);
3770     break;
3771 
3772     // X87Up generally doesn't occur here (long double is passed in
3773     // memory), except in situations involving unions.
3774   case X87Up:
3775   case SSE:
3776     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3777 
3778     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3779       return ABIArgInfo::getDirect(HighPart, 8);
3780 
3781     ++neededSSE;
3782     break;
3783 
3784     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3785     // eightbyte is passed in the upper half of the last used SSE
3786     // register.  This only happens when 128-bit vectors are passed.
3787   case SSEUp:
3788     assert(Lo == SSE && "Unexpected SSEUp classification");
3789     ResType = GetByteVectorType(Ty);
3790     break;
3791   }
3792 
3793   // If a high part was specified, merge it together with the low part.  It is
3794   // known to pass in the high eightbyte of the result.  We do this by forming a
3795   // first class struct aggregate with the high and low part: {low, high}
3796   if (HighPart)
3797     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3798 
3799   return ABIArgInfo::getDirect(ResType);
3800 }
3801 
3802 ABIArgInfo
3803 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3804                                              unsigned &NeededSSE) const {
3805   auto RT = Ty->getAs<RecordType>();
3806   assert(RT && "classifyRegCallStructType only valid with struct types");
3807 
3808   if (RT->getDecl()->hasFlexibleArrayMember())
3809     return getIndirectReturnResult(Ty);
3810 
3811   // Sum up bases
3812   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3813     if (CXXRD->isDynamicClass()) {
3814       NeededInt = NeededSSE = 0;
3815       return getIndirectReturnResult(Ty);
3816     }
3817 
3818     for (const auto &I : CXXRD->bases())
3819       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3820               .isIndirect()) {
3821         NeededInt = NeededSSE = 0;
3822         return getIndirectReturnResult(Ty);
3823       }
3824   }
3825 
3826   // Sum up members
3827   for (const auto *FD : RT->getDecl()->fields()) {
3828     if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3829       if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3830               .isIndirect()) {
3831         NeededInt = NeededSSE = 0;
3832         return getIndirectReturnResult(Ty);
3833       }
3834     } else {
3835       unsigned LocalNeededInt, LocalNeededSSE;
3836       if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3837                                LocalNeededSSE, true)
3838               .isIndirect()) {
3839         NeededInt = NeededSSE = 0;
3840         return getIndirectReturnResult(Ty);
3841       }
3842       NeededInt += LocalNeededInt;
3843       NeededSSE += LocalNeededSSE;
3844     }
3845   }
3846 
3847   return ABIArgInfo::getDirect();
3848 }
3849 
3850 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3851                                                     unsigned &NeededInt,
3852                                                     unsigned &NeededSSE) const {
3853 
3854   NeededInt = 0;
3855   NeededSSE = 0;
3856 
3857   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3858 }
3859 
3860 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3861 
3862   const unsigned CallingConv = FI.getCallingConvention();
3863   // It is possible to force Win64 calling convention on any x86_64 target by
3864   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3865   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3866   if (CallingConv == llvm::CallingConv::Win64) {
3867     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3868     Win64ABIInfo.computeInfo(FI);
3869     return;
3870   }
3871 
3872   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3873 
3874   // Keep track of the number of assigned registers.
3875   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3876   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3877   unsigned NeededInt, NeededSSE;
3878 
3879   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3880     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3881         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3882       FI.getReturnInfo() =
3883           classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3884       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3885         FreeIntRegs -= NeededInt;
3886         FreeSSERegs -= NeededSSE;
3887       } else {
3888         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3889       }
3890     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
3891                getContext().getCanonicalType(FI.getReturnType()
3892                                                  ->getAs<ComplexType>()
3893                                                  ->getElementType()) ==
3894                    getContext().LongDoubleTy)
3895       // Complex Long Double Type is passed in Memory when Regcall
3896       // calling convention is used.
3897       FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3898     else
3899       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3900   }
3901 
3902   // If the return value is indirect, then the hidden argument is consuming one
3903   // integer register.
3904   if (FI.getReturnInfo().isIndirect())
3905     --FreeIntRegs;
3906 
3907   // The chain argument effectively gives us another free register.
3908   if (FI.isChainCall())
3909     ++FreeIntRegs;
3910 
3911   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3912   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3913   // get assigned (in left-to-right order) for passing as follows...
3914   unsigned ArgNo = 0;
3915   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3916        it != ie; ++it, ++ArgNo) {
3917     bool IsNamedArg = ArgNo < NumRequiredArgs;
3918 
3919     if (IsRegCall && it->type->isStructureOrClassType())
3920       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3921     else
3922       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3923                                       NeededSSE, IsNamedArg);
3924 
3925     // AMD64-ABI 3.2.3p3: If there are no registers available for any
3926     // eightbyte of an argument, the whole argument is passed on the
3927     // stack. If registers have already been assigned for some
3928     // eightbytes of such an argument, the assignments get reverted.
3929     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3930       FreeIntRegs -= NeededInt;
3931       FreeSSERegs -= NeededSSE;
3932     } else {
3933       it->info = getIndirectResult(it->type, FreeIntRegs);
3934     }
3935   }
3936 }
3937 
3938 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3939                                          Address VAListAddr, QualType Ty) {
3940   Address overflow_arg_area_p =
3941       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
3942   llvm::Value *overflow_arg_area =
3943     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3944 
3945   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3946   // byte boundary if alignment needed by type exceeds 8 byte boundary.
3947   // It isn't stated explicitly in the standard, but in practice we use
3948   // alignment greater than 16 where necessary.
3949   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3950   if (Align > CharUnits::fromQuantity(8)) {
3951     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3952                                                       Align);
3953   }
3954 
3955   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
3956   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3957   llvm::Value *Res =
3958     CGF.Builder.CreateBitCast(overflow_arg_area,
3959                               llvm::PointerType::getUnqual(LTy));
3960 
3961   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3962   // l->overflow_arg_area + sizeof(type).
3963   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3964   // an 8 byte boundary.
3965 
3966   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
3967   llvm::Value *Offset =
3968       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
3969   overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3970                                             "overflow_arg_area.next");
3971   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3972 
3973   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
3974   return Address(Res, Align);
3975 }
3976 
3977 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3978                                  QualType Ty) const {
3979   // Assume that va_list type is correct; should be pointer to LLVM type:
3980   // struct {
3981   //   i32 gp_offset;
3982   //   i32 fp_offset;
3983   //   i8* overflow_arg_area;
3984   //   i8* reg_save_area;
3985   // };
3986   unsigned neededInt, neededSSE;
3987 
3988   Ty = getContext().getCanonicalType(Ty);
3989   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
3990                                        /*isNamedArg*/false);
3991 
3992   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3993   // in the registers. If not go to step 7.
3994   if (!neededInt && !neededSSE)
3995     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3996 
3997   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3998   // general purpose registers needed to pass type and num_fp to hold
3999   // the number of floating point registers needed.
4000 
4001   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
4002   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
4003   // l->fp_offset > 304 - num_fp * 16 go to step 7.
4004   //
4005   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
4006   // register save space).
4007 
4008   llvm::Value *InRegs = nullptr;
4009   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
4010   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
4011   if (neededInt) {
4012     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
4013     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
4014     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
4015     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
4016   }
4017 
4018   if (neededSSE) {
4019     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
4020     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
4021     llvm::Value *FitsInFP =
4022       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
4023     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
4024     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
4025   }
4026 
4027   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4028   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4029   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4030   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4031 
4032   // Emit code to load the value if it was passed in registers.
4033 
4034   CGF.EmitBlock(InRegBlock);
4035 
4036   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
4037   // an offset of l->gp_offset and/or l->fp_offset. This may require
4038   // copying to a temporary location in case the parameter is passed
4039   // in different register classes or requires an alignment greater
4040   // than 8 for general purpose registers and 16 for XMM registers.
4041   //
4042   // FIXME: This really results in shameful code when we end up needing to
4043   // collect arguments from different places; often what should result in a
4044   // simple assembling of a structure from scattered addresses has many more
4045   // loads than necessary. Can we clean this up?
4046   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4047   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
4048       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
4049 
4050   Address RegAddr = Address::invalid();
4051   if (neededInt && neededSSE) {
4052     // FIXME: Cleanup.
4053     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
4054     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
4055     Address Tmp = CGF.CreateMemTemp(Ty);
4056     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4057     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
4058     llvm::Type *TyLo = ST->getElementType(0);
4059     llvm::Type *TyHi = ST->getElementType(1);
4060     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
4061            "Unexpected ABI info for mixed regs");
4062     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
4063     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
4064     llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
4065     llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
4066     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
4067     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
4068 
4069     // Copy the first element.
4070     // FIXME: Our choice of alignment here and below is probably pessimistic.
4071     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
4072         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
4073         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
4074     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4075 
4076     // Copy the second element.
4077     V = CGF.Builder.CreateAlignedLoad(
4078         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
4079         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
4080     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4081 
4082     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4083   } else if (neededInt) {
4084     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
4085                       CharUnits::fromQuantity(8));
4086     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4087 
4088     // Copy to a temporary if necessary to ensure the appropriate alignment.
4089     auto TInfo = getContext().getTypeInfoInChars(Ty);
4090     uint64_t TySize = TInfo.Width.getQuantity();
4091     CharUnits TyAlign = TInfo.Align;
4092 
4093     // Copy into a temporary if the type is more aligned than the
4094     // register save area.
4095     if (TyAlign.getQuantity() > 8) {
4096       Address Tmp = CGF.CreateMemTemp(Ty);
4097       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
4098       RegAddr = Tmp;
4099     }
4100 
4101   } else if (neededSSE == 1) {
4102     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
4103                       CharUnits::fromQuantity(16));
4104     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4105   } else {
4106     assert(neededSSE == 2 && "Invalid number of needed registers!");
4107     // SSE registers are spaced 16 bytes apart in the register save
4108     // area, we need to collect the two eightbytes together.
4109     // The ABI isn't explicit about this, but it seems reasonable
4110     // to assume that the slots are 16-byte aligned, since the stack is
4111     // naturally 16-byte aligned and the prologue is expected to store
4112     // all the SSE registers to the RSA.
4113     Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
4114                                 CharUnits::fromQuantity(16));
4115     Address RegAddrHi =
4116       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
4117                                              CharUnits::fromQuantity(16));
4118     llvm::Type *ST = AI.canHaveCoerceToType()
4119                          ? AI.getCoerceToType()
4120                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
4121     llvm::Value *V;
4122     Address Tmp = CGF.CreateMemTemp(Ty);
4123     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4124     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4125         RegAddrLo, ST->getStructElementType(0)));
4126     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4127     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4128         RegAddrHi, ST->getStructElementType(1)));
4129     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4130 
4131     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4132   }
4133 
4134   // AMD64-ABI 3.5.7p5: Step 5. Set:
4135   // l->gp_offset = l->gp_offset + num_gp * 8
4136   // l->fp_offset = l->fp_offset + num_fp * 16.
4137   if (neededInt) {
4138     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
4139     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
4140                             gp_offset_p);
4141   }
4142   if (neededSSE) {
4143     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
4144     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
4145                             fp_offset_p);
4146   }
4147   CGF.EmitBranch(ContBlock);
4148 
4149   // Emit code to load the value if it was passed in memory.
4150 
4151   CGF.EmitBlock(InMemBlock);
4152   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4153 
4154   // Return the appropriate result.
4155 
4156   CGF.EmitBlock(ContBlock);
4157   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
4158                                  "vaarg.addr");
4159   return ResAddr;
4160 }
4161 
4162 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4163                                    QualType Ty) const {
4164   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4165   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4166   uint64_t Width = getContext().getTypeSize(Ty);
4167   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4168 
4169   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4170                           CGF.getContext().getTypeInfoInChars(Ty),
4171                           CharUnits::fromQuantity(8),
4172                           /*allowHigherAlign*/ false);
4173 }
4174 
4175 ABIArgInfo WinX86_64ABIInfo::reclassifyHvaArgForVectorCall(
4176     QualType Ty, unsigned &FreeSSERegs, const ABIArgInfo &current) const {
4177   const Type *Base = nullptr;
4178   uint64_t NumElts = 0;
4179 
4180   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
4181       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
4182     FreeSSERegs -= NumElts;
4183     return getDirectX86Hva();
4184   }
4185   return current;
4186 }
4187 
4188 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
4189                                       bool IsReturnType, bool IsVectorCall,
4190                                       bool IsRegCall) const {
4191 
4192   if (Ty->isVoidType())
4193     return ABIArgInfo::getIgnore();
4194 
4195   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4196     Ty = EnumTy->getDecl()->getIntegerType();
4197 
4198   TypeInfo Info = getContext().getTypeInfo(Ty);
4199   uint64_t Width = Info.Width;
4200   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
4201 
4202   const RecordType *RT = Ty->getAs<RecordType>();
4203   if (RT) {
4204     if (!IsReturnType) {
4205       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
4206         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4207     }
4208 
4209     if (RT->getDecl()->hasFlexibleArrayMember())
4210       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4211 
4212   }
4213 
4214   const Type *Base = nullptr;
4215   uint64_t NumElts = 0;
4216   // vectorcall adds the concept of a homogenous vector aggregate, similar to
4217   // other targets.
4218   if ((IsVectorCall || IsRegCall) &&
4219       isHomogeneousAggregate(Ty, Base, NumElts)) {
4220     if (IsRegCall) {
4221       if (FreeSSERegs >= NumElts) {
4222         FreeSSERegs -= NumElts;
4223         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
4224           return ABIArgInfo::getDirect();
4225         return ABIArgInfo::getExpand();
4226       }
4227       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4228     } else if (IsVectorCall) {
4229       if (FreeSSERegs >= NumElts &&
4230           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
4231         FreeSSERegs -= NumElts;
4232         return ABIArgInfo::getDirect();
4233       } else if (IsReturnType) {
4234         return ABIArgInfo::getExpand();
4235       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
4236         // HVAs are delayed and reclassified in the 2nd step.
4237         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4238       }
4239     }
4240   }
4241 
4242   if (Ty->isMemberPointerType()) {
4243     // If the member pointer is represented by an LLVM int or ptr, pass it
4244     // directly.
4245     llvm::Type *LLTy = CGT.ConvertType(Ty);
4246     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
4247       return ABIArgInfo::getDirect();
4248   }
4249 
4250   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
4251     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4252     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4253     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4254       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4255 
4256     // Otherwise, coerce it to a small integer.
4257     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4258   }
4259 
4260   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4261     switch (BT->getKind()) {
4262     case BuiltinType::Bool:
4263       // Bool type is always extended to the ABI, other builtin types are not
4264       // extended.
4265       return ABIArgInfo::getExtend(Ty);
4266 
4267     case BuiltinType::LongDouble:
4268       // Mingw64 GCC uses the old 80 bit extended precision floating point
4269       // unit. It passes them indirectly through memory.
4270       if (IsMingw64) {
4271         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4272         if (LDF == &llvm::APFloat::x87DoubleExtended())
4273           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4274       }
4275       break;
4276 
4277     case BuiltinType::Int128:
4278     case BuiltinType::UInt128:
4279       // If it's a parameter type, the normal ABI rule is that arguments larger
4280       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4281       // even though it isn't particularly efficient.
4282       if (!IsReturnType)
4283         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4284 
4285       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4286       // Clang matches them for compatibility.
4287       return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
4288           llvm::Type::getInt64Ty(getVMContext()), 2));
4289 
4290     default:
4291       break;
4292     }
4293   }
4294 
4295   if (Ty->isExtIntType()) {
4296     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4297     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4298     // However, non-power-of-two _ExtInts will be passed as 1,2,4 or 8 bytes
4299     // anyway as long is it fits in them, so we don't have to check the power of
4300     // 2.
4301     if (Width <= 64)
4302       return ABIArgInfo::getDirect();
4303     return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4304   }
4305 
4306   return ABIArgInfo::getDirect();
4307 }
4308 
4309 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4310   const unsigned CC = FI.getCallingConvention();
4311   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4312   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4313 
4314   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4315   // classification rules.
4316   if (CC == llvm::CallingConv::X86_64_SysV) {
4317     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4318     SysVABIInfo.computeInfo(FI);
4319     return;
4320   }
4321 
4322   unsigned FreeSSERegs = 0;
4323   if (IsVectorCall) {
4324     // We can use up to 4 SSE return registers with vectorcall.
4325     FreeSSERegs = 4;
4326   } else if (IsRegCall) {
4327     // RegCall gives us 16 SSE registers.
4328     FreeSSERegs = 16;
4329   }
4330 
4331   if (!getCXXABI().classifyReturnType(FI))
4332     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4333                                   IsVectorCall, IsRegCall);
4334 
4335   if (IsVectorCall) {
4336     // We can use up to 6 SSE register parameters with vectorcall.
4337     FreeSSERegs = 6;
4338   } else if (IsRegCall) {
4339     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4340     FreeSSERegs = 16;
4341   }
4342 
4343   unsigned ArgNum = 0;
4344   unsigned ZeroSSERegs = 0;
4345   for (auto &I : FI.arguments()) {
4346     // Vectorcall in x64 only permits the first 6 arguments to be passed as
4347     // XMM/YMM registers. After the sixth argument, pretend no vector
4348     // registers are left.
4349     unsigned *MaybeFreeSSERegs =
4350         (IsVectorCall && ArgNum >= 6) ? &ZeroSSERegs : &FreeSSERegs;
4351     I.info =
4352         classify(I.type, *MaybeFreeSSERegs, false, IsVectorCall, IsRegCall);
4353     ++ArgNum;
4354   }
4355 
4356   if (IsVectorCall) {
4357     // For vectorcall, assign aggregate HVAs to any free vector registers in a
4358     // second pass.
4359     for (auto &I : FI.arguments())
4360       I.info = reclassifyHvaArgForVectorCall(I.type, FreeSSERegs, I.info);
4361   }
4362 }
4363 
4364 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4365                                     QualType Ty) const {
4366   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4367   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4368   uint64_t Width = getContext().getTypeSize(Ty);
4369   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4370 
4371   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4372                           CGF.getContext().getTypeInfoInChars(Ty),
4373                           CharUnits::fromQuantity(8),
4374                           /*allowHigherAlign*/ false);
4375 }
4376 
4377 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4378                                         llvm::Value *Address, bool Is64Bit,
4379                                         bool IsAIX) {
4380   // This is calculated from the LLVM and GCC tables and verified
4381   // against gcc output.  AFAIK all PPC ABIs use the same encoding.
4382 
4383   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4384 
4385   llvm::IntegerType *i8 = CGF.Int8Ty;
4386   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4387   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4388   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4389 
4390   // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
4391   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
4392 
4393   // 32-63: fp0-31, the 8-byte floating-point registers
4394   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4395 
4396   // 64-67 are various 4-byte or 8-byte special-purpose registers:
4397   // 64: mq
4398   // 65: lr
4399   // 66: ctr
4400   // 67: ap
4401   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
4402 
4403   // 68-76 are various 4-byte special-purpose registers:
4404   // 68-75 cr0-7
4405   // 76: xer
4406   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4407 
4408   // 77-108: v0-31, the 16-byte vector registers
4409   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4410 
4411   // 109: vrsave
4412   // 110: vscr
4413   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
4414 
4415   // AIX does not utilize the rest of the registers.
4416   if (IsAIX)
4417     return false;
4418 
4419   // 111: spe_acc
4420   // 112: spefscr
4421   // 113: sfp
4422   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
4423 
4424   if (!Is64Bit)
4425     return false;
4426 
4427   // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
4428   // or above CPU.
4429   // 64-bit only registers:
4430   // 114: tfhar
4431   // 115: tfiar
4432   // 116: texasr
4433   AssignToArrayRange(Builder, Address, Eight8, 114, 116);
4434 
4435   return false;
4436 }
4437 
4438 // AIX
4439 namespace {
4440 /// AIXABIInfo - The AIX XCOFF ABI information.
4441 class AIXABIInfo : public ABIInfo {
4442   const bool Is64Bit;
4443   const unsigned PtrByteSize;
4444   CharUnits getParamTypeAlignment(QualType Ty) const;
4445 
4446 public:
4447   AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4448       : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
4449 
4450   bool isPromotableTypeForABI(QualType Ty) const;
4451 
4452   ABIArgInfo classifyReturnType(QualType RetTy) const;
4453   ABIArgInfo classifyArgumentType(QualType Ty) const;
4454 
4455   void computeInfo(CGFunctionInfo &FI) const override {
4456     if (!getCXXABI().classifyReturnType(FI))
4457       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4458 
4459     for (auto &I : FI.arguments())
4460       I.info = classifyArgumentType(I.type);
4461   }
4462 
4463   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4464                     QualType Ty) const override;
4465 };
4466 
4467 class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
4468   const bool Is64Bit;
4469 
4470 public:
4471   AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4472       : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
4473         Is64Bit(Is64Bit) {}
4474   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4475     return 1; // r1 is the dedicated stack pointer
4476   }
4477 
4478   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4479                                llvm::Value *Address) const override;
4480 };
4481 } // namespace
4482 
4483 // Return true if the ABI requires Ty to be passed sign- or zero-
4484 // extended to 32/64 bits.
4485 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
4486   // Treat an enum type as its underlying type.
4487   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4488     Ty = EnumTy->getDecl()->getIntegerType();
4489 
4490   // Promotable integer types are required to be promoted by the ABI.
4491   if (Ty->isPromotableIntegerType())
4492     return true;
4493 
4494   if (!Is64Bit)
4495     return false;
4496 
4497   // For 64 bit mode, in addition to the usual promotable integer types, we also
4498   // need to extend all 32-bit types, since the ABI requires promotion to 64
4499   // bits.
4500   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4501     switch (BT->getKind()) {
4502     case BuiltinType::Int:
4503     case BuiltinType::UInt:
4504       return true;
4505     default:
4506       break;
4507     }
4508 
4509   return false;
4510 }
4511 
4512 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
4513   if (RetTy->isAnyComplexType())
4514     return ABIArgInfo::getDirect();
4515 
4516   if (RetTy->isVectorType())
4517     return ABIArgInfo::getDirect();
4518 
4519   if (RetTy->isVoidType())
4520     return ABIArgInfo::getIgnore();
4521 
4522   if (isAggregateTypeForABI(RetTy))
4523     return getNaturalAlignIndirect(RetTy);
4524 
4525   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4526                                         : ABIArgInfo::getDirect());
4527 }
4528 
4529 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
4530   Ty = useFirstFieldIfTransparentUnion(Ty);
4531 
4532   if (Ty->isAnyComplexType())
4533     return ABIArgInfo::getDirect();
4534 
4535   if (Ty->isVectorType())
4536     return ABIArgInfo::getDirect();
4537 
4538   if (isAggregateTypeForABI(Ty)) {
4539     // Records with non-trivial destructors/copy-constructors should not be
4540     // passed by value.
4541     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4542       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4543 
4544     CharUnits CCAlign = getParamTypeAlignment(Ty);
4545     CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
4546 
4547     return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
4548                                    /*Realign*/ TyAlign > CCAlign);
4549   }
4550 
4551   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4552                                      : ABIArgInfo::getDirect());
4553 }
4554 
4555 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
4556   // Complex types are passed just like their elements.
4557   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4558     Ty = CTy->getElementType();
4559 
4560   if (Ty->isVectorType())
4561     return CharUnits::fromQuantity(16);
4562 
4563   // If the structure contains a vector type, the alignment is 16.
4564   if (isRecordWithSIMDVectorType(getContext(), Ty))
4565     return CharUnits::fromQuantity(16);
4566 
4567   return CharUnits::fromQuantity(PtrByteSize);
4568 }
4569 
4570 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4571                               QualType Ty) const {
4572   if (Ty->isAnyComplexType())
4573     llvm::report_fatal_error("complex type is not supported on AIX yet");
4574 
4575   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4576   TypeInfo.Align = getParamTypeAlignment(Ty);
4577 
4578   CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
4579 
4580   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
4581                           SlotSize, /*AllowHigher*/ true);
4582 }
4583 
4584 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
4585     CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
4586   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
4587 }
4588 
4589 // PowerPC-32
4590 namespace {
4591 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4592 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4593   bool IsSoftFloatABI;
4594   bool IsRetSmallStructInRegABI;
4595 
4596   CharUnits getParamTypeAlignment(QualType Ty) const;
4597 
4598 public:
4599   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
4600                      bool RetSmallStructInRegABI)
4601       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
4602         IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
4603 
4604   ABIArgInfo classifyReturnType(QualType RetTy) const;
4605 
4606   void computeInfo(CGFunctionInfo &FI) const override {
4607     if (!getCXXABI().classifyReturnType(FI))
4608       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4609     for (auto &I : FI.arguments())
4610       I.info = classifyArgumentType(I.type);
4611   }
4612 
4613   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4614                     QualType Ty) const override;
4615 };
4616 
4617 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4618 public:
4619   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
4620                          bool RetSmallStructInRegABI)
4621       : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
4622             CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
4623 
4624   static bool isStructReturnInRegABI(const llvm::Triple &Triple,
4625                                      const CodeGenOptions &Opts);
4626 
4627   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4628     // This is recovered from gcc output.
4629     return 1; // r1 is the dedicated stack pointer
4630   }
4631 
4632   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4633                                llvm::Value *Address) const override;
4634 };
4635 }
4636 
4637 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4638   // Complex types are passed just like their elements.
4639   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4640     Ty = CTy->getElementType();
4641 
4642   if (Ty->isVectorType())
4643     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4644                                                                        : 4);
4645 
4646   // For single-element float/vector structs, we consider the whole type
4647   // to have the same alignment requirements as its single element.
4648   const Type *AlignTy = nullptr;
4649   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4650     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4651     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4652         (BT && BT->isFloatingPoint()))
4653       AlignTy = EltType;
4654   }
4655 
4656   if (AlignTy)
4657     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4658   return CharUnits::fromQuantity(4);
4659 }
4660 
4661 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4662   uint64_t Size;
4663 
4664   // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
4665   if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
4666       (Size = getContext().getTypeSize(RetTy)) <= 64) {
4667     // System V ABI (1995), page 3-22, specified:
4668     // > A structure or union whose size is less than or equal to 8 bytes
4669     // > shall be returned in r3 and r4, as if it were first stored in the
4670     // > 8-byte aligned memory area and then the low addressed word were
4671     // > loaded into r3 and the high-addressed word into r4.  Bits beyond
4672     // > the last member of the structure or union are not defined.
4673     //
4674     // GCC for big-endian PPC32 inserts the pad before the first member,
4675     // not "beyond the last member" of the struct.  To stay compatible
4676     // with GCC, we coerce the struct to an integer of the same size.
4677     // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
4678     if (Size == 0)
4679       return ABIArgInfo::getIgnore();
4680     else {
4681       llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
4682       return ABIArgInfo::getDirect(CoerceTy);
4683     }
4684   }
4685 
4686   return DefaultABIInfo::classifyReturnType(RetTy);
4687 }
4688 
4689 // TODO: this implementation is now likely redundant with
4690 // DefaultABIInfo::EmitVAArg.
4691 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4692                                       QualType Ty) const {
4693   if (getTarget().getTriple().isOSDarwin()) {
4694     auto TI = getContext().getTypeInfoInChars(Ty);
4695     TI.Align = getParamTypeAlignment(Ty);
4696 
4697     CharUnits SlotSize = CharUnits::fromQuantity(4);
4698     return emitVoidPtrVAArg(CGF, VAList, Ty,
4699                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4700                             /*AllowHigherAlign=*/true);
4701   }
4702 
4703   const unsigned OverflowLimit = 8;
4704   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4705     // TODO: Implement this. For now ignore.
4706     (void)CTy;
4707     return Address::invalid(); // FIXME?
4708   }
4709 
4710   // struct __va_list_tag {
4711   //   unsigned char gpr;
4712   //   unsigned char fpr;
4713   //   unsigned short reserved;
4714   //   void *overflow_arg_area;
4715   //   void *reg_save_area;
4716   // };
4717 
4718   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4719   bool isInt = !Ty->isFloatingType();
4720   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4721 
4722   // All aggregates are passed indirectly?  That doesn't seem consistent
4723   // with the argument-lowering code.
4724   bool isIndirect = isAggregateTypeForABI(Ty);
4725 
4726   CGBuilderTy &Builder = CGF.Builder;
4727 
4728   // The calling convention either uses 1-2 GPRs or 1 FPR.
4729   Address NumRegsAddr = Address::invalid();
4730   if (isInt || IsSoftFloatABI) {
4731     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4732   } else {
4733     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4734   }
4735 
4736   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4737 
4738   // "Align" the register count when TY is i64.
4739   if (isI64 || (isF64 && IsSoftFloatABI)) {
4740     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4741     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4742   }
4743 
4744   llvm::Value *CC =
4745       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4746 
4747   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4748   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4749   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4750 
4751   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4752 
4753   llvm::Type *DirectTy = CGF.ConvertType(Ty);
4754   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4755 
4756   // Case 1: consume registers.
4757   Address RegAddr = Address::invalid();
4758   {
4759     CGF.EmitBlock(UsingRegs);
4760 
4761     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4762     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4763                       CharUnits::fromQuantity(8));
4764     assert(RegAddr.getElementType() == CGF.Int8Ty);
4765 
4766     // Floating-point registers start after the general-purpose registers.
4767     if (!(isInt || IsSoftFloatABI)) {
4768       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4769                                                    CharUnits::fromQuantity(32));
4770     }
4771 
4772     // Get the address of the saved value by scaling the number of
4773     // registers we've used by the number of
4774     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4775     llvm::Value *RegOffset =
4776       Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4777     RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4778                                             RegAddr.getPointer(), RegOffset),
4779                       RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4780     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4781 
4782     // Increase the used-register count.
4783     NumRegs =
4784       Builder.CreateAdd(NumRegs,
4785                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4786     Builder.CreateStore(NumRegs, NumRegsAddr);
4787 
4788     CGF.EmitBranch(Cont);
4789   }
4790 
4791   // Case 2: consume space in the overflow area.
4792   Address MemAddr = Address::invalid();
4793   {
4794     CGF.EmitBlock(UsingOverflow);
4795 
4796     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4797 
4798     // Everything in the overflow area is rounded up to a size of at least 4.
4799     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4800 
4801     CharUnits Size;
4802     if (!isIndirect) {
4803       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4804       Size = TypeInfo.Width.alignTo(OverflowAreaAlign);
4805     } else {
4806       Size = CGF.getPointerSize();
4807     }
4808 
4809     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4810     Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
4811                          OverflowAreaAlign);
4812     // Round up address of argument to alignment
4813     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4814     if (Align > OverflowAreaAlign) {
4815       llvm::Value *Ptr = OverflowArea.getPointer();
4816       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4817                                                            Align);
4818     }
4819 
4820     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4821 
4822     // Increase the overflow area.
4823     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4824     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4825     CGF.EmitBranch(Cont);
4826   }
4827 
4828   CGF.EmitBlock(Cont);
4829 
4830   // Merge the cases with a phi.
4831   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4832                                 "vaarg.addr");
4833 
4834   // Load the pointer if the argument was passed indirectly.
4835   if (isIndirect) {
4836     Result = Address(Builder.CreateLoad(Result, "aggr"),
4837                      getContext().getTypeAlignInChars(Ty));
4838   }
4839 
4840   return Result;
4841 }
4842 
4843 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
4844     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4845   assert(Triple.isPPC32());
4846 
4847   switch (Opts.getStructReturnConvention()) {
4848   case CodeGenOptions::SRCK_Default:
4849     break;
4850   case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
4851     return false;
4852   case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
4853     return true;
4854   }
4855 
4856   if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
4857     return true;
4858 
4859   return false;
4860 }
4861 
4862 bool
4863 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4864                                                 llvm::Value *Address) const {
4865   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
4866                                      /*IsAIX*/ false);
4867 }
4868 
4869 // PowerPC-64
4870 
4871 namespace {
4872 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4873 class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
4874 public:
4875   enum ABIKind {
4876     ELFv1 = 0,
4877     ELFv2
4878   };
4879 
4880 private:
4881   static const unsigned GPRBits = 64;
4882   ABIKind Kind;
4883   bool IsSoftFloatABI;
4884 
4885 public:
4886   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind,
4887                      bool SoftFloatABI)
4888       : SwiftABIInfo(CGT), Kind(Kind), IsSoftFloatABI(SoftFloatABI) {}
4889 
4890   bool isPromotableTypeForABI(QualType Ty) const;
4891   CharUnits getParamTypeAlignment(QualType Ty) const;
4892 
4893   ABIArgInfo classifyReturnType(QualType RetTy) const;
4894   ABIArgInfo classifyArgumentType(QualType Ty) const;
4895 
4896   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4897   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4898                                          uint64_t Members) const override;
4899 
4900   // TODO: We can add more logic to computeInfo to improve performance.
4901   // Example: For aggregate arguments that fit in a register, we could
4902   // use getDirectInReg (as is done below for structs containing a single
4903   // floating-point value) to avoid pushing them to memory on function
4904   // entry.  This would require changing the logic in PPCISelLowering
4905   // when lowering the parameters in the caller and args in the callee.
4906   void computeInfo(CGFunctionInfo &FI) const override {
4907     if (!getCXXABI().classifyReturnType(FI))
4908       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4909     for (auto &I : FI.arguments()) {
4910       // We rely on the default argument classification for the most part.
4911       // One exception:  An aggregate containing a single floating-point
4912       // or vector item must be passed in a register if one is available.
4913       const Type *T = isSingleElementStruct(I.type, getContext());
4914       if (T) {
4915         const BuiltinType *BT = T->getAs<BuiltinType>();
4916         if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
4917             (BT && BT->isFloatingPoint())) {
4918           QualType QT(T, 0);
4919           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
4920           continue;
4921         }
4922       }
4923       I.info = classifyArgumentType(I.type);
4924     }
4925   }
4926 
4927   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4928                     QualType Ty) const override;
4929 
4930   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
4931                                     bool asReturnValue) const override {
4932     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4933   }
4934 
4935   bool isSwiftErrorInRegister() const override {
4936     return false;
4937   }
4938 };
4939 
4940 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
4941 
4942 public:
4943   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
4944                                PPC64_SVR4_ABIInfo::ABIKind Kind,
4945                                bool SoftFloatABI)
4946       : TargetCodeGenInfo(
4947             std::make_unique<PPC64_SVR4_ABIInfo>(CGT, Kind, SoftFloatABI)) {}
4948 
4949   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4950     // This is recovered from gcc output.
4951     return 1; // r1 is the dedicated stack pointer
4952   }
4953 
4954   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4955                                llvm::Value *Address) const override;
4956 };
4957 
4958 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4959 public:
4960   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4961 
4962   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4963     // This is recovered from gcc output.
4964     return 1; // r1 is the dedicated stack pointer
4965   }
4966 
4967   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4968                                llvm::Value *Address) const override;
4969 };
4970 
4971 }
4972 
4973 // Return true if the ABI requires Ty to be passed sign- or zero-
4974 // extended to 64 bits.
4975 bool
4976 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4977   // Treat an enum type as its underlying type.
4978   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4979     Ty = EnumTy->getDecl()->getIntegerType();
4980 
4981   // Promotable integer types are required to be promoted by the ABI.
4982   if (isPromotableIntegerTypeForABI(Ty))
4983     return true;
4984 
4985   // In addition to the usual promotable integer types, we also need to
4986   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4987   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4988     switch (BT->getKind()) {
4989     case BuiltinType::Int:
4990     case BuiltinType::UInt:
4991       return true;
4992     default:
4993       break;
4994     }
4995 
4996   if (const auto *EIT = Ty->getAs<ExtIntType>())
4997     if (EIT->getNumBits() < 64)
4998       return true;
4999 
5000   return false;
5001 }
5002 
5003 /// isAlignedParamType - Determine whether a type requires 16-byte or
5004 /// higher alignment in the parameter area.  Always returns at least 8.
5005 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
5006   // Complex types are passed just like their elements.
5007   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
5008     Ty = CTy->getElementType();
5009 
5010   // Only vector types of size 16 bytes need alignment (larger types are
5011   // passed via reference, smaller types are not aligned).
5012   if (Ty->isVectorType()) {
5013     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5014   } else if (Ty->isRealFloatingType() &&
5015              &getContext().getFloatTypeSemantics(Ty) ==
5016                  &llvm::APFloat::IEEEquad()) {
5017     // According to ABI document section 'Optional Save Areas': If extended
5018     // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
5019     // format are supported, map them to a single quadword, quadword aligned.
5020     return CharUnits::fromQuantity(16);
5021   }
5022 
5023   // For single-element float/vector structs, we consider the whole type
5024   // to have the same alignment requirements as its single element.
5025   const Type *AlignAsType = nullptr;
5026   const Type *EltType = isSingleElementStruct(Ty, getContext());
5027   if (EltType) {
5028     const BuiltinType *BT = EltType->getAs<BuiltinType>();
5029     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
5030         (BT && BT->isFloatingPoint()))
5031       AlignAsType = EltType;
5032   }
5033 
5034   // Likewise for ELFv2 homogeneous aggregates.
5035   const Type *Base = nullptr;
5036   uint64_t Members = 0;
5037   if (!AlignAsType && Kind == ELFv2 &&
5038       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
5039     AlignAsType = Base;
5040 
5041   // With special case aggregates, only vector base types need alignment.
5042   if (AlignAsType) {
5043     return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
5044   }
5045 
5046   // Otherwise, we only need alignment for any aggregate type that
5047   // has an alignment requirement of >= 16 bytes.
5048   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
5049     return CharUnits::fromQuantity(16);
5050   }
5051 
5052   return CharUnits::fromQuantity(8);
5053 }
5054 
5055 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
5056 /// aggregate.  Base is set to the base element type, and Members is set
5057 /// to the number of base elements.
5058 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
5059                                      uint64_t &Members) const {
5060   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
5061     uint64_t NElements = AT->getSize().getZExtValue();
5062     if (NElements == 0)
5063       return false;
5064     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
5065       return false;
5066     Members *= NElements;
5067   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
5068     const RecordDecl *RD = RT->getDecl();
5069     if (RD->hasFlexibleArrayMember())
5070       return false;
5071 
5072     Members = 0;
5073 
5074     // If this is a C++ record, check the properties of the record such as
5075     // bases and ABI specific restrictions
5076     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5077       if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
5078         return false;
5079 
5080       for (const auto &I : CXXRD->bases()) {
5081         // Ignore empty records.
5082         if (isEmptyRecord(getContext(), I.getType(), true))
5083           continue;
5084 
5085         uint64_t FldMembers;
5086         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
5087           return false;
5088 
5089         Members += FldMembers;
5090       }
5091     }
5092 
5093     for (const auto *FD : RD->fields()) {
5094       // Ignore (non-zero arrays of) empty records.
5095       QualType FT = FD->getType();
5096       while (const ConstantArrayType *AT =
5097              getContext().getAsConstantArrayType(FT)) {
5098         if (AT->getSize().getZExtValue() == 0)
5099           return false;
5100         FT = AT->getElementType();
5101       }
5102       if (isEmptyRecord(getContext(), FT, true))
5103         continue;
5104 
5105       // For compatibility with GCC, ignore empty bitfields in C++ mode.
5106       if (getContext().getLangOpts().CPlusPlus &&
5107           FD->isZeroLengthBitField(getContext()))
5108         continue;
5109 
5110       uint64_t FldMembers;
5111       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
5112         return false;
5113 
5114       Members = (RD->isUnion() ?
5115                  std::max(Members, FldMembers) : Members + FldMembers);
5116     }
5117 
5118     if (!Base)
5119       return false;
5120 
5121     // Ensure there is no padding.
5122     if (getContext().getTypeSize(Base) * Members !=
5123         getContext().getTypeSize(Ty))
5124       return false;
5125   } else {
5126     Members = 1;
5127     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
5128       Members = 2;
5129       Ty = CT->getElementType();
5130     }
5131 
5132     // Most ABIs only support float, double, and some vector type widths.
5133     if (!isHomogeneousAggregateBaseType(Ty))
5134       return false;
5135 
5136     // The base type must be the same for all members.  Types that
5137     // agree in both total size and mode (float vs. vector) are
5138     // treated as being equivalent here.
5139     const Type *TyPtr = Ty.getTypePtr();
5140     if (!Base) {
5141       Base = TyPtr;
5142       // If it's a non-power-of-2 vector, its size is already a power-of-2,
5143       // so make sure to widen it explicitly.
5144       if (const VectorType *VT = Base->getAs<VectorType>()) {
5145         QualType EltTy = VT->getElementType();
5146         unsigned NumElements =
5147             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
5148         Base = getContext()
5149                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
5150                    .getTypePtr();
5151       }
5152     }
5153 
5154     if (Base->isVectorType() != TyPtr->isVectorType() ||
5155         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
5156       return false;
5157   }
5158   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
5159 }
5160 
5161 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5162   // Homogeneous aggregates for ELFv2 must have base types of float,
5163   // double, long double, or 128-bit vectors.
5164   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5165     if (BT->getKind() == BuiltinType::Float ||
5166         BT->getKind() == BuiltinType::Double ||
5167         BT->getKind() == BuiltinType::LongDouble ||
5168         (getContext().getTargetInfo().hasFloat128Type() &&
5169           (BT->getKind() == BuiltinType::Float128))) {
5170       if (IsSoftFloatABI)
5171         return false;
5172       return true;
5173     }
5174   }
5175   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5176     if (getContext().getTypeSize(VT) == 128)
5177       return true;
5178   }
5179   return false;
5180 }
5181 
5182 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
5183     const Type *Base, uint64_t Members) const {
5184   // Vector and fp128 types require one register, other floating point types
5185   // require one or two registers depending on their size.
5186   uint32_t NumRegs =
5187       ((getContext().getTargetInfo().hasFloat128Type() &&
5188           Base->isFloat128Type()) ||
5189         Base->isVectorType()) ? 1
5190                               : (getContext().getTypeSize(Base) + 63) / 64;
5191 
5192   // Homogeneous Aggregates may occupy at most 8 registers.
5193   return Members * NumRegs <= 8;
5194 }
5195 
5196 ABIArgInfo
5197 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
5198   Ty = useFirstFieldIfTransparentUnion(Ty);
5199 
5200   if (Ty->isAnyComplexType())
5201     return ABIArgInfo::getDirect();
5202 
5203   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
5204   // or via reference (larger than 16 bytes).
5205   if (Ty->isVectorType()) {
5206     uint64_t Size = getContext().getTypeSize(Ty);
5207     if (Size > 128)
5208       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5209     else if (Size < 128) {
5210       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5211       return ABIArgInfo::getDirect(CoerceTy);
5212     }
5213   }
5214 
5215   if (const auto *EIT = Ty->getAs<ExtIntType>())
5216     if (EIT->getNumBits() > 128)
5217       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
5218 
5219   if (isAggregateTypeForABI(Ty)) {
5220     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5221       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5222 
5223     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
5224     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5225 
5226     // ELFv2 homogeneous aggregates are passed as array types.
5227     const Type *Base = nullptr;
5228     uint64_t Members = 0;
5229     if (Kind == ELFv2 &&
5230         isHomogeneousAggregate(Ty, Base, Members)) {
5231       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5232       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5233       return ABIArgInfo::getDirect(CoerceTy);
5234     }
5235 
5236     // If an aggregate may end up fully in registers, we do not
5237     // use the ByVal method, but pass the aggregate as array.
5238     // This is usually beneficial since we avoid forcing the
5239     // back-end to store the argument to memory.
5240     uint64_t Bits = getContext().getTypeSize(Ty);
5241     if (Bits > 0 && Bits <= 8 * GPRBits) {
5242       llvm::Type *CoerceTy;
5243 
5244       // Types up to 8 bytes are passed as integer type (which will be
5245       // properly aligned in the argument save area doubleword).
5246       if (Bits <= GPRBits)
5247         CoerceTy =
5248             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5249       // Larger types are passed as arrays, with the base type selected
5250       // according to the required alignment in the save area.
5251       else {
5252         uint64_t RegBits = ABIAlign * 8;
5253         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
5254         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
5255         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
5256       }
5257 
5258       return ABIArgInfo::getDirect(CoerceTy);
5259     }
5260 
5261     // All other aggregates are passed ByVal.
5262     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5263                                    /*ByVal=*/true,
5264                                    /*Realign=*/TyAlign > ABIAlign);
5265   }
5266 
5267   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
5268                                      : ABIArgInfo::getDirect());
5269 }
5270 
5271 ABIArgInfo
5272 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
5273   if (RetTy->isVoidType())
5274     return ABIArgInfo::getIgnore();
5275 
5276   if (RetTy->isAnyComplexType())
5277     return ABIArgInfo::getDirect();
5278 
5279   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
5280   // or via reference (larger than 16 bytes).
5281   if (RetTy->isVectorType()) {
5282     uint64_t Size = getContext().getTypeSize(RetTy);
5283     if (Size > 128)
5284       return getNaturalAlignIndirect(RetTy);
5285     else if (Size < 128) {
5286       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5287       return ABIArgInfo::getDirect(CoerceTy);
5288     }
5289   }
5290 
5291   if (const auto *EIT = RetTy->getAs<ExtIntType>())
5292     if (EIT->getNumBits() > 128)
5293       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
5294 
5295   if (isAggregateTypeForABI(RetTy)) {
5296     // ELFv2 homogeneous aggregates are returned as array types.
5297     const Type *Base = nullptr;
5298     uint64_t Members = 0;
5299     if (Kind == ELFv2 &&
5300         isHomogeneousAggregate(RetTy, Base, Members)) {
5301       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5302       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5303       return ABIArgInfo::getDirect(CoerceTy);
5304     }
5305 
5306     // ELFv2 small aggregates are returned in up to two registers.
5307     uint64_t Bits = getContext().getTypeSize(RetTy);
5308     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
5309       if (Bits == 0)
5310         return ABIArgInfo::getIgnore();
5311 
5312       llvm::Type *CoerceTy;
5313       if (Bits > GPRBits) {
5314         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
5315         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
5316       } else
5317         CoerceTy =
5318             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5319       return ABIArgInfo::getDirect(CoerceTy);
5320     }
5321 
5322     // All other aggregates are returned indirectly.
5323     return getNaturalAlignIndirect(RetTy);
5324   }
5325 
5326   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
5327                                         : ABIArgInfo::getDirect());
5328 }
5329 
5330 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
5331 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5332                                       QualType Ty) const {
5333   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
5334   TypeInfo.Align = getParamTypeAlignment(Ty);
5335 
5336   CharUnits SlotSize = CharUnits::fromQuantity(8);
5337 
5338   // If we have a complex type and the base type is smaller than 8 bytes,
5339   // the ABI calls for the real and imaginary parts to be right-adjusted
5340   // in separate doublewords.  However, Clang expects us to produce a
5341   // pointer to a structure with the two parts packed tightly.  So generate
5342   // loads of the real and imaginary parts relative to the va_list pointer,
5343   // and store them to a temporary structure.
5344   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
5345     CharUnits EltSize = TypeInfo.Width / 2;
5346     if (EltSize < SlotSize) {
5347       Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
5348                                             SlotSize * 2, SlotSize,
5349                                             SlotSize, /*AllowHigher*/ true);
5350 
5351       Address RealAddr = Addr;
5352       Address ImagAddr = RealAddr;
5353       if (CGF.CGM.getDataLayout().isBigEndian()) {
5354         RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
5355                                                           SlotSize - EltSize);
5356         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
5357                                                       2 * SlotSize - EltSize);
5358       } else {
5359         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
5360       }
5361 
5362       llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
5363       RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
5364       ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
5365       llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
5366       llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
5367 
5368       Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
5369       CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
5370                              /*init*/ true);
5371       return Temp;
5372     }
5373   }
5374 
5375   // Otherwise, just use the general rule.
5376   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
5377                           TypeInfo, SlotSize, /*AllowHigher*/ true);
5378 }
5379 
5380 bool
5381 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
5382   CodeGen::CodeGenFunction &CGF,
5383   llvm::Value *Address) const {
5384   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5385                                      /*IsAIX*/ false);
5386 }
5387 
5388 bool
5389 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5390                                                 llvm::Value *Address) const {
5391   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5392                                      /*IsAIX*/ false);
5393 }
5394 
5395 //===----------------------------------------------------------------------===//
5396 // AArch64 ABI Implementation
5397 //===----------------------------------------------------------------------===//
5398 
5399 namespace {
5400 
5401 class AArch64ABIInfo : public SwiftABIInfo {
5402 public:
5403   enum ABIKind {
5404     AAPCS = 0,
5405     DarwinPCS,
5406     Win64
5407   };
5408 
5409 private:
5410   ABIKind Kind;
5411 
5412 public:
5413   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
5414     : SwiftABIInfo(CGT), Kind(Kind) {}
5415 
5416 private:
5417   ABIKind getABIKind() const { return Kind; }
5418   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5419 
5420   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5421   ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
5422                                   unsigned CallingConvention) const;
5423   ABIArgInfo coerceIllegalVector(QualType Ty) const;
5424   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5425   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5426                                          uint64_t Members) const override;
5427 
5428   bool isIllegalVectorType(QualType Ty) const;
5429 
5430   void computeInfo(CGFunctionInfo &FI) const override {
5431     if (!::classifyReturnType(getCXXABI(), FI, *this))
5432       FI.getReturnInfo() =
5433           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5434 
5435     for (auto &it : FI.arguments())
5436       it.info = classifyArgumentType(it.type, FI.isVariadic(),
5437                                      FI.getCallingConvention());
5438   }
5439 
5440   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5441                           CodeGenFunction &CGF) const;
5442 
5443   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5444                          CodeGenFunction &CGF) const;
5445 
5446   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5447                     QualType Ty) const override {
5448     llvm::Type *BaseTy = CGF.ConvertType(Ty);
5449     if (isa<llvm::ScalableVectorType>(BaseTy))
5450       llvm::report_fatal_error("Passing SVE types to variadic functions is "
5451                                "currently not supported");
5452 
5453     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5454                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5455                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5456   }
5457 
5458   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5459                       QualType Ty) const override;
5460 
5461   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5462                                     bool asReturnValue) const override {
5463     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5464   }
5465   bool isSwiftErrorInRegister() const override {
5466     return true;
5467   }
5468 
5469   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5470                                  unsigned elts) const override;
5471 
5472   bool allowBFloatArgsAndRet() const override {
5473     return getTarget().hasBFloat16Type();
5474   }
5475 };
5476 
5477 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5478 public:
5479   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5480       : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
5481 
5482   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5483     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5484   }
5485 
5486   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5487     return 31;
5488   }
5489 
5490   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5491 
5492   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5493                            CodeGen::CodeGenModule &CGM) const override {
5494     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5495     if (!FD)
5496       return;
5497 
5498     const auto *TA = FD->getAttr<TargetAttr>();
5499     if (TA == nullptr)
5500       return;
5501 
5502     ParsedTargetAttr Attr = TA->parse();
5503     if (Attr.BranchProtection.empty())
5504       return;
5505 
5506     TargetInfo::BranchProtectionInfo BPI;
5507     StringRef Error;
5508     (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
5509                                                    BPI, Error);
5510     assert(Error.empty());
5511 
5512     auto *Fn = cast<llvm::Function>(GV);
5513     static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
5514     Fn->addFnAttr("sign-return-address", SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
5515 
5516     if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
5517       Fn->addFnAttr("sign-return-address-key",
5518                     BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
5519                         ? "a_key"
5520                         : "b_key");
5521     }
5522 
5523     Fn->addFnAttr("branch-target-enforcement",
5524                   BPI.BranchTargetEnforcement ? "true" : "false");
5525   }
5526 };
5527 
5528 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5529 public:
5530   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5531       : AArch64TargetCodeGenInfo(CGT, K) {}
5532 
5533   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5534                            CodeGen::CodeGenModule &CGM) const override;
5535 
5536   void getDependentLibraryOption(llvm::StringRef Lib,
5537                                  llvm::SmallString<24> &Opt) const override {
5538     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5539   }
5540 
5541   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5542                                llvm::SmallString<32> &Opt) const override {
5543     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5544   }
5545 };
5546 
5547 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5548     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5549   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5550   if (GV->isDeclaration())
5551     return;
5552   addStackProbeTargetAttributes(D, GV, CGM);
5553 }
5554 }
5555 
5556 ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty) const {
5557   assert(Ty->isVectorType() && "expected vector type!");
5558 
5559   const auto *VT = Ty->castAs<VectorType>();
5560   if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
5561     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5562     assert(VT->getElementType()->castAs<BuiltinType>()->getKind() ==
5563                BuiltinType::UChar &&
5564            "unexpected builtin type for SVE predicate!");
5565     return ABIArgInfo::getDirect(llvm::ScalableVectorType::get(
5566         llvm::Type::getInt1Ty(getVMContext()), 16));
5567   }
5568 
5569   if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) {
5570     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5571 
5572     const auto *BT = VT->getElementType()->castAs<BuiltinType>();
5573     llvm::ScalableVectorType *ResType = nullptr;
5574     switch (BT->getKind()) {
5575     default:
5576       llvm_unreachable("unexpected builtin type for SVE vector!");
5577     case BuiltinType::SChar:
5578     case BuiltinType::UChar:
5579       ResType = llvm::ScalableVectorType::get(
5580           llvm::Type::getInt8Ty(getVMContext()), 16);
5581       break;
5582     case BuiltinType::Short:
5583     case BuiltinType::UShort:
5584       ResType = llvm::ScalableVectorType::get(
5585           llvm::Type::getInt16Ty(getVMContext()), 8);
5586       break;
5587     case BuiltinType::Int:
5588     case BuiltinType::UInt:
5589       ResType = llvm::ScalableVectorType::get(
5590           llvm::Type::getInt32Ty(getVMContext()), 4);
5591       break;
5592     case BuiltinType::Long:
5593     case BuiltinType::ULong:
5594       ResType = llvm::ScalableVectorType::get(
5595           llvm::Type::getInt64Ty(getVMContext()), 2);
5596       break;
5597     case BuiltinType::Half:
5598       ResType = llvm::ScalableVectorType::get(
5599           llvm::Type::getHalfTy(getVMContext()), 8);
5600       break;
5601     case BuiltinType::Float:
5602       ResType = llvm::ScalableVectorType::get(
5603           llvm::Type::getFloatTy(getVMContext()), 4);
5604       break;
5605     case BuiltinType::Double:
5606       ResType = llvm::ScalableVectorType::get(
5607           llvm::Type::getDoubleTy(getVMContext()), 2);
5608       break;
5609     case BuiltinType::BFloat16:
5610       ResType = llvm::ScalableVectorType::get(
5611           llvm::Type::getBFloatTy(getVMContext()), 8);
5612       break;
5613     }
5614     return ABIArgInfo::getDirect(ResType);
5615   }
5616 
5617   uint64_t Size = getContext().getTypeSize(Ty);
5618   // Android promotes <2 x i8> to i16, not i32
5619   if (isAndroid() && (Size <= 16)) {
5620     llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5621     return ABIArgInfo::getDirect(ResType);
5622   }
5623   if (Size <= 32) {
5624     llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5625     return ABIArgInfo::getDirect(ResType);
5626   }
5627   if (Size == 64) {
5628     auto *ResType =
5629         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5630     return ABIArgInfo::getDirect(ResType);
5631   }
5632   if (Size == 128) {
5633     auto *ResType =
5634         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5635     return ABIArgInfo::getDirect(ResType);
5636   }
5637   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5638 }
5639 
5640 ABIArgInfo
5641 AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadic,
5642                                      unsigned CallingConvention) const {
5643   Ty = useFirstFieldIfTransparentUnion(Ty);
5644 
5645   // Handle illegal vector types here.
5646   if (isIllegalVectorType(Ty))
5647     return coerceIllegalVector(Ty);
5648 
5649   if (!isAggregateTypeForABI(Ty)) {
5650     // Treat an enum type as its underlying type.
5651     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5652       Ty = EnumTy->getDecl()->getIntegerType();
5653 
5654     if (const auto *EIT = Ty->getAs<ExtIntType>())
5655       if (EIT->getNumBits() > 128)
5656         return getNaturalAlignIndirect(Ty);
5657 
5658     return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
5659                 ? ABIArgInfo::getExtend(Ty)
5660                 : ABIArgInfo::getDirect());
5661   }
5662 
5663   // Structures with either a non-trivial destructor or a non-trivial
5664   // copy constructor are always indirect.
5665   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5666     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5667                                      CGCXXABI::RAA_DirectInMemory);
5668   }
5669 
5670   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5671   // elsewhere for GNU compatibility.
5672   uint64_t Size = getContext().getTypeSize(Ty);
5673   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5674   if (IsEmpty || Size == 0) {
5675     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5676       return ABIArgInfo::getIgnore();
5677 
5678     // GNU C mode. The only argument that gets ignored is an empty one with size
5679     // 0.
5680     if (IsEmpty && Size == 0)
5681       return ABIArgInfo::getIgnore();
5682     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5683   }
5684 
5685   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5686   const Type *Base = nullptr;
5687   uint64_t Members = 0;
5688   bool IsWin64 = Kind == Win64 || CallingConvention == llvm::CallingConv::Win64;
5689   bool IsWinVariadic = IsWin64 && IsVariadic;
5690   // In variadic functions on Windows, all composite types are treated alike,
5691   // no special handling of HFAs/HVAs.
5692   if (!IsWinVariadic && isHomogeneousAggregate(Ty, Base, Members)) {
5693     if (Kind != AArch64ABIInfo::AAPCS)
5694       return ABIArgInfo::getDirect(
5695           llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5696 
5697     // For alignment adjusted HFAs, cap the argument alignment to 16, leave it
5698     // default otherwise.
5699     unsigned Align =
5700         getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
5701     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
5702     Align = (Align > BaseAlign && Align >= 16) ? 16 : 0;
5703     return ABIArgInfo::getDirect(
5704         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members), 0,
5705         nullptr, true, Align);
5706   }
5707 
5708   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5709   if (Size <= 128) {
5710     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5711     // same size and alignment.
5712     if (getTarget().isRenderScriptTarget()) {
5713       return coerceToIntArray(Ty, getContext(), getVMContext());
5714     }
5715     unsigned Alignment;
5716     if (Kind == AArch64ABIInfo::AAPCS) {
5717       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5718       Alignment = Alignment < 128 ? 64 : 128;
5719     } else {
5720       Alignment = std::max(getContext().getTypeAlign(Ty),
5721                            (unsigned)getTarget().getPointerWidth(0));
5722     }
5723     Size = llvm::alignTo(Size, Alignment);
5724 
5725     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5726     // For aggregates with 16-byte alignment, we use i128.
5727     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5728     return ABIArgInfo::getDirect(
5729         Size == Alignment ? BaseTy
5730                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5731   }
5732 
5733   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5734 }
5735 
5736 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5737                                               bool IsVariadic) const {
5738   if (RetTy->isVoidType())
5739     return ABIArgInfo::getIgnore();
5740 
5741   if (const auto *VT = RetTy->getAs<VectorType>()) {
5742     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5743         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5744       return coerceIllegalVector(RetTy);
5745   }
5746 
5747   // Large vector types should be returned via memory.
5748   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5749     return getNaturalAlignIndirect(RetTy);
5750 
5751   if (!isAggregateTypeForABI(RetTy)) {
5752     // Treat an enum type as its underlying type.
5753     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5754       RetTy = EnumTy->getDecl()->getIntegerType();
5755 
5756     if (const auto *EIT = RetTy->getAs<ExtIntType>())
5757       if (EIT->getNumBits() > 128)
5758         return getNaturalAlignIndirect(RetTy);
5759 
5760     return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
5761                 ? ABIArgInfo::getExtend(RetTy)
5762                 : ABIArgInfo::getDirect());
5763   }
5764 
5765   uint64_t Size = getContext().getTypeSize(RetTy);
5766   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5767     return ABIArgInfo::getIgnore();
5768 
5769   const Type *Base = nullptr;
5770   uint64_t Members = 0;
5771   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5772       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5773         IsVariadic))
5774     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5775     return ABIArgInfo::getDirect();
5776 
5777   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5778   if (Size <= 128) {
5779     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5780     // same size and alignment.
5781     if (getTarget().isRenderScriptTarget()) {
5782       return coerceToIntArray(RetTy, getContext(), getVMContext());
5783     }
5784 
5785     if (Size <= 64 && getDataLayout().isLittleEndian()) {
5786       // Composite types are returned in lower bits of a 64-bit register for LE,
5787       // and in higher bits for BE. However, integer types are always returned
5788       // in lower bits for both LE and BE, and they are not rounded up to
5789       // 64-bits. We can skip rounding up of composite types for LE, but not for
5790       // BE, otherwise composite types will be indistinguishable from integer
5791       // types.
5792       return ABIArgInfo::getDirect(
5793           llvm::IntegerType::get(getVMContext(), Size));
5794     }
5795 
5796     unsigned Alignment = getContext().getTypeAlign(RetTy);
5797     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5798 
5799     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5800     // For aggregates with 16-byte alignment, we use i128.
5801     if (Alignment < 128 && Size == 128) {
5802       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5803       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5804     }
5805     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5806   }
5807 
5808   return getNaturalAlignIndirect(RetTy);
5809 }
5810 
5811 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5812 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5813   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5814     // Check whether VT is a fixed-length SVE vector. These types are
5815     // represented as scalable vectors in function args/return and must be
5816     // coerced from fixed vectors.
5817     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5818         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5819       return true;
5820 
5821     // Check whether VT is legal.
5822     unsigned NumElements = VT->getNumElements();
5823     uint64_t Size = getContext().getTypeSize(VT);
5824     // NumElements should be power of 2.
5825     if (!llvm::isPowerOf2_32(NumElements))
5826       return true;
5827 
5828     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5829     // vectors for some reason.
5830     llvm::Triple Triple = getTarget().getTriple();
5831     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5832         Triple.isOSBinFormatMachO())
5833       return Size <= 32;
5834 
5835     return Size != 64 && (Size != 128 || NumElements == 1);
5836   }
5837   return false;
5838 }
5839 
5840 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5841                                                llvm::Type *eltTy,
5842                                                unsigned elts) const {
5843   if (!llvm::isPowerOf2_32(elts))
5844     return false;
5845   if (totalSize.getQuantity() != 8 &&
5846       (totalSize.getQuantity() != 16 || elts == 1))
5847     return false;
5848   return true;
5849 }
5850 
5851 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5852   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5853   // point type or a short-vector type. This is the same as the 32-bit ABI,
5854   // but with the difference that any floating-point type is allowed,
5855   // including __fp16.
5856   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5857     if (BT->isFloatingPoint())
5858       return true;
5859   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5860     unsigned VecSize = getContext().getTypeSize(VT);
5861     if (VecSize == 64 || VecSize == 128)
5862       return true;
5863   }
5864   return false;
5865 }
5866 
5867 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5868                                                        uint64_t Members) const {
5869   return Members <= 4;
5870 }
5871 
5872 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5873                                        CodeGenFunction &CGF) const {
5874   ABIArgInfo AI = classifyArgumentType(Ty, /*IsVariadic=*/true,
5875                                        CGF.CurFnInfo->getCallingConvention());
5876   bool IsIndirect = AI.isIndirect();
5877 
5878   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5879   if (IsIndirect)
5880     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5881   else if (AI.getCoerceToType())
5882     BaseTy = AI.getCoerceToType();
5883 
5884   unsigned NumRegs = 1;
5885   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5886     BaseTy = ArrTy->getElementType();
5887     NumRegs = ArrTy->getNumElements();
5888   }
5889   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5890 
5891   // The AArch64 va_list type and handling is specified in the Procedure Call
5892   // Standard, section B.4:
5893   //
5894   // struct {
5895   //   void *__stack;
5896   //   void *__gr_top;
5897   //   void *__vr_top;
5898   //   int __gr_offs;
5899   //   int __vr_offs;
5900   // };
5901 
5902   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5903   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5904   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5905   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5906 
5907   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
5908   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
5909 
5910   Address reg_offs_p = Address::invalid();
5911   llvm::Value *reg_offs = nullptr;
5912   int reg_top_index;
5913   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
5914   if (!IsFPR) {
5915     // 3 is the field number of __gr_offs
5916     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
5917     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5918     reg_top_index = 1; // field number for __gr_top
5919     RegSize = llvm::alignTo(RegSize, 8);
5920   } else {
5921     // 4 is the field number of __vr_offs.
5922     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
5923     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5924     reg_top_index = 2; // field number for __vr_top
5925     RegSize = 16 * NumRegs;
5926   }
5927 
5928   //=======================================
5929   // Find out where argument was passed
5930   //=======================================
5931 
5932   // If reg_offs >= 0 we're already using the stack for this type of
5933   // argument. We don't want to keep updating reg_offs (in case it overflows,
5934   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5935   // whatever they get).
5936   llvm::Value *UsingStack = nullptr;
5937   UsingStack = CGF.Builder.CreateICmpSGE(
5938       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5939 
5940   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5941 
5942   // Otherwise, at least some kind of argument could go in these registers, the
5943   // question is whether this particular type is too big.
5944   CGF.EmitBlock(MaybeRegBlock);
5945 
5946   // Integer arguments may need to correct register alignment (for example a
5947   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5948   // align __gr_offs to calculate the potential address.
5949   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5950     int Align = TyAlign.getQuantity();
5951 
5952     reg_offs = CGF.Builder.CreateAdd(
5953         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5954         "align_regoffs");
5955     reg_offs = CGF.Builder.CreateAnd(
5956         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5957         "aligned_regoffs");
5958   }
5959 
5960   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
5961   // The fact that this is done unconditionally reflects the fact that
5962   // allocating an argument to the stack also uses up all the remaining
5963   // registers of the appropriate kind.
5964   llvm::Value *NewOffset = nullptr;
5965   NewOffset = CGF.Builder.CreateAdd(
5966       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5967   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5968 
5969   // Now we're in a position to decide whether this argument really was in
5970   // registers or not.
5971   llvm::Value *InRegs = nullptr;
5972   InRegs = CGF.Builder.CreateICmpSLE(
5973       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5974 
5975   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5976 
5977   //=======================================
5978   // Argument was in registers
5979   //=======================================
5980 
5981   // Now we emit the code for if the argument was originally passed in
5982   // registers. First start the appropriate block:
5983   CGF.EmitBlock(InRegBlock);
5984 
5985   llvm::Value *reg_top = nullptr;
5986   Address reg_top_p =
5987       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
5988   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
5989   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, reg_top, reg_offs),
5990                    CharUnits::fromQuantity(IsFPR ? 16 : 8));
5991   Address RegAddr = Address::invalid();
5992   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
5993 
5994   if (IsIndirect) {
5995     // If it's been passed indirectly (actually a struct), whatever we find from
5996     // stored registers or on the stack will actually be a struct **.
5997     MemTy = llvm::PointerType::getUnqual(MemTy);
5998   }
5999 
6000   const Type *Base = nullptr;
6001   uint64_t NumMembers = 0;
6002   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
6003   if (IsHFA && NumMembers > 1) {
6004     // Homogeneous aggregates passed in registers will have their elements split
6005     // and stored 16-bytes apart regardless of size (they're notionally in qN,
6006     // qN+1, ...). We reload and store into a temporary local variable
6007     // contiguously.
6008     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
6009     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
6010     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
6011     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
6012     Address Tmp = CGF.CreateTempAlloca(HFATy,
6013                                        std::max(TyAlign, BaseTyInfo.Align));
6014 
6015     // On big-endian platforms, the value will be right-aligned in its slot.
6016     int Offset = 0;
6017     if (CGF.CGM.getDataLayout().isBigEndian() &&
6018         BaseTyInfo.Width.getQuantity() < 16)
6019       Offset = 16 - BaseTyInfo.Width.getQuantity();
6020 
6021     for (unsigned i = 0; i < NumMembers; ++i) {
6022       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
6023       Address LoadAddr =
6024         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
6025       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
6026 
6027       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
6028 
6029       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
6030       CGF.Builder.CreateStore(Elem, StoreAddr);
6031     }
6032 
6033     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
6034   } else {
6035     // Otherwise the object is contiguous in memory.
6036 
6037     // It might be right-aligned in its slot.
6038     CharUnits SlotSize = BaseAddr.getAlignment();
6039     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
6040         (IsHFA || !isAggregateTypeForABI(Ty)) &&
6041         TySize < SlotSize) {
6042       CharUnits Offset = SlotSize - TySize;
6043       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
6044     }
6045 
6046     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
6047   }
6048 
6049   CGF.EmitBranch(ContBlock);
6050 
6051   //=======================================
6052   // Argument was on the stack
6053   //=======================================
6054   CGF.EmitBlock(OnStackBlock);
6055 
6056   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
6057   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
6058 
6059   // Again, stack arguments may need realignment. In this case both integer and
6060   // floating-point ones might be affected.
6061   if (!IsIndirect && TyAlign.getQuantity() > 8) {
6062     int Align = TyAlign.getQuantity();
6063 
6064     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
6065 
6066     OnStackPtr = CGF.Builder.CreateAdd(
6067         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
6068         "align_stack");
6069     OnStackPtr = CGF.Builder.CreateAnd(
6070         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
6071         "align_stack");
6072 
6073     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
6074   }
6075   Address OnStackAddr(OnStackPtr,
6076                       std::max(CharUnits::fromQuantity(8), TyAlign));
6077 
6078   // All stack slots are multiples of 8 bytes.
6079   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
6080   CharUnits StackSize;
6081   if (IsIndirect)
6082     StackSize = StackSlotSize;
6083   else
6084     StackSize = TySize.alignTo(StackSlotSize);
6085 
6086   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
6087   llvm::Value *NewStack = CGF.Builder.CreateInBoundsGEP(
6088       CGF.Int8Ty, OnStackPtr, StackSizeC, "new_stack");
6089 
6090   // Write the new value of __stack for the next call to va_arg
6091   CGF.Builder.CreateStore(NewStack, stack_p);
6092 
6093   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
6094       TySize < StackSlotSize) {
6095     CharUnits Offset = StackSlotSize - TySize;
6096     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
6097   }
6098 
6099   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
6100 
6101   CGF.EmitBranch(ContBlock);
6102 
6103   //=======================================
6104   // Tidy up
6105   //=======================================
6106   CGF.EmitBlock(ContBlock);
6107 
6108   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6109                                  OnStackAddr, OnStackBlock, "vaargs.addr");
6110 
6111   if (IsIndirect)
6112     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
6113                    TyAlign);
6114 
6115   return ResAddr;
6116 }
6117 
6118 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
6119                                         CodeGenFunction &CGF) const {
6120   // The backend's lowering doesn't support va_arg for aggregates or
6121   // illegal vector types.  Lower VAArg here for these cases and use
6122   // the LLVM va_arg instruction for everything else.
6123   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
6124     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
6125 
6126   uint64_t PointerSize = getTarget().getPointerWidth(0) / 8;
6127   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6128 
6129   // Empty records are ignored for parameter passing purposes.
6130   if (isEmptyRecord(getContext(), Ty, true)) {
6131     Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
6132     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6133     return Addr;
6134   }
6135 
6136   // The size of the actual thing passed, which might end up just
6137   // being a pointer for indirect types.
6138   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6139 
6140   // Arguments bigger than 16 bytes which aren't homogeneous
6141   // aggregates should be passed indirectly.
6142   bool IsIndirect = false;
6143   if (TyInfo.Width.getQuantity() > 16) {
6144     const Type *Base = nullptr;
6145     uint64_t Members = 0;
6146     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
6147   }
6148 
6149   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6150                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
6151 }
6152 
6153 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
6154                                     QualType Ty) const {
6155   bool IsIndirect = false;
6156 
6157   // Composites larger than 16 bytes are passed by reference.
6158   if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) > 128)
6159     IsIndirect = true;
6160 
6161   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6162                           CGF.getContext().getTypeInfoInChars(Ty),
6163                           CharUnits::fromQuantity(8),
6164                           /*allowHigherAlign*/ false);
6165 }
6166 
6167 //===----------------------------------------------------------------------===//
6168 // ARM ABI Implementation
6169 //===----------------------------------------------------------------------===//
6170 
6171 namespace {
6172 
6173 class ARMABIInfo : public SwiftABIInfo {
6174 public:
6175   enum ABIKind {
6176     APCS = 0,
6177     AAPCS = 1,
6178     AAPCS_VFP = 2,
6179     AAPCS16_VFP = 3,
6180   };
6181 
6182 private:
6183   ABIKind Kind;
6184   bool IsFloatABISoftFP;
6185 
6186 public:
6187   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
6188       : SwiftABIInfo(CGT), Kind(_Kind) {
6189     setCCs();
6190     IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
6191         CGT.getCodeGenOpts().FloatABI == ""; // default
6192   }
6193 
6194   bool isEABI() const {
6195     switch (getTarget().getTriple().getEnvironment()) {
6196     case llvm::Triple::Android:
6197     case llvm::Triple::EABI:
6198     case llvm::Triple::EABIHF:
6199     case llvm::Triple::GNUEABI:
6200     case llvm::Triple::GNUEABIHF:
6201     case llvm::Triple::MuslEABI:
6202     case llvm::Triple::MuslEABIHF:
6203       return true;
6204     default:
6205       return false;
6206     }
6207   }
6208 
6209   bool isEABIHF() const {
6210     switch (getTarget().getTriple().getEnvironment()) {
6211     case llvm::Triple::EABIHF:
6212     case llvm::Triple::GNUEABIHF:
6213     case llvm::Triple::MuslEABIHF:
6214       return true;
6215     default:
6216       return false;
6217     }
6218   }
6219 
6220   ABIKind getABIKind() const { return Kind; }
6221 
6222   bool allowBFloatArgsAndRet() const override {
6223     return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
6224   }
6225 
6226 private:
6227   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
6228                                 unsigned functionCallConv) const;
6229   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
6230                                   unsigned functionCallConv) const;
6231   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
6232                                           uint64_t Members) const;
6233   ABIArgInfo coerceIllegalVector(QualType Ty) const;
6234   bool isIllegalVectorType(QualType Ty) const;
6235   bool containsAnyFP16Vectors(QualType Ty) const;
6236 
6237   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
6238   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
6239                                          uint64_t Members) const override;
6240 
6241   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
6242 
6243   void computeInfo(CGFunctionInfo &FI) const override;
6244 
6245   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6246                     QualType Ty) const override;
6247 
6248   llvm::CallingConv::ID getLLVMDefaultCC() const;
6249   llvm::CallingConv::ID getABIDefaultCC() const;
6250   void setCCs();
6251 
6252   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
6253                                     bool asReturnValue) const override {
6254     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6255   }
6256   bool isSwiftErrorInRegister() const override {
6257     return true;
6258   }
6259   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
6260                                  unsigned elts) const override;
6261 };
6262 
6263 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
6264 public:
6265   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6266       : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {}
6267 
6268   const ARMABIInfo &getABIInfo() const {
6269     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
6270   }
6271 
6272   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6273     return 13;
6274   }
6275 
6276   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
6277     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
6278   }
6279 
6280   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6281                                llvm::Value *Address) const override {
6282     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6283 
6284     // 0-15 are the 16 integer registers.
6285     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
6286     return false;
6287   }
6288 
6289   unsigned getSizeOfUnwindException() const override {
6290     if (getABIInfo().isEABI()) return 88;
6291     return TargetCodeGenInfo::getSizeOfUnwindException();
6292   }
6293 
6294   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6295                            CodeGen::CodeGenModule &CGM) const override {
6296     if (GV->isDeclaration())
6297       return;
6298     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6299     if (!FD)
6300       return;
6301 
6302     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
6303     if (!Attr)
6304       return;
6305 
6306     const char *Kind;
6307     switch (Attr->getInterrupt()) {
6308     case ARMInterruptAttr::Generic: Kind = ""; break;
6309     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
6310     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
6311     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
6312     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
6313     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
6314     }
6315 
6316     llvm::Function *Fn = cast<llvm::Function>(GV);
6317 
6318     Fn->addFnAttr("interrupt", Kind);
6319 
6320     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
6321     if (ABI == ARMABIInfo::APCS)
6322       return;
6323 
6324     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
6325     // however this is not necessarily true on taking any interrupt. Instruct
6326     // the backend to perform a realignment as part of the function prologue.
6327     llvm::AttrBuilder B;
6328     B.addStackAlignmentAttr(8);
6329     Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
6330   }
6331 };
6332 
6333 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
6334 public:
6335   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6336       : ARMTargetCodeGenInfo(CGT, K) {}
6337 
6338   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6339                            CodeGen::CodeGenModule &CGM) const override;
6340 
6341   void getDependentLibraryOption(llvm::StringRef Lib,
6342                                  llvm::SmallString<24> &Opt) const override {
6343     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
6344   }
6345 
6346   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
6347                                llvm::SmallString<32> &Opt) const override {
6348     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
6349   }
6350 };
6351 
6352 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
6353     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
6354   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
6355   if (GV->isDeclaration())
6356     return;
6357   addStackProbeTargetAttributes(D, GV, CGM);
6358 }
6359 }
6360 
6361 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
6362   if (!::classifyReturnType(getCXXABI(), FI, *this))
6363     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
6364                                             FI.getCallingConvention());
6365 
6366   for (auto &I : FI.arguments())
6367     I.info = classifyArgumentType(I.type, FI.isVariadic(),
6368                                   FI.getCallingConvention());
6369 
6370 
6371   // Always honor user-specified calling convention.
6372   if (FI.getCallingConvention() != llvm::CallingConv::C)
6373     return;
6374 
6375   llvm::CallingConv::ID cc = getRuntimeCC();
6376   if (cc != llvm::CallingConv::C)
6377     FI.setEffectiveCallingConvention(cc);
6378 }
6379 
6380 /// Return the default calling convention that LLVM will use.
6381 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
6382   // The default calling convention that LLVM will infer.
6383   if (isEABIHF() || getTarget().getTriple().isWatchABI())
6384     return llvm::CallingConv::ARM_AAPCS_VFP;
6385   else if (isEABI())
6386     return llvm::CallingConv::ARM_AAPCS;
6387   else
6388     return llvm::CallingConv::ARM_APCS;
6389 }
6390 
6391 /// Return the calling convention that our ABI would like us to use
6392 /// as the C calling convention.
6393 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
6394   switch (getABIKind()) {
6395   case APCS: return llvm::CallingConv::ARM_APCS;
6396   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
6397   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6398   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6399   }
6400   llvm_unreachable("bad ABI kind");
6401 }
6402 
6403 void ARMABIInfo::setCCs() {
6404   assert(getRuntimeCC() == llvm::CallingConv::C);
6405 
6406   // Don't muddy up the IR with a ton of explicit annotations if
6407   // they'd just match what LLVM will infer from the triple.
6408   llvm::CallingConv::ID abiCC = getABIDefaultCC();
6409   if (abiCC != getLLVMDefaultCC())
6410     RuntimeCC = abiCC;
6411 }
6412 
6413 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
6414   uint64_t Size = getContext().getTypeSize(Ty);
6415   if (Size <= 32) {
6416     llvm::Type *ResType =
6417         llvm::Type::getInt32Ty(getVMContext());
6418     return ABIArgInfo::getDirect(ResType);
6419   }
6420   if (Size == 64 || Size == 128) {
6421     auto *ResType = llvm::FixedVectorType::get(
6422         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6423     return ABIArgInfo::getDirect(ResType);
6424   }
6425   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6426 }
6427 
6428 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
6429                                                     const Type *Base,
6430                                                     uint64_t Members) const {
6431   assert(Base && "Base class should be set for homogeneous aggregate");
6432   // Base can be a floating-point or a vector.
6433   if (const VectorType *VT = Base->getAs<VectorType>()) {
6434     // FP16 vectors should be converted to integer vectors
6435     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
6436       uint64_t Size = getContext().getTypeSize(VT);
6437       auto *NewVecTy = llvm::FixedVectorType::get(
6438           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6439       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
6440       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6441     }
6442   }
6443   unsigned Align = 0;
6444   if (getABIKind() == ARMABIInfo::AAPCS ||
6445       getABIKind() == ARMABIInfo::AAPCS_VFP) {
6446     // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
6447     // default otherwise.
6448     Align = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6449     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
6450     Align = (Align > BaseAlign && Align >= 8) ? 8 : 0;
6451   }
6452   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align);
6453 }
6454 
6455 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
6456                                             unsigned functionCallConv) const {
6457   // 6.1.2.1 The following argument types are VFP CPRCs:
6458   //   A single-precision floating-point type (including promoted
6459   //   half-precision types); A double-precision floating-point type;
6460   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
6461   //   with a Base Type of a single- or double-precision floating-point type,
6462   //   64-bit containerized vectors or 128-bit containerized vectors with one
6463   //   to four Elements.
6464   // Variadic functions should always marshal to the base standard.
6465   bool IsAAPCS_VFP =
6466       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
6467 
6468   Ty = useFirstFieldIfTransparentUnion(Ty);
6469 
6470   // Handle illegal vector types here.
6471   if (isIllegalVectorType(Ty))
6472     return coerceIllegalVector(Ty);
6473 
6474   if (!isAggregateTypeForABI(Ty)) {
6475     // Treat an enum type as its underlying type.
6476     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
6477       Ty = EnumTy->getDecl()->getIntegerType();
6478     }
6479 
6480     if (const auto *EIT = Ty->getAs<ExtIntType>())
6481       if (EIT->getNumBits() > 64)
6482         return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6483 
6484     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
6485                                               : ABIArgInfo::getDirect());
6486   }
6487 
6488   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6489     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6490   }
6491 
6492   // Ignore empty records.
6493   if (isEmptyRecord(getContext(), Ty, true))
6494     return ABIArgInfo::getIgnore();
6495 
6496   if (IsAAPCS_VFP) {
6497     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
6498     // into VFP registers.
6499     const Type *Base = nullptr;
6500     uint64_t Members = 0;
6501     if (isHomogeneousAggregate(Ty, Base, Members))
6502       return classifyHomogeneousAggregate(Ty, Base, Members);
6503   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6504     // WatchOS does have homogeneous aggregates. Note that we intentionally use
6505     // this convention even for a variadic function: the backend will use GPRs
6506     // if needed.
6507     const Type *Base = nullptr;
6508     uint64_t Members = 0;
6509     if (isHomogeneousAggregate(Ty, Base, Members)) {
6510       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
6511       llvm::Type *Ty =
6512         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
6513       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6514     }
6515   }
6516 
6517   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6518       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
6519     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
6520     // bigger than 128-bits, they get placed in space allocated by the caller,
6521     // and a pointer is passed.
6522     return ABIArgInfo::getIndirect(
6523         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
6524   }
6525 
6526   // Support byval for ARM.
6527   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
6528   // most 8-byte. We realign the indirect argument if type alignment is bigger
6529   // than ABI alignment.
6530   uint64_t ABIAlign = 4;
6531   uint64_t TyAlign;
6532   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6533       getABIKind() == ARMABIInfo::AAPCS) {
6534     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6535     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
6536   } else {
6537     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
6538   }
6539   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
6540     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
6541     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
6542                                    /*ByVal=*/true,
6543                                    /*Realign=*/TyAlign > ABIAlign);
6544   }
6545 
6546   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
6547   // same size and alignment.
6548   if (getTarget().isRenderScriptTarget()) {
6549     return coerceToIntArray(Ty, getContext(), getVMContext());
6550   }
6551 
6552   // Otherwise, pass by coercing to a structure of the appropriate size.
6553   llvm::Type* ElemTy;
6554   unsigned SizeRegs;
6555   // FIXME: Try to match the types of the arguments more accurately where
6556   // we can.
6557   if (TyAlign <= 4) {
6558     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6559     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6560   } else {
6561     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6562     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6563   }
6564 
6565   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6566 }
6567 
6568 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6569                               llvm::LLVMContext &VMContext) {
6570   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6571   // is called integer-like if its size is less than or equal to one word, and
6572   // the offset of each of its addressable sub-fields is zero.
6573 
6574   uint64_t Size = Context.getTypeSize(Ty);
6575 
6576   // Check that the type fits in a word.
6577   if (Size > 32)
6578     return false;
6579 
6580   // FIXME: Handle vector types!
6581   if (Ty->isVectorType())
6582     return false;
6583 
6584   // Float types are never treated as "integer like".
6585   if (Ty->isRealFloatingType())
6586     return false;
6587 
6588   // If this is a builtin or pointer type then it is ok.
6589   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6590     return true;
6591 
6592   // Small complex integer types are "integer like".
6593   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6594     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6595 
6596   // Single element and zero sized arrays should be allowed, by the definition
6597   // above, but they are not.
6598 
6599   // Otherwise, it must be a record type.
6600   const RecordType *RT = Ty->getAs<RecordType>();
6601   if (!RT) return false;
6602 
6603   // Ignore records with flexible arrays.
6604   const RecordDecl *RD = RT->getDecl();
6605   if (RD->hasFlexibleArrayMember())
6606     return false;
6607 
6608   // Check that all sub-fields are at offset 0, and are themselves "integer
6609   // like".
6610   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6611 
6612   bool HadField = false;
6613   unsigned idx = 0;
6614   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6615        i != e; ++i, ++idx) {
6616     const FieldDecl *FD = *i;
6617 
6618     // Bit-fields are not addressable, we only need to verify they are "integer
6619     // like". We still have to disallow a subsequent non-bitfield, for example:
6620     //   struct { int : 0; int x }
6621     // is non-integer like according to gcc.
6622     if (FD->isBitField()) {
6623       if (!RD->isUnion())
6624         HadField = true;
6625 
6626       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6627         return false;
6628 
6629       continue;
6630     }
6631 
6632     // Check if this field is at offset 0.
6633     if (Layout.getFieldOffset(idx) != 0)
6634       return false;
6635 
6636     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6637       return false;
6638 
6639     // Only allow at most one field in a structure. This doesn't match the
6640     // wording above, but follows gcc in situations with a field following an
6641     // empty structure.
6642     if (!RD->isUnion()) {
6643       if (HadField)
6644         return false;
6645 
6646       HadField = true;
6647     }
6648   }
6649 
6650   return true;
6651 }
6652 
6653 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6654                                           unsigned functionCallConv) const {
6655 
6656   // Variadic functions should always marshal to the base standard.
6657   bool IsAAPCS_VFP =
6658       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6659 
6660   if (RetTy->isVoidType())
6661     return ABIArgInfo::getIgnore();
6662 
6663   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6664     // Large vector types should be returned via memory.
6665     if (getContext().getTypeSize(RetTy) > 128)
6666       return getNaturalAlignIndirect(RetTy);
6667     // TODO: FP16/BF16 vectors should be converted to integer vectors
6668     // This check is similar  to isIllegalVectorType - refactor?
6669     if ((!getTarget().hasLegalHalfType() &&
6670         (VT->getElementType()->isFloat16Type() ||
6671          VT->getElementType()->isHalfType())) ||
6672         (IsFloatABISoftFP &&
6673          VT->getElementType()->isBFloat16Type()))
6674       return coerceIllegalVector(RetTy);
6675   }
6676 
6677   if (!isAggregateTypeForABI(RetTy)) {
6678     // Treat an enum type as its underlying type.
6679     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6680       RetTy = EnumTy->getDecl()->getIntegerType();
6681 
6682     if (const auto *EIT = RetTy->getAs<ExtIntType>())
6683       if (EIT->getNumBits() > 64)
6684         return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
6685 
6686     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6687                                                 : ABIArgInfo::getDirect();
6688   }
6689 
6690   // Are we following APCS?
6691   if (getABIKind() == APCS) {
6692     if (isEmptyRecord(getContext(), RetTy, false))
6693       return ABIArgInfo::getIgnore();
6694 
6695     // Complex types are all returned as packed integers.
6696     //
6697     // FIXME: Consider using 2 x vector types if the back end handles them
6698     // correctly.
6699     if (RetTy->isAnyComplexType())
6700       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6701           getVMContext(), getContext().getTypeSize(RetTy)));
6702 
6703     // Integer like structures are returned in r0.
6704     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6705       // Return in the smallest viable integer type.
6706       uint64_t Size = getContext().getTypeSize(RetTy);
6707       if (Size <= 8)
6708         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6709       if (Size <= 16)
6710         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6711       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6712     }
6713 
6714     // Otherwise return in memory.
6715     return getNaturalAlignIndirect(RetTy);
6716   }
6717 
6718   // Otherwise this is an AAPCS variant.
6719 
6720   if (isEmptyRecord(getContext(), RetTy, true))
6721     return ABIArgInfo::getIgnore();
6722 
6723   // Check for homogeneous aggregates with AAPCS-VFP.
6724   if (IsAAPCS_VFP) {
6725     const Type *Base = nullptr;
6726     uint64_t Members = 0;
6727     if (isHomogeneousAggregate(RetTy, Base, Members))
6728       return classifyHomogeneousAggregate(RetTy, Base, Members);
6729   }
6730 
6731   // Aggregates <= 4 bytes are returned in r0; other aggregates
6732   // are returned indirectly.
6733   uint64_t Size = getContext().getTypeSize(RetTy);
6734   if (Size <= 32) {
6735     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6736     // same size and alignment.
6737     if (getTarget().isRenderScriptTarget()) {
6738       return coerceToIntArray(RetTy, getContext(), getVMContext());
6739     }
6740     if (getDataLayout().isBigEndian())
6741       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6742       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6743 
6744     // Return in the smallest viable integer type.
6745     if (Size <= 8)
6746       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6747     if (Size <= 16)
6748       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6749     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6750   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6751     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6752     llvm::Type *CoerceTy =
6753         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6754     return ABIArgInfo::getDirect(CoerceTy);
6755   }
6756 
6757   return getNaturalAlignIndirect(RetTy);
6758 }
6759 
6760 /// isIllegalVector - check whether Ty is an illegal vector type.
6761 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6762   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6763     // On targets that don't support half, fp16 or bfloat, they are expanded
6764     // into float, and we don't want the ABI to depend on whether or not they
6765     // are supported in hardware. Thus return false to coerce vectors of these
6766     // types into integer vectors.
6767     // We do not depend on hasLegalHalfType for bfloat as it is a
6768     // separate IR type.
6769     if ((!getTarget().hasLegalHalfType() &&
6770         (VT->getElementType()->isFloat16Type() ||
6771          VT->getElementType()->isHalfType())) ||
6772         (IsFloatABISoftFP &&
6773          VT->getElementType()->isBFloat16Type()))
6774       return true;
6775     if (isAndroid()) {
6776       // Android shipped using Clang 3.1, which supported a slightly different
6777       // vector ABI. The primary differences were that 3-element vector types
6778       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6779       // accepts that legacy behavior for Android only.
6780       // Check whether VT is legal.
6781       unsigned NumElements = VT->getNumElements();
6782       // NumElements should be power of 2 or equal to 3.
6783       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6784         return true;
6785     } else {
6786       // Check whether VT is legal.
6787       unsigned NumElements = VT->getNumElements();
6788       uint64_t Size = getContext().getTypeSize(VT);
6789       // NumElements should be power of 2.
6790       if (!llvm::isPowerOf2_32(NumElements))
6791         return true;
6792       // Size should be greater than 32 bits.
6793       return Size <= 32;
6794     }
6795   }
6796   return false;
6797 }
6798 
6799 /// Return true if a type contains any 16-bit floating point vectors
6800 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6801   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6802     uint64_t NElements = AT->getSize().getZExtValue();
6803     if (NElements == 0)
6804       return false;
6805     return containsAnyFP16Vectors(AT->getElementType());
6806   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6807     const RecordDecl *RD = RT->getDecl();
6808 
6809     // If this is a C++ record, check the bases first.
6810     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6811       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6812             return containsAnyFP16Vectors(B.getType());
6813           }))
6814         return true;
6815 
6816     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6817           return FD && containsAnyFP16Vectors(FD->getType());
6818         }))
6819       return true;
6820 
6821     return false;
6822   } else {
6823     if (const VectorType *VT = Ty->getAs<VectorType>())
6824       return (VT->getElementType()->isFloat16Type() ||
6825               VT->getElementType()->isBFloat16Type() ||
6826               VT->getElementType()->isHalfType());
6827     return false;
6828   }
6829 }
6830 
6831 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6832                                            llvm::Type *eltTy,
6833                                            unsigned numElts) const {
6834   if (!llvm::isPowerOf2_32(numElts))
6835     return false;
6836   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6837   if (size > 64)
6838     return false;
6839   if (vectorSize.getQuantity() != 8 &&
6840       (vectorSize.getQuantity() != 16 || numElts == 1))
6841     return false;
6842   return true;
6843 }
6844 
6845 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6846   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6847   // double, or 64-bit or 128-bit vectors.
6848   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6849     if (BT->getKind() == BuiltinType::Float ||
6850         BT->getKind() == BuiltinType::Double ||
6851         BT->getKind() == BuiltinType::LongDouble)
6852       return true;
6853   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6854     unsigned VecSize = getContext().getTypeSize(VT);
6855     if (VecSize == 64 || VecSize == 128)
6856       return true;
6857   }
6858   return false;
6859 }
6860 
6861 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6862                                                    uint64_t Members) const {
6863   return Members <= 4;
6864 }
6865 
6866 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
6867                                         bool acceptHalf) const {
6868   // Give precedence to user-specified calling conventions.
6869   if (callConvention != llvm::CallingConv::C)
6870     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
6871   else
6872     return (getABIKind() == AAPCS_VFP) ||
6873            (acceptHalf && (getABIKind() == AAPCS16_VFP));
6874 }
6875 
6876 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6877                               QualType Ty) const {
6878   CharUnits SlotSize = CharUnits::fromQuantity(4);
6879 
6880   // Empty records are ignored for parameter passing purposes.
6881   if (isEmptyRecord(getContext(), Ty, true)) {
6882     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6883     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6884     return Addr;
6885   }
6886 
6887   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6888   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
6889 
6890   // Use indirect if size of the illegal vector is bigger than 16 bytes.
6891   bool IsIndirect = false;
6892   const Type *Base = nullptr;
6893   uint64_t Members = 0;
6894   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6895     IsIndirect = true;
6896 
6897   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6898   // allocated by the caller.
6899   } else if (TySize > CharUnits::fromQuantity(16) &&
6900              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6901              !isHomogeneousAggregate(Ty, Base, Members)) {
6902     IsIndirect = true;
6903 
6904   // Otherwise, bound the type's ABI alignment.
6905   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6906   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
6907   // Our callers should be prepared to handle an under-aligned address.
6908   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6909              getABIKind() == ARMABIInfo::AAPCS) {
6910     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6911     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
6912   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6913     // ARMv7k allows type alignment up to 16 bytes.
6914     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6915     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
6916   } else {
6917     TyAlignForABI = CharUnits::fromQuantity(4);
6918   }
6919 
6920   TypeInfoChars TyInfo(TySize, TyAlignForABI, false);
6921   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6922                           SlotSize, /*AllowHigherAlign*/ true);
6923 }
6924 
6925 //===----------------------------------------------------------------------===//
6926 // NVPTX ABI Implementation
6927 //===----------------------------------------------------------------------===//
6928 
6929 namespace {
6930 
6931 class NVPTXTargetCodeGenInfo;
6932 
6933 class NVPTXABIInfo : public ABIInfo {
6934   NVPTXTargetCodeGenInfo &CGInfo;
6935 
6936 public:
6937   NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
6938       : ABIInfo(CGT), CGInfo(Info) {}
6939 
6940   ABIArgInfo classifyReturnType(QualType RetTy) const;
6941   ABIArgInfo classifyArgumentType(QualType Ty) const;
6942 
6943   void computeInfo(CGFunctionInfo &FI) const override;
6944   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6945                     QualType Ty) const override;
6946   bool isUnsupportedType(QualType T) const;
6947   ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
6948 };
6949 
6950 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
6951 public:
6952   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6953       : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
6954 
6955   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6956                            CodeGen::CodeGenModule &M) const override;
6957   bool shouldEmitStaticExternCAliases() const override;
6958 
6959   llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
6960     // On the device side, surface reference is represented as an object handle
6961     // in 64-bit integer.
6962     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
6963   }
6964 
6965   llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
6966     // On the device side, texture reference is represented as an object handle
6967     // in 64-bit integer.
6968     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
6969   }
6970 
6971   bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6972                                               LValue Src) const override {
6973     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
6974     return true;
6975   }
6976 
6977   bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6978                                               LValue Src) const override {
6979     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
6980     return true;
6981   }
6982 
6983 private:
6984   // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
6985   // resulting MDNode to the nvvm.annotations MDNode.
6986   static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
6987                               int Operand);
6988 
6989   static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6990                                            LValue Src) {
6991     llvm::Value *Handle = nullptr;
6992     llvm::Constant *C =
6993         llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
6994     // Lookup `addrspacecast` through the constant pointer if any.
6995     if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
6996       C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
6997     if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
6998       // Load the handle from the specific global variable using
6999       // `nvvm.texsurf.handle.internal` intrinsic.
7000       Handle = CGF.EmitRuntimeCall(
7001           CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
7002                                {GV->getType()}),
7003           {GV}, "texsurf_handle");
7004     } else
7005       Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
7006     CGF.EmitStoreOfScalar(Handle, Dst);
7007   }
7008 };
7009 
7010 /// Checks if the type is unsupported directly by the current target.
7011 bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
7012   ASTContext &Context = getContext();
7013   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
7014     return true;
7015   if (!Context.getTargetInfo().hasFloat128Type() &&
7016       (T->isFloat128Type() ||
7017        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
7018     return true;
7019   if (const auto *EIT = T->getAs<ExtIntType>())
7020     return EIT->getNumBits() >
7021            (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
7022   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
7023       Context.getTypeSize(T) > 64U)
7024     return true;
7025   if (const auto *AT = T->getAsArrayTypeUnsafe())
7026     return isUnsupportedType(AT->getElementType());
7027   const auto *RT = T->getAs<RecordType>();
7028   if (!RT)
7029     return false;
7030   const RecordDecl *RD = RT->getDecl();
7031 
7032   // If this is a C++ record, check the bases first.
7033   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7034     for (const CXXBaseSpecifier &I : CXXRD->bases())
7035       if (isUnsupportedType(I.getType()))
7036         return true;
7037 
7038   for (const FieldDecl *I : RD->fields())
7039     if (isUnsupportedType(I->getType()))
7040       return true;
7041   return false;
7042 }
7043 
7044 /// Coerce the given type into an array with maximum allowed size of elements.
7045 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
7046                                                    unsigned MaxSize) const {
7047   // Alignment and Size are measured in bits.
7048   const uint64_t Size = getContext().getTypeSize(Ty);
7049   const uint64_t Alignment = getContext().getTypeAlign(Ty);
7050   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
7051   llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
7052   const uint64_t NumElements = (Size + Div - 1) / Div;
7053   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
7054 }
7055 
7056 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
7057   if (RetTy->isVoidType())
7058     return ABIArgInfo::getIgnore();
7059 
7060   if (getContext().getLangOpts().OpenMP &&
7061       getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
7062     return coerceToIntArrayWithLimit(RetTy, 64);
7063 
7064   // note: this is different from default ABI
7065   if (!RetTy->isScalarType())
7066     return ABIArgInfo::getDirect();
7067 
7068   // Treat an enum type as its underlying type.
7069   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7070     RetTy = EnumTy->getDecl()->getIntegerType();
7071 
7072   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7073                                                : ABIArgInfo::getDirect());
7074 }
7075 
7076 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
7077   // Treat an enum type as its underlying type.
7078   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7079     Ty = EnumTy->getDecl()->getIntegerType();
7080 
7081   // Return aggregates type as indirect by value
7082   if (isAggregateTypeForABI(Ty)) {
7083     // Under CUDA device compilation, tex/surf builtin types are replaced with
7084     // object types and passed directly.
7085     if (getContext().getLangOpts().CUDAIsDevice) {
7086       if (Ty->isCUDADeviceBuiltinSurfaceType())
7087         return ABIArgInfo::getDirect(
7088             CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
7089       if (Ty->isCUDADeviceBuiltinTextureType())
7090         return ABIArgInfo::getDirect(
7091             CGInfo.getCUDADeviceBuiltinTextureDeviceType());
7092     }
7093     return getNaturalAlignIndirect(Ty, /* byval */ true);
7094   }
7095 
7096   if (const auto *EIT = Ty->getAs<ExtIntType>()) {
7097     if ((EIT->getNumBits() > 128) ||
7098         (!getContext().getTargetInfo().hasInt128Type() &&
7099          EIT->getNumBits() > 64))
7100       return getNaturalAlignIndirect(Ty, /* byval */ true);
7101   }
7102 
7103   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
7104                                             : ABIArgInfo::getDirect());
7105 }
7106 
7107 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
7108   if (!getCXXABI().classifyReturnType(FI))
7109     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7110   for (auto &I : FI.arguments())
7111     I.info = classifyArgumentType(I.type);
7112 
7113   // Always honor user-specified calling convention.
7114   if (FI.getCallingConvention() != llvm::CallingConv::C)
7115     return;
7116 
7117   FI.setEffectiveCallingConvention(getRuntimeCC());
7118 }
7119 
7120 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7121                                 QualType Ty) const {
7122   llvm_unreachable("NVPTX does not support varargs");
7123 }
7124 
7125 void NVPTXTargetCodeGenInfo::setTargetAttributes(
7126     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7127   if (GV->isDeclaration())
7128     return;
7129   const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
7130   if (VD) {
7131     if (M.getLangOpts().CUDA) {
7132       if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
7133         addNVVMMetadata(GV, "surface", 1);
7134       else if (VD->getType()->isCUDADeviceBuiltinTextureType())
7135         addNVVMMetadata(GV, "texture", 1);
7136       return;
7137     }
7138   }
7139 
7140   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7141   if (!FD) return;
7142 
7143   llvm::Function *F = cast<llvm::Function>(GV);
7144 
7145   // Perform special handling in OpenCL mode
7146   if (M.getLangOpts().OpenCL) {
7147     // Use OpenCL function attributes to check for kernel functions
7148     // By default, all functions are device functions
7149     if (FD->hasAttr<OpenCLKernelAttr>()) {
7150       // OpenCL __kernel functions get kernel metadata
7151       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7152       addNVVMMetadata(F, "kernel", 1);
7153       // And kernel functions are not subject to inlining
7154       F->addFnAttr(llvm::Attribute::NoInline);
7155     }
7156   }
7157 
7158   // Perform special handling in CUDA mode.
7159   if (M.getLangOpts().CUDA) {
7160     // CUDA __global__ functions get a kernel metadata entry.  Since
7161     // __global__ functions cannot be called from the device, we do not
7162     // need to set the noinline attribute.
7163     if (FD->hasAttr<CUDAGlobalAttr>()) {
7164       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7165       addNVVMMetadata(F, "kernel", 1);
7166     }
7167     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
7168       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
7169       llvm::APSInt MaxThreads(32);
7170       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
7171       if (MaxThreads > 0)
7172         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
7173 
7174       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
7175       // not specified in __launch_bounds__ or if the user specified a 0 value,
7176       // we don't have to add a PTX directive.
7177       if (Attr->getMinBlocks()) {
7178         llvm::APSInt MinBlocks(32);
7179         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
7180         if (MinBlocks > 0)
7181           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
7182           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
7183       }
7184     }
7185   }
7186 }
7187 
7188 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
7189                                              StringRef Name, int Operand) {
7190   llvm::Module *M = GV->getParent();
7191   llvm::LLVMContext &Ctx = M->getContext();
7192 
7193   // Get "nvvm.annotations" metadata node
7194   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
7195 
7196   llvm::Metadata *MDVals[] = {
7197       llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
7198       llvm::ConstantAsMetadata::get(
7199           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
7200   // Append metadata to nvvm.annotations
7201   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7202 }
7203 
7204 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
7205   return false;
7206 }
7207 }
7208 
7209 //===----------------------------------------------------------------------===//
7210 // SystemZ ABI Implementation
7211 //===----------------------------------------------------------------------===//
7212 
7213 namespace {
7214 
7215 class SystemZABIInfo : public SwiftABIInfo {
7216   bool HasVector;
7217   bool IsSoftFloatABI;
7218 
7219 public:
7220   SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
7221     : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
7222 
7223   bool isPromotableIntegerTypeForABI(QualType Ty) const;
7224   bool isCompoundType(QualType Ty) const;
7225   bool isVectorArgumentType(QualType Ty) const;
7226   bool isFPArgumentType(QualType Ty) const;
7227   QualType GetSingleElementType(QualType Ty) const;
7228 
7229   ABIArgInfo classifyReturnType(QualType RetTy) const;
7230   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
7231 
7232   void computeInfo(CGFunctionInfo &FI) const override {
7233     if (!getCXXABI().classifyReturnType(FI))
7234       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7235     for (auto &I : FI.arguments())
7236       I.info = classifyArgumentType(I.type);
7237   }
7238 
7239   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7240                     QualType Ty) const override;
7241 
7242   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
7243                                     bool asReturnValue) const override {
7244     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
7245   }
7246   bool isSwiftErrorInRegister() const override {
7247     return false;
7248   }
7249 };
7250 
7251 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
7252 public:
7253   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
7254       : TargetCodeGenInfo(
7255             std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {}
7256 
7257   llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID,
7258                           CGBuilderTy &Builder,
7259                           CodeGenModule &CGM) const override {
7260     assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
7261     // Only use TDC in constrained FP mode.
7262     if (!Builder.getIsFPConstrained())
7263       return nullptr;
7264 
7265     llvm::Type *Ty = V->getType();
7266     if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) {
7267       llvm::Module &M = CGM.getModule();
7268       auto &Ctx = M.getContext();
7269       llvm::Function *TDCFunc =
7270           llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty);
7271       unsigned TDCBits = 0;
7272       switch (BuiltinID) {
7273       case Builtin::BI__builtin_isnan:
7274         TDCBits = 0xf;
7275         break;
7276       case Builtin::BIfinite:
7277       case Builtin::BI__finite:
7278       case Builtin::BIfinitef:
7279       case Builtin::BI__finitef:
7280       case Builtin::BIfinitel:
7281       case Builtin::BI__finitel:
7282       case Builtin::BI__builtin_isfinite:
7283         TDCBits = 0xfc0;
7284         break;
7285       case Builtin::BI__builtin_isinf:
7286         TDCBits = 0x30;
7287         break;
7288       default:
7289         break;
7290       }
7291       if (TDCBits)
7292         return Builder.CreateCall(
7293             TDCFunc,
7294             {V, llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), TDCBits)});
7295     }
7296     return nullptr;
7297   }
7298 };
7299 }
7300 
7301 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
7302   // Treat an enum type as its underlying type.
7303   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7304     Ty = EnumTy->getDecl()->getIntegerType();
7305 
7306   // Promotable integer types are required to be promoted by the ABI.
7307   if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
7308     return true;
7309 
7310   if (const auto *EIT = Ty->getAs<ExtIntType>())
7311     if (EIT->getNumBits() < 64)
7312       return true;
7313 
7314   // 32-bit values must also be promoted.
7315   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7316     switch (BT->getKind()) {
7317     case BuiltinType::Int:
7318     case BuiltinType::UInt:
7319       return true;
7320     default:
7321       return false;
7322     }
7323   return false;
7324 }
7325 
7326 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
7327   return (Ty->isAnyComplexType() ||
7328           Ty->isVectorType() ||
7329           isAggregateTypeForABI(Ty));
7330 }
7331 
7332 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
7333   return (HasVector &&
7334           Ty->isVectorType() &&
7335           getContext().getTypeSize(Ty) <= 128);
7336 }
7337 
7338 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
7339   if (IsSoftFloatABI)
7340     return false;
7341 
7342   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7343     switch (BT->getKind()) {
7344     case BuiltinType::Float:
7345     case BuiltinType::Double:
7346       return true;
7347     default:
7348       return false;
7349     }
7350 
7351   return false;
7352 }
7353 
7354 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
7355   const RecordType *RT = Ty->getAs<RecordType>();
7356 
7357   if (RT && RT->isStructureOrClassType()) {
7358     const RecordDecl *RD = RT->getDecl();
7359     QualType Found;
7360 
7361     // If this is a C++ record, check the bases first.
7362     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7363       for (const auto &I : CXXRD->bases()) {
7364         QualType Base = I.getType();
7365 
7366         // Empty bases don't affect things either way.
7367         if (isEmptyRecord(getContext(), Base, true))
7368           continue;
7369 
7370         if (!Found.isNull())
7371           return Ty;
7372         Found = GetSingleElementType(Base);
7373       }
7374 
7375     // Check the fields.
7376     for (const auto *FD : RD->fields()) {
7377       // For compatibility with GCC, ignore empty bitfields in C++ mode.
7378       // Unlike isSingleElementStruct(), empty structure and array fields
7379       // do count.  So do anonymous bitfields that aren't zero-sized.
7380       if (getContext().getLangOpts().CPlusPlus &&
7381           FD->isZeroLengthBitField(getContext()))
7382         continue;
7383       // Like isSingleElementStruct(), ignore C++20 empty data members.
7384       if (FD->hasAttr<NoUniqueAddressAttr>() &&
7385           isEmptyRecord(getContext(), FD->getType(), true))
7386         continue;
7387 
7388       // Unlike isSingleElementStruct(), arrays do not count.
7389       // Nested structures still do though.
7390       if (!Found.isNull())
7391         return Ty;
7392       Found = GetSingleElementType(FD->getType());
7393     }
7394 
7395     // Unlike isSingleElementStruct(), trailing padding is allowed.
7396     // An 8-byte aligned struct s { float f; } is passed as a double.
7397     if (!Found.isNull())
7398       return Found;
7399   }
7400 
7401   return Ty;
7402 }
7403 
7404 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7405                                   QualType Ty) const {
7406   // Assume that va_list type is correct; should be pointer to LLVM type:
7407   // struct {
7408   //   i64 __gpr;
7409   //   i64 __fpr;
7410   //   i8 *__overflow_arg_area;
7411   //   i8 *__reg_save_area;
7412   // };
7413 
7414   // Every non-vector argument occupies 8 bytes and is passed by preference
7415   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
7416   // always passed on the stack.
7417   Ty = getContext().getCanonicalType(Ty);
7418   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7419   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
7420   llvm::Type *DirectTy = ArgTy;
7421   ABIArgInfo AI = classifyArgumentType(Ty);
7422   bool IsIndirect = AI.isIndirect();
7423   bool InFPRs = false;
7424   bool IsVector = false;
7425   CharUnits UnpaddedSize;
7426   CharUnits DirectAlign;
7427   if (IsIndirect) {
7428     DirectTy = llvm::PointerType::getUnqual(DirectTy);
7429     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
7430   } else {
7431     if (AI.getCoerceToType())
7432       ArgTy = AI.getCoerceToType();
7433     InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
7434     IsVector = ArgTy->isVectorTy();
7435     UnpaddedSize = TyInfo.Width;
7436     DirectAlign = TyInfo.Align;
7437   }
7438   CharUnits PaddedSize = CharUnits::fromQuantity(8);
7439   if (IsVector && UnpaddedSize > PaddedSize)
7440     PaddedSize = CharUnits::fromQuantity(16);
7441   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
7442 
7443   CharUnits Padding = (PaddedSize - UnpaddedSize);
7444 
7445   llvm::Type *IndexTy = CGF.Int64Ty;
7446   llvm::Value *PaddedSizeV =
7447     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
7448 
7449   if (IsVector) {
7450     // Work out the address of a vector argument on the stack.
7451     // Vector arguments are always passed in the high bits of a
7452     // single (8 byte) or double (16 byte) stack slot.
7453     Address OverflowArgAreaPtr =
7454         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7455     Address OverflowArgArea =
7456       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7457               TyInfo.Align);
7458     Address MemAddr =
7459       CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
7460 
7461     // Update overflow_arg_area_ptr pointer
7462     llvm::Value *NewOverflowArgArea =
7463       CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
7464                             "overflow_arg_area");
7465     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7466 
7467     return MemAddr;
7468   }
7469 
7470   assert(PaddedSize.getQuantity() == 8);
7471 
7472   unsigned MaxRegs, RegCountField, RegSaveIndex;
7473   CharUnits RegPadding;
7474   if (InFPRs) {
7475     MaxRegs = 4; // Maximum of 4 FPR arguments
7476     RegCountField = 1; // __fpr
7477     RegSaveIndex = 16; // save offset for f0
7478     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
7479   } else {
7480     MaxRegs = 5; // Maximum of 5 GPR arguments
7481     RegCountField = 0; // __gpr
7482     RegSaveIndex = 2; // save offset for r2
7483     RegPadding = Padding; // values are passed in the low bits of a GPR
7484   }
7485 
7486   Address RegCountPtr =
7487       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
7488   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
7489   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
7490   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
7491                                                  "fits_in_regs");
7492 
7493   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
7494   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
7495   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
7496   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
7497 
7498   // Emit code to load the value if it was passed in registers.
7499   CGF.EmitBlock(InRegBlock);
7500 
7501   // Work out the address of an argument register.
7502   llvm::Value *ScaledRegCount =
7503     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
7504   llvm::Value *RegBase =
7505     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
7506                                       + RegPadding.getQuantity());
7507   llvm::Value *RegOffset =
7508     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
7509   Address RegSaveAreaPtr =
7510       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
7511   llvm::Value *RegSaveArea =
7512     CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
7513   Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
7514                                            "raw_reg_addr"),
7515                      PaddedSize);
7516   Address RegAddr =
7517     CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
7518 
7519   // Update the register count
7520   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
7521   llvm::Value *NewRegCount =
7522     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
7523   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
7524   CGF.EmitBranch(ContBlock);
7525 
7526   // Emit code to load the value if it was passed in memory.
7527   CGF.EmitBlock(InMemBlock);
7528 
7529   // Work out the address of a stack argument.
7530   Address OverflowArgAreaPtr =
7531       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7532   Address OverflowArgArea =
7533     Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7534             PaddedSize);
7535   Address RawMemAddr =
7536     CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
7537   Address MemAddr =
7538     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
7539 
7540   // Update overflow_arg_area_ptr pointer
7541   llvm::Value *NewOverflowArgArea =
7542     CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
7543                           "overflow_arg_area");
7544   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7545   CGF.EmitBranch(ContBlock);
7546 
7547   // Return the appropriate result.
7548   CGF.EmitBlock(ContBlock);
7549   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
7550                                  MemAddr, InMemBlock, "va_arg.addr");
7551 
7552   if (IsIndirect)
7553     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
7554                       TyInfo.Align);
7555 
7556   return ResAddr;
7557 }
7558 
7559 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
7560   if (RetTy->isVoidType())
7561     return ABIArgInfo::getIgnore();
7562   if (isVectorArgumentType(RetTy))
7563     return ABIArgInfo::getDirect();
7564   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
7565     return getNaturalAlignIndirect(RetTy);
7566   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7567                                                : ABIArgInfo::getDirect());
7568 }
7569 
7570 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
7571   // Handle the generic C++ ABI.
7572   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7573     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7574 
7575   // Integers and enums are extended to full register width.
7576   if (isPromotableIntegerTypeForABI(Ty))
7577     return ABIArgInfo::getExtend(Ty);
7578 
7579   // Handle vector types and vector-like structure types.  Note that
7580   // as opposed to float-like structure types, we do not allow any
7581   // padding for vector-like structures, so verify the sizes match.
7582   uint64_t Size = getContext().getTypeSize(Ty);
7583   QualType SingleElementTy = GetSingleElementType(Ty);
7584   if (isVectorArgumentType(SingleElementTy) &&
7585       getContext().getTypeSize(SingleElementTy) == Size)
7586     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
7587 
7588   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
7589   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
7590     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7591 
7592   // Handle small structures.
7593   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7594     // Structures with flexible arrays have variable length, so really
7595     // fail the size test above.
7596     const RecordDecl *RD = RT->getDecl();
7597     if (RD->hasFlexibleArrayMember())
7598       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7599 
7600     // The structure is passed as an unextended integer, a float, or a double.
7601     llvm::Type *PassTy;
7602     if (isFPArgumentType(SingleElementTy)) {
7603       assert(Size == 32 || Size == 64);
7604       if (Size == 32)
7605         PassTy = llvm::Type::getFloatTy(getVMContext());
7606       else
7607         PassTy = llvm::Type::getDoubleTy(getVMContext());
7608     } else
7609       PassTy = llvm::IntegerType::get(getVMContext(), Size);
7610     return ABIArgInfo::getDirect(PassTy);
7611   }
7612 
7613   // Non-structure compounds are passed indirectly.
7614   if (isCompoundType(Ty))
7615     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7616 
7617   return ABIArgInfo::getDirect(nullptr);
7618 }
7619 
7620 //===----------------------------------------------------------------------===//
7621 // MSP430 ABI Implementation
7622 //===----------------------------------------------------------------------===//
7623 
7624 namespace {
7625 
7626 class MSP430ABIInfo : public DefaultABIInfo {
7627   static ABIArgInfo complexArgInfo() {
7628     ABIArgInfo Info = ABIArgInfo::getDirect();
7629     Info.setCanBeFlattened(false);
7630     return Info;
7631   }
7632 
7633 public:
7634   MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7635 
7636   ABIArgInfo classifyReturnType(QualType RetTy) const {
7637     if (RetTy->isAnyComplexType())
7638       return complexArgInfo();
7639 
7640     return DefaultABIInfo::classifyReturnType(RetTy);
7641   }
7642 
7643   ABIArgInfo classifyArgumentType(QualType RetTy) const {
7644     if (RetTy->isAnyComplexType())
7645       return complexArgInfo();
7646 
7647     return DefaultABIInfo::classifyArgumentType(RetTy);
7648   }
7649 
7650   // Just copy the original implementations because
7651   // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
7652   void computeInfo(CGFunctionInfo &FI) const override {
7653     if (!getCXXABI().classifyReturnType(FI))
7654       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7655     for (auto &I : FI.arguments())
7656       I.info = classifyArgumentType(I.type);
7657   }
7658 
7659   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7660                     QualType Ty) const override {
7661     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
7662   }
7663 };
7664 
7665 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7666 public:
7667   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7668       : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
7669   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7670                            CodeGen::CodeGenModule &M) const override;
7671 };
7672 
7673 }
7674 
7675 void MSP430TargetCodeGenInfo::setTargetAttributes(
7676     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7677   if (GV->isDeclaration())
7678     return;
7679   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7680     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7681     if (!InterruptAttr)
7682       return;
7683 
7684     // Handle 'interrupt' attribute:
7685     llvm::Function *F = cast<llvm::Function>(GV);
7686 
7687     // Step 1: Set ISR calling convention.
7688     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7689 
7690     // Step 2: Add attributes goodness.
7691     F->addFnAttr(llvm::Attribute::NoInline);
7692     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7693   }
7694 }
7695 
7696 //===----------------------------------------------------------------------===//
7697 // MIPS ABI Implementation.  This works for both little-endian and
7698 // big-endian variants.
7699 //===----------------------------------------------------------------------===//
7700 
7701 namespace {
7702 class MipsABIInfo : public ABIInfo {
7703   bool IsO32;
7704   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7705   void CoerceToIntArgs(uint64_t TySize,
7706                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7707   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7708   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7709   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7710 public:
7711   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7712     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7713     StackAlignInBytes(IsO32 ? 8 : 16) {}
7714 
7715   ABIArgInfo classifyReturnType(QualType RetTy) const;
7716   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7717   void computeInfo(CGFunctionInfo &FI) const override;
7718   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7719                     QualType Ty) const override;
7720   ABIArgInfo extendType(QualType Ty) const;
7721 };
7722 
7723 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7724   unsigned SizeOfUnwindException;
7725 public:
7726   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7727       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7728         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7729 
7730   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7731     return 29;
7732   }
7733 
7734   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7735                            CodeGen::CodeGenModule &CGM) const override {
7736     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7737     if (!FD) return;
7738     llvm::Function *Fn = cast<llvm::Function>(GV);
7739 
7740     if (FD->hasAttr<MipsLongCallAttr>())
7741       Fn->addFnAttr("long-call");
7742     else if (FD->hasAttr<MipsShortCallAttr>())
7743       Fn->addFnAttr("short-call");
7744 
7745     // Other attributes do not have a meaning for declarations.
7746     if (GV->isDeclaration())
7747       return;
7748 
7749     if (FD->hasAttr<Mips16Attr>()) {
7750       Fn->addFnAttr("mips16");
7751     }
7752     else if (FD->hasAttr<NoMips16Attr>()) {
7753       Fn->addFnAttr("nomips16");
7754     }
7755 
7756     if (FD->hasAttr<MicroMipsAttr>())
7757       Fn->addFnAttr("micromips");
7758     else if (FD->hasAttr<NoMicroMipsAttr>())
7759       Fn->addFnAttr("nomicromips");
7760 
7761     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7762     if (!Attr)
7763       return;
7764 
7765     const char *Kind;
7766     switch (Attr->getInterrupt()) {
7767     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7768     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7769     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7770     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7771     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7772     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7773     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7774     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7775     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7776     }
7777 
7778     Fn->addFnAttr("interrupt", Kind);
7779 
7780   }
7781 
7782   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7783                                llvm::Value *Address) const override;
7784 
7785   unsigned getSizeOfUnwindException() const override {
7786     return SizeOfUnwindException;
7787   }
7788 };
7789 }
7790 
7791 void MipsABIInfo::CoerceToIntArgs(
7792     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7793   llvm::IntegerType *IntTy =
7794     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7795 
7796   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7797   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7798     ArgList.push_back(IntTy);
7799 
7800   // If necessary, add one more integer type to ArgList.
7801   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7802 
7803   if (R)
7804     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7805 }
7806 
7807 // In N32/64, an aligned double precision floating point field is passed in
7808 // a register.
7809 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7810   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7811 
7812   if (IsO32) {
7813     CoerceToIntArgs(TySize, ArgList);
7814     return llvm::StructType::get(getVMContext(), ArgList);
7815   }
7816 
7817   if (Ty->isComplexType())
7818     return CGT.ConvertType(Ty);
7819 
7820   const RecordType *RT = Ty->getAs<RecordType>();
7821 
7822   // Unions/vectors are passed in integer registers.
7823   if (!RT || !RT->isStructureOrClassType()) {
7824     CoerceToIntArgs(TySize, ArgList);
7825     return llvm::StructType::get(getVMContext(), ArgList);
7826   }
7827 
7828   const RecordDecl *RD = RT->getDecl();
7829   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7830   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7831 
7832   uint64_t LastOffset = 0;
7833   unsigned idx = 0;
7834   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7835 
7836   // Iterate over fields in the struct/class and check if there are any aligned
7837   // double fields.
7838   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
7839        i != e; ++i, ++idx) {
7840     const QualType Ty = i->getType();
7841     const BuiltinType *BT = Ty->getAs<BuiltinType>();
7842 
7843     if (!BT || BT->getKind() != BuiltinType::Double)
7844       continue;
7845 
7846     uint64_t Offset = Layout.getFieldOffset(idx);
7847     if (Offset % 64) // Ignore doubles that are not aligned.
7848       continue;
7849 
7850     // Add ((Offset - LastOffset) / 64) args of type i64.
7851     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
7852       ArgList.push_back(I64);
7853 
7854     // Add double type.
7855     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
7856     LastOffset = Offset + 64;
7857   }
7858 
7859   CoerceToIntArgs(TySize - LastOffset, IntArgList);
7860   ArgList.append(IntArgList.begin(), IntArgList.end());
7861 
7862   return llvm::StructType::get(getVMContext(), ArgList);
7863 }
7864 
7865 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
7866                                         uint64_t Offset) const {
7867   if (OrigOffset + MinABIStackAlignInBytes > Offset)
7868     return nullptr;
7869 
7870   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
7871 }
7872 
7873 ABIArgInfo
7874 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
7875   Ty = useFirstFieldIfTransparentUnion(Ty);
7876 
7877   uint64_t OrigOffset = Offset;
7878   uint64_t TySize = getContext().getTypeSize(Ty);
7879   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
7880 
7881   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
7882                    (uint64_t)StackAlignInBytes);
7883   unsigned CurrOffset = llvm::alignTo(Offset, Align);
7884   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
7885 
7886   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
7887     // Ignore empty aggregates.
7888     if (TySize == 0)
7889       return ABIArgInfo::getIgnore();
7890 
7891     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
7892       Offset = OrigOffset + MinABIStackAlignInBytes;
7893       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7894     }
7895 
7896     // If we have reached here, aggregates are passed directly by coercing to
7897     // another structure type. Padding is inserted if the offset of the
7898     // aggregate is unaligned.
7899     ABIArgInfo ArgInfo =
7900         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
7901                               getPaddingType(OrigOffset, CurrOffset));
7902     ArgInfo.setInReg(true);
7903     return ArgInfo;
7904   }
7905 
7906   // Treat an enum type as its underlying type.
7907   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7908     Ty = EnumTy->getDecl()->getIntegerType();
7909 
7910   // Make sure we pass indirectly things that are too large.
7911   if (const auto *EIT = Ty->getAs<ExtIntType>())
7912     if (EIT->getNumBits() > 128 ||
7913         (EIT->getNumBits() > 64 &&
7914          !getContext().getTargetInfo().hasInt128Type()))
7915       return getNaturalAlignIndirect(Ty);
7916 
7917   // All integral types are promoted to the GPR width.
7918   if (Ty->isIntegralOrEnumerationType())
7919     return extendType(Ty);
7920 
7921   return ABIArgInfo::getDirect(
7922       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
7923 }
7924 
7925 llvm::Type*
7926 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
7927   const RecordType *RT = RetTy->getAs<RecordType>();
7928   SmallVector<llvm::Type*, 8> RTList;
7929 
7930   if (RT && RT->isStructureOrClassType()) {
7931     const RecordDecl *RD = RT->getDecl();
7932     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7933     unsigned FieldCnt = Layout.getFieldCount();
7934 
7935     // N32/64 returns struct/classes in floating point registers if the
7936     // following conditions are met:
7937     // 1. The size of the struct/class is no larger than 128-bit.
7938     // 2. The struct/class has one or two fields all of which are floating
7939     //    point types.
7940     // 3. The offset of the first field is zero (this follows what gcc does).
7941     //
7942     // Any other composite results are returned in integer registers.
7943     //
7944     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
7945       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
7946       for (; b != e; ++b) {
7947         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
7948 
7949         if (!BT || !BT->isFloatingPoint())
7950           break;
7951 
7952         RTList.push_back(CGT.ConvertType(b->getType()));
7953       }
7954 
7955       if (b == e)
7956         return llvm::StructType::get(getVMContext(), RTList,
7957                                      RD->hasAttr<PackedAttr>());
7958 
7959       RTList.clear();
7960     }
7961   }
7962 
7963   CoerceToIntArgs(Size, RTList);
7964   return llvm::StructType::get(getVMContext(), RTList);
7965 }
7966 
7967 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
7968   uint64_t Size = getContext().getTypeSize(RetTy);
7969 
7970   if (RetTy->isVoidType())
7971     return ABIArgInfo::getIgnore();
7972 
7973   // O32 doesn't treat zero-sized structs differently from other structs.
7974   // However, N32/N64 ignores zero sized return values.
7975   if (!IsO32 && Size == 0)
7976     return ABIArgInfo::getIgnore();
7977 
7978   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
7979     if (Size <= 128) {
7980       if (RetTy->isAnyComplexType())
7981         return ABIArgInfo::getDirect();
7982 
7983       // O32 returns integer vectors in registers and N32/N64 returns all small
7984       // aggregates in registers.
7985       if (!IsO32 ||
7986           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
7987         ABIArgInfo ArgInfo =
7988             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
7989         ArgInfo.setInReg(true);
7990         return ArgInfo;
7991       }
7992     }
7993 
7994     return getNaturalAlignIndirect(RetTy);
7995   }
7996 
7997   // Treat an enum type as its underlying type.
7998   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7999     RetTy = EnumTy->getDecl()->getIntegerType();
8000 
8001   // Make sure we pass indirectly things that are too large.
8002   if (const auto *EIT = RetTy->getAs<ExtIntType>())
8003     if (EIT->getNumBits() > 128 ||
8004         (EIT->getNumBits() > 64 &&
8005          !getContext().getTargetInfo().hasInt128Type()))
8006       return getNaturalAlignIndirect(RetTy);
8007 
8008   if (isPromotableIntegerTypeForABI(RetTy))
8009     return ABIArgInfo::getExtend(RetTy);
8010 
8011   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
8012       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
8013     return ABIArgInfo::getSignExtend(RetTy);
8014 
8015   return ABIArgInfo::getDirect();
8016 }
8017 
8018 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
8019   ABIArgInfo &RetInfo = FI.getReturnInfo();
8020   if (!getCXXABI().classifyReturnType(FI))
8021     RetInfo = classifyReturnType(FI.getReturnType());
8022 
8023   // Check if a pointer to an aggregate is passed as a hidden argument.
8024   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
8025 
8026   for (auto &I : FI.arguments())
8027     I.info = classifyArgumentType(I.type, Offset);
8028 }
8029 
8030 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8031                                QualType OrigTy) const {
8032   QualType Ty = OrigTy;
8033 
8034   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
8035   // Pointers are also promoted in the same way but this only matters for N32.
8036   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
8037   unsigned PtrWidth = getTarget().getPointerWidth(0);
8038   bool DidPromote = false;
8039   if ((Ty->isIntegerType() &&
8040           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
8041       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
8042     DidPromote = true;
8043     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
8044                                             Ty->isSignedIntegerType());
8045   }
8046 
8047   auto TyInfo = getContext().getTypeInfoInChars(Ty);
8048 
8049   // The alignment of things in the argument area is never larger than
8050   // StackAlignInBytes.
8051   TyInfo.Align =
8052     std::min(TyInfo.Align, CharUnits::fromQuantity(StackAlignInBytes));
8053 
8054   // MinABIStackAlignInBytes is the size of argument slots on the stack.
8055   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
8056 
8057   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
8058                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
8059 
8060 
8061   // If there was a promotion, "unpromote" into a temporary.
8062   // TODO: can we just use a pointer into a subset of the original slot?
8063   if (DidPromote) {
8064     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
8065     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
8066 
8067     // Truncate down to the right width.
8068     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
8069                                                  : CGF.IntPtrTy);
8070     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
8071     if (OrigTy->isPointerType())
8072       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
8073 
8074     CGF.Builder.CreateStore(V, Temp);
8075     Addr = Temp;
8076   }
8077 
8078   return Addr;
8079 }
8080 
8081 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
8082   int TySize = getContext().getTypeSize(Ty);
8083 
8084   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
8085   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
8086     return ABIArgInfo::getSignExtend(Ty);
8087 
8088   return ABIArgInfo::getExtend(Ty);
8089 }
8090 
8091 bool
8092 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8093                                                llvm::Value *Address) const {
8094   // This information comes from gcc's implementation, which seems to
8095   // as canonical as it gets.
8096 
8097   // Everything on MIPS is 4 bytes.  Double-precision FP registers
8098   // are aliased to pairs of single-precision FP registers.
8099   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8100 
8101   // 0-31 are the general purpose registers, $0 - $31.
8102   // 32-63 are the floating-point registers, $f0 - $f31.
8103   // 64 and 65 are the multiply/divide registers, $hi and $lo.
8104   // 66 is the (notional, I think) register for signal-handler return.
8105   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
8106 
8107   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
8108   // They are one bit wide and ignored here.
8109 
8110   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
8111   // (coprocessor 1 is the FP unit)
8112   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
8113   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
8114   // 176-181 are the DSP accumulator registers.
8115   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
8116   return false;
8117 }
8118 
8119 //===----------------------------------------------------------------------===//
8120 // M68k ABI Implementation
8121 //===----------------------------------------------------------------------===//
8122 
8123 namespace {
8124 
8125 class M68kTargetCodeGenInfo : public TargetCodeGenInfo {
8126 public:
8127   M68kTargetCodeGenInfo(CodeGenTypes &CGT)
8128       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
8129   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8130                            CodeGen::CodeGenModule &M) const override;
8131 };
8132 
8133 } // namespace
8134 
8135 void M68kTargetCodeGenInfo::setTargetAttributes(
8136     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8137   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
8138     if (const auto *attr = FD->getAttr<M68kInterruptAttr>()) {
8139       // Handle 'interrupt' attribute:
8140       llvm::Function *F = cast<llvm::Function>(GV);
8141 
8142       // Step 1: Set ISR calling convention.
8143       F->setCallingConv(llvm::CallingConv::M68k_INTR);
8144 
8145       // Step 2: Add attributes goodness.
8146       F->addFnAttr(llvm::Attribute::NoInline);
8147 
8148       // Step 3: Emit ISR vector alias.
8149       unsigned Num = attr->getNumber() / 2;
8150       llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
8151                                 "__isr_" + Twine(Num), F);
8152     }
8153   }
8154 }
8155 
8156 //===----------------------------------------------------------------------===//
8157 // AVR ABI Implementation. Documented at
8158 // https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
8159 // https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
8160 //===----------------------------------------------------------------------===//
8161 
8162 namespace {
8163 class AVRABIInfo : public DefaultABIInfo {
8164 public:
8165   AVRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8166 
8167   ABIArgInfo classifyReturnType(QualType Ty) const {
8168     // A return struct with size less than or equal to 8 bytes is returned
8169     // directly via registers R18-R25.
8170     if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) <= 64)
8171       return ABIArgInfo::getDirect();
8172     else
8173       return DefaultABIInfo::classifyReturnType(Ty);
8174   }
8175 
8176   // Just copy the original implementation of DefaultABIInfo::computeInfo(),
8177   // since DefaultABIInfo::classify{Return,Argument}Type() are not virtual.
8178   void computeInfo(CGFunctionInfo &FI) const override {
8179     if (!getCXXABI().classifyReturnType(FI))
8180       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8181     for (auto &I : FI.arguments())
8182       I.info = classifyArgumentType(I.type);
8183   }
8184 };
8185 
8186 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
8187 public:
8188   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
8189       : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT)) {}
8190 
8191   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8192                                   const VarDecl *D) const override {
8193     // Check if a global/static variable is defined within address space 1
8194     // but not constant.
8195     LangAS AS = D->getType().getAddressSpace();
8196     if (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 1 &&
8197         !D->getType().isConstQualified())
8198       CGM.getDiags().Report(D->getLocation(),
8199                             diag::err_verify_nonconst_addrspace)
8200           << "__flash";
8201     return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
8202   }
8203 
8204   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8205                            CodeGen::CodeGenModule &CGM) const override {
8206     if (GV->isDeclaration())
8207       return;
8208     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
8209     if (!FD) return;
8210     auto *Fn = cast<llvm::Function>(GV);
8211 
8212     if (FD->getAttr<AVRInterruptAttr>())
8213       Fn->addFnAttr("interrupt");
8214 
8215     if (FD->getAttr<AVRSignalAttr>())
8216       Fn->addFnAttr("signal");
8217   }
8218 };
8219 }
8220 
8221 //===----------------------------------------------------------------------===//
8222 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
8223 // Currently subclassed only to implement custom OpenCL C function attribute
8224 // handling.
8225 //===----------------------------------------------------------------------===//
8226 
8227 namespace {
8228 
8229 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
8230 public:
8231   TCETargetCodeGenInfo(CodeGenTypes &CGT)
8232     : DefaultTargetCodeGenInfo(CGT) {}
8233 
8234   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8235                            CodeGen::CodeGenModule &M) const override;
8236 };
8237 
8238 void TCETargetCodeGenInfo::setTargetAttributes(
8239     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8240   if (GV->isDeclaration())
8241     return;
8242   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8243   if (!FD) return;
8244 
8245   llvm::Function *F = cast<llvm::Function>(GV);
8246 
8247   if (M.getLangOpts().OpenCL) {
8248     if (FD->hasAttr<OpenCLKernelAttr>()) {
8249       // OpenCL C Kernel functions are not subject to inlining
8250       F->addFnAttr(llvm::Attribute::NoInline);
8251       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
8252       if (Attr) {
8253         // Convert the reqd_work_group_size() attributes to metadata.
8254         llvm::LLVMContext &Context = F->getContext();
8255         llvm::NamedMDNode *OpenCLMetadata =
8256             M.getModule().getOrInsertNamedMetadata(
8257                 "opencl.kernel_wg_size_info");
8258 
8259         SmallVector<llvm::Metadata *, 5> Operands;
8260         Operands.push_back(llvm::ConstantAsMetadata::get(F));
8261 
8262         Operands.push_back(
8263             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8264                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8265         Operands.push_back(
8266             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8267                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8268         Operands.push_back(
8269             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8270                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8271 
8272         // Add a boolean constant operand for "required" (true) or "hint"
8273         // (false) for implementing the work_group_size_hint attr later.
8274         // Currently always true as the hint is not yet implemented.
8275         Operands.push_back(
8276             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8277         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8278       }
8279     }
8280   }
8281 }
8282 
8283 }
8284 
8285 //===----------------------------------------------------------------------===//
8286 // Hexagon ABI Implementation
8287 //===----------------------------------------------------------------------===//
8288 
8289 namespace {
8290 
8291 class HexagonABIInfo : public DefaultABIInfo {
8292 public:
8293   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8294 
8295 private:
8296   ABIArgInfo classifyReturnType(QualType RetTy) const;
8297   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8298   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8299 
8300   void computeInfo(CGFunctionInfo &FI) const override;
8301 
8302   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8303                     QualType Ty) const override;
8304   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8305                               QualType Ty) const;
8306   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8307                               QualType Ty) const;
8308   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8309                                    QualType Ty) const;
8310 };
8311 
8312 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8313 public:
8314   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8315       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8316 
8317   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8318     return 29;
8319   }
8320 
8321   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8322                            CodeGen::CodeGenModule &GCM) const override {
8323     if (GV->isDeclaration())
8324       return;
8325     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8326     if (!FD)
8327       return;
8328   }
8329 };
8330 
8331 } // namespace
8332 
8333 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8334   unsigned RegsLeft = 6;
8335   if (!getCXXABI().classifyReturnType(FI))
8336     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8337   for (auto &I : FI.arguments())
8338     I.info = classifyArgumentType(I.type, &RegsLeft);
8339 }
8340 
8341 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8342   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8343                        " through registers");
8344 
8345   if (*RegsLeft == 0)
8346     return false;
8347 
8348   if (Size <= 32) {
8349     (*RegsLeft)--;
8350     return true;
8351   }
8352 
8353   if (2 <= (*RegsLeft & (~1U))) {
8354     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8355     return true;
8356   }
8357 
8358   // Next available register was r5 but candidate was greater than 32-bits so it
8359   // has to go on the stack. However we still consume r5
8360   if (*RegsLeft == 1)
8361     *RegsLeft = 0;
8362 
8363   return false;
8364 }
8365 
8366 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8367                                                 unsigned *RegsLeft) const {
8368   if (!isAggregateTypeForABI(Ty)) {
8369     // Treat an enum type as its underlying type.
8370     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8371       Ty = EnumTy->getDecl()->getIntegerType();
8372 
8373     uint64_t Size = getContext().getTypeSize(Ty);
8374     if (Size <= 64)
8375       HexagonAdjustRegsLeft(Size, RegsLeft);
8376 
8377     if (Size > 64 && Ty->isExtIntType())
8378       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8379 
8380     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8381                                              : ABIArgInfo::getDirect();
8382   }
8383 
8384   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8385     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8386 
8387   // Ignore empty records.
8388   if (isEmptyRecord(getContext(), Ty, true))
8389     return ABIArgInfo::getIgnore();
8390 
8391   uint64_t Size = getContext().getTypeSize(Ty);
8392   unsigned Align = getContext().getTypeAlign(Ty);
8393 
8394   if (Size > 64)
8395     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8396 
8397   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8398     Align = Size <= 32 ? 32 : 64;
8399   if (Size <= Align) {
8400     // Pass in the smallest viable integer type.
8401     if (!llvm::isPowerOf2_64(Size))
8402       Size = llvm::NextPowerOf2(Size);
8403     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8404   }
8405   return DefaultABIInfo::classifyArgumentType(Ty);
8406 }
8407 
8408 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8409   if (RetTy->isVoidType())
8410     return ABIArgInfo::getIgnore();
8411 
8412   const TargetInfo &T = CGT.getTarget();
8413   uint64_t Size = getContext().getTypeSize(RetTy);
8414 
8415   if (RetTy->getAs<VectorType>()) {
8416     // HVX vectors are returned in vector registers or register pairs.
8417     if (T.hasFeature("hvx")) {
8418       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8419       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8420       if (Size == VecSize || Size == 2*VecSize)
8421         return ABIArgInfo::getDirectInReg();
8422     }
8423     // Large vector types should be returned via memory.
8424     if (Size > 64)
8425       return getNaturalAlignIndirect(RetTy);
8426   }
8427 
8428   if (!isAggregateTypeForABI(RetTy)) {
8429     // Treat an enum type as its underlying type.
8430     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8431       RetTy = EnumTy->getDecl()->getIntegerType();
8432 
8433     if (Size > 64 && RetTy->isExtIntType())
8434       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8435 
8436     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8437                                                 : ABIArgInfo::getDirect();
8438   }
8439 
8440   if (isEmptyRecord(getContext(), RetTy, true))
8441     return ABIArgInfo::getIgnore();
8442 
8443   // Aggregates <= 8 bytes are returned in registers, other aggregates
8444   // are returned indirectly.
8445   if (Size <= 64) {
8446     // Return in the smallest viable integer type.
8447     if (!llvm::isPowerOf2_64(Size))
8448       Size = llvm::NextPowerOf2(Size);
8449     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8450   }
8451   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8452 }
8453 
8454 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8455                                             Address VAListAddr,
8456                                             QualType Ty) const {
8457   // Load the overflow area pointer.
8458   Address __overflow_area_pointer_p =
8459       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8460   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8461       __overflow_area_pointer_p, "__overflow_area_pointer");
8462 
8463   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8464   if (Align > 4) {
8465     // Alignment should be a power of 2.
8466     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8467 
8468     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8469     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8470 
8471     // Add offset to the current pointer to access the argument.
8472     __overflow_area_pointer =
8473         CGF.Builder.CreateGEP(__overflow_area_pointer, Offset);
8474     llvm::Value *AsInt =
8475         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8476 
8477     // Create a mask which should be "AND"ed
8478     // with (overflow_arg_area + align - 1)
8479     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8480     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8481         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8482         "__overflow_area_pointer.align");
8483   }
8484 
8485   // Get the type of the argument from memory and bitcast
8486   // overflow area pointer to the argument type.
8487   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8488   Address AddrTyped = CGF.Builder.CreateBitCast(
8489       Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)),
8490       llvm::PointerType::getUnqual(PTy));
8491 
8492   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8493   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8494 
8495   __overflow_area_pointer = CGF.Builder.CreateGEP(
8496       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8497       "__overflow_area_pointer.next");
8498   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8499 
8500   return AddrTyped;
8501 }
8502 
8503 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8504                                             Address VAListAddr,
8505                                             QualType Ty) const {
8506   // FIXME: Need to handle alignment
8507   llvm::Type *BP = CGF.Int8PtrTy;
8508   llvm::Type *BPP = CGF.Int8PtrPtrTy;
8509   CGBuilderTy &Builder = CGF.Builder;
8510   Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
8511   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8512   // Handle address alignment for type alignment > 32 bits
8513   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8514   if (TyAlign > 4) {
8515     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8516     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8517     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8518     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8519     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8520   }
8521   llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
8522   Address AddrTyped = Builder.CreateBitCast(
8523       Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy);
8524 
8525   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8526   llvm::Value *NextAddr = Builder.CreateGEP(
8527       Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8528   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8529 
8530   return AddrTyped;
8531 }
8532 
8533 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8534                                                  Address VAListAddr,
8535                                                  QualType Ty) const {
8536   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8537 
8538   if (ArgSize > 8)
8539     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8540 
8541   // Here we have check if the argument is in register area or
8542   // in overflow area.
8543   // If the saved register area pointer + argsize rounded up to alignment >
8544   // saved register area end pointer, argument is in overflow area.
8545   unsigned RegsLeft = 6;
8546   Ty = CGF.getContext().getCanonicalType(Ty);
8547   (void)classifyArgumentType(Ty, &RegsLeft);
8548 
8549   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8550   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8551   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8552   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8553 
8554   // Get rounded size of the argument.GCC does not allow vararg of
8555   // size < 4 bytes. We follow the same logic here.
8556   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8557   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8558 
8559   // Argument may be in saved register area
8560   CGF.EmitBlock(MaybeRegBlock);
8561 
8562   // Load the current saved register area pointer.
8563   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8564       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8565   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8566       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8567 
8568   // Load the saved register area end pointer.
8569   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8570       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8571   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8572       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8573 
8574   // If the size of argument is > 4 bytes, check if the stack
8575   // location is aligned to 8 bytes
8576   if (ArgAlign > 4) {
8577 
8578     llvm::Value *__current_saved_reg_area_pointer_int =
8579         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8580                                    CGF.Int32Ty);
8581 
8582     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8583         __current_saved_reg_area_pointer_int,
8584         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8585         "align_current_saved_reg_area_pointer");
8586 
8587     __current_saved_reg_area_pointer_int =
8588         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8589                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8590                               "align_current_saved_reg_area_pointer");
8591 
8592     __current_saved_reg_area_pointer =
8593         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8594                                    __current_saved_reg_area_pointer->getType(),
8595                                    "align_current_saved_reg_area_pointer");
8596   }
8597 
8598   llvm::Value *__new_saved_reg_area_pointer =
8599       CGF.Builder.CreateGEP(__current_saved_reg_area_pointer,
8600                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8601                             "__new_saved_reg_area_pointer");
8602 
8603   llvm::Value *UsingStack = 0;
8604   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8605                                          __saved_reg_area_end_pointer);
8606 
8607   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8608 
8609   // Argument in saved register area
8610   // Implement the block where argument is in register saved area
8611   CGF.EmitBlock(InRegBlock);
8612 
8613   llvm::Type *PTy = CGF.ConvertType(Ty);
8614   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8615       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8616 
8617   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8618                           __current_saved_reg_area_pointer_p);
8619 
8620   CGF.EmitBranch(ContBlock);
8621 
8622   // Argument in overflow area
8623   // Implement the block where the argument is in overflow area.
8624   CGF.EmitBlock(OnStackBlock);
8625 
8626   // Load the overflow area pointer
8627   Address __overflow_area_pointer_p =
8628       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8629   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8630       __overflow_area_pointer_p, "__overflow_area_pointer");
8631 
8632   // Align the overflow area pointer according to the alignment of the argument
8633   if (ArgAlign > 4) {
8634     llvm::Value *__overflow_area_pointer_int =
8635         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8636 
8637     __overflow_area_pointer_int =
8638         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8639                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8640                               "align_overflow_area_pointer");
8641 
8642     __overflow_area_pointer_int =
8643         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8644                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8645                               "align_overflow_area_pointer");
8646 
8647     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8648         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8649         "align_overflow_area_pointer");
8650   }
8651 
8652   // Get the pointer for next argument in overflow area and store it
8653   // to overflow area pointer.
8654   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8655       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8656       "__overflow_area_pointer.next");
8657 
8658   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8659                           __overflow_area_pointer_p);
8660 
8661   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8662                           __current_saved_reg_area_pointer_p);
8663 
8664   // Bitcast the overflow area pointer to the type of argument.
8665   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8666   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8667       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8668 
8669   CGF.EmitBranch(ContBlock);
8670 
8671   // Get the correct pointer to load the variable argument
8672   // Implement the ContBlock
8673   CGF.EmitBlock(ContBlock);
8674 
8675   llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
8676   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
8677   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
8678   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
8679 
8680   return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign));
8681 }
8682 
8683 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8684                                   QualType Ty) const {
8685 
8686   if (getTarget().getTriple().isMusl())
8687     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
8688 
8689   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
8690 }
8691 
8692 //===----------------------------------------------------------------------===//
8693 // Lanai ABI Implementation
8694 //===----------------------------------------------------------------------===//
8695 
8696 namespace {
8697 class LanaiABIInfo : public DefaultABIInfo {
8698 public:
8699   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8700 
8701   bool shouldUseInReg(QualType Ty, CCState &State) const;
8702 
8703   void computeInfo(CGFunctionInfo &FI) const override {
8704     CCState State(FI);
8705     // Lanai uses 4 registers to pass arguments unless the function has the
8706     // regparm attribute set.
8707     if (FI.getHasRegParm()) {
8708       State.FreeRegs = FI.getRegParm();
8709     } else {
8710       State.FreeRegs = 4;
8711     }
8712 
8713     if (!getCXXABI().classifyReturnType(FI))
8714       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8715     for (auto &I : FI.arguments())
8716       I.info = classifyArgumentType(I.type, State);
8717   }
8718 
8719   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
8720   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
8721 };
8722 } // end anonymous namespace
8723 
8724 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
8725   unsigned Size = getContext().getTypeSize(Ty);
8726   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
8727 
8728   if (SizeInRegs == 0)
8729     return false;
8730 
8731   if (SizeInRegs > State.FreeRegs) {
8732     State.FreeRegs = 0;
8733     return false;
8734   }
8735 
8736   State.FreeRegs -= SizeInRegs;
8737 
8738   return true;
8739 }
8740 
8741 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
8742                                            CCState &State) const {
8743   if (!ByVal) {
8744     if (State.FreeRegs) {
8745       --State.FreeRegs; // Non-byval indirects just use one pointer.
8746       return getNaturalAlignIndirectInReg(Ty);
8747     }
8748     return getNaturalAlignIndirect(Ty, false);
8749   }
8750 
8751   // Compute the byval alignment.
8752   const unsigned MinABIStackAlignInBytes = 4;
8753   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8754   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8755                                  /*Realign=*/TypeAlign >
8756                                      MinABIStackAlignInBytes);
8757 }
8758 
8759 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
8760                                               CCState &State) const {
8761   // Check with the C++ ABI first.
8762   const RecordType *RT = Ty->getAs<RecordType>();
8763   if (RT) {
8764     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8765     if (RAA == CGCXXABI::RAA_Indirect) {
8766       return getIndirectResult(Ty, /*ByVal=*/false, State);
8767     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
8768       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8769     }
8770   }
8771 
8772   if (isAggregateTypeForABI(Ty)) {
8773     // Structures with flexible arrays are always indirect.
8774     if (RT && RT->getDecl()->hasFlexibleArrayMember())
8775       return getIndirectResult(Ty, /*ByVal=*/true, State);
8776 
8777     // Ignore empty structs/unions.
8778     if (isEmptyRecord(getContext(), Ty, true))
8779       return ABIArgInfo::getIgnore();
8780 
8781     llvm::LLVMContext &LLVMContext = getVMContext();
8782     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
8783     if (SizeInRegs <= State.FreeRegs) {
8784       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
8785       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
8786       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8787       State.FreeRegs -= SizeInRegs;
8788       return ABIArgInfo::getDirectInReg(Result);
8789     } else {
8790       State.FreeRegs = 0;
8791     }
8792     return getIndirectResult(Ty, true, State);
8793   }
8794 
8795   // Treat an enum type as its underlying type.
8796   if (const auto *EnumTy = Ty->getAs<EnumType>())
8797     Ty = EnumTy->getDecl()->getIntegerType();
8798 
8799   bool InReg = shouldUseInReg(Ty, State);
8800 
8801   // Don't pass >64 bit integers in registers.
8802   if (const auto *EIT = Ty->getAs<ExtIntType>())
8803     if (EIT->getNumBits() > 64)
8804       return getIndirectResult(Ty, /*ByVal=*/true, State);
8805 
8806   if (isPromotableIntegerTypeForABI(Ty)) {
8807     if (InReg)
8808       return ABIArgInfo::getDirectInReg();
8809     return ABIArgInfo::getExtend(Ty);
8810   }
8811   if (InReg)
8812     return ABIArgInfo::getDirectInReg();
8813   return ABIArgInfo::getDirect();
8814 }
8815 
8816 namespace {
8817 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
8818 public:
8819   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8820       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
8821 };
8822 }
8823 
8824 //===----------------------------------------------------------------------===//
8825 // AMDGPU ABI Implementation
8826 //===----------------------------------------------------------------------===//
8827 
8828 namespace {
8829 
8830 class AMDGPUABIInfo final : public DefaultABIInfo {
8831 private:
8832   static const unsigned MaxNumRegsForArgsRet = 16;
8833 
8834   unsigned numRegsForType(QualType Ty) const;
8835 
8836   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
8837   bool isHomogeneousAggregateSmallEnough(const Type *Base,
8838                                          uint64_t Members) const override;
8839 
8840   // Coerce HIP scalar pointer arguments from generic pointers to global ones.
8841   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
8842                                        unsigned ToAS) const {
8843     // Single value types.
8844     if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS)
8845       return llvm::PointerType::get(
8846           cast<llvm::PointerType>(Ty)->getElementType(), ToAS);
8847     return Ty;
8848   }
8849 
8850 public:
8851   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
8852     DefaultABIInfo(CGT) {}
8853 
8854   ABIArgInfo classifyReturnType(QualType RetTy) const;
8855   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
8856   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
8857 
8858   void computeInfo(CGFunctionInfo &FI) const override;
8859   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8860                     QualType Ty) const override;
8861 };
8862 
8863 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
8864   return true;
8865 }
8866 
8867 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
8868   const Type *Base, uint64_t Members) const {
8869   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
8870 
8871   // Homogeneous Aggregates may occupy at most 16 registers.
8872   return Members * NumRegs <= MaxNumRegsForArgsRet;
8873 }
8874 
8875 /// Estimate number of registers the type will use when passed in registers.
8876 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
8877   unsigned NumRegs = 0;
8878 
8879   if (const VectorType *VT = Ty->getAs<VectorType>()) {
8880     // Compute from the number of elements. The reported size is based on the
8881     // in-memory size, which includes the padding 4th element for 3-vectors.
8882     QualType EltTy = VT->getElementType();
8883     unsigned EltSize = getContext().getTypeSize(EltTy);
8884 
8885     // 16-bit element vectors should be passed as packed.
8886     if (EltSize == 16)
8887       return (VT->getNumElements() + 1) / 2;
8888 
8889     unsigned EltNumRegs = (EltSize + 31) / 32;
8890     return EltNumRegs * VT->getNumElements();
8891   }
8892 
8893   if (const RecordType *RT = Ty->getAs<RecordType>()) {
8894     const RecordDecl *RD = RT->getDecl();
8895     assert(!RD->hasFlexibleArrayMember());
8896 
8897     for (const FieldDecl *Field : RD->fields()) {
8898       QualType FieldTy = Field->getType();
8899       NumRegs += numRegsForType(FieldTy);
8900     }
8901 
8902     return NumRegs;
8903   }
8904 
8905   return (getContext().getTypeSize(Ty) + 31) / 32;
8906 }
8907 
8908 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
8909   llvm::CallingConv::ID CC = FI.getCallingConvention();
8910 
8911   if (!getCXXABI().classifyReturnType(FI))
8912     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8913 
8914   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
8915   for (auto &Arg : FI.arguments()) {
8916     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
8917       Arg.info = classifyKernelArgumentType(Arg.type);
8918     } else {
8919       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
8920     }
8921   }
8922 }
8923 
8924 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8925                                  QualType Ty) const {
8926   llvm_unreachable("AMDGPU does not support varargs");
8927 }
8928 
8929 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
8930   if (isAggregateTypeForABI(RetTy)) {
8931     // Records with non-trivial destructors/copy-constructors should not be
8932     // returned by value.
8933     if (!getRecordArgABI(RetTy, getCXXABI())) {
8934       // Ignore empty structs/unions.
8935       if (isEmptyRecord(getContext(), RetTy, true))
8936         return ABIArgInfo::getIgnore();
8937 
8938       // Lower single-element structs to just return a regular value.
8939       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
8940         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
8941 
8942       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
8943         const RecordDecl *RD = RT->getDecl();
8944         if (RD->hasFlexibleArrayMember())
8945           return DefaultABIInfo::classifyReturnType(RetTy);
8946       }
8947 
8948       // Pack aggregates <= 4 bytes into single VGPR or pair.
8949       uint64_t Size = getContext().getTypeSize(RetTy);
8950       if (Size <= 16)
8951         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
8952 
8953       if (Size <= 32)
8954         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
8955 
8956       if (Size <= 64) {
8957         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
8958         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
8959       }
8960 
8961       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
8962         return ABIArgInfo::getDirect();
8963     }
8964   }
8965 
8966   // Otherwise just do the default thing.
8967   return DefaultABIInfo::classifyReturnType(RetTy);
8968 }
8969 
8970 /// For kernels all parameters are really passed in a special buffer. It doesn't
8971 /// make sense to pass anything byval, so everything must be direct.
8972 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
8973   Ty = useFirstFieldIfTransparentUnion(Ty);
8974 
8975   // TODO: Can we omit empty structs?
8976 
8977   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
8978     Ty = QualType(SeltTy, 0);
8979 
8980   llvm::Type *OrigLTy = CGT.ConvertType(Ty);
8981   llvm::Type *LTy = OrigLTy;
8982   if (getContext().getLangOpts().HIP) {
8983     LTy = coerceKernelArgumentType(
8984         OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
8985         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
8986   }
8987 
8988   // FIXME: Should also use this for OpenCL, but it requires addressing the
8989   // problem of kernels being called.
8990   //
8991   // FIXME: This doesn't apply the optimization of coercing pointers in structs
8992   // to global address space when using byref. This would require implementing a
8993   // new kind of coercion of the in-memory type when for indirect arguments.
8994   if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
8995       isAggregateTypeForABI(Ty)) {
8996     return ABIArgInfo::getIndirectAliased(
8997         getContext().getTypeAlignInChars(Ty),
8998         getContext().getTargetAddressSpace(LangAS::opencl_constant),
8999         false /*Realign*/, nullptr /*Padding*/);
9000   }
9001 
9002   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
9003   // individual elements, which confuses the Clover OpenCL backend; therefore we
9004   // have to set it to false here. Other args of getDirect() are just defaults.
9005   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
9006 }
9007 
9008 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
9009                                                unsigned &NumRegsLeft) const {
9010   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
9011 
9012   Ty = useFirstFieldIfTransparentUnion(Ty);
9013 
9014   if (isAggregateTypeForABI(Ty)) {
9015     // Records with non-trivial destructors/copy-constructors should not be
9016     // passed by value.
9017     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
9018       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9019 
9020     // Ignore empty structs/unions.
9021     if (isEmptyRecord(getContext(), Ty, true))
9022       return ABIArgInfo::getIgnore();
9023 
9024     // Lower single-element structs to just pass a regular value. TODO: We
9025     // could do reasonable-size multiple-element structs too, using getExpand(),
9026     // though watch out for things like bitfields.
9027     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9028       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9029 
9030     if (const RecordType *RT = Ty->getAs<RecordType>()) {
9031       const RecordDecl *RD = RT->getDecl();
9032       if (RD->hasFlexibleArrayMember())
9033         return DefaultABIInfo::classifyArgumentType(Ty);
9034     }
9035 
9036     // Pack aggregates <= 8 bytes into single VGPR or pair.
9037     uint64_t Size = getContext().getTypeSize(Ty);
9038     if (Size <= 64) {
9039       unsigned NumRegs = (Size + 31) / 32;
9040       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
9041 
9042       if (Size <= 16)
9043         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9044 
9045       if (Size <= 32)
9046         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9047 
9048       // XXX: Should this be i64 instead, and should the limit increase?
9049       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9050       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9051     }
9052 
9053     if (NumRegsLeft > 0) {
9054       unsigned NumRegs = numRegsForType(Ty);
9055       if (NumRegsLeft >= NumRegs) {
9056         NumRegsLeft -= NumRegs;
9057         return ABIArgInfo::getDirect();
9058       }
9059     }
9060   }
9061 
9062   // Otherwise just do the default thing.
9063   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
9064   if (!ArgInfo.isIndirect()) {
9065     unsigned NumRegs = numRegsForType(Ty);
9066     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
9067   }
9068 
9069   return ArgInfo;
9070 }
9071 
9072 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
9073 public:
9074   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
9075       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
9076   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
9077                            CodeGen::CodeGenModule &M) const override;
9078   unsigned getOpenCLKernelCallingConv() const override;
9079 
9080   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
9081       llvm::PointerType *T, QualType QT) const override;
9082 
9083   LangAS getASTAllocaAddressSpace() const override {
9084     return getLangASFromTargetAS(
9085         getABIInfo().getDataLayout().getAllocaAddrSpace());
9086   }
9087   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
9088                                   const VarDecl *D) const override;
9089   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
9090                                          SyncScope Scope,
9091                                          llvm::AtomicOrdering Ordering,
9092                                          llvm::LLVMContext &Ctx) const override;
9093   llvm::Function *
9094   createEnqueuedBlockKernel(CodeGenFunction &CGF,
9095                             llvm::Function *BlockInvokeFunc,
9096                             llvm::Value *BlockLiteral) const override;
9097   bool shouldEmitStaticExternCAliases() const override;
9098   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
9099 };
9100 }
9101 
9102 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
9103                                               llvm::GlobalValue *GV) {
9104   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
9105     return false;
9106 
9107   return D->hasAttr<OpenCLKernelAttr>() ||
9108          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
9109          (isa<VarDecl>(D) &&
9110           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
9111            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
9112            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
9113 }
9114 
9115 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
9116     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
9117   if (requiresAMDGPUProtectedVisibility(D, GV)) {
9118     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
9119     GV->setDSOLocal(true);
9120   }
9121 
9122   if (GV->isDeclaration())
9123     return;
9124   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
9125   if (!FD)
9126     return;
9127 
9128   llvm::Function *F = cast<llvm::Function>(GV);
9129 
9130   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
9131     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
9132 
9133 
9134   const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
9135                               FD->hasAttr<OpenCLKernelAttr>();
9136   const bool IsHIPKernel = M.getLangOpts().HIP &&
9137                            FD->hasAttr<CUDAGlobalAttr>();
9138   if ((IsOpenCLKernel || IsHIPKernel) &&
9139       (M.getTriple().getOS() == llvm::Triple::AMDHSA))
9140     F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
9141 
9142   if (IsHIPKernel)
9143     F->addFnAttr("uniform-work-group-size", "true");
9144 
9145 
9146   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
9147   if (ReqdWGS || FlatWGS) {
9148     unsigned Min = 0;
9149     unsigned Max = 0;
9150     if (FlatWGS) {
9151       Min = FlatWGS->getMin()
9152                 ->EvaluateKnownConstInt(M.getContext())
9153                 .getExtValue();
9154       Max = FlatWGS->getMax()
9155                 ->EvaluateKnownConstInt(M.getContext())
9156                 .getExtValue();
9157     }
9158     if (ReqdWGS && Min == 0 && Max == 0)
9159       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
9160 
9161     if (Min != 0) {
9162       assert(Min <= Max && "Min must be less than or equal Max");
9163 
9164       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
9165       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9166     } else
9167       assert(Max == 0 && "Max must be zero");
9168   } else if (IsOpenCLKernel || IsHIPKernel) {
9169     // By default, restrict the maximum size to a value specified by
9170     // --gpu-max-threads-per-block=n or its default value for HIP.
9171     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
9172     const unsigned DefaultMaxWorkGroupSize =
9173         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
9174                        : M.getLangOpts().GPUMaxThreadsPerBlock;
9175     std::string AttrVal =
9176         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
9177     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9178   }
9179 
9180   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
9181     unsigned Min =
9182         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
9183     unsigned Max = Attr->getMax() ? Attr->getMax()
9184                                         ->EvaluateKnownConstInt(M.getContext())
9185                                         .getExtValue()
9186                                   : 0;
9187 
9188     if (Min != 0) {
9189       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
9190 
9191       std::string AttrVal = llvm::utostr(Min);
9192       if (Max != 0)
9193         AttrVal = AttrVal + "," + llvm::utostr(Max);
9194       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
9195     } else
9196       assert(Max == 0 && "Max must be zero");
9197   }
9198 
9199   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
9200     unsigned NumSGPR = Attr->getNumSGPR();
9201 
9202     if (NumSGPR != 0)
9203       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
9204   }
9205 
9206   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
9207     uint32_t NumVGPR = Attr->getNumVGPR();
9208 
9209     if (NumVGPR != 0)
9210       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
9211   }
9212 
9213   if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics())
9214     F->addFnAttr("amdgpu-unsafe-fp-atomics", "true");
9215 
9216   if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
9217     F->addFnAttr("amdgpu-ieee", "false");
9218 }
9219 
9220 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9221   return llvm::CallingConv::AMDGPU_KERNEL;
9222 }
9223 
9224 // Currently LLVM assumes null pointers always have value 0,
9225 // which results in incorrectly transformed IR. Therefore, instead of
9226 // emitting null pointers in private and local address spaces, a null
9227 // pointer in generic address space is emitted which is casted to a
9228 // pointer in local or private address space.
9229 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
9230     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
9231     QualType QT) const {
9232   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
9233     return llvm::ConstantPointerNull::get(PT);
9234 
9235   auto &Ctx = CGM.getContext();
9236   auto NPT = llvm::PointerType::get(PT->getElementType(),
9237       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
9238   return llvm::ConstantExpr::getAddrSpaceCast(
9239       llvm::ConstantPointerNull::get(NPT), PT);
9240 }
9241 
9242 LangAS
9243 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
9244                                                   const VarDecl *D) const {
9245   assert(!CGM.getLangOpts().OpenCL &&
9246          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
9247          "Address space agnostic languages only");
9248   LangAS DefaultGlobalAS = getLangASFromTargetAS(
9249       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
9250   if (!D)
9251     return DefaultGlobalAS;
9252 
9253   LangAS AddrSpace = D->getType().getAddressSpace();
9254   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9255   if (AddrSpace != LangAS::Default)
9256     return AddrSpace;
9257 
9258   if (CGM.isTypeConstant(D->getType(), false)) {
9259     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9260       return ConstAS.getValue();
9261   }
9262   return DefaultGlobalAS;
9263 }
9264 
9265 llvm::SyncScope::ID
9266 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9267                                             SyncScope Scope,
9268                                             llvm::AtomicOrdering Ordering,
9269                                             llvm::LLVMContext &Ctx) const {
9270   std::string Name;
9271   switch (Scope) {
9272   case SyncScope::OpenCLWorkGroup:
9273     Name = "workgroup";
9274     break;
9275   case SyncScope::OpenCLDevice:
9276     Name = "agent";
9277     break;
9278   case SyncScope::OpenCLAllSVMDevices:
9279     Name = "";
9280     break;
9281   case SyncScope::OpenCLSubGroup:
9282     Name = "wavefront";
9283   }
9284 
9285   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9286     if (!Name.empty())
9287       Name = Twine(Twine(Name) + Twine("-")).str();
9288 
9289     Name = Twine(Twine(Name) + Twine("one-as")).str();
9290   }
9291 
9292   return Ctx.getOrInsertSyncScopeID(Name);
9293 }
9294 
9295 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9296   return false;
9297 }
9298 
9299 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9300     const FunctionType *&FT) const {
9301   FT = getABIInfo().getContext().adjustFunctionType(
9302       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9303 }
9304 
9305 //===----------------------------------------------------------------------===//
9306 // SPARC v8 ABI Implementation.
9307 // Based on the SPARC Compliance Definition version 2.4.1.
9308 //
9309 // Ensures that complex values are passed in registers.
9310 //
9311 namespace {
9312 class SparcV8ABIInfo : public DefaultABIInfo {
9313 public:
9314   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9315 
9316 private:
9317   ABIArgInfo classifyReturnType(QualType RetTy) const;
9318   void computeInfo(CGFunctionInfo &FI) const override;
9319 };
9320 } // end anonymous namespace
9321 
9322 
9323 ABIArgInfo
9324 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9325   if (Ty->isAnyComplexType()) {
9326     return ABIArgInfo::getDirect();
9327   }
9328   else {
9329     return DefaultABIInfo::classifyReturnType(Ty);
9330   }
9331 }
9332 
9333 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9334 
9335   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9336   for (auto &Arg : FI.arguments())
9337     Arg.info = classifyArgumentType(Arg.type);
9338 }
9339 
9340 namespace {
9341 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9342 public:
9343   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9344       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9345 };
9346 } // end anonymous namespace
9347 
9348 //===----------------------------------------------------------------------===//
9349 // SPARC v9 ABI Implementation.
9350 // Based on the SPARC Compliance Definition version 2.4.1.
9351 //
9352 // Function arguments a mapped to a nominal "parameter array" and promoted to
9353 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9354 // the array, structs larger than 16 bytes are passed indirectly.
9355 //
9356 // One case requires special care:
9357 //
9358 //   struct mixed {
9359 //     int i;
9360 //     float f;
9361 //   };
9362 //
9363 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9364 // parameter array, but the int is passed in an integer register, and the float
9365 // is passed in a floating point register. This is represented as two arguments
9366 // with the LLVM IR inreg attribute:
9367 //
9368 //   declare void f(i32 inreg %i, float inreg %f)
9369 //
9370 // The code generator will only allocate 4 bytes from the parameter array for
9371 // the inreg arguments. All other arguments are allocated a multiple of 8
9372 // bytes.
9373 //
9374 namespace {
9375 class SparcV9ABIInfo : public ABIInfo {
9376 public:
9377   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9378 
9379 private:
9380   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9381   void computeInfo(CGFunctionInfo &FI) const override;
9382   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9383                     QualType Ty) const override;
9384 
9385   // Coercion type builder for structs passed in registers. The coercion type
9386   // serves two purposes:
9387   //
9388   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9389   //    in registers.
9390   // 2. Expose aligned floating point elements as first-level elements, so the
9391   //    code generator knows to pass them in floating point registers.
9392   //
9393   // We also compute the InReg flag which indicates that the struct contains
9394   // aligned 32-bit floats.
9395   //
9396   struct CoerceBuilder {
9397     llvm::LLVMContext &Context;
9398     const llvm::DataLayout &DL;
9399     SmallVector<llvm::Type*, 8> Elems;
9400     uint64_t Size;
9401     bool InReg;
9402 
9403     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9404       : Context(c), DL(dl), Size(0), InReg(false) {}
9405 
9406     // Pad Elems with integers until Size is ToSize.
9407     void pad(uint64_t ToSize) {
9408       assert(ToSize >= Size && "Cannot remove elements");
9409       if (ToSize == Size)
9410         return;
9411 
9412       // Finish the current 64-bit word.
9413       uint64_t Aligned = llvm::alignTo(Size, 64);
9414       if (Aligned > Size && Aligned <= ToSize) {
9415         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9416         Size = Aligned;
9417       }
9418 
9419       // Add whole 64-bit words.
9420       while (Size + 64 <= ToSize) {
9421         Elems.push_back(llvm::Type::getInt64Ty(Context));
9422         Size += 64;
9423       }
9424 
9425       // Final in-word padding.
9426       if (Size < ToSize) {
9427         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9428         Size = ToSize;
9429       }
9430     }
9431 
9432     // Add a floating point element at Offset.
9433     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9434       // Unaligned floats are treated as integers.
9435       if (Offset % Bits)
9436         return;
9437       // The InReg flag is only required if there are any floats < 64 bits.
9438       if (Bits < 64)
9439         InReg = true;
9440       pad(Offset);
9441       Elems.push_back(Ty);
9442       Size = Offset + Bits;
9443     }
9444 
9445     // Add a struct type to the coercion type, starting at Offset (in bits).
9446     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9447       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9448       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9449         llvm::Type *ElemTy = StrTy->getElementType(i);
9450         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9451         switch (ElemTy->getTypeID()) {
9452         case llvm::Type::StructTyID:
9453           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9454           break;
9455         case llvm::Type::FloatTyID:
9456           addFloat(ElemOffset, ElemTy, 32);
9457           break;
9458         case llvm::Type::DoubleTyID:
9459           addFloat(ElemOffset, ElemTy, 64);
9460           break;
9461         case llvm::Type::FP128TyID:
9462           addFloat(ElemOffset, ElemTy, 128);
9463           break;
9464         case llvm::Type::PointerTyID:
9465           if (ElemOffset % 64 == 0) {
9466             pad(ElemOffset);
9467             Elems.push_back(ElemTy);
9468             Size += 64;
9469           }
9470           break;
9471         default:
9472           break;
9473         }
9474       }
9475     }
9476 
9477     // Check if Ty is a usable substitute for the coercion type.
9478     bool isUsableType(llvm::StructType *Ty) const {
9479       return llvm::makeArrayRef(Elems) == Ty->elements();
9480     }
9481 
9482     // Get the coercion type as a literal struct type.
9483     llvm::Type *getType() const {
9484       if (Elems.size() == 1)
9485         return Elems.front();
9486       else
9487         return llvm::StructType::get(Context, Elems);
9488     }
9489   };
9490 };
9491 } // end anonymous namespace
9492 
9493 ABIArgInfo
9494 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9495   if (Ty->isVoidType())
9496     return ABIArgInfo::getIgnore();
9497 
9498   uint64_t Size = getContext().getTypeSize(Ty);
9499 
9500   // Anything too big to fit in registers is passed with an explicit indirect
9501   // pointer / sret pointer.
9502   if (Size > SizeLimit)
9503     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9504 
9505   // Treat an enum type as its underlying type.
9506   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9507     Ty = EnumTy->getDecl()->getIntegerType();
9508 
9509   // Integer types smaller than a register are extended.
9510   if (Size < 64 && Ty->isIntegerType())
9511     return ABIArgInfo::getExtend(Ty);
9512 
9513   if (const auto *EIT = Ty->getAs<ExtIntType>())
9514     if (EIT->getNumBits() < 64)
9515       return ABIArgInfo::getExtend(Ty);
9516 
9517   // Other non-aggregates go in registers.
9518   if (!isAggregateTypeForABI(Ty))
9519     return ABIArgInfo::getDirect();
9520 
9521   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9522   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9523   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9524     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9525 
9526   // This is a small aggregate type that should be passed in registers.
9527   // Build a coercion type from the LLVM struct type.
9528   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9529   if (!StrTy)
9530     return ABIArgInfo::getDirect();
9531 
9532   CoerceBuilder CB(getVMContext(), getDataLayout());
9533   CB.addStruct(0, StrTy);
9534   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9535 
9536   // Try to use the original type for coercion.
9537   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9538 
9539   if (CB.InReg)
9540     return ABIArgInfo::getDirectInReg(CoerceTy);
9541   else
9542     return ABIArgInfo::getDirect(CoerceTy);
9543 }
9544 
9545 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9546                                   QualType Ty) const {
9547   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9548   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9549   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9550     AI.setCoerceToType(ArgTy);
9551 
9552   CharUnits SlotSize = CharUnits::fromQuantity(8);
9553 
9554   CGBuilderTy &Builder = CGF.Builder;
9555   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
9556   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9557 
9558   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9559 
9560   Address ArgAddr = Address::invalid();
9561   CharUnits Stride;
9562   switch (AI.getKind()) {
9563   case ABIArgInfo::Expand:
9564   case ABIArgInfo::CoerceAndExpand:
9565   case ABIArgInfo::InAlloca:
9566     llvm_unreachable("Unsupported ABI kind for va_arg");
9567 
9568   case ABIArgInfo::Extend: {
9569     Stride = SlotSize;
9570     CharUnits Offset = SlotSize - TypeInfo.Width;
9571     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9572     break;
9573   }
9574 
9575   case ABIArgInfo::Direct: {
9576     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9577     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9578     ArgAddr = Addr;
9579     break;
9580   }
9581 
9582   case ABIArgInfo::Indirect:
9583   case ABIArgInfo::IndirectAliased:
9584     Stride = SlotSize;
9585     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9586     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
9587                       TypeInfo.Align);
9588     break;
9589 
9590   case ABIArgInfo::Ignore:
9591     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.Align);
9592   }
9593 
9594   // Update VAList.
9595   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9596   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9597 
9598   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
9599 }
9600 
9601 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9602   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9603   for (auto &I : FI.arguments())
9604     I.info = classifyType(I.type, 16 * 8);
9605 }
9606 
9607 namespace {
9608 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9609 public:
9610   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9611       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9612 
9613   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9614     return 14;
9615   }
9616 
9617   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9618                                llvm::Value *Address) const override;
9619 };
9620 } // end anonymous namespace
9621 
9622 bool
9623 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9624                                                 llvm::Value *Address) const {
9625   // This is calculated from the LLVM and GCC tables and verified
9626   // against gcc output.  AFAIK all ABIs use the same encoding.
9627 
9628   CodeGen::CGBuilderTy &Builder = CGF.Builder;
9629 
9630   llvm::IntegerType *i8 = CGF.Int8Ty;
9631   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
9632   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
9633 
9634   // 0-31: the 8-byte general-purpose registers
9635   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
9636 
9637   // 32-63: f0-31, the 4-byte floating-point registers
9638   AssignToArrayRange(Builder, Address, Four8, 32, 63);
9639 
9640   //   Y   = 64
9641   //   PSR = 65
9642   //   WIM = 66
9643   //   TBR = 67
9644   //   PC  = 68
9645   //   NPC = 69
9646   //   FSR = 70
9647   //   CSR = 71
9648   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
9649 
9650   // 72-87: d0-15, the 8-byte floating-point registers
9651   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
9652 
9653   return false;
9654 }
9655 
9656 // ARC ABI implementation.
9657 namespace {
9658 
9659 class ARCABIInfo : public DefaultABIInfo {
9660 public:
9661   using DefaultABIInfo::DefaultABIInfo;
9662 
9663 private:
9664   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9665                     QualType Ty) const override;
9666 
9667   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
9668     if (!State.FreeRegs)
9669       return;
9670     if (Info.isIndirect() && Info.getInReg())
9671       State.FreeRegs--;
9672     else if (Info.isDirect() && Info.getInReg()) {
9673       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
9674       if (sz < State.FreeRegs)
9675         State.FreeRegs -= sz;
9676       else
9677         State.FreeRegs = 0;
9678     }
9679   }
9680 
9681   void computeInfo(CGFunctionInfo &FI) const override {
9682     CCState State(FI);
9683     // ARC uses 8 registers to pass arguments.
9684     State.FreeRegs = 8;
9685 
9686     if (!getCXXABI().classifyReturnType(FI))
9687       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9688     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
9689     for (auto &I : FI.arguments()) {
9690       I.info = classifyArgumentType(I.type, State.FreeRegs);
9691       updateState(I.info, I.type, State);
9692     }
9693   }
9694 
9695   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
9696   ABIArgInfo getIndirectByValue(QualType Ty) const;
9697   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
9698   ABIArgInfo classifyReturnType(QualType RetTy) const;
9699 };
9700 
9701 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
9702 public:
9703   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
9704       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
9705 };
9706 
9707 
9708 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
9709   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
9710                        getNaturalAlignIndirect(Ty, false);
9711 }
9712 
9713 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
9714   // Compute the byval alignment.
9715   const unsigned MinABIStackAlignInBytes = 4;
9716   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9717   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9718                                  TypeAlign > MinABIStackAlignInBytes);
9719 }
9720 
9721 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9722                               QualType Ty) const {
9723   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
9724                           getContext().getTypeInfoInChars(Ty),
9725                           CharUnits::fromQuantity(4), true);
9726 }
9727 
9728 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
9729                                             uint8_t FreeRegs) const {
9730   // Handle the generic C++ ABI.
9731   const RecordType *RT = Ty->getAs<RecordType>();
9732   if (RT) {
9733     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
9734     if (RAA == CGCXXABI::RAA_Indirect)
9735       return getIndirectByRef(Ty, FreeRegs > 0);
9736 
9737     if (RAA == CGCXXABI::RAA_DirectInMemory)
9738       return getIndirectByValue(Ty);
9739   }
9740 
9741   // Treat an enum type as its underlying type.
9742   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9743     Ty = EnumTy->getDecl()->getIntegerType();
9744 
9745   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
9746 
9747   if (isAggregateTypeForABI(Ty)) {
9748     // Structures with flexible arrays are always indirect.
9749     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9750       return getIndirectByValue(Ty);
9751 
9752     // Ignore empty structs/unions.
9753     if (isEmptyRecord(getContext(), Ty, true))
9754       return ABIArgInfo::getIgnore();
9755 
9756     llvm::LLVMContext &LLVMContext = getVMContext();
9757 
9758     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9759     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9760     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9761 
9762     return FreeRegs >= SizeInRegs ?
9763         ABIArgInfo::getDirectInReg(Result) :
9764         ABIArgInfo::getDirect(Result, 0, nullptr, false);
9765   }
9766 
9767   if (const auto *EIT = Ty->getAs<ExtIntType>())
9768     if (EIT->getNumBits() > 64)
9769       return getIndirectByValue(Ty);
9770 
9771   return isPromotableIntegerTypeForABI(Ty)
9772              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
9773                                        : ABIArgInfo::getExtend(Ty))
9774              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
9775                                        : ABIArgInfo::getDirect());
9776 }
9777 
9778 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
9779   if (RetTy->isAnyComplexType())
9780     return ABIArgInfo::getDirectInReg();
9781 
9782   // Arguments of size > 4 registers are indirect.
9783   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
9784   if (RetSize > 4)
9785     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
9786 
9787   return DefaultABIInfo::classifyReturnType(RetTy);
9788 }
9789 
9790 } // End anonymous namespace.
9791 
9792 //===----------------------------------------------------------------------===//
9793 // XCore ABI Implementation
9794 //===----------------------------------------------------------------------===//
9795 
9796 namespace {
9797 
9798 /// A SmallStringEnc instance is used to build up the TypeString by passing
9799 /// it by reference between functions that append to it.
9800 typedef llvm::SmallString<128> SmallStringEnc;
9801 
9802 /// TypeStringCache caches the meta encodings of Types.
9803 ///
9804 /// The reason for caching TypeStrings is two fold:
9805 ///   1. To cache a type's encoding for later uses;
9806 ///   2. As a means to break recursive member type inclusion.
9807 ///
9808 /// A cache Entry can have a Status of:
9809 ///   NonRecursive:   The type encoding is not recursive;
9810 ///   Recursive:      The type encoding is recursive;
9811 ///   Incomplete:     An incomplete TypeString;
9812 ///   IncompleteUsed: An incomplete TypeString that has been used in a
9813 ///                   Recursive type encoding.
9814 ///
9815 /// A NonRecursive entry will have all of its sub-members expanded as fully
9816 /// as possible. Whilst it may contain types which are recursive, the type
9817 /// itself is not recursive and thus its encoding may be safely used whenever
9818 /// the type is encountered.
9819 ///
9820 /// A Recursive entry will have all of its sub-members expanded as fully as
9821 /// possible. The type itself is recursive and it may contain other types which
9822 /// are recursive. The Recursive encoding must not be used during the expansion
9823 /// of a recursive type's recursive branch. For simplicity the code uses
9824 /// IncompleteCount to reject all usage of Recursive encodings for member types.
9825 ///
9826 /// An Incomplete entry is always a RecordType and only encodes its
9827 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
9828 /// are placed into the cache during type expansion as a means to identify and
9829 /// handle recursive inclusion of types as sub-members. If there is recursion
9830 /// the entry becomes IncompleteUsed.
9831 ///
9832 /// During the expansion of a RecordType's members:
9833 ///
9834 ///   If the cache contains a NonRecursive encoding for the member type, the
9835 ///   cached encoding is used;
9836 ///
9837 ///   If the cache contains a Recursive encoding for the member type, the
9838 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
9839 ///
9840 ///   If the member is a RecordType, an Incomplete encoding is placed into the
9841 ///   cache to break potential recursive inclusion of itself as a sub-member;
9842 ///
9843 ///   Once a member RecordType has been expanded, its temporary incomplete
9844 ///   entry is removed from the cache. If a Recursive encoding was swapped out
9845 ///   it is swapped back in;
9846 ///
9847 ///   If an incomplete entry is used to expand a sub-member, the incomplete
9848 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
9849 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
9850 ///
9851 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
9852 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
9853 ///   Else the member is part of a recursive type and thus the recursion has
9854 ///   been exited too soon for the encoding to be correct for the member.
9855 ///
9856 class TypeStringCache {
9857   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
9858   struct Entry {
9859     std::string Str;     // The encoded TypeString for the type.
9860     enum Status State;   // Information about the encoding in 'Str'.
9861     std::string Swapped; // A temporary place holder for a Recursive encoding
9862                          // during the expansion of RecordType's members.
9863   };
9864   std::map<const IdentifierInfo *, struct Entry> Map;
9865   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
9866   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
9867 public:
9868   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
9869   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
9870   bool removeIncomplete(const IdentifierInfo *ID);
9871   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
9872                      bool IsRecursive);
9873   StringRef lookupStr(const IdentifierInfo *ID);
9874 };
9875 
9876 /// TypeString encodings for enum & union fields must be order.
9877 /// FieldEncoding is a helper for this ordering process.
9878 class FieldEncoding {
9879   bool HasName;
9880   std::string Enc;
9881 public:
9882   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
9883   StringRef str() { return Enc; }
9884   bool operator<(const FieldEncoding &rhs) const {
9885     if (HasName != rhs.HasName) return HasName;
9886     return Enc < rhs.Enc;
9887   }
9888 };
9889 
9890 class XCoreABIInfo : public DefaultABIInfo {
9891 public:
9892   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9893   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9894                     QualType Ty) const override;
9895 };
9896 
9897 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
9898   mutable TypeStringCache TSC;
9899   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
9900                     const CodeGen::CodeGenModule &M) const;
9901 
9902 public:
9903   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
9904       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
9905   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
9906                           const llvm::MapVector<GlobalDecl, StringRef>
9907                               &MangledDeclNames) const override;
9908 };
9909 
9910 } // End anonymous namespace.
9911 
9912 // TODO: this implementation is likely now redundant with the default
9913 // EmitVAArg.
9914 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9915                                 QualType Ty) const {
9916   CGBuilderTy &Builder = CGF.Builder;
9917 
9918   // Get the VAList.
9919   CharUnits SlotSize = CharUnits::fromQuantity(4);
9920   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
9921 
9922   // Handle the argument.
9923   ABIArgInfo AI = classifyArgumentType(Ty);
9924   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
9925   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9926   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9927     AI.setCoerceToType(ArgTy);
9928   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9929 
9930   Address Val = Address::invalid();
9931   CharUnits ArgSize = CharUnits::Zero();
9932   switch (AI.getKind()) {
9933   case ABIArgInfo::Expand:
9934   case ABIArgInfo::CoerceAndExpand:
9935   case ABIArgInfo::InAlloca:
9936     llvm_unreachable("Unsupported ABI kind for va_arg");
9937   case ABIArgInfo::Ignore:
9938     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
9939     ArgSize = CharUnits::Zero();
9940     break;
9941   case ABIArgInfo::Extend:
9942   case ABIArgInfo::Direct:
9943     Val = Builder.CreateBitCast(AP, ArgPtrTy);
9944     ArgSize = CharUnits::fromQuantity(
9945                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
9946     ArgSize = ArgSize.alignTo(SlotSize);
9947     break;
9948   case ABIArgInfo::Indirect:
9949   case ABIArgInfo::IndirectAliased:
9950     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
9951     Val = Address(Builder.CreateLoad(Val), TypeAlign);
9952     ArgSize = SlotSize;
9953     break;
9954   }
9955 
9956   // Increment the VAList.
9957   if (!ArgSize.isZero()) {
9958     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
9959     Builder.CreateStore(APN.getPointer(), VAListAddr);
9960   }
9961 
9962   return Val;
9963 }
9964 
9965 /// During the expansion of a RecordType, an incomplete TypeString is placed
9966 /// into the cache as a means to identify and break recursion.
9967 /// If there is a Recursive encoding in the cache, it is swapped out and will
9968 /// be reinserted by removeIncomplete().
9969 /// All other types of encoding should have been used rather than arriving here.
9970 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
9971                                     std::string StubEnc) {
9972   if (!ID)
9973     return;
9974   Entry &E = Map[ID];
9975   assert( (E.Str.empty() || E.State == Recursive) &&
9976          "Incorrectly use of addIncomplete");
9977   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
9978   E.Swapped.swap(E.Str); // swap out the Recursive
9979   E.Str.swap(StubEnc);
9980   E.State = Incomplete;
9981   ++IncompleteCount;
9982 }
9983 
9984 /// Once the RecordType has been expanded, the temporary incomplete TypeString
9985 /// must be removed from the cache.
9986 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
9987 /// Returns true if the RecordType was defined recursively.
9988 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
9989   if (!ID)
9990     return false;
9991   auto I = Map.find(ID);
9992   assert(I != Map.end() && "Entry not present");
9993   Entry &E = I->second;
9994   assert( (E.State == Incomplete ||
9995            E.State == IncompleteUsed) &&
9996          "Entry must be an incomplete type");
9997   bool IsRecursive = false;
9998   if (E.State == IncompleteUsed) {
9999     // We made use of our Incomplete encoding, thus we are recursive.
10000     IsRecursive = true;
10001     --IncompleteUsedCount;
10002   }
10003   if (E.Swapped.empty())
10004     Map.erase(I);
10005   else {
10006     // Swap the Recursive back.
10007     E.Swapped.swap(E.Str);
10008     E.Swapped.clear();
10009     E.State = Recursive;
10010   }
10011   --IncompleteCount;
10012   return IsRecursive;
10013 }
10014 
10015 /// Add the encoded TypeString to the cache only if it is NonRecursive or
10016 /// Recursive (viz: all sub-members were expanded as fully as possible).
10017 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
10018                                     bool IsRecursive) {
10019   if (!ID || IncompleteUsedCount)
10020     return; // No key or it is is an incomplete sub-type so don't add.
10021   Entry &E = Map[ID];
10022   if (IsRecursive && !E.Str.empty()) {
10023     assert(E.State==Recursive && E.Str.size() == Str.size() &&
10024            "This is not the same Recursive entry");
10025     // The parent container was not recursive after all, so we could have used
10026     // this Recursive sub-member entry after all, but we assumed the worse when
10027     // we started viz: IncompleteCount!=0.
10028     return;
10029   }
10030   assert(E.Str.empty() && "Entry already present");
10031   E.Str = Str.str();
10032   E.State = IsRecursive? Recursive : NonRecursive;
10033 }
10034 
10035 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
10036 /// are recursively expanding a type (IncompleteCount != 0) and the cached
10037 /// encoding is Recursive, return an empty StringRef.
10038 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
10039   if (!ID)
10040     return StringRef();   // We have no key.
10041   auto I = Map.find(ID);
10042   if (I == Map.end())
10043     return StringRef();   // We have no encoding.
10044   Entry &E = I->second;
10045   if (E.State == Recursive && IncompleteCount)
10046     return StringRef();   // We don't use Recursive encodings for member types.
10047 
10048   if (E.State == Incomplete) {
10049     // The incomplete type is being used to break out of recursion.
10050     E.State = IncompleteUsed;
10051     ++IncompleteUsedCount;
10052   }
10053   return E.Str;
10054 }
10055 
10056 /// The XCore ABI includes a type information section that communicates symbol
10057 /// type information to the linker. The linker uses this information to verify
10058 /// safety/correctness of things such as array bound and pointers et al.
10059 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
10060 /// This type information (TypeString) is emitted into meta data for all global
10061 /// symbols: definitions, declarations, functions & variables.
10062 ///
10063 /// The TypeString carries type, qualifier, name, size & value details.
10064 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
10065 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
10066 /// The output is tested by test/CodeGen/xcore-stringtype.c.
10067 ///
10068 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10069                           const CodeGen::CodeGenModule &CGM,
10070                           TypeStringCache &TSC);
10071 
10072 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
10073 void XCoreTargetCodeGenInfo::emitTargetMD(
10074     const Decl *D, llvm::GlobalValue *GV,
10075     const CodeGen::CodeGenModule &CGM) const {
10076   SmallStringEnc Enc;
10077   if (getTypeString(Enc, D, CGM, TSC)) {
10078     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
10079     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
10080                                 llvm::MDString::get(Ctx, Enc.str())};
10081     llvm::NamedMDNode *MD =
10082       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
10083     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
10084   }
10085 }
10086 
10087 void XCoreTargetCodeGenInfo::emitTargetMetadata(
10088     CodeGen::CodeGenModule &CGM,
10089     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
10090   // Warning, new MangledDeclNames may be appended within this loop.
10091   // We rely on MapVector insertions adding new elements to the end
10092   // of the container.
10093   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
10094     auto Val = *(MangledDeclNames.begin() + I);
10095     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
10096     if (GV) {
10097       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
10098       emitTargetMD(D, GV, CGM);
10099     }
10100   }
10101 }
10102 //===----------------------------------------------------------------------===//
10103 // SPIR ABI Implementation
10104 //===----------------------------------------------------------------------===//
10105 
10106 namespace {
10107 class SPIRABIInfo : public DefaultABIInfo {
10108 public:
10109   SPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
10110 
10111 private:
10112   void setCCs();
10113 };
10114 } // end anonymous namespace
10115 namespace {
10116 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
10117 public:
10118   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10119       : TargetCodeGenInfo(std::make_unique<SPIRABIInfo>(CGT)) {}
10120 
10121   LangAS getASTAllocaAddressSpace() const override {
10122     return getLangASFromTargetAS(
10123         getABIInfo().getDataLayout().getAllocaAddrSpace());
10124   }
10125 
10126   unsigned getOpenCLKernelCallingConv() const override;
10127 };
10128 
10129 } // End anonymous namespace.
10130 void SPIRABIInfo::setCCs() {
10131   assert(getRuntimeCC() == llvm::CallingConv::C);
10132   RuntimeCC = llvm::CallingConv::SPIR_FUNC;
10133 }
10134 
10135 namespace clang {
10136 namespace CodeGen {
10137 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
10138   DefaultABIInfo SPIRABI(CGM.getTypes());
10139   SPIRABI.computeInfo(FI);
10140 }
10141 }
10142 }
10143 
10144 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
10145   return llvm::CallingConv::SPIR_KERNEL;
10146 }
10147 
10148 static bool appendType(SmallStringEnc &Enc, QualType QType,
10149                        const CodeGen::CodeGenModule &CGM,
10150                        TypeStringCache &TSC);
10151 
10152 /// Helper function for appendRecordType().
10153 /// Builds a SmallVector containing the encoded field types in declaration
10154 /// order.
10155 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
10156                              const RecordDecl *RD,
10157                              const CodeGen::CodeGenModule &CGM,
10158                              TypeStringCache &TSC) {
10159   for (const auto *Field : RD->fields()) {
10160     SmallStringEnc Enc;
10161     Enc += "m(";
10162     Enc += Field->getName();
10163     Enc += "){";
10164     if (Field->isBitField()) {
10165       Enc += "b(";
10166       llvm::raw_svector_ostream OS(Enc);
10167       OS << Field->getBitWidthValue(CGM.getContext());
10168       Enc += ':';
10169     }
10170     if (!appendType(Enc, Field->getType(), CGM, TSC))
10171       return false;
10172     if (Field->isBitField())
10173       Enc += ')';
10174     Enc += '}';
10175     FE.emplace_back(!Field->getName().empty(), Enc);
10176   }
10177   return true;
10178 }
10179 
10180 /// Appends structure and union types to Enc and adds encoding to cache.
10181 /// Recursively calls appendType (via extractFieldType) for each field.
10182 /// Union types have their fields ordered according to the ABI.
10183 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
10184                              const CodeGen::CodeGenModule &CGM,
10185                              TypeStringCache &TSC, const IdentifierInfo *ID) {
10186   // Append the cached TypeString if we have one.
10187   StringRef TypeString = TSC.lookupStr(ID);
10188   if (!TypeString.empty()) {
10189     Enc += TypeString;
10190     return true;
10191   }
10192 
10193   // Start to emit an incomplete TypeString.
10194   size_t Start = Enc.size();
10195   Enc += (RT->isUnionType()? 'u' : 's');
10196   Enc += '(';
10197   if (ID)
10198     Enc += ID->getName();
10199   Enc += "){";
10200 
10201   // We collect all encoded fields and order as necessary.
10202   bool IsRecursive = false;
10203   const RecordDecl *RD = RT->getDecl()->getDefinition();
10204   if (RD && !RD->field_empty()) {
10205     // An incomplete TypeString stub is placed in the cache for this RecordType
10206     // so that recursive calls to this RecordType will use it whilst building a
10207     // complete TypeString for this RecordType.
10208     SmallVector<FieldEncoding, 16> FE;
10209     std::string StubEnc(Enc.substr(Start).str());
10210     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
10211     TSC.addIncomplete(ID, std::move(StubEnc));
10212     if (!extractFieldType(FE, RD, CGM, TSC)) {
10213       (void) TSC.removeIncomplete(ID);
10214       return false;
10215     }
10216     IsRecursive = TSC.removeIncomplete(ID);
10217     // The ABI requires unions to be sorted but not structures.
10218     // See FieldEncoding::operator< for sort algorithm.
10219     if (RT->isUnionType())
10220       llvm::sort(FE);
10221     // We can now complete the TypeString.
10222     unsigned E = FE.size();
10223     for (unsigned I = 0; I != E; ++I) {
10224       if (I)
10225         Enc += ',';
10226       Enc += FE[I].str();
10227     }
10228   }
10229   Enc += '}';
10230   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
10231   return true;
10232 }
10233 
10234 /// Appends enum types to Enc and adds the encoding to the cache.
10235 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
10236                            TypeStringCache &TSC,
10237                            const IdentifierInfo *ID) {
10238   // Append the cached TypeString if we have one.
10239   StringRef TypeString = TSC.lookupStr(ID);
10240   if (!TypeString.empty()) {
10241     Enc += TypeString;
10242     return true;
10243   }
10244 
10245   size_t Start = Enc.size();
10246   Enc += "e(";
10247   if (ID)
10248     Enc += ID->getName();
10249   Enc += "){";
10250 
10251   // We collect all encoded enumerations and order them alphanumerically.
10252   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
10253     SmallVector<FieldEncoding, 16> FE;
10254     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
10255          ++I) {
10256       SmallStringEnc EnumEnc;
10257       EnumEnc += "m(";
10258       EnumEnc += I->getName();
10259       EnumEnc += "){";
10260       I->getInitVal().toString(EnumEnc);
10261       EnumEnc += '}';
10262       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
10263     }
10264     llvm::sort(FE);
10265     unsigned E = FE.size();
10266     for (unsigned I = 0; I != E; ++I) {
10267       if (I)
10268         Enc += ',';
10269       Enc += FE[I].str();
10270     }
10271   }
10272   Enc += '}';
10273   TSC.addIfComplete(ID, Enc.substr(Start), false);
10274   return true;
10275 }
10276 
10277 /// Appends type's qualifier to Enc.
10278 /// This is done prior to appending the type's encoding.
10279 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10280   // Qualifiers are emitted in alphabetical order.
10281   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10282   int Lookup = 0;
10283   if (QT.isConstQualified())
10284     Lookup += 1<<0;
10285   if (QT.isRestrictQualified())
10286     Lookup += 1<<1;
10287   if (QT.isVolatileQualified())
10288     Lookup += 1<<2;
10289   Enc += Table[Lookup];
10290 }
10291 
10292 /// Appends built-in types to Enc.
10293 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10294   const char *EncType;
10295   switch (BT->getKind()) {
10296     case BuiltinType::Void:
10297       EncType = "0";
10298       break;
10299     case BuiltinType::Bool:
10300       EncType = "b";
10301       break;
10302     case BuiltinType::Char_U:
10303       EncType = "uc";
10304       break;
10305     case BuiltinType::UChar:
10306       EncType = "uc";
10307       break;
10308     case BuiltinType::SChar:
10309       EncType = "sc";
10310       break;
10311     case BuiltinType::UShort:
10312       EncType = "us";
10313       break;
10314     case BuiltinType::Short:
10315       EncType = "ss";
10316       break;
10317     case BuiltinType::UInt:
10318       EncType = "ui";
10319       break;
10320     case BuiltinType::Int:
10321       EncType = "si";
10322       break;
10323     case BuiltinType::ULong:
10324       EncType = "ul";
10325       break;
10326     case BuiltinType::Long:
10327       EncType = "sl";
10328       break;
10329     case BuiltinType::ULongLong:
10330       EncType = "ull";
10331       break;
10332     case BuiltinType::LongLong:
10333       EncType = "sll";
10334       break;
10335     case BuiltinType::Float:
10336       EncType = "ft";
10337       break;
10338     case BuiltinType::Double:
10339       EncType = "d";
10340       break;
10341     case BuiltinType::LongDouble:
10342       EncType = "ld";
10343       break;
10344     default:
10345       return false;
10346   }
10347   Enc += EncType;
10348   return true;
10349 }
10350 
10351 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10352 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10353                               const CodeGen::CodeGenModule &CGM,
10354                               TypeStringCache &TSC) {
10355   Enc += "p(";
10356   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10357     return false;
10358   Enc += ')';
10359   return true;
10360 }
10361 
10362 /// Appends array encoding to Enc before calling appendType for the element.
10363 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10364                             const ArrayType *AT,
10365                             const CodeGen::CodeGenModule &CGM,
10366                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10367   if (AT->getSizeModifier() != ArrayType::Normal)
10368     return false;
10369   Enc += "a(";
10370   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10371     CAT->getSize().toStringUnsigned(Enc);
10372   else
10373     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10374   Enc += ':';
10375   // The Qualifiers should be attached to the type rather than the array.
10376   appendQualifier(Enc, QT);
10377   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10378     return false;
10379   Enc += ')';
10380   return true;
10381 }
10382 
10383 /// Appends a function encoding to Enc, calling appendType for the return type
10384 /// and the arguments.
10385 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10386                              const CodeGen::CodeGenModule &CGM,
10387                              TypeStringCache &TSC) {
10388   Enc += "f{";
10389   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10390     return false;
10391   Enc += "}(";
10392   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10393     // N.B. we are only interested in the adjusted param types.
10394     auto I = FPT->param_type_begin();
10395     auto E = FPT->param_type_end();
10396     if (I != E) {
10397       do {
10398         if (!appendType(Enc, *I, CGM, TSC))
10399           return false;
10400         ++I;
10401         if (I != E)
10402           Enc += ',';
10403       } while (I != E);
10404       if (FPT->isVariadic())
10405         Enc += ",va";
10406     } else {
10407       if (FPT->isVariadic())
10408         Enc += "va";
10409       else
10410         Enc += '0';
10411     }
10412   }
10413   Enc += ')';
10414   return true;
10415 }
10416 
10417 /// Handles the type's qualifier before dispatching a call to handle specific
10418 /// type encodings.
10419 static bool appendType(SmallStringEnc &Enc, QualType QType,
10420                        const CodeGen::CodeGenModule &CGM,
10421                        TypeStringCache &TSC) {
10422 
10423   QualType QT = QType.getCanonicalType();
10424 
10425   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10426     // The Qualifiers should be attached to the type rather than the array.
10427     // Thus we don't call appendQualifier() here.
10428     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10429 
10430   appendQualifier(Enc, QT);
10431 
10432   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10433     return appendBuiltinType(Enc, BT);
10434 
10435   if (const PointerType *PT = QT->getAs<PointerType>())
10436     return appendPointerType(Enc, PT, CGM, TSC);
10437 
10438   if (const EnumType *ET = QT->getAs<EnumType>())
10439     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10440 
10441   if (const RecordType *RT = QT->getAsStructureType())
10442     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10443 
10444   if (const RecordType *RT = QT->getAsUnionType())
10445     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10446 
10447   if (const FunctionType *FT = QT->getAs<FunctionType>())
10448     return appendFunctionType(Enc, FT, CGM, TSC);
10449 
10450   return false;
10451 }
10452 
10453 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10454                           const CodeGen::CodeGenModule &CGM,
10455                           TypeStringCache &TSC) {
10456   if (!D)
10457     return false;
10458 
10459   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10460     if (FD->getLanguageLinkage() != CLanguageLinkage)
10461       return false;
10462     return appendType(Enc, FD->getType(), CGM, TSC);
10463   }
10464 
10465   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10466     if (VD->getLanguageLinkage() != CLanguageLinkage)
10467       return false;
10468     QualType QT = VD->getType().getCanonicalType();
10469     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10470       // Global ArrayTypes are given a size of '*' if the size is unknown.
10471       // The Qualifiers should be attached to the type rather than the array.
10472       // Thus we don't call appendQualifier() here.
10473       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10474     }
10475     return appendType(Enc, QT, CGM, TSC);
10476   }
10477   return false;
10478 }
10479 
10480 //===----------------------------------------------------------------------===//
10481 // RISCV ABI Implementation
10482 //===----------------------------------------------------------------------===//
10483 
10484 namespace {
10485 class RISCVABIInfo : public DefaultABIInfo {
10486 private:
10487   // Size of the integer ('x') registers in bits.
10488   unsigned XLen;
10489   // Size of the floating point ('f') registers in bits. Note that the target
10490   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10491   // with soft float ABI has FLen==0).
10492   unsigned FLen;
10493   static const int NumArgGPRs = 8;
10494   static const int NumArgFPRs = 8;
10495   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10496                                       llvm::Type *&Field1Ty,
10497                                       CharUnits &Field1Off,
10498                                       llvm::Type *&Field2Ty,
10499                                       CharUnits &Field2Off) const;
10500 
10501 public:
10502   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10503       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10504 
10505   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10506   // non-virtual, but computeInfo is virtual, so we overload it.
10507   void computeInfo(CGFunctionInfo &FI) const override;
10508 
10509   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10510                                   int &ArgFPRsLeft) const;
10511   ABIArgInfo classifyReturnType(QualType RetTy) const;
10512 
10513   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10514                     QualType Ty) const override;
10515 
10516   ABIArgInfo extendType(QualType Ty) const;
10517 
10518   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10519                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10520                                 CharUnits &Field2Off, int &NeededArgGPRs,
10521                                 int &NeededArgFPRs) const;
10522   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10523                                                CharUnits Field1Off,
10524                                                llvm::Type *Field2Ty,
10525                                                CharUnits Field2Off) const;
10526 };
10527 } // end anonymous namespace
10528 
10529 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10530   QualType RetTy = FI.getReturnType();
10531   if (!getCXXABI().classifyReturnType(FI))
10532     FI.getReturnInfo() = classifyReturnType(RetTy);
10533 
10534   // IsRetIndirect is true if classifyArgumentType indicated the value should
10535   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10536   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10537   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10538   // list and pass indirectly on RV32.
10539   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
10540   if (!IsRetIndirect && RetTy->isScalarType() &&
10541       getContext().getTypeSize(RetTy) > (2 * XLen)) {
10542     if (RetTy->isComplexType() && FLen) {
10543       QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
10544       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
10545     } else {
10546       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
10547       IsRetIndirect = true;
10548     }
10549   }
10550 
10551   // We must track the number of GPRs used in order to conform to the RISC-V
10552   // ABI, as integer scalars passed in registers should have signext/zeroext
10553   // when promoted, but are anyext if passed on the stack. As GPR usage is
10554   // different for variadic arguments, we must also track whether we are
10555   // examining a vararg or not.
10556   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
10557   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
10558   int NumFixedArgs = FI.getNumRequiredArgs();
10559 
10560   int ArgNum = 0;
10561   for (auto &ArgInfo : FI.arguments()) {
10562     bool IsFixed = ArgNum < NumFixedArgs;
10563     ArgInfo.info =
10564         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
10565     ArgNum++;
10566   }
10567 }
10568 
10569 // Returns true if the struct is a potential candidate for the floating point
10570 // calling convention. If this function returns true, the caller is
10571 // responsible for checking that if there is only a single field then that
10572 // field is a float.
10573 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10574                                                   llvm::Type *&Field1Ty,
10575                                                   CharUnits &Field1Off,
10576                                                   llvm::Type *&Field2Ty,
10577                                                   CharUnits &Field2Off) const {
10578   bool IsInt = Ty->isIntegralOrEnumerationType();
10579   bool IsFloat = Ty->isRealFloatingType();
10580 
10581   if (IsInt || IsFloat) {
10582     uint64_t Size = getContext().getTypeSize(Ty);
10583     if (IsInt && Size > XLen)
10584       return false;
10585     // Can't be eligible if larger than the FP registers. Half precision isn't
10586     // currently supported on RISC-V and the ABI hasn't been confirmed, so
10587     // default to the integer ABI in that case.
10588     if (IsFloat && (Size > FLen || Size < 32))
10589       return false;
10590     // Can't be eligible if an integer type was already found (int+int pairs
10591     // are not eligible).
10592     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
10593       return false;
10594     if (!Field1Ty) {
10595       Field1Ty = CGT.ConvertType(Ty);
10596       Field1Off = CurOff;
10597       return true;
10598     }
10599     if (!Field2Ty) {
10600       Field2Ty = CGT.ConvertType(Ty);
10601       Field2Off = CurOff;
10602       return true;
10603     }
10604     return false;
10605   }
10606 
10607   if (auto CTy = Ty->getAs<ComplexType>()) {
10608     if (Field1Ty)
10609       return false;
10610     QualType EltTy = CTy->getElementType();
10611     if (getContext().getTypeSize(EltTy) > FLen)
10612       return false;
10613     Field1Ty = CGT.ConvertType(EltTy);
10614     Field1Off = CurOff;
10615     Field2Ty = Field1Ty;
10616     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
10617     return true;
10618   }
10619 
10620   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
10621     uint64_t ArraySize = ATy->getSize().getZExtValue();
10622     QualType EltTy = ATy->getElementType();
10623     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
10624     for (uint64_t i = 0; i < ArraySize; ++i) {
10625       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
10626                                                 Field1Off, Field2Ty, Field2Off);
10627       if (!Ret)
10628         return false;
10629       CurOff += EltSize;
10630     }
10631     return true;
10632   }
10633 
10634   if (const auto *RTy = Ty->getAs<RecordType>()) {
10635     // Structures with either a non-trivial destructor or a non-trivial
10636     // copy constructor are not eligible for the FP calling convention.
10637     if (getRecordArgABI(Ty, CGT.getCXXABI()))
10638       return false;
10639     if (isEmptyRecord(getContext(), Ty, true))
10640       return true;
10641     const RecordDecl *RD = RTy->getDecl();
10642     // Unions aren't eligible unless they're empty (which is caught above).
10643     if (RD->isUnion())
10644       return false;
10645     int ZeroWidthBitFieldCount = 0;
10646     for (const FieldDecl *FD : RD->fields()) {
10647       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
10648       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
10649       QualType QTy = FD->getType();
10650       if (FD->isBitField()) {
10651         unsigned BitWidth = FD->getBitWidthValue(getContext());
10652         // Allow a bitfield with a type greater than XLen as long as the
10653         // bitwidth is XLen or less.
10654         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
10655           QTy = getContext().getIntTypeForBitwidth(XLen, false);
10656         if (BitWidth == 0) {
10657           ZeroWidthBitFieldCount++;
10658           continue;
10659         }
10660       }
10661 
10662       bool Ret = detectFPCCEligibleStructHelper(
10663           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
10664           Field1Ty, Field1Off, Field2Ty, Field2Off);
10665       if (!Ret)
10666         return false;
10667 
10668       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
10669       // or int+fp structs, but are ignored for a struct with an fp field and
10670       // any number of zero-width bitfields.
10671       if (Field2Ty && ZeroWidthBitFieldCount > 0)
10672         return false;
10673     }
10674     return Field1Ty != nullptr;
10675   }
10676 
10677   return false;
10678 }
10679 
10680 // Determine if a struct is eligible for passing according to the floating
10681 // point calling convention (i.e., when flattened it contains a single fp
10682 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
10683 // NeededArgGPRs are incremented appropriately.
10684 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10685                                             CharUnits &Field1Off,
10686                                             llvm::Type *&Field2Ty,
10687                                             CharUnits &Field2Off,
10688                                             int &NeededArgGPRs,
10689                                             int &NeededArgFPRs) const {
10690   Field1Ty = nullptr;
10691   Field2Ty = nullptr;
10692   NeededArgGPRs = 0;
10693   NeededArgFPRs = 0;
10694   bool IsCandidate = detectFPCCEligibleStructHelper(
10695       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
10696   // Not really a candidate if we have a single int but no float.
10697   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
10698     return false;
10699   if (!IsCandidate)
10700     return false;
10701   if (Field1Ty && Field1Ty->isFloatingPointTy())
10702     NeededArgFPRs++;
10703   else if (Field1Ty)
10704     NeededArgGPRs++;
10705   if (Field2Ty && Field2Ty->isFloatingPointTy())
10706     NeededArgFPRs++;
10707   else if (Field2Ty)
10708     NeededArgGPRs++;
10709   return true;
10710 }
10711 
10712 // Call getCoerceAndExpand for the two-element flattened struct described by
10713 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
10714 // appropriate coerceToType and unpaddedCoerceToType.
10715 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
10716     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
10717     CharUnits Field2Off) const {
10718   SmallVector<llvm::Type *, 3> CoerceElts;
10719   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
10720   if (!Field1Off.isZero())
10721     CoerceElts.push_back(llvm::ArrayType::get(
10722         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
10723 
10724   CoerceElts.push_back(Field1Ty);
10725   UnpaddedCoerceElts.push_back(Field1Ty);
10726 
10727   if (!Field2Ty) {
10728     return ABIArgInfo::getCoerceAndExpand(
10729         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
10730         UnpaddedCoerceElts[0]);
10731   }
10732 
10733   CharUnits Field2Align =
10734       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
10735   CharUnits Field1End = Field1Off +
10736       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
10737   CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
10738 
10739   CharUnits Padding = CharUnits::Zero();
10740   if (Field2Off > Field2OffNoPadNoPack)
10741     Padding = Field2Off - Field2OffNoPadNoPack;
10742   else if (Field2Off != Field2Align && Field2Off > Field1End)
10743     Padding = Field2Off - Field1End;
10744 
10745   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
10746 
10747   if (!Padding.isZero())
10748     CoerceElts.push_back(llvm::ArrayType::get(
10749         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
10750 
10751   CoerceElts.push_back(Field2Ty);
10752   UnpaddedCoerceElts.push_back(Field2Ty);
10753 
10754   auto CoerceToType =
10755       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
10756   auto UnpaddedCoerceToType =
10757       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
10758 
10759   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
10760 }
10761 
10762 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
10763                                               int &ArgGPRsLeft,
10764                                               int &ArgFPRsLeft) const {
10765   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
10766   Ty = useFirstFieldIfTransparentUnion(Ty);
10767 
10768   // Structures with either a non-trivial destructor or a non-trivial
10769   // copy constructor are always passed indirectly.
10770   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
10771     if (ArgGPRsLeft)
10772       ArgGPRsLeft -= 1;
10773     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
10774                                            CGCXXABI::RAA_DirectInMemory);
10775   }
10776 
10777   // Ignore empty structs/unions.
10778   if (isEmptyRecord(getContext(), Ty, true))
10779     return ABIArgInfo::getIgnore();
10780 
10781   uint64_t Size = getContext().getTypeSize(Ty);
10782 
10783   // Pass floating point values via FPRs if possible.
10784   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
10785       FLen >= Size && ArgFPRsLeft) {
10786     ArgFPRsLeft--;
10787     return ABIArgInfo::getDirect();
10788   }
10789 
10790   // Complex types for the hard float ABI must be passed direct rather than
10791   // using CoerceAndExpand.
10792   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
10793     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
10794     if (getContext().getTypeSize(EltTy) <= FLen) {
10795       ArgFPRsLeft -= 2;
10796       return ABIArgInfo::getDirect();
10797     }
10798   }
10799 
10800   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
10801     llvm::Type *Field1Ty = nullptr;
10802     llvm::Type *Field2Ty = nullptr;
10803     CharUnits Field1Off = CharUnits::Zero();
10804     CharUnits Field2Off = CharUnits::Zero();
10805     int NeededArgGPRs = 0;
10806     int NeededArgFPRs = 0;
10807     bool IsCandidate =
10808         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
10809                                  NeededArgGPRs, NeededArgFPRs);
10810     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
10811         NeededArgFPRs <= ArgFPRsLeft) {
10812       ArgGPRsLeft -= NeededArgGPRs;
10813       ArgFPRsLeft -= NeededArgFPRs;
10814       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
10815                                                Field2Off);
10816     }
10817   }
10818 
10819   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
10820   bool MustUseStack = false;
10821   // Determine the number of GPRs needed to pass the current argument
10822   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
10823   // register pairs, so may consume 3 registers.
10824   int NeededArgGPRs = 1;
10825   if (!IsFixed && NeededAlign == 2 * XLen)
10826     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
10827   else if (Size > XLen && Size <= 2 * XLen)
10828     NeededArgGPRs = 2;
10829 
10830   if (NeededArgGPRs > ArgGPRsLeft) {
10831     MustUseStack = true;
10832     NeededArgGPRs = ArgGPRsLeft;
10833   }
10834 
10835   ArgGPRsLeft -= NeededArgGPRs;
10836 
10837   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
10838     // Treat an enum type as its underlying type.
10839     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10840       Ty = EnumTy->getDecl()->getIntegerType();
10841 
10842     // All integral types are promoted to XLen width, unless passed on the
10843     // stack.
10844     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
10845       return extendType(Ty);
10846     }
10847 
10848     if (const auto *EIT = Ty->getAs<ExtIntType>()) {
10849       if (EIT->getNumBits() < XLen && !MustUseStack)
10850         return extendType(Ty);
10851       if (EIT->getNumBits() > 128 ||
10852           (!getContext().getTargetInfo().hasInt128Type() &&
10853            EIT->getNumBits() > 64))
10854         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10855     }
10856 
10857     return ABIArgInfo::getDirect();
10858   }
10859 
10860   // Aggregates which are <= 2*XLen will be passed in registers if possible,
10861   // so coerce to integers.
10862   if (Size <= 2 * XLen) {
10863     unsigned Alignment = getContext().getTypeAlign(Ty);
10864 
10865     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
10866     // required, and a 2-element XLen array if only XLen alignment is required.
10867     if (Size <= XLen) {
10868       return ABIArgInfo::getDirect(
10869           llvm::IntegerType::get(getVMContext(), XLen));
10870     } else if (Alignment == 2 * XLen) {
10871       return ABIArgInfo::getDirect(
10872           llvm::IntegerType::get(getVMContext(), 2 * XLen));
10873     } else {
10874       return ABIArgInfo::getDirect(llvm::ArrayType::get(
10875           llvm::IntegerType::get(getVMContext(), XLen), 2));
10876     }
10877   }
10878   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10879 }
10880 
10881 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
10882   if (RetTy->isVoidType())
10883     return ABIArgInfo::getIgnore();
10884 
10885   int ArgGPRsLeft = 2;
10886   int ArgFPRsLeft = FLen ? 2 : 0;
10887 
10888   // The rules for return and argument types are the same, so defer to
10889   // classifyArgumentType.
10890   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
10891                               ArgFPRsLeft);
10892 }
10893 
10894 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10895                                 QualType Ty) const {
10896   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
10897 
10898   // Empty records are ignored for parameter passing purposes.
10899   if (isEmptyRecord(getContext(), Ty, true)) {
10900     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
10901     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
10902     return Addr;
10903   }
10904 
10905   auto TInfo = getContext().getTypeInfoInChars(Ty);
10906 
10907   // Arguments bigger than 2*Xlen bytes are passed indirectly.
10908   bool IsIndirect = TInfo.Width > 2 * SlotSize;
10909 
10910   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo,
10911                           SlotSize, /*AllowHigherAlign=*/true);
10912 }
10913 
10914 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
10915   int TySize = getContext().getTypeSize(Ty);
10916   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
10917   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
10918     return ABIArgInfo::getSignExtend(Ty);
10919   return ABIArgInfo::getExtend(Ty);
10920 }
10921 
10922 namespace {
10923 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
10924 public:
10925   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
10926                          unsigned FLen)
10927       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
10928 
10929   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
10930                            CodeGen::CodeGenModule &CGM) const override {
10931     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
10932     if (!FD) return;
10933 
10934     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
10935     if (!Attr)
10936       return;
10937 
10938     const char *Kind;
10939     switch (Attr->getInterrupt()) {
10940     case RISCVInterruptAttr::user: Kind = "user"; break;
10941     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
10942     case RISCVInterruptAttr::machine: Kind = "machine"; break;
10943     }
10944 
10945     auto *Fn = cast<llvm::Function>(GV);
10946 
10947     Fn->addFnAttr("interrupt", Kind);
10948   }
10949 };
10950 } // namespace
10951 
10952 //===----------------------------------------------------------------------===//
10953 // VE ABI Implementation.
10954 //
10955 namespace {
10956 class VEABIInfo : public DefaultABIInfo {
10957 public:
10958   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
10959 
10960 private:
10961   ABIArgInfo classifyReturnType(QualType RetTy) const;
10962   ABIArgInfo classifyArgumentType(QualType RetTy) const;
10963   void computeInfo(CGFunctionInfo &FI) const override;
10964 };
10965 } // end anonymous namespace
10966 
10967 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
10968   if (Ty->isAnyComplexType())
10969     return ABIArgInfo::getDirect();
10970   uint64_t Size = getContext().getTypeSize(Ty);
10971   if (Size < 64 && Ty->isIntegerType())
10972     return ABIArgInfo::getExtend(Ty);
10973   return DefaultABIInfo::classifyReturnType(Ty);
10974 }
10975 
10976 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
10977   if (Ty->isAnyComplexType())
10978     return ABIArgInfo::getDirect();
10979   uint64_t Size = getContext().getTypeSize(Ty);
10980   if (Size < 64 && Ty->isIntegerType())
10981     return ABIArgInfo::getExtend(Ty);
10982   return DefaultABIInfo::classifyArgumentType(Ty);
10983 }
10984 
10985 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
10986   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10987   for (auto &Arg : FI.arguments())
10988     Arg.info = classifyArgumentType(Arg.type);
10989 }
10990 
10991 namespace {
10992 class VETargetCodeGenInfo : public TargetCodeGenInfo {
10993 public:
10994   VETargetCodeGenInfo(CodeGenTypes &CGT)
10995       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
10996   // VE ABI requires the arguments of variadic and prototype-less functions
10997   // are passed in both registers and memory.
10998   bool isNoProtoCallVariadic(const CallArgList &args,
10999                              const FunctionNoProtoType *fnType) const override {
11000     return true;
11001   }
11002 };
11003 } // end anonymous namespace
11004 
11005 //===----------------------------------------------------------------------===//
11006 // Driver code
11007 //===----------------------------------------------------------------------===//
11008 
11009 bool CodeGenModule::supportsCOMDAT() const {
11010   return getTriple().supportsCOMDAT();
11011 }
11012 
11013 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
11014   if (TheTargetCodeGenInfo)
11015     return *TheTargetCodeGenInfo;
11016 
11017   // Helper to set the unique_ptr while still keeping the return value.
11018   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
11019     this->TheTargetCodeGenInfo.reset(P);
11020     return *P;
11021   };
11022 
11023   const llvm::Triple &Triple = getTarget().getTriple();
11024   switch (Triple.getArch()) {
11025   default:
11026     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
11027 
11028   case llvm::Triple::le32:
11029     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11030   case llvm::Triple::m68k:
11031     return SetCGInfo(new M68kTargetCodeGenInfo(Types));
11032   case llvm::Triple::mips:
11033   case llvm::Triple::mipsel:
11034     if (Triple.getOS() == llvm::Triple::NaCl)
11035       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11036     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
11037 
11038   case llvm::Triple::mips64:
11039   case llvm::Triple::mips64el:
11040     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
11041 
11042   case llvm::Triple::avr:
11043     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
11044 
11045   case llvm::Triple::aarch64:
11046   case llvm::Triple::aarch64_32:
11047   case llvm::Triple::aarch64_be: {
11048     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
11049     if (getTarget().getABI() == "darwinpcs")
11050       Kind = AArch64ABIInfo::DarwinPCS;
11051     else if (Triple.isOSWindows())
11052       return SetCGInfo(
11053           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
11054 
11055     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
11056   }
11057 
11058   case llvm::Triple::wasm32:
11059   case llvm::Triple::wasm64: {
11060     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
11061     if (getTarget().getABI() == "experimental-mv")
11062       Kind = WebAssemblyABIInfo::ExperimentalMV;
11063     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
11064   }
11065 
11066   case llvm::Triple::arm:
11067   case llvm::Triple::armeb:
11068   case llvm::Triple::thumb:
11069   case llvm::Triple::thumbeb: {
11070     if (Triple.getOS() == llvm::Triple::Win32) {
11071       return SetCGInfo(
11072           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
11073     }
11074 
11075     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
11076     StringRef ABIStr = getTarget().getABI();
11077     if (ABIStr == "apcs-gnu")
11078       Kind = ARMABIInfo::APCS;
11079     else if (ABIStr == "aapcs16")
11080       Kind = ARMABIInfo::AAPCS16_VFP;
11081     else if (CodeGenOpts.FloatABI == "hard" ||
11082              (CodeGenOpts.FloatABI != "soft" &&
11083               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
11084                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
11085                Triple.getEnvironment() == llvm::Triple::EABIHF)))
11086       Kind = ARMABIInfo::AAPCS_VFP;
11087 
11088     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
11089   }
11090 
11091   case llvm::Triple::ppc: {
11092     if (Triple.isOSAIX())
11093       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
11094 
11095     bool IsSoftFloat =
11096         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
11097     bool RetSmallStructInRegABI =
11098         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11099     return SetCGInfo(
11100         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11101   }
11102   case llvm::Triple::ppcle: {
11103     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11104     bool RetSmallStructInRegABI =
11105         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11106     return SetCGInfo(
11107         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11108   }
11109   case llvm::Triple::ppc64:
11110     if (Triple.isOSAIX())
11111       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
11112 
11113     if (Triple.isOSBinFormatELF()) {
11114       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
11115       if (getTarget().getABI() == "elfv2")
11116         Kind = PPC64_SVR4_ABIInfo::ELFv2;
11117       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11118 
11119       return SetCGInfo(
11120           new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11121     }
11122     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
11123   case llvm::Triple::ppc64le: {
11124     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
11125     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
11126     if (getTarget().getABI() == "elfv1")
11127       Kind = PPC64_SVR4_ABIInfo::ELFv1;
11128     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11129 
11130     return SetCGInfo(
11131         new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11132   }
11133 
11134   case llvm::Triple::nvptx:
11135   case llvm::Triple::nvptx64:
11136     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
11137 
11138   case llvm::Triple::msp430:
11139     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
11140 
11141   case llvm::Triple::riscv32:
11142   case llvm::Triple::riscv64: {
11143     StringRef ABIStr = getTarget().getABI();
11144     unsigned XLen = getTarget().getPointerWidth(0);
11145     unsigned ABIFLen = 0;
11146     if (ABIStr.endswith("f"))
11147       ABIFLen = 32;
11148     else if (ABIStr.endswith("d"))
11149       ABIFLen = 64;
11150     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
11151   }
11152 
11153   case llvm::Triple::systemz: {
11154     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
11155     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
11156     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
11157   }
11158 
11159   case llvm::Triple::tce:
11160   case llvm::Triple::tcele:
11161     return SetCGInfo(new TCETargetCodeGenInfo(Types));
11162 
11163   case llvm::Triple::x86: {
11164     bool IsDarwinVectorABI = Triple.isOSDarwin();
11165     bool RetSmallStructInRegABI =
11166         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11167     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
11168 
11169     if (Triple.getOS() == llvm::Triple::Win32) {
11170       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
11171           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11172           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
11173     } else {
11174       return SetCGInfo(new X86_32TargetCodeGenInfo(
11175           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11176           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
11177           CodeGenOpts.FloatABI == "soft"));
11178     }
11179   }
11180 
11181   case llvm::Triple::x86_64: {
11182     StringRef ABI = getTarget().getABI();
11183     X86AVXABILevel AVXLevel =
11184         (ABI == "avx512"
11185              ? X86AVXABILevel::AVX512
11186              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
11187 
11188     switch (Triple.getOS()) {
11189     case llvm::Triple::Win32:
11190       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
11191     default:
11192       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
11193     }
11194   }
11195   case llvm::Triple::hexagon:
11196     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
11197   case llvm::Triple::lanai:
11198     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
11199   case llvm::Triple::r600:
11200     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11201   case llvm::Triple::amdgcn:
11202     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11203   case llvm::Triple::sparc:
11204     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
11205   case llvm::Triple::sparcv9:
11206     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
11207   case llvm::Triple::xcore:
11208     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
11209   case llvm::Triple::arc:
11210     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
11211   case llvm::Triple::spir:
11212   case llvm::Triple::spir64:
11213     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
11214   case llvm::Triple::ve:
11215     return SetCGInfo(new VETargetCodeGenInfo(Types));
11216   }
11217 }
11218 
11219 /// Create an OpenCL kernel for an enqueued block.
11220 ///
11221 /// The kernel has the same function type as the block invoke function. Its
11222 /// name is the name of the block invoke function postfixed with "_kernel".
11223 /// It simply calls the block invoke function then returns.
11224 llvm::Function *
11225 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
11226                                              llvm::Function *Invoke,
11227                                              llvm::Value *BlockLiteral) const {
11228   auto *InvokeFT = Invoke->getFunctionType();
11229   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11230   for (auto &P : InvokeFT->params())
11231     ArgTys.push_back(P);
11232   auto &C = CGF.getLLVMContext();
11233   std::string Name = Invoke->getName().str() + "_kernel";
11234   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11235   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11236                                    &CGF.CGM.getModule());
11237   auto IP = CGF.Builder.saveIP();
11238   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11239   auto &Builder = CGF.Builder;
11240   Builder.SetInsertPoint(BB);
11241   llvm::SmallVector<llvm::Value *, 2> Args;
11242   for (auto &A : F->args())
11243     Args.push_back(&A);
11244   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11245   call->setCallingConv(Invoke->getCallingConv());
11246   Builder.CreateRetVoid();
11247   Builder.restoreIP(IP);
11248   return F;
11249 }
11250 
11251 /// Create an OpenCL kernel for an enqueued block.
11252 ///
11253 /// The type of the first argument (the block literal) is the struct type
11254 /// of the block literal instead of a pointer type. The first argument
11255 /// (block literal) is passed directly by value to the kernel. The kernel
11256 /// allocates the same type of struct on stack and stores the block literal
11257 /// to it and passes its pointer to the block invoke function. The kernel
11258 /// has "enqueued-block" function attribute and kernel argument metadata.
11259 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
11260     CodeGenFunction &CGF, llvm::Function *Invoke,
11261     llvm::Value *BlockLiteral) const {
11262   auto &Builder = CGF.Builder;
11263   auto &C = CGF.getLLVMContext();
11264 
11265   auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
11266   auto *InvokeFT = Invoke->getFunctionType();
11267   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11268   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
11269   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
11270   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
11271   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
11272   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
11273   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
11274 
11275   ArgTys.push_back(BlockTy);
11276   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11277   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
11278   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11279   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11280   AccessQuals.push_back(llvm::MDString::get(C, "none"));
11281   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
11282   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
11283     ArgTys.push_back(InvokeFT->getParamType(I));
11284     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
11285     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
11286     AccessQuals.push_back(llvm::MDString::get(C, "none"));
11287     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
11288     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11289     ArgNames.push_back(
11290         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
11291   }
11292   std::string Name = Invoke->getName().str() + "_kernel";
11293   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11294   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11295                                    &CGF.CGM.getModule());
11296   F->addFnAttr("enqueued-block");
11297   auto IP = CGF.Builder.saveIP();
11298   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11299   Builder.SetInsertPoint(BB);
11300   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
11301   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
11302   BlockPtr->setAlignment(BlockAlign);
11303   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
11304   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
11305   llvm::SmallVector<llvm::Value *, 2> Args;
11306   Args.push_back(Cast);
11307   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
11308     Args.push_back(I);
11309   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11310   call->setCallingConv(Invoke->getCallingConv());
11311   Builder.CreateRetVoid();
11312   Builder.restoreIP(IP);
11313 
11314   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
11315   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
11316   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
11317   F->setMetadata("kernel_arg_base_type",
11318                  llvm::MDNode::get(C, ArgBaseTypeNames));
11319   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
11320   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
11321     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
11322 
11323   return F;
11324 }
11325