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/CodeGen/CGFunctionInfo.h"
25 #include "clang/CodeGen/SwiftCallingConv.h"
26 #include "llvm/ADT/SmallBitVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/IntrinsicsNVPTX.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <algorithm> // std::sort
36 
37 using namespace clang;
38 using namespace CodeGen;
39 
40 // Helper for coercing an aggregate argument or return value into an integer
41 // array of the same size (including padding) and alignment.  This alternate
42 // coercion happens only for the RenderScript ABI and can be removed after
43 // runtimes that rely on it are no longer supported.
44 //
45 // RenderScript assumes that the size of the argument / return value in the IR
46 // is the same as the size of the corresponding qualified type. This helper
47 // coerces the aggregate type into an array of the same size (including
48 // padding).  This coercion is used in lieu of expansion of struct members or
49 // other canonical coercions that return a coerced-type of larger size.
50 //
51 // Ty          - The argument / return value type
52 // Context     - The associated ASTContext
53 // LLVMContext - The associated LLVMContext
54 static ABIArgInfo coerceToIntArray(QualType Ty,
55                                    ASTContext &Context,
56                                    llvm::LLVMContext &LLVMContext) {
57   // Alignment and Size are measured in bits.
58   const uint64_t Size = Context.getTypeSize(Ty);
59   const uint64_t Alignment = Context.getTypeAlign(Ty);
60   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
61   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
62   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
63 }
64 
65 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
66                                llvm::Value *Array,
67                                llvm::Value *Value,
68                                unsigned FirstIndex,
69                                unsigned LastIndex) {
70   // Alternatively, we could emit this as a loop in the source.
71   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
72     llvm::Value *Cell =
73         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
74     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
75   }
76 }
77 
78 static bool isAggregateTypeForABI(QualType T) {
79   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
80          T->isMemberFunctionPointerType();
81 }
82 
83 ABIArgInfo
84 ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
85                                  llvm::Type *Padding) const {
86   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
87                                  ByRef, Realign, Padding);
88 }
89 
90 ABIArgInfo
91 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
92   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
93                                       /*ByRef*/ false, Realign);
94 }
95 
96 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
97                              QualType Ty) const {
98   return Address::invalid();
99 }
100 
101 bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
102   if (Ty->isPromotableIntegerType())
103     return true;
104 
105   if (const auto *EIT = Ty->getAs<ExtIntType>())
106     if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
107       return true;
108 
109   return false;
110 }
111 
112 ABIInfo::~ABIInfo() {}
113 
114 /// Does the given lowering require more than the given number of
115 /// registers when expanded?
116 ///
117 /// This is intended to be the basis of a reasonable basic implementation
118 /// of should{Pass,Return}IndirectlyForSwift.
119 ///
120 /// For most targets, a limit of four total registers is reasonable; this
121 /// limits the amount of code required in order to move around the value
122 /// in case it wasn't produced immediately prior to the call by the caller
123 /// (or wasn't produced in exactly the right registers) or isn't used
124 /// immediately within the callee.  But some targets may need to further
125 /// limit the register count due to an inability to support that many
126 /// return registers.
127 static bool occupiesMoreThan(CodeGenTypes &cgt,
128                              ArrayRef<llvm::Type*> scalarTypes,
129                              unsigned maxAllRegisters) {
130   unsigned intCount = 0, fpCount = 0;
131   for (llvm::Type *type : scalarTypes) {
132     if (type->isPointerTy()) {
133       intCount++;
134     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
135       auto ptrWidth = cgt.getTarget().getPointerWidth(0);
136       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
137     } else {
138       assert(type->isVectorTy() || type->isFloatingPointTy());
139       fpCount++;
140     }
141   }
142 
143   return (intCount + fpCount > maxAllRegisters);
144 }
145 
146 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
147                                              llvm::Type *eltTy,
148                                              unsigned numElts) const {
149   // The default implementation of this assumes that the target guarantees
150   // 128-bit SIMD support but nothing more.
151   return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
152 }
153 
154 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
155                                               CGCXXABI &CXXABI) {
156   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
157   if (!RD) {
158     if (!RT->getDecl()->canPassInRegisters())
159       return CGCXXABI::RAA_Indirect;
160     return CGCXXABI::RAA_Default;
161   }
162   return CXXABI.getRecordArgABI(RD);
163 }
164 
165 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
166                                               CGCXXABI &CXXABI) {
167   const RecordType *RT = T->getAs<RecordType>();
168   if (!RT)
169     return CGCXXABI::RAA_Default;
170   return getRecordArgABI(RT, CXXABI);
171 }
172 
173 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
174                                const ABIInfo &Info) {
175   QualType Ty = FI.getReturnType();
176 
177   if (const auto *RT = Ty->getAs<RecordType>())
178     if (!isa<CXXRecordDecl>(RT->getDecl()) &&
179         !RT->getDecl()->canPassInRegisters()) {
180       FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
181       return true;
182     }
183 
184   return CXXABI.classifyReturnType(FI);
185 }
186 
187 /// Pass transparent unions as if they were the type of the first element. Sema
188 /// should ensure that all elements of the union have the same "machine type".
189 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
190   if (const RecordType *UT = Ty->getAsUnionType()) {
191     const RecordDecl *UD = UT->getDecl();
192     if (UD->hasAttr<TransparentUnionAttr>()) {
193       assert(!UD->field_empty() && "sema created an empty transparent union");
194       return UD->field_begin()->getType();
195     }
196   }
197   return Ty;
198 }
199 
200 CGCXXABI &ABIInfo::getCXXABI() const {
201   return CGT.getCXXABI();
202 }
203 
204 ASTContext &ABIInfo::getContext() const {
205   return CGT.getContext();
206 }
207 
208 llvm::LLVMContext &ABIInfo::getVMContext() const {
209   return CGT.getLLVMContext();
210 }
211 
212 const llvm::DataLayout &ABIInfo::getDataLayout() const {
213   return CGT.getDataLayout();
214 }
215 
216 const TargetInfo &ABIInfo::getTarget() const {
217   return CGT.getTarget();
218 }
219 
220 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
221   return CGT.getCodeGenOpts();
222 }
223 
224 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
225 
226 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
227   return false;
228 }
229 
230 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
231                                                 uint64_t Members) const {
232   return false;
233 }
234 
235 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
236   raw_ostream &OS = llvm::errs();
237   OS << "(ABIArgInfo Kind=";
238   switch (TheKind) {
239   case Direct:
240     OS << "Direct Type=";
241     if (llvm::Type *Ty = getCoerceToType())
242       Ty->print(OS);
243     else
244       OS << "null";
245     break;
246   case Extend:
247     OS << "Extend";
248     break;
249   case Ignore:
250     OS << "Ignore";
251     break;
252   case InAlloca:
253     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
254     break;
255   case Indirect:
256     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
257        << " ByVal=" << getIndirectByVal()
258        << " Realign=" << getIndirectRealign();
259     break;
260   case Expand:
261     OS << "Expand";
262     break;
263   case CoerceAndExpand:
264     OS << "CoerceAndExpand Type=";
265     getCoerceAndExpandType()->print(OS);
266     break;
267   }
268   OS << ")\n";
269 }
270 
271 // Dynamically round a pointer up to a multiple of the given alignment.
272 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
273                                                   llvm::Value *Ptr,
274                                                   CharUnits Align) {
275   llvm::Value *PtrAsInt = Ptr;
276   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
277   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
278   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
279         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
280   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
281            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
282   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
283                                         Ptr->getType(),
284                                         Ptr->getName() + ".aligned");
285   return PtrAsInt;
286 }
287 
288 /// Emit va_arg for a platform using the common void* representation,
289 /// where arguments are simply emitted in an array of slots on the stack.
290 ///
291 /// This version implements the core direct-value passing rules.
292 ///
293 /// \param SlotSize - The size and alignment of a stack slot.
294 ///   Each argument will be allocated to a multiple of this number of
295 ///   slots, and all the slots will be aligned to this value.
296 /// \param AllowHigherAlign - The slot alignment is not a cap;
297 ///   an argument type with an alignment greater than the slot size
298 ///   will be emitted on a higher-alignment address, potentially
299 ///   leaving one or more empty slots behind as padding.  If this
300 ///   is false, the returned address might be less-aligned than
301 ///   DirectAlign.
302 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
303                                       Address VAListAddr,
304                                       llvm::Type *DirectTy,
305                                       CharUnits DirectSize,
306                                       CharUnits DirectAlign,
307                                       CharUnits SlotSize,
308                                       bool AllowHigherAlign) {
309   // Cast the element type to i8* if necessary.  Some platforms define
310   // va_list as a struct containing an i8* instead of just an i8*.
311   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
312     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
313 
314   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
315 
316   // If the CC aligns values higher than the slot size, do so if needed.
317   Address Addr = Address::invalid();
318   if (AllowHigherAlign && DirectAlign > SlotSize) {
319     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
320                                                  DirectAlign);
321   } else {
322     Addr = Address(Ptr, SlotSize);
323   }
324 
325   // Advance the pointer past the argument, then store that back.
326   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
327   Address NextPtr =
328       CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
329   CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
330 
331   // If the argument is smaller than a slot, and this is a big-endian
332   // target, the argument will be right-adjusted in its slot.
333   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
334       !DirectTy->isStructTy()) {
335     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
336   }
337 
338   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
339   return Addr;
340 }
341 
342 /// Emit va_arg for a platform using the common void* representation,
343 /// where arguments are simply emitted in an array of slots on the stack.
344 ///
345 /// \param IsIndirect - Values of this type are passed indirectly.
346 /// \param ValueInfo - The size and alignment of this type, generally
347 ///   computed with getContext().getTypeInfoInChars(ValueTy).
348 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
349 ///   Each argument will be allocated to a multiple of this number of
350 ///   slots, and all the slots will be aligned to this value.
351 /// \param AllowHigherAlign - The slot alignment is not a cap;
352 ///   an argument type with an alignment greater than the slot size
353 ///   will be emitted on a higher-alignment address, potentially
354 ///   leaving one or more empty slots behind as padding.
355 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
356                                 QualType ValueTy, bool IsIndirect,
357                                 std::pair<CharUnits, CharUnits> ValueInfo,
358                                 CharUnits SlotSizeAndAlign,
359                                 bool AllowHigherAlign) {
360   // The size and alignment of the value that was passed directly.
361   CharUnits DirectSize, DirectAlign;
362   if (IsIndirect) {
363     DirectSize = CGF.getPointerSize();
364     DirectAlign = CGF.getPointerAlign();
365   } else {
366     DirectSize = ValueInfo.first;
367     DirectAlign = ValueInfo.second;
368   }
369 
370   // Cast the address we've calculated to the right type.
371   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
372   if (IsIndirect)
373     DirectTy = DirectTy->getPointerTo(0);
374 
375   Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
376                                         DirectSize, DirectAlign,
377                                         SlotSizeAndAlign,
378                                         AllowHigherAlign);
379 
380   if (IsIndirect) {
381     Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
382   }
383 
384   return Addr;
385 
386 }
387 
388 static Address emitMergePHI(CodeGenFunction &CGF,
389                             Address Addr1, llvm::BasicBlock *Block1,
390                             Address Addr2, llvm::BasicBlock *Block2,
391                             const llvm::Twine &Name = "") {
392   assert(Addr1.getType() == Addr2.getType());
393   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
394   PHI->addIncoming(Addr1.getPointer(), Block1);
395   PHI->addIncoming(Addr2.getPointer(), Block2);
396   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
397   return Address(PHI, Align);
398 }
399 
400 TargetCodeGenInfo::~TargetCodeGenInfo() = default;
401 
402 // If someone can figure out a general rule for this, that would be great.
403 // It's probably just doomed to be platform-dependent, though.
404 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
405   // Verified for:
406   //   x86-64     FreeBSD, Linux, Darwin
407   //   x86-32     FreeBSD, Linux, Darwin
408   //   PowerPC    Linux, Darwin
409   //   ARM        Darwin (*not* EABI)
410   //   AArch64    Linux
411   return 32;
412 }
413 
414 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
415                                      const FunctionNoProtoType *fnType) const {
416   // The following conventions are known to require this to be false:
417   //   x86_stdcall
418   //   MIPS
419   // For everything else, we just prefer false unless we opt out.
420   return false;
421 }
422 
423 void
424 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
425                                              llvm::SmallString<24> &Opt) const {
426   // This assumes the user is passing a library name like "rt" instead of a
427   // filename like "librt.a/so", and that they don't care whether it's static or
428   // dynamic.
429   Opt = "-l";
430   Opt += Lib;
431 }
432 
433 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
434   // OpenCL kernels are called via an explicit runtime API with arguments
435   // set with clSetKernelArg(), not as normal sub-functions.
436   // Return SPIR_KERNEL by default as the kernel calling convention to
437   // ensure the fingerprint is fixed such way that each OpenCL argument
438   // gets one matching argument in the produced kernel function argument
439   // list to enable feasible implementation of clSetKernelArg() with
440   // aggregates etc. In case we would use the default C calling conv here,
441   // clSetKernelArg() might break depending on the target-specific
442   // conventions; different targets might split structs passed as values
443   // to multiple function arguments etc.
444   return llvm::CallingConv::SPIR_KERNEL;
445 }
446 
447 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
448     llvm::PointerType *T, QualType QT) const {
449   return llvm::ConstantPointerNull::get(T);
450 }
451 
452 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
453                                                    const VarDecl *D) const {
454   assert(!CGM.getLangOpts().OpenCL &&
455          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
456          "Address space agnostic languages only");
457   return D ? D->getType().getAddressSpace() : LangAS::Default;
458 }
459 
460 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
461     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
462     LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
463   // Since target may map different address spaces in AST to the same address
464   // space, an address space conversion may end up as a bitcast.
465   if (auto *C = dyn_cast<llvm::Constant>(Src))
466     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
467   // Try to preserve the source's name to make IR more readable.
468   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
469       Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
470 }
471 
472 llvm::Constant *
473 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
474                                         LangAS SrcAddr, LangAS DestAddr,
475                                         llvm::Type *DestTy) const {
476   // Since target may map different address spaces in AST to the same address
477   // space, an address space conversion may end up as a bitcast.
478   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
479 }
480 
481 llvm::SyncScope::ID
482 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
483                                       SyncScope Scope,
484                                       llvm::AtomicOrdering Ordering,
485                                       llvm::LLVMContext &Ctx) const {
486   return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
487 }
488 
489 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
490 
491 /// isEmptyField - Return true iff a the field is "empty", that is it
492 /// is an unnamed bit-field or an (array of) empty record(s).
493 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
494                          bool AllowArrays) {
495   if (FD->isUnnamedBitfield())
496     return true;
497 
498   QualType FT = FD->getType();
499 
500   // Constant arrays of empty records count as empty, strip them off.
501   // Constant arrays of zero length always count as empty.
502   if (AllowArrays)
503     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
504       if (AT->getSize() == 0)
505         return true;
506       FT = AT->getElementType();
507     }
508 
509   const RecordType *RT = FT->getAs<RecordType>();
510   if (!RT)
511     return false;
512 
513   // C++ record fields are never empty, at least in the Itanium ABI.
514   //
515   // FIXME: We should use a predicate for whether this behavior is true in the
516   // current ABI.
517   if (isa<CXXRecordDecl>(RT->getDecl()))
518     return false;
519 
520   return isEmptyRecord(Context, FT, AllowArrays);
521 }
522 
523 /// isEmptyRecord - Return true iff a structure contains only empty
524 /// fields. Note that a structure with a flexible array member is not
525 /// considered empty.
526 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
527   const RecordType *RT = T->getAs<RecordType>();
528   if (!RT)
529     return false;
530   const RecordDecl *RD = RT->getDecl();
531   if (RD->hasFlexibleArrayMember())
532     return false;
533 
534   // If this is a C++ record, check the bases first.
535   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
536     for (const auto &I : CXXRD->bases())
537       if (!isEmptyRecord(Context, I.getType(), true))
538         return false;
539 
540   for (const auto *I : RD->fields())
541     if (!isEmptyField(Context, I, AllowArrays))
542       return false;
543   return true;
544 }
545 
546 /// isSingleElementStruct - Determine if a structure is a "single
547 /// element struct", i.e. it has exactly one non-empty field or
548 /// exactly one field which is itself a single element
549 /// struct. Structures with flexible array members are never
550 /// considered single element structs.
551 ///
552 /// \return The field declaration for the single non-empty field, if
553 /// it exists.
554 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
555   const RecordType *RT = T->getAs<RecordType>();
556   if (!RT)
557     return nullptr;
558 
559   const RecordDecl *RD = RT->getDecl();
560   if (RD->hasFlexibleArrayMember())
561     return nullptr;
562 
563   const Type *Found = nullptr;
564 
565   // If this is a C++ record, check the bases first.
566   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
567     for (const auto &I : CXXRD->bases()) {
568       // Ignore empty records.
569       if (isEmptyRecord(Context, I.getType(), true))
570         continue;
571 
572       // If we already found an element then this isn't a single-element struct.
573       if (Found)
574         return nullptr;
575 
576       // If this is non-empty and not a single element struct, the composite
577       // cannot be a single element struct.
578       Found = isSingleElementStruct(I.getType(), Context);
579       if (!Found)
580         return nullptr;
581     }
582   }
583 
584   // Check for single element.
585   for (const auto *FD : RD->fields()) {
586     QualType FT = FD->getType();
587 
588     // Ignore empty fields.
589     if (isEmptyField(Context, FD, true))
590       continue;
591 
592     // If we already found an element then this isn't a single-element
593     // struct.
594     if (Found)
595       return nullptr;
596 
597     // Treat single element arrays as the element.
598     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
599       if (AT->getSize().getZExtValue() != 1)
600         break;
601       FT = AT->getElementType();
602     }
603 
604     if (!isAggregateTypeForABI(FT)) {
605       Found = FT.getTypePtr();
606     } else {
607       Found = isSingleElementStruct(FT, Context);
608       if (!Found)
609         return nullptr;
610     }
611   }
612 
613   // We don't consider a struct a single-element struct if it has
614   // padding beyond the element type.
615   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
616     return nullptr;
617 
618   return Found;
619 }
620 
621 namespace {
622 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
623                        const ABIArgInfo &AI) {
624   // This default implementation defers to the llvm backend's va_arg
625   // instruction. It can handle only passing arguments directly
626   // (typically only handled in the backend for primitive types), or
627   // aggregates passed indirectly by pointer (NOTE: if the "byval"
628   // flag has ABI impact in the callee, this implementation cannot
629   // work.)
630 
631   // Only a few cases are covered here at the moment -- those needed
632   // by the default abi.
633   llvm::Value *Val;
634 
635   if (AI.isIndirect()) {
636     assert(!AI.getPaddingType() &&
637            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
638     assert(
639         !AI.getIndirectRealign() &&
640         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
641 
642     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
643     CharUnits TyAlignForABI = TyInfo.second;
644 
645     llvm::Type *BaseTy =
646         llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
647     llvm::Value *Addr =
648         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
649     return Address(Addr, TyAlignForABI);
650   } else {
651     assert((AI.isDirect() || AI.isExtend()) &&
652            "Unexpected ArgInfo Kind in generic VAArg emitter!");
653 
654     assert(!AI.getInReg() &&
655            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
656     assert(!AI.getPaddingType() &&
657            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
658     assert(!AI.getDirectOffset() &&
659            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
660     assert(!AI.getCoerceToType() &&
661            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
662 
663     Address Temp = CGF.CreateMemTemp(Ty, "varet");
664     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
665     CGF.Builder.CreateStore(Val, Temp);
666     return Temp;
667   }
668 }
669 
670 /// DefaultABIInfo - The default implementation for ABI specific
671 /// details. This implementation provides information which results in
672 /// self-consistent and sensible LLVM IR generation, but does not
673 /// conform to any particular ABI.
674 class DefaultABIInfo : public ABIInfo {
675 public:
676   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
677 
678   ABIArgInfo classifyReturnType(QualType RetTy) const;
679   ABIArgInfo classifyArgumentType(QualType RetTy) const;
680 
681   void computeInfo(CGFunctionInfo &FI) const override {
682     if (!getCXXABI().classifyReturnType(FI))
683       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
684     for (auto &I : FI.arguments())
685       I.info = classifyArgumentType(I.type);
686   }
687 
688   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
689                     QualType Ty) const override {
690     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
691   }
692 };
693 
694 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
695 public:
696   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
697       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
698 };
699 
700 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
701   Ty = useFirstFieldIfTransparentUnion(Ty);
702 
703   if (isAggregateTypeForABI(Ty)) {
704     // Records with non-trivial destructors/copy-constructors should not be
705     // passed by value.
706     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
707       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
708 
709     return getNaturalAlignIndirect(Ty);
710   }
711 
712   // Treat an enum type as its underlying type.
713   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
714     Ty = EnumTy->getDecl()->getIntegerType();
715 
716   ASTContext &Context = getContext();
717   if (const auto *EIT = Ty->getAs<ExtIntType>())
718     if (EIT->getNumBits() >
719         Context.getTypeSize(Context.getTargetInfo().hasInt128Type()
720                                 ? Context.Int128Ty
721                                 : Context.LongLongTy))
722       return getNaturalAlignIndirect(Ty);
723 
724   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
725                                             : ABIArgInfo::getDirect());
726 }
727 
728 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
729   if (RetTy->isVoidType())
730     return ABIArgInfo::getIgnore();
731 
732   if (isAggregateTypeForABI(RetTy))
733     return getNaturalAlignIndirect(RetTy);
734 
735   // Treat an enum type as its underlying type.
736   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
737     RetTy = EnumTy->getDecl()->getIntegerType();
738 
739   if (const auto *EIT = RetTy->getAs<ExtIntType>())
740     if (EIT->getNumBits() >
741         getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
742                                      ? getContext().Int128Ty
743                                      : getContext().LongLongTy))
744       return getNaturalAlignIndirect(RetTy);
745 
746   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
747                                                : ABIArgInfo::getDirect());
748 }
749 
750 //===----------------------------------------------------------------------===//
751 // WebAssembly ABI Implementation
752 //
753 // This is a very simple ABI that relies a lot on DefaultABIInfo.
754 //===----------------------------------------------------------------------===//
755 
756 class WebAssemblyABIInfo final : public SwiftABIInfo {
757 public:
758   enum ABIKind {
759     MVP = 0,
760     ExperimentalMV = 1,
761   };
762 
763 private:
764   DefaultABIInfo defaultInfo;
765   ABIKind Kind;
766 
767 public:
768   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
769       : SwiftABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {}
770 
771 private:
772   ABIArgInfo classifyReturnType(QualType RetTy) const;
773   ABIArgInfo classifyArgumentType(QualType Ty) const;
774 
775   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
776   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
777   // overload them.
778   void computeInfo(CGFunctionInfo &FI) const override {
779     if (!getCXXABI().classifyReturnType(FI))
780       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
781     for (auto &Arg : FI.arguments())
782       Arg.info = classifyArgumentType(Arg.type);
783   }
784 
785   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
786                     QualType Ty) const override;
787 
788   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
789                                     bool asReturnValue) const override {
790     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
791   }
792 
793   bool isSwiftErrorInRegister() const override {
794     return false;
795   }
796 };
797 
798 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
799 public:
800   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
801                                         WebAssemblyABIInfo::ABIKind K)
802       : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {}
803 
804   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
805                            CodeGen::CodeGenModule &CGM) const override {
806     TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
807     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
808       if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
809         llvm::Function *Fn = cast<llvm::Function>(GV);
810         llvm::AttrBuilder B;
811         B.addAttribute("wasm-import-module", Attr->getImportModule());
812         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
813       }
814       if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
815         llvm::Function *Fn = cast<llvm::Function>(GV);
816         llvm::AttrBuilder B;
817         B.addAttribute("wasm-import-name", Attr->getImportName());
818         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
819       }
820       if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
821         llvm::Function *Fn = cast<llvm::Function>(GV);
822         llvm::AttrBuilder B;
823         B.addAttribute("wasm-export-name", Attr->getExportName());
824         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
825       }
826     }
827 
828     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
829       llvm::Function *Fn = cast<llvm::Function>(GV);
830       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
831         Fn->addFnAttr("no-prototype");
832     }
833   }
834 };
835 
836 /// Classify argument of given type \p Ty.
837 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
838   Ty = useFirstFieldIfTransparentUnion(Ty);
839 
840   if (isAggregateTypeForABI(Ty)) {
841     // Records with non-trivial destructors/copy-constructors should not be
842     // passed by value.
843     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
844       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
845     // Ignore empty structs/unions.
846     if (isEmptyRecord(getContext(), Ty, true))
847       return ABIArgInfo::getIgnore();
848     // Lower single-element structs to just pass a regular value. TODO: We
849     // could do reasonable-size multiple-element structs too, using getExpand(),
850     // though watch out for things like bitfields.
851     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
852       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
853     // For the experimental multivalue ABI, fully expand all other aggregates
854     if (Kind == ABIKind::ExperimentalMV) {
855       const RecordType *RT = Ty->getAs<RecordType>();
856       assert(RT);
857       bool HasBitField = false;
858       for (auto *Field : RT->getDecl()->fields()) {
859         if (Field->isBitField()) {
860           HasBitField = true;
861           break;
862         }
863       }
864       if (!HasBitField)
865         return ABIArgInfo::getExpand();
866     }
867   }
868 
869   // Otherwise just do the default thing.
870   return defaultInfo.classifyArgumentType(Ty);
871 }
872 
873 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
874   if (isAggregateTypeForABI(RetTy)) {
875     // Records with non-trivial destructors/copy-constructors should not be
876     // returned by value.
877     if (!getRecordArgABI(RetTy, getCXXABI())) {
878       // Ignore empty structs/unions.
879       if (isEmptyRecord(getContext(), RetTy, true))
880         return ABIArgInfo::getIgnore();
881       // Lower single-element structs to just return a regular value. TODO: We
882       // could do reasonable-size multiple-element structs too, using
883       // ABIArgInfo::getDirect().
884       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
885         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
886       // For the experimental multivalue ABI, return all other aggregates
887       if (Kind == ABIKind::ExperimentalMV)
888         return ABIArgInfo::getDirect();
889     }
890   }
891 
892   // Otherwise just do the default thing.
893   return defaultInfo.classifyReturnType(RetTy);
894 }
895 
896 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
897                                       QualType Ty) const {
898   bool IsIndirect = isAggregateTypeForABI(Ty) &&
899                     !isEmptyRecord(getContext(), Ty, true) &&
900                     !isSingleElementStruct(Ty, getContext());
901   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
902                           getContext().getTypeInfoInChars(Ty),
903                           CharUnits::fromQuantity(4),
904                           /*AllowHigherAlign=*/true);
905 }
906 
907 //===----------------------------------------------------------------------===//
908 // le32/PNaCl bitcode ABI Implementation
909 //
910 // This is a simplified version of the x86_32 ABI.  Arguments and return values
911 // are always passed on the stack.
912 //===----------------------------------------------------------------------===//
913 
914 class PNaClABIInfo : public ABIInfo {
915  public:
916   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
917 
918   ABIArgInfo classifyReturnType(QualType RetTy) const;
919   ABIArgInfo classifyArgumentType(QualType RetTy) const;
920 
921   void computeInfo(CGFunctionInfo &FI) const override;
922   Address EmitVAArg(CodeGenFunction &CGF,
923                     Address VAListAddr, QualType Ty) const override;
924 };
925 
926 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
927  public:
928    PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
929        : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {}
930 };
931 
932 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
933   if (!getCXXABI().classifyReturnType(FI))
934     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
935 
936   for (auto &I : FI.arguments())
937     I.info = classifyArgumentType(I.type);
938 }
939 
940 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
941                                 QualType Ty) const {
942   // The PNaCL ABI is a bit odd, in that varargs don't use normal
943   // function classification. Structs get passed directly for varargs
944   // functions, through a rewriting transform in
945   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
946   // this target to actually support a va_arg instructions with an
947   // aggregate type, unlike other targets.
948   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
949 }
950 
951 /// Classify argument of given type \p Ty.
952 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
953   if (isAggregateTypeForABI(Ty)) {
954     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
955       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
956     return getNaturalAlignIndirect(Ty);
957   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
958     // Treat an enum type as its underlying type.
959     Ty = EnumTy->getDecl()->getIntegerType();
960   } else if (Ty->isFloatingType()) {
961     // Floating-point types don't go inreg.
962     return ABIArgInfo::getDirect();
963   } else if (const auto *EIT = Ty->getAs<ExtIntType>()) {
964     // Treat extended integers as integers if <=64, otherwise pass indirectly.
965     if (EIT->getNumBits() > 64)
966       return getNaturalAlignIndirect(Ty);
967     return ABIArgInfo::getDirect();
968   }
969 
970   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
971                                             : ABIArgInfo::getDirect());
972 }
973 
974 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
975   if (RetTy->isVoidType())
976     return ABIArgInfo::getIgnore();
977 
978   // In the PNaCl ABI we always return records/structures on the stack.
979   if (isAggregateTypeForABI(RetTy))
980     return getNaturalAlignIndirect(RetTy);
981 
982   // Treat extended integers as integers if <=64, otherwise pass indirectly.
983   if (const auto *EIT = RetTy->getAs<ExtIntType>()) {
984     if (EIT->getNumBits() > 64)
985       return getNaturalAlignIndirect(RetTy);
986     return ABIArgInfo::getDirect();
987   }
988 
989   // Treat an enum type as its underlying type.
990   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
991     RetTy = EnumTy->getDecl()->getIntegerType();
992 
993   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
994                                                : ABIArgInfo::getDirect());
995 }
996 
997 /// IsX86_MMXType - Return true if this is an MMX type.
998 bool IsX86_MMXType(llvm::Type *IRType) {
999   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
1000   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
1001     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
1002     IRType->getScalarSizeInBits() != 64;
1003 }
1004 
1005 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1006                                           StringRef Constraint,
1007                                           llvm::Type* Ty) {
1008   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
1009                      .Cases("y", "&y", "^Ym", true)
1010                      .Default(false);
1011   if (IsMMXCons && Ty->isVectorTy()) {
1012     if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedSize() !=
1013         64) {
1014       // Invalid MMX constraint
1015       return nullptr;
1016     }
1017 
1018     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
1019   }
1020 
1021   // No operation needed
1022   return Ty;
1023 }
1024 
1025 /// Returns true if this type can be passed in SSE registers with the
1026 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1027 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
1028   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1029     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
1030       if (BT->getKind() == BuiltinType::LongDouble) {
1031         if (&Context.getTargetInfo().getLongDoubleFormat() ==
1032             &llvm::APFloat::x87DoubleExtended())
1033           return false;
1034       }
1035       return true;
1036     }
1037   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
1038     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
1039     // registers specially.
1040     unsigned VecSize = Context.getTypeSize(VT);
1041     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
1042       return true;
1043   }
1044   return false;
1045 }
1046 
1047 /// Returns true if this aggregate is small enough to be passed in SSE registers
1048 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1049 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
1050   return NumMembers <= 4;
1051 }
1052 
1053 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
1054 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
1055   auto AI = ABIArgInfo::getDirect(T);
1056   AI.setInReg(true);
1057   AI.setCanBeFlattened(false);
1058   return AI;
1059 }
1060 
1061 //===----------------------------------------------------------------------===//
1062 // X86-32 ABI Implementation
1063 //===----------------------------------------------------------------------===//
1064 
1065 /// Similar to llvm::CCState, but for Clang.
1066 struct CCState {
1067   CCState(CGFunctionInfo &FI)
1068       : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {}
1069 
1070   llvm::SmallBitVector IsPreassigned;
1071   unsigned CC = CallingConv::CC_C;
1072   unsigned FreeRegs = 0;
1073   unsigned FreeSSERegs = 0;
1074 };
1075 
1076 enum {
1077   // Vectorcall only allows the first 6 parameters to be passed in registers.
1078   VectorcallMaxParamNumAsReg = 6
1079 };
1080 
1081 /// X86_32ABIInfo - The X86-32 ABI information.
1082 class X86_32ABIInfo : public SwiftABIInfo {
1083   enum Class {
1084     Integer,
1085     Float
1086   };
1087 
1088   static const unsigned MinABIStackAlignInBytes = 4;
1089 
1090   bool IsDarwinVectorABI;
1091   bool IsRetSmallStructInRegABI;
1092   bool IsWin32StructABI;
1093   bool IsSoftFloatABI;
1094   bool IsMCUABI;
1095   unsigned DefaultNumRegisterParameters;
1096 
1097   static bool isRegisterSize(unsigned Size) {
1098     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
1099   }
1100 
1101   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1102     // FIXME: Assumes vectorcall is in use.
1103     return isX86VectorTypeForVectorCall(getContext(), Ty);
1104   }
1105 
1106   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1107                                          uint64_t NumMembers) const override {
1108     // FIXME: Assumes vectorcall is in use.
1109     return isX86VectorCallAggregateSmallEnough(NumMembers);
1110   }
1111 
1112   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
1113 
1114   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1115   /// such that the argument will be passed in memory.
1116   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
1117 
1118   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
1119 
1120   /// Return the alignment to use for the given type on the stack.
1121   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
1122 
1123   Class classify(QualType Ty) const;
1124   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
1125   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
1126 
1127   /// Updates the number of available free registers, returns
1128   /// true if any registers were allocated.
1129   bool updateFreeRegs(QualType Ty, CCState &State) const;
1130 
1131   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1132                                 bool &NeedsPadding) const;
1133   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1134 
1135   bool canExpandIndirectArgument(QualType Ty) const;
1136 
1137   /// Rewrite the function info so that all memory arguments use
1138   /// inalloca.
1139   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1140 
1141   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1142                            CharUnits &StackOffset, ABIArgInfo &Info,
1143                            QualType Type) const;
1144   void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const;
1145 
1146 public:
1147 
1148   void computeInfo(CGFunctionInfo &FI) const override;
1149   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1150                     QualType Ty) const override;
1151 
1152   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1153                 bool RetSmallStructInRegABI, bool Win32StructABI,
1154                 unsigned NumRegisterParameters, bool SoftFloatABI)
1155     : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1156       IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1157       IsWin32StructABI(Win32StructABI),
1158       IsSoftFloatABI(SoftFloatABI),
1159       IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1160       DefaultNumRegisterParameters(NumRegisterParameters) {}
1161 
1162   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
1163                                     bool asReturnValue) const override {
1164     // LLVM's x86-32 lowering currently only assigns up to three
1165     // integer registers and three fp registers.  Oddly, it'll use up to
1166     // four vector registers for vectors, but those can overlap with the
1167     // scalar registers.
1168     return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1169   }
1170 
1171   bool isSwiftErrorInRegister() const override {
1172     // x86-32 lowering does not support passing swifterror in a register.
1173     return false;
1174   }
1175 };
1176 
1177 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1178 public:
1179   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1180                           bool RetSmallStructInRegABI, bool Win32StructABI,
1181                           unsigned NumRegisterParameters, bool SoftFloatABI)
1182       : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>(
1183             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1184             NumRegisterParameters, SoftFloatABI)) {}
1185 
1186   static bool isStructReturnInRegABI(
1187       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1188 
1189   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1190                            CodeGen::CodeGenModule &CGM) const override;
1191 
1192   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1193     // Darwin uses different dwarf register numbers for EH.
1194     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1195     return 4;
1196   }
1197 
1198   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1199                                llvm::Value *Address) const override;
1200 
1201   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1202                                   StringRef Constraint,
1203                                   llvm::Type* Ty) const override {
1204     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1205   }
1206 
1207   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1208                                 std::string &Constraints,
1209                                 std::vector<llvm::Type *> &ResultRegTypes,
1210                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1211                                 std::vector<LValue> &ResultRegDests,
1212                                 std::string &AsmString,
1213                                 unsigned NumOutputs) const override;
1214 
1215   llvm::Constant *
1216   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1217     unsigned Sig = (0xeb << 0) |  // jmp rel8
1218                    (0x06 << 8) |  //           .+0x08
1219                    ('v' << 16) |
1220                    ('2' << 24);
1221     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1222   }
1223 
1224   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1225     return "movl\t%ebp, %ebp"
1226            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1227   }
1228 };
1229 
1230 }
1231 
1232 /// Rewrite input constraint references after adding some output constraints.
1233 /// In the case where there is one output and one input and we add one output,
1234 /// we need to replace all operand references greater than or equal to 1:
1235 ///     mov $0, $1
1236 ///     mov eax, $1
1237 /// The result will be:
1238 ///     mov $0, $2
1239 ///     mov eax, $2
1240 static void rewriteInputConstraintReferences(unsigned FirstIn,
1241                                              unsigned NumNewOuts,
1242                                              std::string &AsmString) {
1243   std::string Buf;
1244   llvm::raw_string_ostream OS(Buf);
1245   size_t Pos = 0;
1246   while (Pos < AsmString.size()) {
1247     size_t DollarStart = AsmString.find('$', Pos);
1248     if (DollarStart == std::string::npos)
1249       DollarStart = AsmString.size();
1250     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1251     if (DollarEnd == std::string::npos)
1252       DollarEnd = AsmString.size();
1253     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1254     Pos = DollarEnd;
1255     size_t NumDollars = DollarEnd - DollarStart;
1256     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1257       // We have an operand reference.
1258       size_t DigitStart = Pos;
1259       if (AsmString[DigitStart] == '{') {
1260         OS << '{';
1261         ++DigitStart;
1262       }
1263       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1264       if (DigitEnd == std::string::npos)
1265         DigitEnd = AsmString.size();
1266       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1267       unsigned OperandIndex;
1268       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1269         if (OperandIndex >= FirstIn)
1270           OperandIndex += NumNewOuts;
1271         OS << OperandIndex;
1272       } else {
1273         OS << OperandStr;
1274       }
1275       Pos = DigitEnd;
1276     }
1277   }
1278   AsmString = std::move(OS.str());
1279 }
1280 
1281 /// Add output constraints for EAX:EDX because they are return registers.
1282 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1283     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1284     std::vector<llvm::Type *> &ResultRegTypes,
1285     std::vector<llvm::Type *> &ResultTruncRegTypes,
1286     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1287     unsigned NumOutputs) const {
1288   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1289 
1290   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1291   // larger.
1292   if (!Constraints.empty())
1293     Constraints += ',';
1294   if (RetWidth <= 32) {
1295     Constraints += "={eax}";
1296     ResultRegTypes.push_back(CGF.Int32Ty);
1297   } else {
1298     // Use the 'A' constraint for EAX:EDX.
1299     Constraints += "=A";
1300     ResultRegTypes.push_back(CGF.Int64Ty);
1301   }
1302 
1303   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1304   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1305   ResultTruncRegTypes.push_back(CoerceTy);
1306 
1307   // Coerce the integer by bitcasting the return slot pointer.
1308   ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF),
1309                                                   CoerceTy->getPointerTo()));
1310   ResultRegDests.push_back(ReturnSlot);
1311 
1312   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1313 }
1314 
1315 /// shouldReturnTypeInRegister - Determine if the given type should be
1316 /// returned in a register (for the Darwin and MCU ABI).
1317 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1318                                                ASTContext &Context) const {
1319   uint64_t Size = Context.getTypeSize(Ty);
1320 
1321   // For i386, type must be register sized.
1322   // For the MCU ABI, it only needs to be <= 8-byte
1323   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1324    return false;
1325 
1326   if (Ty->isVectorType()) {
1327     // 64- and 128- bit vectors inside structures are not returned in
1328     // registers.
1329     if (Size == 64 || Size == 128)
1330       return false;
1331 
1332     return true;
1333   }
1334 
1335   // If this is a builtin, pointer, enum, complex type, member pointer, or
1336   // member function pointer it is ok.
1337   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1338       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1339       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1340     return true;
1341 
1342   // Arrays are treated like records.
1343   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1344     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1345 
1346   // Otherwise, it must be a record type.
1347   const RecordType *RT = Ty->getAs<RecordType>();
1348   if (!RT) return false;
1349 
1350   // FIXME: Traverse bases here too.
1351 
1352   // Structure types are passed in register if all fields would be
1353   // passed in a register.
1354   for (const auto *FD : RT->getDecl()->fields()) {
1355     // Empty fields are ignored.
1356     if (isEmptyField(Context, FD, true))
1357       continue;
1358 
1359     // Check fields recursively.
1360     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1361       return false;
1362   }
1363   return true;
1364 }
1365 
1366 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1367   // Treat complex types as the element type.
1368   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1369     Ty = CTy->getElementType();
1370 
1371   // Check for a type which we know has a simple scalar argument-passing
1372   // convention without any padding.  (We're specifically looking for 32
1373   // and 64-bit integer and integer-equivalents, float, and double.)
1374   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1375       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1376     return false;
1377 
1378   uint64_t Size = Context.getTypeSize(Ty);
1379   return Size == 32 || Size == 64;
1380 }
1381 
1382 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1383                           uint64_t &Size) {
1384   for (const auto *FD : RD->fields()) {
1385     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1386     // argument is smaller than 32-bits, expanding the struct will create
1387     // alignment padding.
1388     if (!is32Or64BitBasicType(FD->getType(), Context))
1389       return false;
1390 
1391     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1392     // how to expand them yet, and the predicate for telling if a bitfield still
1393     // counts as "basic" is more complicated than what we were doing previously.
1394     if (FD->isBitField())
1395       return false;
1396 
1397     Size += Context.getTypeSize(FD->getType());
1398   }
1399   return true;
1400 }
1401 
1402 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1403                                  uint64_t &Size) {
1404   // Don't do this if there are any non-empty bases.
1405   for (const CXXBaseSpecifier &Base : RD->bases()) {
1406     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1407                               Size))
1408       return false;
1409   }
1410   if (!addFieldSizes(Context, RD, Size))
1411     return false;
1412   return true;
1413 }
1414 
1415 /// Test whether an argument type which is to be passed indirectly (on the
1416 /// stack) would have the equivalent layout if it was expanded into separate
1417 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1418 /// optimizations.
1419 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1420   // We can only expand structure types.
1421   const RecordType *RT = Ty->getAs<RecordType>();
1422   if (!RT)
1423     return false;
1424   const RecordDecl *RD = RT->getDecl();
1425   uint64_t Size = 0;
1426   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1427     if (!IsWin32StructABI) {
1428       // On non-Windows, we have to conservatively match our old bitcode
1429       // prototypes in order to be ABI-compatible at the bitcode level.
1430       if (!CXXRD->isCLike())
1431         return false;
1432     } else {
1433       // Don't do this for dynamic classes.
1434       if (CXXRD->isDynamicClass())
1435         return false;
1436     }
1437     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1438       return false;
1439   } else {
1440     if (!addFieldSizes(getContext(), RD, Size))
1441       return false;
1442   }
1443 
1444   // We can do this if there was no alignment padding.
1445   return Size == getContext().getTypeSize(Ty);
1446 }
1447 
1448 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1449   // If the return value is indirect, then the hidden argument is consuming one
1450   // integer register.
1451   if (State.FreeRegs) {
1452     --State.FreeRegs;
1453     if (!IsMCUABI)
1454       return getNaturalAlignIndirectInReg(RetTy);
1455   }
1456   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1457 }
1458 
1459 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1460                                              CCState &State) const {
1461   if (RetTy->isVoidType())
1462     return ABIArgInfo::getIgnore();
1463 
1464   const Type *Base = nullptr;
1465   uint64_t NumElts = 0;
1466   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1467        State.CC == llvm::CallingConv::X86_RegCall) &&
1468       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1469     // The LLVM struct type for such an aggregate should lower properly.
1470     return ABIArgInfo::getDirect();
1471   }
1472 
1473   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1474     // On Darwin, some vectors are returned in registers.
1475     if (IsDarwinVectorABI) {
1476       uint64_t Size = getContext().getTypeSize(RetTy);
1477 
1478       // 128-bit vectors are a special case; they are returned in
1479       // registers and we need to make sure to pick a type the LLVM
1480       // backend will like.
1481       if (Size == 128)
1482         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1483             llvm::Type::getInt64Ty(getVMContext()), 2));
1484 
1485       // Always return in register if it fits in a general purpose
1486       // register, or if it is 64 bits and has a single element.
1487       if ((Size == 8 || Size == 16 || Size == 32) ||
1488           (Size == 64 && VT->getNumElements() == 1))
1489         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1490                                                             Size));
1491 
1492       return getIndirectReturnResult(RetTy, State);
1493     }
1494 
1495     return ABIArgInfo::getDirect();
1496   }
1497 
1498   if (isAggregateTypeForABI(RetTy)) {
1499     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1500       // Structures with flexible arrays are always indirect.
1501       if (RT->getDecl()->hasFlexibleArrayMember())
1502         return getIndirectReturnResult(RetTy, State);
1503     }
1504 
1505     // If specified, structs and unions are always indirect.
1506     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1507       return getIndirectReturnResult(RetTy, State);
1508 
1509     // Ignore empty structs/unions.
1510     if (isEmptyRecord(getContext(), RetTy, true))
1511       return ABIArgInfo::getIgnore();
1512 
1513     // Small structures which are register sized are generally returned
1514     // in a register.
1515     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1516       uint64_t Size = getContext().getTypeSize(RetTy);
1517 
1518       // As a special-case, if the struct is a "single-element" struct, and
1519       // the field is of type "float" or "double", return it in a
1520       // floating-point register. (MSVC does not apply this special case.)
1521       // We apply a similar transformation for pointer types to improve the
1522       // quality of the generated IR.
1523       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1524         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1525             || SeltTy->hasPointerRepresentation())
1526           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1527 
1528       // FIXME: We should be able to narrow this integer in cases with dead
1529       // padding.
1530       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1531     }
1532 
1533     return getIndirectReturnResult(RetTy, State);
1534   }
1535 
1536   // Treat an enum type as its underlying type.
1537   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1538     RetTy = EnumTy->getDecl()->getIntegerType();
1539 
1540   if (const auto *EIT = RetTy->getAs<ExtIntType>())
1541     if (EIT->getNumBits() > 64)
1542       return getIndirectReturnResult(RetTy, State);
1543 
1544   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1545                                                : ABIArgInfo::getDirect());
1546 }
1547 
1548 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) {
1549   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1550 }
1551 
1552 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) {
1553   const RecordType *RT = Ty->getAs<RecordType>();
1554   if (!RT)
1555     return 0;
1556   const RecordDecl *RD = RT->getDecl();
1557 
1558   // If this is a C++ record, check the bases first.
1559   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1560     for (const auto &I : CXXRD->bases())
1561       if (!isRecordWithSIMDVectorType(Context, I.getType()))
1562         return false;
1563 
1564   for (const auto *i : RD->fields()) {
1565     QualType FT = i->getType();
1566 
1567     if (isSIMDVectorType(Context, FT))
1568       return true;
1569 
1570     if (isRecordWithSIMDVectorType(Context, FT))
1571       return true;
1572   }
1573 
1574   return false;
1575 }
1576 
1577 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1578                                                  unsigned Align) const {
1579   // Otherwise, if the alignment is less than or equal to the minimum ABI
1580   // alignment, just use the default; the backend will handle this.
1581   if (Align <= MinABIStackAlignInBytes)
1582     return 0; // Use default alignment.
1583 
1584   // On non-Darwin, the stack type alignment is always 4.
1585   if (!IsDarwinVectorABI) {
1586     // Set explicit alignment, since we may need to realign the top.
1587     return MinABIStackAlignInBytes;
1588   }
1589 
1590   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1591   if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) ||
1592                       isRecordWithSIMDVectorType(getContext(), Ty)))
1593     return 16;
1594 
1595   return MinABIStackAlignInBytes;
1596 }
1597 
1598 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1599                                             CCState &State) const {
1600   if (!ByVal) {
1601     if (State.FreeRegs) {
1602       --State.FreeRegs; // Non-byval indirects just use one pointer.
1603       if (!IsMCUABI)
1604         return getNaturalAlignIndirectInReg(Ty);
1605     }
1606     return getNaturalAlignIndirect(Ty, false);
1607   }
1608 
1609   // Compute the byval alignment.
1610   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1611   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1612   if (StackAlign == 0)
1613     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1614 
1615   // If the stack alignment is less than the type alignment, realign the
1616   // argument.
1617   bool Realign = TypeAlign > StackAlign;
1618   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1619                                  /*ByVal=*/true, Realign);
1620 }
1621 
1622 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1623   const Type *T = isSingleElementStruct(Ty, getContext());
1624   if (!T)
1625     T = Ty.getTypePtr();
1626 
1627   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1628     BuiltinType::Kind K = BT->getKind();
1629     if (K == BuiltinType::Float || K == BuiltinType::Double)
1630       return Float;
1631   }
1632   return Integer;
1633 }
1634 
1635 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1636   if (!IsSoftFloatABI) {
1637     Class C = classify(Ty);
1638     if (C == Float)
1639       return false;
1640   }
1641 
1642   unsigned Size = getContext().getTypeSize(Ty);
1643   unsigned SizeInRegs = (Size + 31) / 32;
1644 
1645   if (SizeInRegs == 0)
1646     return false;
1647 
1648   if (!IsMCUABI) {
1649     if (SizeInRegs > State.FreeRegs) {
1650       State.FreeRegs = 0;
1651       return false;
1652     }
1653   } else {
1654     // The MCU psABI allows passing parameters in-reg even if there are
1655     // earlier parameters that are passed on the stack. Also,
1656     // it does not allow passing >8-byte structs in-register,
1657     // even if there are 3 free registers available.
1658     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1659       return false;
1660   }
1661 
1662   State.FreeRegs -= SizeInRegs;
1663   return true;
1664 }
1665 
1666 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1667                                              bool &InReg,
1668                                              bool &NeedsPadding) const {
1669   // On Windows, aggregates other than HFAs are never passed in registers, and
1670   // they do not consume register slots. Homogenous floating-point aggregates
1671   // (HFAs) have already been dealt with at this point.
1672   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1673     return false;
1674 
1675   NeedsPadding = false;
1676   InReg = !IsMCUABI;
1677 
1678   if (!updateFreeRegs(Ty, State))
1679     return false;
1680 
1681   if (IsMCUABI)
1682     return true;
1683 
1684   if (State.CC == llvm::CallingConv::X86_FastCall ||
1685       State.CC == llvm::CallingConv::X86_VectorCall ||
1686       State.CC == llvm::CallingConv::X86_RegCall) {
1687     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1688       NeedsPadding = true;
1689 
1690     return false;
1691   }
1692 
1693   return true;
1694 }
1695 
1696 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1697   if (!updateFreeRegs(Ty, State))
1698     return false;
1699 
1700   if (IsMCUABI)
1701     return false;
1702 
1703   if (State.CC == llvm::CallingConv::X86_FastCall ||
1704       State.CC == llvm::CallingConv::X86_VectorCall ||
1705       State.CC == llvm::CallingConv::X86_RegCall) {
1706     if (getContext().getTypeSize(Ty) > 32)
1707       return false;
1708 
1709     return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1710         Ty->isReferenceType());
1711   }
1712 
1713   return true;
1714 }
1715 
1716 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const {
1717   // Vectorcall x86 works subtly different than in x64, so the format is
1718   // a bit different than the x64 version.  First, all vector types (not HVAs)
1719   // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers.
1720   // This differs from the x64 implementation, where the first 6 by INDEX get
1721   // registers.
1722   // In the second pass over the arguments, HVAs are passed in the remaining
1723   // vector registers if possible, or indirectly by address. The address will be
1724   // passed in ECX/EDX if available. Any other arguments are passed according to
1725   // the usual fastcall rules.
1726   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1727   for (int I = 0, E = Args.size(); I < E; ++I) {
1728     const Type *Base = nullptr;
1729     uint64_t NumElts = 0;
1730     const QualType &Ty = Args[I].type;
1731     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1732         isHomogeneousAggregate(Ty, Base, NumElts)) {
1733       if (State.FreeSSERegs >= NumElts) {
1734         State.FreeSSERegs -= NumElts;
1735         Args[I].info = ABIArgInfo::getDirectInReg();
1736         State.IsPreassigned.set(I);
1737       }
1738     }
1739   }
1740 }
1741 
1742 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1743                                                CCState &State) const {
1744   // FIXME: Set alignment on indirect arguments.
1745   bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall;
1746   bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall;
1747   bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall;
1748 
1749   Ty = useFirstFieldIfTransparentUnion(Ty);
1750   TypeInfo TI = getContext().getTypeInfo(Ty);
1751 
1752   // Check with the C++ ABI first.
1753   const RecordType *RT = Ty->getAs<RecordType>();
1754   if (RT) {
1755     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1756     if (RAA == CGCXXABI::RAA_Indirect) {
1757       return getIndirectResult(Ty, false, State);
1758     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1759       // The field index doesn't matter, we'll fix it up later.
1760       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1761     }
1762   }
1763 
1764   // Regcall uses the concept of a homogenous vector aggregate, similar
1765   // to other targets.
1766   const Type *Base = nullptr;
1767   uint64_t NumElts = 0;
1768   if ((IsRegCall || IsVectorCall) &&
1769       isHomogeneousAggregate(Ty, Base, NumElts)) {
1770     if (State.FreeSSERegs >= NumElts) {
1771       State.FreeSSERegs -= NumElts;
1772 
1773       // Vectorcall passes HVAs directly and does not flatten them, but regcall
1774       // does.
1775       if (IsVectorCall)
1776         return getDirectX86Hva();
1777 
1778       if (Ty->isBuiltinType() || Ty->isVectorType())
1779         return ABIArgInfo::getDirect();
1780       return ABIArgInfo::getExpand();
1781     }
1782     return getIndirectResult(Ty, /*ByVal=*/false, State);
1783   }
1784 
1785   if (isAggregateTypeForABI(Ty)) {
1786     // Structures with flexible arrays are always indirect.
1787     // FIXME: This should not be byval!
1788     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1789       return getIndirectResult(Ty, true, State);
1790 
1791     // Ignore empty structs/unions on non-Windows.
1792     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1793       return ABIArgInfo::getIgnore();
1794 
1795     llvm::LLVMContext &LLVMContext = getVMContext();
1796     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1797     bool NeedsPadding = false;
1798     bool InReg;
1799     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1800       unsigned SizeInRegs = (TI.Width + 31) / 32;
1801       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1802       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1803       if (InReg)
1804         return ABIArgInfo::getDirectInReg(Result);
1805       else
1806         return ABIArgInfo::getDirect(Result);
1807     }
1808     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1809 
1810     // Pass over-aligned aggregates on Windows indirectly. This behavior was
1811     // added in MSVC 2015.
1812     if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32)
1813       return getIndirectResult(Ty, /*ByVal=*/false, State);
1814 
1815     // Expand small (<= 128-bit) record types when we know that the stack layout
1816     // of those arguments will match the struct. This is important because the
1817     // LLVM backend isn't smart enough to remove byval, which inhibits many
1818     // optimizations.
1819     // Don't do this for the MCU if there are still free integer registers
1820     // (see X86_64 ABI for full explanation).
1821     if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) &&
1822         canExpandIndirectArgument(Ty))
1823       return ABIArgInfo::getExpandWithPadding(
1824           IsFastCall || IsVectorCall || IsRegCall, PaddingType);
1825 
1826     return getIndirectResult(Ty, true, State);
1827   }
1828 
1829   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1830     // On Windows, vectors are passed directly if registers are available, or
1831     // indirectly if not. This avoids the need to align argument memory. Pass
1832     // user-defined vector types larger than 512 bits indirectly for simplicity.
1833     if (IsWin32StructABI) {
1834       if (TI.Width <= 512 && State.FreeSSERegs > 0) {
1835         --State.FreeSSERegs;
1836         return ABIArgInfo::getDirectInReg();
1837       }
1838       return getIndirectResult(Ty, /*ByVal=*/false, State);
1839     }
1840 
1841     // On Darwin, some vectors are passed in memory, we handle this by passing
1842     // it as an i8/i16/i32/i64.
1843     if (IsDarwinVectorABI) {
1844       if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) ||
1845           (TI.Width == 64 && VT->getNumElements() == 1))
1846         return ABIArgInfo::getDirect(
1847             llvm::IntegerType::get(getVMContext(), TI.Width));
1848     }
1849 
1850     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1851       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1852 
1853     return ABIArgInfo::getDirect();
1854   }
1855 
1856 
1857   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1858     Ty = EnumTy->getDecl()->getIntegerType();
1859 
1860   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1861 
1862   if (isPromotableIntegerTypeForABI(Ty)) {
1863     if (InReg)
1864       return ABIArgInfo::getExtendInReg(Ty);
1865     return ABIArgInfo::getExtend(Ty);
1866   }
1867 
1868   if (const auto * EIT = Ty->getAs<ExtIntType>()) {
1869     if (EIT->getNumBits() <= 64) {
1870       if (InReg)
1871         return ABIArgInfo::getDirectInReg();
1872       return ABIArgInfo::getDirect();
1873     }
1874     return getIndirectResult(Ty, /*ByVal=*/false, State);
1875   }
1876 
1877   if (InReg)
1878     return ABIArgInfo::getDirectInReg();
1879   return ABIArgInfo::getDirect();
1880 }
1881 
1882 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1883   CCState State(FI);
1884   if (IsMCUABI)
1885     State.FreeRegs = 3;
1886   else if (State.CC == llvm::CallingConv::X86_FastCall) {
1887     State.FreeRegs = 2;
1888     State.FreeSSERegs = 3;
1889   } else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1890     State.FreeRegs = 2;
1891     State.FreeSSERegs = 6;
1892   } else if (FI.getHasRegParm())
1893     State.FreeRegs = FI.getRegParm();
1894   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1895     State.FreeRegs = 5;
1896     State.FreeSSERegs = 8;
1897   } else if (IsWin32StructABI) {
1898     // Since MSVC 2015, the first three SSE vectors have been passed in
1899     // registers. The rest are passed indirectly.
1900     State.FreeRegs = DefaultNumRegisterParameters;
1901     State.FreeSSERegs = 3;
1902   } else
1903     State.FreeRegs = DefaultNumRegisterParameters;
1904 
1905   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
1906     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1907   } else if (FI.getReturnInfo().isIndirect()) {
1908     // The C++ ABI is not aware of register usage, so we have to check if the
1909     // return value was sret and put it in a register ourselves if appropriate.
1910     if (State.FreeRegs) {
1911       --State.FreeRegs;  // The sret parameter consumes a register.
1912       if (!IsMCUABI)
1913         FI.getReturnInfo().setInReg(true);
1914     }
1915   }
1916 
1917   // The chain argument effectively gives us another free register.
1918   if (FI.isChainCall())
1919     ++State.FreeRegs;
1920 
1921   // For vectorcall, do a first pass over the arguments, assigning FP and vector
1922   // arguments to XMM registers as available.
1923   if (State.CC == llvm::CallingConv::X86_VectorCall)
1924     runVectorCallFirstPass(FI, State);
1925 
1926   bool UsedInAlloca = false;
1927   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1928   for (int I = 0, E = Args.size(); I < E; ++I) {
1929     // Skip arguments that have already been assigned.
1930     if (State.IsPreassigned.test(I))
1931       continue;
1932 
1933     Args[I].info = classifyArgumentType(Args[I].type, State);
1934     UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca);
1935   }
1936 
1937   // If we needed to use inalloca for any argument, do a second pass and rewrite
1938   // all the memory arguments to use inalloca.
1939   if (UsedInAlloca)
1940     rewriteWithInAlloca(FI);
1941 }
1942 
1943 void
1944 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1945                                    CharUnits &StackOffset, ABIArgInfo &Info,
1946                                    QualType Type) const {
1947   // Arguments are always 4-byte-aligned.
1948   CharUnits WordSize = CharUnits::fromQuantity(4);
1949   assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct");
1950 
1951   // sret pointers and indirect things will require an extra pointer
1952   // indirection, unless they are byval. Most things are byval, and will not
1953   // require this indirection.
1954   bool IsIndirect = false;
1955   if (Info.isIndirect() && !Info.getIndirectByVal())
1956     IsIndirect = true;
1957   Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect);
1958   llvm::Type *LLTy = CGT.ConvertTypeForMem(Type);
1959   if (IsIndirect)
1960     LLTy = LLTy->getPointerTo(0);
1961   FrameFields.push_back(LLTy);
1962   StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type);
1963 
1964   // Insert padding bytes to respect alignment.
1965   CharUnits FieldEnd = StackOffset;
1966   StackOffset = FieldEnd.alignTo(WordSize);
1967   if (StackOffset != FieldEnd) {
1968     CharUnits NumBytes = StackOffset - FieldEnd;
1969     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1970     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
1971     FrameFields.push_back(Ty);
1972   }
1973 }
1974 
1975 static bool isArgInAlloca(const ABIArgInfo &Info) {
1976   // Leave ignored and inreg arguments alone.
1977   switch (Info.getKind()) {
1978   case ABIArgInfo::InAlloca:
1979     return true;
1980   case ABIArgInfo::Ignore:
1981     return false;
1982   case ABIArgInfo::Indirect:
1983   case ABIArgInfo::Direct:
1984   case ABIArgInfo::Extend:
1985     return !Info.getInReg();
1986   case ABIArgInfo::Expand:
1987   case ABIArgInfo::CoerceAndExpand:
1988     // These are aggregate types which are never passed in registers when
1989     // inalloca is involved.
1990     return true;
1991   }
1992   llvm_unreachable("invalid enum");
1993 }
1994 
1995 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1996   assert(IsWin32StructABI && "inalloca only supported on win32");
1997 
1998   // Build a packed struct type for all of the arguments in memory.
1999   SmallVector<llvm::Type *, 6> FrameFields;
2000 
2001   // The stack alignment is always 4.
2002   CharUnits StackAlign = CharUnits::fromQuantity(4);
2003 
2004   CharUnits StackOffset;
2005   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
2006 
2007   // Put 'this' into the struct before 'sret', if necessary.
2008   bool IsThisCall =
2009       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
2010   ABIArgInfo &Ret = FI.getReturnInfo();
2011   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
2012       isArgInAlloca(I->info)) {
2013     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2014     ++I;
2015   }
2016 
2017   // Put the sret parameter into the inalloca struct if it's in memory.
2018   if (Ret.isIndirect() && !Ret.getInReg()) {
2019     addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType());
2020     // On Windows, the hidden sret parameter is always returned in eax.
2021     Ret.setInAllocaSRet(IsWin32StructABI);
2022   }
2023 
2024   // Skip the 'this' parameter in ecx.
2025   if (IsThisCall)
2026     ++I;
2027 
2028   // Put arguments passed in memory into the struct.
2029   for (; I != E; ++I) {
2030     if (isArgInAlloca(I->info))
2031       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2032   }
2033 
2034   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
2035                                         /*isPacked=*/true),
2036                   StackAlign);
2037 }
2038 
2039 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
2040                                  Address VAListAddr, QualType Ty) const {
2041 
2042   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
2043 
2044   // x86-32 changes the alignment of certain arguments on the stack.
2045   //
2046   // Just messing with TypeInfo like this works because we never pass
2047   // anything indirectly.
2048   TypeInfo.second = CharUnits::fromQuantity(
2049                 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
2050 
2051   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
2052                           TypeInfo, CharUnits::fromQuantity(4),
2053                           /*AllowHigherAlign*/ true);
2054 }
2055 
2056 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
2057     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
2058   assert(Triple.getArch() == llvm::Triple::x86);
2059 
2060   switch (Opts.getStructReturnConvention()) {
2061   case CodeGenOptions::SRCK_Default:
2062     break;
2063   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
2064     return false;
2065   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
2066     return true;
2067   }
2068 
2069   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
2070     return true;
2071 
2072   switch (Triple.getOS()) {
2073   case llvm::Triple::DragonFly:
2074   case llvm::Triple::FreeBSD:
2075   case llvm::Triple::OpenBSD:
2076   case llvm::Triple::Win32:
2077     return true;
2078   default:
2079     return false;
2080   }
2081 }
2082 
2083 void X86_32TargetCodeGenInfo::setTargetAttributes(
2084     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2085   if (GV->isDeclaration())
2086     return;
2087   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2088     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2089       llvm::Function *Fn = cast<llvm::Function>(GV);
2090       Fn->addFnAttr("stackrealign");
2091     }
2092     if (FD->hasAttr<AnyX86InterruptAttr>()) {
2093       llvm::Function *Fn = cast<llvm::Function>(GV);
2094       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2095     }
2096   }
2097 }
2098 
2099 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
2100                                                CodeGen::CodeGenFunction &CGF,
2101                                                llvm::Value *Address) const {
2102   CodeGen::CGBuilderTy &Builder = CGF.Builder;
2103 
2104   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
2105 
2106   // 0-7 are the eight integer registers;  the order is different
2107   //   on Darwin (for EH), but the range is the same.
2108   // 8 is %eip.
2109   AssignToArrayRange(Builder, Address, Four8, 0, 8);
2110 
2111   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
2112     // 12-16 are st(0..4).  Not sure why we stop at 4.
2113     // These have size 16, which is sizeof(long double) on
2114     // platforms with 8-byte alignment for that type.
2115     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
2116     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
2117 
2118   } else {
2119     // 9 is %eflags, which doesn't get a size on Darwin for some
2120     // reason.
2121     Builder.CreateAlignedStore(
2122         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
2123                                CharUnits::One());
2124 
2125     // 11-16 are st(0..5).  Not sure why we stop at 5.
2126     // These have size 12, which is sizeof(long double) on
2127     // platforms with 4-byte alignment for that type.
2128     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
2129     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
2130   }
2131 
2132   return false;
2133 }
2134 
2135 //===----------------------------------------------------------------------===//
2136 // X86-64 ABI Implementation
2137 //===----------------------------------------------------------------------===//
2138 
2139 
2140 namespace {
2141 /// The AVX ABI level for X86 targets.
2142 enum class X86AVXABILevel {
2143   None,
2144   AVX,
2145   AVX512
2146 };
2147 
2148 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2149 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2150   switch (AVXLevel) {
2151   case X86AVXABILevel::AVX512:
2152     return 512;
2153   case X86AVXABILevel::AVX:
2154     return 256;
2155   case X86AVXABILevel::None:
2156     return 128;
2157   }
2158   llvm_unreachable("Unknown AVXLevel");
2159 }
2160 
2161 /// X86_64ABIInfo - The X86_64 ABI information.
2162 class X86_64ABIInfo : public SwiftABIInfo {
2163   enum Class {
2164     Integer = 0,
2165     SSE,
2166     SSEUp,
2167     X87,
2168     X87Up,
2169     ComplexX87,
2170     NoClass,
2171     Memory
2172   };
2173 
2174   /// merge - Implement the X86_64 ABI merging algorithm.
2175   ///
2176   /// Merge an accumulating classification \arg Accum with a field
2177   /// classification \arg Field.
2178   ///
2179   /// \param Accum - The accumulating classification. This should
2180   /// always be either NoClass or the result of a previous merge
2181   /// call. In addition, this should never be Memory (the caller
2182   /// should just return Memory for the aggregate).
2183   static Class merge(Class Accum, Class Field);
2184 
2185   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2186   ///
2187   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2188   /// final MEMORY or SSE classes when necessary.
2189   ///
2190   /// \param AggregateSize - The size of the current aggregate in
2191   /// the classification process.
2192   ///
2193   /// \param Lo - The classification for the parts of the type
2194   /// residing in the low word of the containing object.
2195   ///
2196   /// \param Hi - The classification for the parts of the type
2197   /// residing in the higher words of the containing object.
2198   ///
2199   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2200 
2201   /// classify - Determine the x86_64 register classes in which the
2202   /// given type T should be passed.
2203   ///
2204   /// \param Lo - The classification for the parts of the type
2205   /// residing in the low word of the containing object.
2206   ///
2207   /// \param Hi - The classification for the parts of the type
2208   /// residing in the high word of the containing object.
2209   ///
2210   /// \param OffsetBase - The bit offset of this type in the
2211   /// containing object.  Some parameters are classified different
2212   /// depending on whether they straddle an eightbyte boundary.
2213   ///
2214   /// \param isNamedArg - Whether the argument in question is a "named"
2215   /// argument, as used in AMD64-ABI 3.5.7.
2216   ///
2217   /// If a word is unused its result will be NoClass; if a type should
2218   /// be passed in Memory then at least the classification of \arg Lo
2219   /// will be Memory.
2220   ///
2221   /// The \arg Lo class will be NoClass iff the argument is ignored.
2222   ///
2223   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2224   /// also be ComplexX87.
2225   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2226                 bool isNamedArg) const;
2227 
2228   llvm::Type *GetByteVectorType(QualType Ty) const;
2229   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2230                                  unsigned IROffset, QualType SourceTy,
2231                                  unsigned SourceOffset) const;
2232   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2233                                      unsigned IROffset, QualType SourceTy,
2234                                      unsigned SourceOffset) const;
2235 
2236   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2237   /// such that the argument will be returned in memory.
2238   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2239 
2240   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2241   /// such that the argument will be passed in memory.
2242   ///
2243   /// \param freeIntRegs - The number of free integer registers remaining
2244   /// available.
2245   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2246 
2247   ABIArgInfo classifyReturnType(QualType RetTy) const;
2248 
2249   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2250                                   unsigned &neededInt, unsigned &neededSSE,
2251                                   bool isNamedArg) const;
2252 
2253   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2254                                        unsigned &NeededSSE) const;
2255 
2256   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2257                                            unsigned &NeededSSE) const;
2258 
2259   bool IsIllegalVectorType(QualType Ty) const;
2260 
2261   /// The 0.98 ABI revision clarified a lot of ambiguities,
2262   /// unfortunately in ways that were not always consistent with
2263   /// certain previous compilers.  In particular, platforms which
2264   /// required strict binary compatibility with older versions of GCC
2265   /// may need to exempt themselves.
2266   bool honorsRevision0_98() const {
2267     return !getTarget().getTriple().isOSDarwin();
2268   }
2269 
2270   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2271   /// classify it as INTEGER (for compatibility with older clang compilers).
2272   bool classifyIntegerMMXAsSSE() const {
2273     // Clang <= 3.8 did not do this.
2274     if (getContext().getLangOpts().getClangABICompat() <=
2275         LangOptions::ClangABI::Ver3_8)
2276       return false;
2277 
2278     const llvm::Triple &Triple = getTarget().getTriple();
2279     if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2280       return false;
2281     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2282       return false;
2283     return true;
2284   }
2285 
2286   // GCC classifies vectors of __int128 as memory.
2287   bool passInt128VectorsInMem() const {
2288     // Clang <= 9.0 did not do this.
2289     if (getContext().getLangOpts().getClangABICompat() <=
2290         LangOptions::ClangABI::Ver9)
2291       return false;
2292 
2293     const llvm::Triple &T = getTarget().getTriple();
2294     return T.isOSLinux() || T.isOSNetBSD();
2295   }
2296 
2297   X86AVXABILevel AVXLevel;
2298   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2299   // 64-bit hardware.
2300   bool Has64BitPointers;
2301 
2302 public:
2303   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
2304       SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2305       Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
2306   }
2307 
2308   bool isPassedUsingAVXType(QualType type) const {
2309     unsigned neededInt, neededSSE;
2310     // The freeIntRegs argument doesn't matter here.
2311     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2312                                            /*isNamedArg*/true);
2313     if (info.isDirect()) {
2314       llvm::Type *ty = info.getCoerceToType();
2315       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2316         return vectorTy->getPrimitiveSizeInBits().getFixedSize() > 128;
2317     }
2318     return false;
2319   }
2320 
2321   void computeInfo(CGFunctionInfo &FI) const override;
2322 
2323   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2324                     QualType Ty) const override;
2325   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2326                       QualType Ty) const override;
2327 
2328   bool has64BitPointers() const {
2329     return Has64BitPointers;
2330   }
2331 
2332   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
2333                                     bool asReturnValue) const override {
2334     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2335   }
2336   bool isSwiftErrorInRegister() const override {
2337     return true;
2338   }
2339 };
2340 
2341 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2342 class WinX86_64ABIInfo : public SwiftABIInfo {
2343 public:
2344   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2345       : SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2346         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2347 
2348   void computeInfo(CGFunctionInfo &FI) const override;
2349 
2350   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2351                     QualType Ty) const override;
2352 
2353   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2354     // FIXME: Assumes vectorcall is in use.
2355     return isX86VectorTypeForVectorCall(getContext(), Ty);
2356   }
2357 
2358   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2359                                          uint64_t NumMembers) const override {
2360     // FIXME: Assumes vectorcall is in use.
2361     return isX86VectorCallAggregateSmallEnough(NumMembers);
2362   }
2363 
2364   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
2365                                     bool asReturnValue) const override {
2366     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2367   }
2368 
2369   bool isSwiftErrorInRegister() const override {
2370     return true;
2371   }
2372 
2373 private:
2374   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2375                       bool IsVectorCall, bool IsRegCall) const;
2376   ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2377                                       const ABIArgInfo &current) const;
2378   void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2379                              bool IsVectorCall, bool IsRegCall) const;
2380 
2381   X86AVXABILevel AVXLevel;
2382 
2383   bool IsMingw64;
2384 };
2385 
2386 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2387 public:
2388   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2389       : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {}
2390 
2391   const X86_64ABIInfo &getABIInfo() const {
2392     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2393   }
2394 
2395   /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
2396   /// the autoreleaseRV/retainRV optimization.
2397   bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override {
2398     return true;
2399   }
2400 
2401   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2402     return 7;
2403   }
2404 
2405   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2406                                llvm::Value *Address) const override {
2407     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2408 
2409     // 0-15 are the 16 integer registers.
2410     // 16 is %rip.
2411     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2412     return false;
2413   }
2414 
2415   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2416                                   StringRef Constraint,
2417                                   llvm::Type* Ty) const override {
2418     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2419   }
2420 
2421   bool isNoProtoCallVariadic(const CallArgList &args,
2422                              const FunctionNoProtoType *fnType) const override {
2423     // The default CC on x86-64 sets %al to the number of SSA
2424     // registers used, and GCC sets this when calling an unprototyped
2425     // function, so we override the default behavior.  However, don't do
2426     // that when AVX types are involved: the ABI explicitly states it is
2427     // undefined, and it doesn't work in practice because of how the ABI
2428     // defines varargs anyway.
2429     if (fnType->getCallConv() == CC_C) {
2430       bool HasAVXType = false;
2431       for (CallArgList::const_iterator
2432              it = args.begin(), ie = args.end(); it != ie; ++it) {
2433         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2434           HasAVXType = true;
2435           break;
2436         }
2437       }
2438 
2439       if (!HasAVXType)
2440         return true;
2441     }
2442 
2443     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2444   }
2445 
2446   llvm::Constant *
2447   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2448     unsigned Sig = (0xeb << 0) | // jmp rel8
2449                    (0x06 << 8) | //           .+0x08
2450                    ('v' << 16) |
2451                    ('2' << 24);
2452     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2453   }
2454 
2455   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2456                            CodeGen::CodeGenModule &CGM) const override {
2457     if (GV->isDeclaration())
2458       return;
2459     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2460       if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2461         llvm::Function *Fn = cast<llvm::Function>(GV);
2462         Fn->addFnAttr("stackrealign");
2463       }
2464       if (FD->hasAttr<AnyX86InterruptAttr>()) {
2465         llvm::Function *Fn = cast<llvm::Function>(GV);
2466         Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2467       }
2468     }
2469   }
2470 
2471   void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
2472                             const FunctionDecl *Caller,
2473                             const FunctionDecl *Callee,
2474                             const CallArgList &Args) const override;
2475 };
2476 
2477 static void initFeatureMaps(const ASTContext &Ctx,
2478                             llvm::StringMap<bool> &CallerMap,
2479                             const FunctionDecl *Caller,
2480                             llvm::StringMap<bool> &CalleeMap,
2481                             const FunctionDecl *Callee) {
2482   if (CalleeMap.empty() && CallerMap.empty()) {
2483     // The caller is potentially nullptr in the case where the call isn't in a
2484     // function.  In this case, the getFunctionFeatureMap ensures we just get
2485     // the TU level setting (since it cannot be modified by 'target'..
2486     Ctx.getFunctionFeatureMap(CallerMap, Caller);
2487     Ctx.getFunctionFeatureMap(CalleeMap, Callee);
2488   }
2489 }
2490 
2491 static bool checkAVXParamFeature(DiagnosticsEngine &Diag,
2492                                  SourceLocation CallLoc,
2493                                  const llvm::StringMap<bool> &CallerMap,
2494                                  const llvm::StringMap<bool> &CalleeMap,
2495                                  QualType Ty, StringRef Feature,
2496                                  bool IsArgument) {
2497   bool CallerHasFeat = CallerMap.lookup(Feature);
2498   bool CalleeHasFeat = CalleeMap.lookup(Feature);
2499   if (!CallerHasFeat && !CalleeHasFeat)
2500     return Diag.Report(CallLoc, diag::warn_avx_calling_convention)
2501            << IsArgument << Ty << Feature;
2502 
2503   // Mixing calling conventions here is very clearly an error.
2504   if (!CallerHasFeat || !CalleeHasFeat)
2505     return Diag.Report(CallLoc, diag::err_avx_calling_convention)
2506            << IsArgument << Ty << Feature;
2507 
2508   // Else, both caller and callee have the required feature, so there is no need
2509   // to diagnose.
2510   return false;
2511 }
2512 
2513 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx,
2514                           SourceLocation CallLoc,
2515                           const llvm::StringMap<bool> &CallerMap,
2516                           const llvm::StringMap<bool> &CalleeMap, QualType Ty,
2517                           bool IsArgument) {
2518   uint64_t Size = Ctx.getTypeSize(Ty);
2519   if (Size > 256)
2520     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty,
2521                                 "avx512f", IsArgument);
2522 
2523   if (Size > 128)
2524     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx",
2525                                 IsArgument);
2526 
2527   return false;
2528 }
2529 
2530 void X86_64TargetCodeGenInfo::checkFunctionCallABI(
2531     CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller,
2532     const FunctionDecl *Callee, const CallArgList &Args) const {
2533   llvm::StringMap<bool> CallerMap;
2534   llvm::StringMap<bool> CalleeMap;
2535   unsigned ArgIndex = 0;
2536 
2537   // We need to loop through the actual call arguments rather than the the
2538   // function's parameters, in case this variadic.
2539   for (const CallArg &Arg : Args) {
2540     // The "avx" feature changes how vectors >128 in size are passed. "avx512f"
2541     // additionally changes how vectors >256 in size are passed. Like GCC, we
2542     // warn when a function is called with an argument where this will change.
2543     // Unlike GCC, we also error when it is an obvious ABI mismatch, that is,
2544     // the caller and callee features are mismatched.
2545     // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can
2546     // change its ABI with attribute-target after this call.
2547     if (Arg.getType()->isVectorType() &&
2548         CGM.getContext().getTypeSize(Arg.getType()) > 128) {
2549       initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2550       QualType Ty = Arg.getType();
2551       // The CallArg seems to have desugared the type already, so for clearer
2552       // diagnostics, replace it with the type in the FunctionDecl if possible.
2553       if (ArgIndex < Callee->getNumParams())
2554         Ty = Callee->getParamDecl(ArgIndex)->getType();
2555 
2556       if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2557                         CalleeMap, Ty, /*IsArgument*/ true))
2558         return;
2559     }
2560     ++ArgIndex;
2561   }
2562 
2563   // Check return always, as we don't have a good way of knowing in codegen
2564   // whether this value is used, tail-called, etc.
2565   if (Callee->getReturnType()->isVectorType() &&
2566       CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) {
2567     initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2568     checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2569                   CalleeMap, Callee->getReturnType(),
2570                   /*IsArgument*/ false);
2571   }
2572 }
2573 
2574 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2575   // If the argument does not end in .lib, automatically add the suffix.
2576   // If the argument contains a space, enclose it in quotes.
2577   // This matches the behavior of MSVC.
2578   bool Quote = (Lib.find(" ") != StringRef::npos);
2579   std::string ArgStr = Quote ? "\"" : "";
2580   ArgStr += Lib;
2581   if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a"))
2582     ArgStr += ".lib";
2583   ArgStr += Quote ? "\"" : "";
2584   return ArgStr;
2585 }
2586 
2587 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2588 public:
2589   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2590         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2591         unsigned NumRegisterParameters)
2592     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2593         Win32StructABI, NumRegisterParameters, false) {}
2594 
2595   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2596                            CodeGen::CodeGenModule &CGM) const override;
2597 
2598   void getDependentLibraryOption(llvm::StringRef Lib,
2599                                  llvm::SmallString<24> &Opt) const override {
2600     Opt = "/DEFAULTLIB:";
2601     Opt += qualifyWindowsLibrary(Lib);
2602   }
2603 
2604   void getDetectMismatchOption(llvm::StringRef Name,
2605                                llvm::StringRef Value,
2606                                llvm::SmallString<32> &Opt) const override {
2607     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2608   }
2609 };
2610 
2611 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2612                                           CodeGen::CodeGenModule &CGM) {
2613   if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
2614 
2615     if (CGM.getCodeGenOpts().StackProbeSize != 4096)
2616       Fn->addFnAttr("stack-probe-size",
2617                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2618     if (CGM.getCodeGenOpts().NoStackArgProbe)
2619       Fn->addFnAttr("no-stack-arg-probe");
2620   }
2621 }
2622 
2623 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2624     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2625   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2626   if (GV->isDeclaration())
2627     return;
2628   addStackProbeTargetAttributes(D, GV, CGM);
2629 }
2630 
2631 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2632 public:
2633   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2634                              X86AVXABILevel AVXLevel)
2635       : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {}
2636 
2637   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2638                            CodeGen::CodeGenModule &CGM) const override;
2639 
2640   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2641     return 7;
2642   }
2643 
2644   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2645                                llvm::Value *Address) const override {
2646     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2647 
2648     // 0-15 are the 16 integer registers.
2649     // 16 is %rip.
2650     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2651     return false;
2652   }
2653 
2654   void getDependentLibraryOption(llvm::StringRef Lib,
2655                                  llvm::SmallString<24> &Opt) const override {
2656     Opt = "/DEFAULTLIB:";
2657     Opt += qualifyWindowsLibrary(Lib);
2658   }
2659 
2660   void getDetectMismatchOption(llvm::StringRef Name,
2661                                llvm::StringRef Value,
2662                                llvm::SmallString<32> &Opt) const override {
2663     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2664   }
2665 };
2666 
2667 void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2668     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2669   TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2670   if (GV->isDeclaration())
2671     return;
2672   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2673     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2674       llvm::Function *Fn = cast<llvm::Function>(GV);
2675       Fn->addFnAttr("stackrealign");
2676     }
2677     if (FD->hasAttr<AnyX86InterruptAttr>()) {
2678       llvm::Function *Fn = cast<llvm::Function>(GV);
2679       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2680     }
2681   }
2682 
2683   addStackProbeTargetAttributes(D, GV, CGM);
2684 }
2685 }
2686 
2687 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2688                               Class &Hi) const {
2689   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2690   //
2691   // (a) If one of the classes is Memory, the whole argument is passed in
2692   //     memory.
2693   //
2694   // (b) If X87UP is not preceded by X87, the whole argument is passed in
2695   //     memory.
2696   //
2697   // (c) If the size of the aggregate exceeds two eightbytes and the first
2698   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2699   //     argument is passed in memory. NOTE: This is necessary to keep the
2700   //     ABI working for processors that don't support the __m256 type.
2701   //
2702   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2703   //
2704   // Some of these are enforced by the merging logic.  Others can arise
2705   // only with unions; for example:
2706   //   union { _Complex double; unsigned; }
2707   //
2708   // Note that clauses (b) and (c) were added in 0.98.
2709   //
2710   if (Hi == Memory)
2711     Lo = Memory;
2712   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2713     Lo = Memory;
2714   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2715     Lo = Memory;
2716   if (Hi == SSEUp && Lo != SSE)
2717     Hi = SSE;
2718 }
2719 
2720 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2721   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2722   // classified recursively so that always two fields are
2723   // considered. The resulting class is calculated according to
2724   // the classes of the fields in the eightbyte:
2725   //
2726   // (a) If both classes are equal, this is the resulting class.
2727   //
2728   // (b) If one of the classes is NO_CLASS, the resulting class is
2729   // the other class.
2730   //
2731   // (c) If one of the classes is MEMORY, the result is the MEMORY
2732   // class.
2733   //
2734   // (d) If one of the classes is INTEGER, the result is the
2735   // INTEGER.
2736   //
2737   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2738   // MEMORY is used as class.
2739   //
2740   // (f) Otherwise class SSE is used.
2741 
2742   // Accum should never be memory (we should have returned) or
2743   // ComplexX87 (because this cannot be passed in a structure).
2744   assert((Accum != Memory && Accum != ComplexX87) &&
2745          "Invalid accumulated classification during merge.");
2746   if (Accum == Field || Field == NoClass)
2747     return Accum;
2748   if (Field == Memory)
2749     return Memory;
2750   if (Accum == NoClass)
2751     return Field;
2752   if (Accum == Integer || Field == Integer)
2753     return Integer;
2754   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2755       Accum == X87 || Accum == X87Up)
2756     return Memory;
2757   return SSE;
2758 }
2759 
2760 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
2761                              Class &Lo, Class &Hi, bool isNamedArg) const {
2762   // FIXME: This code can be simplified by introducing a simple value class for
2763   // Class pairs with appropriate constructor methods for the various
2764   // situations.
2765 
2766   // FIXME: Some of the split computations are wrong; unaligned vectors
2767   // shouldn't be passed in registers for example, so there is no chance they
2768   // can straddle an eightbyte. Verify & simplify.
2769 
2770   Lo = Hi = NoClass;
2771 
2772   Class &Current = OffsetBase < 64 ? Lo : Hi;
2773   Current = Memory;
2774 
2775   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2776     BuiltinType::Kind k = BT->getKind();
2777 
2778     if (k == BuiltinType::Void) {
2779       Current = NoClass;
2780     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2781       Lo = Integer;
2782       Hi = Integer;
2783     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2784       Current = Integer;
2785     } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
2786       Current = SSE;
2787     } else if (k == BuiltinType::LongDouble) {
2788       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2789       if (LDF == &llvm::APFloat::IEEEquad()) {
2790         Lo = SSE;
2791         Hi = SSEUp;
2792       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2793         Lo = X87;
2794         Hi = X87Up;
2795       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2796         Current = SSE;
2797       } else
2798         llvm_unreachable("unexpected long double representation!");
2799     }
2800     // FIXME: _Decimal32 and _Decimal64 are SSE.
2801     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2802     return;
2803   }
2804 
2805   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2806     // Classify the underlying integer type.
2807     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2808     return;
2809   }
2810 
2811   if (Ty->hasPointerRepresentation()) {
2812     Current = Integer;
2813     return;
2814   }
2815 
2816   if (Ty->isMemberPointerType()) {
2817     if (Ty->isMemberFunctionPointerType()) {
2818       if (Has64BitPointers) {
2819         // If Has64BitPointers, this is an {i64, i64}, so classify both
2820         // Lo and Hi now.
2821         Lo = Hi = Integer;
2822       } else {
2823         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2824         // straddles an eightbyte boundary, Hi should be classified as well.
2825         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2826         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2827         if (EB_FuncPtr != EB_ThisAdj) {
2828           Lo = Hi = Integer;
2829         } else {
2830           Current = Integer;
2831         }
2832       }
2833     } else {
2834       Current = Integer;
2835     }
2836     return;
2837   }
2838 
2839   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2840     uint64_t Size = getContext().getTypeSize(VT);
2841     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2842       // gcc passes the following as integer:
2843       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2844       // 2 bytes - <2 x char>, <1 x short>
2845       // 1 byte  - <1 x char>
2846       Current = Integer;
2847 
2848       // If this type crosses an eightbyte boundary, it should be
2849       // split.
2850       uint64_t EB_Lo = (OffsetBase) / 64;
2851       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2852       if (EB_Lo != EB_Hi)
2853         Hi = Lo;
2854     } else if (Size == 64) {
2855       QualType ElementType = VT->getElementType();
2856 
2857       // gcc passes <1 x double> in memory. :(
2858       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2859         return;
2860 
2861       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2862       // pass them as integer.  For platforms where clang is the de facto
2863       // platform compiler, we must continue to use integer.
2864       if (!classifyIntegerMMXAsSSE() &&
2865           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2866            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2867            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2868            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2869         Current = Integer;
2870       else
2871         Current = SSE;
2872 
2873       // If this type crosses an eightbyte boundary, it should be
2874       // split.
2875       if (OffsetBase && OffsetBase != 64)
2876         Hi = Lo;
2877     } else if (Size == 128 ||
2878                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2879       QualType ElementType = VT->getElementType();
2880 
2881       // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
2882       if (passInt128VectorsInMem() && Size != 128 &&
2883           (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
2884            ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
2885         return;
2886 
2887       // Arguments of 256-bits are split into four eightbyte chunks. The
2888       // least significant one belongs to class SSE and all the others to class
2889       // SSEUP. The original Lo and Hi design considers that types can't be
2890       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2891       // This design isn't correct for 256-bits, but since there're no cases
2892       // where the upper parts would need to be inspected, avoid adding
2893       // complexity and just consider Hi to match the 64-256 part.
2894       //
2895       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2896       // registers if they are "named", i.e. not part of the "..." of a
2897       // variadic function.
2898       //
2899       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2900       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2901       Lo = SSE;
2902       Hi = SSEUp;
2903     }
2904     return;
2905   }
2906 
2907   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2908     QualType ET = getContext().getCanonicalType(CT->getElementType());
2909 
2910     uint64_t Size = getContext().getTypeSize(Ty);
2911     if (ET->isIntegralOrEnumerationType()) {
2912       if (Size <= 64)
2913         Current = Integer;
2914       else if (Size <= 128)
2915         Lo = Hi = Integer;
2916     } else if (ET == getContext().FloatTy) {
2917       Current = SSE;
2918     } else if (ET == getContext().DoubleTy) {
2919       Lo = Hi = SSE;
2920     } else if (ET == getContext().LongDoubleTy) {
2921       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2922       if (LDF == &llvm::APFloat::IEEEquad())
2923         Current = Memory;
2924       else if (LDF == &llvm::APFloat::x87DoubleExtended())
2925         Current = ComplexX87;
2926       else if (LDF == &llvm::APFloat::IEEEdouble())
2927         Lo = Hi = SSE;
2928       else
2929         llvm_unreachable("unexpected long double representation!");
2930     }
2931 
2932     // If this complex type crosses an eightbyte boundary then it
2933     // should be split.
2934     uint64_t EB_Real = (OffsetBase) / 64;
2935     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2936     if (Hi == NoClass && EB_Real != EB_Imag)
2937       Hi = Lo;
2938 
2939     return;
2940   }
2941 
2942   if (const auto *EITy = Ty->getAs<ExtIntType>()) {
2943     if (EITy->getNumBits() <= 64)
2944       Current = Integer;
2945     else if (EITy->getNumBits() <= 128)
2946       Lo = Hi = Integer;
2947     // Larger values need to get passed in memory.
2948     return;
2949   }
2950 
2951   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2952     // Arrays are treated like structures.
2953 
2954     uint64_t Size = getContext().getTypeSize(Ty);
2955 
2956     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2957     // than eight eightbytes, ..., it has class MEMORY.
2958     if (Size > 512)
2959       return;
2960 
2961     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2962     // fields, it has class MEMORY.
2963     //
2964     // Only need to check alignment of array base.
2965     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2966       return;
2967 
2968     // Otherwise implement simplified merge. We could be smarter about
2969     // this, but it isn't worth it and would be harder to verify.
2970     Current = NoClass;
2971     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
2972     uint64_t ArraySize = AT->getSize().getZExtValue();
2973 
2974     // The only case a 256-bit wide vector could be used is when the array
2975     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2976     // to work for sizes wider than 128, early check and fallback to memory.
2977     //
2978     if (Size > 128 &&
2979         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
2980       return;
2981 
2982     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2983       Class FieldLo, FieldHi;
2984       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
2985       Lo = merge(Lo, FieldLo);
2986       Hi = merge(Hi, FieldHi);
2987       if (Lo == Memory || Hi == Memory)
2988         break;
2989     }
2990 
2991     postMerge(Size, Lo, Hi);
2992     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
2993     return;
2994   }
2995 
2996   if (const RecordType *RT = Ty->getAs<RecordType>()) {
2997     uint64_t Size = getContext().getTypeSize(Ty);
2998 
2999     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3000     // than eight eightbytes, ..., it has class MEMORY.
3001     if (Size > 512)
3002       return;
3003 
3004     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
3005     // copy constructor or a non-trivial destructor, it is passed by invisible
3006     // reference.
3007     if (getRecordArgABI(RT, getCXXABI()))
3008       return;
3009 
3010     const RecordDecl *RD = RT->getDecl();
3011 
3012     // Assume variable sized types are passed in memory.
3013     if (RD->hasFlexibleArrayMember())
3014       return;
3015 
3016     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3017 
3018     // Reset Lo class, this will be recomputed.
3019     Current = NoClass;
3020 
3021     // If this is a C++ record, classify the bases first.
3022     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3023       for (const auto &I : CXXRD->bases()) {
3024         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3025                "Unexpected base class!");
3026         const auto *Base =
3027             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3028 
3029         // Classify this field.
3030         //
3031         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
3032         // single eightbyte, each is classified separately. Each eightbyte gets
3033         // initialized to class NO_CLASS.
3034         Class FieldLo, FieldHi;
3035         uint64_t Offset =
3036           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
3037         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
3038         Lo = merge(Lo, FieldLo);
3039         Hi = merge(Hi, FieldHi);
3040         if (Lo == Memory || Hi == Memory) {
3041           postMerge(Size, Lo, Hi);
3042           return;
3043         }
3044       }
3045     }
3046 
3047     // Classify the fields one at a time, merging the results.
3048     unsigned idx = 0;
3049     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3050            i != e; ++i, ++idx) {
3051       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3052       bool BitField = i->isBitField();
3053 
3054       // Ignore padding bit-fields.
3055       if (BitField && i->isUnnamedBitfield())
3056         continue;
3057 
3058       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
3059       // four eightbytes, or it contains unaligned fields, it has class MEMORY.
3060       //
3061       // The only case a 256-bit wide vector could be used is when the struct
3062       // contains a single 256-bit element. Since Lo and Hi logic isn't extended
3063       // to work for sizes wider than 128, early check and fallback to memory.
3064       //
3065       if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
3066                          Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
3067         Lo = Memory;
3068         postMerge(Size, Lo, Hi);
3069         return;
3070       }
3071       // Note, skip this test for bit-fields, see below.
3072       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
3073         Lo = Memory;
3074         postMerge(Size, Lo, Hi);
3075         return;
3076       }
3077 
3078       // Classify this field.
3079       //
3080       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
3081       // exceeds a single eightbyte, each is classified
3082       // separately. Each eightbyte gets initialized to class
3083       // NO_CLASS.
3084       Class FieldLo, FieldHi;
3085 
3086       // Bit-fields require special handling, they do not force the
3087       // structure to be passed in memory even if unaligned, and
3088       // therefore they can straddle an eightbyte.
3089       if (BitField) {
3090         assert(!i->isUnnamedBitfield());
3091         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3092         uint64_t Size = i->getBitWidthValue(getContext());
3093 
3094         uint64_t EB_Lo = Offset / 64;
3095         uint64_t EB_Hi = (Offset + Size - 1) / 64;
3096 
3097         if (EB_Lo) {
3098           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
3099           FieldLo = NoClass;
3100           FieldHi = Integer;
3101         } else {
3102           FieldLo = Integer;
3103           FieldHi = EB_Hi ? Integer : NoClass;
3104         }
3105       } else
3106         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
3107       Lo = merge(Lo, FieldLo);
3108       Hi = merge(Hi, FieldHi);
3109       if (Lo == Memory || Hi == Memory)
3110         break;
3111     }
3112 
3113     postMerge(Size, Lo, Hi);
3114   }
3115 }
3116 
3117 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
3118   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3119   // place naturally.
3120   if (!isAggregateTypeForABI(Ty)) {
3121     // Treat an enum type as its underlying type.
3122     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3123       Ty = EnumTy->getDecl()->getIntegerType();
3124 
3125     if (Ty->isExtIntType())
3126       return getNaturalAlignIndirect(Ty);
3127 
3128     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3129                                               : ABIArgInfo::getDirect());
3130   }
3131 
3132   return getNaturalAlignIndirect(Ty);
3133 }
3134 
3135 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
3136   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
3137     uint64_t Size = getContext().getTypeSize(VecTy);
3138     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
3139     if (Size <= 64 || Size > LargestVector)
3140       return true;
3141     QualType EltTy = VecTy->getElementType();
3142     if (passInt128VectorsInMem() &&
3143         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
3144          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
3145       return true;
3146   }
3147 
3148   return false;
3149 }
3150 
3151 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
3152                                             unsigned freeIntRegs) const {
3153   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3154   // place naturally.
3155   //
3156   // This assumption is optimistic, as there could be free registers available
3157   // when we need to pass this argument in memory, and LLVM could try to pass
3158   // the argument in the free register. This does not seem to happen currently,
3159   // but this code would be much safer if we could mark the argument with
3160   // 'onstack'. See PR12193.
3161   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
3162       !Ty->isExtIntType()) {
3163     // Treat an enum type as its underlying type.
3164     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3165       Ty = EnumTy->getDecl()->getIntegerType();
3166 
3167     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3168                                               : ABIArgInfo::getDirect());
3169   }
3170 
3171   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3172     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3173 
3174   // Compute the byval alignment. We specify the alignment of the byval in all
3175   // cases so that the mid-level optimizer knows the alignment of the byval.
3176   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
3177 
3178   // Attempt to avoid passing indirect results using byval when possible. This
3179   // is important for good codegen.
3180   //
3181   // We do this by coercing the value into a scalar type which the backend can
3182   // handle naturally (i.e., without using byval).
3183   //
3184   // For simplicity, we currently only do this when we have exhausted all of the
3185   // free integer registers. Doing this when there are free integer registers
3186   // would require more care, as we would have to ensure that the coerced value
3187   // did not claim the unused register. That would require either reording the
3188   // arguments to the function (so that any subsequent inreg values came first),
3189   // or only doing this optimization when there were no following arguments that
3190   // might be inreg.
3191   //
3192   // We currently expect it to be rare (particularly in well written code) for
3193   // arguments to be passed on the stack when there are still free integer
3194   // registers available (this would typically imply large structs being passed
3195   // by value), so this seems like a fair tradeoff for now.
3196   //
3197   // We can revisit this if the backend grows support for 'onstack' parameter
3198   // attributes. See PR12193.
3199   if (freeIntRegs == 0) {
3200     uint64_t Size = getContext().getTypeSize(Ty);
3201 
3202     // If this type fits in an eightbyte, coerce it into the matching integral
3203     // type, which will end up on the stack (with alignment 8).
3204     if (Align == 8 && Size <= 64)
3205       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3206                                                           Size));
3207   }
3208 
3209   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
3210 }
3211 
3212 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3213 /// register. Pick an LLVM IR type that will be passed as a vector register.
3214 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3215   // Wrapper structs/arrays that only contain vectors are passed just like
3216   // vectors; strip them off if present.
3217   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3218     Ty = QualType(InnerTy, 0);
3219 
3220   llvm::Type *IRType = CGT.ConvertType(Ty);
3221   if (isa<llvm::VectorType>(IRType)) {
3222     // Don't pass vXi128 vectors in their native type, the backend can't
3223     // legalize them.
3224     if (passInt128VectorsInMem() &&
3225         cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
3226       // Use a vXi64 vector.
3227       uint64_t Size = getContext().getTypeSize(Ty);
3228       return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3229                                         Size / 64);
3230     }
3231 
3232     return IRType;
3233   }
3234 
3235   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3236     return IRType;
3237 
3238   // We couldn't find the preferred IR vector type for 'Ty'.
3239   uint64_t Size = getContext().getTypeSize(Ty);
3240   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3241 
3242 
3243   // Return a LLVM IR vector type based on the size of 'Ty'.
3244   return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3245                                     Size / 64);
3246 }
3247 
3248 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3249 /// is known to either be off the end of the specified type or being in
3250 /// alignment padding.  The user type specified is known to be at most 128 bits
3251 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3252 /// classification that put one of the two halves in the INTEGER class.
3253 ///
3254 /// It is conservatively correct to return false.
3255 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3256                                   unsigned EndBit, ASTContext &Context) {
3257   // If the bytes being queried are off the end of the type, there is no user
3258   // data hiding here.  This handles analysis of builtins, vectors and other
3259   // types that don't contain interesting padding.
3260   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3261   if (TySize <= StartBit)
3262     return true;
3263 
3264   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3265     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3266     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3267 
3268     // Check each element to see if the element overlaps with the queried range.
3269     for (unsigned i = 0; i != NumElts; ++i) {
3270       // If the element is after the span we care about, then we're done..
3271       unsigned EltOffset = i*EltSize;
3272       if (EltOffset >= EndBit) break;
3273 
3274       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3275       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3276                                  EndBit-EltOffset, Context))
3277         return false;
3278     }
3279     // If it overlaps no elements, then it is safe to process as padding.
3280     return true;
3281   }
3282 
3283   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3284     const RecordDecl *RD = RT->getDecl();
3285     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3286 
3287     // If this is a C++ record, check the bases first.
3288     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3289       for (const auto &I : CXXRD->bases()) {
3290         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3291                "Unexpected base class!");
3292         const auto *Base =
3293             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3294 
3295         // If the base is after the span we care about, ignore it.
3296         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3297         if (BaseOffset >= EndBit) continue;
3298 
3299         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3300         if (!BitsContainNoUserData(I.getType(), BaseStart,
3301                                    EndBit-BaseOffset, Context))
3302           return false;
3303       }
3304     }
3305 
3306     // Verify that no field has data that overlaps the region of interest.  Yes
3307     // this could be sped up a lot by being smarter about queried fields,
3308     // however we're only looking at structs up to 16 bytes, so we don't care
3309     // much.
3310     unsigned idx = 0;
3311     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3312          i != e; ++i, ++idx) {
3313       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3314 
3315       // If we found a field after the region we care about, then we're done.
3316       if (FieldOffset >= EndBit) break;
3317 
3318       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3319       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3320                                  Context))
3321         return false;
3322     }
3323 
3324     // If nothing in this record overlapped the area of interest, then we're
3325     // clean.
3326     return true;
3327   }
3328 
3329   return false;
3330 }
3331 
3332 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3333 /// float member at the specified offset.  For example, {int,{float}} has a
3334 /// float at offset 4.  It is conservatively correct for this routine to return
3335 /// false.
3336 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
3337                                   const llvm::DataLayout &TD) {
3338   // Base case if we find a float.
3339   if (IROffset == 0 && IRType->isFloatTy())
3340     return true;
3341 
3342   // If this is a struct, recurse into the field at the specified offset.
3343   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3344     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3345     unsigned Elt = SL->getElementContainingOffset(IROffset);
3346     IROffset -= SL->getElementOffset(Elt);
3347     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3348   }
3349 
3350   // If this is an array, recurse into the field at the specified offset.
3351   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3352     llvm::Type *EltTy = ATy->getElementType();
3353     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3354     IROffset -= IROffset/EltSize*EltSize;
3355     return ContainsFloatAtOffset(EltTy, IROffset, TD);
3356   }
3357 
3358   return false;
3359 }
3360 
3361 
3362 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3363 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3364 llvm::Type *X86_64ABIInfo::
3365 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3366                    QualType SourceTy, unsigned SourceOffset) const {
3367   // The only three choices we have are either double, <2 x float>, or float. We
3368   // pass as float if the last 4 bytes is just padding.  This happens for
3369   // structs that contain 3 floats.
3370   if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3371                             SourceOffset*8+64, getContext()))
3372     return llvm::Type::getFloatTy(getVMContext());
3373 
3374   // We want to pass as <2 x float> if the LLVM IR type contains a float at
3375   // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
3376   // case.
3377   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3378       ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
3379     return llvm::FixedVectorType::get(llvm::Type::getFloatTy(getVMContext()),
3380                                       2);
3381 
3382   return llvm::Type::getDoubleTy(getVMContext());
3383 }
3384 
3385 
3386 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3387 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3388 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3389 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3390 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3391 /// etc).
3392 ///
3393 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3394 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3395 /// the 8-byte value references.  PrefType may be null.
3396 ///
3397 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3398 /// an offset into this that we're processing (which is always either 0 or 8).
3399 ///
3400 llvm::Type *X86_64ABIInfo::
3401 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3402                        QualType SourceTy, unsigned SourceOffset) const {
3403   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3404   // returning an 8-byte unit starting with it.  See if we can safely use it.
3405   if (IROffset == 0) {
3406     // Pointers and int64's always fill the 8-byte unit.
3407     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3408         IRType->isIntegerTy(64))
3409       return IRType;
3410 
3411     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3412     // goodness in the source type is just tail padding.  This is allowed to
3413     // kick in for struct {double,int} on the int, but not on
3414     // struct{double,int,int} because we wouldn't return the second int.  We
3415     // have to do this analysis on the source type because we can't depend on
3416     // unions being lowered a specific way etc.
3417     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3418         IRType->isIntegerTy(32) ||
3419         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3420       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3421           cast<llvm::IntegerType>(IRType)->getBitWidth();
3422 
3423       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3424                                 SourceOffset*8+64, getContext()))
3425         return IRType;
3426     }
3427   }
3428 
3429   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3430     // If this is a struct, recurse into the field at the specified offset.
3431     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3432     if (IROffset < SL->getSizeInBytes()) {
3433       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3434       IROffset -= SL->getElementOffset(FieldIdx);
3435 
3436       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3437                                     SourceTy, SourceOffset);
3438     }
3439   }
3440 
3441   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3442     llvm::Type *EltTy = ATy->getElementType();
3443     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3444     unsigned EltOffset = IROffset/EltSize*EltSize;
3445     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3446                                   SourceOffset);
3447   }
3448 
3449   // Okay, we don't have any better idea of what to pass, so we pass this in an
3450   // integer register that isn't too big to fit the rest of the struct.
3451   unsigned TySizeInBytes =
3452     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3453 
3454   assert(TySizeInBytes != SourceOffset && "Empty field?");
3455 
3456   // It is always safe to classify this as an integer type up to i64 that
3457   // isn't larger than the structure.
3458   return llvm::IntegerType::get(getVMContext(),
3459                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3460 }
3461 
3462 
3463 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3464 /// be used as elements of a two register pair to pass or return, return a
3465 /// first class aggregate to represent them.  For example, if the low part of
3466 /// a by-value argument should be passed as i32* and the high part as float,
3467 /// return {i32*, float}.
3468 static llvm::Type *
3469 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3470                            const llvm::DataLayout &TD) {
3471   // In order to correctly satisfy the ABI, we need to the high part to start
3472   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3473   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3474   // the second element at offset 8.  Check for this:
3475   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3476   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3477   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3478   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3479 
3480   // To handle this, we have to increase the size of the low part so that the
3481   // second element will start at an 8 byte offset.  We can't increase the size
3482   // of the second element because it might make us access off the end of the
3483   // struct.
3484   if (HiStart != 8) {
3485     // There are usually two sorts of types the ABI generation code can produce
3486     // for the low part of a pair that aren't 8 bytes in size: float or
3487     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3488     // NaCl).
3489     // Promote these to a larger type.
3490     if (Lo->isFloatTy())
3491       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3492     else {
3493       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3494              && "Invalid/unknown lo type");
3495       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3496     }
3497   }
3498 
3499   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3500 
3501   // Verify that the second element is at an 8-byte offset.
3502   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3503          "Invalid x86-64 argument pair!");
3504   return Result;
3505 }
3506 
3507 ABIArgInfo X86_64ABIInfo::
3508 classifyReturnType(QualType RetTy) const {
3509   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3510   // classification algorithm.
3511   X86_64ABIInfo::Class Lo, Hi;
3512   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3513 
3514   // Check some invariants.
3515   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3516   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3517 
3518   llvm::Type *ResType = nullptr;
3519   switch (Lo) {
3520   case NoClass:
3521     if (Hi == NoClass)
3522       return ABIArgInfo::getIgnore();
3523     // If the low part is just padding, it takes no register, leave ResType
3524     // null.
3525     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3526            "Unknown missing lo part");
3527     break;
3528 
3529   case SSEUp:
3530   case X87Up:
3531     llvm_unreachable("Invalid classification for lo word.");
3532 
3533     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3534     // hidden argument.
3535   case Memory:
3536     return getIndirectReturnResult(RetTy);
3537 
3538     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3539     // available register of the sequence %rax, %rdx is used.
3540   case Integer:
3541     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3542 
3543     // If we have a sign or zero extended integer, make sure to return Extend
3544     // so that the parameter gets the right LLVM IR attributes.
3545     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3546       // Treat an enum type as its underlying type.
3547       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3548         RetTy = EnumTy->getDecl()->getIntegerType();
3549 
3550       if (RetTy->isIntegralOrEnumerationType() &&
3551           isPromotableIntegerTypeForABI(RetTy))
3552         return ABIArgInfo::getExtend(RetTy);
3553     }
3554     break;
3555 
3556     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3557     // available SSE register of the sequence %xmm0, %xmm1 is used.
3558   case SSE:
3559     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3560     break;
3561 
3562     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3563     // returned on the X87 stack in %st0 as 80-bit x87 number.
3564   case X87:
3565     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3566     break;
3567 
3568     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3569     // part of the value is returned in %st0 and the imaginary part in
3570     // %st1.
3571   case ComplexX87:
3572     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3573     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3574                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3575     break;
3576   }
3577 
3578   llvm::Type *HighPart = nullptr;
3579   switch (Hi) {
3580     // Memory was handled previously and X87 should
3581     // never occur as a hi class.
3582   case Memory:
3583   case X87:
3584     llvm_unreachable("Invalid classification for hi word.");
3585 
3586   case ComplexX87: // Previously handled.
3587   case NoClass:
3588     break;
3589 
3590   case Integer:
3591     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3592     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3593       return ABIArgInfo::getDirect(HighPart, 8);
3594     break;
3595   case SSE:
3596     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3597     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3598       return ABIArgInfo::getDirect(HighPart, 8);
3599     break;
3600 
3601     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3602     // is passed in the next available eightbyte chunk if the last used
3603     // vector register.
3604     //
3605     // SSEUP should always be preceded by SSE, just widen.
3606   case SSEUp:
3607     assert(Lo == SSE && "Unexpected SSEUp classification.");
3608     ResType = GetByteVectorType(RetTy);
3609     break;
3610 
3611     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3612     // returned together with the previous X87 value in %st0.
3613   case X87Up:
3614     // If X87Up is preceded by X87, we don't need to do
3615     // anything. However, in some cases with unions it may not be
3616     // preceded by X87. In such situations we follow gcc and pass the
3617     // extra bits in an SSE reg.
3618     if (Lo != X87) {
3619       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3620       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3621         return ABIArgInfo::getDirect(HighPart, 8);
3622     }
3623     break;
3624   }
3625 
3626   // If a high part was specified, merge it together with the low part.  It is
3627   // known to pass in the high eightbyte of the result.  We do this by forming a
3628   // first class struct aggregate with the high and low part: {low, high}
3629   if (HighPart)
3630     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3631 
3632   return ABIArgInfo::getDirect(ResType);
3633 }
3634 
3635 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
3636   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3637   bool isNamedArg)
3638   const
3639 {
3640   Ty = useFirstFieldIfTransparentUnion(Ty);
3641 
3642   X86_64ABIInfo::Class Lo, Hi;
3643   classify(Ty, 0, Lo, Hi, isNamedArg);
3644 
3645   // Check some invariants.
3646   // FIXME: Enforce these by construction.
3647   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3648   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3649 
3650   neededInt = 0;
3651   neededSSE = 0;
3652   llvm::Type *ResType = nullptr;
3653   switch (Lo) {
3654   case NoClass:
3655     if (Hi == NoClass)
3656       return ABIArgInfo::getIgnore();
3657     // If the low part is just padding, it takes no register, leave ResType
3658     // null.
3659     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3660            "Unknown missing lo part");
3661     break;
3662 
3663     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3664     // on the stack.
3665   case Memory:
3666 
3667     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3668     // COMPLEX_X87, it is passed in memory.
3669   case X87:
3670   case ComplexX87:
3671     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3672       ++neededInt;
3673     return getIndirectResult(Ty, freeIntRegs);
3674 
3675   case SSEUp:
3676   case X87Up:
3677     llvm_unreachable("Invalid classification for lo word.");
3678 
3679     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3680     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3681     // and %r9 is used.
3682   case Integer:
3683     ++neededInt;
3684 
3685     // Pick an 8-byte type based on the preferred type.
3686     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3687 
3688     // If we have a sign or zero extended integer, make sure to return Extend
3689     // so that the parameter gets the right LLVM IR attributes.
3690     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3691       // Treat an enum type as its underlying type.
3692       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3693         Ty = EnumTy->getDecl()->getIntegerType();
3694 
3695       if (Ty->isIntegralOrEnumerationType() &&
3696           isPromotableIntegerTypeForABI(Ty))
3697         return ABIArgInfo::getExtend(Ty);
3698     }
3699 
3700     break;
3701 
3702     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3703     // available SSE register is used, the registers are taken in the
3704     // order from %xmm0 to %xmm7.
3705   case SSE: {
3706     llvm::Type *IRType = CGT.ConvertType(Ty);
3707     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3708     ++neededSSE;
3709     break;
3710   }
3711   }
3712 
3713   llvm::Type *HighPart = nullptr;
3714   switch (Hi) {
3715     // Memory was handled previously, ComplexX87 and X87 should
3716     // never occur as hi classes, and X87Up must be preceded by X87,
3717     // which is passed in memory.
3718   case Memory:
3719   case X87:
3720   case ComplexX87:
3721     llvm_unreachable("Invalid classification for hi word.");
3722 
3723   case NoClass: break;
3724 
3725   case Integer:
3726     ++neededInt;
3727     // Pick an 8-byte type based on the preferred type.
3728     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3729 
3730     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3731       return ABIArgInfo::getDirect(HighPart, 8);
3732     break;
3733 
3734     // X87Up generally doesn't occur here (long double is passed in
3735     // memory), except in situations involving unions.
3736   case X87Up:
3737   case SSE:
3738     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3739 
3740     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3741       return ABIArgInfo::getDirect(HighPart, 8);
3742 
3743     ++neededSSE;
3744     break;
3745 
3746     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3747     // eightbyte is passed in the upper half of the last used SSE
3748     // register.  This only happens when 128-bit vectors are passed.
3749   case SSEUp:
3750     assert(Lo == SSE && "Unexpected SSEUp classification");
3751     ResType = GetByteVectorType(Ty);
3752     break;
3753   }
3754 
3755   // If a high part was specified, merge it together with the low part.  It is
3756   // known to pass in the high eightbyte of the result.  We do this by forming a
3757   // first class struct aggregate with the high and low part: {low, high}
3758   if (HighPart)
3759     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3760 
3761   return ABIArgInfo::getDirect(ResType);
3762 }
3763 
3764 ABIArgInfo
3765 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3766                                              unsigned &NeededSSE) const {
3767   auto RT = Ty->getAs<RecordType>();
3768   assert(RT && "classifyRegCallStructType only valid with struct types");
3769 
3770   if (RT->getDecl()->hasFlexibleArrayMember())
3771     return getIndirectReturnResult(Ty);
3772 
3773   // Sum up bases
3774   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3775     if (CXXRD->isDynamicClass()) {
3776       NeededInt = NeededSSE = 0;
3777       return getIndirectReturnResult(Ty);
3778     }
3779 
3780     for (const auto &I : CXXRD->bases())
3781       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3782               .isIndirect()) {
3783         NeededInt = NeededSSE = 0;
3784         return getIndirectReturnResult(Ty);
3785       }
3786   }
3787 
3788   // Sum up members
3789   for (const auto *FD : RT->getDecl()->fields()) {
3790     if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3791       if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3792               .isIndirect()) {
3793         NeededInt = NeededSSE = 0;
3794         return getIndirectReturnResult(Ty);
3795       }
3796     } else {
3797       unsigned LocalNeededInt, LocalNeededSSE;
3798       if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3799                                LocalNeededSSE, true)
3800               .isIndirect()) {
3801         NeededInt = NeededSSE = 0;
3802         return getIndirectReturnResult(Ty);
3803       }
3804       NeededInt += LocalNeededInt;
3805       NeededSSE += LocalNeededSSE;
3806     }
3807   }
3808 
3809   return ABIArgInfo::getDirect();
3810 }
3811 
3812 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3813                                                     unsigned &NeededInt,
3814                                                     unsigned &NeededSSE) const {
3815 
3816   NeededInt = 0;
3817   NeededSSE = 0;
3818 
3819   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3820 }
3821 
3822 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3823 
3824   const unsigned CallingConv = FI.getCallingConvention();
3825   // It is possible to force Win64 calling convention on any x86_64 target by
3826   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3827   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3828   if (CallingConv == llvm::CallingConv::Win64) {
3829     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3830     Win64ABIInfo.computeInfo(FI);
3831     return;
3832   }
3833 
3834   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3835 
3836   // Keep track of the number of assigned registers.
3837   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3838   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3839   unsigned NeededInt, NeededSSE;
3840 
3841   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3842     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3843         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3844       FI.getReturnInfo() =
3845           classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3846       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3847         FreeIntRegs -= NeededInt;
3848         FreeSSERegs -= NeededSSE;
3849       } else {
3850         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3851       }
3852     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
3853                getContext().getCanonicalType(FI.getReturnType()
3854                                                  ->getAs<ComplexType>()
3855                                                  ->getElementType()) ==
3856                    getContext().LongDoubleTy)
3857       // Complex Long Double Type is passed in Memory when Regcall
3858       // calling convention is used.
3859       FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3860     else
3861       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3862   }
3863 
3864   // If the return value is indirect, then the hidden argument is consuming one
3865   // integer register.
3866   if (FI.getReturnInfo().isIndirect())
3867     --FreeIntRegs;
3868 
3869   // The chain argument effectively gives us another free register.
3870   if (FI.isChainCall())
3871     ++FreeIntRegs;
3872 
3873   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3874   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3875   // get assigned (in left-to-right order) for passing as follows...
3876   unsigned ArgNo = 0;
3877   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3878        it != ie; ++it, ++ArgNo) {
3879     bool IsNamedArg = ArgNo < NumRequiredArgs;
3880 
3881     if (IsRegCall && it->type->isStructureOrClassType())
3882       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3883     else
3884       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3885                                       NeededSSE, IsNamedArg);
3886 
3887     // AMD64-ABI 3.2.3p3: If there are no registers available for any
3888     // eightbyte of an argument, the whole argument is passed on the
3889     // stack. If registers have already been assigned for some
3890     // eightbytes of such an argument, the assignments get reverted.
3891     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3892       FreeIntRegs -= NeededInt;
3893       FreeSSERegs -= NeededSSE;
3894     } else {
3895       it->info = getIndirectResult(it->type, FreeIntRegs);
3896     }
3897   }
3898 }
3899 
3900 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3901                                          Address VAListAddr, QualType Ty) {
3902   Address overflow_arg_area_p =
3903       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
3904   llvm::Value *overflow_arg_area =
3905     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3906 
3907   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3908   // byte boundary if alignment needed by type exceeds 8 byte boundary.
3909   // It isn't stated explicitly in the standard, but in practice we use
3910   // alignment greater than 16 where necessary.
3911   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3912   if (Align > CharUnits::fromQuantity(8)) {
3913     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3914                                                       Align);
3915   }
3916 
3917   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
3918   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3919   llvm::Value *Res =
3920     CGF.Builder.CreateBitCast(overflow_arg_area,
3921                               llvm::PointerType::getUnqual(LTy));
3922 
3923   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3924   // l->overflow_arg_area + sizeof(type).
3925   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3926   // an 8 byte boundary.
3927 
3928   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
3929   llvm::Value *Offset =
3930       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
3931   overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3932                                             "overflow_arg_area.next");
3933   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3934 
3935   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
3936   return Address(Res, Align);
3937 }
3938 
3939 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3940                                  QualType Ty) const {
3941   // Assume that va_list type is correct; should be pointer to LLVM type:
3942   // struct {
3943   //   i32 gp_offset;
3944   //   i32 fp_offset;
3945   //   i8* overflow_arg_area;
3946   //   i8* reg_save_area;
3947   // };
3948   unsigned neededInt, neededSSE;
3949 
3950   Ty = getContext().getCanonicalType(Ty);
3951   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
3952                                        /*isNamedArg*/false);
3953 
3954   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3955   // in the registers. If not go to step 7.
3956   if (!neededInt && !neededSSE)
3957     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3958 
3959   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3960   // general purpose registers needed to pass type and num_fp to hold
3961   // the number of floating point registers needed.
3962 
3963   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3964   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3965   // l->fp_offset > 304 - num_fp * 16 go to step 7.
3966   //
3967   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3968   // register save space).
3969 
3970   llvm::Value *InRegs = nullptr;
3971   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3972   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
3973   if (neededInt) {
3974     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
3975     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
3976     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3977     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
3978   }
3979 
3980   if (neededSSE) {
3981     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
3982     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3983     llvm::Value *FitsInFP =
3984       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3985     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
3986     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3987   }
3988 
3989   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3990   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3991   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3992   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3993 
3994   // Emit code to load the value if it was passed in registers.
3995 
3996   CGF.EmitBlock(InRegBlock);
3997 
3998   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3999   // an offset of l->gp_offset and/or l->fp_offset. This may require
4000   // copying to a temporary location in case the parameter is passed
4001   // in different register classes or requires an alignment greater
4002   // than 8 for general purpose registers and 16 for XMM registers.
4003   //
4004   // FIXME: This really results in shameful code when we end up needing to
4005   // collect arguments from different places; often what should result in a
4006   // simple assembling of a structure from scattered addresses has many more
4007   // loads than necessary. Can we clean this up?
4008   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4009   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
4010       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
4011 
4012   Address RegAddr = Address::invalid();
4013   if (neededInt && neededSSE) {
4014     // FIXME: Cleanup.
4015     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
4016     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
4017     Address Tmp = CGF.CreateMemTemp(Ty);
4018     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4019     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
4020     llvm::Type *TyLo = ST->getElementType(0);
4021     llvm::Type *TyHi = ST->getElementType(1);
4022     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
4023            "Unexpected ABI info for mixed regs");
4024     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
4025     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
4026     llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
4027     llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
4028     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
4029     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
4030 
4031     // Copy the first element.
4032     // FIXME: Our choice of alignment here and below is probably pessimistic.
4033     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
4034         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
4035         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
4036     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4037 
4038     // Copy the second element.
4039     V = CGF.Builder.CreateAlignedLoad(
4040         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
4041         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
4042     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4043 
4044     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4045   } else if (neededInt) {
4046     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
4047                       CharUnits::fromQuantity(8));
4048     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4049 
4050     // Copy to a temporary if necessary to ensure the appropriate alignment.
4051     std::pair<CharUnits, CharUnits> SizeAlign =
4052         getContext().getTypeInfoInChars(Ty);
4053     uint64_t TySize = SizeAlign.first.getQuantity();
4054     CharUnits TyAlign = SizeAlign.second;
4055 
4056     // Copy into a temporary if the type is more aligned than the
4057     // register save area.
4058     if (TyAlign.getQuantity() > 8) {
4059       Address Tmp = CGF.CreateMemTemp(Ty);
4060       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
4061       RegAddr = Tmp;
4062     }
4063 
4064   } else if (neededSSE == 1) {
4065     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
4066                       CharUnits::fromQuantity(16));
4067     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4068   } else {
4069     assert(neededSSE == 2 && "Invalid number of needed registers!");
4070     // SSE registers are spaced 16 bytes apart in the register save
4071     // area, we need to collect the two eightbytes together.
4072     // The ABI isn't explicit about this, but it seems reasonable
4073     // to assume that the slots are 16-byte aligned, since the stack is
4074     // naturally 16-byte aligned and the prologue is expected to store
4075     // all the SSE registers to the RSA.
4076     Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
4077                                 CharUnits::fromQuantity(16));
4078     Address RegAddrHi =
4079       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
4080                                              CharUnits::fromQuantity(16));
4081     llvm::Type *ST = AI.canHaveCoerceToType()
4082                          ? AI.getCoerceToType()
4083                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
4084     llvm::Value *V;
4085     Address Tmp = CGF.CreateMemTemp(Ty);
4086     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4087     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4088         RegAddrLo, ST->getStructElementType(0)));
4089     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4090     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4091         RegAddrHi, ST->getStructElementType(1)));
4092     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4093 
4094     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4095   }
4096 
4097   // AMD64-ABI 3.5.7p5: Step 5. Set:
4098   // l->gp_offset = l->gp_offset + num_gp * 8
4099   // l->fp_offset = l->fp_offset + num_fp * 16.
4100   if (neededInt) {
4101     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
4102     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
4103                             gp_offset_p);
4104   }
4105   if (neededSSE) {
4106     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
4107     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
4108                             fp_offset_p);
4109   }
4110   CGF.EmitBranch(ContBlock);
4111 
4112   // Emit code to load the value if it was passed in memory.
4113 
4114   CGF.EmitBlock(InMemBlock);
4115   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4116 
4117   // Return the appropriate result.
4118 
4119   CGF.EmitBlock(ContBlock);
4120   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
4121                                  "vaarg.addr");
4122   return ResAddr;
4123 }
4124 
4125 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4126                                    QualType Ty) const {
4127   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
4128                           CGF.getContext().getTypeInfoInChars(Ty),
4129                           CharUnits::fromQuantity(8),
4130                           /*allowHigherAlign*/ false);
4131 }
4132 
4133 ABIArgInfo
4134 WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
4135                                     const ABIArgInfo &current) const {
4136   // Assumes vectorCall calling convention.
4137   const Type *Base = nullptr;
4138   uint64_t NumElts = 0;
4139 
4140   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
4141       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
4142     FreeSSERegs -= NumElts;
4143     return getDirectX86Hva();
4144   }
4145   return current;
4146 }
4147 
4148 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
4149                                       bool IsReturnType, bool IsVectorCall,
4150                                       bool IsRegCall) const {
4151 
4152   if (Ty->isVoidType())
4153     return ABIArgInfo::getIgnore();
4154 
4155   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4156     Ty = EnumTy->getDecl()->getIntegerType();
4157 
4158   TypeInfo Info = getContext().getTypeInfo(Ty);
4159   uint64_t Width = Info.Width;
4160   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
4161 
4162   const RecordType *RT = Ty->getAs<RecordType>();
4163   if (RT) {
4164     if (!IsReturnType) {
4165       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
4166         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4167     }
4168 
4169     if (RT->getDecl()->hasFlexibleArrayMember())
4170       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4171 
4172   }
4173 
4174   const Type *Base = nullptr;
4175   uint64_t NumElts = 0;
4176   // vectorcall adds the concept of a homogenous vector aggregate, similar to
4177   // other targets.
4178   if ((IsVectorCall || IsRegCall) &&
4179       isHomogeneousAggregate(Ty, Base, NumElts)) {
4180     if (IsRegCall) {
4181       if (FreeSSERegs >= NumElts) {
4182         FreeSSERegs -= NumElts;
4183         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
4184           return ABIArgInfo::getDirect();
4185         return ABIArgInfo::getExpand();
4186       }
4187       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4188     } else if (IsVectorCall) {
4189       if (FreeSSERegs >= NumElts &&
4190           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
4191         FreeSSERegs -= NumElts;
4192         return ABIArgInfo::getDirect();
4193       } else if (IsReturnType) {
4194         return ABIArgInfo::getExpand();
4195       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
4196         // HVAs are delayed and reclassified in the 2nd step.
4197         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4198       }
4199     }
4200   }
4201 
4202   if (Ty->isMemberPointerType()) {
4203     // If the member pointer is represented by an LLVM int or ptr, pass it
4204     // directly.
4205     llvm::Type *LLTy = CGT.ConvertType(Ty);
4206     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
4207       return ABIArgInfo::getDirect();
4208   }
4209 
4210   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
4211     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4212     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4213     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4214       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4215 
4216     // Otherwise, coerce it to a small integer.
4217     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4218   }
4219 
4220   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4221     switch (BT->getKind()) {
4222     case BuiltinType::Bool:
4223       // Bool type is always extended to the ABI, other builtin types are not
4224       // extended.
4225       return ABIArgInfo::getExtend(Ty);
4226 
4227     case BuiltinType::LongDouble:
4228       // Mingw64 GCC uses the old 80 bit extended precision floating point
4229       // unit. It passes them indirectly through memory.
4230       if (IsMingw64) {
4231         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4232         if (LDF == &llvm::APFloat::x87DoubleExtended())
4233           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4234       }
4235       break;
4236 
4237     case BuiltinType::Int128:
4238     case BuiltinType::UInt128:
4239       // If it's a parameter type, the normal ABI rule is that arguments larger
4240       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4241       // even though it isn't particularly efficient.
4242       if (!IsReturnType)
4243         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4244 
4245       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4246       // Clang matches them for compatibility.
4247       return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
4248           llvm::Type::getInt64Ty(getVMContext()), 2));
4249 
4250     default:
4251       break;
4252     }
4253   }
4254 
4255   if (Ty->isExtIntType()) {
4256     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4257     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4258     // However, non-power-of-two _ExtInts will be passed as 1,2,4 or 8 bytes
4259     // anyway as long is it fits in them, so we don't have to check the power of
4260     // 2.
4261     if (Width <= 64)
4262       return ABIArgInfo::getDirect();
4263     return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4264   }
4265 
4266   return ABIArgInfo::getDirect();
4267 }
4268 
4269 void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
4270                                              unsigned FreeSSERegs,
4271                                              bool IsVectorCall,
4272                                              bool IsRegCall) const {
4273   unsigned Count = 0;
4274   for (auto &I : FI.arguments()) {
4275     // Vectorcall in x64 only permits the first 6 arguments to be passed
4276     // as XMM/YMM registers.
4277     if (Count < VectorcallMaxParamNumAsReg)
4278       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4279     else {
4280       // Since these cannot be passed in registers, pretend no registers
4281       // are left.
4282       unsigned ZeroSSERegsAvail = 0;
4283       I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
4284                         IsVectorCall, IsRegCall);
4285     }
4286     ++Count;
4287   }
4288 
4289   for (auto &I : FI.arguments()) {
4290     I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
4291   }
4292 }
4293 
4294 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4295   const unsigned CC = FI.getCallingConvention();
4296   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4297   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4298 
4299   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4300   // classification rules.
4301   if (CC == llvm::CallingConv::X86_64_SysV) {
4302     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4303     SysVABIInfo.computeInfo(FI);
4304     return;
4305   }
4306 
4307   unsigned FreeSSERegs = 0;
4308   if (IsVectorCall) {
4309     // We can use up to 4 SSE return registers with vectorcall.
4310     FreeSSERegs = 4;
4311   } else if (IsRegCall) {
4312     // RegCall gives us 16 SSE registers.
4313     FreeSSERegs = 16;
4314   }
4315 
4316   if (!getCXXABI().classifyReturnType(FI))
4317     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4318                                   IsVectorCall, IsRegCall);
4319 
4320   if (IsVectorCall) {
4321     // We can use up to 6 SSE register parameters with vectorcall.
4322     FreeSSERegs = 6;
4323   } else if (IsRegCall) {
4324     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4325     FreeSSERegs = 16;
4326   }
4327 
4328   if (IsVectorCall) {
4329     computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
4330   } else {
4331     for (auto &I : FI.arguments())
4332       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4333   }
4334 
4335 }
4336 
4337 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4338                                     QualType Ty) const {
4339 
4340   bool IsIndirect = false;
4341 
4342   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4343   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4344   if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
4345     uint64_t Width = getContext().getTypeSize(Ty);
4346     IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4347   }
4348 
4349   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4350                           CGF.getContext().getTypeInfoInChars(Ty),
4351                           CharUnits::fromQuantity(8),
4352                           /*allowHigherAlign*/ false);
4353 }
4354 
4355 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4356                                         llvm::Value *Address, bool Is64Bit,
4357                                         bool IsAIX) {
4358   // This is calculated from the LLVM and GCC tables and verified
4359   // against gcc output.  AFAIK all PPC ABIs use the same encoding.
4360 
4361   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4362 
4363   llvm::IntegerType *i8 = CGF.Int8Ty;
4364   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4365   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4366   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4367 
4368   // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
4369   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
4370 
4371   // 32-63: fp0-31, the 8-byte floating-point registers
4372   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4373 
4374   // 64-67 are various 4-byte or 8-byte special-purpose registers:
4375   // 64: mq
4376   // 65: lr
4377   // 66: ctr
4378   // 67: ap
4379   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
4380 
4381   // 68-76 are various 4-byte special-purpose registers:
4382   // 68-75 cr0-7
4383   // 76: xer
4384   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4385 
4386   // 77-108: v0-31, the 16-byte vector registers
4387   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4388 
4389   // 109: vrsave
4390   // 110: vscr
4391   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
4392 
4393   // AIX does not utilize the rest of the registers.
4394   if (IsAIX)
4395     return false;
4396 
4397   // 111: spe_acc
4398   // 112: spefscr
4399   // 113: sfp
4400   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
4401 
4402   if (!Is64Bit)
4403     return false;
4404 
4405   // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
4406   // or above CPU.
4407   // 64-bit only registers:
4408   // 114: tfhar
4409   // 115: tfiar
4410   // 116: texasr
4411   AssignToArrayRange(Builder, Address, Eight8, 114, 116);
4412 
4413   return false;
4414 }
4415 
4416 // AIX
4417 namespace {
4418 /// AIXABIInfo - The AIX XCOFF ABI information.
4419 class AIXABIInfo : public ABIInfo {
4420   const bool Is64Bit;
4421   const unsigned PtrByteSize;
4422   CharUnits getParamTypeAlignment(QualType Ty) const;
4423 
4424 public:
4425   AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4426       : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
4427 
4428   bool isPromotableTypeForABI(QualType Ty) const;
4429 
4430   ABIArgInfo classifyReturnType(QualType RetTy) const;
4431   ABIArgInfo classifyArgumentType(QualType Ty) const;
4432 
4433   void computeInfo(CGFunctionInfo &FI) const override {
4434     if (!getCXXABI().classifyReturnType(FI))
4435       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4436 
4437     for (auto &I : FI.arguments())
4438       I.info = classifyArgumentType(I.type);
4439   }
4440 
4441   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4442                     QualType Ty) const override;
4443 };
4444 
4445 class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
4446   const bool Is64Bit;
4447 
4448 public:
4449   AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4450       : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
4451         Is64Bit(Is64Bit) {}
4452   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4453     return 1; // r1 is the dedicated stack pointer
4454   }
4455 
4456   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4457                                llvm::Value *Address) const override;
4458 };
4459 } // namespace
4460 
4461 // Return true if the ABI requires Ty to be passed sign- or zero-
4462 // extended to 32/64 bits.
4463 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
4464   // Treat an enum type as its underlying type.
4465   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4466     Ty = EnumTy->getDecl()->getIntegerType();
4467 
4468   // Promotable integer types are required to be promoted by the ABI.
4469   if (Ty->isPromotableIntegerType())
4470     return true;
4471 
4472   if (!Is64Bit)
4473     return false;
4474 
4475   // For 64 bit mode, in addition to the usual promotable integer types, we also
4476   // need to extend all 32-bit types, since the ABI requires promotion to 64
4477   // bits.
4478   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4479     switch (BT->getKind()) {
4480     case BuiltinType::Int:
4481     case BuiltinType::UInt:
4482       return true;
4483     default:
4484       break;
4485     }
4486 
4487   return false;
4488 }
4489 
4490 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
4491   if (RetTy->isAnyComplexType())
4492     llvm::report_fatal_error("complex type is not supported on AIX yet");
4493 
4494   if (RetTy->isVectorType())
4495     llvm::report_fatal_error("vector type is not supported on AIX yet");
4496 
4497   if (RetTy->isVoidType())
4498     return ABIArgInfo::getIgnore();
4499 
4500   // TODO:  Evaluate if AIX power alignment rule would have an impact on the
4501   // alignment here.
4502   if (isAggregateTypeForABI(RetTy))
4503     return getNaturalAlignIndirect(RetTy);
4504 
4505   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4506                                         : ABIArgInfo::getDirect());
4507 }
4508 
4509 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
4510   Ty = useFirstFieldIfTransparentUnion(Ty);
4511 
4512   if (Ty->isAnyComplexType())
4513     llvm::report_fatal_error("complex type is not supported on AIX yet");
4514 
4515   if (Ty->isVectorType())
4516     llvm::report_fatal_error("vector type is not supported on AIX yet");
4517 
4518   // TODO:  Evaluate if AIX power alignment rule would have an impact on the
4519   // alignment here.
4520   if (isAggregateTypeForABI(Ty)) {
4521     // Records with non-trivial destructors/copy-constructors should not be
4522     // passed by value.
4523     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4524       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4525 
4526     CharUnits CCAlign = getParamTypeAlignment(Ty);
4527     CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
4528 
4529     return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
4530                                    /*Realign*/ TyAlign > CCAlign);
4531   }
4532 
4533   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4534                                      : ABIArgInfo::getDirect());
4535 }
4536 
4537 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
4538   if (Ty->isAnyComplexType())
4539     llvm::report_fatal_error("complex type is not supported on AIX yet");
4540 
4541   if (Ty->isVectorType())
4542     llvm::report_fatal_error("vector type is not supported on AIX yet");
4543 
4544   // If the structure contains a vector type, the alignment is 16.
4545   if (isRecordWithSIMDVectorType(getContext(), Ty))
4546     return CharUnits::fromQuantity(16);
4547 
4548   return CharUnits::fromQuantity(PtrByteSize);
4549 }
4550 
4551 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4552                               QualType Ty) const {
4553   if (Ty->isAnyComplexType())
4554     llvm::report_fatal_error("complex type is not supported on AIX yet");
4555 
4556   if (Ty->isVectorType())
4557     llvm::report_fatal_error("vector type is not supported on AIX yet");
4558 
4559   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4560   TypeInfo.second = getParamTypeAlignment(Ty);
4561 
4562   CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
4563 
4564   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
4565                           SlotSize, /*AllowHigher*/ true);
4566 }
4567 
4568 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
4569     CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
4570   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
4571 }
4572 
4573 // PowerPC-32
4574 namespace {
4575 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4576 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4577   bool IsSoftFloatABI;
4578   bool IsRetSmallStructInRegABI;
4579 
4580   CharUnits getParamTypeAlignment(QualType Ty) const;
4581 
4582 public:
4583   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
4584                      bool RetSmallStructInRegABI)
4585       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
4586         IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
4587 
4588   ABIArgInfo classifyReturnType(QualType RetTy) const;
4589 
4590   void computeInfo(CGFunctionInfo &FI) const override {
4591     if (!getCXXABI().classifyReturnType(FI))
4592       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4593     for (auto &I : FI.arguments())
4594       I.info = classifyArgumentType(I.type);
4595   }
4596 
4597   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4598                     QualType Ty) const override;
4599 };
4600 
4601 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4602 public:
4603   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
4604                          bool RetSmallStructInRegABI)
4605       : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
4606             CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
4607 
4608   static bool isStructReturnInRegABI(const llvm::Triple &Triple,
4609                                      const CodeGenOptions &Opts);
4610 
4611   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4612     // This is recovered from gcc output.
4613     return 1; // r1 is the dedicated stack pointer
4614   }
4615 
4616   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4617                                llvm::Value *Address) const override;
4618 };
4619 }
4620 
4621 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4622   // Complex types are passed just like their elements.
4623   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4624     Ty = CTy->getElementType();
4625 
4626   if (Ty->isVectorType())
4627     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4628                                                                        : 4);
4629 
4630   // For single-element float/vector structs, we consider the whole type
4631   // to have the same alignment requirements as its single element.
4632   const Type *AlignTy = nullptr;
4633   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4634     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4635     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4636         (BT && BT->isFloatingPoint()))
4637       AlignTy = EltType;
4638   }
4639 
4640   if (AlignTy)
4641     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4642   return CharUnits::fromQuantity(4);
4643 }
4644 
4645 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4646   uint64_t Size;
4647 
4648   // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
4649   if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
4650       (Size = getContext().getTypeSize(RetTy)) <= 64) {
4651     // System V ABI (1995), page 3-22, specified:
4652     // > A structure or union whose size is less than or equal to 8 bytes
4653     // > shall be returned in r3 and r4, as if it were first stored in the
4654     // > 8-byte aligned memory area and then the low addressed word were
4655     // > loaded into r3 and the high-addressed word into r4.  Bits beyond
4656     // > the last member of the structure or union are not defined.
4657     //
4658     // GCC for big-endian PPC32 inserts the pad before the first member,
4659     // not "beyond the last member" of the struct.  To stay compatible
4660     // with GCC, we coerce the struct to an integer of the same size.
4661     // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
4662     if (Size == 0)
4663       return ABIArgInfo::getIgnore();
4664     else {
4665       llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
4666       return ABIArgInfo::getDirect(CoerceTy);
4667     }
4668   }
4669 
4670   return DefaultABIInfo::classifyReturnType(RetTy);
4671 }
4672 
4673 // TODO: this implementation is now likely redundant with
4674 // DefaultABIInfo::EmitVAArg.
4675 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4676                                       QualType Ty) const {
4677   if (getTarget().getTriple().isOSDarwin()) {
4678     auto TI = getContext().getTypeInfoInChars(Ty);
4679     TI.second = getParamTypeAlignment(Ty);
4680 
4681     CharUnits SlotSize = CharUnits::fromQuantity(4);
4682     return emitVoidPtrVAArg(CGF, VAList, Ty,
4683                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4684                             /*AllowHigherAlign=*/true);
4685   }
4686 
4687   const unsigned OverflowLimit = 8;
4688   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4689     // TODO: Implement this. For now ignore.
4690     (void)CTy;
4691     return Address::invalid(); // FIXME?
4692   }
4693 
4694   // struct __va_list_tag {
4695   //   unsigned char gpr;
4696   //   unsigned char fpr;
4697   //   unsigned short reserved;
4698   //   void *overflow_arg_area;
4699   //   void *reg_save_area;
4700   // };
4701 
4702   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4703   bool isInt =
4704       Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
4705   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4706 
4707   // All aggregates are passed indirectly?  That doesn't seem consistent
4708   // with the argument-lowering code.
4709   bool isIndirect = Ty->isAggregateType();
4710 
4711   CGBuilderTy &Builder = CGF.Builder;
4712 
4713   // The calling convention either uses 1-2 GPRs or 1 FPR.
4714   Address NumRegsAddr = Address::invalid();
4715   if (isInt || IsSoftFloatABI) {
4716     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4717   } else {
4718     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4719   }
4720 
4721   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4722 
4723   // "Align" the register count when TY is i64.
4724   if (isI64 || (isF64 && IsSoftFloatABI)) {
4725     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4726     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4727   }
4728 
4729   llvm::Value *CC =
4730       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4731 
4732   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4733   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4734   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4735 
4736   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4737 
4738   llvm::Type *DirectTy = CGF.ConvertType(Ty);
4739   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4740 
4741   // Case 1: consume registers.
4742   Address RegAddr = Address::invalid();
4743   {
4744     CGF.EmitBlock(UsingRegs);
4745 
4746     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4747     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4748                       CharUnits::fromQuantity(8));
4749     assert(RegAddr.getElementType() == CGF.Int8Ty);
4750 
4751     // Floating-point registers start after the general-purpose registers.
4752     if (!(isInt || IsSoftFloatABI)) {
4753       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4754                                                    CharUnits::fromQuantity(32));
4755     }
4756 
4757     // Get the address of the saved value by scaling the number of
4758     // registers we've used by the number of
4759     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4760     llvm::Value *RegOffset =
4761       Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4762     RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4763                                             RegAddr.getPointer(), RegOffset),
4764                       RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4765     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4766 
4767     // Increase the used-register count.
4768     NumRegs =
4769       Builder.CreateAdd(NumRegs,
4770                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4771     Builder.CreateStore(NumRegs, NumRegsAddr);
4772 
4773     CGF.EmitBranch(Cont);
4774   }
4775 
4776   // Case 2: consume space in the overflow area.
4777   Address MemAddr = Address::invalid();
4778   {
4779     CGF.EmitBlock(UsingOverflow);
4780 
4781     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4782 
4783     // Everything in the overflow area is rounded up to a size of at least 4.
4784     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4785 
4786     CharUnits Size;
4787     if (!isIndirect) {
4788       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4789       Size = TypeInfo.first.alignTo(OverflowAreaAlign);
4790     } else {
4791       Size = CGF.getPointerSize();
4792     }
4793 
4794     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4795     Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
4796                          OverflowAreaAlign);
4797     // Round up address of argument to alignment
4798     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4799     if (Align > OverflowAreaAlign) {
4800       llvm::Value *Ptr = OverflowArea.getPointer();
4801       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4802                                                            Align);
4803     }
4804 
4805     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4806 
4807     // Increase the overflow area.
4808     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4809     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4810     CGF.EmitBranch(Cont);
4811   }
4812 
4813   CGF.EmitBlock(Cont);
4814 
4815   // Merge the cases with a phi.
4816   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4817                                 "vaarg.addr");
4818 
4819   // Load the pointer if the argument was passed indirectly.
4820   if (isIndirect) {
4821     Result = Address(Builder.CreateLoad(Result, "aggr"),
4822                      getContext().getTypeAlignInChars(Ty));
4823   }
4824 
4825   return Result;
4826 }
4827 
4828 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
4829     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4830   assert(Triple.getArch() == llvm::Triple::ppc);
4831 
4832   switch (Opts.getStructReturnConvention()) {
4833   case CodeGenOptions::SRCK_Default:
4834     break;
4835   case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
4836     return false;
4837   case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
4838     return true;
4839   }
4840 
4841   if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
4842     return true;
4843 
4844   return false;
4845 }
4846 
4847 bool
4848 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4849                                                 llvm::Value *Address) const {
4850   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
4851                                      /*IsAIX*/ false);
4852 }
4853 
4854 // PowerPC-64
4855 
4856 namespace {
4857 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4858 class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
4859 public:
4860   enum ABIKind {
4861     ELFv1 = 0,
4862     ELFv2
4863   };
4864 
4865 private:
4866   static const unsigned GPRBits = 64;
4867   ABIKind Kind;
4868   bool HasQPX;
4869   bool IsSoftFloatABI;
4870 
4871   // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4872   // will be passed in a QPX register.
4873   bool IsQPXVectorTy(const Type *Ty) const {
4874     if (!HasQPX)
4875       return false;
4876 
4877     if (const VectorType *VT = Ty->getAs<VectorType>()) {
4878       unsigned NumElements = VT->getNumElements();
4879       if (NumElements == 1)
4880         return false;
4881 
4882       if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4883         if (getContext().getTypeSize(Ty) <= 256)
4884           return true;
4885       } else if (VT->getElementType()->
4886                    isSpecificBuiltinType(BuiltinType::Float)) {
4887         if (getContext().getTypeSize(Ty) <= 128)
4888           return true;
4889       }
4890     }
4891 
4892     return false;
4893   }
4894 
4895   bool IsQPXVectorTy(QualType Ty) const {
4896     return IsQPXVectorTy(Ty.getTypePtr());
4897   }
4898 
4899 public:
4900   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4901                      bool SoftFloatABI)
4902       : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4903         IsSoftFloatABI(SoftFloatABI) {}
4904 
4905   bool isPromotableTypeForABI(QualType Ty) const;
4906   CharUnits getParamTypeAlignment(QualType Ty) const;
4907 
4908   ABIArgInfo classifyReturnType(QualType RetTy) const;
4909   ABIArgInfo classifyArgumentType(QualType Ty) const;
4910 
4911   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4912   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4913                                          uint64_t Members) const override;
4914 
4915   // TODO: We can add more logic to computeInfo to improve performance.
4916   // Example: For aggregate arguments that fit in a register, we could
4917   // use getDirectInReg (as is done below for structs containing a single
4918   // floating-point value) to avoid pushing them to memory on function
4919   // entry.  This would require changing the logic in PPCISelLowering
4920   // when lowering the parameters in the caller and args in the callee.
4921   void computeInfo(CGFunctionInfo &FI) const override {
4922     if (!getCXXABI().classifyReturnType(FI))
4923       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4924     for (auto &I : FI.arguments()) {
4925       // We rely on the default argument classification for the most part.
4926       // One exception:  An aggregate containing a single floating-point
4927       // or vector item must be passed in a register if one is available.
4928       const Type *T = isSingleElementStruct(I.type, getContext());
4929       if (T) {
4930         const BuiltinType *BT = T->getAs<BuiltinType>();
4931         if (IsQPXVectorTy(T) ||
4932             (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
4933             (BT && BT->isFloatingPoint())) {
4934           QualType QT(T, 0);
4935           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
4936           continue;
4937         }
4938       }
4939       I.info = classifyArgumentType(I.type);
4940     }
4941   }
4942 
4943   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4944                     QualType Ty) const override;
4945 
4946   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
4947                                     bool asReturnValue) const override {
4948     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4949   }
4950 
4951   bool isSwiftErrorInRegister() const override {
4952     return false;
4953   }
4954 };
4955 
4956 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
4957 
4958 public:
4959   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
4960                                PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4961                                bool SoftFloatABI)
4962       : TargetCodeGenInfo(std::make_unique<PPC64_SVR4_ABIInfo>(
4963             CGT, Kind, HasQPX, SoftFloatABI)) {}
4964 
4965   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4966     // This is recovered from gcc output.
4967     return 1; // r1 is the dedicated stack pointer
4968   }
4969 
4970   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4971                                llvm::Value *Address) const override;
4972 };
4973 
4974 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4975 public:
4976   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4977 
4978   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4979     // This is recovered from gcc output.
4980     return 1; // r1 is the dedicated stack pointer
4981   }
4982 
4983   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4984                                llvm::Value *Address) const override;
4985 };
4986 
4987 }
4988 
4989 // Return true if the ABI requires Ty to be passed sign- or zero-
4990 // extended to 64 bits.
4991 bool
4992 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4993   // Treat an enum type as its underlying type.
4994   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4995     Ty = EnumTy->getDecl()->getIntegerType();
4996 
4997   // Promotable integer types are required to be promoted by the ABI.
4998   if (isPromotableIntegerTypeForABI(Ty))
4999     return true;
5000 
5001   // In addition to the usual promotable integer types, we also need to
5002   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
5003   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5004     switch (BT->getKind()) {
5005     case BuiltinType::Int:
5006     case BuiltinType::UInt:
5007       return true;
5008     default:
5009       break;
5010     }
5011 
5012   if (const auto *EIT = Ty->getAs<ExtIntType>())
5013     if (EIT->getNumBits() < 64)
5014       return true;
5015 
5016   return false;
5017 }
5018 
5019 /// isAlignedParamType - Determine whether a type requires 16-byte or
5020 /// higher alignment in the parameter area.  Always returns at least 8.
5021 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
5022   // Complex types are passed just like their elements.
5023   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
5024     Ty = CTy->getElementType();
5025 
5026   // Only vector types of size 16 bytes need alignment (larger types are
5027   // passed via reference, smaller types are not aligned).
5028   if (IsQPXVectorTy(Ty)) {
5029     if (getContext().getTypeSize(Ty) > 128)
5030       return CharUnits::fromQuantity(32);
5031 
5032     return CharUnits::fromQuantity(16);
5033   } else if (Ty->isVectorType()) {
5034     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5035   }
5036 
5037   // For single-element float/vector structs, we consider the whole type
5038   // to have the same alignment requirements as its single element.
5039   const Type *AlignAsType = nullptr;
5040   const Type *EltType = isSingleElementStruct(Ty, getContext());
5041   if (EltType) {
5042     const BuiltinType *BT = EltType->getAs<BuiltinType>();
5043     if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
5044          getContext().getTypeSize(EltType) == 128) ||
5045         (BT && BT->isFloatingPoint()))
5046       AlignAsType = EltType;
5047   }
5048 
5049   // Likewise for ELFv2 homogeneous aggregates.
5050   const Type *Base = nullptr;
5051   uint64_t Members = 0;
5052   if (!AlignAsType && Kind == ELFv2 &&
5053       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
5054     AlignAsType = Base;
5055 
5056   // With special case aggregates, only vector base types need alignment.
5057   if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
5058     if (getContext().getTypeSize(AlignAsType) > 128)
5059       return CharUnits::fromQuantity(32);
5060 
5061     return CharUnits::fromQuantity(16);
5062   } else if (AlignAsType) {
5063     return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
5064   }
5065 
5066   // Otherwise, we only need alignment for any aggregate type that
5067   // has an alignment requirement of >= 16 bytes.
5068   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
5069     if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
5070       return CharUnits::fromQuantity(32);
5071     return CharUnits::fromQuantity(16);
5072   }
5073 
5074   return CharUnits::fromQuantity(8);
5075 }
5076 
5077 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
5078 /// aggregate.  Base is set to the base element type, and Members is set
5079 /// to the number of base elements.
5080 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
5081                                      uint64_t &Members) const {
5082   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
5083     uint64_t NElements = AT->getSize().getZExtValue();
5084     if (NElements == 0)
5085       return false;
5086     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
5087       return false;
5088     Members *= NElements;
5089   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
5090     const RecordDecl *RD = RT->getDecl();
5091     if (RD->hasFlexibleArrayMember())
5092       return false;
5093 
5094     Members = 0;
5095 
5096     // If this is a C++ record, check the bases first.
5097     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5098       for (const auto &I : CXXRD->bases()) {
5099         // Ignore empty records.
5100         if (isEmptyRecord(getContext(), I.getType(), true))
5101           continue;
5102 
5103         uint64_t FldMembers;
5104         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
5105           return false;
5106 
5107         Members += FldMembers;
5108       }
5109     }
5110 
5111     for (const auto *FD : RD->fields()) {
5112       // Ignore (non-zero arrays of) empty records.
5113       QualType FT = FD->getType();
5114       while (const ConstantArrayType *AT =
5115              getContext().getAsConstantArrayType(FT)) {
5116         if (AT->getSize().getZExtValue() == 0)
5117           return false;
5118         FT = AT->getElementType();
5119       }
5120       if (isEmptyRecord(getContext(), FT, true))
5121         continue;
5122 
5123       // For compatibility with GCC, ignore empty bitfields in C++ mode.
5124       if (getContext().getLangOpts().CPlusPlus &&
5125           FD->isZeroLengthBitField(getContext()))
5126         continue;
5127 
5128       uint64_t FldMembers;
5129       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
5130         return false;
5131 
5132       Members = (RD->isUnion() ?
5133                  std::max(Members, FldMembers) : Members + FldMembers);
5134     }
5135 
5136     if (!Base)
5137       return false;
5138 
5139     // Ensure there is no padding.
5140     if (getContext().getTypeSize(Base) * Members !=
5141         getContext().getTypeSize(Ty))
5142       return false;
5143   } else {
5144     Members = 1;
5145     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
5146       Members = 2;
5147       Ty = CT->getElementType();
5148     }
5149 
5150     // Most ABIs only support float, double, and some vector type widths.
5151     if (!isHomogeneousAggregateBaseType(Ty))
5152       return false;
5153 
5154     // The base type must be the same for all members.  Types that
5155     // agree in both total size and mode (float vs. vector) are
5156     // treated as being equivalent here.
5157     const Type *TyPtr = Ty.getTypePtr();
5158     if (!Base) {
5159       Base = TyPtr;
5160       // If it's a non-power-of-2 vector, its size is already a power-of-2,
5161       // so make sure to widen it explicitly.
5162       if (const VectorType *VT = Base->getAs<VectorType>()) {
5163         QualType EltTy = VT->getElementType();
5164         unsigned NumElements =
5165             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
5166         Base = getContext()
5167                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
5168                    .getTypePtr();
5169       }
5170     }
5171 
5172     if (Base->isVectorType() != TyPtr->isVectorType() ||
5173         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
5174       return false;
5175   }
5176   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
5177 }
5178 
5179 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5180   // Homogeneous aggregates for ELFv2 must have base types of float,
5181   // double, long double, or 128-bit vectors.
5182   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5183     if (BT->getKind() == BuiltinType::Float ||
5184         BT->getKind() == BuiltinType::Double ||
5185         BT->getKind() == BuiltinType::LongDouble ||
5186         (getContext().getTargetInfo().hasFloat128Type() &&
5187           (BT->getKind() == BuiltinType::Float128))) {
5188       if (IsSoftFloatABI)
5189         return false;
5190       return true;
5191     }
5192   }
5193   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5194     if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
5195       return true;
5196   }
5197   return false;
5198 }
5199 
5200 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
5201     const Type *Base, uint64_t Members) const {
5202   // Vector and fp128 types require one register, other floating point types
5203   // require one or two registers depending on their size.
5204   uint32_t NumRegs =
5205       ((getContext().getTargetInfo().hasFloat128Type() &&
5206           Base->isFloat128Type()) ||
5207         Base->isVectorType()) ? 1
5208                               : (getContext().getTypeSize(Base) + 63) / 64;
5209 
5210   // Homogeneous Aggregates may occupy at most 8 registers.
5211   return Members * NumRegs <= 8;
5212 }
5213 
5214 ABIArgInfo
5215 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
5216   Ty = useFirstFieldIfTransparentUnion(Ty);
5217 
5218   if (Ty->isAnyComplexType())
5219     return ABIArgInfo::getDirect();
5220 
5221   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
5222   // or via reference (larger than 16 bytes).
5223   if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
5224     uint64_t Size = getContext().getTypeSize(Ty);
5225     if (Size > 128)
5226       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5227     else if (Size < 128) {
5228       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5229       return ABIArgInfo::getDirect(CoerceTy);
5230     }
5231   }
5232 
5233   if (const auto *EIT = Ty->getAs<ExtIntType>())
5234     if (EIT->getNumBits() > 128)
5235       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
5236 
5237   if (isAggregateTypeForABI(Ty)) {
5238     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5239       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5240 
5241     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
5242     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5243 
5244     // ELFv2 homogeneous aggregates are passed as array types.
5245     const Type *Base = nullptr;
5246     uint64_t Members = 0;
5247     if (Kind == ELFv2 &&
5248         isHomogeneousAggregate(Ty, Base, Members)) {
5249       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5250       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5251       return ABIArgInfo::getDirect(CoerceTy);
5252     }
5253 
5254     // If an aggregate may end up fully in registers, we do not
5255     // use the ByVal method, but pass the aggregate as array.
5256     // This is usually beneficial since we avoid forcing the
5257     // back-end to store the argument to memory.
5258     uint64_t Bits = getContext().getTypeSize(Ty);
5259     if (Bits > 0 && Bits <= 8 * GPRBits) {
5260       llvm::Type *CoerceTy;
5261 
5262       // Types up to 8 bytes are passed as integer type (which will be
5263       // properly aligned in the argument save area doubleword).
5264       if (Bits <= GPRBits)
5265         CoerceTy =
5266             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5267       // Larger types are passed as arrays, with the base type selected
5268       // according to the required alignment in the save area.
5269       else {
5270         uint64_t RegBits = ABIAlign * 8;
5271         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
5272         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
5273         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
5274       }
5275 
5276       return ABIArgInfo::getDirect(CoerceTy);
5277     }
5278 
5279     // All other aggregates are passed ByVal.
5280     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5281                                    /*ByVal=*/true,
5282                                    /*Realign=*/TyAlign > ABIAlign);
5283   }
5284 
5285   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
5286                                      : ABIArgInfo::getDirect());
5287 }
5288 
5289 ABIArgInfo
5290 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
5291   if (RetTy->isVoidType())
5292     return ABIArgInfo::getIgnore();
5293 
5294   if (RetTy->isAnyComplexType())
5295     return ABIArgInfo::getDirect();
5296 
5297   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
5298   // or via reference (larger than 16 bytes).
5299   if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
5300     uint64_t Size = getContext().getTypeSize(RetTy);
5301     if (Size > 128)
5302       return getNaturalAlignIndirect(RetTy);
5303     else if (Size < 128) {
5304       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5305       return ABIArgInfo::getDirect(CoerceTy);
5306     }
5307   }
5308 
5309   if (const auto *EIT = RetTy->getAs<ExtIntType>())
5310     if (EIT->getNumBits() > 128)
5311       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
5312 
5313   if (isAggregateTypeForABI(RetTy)) {
5314     // ELFv2 homogeneous aggregates are returned as array types.
5315     const Type *Base = nullptr;
5316     uint64_t Members = 0;
5317     if (Kind == ELFv2 &&
5318         isHomogeneousAggregate(RetTy, Base, Members)) {
5319       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5320       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5321       return ABIArgInfo::getDirect(CoerceTy);
5322     }
5323 
5324     // ELFv2 small aggregates are returned in up to two registers.
5325     uint64_t Bits = getContext().getTypeSize(RetTy);
5326     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
5327       if (Bits == 0)
5328         return ABIArgInfo::getIgnore();
5329 
5330       llvm::Type *CoerceTy;
5331       if (Bits > GPRBits) {
5332         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
5333         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
5334       } else
5335         CoerceTy =
5336             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5337       return ABIArgInfo::getDirect(CoerceTy);
5338     }
5339 
5340     // All other aggregates are returned indirectly.
5341     return getNaturalAlignIndirect(RetTy);
5342   }
5343 
5344   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
5345                                         : ABIArgInfo::getDirect());
5346 }
5347 
5348 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
5349 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5350                                       QualType Ty) const {
5351   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
5352   TypeInfo.second = getParamTypeAlignment(Ty);
5353 
5354   CharUnits SlotSize = CharUnits::fromQuantity(8);
5355 
5356   // If we have a complex type and the base type is smaller than 8 bytes,
5357   // the ABI calls for the real and imaginary parts to be right-adjusted
5358   // in separate doublewords.  However, Clang expects us to produce a
5359   // pointer to a structure with the two parts packed tightly.  So generate
5360   // loads of the real and imaginary parts relative to the va_list pointer,
5361   // and store them to a temporary structure.
5362   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
5363     CharUnits EltSize = TypeInfo.first / 2;
5364     if (EltSize < SlotSize) {
5365       Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
5366                                             SlotSize * 2, SlotSize,
5367                                             SlotSize, /*AllowHigher*/ true);
5368 
5369       Address RealAddr = Addr;
5370       Address ImagAddr = RealAddr;
5371       if (CGF.CGM.getDataLayout().isBigEndian()) {
5372         RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
5373                                                           SlotSize - EltSize);
5374         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
5375                                                       2 * SlotSize - EltSize);
5376       } else {
5377         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
5378       }
5379 
5380       llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
5381       RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
5382       ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
5383       llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
5384       llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
5385 
5386       Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
5387       CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
5388                              /*init*/ true);
5389       return Temp;
5390     }
5391   }
5392 
5393   // Otherwise, just use the general rule.
5394   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
5395                           TypeInfo, SlotSize, /*AllowHigher*/ true);
5396 }
5397 
5398 bool
5399 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
5400   CodeGen::CodeGenFunction &CGF,
5401   llvm::Value *Address) const {
5402   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5403                                      /*IsAIX*/ false);
5404 }
5405 
5406 bool
5407 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5408                                                 llvm::Value *Address) const {
5409   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5410                                      /*IsAIX*/ false);
5411 }
5412 
5413 //===----------------------------------------------------------------------===//
5414 // AArch64 ABI Implementation
5415 //===----------------------------------------------------------------------===//
5416 
5417 namespace {
5418 
5419 class AArch64ABIInfo : public SwiftABIInfo {
5420 public:
5421   enum ABIKind {
5422     AAPCS = 0,
5423     DarwinPCS,
5424     Win64
5425   };
5426 
5427 private:
5428   ABIKind Kind;
5429 
5430 public:
5431   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
5432     : SwiftABIInfo(CGT), Kind(Kind) {}
5433 
5434 private:
5435   ABIKind getABIKind() const { return Kind; }
5436   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5437 
5438   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5439   ABIArgInfo classifyArgumentType(QualType RetTy) const;
5440   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5441   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5442                                          uint64_t Members) const override;
5443 
5444   bool isIllegalVectorType(QualType Ty) const;
5445 
5446   void computeInfo(CGFunctionInfo &FI) const override {
5447     if (!::classifyReturnType(getCXXABI(), FI, *this))
5448       FI.getReturnInfo() =
5449           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5450 
5451     for (auto &it : FI.arguments())
5452       it.info = classifyArgumentType(it.type);
5453   }
5454 
5455   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5456                           CodeGenFunction &CGF) const;
5457 
5458   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5459                          CodeGenFunction &CGF) const;
5460 
5461   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5462                     QualType Ty) const override {
5463     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5464                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5465                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5466   }
5467 
5468   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5469                       QualType Ty) const override;
5470 
5471   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5472                                     bool asReturnValue) const override {
5473     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5474   }
5475   bool isSwiftErrorInRegister() const override {
5476     return true;
5477   }
5478 
5479   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5480                                  unsigned elts) const override;
5481 
5482   bool allowBFloatArgsAndRet() const override {
5483     return getTarget().hasBFloat16Type();
5484   }
5485 };
5486 
5487 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5488 public:
5489   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5490       : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
5491 
5492   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5493     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5494   }
5495 
5496   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5497     return 31;
5498   }
5499 
5500   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5501 
5502   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5503                            CodeGen::CodeGenModule &CGM) const override {
5504     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5505     if (!FD)
5506       return;
5507 
5508     LangOptions::SignReturnAddressScopeKind Scope =
5509         CGM.getLangOpts().getSignReturnAddressScope();
5510     LangOptions::SignReturnAddressKeyKind Key =
5511         CGM.getLangOpts().getSignReturnAddressKey();
5512     bool BranchTargetEnforcement = CGM.getLangOpts().BranchTargetEnforcement;
5513     if (const auto *TA = FD->getAttr<TargetAttr>()) {
5514       ParsedTargetAttr Attr = TA->parse();
5515       if (!Attr.BranchProtection.empty()) {
5516         TargetInfo::BranchProtectionInfo BPI;
5517         StringRef Error;
5518         (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
5519                                                        BPI, Error);
5520         assert(Error.empty());
5521         Scope = BPI.SignReturnAddr;
5522         Key = BPI.SignKey;
5523         BranchTargetEnforcement = BPI.BranchTargetEnforcement;
5524       }
5525     }
5526 
5527     auto *Fn = cast<llvm::Function>(GV);
5528     if (Scope != LangOptions::SignReturnAddressScopeKind::None) {
5529       Fn->addFnAttr("sign-return-address",
5530                     Scope == LangOptions::SignReturnAddressScopeKind::All
5531                         ? "all"
5532                         : "non-leaf");
5533 
5534       Fn->addFnAttr("sign-return-address-key",
5535                     Key == LangOptions::SignReturnAddressKeyKind::AKey
5536                         ? "a_key"
5537                         : "b_key");
5538     }
5539 
5540     if (BranchTargetEnforcement)
5541       Fn->addFnAttr("branch-target-enforcement");
5542   }
5543 };
5544 
5545 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5546 public:
5547   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5548       : AArch64TargetCodeGenInfo(CGT, K) {}
5549 
5550   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5551                            CodeGen::CodeGenModule &CGM) const override;
5552 
5553   void getDependentLibraryOption(llvm::StringRef Lib,
5554                                  llvm::SmallString<24> &Opt) const override {
5555     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5556   }
5557 
5558   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5559                                llvm::SmallString<32> &Opt) const override {
5560     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5561   }
5562 };
5563 
5564 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5565     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5566   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5567   if (GV->isDeclaration())
5568     return;
5569   addStackProbeTargetAttributes(D, GV, CGM);
5570 }
5571 }
5572 
5573 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
5574   Ty = useFirstFieldIfTransparentUnion(Ty);
5575 
5576   // Handle illegal vector types here.
5577   if (isIllegalVectorType(Ty)) {
5578     uint64_t Size = getContext().getTypeSize(Ty);
5579     // Android promotes <2 x i8> to i16, not i32
5580     if (isAndroid() && (Size <= 16)) {
5581       llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5582       return ABIArgInfo::getDirect(ResType);
5583     }
5584     if (Size <= 32) {
5585       llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5586       return ABIArgInfo::getDirect(ResType);
5587     }
5588     if (Size == 64) {
5589       auto *ResType =
5590           llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5591       return ABIArgInfo::getDirect(ResType);
5592     }
5593     if (Size == 128) {
5594       auto *ResType =
5595           llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5596       return ABIArgInfo::getDirect(ResType);
5597     }
5598     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5599   }
5600 
5601   if (!isAggregateTypeForABI(Ty)) {
5602     // Treat an enum type as its underlying type.
5603     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5604       Ty = EnumTy->getDecl()->getIntegerType();
5605 
5606     if (const auto *EIT = Ty->getAs<ExtIntType>())
5607       if (EIT->getNumBits() > 128)
5608         return getNaturalAlignIndirect(Ty);
5609 
5610     return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
5611                 ? ABIArgInfo::getExtend(Ty)
5612                 : ABIArgInfo::getDirect());
5613   }
5614 
5615   // Structures with either a non-trivial destructor or a non-trivial
5616   // copy constructor are always indirect.
5617   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5618     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5619                                      CGCXXABI::RAA_DirectInMemory);
5620   }
5621 
5622   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5623   // elsewhere for GNU compatibility.
5624   uint64_t Size = getContext().getTypeSize(Ty);
5625   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5626   if (IsEmpty || Size == 0) {
5627     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5628       return ABIArgInfo::getIgnore();
5629 
5630     // GNU C mode. The only argument that gets ignored is an empty one with size
5631     // 0.
5632     if (IsEmpty && Size == 0)
5633       return ABIArgInfo::getIgnore();
5634     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5635   }
5636 
5637   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5638   const Type *Base = nullptr;
5639   uint64_t Members = 0;
5640   if (isHomogeneousAggregate(Ty, Base, Members)) {
5641     return ABIArgInfo::getDirect(
5642         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5643   }
5644 
5645   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5646   if (Size <= 128) {
5647     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5648     // same size and alignment.
5649     if (getTarget().isRenderScriptTarget()) {
5650       return coerceToIntArray(Ty, getContext(), getVMContext());
5651     }
5652     unsigned Alignment;
5653     if (Kind == AArch64ABIInfo::AAPCS) {
5654       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5655       Alignment = Alignment < 128 ? 64 : 128;
5656     } else {
5657       Alignment = std::max(getContext().getTypeAlign(Ty),
5658                            (unsigned)getTarget().getPointerWidth(0));
5659     }
5660     Size = llvm::alignTo(Size, Alignment);
5661 
5662     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5663     // For aggregates with 16-byte alignment, we use i128.
5664     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5665     return ABIArgInfo::getDirect(
5666         Size == Alignment ? BaseTy
5667                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5668   }
5669 
5670   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5671 }
5672 
5673 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5674                                               bool IsVariadic) const {
5675   if (RetTy->isVoidType())
5676     return ABIArgInfo::getIgnore();
5677 
5678   // Large vector types should be returned via memory.
5679   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5680     return getNaturalAlignIndirect(RetTy);
5681 
5682   if (!isAggregateTypeForABI(RetTy)) {
5683     // Treat an enum type as its underlying type.
5684     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5685       RetTy = EnumTy->getDecl()->getIntegerType();
5686 
5687     if (const auto *EIT = RetTy->getAs<ExtIntType>())
5688       if (EIT->getNumBits() > 128)
5689         return getNaturalAlignIndirect(RetTy);
5690 
5691     return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
5692                 ? ABIArgInfo::getExtend(RetTy)
5693                 : ABIArgInfo::getDirect());
5694   }
5695 
5696   uint64_t Size = getContext().getTypeSize(RetTy);
5697   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5698     return ABIArgInfo::getIgnore();
5699 
5700   const Type *Base = nullptr;
5701   uint64_t Members = 0;
5702   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5703       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5704         IsVariadic))
5705     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5706     return ABIArgInfo::getDirect();
5707 
5708   // Aggregates <= 16 bytes are returned 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(RetTy, getContext(), getVMContext());
5714     }
5715     unsigned Alignment = getContext().getTypeAlign(RetTy);
5716     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5717 
5718     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5719     // For aggregates with 16-byte alignment, we use i128.
5720     if (Alignment < 128 && Size == 128) {
5721       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5722       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5723     }
5724     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5725   }
5726 
5727   return getNaturalAlignIndirect(RetTy);
5728 }
5729 
5730 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5731 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5732   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5733     // Check whether VT is legal.
5734     unsigned NumElements = VT->getNumElements();
5735     uint64_t Size = getContext().getTypeSize(VT);
5736     // NumElements should be power of 2.
5737     if (!llvm::isPowerOf2_32(NumElements))
5738       return true;
5739 
5740     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5741     // vectors for some reason.
5742     llvm::Triple Triple = getTarget().getTriple();
5743     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5744         Triple.isOSBinFormatMachO())
5745       return Size <= 32;
5746 
5747     return Size != 64 && (Size != 128 || NumElements == 1);
5748   }
5749   return false;
5750 }
5751 
5752 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5753                                                llvm::Type *eltTy,
5754                                                unsigned elts) const {
5755   if (!llvm::isPowerOf2_32(elts))
5756     return false;
5757   if (totalSize.getQuantity() != 8 &&
5758       (totalSize.getQuantity() != 16 || elts == 1))
5759     return false;
5760   return true;
5761 }
5762 
5763 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5764   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5765   // point type or a short-vector type. This is the same as the 32-bit ABI,
5766   // but with the difference that any floating-point type is allowed,
5767   // including __fp16.
5768   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5769     if (BT->isFloatingPoint())
5770       return true;
5771   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5772     unsigned VecSize = getContext().getTypeSize(VT);
5773     if (VecSize == 64 || VecSize == 128)
5774       return true;
5775   }
5776   return false;
5777 }
5778 
5779 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5780                                                        uint64_t Members) const {
5781   return Members <= 4;
5782 }
5783 
5784 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
5785                                             QualType Ty,
5786                                             CodeGenFunction &CGF) const {
5787   ABIArgInfo AI = classifyArgumentType(Ty);
5788   bool IsIndirect = AI.isIndirect();
5789 
5790   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5791   if (IsIndirect)
5792     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5793   else if (AI.getCoerceToType())
5794     BaseTy = AI.getCoerceToType();
5795 
5796   unsigned NumRegs = 1;
5797   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5798     BaseTy = ArrTy->getElementType();
5799     NumRegs = ArrTy->getNumElements();
5800   }
5801   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5802 
5803   // The AArch64 va_list type and handling is specified in the Procedure Call
5804   // Standard, section B.4:
5805   //
5806   // struct {
5807   //   void *__stack;
5808   //   void *__gr_top;
5809   //   void *__vr_top;
5810   //   int __gr_offs;
5811   //   int __vr_offs;
5812   // };
5813 
5814   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5815   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5816   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5817   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5818 
5819   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
5820   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
5821 
5822   Address reg_offs_p = Address::invalid();
5823   llvm::Value *reg_offs = nullptr;
5824   int reg_top_index;
5825   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
5826   if (!IsFPR) {
5827     // 3 is the field number of __gr_offs
5828     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
5829     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5830     reg_top_index = 1; // field number for __gr_top
5831     RegSize = llvm::alignTo(RegSize, 8);
5832   } else {
5833     // 4 is the field number of __vr_offs.
5834     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
5835     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5836     reg_top_index = 2; // field number for __vr_top
5837     RegSize = 16 * NumRegs;
5838   }
5839 
5840   //=======================================
5841   // Find out where argument was passed
5842   //=======================================
5843 
5844   // If reg_offs >= 0 we're already using the stack for this type of
5845   // argument. We don't want to keep updating reg_offs (in case it overflows,
5846   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5847   // whatever they get).
5848   llvm::Value *UsingStack = nullptr;
5849   UsingStack = CGF.Builder.CreateICmpSGE(
5850       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5851 
5852   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5853 
5854   // Otherwise, at least some kind of argument could go in these registers, the
5855   // question is whether this particular type is too big.
5856   CGF.EmitBlock(MaybeRegBlock);
5857 
5858   // Integer arguments may need to correct register alignment (for example a
5859   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5860   // align __gr_offs to calculate the potential address.
5861   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5862     int Align = TyAlign.getQuantity();
5863 
5864     reg_offs = CGF.Builder.CreateAdd(
5865         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5866         "align_regoffs");
5867     reg_offs = CGF.Builder.CreateAnd(
5868         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5869         "aligned_regoffs");
5870   }
5871 
5872   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
5873   // The fact that this is done unconditionally reflects the fact that
5874   // allocating an argument to the stack also uses up all the remaining
5875   // registers of the appropriate kind.
5876   llvm::Value *NewOffset = nullptr;
5877   NewOffset = CGF.Builder.CreateAdd(
5878       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5879   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5880 
5881   // Now we're in a position to decide whether this argument really was in
5882   // registers or not.
5883   llvm::Value *InRegs = nullptr;
5884   InRegs = CGF.Builder.CreateICmpSLE(
5885       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5886 
5887   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5888 
5889   //=======================================
5890   // Argument was in registers
5891   //=======================================
5892 
5893   // Now we emit the code for if the argument was originally passed in
5894   // registers. First start the appropriate block:
5895   CGF.EmitBlock(InRegBlock);
5896 
5897   llvm::Value *reg_top = nullptr;
5898   Address reg_top_p =
5899       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
5900   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
5901   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5902                    CharUnits::fromQuantity(IsFPR ? 16 : 8));
5903   Address RegAddr = Address::invalid();
5904   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
5905 
5906   if (IsIndirect) {
5907     // If it's been passed indirectly (actually a struct), whatever we find from
5908     // stored registers or on the stack will actually be a struct **.
5909     MemTy = llvm::PointerType::getUnqual(MemTy);
5910   }
5911 
5912   const Type *Base = nullptr;
5913   uint64_t NumMembers = 0;
5914   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
5915   if (IsHFA && NumMembers > 1) {
5916     // Homogeneous aggregates passed in registers will have their elements split
5917     // and stored 16-bytes apart regardless of size (they're notionally in qN,
5918     // qN+1, ...). We reload and store into a temporary local variable
5919     // contiguously.
5920     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
5921     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
5922     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5923     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
5924     Address Tmp = CGF.CreateTempAlloca(HFATy,
5925                                        std::max(TyAlign, BaseTyInfo.second));
5926 
5927     // On big-endian platforms, the value will be right-aligned in its slot.
5928     int Offset = 0;
5929     if (CGF.CGM.getDataLayout().isBigEndian() &&
5930         BaseTyInfo.first.getQuantity() < 16)
5931       Offset = 16 - BaseTyInfo.first.getQuantity();
5932 
5933     for (unsigned i = 0; i < NumMembers; ++i) {
5934       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5935       Address LoadAddr =
5936         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5937       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5938 
5939       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
5940 
5941       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5942       CGF.Builder.CreateStore(Elem, StoreAddr);
5943     }
5944 
5945     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
5946   } else {
5947     // Otherwise the object is contiguous in memory.
5948 
5949     // It might be right-aligned in its slot.
5950     CharUnits SlotSize = BaseAddr.getAlignment();
5951     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
5952         (IsHFA || !isAggregateTypeForABI(Ty)) &&
5953         TySize < SlotSize) {
5954       CharUnits Offset = SlotSize - TySize;
5955       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
5956     }
5957 
5958     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
5959   }
5960 
5961   CGF.EmitBranch(ContBlock);
5962 
5963   //=======================================
5964   // Argument was on the stack
5965   //=======================================
5966   CGF.EmitBlock(OnStackBlock);
5967 
5968   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
5969   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
5970 
5971   // Again, stack arguments may need realignment. In this case both integer and
5972   // floating-point ones might be affected.
5973   if (!IsIndirect && TyAlign.getQuantity() > 8) {
5974     int Align = TyAlign.getQuantity();
5975 
5976     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
5977 
5978     OnStackPtr = CGF.Builder.CreateAdd(
5979         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
5980         "align_stack");
5981     OnStackPtr = CGF.Builder.CreateAnd(
5982         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
5983         "align_stack");
5984 
5985     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
5986   }
5987   Address OnStackAddr(OnStackPtr,
5988                       std::max(CharUnits::fromQuantity(8), TyAlign));
5989 
5990   // All stack slots are multiples of 8 bytes.
5991   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5992   CharUnits StackSize;
5993   if (IsIndirect)
5994     StackSize = StackSlotSize;
5995   else
5996     StackSize = TySize.alignTo(StackSlotSize);
5997 
5998   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
5999   llvm::Value *NewStack =
6000       CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
6001 
6002   // Write the new value of __stack for the next call to va_arg
6003   CGF.Builder.CreateStore(NewStack, stack_p);
6004 
6005   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
6006       TySize < StackSlotSize) {
6007     CharUnits Offset = StackSlotSize - TySize;
6008     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
6009   }
6010 
6011   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
6012 
6013   CGF.EmitBranch(ContBlock);
6014 
6015   //=======================================
6016   // Tidy up
6017   //=======================================
6018   CGF.EmitBlock(ContBlock);
6019 
6020   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6021                                  OnStackAddr, OnStackBlock, "vaargs.addr");
6022 
6023   if (IsIndirect)
6024     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
6025                    TyAlign);
6026 
6027   return ResAddr;
6028 }
6029 
6030 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
6031                                         CodeGenFunction &CGF) const {
6032   // The backend's lowering doesn't support va_arg for aggregates or
6033   // illegal vector types.  Lower VAArg here for these cases and use
6034   // the LLVM va_arg instruction for everything else.
6035   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
6036     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
6037 
6038   uint64_t PointerSize = getTarget().getPointerWidth(0) / 8;
6039   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6040 
6041   // Empty records are ignored for parameter passing purposes.
6042   if (isEmptyRecord(getContext(), Ty, true)) {
6043     Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
6044     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6045     return Addr;
6046   }
6047 
6048   // The size of the actual thing passed, which might end up just
6049   // being a pointer for indirect types.
6050   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6051 
6052   // Arguments bigger than 16 bytes which aren't homogeneous
6053   // aggregates should be passed indirectly.
6054   bool IsIndirect = false;
6055   if (TyInfo.first.getQuantity() > 16) {
6056     const Type *Base = nullptr;
6057     uint64_t Members = 0;
6058     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
6059   }
6060 
6061   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6062                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
6063 }
6064 
6065 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
6066                                     QualType Ty) const {
6067   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6068                           CGF.getContext().getTypeInfoInChars(Ty),
6069                           CharUnits::fromQuantity(8),
6070                           /*allowHigherAlign*/ false);
6071 }
6072 
6073 //===----------------------------------------------------------------------===//
6074 // ARM ABI Implementation
6075 //===----------------------------------------------------------------------===//
6076 
6077 namespace {
6078 
6079 class ARMABIInfo : public SwiftABIInfo {
6080 public:
6081   enum ABIKind {
6082     APCS = 0,
6083     AAPCS = 1,
6084     AAPCS_VFP = 2,
6085     AAPCS16_VFP = 3,
6086   };
6087 
6088 private:
6089   ABIKind Kind;
6090   bool IsFloatABISoftFP;
6091 
6092 public:
6093   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
6094       : SwiftABIInfo(CGT), Kind(_Kind) {
6095     setCCs();
6096     IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
6097         CGT.getCodeGenOpts().FloatABI == ""; // default
6098   }
6099 
6100   bool isEABI() const {
6101     switch (getTarget().getTriple().getEnvironment()) {
6102     case llvm::Triple::Android:
6103     case llvm::Triple::EABI:
6104     case llvm::Triple::EABIHF:
6105     case llvm::Triple::GNUEABI:
6106     case llvm::Triple::GNUEABIHF:
6107     case llvm::Triple::MuslEABI:
6108     case llvm::Triple::MuslEABIHF:
6109       return true;
6110     default:
6111       return false;
6112     }
6113   }
6114 
6115   bool isEABIHF() const {
6116     switch (getTarget().getTriple().getEnvironment()) {
6117     case llvm::Triple::EABIHF:
6118     case llvm::Triple::GNUEABIHF:
6119     case llvm::Triple::MuslEABIHF:
6120       return true;
6121     default:
6122       return false;
6123     }
6124   }
6125 
6126   ABIKind getABIKind() const { return Kind; }
6127 
6128   bool allowBFloatArgsAndRet() const override {
6129     return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
6130   }
6131 
6132 private:
6133   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
6134                                 unsigned functionCallConv) const;
6135   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
6136                                   unsigned functionCallConv) const;
6137   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
6138                                           uint64_t Members) const;
6139   ABIArgInfo coerceIllegalVector(QualType Ty) const;
6140   bool isIllegalVectorType(QualType Ty) const;
6141   bool containsAnyFP16Vectors(QualType Ty) const;
6142 
6143   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
6144   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
6145                                          uint64_t Members) const override;
6146 
6147   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
6148 
6149   void computeInfo(CGFunctionInfo &FI) const override;
6150 
6151   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6152                     QualType Ty) const override;
6153 
6154   llvm::CallingConv::ID getLLVMDefaultCC() const;
6155   llvm::CallingConv::ID getABIDefaultCC() const;
6156   void setCCs();
6157 
6158   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
6159                                     bool asReturnValue) const override {
6160     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6161   }
6162   bool isSwiftErrorInRegister() const override {
6163     return true;
6164   }
6165   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
6166                                  unsigned elts) const override;
6167 };
6168 
6169 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
6170 public:
6171   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6172       : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {}
6173 
6174   const ARMABIInfo &getABIInfo() const {
6175     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
6176   }
6177 
6178   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6179     return 13;
6180   }
6181 
6182   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
6183     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
6184   }
6185 
6186   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6187                                llvm::Value *Address) const override {
6188     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6189 
6190     // 0-15 are the 16 integer registers.
6191     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
6192     return false;
6193   }
6194 
6195   unsigned getSizeOfUnwindException() const override {
6196     if (getABIInfo().isEABI()) return 88;
6197     return TargetCodeGenInfo::getSizeOfUnwindException();
6198   }
6199 
6200   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6201                            CodeGen::CodeGenModule &CGM) const override {
6202     if (GV->isDeclaration())
6203       return;
6204     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6205     if (!FD)
6206       return;
6207 
6208     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
6209     if (!Attr)
6210       return;
6211 
6212     const char *Kind;
6213     switch (Attr->getInterrupt()) {
6214     case ARMInterruptAttr::Generic: Kind = ""; break;
6215     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
6216     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
6217     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
6218     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
6219     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
6220     }
6221 
6222     llvm::Function *Fn = cast<llvm::Function>(GV);
6223 
6224     Fn->addFnAttr("interrupt", Kind);
6225 
6226     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
6227     if (ABI == ARMABIInfo::APCS)
6228       return;
6229 
6230     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
6231     // however this is not necessarily true on taking any interrupt. Instruct
6232     // the backend to perform a realignment as part of the function prologue.
6233     llvm::AttrBuilder B;
6234     B.addStackAlignmentAttr(8);
6235     Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
6236   }
6237 };
6238 
6239 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
6240 public:
6241   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6242       : ARMTargetCodeGenInfo(CGT, K) {}
6243 
6244   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6245                            CodeGen::CodeGenModule &CGM) const override;
6246 
6247   void getDependentLibraryOption(llvm::StringRef Lib,
6248                                  llvm::SmallString<24> &Opt) const override {
6249     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
6250   }
6251 
6252   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
6253                                llvm::SmallString<32> &Opt) const override {
6254     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
6255   }
6256 };
6257 
6258 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
6259     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
6260   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
6261   if (GV->isDeclaration())
6262     return;
6263   addStackProbeTargetAttributes(D, GV, CGM);
6264 }
6265 }
6266 
6267 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
6268   if (!::classifyReturnType(getCXXABI(), FI, *this))
6269     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
6270                                             FI.getCallingConvention());
6271 
6272   for (auto &I : FI.arguments())
6273     I.info = classifyArgumentType(I.type, FI.isVariadic(),
6274                                   FI.getCallingConvention());
6275 
6276 
6277   // Always honor user-specified calling convention.
6278   if (FI.getCallingConvention() != llvm::CallingConv::C)
6279     return;
6280 
6281   llvm::CallingConv::ID cc = getRuntimeCC();
6282   if (cc != llvm::CallingConv::C)
6283     FI.setEffectiveCallingConvention(cc);
6284 }
6285 
6286 /// Return the default calling convention that LLVM will use.
6287 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
6288   // The default calling convention that LLVM will infer.
6289   if (isEABIHF() || getTarget().getTriple().isWatchABI())
6290     return llvm::CallingConv::ARM_AAPCS_VFP;
6291   else if (isEABI())
6292     return llvm::CallingConv::ARM_AAPCS;
6293   else
6294     return llvm::CallingConv::ARM_APCS;
6295 }
6296 
6297 /// Return the calling convention that our ABI would like us to use
6298 /// as the C calling convention.
6299 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
6300   switch (getABIKind()) {
6301   case APCS: return llvm::CallingConv::ARM_APCS;
6302   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
6303   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6304   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6305   }
6306   llvm_unreachable("bad ABI kind");
6307 }
6308 
6309 void ARMABIInfo::setCCs() {
6310   assert(getRuntimeCC() == llvm::CallingConv::C);
6311 
6312   // Don't muddy up the IR with a ton of explicit annotations if
6313   // they'd just match what LLVM will infer from the triple.
6314   llvm::CallingConv::ID abiCC = getABIDefaultCC();
6315   if (abiCC != getLLVMDefaultCC())
6316     RuntimeCC = abiCC;
6317 }
6318 
6319 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
6320   uint64_t Size = getContext().getTypeSize(Ty);
6321   if (Size <= 32) {
6322     llvm::Type *ResType =
6323         llvm::Type::getInt32Ty(getVMContext());
6324     return ABIArgInfo::getDirect(ResType);
6325   }
6326   if (Size == 64 || Size == 128) {
6327     auto *ResType = llvm::FixedVectorType::get(
6328         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6329     return ABIArgInfo::getDirect(ResType);
6330   }
6331   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6332 }
6333 
6334 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
6335                                                     const Type *Base,
6336                                                     uint64_t Members) const {
6337   assert(Base && "Base class should be set for homogeneous aggregate");
6338   // Base can be a floating-point or a vector.
6339   if (const VectorType *VT = Base->getAs<VectorType>()) {
6340     // FP16 vectors should be converted to integer vectors
6341     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
6342       uint64_t Size = getContext().getTypeSize(VT);
6343       auto *NewVecTy = llvm::FixedVectorType::get(
6344           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6345       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
6346       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6347     }
6348   }
6349   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
6350 }
6351 
6352 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
6353                                             unsigned functionCallConv) const {
6354   // 6.1.2.1 The following argument types are VFP CPRCs:
6355   //   A single-precision floating-point type (including promoted
6356   //   half-precision types); A double-precision floating-point type;
6357   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
6358   //   with a Base Type of a single- or double-precision floating-point type,
6359   //   64-bit containerized vectors or 128-bit containerized vectors with one
6360   //   to four Elements.
6361   // Variadic functions should always marshal to the base standard.
6362   bool IsAAPCS_VFP =
6363       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
6364 
6365   Ty = useFirstFieldIfTransparentUnion(Ty);
6366 
6367   // Handle illegal vector types here.
6368   if (isIllegalVectorType(Ty))
6369     return coerceIllegalVector(Ty);
6370 
6371   if (!isAggregateTypeForABI(Ty)) {
6372     // Treat an enum type as its underlying type.
6373     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
6374       Ty = EnumTy->getDecl()->getIntegerType();
6375     }
6376 
6377     if (const auto *EIT = Ty->getAs<ExtIntType>())
6378       if (EIT->getNumBits() > 64)
6379         return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6380 
6381     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
6382                                               : ABIArgInfo::getDirect());
6383   }
6384 
6385   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6386     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6387   }
6388 
6389   // Ignore empty records.
6390   if (isEmptyRecord(getContext(), Ty, true))
6391     return ABIArgInfo::getIgnore();
6392 
6393   if (IsAAPCS_VFP) {
6394     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
6395     // into VFP registers.
6396     const Type *Base = nullptr;
6397     uint64_t Members = 0;
6398     if (isHomogeneousAggregate(Ty, Base, Members))
6399       return classifyHomogeneousAggregate(Ty, Base, Members);
6400   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6401     // WatchOS does have homogeneous aggregates. Note that we intentionally use
6402     // this convention even for a variadic function: the backend will use GPRs
6403     // if needed.
6404     const Type *Base = nullptr;
6405     uint64_t Members = 0;
6406     if (isHomogeneousAggregate(Ty, Base, Members)) {
6407       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
6408       llvm::Type *Ty =
6409         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
6410       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6411     }
6412   }
6413 
6414   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6415       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
6416     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
6417     // bigger than 128-bits, they get placed in space allocated by the caller,
6418     // and a pointer is passed.
6419     return ABIArgInfo::getIndirect(
6420         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
6421   }
6422 
6423   // Support byval for ARM.
6424   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
6425   // most 8-byte. We realign the indirect argument if type alignment is bigger
6426   // than ABI alignment.
6427   uint64_t ABIAlign = 4;
6428   uint64_t TyAlign;
6429   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6430       getABIKind() == ARMABIInfo::AAPCS) {
6431     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6432     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
6433   } else {
6434     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
6435   }
6436   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
6437     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
6438     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
6439                                    /*ByVal=*/true,
6440                                    /*Realign=*/TyAlign > ABIAlign);
6441   }
6442 
6443   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
6444   // same size and alignment.
6445   if (getTarget().isRenderScriptTarget()) {
6446     return coerceToIntArray(Ty, getContext(), getVMContext());
6447   }
6448 
6449   // Otherwise, pass by coercing to a structure of the appropriate size.
6450   llvm::Type* ElemTy;
6451   unsigned SizeRegs;
6452   // FIXME: Try to match the types of the arguments more accurately where
6453   // we can.
6454   if (TyAlign <= 4) {
6455     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6456     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6457   } else {
6458     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6459     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6460   }
6461 
6462   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6463 }
6464 
6465 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6466                               llvm::LLVMContext &VMContext) {
6467   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6468   // is called integer-like if its size is less than or equal to one word, and
6469   // the offset of each of its addressable sub-fields is zero.
6470 
6471   uint64_t Size = Context.getTypeSize(Ty);
6472 
6473   // Check that the type fits in a word.
6474   if (Size > 32)
6475     return false;
6476 
6477   // FIXME: Handle vector types!
6478   if (Ty->isVectorType())
6479     return false;
6480 
6481   // Float types are never treated as "integer like".
6482   if (Ty->isRealFloatingType())
6483     return false;
6484 
6485   // If this is a builtin or pointer type then it is ok.
6486   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6487     return true;
6488 
6489   // Small complex integer types are "integer like".
6490   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6491     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6492 
6493   // Single element and zero sized arrays should be allowed, by the definition
6494   // above, but they are not.
6495 
6496   // Otherwise, it must be a record type.
6497   const RecordType *RT = Ty->getAs<RecordType>();
6498   if (!RT) return false;
6499 
6500   // Ignore records with flexible arrays.
6501   const RecordDecl *RD = RT->getDecl();
6502   if (RD->hasFlexibleArrayMember())
6503     return false;
6504 
6505   // Check that all sub-fields are at offset 0, and are themselves "integer
6506   // like".
6507   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6508 
6509   bool HadField = false;
6510   unsigned idx = 0;
6511   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6512        i != e; ++i, ++idx) {
6513     const FieldDecl *FD = *i;
6514 
6515     // Bit-fields are not addressable, we only need to verify they are "integer
6516     // like". We still have to disallow a subsequent non-bitfield, for example:
6517     //   struct { int : 0; int x }
6518     // is non-integer like according to gcc.
6519     if (FD->isBitField()) {
6520       if (!RD->isUnion())
6521         HadField = true;
6522 
6523       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6524         return false;
6525 
6526       continue;
6527     }
6528 
6529     // Check if this field is at offset 0.
6530     if (Layout.getFieldOffset(idx) != 0)
6531       return false;
6532 
6533     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6534       return false;
6535 
6536     // Only allow at most one field in a structure. This doesn't match the
6537     // wording above, but follows gcc in situations with a field following an
6538     // empty structure.
6539     if (!RD->isUnion()) {
6540       if (HadField)
6541         return false;
6542 
6543       HadField = true;
6544     }
6545   }
6546 
6547   return true;
6548 }
6549 
6550 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6551                                           unsigned functionCallConv) const {
6552 
6553   // Variadic functions should always marshal to the base standard.
6554   bool IsAAPCS_VFP =
6555       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6556 
6557   if (RetTy->isVoidType())
6558     return ABIArgInfo::getIgnore();
6559 
6560   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6561     // Large vector types should be returned via memory.
6562     if (getContext().getTypeSize(RetTy) > 128)
6563       return getNaturalAlignIndirect(RetTy);
6564     // TODO: FP16/BF16 vectors should be converted to integer vectors
6565     // This check is similar  to isIllegalVectorType - refactor?
6566     if ((!getTarget().hasLegalHalfType() &&
6567         (VT->getElementType()->isFloat16Type() ||
6568          VT->getElementType()->isHalfType())) ||
6569         (IsFloatABISoftFP &&
6570          VT->getElementType()->isBFloat16Type()))
6571       return coerceIllegalVector(RetTy);
6572   }
6573 
6574   if (!isAggregateTypeForABI(RetTy)) {
6575     // Treat an enum type as its underlying type.
6576     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6577       RetTy = EnumTy->getDecl()->getIntegerType();
6578 
6579     if (const auto *EIT = RetTy->getAs<ExtIntType>())
6580       if (EIT->getNumBits() > 64)
6581         return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
6582 
6583     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6584                                                 : ABIArgInfo::getDirect();
6585   }
6586 
6587   // Are we following APCS?
6588   if (getABIKind() == APCS) {
6589     if (isEmptyRecord(getContext(), RetTy, false))
6590       return ABIArgInfo::getIgnore();
6591 
6592     // Complex types are all returned as packed integers.
6593     //
6594     // FIXME: Consider using 2 x vector types if the back end handles them
6595     // correctly.
6596     if (RetTy->isAnyComplexType())
6597       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6598           getVMContext(), getContext().getTypeSize(RetTy)));
6599 
6600     // Integer like structures are returned in r0.
6601     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6602       // Return in the smallest viable integer type.
6603       uint64_t Size = getContext().getTypeSize(RetTy);
6604       if (Size <= 8)
6605         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6606       if (Size <= 16)
6607         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6608       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6609     }
6610 
6611     // Otherwise return in memory.
6612     return getNaturalAlignIndirect(RetTy);
6613   }
6614 
6615   // Otherwise this is an AAPCS variant.
6616 
6617   if (isEmptyRecord(getContext(), RetTy, true))
6618     return ABIArgInfo::getIgnore();
6619 
6620   // Check for homogeneous aggregates with AAPCS-VFP.
6621   if (IsAAPCS_VFP) {
6622     const Type *Base = nullptr;
6623     uint64_t Members = 0;
6624     if (isHomogeneousAggregate(RetTy, Base, Members))
6625       return classifyHomogeneousAggregate(RetTy, Base, Members);
6626   }
6627 
6628   // Aggregates <= 4 bytes are returned in r0; other aggregates
6629   // are returned indirectly.
6630   uint64_t Size = getContext().getTypeSize(RetTy);
6631   if (Size <= 32) {
6632     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6633     // same size and alignment.
6634     if (getTarget().isRenderScriptTarget()) {
6635       return coerceToIntArray(RetTy, getContext(), getVMContext());
6636     }
6637     if (getDataLayout().isBigEndian())
6638       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6639       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6640 
6641     // Return in the smallest viable integer type.
6642     if (Size <= 8)
6643       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6644     if (Size <= 16)
6645       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6646     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6647   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6648     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6649     llvm::Type *CoerceTy =
6650         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6651     return ABIArgInfo::getDirect(CoerceTy);
6652   }
6653 
6654   return getNaturalAlignIndirect(RetTy);
6655 }
6656 
6657 /// isIllegalVector - check whether Ty is an illegal vector type.
6658 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6659   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6660     // On targets that don't support half, fp16 or bfloat, they are expanded
6661     // into float, and we don't want the ABI to depend on whether or not they
6662     // are supported in hardware. Thus return false to coerce vectors of these
6663     // types into integer vectors.
6664     // We do not depend on hasLegalHalfType for bfloat as it is a
6665     // separate IR type.
6666     if ((!getTarget().hasLegalHalfType() &&
6667         (VT->getElementType()->isFloat16Type() ||
6668          VT->getElementType()->isHalfType())) ||
6669         (IsFloatABISoftFP &&
6670          VT->getElementType()->isBFloat16Type()))
6671       return true;
6672     if (isAndroid()) {
6673       // Android shipped using Clang 3.1, which supported a slightly different
6674       // vector ABI. The primary differences were that 3-element vector types
6675       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6676       // accepts that legacy behavior for Android only.
6677       // Check whether VT is legal.
6678       unsigned NumElements = VT->getNumElements();
6679       // NumElements should be power of 2 or equal to 3.
6680       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6681         return true;
6682     } else {
6683       // Check whether VT is legal.
6684       unsigned NumElements = VT->getNumElements();
6685       uint64_t Size = getContext().getTypeSize(VT);
6686       // NumElements should be power of 2.
6687       if (!llvm::isPowerOf2_32(NumElements))
6688         return true;
6689       // Size should be greater than 32 bits.
6690       return Size <= 32;
6691     }
6692   }
6693   return false;
6694 }
6695 
6696 /// Return true if a type contains any 16-bit floating point vectors
6697 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6698   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6699     uint64_t NElements = AT->getSize().getZExtValue();
6700     if (NElements == 0)
6701       return false;
6702     return containsAnyFP16Vectors(AT->getElementType());
6703   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6704     const RecordDecl *RD = RT->getDecl();
6705 
6706     // If this is a C++ record, check the bases first.
6707     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6708       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6709             return containsAnyFP16Vectors(B.getType());
6710           }))
6711         return true;
6712 
6713     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6714           return FD && containsAnyFP16Vectors(FD->getType());
6715         }))
6716       return true;
6717 
6718     return false;
6719   } else {
6720     if (const VectorType *VT = Ty->getAs<VectorType>())
6721       return (VT->getElementType()->isFloat16Type() ||
6722               VT->getElementType()->isBFloat16Type() ||
6723               VT->getElementType()->isHalfType());
6724     return false;
6725   }
6726 }
6727 
6728 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6729                                            llvm::Type *eltTy,
6730                                            unsigned numElts) const {
6731   if (!llvm::isPowerOf2_32(numElts))
6732     return false;
6733   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6734   if (size > 64)
6735     return false;
6736   if (vectorSize.getQuantity() != 8 &&
6737       (vectorSize.getQuantity() != 16 || numElts == 1))
6738     return false;
6739   return true;
6740 }
6741 
6742 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6743   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6744   // double, or 64-bit or 128-bit vectors.
6745   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6746     if (BT->getKind() == BuiltinType::Float ||
6747         BT->getKind() == BuiltinType::Double ||
6748         BT->getKind() == BuiltinType::LongDouble)
6749       return true;
6750   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6751     unsigned VecSize = getContext().getTypeSize(VT);
6752     if (VecSize == 64 || VecSize == 128)
6753       return true;
6754   }
6755   return false;
6756 }
6757 
6758 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6759                                                    uint64_t Members) const {
6760   return Members <= 4;
6761 }
6762 
6763 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
6764                                         bool acceptHalf) const {
6765   // Give precedence to user-specified calling conventions.
6766   if (callConvention != llvm::CallingConv::C)
6767     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
6768   else
6769     return (getABIKind() == AAPCS_VFP) ||
6770            (acceptHalf && (getABIKind() == AAPCS16_VFP));
6771 }
6772 
6773 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6774                               QualType Ty) const {
6775   CharUnits SlotSize = CharUnits::fromQuantity(4);
6776 
6777   // Empty records are ignored for parameter passing purposes.
6778   if (isEmptyRecord(getContext(), Ty, true)) {
6779     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6780     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6781     return Addr;
6782   }
6783 
6784   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6785   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
6786 
6787   // Use indirect if size of the illegal vector is bigger than 16 bytes.
6788   bool IsIndirect = false;
6789   const Type *Base = nullptr;
6790   uint64_t Members = 0;
6791   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6792     IsIndirect = true;
6793 
6794   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6795   // allocated by the caller.
6796   } else if (TySize > CharUnits::fromQuantity(16) &&
6797              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6798              !isHomogeneousAggregate(Ty, Base, Members)) {
6799     IsIndirect = true;
6800 
6801   // Otherwise, bound the type's ABI alignment.
6802   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6803   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
6804   // Our callers should be prepared to handle an under-aligned address.
6805   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6806              getABIKind() == ARMABIInfo::AAPCS) {
6807     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6808     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
6809   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6810     // ARMv7k allows type alignment up to 16 bytes.
6811     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6812     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
6813   } else {
6814     TyAlignForABI = CharUnits::fromQuantity(4);
6815   }
6816 
6817   std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI };
6818   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6819                           SlotSize, /*AllowHigherAlign*/ true);
6820 }
6821 
6822 //===----------------------------------------------------------------------===//
6823 // NVPTX ABI Implementation
6824 //===----------------------------------------------------------------------===//
6825 
6826 namespace {
6827 
6828 class NVPTXTargetCodeGenInfo;
6829 
6830 class NVPTXABIInfo : public ABIInfo {
6831   NVPTXTargetCodeGenInfo &CGInfo;
6832 
6833 public:
6834   NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
6835       : ABIInfo(CGT), CGInfo(Info) {}
6836 
6837   ABIArgInfo classifyReturnType(QualType RetTy) const;
6838   ABIArgInfo classifyArgumentType(QualType Ty) const;
6839 
6840   void computeInfo(CGFunctionInfo &FI) const override;
6841   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6842                     QualType Ty) const override;
6843   bool isUnsupportedType(QualType T) const;
6844   ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
6845 };
6846 
6847 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
6848 public:
6849   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6850       : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
6851 
6852   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6853                            CodeGen::CodeGenModule &M) const override;
6854   bool shouldEmitStaticExternCAliases() const override;
6855 
6856   llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
6857     // On the device side, surface reference is represented as an object handle
6858     // in 64-bit integer.
6859     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
6860   }
6861 
6862   llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
6863     // On the device side, texture reference is represented as an object handle
6864     // in 64-bit integer.
6865     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
6866   }
6867 
6868   bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6869                                               LValue Src) const override {
6870     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
6871     return true;
6872   }
6873 
6874   bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6875                                               LValue Src) const override {
6876     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
6877     return true;
6878   }
6879 
6880 private:
6881   // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
6882   // resulting MDNode to the nvvm.annotations MDNode.
6883   static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
6884                               int Operand);
6885 
6886   static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
6887                                            LValue Src) {
6888     llvm::Value *Handle = nullptr;
6889     llvm::Constant *C =
6890         llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
6891     // Lookup `addrspacecast` through the constant pointer if any.
6892     if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
6893       C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
6894     if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
6895       // Load the handle from the specific global variable using
6896       // `nvvm.texsurf.handle.internal` intrinsic.
6897       Handle = CGF.EmitRuntimeCall(
6898           CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
6899                                {GV->getType()}),
6900           {GV}, "texsurf_handle");
6901     } else
6902       Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
6903     CGF.EmitStoreOfScalar(Handle, Dst);
6904   }
6905 };
6906 
6907 /// Checks if the type is unsupported directly by the current target.
6908 bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
6909   ASTContext &Context = getContext();
6910   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
6911     return true;
6912   if (!Context.getTargetInfo().hasFloat128Type() &&
6913       (T->isFloat128Type() ||
6914        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
6915     return true;
6916   if (const auto *EIT = T->getAs<ExtIntType>())
6917     return EIT->getNumBits() >
6918            (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
6919   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
6920       Context.getTypeSize(T) > 64U)
6921     return true;
6922   if (const auto *AT = T->getAsArrayTypeUnsafe())
6923     return isUnsupportedType(AT->getElementType());
6924   const auto *RT = T->getAs<RecordType>();
6925   if (!RT)
6926     return false;
6927   const RecordDecl *RD = RT->getDecl();
6928 
6929   // If this is a C++ record, check the bases first.
6930   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6931     for (const CXXBaseSpecifier &I : CXXRD->bases())
6932       if (isUnsupportedType(I.getType()))
6933         return true;
6934 
6935   for (const FieldDecl *I : RD->fields())
6936     if (isUnsupportedType(I->getType()))
6937       return true;
6938   return false;
6939 }
6940 
6941 /// Coerce the given type into an array with maximum allowed size of elements.
6942 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
6943                                                    unsigned MaxSize) const {
6944   // Alignment and Size are measured in bits.
6945   const uint64_t Size = getContext().getTypeSize(Ty);
6946   const uint64_t Alignment = getContext().getTypeAlign(Ty);
6947   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
6948   llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
6949   const uint64_t NumElements = (Size + Div - 1) / Div;
6950   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
6951 }
6952 
6953 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
6954   if (RetTy->isVoidType())
6955     return ABIArgInfo::getIgnore();
6956 
6957   if (getContext().getLangOpts().OpenMP &&
6958       getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
6959     return coerceToIntArrayWithLimit(RetTy, 64);
6960 
6961   // note: this is different from default ABI
6962   if (!RetTy->isScalarType())
6963     return ABIArgInfo::getDirect();
6964 
6965   // Treat an enum type as its underlying type.
6966   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6967     RetTy = EnumTy->getDecl()->getIntegerType();
6968 
6969   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6970                                                : ABIArgInfo::getDirect());
6971 }
6972 
6973 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
6974   // Treat an enum type as its underlying type.
6975   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6976     Ty = EnumTy->getDecl()->getIntegerType();
6977 
6978   // Return aggregates type as indirect by value
6979   if (isAggregateTypeForABI(Ty)) {
6980     // Under CUDA device compilation, tex/surf builtin types are replaced with
6981     // object types and passed directly.
6982     if (getContext().getLangOpts().CUDAIsDevice) {
6983       if (Ty->isCUDADeviceBuiltinSurfaceType())
6984         return ABIArgInfo::getDirect(
6985             CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
6986       if (Ty->isCUDADeviceBuiltinTextureType())
6987         return ABIArgInfo::getDirect(
6988             CGInfo.getCUDADeviceBuiltinTextureDeviceType());
6989     }
6990     return getNaturalAlignIndirect(Ty, /* byval */ true);
6991   }
6992 
6993   if (const auto *EIT = Ty->getAs<ExtIntType>()) {
6994     if ((EIT->getNumBits() > 128) ||
6995         (!getContext().getTargetInfo().hasInt128Type() &&
6996          EIT->getNumBits() > 64))
6997       return getNaturalAlignIndirect(Ty, /* byval */ true);
6998   }
6999 
7000   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
7001                                             : ABIArgInfo::getDirect());
7002 }
7003 
7004 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
7005   if (!getCXXABI().classifyReturnType(FI))
7006     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7007   for (auto &I : FI.arguments())
7008     I.info = classifyArgumentType(I.type);
7009 
7010   // Always honor user-specified calling convention.
7011   if (FI.getCallingConvention() != llvm::CallingConv::C)
7012     return;
7013 
7014   FI.setEffectiveCallingConvention(getRuntimeCC());
7015 }
7016 
7017 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7018                                 QualType Ty) const {
7019   llvm_unreachable("NVPTX does not support varargs");
7020 }
7021 
7022 void NVPTXTargetCodeGenInfo::setTargetAttributes(
7023     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7024   if (GV->isDeclaration())
7025     return;
7026   const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
7027   if (VD) {
7028     if (M.getLangOpts().CUDA) {
7029       if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
7030         addNVVMMetadata(GV, "surface", 1);
7031       else if (VD->getType()->isCUDADeviceBuiltinTextureType())
7032         addNVVMMetadata(GV, "texture", 1);
7033       return;
7034     }
7035   }
7036 
7037   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7038   if (!FD) return;
7039 
7040   llvm::Function *F = cast<llvm::Function>(GV);
7041 
7042   // Perform special handling in OpenCL mode
7043   if (M.getLangOpts().OpenCL) {
7044     // Use OpenCL function attributes to check for kernel functions
7045     // By default, all functions are device functions
7046     if (FD->hasAttr<OpenCLKernelAttr>()) {
7047       // OpenCL __kernel functions get kernel metadata
7048       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7049       addNVVMMetadata(F, "kernel", 1);
7050       // And kernel functions are not subject to inlining
7051       F->addFnAttr(llvm::Attribute::NoInline);
7052     }
7053   }
7054 
7055   // Perform special handling in CUDA mode.
7056   if (M.getLangOpts().CUDA) {
7057     // CUDA __global__ functions get a kernel metadata entry.  Since
7058     // __global__ functions cannot be called from the device, we do not
7059     // need to set the noinline attribute.
7060     if (FD->hasAttr<CUDAGlobalAttr>()) {
7061       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7062       addNVVMMetadata(F, "kernel", 1);
7063     }
7064     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
7065       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
7066       llvm::APSInt MaxThreads(32);
7067       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
7068       if (MaxThreads > 0)
7069         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
7070 
7071       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
7072       // not specified in __launch_bounds__ or if the user specified a 0 value,
7073       // we don't have to add a PTX directive.
7074       if (Attr->getMinBlocks()) {
7075         llvm::APSInt MinBlocks(32);
7076         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
7077         if (MinBlocks > 0)
7078           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
7079           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
7080       }
7081     }
7082   }
7083 }
7084 
7085 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
7086                                              StringRef Name, int Operand) {
7087   llvm::Module *M = GV->getParent();
7088   llvm::LLVMContext &Ctx = M->getContext();
7089 
7090   // Get "nvvm.annotations" metadata node
7091   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
7092 
7093   llvm::Metadata *MDVals[] = {
7094       llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
7095       llvm::ConstantAsMetadata::get(
7096           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
7097   // Append metadata to nvvm.annotations
7098   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7099 }
7100 
7101 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
7102   return false;
7103 }
7104 }
7105 
7106 //===----------------------------------------------------------------------===//
7107 // SystemZ ABI Implementation
7108 //===----------------------------------------------------------------------===//
7109 
7110 namespace {
7111 
7112 class SystemZABIInfo : public SwiftABIInfo {
7113   bool HasVector;
7114   bool IsSoftFloatABI;
7115 
7116 public:
7117   SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
7118     : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
7119 
7120   bool isPromotableIntegerTypeForABI(QualType Ty) const;
7121   bool isCompoundType(QualType Ty) const;
7122   bool isVectorArgumentType(QualType Ty) const;
7123   bool isFPArgumentType(QualType Ty) const;
7124   QualType GetSingleElementType(QualType Ty) const;
7125 
7126   ABIArgInfo classifyReturnType(QualType RetTy) const;
7127   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
7128 
7129   void computeInfo(CGFunctionInfo &FI) const override {
7130     if (!getCXXABI().classifyReturnType(FI))
7131       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7132     for (auto &I : FI.arguments())
7133       I.info = classifyArgumentType(I.type);
7134   }
7135 
7136   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7137                     QualType Ty) const override;
7138 
7139   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
7140                                     bool asReturnValue) const override {
7141     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
7142   }
7143   bool isSwiftErrorInRegister() const override {
7144     return false;
7145   }
7146 };
7147 
7148 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
7149 public:
7150   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
7151       : TargetCodeGenInfo(
7152             std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {}
7153 };
7154 
7155 }
7156 
7157 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
7158   // Treat an enum type as its underlying type.
7159   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7160     Ty = EnumTy->getDecl()->getIntegerType();
7161 
7162   // Promotable integer types are required to be promoted by the ABI.
7163   if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
7164     return true;
7165 
7166   if (const auto *EIT = Ty->getAs<ExtIntType>())
7167     if (EIT->getNumBits() < 64)
7168       return true;
7169 
7170   // 32-bit values must also be promoted.
7171   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7172     switch (BT->getKind()) {
7173     case BuiltinType::Int:
7174     case BuiltinType::UInt:
7175       return true;
7176     default:
7177       return false;
7178     }
7179   return false;
7180 }
7181 
7182 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
7183   return (Ty->isAnyComplexType() ||
7184           Ty->isVectorType() ||
7185           isAggregateTypeForABI(Ty));
7186 }
7187 
7188 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
7189   return (HasVector &&
7190           Ty->isVectorType() &&
7191           getContext().getTypeSize(Ty) <= 128);
7192 }
7193 
7194 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
7195   if (IsSoftFloatABI)
7196     return false;
7197 
7198   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7199     switch (BT->getKind()) {
7200     case BuiltinType::Float:
7201     case BuiltinType::Double:
7202       return true;
7203     default:
7204       return false;
7205     }
7206 
7207   return false;
7208 }
7209 
7210 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
7211   const RecordType *RT = Ty->getAs<RecordType>();
7212 
7213   if (RT && RT->isStructureOrClassType()) {
7214     const RecordDecl *RD = RT->getDecl();
7215     QualType Found;
7216 
7217     // If this is a C++ record, check the bases first.
7218     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7219       for (const auto &I : CXXRD->bases()) {
7220         QualType Base = I.getType();
7221 
7222         // Empty bases don't affect things either way.
7223         if (isEmptyRecord(getContext(), Base, true))
7224           continue;
7225 
7226         if (!Found.isNull())
7227           return Ty;
7228         Found = GetSingleElementType(Base);
7229       }
7230 
7231     // Check the fields.
7232     for (const auto *FD : RD->fields()) {
7233       // For compatibility with GCC, ignore empty bitfields in C++ mode.
7234       // Unlike isSingleElementStruct(), empty structure and array fields
7235       // do count.  So do anonymous bitfields that aren't zero-sized.
7236       if (getContext().getLangOpts().CPlusPlus &&
7237           FD->isZeroLengthBitField(getContext()))
7238         continue;
7239 
7240       // Unlike isSingleElementStruct(), arrays do not count.
7241       // Nested structures still do though.
7242       if (!Found.isNull())
7243         return Ty;
7244       Found = GetSingleElementType(FD->getType());
7245     }
7246 
7247     // Unlike isSingleElementStruct(), trailing padding is allowed.
7248     // An 8-byte aligned struct s { float f; } is passed as a double.
7249     if (!Found.isNull())
7250       return Found;
7251   }
7252 
7253   return Ty;
7254 }
7255 
7256 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7257                                   QualType Ty) const {
7258   // Assume that va_list type is correct; should be pointer to LLVM type:
7259   // struct {
7260   //   i64 __gpr;
7261   //   i64 __fpr;
7262   //   i8 *__overflow_arg_area;
7263   //   i8 *__reg_save_area;
7264   // };
7265 
7266   // Every non-vector argument occupies 8 bytes and is passed by preference
7267   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
7268   // always passed on the stack.
7269   Ty = getContext().getCanonicalType(Ty);
7270   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7271   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
7272   llvm::Type *DirectTy = ArgTy;
7273   ABIArgInfo AI = classifyArgumentType(Ty);
7274   bool IsIndirect = AI.isIndirect();
7275   bool InFPRs = false;
7276   bool IsVector = false;
7277   CharUnits UnpaddedSize;
7278   CharUnits DirectAlign;
7279   if (IsIndirect) {
7280     DirectTy = llvm::PointerType::getUnqual(DirectTy);
7281     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
7282   } else {
7283     if (AI.getCoerceToType())
7284       ArgTy = AI.getCoerceToType();
7285     InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
7286     IsVector = ArgTy->isVectorTy();
7287     UnpaddedSize = TyInfo.first;
7288     DirectAlign = TyInfo.second;
7289   }
7290   CharUnits PaddedSize = CharUnits::fromQuantity(8);
7291   if (IsVector && UnpaddedSize > PaddedSize)
7292     PaddedSize = CharUnits::fromQuantity(16);
7293   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
7294 
7295   CharUnits Padding = (PaddedSize - UnpaddedSize);
7296 
7297   llvm::Type *IndexTy = CGF.Int64Ty;
7298   llvm::Value *PaddedSizeV =
7299     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
7300 
7301   if (IsVector) {
7302     // Work out the address of a vector argument on the stack.
7303     // Vector arguments are always passed in the high bits of a
7304     // single (8 byte) or double (16 byte) stack slot.
7305     Address OverflowArgAreaPtr =
7306         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7307     Address OverflowArgArea =
7308       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7309               TyInfo.second);
7310     Address MemAddr =
7311       CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
7312 
7313     // Update overflow_arg_area_ptr pointer
7314     llvm::Value *NewOverflowArgArea =
7315       CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
7316                             "overflow_arg_area");
7317     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7318 
7319     return MemAddr;
7320   }
7321 
7322   assert(PaddedSize.getQuantity() == 8);
7323 
7324   unsigned MaxRegs, RegCountField, RegSaveIndex;
7325   CharUnits RegPadding;
7326   if (InFPRs) {
7327     MaxRegs = 4; // Maximum of 4 FPR arguments
7328     RegCountField = 1; // __fpr
7329     RegSaveIndex = 16; // save offset for f0
7330     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
7331   } else {
7332     MaxRegs = 5; // Maximum of 5 GPR arguments
7333     RegCountField = 0; // __gpr
7334     RegSaveIndex = 2; // save offset for r2
7335     RegPadding = Padding; // values are passed in the low bits of a GPR
7336   }
7337 
7338   Address RegCountPtr =
7339       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
7340   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
7341   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
7342   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
7343                                                  "fits_in_regs");
7344 
7345   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
7346   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
7347   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
7348   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
7349 
7350   // Emit code to load the value if it was passed in registers.
7351   CGF.EmitBlock(InRegBlock);
7352 
7353   // Work out the address of an argument register.
7354   llvm::Value *ScaledRegCount =
7355     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
7356   llvm::Value *RegBase =
7357     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
7358                                       + RegPadding.getQuantity());
7359   llvm::Value *RegOffset =
7360     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
7361   Address RegSaveAreaPtr =
7362       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
7363   llvm::Value *RegSaveArea =
7364     CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
7365   Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
7366                                            "raw_reg_addr"),
7367                      PaddedSize);
7368   Address RegAddr =
7369     CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
7370 
7371   // Update the register count
7372   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
7373   llvm::Value *NewRegCount =
7374     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
7375   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
7376   CGF.EmitBranch(ContBlock);
7377 
7378   // Emit code to load the value if it was passed in memory.
7379   CGF.EmitBlock(InMemBlock);
7380 
7381   // Work out the address of a stack argument.
7382   Address OverflowArgAreaPtr =
7383       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7384   Address OverflowArgArea =
7385     Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7386             PaddedSize);
7387   Address RawMemAddr =
7388     CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
7389   Address MemAddr =
7390     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
7391 
7392   // Update overflow_arg_area_ptr pointer
7393   llvm::Value *NewOverflowArgArea =
7394     CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
7395                           "overflow_arg_area");
7396   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7397   CGF.EmitBranch(ContBlock);
7398 
7399   // Return the appropriate result.
7400   CGF.EmitBlock(ContBlock);
7401   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
7402                                  MemAddr, InMemBlock, "va_arg.addr");
7403 
7404   if (IsIndirect)
7405     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
7406                       TyInfo.second);
7407 
7408   return ResAddr;
7409 }
7410 
7411 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
7412   if (RetTy->isVoidType())
7413     return ABIArgInfo::getIgnore();
7414   if (isVectorArgumentType(RetTy))
7415     return ABIArgInfo::getDirect();
7416   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
7417     return getNaturalAlignIndirect(RetTy);
7418   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7419                                                : ABIArgInfo::getDirect());
7420 }
7421 
7422 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
7423   // Handle the generic C++ ABI.
7424   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7425     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7426 
7427   // Integers and enums are extended to full register width.
7428   if (isPromotableIntegerTypeForABI(Ty))
7429     return ABIArgInfo::getExtend(Ty);
7430 
7431   // Handle vector types and vector-like structure types.  Note that
7432   // as opposed to float-like structure types, we do not allow any
7433   // padding for vector-like structures, so verify the sizes match.
7434   uint64_t Size = getContext().getTypeSize(Ty);
7435   QualType SingleElementTy = GetSingleElementType(Ty);
7436   if (isVectorArgumentType(SingleElementTy) &&
7437       getContext().getTypeSize(SingleElementTy) == Size)
7438     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
7439 
7440   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
7441   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
7442     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7443 
7444   // Handle small structures.
7445   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7446     // Structures with flexible arrays have variable length, so really
7447     // fail the size test above.
7448     const RecordDecl *RD = RT->getDecl();
7449     if (RD->hasFlexibleArrayMember())
7450       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7451 
7452     // The structure is passed as an unextended integer, a float, or a double.
7453     llvm::Type *PassTy;
7454     if (isFPArgumentType(SingleElementTy)) {
7455       assert(Size == 32 || Size == 64);
7456       if (Size == 32)
7457         PassTy = llvm::Type::getFloatTy(getVMContext());
7458       else
7459         PassTy = llvm::Type::getDoubleTy(getVMContext());
7460     } else
7461       PassTy = llvm::IntegerType::get(getVMContext(), Size);
7462     return ABIArgInfo::getDirect(PassTy);
7463   }
7464 
7465   // Non-structure compounds are passed indirectly.
7466   if (isCompoundType(Ty))
7467     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7468 
7469   return ABIArgInfo::getDirect(nullptr);
7470 }
7471 
7472 //===----------------------------------------------------------------------===//
7473 // MSP430 ABI Implementation
7474 //===----------------------------------------------------------------------===//
7475 
7476 namespace {
7477 
7478 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7479 public:
7480   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7481       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
7482   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7483                            CodeGen::CodeGenModule &M) const override;
7484 };
7485 
7486 }
7487 
7488 void MSP430TargetCodeGenInfo::setTargetAttributes(
7489     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7490   if (GV->isDeclaration())
7491     return;
7492   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7493     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7494     if (!InterruptAttr)
7495       return;
7496 
7497     // Handle 'interrupt' attribute:
7498     llvm::Function *F = cast<llvm::Function>(GV);
7499 
7500     // Step 1: Set ISR calling convention.
7501     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7502 
7503     // Step 2: Add attributes goodness.
7504     F->addFnAttr(llvm::Attribute::NoInline);
7505     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7506   }
7507 }
7508 
7509 //===----------------------------------------------------------------------===//
7510 // MIPS ABI Implementation.  This works for both little-endian and
7511 // big-endian variants.
7512 //===----------------------------------------------------------------------===//
7513 
7514 namespace {
7515 class MipsABIInfo : public ABIInfo {
7516   bool IsO32;
7517   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7518   void CoerceToIntArgs(uint64_t TySize,
7519                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7520   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7521   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7522   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7523 public:
7524   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7525     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7526     StackAlignInBytes(IsO32 ? 8 : 16) {}
7527 
7528   ABIArgInfo classifyReturnType(QualType RetTy) const;
7529   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7530   void computeInfo(CGFunctionInfo &FI) const override;
7531   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7532                     QualType Ty) const override;
7533   ABIArgInfo extendType(QualType Ty) const;
7534 };
7535 
7536 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7537   unsigned SizeOfUnwindException;
7538 public:
7539   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7540       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7541         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7542 
7543   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7544     return 29;
7545   }
7546 
7547   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7548                            CodeGen::CodeGenModule &CGM) const override {
7549     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7550     if (!FD) return;
7551     llvm::Function *Fn = cast<llvm::Function>(GV);
7552 
7553     if (FD->hasAttr<MipsLongCallAttr>())
7554       Fn->addFnAttr("long-call");
7555     else if (FD->hasAttr<MipsShortCallAttr>())
7556       Fn->addFnAttr("short-call");
7557 
7558     // Other attributes do not have a meaning for declarations.
7559     if (GV->isDeclaration())
7560       return;
7561 
7562     if (FD->hasAttr<Mips16Attr>()) {
7563       Fn->addFnAttr("mips16");
7564     }
7565     else if (FD->hasAttr<NoMips16Attr>()) {
7566       Fn->addFnAttr("nomips16");
7567     }
7568 
7569     if (FD->hasAttr<MicroMipsAttr>())
7570       Fn->addFnAttr("micromips");
7571     else if (FD->hasAttr<NoMicroMipsAttr>())
7572       Fn->addFnAttr("nomicromips");
7573 
7574     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7575     if (!Attr)
7576       return;
7577 
7578     const char *Kind;
7579     switch (Attr->getInterrupt()) {
7580     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7581     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7582     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7583     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7584     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7585     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7586     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7587     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7588     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7589     }
7590 
7591     Fn->addFnAttr("interrupt", Kind);
7592 
7593   }
7594 
7595   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7596                                llvm::Value *Address) const override;
7597 
7598   unsigned getSizeOfUnwindException() const override {
7599     return SizeOfUnwindException;
7600   }
7601 };
7602 }
7603 
7604 void MipsABIInfo::CoerceToIntArgs(
7605     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7606   llvm::IntegerType *IntTy =
7607     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7608 
7609   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7610   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7611     ArgList.push_back(IntTy);
7612 
7613   // If necessary, add one more integer type to ArgList.
7614   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7615 
7616   if (R)
7617     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7618 }
7619 
7620 // In N32/64, an aligned double precision floating point field is passed in
7621 // a register.
7622 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7623   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7624 
7625   if (IsO32) {
7626     CoerceToIntArgs(TySize, ArgList);
7627     return llvm::StructType::get(getVMContext(), ArgList);
7628   }
7629 
7630   if (Ty->isComplexType())
7631     return CGT.ConvertType(Ty);
7632 
7633   const RecordType *RT = Ty->getAs<RecordType>();
7634 
7635   // Unions/vectors are passed in integer registers.
7636   if (!RT || !RT->isStructureOrClassType()) {
7637     CoerceToIntArgs(TySize, ArgList);
7638     return llvm::StructType::get(getVMContext(), ArgList);
7639   }
7640 
7641   const RecordDecl *RD = RT->getDecl();
7642   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7643   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7644 
7645   uint64_t LastOffset = 0;
7646   unsigned idx = 0;
7647   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7648 
7649   // Iterate over fields in the struct/class and check if there are any aligned
7650   // double fields.
7651   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
7652        i != e; ++i, ++idx) {
7653     const QualType Ty = i->getType();
7654     const BuiltinType *BT = Ty->getAs<BuiltinType>();
7655 
7656     if (!BT || BT->getKind() != BuiltinType::Double)
7657       continue;
7658 
7659     uint64_t Offset = Layout.getFieldOffset(idx);
7660     if (Offset % 64) // Ignore doubles that are not aligned.
7661       continue;
7662 
7663     // Add ((Offset - LastOffset) / 64) args of type i64.
7664     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
7665       ArgList.push_back(I64);
7666 
7667     // Add double type.
7668     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
7669     LastOffset = Offset + 64;
7670   }
7671 
7672   CoerceToIntArgs(TySize - LastOffset, IntArgList);
7673   ArgList.append(IntArgList.begin(), IntArgList.end());
7674 
7675   return llvm::StructType::get(getVMContext(), ArgList);
7676 }
7677 
7678 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
7679                                         uint64_t Offset) const {
7680   if (OrigOffset + MinABIStackAlignInBytes > Offset)
7681     return nullptr;
7682 
7683   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
7684 }
7685 
7686 ABIArgInfo
7687 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
7688   Ty = useFirstFieldIfTransparentUnion(Ty);
7689 
7690   uint64_t OrigOffset = Offset;
7691   uint64_t TySize = getContext().getTypeSize(Ty);
7692   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
7693 
7694   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
7695                    (uint64_t)StackAlignInBytes);
7696   unsigned CurrOffset = llvm::alignTo(Offset, Align);
7697   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
7698 
7699   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
7700     // Ignore empty aggregates.
7701     if (TySize == 0)
7702       return ABIArgInfo::getIgnore();
7703 
7704     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
7705       Offset = OrigOffset + MinABIStackAlignInBytes;
7706       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7707     }
7708 
7709     // If we have reached here, aggregates are passed directly by coercing to
7710     // another structure type. Padding is inserted if the offset of the
7711     // aggregate is unaligned.
7712     ABIArgInfo ArgInfo =
7713         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
7714                               getPaddingType(OrigOffset, CurrOffset));
7715     ArgInfo.setInReg(true);
7716     return ArgInfo;
7717   }
7718 
7719   // Treat an enum type as its underlying type.
7720   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7721     Ty = EnumTy->getDecl()->getIntegerType();
7722 
7723   // Make sure we pass indirectly things that are too large.
7724   if (const auto *EIT = Ty->getAs<ExtIntType>())
7725     if (EIT->getNumBits() > 128 ||
7726         (EIT->getNumBits() > 64 &&
7727          !getContext().getTargetInfo().hasInt128Type()))
7728       return getNaturalAlignIndirect(Ty);
7729 
7730   // All integral types are promoted to the GPR width.
7731   if (Ty->isIntegralOrEnumerationType())
7732     return extendType(Ty);
7733 
7734   return ABIArgInfo::getDirect(
7735       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
7736 }
7737 
7738 llvm::Type*
7739 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
7740   const RecordType *RT = RetTy->getAs<RecordType>();
7741   SmallVector<llvm::Type*, 8> RTList;
7742 
7743   if (RT && RT->isStructureOrClassType()) {
7744     const RecordDecl *RD = RT->getDecl();
7745     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7746     unsigned FieldCnt = Layout.getFieldCount();
7747 
7748     // N32/64 returns struct/classes in floating point registers if the
7749     // following conditions are met:
7750     // 1. The size of the struct/class is no larger than 128-bit.
7751     // 2. The struct/class has one or two fields all of which are floating
7752     //    point types.
7753     // 3. The offset of the first field is zero (this follows what gcc does).
7754     //
7755     // Any other composite results are returned in integer registers.
7756     //
7757     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
7758       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
7759       for (; b != e; ++b) {
7760         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
7761 
7762         if (!BT || !BT->isFloatingPoint())
7763           break;
7764 
7765         RTList.push_back(CGT.ConvertType(b->getType()));
7766       }
7767 
7768       if (b == e)
7769         return llvm::StructType::get(getVMContext(), RTList,
7770                                      RD->hasAttr<PackedAttr>());
7771 
7772       RTList.clear();
7773     }
7774   }
7775 
7776   CoerceToIntArgs(Size, RTList);
7777   return llvm::StructType::get(getVMContext(), RTList);
7778 }
7779 
7780 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
7781   uint64_t Size = getContext().getTypeSize(RetTy);
7782 
7783   if (RetTy->isVoidType())
7784     return ABIArgInfo::getIgnore();
7785 
7786   // O32 doesn't treat zero-sized structs differently from other structs.
7787   // However, N32/N64 ignores zero sized return values.
7788   if (!IsO32 && Size == 0)
7789     return ABIArgInfo::getIgnore();
7790 
7791   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
7792     if (Size <= 128) {
7793       if (RetTy->isAnyComplexType())
7794         return ABIArgInfo::getDirect();
7795 
7796       // O32 returns integer vectors in registers and N32/N64 returns all small
7797       // aggregates in registers.
7798       if (!IsO32 ||
7799           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
7800         ABIArgInfo ArgInfo =
7801             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
7802         ArgInfo.setInReg(true);
7803         return ArgInfo;
7804       }
7805     }
7806 
7807     return getNaturalAlignIndirect(RetTy);
7808   }
7809 
7810   // Treat an enum type as its underlying type.
7811   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7812     RetTy = EnumTy->getDecl()->getIntegerType();
7813 
7814   // Make sure we pass indirectly things that are too large.
7815   if (const auto *EIT = RetTy->getAs<ExtIntType>())
7816     if (EIT->getNumBits() > 128 ||
7817         (EIT->getNumBits() > 64 &&
7818          !getContext().getTargetInfo().hasInt128Type()))
7819       return getNaturalAlignIndirect(RetTy);
7820 
7821   if (isPromotableIntegerTypeForABI(RetTy))
7822     return ABIArgInfo::getExtend(RetTy);
7823 
7824   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
7825       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
7826     return ABIArgInfo::getSignExtend(RetTy);
7827 
7828   return ABIArgInfo::getDirect();
7829 }
7830 
7831 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
7832   ABIArgInfo &RetInfo = FI.getReturnInfo();
7833   if (!getCXXABI().classifyReturnType(FI))
7834     RetInfo = classifyReturnType(FI.getReturnType());
7835 
7836   // Check if a pointer to an aggregate is passed as a hidden argument.
7837   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
7838 
7839   for (auto &I : FI.arguments())
7840     I.info = classifyArgumentType(I.type, Offset);
7841 }
7842 
7843 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7844                                QualType OrigTy) const {
7845   QualType Ty = OrigTy;
7846 
7847   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
7848   // Pointers are also promoted in the same way but this only matters for N32.
7849   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
7850   unsigned PtrWidth = getTarget().getPointerWidth(0);
7851   bool DidPromote = false;
7852   if ((Ty->isIntegerType() &&
7853           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
7854       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
7855     DidPromote = true;
7856     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
7857                                             Ty->isSignedIntegerType());
7858   }
7859 
7860   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7861 
7862   // The alignment of things in the argument area is never larger than
7863   // StackAlignInBytes.
7864   TyInfo.second =
7865     std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
7866 
7867   // MinABIStackAlignInBytes is the size of argument slots on the stack.
7868   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
7869 
7870   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7871                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
7872 
7873 
7874   // If there was a promotion, "unpromote" into a temporary.
7875   // TODO: can we just use a pointer into a subset of the original slot?
7876   if (DidPromote) {
7877     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
7878     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
7879 
7880     // Truncate down to the right width.
7881     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
7882                                                  : CGF.IntPtrTy);
7883     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
7884     if (OrigTy->isPointerType())
7885       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
7886 
7887     CGF.Builder.CreateStore(V, Temp);
7888     Addr = Temp;
7889   }
7890 
7891   return Addr;
7892 }
7893 
7894 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
7895   int TySize = getContext().getTypeSize(Ty);
7896 
7897   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7898   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
7899     return ABIArgInfo::getSignExtend(Ty);
7900 
7901   return ABIArgInfo::getExtend(Ty);
7902 }
7903 
7904 bool
7905 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7906                                                llvm::Value *Address) const {
7907   // This information comes from gcc's implementation, which seems to
7908   // as canonical as it gets.
7909 
7910   // Everything on MIPS is 4 bytes.  Double-precision FP registers
7911   // are aliased to pairs of single-precision FP registers.
7912   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
7913 
7914   // 0-31 are the general purpose registers, $0 - $31.
7915   // 32-63 are the floating-point registers, $f0 - $f31.
7916   // 64 and 65 are the multiply/divide registers, $hi and $lo.
7917   // 66 is the (notional, I think) register for signal-handler return.
7918   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
7919 
7920   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7921   // They are one bit wide and ignored here.
7922 
7923   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7924   // (coprocessor 1 is the FP unit)
7925   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7926   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7927   // 176-181 are the DSP accumulator registers.
7928   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
7929   return false;
7930 }
7931 
7932 //===----------------------------------------------------------------------===//
7933 // AVR ABI Implementation.
7934 //===----------------------------------------------------------------------===//
7935 
7936 namespace {
7937 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7938 public:
7939   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7940       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
7941 
7942   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7943                            CodeGen::CodeGenModule &CGM) const override {
7944     if (GV->isDeclaration())
7945       return;
7946     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7947     if (!FD) return;
7948     auto *Fn = cast<llvm::Function>(GV);
7949 
7950     if (FD->getAttr<AVRInterruptAttr>())
7951       Fn->addFnAttr("interrupt");
7952 
7953     if (FD->getAttr<AVRSignalAttr>())
7954       Fn->addFnAttr("signal");
7955   }
7956 };
7957 }
7958 
7959 //===----------------------------------------------------------------------===//
7960 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
7961 // Currently subclassed only to implement custom OpenCL C function attribute
7962 // handling.
7963 //===----------------------------------------------------------------------===//
7964 
7965 namespace {
7966 
7967 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
7968 public:
7969   TCETargetCodeGenInfo(CodeGenTypes &CGT)
7970     : DefaultTargetCodeGenInfo(CGT) {}
7971 
7972   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7973                            CodeGen::CodeGenModule &M) const override;
7974 };
7975 
7976 void TCETargetCodeGenInfo::setTargetAttributes(
7977     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7978   if (GV->isDeclaration())
7979     return;
7980   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7981   if (!FD) return;
7982 
7983   llvm::Function *F = cast<llvm::Function>(GV);
7984 
7985   if (M.getLangOpts().OpenCL) {
7986     if (FD->hasAttr<OpenCLKernelAttr>()) {
7987       // OpenCL C Kernel functions are not subject to inlining
7988       F->addFnAttr(llvm::Attribute::NoInline);
7989       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7990       if (Attr) {
7991         // Convert the reqd_work_group_size() attributes to metadata.
7992         llvm::LLVMContext &Context = F->getContext();
7993         llvm::NamedMDNode *OpenCLMetadata =
7994             M.getModule().getOrInsertNamedMetadata(
7995                 "opencl.kernel_wg_size_info");
7996 
7997         SmallVector<llvm::Metadata *, 5> Operands;
7998         Operands.push_back(llvm::ConstantAsMetadata::get(F));
7999 
8000         Operands.push_back(
8001             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8002                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8003         Operands.push_back(
8004             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8005                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8006         Operands.push_back(
8007             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8008                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8009 
8010         // Add a boolean constant operand for "required" (true) or "hint"
8011         // (false) for implementing the work_group_size_hint attr later.
8012         // Currently always true as the hint is not yet implemented.
8013         Operands.push_back(
8014             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8015         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8016       }
8017     }
8018   }
8019 }
8020 
8021 }
8022 
8023 //===----------------------------------------------------------------------===//
8024 // Hexagon ABI Implementation
8025 //===----------------------------------------------------------------------===//
8026 
8027 namespace {
8028 
8029 class HexagonABIInfo : public DefaultABIInfo {
8030 public:
8031   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8032 
8033 private:
8034   ABIArgInfo classifyReturnType(QualType RetTy) const;
8035   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8036   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8037 
8038   void computeInfo(CGFunctionInfo &FI) const override;
8039 
8040   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8041                     QualType Ty) const override;
8042   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8043                               QualType Ty) const;
8044   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8045                               QualType Ty) const;
8046   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8047                                    QualType Ty) const;
8048 };
8049 
8050 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8051 public:
8052   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8053       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8054 
8055   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8056     return 29;
8057   }
8058 
8059   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8060                            CodeGen::CodeGenModule &GCM) const override {
8061     if (GV->isDeclaration())
8062       return;
8063     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8064     if (!FD)
8065       return;
8066   }
8067 };
8068 
8069 } // namespace
8070 
8071 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8072   unsigned RegsLeft = 6;
8073   if (!getCXXABI().classifyReturnType(FI))
8074     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8075   for (auto &I : FI.arguments())
8076     I.info = classifyArgumentType(I.type, &RegsLeft);
8077 }
8078 
8079 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8080   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8081                        " through registers");
8082 
8083   if (*RegsLeft == 0)
8084     return false;
8085 
8086   if (Size <= 32) {
8087     (*RegsLeft)--;
8088     return true;
8089   }
8090 
8091   if (2 <= (*RegsLeft & (~1U))) {
8092     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8093     return true;
8094   }
8095 
8096   // Next available register was r5 but candidate was greater than 32-bits so it
8097   // has to go on the stack. However we still consume r5
8098   if (*RegsLeft == 1)
8099     *RegsLeft = 0;
8100 
8101   return false;
8102 }
8103 
8104 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8105                                                 unsigned *RegsLeft) const {
8106   if (!isAggregateTypeForABI(Ty)) {
8107     // Treat an enum type as its underlying type.
8108     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8109       Ty = EnumTy->getDecl()->getIntegerType();
8110 
8111     uint64_t Size = getContext().getTypeSize(Ty);
8112     if (Size <= 64)
8113       HexagonAdjustRegsLeft(Size, RegsLeft);
8114 
8115     if (Size > 64 && Ty->isExtIntType())
8116       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8117 
8118     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8119                                              : ABIArgInfo::getDirect();
8120   }
8121 
8122   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8123     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8124 
8125   // Ignore empty records.
8126   if (isEmptyRecord(getContext(), Ty, true))
8127     return ABIArgInfo::getIgnore();
8128 
8129   uint64_t Size = getContext().getTypeSize(Ty);
8130   unsigned Align = getContext().getTypeAlign(Ty);
8131 
8132   if (Size > 64)
8133     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8134 
8135   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8136     Align = Size <= 32 ? 32 : 64;
8137   if (Size <= Align) {
8138     // Pass in the smallest viable integer type.
8139     if (!llvm::isPowerOf2_64(Size))
8140       Size = llvm::NextPowerOf2(Size);
8141     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8142   }
8143   return DefaultABIInfo::classifyArgumentType(Ty);
8144 }
8145 
8146 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8147   if (RetTy->isVoidType())
8148     return ABIArgInfo::getIgnore();
8149 
8150   const TargetInfo &T = CGT.getTarget();
8151   uint64_t Size = getContext().getTypeSize(RetTy);
8152 
8153   if (RetTy->getAs<VectorType>()) {
8154     // HVX vectors are returned in vector registers or register pairs.
8155     if (T.hasFeature("hvx")) {
8156       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8157       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8158       if (Size == VecSize || Size == 2*VecSize)
8159         return ABIArgInfo::getDirectInReg();
8160     }
8161     // Large vector types should be returned via memory.
8162     if (Size > 64)
8163       return getNaturalAlignIndirect(RetTy);
8164   }
8165 
8166   if (!isAggregateTypeForABI(RetTy)) {
8167     // Treat an enum type as its underlying type.
8168     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8169       RetTy = EnumTy->getDecl()->getIntegerType();
8170 
8171     if (Size > 64 && RetTy->isExtIntType())
8172       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8173 
8174     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8175                                                 : ABIArgInfo::getDirect();
8176   }
8177 
8178   if (isEmptyRecord(getContext(), RetTy, true))
8179     return ABIArgInfo::getIgnore();
8180 
8181   // Aggregates <= 8 bytes are returned in registers, other aggregates
8182   // are returned indirectly.
8183   if (Size <= 64) {
8184     // Return in the smallest viable integer type.
8185     if (!llvm::isPowerOf2_64(Size))
8186       Size = llvm::NextPowerOf2(Size);
8187     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8188   }
8189   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8190 }
8191 
8192 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8193                                             Address VAListAddr,
8194                                             QualType Ty) const {
8195   // Load the overflow area pointer.
8196   Address __overflow_area_pointer_p =
8197       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8198   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8199       __overflow_area_pointer_p, "__overflow_area_pointer");
8200 
8201   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8202   if (Align > 4) {
8203     // Alignment should be a power of 2.
8204     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8205 
8206     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8207     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8208 
8209     // Add offset to the current pointer to access the argument.
8210     __overflow_area_pointer =
8211         CGF.Builder.CreateGEP(__overflow_area_pointer, Offset);
8212     llvm::Value *AsInt =
8213         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8214 
8215     // Create a mask which should be "AND"ed
8216     // with (overflow_arg_area + align - 1)
8217     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8218     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8219         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8220         "__overflow_area_pointer.align");
8221   }
8222 
8223   // Get the type of the argument from memory and bitcast
8224   // overflow area pointer to the argument type.
8225   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8226   Address AddrTyped = CGF.Builder.CreateBitCast(
8227       Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)),
8228       llvm::PointerType::getUnqual(PTy));
8229 
8230   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8231   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8232 
8233   __overflow_area_pointer = CGF.Builder.CreateGEP(
8234       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8235       "__overflow_area_pointer.next");
8236   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8237 
8238   return AddrTyped;
8239 }
8240 
8241 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8242                                             Address VAListAddr,
8243                                             QualType Ty) const {
8244   // FIXME: Need to handle alignment
8245   llvm::Type *BP = CGF.Int8PtrTy;
8246   llvm::Type *BPP = CGF.Int8PtrPtrTy;
8247   CGBuilderTy &Builder = CGF.Builder;
8248   Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
8249   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8250   // Handle address alignment for type alignment > 32 bits
8251   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8252   if (TyAlign > 4) {
8253     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8254     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8255     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8256     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8257     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8258   }
8259   llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
8260   Address AddrTyped = Builder.CreateBitCast(
8261       Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy);
8262 
8263   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8264   llvm::Value *NextAddr = Builder.CreateGEP(
8265       Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8266   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8267 
8268   return AddrTyped;
8269 }
8270 
8271 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8272                                                  Address VAListAddr,
8273                                                  QualType Ty) const {
8274   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8275 
8276   if (ArgSize > 8)
8277     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8278 
8279   // Here we have check if the argument is in register area or
8280   // in overflow area.
8281   // If the saved register area pointer + argsize rounded up to alignment >
8282   // saved register area end pointer, argument is in overflow area.
8283   unsigned RegsLeft = 6;
8284   Ty = CGF.getContext().getCanonicalType(Ty);
8285   (void)classifyArgumentType(Ty, &RegsLeft);
8286 
8287   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8288   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8289   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8290   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8291 
8292   // Get rounded size of the argument.GCC does not allow vararg of
8293   // size < 4 bytes. We follow the same logic here.
8294   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8295   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8296 
8297   // Argument may be in saved register area
8298   CGF.EmitBlock(MaybeRegBlock);
8299 
8300   // Load the current saved register area pointer.
8301   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8302       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8303   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8304       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8305 
8306   // Load the saved register area end pointer.
8307   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8308       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8309   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8310       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8311 
8312   // If the size of argument is > 4 bytes, check if the stack
8313   // location is aligned to 8 bytes
8314   if (ArgAlign > 4) {
8315 
8316     llvm::Value *__current_saved_reg_area_pointer_int =
8317         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8318                                    CGF.Int32Ty);
8319 
8320     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8321         __current_saved_reg_area_pointer_int,
8322         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8323         "align_current_saved_reg_area_pointer");
8324 
8325     __current_saved_reg_area_pointer_int =
8326         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8327                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8328                               "align_current_saved_reg_area_pointer");
8329 
8330     __current_saved_reg_area_pointer =
8331         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8332                                    __current_saved_reg_area_pointer->getType(),
8333                                    "align_current_saved_reg_area_pointer");
8334   }
8335 
8336   llvm::Value *__new_saved_reg_area_pointer =
8337       CGF.Builder.CreateGEP(__current_saved_reg_area_pointer,
8338                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8339                             "__new_saved_reg_area_pointer");
8340 
8341   llvm::Value *UsingStack = 0;
8342   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8343                                          __saved_reg_area_end_pointer);
8344 
8345   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8346 
8347   // Argument in saved register area
8348   // Implement the block where argument is in register saved area
8349   CGF.EmitBlock(InRegBlock);
8350 
8351   llvm::Type *PTy = CGF.ConvertType(Ty);
8352   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8353       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8354 
8355   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8356                           __current_saved_reg_area_pointer_p);
8357 
8358   CGF.EmitBranch(ContBlock);
8359 
8360   // Argument in overflow area
8361   // Implement the block where the argument is in overflow area.
8362   CGF.EmitBlock(OnStackBlock);
8363 
8364   // Load the overflow area pointer
8365   Address __overflow_area_pointer_p =
8366       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8367   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8368       __overflow_area_pointer_p, "__overflow_area_pointer");
8369 
8370   // Align the overflow area pointer according to the alignment of the argument
8371   if (ArgAlign > 4) {
8372     llvm::Value *__overflow_area_pointer_int =
8373         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8374 
8375     __overflow_area_pointer_int =
8376         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8377                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8378                               "align_overflow_area_pointer");
8379 
8380     __overflow_area_pointer_int =
8381         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8382                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8383                               "align_overflow_area_pointer");
8384 
8385     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8386         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8387         "align_overflow_area_pointer");
8388   }
8389 
8390   // Get the pointer for next argument in overflow area and store it
8391   // to overflow area pointer.
8392   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8393       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8394       "__overflow_area_pointer.next");
8395 
8396   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8397                           __overflow_area_pointer_p);
8398 
8399   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8400                           __current_saved_reg_area_pointer_p);
8401 
8402   // Bitcast the overflow area pointer to the type of argument.
8403   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8404   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8405       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8406 
8407   CGF.EmitBranch(ContBlock);
8408 
8409   // Get the correct pointer to load the variable argument
8410   // Implement the ContBlock
8411   CGF.EmitBlock(ContBlock);
8412 
8413   llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
8414   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
8415   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
8416   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
8417 
8418   return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign));
8419 }
8420 
8421 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8422                                   QualType Ty) const {
8423 
8424   if (getTarget().getTriple().isMusl())
8425     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
8426 
8427   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
8428 }
8429 
8430 //===----------------------------------------------------------------------===//
8431 // Lanai ABI Implementation
8432 //===----------------------------------------------------------------------===//
8433 
8434 namespace {
8435 class LanaiABIInfo : public DefaultABIInfo {
8436 public:
8437   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8438 
8439   bool shouldUseInReg(QualType Ty, CCState &State) const;
8440 
8441   void computeInfo(CGFunctionInfo &FI) const override {
8442     CCState State(FI);
8443     // Lanai uses 4 registers to pass arguments unless the function has the
8444     // regparm attribute set.
8445     if (FI.getHasRegParm()) {
8446       State.FreeRegs = FI.getRegParm();
8447     } else {
8448       State.FreeRegs = 4;
8449     }
8450 
8451     if (!getCXXABI().classifyReturnType(FI))
8452       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8453     for (auto &I : FI.arguments())
8454       I.info = classifyArgumentType(I.type, State);
8455   }
8456 
8457   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
8458   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
8459 };
8460 } // end anonymous namespace
8461 
8462 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
8463   unsigned Size = getContext().getTypeSize(Ty);
8464   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
8465 
8466   if (SizeInRegs == 0)
8467     return false;
8468 
8469   if (SizeInRegs > State.FreeRegs) {
8470     State.FreeRegs = 0;
8471     return false;
8472   }
8473 
8474   State.FreeRegs -= SizeInRegs;
8475 
8476   return true;
8477 }
8478 
8479 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
8480                                            CCState &State) const {
8481   if (!ByVal) {
8482     if (State.FreeRegs) {
8483       --State.FreeRegs; // Non-byval indirects just use one pointer.
8484       return getNaturalAlignIndirectInReg(Ty);
8485     }
8486     return getNaturalAlignIndirect(Ty, false);
8487   }
8488 
8489   // Compute the byval alignment.
8490   const unsigned MinABIStackAlignInBytes = 4;
8491   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8492   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8493                                  /*Realign=*/TypeAlign >
8494                                      MinABIStackAlignInBytes);
8495 }
8496 
8497 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
8498                                               CCState &State) const {
8499   // Check with the C++ ABI first.
8500   const RecordType *RT = Ty->getAs<RecordType>();
8501   if (RT) {
8502     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8503     if (RAA == CGCXXABI::RAA_Indirect) {
8504       return getIndirectResult(Ty, /*ByVal=*/false, State);
8505     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
8506       return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
8507     }
8508   }
8509 
8510   if (isAggregateTypeForABI(Ty)) {
8511     // Structures with flexible arrays are always indirect.
8512     if (RT && RT->getDecl()->hasFlexibleArrayMember())
8513       return getIndirectResult(Ty, /*ByVal=*/true, State);
8514 
8515     // Ignore empty structs/unions.
8516     if (isEmptyRecord(getContext(), Ty, true))
8517       return ABIArgInfo::getIgnore();
8518 
8519     llvm::LLVMContext &LLVMContext = getVMContext();
8520     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
8521     if (SizeInRegs <= State.FreeRegs) {
8522       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
8523       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
8524       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8525       State.FreeRegs -= SizeInRegs;
8526       return ABIArgInfo::getDirectInReg(Result);
8527     } else {
8528       State.FreeRegs = 0;
8529     }
8530     return getIndirectResult(Ty, true, State);
8531   }
8532 
8533   // Treat an enum type as its underlying type.
8534   if (const auto *EnumTy = Ty->getAs<EnumType>())
8535     Ty = EnumTy->getDecl()->getIntegerType();
8536 
8537   bool InReg = shouldUseInReg(Ty, State);
8538 
8539   // Don't pass >64 bit integers in registers.
8540   if (const auto *EIT = Ty->getAs<ExtIntType>())
8541     if (EIT->getNumBits() > 64)
8542       return getIndirectResult(Ty, /*ByVal=*/true, State);
8543 
8544   if (isPromotableIntegerTypeForABI(Ty)) {
8545     if (InReg)
8546       return ABIArgInfo::getDirectInReg();
8547     return ABIArgInfo::getExtend(Ty);
8548   }
8549   if (InReg)
8550     return ABIArgInfo::getDirectInReg();
8551   return ABIArgInfo::getDirect();
8552 }
8553 
8554 namespace {
8555 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
8556 public:
8557   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8558       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
8559 };
8560 }
8561 
8562 //===----------------------------------------------------------------------===//
8563 // AMDGPU ABI Implementation
8564 //===----------------------------------------------------------------------===//
8565 
8566 namespace {
8567 
8568 class AMDGPUABIInfo final : public DefaultABIInfo {
8569 private:
8570   static const unsigned MaxNumRegsForArgsRet = 16;
8571 
8572   unsigned numRegsForType(QualType Ty) const;
8573 
8574   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
8575   bool isHomogeneousAggregateSmallEnough(const Type *Base,
8576                                          uint64_t Members) const override;
8577 
8578   // Coerce HIP pointer arguments from generic pointers to global ones.
8579   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
8580                                        unsigned ToAS) const {
8581     // Structure types.
8582     if (auto STy = dyn_cast<llvm::StructType>(Ty)) {
8583       SmallVector<llvm::Type *, 8> EltTys;
8584       bool Changed = false;
8585       for (auto T : STy->elements()) {
8586         auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
8587         EltTys.push_back(NT);
8588         Changed |= (NT != T);
8589       }
8590       // Skip if there is no change in element types.
8591       if (!Changed)
8592         return STy;
8593       if (STy->hasName())
8594         return llvm::StructType::create(
8595             EltTys, (STy->getName() + ".coerce").str(), STy->isPacked());
8596       return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked());
8597     }
8598     // Array types.
8599     if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) {
8600       auto T = ATy->getElementType();
8601       auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
8602       // Skip if there is no change in that element type.
8603       if (NT == T)
8604         return ATy;
8605       return llvm::ArrayType::get(NT, ATy->getNumElements());
8606     }
8607     // Single value types.
8608     if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS)
8609       return llvm::PointerType::get(
8610           cast<llvm::PointerType>(Ty)->getElementType(), ToAS);
8611     return Ty;
8612   }
8613 
8614 public:
8615   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
8616     DefaultABIInfo(CGT) {}
8617 
8618   ABIArgInfo classifyReturnType(QualType RetTy) const;
8619   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
8620   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
8621 
8622   void computeInfo(CGFunctionInfo &FI) const override;
8623   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8624                     QualType Ty) const override;
8625 };
8626 
8627 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
8628   return true;
8629 }
8630 
8631 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
8632   const Type *Base, uint64_t Members) const {
8633   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
8634 
8635   // Homogeneous Aggregates may occupy at most 16 registers.
8636   return Members * NumRegs <= MaxNumRegsForArgsRet;
8637 }
8638 
8639 /// Estimate number of registers the type will use when passed in registers.
8640 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
8641   unsigned NumRegs = 0;
8642 
8643   if (const VectorType *VT = Ty->getAs<VectorType>()) {
8644     // Compute from the number of elements. The reported size is based on the
8645     // in-memory size, which includes the padding 4th element for 3-vectors.
8646     QualType EltTy = VT->getElementType();
8647     unsigned EltSize = getContext().getTypeSize(EltTy);
8648 
8649     // 16-bit element vectors should be passed as packed.
8650     if (EltSize == 16)
8651       return (VT->getNumElements() + 1) / 2;
8652 
8653     unsigned EltNumRegs = (EltSize + 31) / 32;
8654     return EltNumRegs * VT->getNumElements();
8655   }
8656 
8657   if (const RecordType *RT = Ty->getAs<RecordType>()) {
8658     const RecordDecl *RD = RT->getDecl();
8659     assert(!RD->hasFlexibleArrayMember());
8660 
8661     for (const FieldDecl *Field : RD->fields()) {
8662       QualType FieldTy = Field->getType();
8663       NumRegs += numRegsForType(FieldTy);
8664     }
8665 
8666     return NumRegs;
8667   }
8668 
8669   return (getContext().getTypeSize(Ty) + 31) / 32;
8670 }
8671 
8672 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
8673   llvm::CallingConv::ID CC = FI.getCallingConvention();
8674 
8675   if (!getCXXABI().classifyReturnType(FI))
8676     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8677 
8678   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
8679   for (auto &Arg : FI.arguments()) {
8680     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
8681       Arg.info = classifyKernelArgumentType(Arg.type);
8682     } else {
8683       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
8684     }
8685   }
8686 }
8687 
8688 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8689                                  QualType Ty) const {
8690   llvm_unreachable("AMDGPU does not support varargs");
8691 }
8692 
8693 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
8694   if (isAggregateTypeForABI(RetTy)) {
8695     // Records with non-trivial destructors/copy-constructors should not be
8696     // returned by value.
8697     if (!getRecordArgABI(RetTy, getCXXABI())) {
8698       // Ignore empty structs/unions.
8699       if (isEmptyRecord(getContext(), RetTy, true))
8700         return ABIArgInfo::getIgnore();
8701 
8702       // Lower single-element structs to just return a regular value.
8703       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
8704         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
8705 
8706       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
8707         const RecordDecl *RD = RT->getDecl();
8708         if (RD->hasFlexibleArrayMember())
8709           return DefaultABIInfo::classifyReturnType(RetTy);
8710       }
8711 
8712       // Pack aggregates <= 4 bytes into single VGPR or pair.
8713       uint64_t Size = getContext().getTypeSize(RetTy);
8714       if (Size <= 16)
8715         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
8716 
8717       if (Size <= 32)
8718         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
8719 
8720       if (Size <= 64) {
8721         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
8722         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
8723       }
8724 
8725       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
8726         return ABIArgInfo::getDirect();
8727     }
8728   }
8729 
8730   // Otherwise just do the default thing.
8731   return DefaultABIInfo::classifyReturnType(RetTy);
8732 }
8733 
8734 /// For kernels all parameters are really passed in a special buffer. It doesn't
8735 /// make sense to pass anything byval, so everything must be direct.
8736 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
8737   Ty = useFirstFieldIfTransparentUnion(Ty);
8738 
8739   // TODO: Can we omit empty structs?
8740 
8741   llvm::Type *LTy = nullptr;
8742   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
8743     LTy = CGT.ConvertType(QualType(SeltTy, 0));
8744 
8745   if (getContext().getLangOpts().HIP) {
8746     if (!LTy)
8747       LTy = CGT.ConvertType(Ty);
8748     LTy = coerceKernelArgumentType(
8749         LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
8750         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
8751   }
8752 
8753   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
8754   // individual elements, which confuses the Clover OpenCL backend; therefore we
8755   // have to set it to false here. Other args of getDirect() are just defaults.
8756   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
8757 }
8758 
8759 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
8760                                                unsigned &NumRegsLeft) const {
8761   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
8762 
8763   Ty = useFirstFieldIfTransparentUnion(Ty);
8764 
8765   if (isAggregateTypeForABI(Ty)) {
8766     // Records with non-trivial destructors/copy-constructors should not be
8767     // passed by value.
8768     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
8769       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8770 
8771     // Ignore empty structs/unions.
8772     if (isEmptyRecord(getContext(), Ty, true))
8773       return ABIArgInfo::getIgnore();
8774 
8775     // Lower single-element structs to just pass a regular value. TODO: We
8776     // could do reasonable-size multiple-element structs too, using getExpand(),
8777     // though watch out for things like bitfields.
8778     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
8779       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
8780 
8781     if (const RecordType *RT = Ty->getAs<RecordType>()) {
8782       const RecordDecl *RD = RT->getDecl();
8783       if (RD->hasFlexibleArrayMember())
8784         return DefaultABIInfo::classifyArgumentType(Ty);
8785     }
8786 
8787     // Pack aggregates <= 8 bytes into single VGPR or pair.
8788     uint64_t Size = getContext().getTypeSize(Ty);
8789     if (Size <= 64) {
8790       unsigned NumRegs = (Size + 31) / 32;
8791       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
8792 
8793       if (Size <= 16)
8794         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
8795 
8796       if (Size <= 32)
8797         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
8798 
8799       // XXX: Should this be i64 instead, and should the limit increase?
8800       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
8801       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
8802     }
8803 
8804     if (NumRegsLeft > 0) {
8805       unsigned NumRegs = numRegsForType(Ty);
8806       if (NumRegsLeft >= NumRegs) {
8807         NumRegsLeft -= NumRegs;
8808         return ABIArgInfo::getDirect();
8809       }
8810     }
8811   }
8812 
8813   // Otherwise just do the default thing.
8814   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
8815   if (!ArgInfo.isIndirect()) {
8816     unsigned NumRegs = numRegsForType(Ty);
8817     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
8818   }
8819 
8820   return ArgInfo;
8821 }
8822 
8823 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
8824 public:
8825   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
8826       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
8827   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8828                            CodeGen::CodeGenModule &M) const override;
8829   unsigned getOpenCLKernelCallingConv() const override;
8830 
8831   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
8832       llvm::PointerType *T, QualType QT) const override;
8833 
8834   LangAS getASTAllocaAddressSpace() const override {
8835     return getLangASFromTargetAS(
8836         getABIInfo().getDataLayout().getAllocaAddrSpace());
8837   }
8838   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8839                                   const VarDecl *D) const override;
8840   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
8841                                          SyncScope Scope,
8842                                          llvm::AtomicOrdering Ordering,
8843                                          llvm::LLVMContext &Ctx) const override;
8844   llvm::Function *
8845   createEnqueuedBlockKernel(CodeGenFunction &CGF,
8846                             llvm::Function *BlockInvokeFunc,
8847                             llvm::Value *BlockLiteral) const override;
8848   bool shouldEmitStaticExternCAliases() const override;
8849   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
8850 };
8851 }
8852 
8853 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
8854                                               llvm::GlobalValue *GV) {
8855   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
8856     return false;
8857 
8858   return D->hasAttr<OpenCLKernelAttr>() ||
8859          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
8860          (isa<VarDecl>(D) &&
8861           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
8862            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
8863            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
8864 }
8865 
8866 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
8867     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8868   if (requiresAMDGPUProtectedVisibility(D, GV)) {
8869     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
8870     GV->setDSOLocal(true);
8871   }
8872 
8873   if (GV->isDeclaration())
8874     return;
8875   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8876   if (!FD)
8877     return;
8878 
8879   llvm::Function *F = cast<llvm::Function>(GV);
8880 
8881   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
8882     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
8883 
8884 
8885   const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
8886                               FD->hasAttr<OpenCLKernelAttr>();
8887   const bool IsHIPKernel = M.getLangOpts().HIP &&
8888                            FD->hasAttr<CUDAGlobalAttr>();
8889   if ((IsOpenCLKernel || IsHIPKernel) &&
8890       (M.getTriple().getOS() == llvm::Triple::AMDHSA))
8891     F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
8892 
8893   if (IsHIPKernel)
8894     F->addFnAttr("uniform-work-group-size", "true");
8895 
8896 
8897   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
8898   if (ReqdWGS || FlatWGS) {
8899     unsigned Min = 0;
8900     unsigned Max = 0;
8901     if (FlatWGS) {
8902       Min = FlatWGS->getMin()
8903                 ->EvaluateKnownConstInt(M.getContext())
8904                 .getExtValue();
8905       Max = FlatWGS->getMax()
8906                 ->EvaluateKnownConstInt(M.getContext())
8907                 .getExtValue();
8908     }
8909     if (ReqdWGS && Min == 0 && Max == 0)
8910       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
8911 
8912     if (Min != 0) {
8913       assert(Min <= Max && "Min must be less than or equal Max");
8914 
8915       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
8916       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
8917     } else
8918       assert(Max == 0 && "Max must be zero");
8919   } else if (IsOpenCLKernel || IsHIPKernel) {
8920     // By default, restrict the maximum size to a value specified by
8921     // --gpu-max-threads-per-block=n or its default value for HIP.
8922     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
8923     const unsigned DefaultMaxWorkGroupSize =
8924         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
8925                        : M.getLangOpts().GPUMaxThreadsPerBlock;
8926     std::string AttrVal =
8927         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
8928     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
8929   }
8930 
8931   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
8932     unsigned Min =
8933         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
8934     unsigned Max = Attr->getMax() ? Attr->getMax()
8935                                         ->EvaluateKnownConstInt(M.getContext())
8936                                         .getExtValue()
8937                                   : 0;
8938 
8939     if (Min != 0) {
8940       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
8941 
8942       std::string AttrVal = llvm::utostr(Min);
8943       if (Max != 0)
8944         AttrVal = AttrVal + "," + llvm::utostr(Max);
8945       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
8946     } else
8947       assert(Max == 0 && "Max must be zero");
8948   }
8949 
8950   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
8951     unsigned NumSGPR = Attr->getNumSGPR();
8952 
8953     if (NumSGPR != 0)
8954       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
8955   }
8956 
8957   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
8958     uint32_t NumVGPR = Attr->getNumVGPR();
8959 
8960     if (NumVGPR != 0)
8961       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
8962   }
8963 }
8964 
8965 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8966   return llvm::CallingConv::AMDGPU_KERNEL;
8967 }
8968 
8969 // Currently LLVM assumes null pointers always have value 0,
8970 // which results in incorrectly transformed IR. Therefore, instead of
8971 // emitting null pointers in private and local address spaces, a null
8972 // pointer in generic address space is emitted which is casted to a
8973 // pointer in local or private address space.
8974 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
8975     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
8976     QualType QT) const {
8977   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
8978     return llvm::ConstantPointerNull::get(PT);
8979 
8980   auto &Ctx = CGM.getContext();
8981   auto NPT = llvm::PointerType::get(PT->getElementType(),
8982       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
8983   return llvm::ConstantExpr::getAddrSpaceCast(
8984       llvm::ConstantPointerNull::get(NPT), PT);
8985 }
8986 
8987 LangAS
8988 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
8989                                                   const VarDecl *D) const {
8990   assert(!CGM.getLangOpts().OpenCL &&
8991          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
8992          "Address space agnostic languages only");
8993   LangAS DefaultGlobalAS = getLangASFromTargetAS(
8994       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
8995   if (!D)
8996     return DefaultGlobalAS;
8997 
8998   LangAS AddrSpace = D->getType().getAddressSpace();
8999   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9000   if (AddrSpace != LangAS::Default)
9001     return AddrSpace;
9002 
9003   if (CGM.isTypeConstant(D->getType(), false)) {
9004     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9005       return ConstAS.getValue();
9006   }
9007   return DefaultGlobalAS;
9008 }
9009 
9010 llvm::SyncScope::ID
9011 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9012                                             SyncScope Scope,
9013                                             llvm::AtomicOrdering Ordering,
9014                                             llvm::LLVMContext &Ctx) const {
9015   std::string Name;
9016   switch (Scope) {
9017   case SyncScope::OpenCLWorkGroup:
9018     Name = "workgroup";
9019     break;
9020   case SyncScope::OpenCLDevice:
9021     Name = "agent";
9022     break;
9023   case SyncScope::OpenCLAllSVMDevices:
9024     Name = "";
9025     break;
9026   case SyncScope::OpenCLSubGroup:
9027     Name = "wavefront";
9028   }
9029 
9030   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9031     if (!Name.empty())
9032       Name = Twine(Twine(Name) + Twine("-")).str();
9033 
9034     Name = Twine(Twine(Name) + Twine("one-as")).str();
9035   }
9036 
9037   return Ctx.getOrInsertSyncScopeID(Name);
9038 }
9039 
9040 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9041   return false;
9042 }
9043 
9044 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9045     const FunctionType *&FT) const {
9046   FT = getABIInfo().getContext().adjustFunctionType(
9047       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9048 }
9049 
9050 //===----------------------------------------------------------------------===//
9051 // SPARC v8 ABI Implementation.
9052 // Based on the SPARC Compliance Definition version 2.4.1.
9053 //
9054 // Ensures that complex values are passed in registers.
9055 //
9056 namespace {
9057 class SparcV8ABIInfo : public DefaultABIInfo {
9058 public:
9059   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9060 
9061 private:
9062   ABIArgInfo classifyReturnType(QualType RetTy) const;
9063   void computeInfo(CGFunctionInfo &FI) const override;
9064 };
9065 } // end anonymous namespace
9066 
9067 
9068 ABIArgInfo
9069 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9070   if (Ty->isAnyComplexType()) {
9071     return ABIArgInfo::getDirect();
9072   }
9073   else {
9074     return DefaultABIInfo::classifyReturnType(Ty);
9075   }
9076 }
9077 
9078 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9079 
9080   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9081   for (auto &Arg : FI.arguments())
9082     Arg.info = classifyArgumentType(Arg.type);
9083 }
9084 
9085 namespace {
9086 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9087 public:
9088   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9089       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9090 };
9091 } // end anonymous namespace
9092 
9093 //===----------------------------------------------------------------------===//
9094 // SPARC v9 ABI Implementation.
9095 // Based on the SPARC Compliance Definition version 2.4.1.
9096 //
9097 // Function arguments a mapped to a nominal "parameter array" and promoted to
9098 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9099 // the array, structs larger than 16 bytes are passed indirectly.
9100 //
9101 // One case requires special care:
9102 //
9103 //   struct mixed {
9104 //     int i;
9105 //     float f;
9106 //   };
9107 //
9108 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9109 // parameter array, but the int is passed in an integer register, and the float
9110 // is passed in a floating point register. This is represented as two arguments
9111 // with the LLVM IR inreg attribute:
9112 //
9113 //   declare void f(i32 inreg %i, float inreg %f)
9114 //
9115 // The code generator will only allocate 4 bytes from the parameter array for
9116 // the inreg arguments. All other arguments are allocated a multiple of 8
9117 // bytes.
9118 //
9119 namespace {
9120 class SparcV9ABIInfo : public ABIInfo {
9121 public:
9122   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9123 
9124 private:
9125   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9126   void computeInfo(CGFunctionInfo &FI) const override;
9127   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9128                     QualType Ty) const override;
9129 
9130   // Coercion type builder for structs passed in registers. The coercion type
9131   // serves two purposes:
9132   //
9133   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9134   //    in registers.
9135   // 2. Expose aligned floating point elements as first-level elements, so the
9136   //    code generator knows to pass them in floating point registers.
9137   //
9138   // We also compute the InReg flag which indicates that the struct contains
9139   // aligned 32-bit floats.
9140   //
9141   struct CoerceBuilder {
9142     llvm::LLVMContext &Context;
9143     const llvm::DataLayout &DL;
9144     SmallVector<llvm::Type*, 8> Elems;
9145     uint64_t Size;
9146     bool InReg;
9147 
9148     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9149       : Context(c), DL(dl), Size(0), InReg(false) {}
9150 
9151     // Pad Elems with integers until Size is ToSize.
9152     void pad(uint64_t ToSize) {
9153       assert(ToSize >= Size && "Cannot remove elements");
9154       if (ToSize == Size)
9155         return;
9156 
9157       // Finish the current 64-bit word.
9158       uint64_t Aligned = llvm::alignTo(Size, 64);
9159       if (Aligned > Size && Aligned <= ToSize) {
9160         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9161         Size = Aligned;
9162       }
9163 
9164       // Add whole 64-bit words.
9165       while (Size + 64 <= ToSize) {
9166         Elems.push_back(llvm::Type::getInt64Ty(Context));
9167         Size += 64;
9168       }
9169 
9170       // Final in-word padding.
9171       if (Size < ToSize) {
9172         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9173         Size = ToSize;
9174       }
9175     }
9176 
9177     // Add a floating point element at Offset.
9178     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9179       // Unaligned floats are treated as integers.
9180       if (Offset % Bits)
9181         return;
9182       // The InReg flag is only required if there are any floats < 64 bits.
9183       if (Bits < 64)
9184         InReg = true;
9185       pad(Offset);
9186       Elems.push_back(Ty);
9187       Size = Offset + Bits;
9188     }
9189 
9190     // Add a struct type to the coercion type, starting at Offset (in bits).
9191     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9192       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9193       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9194         llvm::Type *ElemTy = StrTy->getElementType(i);
9195         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9196         switch (ElemTy->getTypeID()) {
9197         case llvm::Type::StructTyID:
9198           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9199           break;
9200         case llvm::Type::FloatTyID:
9201           addFloat(ElemOffset, ElemTy, 32);
9202           break;
9203         case llvm::Type::DoubleTyID:
9204           addFloat(ElemOffset, ElemTy, 64);
9205           break;
9206         case llvm::Type::FP128TyID:
9207           addFloat(ElemOffset, ElemTy, 128);
9208           break;
9209         case llvm::Type::PointerTyID:
9210           if (ElemOffset % 64 == 0) {
9211             pad(ElemOffset);
9212             Elems.push_back(ElemTy);
9213             Size += 64;
9214           }
9215           break;
9216         default:
9217           break;
9218         }
9219       }
9220     }
9221 
9222     // Check if Ty is a usable substitute for the coercion type.
9223     bool isUsableType(llvm::StructType *Ty) const {
9224       return llvm::makeArrayRef(Elems) == Ty->elements();
9225     }
9226 
9227     // Get the coercion type as a literal struct type.
9228     llvm::Type *getType() const {
9229       if (Elems.size() == 1)
9230         return Elems.front();
9231       else
9232         return llvm::StructType::get(Context, Elems);
9233     }
9234   };
9235 };
9236 } // end anonymous namespace
9237 
9238 ABIArgInfo
9239 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9240   if (Ty->isVoidType())
9241     return ABIArgInfo::getIgnore();
9242 
9243   uint64_t Size = getContext().getTypeSize(Ty);
9244 
9245   // Anything too big to fit in registers is passed with an explicit indirect
9246   // pointer / sret pointer.
9247   if (Size > SizeLimit)
9248     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9249 
9250   // Treat an enum type as its underlying type.
9251   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9252     Ty = EnumTy->getDecl()->getIntegerType();
9253 
9254   // Integer types smaller than a register are extended.
9255   if (Size < 64 && Ty->isIntegerType())
9256     return ABIArgInfo::getExtend(Ty);
9257 
9258   if (const auto *EIT = Ty->getAs<ExtIntType>())
9259     if (EIT->getNumBits() < 64)
9260       return ABIArgInfo::getExtend(Ty);
9261 
9262   // Other non-aggregates go in registers.
9263   if (!isAggregateTypeForABI(Ty))
9264     return ABIArgInfo::getDirect();
9265 
9266   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9267   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9268   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9269     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9270 
9271   // This is a small aggregate type that should be passed in registers.
9272   // Build a coercion type from the LLVM struct type.
9273   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9274   if (!StrTy)
9275     return ABIArgInfo::getDirect();
9276 
9277   CoerceBuilder CB(getVMContext(), getDataLayout());
9278   CB.addStruct(0, StrTy);
9279   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9280 
9281   // Try to use the original type for coercion.
9282   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9283 
9284   if (CB.InReg)
9285     return ABIArgInfo::getDirectInReg(CoerceTy);
9286   else
9287     return ABIArgInfo::getDirect(CoerceTy);
9288 }
9289 
9290 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9291                                   QualType Ty) const {
9292   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9293   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9294   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9295     AI.setCoerceToType(ArgTy);
9296 
9297   CharUnits SlotSize = CharUnits::fromQuantity(8);
9298 
9299   CGBuilderTy &Builder = CGF.Builder;
9300   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
9301   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9302 
9303   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9304 
9305   Address ArgAddr = Address::invalid();
9306   CharUnits Stride;
9307   switch (AI.getKind()) {
9308   case ABIArgInfo::Expand:
9309   case ABIArgInfo::CoerceAndExpand:
9310   case ABIArgInfo::InAlloca:
9311     llvm_unreachable("Unsupported ABI kind for va_arg");
9312 
9313   case ABIArgInfo::Extend: {
9314     Stride = SlotSize;
9315     CharUnits Offset = SlotSize - TypeInfo.first;
9316     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9317     break;
9318   }
9319 
9320   case ABIArgInfo::Direct: {
9321     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9322     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9323     ArgAddr = Addr;
9324     break;
9325   }
9326 
9327   case ABIArgInfo::Indirect:
9328     Stride = SlotSize;
9329     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9330     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
9331                       TypeInfo.second);
9332     break;
9333 
9334   case ABIArgInfo::Ignore:
9335     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
9336   }
9337 
9338   // Update VAList.
9339   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9340   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9341 
9342   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
9343 }
9344 
9345 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9346   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9347   for (auto &I : FI.arguments())
9348     I.info = classifyType(I.type, 16 * 8);
9349 }
9350 
9351 namespace {
9352 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9353 public:
9354   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9355       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9356 
9357   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9358     return 14;
9359   }
9360 
9361   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9362                                llvm::Value *Address) const override;
9363 };
9364 } // end anonymous namespace
9365 
9366 bool
9367 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9368                                                 llvm::Value *Address) const {
9369   // This is calculated from the LLVM and GCC tables and verified
9370   // against gcc output.  AFAIK all ABIs use the same encoding.
9371 
9372   CodeGen::CGBuilderTy &Builder = CGF.Builder;
9373 
9374   llvm::IntegerType *i8 = CGF.Int8Ty;
9375   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
9376   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
9377 
9378   // 0-31: the 8-byte general-purpose registers
9379   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
9380 
9381   // 32-63: f0-31, the 4-byte floating-point registers
9382   AssignToArrayRange(Builder, Address, Four8, 32, 63);
9383 
9384   //   Y   = 64
9385   //   PSR = 65
9386   //   WIM = 66
9387   //   TBR = 67
9388   //   PC  = 68
9389   //   NPC = 69
9390   //   FSR = 70
9391   //   CSR = 71
9392   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
9393 
9394   // 72-87: d0-15, the 8-byte floating-point registers
9395   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
9396 
9397   return false;
9398 }
9399 
9400 // ARC ABI implementation.
9401 namespace {
9402 
9403 class ARCABIInfo : public DefaultABIInfo {
9404 public:
9405   using DefaultABIInfo::DefaultABIInfo;
9406 
9407 private:
9408   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9409                     QualType Ty) const override;
9410 
9411   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
9412     if (!State.FreeRegs)
9413       return;
9414     if (Info.isIndirect() && Info.getInReg())
9415       State.FreeRegs--;
9416     else if (Info.isDirect() && Info.getInReg()) {
9417       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
9418       if (sz < State.FreeRegs)
9419         State.FreeRegs -= sz;
9420       else
9421         State.FreeRegs = 0;
9422     }
9423   }
9424 
9425   void computeInfo(CGFunctionInfo &FI) const override {
9426     CCState State(FI);
9427     // ARC uses 8 registers to pass arguments.
9428     State.FreeRegs = 8;
9429 
9430     if (!getCXXABI().classifyReturnType(FI))
9431       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9432     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
9433     for (auto &I : FI.arguments()) {
9434       I.info = classifyArgumentType(I.type, State.FreeRegs);
9435       updateState(I.info, I.type, State);
9436     }
9437   }
9438 
9439   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
9440   ABIArgInfo getIndirectByValue(QualType Ty) const;
9441   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
9442   ABIArgInfo classifyReturnType(QualType RetTy) const;
9443 };
9444 
9445 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
9446 public:
9447   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
9448       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
9449 };
9450 
9451 
9452 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
9453   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
9454                        getNaturalAlignIndirect(Ty, false);
9455 }
9456 
9457 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
9458   // Compute the byval alignment.
9459   const unsigned MinABIStackAlignInBytes = 4;
9460   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9461   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9462                                  TypeAlign > MinABIStackAlignInBytes);
9463 }
9464 
9465 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9466                               QualType Ty) const {
9467   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
9468                           getContext().getTypeInfoInChars(Ty),
9469                           CharUnits::fromQuantity(4), true);
9470 }
9471 
9472 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
9473                                             uint8_t FreeRegs) const {
9474   // Handle the generic C++ ABI.
9475   const RecordType *RT = Ty->getAs<RecordType>();
9476   if (RT) {
9477     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
9478     if (RAA == CGCXXABI::RAA_Indirect)
9479       return getIndirectByRef(Ty, FreeRegs > 0);
9480 
9481     if (RAA == CGCXXABI::RAA_DirectInMemory)
9482       return getIndirectByValue(Ty);
9483   }
9484 
9485   // Treat an enum type as its underlying type.
9486   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9487     Ty = EnumTy->getDecl()->getIntegerType();
9488 
9489   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
9490 
9491   if (isAggregateTypeForABI(Ty)) {
9492     // Structures with flexible arrays are always indirect.
9493     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9494       return getIndirectByValue(Ty);
9495 
9496     // Ignore empty structs/unions.
9497     if (isEmptyRecord(getContext(), Ty, true))
9498       return ABIArgInfo::getIgnore();
9499 
9500     llvm::LLVMContext &LLVMContext = getVMContext();
9501 
9502     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9503     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9504     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9505 
9506     return FreeRegs >= SizeInRegs ?
9507         ABIArgInfo::getDirectInReg(Result) :
9508         ABIArgInfo::getDirect(Result, 0, nullptr, false);
9509   }
9510 
9511   if (const auto *EIT = Ty->getAs<ExtIntType>())
9512     if (EIT->getNumBits() > 64)
9513       return getIndirectByValue(Ty);
9514 
9515   return isPromotableIntegerTypeForABI(Ty)
9516              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
9517                                        : ABIArgInfo::getExtend(Ty))
9518              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
9519                                        : ABIArgInfo::getDirect());
9520 }
9521 
9522 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
9523   if (RetTy->isAnyComplexType())
9524     return ABIArgInfo::getDirectInReg();
9525 
9526   // Arguments of size > 4 registers are indirect.
9527   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
9528   if (RetSize > 4)
9529     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
9530 
9531   return DefaultABIInfo::classifyReturnType(RetTy);
9532 }
9533 
9534 } // End anonymous namespace.
9535 
9536 //===----------------------------------------------------------------------===//
9537 // XCore ABI Implementation
9538 //===----------------------------------------------------------------------===//
9539 
9540 namespace {
9541 
9542 /// A SmallStringEnc instance is used to build up the TypeString by passing
9543 /// it by reference between functions that append to it.
9544 typedef llvm::SmallString<128> SmallStringEnc;
9545 
9546 /// TypeStringCache caches the meta encodings of Types.
9547 ///
9548 /// The reason for caching TypeStrings is two fold:
9549 ///   1. To cache a type's encoding for later uses;
9550 ///   2. As a means to break recursive member type inclusion.
9551 ///
9552 /// A cache Entry can have a Status of:
9553 ///   NonRecursive:   The type encoding is not recursive;
9554 ///   Recursive:      The type encoding is recursive;
9555 ///   Incomplete:     An incomplete TypeString;
9556 ///   IncompleteUsed: An incomplete TypeString that has been used in a
9557 ///                   Recursive type encoding.
9558 ///
9559 /// A NonRecursive entry will have all of its sub-members expanded as fully
9560 /// as possible. Whilst it may contain types which are recursive, the type
9561 /// itself is not recursive and thus its encoding may be safely used whenever
9562 /// the type is encountered.
9563 ///
9564 /// A Recursive entry will have all of its sub-members expanded as fully as
9565 /// possible. The type itself is recursive and it may contain other types which
9566 /// are recursive. The Recursive encoding must not be used during the expansion
9567 /// of a recursive type's recursive branch. For simplicity the code uses
9568 /// IncompleteCount to reject all usage of Recursive encodings for member types.
9569 ///
9570 /// An Incomplete entry is always a RecordType and only encodes its
9571 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
9572 /// are placed into the cache during type expansion as a means to identify and
9573 /// handle recursive inclusion of types as sub-members. If there is recursion
9574 /// the entry becomes IncompleteUsed.
9575 ///
9576 /// During the expansion of a RecordType's members:
9577 ///
9578 ///   If the cache contains a NonRecursive encoding for the member type, the
9579 ///   cached encoding is used;
9580 ///
9581 ///   If the cache contains a Recursive encoding for the member type, the
9582 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
9583 ///
9584 ///   If the member is a RecordType, an Incomplete encoding is placed into the
9585 ///   cache to break potential recursive inclusion of itself as a sub-member;
9586 ///
9587 ///   Once a member RecordType has been expanded, its temporary incomplete
9588 ///   entry is removed from the cache. If a Recursive encoding was swapped out
9589 ///   it is swapped back in;
9590 ///
9591 ///   If an incomplete entry is used to expand a sub-member, the incomplete
9592 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
9593 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
9594 ///
9595 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
9596 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
9597 ///   Else the member is part of a recursive type and thus the recursion has
9598 ///   been exited too soon for the encoding to be correct for the member.
9599 ///
9600 class TypeStringCache {
9601   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
9602   struct Entry {
9603     std::string Str;     // The encoded TypeString for the type.
9604     enum Status State;   // Information about the encoding in 'Str'.
9605     std::string Swapped; // A temporary place holder for a Recursive encoding
9606                          // during the expansion of RecordType's members.
9607   };
9608   std::map<const IdentifierInfo *, struct Entry> Map;
9609   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
9610   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
9611 public:
9612   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
9613   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
9614   bool removeIncomplete(const IdentifierInfo *ID);
9615   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
9616                      bool IsRecursive);
9617   StringRef lookupStr(const IdentifierInfo *ID);
9618 };
9619 
9620 /// TypeString encodings for enum & union fields must be order.
9621 /// FieldEncoding is a helper for this ordering process.
9622 class FieldEncoding {
9623   bool HasName;
9624   std::string Enc;
9625 public:
9626   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
9627   StringRef str() { return Enc; }
9628   bool operator<(const FieldEncoding &rhs) const {
9629     if (HasName != rhs.HasName) return HasName;
9630     return Enc < rhs.Enc;
9631   }
9632 };
9633 
9634 class XCoreABIInfo : public DefaultABIInfo {
9635 public:
9636   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9637   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9638                     QualType Ty) const override;
9639 };
9640 
9641 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
9642   mutable TypeStringCache TSC;
9643   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
9644                     const CodeGen::CodeGenModule &M) const;
9645 
9646 public:
9647   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
9648       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
9649   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
9650                           const llvm::MapVector<GlobalDecl, StringRef>
9651                               &MangledDeclNames) const override;
9652 };
9653 
9654 } // End anonymous namespace.
9655 
9656 // TODO: this implementation is likely now redundant with the default
9657 // EmitVAArg.
9658 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9659                                 QualType Ty) const {
9660   CGBuilderTy &Builder = CGF.Builder;
9661 
9662   // Get the VAList.
9663   CharUnits SlotSize = CharUnits::fromQuantity(4);
9664   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
9665 
9666   // Handle the argument.
9667   ABIArgInfo AI = classifyArgumentType(Ty);
9668   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
9669   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9670   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9671     AI.setCoerceToType(ArgTy);
9672   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9673 
9674   Address Val = Address::invalid();
9675   CharUnits ArgSize = CharUnits::Zero();
9676   switch (AI.getKind()) {
9677   case ABIArgInfo::Expand:
9678   case ABIArgInfo::CoerceAndExpand:
9679   case ABIArgInfo::InAlloca:
9680     llvm_unreachable("Unsupported ABI kind for va_arg");
9681   case ABIArgInfo::Ignore:
9682     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
9683     ArgSize = CharUnits::Zero();
9684     break;
9685   case ABIArgInfo::Extend:
9686   case ABIArgInfo::Direct:
9687     Val = Builder.CreateBitCast(AP, ArgPtrTy);
9688     ArgSize = CharUnits::fromQuantity(
9689                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
9690     ArgSize = ArgSize.alignTo(SlotSize);
9691     break;
9692   case ABIArgInfo::Indirect:
9693     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
9694     Val = Address(Builder.CreateLoad(Val), TypeAlign);
9695     ArgSize = SlotSize;
9696     break;
9697   }
9698 
9699   // Increment the VAList.
9700   if (!ArgSize.isZero()) {
9701     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
9702     Builder.CreateStore(APN.getPointer(), VAListAddr);
9703   }
9704 
9705   return Val;
9706 }
9707 
9708 /// During the expansion of a RecordType, an incomplete TypeString is placed
9709 /// into the cache as a means to identify and break recursion.
9710 /// If there is a Recursive encoding in the cache, it is swapped out and will
9711 /// be reinserted by removeIncomplete().
9712 /// All other types of encoding should have been used rather than arriving here.
9713 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
9714                                     std::string StubEnc) {
9715   if (!ID)
9716     return;
9717   Entry &E = Map[ID];
9718   assert( (E.Str.empty() || E.State == Recursive) &&
9719          "Incorrectly use of addIncomplete");
9720   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
9721   E.Swapped.swap(E.Str); // swap out the Recursive
9722   E.Str.swap(StubEnc);
9723   E.State = Incomplete;
9724   ++IncompleteCount;
9725 }
9726 
9727 /// Once the RecordType has been expanded, the temporary incomplete TypeString
9728 /// must be removed from the cache.
9729 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
9730 /// Returns true if the RecordType was defined recursively.
9731 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
9732   if (!ID)
9733     return false;
9734   auto I = Map.find(ID);
9735   assert(I != Map.end() && "Entry not present");
9736   Entry &E = I->second;
9737   assert( (E.State == Incomplete ||
9738            E.State == IncompleteUsed) &&
9739          "Entry must be an incomplete type");
9740   bool IsRecursive = false;
9741   if (E.State == IncompleteUsed) {
9742     // We made use of our Incomplete encoding, thus we are recursive.
9743     IsRecursive = true;
9744     --IncompleteUsedCount;
9745   }
9746   if (E.Swapped.empty())
9747     Map.erase(I);
9748   else {
9749     // Swap the Recursive back.
9750     E.Swapped.swap(E.Str);
9751     E.Swapped.clear();
9752     E.State = Recursive;
9753   }
9754   --IncompleteCount;
9755   return IsRecursive;
9756 }
9757 
9758 /// Add the encoded TypeString to the cache only if it is NonRecursive or
9759 /// Recursive (viz: all sub-members were expanded as fully as possible).
9760 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
9761                                     bool IsRecursive) {
9762   if (!ID || IncompleteUsedCount)
9763     return; // No key or it is is an incomplete sub-type so don't add.
9764   Entry &E = Map[ID];
9765   if (IsRecursive && !E.Str.empty()) {
9766     assert(E.State==Recursive && E.Str.size() == Str.size() &&
9767            "This is not the same Recursive entry");
9768     // The parent container was not recursive after all, so we could have used
9769     // this Recursive sub-member entry after all, but we assumed the worse when
9770     // we started viz: IncompleteCount!=0.
9771     return;
9772   }
9773   assert(E.Str.empty() && "Entry already present");
9774   E.Str = Str.str();
9775   E.State = IsRecursive? Recursive : NonRecursive;
9776 }
9777 
9778 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
9779 /// are recursively expanding a type (IncompleteCount != 0) and the cached
9780 /// encoding is Recursive, return an empty StringRef.
9781 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
9782   if (!ID)
9783     return StringRef();   // We have no key.
9784   auto I = Map.find(ID);
9785   if (I == Map.end())
9786     return StringRef();   // We have no encoding.
9787   Entry &E = I->second;
9788   if (E.State == Recursive && IncompleteCount)
9789     return StringRef();   // We don't use Recursive encodings for member types.
9790 
9791   if (E.State == Incomplete) {
9792     // The incomplete type is being used to break out of recursion.
9793     E.State = IncompleteUsed;
9794     ++IncompleteUsedCount;
9795   }
9796   return E.Str;
9797 }
9798 
9799 /// The XCore ABI includes a type information section that communicates symbol
9800 /// type information to the linker. The linker uses this information to verify
9801 /// safety/correctness of things such as array bound and pointers et al.
9802 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
9803 /// This type information (TypeString) is emitted into meta data for all global
9804 /// symbols: definitions, declarations, functions & variables.
9805 ///
9806 /// The TypeString carries type, qualifier, name, size & value details.
9807 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
9808 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
9809 /// The output is tested by test/CodeGen/xcore-stringtype.c.
9810 ///
9811 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
9812                           const CodeGen::CodeGenModule &CGM,
9813                           TypeStringCache &TSC);
9814 
9815 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
9816 void XCoreTargetCodeGenInfo::emitTargetMD(
9817     const Decl *D, llvm::GlobalValue *GV,
9818     const CodeGen::CodeGenModule &CGM) const {
9819   SmallStringEnc Enc;
9820   if (getTypeString(Enc, D, CGM, TSC)) {
9821     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
9822     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
9823                                 llvm::MDString::get(Ctx, Enc.str())};
9824     llvm::NamedMDNode *MD =
9825       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
9826     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
9827   }
9828 }
9829 
9830 void XCoreTargetCodeGenInfo::emitTargetMetadata(
9831     CodeGen::CodeGenModule &CGM,
9832     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
9833   // Warning, new MangledDeclNames may be appended within this loop.
9834   // We rely on MapVector insertions adding new elements to the end
9835   // of the container.
9836   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
9837     auto Val = *(MangledDeclNames.begin() + I);
9838     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
9839     if (GV) {
9840       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
9841       emitTargetMD(D, GV, CGM);
9842     }
9843   }
9844 }
9845 //===----------------------------------------------------------------------===//
9846 // SPIR ABI Implementation
9847 //===----------------------------------------------------------------------===//
9848 
9849 namespace {
9850 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
9851 public:
9852   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
9853       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
9854   unsigned getOpenCLKernelCallingConv() const override;
9855 };
9856 
9857 } // End anonymous namespace.
9858 
9859 namespace clang {
9860 namespace CodeGen {
9861 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
9862   DefaultABIInfo SPIRABI(CGM.getTypes());
9863   SPIRABI.computeInfo(FI);
9864 }
9865 }
9866 }
9867 
9868 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9869   return llvm::CallingConv::SPIR_KERNEL;
9870 }
9871 
9872 static bool appendType(SmallStringEnc &Enc, QualType QType,
9873                        const CodeGen::CodeGenModule &CGM,
9874                        TypeStringCache &TSC);
9875 
9876 /// Helper function for appendRecordType().
9877 /// Builds a SmallVector containing the encoded field types in declaration
9878 /// order.
9879 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
9880                              const RecordDecl *RD,
9881                              const CodeGen::CodeGenModule &CGM,
9882                              TypeStringCache &TSC) {
9883   for (const auto *Field : RD->fields()) {
9884     SmallStringEnc Enc;
9885     Enc += "m(";
9886     Enc += Field->getName();
9887     Enc += "){";
9888     if (Field->isBitField()) {
9889       Enc += "b(";
9890       llvm::raw_svector_ostream OS(Enc);
9891       OS << Field->getBitWidthValue(CGM.getContext());
9892       Enc += ':';
9893     }
9894     if (!appendType(Enc, Field->getType(), CGM, TSC))
9895       return false;
9896     if (Field->isBitField())
9897       Enc += ')';
9898     Enc += '}';
9899     FE.emplace_back(!Field->getName().empty(), Enc);
9900   }
9901   return true;
9902 }
9903 
9904 /// Appends structure and union types to Enc and adds encoding to cache.
9905 /// Recursively calls appendType (via extractFieldType) for each field.
9906 /// Union types have their fields ordered according to the ABI.
9907 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
9908                              const CodeGen::CodeGenModule &CGM,
9909                              TypeStringCache &TSC, const IdentifierInfo *ID) {
9910   // Append the cached TypeString if we have one.
9911   StringRef TypeString = TSC.lookupStr(ID);
9912   if (!TypeString.empty()) {
9913     Enc += TypeString;
9914     return true;
9915   }
9916 
9917   // Start to emit an incomplete TypeString.
9918   size_t Start = Enc.size();
9919   Enc += (RT->isUnionType()? 'u' : 's');
9920   Enc += '(';
9921   if (ID)
9922     Enc += ID->getName();
9923   Enc += "){";
9924 
9925   // We collect all encoded fields and order as necessary.
9926   bool IsRecursive = false;
9927   const RecordDecl *RD = RT->getDecl()->getDefinition();
9928   if (RD && !RD->field_empty()) {
9929     // An incomplete TypeString stub is placed in the cache for this RecordType
9930     // so that recursive calls to this RecordType will use it whilst building a
9931     // complete TypeString for this RecordType.
9932     SmallVector<FieldEncoding, 16> FE;
9933     std::string StubEnc(Enc.substr(Start).str());
9934     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
9935     TSC.addIncomplete(ID, std::move(StubEnc));
9936     if (!extractFieldType(FE, RD, CGM, TSC)) {
9937       (void) TSC.removeIncomplete(ID);
9938       return false;
9939     }
9940     IsRecursive = TSC.removeIncomplete(ID);
9941     // The ABI requires unions to be sorted but not structures.
9942     // See FieldEncoding::operator< for sort algorithm.
9943     if (RT->isUnionType())
9944       llvm::sort(FE);
9945     // We can now complete the TypeString.
9946     unsigned E = FE.size();
9947     for (unsigned I = 0; I != E; ++I) {
9948       if (I)
9949         Enc += ',';
9950       Enc += FE[I].str();
9951     }
9952   }
9953   Enc += '}';
9954   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
9955   return true;
9956 }
9957 
9958 /// Appends enum types to Enc and adds the encoding to the cache.
9959 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
9960                            TypeStringCache &TSC,
9961                            const IdentifierInfo *ID) {
9962   // Append the cached TypeString if we have one.
9963   StringRef TypeString = TSC.lookupStr(ID);
9964   if (!TypeString.empty()) {
9965     Enc += TypeString;
9966     return true;
9967   }
9968 
9969   size_t Start = Enc.size();
9970   Enc += "e(";
9971   if (ID)
9972     Enc += ID->getName();
9973   Enc += "){";
9974 
9975   // We collect all encoded enumerations and order them alphanumerically.
9976   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
9977     SmallVector<FieldEncoding, 16> FE;
9978     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
9979          ++I) {
9980       SmallStringEnc EnumEnc;
9981       EnumEnc += "m(";
9982       EnumEnc += I->getName();
9983       EnumEnc += "){";
9984       I->getInitVal().toString(EnumEnc);
9985       EnumEnc += '}';
9986       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
9987     }
9988     llvm::sort(FE);
9989     unsigned E = FE.size();
9990     for (unsigned I = 0; I != E; ++I) {
9991       if (I)
9992         Enc += ',';
9993       Enc += FE[I].str();
9994     }
9995   }
9996   Enc += '}';
9997   TSC.addIfComplete(ID, Enc.substr(Start), false);
9998   return true;
9999 }
10000 
10001 /// Appends type's qualifier to Enc.
10002 /// This is done prior to appending the type's encoding.
10003 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10004   // Qualifiers are emitted in alphabetical order.
10005   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10006   int Lookup = 0;
10007   if (QT.isConstQualified())
10008     Lookup += 1<<0;
10009   if (QT.isRestrictQualified())
10010     Lookup += 1<<1;
10011   if (QT.isVolatileQualified())
10012     Lookup += 1<<2;
10013   Enc += Table[Lookup];
10014 }
10015 
10016 /// Appends built-in types to Enc.
10017 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10018   const char *EncType;
10019   switch (BT->getKind()) {
10020     case BuiltinType::Void:
10021       EncType = "0";
10022       break;
10023     case BuiltinType::Bool:
10024       EncType = "b";
10025       break;
10026     case BuiltinType::Char_U:
10027       EncType = "uc";
10028       break;
10029     case BuiltinType::UChar:
10030       EncType = "uc";
10031       break;
10032     case BuiltinType::SChar:
10033       EncType = "sc";
10034       break;
10035     case BuiltinType::UShort:
10036       EncType = "us";
10037       break;
10038     case BuiltinType::Short:
10039       EncType = "ss";
10040       break;
10041     case BuiltinType::UInt:
10042       EncType = "ui";
10043       break;
10044     case BuiltinType::Int:
10045       EncType = "si";
10046       break;
10047     case BuiltinType::ULong:
10048       EncType = "ul";
10049       break;
10050     case BuiltinType::Long:
10051       EncType = "sl";
10052       break;
10053     case BuiltinType::ULongLong:
10054       EncType = "ull";
10055       break;
10056     case BuiltinType::LongLong:
10057       EncType = "sll";
10058       break;
10059     case BuiltinType::Float:
10060       EncType = "ft";
10061       break;
10062     case BuiltinType::Double:
10063       EncType = "d";
10064       break;
10065     case BuiltinType::LongDouble:
10066       EncType = "ld";
10067       break;
10068     default:
10069       return false;
10070   }
10071   Enc += EncType;
10072   return true;
10073 }
10074 
10075 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10076 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10077                               const CodeGen::CodeGenModule &CGM,
10078                               TypeStringCache &TSC) {
10079   Enc += "p(";
10080   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10081     return false;
10082   Enc += ')';
10083   return true;
10084 }
10085 
10086 /// Appends array encoding to Enc before calling appendType for the element.
10087 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10088                             const ArrayType *AT,
10089                             const CodeGen::CodeGenModule &CGM,
10090                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10091   if (AT->getSizeModifier() != ArrayType::Normal)
10092     return false;
10093   Enc += "a(";
10094   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10095     CAT->getSize().toStringUnsigned(Enc);
10096   else
10097     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10098   Enc += ':';
10099   // The Qualifiers should be attached to the type rather than the array.
10100   appendQualifier(Enc, QT);
10101   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10102     return false;
10103   Enc += ')';
10104   return true;
10105 }
10106 
10107 /// Appends a function encoding to Enc, calling appendType for the return type
10108 /// and the arguments.
10109 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10110                              const CodeGen::CodeGenModule &CGM,
10111                              TypeStringCache &TSC) {
10112   Enc += "f{";
10113   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10114     return false;
10115   Enc += "}(";
10116   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10117     // N.B. we are only interested in the adjusted param types.
10118     auto I = FPT->param_type_begin();
10119     auto E = FPT->param_type_end();
10120     if (I != E) {
10121       do {
10122         if (!appendType(Enc, *I, CGM, TSC))
10123           return false;
10124         ++I;
10125         if (I != E)
10126           Enc += ',';
10127       } while (I != E);
10128       if (FPT->isVariadic())
10129         Enc += ",va";
10130     } else {
10131       if (FPT->isVariadic())
10132         Enc += "va";
10133       else
10134         Enc += '0';
10135     }
10136   }
10137   Enc += ')';
10138   return true;
10139 }
10140 
10141 /// Handles the type's qualifier before dispatching a call to handle specific
10142 /// type encodings.
10143 static bool appendType(SmallStringEnc &Enc, QualType QType,
10144                        const CodeGen::CodeGenModule &CGM,
10145                        TypeStringCache &TSC) {
10146 
10147   QualType QT = QType.getCanonicalType();
10148 
10149   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10150     // The Qualifiers should be attached to the type rather than the array.
10151     // Thus we don't call appendQualifier() here.
10152     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10153 
10154   appendQualifier(Enc, QT);
10155 
10156   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10157     return appendBuiltinType(Enc, BT);
10158 
10159   if (const PointerType *PT = QT->getAs<PointerType>())
10160     return appendPointerType(Enc, PT, CGM, TSC);
10161 
10162   if (const EnumType *ET = QT->getAs<EnumType>())
10163     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10164 
10165   if (const RecordType *RT = QT->getAsStructureType())
10166     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10167 
10168   if (const RecordType *RT = QT->getAsUnionType())
10169     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10170 
10171   if (const FunctionType *FT = QT->getAs<FunctionType>())
10172     return appendFunctionType(Enc, FT, CGM, TSC);
10173 
10174   return false;
10175 }
10176 
10177 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10178                           const CodeGen::CodeGenModule &CGM,
10179                           TypeStringCache &TSC) {
10180   if (!D)
10181     return false;
10182 
10183   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10184     if (FD->getLanguageLinkage() != CLanguageLinkage)
10185       return false;
10186     return appendType(Enc, FD->getType(), CGM, TSC);
10187   }
10188 
10189   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10190     if (VD->getLanguageLinkage() != CLanguageLinkage)
10191       return false;
10192     QualType QT = VD->getType().getCanonicalType();
10193     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10194       // Global ArrayTypes are given a size of '*' if the size is unknown.
10195       // The Qualifiers should be attached to the type rather than the array.
10196       // Thus we don't call appendQualifier() here.
10197       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10198     }
10199     return appendType(Enc, QT, CGM, TSC);
10200   }
10201   return false;
10202 }
10203 
10204 //===----------------------------------------------------------------------===//
10205 // RISCV ABI Implementation
10206 //===----------------------------------------------------------------------===//
10207 
10208 namespace {
10209 class RISCVABIInfo : public DefaultABIInfo {
10210 private:
10211   // Size of the integer ('x') registers in bits.
10212   unsigned XLen;
10213   // Size of the floating point ('f') registers in bits. Note that the target
10214   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10215   // with soft float ABI has FLen==0).
10216   unsigned FLen;
10217   static const int NumArgGPRs = 8;
10218   static const int NumArgFPRs = 8;
10219   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10220                                       llvm::Type *&Field1Ty,
10221                                       CharUnits &Field1Off,
10222                                       llvm::Type *&Field2Ty,
10223                                       CharUnits &Field2Off) const;
10224 
10225 public:
10226   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10227       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10228 
10229   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10230   // non-virtual, but computeInfo is virtual, so we overload it.
10231   void computeInfo(CGFunctionInfo &FI) const override;
10232 
10233   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10234                                   int &ArgFPRsLeft) const;
10235   ABIArgInfo classifyReturnType(QualType RetTy) const;
10236 
10237   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10238                     QualType Ty) const override;
10239 
10240   ABIArgInfo extendType(QualType Ty) const;
10241 
10242   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10243                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10244                                 CharUnits &Field2Off, int &NeededArgGPRs,
10245                                 int &NeededArgFPRs) const;
10246   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10247                                                CharUnits Field1Off,
10248                                                llvm::Type *Field2Ty,
10249                                                CharUnits Field2Off) const;
10250 };
10251 } // end anonymous namespace
10252 
10253 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10254   QualType RetTy = FI.getReturnType();
10255   if (!getCXXABI().classifyReturnType(FI))
10256     FI.getReturnInfo() = classifyReturnType(RetTy);
10257 
10258   // IsRetIndirect is true if classifyArgumentType indicated the value should
10259   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10260   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10261   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10262   // list and pass indirectly on RV32.
10263   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
10264   if (!IsRetIndirect && RetTy->isScalarType() &&
10265       getContext().getTypeSize(RetTy) > (2 * XLen)) {
10266     if (RetTy->isComplexType() && FLen) {
10267       QualType EltTy = RetTy->getAs<ComplexType>()->getElementType();
10268       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
10269     } else {
10270       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
10271       IsRetIndirect = true;
10272     }
10273   }
10274 
10275   // We must track the number of GPRs used in order to conform to the RISC-V
10276   // ABI, as integer scalars passed in registers should have signext/zeroext
10277   // when promoted, but are anyext if passed on the stack. As GPR usage is
10278   // different for variadic arguments, we must also track whether we are
10279   // examining a vararg or not.
10280   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
10281   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
10282   int NumFixedArgs = FI.getNumRequiredArgs();
10283 
10284   int ArgNum = 0;
10285   for (auto &ArgInfo : FI.arguments()) {
10286     bool IsFixed = ArgNum < NumFixedArgs;
10287     ArgInfo.info =
10288         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
10289     ArgNum++;
10290   }
10291 }
10292 
10293 // Returns true if the struct is a potential candidate for the floating point
10294 // calling convention. If this function returns true, the caller is
10295 // responsible for checking that if there is only a single field then that
10296 // field is a float.
10297 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10298                                                   llvm::Type *&Field1Ty,
10299                                                   CharUnits &Field1Off,
10300                                                   llvm::Type *&Field2Ty,
10301                                                   CharUnits &Field2Off) const {
10302   bool IsInt = Ty->isIntegralOrEnumerationType();
10303   bool IsFloat = Ty->isRealFloatingType();
10304 
10305   if (IsInt || IsFloat) {
10306     uint64_t Size = getContext().getTypeSize(Ty);
10307     if (IsInt && Size > XLen)
10308       return false;
10309     // Can't be eligible if larger than the FP registers. Half precision isn't
10310     // currently supported on RISC-V and the ABI hasn't been confirmed, so
10311     // default to the integer ABI in that case.
10312     if (IsFloat && (Size > FLen || Size < 32))
10313       return false;
10314     // Can't be eligible if an integer type was already found (int+int pairs
10315     // are not eligible).
10316     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
10317       return false;
10318     if (!Field1Ty) {
10319       Field1Ty = CGT.ConvertType(Ty);
10320       Field1Off = CurOff;
10321       return true;
10322     }
10323     if (!Field2Ty) {
10324       Field2Ty = CGT.ConvertType(Ty);
10325       Field2Off = CurOff;
10326       return true;
10327     }
10328     return false;
10329   }
10330 
10331   if (auto CTy = Ty->getAs<ComplexType>()) {
10332     if (Field1Ty)
10333       return false;
10334     QualType EltTy = CTy->getElementType();
10335     if (getContext().getTypeSize(EltTy) > FLen)
10336       return false;
10337     Field1Ty = CGT.ConvertType(EltTy);
10338     Field1Off = CurOff;
10339     assert(CurOff.isZero() && "Unexpected offset for first field");
10340     Field2Ty = Field1Ty;
10341     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
10342     return true;
10343   }
10344 
10345   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
10346     uint64_t ArraySize = ATy->getSize().getZExtValue();
10347     QualType EltTy = ATy->getElementType();
10348     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
10349     for (uint64_t i = 0; i < ArraySize; ++i) {
10350       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
10351                                                 Field1Off, Field2Ty, Field2Off);
10352       if (!Ret)
10353         return false;
10354       CurOff += EltSize;
10355     }
10356     return true;
10357   }
10358 
10359   if (const auto *RTy = Ty->getAs<RecordType>()) {
10360     // Structures with either a non-trivial destructor or a non-trivial
10361     // copy constructor are not eligible for the FP calling convention.
10362     if (getRecordArgABI(Ty, CGT.getCXXABI()))
10363       return false;
10364     if (isEmptyRecord(getContext(), Ty, true))
10365       return true;
10366     const RecordDecl *RD = RTy->getDecl();
10367     // Unions aren't eligible unless they're empty (which is caught above).
10368     if (RD->isUnion())
10369       return false;
10370     int ZeroWidthBitFieldCount = 0;
10371     for (const FieldDecl *FD : RD->fields()) {
10372       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
10373       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
10374       QualType QTy = FD->getType();
10375       if (FD->isBitField()) {
10376         unsigned BitWidth = FD->getBitWidthValue(getContext());
10377         // Allow a bitfield with a type greater than XLen as long as the
10378         // bitwidth is XLen or less.
10379         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
10380           QTy = getContext().getIntTypeForBitwidth(XLen, false);
10381         if (BitWidth == 0) {
10382           ZeroWidthBitFieldCount++;
10383           continue;
10384         }
10385       }
10386 
10387       bool Ret = detectFPCCEligibleStructHelper(
10388           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
10389           Field1Ty, Field1Off, Field2Ty, Field2Off);
10390       if (!Ret)
10391         return false;
10392 
10393       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
10394       // or int+fp structs, but are ignored for a struct with an fp field and
10395       // any number of zero-width bitfields.
10396       if (Field2Ty && ZeroWidthBitFieldCount > 0)
10397         return false;
10398     }
10399     return Field1Ty != nullptr;
10400   }
10401 
10402   return false;
10403 }
10404 
10405 // Determine if a struct is eligible for passing according to the floating
10406 // point calling convention (i.e., when flattened it contains a single fp
10407 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
10408 // NeededArgGPRs are incremented appropriately.
10409 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10410                                             CharUnits &Field1Off,
10411                                             llvm::Type *&Field2Ty,
10412                                             CharUnits &Field2Off,
10413                                             int &NeededArgGPRs,
10414                                             int &NeededArgFPRs) const {
10415   Field1Ty = nullptr;
10416   Field2Ty = nullptr;
10417   NeededArgGPRs = 0;
10418   NeededArgFPRs = 0;
10419   bool IsCandidate = detectFPCCEligibleStructHelper(
10420       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
10421   // Not really a candidate if we have a single int but no float.
10422   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
10423     return false;
10424   if (!IsCandidate)
10425     return false;
10426   if (Field1Ty && Field1Ty->isFloatingPointTy())
10427     NeededArgFPRs++;
10428   else if (Field1Ty)
10429     NeededArgGPRs++;
10430   if (Field2Ty && Field2Ty->isFloatingPointTy())
10431     NeededArgFPRs++;
10432   else if (Field2Ty)
10433     NeededArgGPRs++;
10434   return IsCandidate;
10435 }
10436 
10437 // Call getCoerceAndExpand for the two-element flattened struct described by
10438 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
10439 // appropriate coerceToType and unpaddedCoerceToType.
10440 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
10441     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
10442     CharUnits Field2Off) const {
10443   SmallVector<llvm::Type *, 3> CoerceElts;
10444   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
10445   if (!Field1Off.isZero())
10446     CoerceElts.push_back(llvm::ArrayType::get(
10447         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
10448 
10449   CoerceElts.push_back(Field1Ty);
10450   UnpaddedCoerceElts.push_back(Field1Ty);
10451 
10452   if (!Field2Ty) {
10453     return ABIArgInfo::getCoerceAndExpand(
10454         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
10455         UnpaddedCoerceElts[0]);
10456   }
10457 
10458   CharUnits Field2Align =
10459       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
10460   CharUnits Field1Size =
10461       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
10462   CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align);
10463 
10464   CharUnits Padding = CharUnits::Zero();
10465   if (Field2Off > Field2OffNoPadNoPack)
10466     Padding = Field2Off - Field2OffNoPadNoPack;
10467   else if (Field2Off != Field2Align && Field2Off > Field1Size)
10468     Padding = Field2Off - Field1Size;
10469 
10470   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
10471 
10472   if (!Padding.isZero())
10473     CoerceElts.push_back(llvm::ArrayType::get(
10474         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
10475 
10476   CoerceElts.push_back(Field2Ty);
10477   UnpaddedCoerceElts.push_back(Field2Ty);
10478 
10479   auto CoerceToType =
10480       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
10481   auto UnpaddedCoerceToType =
10482       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
10483 
10484   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
10485 }
10486 
10487 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
10488                                               int &ArgGPRsLeft,
10489                                               int &ArgFPRsLeft) const {
10490   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
10491   Ty = useFirstFieldIfTransparentUnion(Ty);
10492 
10493   // Structures with either a non-trivial destructor or a non-trivial
10494   // copy constructor are always passed indirectly.
10495   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
10496     if (ArgGPRsLeft)
10497       ArgGPRsLeft -= 1;
10498     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
10499                                            CGCXXABI::RAA_DirectInMemory);
10500   }
10501 
10502   // Ignore empty structs/unions.
10503   if (isEmptyRecord(getContext(), Ty, true))
10504     return ABIArgInfo::getIgnore();
10505 
10506   uint64_t Size = getContext().getTypeSize(Ty);
10507 
10508   // Pass floating point values via FPRs if possible.
10509   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
10510       FLen >= Size && ArgFPRsLeft) {
10511     ArgFPRsLeft--;
10512     return ABIArgInfo::getDirect();
10513   }
10514 
10515   // Complex types for the hard float ABI must be passed direct rather than
10516   // using CoerceAndExpand.
10517   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
10518     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
10519     if (getContext().getTypeSize(EltTy) <= FLen) {
10520       ArgFPRsLeft -= 2;
10521       return ABIArgInfo::getDirect();
10522     }
10523   }
10524 
10525   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
10526     llvm::Type *Field1Ty = nullptr;
10527     llvm::Type *Field2Ty = nullptr;
10528     CharUnits Field1Off = CharUnits::Zero();
10529     CharUnits Field2Off = CharUnits::Zero();
10530     int NeededArgGPRs;
10531     int NeededArgFPRs;
10532     bool IsCandidate =
10533         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
10534                                  NeededArgGPRs, NeededArgFPRs);
10535     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
10536         NeededArgFPRs <= ArgFPRsLeft) {
10537       ArgGPRsLeft -= NeededArgGPRs;
10538       ArgFPRsLeft -= NeededArgFPRs;
10539       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
10540                                                Field2Off);
10541     }
10542   }
10543 
10544   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
10545   bool MustUseStack = false;
10546   // Determine the number of GPRs needed to pass the current argument
10547   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
10548   // register pairs, so may consume 3 registers.
10549   int NeededArgGPRs = 1;
10550   if (!IsFixed && NeededAlign == 2 * XLen)
10551     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
10552   else if (Size > XLen && Size <= 2 * XLen)
10553     NeededArgGPRs = 2;
10554 
10555   if (NeededArgGPRs > ArgGPRsLeft) {
10556     MustUseStack = true;
10557     NeededArgGPRs = ArgGPRsLeft;
10558   }
10559 
10560   ArgGPRsLeft -= NeededArgGPRs;
10561 
10562   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
10563     // Treat an enum type as its underlying type.
10564     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10565       Ty = EnumTy->getDecl()->getIntegerType();
10566 
10567     // All integral types are promoted to XLen width, unless passed on the
10568     // stack.
10569     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
10570       return extendType(Ty);
10571     }
10572 
10573     if (const auto *EIT = Ty->getAs<ExtIntType>()) {
10574       if (EIT->getNumBits() < XLen && !MustUseStack)
10575         return extendType(Ty);
10576       if (EIT->getNumBits() > 128 ||
10577           (!getContext().getTargetInfo().hasInt128Type() &&
10578            EIT->getNumBits() > 64))
10579         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10580     }
10581 
10582     return ABIArgInfo::getDirect();
10583   }
10584 
10585   // Aggregates which are <= 2*XLen will be passed in registers if possible,
10586   // so coerce to integers.
10587   if (Size <= 2 * XLen) {
10588     unsigned Alignment = getContext().getTypeAlign(Ty);
10589 
10590     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
10591     // required, and a 2-element XLen array if only XLen alignment is required.
10592     if (Size <= XLen) {
10593       return ABIArgInfo::getDirect(
10594           llvm::IntegerType::get(getVMContext(), XLen));
10595     } else if (Alignment == 2 * XLen) {
10596       return ABIArgInfo::getDirect(
10597           llvm::IntegerType::get(getVMContext(), 2 * XLen));
10598     } else {
10599       return ABIArgInfo::getDirect(llvm::ArrayType::get(
10600           llvm::IntegerType::get(getVMContext(), XLen), 2));
10601     }
10602   }
10603   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10604 }
10605 
10606 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
10607   if (RetTy->isVoidType())
10608     return ABIArgInfo::getIgnore();
10609 
10610   int ArgGPRsLeft = 2;
10611   int ArgFPRsLeft = FLen ? 2 : 0;
10612 
10613   // The rules for return and argument types are the same, so defer to
10614   // classifyArgumentType.
10615   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
10616                               ArgFPRsLeft);
10617 }
10618 
10619 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10620                                 QualType Ty) const {
10621   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
10622 
10623   // Empty records are ignored for parameter passing purposes.
10624   if (isEmptyRecord(getContext(), Ty, true)) {
10625     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
10626     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
10627     return Addr;
10628   }
10629 
10630   std::pair<CharUnits, CharUnits> SizeAndAlign =
10631       getContext().getTypeInfoInChars(Ty);
10632 
10633   // Arguments bigger than 2*Xlen bytes are passed indirectly.
10634   bool IsIndirect = SizeAndAlign.first > 2 * SlotSize;
10635 
10636   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign,
10637                           SlotSize, /*AllowHigherAlign=*/true);
10638 }
10639 
10640 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
10641   int TySize = getContext().getTypeSize(Ty);
10642   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
10643   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
10644     return ABIArgInfo::getSignExtend(Ty);
10645   return ABIArgInfo::getExtend(Ty);
10646 }
10647 
10648 namespace {
10649 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
10650 public:
10651   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
10652                          unsigned FLen)
10653       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
10654 
10655   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
10656                            CodeGen::CodeGenModule &CGM) const override {
10657     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
10658     if (!FD) return;
10659 
10660     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
10661     if (!Attr)
10662       return;
10663 
10664     const char *Kind;
10665     switch (Attr->getInterrupt()) {
10666     case RISCVInterruptAttr::user: Kind = "user"; break;
10667     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
10668     case RISCVInterruptAttr::machine: Kind = "machine"; break;
10669     }
10670 
10671     auto *Fn = cast<llvm::Function>(GV);
10672 
10673     Fn->addFnAttr("interrupt", Kind);
10674   }
10675 };
10676 } // namespace
10677 
10678 //===----------------------------------------------------------------------===//
10679 // VE ABI Implementation.
10680 //
10681 namespace {
10682 class VEABIInfo : public DefaultABIInfo {
10683 public:
10684   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
10685 
10686 private:
10687   ABIArgInfo classifyReturnType(QualType RetTy) const;
10688   ABIArgInfo classifyArgumentType(QualType RetTy) const;
10689   void computeInfo(CGFunctionInfo &FI) const override;
10690 };
10691 } // end anonymous namespace
10692 
10693 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
10694   if (Ty->isAnyComplexType()) {
10695     return ABIArgInfo::getDirect();
10696   }
10697   return DefaultABIInfo::classifyReturnType(Ty);
10698 }
10699 
10700 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
10701   if (Ty->isAnyComplexType()) {
10702     return ABIArgInfo::getDirect();
10703   }
10704   return DefaultABIInfo::classifyArgumentType(Ty);
10705 }
10706 
10707 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
10708 
10709   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10710   for (auto &Arg : FI.arguments())
10711     Arg.info = classifyArgumentType(Arg.type);
10712 }
10713 
10714 namespace {
10715 class VETargetCodeGenInfo : public TargetCodeGenInfo {
10716 public:
10717   VETargetCodeGenInfo(CodeGenTypes &CGT)
10718       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
10719   // VE ABI requires the arguments of variadic and prototype-less functions
10720   // are passed in both registers and memory.
10721   bool isNoProtoCallVariadic(const CallArgList &args,
10722                              const FunctionNoProtoType *fnType) const override {
10723     return true;
10724   }
10725 };
10726 } // end anonymous namespace
10727 
10728 //===----------------------------------------------------------------------===//
10729 // Driver code
10730 //===----------------------------------------------------------------------===//
10731 
10732 bool CodeGenModule::supportsCOMDAT() const {
10733   return getTriple().supportsCOMDAT();
10734 }
10735 
10736 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
10737   if (TheTargetCodeGenInfo)
10738     return *TheTargetCodeGenInfo;
10739 
10740   // Helper to set the unique_ptr while still keeping the return value.
10741   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
10742     this->TheTargetCodeGenInfo.reset(P);
10743     return *P;
10744   };
10745 
10746   const llvm::Triple &Triple = getTarget().getTriple();
10747   switch (Triple.getArch()) {
10748   default:
10749     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
10750 
10751   case llvm::Triple::le32:
10752     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
10753   case llvm::Triple::mips:
10754   case llvm::Triple::mipsel:
10755     if (Triple.getOS() == llvm::Triple::NaCl)
10756       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
10757     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
10758 
10759   case llvm::Triple::mips64:
10760   case llvm::Triple::mips64el:
10761     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
10762 
10763   case llvm::Triple::avr:
10764     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
10765 
10766   case llvm::Triple::aarch64:
10767   case llvm::Triple::aarch64_32:
10768   case llvm::Triple::aarch64_be: {
10769     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
10770     if (getTarget().getABI() == "darwinpcs")
10771       Kind = AArch64ABIInfo::DarwinPCS;
10772     else if (Triple.isOSWindows())
10773       return SetCGInfo(
10774           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
10775 
10776     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
10777   }
10778 
10779   case llvm::Triple::wasm32:
10780   case llvm::Triple::wasm64: {
10781     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
10782     if (getTarget().getABI() == "experimental-mv")
10783       Kind = WebAssemblyABIInfo::ExperimentalMV;
10784     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
10785   }
10786 
10787   case llvm::Triple::arm:
10788   case llvm::Triple::armeb:
10789   case llvm::Triple::thumb:
10790   case llvm::Triple::thumbeb: {
10791     if (Triple.getOS() == llvm::Triple::Win32) {
10792       return SetCGInfo(
10793           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
10794     }
10795 
10796     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
10797     StringRef ABIStr = getTarget().getABI();
10798     if (ABIStr == "apcs-gnu")
10799       Kind = ARMABIInfo::APCS;
10800     else if (ABIStr == "aapcs16")
10801       Kind = ARMABIInfo::AAPCS16_VFP;
10802     else if (CodeGenOpts.FloatABI == "hard" ||
10803              (CodeGenOpts.FloatABI != "soft" &&
10804               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
10805                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
10806                Triple.getEnvironment() == llvm::Triple::EABIHF)))
10807       Kind = ARMABIInfo::AAPCS_VFP;
10808 
10809     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
10810   }
10811 
10812   case llvm::Triple::ppc: {
10813     if (Triple.isOSAIX())
10814       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
10815 
10816     bool IsSoftFloat =
10817         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
10818     bool RetSmallStructInRegABI =
10819         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
10820     return SetCGInfo(
10821         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
10822   }
10823   case llvm::Triple::ppc64:
10824     if (Triple.isOSAIX())
10825       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
10826 
10827     if (Triple.isOSBinFormatELF()) {
10828       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
10829       if (getTarget().getABI() == "elfv2")
10830         Kind = PPC64_SVR4_ABIInfo::ELFv2;
10831       bool HasQPX = getTarget().getABI() == "elfv1-qpx";
10832       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
10833 
10834       return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
10835                                                         IsSoftFloat));
10836     }
10837     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
10838   case llvm::Triple::ppc64le: {
10839     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
10840     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
10841     if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
10842       Kind = PPC64_SVR4_ABIInfo::ELFv1;
10843     bool HasQPX = getTarget().getABI() == "elfv1-qpx";
10844     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
10845 
10846     return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
10847                                                       IsSoftFloat));
10848   }
10849 
10850   case llvm::Triple::nvptx:
10851   case llvm::Triple::nvptx64:
10852     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
10853 
10854   case llvm::Triple::msp430:
10855     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
10856 
10857   case llvm::Triple::riscv32:
10858   case llvm::Triple::riscv64: {
10859     StringRef ABIStr = getTarget().getABI();
10860     unsigned XLen = getTarget().getPointerWidth(0);
10861     unsigned ABIFLen = 0;
10862     if (ABIStr.endswith("f"))
10863       ABIFLen = 32;
10864     else if (ABIStr.endswith("d"))
10865       ABIFLen = 64;
10866     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
10867   }
10868 
10869   case llvm::Triple::systemz: {
10870     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
10871     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
10872     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
10873   }
10874 
10875   case llvm::Triple::tce:
10876   case llvm::Triple::tcele:
10877     return SetCGInfo(new TCETargetCodeGenInfo(Types));
10878 
10879   case llvm::Triple::x86: {
10880     bool IsDarwinVectorABI = Triple.isOSDarwin();
10881     bool RetSmallStructInRegABI =
10882         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
10883     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
10884 
10885     if (Triple.getOS() == llvm::Triple::Win32) {
10886       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
10887           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
10888           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
10889     } else {
10890       return SetCGInfo(new X86_32TargetCodeGenInfo(
10891           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
10892           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
10893           CodeGenOpts.FloatABI == "soft"));
10894     }
10895   }
10896 
10897   case llvm::Triple::x86_64: {
10898     StringRef ABI = getTarget().getABI();
10899     X86AVXABILevel AVXLevel =
10900         (ABI == "avx512"
10901              ? X86AVXABILevel::AVX512
10902              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
10903 
10904     switch (Triple.getOS()) {
10905     case llvm::Triple::Win32:
10906       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
10907     default:
10908       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
10909     }
10910   }
10911   case llvm::Triple::hexagon:
10912     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
10913   case llvm::Triple::lanai:
10914     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
10915   case llvm::Triple::r600:
10916     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
10917   case llvm::Triple::amdgcn:
10918     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
10919   case llvm::Triple::sparc:
10920     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
10921   case llvm::Triple::sparcv9:
10922     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
10923   case llvm::Triple::xcore:
10924     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
10925   case llvm::Triple::arc:
10926     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
10927   case llvm::Triple::spir:
10928   case llvm::Triple::spir64:
10929     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
10930   case llvm::Triple::ve:
10931     return SetCGInfo(new VETargetCodeGenInfo(Types));
10932   }
10933 }
10934 
10935 /// Create an OpenCL kernel for an enqueued block.
10936 ///
10937 /// The kernel has the same function type as the block invoke function. Its
10938 /// name is the name of the block invoke function postfixed with "_kernel".
10939 /// It simply calls the block invoke function then returns.
10940 llvm::Function *
10941 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
10942                                              llvm::Function *Invoke,
10943                                              llvm::Value *BlockLiteral) const {
10944   auto *InvokeFT = Invoke->getFunctionType();
10945   llvm::SmallVector<llvm::Type *, 2> ArgTys;
10946   for (auto &P : InvokeFT->params())
10947     ArgTys.push_back(P);
10948   auto &C = CGF.getLLVMContext();
10949   std::string Name = Invoke->getName().str() + "_kernel";
10950   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
10951   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
10952                                    &CGF.CGM.getModule());
10953   auto IP = CGF.Builder.saveIP();
10954   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
10955   auto &Builder = CGF.Builder;
10956   Builder.SetInsertPoint(BB);
10957   llvm::SmallVector<llvm::Value *, 2> Args;
10958   for (auto &A : F->args())
10959     Args.push_back(&A);
10960   Builder.CreateCall(Invoke, Args);
10961   Builder.CreateRetVoid();
10962   Builder.restoreIP(IP);
10963   return F;
10964 }
10965 
10966 /// Create an OpenCL kernel for an enqueued block.
10967 ///
10968 /// The type of the first argument (the block literal) is the struct type
10969 /// of the block literal instead of a pointer type. The first argument
10970 /// (block literal) is passed directly by value to the kernel. The kernel
10971 /// allocates the same type of struct on stack and stores the block literal
10972 /// to it and passes its pointer to the block invoke function. The kernel
10973 /// has "enqueued-block" function attribute and kernel argument metadata.
10974 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
10975     CodeGenFunction &CGF, llvm::Function *Invoke,
10976     llvm::Value *BlockLiteral) const {
10977   auto &Builder = CGF.Builder;
10978   auto &C = CGF.getLLVMContext();
10979 
10980   auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
10981   auto *InvokeFT = Invoke->getFunctionType();
10982   llvm::SmallVector<llvm::Type *, 2> ArgTys;
10983   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
10984   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
10985   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
10986   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
10987   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
10988   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
10989 
10990   ArgTys.push_back(BlockTy);
10991   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
10992   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
10993   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
10994   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
10995   AccessQuals.push_back(llvm::MDString::get(C, "none"));
10996   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
10997   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
10998     ArgTys.push_back(InvokeFT->getParamType(I));
10999     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
11000     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
11001     AccessQuals.push_back(llvm::MDString::get(C, "none"));
11002     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
11003     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11004     ArgNames.push_back(
11005         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
11006   }
11007   std::string Name = Invoke->getName().str() + "_kernel";
11008   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11009   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11010                                    &CGF.CGM.getModule());
11011   F->addFnAttr("enqueued-block");
11012   auto IP = CGF.Builder.saveIP();
11013   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11014   Builder.SetInsertPoint(BB);
11015   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
11016   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
11017   BlockPtr->setAlignment(BlockAlign);
11018   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
11019   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
11020   llvm::SmallVector<llvm::Value *, 2> Args;
11021   Args.push_back(Cast);
11022   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
11023     Args.push_back(I);
11024   Builder.CreateCall(Invoke, Args);
11025   Builder.CreateRetVoid();
11026   Builder.restoreIP(IP);
11027 
11028   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
11029   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
11030   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
11031   F->setMetadata("kernel_arg_base_type",
11032                  llvm::MDNode::get(C, ArgBaseTypeNames));
11033   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
11034   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
11035     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
11036 
11037   return F;
11038 }
11039