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 MSP430ABIInfo : public DefaultABIInfo {
7479   static ABIArgInfo complexArgInfo() {
7480     ABIArgInfo Info = ABIArgInfo::getDirect();
7481     Info.setCanBeFlattened(false);
7482     return Info;
7483   }
7484 
7485 public:
7486   MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7487 
7488   ABIArgInfo classifyReturnType(QualType RetTy) const {
7489     if (RetTy->isAnyComplexType())
7490       return complexArgInfo();
7491 
7492     return DefaultABIInfo::classifyReturnType(RetTy);
7493   }
7494 
7495   ABIArgInfo classifyArgumentType(QualType RetTy) const {
7496     if (RetTy->isAnyComplexType())
7497       return complexArgInfo();
7498 
7499     return DefaultABIInfo::classifyArgumentType(RetTy);
7500   }
7501 
7502   // Just copy the original implementations because
7503   // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
7504   void computeInfo(CGFunctionInfo &FI) const override {
7505     if (!getCXXABI().classifyReturnType(FI))
7506       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7507     for (auto &I : FI.arguments())
7508       I.info = classifyArgumentType(I.type);
7509   }
7510 
7511   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7512                     QualType Ty) const override {
7513     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
7514   }
7515 };
7516 
7517 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7518 public:
7519   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7520       : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
7521   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7522                            CodeGen::CodeGenModule &M) const override;
7523 };
7524 
7525 }
7526 
7527 void MSP430TargetCodeGenInfo::setTargetAttributes(
7528     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7529   if (GV->isDeclaration())
7530     return;
7531   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7532     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7533     if (!InterruptAttr)
7534       return;
7535 
7536     // Handle 'interrupt' attribute:
7537     llvm::Function *F = cast<llvm::Function>(GV);
7538 
7539     // Step 1: Set ISR calling convention.
7540     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7541 
7542     // Step 2: Add attributes goodness.
7543     F->addFnAttr(llvm::Attribute::NoInline);
7544     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7545   }
7546 }
7547 
7548 //===----------------------------------------------------------------------===//
7549 // MIPS ABI Implementation.  This works for both little-endian and
7550 // big-endian variants.
7551 //===----------------------------------------------------------------------===//
7552 
7553 namespace {
7554 class MipsABIInfo : public ABIInfo {
7555   bool IsO32;
7556   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7557   void CoerceToIntArgs(uint64_t TySize,
7558                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7559   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7560   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7561   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7562 public:
7563   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7564     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7565     StackAlignInBytes(IsO32 ? 8 : 16) {}
7566 
7567   ABIArgInfo classifyReturnType(QualType RetTy) const;
7568   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7569   void computeInfo(CGFunctionInfo &FI) const override;
7570   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7571                     QualType Ty) const override;
7572   ABIArgInfo extendType(QualType Ty) const;
7573 };
7574 
7575 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7576   unsigned SizeOfUnwindException;
7577 public:
7578   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7579       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7580         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7581 
7582   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7583     return 29;
7584   }
7585 
7586   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7587                            CodeGen::CodeGenModule &CGM) const override {
7588     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7589     if (!FD) return;
7590     llvm::Function *Fn = cast<llvm::Function>(GV);
7591 
7592     if (FD->hasAttr<MipsLongCallAttr>())
7593       Fn->addFnAttr("long-call");
7594     else if (FD->hasAttr<MipsShortCallAttr>())
7595       Fn->addFnAttr("short-call");
7596 
7597     // Other attributes do not have a meaning for declarations.
7598     if (GV->isDeclaration())
7599       return;
7600 
7601     if (FD->hasAttr<Mips16Attr>()) {
7602       Fn->addFnAttr("mips16");
7603     }
7604     else if (FD->hasAttr<NoMips16Attr>()) {
7605       Fn->addFnAttr("nomips16");
7606     }
7607 
7608     if (FD->hasAttr<MicroMipsAttr>())
7609       Fn->addFnAttr("micromips");
7610     else if (FD->hasAttr<NoMicroMipsAttr>())
7611       Fn->addFnAttr("nomicromips");
7612 
7613     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7614     if (!Attr)
7615       return;
7616 
7617     const char *Kind;
7618     switch (Attr->getInterrupt()) {
7619     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7620     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7621     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7622     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7623     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7624     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7625     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7626     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7627     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7628     }
7629 
7630     Fn->addFnAttr("interrupt", Kind);
7631 
7632   }
7633 
7634   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7635                                llvm::Value *Address) const override;
7636 
7637   unsigned getSizeOfUnwindException() const override {
7638     return SizeOfUnwindException;
7639   }
7640 };
7641 }
7642 
7643 void MipsABIInfo::CoerceToIntArgs(
7644     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7645   llvm::IntegerType *IntTy =
7646     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7647 
7648   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7649   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7650     ArgList.push_back(IntTy);
7651 
7652   // If necessary, add one more integer type to ArgList.
7653   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7654 
7655   if (R)
7656     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7657 }
7658 
7659 // In N32/64, an aligned double precision floating point field is passed in
7660 // a register.
7661 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7662   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7663 
7664   if (IsO32) {
7665     CoerceToIntArgs(TySize, ArgList);
7666     return llvm::StructType::get(getVMContext(), ArgList);
7667   }
7668 
7669   if (Ty->isComplexType())
7670     return CGT.ConvertType(Ty);
7671 
7672   const RecordType *RT = Ty->getAs<RecordType>();
7673 
7674   // Unions/vectors are passed in integer registers.
7675   if (!RT || !RT->isStructureOrClassType()) {
7676     CoerceToIntArgs(TySize, ArgList);
7677     return llvm::StructType::get(getVMContext(), ArgList);
7678   }
7679 
7680   const RecordDecl *RD = RT->getDecl();
7681   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7682   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7683 
7684   uint64_t LastOffset = 0;
7685   unsigned idx = 0;
7686   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7687 
7688   // Iterate over fields in the struct/class and check if there are any aligned
7689   // double fields.
7690   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
7691        i != e; ++i, ++idx) {
7692     const QualType Ty = i->getType();
7693     const BuiltinType *BT = Ty->getAs<BuiltinType>();
7694 
7695     if (!BT || BT->getKind() != BuiltinType::Double)
7696       continue;
7697 
7698     uint64_t Offset = Layout.getFieldOffset(idx);
7699     if (Offset % 64) // Ignore doubles that are not aligned.
7700       continue;
7701 
7702     // Add ((Offset - LastOffset) / 64) args of type i64.
7703     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
7704       ArgList.push_back(I64);
7705 
7706     // Add double type.
7707     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
7708     LastOffset = Offset + 64;
7709   }
7710 
7711   CoerceToIntArgs(TySize - LastOffset, IntArgList);
7712   ArgList.append(IntArgList.begin(), IntArgList.end());
7713 
7714   return llvm::StructType::get(getVMContext(), ArgList);
7715 }
7716 
7717 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
7718                                         uint64_t Offset) const {
7719   if (OrigOffset + MinABIStackAlignInBytes > Offset)
7720     return nullptr;
7721 
7722   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
7723 }
7724 
7725 ABIArgInfo
7726 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
7727   Ty = useFirstFieldIfTransparentUnion(Ty);
7728 
7729   uint64_t OrigOffset = Offset;
7730   uint64_t TySize = getContext().getTypeSize(Ty);
7731   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
7732 
7733   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
7734                    (uint64_t)StackAlignInBytes);
7735   unsigned CurrOffset = llvm::alignTo(Offset, Align);
7736   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
7737 
7738   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
7739     // Ignore empty aggregates.
7740     if (TySize == 0)
7741       return ABIArgInfo::getIgnore();
7742 
7743     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
7744       Offset = OrigOffset + MinABIStackAlignInBytes;
7745       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7746     }
7747 
7748     // If we have reached here, aggregates are passed directly by coercing to
7749     // another structure type. Padding is inserted if the offset of the
7750     // aggregate is unaligned.
7751     ABIArgInfo ArgInfo =
7752         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
7753                               getPaddingType(OrigOffset, CurrOffset));
7754     ArgInfo.setInReg(true);
7755     return ArgInfo;
7756   }
7757 
7758   // Treat an enum type as its underlying type.
7759   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7760     Ty = EnumTy->getDecl()->getIntegerType();
7761 
7762   // Make sure we pass indirectly things that are too large.
7763   if (const auto *EIT = Ty->getAs<ExtIntType>())
7764     if (EIT->getNumBits() > 128 ||
7765         (EIT->getNumBits() > 64 &&
7766          !getContext().getTargetInfo().hasInt128Type()))
7767       return getNaturalAlignIndirect(Ty);
7768 
7769   // All integral types are promoted to the GPR width.
7770   if (Ty->isIntegralOrEnumerationType())
7771     return extendType(Ty);
7772 
7773   return ABIArgInfo::getDirect(
7774       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
7775 }
7776 
7777 llvm::Type*
7778 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
7779   const RecordType *RT = RetTy->getAs<RecordType>();
7780   SmallVector<llvm::Type*, 8> RTList;
7781 
7782   if (RT && RT->isStructureOrClassType()) {
7783     const RecordDecl *RD = RT->getDecl();
7784     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7785     unsigned FieldCnt = Layout.getFieldCount();
7786 
7787     // N32/64 returns struct/classes in floating point registers if the
7788     // following conditions are met:
7789     // 1. The size of the struct/class is no larger than 128-bit.
7790     // 2. The struct/class has one or two fields all of which are floating
7791     //    point types.
7792     // 3. The offset of the first field is zero (this follows what gcc does).
7793     //
7794     // Any other composite results are returned in integer registers.
7795     //
7796     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
7797       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
7798       for (; b != e; ++b) {
7799         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
7800 
7801         if (!BT || !BT->isFloatingPoint())
7802           break;
7803 
7804         RTList.push_back(CGT.ConvertType(b->getType()));
7805       }
7806 
7807       if (b == e)
7808         return llvm::StructType::get(getVMContext(), RTList,
7809                                      RD->hasAttr<PackedAttr>());
7810 
7811       RTList.clear();
7812     }
7813   }
7814 
7815   CoerceToIntArgs(Size, RTList);
7816   return llvm::StructType::get(getVMContext(), RTList);
7817 }
7818 
7819 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
7820   uint64_t Size = getContext().getTypeSize(RetTy);
7821 
7822   if (RetTy->isVoidType())
7823     return ABIArgInfo::getIgnore();
7824 
7825   // O32 doesn't treat zero-sized structs differently from other structs.
7826   // However, N32/N64 ignores zero sized return values.
7827   if (!IsO32 && Size == 0)
7828     return ABIArgInfo::getIgnore();
7829 
7830   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
7831     if (Size <= 128) {
7832       if (RetTy->isAnyComplexType())
7833         return ABIArgInfo::getDirect();
7834 
7835       // O32 returns integer vectors in registers and N32/N64 returns all small
7836       // aggregates in registers.
7837       if (!IsO32 ||
7838           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
7839         ABIArgInfo ArgInfo =
7840             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
7841         ArgInfo.setInReg(true);
7842         return ArgInfo;
7843       }
7844     }
7845 
7846     return getNaturalAlignIndirect(RetTy);
7847   }
7848 
7849   // Treat an enum type as its underlying type.
7850   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7851     RetTy = EnumTy->getDecl()->getIntegerType();
7852 
7853   // Make sure we pass indirectly things that are too large.
7854   if (const auto *EIT = RetTy->getAs<ExtIntType>())
7855     if (EIT->getNumBits() > 128 ||
7856         (EIT->getNumBits() > 64 &&
7857          !getContext().getTargetInfo().hasInt128Type()))
7858       return getNaturalAlignIndirect(RetTy);
7859 
7860   if (isPromotableIntegerTypeForABI(RetTy))
7861     return ABIArgInfo::getExtend(RetTy);
7862 
7863   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
7864       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
7865     return ABIArgInfo::getSignExtend(RetTy);
7866 
7867   return ABIArgInfo::getDirect();
7868 }
7869 
7870 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
7871   ABIArgInfo &RetInfo = FI.getReturnInfo();
7872   if (!getCXXABI().classifyReturnType(FI))
7873     RetInfo = classifyReturnType(FI.getReturnType());
7874 
7875   // Check if a pointer to an aggregate is passed as a hidden argument.
7876   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
7877 
7878   for (auto &I : FI.arguments())
7879     I.info = classifyArgumentType(I.type, Offset);
7880 }
7881 
7882 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7883                                QualType OrigTy) const {
7884   QualType Ty = OrigTy;
7885 
7886   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
7887   // Pointers are also promoted in the same way but this only matters for N32.
7888   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
7889   unsigned PtrWidth = getTarget().getPointerWidth(0);
7890   bool DidPromote = false;
7891   if ((Ty->isIntegerType() &&
7892           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
7893       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
7894     DidPromote = true;
7895     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
7896                                             Ty->isSignedIntegerType());
7897   }
7898 
7899   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7900 
7901   // The alignment of things in the argument area is never larger than
7902   // StackAlignInBytes.
7903   TyInfo.second =
7904     std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
7905 
7906   // MinABIStackAlignInBytes is the size of argument slots on the stack.
7907   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
7908 
7909   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7910                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
7911 
7912 
7913   // If there was a promotion, "unpromote" into a temporary.
7914   // TODO: can we just use a pointer into a subset of the original slot?
7915   if (DidPromote) {
7916     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
7917     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
7918 
7919     // Truncate down to the right width.
7920     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
7921                                                  : CGF.IntPtrTy);
7922     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
7923     if (OrigTy->isPointerType())
7924       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
7925 
7926     CGF.Builder.CreateStore(V, Temp);
7927     Addr = Temp;
7928   }
7929 
7930   return Addr;
7931 }
7932 
7933 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
7934   int TySize = getContext().getTypeSize(Ty);
7935 
7936   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7937   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
7938     return ABIArgInfo::getSignExtend(Ty);
7939 
7940   return ABIArgInfo::getExtend(Ty);
7941 }
7942 
7943 bool
7944 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7945                                                llvm::Value *Address) const {
7946   // This information comes from gcc's implementation, which seems to
7947   // as canonical as it gets.
7948 
7949   // Everything on MIPS is 4 bytes.  Double-precision FP registers
7950   // are aliased to pairs of single-precision FP registers.
7951   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
7952 
7953   // 0-31 are the general purpose registers, $0 - $31.
7954   // 32-63 are the floating-point registers, $f0 - $f31.
7955   // 64 and 65 are the multiply/divide registers, $hi and $lo.
7956   // 66 is the (notional, I think) register for signal-handler return.
7957   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
7958 
7959   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7960   // They are one bit wide and ignored here.
7961 
7962   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7963   // (coprocessor 1 is the FP unit)
7964   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7965   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7966   // 176-181 are the DSP accumulator registers.
7967   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
7968   return false;
7969 }
7970 
7971 //===----------------------------------------------------------------------===//
7972 // AVR ABI Implementation.
7973 //===----------------------------------------------------------------------===//
7974 
7975 namespace {
7976 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7977 public:
7978   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7979       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
7980 
7981   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7982                            CodeGen::CodeGenModule &CGM) const override {
7983     if (GV->isDeclaration())
7984       return;
7985     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7986     if (!FD) return;
7987     auto *Fn = cast<llvm::Function>(GV);
7988 
7989     if (FD->getAttr<AVRInterruptAttr>())
7990       Fn->addFnAttr("interrupt");
7991 
7992     if (FD->getAttr<AVRSignalAttr>())
7993       Fn->addFnAttr("signal");
7994   }
7995 };
7996 }
7997 
7998 //===----------------------------------------------------------------------===//
7999 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
8000 // Currently subclassed only to implement custom OpenCL C function attribute
8001 // handling.
8002 //===----------------------------------------------------------------------===//
8003 
8004 namespace {
8005 
8006 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
8007 public:
8008   TCETargetCodeGenInfo(CodeGenTypes &CGT)
8009     : DefaultTargetCodeGenInfo(CGT) {}
8010 
8011   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8012                            CodeGen::CodeGenModule &M) const override;
8013 };
8014 
8015 void TCETargetCodeGenInfo::setTargetAttributes(
8016     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8017   if (GV->isDeclaration())
8018     return;
8019   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8020   if (!FD) return;
8021 
8022   llvm::Function *F = cast<llvm::Function>(GV);
8023 
8024   if (M.getLangOpts().OpenCL) {
8025     if (FD->hasAttr<OpenCLKernelAttr>()) {
8026       // OpenCL C Kernel functions are not subject to inlining
8027       F->addFnAttr(llvm::Attribute::NoInline);
8028       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
8029       if (Attr) {
8030         // Convert the reqd_work_group_size() attributes to metadata.
8031         llvm::LLVMContext &Context = F->getContext();
8032         llvm::NamedMDNode *OpenCLMetadata =
8033             M.getModule().getOrInsertNamedMetadata(
8034                 "opencl.kernel_wg_size_info");
8035 
8036         SmallVector<llvm::Metadata *, 5> Operands;
8037         Operands.push_back(llvm::ConstantAsMetadata::get(F));
8038 
8039         Operands.push_back(
8040             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8041                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8042         Operands.push_back(
8043             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8044                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8045         Operands.push_back(
8046             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8047                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8048 
8049         // Add a boolean constant operand for "required" (true) or "hint"
8050         // (false) for implementing the work_group_size_hint attr later.
8051         // Currently always true as the hint is not yet implemented.
8052         Operands.push_back(
8053             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8054         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8055       }
8056     }
8057   }
8058 }
8059 
8060 }
8061 
8062 //===----------------------------------------------------------------------===//
8063 // Hexagon ABI Implementation
8064 //===----------------------------------------------------------------------===//
8065 
8066 namespace {
8067 
8068 class HexagonABIInfo : public DefaultABIInfo {
8069 public:
8070   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8071 
8072 private:
8073   ABIArgInfo classifyReturnType(QualType RetTy) const;
8074   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8075   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8076 
8077   void computeInfo(CGFunctionInfo &FI) const override;
8078 
8079   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8080                     QualType Ty) const override;
8081   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8082                               QualType Ty) const;
8083   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8084                               QualType Ty) const;
8085   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8086                                    QualType Ty) const;
8087 };
8088 
8089 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8090 public:
8091   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8092       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8093 
8094   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8095     return 29;
8096   }
8097 
8098   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8099                            CodeGen::CodeGenModule &GCM) const override {
8100     if (GV->isDeclaration())
8101       return;
8102     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8103     if (!FD)
8104       return;
8105   }
8106 };
8107 
8108 } // namespace
8109 
8110 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8111   unsigned RegsLeft = 6;
8112   if (!getCXXABI().classifyReturnType(FI))
8113     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8114   for (auto &I : FI.arguments())
8115     I.info = classifyArgumentType(I.type, &RegsLeft);
8116 }
8117 
8118 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8119   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8120                        " through registers");
8121 
8122   if (*RegsLeft == 0)
8123     return false;
8124 
8125   if (Size <= 32) {
8126     (*RegsLeft)--;
8127     return true;
8128   }
8129 
8130   if (2 <= (*RegsLeft & (~1U))) {
8131     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8132     return true;
8133   }
8134 
8135   // Next available register was r5 but candidate was greater than 32-bits so it
8136   // has to go on the stack. However we still consume r5
8137   if (*RegsLeft == 1)
8138     *RegsLeft = 0;
8139 
8140   return false;
8141 }
8142 
8143 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8144                                                 unsigned *RegsLeft) const {
8145   if (!isAggregateTypeForABI(Ty)) {
8146     // Treat an enum type as its underlying type.
8147     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8148       Ty = EnumTy->getDecl()->getIntegerType();
8149 
8150     uint64_t Size = getContext().getTypeSize(Ty);
8151     if (Size <= 64)
8152       HexagonAdjustRegsLeft(Size, RegsLeft);
8153 
8154     if (Size > 64 && Ty->isExtIntType())
8155       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8156 
8157     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8158                                              : ABIArgInfo::getDirect();
8159   }
8160 
8161   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8162     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8163 
8164   // Ignore empty records.
8165   if (isEmptyRecord(getContext(), Ty, true))
8166     return ABIArgInfo::getIgnore();
8167 
8168   uint64_t Size = getContext().getTypeSize(Ty);
8169   unsigned Align = getContext().getTypeAlign(Ty);
8170 
8171   if (Size > 64)
8172     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8173 
8174   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8175     Align = Size <= 32 ? 32 : 64;
8176   if (Size <= Align) {
8177     // Pass in the smallest viable integer type.
8178     if (!llvm::isPowerOf2_64(Size))
8179       Size = llvm::NextPowerOf2(Size);
8180     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8181   }
8182   return DefaultABIInfo::classifyArgumentType(Ty);
8183 }
8184 
8185 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8186   if (RetTy->isVoidType())
8187     return ABIArgInfo::getIgnore();
8188 
8189   const TargetInfo &T = CGT.getTarget();
8190   uint64_t Size = getContext().getTypeSize(RetTy);
8191 
8192   if (RetTy->getAs<VectorType>()) {
8193     // HVX vectors are returned in vector registers or register pairs.
8194     if (T.hasFeature("hvx")) {
8195       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8196       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8197       if (Size == VecSize || Size == 2*VecSize)
8198         return ABIArgInfo::getDirectInReg();
8199     }
8200     // Large vector types should be returned via memory.
8201     if (Size > 64)
8202       return getNaturalAlignIndirect(RetTy);
8203   }
8204 
8205   if (!isAggregateTypeForABI(RetTy)) {
8206     // Treat an enum type as its underlying type.
8207     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8208       RetTy = EnumTy->getDecl()->getIntegerType();
8209 
8210     if (Size > 64 && RetTy->isExtIntType())
8211       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8212 
8213     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8214                                                 : ABIArgInfo::getDirect();
8215   }
8216 
8217   if (isEmptyRecord(getContext(), RetTy, true))
8218     return ABIArgInfo::getIgnore();
8219 
8220   // Aggregates <= 8 bytes are returned in registers, other aggregates
8221   // are returned indirectly.
8222   if (Size <= 64) {
8223     // Return in the smallest viable integer type.
8224     if (!llvm::isPowerOf2_64(Size))
8225       Size = llvm::NextPowerOf2(Size);
8226     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8227   }
8228   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8229 }
8230 
8231 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8232                                             Address VAListAddr,
8233                                             QualType Ty) const {
8234   // Load the overflow area pointer.
8235   Address __overflow_area_pointer_p =
8236       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8237   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8238       __overflow_area_pointer_p, "__overflow_area_pointer");
8239 
8240   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8241   if (Align > 4) {
8242     // Alignment should be a power of 2.
8243     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8244 
8245     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8246     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8247 
8248     // Add offset to the current pointer to access the argument.
8249     __overflow_area_pointer =
8250         CGF.Builder.CreateGEP(__overflow_area_pointer, Offset);
8251     llvm::Value *AsInt =
8252         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8253 
8254     // Create a mask which should be "AND"ed
8255     // with (overflow_arg_area + align - 1)
8256     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8257     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8258         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8259         "__overflow_area_pointer.align");
8260   }
8261 
8262   // Get the type of the argument from memory and bitcast
8263   // overflow area pointer to the argument type.
8264   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8265   Address AddrTyped = CGF.Builder.CreateBitCast(
8266       Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)),
8267       llvm::PointerType::getUnqual(PTy));
8268 
8269   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8270   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8271 
8272   __overflow_area_pointer = CGF.Builder.CreateGEP(
8273       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8274       "__overflow_area_pointer.next");
8275   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8276 
8277   return AddrTyped;
8278 }
8279 
8280 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8281                                             Address VAListAddr,
8282                                             QualType Ty) const {
8283   // FIXME: Need to handle alignment
8284   llvm::Type *BP = CGF.Int8PtrTy;
8285   llvm::Type *BPP = CGF.Int8PtrPtrTy;
8286   CGBuilderTy &Builder = CGF.Builder;
8287   Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
8288   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8289   // Handle address alignment for type alignment > 32 bits
8290   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8291   if (TyAlign > 4) {
8292     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8293     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8294     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8295     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8296     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8297   }
8298   llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
8299   Address AddrTyped = Builder.CreateBitCast(
8300       Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy);
8301 
8302   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8303   llvm::Value *NextAddr = Builder.CreateGEP(
8304       Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8305   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8306 
8307   return AddrTyped;
8308 }
8309 
8310 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8311                                                  Address VAListAddr,
8312                                                  QualType Ty) const {
8313   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8314 
8315   if (ArgSize > 8)
8316     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8317 
8318   // Here we have check if the argument is in register area or
8319   // in overflow area.
8320   // If the saved register area pointer + argsize rounded up to alignment >
8321   // saved register area end pointer, argument is in overflow area.
8322   unsigned RegsLeft = 6;
8323   Ty = CGF.getContext().getCanonicalType(Ty);
8324   (void)classifyArgumentType(Ty, &RegsLeft);
8325 
8326   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8327   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8328   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8329   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8330 
8331   // Get rounded size of the argument.GCC does not allow vararg of
8332   // size < 4 bytes. We follow the same logic here.
8333   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8334   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8335 
8336   // Argument may be in saved register area
8337   CGF.EmitBlock(MaybeRegBlock);
8338 
8339   // Load the current saved register area pointer.
8340   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8341       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8342   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8343       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8344 
8345   // Load the saved register area end pointer.
8346   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8347       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8348   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8349       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8350 
8351   // If the size of argument is > 4 bytes, check if the stack
8352   // location is aligned to 8 bytes
8353   if (ArgAlign > 4) {
8354 
8355     llvm::Value *__current_saved_reg_area_pointer_int =
8356         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8357                                    CGF.Int32Ty);
8358 
8359     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8360         __current_saved_reg_area_pointer_int,
8361         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8362         "align_current_saved_reg_area_pointer");
8363 
8364     __current_saved_reg_area_pointer_int =
8365         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8366                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8367                               "align_current_saved_reg_area_pointer");
8368 
8369     __current_saved_reg_area_pointer =
8370         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8371                                    __current_saved_reg_area_pointer->getType(),
8372                                    "align_current_saved_reg_area_pointer");
8373   }
8374 
8375   llvm::Value *__new_saved_reg_area_pointer =
8376       CGF.Builder.CreateGEP(__current_saved_reg_area_pointer,
8377                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8378                             "__new_saved_reg_area_pointer");
8379 
8380   llvm::Value *UsingStack = 0;
8381   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8382                                          __saved_reg_area_end_pointer);
8383 
8384   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8385 
8386   // Argument in saved register area
8387   // Implement the block where argument is in register saved area
8388   CGF.EmitBlock(InRegBlock);
8389 
8390   llvm::Type *PTy = CGF.ConvertType(Ty);
8391   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8392       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8393 
8394   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8395                           __current_saved_reg_area_pointer_p);
8396 
8397   CGF.EmitBranch(ContBlock);
8398 
8399   // Argument in overflow area
8400   // Implement the block where the argument is in overflow area.
8401   CGF.EmitBlock(OnStackBlock);
8402 
8403   // Load the overflow area pointer
8404   Address __overflow_area_pointer_p =
8405       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8406   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8407       __overflow_area_pointer_p, "__overflow_area_pointer");
8408 
8409   // Align the overflow area pointer according to the alignment of the argument
8410   if (ArgAlign > 4) {
8411     llvm::Value *__overflow_area_pointer_int =
8412         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8413 
8414     __overflow_area_pointer_int =
8415         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8416                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8417                               "align_overflow_area_pointer");
8418 
8419     __overflow_area_pointer_int =
8420         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8421                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8422                               "align_overflow_area_pointer");
8423 
8424     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8425         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8426         "align_overflow_area_pointer");
8427   }
8428 
8429   // Get the pointer for next argument in overflow area and store it
8430   // to overflow area pointer.
8431   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8432       __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8433       "__overflow_area_pointer.next");
8434 
8435   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8436                           __overflow_area_pointer_p);
8437 
8438   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8439                           __current_saved_reg_area_pointer_p);
8440 
8441   // Bitcast the overflow area pointer to the type of argument.
8442   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8443   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8444       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8445 
8446   CGF.EmitBranch(ContBlock);
8447 
8448   // Get the correct pointer to load the variable argument
8449   // Implement the ContBlock
8450   CGF.EmitBlock(ContBlock);
8451 
8452   llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
8453   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
8454   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
8455   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
8456 
8457   return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign));
8458 }
8459 
8460 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8461                                   QualType Ty) const {
8462 
8463   if (getTarget().getTriple().isMusl())
8464     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
8465 
8466   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
8467 }
8468 
8469 //===----------------------------------------------------------------------===//
8470 // Lanai ABI Implementation
8471 //===----------------------------------------------------------------------===//
8472 
8473 namespace {
8474 class LanaiABIInfo : public DefaultABIInfo {
8475 public:
8476   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8477 
8478   bool shouldUseInReg(QualType Ty, CCState &State) const;
8479 
8480   void computeInfo(CGFunctionInfo &FI) const override {
8481     CCState State(FI);
8482     // Lanai uses 4 registers to pass arguments unless the function has the
8483     // regparm attribute set.
8484     if (FI.getHasRegParm()) {
8485       State.FreeRegs = FI.getRegParm();
8486     } else {
8487       State.FreeRegs = 4;
8488     }
8489 
8490     if (!getCXXABI().classifyReturnType(FI))
8491       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8492     for (auto &I : FI.arguments())
8493       I.info = classifyArgumentType(I.type, State);
8494   }
8495 
8496   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
8497   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
8498 };
8499 } // end anonymous namespace
8500 
8501 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
8502   unsigned Size = getContext().getTypeSize(Ty);
8503   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
8504 
8505   if (SizeInRegs == 0)
8506     return false;
8507 
8508   if (SizeInRegs > State.FreeRegs) {
8509     State.FreeRegs = 0;
8510     return false;
8511   }
8512 
8513   State.FreeRegs -= SizeInRegs;
8514 
8515   return true;
8516 }
8517 
8518 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
8519                                            CCState &State) const {
8520   if (!ByVal) {
8521     if (State.FreeRegs) {
8522       --State.FreeRegs; // Non-byval indirects just use one pointer.
8523       return getNaturalAlignIndirectInReg(Ty);
8524     }
8525     return getNaturalAlignIndirect(Ty, false);
8526   }
8527 
8528   // Compute the byval alignment.
8529   const unsigned MinABIStackAlignInBytes = 4;
8530   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8531   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8532                                  /*Realign=*/TypeAlign >
8533                                      MinABIStackAlignInBytes);
8534 }
8535 
8536 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
8537                                               CCState &State) const {
8538   // Check with the C++ ABI first.
8539   const RecordType *RT = Ty->getAs<RecordType>();
8540   if (RT) {
8541     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8542     if (RAA == CGCXXABI::RAA_Indirect) {
8543       return getIndirectResult(Ty, /*ByVal=*/false, State);
8544     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
8545       return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
8546     }
8547   }
8548 
8549   if (isAggregateTypeForABI(Ty)) {
8550     // Structures with flexible arrays are always indirect.
8551     if (RT && RT->getDecl()->hasFlexibleArrayMember())
8552       return getIndirectResult(Ty, /*ByVal=*/true, State);
8553 
8554     // Ignore empty structs/unions.
8555     if (isEmptyRecord(getContext(), Ty, true))
8556       return ABIArgInfo::getIgnore();
8557 
8558     llvm::LLVMContext &LLVMContext = getVMContext();
8559     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
8560     if (SizeInRegs <= State.FreeRegs) {
8561       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
8562       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
8563       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8564       State.FreeRegs -= SizeInRegs;
8565       return ABIArgInfo::getDirectInReg(Result);
8566     } else {
8567       State.FreeRegs = 0;
8568     }
8569     return getIndirectResult(Ty, true, State);
8570   }
8571 
8572   // Treat an enum type as its underlying type.
8573   if (const auto *EnumTy = Ty->getAs<EnumType>())
8574     Ty = EnumTy->getDecl()->getIntegerType();
8575 
8576   bool InReg = shouldUseInReg(Ty, State);
8577 
8578   // Don't pass >64 bit integers in registers.
8579   if (const auto *EIT = Ty->getAs<ExtIntType>())
8580     if (EIT->getNumBits() > 64)
8581       return getIndirectResult(Ty, /*ByVal=*/true, State);
8582 
8583   if (isPromotableIntegerTypeForABI(Ty)) {
8584     if (InReg)
8585       return ABIArgInfo::getDirectInReg();
8586     return ABIArgInfo::getExtend(Ty);
8587   }
8588   if (InReg)
8589     return ABIArgInfo::getDirectInReg();
8590   return ABIArgInfo::getDirect();
8591 }
8592 
8593 namespace {
8594 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
8595 public:
8596   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8597       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
8598 };
8599 }
8600 
8601 //===----------------------------------------------------------------------===//
8602 // AMDGPU ABI Implementation
8603 //===----------------------------------------------------------------------===//
8604 
8605 namespace {
8606 
8607 class AMDGPUABIInfo final : public DefaultABIInfo {
8608 private:
8609   static const unsigned MaxNumRegsForArgsRet = 16;
8610 
8611   unsigned numRegsForType(QualType Ty) const;
8612 
8613   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
8614   bool isHomogeneousAggregateSmallEnough(const Type *Base,
8615                                          uint64_t Members) const override;
8616 
8617   // Coerce HIP pointer arguments from generic pointers to global ones.
8618   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
8619                                        unsigned ToAS) const {
8620     // Structure types.
8621     if (auto STy = dyn_cast<llvm::StructType>(Ty)) {
8622       SmallVector<llvm::Type *, 8> EltTys;
8623       bool Changed = false;
8624       for (auto T : STy->elements()) {
8625         auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
8626         EltTys.push_back(NT);
8627         Changed |= (NT != T);
8628       }
8629       // Skip if there is no change in element types.
8630       if (!Changed)
8631         return STy;
8632       if (STy->hasName())
8633         return llvm::StructType::create(
8634             EltTys, (STy->getName() + ".coerce").str(), STy->isPacked());
8635       return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked());
8636     }
8637     // Array types.
8638     if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) {
8639       auto T = ATy->getElementType();
8640       auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
8641       // Skip if there is no change in that element type.
8642       if (NT == T)
8643         return ATy;
8644       return llvm::ArrayType::get(NT, ATy->getNumElements());
8645     }
8646     // Single value types.
8647     if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS)
8648       return llvm::PointerType::get(
8649           cast<llvm::PointerType>(Ty)->getElementType(), ToAS);
8650     return Ty;
8651   }
8652 
8653 public:
8654   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
8655     DefaultABIInfo(CGT) {}
8656 
8657   ABIArgInfo classifyReturnType(QualType RetTy) const;
8658   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
8659   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
8660 
8661   void computeInfo(CGFunctionInfo &FI) const override;
8662   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8663                     QualType Ty) const override;
8664 };
8665 
8666 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
8667   return true;
8668 }
8669 
8670 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
8671   const Type *Base, uint64_t Members) const {
8672   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
8673 
8674   // Homogeneous Aggregates may occupy at most 16 registers.
8675   return Members * NumRegs <= MaxNumRegsForArgsRet;
8676 }
8677 
8678 /// Estimate number of registers the type will use when passed in registers.
8679 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
8680   unsigned NumRegs = 0;
8681 
8682   if (const VectorType *VT = Ty->getAs<VectorType>()) {
8683     // Compute from the number of elements. The reported size is based on the
8684     // in-memory size, which includes the padding 4th element for 3-vectors.
8685     QualType EltTy = VT->getElementType();
8686     unsigned EltSize = getContext().getTypeSize(EltTy);
8687 
8688     // 16-bit element vectors should be passed as packed.
8689     if (EltSize == 16)
8690       return (VT->getNumElements() + 1) / 2;
8691 
8692     unsigned EltNumRegs = (EltSize + 31) / 32;
8693     return EltNumRegs * VT->getNumElements();
8694   }
8695 
8696   if (const RecordType *RT = Ty->getAs<RecordType>()) {
8697     const RecordDecl *RD = RT->getDecl();
8698     assert(!RD->hasFlexibleArrayMember());
8699 
8700     for (const FieldDecl *Field : RD->fields()) {
8701       QualType FieldTy = Field->getType();
8702       NumRegs += numRegsForType(FieldTy);
8703     }
8704 
8705     return NumRegs;
8706   }
8707 
8708   return (getContext().getTypeSize(Ty) + 31) / 32;
8709 }
8710 
8711 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
8712   llvm::CallingConv::ID CC = FI.getCallingConvention();
8713 
8714   if (!getCXXABI().classifyReturnType(FI))
8715     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8716 
8717   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
8718   for (auto &Arg : FI.arguments()) {
8719     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
8720       Arg.info = classifyKernelArgumentType(Arg.type);
8721     } else {
8722       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
8723     }
8724   }
8725 }
8726 
8727 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8728                                  QualType Ty) const {
8729   llvm_unreachable("AMDGPU does not support varargs");
8730 }
8731 
8732 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
8733   if (isAggregateTypeForABI(RetTy)) {
8734     // Records with non-trivial destructors/copy-constructors should not be
8735     // returned by value.
8736     if (!getRecordArgABI(RetTy, getCXXABI())) {
8737       // Ignore empty structs/unions.
8738       if (isEmptyRecord(getContext(), RetTy, true))
8739         return ABIArgInfo::getIgnore();
8740 
8741       // Lower single-element structs to just return a regular value.
8742       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
8743         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
8744 
8745       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
8746         const RecordDecl *RD = RT->getDecl();
8747         if (RD->hasFlexibleArrayMember())
8748           return DefaultABIInfo::classifyReturnType(RetTy);
8749       }
8750 
8751       // Pack aggregates <= 4 bytes into single VGPR or pair.
8752       uint64_t Size = getContext().getTypeSize(RetTy);
8753       if (Size <= 16)
8754         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
8755 
8756       if (Size <= 32)
8757         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
8758 
8759       if (Size <= 64) {
8760         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
8761         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
8762       }
8763 
8764       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
8765         return ABIArgInfo::getDirect();
8766     }
8767   }
8768 
8769   // Otherwise just do the default thing.
8770   return DefaultABIInfo::classifyReturnType(RetTy);
8771 }
8772 
8773 /// For kernels all parameters are really passed in a special buffer. It doesn't
8774 /// make sense to pass anything byval, so everything must be direct.
8775 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
8776   Ty = useFirstFieldIfTransparentUnion(Ty);
8777 
8778   // TODO: Can we omit empty structs?
8779 
8780   llvm::Type *LTy = nullptr;
8781   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
8782     LTy = CGT.ConvertType(QualType(SeltTy, 0));
8783 
8784   if (getContext().getLangOpts().HIP) {
8785     if (!LTy)
8786       LTy = CGT.ConvertType(Ty);
8787     LTy = coerceKernelArgumentType(
8788         LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
8789         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
8790   }
8791 
8792   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
8793   // individual elements, which confuses the Clover OpenCL backend; therefore we
8794   // have to set it to false here. Other args of getDirect() are just defaults.
8795   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
8796 }
8797 
8798 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
8799                                                unsigned &NumRegsLeft) const {
8800   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
8801 
8802   Ty = useFirstFieldIfTransparentUnion(Ty);
8803 
8804   if (isAggregateTypeForABI(Ty)) {
8805     // Records with non-trivial destructors/copy-constructors should not be
8806     // passed by value.
8807     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
8808       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8809 
8810     // Ignore empty structs/unions.
8811     if (isEmptyRecord(getContext(), Ty, true))
8812       return ABIArgInfo::getIgnore();
8813 
8814     // Lower single-element structs to just pass a regular value. TODO: We
8815     // could do reasonable-size multiple-element structs too, using getExpand(),
8816     // though watch out for things like bitfields.
8817     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
8818       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
8819 
8820     if (const RecordType *RT = Ty->getAs<RecordType>()) {
8821       const RecordDecl *RD = RT->getDecl();
8822       if (RD->hasFlexibleArrayMember())
8823         return DefaultABIInfo::classifyArgumentType(Ty);
8824     }
8825 
8826     // Pack aggregates <= 8 bytes into single VGPR or pair.
8827     uint64_t Size = getContext().getTypeSize(Ty);
8828     if (Size <= 64) {
8829       unsigned NumRegs = (Size + 31) / 32;
8830       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
8831 
8832       if (Size <= 16)
8833         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
8834 
8835       if (Size <= 32)
8836         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
8837 
8838       // XXX: Should this be i64 instead, and should the limit increase?
8839       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
8840       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
8841     }
8842 
8843     if (NumRegsLeft > 0) {
8844       unsigned NumRegs = numRegsForType(Ty);
8845       if (NumRegsLeft >= NumRegs) {
8846         NumRegsLeft -= NumRegs;
8847         return ABIArgInfo::getDirect();
8848       }
8849     }
8850   }
8851 
8852   // Otherwise just do the default thing.
8853   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
8854   if (!ArgInfo.isIndirect()) {
8855     unsigned NumRegs = numRegsForType(Ty);
8856     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
8857   }
8858 
8859   return ArgInfo;
8860 }
8861 
8862 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
8863 public:
8864   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
8865       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
8866   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8867                            CodeGen::CodeGenModule &M) const override;
8868   unsigned getOpenCLKernelCallingConv() const override;
8869 
8870   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
8871       llvm::PointerType *T, QualType QT) const override;
8872 
8873   LangAS getASTAllocaAddressSpace() const override {
8874     return getLangASFromTargetAS(
8875         getABIInfo().getDataLayout().getAllocaAddrSpace());
8876   }
8877   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8878                                   const VarDecl *D) const override;
8879   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
8880                                          SyncScope Scope,
8881                                          llvm::AtomicOrdering Ordering,
8882                                          llvm::LLVMContext &Ctx) const override;
8883   llvm::Function *
8884   createEnqueuedBlockKernel(CodeGenFunction &CGF,
8885                             llvm::Function *BlockInvokeFunc,
8886                             llvm::Value *BlockLiteral) const override;
8887   bool shouldEmitStaticExternCAliases() const override;
8888   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
8889 };
8890 }
8891 
8892 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
8893                                               llvm::GlobalValue *GV) {
8894   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
8895     return false;
8896 
8897   return D->hasAttr<OpenCLKernelAttr>() ||
8898          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
8899          (isa<VarDecl>(D) &&
8900           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
8901            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
8902            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
8903 }
8904 
8905 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
8906     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8907   if (requiresAMDGPUProtectedVisibility(D, GV)) {
8908     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
8909     GV->setDSOLocal(true);
8910   }
8911 
8912   if (GV->isDeclaration())
8913     return;
8914   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8915   if (!FD)
8916     return;
8917 
8918   llvm::Function *F = cast<llvm::Function>(GV);
8919 
8920   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
8921     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
8922 
8923 
8924   const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
8925                               FD->hasAttr<OpenCLKernelAttr>();
8926   const bool IsHIPKernel = M.getLangOpts().HIP &&
8927                            FD->hasAttr<CUDAGlobalAttr>();
8928   if ((IsOpenCLKernel || IsHIPKernel) &&
8929       (M.getTriple().getOS() == llvm::Triple::AMDHSA))
8930     F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
8931 
8932   if (IsHIPKernel)
8933     F->addFnAttr("uniform-work-group-size", "true");
8934 
8935 
8936   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
8937   if (ReqdWGS || FlatWGS) {
8938     unsigned Min = 0;
8939     unsigned Max = 0;
8940     if (FlatWGS) {
8941       Min = FlatWGS->getMin()
8942                 ->EvaluateKnownConstInt(M.getContext())
8943                 .getExtValue();
8944       Max = FlatWGS->getMax()
8945                 ->EvaluateKnownConstInt(M.getContext())
8946                 .getExtValue();
8947     }
8948     if (ReqdWGS && Min == 0 && Max == 0)
8949       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
8950 
8951     if (Min != 0) {
8952       assert(Min <= Max && "Min must be less than or equal Max");
8953 
8954       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
8955       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
8956     } else
8957       assert(Max == 0 && "Max must be zero");
8958   } else if (IsOpenCLKernel || IsHIPKernel) {
8959     // By default, restrict the maximum size to a value specified by
8960     // --gpu-max-threads-per-block=n or its default value for HIP.
8961     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
8962     const unsigned DefaultMaxWorkGroupSize =
8963         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
8964                        : M.getLangOpts().GPUMaxThreadsPerBlock;
8965     std::string AttrVal =
8966         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
8967     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
8968   }
8969 
8970   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
8971     unsigned Min =
8972         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
8973     unsigned Max = Attr->getMax() ? Attr->getMax()
8974                                         ->EvaluateKnownConstInt(M.getContext())
8975                                         .getExtValue()
8976                                   : 0;
8977 
8978     if (Min != 0) {
8979       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
8980 
8981       std::string AttrVal = llvm::utostr(Min);
8982       if (Max != 0)
8983         AttrVal = AttrVal + "," + llvm::utostr(Max);
8984       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
8985     } else
8986       assert(Max == 0 && "Max must be zero");
8987   }
8988 
8989   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
8990     unsigned NumSGPR = Attr->getNumSGPR();
8991 
8992     if (NumSGPR != 0)
8993       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
8994   }
8995 
8996   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
8997     uint32_t NumVGPR = Attr->getNumVGPR();
8998 
8999     if (NumVGPR != 0)
9000       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
9001   }
9002 }
9003 
9004 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9005   return llvm::CallingConv::AMDGPU_KERNEL;
9006 }
9007 
9008 // Currently LLVM assumes null pointers always have value 0,
9009 // which results in incorrectly transformed IR. Therefore, instead of
9010 // emitting null pointers in private and local address spaces, a null
9011 // pointer in generic address space is emitted which is casted to a
9012 // pointer in local or private address space.
9013 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
9014     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
9015     QualType QT) const {
9016   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
9017     return llvm::ConstantPointerNull::get(PT);
9018 
9019   auto &Ctx = CGM.getContext();
9020   auto NPT = llvm::PointerType::get(PT->getElementType(),
9021       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
9022   return llvm::ConstantExpr::getAddrSpaceCast(
9023       llvm::ConstantPointerNull::get(NPT), PT);
9024 }
9025 
9026 LangAS
9027 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
9028                                                   const VarDecl *D) const {
9029   assert(!CGM.getLangOpts().OpenCL &&
9030          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
9031          "Address space agnostic languages only");
9032   LangAS DefaultGlobalAS = getLangASFromTargetAS(
9033       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
9034   if (!D)
9035     return DefaultGlobalAS;
9036 
9037   LangAS AddrSpace = D->getType().getAddressSpace();
9038   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9039   if (AddrSpace != LangAS::Default)
9040     return AddrSpace;
9041 
9042   if (CGM.isTypeConstant(D->getType(), false)) {
9043     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9044       return ConstAS.getValue();
9045   }
9046   return DefaultGlobalAS;
9047 }
9048 
9049 llvm::SyncScope::ID
9050 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9051                                             SyncScope Scope,
9052                                             llvm::AtomicOrdering Ordering,
9053                                             llvm::LLVMContext &Ctx) const {
9054   std::string Name;
9055   switch (Scope) {
9056   case SyncScope::OpenCLWorkGroup:
9057     Name = "workgroup";
9058     break;
9059   case SyncScope::OpenCLDevice:
9060     Name = "agent";
9061     break;
9062   case SyncScope::OpenCLAllSVMDevices:
9063     Name = "";
9064     break;
9065   case SyncScope::OpenCLSubGroup:
9066     Name = "wavefront";
9067   }
9068 
9069   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9070     if (!Name.empty())
9071       Name = Twine(Twine(Name) + Twine("-")).str();
9072 
9073     Name = Twine(Twine(Name) + Twine("one-as")).str();
9074   }
9075 
9076   return Ctx.getOrInsertSyncScopeID(Name);
9077 }
9078 
9079 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9080   return false;
9081 }
9082 
9083 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9084     const FunctionType *&FT) const {
9085   FT = getABIInfo().getContext().adjustFunctionType(
9086       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9087 }
9088 
9089 //===----------------------------------------------------------------------===//
9090 // SPARC v8 ABI Implementation.
9091 // Based on the SPARC Compliance Definition version 2.4.1.
9092 //
9093 // Ensures that complex values are passed in registers.
9094 //
9095 namespace {
9096 class SparcV8ABIInfo : public DefaultABIInfo {
9097 public:
9098   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9099 
9100 private:
9101   ABIArgInfo classifyReturnType(QualType RetTy) const;
9102   void computeInfo(CGFunctionInfo &FI) const override;
9103 };
9104 } // end anonymous namespace
9105 
9106 
9107 ABIArgInfo
9108 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9109   if (Ty->isAnyComplexType()) {
9110     return ABIArgInfo::getDirect();
9111   }
9112   else {
9113     return DefaultABIInfo::classifyReturnType(Ty);
9114   }
9115 }
9116 
9117 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9118 
9119   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9120   for (auto &Arg : FI.arguments())
9121     Arg.info = classifyArgumentType(Arg.type);
9122 }
9123 
9124 namespace {
9125 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9126 public:
9127   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9128       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9129 };
9130 } // end anonymous namespace
9131 
9132 //===----------------------------------------------------------------------===//
9133 // SPARC v9 ABI Implementation.
9134 // Based on the SPARC Compliance Definition version 2.4.1.
9135 //
9136 // Function arguments a mapped to a nominal "parameter array" and promoted to
9137 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9138 // the array, structs larger than 16 bytes are passed indirectly.
9139 //
9140 // One case requires special care:
9141 //
9142 //   struct mixed {
9143 //     int i;
9144 //     float f;
9145 //   };
9146 //
9147 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9148 // parameter array, but the int is passed in an integer register, and the float
9149 // is passed in a floating point register. This is represented as two arguments
9150 // with the LLVM IR inreg attribute:
9151 //
9152 //   declare void f(i32 inreg %i, float inreg %f)
9153 //
9154 // The code generator will only allocate 4 bytes from the parameter array for
9155 // the inreg arguments. All other arguments are allocated a multiple of 8
9156 // bytes.
9157 //
9158 namespace {
9159 class SparcV9ABIInfo : public ABIInfo {
9160 public:
9161   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9162 
9163 private:
9164   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9165   void computeInfo(CGFunctionInfo &FI) const override;
9166   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9167                     QualType Ty) const override;
9168 
9169   // Coercion type builder for structs passed in registers. The coercion type
9170   // serves two purposes:
9171   //
9172   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9173   //    in registers.
9174   // 2. Expose aligned floating point elements as first-level elements, so the
9175   //    code generator knows to pass them in floating point registers.
9176   //
9177   // We also compute the InReg flag which indicates that the struct contains
9178   // aligned 32-bit floats.
9179   //
9180   struct CoerceBuilder {
9181     llvm::LLVMContext &Context;
9182     const llvm::DataLayout &DL;
9183     SmallVector<llvm::Type*, 8> Elems;
9184     uint64_t Size;
9185     bool InReg;
9186 
9187     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9188       : Context(c), DL(dl), Size(0), InReg(false) {}
9189 
9190     // Pad Elems with integers until Size is ToSize.
9191     void pad(uint64_t ToSize) {
9192       assert(ToSize >= Size && "Cannot remove elements");
9193       if (ToSize == Size)
9194         return;
9195 
9196       // Finish the current 64-bit word.
9197       uint64_t Aligned = llvm::alignTo(Size, 64);
9198       if (Aligned > Size && Aligned <= ToSize) {
9199         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9200         Size = Aligned;
9201       }
9202 
9203       // Add whole 64-bit words.
9204       while (Size + 64 <= ToSize) {
9205         Elems.push_back(llvm::Type::getInt64Ty(Context));
9206         Size += 64;
9207       }
9208 
9209       // Final in-word padding.
9210       if (Size < ToSize) {
9211         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9212         Size = ToSize;
9213       }
9214     }
9215 
9216     // Add a floating point element at Offset.
9217     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9218       // Unaligned floats are treated as integers.
9219       if (Offset % Bits)
9220         return;
9221       // The InReg flag is only required if there are any floats < 64 bits.
9222       if (Bits < 64)
9223         InReg = true;
9224       pad(Offset);
9225       Elems.push_back(Ty);
9226       Size = Offset + Bits;
9227     }
9228 
9229     // Add a struct type to the coercion type, starting at Offset (in bits).
9230     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9231       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9232       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9233         llvm::Type *ElemTy = StrTy->getElementType(i);
9234         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9235         switch (ElemTy->getTypeID()) {
9236         case llvm::Type::StructTyID:
9237           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9238           break;
9239         case llvm::Type::FloatTyID:
9240           addFloat(ElemOffset, ElemTy, 32);
9241           break;
9242         case llvm::Type::DoubleTyID:
9243           addFloat(ElemOffset, ElemTy, 64);
9244           break;
9245         case llvm::Type::FP128TyID:
9246           addFloat(ElemOffset, ElemTy, 128);
9247           break;
9248         case llvm::Type::PointerTyID:
9249           if (ElemOffset % 64 == 0) {
9250             pad(ElemOffset);
9251             Elems.push_back(ElemTy);
9252             Size += 64;
9253           }
9254           break;
9255         default:
9256           break;
9257         }
9258       }
9259     }
9260 
9261     // Check if Ty is a usable substitute for the coercion type.
9262     bool isUsableType(llvm::StructType *Ty) const {
9263       return llvm::makeArrayRef(Elems) == Ty->elements();
9264     }
9265 
9266     // Get the coercion type as a literal struct type.
9267     llvm::Type *getType() const {
9268       if (Elems.size() == 1)
9269         return Elems.front();
9270       else
9271         return llvm::StructType::get(Context, Elems);
9272     }
9273   };
9274 };
9275 } // end anonymous namespace
9276 
9277 ABIArgInfo
9278 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9279   if (Ty->isVoidType())
9280     return ABIArgInfo::getIgnore();
9281 
9282   uint64_t Size = getContext().getTypeSize(Ty);
9283 
9284   // Anything too big to fit in registers is passed with an explicit indirect
9285   // pointer / sret pointer.
9286   if (Size > SizeLimit)
9287     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9288 
9289   // Treat an enum type as its underlying type.
9290   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9291     Ty = EnumTy->getDecl()->getIntegerType();
9292 
9293   // Integer types smaller than a register are extended.
9294   if (Size < 64 && Ty->isIntegerType())
9295     return ABIArgInfo::getExtend(Ty);
9296 
9297   if (const auto *EIT = Ty->getAs<ExtIntType>())
9298     if (EIT->getNumBits() < 64)
9299       return ABIArgInfo::getExtend(Ty);
9300 
9301   // Other non-aggregates go in registers.
9302   if (!isAggregateTypeForABI(Ty))
9303     return ABIArgInfo::getDirect();
9304 
9305   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9306   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9307   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9308     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9309 
9310   // This is a small aggregate type that should be passed in registers.
9311   // Build a coercion type from the LLVM struct type.
9312   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9313   if (!StrTy)
9314     return ABIArgInfo::getDirect();
9315 
9316   CoerceBuilder CB(getVMContext(), getDataLayout());
9317   CB.addStruct(0, StrTy);
9318   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9319 
9320   // Try to use the original type for coercion.
9321   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9322 
9323   if (CB.InReg)
9324     return ABIArgInfo::getDirectInReg(CoerceTy);
9325   else
9326     return ABIArgInfo::getDirect(CoerceTy);
9327 }
9328 
9329 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9330                                   QualType Ty) const {
9331   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9332   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9333   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9334     AI.setCoerceToType(ArgTy);
9335 
9336   CharUnits SlotSize = CharUnits::fromQuantity(8);
9337 
9338   CGBuilderTy &Builder = CGF.Builder;
9339   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
9340   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9341 
9342   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9343 
9344   Address ArgAddr = Address::invalid();
9345   CharUnits Stride;
9346   switch (AI.getKind()) {
9347   case ABIArgInfo::Expand:
9348   case ABIArgInfo::CoerceAndExpand:
9349   case ABIArgInfo::InAlloca:
9350     llvm_unreachable("Unsupported ABI kind for va_arg");
9351 
9352   case ABIArgInfo::Extend: {
9353     Stride = SlotSize;
9354     CharUnits Offset = SlotSize - TypeInfo.first;
9355     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9356     break;
9357   }
9358 
9359   case ABIArgInfo::Direct: {
9360     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9361     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9362     ArgAddr = Addr;
9363     break;
9364   }
9365 
9366   case ABIArgInfo::Indirect:
9367     Stride = SlotSize;
9368     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9369     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
9370                       TypeInfo.second);
9371     break;
9372 
9373   case ABIArgInfo::Ignore:
9374     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
9375   }
9376 
9377   // Update VAList.
9378   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9379   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9380 
9381   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
9382 }
9383 
9384 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9385   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9386   for (auto &I : FI.arguments())
9387     I.info = classifyType(I.type, 16 * 8);
9388 }
9389 
9390 namespace {
9391 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9392 public:
9393   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9394       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9395 
9396   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9397     return 14;
9398   }
9399 
9400   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9401                                llvm::Value *Address) const override;
9402 };
9403 } // end anonymous namespace
9404 
9405 bool
9406 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9407                                                 llvm::Value *Address) const {
9408   // This is calculated from the LLVM and GCC tables and verified
9409   // against gcc output.  AFAIK all ABIs use the same encoding.
9410 
9411   CodeGen::CGBuilderTy &Builder = CGF.Builder;
9412 
9413   llvm::IntegerType *i8 = CGF.Int8Ty;
9414   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
9415   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
9416 
9417   // 0-31: the 8-byte general-purpose registers
9418   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
9419 
9420   // 32-63: f0-31, the 4-byte floating-point registers
9421   AssignToArrayRange(Builder, Address, Four8, 32, 63);
9422 
9423   //   Y   = 64
9424   //   PSR = 65
9425   //   WIM = 66
9426   //   TBR = 67
9427   //   PC  = 68
9428   //   NPC = 69
9429   //   FSR = 70
9430   //   CSR = 71
9431   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
9432 
9433   // 72-87: d0-15, the 8-byte floating-point registers
9434   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
9435 
9436   return false;
9437 }
9438 
9439 // ARC ABI implementation.
9440 namespace {
9441 
9442 class ARCABIInfo : public DefaultABIInfo {
9443 public:
9444   using DefaultABIInfo::DefaultABIInfo;
9445 
9446 private:
9447   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9448                     QualType Ty) const override;
9449 
9450   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
9451     if (!State.FreeRegs)
9452       return;
9453     if (Info.isIndirect() && Info.getInReg())
9454       State.FreeRegs--;
9455     else if (Info.isDirect() && Info.getInReg()) {
9456       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
9457       if (sz < State.FreeRegs)
9458         State.FreeRegs -= sz;
9459       else
9460         State.FreeRegs = 0;
9461     }
9462   }
9463 
9464   void computeInfo(CGFunctionInfo &FI) const override {
9465     CCState State(FI);
9466     // ARC uses 8 registers to pass arguments.
9467     State.FreeRegs = 8;
9468 
9469     if (!getCXXABI().classifyReturnType(FI))
9470       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9471     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
9472     for (auto &I : FI.arguments()) {
9473       I.info = classifyArgumentType(I.type, State.FreeRegs);
9474       updateState(I.info, I.type, State);
9475     }
9476   }
9477 
9478   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
9479   ABIArgInfo getIndirectByValue(QualType Ty) const;
9480   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
9481   ABIArgInfo classifyReturnType(QualType RetTy) const;
9482 };
9483 
9484 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
9485 public:
9486   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
9487       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
9488 };
9489 
9490 
9491 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
9492   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
9493                        getNaturalAlignIndirect(Ty, false);
9494 }
9495 
9496 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
9497   // Compute the byval alignment.
9498   const unsigned MinABIStackAlignInBytes = 4;
9499   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9500   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9501                                  TypeAlign > MinABIStackAlignInBytes);
9502 }
9503 
9504 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9505                               QualType Ty) const {
9506   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
9507                           getContext().getTypeInfoInChars(Ty),
9508                           CharUnits::fromQuantity(4), true);
9509 }
9510 
9511 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
9512                                             uint8_t FreeRegs) const {
9513   // Handle the generic C++ ABI.
9514   const RecordType *RT = Ty->getAs<RecordType>();
9515   if (RT) {
9516     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
9517     if (RAA == CGCXXABI::RAA_Indirect)
9518       return getIndirectByRef(Ty, FreeRegs > 0);
9519 
9520     if (RAA == CGCXXABI::RAA_DirectInMemory)
9521       return getIndirectByValue(Ty);
9522   }
9523 
9524   // Treat an enum type as its underlying type.
9525   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9526     Ty = EnumTy->getDecl()->getIntegerType();
9527 
9528   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
9529 
9530   if (isAggregateTypeForABI(Ty)) {
9531     // Structures with flexible arrays are always indirect.
9532     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9533       return getIndirectByValue(Ty);
9534 
9535     // Ignore empty structs/unions.
9536     if (isEmptyRecord(getContext(), Ty, true))
9537       return ABIArgInfo::getIgnore();
9538 
9539     llvm::LLVMContext &LLVMContext = getVMContext();
9540 
9541     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9542     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9543     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9544 
9545     return FreeRegs >= SizeInRegs ?
9546         ABIArgInfo::getDirectInReg(Result) :
9547         ABIArgInfo::getDirect(Result, 0, nullptr, false);
9548   }
9549 
9550   if (const auto *EIT = Ty->getAs<ExtIntType>())
9551     if (EIT->getNumBits() > 64)
9552       return getIndirectByValue(Ty);
9553 
9554   return isPromotableIntegerTypeForABI(Ty)
9555              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
9556                                        : ABIArgInfo::getExtend(Ty))
9557              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
9558                                        : ABIArgInfo::getDirect());
9559 }
9560 
9561 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
9562   if (RetTy->isAnyComplexType())
9563     return ABIArgInfo::getDirectInReg();
9564 
9565   // Arguments of size > 4 registers are indirect.
9566   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
9567   if (RetSize > 4)
9568     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
9569 
9570   return DefaultABIInfo::classifyReturnType(RetTy);
9571 }
9572 
9573 } // End anonymous namespace.
9574 
9575 //===----------------------------------------------------------------------===//
9576 // XCore ABI Implementation
9577 //===----------------------------------------------------------------------===//
9578 
9579 namespace {
9580 
9581 /// A SmallStringEnc instance is used to build up the TypeString by passing
9582 /// it by reference between functions that append to it.
9583 typedef llvm::SmallString<128> SmallStringEnc;
9584 
9585 /// TypeStringCache caches the meta encodings of Types.
9586 ///
9587 /// The reason for caching TypeStrings is two fold:
9588 ///   1. To cache a type's encoding for later uses;
9589 ///   2. As a means to break recursive member type inclusion.
9590 ///
9591 /// A cache Entry can have a Status of:
9592 ///   NonRecursive:   The type encoding is not recursive;
9593 ///   Recursive:      The type encoding is recursive;
9594 ///   Incomplete:     An incomplete TypeString;
9595 ///   IncompleteUsed: An incomplete TypeString that has been used in a
9596 ///                   Recursive type encoding.
9597 ///
9598 /// A NonRecursive entry will have all of its sub-members expanded as fully
9599 /// as possible. Whilst it may contain types which are recursive, the type
9600 /// itself is not recursive and thus its encoding may be safely used whenever
9601 /// the type is encountered.
9602 ///
9603 /// A Recursive entry will have all of its sub-members expanded as fully as
9604 /// possible. The type itself is recursive and it may contain other types which
9605 /// are recursive. The Recursive encoding must not be used during the expansion
9606 /// of a recursive type's recursive branch. For simplicity the code uses
9607 /// IncompleteCount to reject all usage of Recursive encodings for member types.
9608 ///
9609 /// An Incomplete entry is always a RecordType and only encodes its
9610 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
9611 /// are placed into the cache during type expansion as a means to identify and
9612 /// handle recursive inclusion of types as sub-members. If there is recursion
9613 /// the entry becomes IncompleteUsed.
9614 ///
9615 /// During the expansion of a RecordType's members:
9616 ///
9617 ///   If the cache contains a NonRecursive encoding for the member type, the
9618 ///   cached encoding is used;
9619 ///
9620 ///   If the cache contains a Recursive encoding for the member type, the
9621 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
9622 ///
9623 ///   If the member is a RecordType, an Incomplete encoding is placed into the
9624 ///   cache to break potential recursive inclusion of itself as a sub-member;
9625 ///
9626 ///   Once a member RecordType has been expanded, its temporary incomplete
9627 ///   entry is removed from the cache. If a Recursive encoding was swapped out
9628 ///   it is swapped back in;
9629 ///
9630 ///   If an incomplete entry is used to expand a sub-member, the incomplete
9631 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
9632 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
9633 ///
9634 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
9635 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
9636 ///   Else the member is part of a recursive type and thus the recursion has
9637 ///   been exited too soon for the encoding to be correct for the member.
9638 ///
9639 class TypeStringCache {
9640   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
9641   struct Entry {
9642     std::string Str;     // The encoded TypeString for the type.
9643     enum Status State;   // Information about the encoding in 'Str'.
9644     std::string Swapped; // A temporary place holder for a Recursive encoding
9645                          // during the expansion of RecordType's members.
9646   };
9647   std::map<const IdentifierInfo *, struct Entry> Map;
9648   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
9649   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
9650 public:
9651   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
9652   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
9653   bool removeIncomplete(const IdentifierInfo *ID);
9654   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
9655                      bool IsRecursive);
9656   StringRef lookupStr(const IdentifierInfo *ID);
9657 };
9658 
9659 /// TypeString encodings for enum & union fields must be order.
9660 /// FieldEncoding is a helper for this ordering process.
9661 class FieldEncoding {
9662   bool HasName;
9663   std::string Enc;
9664 public:
9665   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
9666   StringRef str() { return Enc; }
9667   bool operator<(const FieldEncoding &rhs) const {
9668     if (HasName != rhs.HasName) return HasName;
9669     return Enc < rhs.Enc;
9670   }
9671 };
9672 
9673 class XCoreABIInfo : public DefaultABIInfo {
9674 public:
9675   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9676   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9677                     QualType Ty) const override;
9678 };
9679 
9680 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
9681   mutable TypeStringCache TSC;
9682   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
9683                     const CodeGen::CodeGenModule &M) const;
9684 
9685 public:
9686   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
9687       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
9688   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
9689                           const llvm::MapVector<GlobalDecl, StringRef>
9690                               &MangledDeclNames) const override;
9691 };
9692 
9693 } // End anonymous namespace.
9694 
9695 // TODO: this implementation is likely now redundant with the default
9696 // EmitVAArg.
9697 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9698                                 QualType Ty) const {
9699   CGBuilderTy &Builder = CGF.Builder;
9700 
9701   // Get the VAList.
9702   CharUnits SlotSize = CharUnits::fromQuantity(4);
9703   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
9704 
9705   // Handle the argument.
9706   ABIArgInfo AI = classifyArgumentType(Ty);
9707   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
9708   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9709   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9710     AI.setCoerceToType(ArgTy);
9711   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9712 
9713   Address Val = Address::invalid();
9714   CharUnits ArgSize = CharUnits::Zero();
9715   switch (AI.getKind()) {
9716   case ABIArgInfo::Expand:
9717   case ABIArgInfo::CoerceAndExpand:
9718   case ABIArgInfo::InAlloca:
9719     llvm_unreachable("Unsupported ABI kind for va_arg");
9720   case ABIArgInfo::Ignore:
9721     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
9722     ArgSize = CharUnits::Zero();
9723     break;
9724   case ABIArgInfo::Extend:
9725   case ABIArgInfo::Direct:
9726     Val = Builder.CreateBitCast(AP, ArgPtrTy);
9727     ArgSize = CharUnits::fromQuantity(
9728                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
9729     ArgSize = ArgSize.alignTo(SlotSize);
9730     break;
9731   case ABIArgInfo::Indirect:
9732     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
9733     Val = Address(Builder.CreateLoad(Val), TypeAlign);
9734     ArgSize = SlotSize;
9735     break;
9736   }
9737 
9738   // Increment the VAList.
9739   if (!ArgSize.isZero()) {
9740     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
9741     Builder.CreateStore(APN.getPointer(), VAListAddr);
9742   }
9743 
9744   return Val;
9745 }
9746 
9747 /// During the expansion of a RecordType, an incomplete TypeString is placed
9748 /// into the cache as a means to identify and break recursion.
9749 /// If there is a Recursive encoding in the cache, it is swapped out and will
9750 /// be reinserted by removeIncomplete().
9751 /// All other types of encoding should have been used rather than arriving here.
9752 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
9753                                     std::string StubEnc) {
9754   if (!ID)
9755     return;
9756   Entry &E = Map[ID];
9757   assert( (E.Str.empty() || E.State == Recursive) &&
9758          "Incorrectly use of addIncomplete");
9759   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
9760   E.Swapped.swap(E.Str); // swap out the Recursive
9761   E.Str.swap(StubEnc);
9762   E.State = Incomplete;
9763   ++IncompleteCount;
9764 }
9765 
9766 /// Once the RecordType has been expanded, the temporary incomplete TypeString
9767 /// must be removed from the cache.
9768 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
9769 /// Returns true if the RecordType was defined recursively.
9770 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
9771   if (!ID)
9772     return false;
9773   auto I = Map.find(ID);
9774   assert(I != Map.end() && "Entry not present");
9775   Entry &E = I->second;
9776   assert( (E.State == Incomplete ||
9777            E.State == IncompleteUsed) &&
9778          "Entry must be an incomplete type");
9779   bool IsRecursive = false;
9780   if (E.State == IncompleteUsed) {
9781     // We made use of our Incomplete encoding, thus we are recursive.
9782     IsRecursive = true;
9783     --IncompleteUsedCount;
9784   }
9785   if (E.Swapped.empty())
9786     Map.erase(I);
9787   else {
9788     // Swap the Recursive back.
9789     E.Swapped.swap(E.Str);
9790     E.Swapped.clear();
9791     E.State = Recursive;
9792   }
9793   --IncompleteCount;
9794   return IsRecursive;
9795 }
9796 
9797 /// Add the encoded TypeString to the cache only if it is NonRecursive or
9798 /// Recursive (viz: all sub-members were expanded as fully as possible).
9799 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
9800                                     bool IsRecursive) {
9801   if (!ID || IncompleteUsedCount)
9802     return; // No key or it is is an incomplete sub-type so don't add.
9803   Entry &E = Map[ID];
9804   if (IsRecursive && !E.Str.empty()) {
9805     assert(E.State==Recursive && E.Str.size() == Str.size() &&
9806            "This is not the same Recursive entry");
9807     // The parent container was not recursive after all, so we could have used
9808     // this Recursive sub-member entry after all, but we assumed the worse when
9809     // we started viz: IncompleteCount!=0.
9810     return;
9811   }
9812   assert(E.Str.empty() && "Entry already present");
9813   E.Str = Str.str();
9814   E.State = IsRecursive? Recursive : NonRecursive;
9815 }
9816 
9817 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
9818 /// are recursively expanding a type (IncompleteCount != 0) and the cached
9819 /// encoding is Recursive, return an empty StringRef.
9820 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
9821   if (!ID)
9822     return StringRef();   // We have no key.
9823   auto I = Map.find(ID);
9824   if (I == Map.end())
9825     return StringRef();   // We have no encoding.
9826   Entry &E = I->second;
9827   if (E.State == Recursive && IncompleteCount)
9828     return StringRef();   // We don't use Recursive encodings for member types.
9829 
9830   if (E.State == Incomplete) {
9831     // The incomplete type is being used to break out of recursion.
9832     E.State = IncompleteUsed;
9833     ++IncompleteUsedCount;
9834   }
9835   return E.Str;
9836 }
9837 
9838 /// The XCore ABI includes a type information section that communicates symbol
9839 /// type information to the linker. The linker uses this information to verify
9840 /// safety/correctness of things such as array bound and pointers et al.
9841 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
9842 /// This type information (TypeString) is emitted into meta data for all global
9843 /// symbols: definitions, declarations, functions & variables.
9844 ///
9845 /// The TypeString carries type, qualifier, name, size & value details.
9846 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
9847 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
9848 /// The output is tested by test/CodeGen/xcore-stringtype.c.
9849 ///
9850 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
9851                           const CodeGen::CodeGenModule &CGM,
9852                           TypeStringCache &TSC);
9853 
9854 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
9855 void XCoreTargetCodeGenInfo::emitTargetMD(
9856     const Decl *D, llvm::GlobalValue *GV,
9857     const CodeGen::CodeGenModule &CGM) const {
9858   SmallStringEnc Enc;
9859   if (getTypeString(Enc, D, CGM, TSC)) {
9860     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
9861     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
9862                                 llvm::MDString::get(Ctx, Enc.str())};
9863     llvm::NamedMDNode *MD =
9864       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
9865     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
9866   }
9867 }
9868 
9869 void XCoreTargetCodeGenInfo::emitTargetMetadata(
9870     CodeGen::CodeGenModule &CGM,
9871     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
9872   // Warning, new MangledDeclNames may be appended within this loop.
9873   // We rely on MapVector insertions adding new elements to the end
9874   // of the container.
9875   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
9876     auto Val = *(MangledDeclNames.begin() + I);
9877     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
9878     if (GV) {
9879       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
9880       emitTargetMD(D, GV, CGM);
9881     }
9882   }
9883 }
9884 //===----------------------------------------------------------------------===//
9885 // SPIR ABI Implementation
9886 //===----------------------------------------------------------------------===//
9887 
9888 namespace {
9889 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
9890 public:
9891   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
9892       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
9893   unsigned getOpenCLKernelCallingConv() const override;
9894 };
9895 
9896 } // End anonymous namespace.
9897 
9898 namespace clang {
9899 namespace CodeGen {
9900 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
9901   DefaultABIInfo SPIRABI(CGM.getTypes());
9902   SPIRABI.computeInfo(FI);
9903 }
9904 }
9905 }
9906 
9907 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9908   return llvm::CallingConv::SPIR_KERNEL;
9909 }
9910 
9911 static bool appendType(SmallStringEnc &Enc, QualType QType,
9912                        const CodeGen::CodeGenModule &CGM,
9913                        TypeStringCache &TSC);
9914 
9915 /// Helper function for appendRecordType().
9916 /// Builds a SmallVector containing the encoded field types in declaration
9917 /// order.
9918 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
9919                              const RecordDecl *RD,
9920                              const CodeGen::CodeGenModule &CGM,
9921                              TypeStringCache &TSC) {
9922   for (const auto *Field : RD->fields()) {
9923     SmallStringEnc Enc;
9924     Enc += "m(";
9925     Enc += Field->getName();
9926     Enc += "){";
9927     if (Field->isBitField()) {
9928       Enc += "b(";
9929       llvm::raw_svector_ostream OS(Enc);
9930       OS << Field->getBitWidthValue(CGM.getContext());
9931       Enc += ':';
9932     }
9933     if (!appendType(Enc, Field->getType(), CGM, TSC))
9934       return false;
9935     if (Field->isBitField())
9936       Enc += ')';
9937     Enc += '}';
9938     FE.emplace_back(!Field->getName().empty(), Enc);
9939   }
9940   return true;
9941 }
9942 
9943 /// Appends structure and union types to Enc and adds encoding to cache.
9944 /// Recursively calls appendType (via extractFieldType) for each field.
9945 /// Union types have their fields ordered according to the ABI.
9946 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
9947                              const CodeGen::CodeGenModule &CGM,
9948                              TypeStringCache &TSC, const IdentifierInfo *ID) {
9949   // Append the cached TypeString if we have one.
9950   StringRef TypeString = TSC.lookupStr(ID);
9951   if (!TypeString.empty()) {
9952     Enc += TypeString;
9953     return true;
9954   }
9955 
9956   // Start to emit an incomplete TypeString.
9957   size_t Start = Enc.size();
9958   Enc += (RT->isUnionType()? 'u' : 's');
9959   Enc += '(';
9960   if (ID)
9961     Enc += ID->getName();
9962   Enc += "){";
9963 
9964   // We collect all encoded fields and order as necessary.
9965   bool IsRecursive = false;
9966   const RecordDecl *RD = RT->getDecl()->getDefinition();
9967   if (RD && !RD->field_empty()) {
9968     // An incomplete TypeString stub is placed in the cache for this RecordType
9969     // so that recursive calls to this RecordType will use it whilst building a
9970     // complete TypeString for this RecordType.
9971     SmallVector<FieldEncoding, 16> FE;
9972     std::string StubEnc(Enc.substr(Start).str());
9973     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
9974     TSC.addIncomplete(ID, std::move(StubEnc));
9975     if (!extractFieldType(FE, RD, CGM, TSC)) {
9976       (void) TSC.removeIncomplete(ID);
9977       return false;
9978     }
9979     IsRecursive = TSC.removeIncomplete(ID);
9980     // The ABI requires unions to be sorted but not structures.
9981     // See FieldEncoding::operator< for sort algorithm.
9982     if (RT->isUnionType())
9983       llvm::sort(FE);
9984     // We can now complete the TypeString.
9985     unsigned E = FE.size();
9986     for (unsigned I = 0; I != E; ++I) {
9987       if (I)
9988         Enc += ',';
9989       Enc += FE[I].str();
9990     }
9991   }
9992   Enc += '}';
9993   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
9994   return true;
9995 }
9996 
9997 /// Appends enum types to Enc and adds the encoding to the cache.
9998 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
9999                            TypeStringCache &TSC,
10000                            const IdentifierInfo *ID) {
10001   // Append the cached TypeString if we have one.
10002   StringRef TypeString = TSC.lookupStr(ID);
10003   if (!TypeString.empty()) {
10004     Enc += TypeString;
10005     return true;
10006   }
10007 
10008   size_t Start = Enc.size();
10009   Enc += "e(";
10010   if (ID)
10011     Enc += ID->getName();
10012   Enc += "){";
10013 
10014   // We collect all encoded enumerations and order them alphanumerically.
10015   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
10016     SmallVector<FieldEncoding, 16> FE;
10017     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
10018          ++I) {
10019       SmallStringEnc EnumEnc;
10020       EnumEnc += "m(";
10021       EnumEnc += I->getName();
10022       EnumEnc += "){";
10023       I->getInitVal().toString(EnumEnc);
10024       EnumEnc += '}';
10025       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
10026     }
10027     llvm::sort(FE);
10028     unsigned E = FE.size();
10029     for (unsigned I = 0; I != E; ++I) {
10030       if (I)
10031         Enc += ',';
10032       Enc += FE[I].str();
10033     }
10034   }
10035   Enc += '}';
10036   TSC.addIfComplete(ID, Enc.substr(Start), false);
10037   return true;
10038 }
10039 
10040 /// Appends type's qualifier to Enc.
10041 /// This is done prior to appending the type's encoding.
10042 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10043   // Qualifiers are emitted in alphabetical order.
10044   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10045   int Lookup = 0;
10046   if (QT.isConstQualified())
10047     Lookup += 1<<0;
10048   if (QT.isRestrictQualified())
10049     Lookup += 1<<1;
10050   if (QT.isVolatileQualified())
10051     Lookup += 1<<2;
10052   Enc += Table[Lookup];
10053 }
10054 
10055 /// Appends built-in types to Enc.
10056 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10057   const char *EncType;
10058   switch (BT->getKind()) {
10059     case BuiltinType::Void:
10060       EncType = "0";
10061       break;
10062     case BuiltinType::Bool:
10063       EncType = "b";
10064       break;
10065     case BuiltinType::Char_U:
10066       EncType = "uc";
10067       break;
10068     case BuiltinType::UChar:
10069       EncType = "uc";
10070       break;
10071     case BuiltinType::SChar:
10072       EncType = "sc";
10073       break;
10074     case BuiltinType::UShort:
10075       EncType = "us";
10076       break;
10077     case BuiltinType::Short:
10078       EncType = "ss";
10079       break;
10080     case BuiltinType::UInt:
10081       EncType = "ui";
10082       break;
10083     case BuiltinType::Int:
10084       EncType = "si";
10085       break;
10086     case BuiltinType::ULong:
10087       EncType = "ul";
10088       break;
10089     case BuiltinType::Long:
10090       EncType = "sl";
10091       break;
10092     case BuiltinType::ULongLong:
10093       EncType = "ull";
10094       break;
10095     case BuiltinType::LongLong:
10096       EncType = "sll";
10097       break;
10098     case BuiltinType::Float:
10099       EncType = "ft";
10100       break;
10101     case BuiltinType::Double:
10102       EncType = "d";
10103       break;
10104     case BuiltinType::LongDouble:
10105       EncType = "ld";
10106       break;
10107     default:
10108       return false;
10109   }
10110   Enc += EncType;
10111   return true;
10112 }
10113 
10114 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10115 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10116                               const CodeGen::CodeGenModule &CGM,
10117                               TypeStringCache &TSC) {
10118   Enc += "p(";
10119   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10120     return false;
10121   Enc += ')';
10122   return true;
10123 }
10124 
10125 /// Appends array encoding to Enc before calling appendType for the element.
10126 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10127                             const ArrayType *AT,
10128                             const CodeGen::CodeGenModule &CGM,
10129                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10130   if (AT->getSizeModifier() != ArrayType::Normal)
10131     return false;
10132   Enc += "a(";
10133   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10134     CAT->getSize().toStringUnsigned(Enc);
10135   else
10136     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10137   Enc += ':';
10138   // The Qualifiers should be attached to the type rather than the array.
10139   appendQualifier(Enc, QT);
10140   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10141     return false;
10142   Enc += ')';
10143   return true;
10144 }
10145 
10146 /// Appends a function encoding to Enc, calling appendType for the return type
10147 /// and the arguments.
10148 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10149                              const CodeGen::CodeGenModule &CGM,
10150                              TypeStringCache &TSC) {
10151   Enc += "f{";
10152   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10153     return false;
10154   Enc += "}(";
10155   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10156     // N.B. we are only interested in the adjusted param types.
10157     auto I = FPT->param_type_begin();
10158     auto E = FPT->param_type_end();
10159     if (I != E) {
10160       do {
10161         if (!appendType(Enc, *I, CGM, TSC))
10162           return false;
10163         ++I;
10164         if (I != E)
10165           Enc += ',';
10166       } while (I != E);
10167       if (FPT->isVariadic())
10168         Enc += ",va";
10169     } else {
10170       if (FPT->isVariadic())
10171         Enc += "va";
10172       else
10173         Enc += '0';
10174     }
10175   }
10176   Enc += ')';
10177   return true;
10178 }
10179 
10180 /// Handles the type's qualifier before dispatching a call to handle specific
10181 /// type encodings.
10182 static bool appendType(SmallStringEnc &Enc, QualType QType,
10183                        const CodeGen::CodeGenModule &CGM,
10184                        TypeStringCache &TSC) {
10185 
10186   QualType QT = QType.getCanonicalType();
10187 
10188   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10189     // The Qualifiers should be attached to the type rather than the array.
10190     // Thus we don't call appendQualifier() here.
10191     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10192 
10193   appendQualifier(Enc, QT);
10194 
10195   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10196     return appendBuiltinType(Enc, BT);
10197 
10198   if (const PointerType *PT = QT->getAs<PointerType>())
10199     return appendPointerType(Enc, PT, CGM, TSC);
10200 
10201   if (const EnumType *ET = QT->getAs<EnumType>())
10202     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10203 
10204   if (const RecordType *RT = QT->getAsStructureType())
10205     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10206 
10207   if (const RecordType *RT = QT->getAsUnionType())
10208     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10209 
10210   if (const FunctionType *FT = QT->getAs<FunctionType>())
10211     return appendFunctionType(Enc, FT, CGM, TSC);
10212 
10213   return false;
10214 }
10215 
10216 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10217                           const CodeGen::CodeGenModule &CGM,
10218                           TypeStringCache &TSC) {
10219   if (!D)
10220     return false;
10221 
10222   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10223     if (FD->getLanguageLinkage() != CLanguageLinkage)
10224       return false;
10225     return appendType(Enc, FD->getType(), CGM, TSC);
10226   }
10227 
10228   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10229     if (VD->getLanguageLinkage() != CLanguageLinkage)
10230       return false;
10231     QualType QT = VD->getType().getCanonicalType();
10232     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10233       // Global ArrayTypes are given a size of '*' if the size is unknown.
10234       // The Qualifiers should be attached to the type rather than the array.
10235       // Thus we don't call appendQualifier() here.
10236       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10237     }
10238     return appendType(Enc, QT, CGM, TSC);
10239   }
10240   return false;
10241 }
10242 
10243 //===----------------------------------------------------------------------===//
10244 // RISCV ABI Implementation
10245 //===----------------------------------------------------------------------===//
10246 
10247 namespace {
10248 class RISCVABIInfo : public DefaultABIInfo {
10249 private:
10250   // Size of the integer ('x') registers in bits.
10251   unsigned XLen;
10252   // Size of the floating point ('f') registers in bits. Note that the target
10253   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10254   // with soft float ABI has FLen==0).
10255   unsigned FLen;
10256   static const int NumArgGPRs = 8;
10257   static const int NumArgFPRs = 8;
10258   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10259                                       llvm::Type *&Field1Ty,
10260                                       CharUnits &Field1Off,
10261                                       llvm::Type *&Field2Ty,
10262                                       CharUnits &Field2Off) const;
10263 
10264 public:
10265   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10266       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10267 
10268   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10269   // non-virtual, but computeInfo is virtual, so we overload it.
10270   void computeInfo(CGFunctionInfo &FI) const override;
10271 
10272   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10273                                   int &ArgFPRsLeft) const;
10274   ABIArgInfo classifyReturnType(QualType RetTy) const;
10275 
10276   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10277                     QualType Ty) const override;
10278 
10279   ABIArgInfo extendType(QualType Ty) const;
10280 
10281   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10282                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10283                                 CharUnits &Field2Off, int &NeededArgGPRs,
10284                                 int &NeededArgFPRs) const;
10285   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10286                                                CharUnits Field1Off,
10287                                                llvm::Type *Field2Ty,
10288                                                CharUnits Field2Off) const;
10289 };
10290 } // end anonymous namespace
10291 
10292 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10293   QualType RetTy = FI.getReturnType();
10294   if (!getCXXABI().classifyReturnType(FI))
10295     FI.getReturnInfo() = classifyReturnType(RetTy);
10296 
10297   // IsRetIndirect is true if classifyArgumentType indicated the value should
10298   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10299   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10300   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10301   // list and pass indirectly on RV32.
10302   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
10303   if (!IsRetIndirect && RetTy->isScalarType() &&
10304       getContext().getTypeSize(RetTy) > (2 * XLen)) {
10305     if (RetTy->isComplexType() && FLen) {
10306       QualType EltTy = RetTy->getAs<ComplexType>()->getElementType();
10307       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
10308     } else {
10309       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
10310       IsRetIndirect = true;
10311     }
10312   }
10313 
10314   // We must track the number of GPRs used in order to conform to the RISC-V
10315   // ABI, as integer scalars passed in registers should have signext/zeroext
10316   // when promoted, but are anyext if passed on the stack. As GPR usage is
10317   // different for variadic arguments, we must also track whether we are
10318   // examining a vararg or not.
10319   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
10320   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
10321   int NumFixedArgs = FI.getNumRequiredArgs();
10322 
10323   int ArgNum = 0;
10324   for (auto &ArgInfo : FI.arguments()) {
10325     bool IsFixed = ArgNum < NumFixedArgs;
10326     ArgInfo.info =
10327         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
10328     ArgNum++;
10329   }
10330 }
10331 
10332 // Returns true if the struct is a potential candidate for the floating point
10333 // calling convention. If this function returns true, the caller is
10334 // responsible for checking that if there is only a single field then that
10335 // field is a float.
10336 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10337                                                   llvm::Type *&Field1Ty,
10338                                                   CharUnits &Field1Off,
10339                                                   llvm::Type *&Field2Ty,
10340                                                   CharUnits &Field2Off) const {
10341   bool IsInt = Ty->isIntegralOrEnumerationType();
10342   bool IsFloat = Ty->isRealFloatingType();
10343 
10344   if (IsInt || IsFloat) {
10345     uint64_t Size = getContext().getTypeSize(Ty);
10346     if (IsInt && Size > XLen)
10347       return false;
10348     // Can't be eligible if larger than the FP registers. Half precision isn't
10349     // currently supported on RISC-V and the ABI hasn't been confirmed, so
10350     // default to the integer ABI in that case.
10351     if (IsFloat && (Size > FLen || Size < 32))
10352       return false;
10353     // Can't be eligible if an integer type was already found (int+int pairs
10354     // are not eligible).
10355     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
10356       return false;
10357     if (!Field1Ty) {
10358       Field1Ty = CGT.ConvertType(Ty);
10359       Field1Off = CurOff;
10360       return true;
10361     }
10362     if (!Field2Ty) {
10363       Field2Ty = CGT.ConvertType(Ty);
10364       Field2Off = CurOff;
10365       return true;
10366     }
10367     return false;
10368   }
10369 
10370   if (auto CTy = Ty->getAs<ComplexType>()) {
10371     if (Field1Ty)
10372       return false;
10373     QualType EltTy = CTy->getElementType();
10374     if (getContext().getTypeSize(EltTy) > FLen)
10375       return false;
10376     Field1Ty = CGT.ConvertType(EltTy);
10377     Field1Off = CurOff;
10378     assert(CurOff.isZero() && "Unexpected offset for first field");
10379     Field2Ty = Field1Ty;
10380     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
10381     return true;
10382   }
10383 
10384   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
10385     uint64_t ArraySize = ATy->getSize().getZExtValue();
10386     QualType EltTy = ATy->getElementType();
10387     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
10388     for (uint64_t i = 0; i < ArraySize; ++i) {
10389       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
10390                                                 Field1Off, Field2Ty, Field2Off);
10391       if (!Ret)
10392         return false;
10393       CurOff += EltSize;
10394     }
10395     return true;
10396   }
10397 
10398   if (const auto *RTy = Ty->getAs<RecordType>()) {
10399     // Structures with either a non-trivial destructor or a non-trivial
10400     // copy constructor are not eligible for the FP calling convention.
10401     if (getRecordArgABI(Ty, CGT.getCXXABI()))
10402       return false;
10403     if (isEmptyRecord(getContext(), Ty, true))
10404       return true;
10405     const RecordDecl *RD = RTy->getDecl();
10406     // Unions aren't eligible unless they're empty (which is caught above).
10407     if (RD->isUnion())
10408       return false;
10409     int ZeroWidthBitFieldCount = 0;
10410     for (const FieldDecl *FD : RD->fields()) {
10411       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
10412       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
10413       QualType QTy = FD->getType();
10414       if (FD->isBitField()) {
10415         unsigned BitWidth = FD->getBitWidthValue(getContext());
10416         // Allow a bitfield with a type greater than XLen as long as the
10417         // bitwidth is XLen or less.
10418         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
10419           QTy = getContext().getIntTypeForBitwidth(XLen, false);
10420         if (BitWidth == 0) {
10421           ZeroWidthBitFieldCount++;
10422           continue;
10423         }
10424       }
10425 
10426       bool Ret = detectFPCCEligibleStructHelper(
10427           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
10428           Field1Ty, Field1Off, Field2Ty, Field2Off);
10429       if (!Ret)
10430         return false;
10431 
10432       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
10433       // or int+fp structs, but are ignored for a struct with an fp field and
10434       // any number of zero-width bitfields.
10435       if (Field2Ty && ZeroWidthBitFieldCount > 0)
10436         return false;
10437     }
10438     return Field1Ty != nullptr;
10439   }
10440 
10441   return false;
10442 }
10443 
10444 // Determine if a struct is eligible for passing according to the floating
10445 // point calling convention (i.e., when flattened it contains a single fp
10446 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
10447 // NeededArgGPRs are incremented appropriately.
10448 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10449                                             CharUnits &Field1Off,
10450                                             llvm::Type *&Field2Ty,
10451                                             CharUnits &Field2Off,
10452                                             int &NeededArgGPRs,
10453                                             int &NeededArgFPRs) const {
10454   Field1Ty = nullptr;
10455   Field2Ty = nullptr;
10456   NeededArgGPRs = 0;
10457   NeededArgFPRs = 0;
10458   bool IsCandidate = detectFPCCEligibleStructHelper(
10459       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
10460   // Not really a candidate if we have a single int but no float.
10461   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
10462     return false;
10463   if (!IsCandidate)
10464     return false;
10465   if (Field1Ty && Field1Ty->isFloatingPointTy())
10466     NeededArgFPRs++;
10467   else if (Field1Ty)
10468     NeededArgGPRs++;
10469   if (Field2Ty && Field2Ty->isFloatingPointTy())
10470     NeededArgFPRs++;
10471   else if (Field2Ty)
10472     NeededArgGPRs++;
10473   return IsCandidate;
10474 }
10475 
10476 // Call getCoerceAndExpand for the two-element flattened struct described by
10477 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
10478 // appropriate coerceToType and unpaddedCoerceToType.
10479 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
10480     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
10481     CharUnits Field2Off) const {
10482   SmallVector<llvm::Type *, 3> CoerceElts;
10483   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
10484   if (!Field1Off.isZero())
10485     CoerceElts.push_back(llvm::ArrayType::get(
10486         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
10487 
10488   CoerceElts.push_back(Field1Ty);
10489   UnpaddedCoerceElts.push_back(Field1Ty);
10490 
10491   if (!Field2Ty) {
10492     return ABIArgInfo::getCoerceAndExpand(
10493         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
10494         UnpaddedCoerceElts[0]);
10495   }
10496 
10497   CharUnits Field2Align =
10498       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
10499   CharUnits Field1Size =
10500       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
10501   CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align);
10502 
10503   CharUnits Padding = CharUnits::Zero();
10504   if (Field2Off > Field2OffNoPadNoPack)
10505     Padding = Field2Off - Field2OffNoPadNoPack;
10506   else if (Field2Off != Field2Align && Field2Off > Field1Size)
10507     Padding = Field2Off - Field1Size;
10508 
10509   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
10510 
10511   if (!Padding.isZero())
10512     CoerceElts.push_back(llvm::ArrayType::get(
10513         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
10514 
10515   CoerceElts.push_back(Field2Ty);
10516   UnpaddedCoerceElts.push_back(Field2Ty);
10517 
10518   auto CoerceToType =
10519       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
10520   auto UnpaddedCoerceToType =
10521       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
10522 
10523   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
10524 }
10525 
10526 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
10527                                               int &ArgGPRsLeft,
10528                                               int &ArgFPRsLeft) const {
10529   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
10530   Ty = useFirstFieldIfTransparentUnion(Ty);
10531 
10532   // Structures with either a non-trivial destructor or a non-trivial
10533   // copy constructor are always passed indirectly.
10534   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
10535     if (ArgGPRsLeft)
10536       ArgGPRsLeft -= 1;
10537     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
10538                                            CGCXXABI::RAA_DirectInMemory);
10539   }
10540 
10541   // Ignore empty structs/unions.
10542   if (isEmptyRecord(getContext(), Ty, true))
10543     return ABIArgInfo::getIgnore();
10544 
10545   uint64_t Size = getContext().getTypeSize(Ty);
10546 
10547   // Pass floating point values via FPRs if possible.
10548   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
10549       FLen >= Size && ArgFPRsLeft) {
10550     ArgFPRsLeft--;
10551     return ABIArgInfo::getDirect();
10552   }
10553 
10554   // Complex types for the hard float ABI must be passed direct rather than
10555   // using CoerceAndExpand.
10556   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
10557     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
10558     if (getContext().getTypeSize(EltTy) <= FLen) {
10559       ArgFPRsLeft -= 2;
10560       return ABIArgInfo::getDirect();
10561     }
10562   }
10563 
10564   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
10565     llvm::Type *Field1Ty = nullptr;
10566     llvm::Type *Field2Ty = nullptr;
10567     CharUnits Field1Off = CharUnits::Zero();
10568     CharUnits Field2Off = CharUnits::Zero();
10569     int NeededArgGPRs;
10570     int NeededArgFPRs;
10571     bool IsCandidate =
10572         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
10573                                  NeededArgGPRs, NeededArgFPRs);
10574     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
10575         NeededArgFPRs <= ArgFPRsLeft) {
10576       ArgGPRsLeft -= NeededArgGPRs;
10577       ArgFPRsLeft -= NeededArgFPRs;
10578       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
10579                                                Field2Off);
10580     }
10581   }
10582 
10583   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
10584   bool MustUseStack = false;
10585   // Determine the number of GPRs needed to pass the current argument
10586   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
10587   // register pairs, so may consume 3 registers.
10588   int NeededArgGPRs = 1;
10589   if (!IsFixed && NeededAlign == 2 * XLen)
10590     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
10591   else if (Size > XLen && Size <= 2 * XLen)
10592     NeededArgGPRs = 2;
10593 
10594   if (NeededArgGPRs > ArgGPRsLeft) {
10595     MustUseStack = true;
10596     NeededArgGPRs = ArgGPRsLeft;
10597   }
10598 
10599   ArgGPRsLeft -= NeededArgGPRs;
10600 
10601   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
10602     // Treat an enum type as its underlying type.
10603     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10604       Ty = EnumTy->getDecl()->getIntegerType();
10605 
10606     // All integral types are promoted to XLen width, unless passed on the
10607     // stack.
10608     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
10609       return extendType(Ty);
10610     }
10611 
10612     if (const auto *EIT = Ty->getAs<ExtIntType>()) {
10613       if (EIT->getNumBits() < XLen && !MustUseStack)
10614         return extendType(Ty);
10615       if (EIT->getNumBits() > 128 ||
10616           (!getContext().getTargetInfo().hasInt128Type() &&
10617            EIT->getNumBits() > 64))
10618         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10619     }
10620 
10621     return ABIArgInfo::getDirect();
10622   }
10623 
10624   // Aggregates which are <= 2*XLen will be passed in registers if possible,
10625   // so coerce to integers.
10626   if (Size <= 2 * XLen) {
10627     unsigned Alignment = getContext().getTypeAlign(Ty);
10628 
10629     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
10630     // required, and a 2-element XLen array if only XLen alignment is required.
10631     if (Size <= XLen) {
10632       return ABIArgInfo::getDirect(
10633           llvm::IntegerType::get(getVMContext(), XLen));
10634     } else if (Alignment == 2 * XLen) {
10635       return ABIArgInfo::getDirect(
10636           llvm::IntegerType::get(getVMContext(), 2 * XLen));
10637     } else {
10638       return ABIArgInfo::getDirect(llvm::ArrayType::get(
10639           llvm::IntegerType::get(getVMContext(), XLen), 2));
10640     }
10641   }
10642   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
10643 }
10644 
10645 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
10646   if (RetTy->isVoidType())
10647     return ABIArgInfo::getIgnore();
10648 
10649   int ArgGPRsLeft = 2;
10650   int ArgFPRsLeft = FLen ? 2 : 0;
10651 
10652   // The rules for return and argument types are the same, so defer to
10653   // classifyArgumentType.
10654   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
10655                               ArgFPRsLeft);
10656 }
10657 
10658 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10659                                 QualType Ty) const {
10660   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
10661 
10662   // Empty records are ignored for parameter passing purposes.
10663   if (isEmptyRecord(getContext(), Ty, true)) {
10664     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
10665     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
10666     return Addr;
10667   }
10668 
10669   std::pair<CharUnits, CharUnits> SizeAndAlign =
10670       getContext().getTypeInfoInChars(Ty);
10671 
10672   // Arguments bigger than 2*Xlen bytes are passed indirectly.
10673   bool IsIndirect = SizeAndAlign.first > 2 * SlotSize;
10674 
10675   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign,
10676                           SlotSize, /*AllowHigherAlign=*/true);
10677 }
10678 
10679 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
10680   int TySize = getContext().getTypeSize(Ty);
10681   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
10682   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
10683     return ABIArgInfo::getSignExtend(Ty);
10684   return ABIArgInfo::getExtend(Ty);
10685 }
10686 
10687 namespace {
10688 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
10689 public:
10690   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
10691                          unsigned FLen)
10692       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
10693 
10694   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
10695                            CodeGen::CodeGenModule &CGM) const override {
10696     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
10697     if (!FD) return;
10698 
10699     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
10700     if (!Attr)
10701       return;
10702 
10703     const char *Kind;
10704     switch (Attr->getInterrupt()) {
10705     case RISCVInterruptAttr::user: Kind = "user"; break;
10706     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
10707     case RISCVInterruptAttr::machine: Kind = "machine"; break;
10708     }
10709 
10710     auto *Fn = cast<llvm::Function>(GV);
10711 
10712     Fn->addFnAttr("interrupt", Kind);
10713   }
10714 };
10715 } // namespace
10716 
10717 //===----------------------------------------------------------------------===//
10718 // VE ABI Implementation.
10719 //
10720 namespace {
10721 class VEABIInfo : public DefaultABIInfo {
10722 public:
10723   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
10724 
10725 private:
10726   ABIArgInfo classifyReturnType(QualType RetTy) const;
10727   ABIArgInfo classifyArgumentType(QualType RetTy) const;
10728   void computeInfo(CGFunctionInfo &FI) const override;
10729 };
10730 } // end anonymous namespace
10731 
10732 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
10733   if (Ty->isAnyComplexType()) {
10734     return ABIArgInfo::getDirect();
10735   }
10736   return DefaultABIInfo::classifyReturnType(Ty);
10737 }
10738 
10739 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
10740   if (Ty->isAnyComplexType()) {
10741     return ABIArgInfo::getDirect();
10742   }
10743   return DefaultABIInfo::classifyArgumentType(Ty);
10744 }
10745 
10746 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
10747 
10748   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10749   for (auto &Arg : FI.arguments())
10750     Arg.info = classifyArgumentType(Arg.type);
10751 }
10752 
10753 namespace {
10754 class VETargetCodeGenInfo : public TargetCodeGenInfo {
10755 public:
10756   VETargetCodeGenInfo(CodeGenTypes &CGT)
10757       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
10758   // VE ABI requires the arguments of variadic and prototype-less functions
10759   // are passed in both registers and memory.
10760   bool isNoProtoCallVariadic(const CallArgList &args,
10761                              const FunctionNoProtoType *fnType) const override {
10762     return true;
10763   }
10764 };
10765 } // end anonymous namespace
10766 
10767 //===----------------------------------------------------------------------===//
10768 // Driver code
10769 //===----------------------------------------------------------------------===//
10770 
10771 bool CodeGenModule::supportsCOMDAT() const {
10772   return getTriple().supportsCOMDAT();
10773 }
10774 
10775 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
10776   if (TheTargetCodeGenInfo)
10777     return *TheTargetCodeGenInfo;
10778 
10779   // Helper to set the unique_ptr while still keeping the return value.
10780   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
10781     this->TheTargetCodeGenInfo.reset(P);
10782     return *P;
10783   };
10784 
10785   const llvm::Triple &Triple = getTarget().getTriple();
10786   switch (Triple.getArch()) {
10787   default:
10788     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
10789 
10790   case llvm::Triple::le32:
10791     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
10792   case llvm::Triple::mips:
10793   case llvm::Triple::mipsel:
10794     if (Triple.getOS() == llvm::Triple::NaCl)
10795       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
10796     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
10797 
10798   case llvm::Triple::mips64:
10799   case llvm::Triple::mips64el:
10800     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
10801 
10802   case llvm::Triple::avr:
10803     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
10804 
10805   case llvm::Triple::aarch64:
10806   case llvm::Triple::aarch64_32:
10807   case llvm::Triple::aarch64_be: {
10808     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
10809     if (getTarget().getABI() == "darwinpcs")
10810       Kind = AArch64ABIInfo::DarwinPCS;
10811     else if (Triple.isOSWindows())
10812       return SetCGInfo(
10813           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
10814 
10815     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
10816   }
10817 
10818   case llvm::Triple::wasm32:
10819   case llvm::Triple::wasm64: {
10820     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
10821     if (getTarget().getABI() == "experimental-mv")
10822       Kind = WebAssemblyABIInfo::ExperimentalMV;
10823     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
10824   }
10825 
10826   case llvm::Triple::arm:
10827   case llvm::Triple::armeb:
10828   case llvm::Triple::thumb:
10829   case llvm::Triple::thumbeb: {
10830     if (Triple.getOS() == llvm::Triple::Win32) {
10831       return SetCGInfo(
10832           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
10833     }
10834 
10835     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
10836     StringRef ABIStr = getTarget().getABI();
10837     if (ABIStr == "apcs-gnu")
10838       Kind = ARMABIInfo::APCS;
10839     else if (ABIStr == "aapcs16")
10840       Kind = ARMABIInfo::AAPCS16_VFP;
10841     else if (CodeGenOpts.FloatABI == "hard" ||
10842              (CodeGenOpts.FloatABI != "soft" &&
10843               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
10844                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
10845                Triple.getEnvironment() == llvm::Triple::EABIHF)))
10846       Kind = ARMABIInfo::AAPCS_VFP;
10847 
10848     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
10849   }
10850 
10851   case llvm::Triple::ppc: {
10852     if (Triple.isOSAIX())
10853       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
10854 
10855     bool IsSoftFloat =
10856         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
10857     bool RetSmallStructInRegABI =
10858         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
10859     return SetCGInfo(
10860         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
10861   }
10862   case llvm::Triple::ppc64:
10863     if (Triple.isOSAIX())
10864       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
10865 
10866     if (Triple.isOSBinFormatELF()) {
10867       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
10868       if (getTarget().getABI() == "elfv2")
10869         Kind = PPC64_SVR4_ABIInfo::ELFv2;
10870       bool HasQPX = getTarget().getABI() == "elfv1-qpx";
10871       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
10872 
10873       return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
10874                                                         IsSoftFloat));
10875     }
10876     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
10877   case llvm::Triple::ppc64le: {
10878     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
10879     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
10880     if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
10881       Kind = PPC64_SVR4_ABIInfo::ELFv1;
10882     bool HasQPX = getTarget().getABI() == "elfv1-qpx";
10883     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
10884 
10885     return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
10886                                                       IsSoftFloat));
10887   }
10888 
10889   case llvm::Triple::nvptx:
10890   case llvm::Triple::nvptx64:
10891     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
10892 
10893   case llvm::Triple::msp430:
10894     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
10895 
10896   case llvm::Triple::riscv32:
10897   case llvm::Triple::riscv64: {
10898     StringRef ABIStr = getTarget().getABI();
10899     unsigned XLen = getTarget().getPointerWidth(0);
10900     unsigned ABIFLen = 0;
10901     if (ABIStr.endswith("f"))
10902       ABIFLen = 32;
10903     else if (ABIStr.endswith("d"))
10904       ABIFLen = 64;
10905     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
10906   }
10907 
10908   case llvm::Triple::systemz: {
10909     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
10910     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
10911     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
10912   }
10913 
10914   case llvm::Triple::tce:
10915   case llvm::Triple::tcele:
10916     return SetCGInfo(new TCETargetCodeGenInfo(Types));
10917 
10918   case llvm::Triple::x86: {
10919     bool IsDarwinVectorABI = Triple.isOSDarwin();
10920     bool RetSmallStructInRegABI =
10921         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
10922     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
10923 
10924     if (Triple.getOS() == llvm::Triple::Win32) {
10925       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
10926           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
10927           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
10928     } else {
10929       return SetCGInfo(new X86_32TargetCodeGenInfo(
10930           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
10931           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
10932           CodeGenOpts.FloatABI == "soft"));
10933     }
10934   }
10935 
10936   case llvm::Triple::x86_64: {
10937     StringRef ABI = getTarget().getABI();
10938     X86AVXABILevel AVXLevel =
10939         (ABI == "avx512"
10940              ? X86AVXABILevel::AVX512
10941              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
10942 
10943     switch (Triple.getOS()) {
10944     case llvm::Triple::Win32:
10945       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
10946     default:
10947       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
10948     }
10949   }
10950   case llvm::Triple::hexagon:
10951     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
10952   case llvm::Triple::lanai:
10953     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
10954   case llvm::Triple::r600:
10955     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
10956   case llvm::Triple::amdgcn:
10957     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
10958   case llvm::Triple::sparc:
10959     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
10960   case llvm::Triple::sparcv9:
10961     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
10962   case llvm::Triple::xcore:
10963     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
10964   case llvm::Triple::arc:
10965     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
10966   case llvm::Triple::spir:
10967   case llvm::Triple::spir64:
10968     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
10969   case llvm::Triple::ve:
10970     return SetCGInfo(new VETargetCodeGenInfo(Types));
10971   }
10972 }
10973 
10974 /// Create an OpenCL kernel for an enqueued block.
10975 ///
10976 /// The kernel has the same function type as the block invoke function. Its
10977 /// name is the name of the block invoke function postfixed with "_kernel".
10978 /// It simply calls the block invoke function then returns.
10979 llvm::Function *
10980 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
10981                                              llvm::Function *Invoke,
10982                                              llvm::Value *BlockLiteral) const {
10983   auto *InvokeFT = Invoke->getFunctionType();
10984   llvm::SmallVector<llvm::Type *, 2> ArgTys;
10985   for (auto &P : InvokeFT->params())
10986     ArgTys.push_back(P);
10987   auto &C = CGF.getLLVMContext();
10988   std::string Name = Invoke->getName().str() + "_kernel";
10989   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
10990   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
10991                                    &CGF.CGM.getModule());
10992   auto IP = CGF.Builder.saveIP();
10993   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
10994   auto &Builder = CGF.Builder;
10995   Builder.SetInsertPoint(BB);
10996   llvm::SmallVector<llvm::Value *, 2> Args;
10997   for (auto &A : F->args())
10998     Args.push_back(&A);
10999   Builder.CreateCall(Invoke, Args);
11000   Builder.CreateRetVoid();
11001   Builder.restoreIP(IP);
11002   return F;
11003 }
11004 
11005 /// Create an OpenCL kernel for an enqueued block.
11006 ///
11007 /// The type of the first argument (the block literal) is the struct type
11008 /// of the block literal instead of a pointer type. The first argument
11009 /// (block literal) is passed directly by value to the kernel. The kernel
11010 /// allocates the same type of struct on stack and stores the block literal
11011 /// to it and passes its pointer to the block invoke function. The kernel
11012 /// has "enqueued-block" function attribute and kernel argument metadata.
11013 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
11014     CodeGenFunction &CGF, llvm::Function *Invoke,
11015     llvm::Value *BlockLiteral) const {
11016   auto &Builder = CGF.Builder;
11017   auto &C = CGF.getLLVMContext();
11018 
11019   auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
11020   auto *InvokeFT = Invoke->getFunctionType();
11021   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11022   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
11023   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
11024   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
11025   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
11026   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
11027   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
11028 
11029   ArgTys.push_back(BlockTy);
11030   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11031   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
11032   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11033   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11034   AccessQuals.push_back(llvm::MDString::get(C, "none"));
11035   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
11036   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
11037     ArgTys.push_back(InvokeFT->getParamType(I));
11038     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
11039     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
11040     AccessQuals.push_back(llvm::MDString::get(C, "none"));
11041     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
11042     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11043     ArgNames.push_back(
11044         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
11045   }
11046   std::string Name = Invoke->getName().str() + "_kernel";
11047   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11048   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11049                                    &CGF.CGM.getModule());
11050   F->addFnAttr("enqueued-block");
11051   auto IP = CGF.Builder.saveIP();
11052   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11053   Builder.SetInsertPoint(BB);
11054   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
11055   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
11056   BlockPtr->setAlignment(BlockAlign);
11057   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
11058   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
11059   llvm::SmallVector<llvm::Value *, 2> Args;
11060   Args.push_back(Cast);
11061   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
11062     Args.push_back(I);
11063   Builder.CreateCall(Invoke, Args);
11064   Builder.CreateRetVoid();
11065   Builder.restoreIP(IP);
11066 
11067   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
11068   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
11069   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
11070   F->setMetadata("kernel_arg_base_type",
11071                  llvm::MDNode::get(C, ArgBaseTypeNames));
11072   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
11073   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
11074     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
11075 
11076   return F;
11077 }
11078