xref: /freebsd-src/contrib/llvm-project/clang/lib/CodeGen/TargetInfo.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
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/Builtins.h"
23 #include "clang/Basic/CodeGenOptions.h"
24 #include "clang/Basic/DiagnosticFrontend.h"
25 #include "clang/CodeGen/CGFunctionInfo.h"
26 #include "clang/CodeGen/SwiftCallingConv.h"
27 #include "llvm/ADT/SmallBitVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/IntrinsicsNVPTX.h"
34 #include "llvm/IR/IntrinsicsS390.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <algorithm> // std::sort
39 
40 using namespace clang;
41 using namespace CodeGen;
42 
43 // Helper for coercing an aggregate argument or return value into an integer
44 // array of the same size (including padding) and alignment.  This alternate
45 // coercion happens only for the RenderScript ABI and can be removed after
46 // runtimes that rely on it are no longer supported.
47 //
48 // RenderScript assumes that the size of the argument / return value in the IR
49 // is the same as the size of the corresponding qualified type. This helper
50 // coerces the aggregate type into an array of the same size (including
51 // padding).  This coercion is used in lieu of expansion of struct members or
52 // other canonical coercions that return a coerced-type of larger size.
53 //
54 // Ty          - The argument / return value type
55 // Context     - The associated ASTContext
56 // LLVMContext - The associated LLVMContext
57 static ABIArgInfo coerceToIntArray(QualType Ty,
58                                    ASTContext &Context,
59                                    llvm::LLVMContext &LLVMContext) {
60   // Alignment and Size are measured in bits.
61   const uint64_t Size = Context.getTypeSize(Ty);
62   const uint64_t Alignment = Context.getTypeAlign(Ty);
63   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
64   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
65   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
66 }
67 
68 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
69                                llvm::Value *Array,
70                                llvm::Value *Value,
71                                unsigned FirstIndex,
72                                unsigned LastIndex) {
73   // Alternatively, we could emit this as a loop in the source.
74   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
75     llvm::Value *Cell =
76         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
77     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
78   }
79 }
80 
81 static bool isAggregateTypeForABI(QualType T) {
82   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
83          T->isMemberFunctionPointerType();
84 }
85 
86 ABIArgInfo ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByVal,
87                                             bool Realign,
88                                             llvm::Type *Padding) const {
89   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), ByVal,
90                                  Realign, Padding);
91 }
92 
93 ABIArgInfo
94 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
95   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
96                                       /*ByVal*/ false, Realign);
97 }
98 
99 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
100                              QualType Ty) const {
101   return Address::invalid();
102 }
103 
104 static llvm::Type *getVAListElementType(CodeGenFunction &CGF) {
105   return CGF.ConvertTypeForMem(
106       CGF.getContext().getBuiltinVaListType()->getPointeeType());
107 }
108 
109 bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
110   if (Ty->isPromotableIntegerType())
111     return true;
112 
113   if (const auto *EIT = Ty->getAs<BitIntType>())
114     if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
115       return true;
116 
117   return false;
118 }
119 
120 ABIInfo::~ABIInfo() {}
121 
122 /// Does the given lowering require more than the given number of
123 /// registers when expanded?
124 ///
125 /// This is intended to be the basis of a reasonable basic implementation
126 /// of should{Pass,Return}IndirectlyForSwift.
127 ///
128 /// For most targets, a limit of four total registers is reasonable; this
129 /// limits the amount of code required in order to move around the value
130 /// in case it wasn't produced immediately prior to the call by the caller
131 /// (or wasn't produced in exactly the right registers) or isn't used
132 /// immediately within the callee.  But some targets may need to further
133 /// limit the register count due to an inability to support that many
134 /// return registers.
135 static bool occupiesMoreThan(CodeGenTypes &cgt,
136                              ArrayRef<llvm::Type*> scalarTypes,
137                              unsigned maxAllRegisters) {
138   unsigned intCount = 0, fpCount = 0;
139   for (llvm::Type *type : scalarTypes) {
140     if (type->isPointerTy()) {
141       intCount++;
142     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
143       auto ptrWidth = cgt.getTarget().getPointerWidth(0);
144       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
145     } else {
146       assert(type->isVectorTy() || type->isFloatingPointTy());
147       fpCount++;
148     }
149   }
150 
151   return (intCount + fpCount > maxAllRegisters);
152 }
153 
154 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
155                                              llvm::Type *eltTy,
156                                              unsigned numElts) const {
157   // The default implementation of this assumes that the target guarantees
158   // 128-bit SIMD support but nothing more.
159   return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
160 }
161 
162 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
163                                               CGCXXABI &CXXABI) {
164   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
165   if (!RD) {
166     if (!RT->getDecl()->canPassInRegisters())
167       return CGCXXABI::RAA_Indirect;
168     return CGCXXABI::RAA_Default;
169   }
170   return CXXABI.getRecordArgABI(RD);
171 }
172 
173 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
174                                               CGCXXABI &CXXABI) {
175   const RecordType *RT = T->getAs<RecordType>();
176   if (!RT)
177     return CGCXXABI::RAA_Default;
178   return getRecordArgABI(RT, CXXABI);
179 }
180 
181 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
182                                const ABIInfo &Info) {
183   QualType Ty = FI.getReturnType();
184 
185   if (const auto *RT = Ty->getAs<RecordType>())
186     if (!isa<CXXRecordDecl>(RT->getDecl()) &&
187         !RT->getDecl()->canPassInRegisters()) {
188       FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
189       return true;
190     }
191 
192   return CXXABI.classifyReturnType(FI);
193 }
194 
195 /// Pass transparent unions as if they were the type of the first element. Sema
196 /// should ensure that all elements of the union have the same "machine type".
197 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
198   if (const RecordType *UT = Ty->getAsUnionType()) {
199     const RecordDecl *UD = UT->getDecl();
200     if (UD->hasAttr<TransparentUnionAttr>()) {
201       assert(!UD->field_empty() && "sema created an empty transparent union");
202       return UD->field_begin()->getType();
203     }
204   }
205   return Ty;
206 }
207 
208 CGCXXABI &ABIInfo::getCXXABI() const {
209   return CGT.getCXXABI();
210 }
211 
212 ASTContext &ABIInfo::getContext() const {
213   return CGT.getContext();
214 }
215 
216 llvm::LLVMContext &ABIInfo::getVMContext() const {
217   return CGT.getLLVMContext();
218 }
219 
220 const llvm::DataLayout &ABIInfo::getDataLayout() const {
221   return CGT.getDataLayout();
222 }
223 
224 const TargetInfo &ABIInfo::getTarget() const {
225   return CGT.getTarget();
226 }
227 
228 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
229   return CGT.getCodeGenOpts();
230 }
231 
232 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
233 
234 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
235   return false;
236 }
237 
238 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
239                                                 uint64_t Members) const {
240   return false;
241 }
242 
243 bool ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
244   // For compatibility with GCC, ignore empty bitfields in C++ mode.
245   return getContext().getLangOpts().CPlusPlus;
246 }
247 
248 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
249   raw_ostream &OS = llvm::errs();
250   OS << "(ABIArgInfo Kind=";
251   switch (TheKind) {
252   case Direct:
253     OS << "Direct Type=";
254     if (llvm::Type *Ty = getCoerceToType())
255       Ty->print(OS);
256     else
257       OS << "null";
258     break;
259   case Extend:
260     OS << "Extend";
261     break;
262   case Ignore:
263     OS << "Ignore";
264     break;
265   case InAlloca:
266     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
267     break;
268   case Indirect:
269     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
270        << " ByVal=" << getIndirectByVal()
271        << " Realign=" << getIndirectRealign();
272     break;
273   case IndirectAliased:
274     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
275        << " AadrSpace=" << getIndirectAddrSpace()
276        << " Realign=" << getIndirectRealign();
277     break;
278   case Expand:
279     OS << "Expand";
280     break;
281   case CoerceAndExpand:
282     OS << "CoerceAndExpand Type=";
283     getCoerceAndExpandType()->print(OS);
284     break;
285   }
286   OS << ")\n";
287 }
288 
289 // Dynamically round a pointer up to a multiple of the given alignment.
290 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
291                                                   llvm::Value *Ptr,
292                                                   CharUnits Align) {
293   llvm::Value *PtrAsInt = Ptr;
294   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
295   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
296   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
297         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
298   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
299            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
300   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
301                                         Ptr->getType(),
302                                         Ptr->getName() + ".aligned");
303   return PtrAsInt;
304 }
305 
306 /// Emit va_arg for a platform using the common void* representation,
307 /// where arguments are simply emitted in an array of slots on the stack.
308 ///
309 /// This version implements the core direct-value passing rules.
310 ///
311 /// \param SlotSize - The size and alignment of a stack slot.
312 ///   Each argument will be allocated to a multiple of this number of
313 ///   slots, and all the slots will be aligned to this value.
314 /// \param AllowHigherAlign - The slot alignment is not a cap;
315 ///   an argument type with an alignment greater than the slot size
316 ///   will be emitted on a higher-alignment address, potentially
317 ///   leaving one or more empty slots behind as padding.  If this
318 ///   is false, the returned address might be less-aligned than
319 ///   DirectAlign.
320 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
321                                       Address VAListAddr,
322                                       llvm::Type *DirectTy,
323                                       CharUnits DirectSize,
324                                       CharUnits DirectAlign,
325                                       CharUnits SlotSize,
326                                       bool AllowHigherAlign) {
327   // Cast the element type to i8* if necessary.  Some platforms define
328   // va_list as a struct containing an i8* instead of just an i8*.
329   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
330     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
331 
332   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
333 
334   // If the CC aligns values higher than the slot size, do so if needed.
335   Address Addr = Address::invalid();
336   if (AllowHigherAlign && DirectAlign > SlotSize) {
337     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
338                    CGF.Int8Ty, DirectAlign);
339   } else {
340     Addr = Address(Ptr, CGF.Int8Ty, SlotSize);
341   }
342 
343   // Advance the pointer past the argument, then store that back.
344   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
345   Address NextPtr =
346       CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
347   CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
348 
349   // If the argument is smaller than a slot, and this is a big-endian
350   // target, the argument will be right-adjusted in its slot.
351   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
352       !DirectTy->isStructTy()) {
353     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
354   }
355 
356   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
357   return Addr;
358 }
359 
360 /// Emit va_arg for a platform using the common void* representation,
361 /// where arguments are simply emitted in an array of slots on the stack.
362 ///
363 /// \param IsIndirect - Values of this type are passed indirectly.
364 /// \param ValueInfo - The size and alignment of this type, generally
365 ///   computed with getContext().getTypeInfoInChars(ValueTy).
366 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
367 ///   Each argument will be allocated to a multiple of this number of
368 ///   slots, and all the slots will be aligned to this value.
369 /// \param AllowHigherAlign - The slot alignment is not a cap;
370 ///   an argument type with an alignment greater than the slot size
371 ///   will be emitted on a higher-alignment address, potentially
372 ///   leaving one or more empty slots behind as padding.
373 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
374                                 QualType ValueTy, bool IsIndirect,
375                                 TypeInfoChars ValueInfo,
376                                 CharUnits SlotSizeAndAlign,
377                                 bool AllowHigherAlign) {
378   // The size and alignment of the value that was passed directly.
379   CharUnits DirectSize, DirectAlign;
380   if (IsIndirect) {
381     DirectSize = CGF.getPointerSize();
382     DirectAlign = CGF.getPointerAlign();
383   } else {
384     DirectSize = ValueInfo.Width;
385     DirectAlign = ValueInfo.Align;
386   }
387 
388   // Cast the address we've calculated to the right type.
389   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy), *ElementTy = DirectTy;
390   if (IsIndirect)
391     DirectTy = DirectTy->getPointerTo(0);
392 
393   Address Addr =
394       emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, DirectSize, DirectAlign,
395                              SlotSizeAndAlign, AllowHigherAlign);
396 
397   if (IsIndirect) {
398     Addr = Address(CGF.Builder.CreateLoad(Addr), ElementTy, ValueInfo.Align);
399   }
400 
401   return Addr;
402 }
403 
404 static Address complexTempStructure(CodeGenFunction &CGF, Address VAListAddr,
405                                     QualType Ty, CharUnits SlotSize,
406                                     CharUnits EltSize, const ComplexType *CTy) {
407   Address Addr =
408       emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, SlotSize * 2,
409                              SlotSize, SlotSize, /*AllowHigher*/ true);
410 
411   Address RealAddr = Addr;
412   Address ImagAddr = RealAddr;
413   if (CGF.CGM.getDataLayout().isBigEndian()) {
414     RealAddr =
415         CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize - EltSize);
416     ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
417                                                       2 * SlotSize - EltSize);
418   } else {
419     ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
420   }
421 
422   llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
423   RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
424   ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
425   llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
426   llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
427 
428   Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
429   CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
430                          /*init*/ true);
431   return Temp;
432 }
433 
434 static Address emitMergePHI(CodeGenFunction &CGF,
435                             Address Addr1, llvm::BasicBlock *Block1,
436                             Address Addr2, llvm::BasicBlock *Block2,
437                             const llvm::Twine &Name = "") {
438   assert(Addr1.getType() == Addr2.getType());
439   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
440   PHI->addIncoming(Addr1.getPointer(), Block1);
441   PHI->addIncoming(Addr2.getPointer(), Block2);
442   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
443   return Address(PHI, Addr1.getElementType(), Align);
444 }
445 
446 TargetCodeGenInfo::~TargetCodeGenInfo() = default;
447 
448 // If someone can figure out a general rule for this, that would be great.
449 // It's probably just doomed to be platform-dependent, though.
450 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
451   // Verified for:
452   //   x86-64     FreeBSD, Linux, Darwin
453   //   x86-32     FreeBSD, Linux, Darwin
454   //   PowerPC    Linux, Darwin
455   //   ARM        Darwin (*not* EABI)
456   //   AArch64    Linux
457   return 32;
458 }
459 
460 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
461                                      const FunctionNoProtoType *fnType) const {
462   // The following conventions are known to require this to be false:
463   //   x86_stdcall
464   //   MIPS
465   // For everything else, we just prefer false unless we opt out.
466   return false;
467 }
468 
469 void
470 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
471                                              llvm::SmallString<24> &Opt) const {
472   // This assumes the user is passing a library name like "rt" instead of a
473   // filename like "librt.a/so", and that they don't care whether it's static or
474   // dynamic.
475   Opt = "-l";
476   Opt += Lib;
477 }
478 
479 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
480   // OpenCL kernels are called via an explicit runtime API with arguments
481   // set with clSetKernelArg(), not as normal sub-functions.
482   // Return SPIR_KERNEL by default as the kernel calling convention to
483   // ensure the fingerprint is fixed such way that each OpenCL argument
484   // gets one matching argument in the produced kernel function argument
485   // list to enable feasible implementation of clSetKernelArg() with
486   // aggregates etc. In case we would use the default C calling conv here,
487   // clSetKernelArg() might break depending on the target-specific
488   // conventions; different targets might split structs passed as values
489   // to multiple function arguments etc.
490   return llvm::CallingConv::SPIR_KERNEL;
491 }
492 
493 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
494     llvm::PointerType *T, QualType QT) const {
495   return llvm::ConstantPointerNull::get(T);
496 }
497 
498 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
499                                                    const VarDecl *D) const {
500   assert(!CGM.getLangOpts().OpenCL &&
501          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
502          "Address space agnostic languages only");
503   return D ? D->getType().getAddressSpace() : LangAS::Default;
504 }
505 
506 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
507     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
508     LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
509   // Since target may map different address spaces in AST to the same address
510   // space, an address space conversion may end up as a bitcast.
511   if (auto *C = dyn_cast<llvm::Constant>(Src))
512     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
513   // Try to preserve the source's name to make IR more readable.
514   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
515       Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
516 }
517 
518 llvm::Constant *
519 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
520                                         LangAS SrcAddr, LangAS DestAddr,
521                                         llvm::Type *DestTy) const {
522   // Since target may map different address spaces in AST to the same address
523   // space, an address space conversion may end up as a bitcast.
524   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
525 }
526 
527 llvm::SyncScope::ID
528 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
529                                       SyncScope Scope,
530                                       llvm::AtomicOrdering Ordering,
531                                       llvm::LLVMContext &Ctx) const {
532   return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
533 }
534 
535 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
536 
537 /// isEmptyField - Return true iff a the field is "empty", that is it
538 /// is an unnamed bit-field or an (array of) empty record(s).
539 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
540                          bool AllowArrays) {
541   if (FD->isUnnamedBitfield())
542     return true;
543 
544   QualType FT = FD->getType();
545 
546   // Constant arrays of empty records count as empty, strip them off.
547   // Constant arrays of zero length always count as empty.
548   bool WasArray = false;
549   if (AllowArrays)
550     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
551       if (AT->getSize() == 0)
552         return true;
553       FT = AT->getElementType();
554       // The [[no_unique_address]] special case below does not apply to
555       // arrays of C++ empty records, so we need to remember this fact.
556       WasArray = true;
557     }
558 
559   const RecordType *RT = FT->getAs<RecordType>();
560   if (!RT)
561     return false;
562 
563   // C++ record fields are never empty, at least in the Itanium ABI.
564   //
565   // FIXME: We should use a predicate for whether this behavior is true in the
566   // current ABI.
567   //
568   // The exception to the above rule are fields marked with the
569   // [[no_unique_address]] attribute (since C++20).  Those do count as empty
570   // according to the Itanium ABI.  The exception applies only to records,
571   // not arrays of records, so we must also check whether we stripped off an
572   // array type above.
573   if (isa<CXXRecordDecl>(RT->getDecl()) &&
574       (WasArray || !FD->hasAttr<NoUniqueAddressAttr>()))
575     return false;
576 
577   return isEmptyRecord(Context, FT, AllowArrays);
578 }
579 
580 /// isEmptyRecord - Return true iff a structure contains only empty
581 /// fields. Note that a structure with a flexible array member is not
582 /// considered empty.
583 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
584   const RecordType *RT = T->getAs<RecordType>();
585   if (!RT)
586     return false;
587   const RecordDecl *RD = RT->getDecl();
588   if (RD->hasFlexibleArrayMember())
589     return false;
590 
591   // If this is a C++ record, check the bases first.
592   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
593     for (const auto &I : CXXRD->bases())
594       if (!isEmptyRecord(Context, I.getType(), true))
595         return false;
596 
597   for (const auto *I : RD->fields())
598     if (!isEmptyField(Context, I, AllowArrays))
599       return false;
600   return true;
601 }
602 
603 /// isSingleElementStruct - Determine if a structure is a "single
604 /// element struct", i.e. it has exactly one non-empty field or
605 /// exactly one field which is itself a single element
606 /// struct. Structures with flexible array members are never
607 /// considered single element structs.
608 ///
609 /// \return The field declaration for the single non-empty field, if
610 /// it exists.
611 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
612   const RecordType *RT = T->getAs<RecordType>();
613   if (!RT)
614     return nullptr;
615 
616   const RecordDecl *RD = RT->getDecl();
617   if (RD->hasFlexibleArrayMember())
618     return nullptr;
619 
620   const Type *Found = nullptr;
621 
622   // If this is a C++ record, check the bases first.
623   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
624     for (const auto &I : CXXRD->bases()) {
625       // Ignore empty records.
626       if (isEmptyRecord(Context, I.getType(), true))
627         continue;
628 
629       // If we already found an element then this isn't a single-element struct.
630       if (Found)
631         return nullptr;
632 
633       // If this is non-empty and not a single element struct, the composite
634       // cannot be a single element struct.
635       Found = isSingleElementStruct(I.getType(), Context);
636       if (!Found)
637         return nullptr;
638     }
639   }
640 
641   // Check for single element.
642   for (const auto *FD : RD->fields()) {
643     QualType FT = FD->getType();
644 
645     // Ignore empty fields.
646     if (isEmptyField(Context, FD, true))
647       continue;
648 
649     // If we already found an element then this isn't a single-element
650     // struct.
651     if (Found)
652       return nullptr;
653 
654     // Treat single element arrays as the element.
655     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
656       if (AT->getSize().getZExtValue() != 1)
657         break;
658       FT = AT->getElementType();
659     }
660 
661     if (!isAggregateTypeForABI(FT)) {
662       Found = FT.getTypePtr();
663     } else {
664       Found = isSingleElementStruct(FT, Context);
665       if (!Found)
666         return nullptr;
667     }
668   }
669 
670   // We don't consider a struct a single-element struct if it has
671   // padding beyond the element type.
672   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
673     return nullptr;
674 
675   return Found;
676 }
677 
678 namespace {
679 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
680                        const ABIArgInfo &AI) {
681   // This default implementation defers to the llvm backend's va_arg
682   // instruction. It can handle only passing arguments directly
683   // (typically only handled in the backend for primitive types), or
684   // aggregates passed indirectly by pointer (NOTE: if the "byval"
685   // flag has ABI impact in the callee, this implementation cannot
686   // work.)
687 
688   // Only a few cases are covered here at the moment -- those needed
689   // by the default abi.
690   llvm::Value *Val;
691 
692   if (AI.isIndirect()) {
693     assert(!AI.getPaddingType() &&
694            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
695     assert(
696         !AI.getIndirectRealign() &&
697         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
698 
699     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
700     CharUnits TyAlignForABI = TyInfo.Align;
701 
702     llvm::Type *ElementTy = CGF.ConvertTypeForMem(Ty);
703     llvm::Type *BaseTy = llvm::PointerType::getUnqual(ElementTy);
704     llvm::Value *Addr =
705         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
706     return Address(Addr, ElementTy, TyAlignForABI);
707   } else {
708     assert((AI.isDirect() || AI.isExtend()) &&
709            "Unexpected ArgInfo Kind in generic VAArg emitter!");
710 
711     assert(!AI.getInReg() &&
712            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
713     assert(!AI.getPaddingType() &&
714            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
715     assert(!AI.getDirectOffset() &&
716            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
717     assert(!AI.getCoerceToType() &&
718            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
719 
720     Address Temp = CGF.CreateMemTemp(Ty, "varet");
721     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(),
722                                   CGF.ConvertTypeForMem(Ty));
723     CGF.Builder.CreateStore(Val, Temp);
724     return Temp;
725   }
726 }
727 
728 /// DefaultABIInfo - The default implementation for ABI specific
729 /// details. This implementation provides information which results in
730 /// self-consistent and sensible LLVM IR generation, but does not
731 /// conform to any particular ABI.
732 class DefaultABIInfo : public ABIInfo {
733 public:
734   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
735 
736   ABIArgInfo classifyReturnType(QualType RetTy) const;
737   ABIArgInfo classifyArgumentType(QualType RetTy) const;
738 
739   void computeInfo(CGFunctionInfo &FI) const override {
740     if (!getCXXABI().classifyReturnType(FI))
741       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
742     for (auto &I : FI.arguments())
743       I.info = classifyArgumentType(I.type);
744   }
745 
746   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
747                     QualType Ty) const override {
748     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
749   }
750 };
751 
752 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
753 public:
754   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
755       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
756 };
757 
758 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
759   Ty = useFirstFieldIfTransparentUnion(Ty);
760 
761   if (isAggregateTypeForABI(Ty)) {
762     // Records with non-trivial destructors/copy-constructors should not be
763     // passed by value.
764     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
765       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
766 
767     return getNaturalAlignIndirect(Ty);
768   }
769 
770   // Treat an enum type as its underlying type.
771   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
772     Ty = EnumTy->getDecl()->getIntegerType();
773 
774   ASTContext &Context = getContext();
775   if (const auto *EIT = Ty->getAs<BitIntType>())
776     if (EIT->getNumBits() >
777         Context.getTypeSize(Context.getTargetInfo().hasInt128Type()
778                                 ? Context.Int128Ty
779                                 : Context.LongLongTy))
780       return getNaturalAlignIndirect(Ty);
781 
782   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
783                                             : ABIArgInfo::getDirect());
784 }
785 
786 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
787   if (RetTy->isVoidType())
788     return ABIArgInfo::getIgnore();
789 
790   if (isAggregateTypeForABI(RetTy))
791     return getNaturalAlignIndirect(RetTy);
792 
793   // Treat an enum type as its underlying type.
794   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
795     RetTy = EnumTy->getDecl()->getIntegerType();
796 
797   if (const auto *EIT = RetTy->getAs<BitIntType>())
798     if (EIT->getNumBits() >
799         getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
800                                      ? getContext().Int128Ty
801                                      : getContext().LongLongTy))
802       return getNaturalAlignIndirect(RetTy);
803 
804   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
805                                                : ABIArgInfo::getDirect());
806 }
807 
808 //===----------------------------------------------------------------------===//
809 // WebAssembly ABI Implementation
810 //
811 // This is a very simple ABI that relies a lot on DefaultABIInfo.
812 //===----------------------------------------------------------------------===//
813 
814 class WebAssemblyABIInfo final : public SwiftABIInfo {
815 public:
816   enum ABIKind {
817     MVP = 0,
818     ExperimentalMV = 1,
819   };
820 
821 private:
822   DefaultABIInfo defaultInfo;
823   ABIKind Kind;
824 
825 public:
826   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
827       : SwiftABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {}
828 
829 private:
830   ABIArgInfo classifyReturnType(QualType RetTy) const;
831   ABIArgInfo classifyArgumentType(QualType Ty) const;
832 
833   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
834   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
835   // overload them.
836   void computeInfo(CGFunctionInfo &FI) const override {
837     if (!getCXXABI().classifyReturnType(FI))
838       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
839     for (auto &Arg : FI.arguments())
840       Arg.info = classifyArgumentType(Arg.type);
841   }
842 
843   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
844                     QualType Ty) const override;
845 
846   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
847                                     bool asReturnValue) const override {
848     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
849   }
850 
851   bool isSwiftErrorInRegister() const override {
852     return false;
853   }
854 };
855 
856 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
857 public:
858   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
859                                         WebAssemblyABIInfo::ABIKind K)
860       : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {}
861 
862   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
863                            CodeGen::CodeGenModule &CGM) const override {
864     TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
865     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
866       if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
867         llvm::Function *Fn = cast<llvm::Function>(GV);
868         llvm::AttrBuilder B(GV->getContext());
869         B.addAttribute("wasm-import-module", Attr->getImportModule());
870         Fn->addFnAttrs(B);
871       }
872       if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
873         llvm::Function *Fn = cast<llvm::Function>(GV);
874         llvm::AttrBuilder B(GV->getContext());
875         B.addAttribute("wasm-import-name", Attr->getImportName());
876         Fn->addFnAttrs(B);
877       }
878       if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
879         llvm::Function *Fn = cast<llvm::Function>(GV);
880         llvm::AttrBuilder B(GV->getContext());
881         B.addAttribute("wasm-export-name", Attr->getExportName());
882         Fn->addFnAttrs(B);
883       }
884     }
885 
886     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
887       llvm::Function *Fn = cast<llvm::Function>(GV);
888       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
889         Fn->addFnAttr("no-prototype");
890     }
891   }
892 };
893 
894 /// Classify argument of given type \p Ty.
895 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
896   Ty = useFirstFieldIfTransparentUnion(Ty);
897 
898   if (isAggregateTypeForABI(Ty)) {
899     // Records with non-trivial destructors/copy-constructors should not be
900     // passed by value.
901     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
902       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
903     // Ignore empty structs/unions.
904     if (isEmptyRecord(getContext(), Ty, true))
905       return ABIArgInfo::getIgnore();
906     // Lower single-element structs to just pass a regular value. TODO: We
907     // could do reasonable-size multiple-element structs too, using getExpand(),
908     // though watch out for things like bitfields.
909     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
910       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
911     // For the experimental multivalue ABI, fully expand all other aggregates
912     if (Kind == ABIKind::ExperimentalMV) {
913       const RecordType *RT = Ty->getAs<RecordType>();
914       assert(RT);
915       bool HasBitField = false;
916       for (auto *Field : RT->getDecl()->fields()) {
917         if (Field->isBitField()) {
918           HasBitField = true;
919           break;
920         }
921       }
922       if (!HasBitField)
923         return ABIArgInfo::getExpand();
924     }
925   }
926 
927   // Otherwise just do the default thing.
928   return defaultInfo.classifyArgumentType(Ty);
929 }
930 
931 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
932   if (isAggregateTypeForABI(RetTy)) {
933     // Records with non-trivial destructors/copy-constructors should not be
934     // returned by value.
935     if (!getRecordArgABI(RetTy, getCXXABI())) {
936       // Ignore empty structs/unions.
937       if (isEmptyRecord(getContext(), RetTy, true))
938         return ABIArgInfo::getIgnore();
939       // Lower single-element structs to just return a regular value. TODO: We
940       // could do reasonable-size multiple-element structs too, using
941       // ABIArgInfo::getDirect().
942       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
943         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
944       // For the experimental multivalue ABI, return all other aggregates
945       if (Kind == ABIKind::ExperimentalMV)
946         return ABIArgInfo::getDirect();
947     }
948   }
949 
950   // Otherwise just do the default thing.
951   return defaultInfo.classifyReturnType(RetTy);
952 }
953 
954 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
955                                       QualType Ty) const {
956   bool IsIndirect = isAggregateTypeForABI(Ty) &&
957                     !isEmptyRecord(getContext(), Ty, true) &&
958                     !isSingleElementStruct(Ty, getContext());
959   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
960                           getContext().getTypeInfoInChars(Ty),
961                           CharUnits::fromQuantity(4),
962                           /*AllowHigherAlign=*/true);
963 }
964 
965 //===----------------------------------------------------------------------===//
966 // le32/PNaCl bitcode ABI Implementation
967 //
968 // This is a simplified version of the x86_32 ABI.  Arguments and return values
969 // are always passed on the stack.
970 //===----------------------------------------------------------------------===//
971 
972 class PNaClABIInfo : public ABIInfo {
973  public:
974   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
975 
976   ABIArgInfo classifyReturnType(QualType RetTy) const;
977   ABIArgInfo classifyArgumentType(QualType RetTy) const;
978 
979   void computeInfo(CGFunctionInfo &FI) const override;
980   Address EmitVAArg(CodeGenFunction &CGF,
981                     Address VAListAddr, QualType Ty) const override;
982 };
983 
984 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
985  public:
986    PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
987        : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {}
988 };
989 
990 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
991   if (!getCXXABI().classifyReturnType(FI))
992     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
993 
994   for (auto &I : FI.arguments())
995     I.info = classifyArgumentType(I.type);
996 }
997 
998 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
999                                 QualType Ty) const {
1000   // The PNaCL ABI is a bit odd, in that varargs don't use normal
1001   // function classification. Structs get passed directly for varargs
1002   // functions, through a rewriting transform in
1003   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
1004   // this target to actually support a va_arg instructions with an
1005   // aggregate type, unlike other targets.
1006   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
1007 }
1008 
1009 /// Classify argument of given type \p Ty.
1010 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
1011   if (isAggregateTypeForABI(Ty)) {
1012     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
1013       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
1014     return getNaturalAlignIndirect(Ty);
1015   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
1016     // Treat an enum type as its underlying type.
1017     Ty = EnumTy->getDecl()->getIntegerType();
1018   } else if (Ty->isFloatingType()) {
1019     // Floating-point types don't go inreg.
1020     return ABIArgInfo::getDirect();
1021   } else if (const auto *EIT = Ty->getAs<BitIntType>()) {
1022     // Treat bit-precise integers as integers if <= 64, otherwise pass
1023     // indirectly.
1024     if (EIT->getNumBits() > 64)
1025       return getNaturalAlignIndirect(Ty);
1026     return ABIArgInfo::getDirect();
1027   }
1028 
1029   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
1030                                             : ABIArgInfo::getDirect());
1031 }
1032 
1033 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
1034   if (RetTy->isVoidType())
1035     return ABIArgInfo::getIgnore();
1036 
1037   // In the PNaCl ABI we always return records/structures on the stack.
1038   if (isAggregateTypeForABI(RetTy))
1039     return getNaturalAlignIndirect(RetTy);
1040 
1041   // Treat bit-precise integers as integers if <= 64, otherwise pass indirectly.
1042   if (const auto *EIT = RetTy->getAs<BitIntType>()) {
1043     if (EIT->getNumBits() > 64)
1044       return getNaturalAlignIndirect(RetTy);
1045     return ABIArgInfo::getDirect();
1046   }
1047 
1048   // Treat an enum type as its underlying type.
1049   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1050     RetTy = EnumTy->getDecl()->getIntegerType();
1051 
1052   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1053                                                : ABIArgInfo::getDirect());
1054 }
1055 
1056 /// IsX86_MMXType - Return true if this is an MMX type.
1057 bool IsX86_MMXType(llvm::Type *IRType) {
1058   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
1059   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
1060     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
1061     IRType->getScalarSizeInBits() != 64;
1062 }
1063 
1064 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1065                                           StringRef Constraint,
1066                                           llvm::Type* Ty) {
1067   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
1068                      .Cases("y", "&y", "^Ym", true)
1069                      .Default(false);
1070   if (IsMMXCons && Ty->isVectorTy()) {
1071     if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedSize() !=
1072         64) {
1073       // Invalid MMX constraint
1074       return nullptr;
1075     }
1076 
1077     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
1078   }
1079 
1080   // No operation needed
1081   return Ty;
1082 }
1083 
1084 /// Returns true if this type can be passed in SSE registers with the
1085 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1086 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
1087   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1088     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
1089       if (BT->getKind() == BuiltinType::LongDouble) {
1090         if (&Context.getTargetInfo().getLongDoubleFormat() ==
1091             &llvm::APFloat::x87DoubleExtended())
1092           return false;
1093       }
1094       return true;
1095     }
1096   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
1097     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
1098     // registers specially.
1099     unsigned VecSize = Context.getTypeSize(VT);
1100     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
1101       return true;
1102   }
1103   return false;
1104 }
1105 
1106 /// Returns true if this aggregate is small enough to be passed in SSE registers
1107 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1108 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
1109   return NumMembers <= 4;
1110 }
1111 
1112 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
1113 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
1114   auto AI = ABIArgInfo::getDirect(T);
1115   AI.setInReg(true);
1116   AI.setCanBeFlattened(false);
1117   return AI;
1118 }
1119 
1120 //===----------------------------------------------------------------------===//
1121 // X86-32 ABI Implementation
1122 //===----------------------------------------------------------------------===//
1123 
1124 /// Similar to llvm::CCState, but for Clang.
1125 struct CCState {
1126   CCState(CGFunctionInfo &FI)
1127       : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {}
1128 
1129   llvm::SmallBitVector IsPreassigned;
1130   unsigned CC = CallingConv::CC_C;
1131   unsigned FreeRegs = 0;
1132   unsigned FreeSSERegs = 0;
1133 };
1134 
1135 /// X86_32ABIInfo - The X86-32 ABI information.
1136 class X86_32ABIInfo : public SwiftABIInfo {
1137   enum Class {
1138     Integer,
1139     Float
1140   };
1141 
1142   static const unsigned MinABIStackAlignInBytes = 4;
1143 
1144   bool IsDarwinVectorABI;
1145   bool IsRetSmallStructInRegABI;
1146   bool IsWin32StructABI;
1147   bool IsSoftFloatABI;
1148   bool IsMCUABI;
1149   bool IsLinuxABI;
1150   unsigned DefaultNumRegisterParameters;
1151 
1152   static bool isRegisterSize(unsigned Size) {
1153     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
1154   }
1155 
1156   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1157     // FIXME: Assumes vectorcall is in use.
1158     return isX86VectorTypeForVectorCall(getContext(), Ty);
1159   }
1160 
1161   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1162                                          uint64_t NumMembers) const override {
1163     // FIXME: Assumes vectorcall is in use.
1164     return isX86VectorCallAggregateSmallEnough(NumMembers);
1165   }
1166 
1167   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
1168 
1169   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1170   /// such that the argument will be passed in memory.
1171   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
1172 
1173   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
1174 
1175   /// Return the alignment to use for the given type on the stack.
1176   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
1177 
1178   Class classify(QualType Ty) const;
1179   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
1180   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
1181 
1182   /// Updates the number of available free registers, returns
1183   /// true if any registers were allocated.
1184   bool updateFreeRegs(QualType Ty, CCState &State) const;
1185 
1186   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1187                                 bool &NeedsPadding) const;
1188   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1189 
1190   bool canExpandIndirectArgument(QualType Ty) const;
1191 
1192   /// Rewrite the function info so that all memory arguments use
1193   /// inalloca.
1194   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1195 
1196   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1197                            CharUnits &StackOffset, ABIArgInfo &Info,
1198                            QualType Type) const;
1199   void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const;
1200 
1201 public:
1202 
1203   void computeInfo(CGFunctionInfo &FI) const override;
1204   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1205                     QualType Ty) const override;
1206 
1207   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1208                 bool RetSmallStructInRegABI, bool Win32StructABI,
1209                 unsigned NumRegisterParameters, bool SoftFloatABI)
1210     : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1211       IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1212       IsWin32StructABI(Win32StructABI), IsSoftFloatABI(SoftFloatABI),
1213       IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1214       IsLinuxABI(CGT.getTarget().getTriple().isOSLinux() ||
1215                  CGT.getTarget().getTriple().isOSCygMing()),
1216       DefaultNumRegisterParameters(NumRegisterParameters) {}
1217 
1218   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
1219                                     bool asReturnValue) const override {
1220     // LLVM's x86-32 lowering currently only assigns up to three
1221     // integer registers and three fp registers.  Oddly, it'll use up to
1222     // four vector registers for vectors, but those can overlap with the
1223     // scalar registers.
1224     return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1225   }
1226 
1227   bool isSwiftErrorInRegister() const override {
1228     // x86-32 lowering does not support passing swifterror in a register.
1229     return false;
1230   }
1231 };
1232 
1233 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1234 public:
1235   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1236                           bool RetSmallStructInRegABI, bool Win32StructABI,
1237                           unsigned NumRegisterParameters, bool SoftFloatABI)
1238       : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>(
1239             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1240             NumRegisterParameters, SoftFloatABI)) {}
1241 
1242   static bool isStructReturnInRegABI(
1243       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1244 
1245   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1246                            CodeGen::CodeGenModule &CGM) const override;
1247 
1248   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1249     // Darwin uses different dwarf register numbers for EH.
1250     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1251     return 4;
1252   }
1253 
1254   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1255                                llvm::Value *Address) const override;
1256 
1257   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1258                                   StringRef Constraint,
1259                                   llvm::Type* Ty) const override {
1260     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1261   }
1262 
1263   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1264                                 std::string &Constraints,
1265                                 std::vector<llvm::Type *> &ResultRegTypes,
1266                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1267                                 std::vector<LValue> &ResultRegDests,
1268                                 std::string &AsmString,
1269                                 unsigned NumOutputs) const override;
1270 
1271   llvm::Constant *
1272   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1273     unsigned Sig = (0xeb << 0) |  // jmp rel8
1274                    (0x06 << 8) |  //           .+0x08
1275                    ('v' << 16) |
1276                    ('2' << 24);
1277     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1278   }
1279 
1280   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1281     return "movl\t%ebp, %ebp"
1282            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1283   }
1284 };
1285 
1286 }
1287 
1288 /// Rewrite input constraint references after adding some output constraints.
1289 /// In the case where there is one output and one input and we add one output,
1290 /// we need to replace all operand references greater than or equal to 1:
1291 ///     mov $0, $1
1292 ///     mov eax, $1
1293 /// The result will be:
1294 ///     mov $0, $2
1295 ///     mov eax, $2
1296 static void rewriteInputConstraintReferences(unsigned FirstIn,
1297                                              unsigned NumNewOuts,
1298                                              std::string &AsmString) {
1299   std::string Buf;
1300   llvm::raw_string_ostream OS(Buf);
1301   size_t Pos = 0;
1302   while (Pos < AsmString.size()) {
1303     size_t DollarStart = AsmString.find('$', Pos);
1304     if (DollarStart == std::string::npos)
1305       DollarStart = AsmString.size();
1306     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1307     if (DollarEnd == std::string::npos)
1308       DollarEnd = AsmString.size();
1309     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1310     Pos = DollarEnd;
1311     size_t NumDollars = DollarEnd - DollarStart;
1312     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1313       // We have an operand reference.
1314       size_t DigitStart = Pos;
1315       if (AsmString[DigitStart] == '{') {
1316         OS << '{';
1317         ++DigitStart;
1318       }
1319       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1320       if (DigitEnd == std::string::npos)
1321         DigitEnd = AsmString.size();
1322       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1323       unsigned OperandIndex;
1324       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1325         if (OperandIndex >= FirstIn)
1326           OperandIndex += NumNewOuts;
1327         OS << OperandIndex;
1328       } else {
1329         OS << OperandStr;
1330       }
1331       Pos = DigitEnd;
1332     }
1333   }
1334   AsmString = std::move(OS.str());
1335 }
1336 
1337 /// Add output constraints for EAX:EDX because they are return registers.
1338 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1339     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1340     std::vector<llvm::Type *> &ResultRegTypes,
1341     std::vector<llvm::Type *> &ResultTruncRegTypes,
1342     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1343     unsigned NumOutputs) const {
1344   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1345 
1346   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1347   // larger.
1348   if (!Constraints.empty())
1349     Constraints += ',';
1350   if (RetWidth <= 32) {
1351     Constraints += "={eax}";
1352     ResultRegTypes.push_back(CGF.Int32Ty);
1353   } else {
1354     // Use the 'A' constraint for EAX:EDX.
1355     Constraints += "=A";
1356     ResultRegTypes.push_back(CGF.Int64Ty);
1357   }
1358 
1359   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1360   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1361   ResultTruncRegTypes.push_back(CoerceTy);
1362 
1363   // Coerce the integer by bitcasting the return slot pointer.
1364   ReturnSlot.setAddress(
1365       CGF.Builder.CreateElementBitCast(ReturnSlot.getAddress(CGF), CoerceTy));
1366   ResultRegDests.push_back(ReturnSlot);
1367 
1368   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1369 }
1370 
1371 /// shouldReturnTypeInRegister - Determine if the given type should be
1372 /// returned in a register (for the Darwin and MCU ABI).
1373 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1374                                                ASTContext &Context) const {
1375   uint64_t Size = Context.getTypeSize(Ty);
1376 
1377   // For i386, type must be register sized.
1378   // For the MCU ABI, it only needs to be <= 8-byte
1379   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1380    return false;
1381 
1382   if (Ty->isVectorType()) {
1383     // 64- and 128- bit vectors inside structures are not returned in
1384     // registers.
1385     if (Size == 64 || Size == 128)
1386       return false;
1387 
1388     return true;
1389   }
1390 
1391   // If this is a builtin, pointer, enum, complex type, member pointer, or
1392   // member function pointer it is ok.
1393   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1394       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1395       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1396     return true;
1397 
1398   // Arrays are treated like records.
1399   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1400     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1401 
1402   // Otherwise, it must be a record type.
1403   const RecordType *RT = Ty->getAs<RecordType>();
1404   if (!RT) return false;
1405 
1406   // FIXME: Traverse bases here too.
1407 
1408   // Structure types are passed in register if all fields would be
1409   // passed in a register.
1410   for (const auto *FD : RT->getDecl()->fields()) {
1411     // Empty fields are ignored.
1412     if (isEmptyField(Context, FD, true))
1413       continue;
1414 
1415     // Check fields recursively.
1416     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1417       return false;
1418   }
1419   return true;
1420 }
1421 
1422 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1423   // Treat complex types as the element type.
1424   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1425     Ty = CTy->getElementType();
1426 
1427   // Check for a type which we know has a simple scalar argument-passing
1428   // convention without any padding.  (We're specifically looking for 32
1429   // and 64-bit integer and integer-equivalents, float, and double.)
1430   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1431       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1432     return false;
1433 
1434   uint64_t Size = Context.getTypeSize(Ty);
1435   return Size == 32 || Size == 64;
1436 }
1437 
1438 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1439                           uint64_t &Size) {
1440   for (const auto *FD : RD->fields()) {
1441     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1442     // argument is smaller than 32-bits, expanding the struct will create
1443     // alignment padding.
1444     if (!is32Or64BitBasicType(FD->getType(), Context))
1445       return false;
1446 
1447     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1448     // how to expand them yet, and the predicate for telling if a bitfield still
1449     // counts as "basic" is more complicated than what we were doing previously.
1450     if (FD->isBitField())
1451       return false;
1452 
1453     Size += Context.getTypeSize(FD->getType());
1454   }
1455   return true;
1456 }
1457 
1458 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1459                                  uint64_t &Size) {
1460   // Don't do this if there are any non-empty bases.
1461   for (const CXXBaseSpecifier &Base : RD->bases()) {
1462     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1463                               Size))
1464       return false;
1465   }
1466   if (!addFieldSizes(Context, RD, Size))
1467     return false;
1468   return true;
1469 }
1470 
1471 /// Test whether an argument type which is to be passed indirectly (on the
1472 /// stack) would have the equivalent layout if it was expanded into separate
1473 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1474 /// optimizations.
1475 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1476   // We can only expand structure types.
1477   const RecordType *RT = Ty->getAs<RecordType>();
1478   if (!RT)
1479     return false;
1480   const RecordDecl *RD = RT->getDecl();
1481   uint64_t Size = 0;
1482   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1483     if (!IsWin32StructABI) {
1484       // On non-Windows, we have to conservatively match our old bitcode
1485       // prototypes in order to be ABI-compatible at the bitcode level.
1486       if (!CXXRD->isCLike())
1487         return false;
1488     } else {
1489       // Don't do this for dynamic classes.
1490       if (CXXRD->isDynamicClass())
1491         return false;
1492     }
1493     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1494       return false;
1495   } else {
1496     if (!addFieldSizes(getContext(), RD, Size))
1497       return false;
1498   }
1499 
1500   // We can do this if there was no alignment padding.
1501   return Size == getContext().getTypeSize(Ty);
1502 }
1503 
1504 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1505   // If the return value is indirect, then the hidden argument is consuming one
1506   // integer register.
1507   if (State.FreeRegs) {
1508     --State.FreeRegs;
1509     if (!IsMCUABI)
1510       return getNaturalAlignIndirectInReg(RetTy);
1511   }
1512   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1513 }
1514 
1515 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1516                                              CCState &State) const {
1517   if (RetTy->isVoidType())
1518     return ABIArgInfo::getIgnore();
1519 
1520   const Type *Base = nullptr;
1521   uint64_t NumElts = 0;
1522   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1523        State.CC == llvm::CallingConv::X86_RegCall) &&
1524       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1525     // The LLVM struct type for such an aggregate should lower properly.
1526     return ABIArgInfo::getDirect();
1527   }
1528 
1529   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1530     // On Darwin, some vectors are returned in registers.
1531     if (IsDarwinVectorABI) {
1532       uint64_t Size = getContext().getTypeSize(RetTy);
1533 
1534       // 128-bit vectors are a special case; they are returned in
1535       // registers and we need to make sure to pick a type the LLVM
1536       // backend will like.
1537       if (Size == 128)
1538         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1539             llvm::Type::getInt64Ty(getVMContext()), 2));
1540 
1541       // Always return in register if it fits in a general purpose
1542       // register, or if it is 64 bits and has a single element.
1543       if ((Size == 8 || Size == 16 || Size == 32) ||
1544           (Size == 64 && VT->getNumElements() == 1))
1545         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1546                                                             Size));
1547 
1548       return getIndirectReturnResult(RetTy, State);
1549     }
1550 
1551     return ABIArgInfo::getDirect();
1552   }
1553 
1554   if (isAggregateTypeForABI(RetTy)) {
1555     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1556       // Structures with flexible arrays are always indirect.
1557       if (RT->getDecl()->hasFlexibleArrayMember())
1558         return getIndirectReturnResult(RetTy, State);
1559     }
1560 
1561     // If specified, structs and unions are always indirect.
1562     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1563       return getIndirectReturnResult(RetTy, State);
1564 
1565     // Ignore empty structs/unions.
1566     if (isEmptyRecord(getContext(), RetTy, true))
1567       return ABIArgInfo::getIgnore();
1568 
1569     // Return complex of _Float16 as <2 x half> so the backend will use xmm0.
1570     if (const ComplexType *CT = RetTy->getAs<ComplexType>()) {
1571       QualType ET = getContext().getCanonicalType(CT->getElementType());
1572       if (ET->isFloat16Type())
1573         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1574             llvm::Type::getHalfTy(getVMContext()), 2));
1575     }
1576 
1577     // Small structures which are register sized are generally returned
1578     // in a register.
1579     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1580       uint64_t Size = getContext().getTypeSize(RetTy);
1581 
1582       // As a special-case, if the struct is a "single-element" struct, and
1583       // the field is of type "float" or "double", return it in a
1584       // floating-point register. (MSVC does not apply this special case.)
1585       // We apply a similar transformation for pointer types to improve the
1586       // quality of the generated IR.
1587       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1588         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1589             || SeltTy->hasPointerRepresentation())
1590           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1591 
1592       // FIXME: We should be able to narrow this integer in cases with dead
1593       // padding.
1594       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1595     }
1596 
1597     return getIndirectReturnResult(RetTy, State);
1598   }
1599 
1600   // Treat an enum type as its underlying type.
1601   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1602     RetTy = EnumTy->getDecl()->getIntegerType();
1603 
1604   if (const auto *EIT = RetTy->getAs<BitIntType>())
1605     if (EIT->getNumBits() > 64)
1606       return getIndirectReturnResult(RetTy, State);
1607 
1608   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1609                                                : ABIArgInfo::getDirect());
1610 }
1611 
1612 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) {
1613   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1614 }
1615 
1616 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) {
1617   const RecordType *RT = Ty->getAs<RecordType>();
1618   if (!RT)
1619     return false;
1620   const RecordDecl *RD = RT->getDecl();
1621 
1622   // If this is a C++ record, check the bases first.
1623   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1624     for (const auto &I : CXXRD->bases())
1625       if (!isRecordWithSIMDVectorType(Context, I.getType()))
1626         return false;
1627 
1628   for (const auto *i : RD->fields()) {
1629     QualType FT = i->getType();
1630 
1631     if (isSIMDVectorType(Context, FT))
1632       return true;
1633 
1634     if (isRecordWithSIMDVectorType(Context, FT))
1635       return true;
1636   }
1637 
1638   return false;
1639 }
1640 
1641 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1642                                                  unsigned Align) const {
1643   // Otherwise, if the alignment is less than or equal to the minimum ABI
1644   // alignment, just use the default; the backend will handle this.
1645   if (Align <= MinABIStackAlignInBytes)
1646     return 0; // Use default alignment.
1647 
1648   if (IsLinuxABI) {
1649     // Exclude other System V OS (e.g Darwin, PS4 and FreeBSD) since we don't
1650     // want to spend any effort dealing with the ramifications of ABI breaks.
1651     //
1652     // If the vector type is __m128/__m256/__m512, return the default alignment.
1653     if (Ty->isVectorType() && (Align == 16 || Align == 32 || Align == 64))
1654       return Align;
1655   }
1656   // On non-Darwin, the stack type alignment is always 4.
1657   if (!IsDarwinVectorABI) {
1658     // Set explicit alignment, since we may need to realign the top.
1659     return MinABIStackAlignInBytes;
1660   }
1661 
1662   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1663   if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) ||
1664                       isRecordWithSIMDVectorType(getContext(), Ty)))
1665     return 16;
1666 
1667   return MinABIStackAlignInBytes;
1668 }
1669 
1670 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1671                                             CCState &State) const {
1672   if (!ByVal) {
1673     if (State.FreeRegs) {
1674       --State.FreeRegs; // Non-byval indirects just use one pointer.
1675       if (!IsMCUABI)
1676         return getNaturalAlignIndirectInReg(Ty);
1677     }
1678     return getNaturalAlignIndirect(Ty, false);
1679   }
1680 
1681   // Compute the byval alignment.
1682   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1683   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1684   if (StackAlign == 0)
1685     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1686 
1687   // If the stack alignment is less than the type alignment, realign the
1688   // argument.
1689   bool Realign = TypeAlign > StackAlign;
1690   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1691                                  /*ByVal=*/true, Realign);
1692 }
1693 
1694 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1695   const Type *T = isSingleElementStruct(Ty, getContext());
1696   if (!T)
1697     T = Ty.getTypePtr();
1698 
1699   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1700     BuiltinType::Kind K = BT->getKind();
1701     if (K == BuiltinType::Float || K == BuiltinType::Double)
1702       return Float;
1703   }
1704   return Integer;
1705 }
1706 
1707 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1708   if (!IsSoftFloatABI) {
1709     Class C = classify(Ty);
1710     if (C == Float)
1711       return false;
1712   }
1713 
1714   unsigned Size = getContext().getTypeSize(Ty);
1715   unsigned SizeInRegs = (Size + 31) / 32;
1716 
1717   if (SizeInRegs == 0)
1718     return false;
1719 
1720   if (!IsMCUABI) {
1721     if (SizeInRegs > State.FreeRegs) {
1722       State.FreeRegs = 0;
1723       return false;
1724     }
1725   } else {
1726     // The MCU psABI allows passing parameters in-reg even if there are
1727     // earlier parameters that are passed on the stack. Also,
1728     // it does not allow passing >8-byte structs in-register,
1729     // even if there are 3 free registers available.
1730     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1731       return false;
1732   }
1733 
1734   State.FreeRegs -= SizeInRegs;
1735   return true;
1736 }
1737 
1738 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1739                                              bool &InReg,
1740                                              bool &NeedsPadding) const {
1741   // On Windows, aggregates other than HFAs are never passed in registers, and
1742   // they do not consume register slots. Homogenous floating-point aggregates
1743   // (HFAs) have already been dealt with at this point.
1744   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1745     return false;
1746 
1747   NeedsPadding = false;
1748   InReg = !IsMCUABI;
1749 
1750   if (!updateFreeRegs(Ty, State))
1751     return false;
1752 
1753   if (IsMCUABI)
1754     return true;
1755 
1756   if (State.CC == llvm::CallingConv::X86_FastCall ||
1757       State.CC == llvm::CallingConv::X86_VectorCall ||
1758       State.CC == llvm::CallingConv::X86_RegCall) {
1759     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1760       NeedsPadding = true;
1761 
1762     return false;
1763   }
1764 
1765   return true;
1766 }
1767 
1768 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1769   if (!updateFreeRegs(Ty, State))
1770     return false;
1771 
1772   if (IsMCUABI)
1773     return false;
1774 
1775   if (State.CC == llvm::CallingConv::X86_FastCall ||
1776       State.CC == llvm::CallingConv::X86_VectorCall ||
1777       State.CC == llvm::CallingConv::X86_RegCall) {
1778     if (getContext().getTypeSize(Ty) > 32)
1779       return false;
1780 
1781     return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1782         Ty->isReferenceType());
1783   }
1784 
1785   return true;
1786 }
1787 
1788 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const {
1789   // Vectorcall x86 works subtly different than in x64, so the format is
1790   // a bit different than the x64 version.  First, all vector types (not HVAs)
1791   // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers.
1792   // This differs from the x64 implementation, where the first 6 by INDEX get
1793   // registers.
1794   // In the second pass over the arguments, HVAs are passed in the remaining
1795   // vector registers if possible, or indirectly by address. The address will be
1796   // passed in ECX/EDX if available. Any other arguments are passed according to
1797   // the usual fastcall rules.
1798   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1799   for (int I = 0, E = Args.size(); I < E; ++I) {
1800     const Type *Base = nullptr;
1801     uint64_t NumElts = 0;
1802     const QualType &Ty = Args[I].type;
1803     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1804         isHomogeneousAggregate(Ty, Base, NumElts)) {
1805       if (State.FreeSSERegs >= NumElts) {
1806         State.FreeSSERegs -= NumElts;
1807         Args[I].info = ABIArgInfo::getDirectInReg();
1808         State.IsPreassigned.set(I);
1809       }
1810     }
1811   }
1812 }
1813 
1814 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1815                                                CCState &State) const {
1816   // FIXME: Set alignment on indirect arguments.
1817   bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall;
1818   bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall;
1819   bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall;
1820 
1821   Ty = useFirstFieldIfTransparentUnion(Ty);
1822   TypeInfo TI = getContext().getTypeInfo(Ty);
1823 
1824   // Check with the C++ ABI first.
1825   const RecordType *RT = Ty->getAs<RecordType>();
1826   if (RT) {
1827     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1828     if (RAA == CGCXXABI::RAA_Indirect) {
1829       return getIndirectResult(Ty, false, State);
1830     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1831       // The field index doesn't matter, we'll fix it up later.
1832       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1833     }
1834   }
1835 
1836   // Regcall uses the concept of a homogenous vector aggregate, similar
1837   // to other targets.
1838   const Type *Base = nullptr;
1839   uint64_t NumElts = 0;
1840   if ((IsRegCall || IsVectorCall) &&
1841       isHomogeneousAggregate(Ty, Base, NumElts)) {
1842     if (State.FreeSSERegs >= NumElts) {
1843       State.FreeSSERegs -= NumElts;
1844 
1845       // Vectorcall passes HVAs directly and does not flatten them, but regcall
1846       // does.
1847       if (IsVectorCall)
1848         return getDirectX86Hva();
1849 
1850       if (Ty->isBuiltinType() || Ty->isVectorType())
1851         return ABIArgInfo::getDirect();
1852       return ABIArgInfo::getExpand();
1853     }
1854     return getIndirectResult(Ty, /*ByVal=*/false, State);
1855   }
1856 
1857   if (isAggregateTypeForABI(Ty)) {
1858     // Structures with flexible arrays are always indirect.
1859     // FIXME: This should not be byval!
1860     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1861       return getIndirectResult(Ty, true, State);
1862 
1863     // Ignore empty structs/unions on non-Windows.
1864     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1865       return ABIArgInfo::getIgnore();
1866 
1867     llvm::LLVMContext &LLVMContext = getVMContext();
1868     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1869     bool NeedsPadding = false;
1870     bool InReg;
1871     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1872       unsigned SizeInRegs = (TI.Width + 31) / 32;
1873       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1874       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1875       if (InReg)
1876         return ABIArgInfo::getDirectInReg(Result);
1877       else
1878         return ABIArgInfo::getDirect(Result);
1879     }
1880     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1881 
1882     // Pass over-aligned aggregates on Windows indirectly. This behavior was
1883     // added in MSVC 2015.
1884     if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32)
1885       return getIndirectResult(Ty, /*ByVal=*/false, State);
1886 
1887     // Expand small (<= 128-bit) record types when we know that the stack layout
1888     // of those arguments will match the struct. This is important because the
1889     // LLVM backend isn't smart enough to remove byval, which inhibits many
1890     // optimizations.
1891     // Don't do this for the MCU if there are still free integer registers
1892     // (see X86_64 ABI for full explanation).
1893     if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) &&
1894         canExpandIndirectArgument(Ty))
1895       return ABIArgInfo::getExpandWithPadding(
1896           IsFastCall || IsVectorCall || IsRegCall, PaddingType);
1897 
1898     return getIndirectResult(Ty, true, State);
1899   }
1900 
1901   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1902     // On Windows, vectors are passed directly if registers are available, or
1903     // indirectly if not. This avoids the need to align argument memory. Pass
1904     // user-defined vector types larger than 512 bits indirectly for simplicity.
1905     if (IsWin32StructABI) {
1906       if (TI.Width <= 512 && State.FreeSSERegs > 0) {
1907         --State.FreeSSERegs;
1908         return ABIArgInfo::getDirectInReg();
1909       }
1910       return getIndirectResult(Ty, /*ByVal=*/false, State);
1911     }
1912 
1913     // On Darwin, some vectors are passed in memory, we handle this by passing
1914     // it as an i8/i16/i32/i64.
1915     if (IsDarwinVectorABI) {
1916       if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) ||
1917           (TI.Width == 64 && VT->getNumElements() == 1))
1918         return ABIArgInfo::getDirect(
1919             llvm::IntegerType::get(getVMContext(), TI.Width));
1920     }
1921 
1922     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1923       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1924 
1925     return ABIArgInfo::getDirect();
1926   }
1927 
1928 
1929   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1930     Ty = EnumTy->getDecl()->getIntegerType();
1931 
1932   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1933 
1934   if (isPromotableIntegerTypeForABI(Ty)) {
1935     if (InReg)
1936       return ABIArgInfo::getExtendInReg(Ty);
1937     return ABIArgInfo::getExtend(Ty);
1938   }
1939 
1940   if (const auto *EIT = Ty->getAs<BitIntType>()) {
1941     if (EIT->getNumBits() <= 64) {
1942       if (InReg)
1943         return ABIArgInfo::getDirectInReg();
1944       return ABIArgInfo::getDirect();
1945     }
1946     return getIndirectResult(Ty, /*ByVal=*/false, State);
1947   }
1948 
1949   if (InReg)
1950     return ABIArgInfo::getDirectInReg();
1951   return ABIArgInfo::getDirect();
1952 }
1953 
1954 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1955   CCState State(FI);
1956   if (IsMCUABI)
1957     State.FreeRegs = 3;
1958   else if (State.CC == llvm::CallingConv::X86_FastCall) {
1959     State.FreeRegs = 2;
1960     State.FreeSSERegs = 3;
1961   } else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1962     State.FreeRegs = 2;
1963     State.FreeSSERegs = 6;
1964   } else if (FI.getHasRegParm())
1965     State.FreeRegs = FI.getRegParm();
1966   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1967     State.FreeRegs = 5;
1968     State.FreeSSERegs = 8;
1969   } else if (IsWin32StructABI) {
1970     // Since MSVC 2015, the first three SSE vectors have been passed in
1971     // registers. The rest are passed indirectly.
1972     State.FreeRegs = DefaultNumRegisterParameters;
1973     State.FreeSSERegs = 3;
1974   } else
1975     State.FreeRegs = DefaultNumRegisterParameters;
1976 
1977   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
1978     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1979   } else if (FI.getReturnInfo().isIndirect()) {
1980     // The C++ ABI is not aware of register usage, so we have to check if the
1981     // return value was sret and put it in a register ourselves if appropriate.
1982     if (State.FreeRegs) {
1983       --State.FreeRegs;  // The sret parameter consumes a register.
1984       if (!IsMCUABI)
1985         FI.getReturnInfo().setInReg(true);
1986     }
1987   }
1988 
1989   // The chain argument effectively gives us another free register.
1990   if (FI.isChainCall())
1991     ++State.FreeRegs;
1992 
1993   // For vectorcall, do a first pass over the arguments, assigning FP and vector
1994   // arguments to XMM registers as available.
1995   if (State.CC == llvm::CallingConv::X86_VectorCall)
1996     runVectorCallFirstPass(FI, State);
1997 
1998   bool UsedInAlloca = false;
1999   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
2000   for (int I = 0, E = Args.size(); I < E; ++I) {
2001     // Skip arguments that have already been assigned.
2002     if (State.IsPreassigned.test(I))
2003       continue;
2004 
2005     Args[I].info = classifyArgumentType(Args[I].type, State);
2006     UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca);
2007   }
2008 
2009   // If we needed to use inalloca for any argument, do a second pass and rewrite
2010   // all the memory arguments to use inalloca.
2011   if (UsedInAlloca)
2012     rewriteWithInAlloca(FI);
2013 }
2014 
2015 void
2016 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
2017                                    CharUnits &StackOffset, ABIArgInfo &Info,
2018                                    QualType Type) const {
2019   // Arguments are always 4-byte-aligned.
2020   CharUnits WordSize = CharUnits::fromQuantity(4);
2021   assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct");
2022 
2023   // sret pointers and indirect things will require an extra pointer
2024   // indirection, unless they are byval. Most things are byval, and will not
2025   // require this indirection.
2026   bool IsIndirect = false;
2027   if (Info.isIndirect() && !Info.getIndirectByVal())
2028     IsIndirect = true;
2029   Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect);
2030   llvm::Type *LLTy = CGT.ConvertTypeForMem(Type);
2031   if (IsIndirect)
2032     LLTy = LLTy->getPointerTo(0);
2033   FrameFields.push_back(LLTy);
2034   StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type);
2035 
2036   // Insert padding bytes to respect alignment.
2037   CharUnits FieldEnd = StackOffset;
2038   StackOffset = FieldEnd.alignTo(WordSize);
2039   if (StackOffset != FieldEnd) {
2040     CharUnits NumBytes = StackOffset - FieldEnd;
2041     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
2042     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
2043     FrameFields.push_back(Ty);
2044   }
2045 }
2046 
2047 static bool isArgInAlloca(const ABIArgInfo &Info) {
2048   // Leave ignored and inreg arguments alone.
2049   switch (Info.getKind()) {
2050   case ABIArgInfo::InAlloca:
2051     return true;
2052   case ABIArgInfo::Ignore:
2053   case ABIArgInfo::IndirectAliased:
2054     return false;
2055   case ABIArgInfo::Indirect:
2056   case ABIArgInfo::Direct:
2057   case ABIArgInfo::Extend:
2058     return !Info.getInReg();
2059   case ABIArgInfo::Expand:
2060   case ABIArgInfo::CoerceAndExpand:
2061     // These are aggregate types which are never passed in registers when
2062     // inalloca is involved.
2063     return true;
2064   }
2065   llvm_unreachable("invalid enum");
2066 }
2067 
2068 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
2069   assert(IsWin32StructABI && "inalloca only supported on win32");
2070 
2071   // Build a packed struct type for all of the arguments in memory.
2072   SmallVector<llvm::Type *, 6> FrameFields;
2073 
2074   // The stack alignment is always 4.
2075   CharUnits StackAlign = CharUnits::fromQuantity(4);
2076 
2077   CharUnits StackOffset;
2078   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
2079 
2080   // Put 'this' into the struct before 'sret', if necessary.
2081   bool IsThisCall =
2082       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
2083   ABIArgInfo &Ret = FI.getReturnInfo();
2084   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
2085       isArgInAlloca(I->info)) {
2086     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2087     ++I;
2088   }
2089 
2090   // Put the sret parameter into the inalloca struct if it's in memory.
2091   if (Ret.isIndirect() && !Ret.getInReg()) {
2092     addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType());
2093     // On Windows, the hidden sret parameter is always returned in eax.
2094     Ret.setInAllocaSRet(IsWin32StructABI);
2095   }
2096 
2097   // Skip the 'this' parameter in ecx.
2098   if (IsThisCall)
2099     ++I;
2100 
2101   // Put arguments passed in memory into the struct.
2102   for (; I != E; ++I) {
2103     if (isArgInAlloca(I->info))
2104       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2105   }
2106 
2107   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
2108                                         /*isPacked=*/true),
2109                   StackAlign);
2110 }
2111 
2112 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
2113                                  Address VAListAddr, QualType Ty) const {
2114 
2115   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
2116 
2117   // x86-32 changes the alignment of certain arguments on the stack.
2118   //
2119   // Just messing with TypeInfo like this works because we never pass
2120   // anything indirectly.
2121   TypeInfo.Align = CharUnits::fromQuantity(
2122                 getTypeStackAlignInBytes(Ty, TypeInfo.Align.getQuantity()));
2123 
2124   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
2125                           TypeInfo, CharUnits::fromQuantity(4),
2126                           /*AllowHigherAlign*/ true);
2127 }
2128 
2129 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
2130     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
2131   assert(Triple.getArch() == llvm::Triple::x86);
2132 
2133   switch (Opts.getStructReturnConvention()) {
2134   case CodeGenOptions::SRCK_Default:
2135     break;
2136   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
2137     return false;
2138   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
2139     return true;
2140   }
2141 
2142   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
2143     return true;
2144 
2145   switch (Triple.getOS()) {
2146   case llvm::Triple::DragonFly:
2147   case llvm::Triple::FreeBSD:
2148   case llvm::Triple::OpenBSD:
2149   case llvm::Triple::Win32:
2150     return true;
2151   default:
2152     return false;
2153   }
2154 }
2155 
2156 static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
2157                                  CodeGen::CodeGenModule &CGM) {
2158   if (!FD->hasAttr<AnyX86InterruptAttr>())
2159     return;
2160 
2161   llvm::Function *Fn = cast<llvm::Function>(GV);
2162   Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2163   if (FD->getNumParams() == 0)
2164     return;
2165 
2166   auto PtrTy = cast<PointerType>(FD->getParamDecl(0)->getType());
2167   llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
2168   llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
2169     Fn->getContext(), ByValTy);
2170   Fn->addParamAttr(0, NewAttr);
2171 }
2172 
2173 void X86_32TargetCodeGenInfo::setTargetAttributes(
2174     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2175   if (GV->isDeclaration())
2176     return;
2177   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2178     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2179       llvm::Function *Fn = cast<llvm::Function>(GV);
2180       Fn->addFnAttr("stackrealign");
2181     }
2182 
2183     addX86InterruptAttrs(FD, GV, CGM);
2184   }
2185 }
2186 
2187 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
2188                                                CodeGen::CodeGenFunction &CGF,
2189                                                llvm::Value *Address) const {
2190   CodeGen::CGBuilderTy &Builder = CGF.Builder;
2191 
2192   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
2193 
2194   // 0-7 are the eight integer registers;  the order is different
2195   //   on Darwin (for EH), but the range is the same.
2196   // 8 is %eip.
2197   AssignToArrayRange(Builder, Address, Four8, 0, 8);
2198 
2199   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
2200     // 12-16 are st(0..4).  Not sure why we stop at 4.
2201     // These have size 16, which is sizeof(long double) on
2202     // platforms with 8-byte alignment for that type.
2203     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
2204     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
2205 
2206   } else {
2207     // 9 is %eflags, which doesn't get a size on Darwin for some
2208     // reason.
2209     Builder.CreateAlignedStore(
2210         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
2211                                CharUnits::One());
2212 
2213     // 11-16 are st(0..5).  Not sure why we stop at 5.
2214     // These have size 12, which is sizeof(long double) on
2215     // platforms with 4-byte alignment for that type.
2216     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
2217     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
2218   }
2219 
2220   return false;
2221 }
2222 
2223 //===----------------------------------------------------------------------===//
2224 // X86-64 ABI Implementation
2225 //===----------------------------------------------------------------------===//
2226 
2227 
2228 namespace {
2229 /// The AVX ABI level for X86 targets.
2230 enum class X86AVXABILevel {
2231   None,
2232   AVX,
2233   AVX512
2234 };
2235 
2236 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2237 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2238   switch (AVXLevel) {
2239   case X86AVXABILevel::AVX512:
2240     return 512;
2241   case X86AVXABILevel::AVX:
2242     return 256;
2243   case X86AVXABILevel::None:
2244     return 128;
2245   }
2246   llvm_unreachable("Unknown AVXLevel");
2247 }
2248 
2249 /// X86_64ABIInfo - The X86_64 ABI information.
2250 class X86_64ABIInfo : public SwiftABIInfo {
2251   enum Class {
2252     Integer = 0,
2253     SSE,
2254     SSEUp,
2255     X87,
2256     X87Up,
2257     ComplexX87,
2258     NoClass,
2259     Memory
2260   };
2261 
2262   /// merge - Implement the X86_64 ABI merging algorithm.
2263   ///
2264   /// Merge an accumulating classification \arg Accum with a field
2265   /// classification \arg Field.
2266   ///
2267   /// \param Accum - The accumulating classification. This should
2268   /// always be either NoClass or the result of a previous merge
2269   /// call. In addition, this should never be Memory (the caller
2270   /// should just return Memory for the aggregate).
2271   static Class merge(Class Accum, Class Field);
2272 
2273   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2274   ///
2275   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2276   /// final MEMORY or SSE classes when necessary.
2277   ///
2278   /// \param AggregateSize - The size of the current aggregate in
2279   /// the classification process.
2280   ///
2281   /// \param Lo - The classification for the parts of the type
2282   /// residing in the low word of the containing object.
2283   ///
2284   /// \param Hi - The classification for the parts of the type
2285   /// residing in the higher words of the containing object.
2286   ///
2287   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2288 
2289   /// classify - Determine the x86_64 register classes in which the
2290   /// given type T should be passed.
2291   ///
2292   /// \param Lo - The classification for the parts of the type
2293   /// residing in the low word of the containing object.
2294   ///
2295   /// \param Hi - The classification for the parts of the type
2296   /// residing in the high word of the containing object.
2297   ///
2298   /// \param OffsetBase - The bit offset of this type in the
2299   /// containing object.  Some parameters are classified different
2300   /// depending on whether they straddle an eightbyte boundary.
2301   ///
2302   /// \param isNamedArg - Whether the argument in question is a "named"
2303   /// argument, as used in AMD64-ABI 3.5.7.
2304   ///
2305   /// \param IsRegCall - Whether the calling conversion is regcall.
2306   ///
2307   /// If a word is unused its result will be NoClass; if a type should
2308   /// be passed in Memory then at least the classification of \arg Lo
2309   /// will be Memory.
2310   ///
2311   /// The \arg Lo class will be NoClass iff the argument is ignored.
2312   ///
2313   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2314   /// also be ComplexX87.
2315   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2316                 bool isNamedArg, bool IsRegCall = false) const;
2317 
2318   llvm::Type *GetByteVectorType(QualType Ty) const;
2319   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2320                                  unsigned IROffset, QualType SourceTy,
2321                                  unsigned SourceOffset) const;
2322   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2323                                      unsigned IROffset, QualType SourceTy,
2324                                      unsigned SourceOffset) const;
2325 
2326   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2327   /// such that the argument will be returned in memory.
2328   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2329 
2330   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2331   /// such that the argument will be passed in memory.
2332   ///
2333   /// \param freeIntRegs - The number of free integer registers remaining
2334   /// available.
2335   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2336 
2337   ABIArgInfo classifyReturnType(QualType RetTy) const;
2338 
2339   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2340                                   unsigned &neededInt, unsigned &neededSSE,
2341                                   bool isNamedArg,
2342                                   bool IsRegCall = false) const;
2343 
2344   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2345                                        unsigned &NeededSSE,
2346                                        unsigned &MaxVectorWidth) const;
2347 
2348   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2349                                            unsigned &NeededSSE,
2350                                            unsigned &MaxVectorWidth) const;
2351 
2352   bool IsIllegalVectorType(QualType Ty) const;
2353 
2354   /// The 0.98 ABI revision clarified a lot of ambiguities,
2355   /// unfortunately in ways that were not always consistent with
2356   /// certain previous compilers.  In particular, platforms which
2357   /// required strict binary compatibility with older versions of GCC
2358   /// may need to exempt themselves.
2359   bool honorsRevision0_98() const {
2360     return !getTarget().getTriple().isOSDarwin();
2361   }
2362 
2363   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2364   /// classify it as INTEGER (for compatibility with older clang compilers).
2365   bool classifyIntegerMMXAsSSE() const {
2366     // Clang <= 3.8 did not do this.
2367     if (getContext().getLangOpts().getClangABICompat() <=
2368         LangOptions::ClangABI::Ver3_8)
2369       return false;
2370 
2371     const llvm::Triple &Triple = getTarget().getTriple();
2372     if (Triple.isOSDarwin() || Triple.isPS())
2373       return false;
2374     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2375       return false;
2376     return true;
2377   }
2378 
2379   // GCC classifies vectors of __int128 as memory.
2380   bool passInt128VectorsInMem() const {
2381     // Clang <= 9.0 did not do this.
2382     if (getContext().getLangOpts().getClangABICompat() <=
2383         LangOptions::ClangABI::Ver9)
2384       return false;
2385 
2386     const llvm::Triple &T = getTarget().getTriple();
2387     return T.isOSLinux() || T.isOSNetBSD();
2388   }
2389 
2390   X86AVXABILevel AVXLevel;
2391   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2392   // 64-bit hardware.
2393   bool Has64BitPointers;
2394 
2395 public:
2396   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
2397       SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2398       Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
2399   }
2400 
2401   bool isPassedUsingAVXType(QualType type) const {
2402     unsigned neededInt, neededSSE;
2403     // The freeIntRegs argument doesn't matter here.
2404     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2405                                            /*isNamedArg*/true);
2406     if (info.isDirect()) {
2407       llvm::Type *ty = info.getCoerceToType();
2408       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2409         return vectorTy->getPrimitiveSizeInBits().getFixedSize() > 128;
2410     }
2411     return false;
2412   }
2413 
2414   void computeInfo(CGFunctionInfo &FI) const override;
2415 
2416   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2417                     QualType Ty) const override;
2418   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2419                       QualType Ty) const override;
2420 
2421   bool has64BitPointers() const {
2422     return Has64BitPointers;
2423   }
2424 
2425   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
2426                                     bool asReturnValue) const override {
2427     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2428   }
2429   bool isSwiftErrorInRegister() const override {
2430     return true;
2431   }
2432 };
2433 
2434 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2435 class WinX86_64ABIInfo : public SwiftABIInfo {
2436 public:
2437   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2438       : SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2439         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2440 
2441   void computeInfo(CGFunctionInfo &FI) const override;
2442 
2443   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2444                     QualType Ty) const override;
2445 
2446   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2447     // FIXME: Assumes vectorcall is in use.
2448     return isX86VectorTypeForVectorCall(getContext(), Ty);
2449   }
2450 
2451   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2452                                          uint64_t NumMembers) const override {
2453     // FIXME: Assumes vectorcall is in use.
2454     return isX86VectorCallAggregateSmallEnough(NumMembers);
2455   }
2456 
2457   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
2458                                     bool asReturnValue) const override {
2459     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2460   }
2461 
2462   bool isSwiftErrorInRegister() const override {
2463     return true;
2464   }
2465 
2466 private:
2467   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2468                       bool IsVectorCall, bool IsRegCall) const;
2469   ABIArgInfo reclassifyHvaArgForVectorCall(QualType Ty, unsigned &FreeSSERegs,
2470                                            const ABIArgInfo &current) const;
2471 
2472   X86AVXABILevel AVXLevel;
2473 
2474   bool IsMingw64;
2475 };
2476 
2477 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2478 public:
2479   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2480       : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {}
2481 
2482   const X86_64ABIInfo &getABIInfo() const {
2483     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2484   }
2485 
2486   /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
2487   /// autoreleaseRV/retainRV and autoreleaseRV/unsafeClaimRV optimizations.
2488   bool markARCOptimizedReturnCallsAsNoTail() const override { return true; }
2489 
2490   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2491     return 7;
2492   }
2493 
2494   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2495                                llvm::Value *Address) const override {
2496     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2497 
2498     // 0-15 are the 16 integer registers.
2499     // 16 is %rip.
2500     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2501     return false;
2502   }
2503 
2504   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2505                                   StringRef Constraint,
2506                                   llvm::Type* Ty) const override {
2507     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2508   }
2509 
2510   bool isNoProtoCallVariadic(const CallArgList &args,
2511                              const FunctionNoProtoType *fnType) const override {
2512     // The default CC on x86-64 sets %al to the number of SSA
2513     // registers used, and GCC sets this when calling an unprototyped
2514     // function, so we override the default behavior.  However, don't do
2515     // that when AVX types are involved: the ABI explicitly states it is
2516     // undefined, and it doesn't work in practice because of how the ABI
2517     // defines varargs anyway.
2518     if (fnType->getCallConv() == CC_C) {
2519       bool HasAVXType = false;
2520       for (CallArgList::const_iterator
2521              it = args.begin(), ie = args.end(); it != ie; ++it) {
2522         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2523           HasAVXType = true;
2524           break;
2525         }
2526       }
2527 
2528       if (!HasAVXType)
2529         return true;
2530     }
2531 
2532     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2533   }
2534 
2535   llvm::Constant *
2536   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2537     unsigned Sig = (0xeb << 0) | // jmp rel8
2538                    (0x06 << 8) | //           .+0x08
2539                    ('v' << 16) |
2540                    ('2' << 24);
2541     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2542   }
2543 
2544   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2545                            CodeGen::CodeGenModule &CGM) const override {
2546     if (GV->isDeclaration())
2547       return;
2548     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2549       if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2550         llvm::Function *Fn = cast<llvm::Function>(GV);
2551         Fn->addFnAttr("stackrealign");
2552       }
2553 
2554       addX86InterruptAttrs(FD, GV, CGM);
2555     }
2556   }
2557 
2558   void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
2559                             const FunctionDecl *Caller,
2560                             const FunctionDecl *Callee,
2561                             const CallArgList &Args) const override;
2562 };
2563 
2564 static void initFeatureMaps(const ASTContext &Ctx,
2565                             llvm::StringMap<bool> &CallerMap,
2566                             const FunctionDecl *Caller,
2567                             llvm::StringMap<bool> &CalleeMap,
2568                             const FunctionDecl *Callee) {
2569   if (CalleeMap.empty() && CallerMap.empty()) {
2570     // The caller is potentially nullptr in the case where the call isn't in a
2571     // function.  In this case, the getFunctionFeatureMap ensures we just get
2572     // the TU level setting (since it cannot be modified by 'target'..
2573     Ctx.getFunctionFeatureMap(CallerMap, Caller);
2574     Ctx.getFunctionFeatureMap(CalleeMap, Callee);
2575   }
2576 }
2577 
2578 static bool checkAVXParamFeature(DiagnosticsEngine &Diag,
2579                                  SourceLocation CallLoc,
2580                                  const llvm::StringMap<bool> &CallerMap,
2581                                  const llvm::StringMap<bool> &CalleeMap,
2582                                  QualType Ty, StringRef Feature,
2583                                  bool IsArgument) {
2584   bool CallerHasFeat = CallerMap.lookup(Feature);
2585   bool CalleeHasFeat = CalleeMap.lookup(Feature);
2586   if (!CallerHasFeat && !CalleeHasFeat)
2587     return Diag.Report(CallLoc, diag::warn_avx_calling_convention)
2588            << IsArgument << Ty << Feature;
2589 
2590   // Mixing calling conventions here is very clearly an error.
2591   if (!CallerHasFeat || !CalleeHasFeat)
2592     return Diag.Report(CallLoc, diag::err_avx_calling_convention)
2593            << IsArgument << Ty << Feature;
2594 
2595   // Else, both caller and callee have the required feature, so there is no need
2596   // to diagnose.
2597   return false;
2598 }
2599 
2600 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx,
2601                           SourceLocation CallLoc,
2602                           const llvm::StringMap<bool> &CallerMap,
2603                           const llvm::StringMap<bool> &CalleeMap, QualType Ty,
2604                           bool IsArgument) {
2605   uint64_t Size = Ctx.getTypeSize(Ty);
2606   if (Size > 256)
2607     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty,
2608                                 "avx512f", IsArgument);
2609 
2610   if (Size > 128)
2611     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx",
2612                                 IsArgument);
2613 
2614   return false;
2615 }
2616 
2617 void X86_64TargetCodeGenInfo::checkFunctionCallABI(
2618     CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller,
2619     const FunctionDecl *Callee, const CallArgList &Args) const {
2620   llvm::StringMap<bool> CallerMap;
2621   llvm::StringMap<bool> CalleeMap;
2622   unsigned ArgIndex = 0;
2623 
2624   // We need to loop through the actual call arguments rather than the the
2625   // function's parameters, in case this variadic.
2626   for (const CallArg &Arg : Args) {
2627     // The "avx" feature changes how vectors >128 in size are passed. "avx512f"
2628     // additionally changes how vectors >256 in size are passed. Like GCC, we
2629     // warn when a function is called with an argument where this will change.
2630     // Unlike GCC, we also error when it is an obvious ABI mismatch, that is,
2631     // the caller and callee features are mismatched.
2632     // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can
2633     // change its ABI with attribute-target after this call.
2634     if (Arg.getType()->isVectorType() &&
2635         CGM.getContext().getTypeSize(Arg.getType()) > 128) {
2636       initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2637       QualType Ty = Arg.getType();
2638       // The CallArg seems to have desugared the type already, so for clearer
2639       // diagnostics, replace it with the type in the FunctionDecl if possible.
2640       if (ArgIndex < Callee->getNumParams())
2641         Ty = Callee->getParamDecl(ArgIndex)->getType();
2642 
2643       if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2644                         CalleeMap, Ty, /*IsArgument*/ true))
2645         return;
2646     }
2647     ++ArgIndex;
2648   }
2649 
2650   // Check return always, as we don't have a good way of knowing in codegen
2651   // whether this value is used, tail-called, etc.
2652   if (Callee->getReturnType()->isVectorType() &&
2653       CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) {
2654     initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2655     checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2656                   CalleeMap, Callee->getReturnType(),
2657                   /*IsArgument*/ false);
2658   }
2659 }
2660 
2661 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2662   // If the argument does not end in .lib, automatically add the suffix.
2663   // If the argument contains a space, enclose it in quotes.
2664   // This matches the behavior of MSVC.
2665   bool Quote = Lib.contains(' ');
2666   std::string ArgStr = Quote ? "\"" : "";
2667   ArgStr += Lib;
2668   if (!Lib.endswith_insensitive(".lib") && !Lib.endswith_insensitive(".a"))
2669     ArgStr += ".lib";
2670   ArgStr += Quote ? "\"" : "";
2671   return ArgStr;
2672 }
2673 
2674 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2675 public:
2676   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2677         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2678         unsigned NumRegisterParameters)
2679     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2680         Win32StructABI, NumRegisterParameters, false) {}
2681 
2682   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2683                            CodeGen::CodeGenModule &CGM) const override;
2684 
2685   void getDependentLibraryOption(llvm::StringRef Lib,
2686                                  llvm::SmallString<24> &Opt) const override {
2687     Opt = "/DEFAULTLIB:";
2688     Opt += qualifyWindowsLibrary(Lib);
2689   }
2690 
2691   void getDetectMismatchOption(llvm::StringRef Name,
2692                                llvm::StringRef Value,
2693                                llvm::SmallString<32> &Opt) const override {
2694     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2695   }
2696 };
2697 
2698 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2699                                           CodeGen::CodeGenModule &CGM) {
2700   if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
2701 
2702     if (CGM.getCodeGenOpts().StackProbeSize != 4096)
2703       Fn->addFnAttr("stack-probe-size",
2704                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2705     if (CGM.getCodeGenOpts().NoStackArgProbe)
2706       Fn->addFnAttr("no-stack-arg-probe");
2707   }
2708 }
2709 
2710 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2711     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2712   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2713   if (GV->isDeclaration())
2714     return;
2715   addStackProbeTargetAttributes(D, GV, CGM);
2716 }
2717 
2718 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2719 public:
2720   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2721                              X86AVXABILevel AVXLevel)
2722       : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {}
2723 
2724   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2725                            CodeGen::CodeGenModule &CGM) const override;
2726 
2727   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2728     return 7;
2729   }
2730 
2731   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2732                                llvm::Value *Address) const override {
2733     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2734 
2735     // 0-15 are the 16 integer registers.
2736     // 16 is %rip.
2737     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2738     return false;
2739   }
2740 
2741   void getDependentLibraryOption(llvm::StringRef Lib,
2742                                  llvm::SmallString<24> &Opt) const override {
2743     Opt = "/DEFAULTLIB:";
2744     Opt += qualifyWindowsLibrary(Lib);
2745   }
2746 
2747   void getDetectMismatchOption(llvm::StringRef Name,
2748                                llvm::StringRef Value,
2749                                llvm::SmallString<32> &Opt) const override {
2750     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2751   }
2752 };
2753 
2754 void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2755     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2756   TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2757   if (GV->isDeclaration())
2758     return;
2759   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2760     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2761       llvm::Function *Fn = cast<llvm::Function>(GV);
2762       Fn->addFnAttr("stackrealign");
2763     }
2764 
2765     addX86InterruptAttrs(FD, GV, CGM);
2766   }
2767 
2768   addStackProbeTargetAttributes(D, GV, CGM);
2769 }
2770 }
2771 
2772 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2773                               Class &Hi) const {
2774   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2775   //
2776   // (a) If one of the classes is Memory, the whole argument is passed in
2777   //     memory.
2778   //
2779   // (b) If X87UP is not preceded by X87, the whole argument is passed in
2780   //     memory.
2781   //
2782   // (c) If the size of the aggregate exceeds two eightbytes and the first
2783   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2784   //     argument is passed in memory. NOTE: This is necessary to keep the
2785   //     ABI working for processors that don't support the __m256 type.
2786   //
2787   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2788   //
2789   // Some of these are enforced by the merging logic.  Others can arise
2790   // only with unions; for example:
2791   //   union { _Complex double; unsigned; }
2792   //
2793   // Note that clauses (b) and (c) were added in 0.98.
2794   //
2795   if (Hi == Memory)
2796     Lo = Memory;
2797   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2798     Lo = Memory;
2799   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2800     Lo = Memory;
2801   if (Hi == SSEUp && Lo != SSE)
2802     Hi = SSE;
2803 }
2804 
2805 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2806   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2807   // classified recursively so that always two fields are
2808   // considered. The resulting class is calculated according to
2809   // the classes of the fields in the eightbyte:
2810   //
2811   // (a) If both classes are equal, this is the resulting class.
2812   //
2813   // (b) If one of the classes is NO_CLASS, the resulting class is
2814   // the other class.
2815   //
2816   // (c) If one of the classes is MEMORY, the result is the MEMORY
2817   // class.
2818   //
2819   // (d) If one of the classes is INTEGER, the result is the
2820   // INTEGER.
2821   //
2822   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2823   // MEMORY is used as class.
2824   //
2825   // (f) Otherwise class SSE is used.
2826 
2827   // Accum should never be memory (we should have returned) or
2828   // ComplexX87 (because this cannot be passed in a structure).
2829   assert((Accum != Memory && Accum != ComplexX87) &&
2830          "Invalid accumulated classification during merge.");
2831   if (Accum == Field || Field == NoClass)
2832     return Accum;
2833   if (Field == Memory)
2834     return Memory;
2835   if (Accum == NoClass)
2836     return Field;
2837   if (Accum == Integer || Field == Integer)
2838     return Integer;
2839   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2840       Accum == X87 || Accum == X87Up)
2841     return Memory;
2842   return SSE;
2843 }
2844 
2845 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
2846                              Class &Hi, bool isNamedArg, bool IsRegCall) const {
2847   // FIXME: This code can be simplified by introducing a simple value class for
2848   // Class pairs with appropriate constructor methods for the various
2849   // situations.
2850 
2851   // FIXME: Some of the split computations are wrong; unaligned vectors
2852   // shouldn't be passed in registers for example, so there is no chance they
2853   // can straddle an eightbyte. Verify & simplify.
2854 
2855   Lo = Hi = NoClass;
2856 
2857   Class &Current = OffsetBase < 64 ? Lo : Hi;
2858   Current = Memory;
2859 
2860   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2861     BuiltinType::Kind k = BT->getKind();
2862 
2863     if (k == BuiltinType::Void) {
2864       Current = NoClass;
2865     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2866       Lo = Integer;
2867       Hi = Integer;
2868     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2869       Current = Integer;
2870     } else if (k == BuiltinType::Float || k == BuiltinType::Double ||
2871                k == BuiltinType::Float16) {
2872       Current = SSE;
2873     } else if (k == BuiltinType::LongDouble) {
2874       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2875       if (LDF == &llvm::APFloat::IEEEquad()) {
2876         Lo = SSE;
2877         Hi = SSEUp;
2878       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2879         Lo = X87;
2880         Hi = X87Up;
2881       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2882         Current = SSE;
2883       } else
2884         llvm_unreachable("unexpected long double representation!");
2885     }
2886     // FIXME: _Decimal32 and _Decimal64 are SSE.
2887     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2888     return;
2889   }
2890 
2891   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2892     // Classify the underlying integer type.
2893     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2894     return;
2895   }
2896 
2897   if (Ty->hasPointerRepresentation()) {
2898     Current = Integer;
2899     return;
2900   }
2901 
2902   if (Ty->isMemberPointerType()) {
2903     if (Ty->isMemberFunctionPointerType()) {
2904       if (Has64BitPointers) {
2905         // If Has64BitPointers, this is an {i64, i64}, so classify both
2906         // Lo and Hi now.
2907         Lo = Hi = Integer;
2908       } else {
2909         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2910         // straddles an eightbyte boundary, Hi should be classified as well.
2911         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2912         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2913         if (EB_FuncPtr != EB_ThisAdj) {
2914           Lo = Hi = Integer;
2915         } else {
2916           Current = Integer;
2917         }
2918       }
2919     } else {
2920       Current = Integer;
2921     }
2922     return;
2923   }
2924 
2925   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2926     uint64_t Size = getContext().getTypeSize(VT);
2927     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2928       // gcc passes the following as integer:
2929       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2930       // 2 bytes - <2 x char>, <1 x short>
2931       // 1 byte  - <1 x char>
2932       Current = Integer;
2933 
2934       // If this type crosses an eightbyte boundary, it should be
2935       // split.
2936       uint64_t EB_Lo = (OffsetBase) / 64;
2937       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2938       if (EB_Lo != EB_Hi)
2939         Hi = Lo;
2940     } else if (Size == 64) {
2941       QualType ElementType = VT->getElementType();
2942 
2943       // gcc passes <1 x double> in memory. :(
2944       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2945         return;
2946 
2947       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2948       // pass them as integer.  For platforms where clang is the de facto
2949       // platform compiler, we must continue to use integer.
2950       if (!classifyIntegerMMXAsSSE() &&
2951           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2952            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2953            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2954            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2955         Current = Integer;
2956       else
2957         Current = SSE;
2958 
2959       // If this type crosses an eightbyte boundary, it should be
2960       // split.
2961       if (OffsetBase && OffsetBase != 64)
2962         Hi = Lo;
2963     } else if (Size == 128 ||
2964                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2965       QualType ElementType = VT->getElementType();
2966 
2967       // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
2968       if (passInt128VectorsInMem() && Size != 128 &&
2969           (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
2970            ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
2971         return;
2972 
2973       // Arguments of 256-bits are split into four eightbyte chunks. The
2974       // least significant one belongs to class SSE and all the others to class
2975       // SSEUP. The original Lo and Hi design considers that types can't be
2976       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2977       // This design isn't correct for 256-bits, but since there're no cases
2978       // where the upper parts would need to be inspected, avoid adding
2979       // complexity and just consider Hi to match the 64-256 part.
2980       //
2981       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2982       // registers if they are "named", i.e. not part of the "..." of a
2983       // variadic function.
2984       //
2985       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2986       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2987       Lo = SSE;
2988       Hi = SSEUp;
2989     }
2990     return;
2991   }
2992 
2993   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2994     QualType ET = getContext().getCanonicalType(CT->getElementType());
2995 
2996     uint64_t Size = getContext().getTypeSize(Ty);
2997     if (ET->isIntegralOrEnumerationType()) {
2998       if (Size <= 64)
2999         Current = Integer;
3000       else if (Size <= 128)
3001         Lo = Hi = Integer;
3002     } else if (ET->isFloat16Type() || ET == getContext().FloatTy) {
3003       Current = SSE;
3004     } else if (ET == getContext().DoubleTy) {
3005       Lo = Hi = SSE;
3006     } else if (ET == getContext().LongDoubleTy) {
3007       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3008       if (LDF == &llvm::APFloat::IEEEquad())
3009         Current = Memory;
3010       else if (LDF == &llvm::APFloat::x87DoubleExtended())
3011         Current = ComplexX87;
3012       else if (LDF == &llvm::APFloat::IEEEdouble())
3013         Lo = Hi = SSE;
3014       else
3015         llvm_unreachable("unexpected long double representation!");
3016     }
3017 
3018     // If this complex type crosses an eightbyte boundary then it
3019     // should be split.
3020     uint64_t EB_Real = (OffsetBase) / 64;
3021     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
3022     if (Hi == NoClass && EB_Real != EB_Imag)
3023       Hi = Lo;
3024 
3025     return;
3026   }
3027 
3028   if (const auto *EITy = Ty->getAs<BitIntType>()) {
3029     if (EITy->getNumBits() <= 64)
3030       Current = Integer;
3031     else if (EITy->getNumBits() <= 128)
3032       Lo = Hi = Integer;
3033     // Larger values need to get passed in memory.
3034     return;
3035   }
3036 
3037   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3038     // Arrays are treated like structures.
3039 
3040     uint64_t Size = getContext().getTypeSize(Ty);
3041 
3042     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3043     // than eight eightbytes, ..., it has class MEMORY.
3044     // regcall ABI doesn't have limitation to an object. The only limitation
3045     // is the free registers, which will be checked in computeInfo.
3046     if (!IsRegCall && Size > 512)
3047       return;
3048 
3049     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
3050     // fields, it has class MEMORY.
3051     //
3052     // Only need to check alignment of array base.
3053     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
3054       return;
3055 
3056     // Otherwise implement simplified merge. We could be smarter about
3057     // this, but it isn't worth it and would be harder to verify.
3058     Current = NoClass;
3059     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
3060     uint64_t ArraySize = AT->getSize().getZExtValue();
3061 
3062     // The only case a 256-bit wide vector could be used is when the array
3063     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
3064     // to work for sizes wider than 128, early check and fallback to memory.
3065     //
3066     if (Size > 128 &&
3067         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
3068       return;
3069 
3070     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
3071       Class FieldLo, FieldHi;
3072       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
3073       Lo = merge(Lo, FieldLo);
3074       Hi = merge(Hi, FieldHi);
3075       if (Lo == Memory || Hi == Memory)
3076         break;
3077     }
3078 
3079     postMerge(Size, Lo, Hi);
3080     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
3081     return;
3082   }
3083 
3084   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3085     uint64_t Size = getContext().getTypeSize(Ty);
3086 
3087     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3088     // than eight eightbytes, ..., it has class MEMORY.
3089     if (Size > 512)
3090       return;
3091 
3092     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
3093     // copy constructor or a non-trivial destructor, it is passed by invisible
3094     // reference.
3095     if (getRecordArgABI(RT, getCXXABI()))
3096       return;
3097 
3098     const RecordDecl *RD = RT->getDecl();
3099 
3100     // Assume variable sized types are passed in memory.
3101     if (RD->hasFlexibleArrayMember())
3102       return;
3103 
3104     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3105 
3106     // Reset Lo class, this will be recomputed.
3107     Current = NoClass;
3108 
3109     // If this is a C++ record, classify the bases first.
3110     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3111       for (const auto &I : CXXRD->bases()) {
3112         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3113                "Unexpected base class!");
3114         const auto *Base =
3115             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3116 
3117         // Classify this field.
3118         //
3119         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
3120         // single eightbyte, each is classified separately. Each eightbyte gets
3121         // initialized to class NO_CLASS.
3122         Class FieldLo, FieldHi;
3123         uint64_t Offset =
3124           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
3125         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
3126         Lo = merge(Lo, FieldLo);
3127         Hi = merge(Hi, FieldHi);
3128         if (Lo == Memory || Hi == Memory) {
3129           postMerge(Size, Lo, Hi);
3130           return;
3131         }
3132       }
3133     }
3134 
3135     // Classify the fields one at a time, merging the results.
3136     unsigned idx = 0;
3137     bool UseClang11Compat = getContext().getLangOpts().getClangABICompat() <=
3138                                 LangOptions::ClangABI::Ver11 ||
3139                             getContext().getTargetInfo().getTriple().isPS();
3140     bool IsUnion = RT->isUnionType() && !UseClang11Compat;
3141 
3142     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3143            i != e; ++i, ++idx) {
3144       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3145       bool BitField = i->isBitField();
3146 
3147       // Ignore padding bit-fields.
3148       if (BitField && i->isUnnamedBitfield())
3149         continue;
3150 
3151       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
3152       // eight eightbytes, or it contains unaligned fields, it has class MEMORY.
3153       //
3154       // The only case a 256-bit or a 512-bit wide vector could be used is when
3155       // the struct contains a single 256-bit or 512-bit element. Early check
3156       // and fallback to memory.
3157       //
3158       // FIXME: Extended the Lo and Hi logic properly to work for size wider
3159       // than 128.
3160       if (Size > 128 &&
3161           ((!IsUnion && Size != getContext().getTypeSize(i->getType())) ||
3162            Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
3163         Lo = Memory;
3164         postMerge(Size, Lo, Hi);
3165         return;
3166       }
3167       // Note, skip this test for bit-fields, see below.
3168       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
3169         Lo = Memory;
3170         postMerge(Size, Lo, Hi);
3171         return;
3172       }
3173 
3174       // Classify this field.
3175       //
3176       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
3177       // exceeds a single eightbyte, each is classified
3178       // separately. Each eightbyte gets initialized to class
3179       // NO_CLASS.
3180       Class FieldLo, FieldHi;
3181 
3182       // Bit-fields require special handling, they do not force the
3183       // structure to be passed in memory even if unaligned, and
3184       // therefore they can straddle an eightbyte.
3185       if (BitField) {
3186         assert(!i->isUnnamedBitfield());
3187         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3188         uint64_t Size = i->getBitWidthValue(getContext());
3189 
3190         uint64_t EB_Lo = Offset / 64;
3191         uint64_t EB_Hi = (Offset + Size - 1) / 64;
3192 
3193         if (EB_Lo) {
3194           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
3195           FieldLo = NoClass;
3196           FieldHi = Integer;
3197         } else {
3198           FieldLo = Integer;
3199           FieldHi = EB_Hi ? Integer : NoClass;
3200         }
3201       } else
3202         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
3203       Lo = merge(Lo, FieldLo);
3204       Hi = merge(Hi, FieldHi);
3205       if (Lo == Memory || Hi == Memory)
3206         break;
3207     }
3208 
3209     postMerge(Size, Lo, Hi);
3210   }
3211 }
3212 
3213 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
3214   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3215   // place naturally.
3216   if (!isAggregateTypeForABI(Ty)) {
3217     // Treat an enum type as its underlying type.
3218     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3219       Ty = EnumTy->getDecl()->getIntegerType();
3220 
3221     if (Ty->isBitIntType())
3222       return getNaturalAlignIndirect(Ty);
3223 
3224     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3225                                               : ABIArgInfo::getDirect());
3226   }
3227 
3228   return getNaturalAlignIndirect(Ty);
3229 }
3230 
3231 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
3232   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
3233     uint64_t Size = getContext().getTypeSize(VecTy);
3234     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
3235     if (Size <= 64 || Size > LargestVector)
3236       return true;
3237     QualType EltTy = VecTy->getElementType();
3238     if (passInt128VectorsInMem() &&
3239         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
3240          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
3241       return true;
3242   }
3243 
3244   return false;
3245 }
3246 
3247 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
3248                                             unsigned freeIntRegs) const {
3249   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3250   // place naturally.
3251   //
3252   // This assumption is optimistic, as there could be free registers available
3253   // when we need to pass this argument in memory, and LLVM could try to pass
3254   // the argument in the free register. This does not seem to happen currently,
3255   // but this code would be much safer if we could mark the argument with
3256   // 'onstack'. See PR12193.
3257   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
3258       !Ty->isBitIntType()) {
3259     // Treat an enum type as its underlying type.
3260     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3261       Ty = EnumTy->getDecl()->getIntegerType();
3262 
3263     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3264                                               : ABIArgInfo::getDirect());
3265   }
3266 
3267   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3268     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3269 
3270   // Compute the byval alignment. We specify the alignment of the byval in all
3271   // cases so that the mid-level optimizer knows the alignment of the byval.
3272   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
3273 
3274   // Attempt to avoid passing indirect results using byval when possible. This
3275   // is important for good codegen.
3276   //
3277   // We do this by coercing the value into a scalar type which the backend can
3278   // handle naturally (i.e., without using byval).
3279   //
3280   // For simplicity, we currently only do this when we have exhausted all of the
3281   // free integer registers. Doing this when there are free integer registers
3282   // would require more care, as we would have to ensure that the coerced value
3283   // did not claim the unused register. That would require either reording the
3284   // arguments to the function (so that any subsequent inreg values came first),
3285   // or only doing this optimization when there were no following arguments that
3286   // might be inreg.
3287   //
3288   // We currently expect it to be rare (particularly in well written code) for
3289   // arguments to be passed on the stack when there are still free integer
3290   // registers available (this would typically imply large structs being passed
3291   // by value), so this seems like a fair tradeoff for now.
3292   //
3293   // We can revisit this if the backend grows support for 'onstack' parameter
3294   // attributes. See PR12193.
3295   if (freeIntRegs == 0) {
3296     uint64_t Size = getContext().getTypeSize(Ty);
3297 
3298     // If this type fits in an eightbyte, coerce it into the matching integral
3299     // type, which will end up on the stack (with alignment 8).
3300     if (Align == 8 && Size <= 64)
3301       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3302                                                           Size));
3303   }
3304 
3305   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
3306 }
3307 
3308 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3309 /// register. Pick an LLVM IR type that will be passed as a vector register.
3310 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3311   // Wrapper structs/arrays that only contain vectors are passed just like
3312   // vectors; strip them off if present.
3313   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3314     Ty = QualType(InnerTy, 0);
3315 
3316   llvm::Type *IRType = CGT.ConvertType(Ty);
3317   if (isa<llvm::VectorType>(IRType)) {
3318     // Don't pass vXi128 vectors in their native type, the backend can't
3319     // legalize them.
3320     if (passInt128VectorsInMem() &&
3321         cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
3322       // Use a vXi64 vector.
3323       uint64_t Size = getContext().getTypeSize(Ty);
3324       return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3325                                         Size / 64);
3326     }
3327 
3328     return IRType;
3329   }
3330 
3331   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3332     return IRType;
3333 
3334   // We couldn't find the preferred IR vector type for 'Ty'.
3335   uint64_t Size = getContext().getTypeSize(Ty);
3336   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3337 
3338 
3339   // Return a LLVM IR vector type based on the size of 'Ty'.
3340   return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3341                                     Size / 64);
3342 }
3343 
3344 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3345 /// is known to either be off the end of the specified type or being in
3346 /// alignment padding.  The user type specified is known to be at most 128 bits
3347 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3348 /// classification that put one of the two halves in the INTEGER class.
3349 ///
3350 /// It is conservatively correct to return false.
3351 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3352                                   unsigned EndBit, ASTContext &Context) {
3353   // If the bytes being queried are off the end of the type, there is no user
3354   // data hiding here.  This handles analysis of builtins, vectors and other
3355   // types that don't contain interesting padding.
3356   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3357   if (TySize <= StartBit)
3358     return true;
3359 
3360   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3361     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3362     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3363 
3364     // Check each element to see if the element overlaps with the queried range.
3365     for (unsigned i = 0; i != NumElts; ++i) {
3366       // If the element is after the span we care about, then we're done..
3367       unsigned EltOffset = i*EltSize;
3368       if (EltOffset >= EndBit) break;
3369 
3370       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3371       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3372                                  EndBit-EltOffset, Context))
3373         return false;
3374     }
3375     // If it overlaps no elements, then it is safe to process as padding.
3376     return true;
3377   }
3378 
3379   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3380     const RecordDecl *RD = RT->getDecl();
3381     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3382 
3383     // If this is a C++ record, check the bases first.
3384     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3385       for (const auto &I : CXXRD->bases()) {
3386         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3387                "Unexpected base class!");
3388         const auto *Base =
3389             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3390 
3391         // If the base is after the span we care about, ignore it.
3392         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3393         if (BaseOffset >= EndBit) continue;
3394 
3395         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3396         if (!BitsContainNoUserData(I.getType(), BaseStart,
3397                                    EndBit-BaseOffset, Context))
3398           return false;
3399       }
3400     }
3401 
3402     // Verify that no field has data that overlaps the region of interest.  Yes
3403     // this could be sped up a lot by being smarter about queried fields,
3404     // however we're only looking at structs up to 16 bytes, so we don't care
3405     // much.
3406     unsigned idx = 0;
3407     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3408          i != e; ++i, ++idx) {
3409       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3410 
3411       // If we found a field after the region we care about, then we're done.
3412       if (FieldOffset >= EndBit) break;
3413 
3414       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3415       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3416                                  Context))
3417         return false;
3418     }
3419 
3420     // If nothing in this record overlapped the area of interest, then we're
3421     // clean.
3422     return true;
3423   }
3424 
3425   return false;
3426 }
3427 
3428 /// getFPTypeAtOffset - Return a floating point type at the specified offset.
3429 static llvm::Type *getFPTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3430                                      const llvm::DataLayout &TD) {
3431   if (IROffset == 0 && IRType->isFloatingPointTy())
3432     return IRType;
3433 
3434   // If this is a struct, recurse into the field at the specified offset.
3435   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3436     if (!STy->getNumContainedTypes())
3437       return nullptr;
3438 
3439     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3440     unsigned Elt = SL->getElementContainingOffset(IROffset);
3441     IROffset -= SL->getElementOffset(Elt);
3442     return getFPTypeAtOffset(STy->getElementType(Elt), IROffset, TD);
3443   }
3444 
3445   // If this is an array, recurse into the field at the specified offset.
3446   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3447     llvm::Type *EltTy = ATy->getElementType();
3448     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3449     IROffset -= IROffset / EltSize * EltSize;
3450     return getFPTypeAtOffset(EltTy, IROffset, TD);
3451   }
3452 
3453   return nullptr;
3454 }
3455 
3456 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3457 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3458 llvm::Type *X86_64ABIInfo::
3459 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3460                    QualType SourceTy, unsigned SourceOffset) const {
3461   const llvm::DataLayout &TD = getDataLayout();
3462   unsigned SourceSize =
3463       (unsigned)getContext().getTypeSize(SourceTy) / 8 - SourceOffset;
3464   llvm::Type *T0 = getFPTypeAtOffset(IRType, IROffset, TD);
3465   if (!T0 || T0->isDoubleTy())
3466     return llvm::Type::getDoubleTy(getVMContext());
3467 
3468   // Get the adjacent FP type.
3469   llvm::Type *T1 = nullptr;
3470   unsigned T0Size = TD.getTypeAllocSize(T0);
3471   if (SourceSize > T0Size)
3472       T1 = getFPTypeAtOffset(IRType, IROffset + T0Size, TD);
3473   if (T1 == nullptr) {
3474     // Check if IRType is a half + float. float type will be in IROffset+4 due
3475     // to its alignment.
3476     if (T0->isHalfTy() && SourceSize > 4)
3477       T1 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
3478     // If we can't get a second FP type, return a simple half or float.
3479     // avx512fp16-abi.c:pr51813_2 shows it works to return float for
3480     // {float, i8} too.
3481     if (T1 == nullptr)
3482       return T0;
3483   }
3484 
3485   if (T0->isFloatTy() && T1->isFloatTy())
3486     return llvm::FixedVectorType::get(T0, 2);
3487 
3488   if (T0->isHalfTy() && T1->isHalfTy()) {
3489     llvm::Type *T2 = nullptr;
3490     if (SourceSize > 4)
3491       T2 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
3492     if (T2 == nullptr)
3493       return llvm::FixedVectorType::get(T0, 2);
3494     return llvm::FixedVectorType::get(T0, 4);
3495   }
3496 
3497   if (T0->isHalfTy() || T1->isHalfTy())
3498     return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()), 4);
3499 
3500   return llvm::Type::getDoubleTy(getVMContext());
3501 }
3502 
3503 
3504 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3505 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3506 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3507 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3508 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3509 /// etc).
3510 ///
3511 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3512 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3513 /// the 8-byte value references.  PrefType may be null.
3514 ///
3515 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3516 /// an offset into this that we're processing (which is always either 0 or 8).
3517 ///
3518 llvm::Type *X86_64ABIInfo::
3519 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3520                        QualType SourceTy, unsigned SourceOffset) const {
3521   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3522   // returning an 8-byte unit starting with it.  See if we can safely use it.
3523   if (IROffset == 0) {
3524     // Pointers and int64's always fill the 8-byte unit.
3525     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3526         IRType->isIntegerTy(64))
3527       return IRType;
3528 
3529     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3530     // goodness in the source type is just tail padding.  This is allowed to
3531     // kick in for struct {double,int} on the int, but not on
3532     // struct{double,int,int} because we wouldn't return the second int.  We
3533     // have to do this analysis on the source type because we can't depend on
3534     // unions being lowered a specific way etc.
3535     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3536         IRType->isIntegerTy(32) ||
3537         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3538       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3539           cast<llvm::IntegerType>(IRType)->getBitWidth();
3540 
3541       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3542                                 SourceOffset*8+64, getContext()))
3543         return IRType;
3544     }
3545   }
3546 
3547   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3548     // If this is a struct, recurse into the field at the specified offset.
3549     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3550     if (IROffset < SL->getSizeInBytes()) {
3551       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3552       IROffset -= SL->getElementOffset(FieldIdx);
3553 
3554       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3555                                     SourceTy, SourceOffset);
3556     }
3557   }
3558 
3559   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3560     llvm::Type *EltTy = ATy->getElementType();
3561     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3562     unsigned EltOffset = IROffset/EltSize*EltSize;
3563     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3564                                   SourceOffset);
3565   }
3566 
3567   // Okay, we don't have any better idea of what to pass, so we pass this in an
3568   // integer register that isn't too big to fit the rest of the struct.
3569   unsigned TySizeInBytes =
3570     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3571 
3572   assert(TySizeInBytes != SourceOffset && "Empty field?");
3573 
3574   // It is always safe to classify this as an integer type up to i64 that
3575   // isn't larger than the structure.
3576   return llvm::IntegerType::get(getVMContext(),
3577                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3578 }
3579 
3580 
3581 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3582 /// be used as elements of a two register pair to pass or return, return a
3583 /// first class aggregate to represent them.  For example, if the low part of
3584 /// a by-value argument should be passed as i32* and the high part as float,
3585 /// return {i32*, float}.
3586 static llvm::Type *
3587 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3588                            const llvm::DataLayout &TD) {
3589   // In order to correctly satisfy the ABI, we need to the high part to start
3590   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3591   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3592   // the second element at offset 8.  Check for this:
3593   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3594   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3595   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3596   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3597 
3598   // To handle this, we have to increase the size of the low part so that the
3599   // second element will start at an 8 byte offset.  We can't increase the size
3600   // of the second element because it might make us access off the end of the
3601   // struct.
3602   if (HiStart != 8) {
3603     // There are usually two sorts of types the ABI generation code can produce
3604     // for the low part of a pair that aren't 8 bytes in size: half, float or
3605     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3606     // NaCl).
3607     // Promote these to a larger type.
3608     if (Lo->isHalfTy() || Lo->isFloatTy())
3609       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3610     else {
3611       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3612              && "Invalid/unknown lo type");
3613       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3614     }
3615   }
3616 
3617   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3618 
3619   // Verify that the second element is at an 8-byte offset.
3620   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3621          "Invalid x86-64 argument pair!");
3622   return Result;
3623 }
3624 
3625 ABIArgInfo X86_64ABIInfo::
3626 classifyReturnType(QualType RetTy) const {
3627   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3628   // classification algorithm.
3629   X86_64ABIInfo::Class Lo, Hi;
3630   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3631 
3632   // Check some invariants.
3633   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3634   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3635 
3636   llvm::Type *ResType = nullptr;
3637   switch (Lo) {
3638   case NoClass:
3639     if (Hi == NoClass)
3640       return ABIArgInfo::getIgnore();
3641     // If the low part is just padding, it takes no register, leave ResType
3642     // null.
3643     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3644            "Unknown missing lo part");
3645     break;
3646 
3647   case SSEUp:
3648   case X87Up:
3649     llvm_unreachable("Invalid classification for lo word.");
3650 
3651     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3652     // hidden argument.
3653   case Memory:
3654     return getIndirectReturnResult(RetTy);
3655 
3656     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3657     // available register of the sequence %rax, %rdx is used.
3658   case Integer:
3659     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3660 
3661     // If we have a sign or zero extended integer, make sure to return Extend
3662     // so that the parameter gets the right LLVM IR attributes.
3663     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3664       // Treat an enum type as its underlying type.
3665       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3666         RetTy = EnumTy->getDecl()->getIntegerType();
3667 
3668       if (RetTy->isIntegralOrEnumerationType() &&
3669           isPromotableIntegerTypeForABI(RetTy))
3670         return ABIArgInfo::getExtend(RetTy);
3671     }
3672     break;
3673 
3674     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3675     // available SSE register of the sequence %xmm0, %xmm1 is used.
3676   case SSE:
3677     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3678     break;
3679 
3680     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3681     // returned on the X87 stack in %st0 as 80-bit x87 number.
3682   case X87:
3683     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3684     break;
3685 
3686     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3687     // part of the value is returned in %st0 and the imaginary part in
3688     // %st1.
3689   case ComplexX87:
3690     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3691     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3692                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3693     break;
3694   }
3695 
3696   llvm::Type *HighPart = nullptr;
3697   switch (Hi) {
3698     // Memory was handled previously and X87 should
3699     // never occur as a hi class.
3700   case Memory:
3701   case X87:
3702     llvm_unreachable("Invalid classification for hi word.");
3703 
3704   case ComplexX87: // Previously handled.
3705   case NoClass:
3706     break;
3707 
3708   case Integer:
3709     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3710     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3711       return ABIArgInfo::getDirect(HighPart, 8);
3712     break;
3713   case SSE:
3714     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3715     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3716       return ABIArgInfo::getDirect(HighPart, 8);
3717     break;
3718 
3719     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3720     // is passed in the next available eightbyte chunk if the last used
3721     // vector register.
3722     //
3723     // SSEUP should always be preceded by SSE, just widen.
3724   case SSEUp:
3725     assert(Lo == SSE && "Unexpected SSEUp classification.");
3726     ResType = GetByteVectorType(RetTy);
3727     break;
3728 
3729     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3730     // returned together with the previous X87 value in %st0.
3731   case X87Up:
3732     // If X87Up is preceded by X87, we don't need to do
3733     // anything. However, in some cases with unions it may not be
3734     // preceded by X87. In such situations we follow gcc and pass the
3735     // extra bits in an SSE reg.
3736     if (Lo != X87) {
3737       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3738       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3739         return ABIArgInfo::getDirect(HighPart, 8);
3740     }
3741     break;
3742   }
3743 
3744   // If a high part was specified, merge it together with the low part.  It is
3745   // known to pass in the high eightbyte of the result.  We do this by forming a
3746   // first class struct aggregate with the high and low part: {low, high}
3747   if (HighPart)
3748     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3749 
3750   return ABIArgInfo::getDirect(ResType);
3751 }
3752 
3753 ABIArgInfo
3754 X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned freeIntRegs,
3755                                     unsigned &neededInt, unsigned &neededSSE,
3756                                     bool isNamedArg, bool IsRegCall) const {
3757   Ty = useFirstFieldIfTransparentUnion(Ty);
3758 
3759   X86_64ABIInfo::Class Lo, Hi;
3760   classify(Ty, 0, Lo, Hi, isNamedArg, IsRegCall);
3761 
3762   // Check some invariants.
3763   // FIXME: Enforce these by construction.
3764   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3765   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3766 
3767   neededInt = 0;
3768   neededSSE = 0;
3769   llvm::Type *ResType = nullptr;
3770   switch (Lo) {
3771   case NoClass:
3772     if (Hi == NoClass)
3773       return ABIArgInfo::getIgnore();
3774     // If the low part is just padding, it takes no register, leave ResType
3775     // null.
3776     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3777            "Unknown missing lo part");
3778     break;
3779 
3780     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3781     // on the stack.
3782   case Memory:
3783 
3784     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3785     // COMPLEX_X87, it is passed in memory.
3786   case X87:
3787   case ComplexX87:
3788     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3789       ++neededInt;
3790     return getIndirectResult(Ty, freeIntRegs);
3791 
3792   case SSEUp:
3793   case X87Up:
3794     llvm_unreachable("Invalid classification for lo word.");
3795 
3796     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3797     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3798     // and %r9 is used.
3799   case Integer:
3800     ++neededInt;
3801 
3802     // Pick an 8-byte type based on the preferred type.
3803     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3804 
3805     // If we have a sign or zero extended integer, make sure to return Extend
3806     // so that the parameter gets the right LLVM IR attributes.
3807     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3808       // Treat an enum type as its underlying type.
3809       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3810         Ty = EnumTy->getDecl()->getIntegerType();
3811 
3812       if (Ty->isIntegralOrEnumerationType() &&
3813           isPromotableIntegerTypeForABI(Ty))
3814         return ABIArgInfo::getExtend(Ty);
3815     }
3816 
3817     break;
3818 
3819     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3820     // available SSE register is used, the registers are taken in the
3821     // order from %xmm0 to %xmm7.
3822   case SSE: {
3823     llvm::Type *IRType = CGT.ConvertType(Ty);
3824     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3825     ++neededSSE;
3826     break;
3827   }
3828   }
3829 
3830   llvm::Type *HighPart = nullptr;
3831   switch (Hi) {
3832     // Memory was handled previously, ComplexX87 and X87 should
3833     // never occur as hi classes, and X87Up must be preceded by X87,
3834     // which is passed in memory.
3835   case Memory:
3836   case X87:
3837   case ComplexX87:
3838     llvm_unreachable("Invalid classification for hi word.");
3839 
3840   case NoClass: break;
3841 
3842   case Integer:
3843     ++neededInt;
3844     // Pick an 8-byte type based on the preferred type.
3845     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3846 
3847     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3848       return ABIArgInfo::getDirect(HighPart, 8);
3849     break;
3850 
3851     // X87Up generally doesn't occur here (long double is passed in
3852     // memory), except in situations involving unions.
3853   case X87Up:
3854   case SSE:
3855     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3856 
3857     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3858       return ABIArgInfo::getDirect(HighPart, 8);
3859 
3860     ++neededSSE;
3861     break;
3862 
3863     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3864     // eightbyte is passed in the upper half of the last used SSE
3865     // register.  This only happens when 128-bit vectors are passed.
3866   case SSEUp:
3867     assert(Lo == SSE && "Unexpected SSEUp classification");
3868     ResType = GetByteVectorType(Ty);
3869     break;
3870   }
3871 
3872   // If a high part was specified, merge it together with the low part.  It is
3873   // known to pass in the high eightbyte of the result.  We do this by forming a
3874   // first class struct aggregate with the high and low part: {low, high}
3875   if (HighPart)
3876     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3877 
3878   return ABIArgInfo::getDirect(ResType);
3879 }
3880 
3881 ABIArgInfo
3882 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3883                                              unsigned &NeededSSE,
3884                                              unsigned &MaxVectorWidth) const {
3885   auto RT = Ty->getAs<RecordType>();
3886   assert(RT && "classifyRegCallStructType only valid with struct types");
3887 
3888   if (RT->getDecl()->hasFlexibleArrayMember())
3889     return getIndirectReturnResult(Ty);
3890 
3891   // Sum up bases
3892   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3893     if (CXXRD->isDynamicClass()) {
3894       NeededInt = NeededSSE = 0;
3895       return getIndirectReturnResult(Ty);
3896     }
3897 
3898     for (const auto &I : CXXRD->bases())
3899       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE,
3900                                         MaxVectorWidth)
3901               .isIndirect()) {
3902         NeededInt = NeededSSE = 0;
3903         return getIndirectReturnResult(Ty);
3904       }
3905   }
3906 
3907   // Sum up members
3908   for (const auto *FD : RT->getDecl()->fields()) {
3909     QualType MTy = FD->getType();
3910     if (MTy->isRecordType() && !MTy->isUnionType()) {
3911       if (classifyRegCallStructTypeImpl(MTy, NeededInt, NeededSSE,
3912                                         MaxVectorWidth)
3913               .isIndirect()) {
3914         NeededInt = NeededSSE = 0;
3915         return getIndirectReturnResult(Ty);
3916       }
3917     } else {
3918       unsigned LocalNeededInt, LocalNeededSSE;
3919       if (classifyArgumentType(MTy, UINT_MAX, LocalNeededInt, LocalNeededSSE,
3920                                true, true)
3921               .isIndirect()) {
3922         NeededInt = NeededSSE = 0;
3923         return getIndirectReturnResult(Ty);
3924       }
3925       if (const auto *AT = getContext().getAsConstantArrayType(MTy))
3926         MTy = AT->getElementType();
3927       if (const auto *VT = MTy->getAs<VectorType>())
3928         if (getContext().getTypeSize(VT) > MaxVectorWidth)
3929           MaxVectorWidth = getContext().getTypeSize(VT);
3930       NeededInt += LocalNeededInt;
3931       NeededSSE += LocalNeededSSE;
3932     }
3933   }
3934 
3935   return ABIArgInfo::getDirect();
3936 }
3937 
3938 ABIArgInfo
3939 X86_64ABIInfo::classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
3940                                          unsigned &NeededSSE,
3941                                          unsigned &MaxVectorWidth) const {
3942 
3943   NeededInt = 0;
3944   NeededSSE = 0;
3945   MaxVectorWidth = 0;
3946 
3947   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE,
3948                                        MaxVectorWidth);
3949 }
3950 
3951 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3952 
3953   const unsigned CallingConv = FI.getCallingConvention();
3954   // It is possible to force Win64 calling convention on any x86_64 target by
3955   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3956   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3957   if (CallingConv == llvm::CallingConv::Win64) {
3958     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3959     Win64ABIInfo.computeInfo(FI);
3960     return;
3961   }
3962 
3963   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3964 
3965   // Keep track of the number of assigned registers.
3966   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3967   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3968   unsigned NeededInt = 0, NeededSSE = 0, MaxVectorWidth = 0;
3969 
3970   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3971     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3972         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3973       FI.getReturnInfo() = classifyRegCallStructType(
3974           FI.getReturnType(), NeededInt, NeededSSE, MaxVectorWidth);
3975       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3976         FreeIntRegs -= NeededInt;
3977         FreeSSERegs -= NeededSSE;
3978       } else {
3979         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3980       }
3981     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
3982                getContext().getCanonicalType(FI.getReturnType()
3983                                                  ->getAs<ComplexType>()
3984                                                  ->getElementType()) ==
3985                    getContext().LongDoubleTy)
3986       // Complex Long Double Type is passed in Memory when Regcall
3987       // calling convention is used.
3988       FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3989     else
3990       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3991   }
3992 
3993   // If the return value is indirect, then the hidden argument is consuming one
3994   // integer register.
3995   if (FI.getReturnInfo().isIndirect())
3996     --FreeIntRegs;
3997   else if (NeededSSE && MaxVectorWidth > 0)
3998     FI.setMaxVectorWidth(MaxVectorWidth);
3999 
4000   // The chain argument effectively gives us another free register.
4001   if (FI.isChainCall())
4002     ++FreeIntRegs;
4003 
4004   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
4005   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
4006   // get assigned (in left-to-right order) for passing as follows...
4007   unsigned ArgNo = 0;
4008   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4009        it != ie; ++it, ++ArgNo) {
4010     bool IsNamedArg = ArgNo < NumRequiredArgs;
4011 
4012     if (IsRegCall && it->type->isStructureOrClassType())
4013       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE,
4014                                            MaxVectorWidth);
4015     else
4016       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
4017                                       NeededSSE, IsNamedArg);
4018 
4019     // AMD64-ABI 3.2.3p3: If there are no registers available for any
4020     // eightbyte of an argument, the whole argument is passed on the
4021     // stack. If registers have already been assigned for some
4022     // eightbytes of such an argument, the assignments get reverted.
4023     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
4024       FreeIntRegs -= NeededInt;
4025       FreeSSERegs -= NeededSSE;
4026       if (MaxVectorWidth > FI.getMaxVectorWidth())
4027         FI.setMaxVectorWidth(MaxVectorWidth);
4028     } else {
4029       it->info = getIndirectResult(it->type, FreeIntRegs);
4030     }
4031   }
4032 }
4033 
4034 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
4035                                          Address VAListAddr, QualType Ty) {
4036   Address overflow_arg_area_p =
4037       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
4038   llvm::Value *overflow_arg_area =
4039     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
4040 
4041   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
4042   // byte boundary if alignment needed by type exceeds 8 byte boundary.
4043   // It isn't stated explicitly in the standard, but in practice we use
4044   // alignment greater than 16 where necessary.
4045   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4046   if (Align > CharUnits::fromQuantity(8)) {
4047     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
4048                                                       Align);
4049   }
4050 
4051   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
4052   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4053   llvm::Value *Res =
4054     CGF.Builder.CreateBitCast(overflow_arg_area,
4055                               llvm::PointerType::getUnqual(LTy));
4056 
4057   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
4058   // l->overflow_arg_area + sizeof(type).
4059   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
4060   // an 8 byte boundary.
4061 
4062   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
4063   llvm::Value *Offset =
4064       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
4065   overflow_arg_area = CGF.Builder.CreateGEP(CGF.Int8Ty, overflow_arg_area,
4066                                             Offset, "overflow_arg_area.next");
4067   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
4068 
4069   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
4070   return Address(Res, LTy, Align);
4071 }
4072 
4073 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4074                                  QualType Ty) const {
4075   // Assume that va_list type is correct; should be pointer to LLVM type:
4076   // struct {
4077   //   i32 gp_offset;
4078   //   i32 fp_offset;
4079   //   i8* overflow_arg_area;
4080   //   i8* reg_save_area;
4081   // };
4082   unsigned neededInt, neededSSE;
4083 
4084   Ty = getContext().getCanonicalType(Ty);
4085   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
4086                                        /*isNamedArg*/false);
4087 
4088   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
4089   // in the registers. If not go to step 7.
4090   if (!neededInt && !neededSSE)
4091     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4092 
4093   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
4094   // general purpose registers needed to pass type and num_fp to hold
4095   // the number of floating point registers needed.
4096 
4097   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
4098   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
4099   // l->fp_offset > 304 - num_fp * 16 go to step 7.
4100   //
4101   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
4102   // register save space).
4103 
4104   llvm::Value *InRegs = nullptr;
4105   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
4106   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
4107   if (neededInt) {
4108     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
4109     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
4110     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
4111     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
4112   }
4113 
4114   if (neededSSE) {
4115     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
4116     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
4117     llvm::Value *FitsInFP =
4118       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
4119     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
4120     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
4121   }
4122 
4123   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4124   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4125   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4126   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4127 
4128   // Emit code to load the value if it was passed in registers.
4129 
4130   CGF.EmitBlock(InRegBlock);
4131 
4132   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
4133   // an offset of l->gp_offset and/or l->fp_offset. This may require
4134   // copying to a temporary location in case the parameter is passed
4135   // in different register classes or requires an alignment greater
4136   // than 8 for general purpose registers and 16 for XMM registers.
4137   //
4138   // FIXME: This really results in shameful code when we end up needing to
4139   // collect arguments from different places; often what should result in a
4140   // simple assembling of a structure from scattered addresses has many more
4141   // loads than necessary. Can we clean this up?
4142   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4143   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
4144       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
4145 
4146   Address RegAddr = Address::invalid();
4147   if (neededInt && neededSSE) {
4148     // FIXME: Cleanup.
4149     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
4150     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
4151     Address Tmp = CGF.CreateMemTemp(Ty);
4152     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4153     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
4154     llvm::Type *TyLo = ST->getElementType(0);
4155     llvm::Type *TyHi = ST->getElementType(1);
4156     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
4157            "Unexpected ABI info for mixed regs");
4158     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
4159     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
4160     llvm::Value *GPAddr =
4161         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset);
4162     llvm::Value *FPAddr =
4163         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset);
4164     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
4165     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
4166 
4167     // Copy the first element.
4168     // FIXME: Our choice of alignment here and below is probably pessimistic.
4169     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
4170         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
4171         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
4172     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4173 
4174     // Copy the second element.
4175     V = CGF.Builder.CreateAlignedLoad(
4176         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
4177         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
4178     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4179 
4180     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4181   } else if (neededInt) {
4182     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset),
4183                       CGF.Int8Ty, CharUnits::fromQuantity(8));
4184     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4185 
4186     // Copy to a temporary if necessary to ensure the appropriate alignment.
4187     auto TInfo = getContext().getTypeInfoInChars(Ty);
4188     uint64_t TySize = TInfo.Width.getQuantity();
4189     CharUnits TyAlign = TInfo.Align;
4190 
4191     // Copy into a temporary if the type is more aligned than the
4192     // register save area.
4193     if (TyAlign.getQuantity() > 8) {
4194       Address Tmp = CGF.CreateMemTemp(Ty);
4195       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
4196       RegAddr = Tmp;
4197     }
4198 
4199   } else if (neededSSE == 1) {
4200     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset),
4201                       CGF.Int8Ty, CharUnits::fromQuantity(16));
4202     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4203   } else {
4204     assert(neededSSE == 2 && "Invalid number of needed registers!");
4205     // SSE registers are spaced 16 bytes apart in the register save
4206     // area, we need to collect the two eightbytes together.
4207     // The ABI isn't explicit about this, but it seems reasonable
4208     // to assume that the slots are 16-byte aligned, since the stack is
4209     // naturally 16-byte aligned and the prologue is expected to store
4210     // all the SSE registers to the RSA.
4211     Address RegAddrLo = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea,
4212                                                       fp_offset),
4213                                 CGF.Int8Ty, CharUnits::fromQuantity(16));
4214     Address RegAddrHi =
4215       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
4216                                              CharUnits::fromQuantity(16));
4217     llvm::Type *ST = AI.canHaveCoerceToType()
4218                          ? AI.getCoerceToType()
4219                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
4220     llvm::Value *V;
4221     Address Tmp = CGF.CreateMemTemp(Ty);
4222     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4223     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4224         RegAddrLo, ST->getStructElementType(0)));
4225     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4226     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4227         RegAddrHi, ST->getStructElementType(1)));
4228     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4229 
4230     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4231   }
4232 
4233   // AMD64-ABI 3.5.7p5: Step 5. Set:
4234   // l->gp_offset = l->gp_offset + num_gp * 8
4235   // l->fp_offset = l->fp_offset + num_fp * 16.
4236   if (neededInt) {
4237     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
4238     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
4239                             gp_offset_p);
4240   }
4241   if (neededSSE) {
4242     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
4243     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
4244                             fp_offset_p);
4245   }
4246   CGF.EmitBranch(ContBlock);
4247 
4248   // Emit code to load the value if it was passed in memory.
4249 
4250   CGF.EmitBlock(InMemBlock);
4251   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4252 
4253   // Return the appropriate result.
4254 
4255   CGF.EmitBlock(ContBlock);
4256   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
4257                                  "vaarg.addr");
4258   return ResAddr;
4259 }
4260 
4261 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4262                                    QualType Ty) const {
4263   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4264   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4265   uint64_t Width = getContext().getTypeSize(Ty);
4266   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4267 
4268   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4269                           CGF.getContext().getTypeInfoInChars(Ty),
4270                           CharUnits::fromQuantity(8),
4271                           /*allowHigherAlign*/ false);
4272 }
4273 
4274 ABIArgInfo WinX86_64ABIInfo::reclassifyHvaArgForVectorCall(
4275     QualType Ty, unsigned &FreeSSERegs, const ABIArgInfo &current) const {
4276   const Type *Base = nullptr;
4277   uint64_t NumElts = 0;
4278 
4279   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
4280       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
4281     FreeSSERegs -= NumElts;
4282     return getDirectX86Hva();
4283   }
4284   return current;
4285 }
4286 
4287 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
4288                                       bool IsReturnType, bool IsVectorCall,
4289                                       bool IsRegCall) const {
4290 
4291   if (Ty->isVoidType())
4292     return ABIArgInfo::getIgnore();
4293 
4294   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4295     Ty = EnumTy->getDecl()->getIntegerType();
4296 
4297   TypeInfo Info = getContext().getTypeInfo(Ty);
4298   uint64_t Width = Info.Width;
4299   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
4300 
4301   const RecordType *RT = Ty->getAs<RecordType>();
4302   if (RT) {
4303     if (!IsReturnType) {
4304       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
4305         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4306     }
4307 
4308     if (RT->getDecl()->hasFlexibleArrayMember())
4309       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4310 
4311   }
4312 
4313   const Type *Base = nullptr;
4314   uint64_t NumElts = 0;
4315   // vectorcall adds the concept of a homogenous vector aggregate, similar to
4316   // other targets.
4317   if ((IsVectorCall || IsRegCall) &&
4318       isHomogeneousAggregate(Ty, Base, NumElts)) {
4319     if (IsRegCall) {
4320       if (FreeSSERegs >= NumElts) {
4321         FreeSSERegs -= NumElts;
4322         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
4323           return ABIArgInfo::getDirect();
4324         return ABIArgInfo::getExpand();
4325       }
4326       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4327     } else if (IsVectorCall) {
4328       if (FreeSSERegs >= NumElts &&
4329           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
4330         FreeSSERegs -= NumElts;
4331         return ABIArgInfo::getDirect();
4332       } else if (IsReturnType) {
4333         return ABIArgInfo::getExpand();
4334       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
4335         // HVAs are delayed and reclassified in the 2nd step.
4336         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4337       }
4338     }
4339   }
4340 
4341   if (Ty->isMemberPointerType()) {
4342     // If the member pointer is represented by an LLVM int or ptr, pass it
4343     // directly.
4344     llvm::Type *LLTy = CGT.ConvertType(Ty);
4345     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
4346       return ABIArgInfo::getDirect();
4347   }
4348 
4349   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
4350     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4351     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4352     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4353       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4354 
4355     // Otherwise, coerce it to a small integer.
4356     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4357   }
4358 
4359   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4360     switch (BT->getKind()) {
4361     case BuiltinType::Bool:
4362       // Bool type is always extended to the ABI, other builtin types are not
4363       // extended.
4364       return ABIArgInfo::getExtend(Ty);
4365 
4366     case BuiltinType::LongDouble:
4367       // Mingw64 GCC uses the old 80 bit extended precision floating point
4368       // unit. It passes them indirectly through memory.
4369       if (IsMingw64) {
4370         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4371         if (LDF == &llvm::APFloat::x87DoubleExtended())
4372           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4373       }
4374       break;
4375 
4376     case BuiltinType::Int128:
4377     case BuiltinType::UInt128:
4378       // If it's a parameter type, the normal ABI rule is that arguments larger
4379       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4380       // even though it isn't particularly efficient.
4381       if (!IsReturnType)
4382         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4383 
4384       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4385       // Clang matches them for compatibility.
4386       return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
4387           llvm::Type::getInt64Ty(getVMContext()), 2));
4388 
4389     default:
4390       break;
4391     }
4392   }
4393 
4394   if (Ty->isBitIntType()) {
4395     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4396     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4397     // However, non-power-of-two bit-precise integers will be passed as 1, 2, 4,
4398     // or 8 bytes anyway as long is it fits in them, so we don't have to check
4399     // the power of 2.
4400     if (Width <= 64)
4401       return ABIArgInfo::getDirect();
4402     return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4403   }
4404 
4405   return ABIArgInfo::getDirect();
4406 }
4407 
4408 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4409   const unsigned CC = FI.getCallingConvention();
4410   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4411   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4412 
4413   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4414   // classification rules.
4415   if (CC == llvm::CallingConv::X86_64_SysV) {
4416     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4417     SysVABIInfo.computeInfo(FI);
4418     return;
4419   }
4420 
4421   unsigned FreeSSERegs = 0;
4422   if (IsVectorCall) {
4423     // We can use up to 4 SSE return registers with vectorcall.
4424     FreeSSERegs = 4;
4425   } else if (IsRegCall) {
4426     // RegCall gives us 16 SSE registers.
4427     FreeSSERegs = 16;
4428   }
4429 
4430   if (!getCXXABI().classifyReturnType(FI))
4431     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4432                                   IsVectorCall, IsRegCall);
4433 
4434   if (IsVectorCall) {
4435     // We can use up to 6 SSE register parameters with vectorcall.
4436     FreeSSERegs = 6;
4437   } else if (IsRegCall) {
4438     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4439     FreeSSERegs = 16;
4440   }
4441 
4442   unsigned ArgNum = 0;
4443   unsigned ZeroSSERegs = 0;
4444   for (auto &I : FI.arguments()) {
4445     // Vectorcall in x64 only permits the first 6 arguments to be passed as
4446     // XMM/YMM registers. After the sixth argument, pretend no vector
4447     // registers are left.
4448     unsigned *MaybeFreeSSERegs =
4449         (IsVectorCall && ArgNum >= 6) ? &ZeroSSERegs : &FreeSSERegs;
4450     I.info =
4451         classify(I.type, *MaybeFreeSSERegs, false, IsVectorCall, IsRegCall);
4452     ++ArgNum;
4453   }
4454 
4455   if (IsVectorCall) {
4456     // For vectorcall, assign aggregate HVAs to any free vector registers in a
4457     // second pass.
4458     for (auto &I : FI.arguments())
4459       I.info = reclassifyHvaArgForVectorCall(I.type, FreeSSERegs, I.info);
4460   }
4461 }
4462 
4463 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4464                                     QualType Ty) const {
4465   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4466   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4467   uint64_t Width = getContext().getTypeSize(Ty);
4468   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4469 
4470   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4471                           CGF.getContext().getTypeInfoInChars(Ty),
4472                           CharUnits::fromQuantity(8),
4473                           /*allowHigherAlign*/ false);
4474 }
4475 
4476 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4477                                         llvm::Value *Address, bool Is64Bit,
4478                                         bool IsAIX) {
4479   // This is calculated from the LLVM and GCC tables and verified
4480   // against gcc output.  AFAIK all PPC ABIs use the same encoding.
4481 
4482   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4483 
4484   llvm::IntegerType *i8 = CGF.Int8Ty;
4485   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4486   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4487   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4488 
4489   // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
4490   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
4491 
4492   // 32-63: fp0-31, the 8-byte floating-point registers
4493   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4494 
4495   // 64-67 are various 4-byte or 8-byte special-purpose registers:
4496   // 64: mq
4497   // 65: lr
4498   // 66: ctr
4499   // 67: ap
4500   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
4501 
4502   // 68-76 are various 4-byte special-purpose registers:
4503   // 68-75 cr0-7
4504   // 76: xer
4505   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4506 
4507   // 77-108: v0-31, the 16-byte vector registers
4508   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4509 
4510   // 109: vrsave
4511   // 110: vscr
4512   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
4513 
4514   // AIX does not utilize the rest of the registers.
4515   if (IsAIX)
4516     return false;
4517 
4518   // 111: spe_acc
4519   // 112: spefscr
4520   // 113: sfp
4521   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
4522 
4523   if (!Is64Bit)
4524     return false;
4525 
4526   // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
4527   // or above CPU.
4528   // 64-bit only registers:
4529   // 114: tfhar
4530   // 115: tfiar
4531   // 116: texasr
4532   AssignToArrayRange(Builder, Address, Eight8, 114, 116);
4533 
4534   return false;
4535 }
4536 
4537 // AIX
4538 namespace {
4539 /// AIXABIInfo - The AIX XCOFF ABI information.
4540 class AIXABIInfo : public ABIInfo {
4541   const bool Is64Bit;
4542   const unsigned PtrByteSize;
4543   CharUnits getParamTypeAlignment(QualType Ty) const;
4544 
4545 public:
4546   AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4547       : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
4548 
4549   bool isPromotableTypeForABI(QualType Ty) const;
4550 
4551   ABIArgInfo classifyReturnType(QualType RetTy) const;
4552   ABIArgInfo classifyArgumentType(QualType Ty) const;
4553 
4554   void computeInfo(CGFunctionInfo &FI) const override {
4555     if (!getCXXABI().classifyReturnType(FI))
4556       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4557 
4558     for (auto &I : FI.arguments())
4559       I.info = classifyArgumentType(I.type);
4560   }
4561 
4562   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4563                     QualType Ty) const override;
4564 };
4565 
4566 class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
4567   const bool Is64Bit;
4568 
4569 public:
4570   AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4571       : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
4572         Is64Bit(Is64Bit) {}
4573   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4574     return 1; // r1 is the dedicated stack pointer
4575   }
4576 
4577   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4578                                llvm::Value *Address) const override;
4579 };
4580 } // namespace
4581 
4582 // Return true if the ABI requires Ty to be passed sign- or zero-
4583 // extended to 32/64 bits.
4584 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
4585   // Treat an enum type as its underlying type.
4586   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4587     Ty = EnumTy->getDecl()->getIntegerType();
4588 
4589   // Promotable integer types are required to be promoted by the ABI.
4590   if (Ty->isPromotableIntegerType())
4591     return true;
4592 
4593   if (!Is64Bit)
4594     return false;
4595 
4596   // For 64 bit mode, in addition to the usual promotable integer types, we also
4597   // need to extend all 32-bit types, since the ABI requires promotion to 64
4598   // bits.
4599   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4600     switch (BT->getKind()) {
4601     case BuiltinType::Int:
4602     case BuiltinType::UInt:
4603       return true;
4604     default:
4605       break;
4606     }
4607 
4608   return false;
4609 }
4610 
4611 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
4612   if (RetTy->isAnyComplexType())
4613     return ABIArgInfo::getDirect();
4614 
4615   if (RetTy->isVectorType())
4616     return ABIArgInfo::getDirect();
4617 
4618   if (RetTy->isVoidType())
4619     return ABIArgInfo::getIgnore();
4620 
4621   if (isAggregateTypeForABI(RetTy))
4622     return getNaturalAlignIndirect(RetTy);
4623 
4624   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4625                                         : ABIArgInfo::getDirect());
4626 }
4627 
4628 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
4629   Ty = useFirstFieldIfTransparentUnion(Ty);
4630 
4631   if (Ty->isAnyComplexType())
4632     return ABIArgInfo::getDirect();
4633 
4634   if (Ty->isVectorType())
4635     return ABIArgInfo::getDirect();
4636 
4637   if (isAggregateTypeForABI(Ty)) {
4638     // Records with non-trivial destructors/copy-constructors should not be
4639     // passed by value.
4640     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4641       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4642 
4643     CharUnits CCAlign = getParamTypeAlignment(Ty);
4644     CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
4645 
4646     return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
4647                                    /*Realign*/ TyAlign > CCAlign);
4648   }
4649 
4650   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4651                                      : ABIArgInfo::getDirect());
4652 }
4653 
4654 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
4655   // Complex types are passed just like their elements.
4656   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4657     Ty = CTy->getElementType();
4658 
4659   if (Ty->isVectorType())
4660     return CharUnits::fromQuantity(16);
4661 
4662   // If the structure contains a vector type, the alignment is 16.
4663   if (isRecordWithSIMDVectorType(getContext(), Ty))
4664     return CharUnits::fromQuantity(16);
4665 
4666   return CharUnits::fromQuantity(PtrByteSize);
4667 }
4668 
4669 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4670                               QualType Ty) const {
4671 
4672   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4673   TypeInfo.Align = getParamTypeAlignment(Ty);
4674 
4675   CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
4676 
4677   // If we have a complex type and the base type is smaller than the register
4678   // size, the ABI calls for the real and imaginary parts to be right-adjusted
4679   // in separate words in 32bit mode or doublewords in 64bit mode. However,
4680   // Clang expects us to produce a pointer to a structure with the two parts
4681   // packed tightly. So generate loads of the real and imaginary parts relative
4682   // to the va_list pointer, and store them to a temporary structure. We do the
4683   // same as the PPC64ABI here.
4684   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4685     CharUnits EltSize = TypeInfo.Width / 2;
4686     if (EltSize < SlotSize)
4687       return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
4688   }
4689 
4690   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
4691                           SlotSize, /*AllowHigher*/ true);
4692 }
4693 
4694 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
4695     CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
4696   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
4697 }
4698 
4699 // PowerPC-32
4700 namespace {
4701 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4702 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4703   bool IsSoftFloatABI;
4704   bool IsRetSmallStructInRegABI;
4705 
4706   CharUnits getParamTypeAlignment(QualType Ty) const;
4707 
4708 public:
4709   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
4710                      bool RetSmallStructInRegABI)
4711       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
4712         IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
4713 
4714   ABIArgInfo classifyReturnType(QualType RetTy) const;
4715 
4716   void computeInfo(CGFunctionInfo &FI) const override {
4717     if (!getCXXABI().classifyReturnType(FI))
4718       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4719     for (auto &I : FI.arguments())
4720       I.info = classifyArgumentType(I.type);
4721   }
4722 
4723   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4724                     QualType Ty) const override;
4725 };
4726 
4727 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4728 public:
4729   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
4730                          bool RetSmallStructInRegABI)
4731       : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
4732             CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
4733 
4734   static bool isStructReturnInRegABI(const llvm::Triple &Triple,
4735                                      const CodeGenOptions &Opts);
4736 
4737   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4738     // This is recovered from gcc output.
4739     return 1; // r1 is the dedicated stack pointer
4740   }
4741 
4742   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4743                                llvm::Value *Address) const override;
4744 };
4745 }
4746 
4747 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4748   // Complex types are passed just like their elements.
4749   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4750     Ty = CTy->getElementType();
4751 
4752   if (Ty->isVectorType())
4753     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4754                                                                        : 4);
4755 
4756   // For single-element float/vector structs, we consider the whole type
4757   // to have the same alignment requirements as its single element.
4758   const Type *AlignTy = nullptr;
4759   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4760     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4761     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4762         (BT && BT->isFloatingPoint()))
4763       AlignTy = EltType;
4764   }
4765 
4766   if (AlignTy)
4767     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4768   return CharUnits::fromQuantity(4);
4769 }
4770 
4771 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4772   uint64_t Size;
4773 
4774   // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
4775   if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
4776       (Size = getContext().getTypeSize(RetTy)) <= 64) {
4777     // System V ABI (1995), page 3-22, specified:
4778     // > A structure or union whose size is less than or equal to 8 bytes
4779     // > shall be returned in r3 and r4, as if it were first stored in the
4780     // > 8-byte aligned memory area and then the low addressed word were
4781     // > loaded into r3 and the high-addressed word into r4.  Bits beyond
4782     // > the last member of the structure or union are not defined.
4783     //
4784     // GCC for big-endian PPC32 inserts the pad before the first member,
4785     // not "beyond the last member" of the struct.  To stay compatible
4786     // with GCC, we coerce the struct to an integer of the same size.
4787     // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
4788     if (Size == 0)
4789       return ABIArgInfo::getIgnore();
4790     else {
4791       llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
4792       return ABIArgInfo::getDirect(CoerceTy);
4793     }
4794   }
4795 
4796   return DefaultABIInfo::classifyReturnType(RetTy);
4797 }
4798 
4799 // TODO: this implementation is now likely redundant with
4800 // DefaultABIInfo::EmitVAArg.
4801 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4802                                       QualType Ty) const {
4803   if (getTarget().getTriple().isOSDarwin()) {
4804     auto TI = getContext().getTypeInfoInChars(Ty);
4805     TI.Align = getParamTypeAlignment(Ty);
4806 
4807     CharUnits SlotSize = CharUnits::fromQuantity(4);
4808     return emitVoidPtrVAArg(CGF, VAList, Ty,
4809                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4810                             /*AllowHigherAlign=*/true);
4811   }
4812 
4813   const unsigned OverflowLimit = 8;
4814   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4815     // TODO: Implement this. For now ignore.
4816     (void)CTy;
4817     return Address::invalid(); // FIXME?
4818   }
4819 
4820   // struct __va_list_tag {
4821   //   unsigned char gpr;
4822   //   unsigned char fpr;
4823   //   unsigned short reserved;
4824   //   void *overflow_arg_area;
4825   //   void *reg_save_area;
4826   // };
4827 
4828   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4829   bool isInt = !Ty->isFloatingType();
4830   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4831 
4832   // All aggregates are passed indirectly?  That doesn't seem consistent
4833   // with the argument-lowering code.
4834   bool isIndirect = isAggregateTypeForABI(Ty);
4835 
4836   CGBuilderTy &Builder = CGF.Builder;
4837 
4838   // The calling convention either uses 1-2 GPRs or 1 FPR.
4839   Address NumRegsAddr = Address::invalid();
4840   if (isInt || IsSoftFloatABI) {
4841     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4842   } else {
4843     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4844   }
4845 
4846   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4847 
4848   // "Align" the register count when TY is i64.
4849   if (isI64 || (isF64 && IsSoftFloatABI)) {
4850     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4851     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4852   }
4853 
4854   llvm::Value *CC =
4855       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4856 
4857   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4858   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4859   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4860 
4861   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4862 
4863   llvm::Type *DirectTy = CGF.ConvertType(Ty), *ElementTy = DirectTy;
4864   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4865 
4866   // Case 1: consume registers.
4867   Address RegAddr = Address::invalid();
4868   {
4869     CGF.EmitBlock(UsingRegs);
4870 
4871     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4872     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), CGF.Int8Ty,
4873                       CharUnits::fromQuantity(8));
4874     assert(RegAddr.getElementType() == CGF.Int8Ty);
4875 
4876     // Floating-point registers start after the general-purpose registers.
4877     if (!(isInt || IsSoftFloatABI)) {
4878       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4879                                                    CharUnits::fromQuantity(32));
4880     }
4881 
4882     // Get the address of the saved value by scaling the number of
4883     // registers we've used by the number of
4884     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4885     llvm::Value *RegOffset =
4886         Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4887     RegAddr = Address(
4888         Builder.CreateInBoundsGEP(CGF.Int8Ty, RegAddr.getPointer(), RegOffset),
4889         CGF.Int8Ty, RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4890     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4891 
4892     // Increase the used-register count.
4893     NumRegs =
4894       Builder.CreateAdd(NumRegs,
4895                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4896     Builder.CreateStore(NumRegs, NumRegsAddr);
4897 
4898     CGF.EmitBranch(Cont);
4899   }
4900 
4901   // Case 2: consume space in the overflow area.
4902   Address MemAddr = Address::invalid();
4903   {
4904     CGF.EmitBlock(UsingOverflow);
4905 
4906     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4907 
4908     // Everything in the overflow area is rounded up to a size of at least 4.
4909     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4910 
4911     CharUnits Size;
4912     if (!isIndirect) {
4913       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4914       Size = TypeInfo.Width.alignTo(OverflowAreaAlign);
4915     } else {
4916       Size = CGF.getPointerSize();
4917     }
4918 
4919     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4920     Address OverflowArea =
4921         Address(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), CGF.Int8Ty,
4922                 OverflowAreaAlign);
4923     // Round up address of argument to alignment
4924     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4925     if (Align > OverflowAreaAlign) {
4926       llvm::Value *Ptr = OverflowArea.getPointer();
4927       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4928                              OverflowArea.getElementType(), Align);
4929     }
4930 
4931     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4932 
4933     // Increase the overflow area.
4934     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4935     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4936     CGF.EmitBranch(Cont);
4937   }
4938 
4939   CGF.EmitBlock(Cont);
4940 
4941   // Merge the cases with a phi.
4942   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4943                                 "vaarg.addr");
4944 
4945   // Load the pointer if the argument was passed indirectly.
4946   if (isIndirect) {
4947     Result = Address(Builder.CreateLoad(Result, "aggr"), ElementTy,
4948                      getContext().getTypeAlignInChars(Ty));
4949   }
4950 
4951   return Result;
4952 }
4953 
4954 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
4955     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4956   assert(Triple.isPPC32());
4957 
4958   switch (Opts.getStructReturnConvention()) {
4959   case CodeGenOptions::SRCK_Default:
4960     break;
4961   case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
4962     return false;
4963   case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
4964     return true;
4965   }
4966 
4967   if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
4968     return true;
4969 
4970   return false;
4971 }
4972 
4973 bool
4974 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4975                                                 llvm::Value *Address) const {
4976   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
4977                                      /*IsAIX*/ false);
4978 }
4979 
4980 // PowerPC-64
4981 
4982 namespace {
4983 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4984 class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
4985 public:
4986   enum ABIKind {
4987     ELFv1 = 0,
4988     ELFv2
4989   };
4990 
4991 private:
4992   static const unsigned GPRBits = 64;
4993   ABIKind Kind;
4994   bool IsSoftFloatABI;
4995 
4996 public:
4997   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind,
4998                      bool SoftFloatABI)
4999       : SwiftABIInfo(CGT), Kind(Kind), IsSoftFloatABI(SoftFloatABI) {}
5000 
5001   bool isPromotableTypeForABI(QualType Ty) const;
5002   CharUnits getParamTypeAlignment(QualType Ty) const;
5003 
5004   ABIArgInfo classifyReturnType(QualType RetTy) const;
5005   ABIArgInfo classifyArgumentType(QualType Ty) const;
5006 
5007   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5008   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5009                                          uint64_t Members) const override;
5010 
5011   // TODO: We can add more logic to computeInfo to improve performance.
5012   // Example: For aggregate arguments that fit in a register, we could
5013   // use getDirectInReg (as is done below for structs containing a single
5014   // floating-point value) to avoid pushing them to memory on function
5015   // entry.  This would require changing the logic in PPCISelLowering
5016   // when lowering the parameters in the caller and args in the callee.
5017   void computeInfo(CGFunctionInfo &FI) const override {
5018     if (!getCXXABI().classifyReturnType(FI))
5019       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5020     for (auto &I : FI.arguments()) {
5021       // We rely on the default argument classification for the most part.
5022       // One exception:  An aggregate containing a single floating-point
5023       // or vector item must be passed in a register if one is available.
5024       const Type *T = isSingleElementStruct(I.type, getContext());
5025       if (T) {
5026         const BuiltinType *BT = T->getAs<BuiltinType>();
5027         if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
5028             (BT && BT->isFloatingPoint())) {
5029           QualType QT(T, 0);
5030           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
5031           continue;
5032         }
5033       }
5034       I.info = classifyArgumentType(I.type);
5035     }
5036   }
5037 
5038   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5039                     QualType Ty) const override;
5040 
5041   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5042                                     bool asReturnValue) const override {
5043     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5044   }
5045 
5046   bool isSwiftErrorInRegister() const override {
5047     return false;
5048   }
5049 };
5050 
5051 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
5052 
5053 public:
5054   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
5055                                PPC64_SVR4_ABIInfo::ABIKind Kind,
5056                                bool SoftFloatABI)
5057       : TargetCodeGenInfo(
5058             std::make_unique<PPC64_SVR4_ABIInfo>(CGT, Kind, SoftFloatABI)) {}
5059 
5060   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5061     // This is recovered from gcc output.
5062     return 1; // r1 is the dedicated stack pointer
5063   }
5064 
5065   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5066                                llvm::Value *Address) const override;
5067 };
5068 
5069 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5070 public:
5071   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
5072 
5073   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5074     // This is recovered from gcc output.
5075     return 1; // r1 is the dedicated stack pointer
5076   }
5077 
5078   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5079                                llvm::Value *Address) const override;
5080 };
5081 
5082 }
5083 
5084 // Return true if the ABI requires Ty to be passed sign- or zero-
5085 // extended to 64 bits.
5086 bool
5087 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
5088   // Treat an enum type as its underlying type.
5089   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5090     Ty = EnumTy->getDecl()->getIntegerType();
5091 
5092   // Promotable integer types are required to be promoted by the ABI.
5093   if (isPromotableIntegerTypeForABI(Ty))
5094     return true;
5095 
5096   // In addition to the usual promotable integer types, we also need to
5097   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
5098   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5099     switch (BT->getKind()) {
5100     case BuiltinType::Int:
5101     case BuiltinType::UInt:
5102       return true;
5103     default:
5104       break;
5105     }
5106 
5107   if (const auto *EIT = Ty->getAs<BitIntType>())
5108     if (EIT->getNumBits() < 64)
5109       return true;
5110 
5111   return false;
5112 }
5113 
5114 /// isAlignedParamType - Determine whether a type requires 16-byte or
5115 /// higher alignment in the parameter area.  Always returns at least 8.
5116 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
5117   // Complex types are passed just like their elements.
5118   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
5119     Ty = CTy->getElementType();
5120 
5121   auto FloatUsesVector = [this](QualType Ty){
5122     return Ty->isRealFloatingType() && &getContext().getFloatTypeSemantics(
5123                                            Ty) == &llvm::APFloat::IEEEquad();
5124   };
5125 
5126   // Only vector types of size 16 bytes need alignment (larger types are
5127   // passed via reference, smaller types are not aligned).
5128   if (Ty->isVectorType()) {
5129     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5130   } else if (FloatUsesVector(Ty)) {
5131     // According to ABI document section 'Optional Save Areas': If extended
5132     // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
5133     // format are supported, map them to a single quadword, quadword aligned.
5134     return CharUnits::fromQuantity(16);
5135   }
5136 
5137   // For single-element float/vector structs, we consider the whole type
5138   // to have the same alignment requirements as its single element.
5139   const Type *AlignAsType = nullptr;
5140   const Type *EltType = isSingleElementStruct(Ty, getContext());
5141   if (EltType) {
5142     const BuiltinType *BT = EltType->getAs<BuiltinType>();
5143     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
5144         (BT && BT->isFloatingPoint()))
5145       AlignAsType = EltType;
5146   }
5147 
5148   // Likewise for ELFv2 homogeneous aggregates.
5149   const Type *Base = nullptr;
5150   uint64_t Members = 0;
5151   if (!AlignAsType && Kind == ELFv2 &&
5152       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
5153     AlignAsType = Base;
5154 
5155   // With special case aggregates, only vector base types need alignment.
5156   if (AlignAsType) {
5157     bool UsesVector = AlignAsType->isVectorType() ||
5158                       FloatUsesVector(QualType(AlignAsType, 0));
5159     return CharUnits::fromQuantity(UsesVector ? 16 : 8);
5160   }
5161 
5162   // Otherwise, we only need alignment for any aggregate type that
5163   // has an alignment requirement of >= 16 bytes.
5164   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
5165     return CharUnits::fromQuantity(16);
5166   }
5167 
5168   return CharUnits::fromQuantity(8);
5169 }
5170 
5171 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
5172 /// aggregate.  Base is set to the base element type, and Members is set
5173 /// to the number of base elements.
5174 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
5175                                      uint64_t &Members) const {
5176   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
5177     uint64_t NElements = AT->getSize().getZExtValue();
5178     if (NElements == 0)
5179       return false;
5180     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
5181       return false;
5182     Members *= NElements;
5183   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
5184     const RecordDecl *RD = RT->getDecl();
5185     if (RD->hasFlexibleArrayMember())
5186       return false;
5187 
5188     Members = 0;
5189 
5190     // If this is a C++ record, check the properties of the record such as
5191     // bases and ABI specific restrictions
5192     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5193       if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
5194         return false;
5195 
5196       for (const auto &I : CXXRD->bases()) {
5197         // Ignore empty records.
5198         if (isEmptyRecord(getContext(), I.getType(), true))
5199           continue;
5200 
5201         uint64_t FldMembers;
5202         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
5203           return false;
5204 
5205         Members += FldMembers;
5206       }
5207     }
5208 
5209     for (const auto *FD : RD->fields()) {
5210       // Ignore (non-zero arrays of) empty records.
5211       QualType FT = FD->getType();
5212       while (const ConstantArrayType *AT =
5213              getContext().getAsConstantArrayType(FT)) {
5214         if (AT->getSize().getZExtValue() == 0)
5215           return false;
5216         FT = AT->getElementType();
5217       }
5218       if (isEmptyRecord(getContext(), FT, true))
5219         continue;
5220 
5221       if (isZeroLengthBitfieldPermittedInHomogeneousAggregate() &&
5222           FD->isZeroLengthBitField(getContext()))
5223         continue;
5224 
5225       uint64_t FldMembers;
5226       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
5227         return false;
5228 
5229       Members = (RD->isUnion() ?
5230                  std::max(Members, FldMembers) : Members + FldMembers);
5231     }
5232 
5233     if (!Base)
5234       return false;
5235 
5236     // Ensure there is no padding.
5237     if (getContext().getTypeSize(Base) * Members !=
5238         getContext().getTypeSize(Ty))
5239       return false;
5240   } else {
5241     Members = 1;
5242     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
5243       Members = 2;
5244       Ty = CT->getElementType();
5245     }
5246 
5247     // Most ABIs only support float, double, and some vector type widths.
5248     if (!isHomogeneousAggregateBaseType(Ty))
5249       return false;
5250 
5251     // The base type must be the same for all members.  Types that
5252     // agree in both total size and mode (float vs. vector) are
5253     // treated as being equivalent here.
5254     const Type *TyPtr = Ty.getTypePtr();
5255     if (!Base) {
5256       Base = TyPtr;
5257       // If it's a non-power-of-2 vector, its size is already a power-of-2,
5258       // so make sure to widen it explicitly.
5259       if (const VectorType *VT = Base->getAs<VectorType>()) {
5260         QualType EltTy = VT->getElementType();
5261         unsigned NumElements =
5262             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
5263         Base = getContext()
5264                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
5265                    .getTypePtr();
5266       }
5267     }
5268 
5269     if (Base->isVectorType() != TyPtr->isVectorType() ||
5270         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
5271       return false;
5272   }
5273   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
5274 }
5275 
5276 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5277   // Homogeneous aggregates for ELFv2 must have base types of float,
5278   // double, long double, or 128-bit vectors.
5279   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5280     if (BT->getKind() == BuiltinType::Float ||
5281         BT->getKind() == BuiltinType::Double ||
5282         BT->getKind() == BuiltinType::LongDouble ||
5283         BT->getKind() == BuiltinType::Ibm128 ||
5284         (getContext().getTargetInfo().hasFloat128Type() &&
5285          (BT->getKind() == BuiltinType::Float128))) {
5286       if (IsSoftFloatABI)
5287         return false;
5288       return true;
5289     }
5290   }
5291   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5292     if (getContext().getTypeSize(VT) == 128)
5293       return true;
5294   }
5295   return false;
5296 }
5297 
5298 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
5299     const Type *Base, uint64_t Members) const {
5300   // Vector and fp128 types require one register, other floating point types
5301   // require one or two registers depending on their size.
5302   uint32_t NumRegs =
5303       ((getContext().getTargetInfo().hasFloat128Type() &&
5304           Base->isFloat128Type()) ||
5305         Base->isVectorType()) ? 1
5306                               : (getContext().getTypeSize(Base) + 63) / 64;
5307 
5308   // Homogeneous Aggregates may occupy at most 8 registers.
5309   return Members * NumRegs <= 8;
5310 }
5311 
5312 ABIArgInfo
5313 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
5314   Ty = useFirstFieldIfTransparentUnion(Ty);
5315 
5316   if (Ty->isAnyComplexType())
5317     return ABIArgInfo::getDirect();
5318 
5319   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
5320   // or via reference (larger than 16 bytes).
5321   if (Ty->isVectorType()) {
5322     uint64_t Size = getContext().getTypeSize(Ty);
5323     if (Size > 128)
5324       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5325     else if (Size < 128) {
5326       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5327       return ABIArgInfo::getDirect(CoerceTy);
5328     }
5329   }
5330 
5331   if (const auto *EIT = Ty->getAs<BitIntType>())
5332     if (EIT->getNumBits() > 128)
5333       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
5334 
5335   if (isAggregateTypeForABI(Ty)) {
5336     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5337       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5338 
5339     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
5340     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5341 
5342     // ELFv2 homogeneous aggregates are passed as array types.
5343     const Type *Base = nullptr;
5344     uint64_t Members = 0;
5345     if (Kind == ELFv2 &&
5346         isHomogeneousAggregate(Ty, Base, Members)) {
5347       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5348       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5349       return ABIArgInfo::getDirect(CoerceTy);
5350     }
5351 
5352     // If an aggregate may end up fully in registers, we do not
5353     // use the ByVal method, but pass the aggregate as array.
5354     // This is usually beneficial since we avoid forcing the
5355     // back-end to store the argument to memory.
5356     uint64_t Bits = getContext().getTypeSize(Ty);
5357     if (Bits > 0 && Bits <= 8 * GPRBits) {
5358       llvm::Type *CoerceTy;
5359 
5360       // Types up to 8 bytes are passed as integer type (which will be
5361       // properly aligned in the argument save area doubleword).
5362       if (Bits <= GPRBits)
5363         CoerceTy =
5364             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5365       // Larger types are passed as arrays, with the base type selected
5366       // according to the required alignment in the save area.
5367       else {
5368         uint64_t RegBits = ABIAlign * 8;
5369         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
5370         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
5371         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
5372       }
5373 
5374       return ABIArgInfo::getDirect(CoerceTy);
5375     }
5376 
5377     // All other aggregates are passed ByVal.
5378     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5379                                    /*ByVal=*/true,
5380                                    /*Realign=*/TyAlign > ABIAlign);
5381   }
5382 
5383   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
5384                                      : ABIArgInfo::getDirect());
5385 }
5386 
5387 ABIArgInfo
5388 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
5389   if (RetTy->isVoidType())
5390     return ABIArgInfo::getIgnore();
5391 
5392   if (RetTy->isAnyComplexType())
5393     return ABIArgInfo::getDirect();
5394 
5395   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
5396   // or via reference (larger than 16 bytes).
5397   if (RetTy->isVectorType()) {
5398     uint64_t Size = getContext().getTypeSize(RetTy);
5399     if (Size > 128)
5400       return getNaturalAlignIndirect(RetTy);
5401     else if (Size < 128) {
5402       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5403       return ABIArgInfo::getDirect(CoerceTy);
5404     }
5405   }
5406 
5407   if (const auto *EIT = RetTy->getAs<BitIntType>())
5408     if (EIT->getNumBits() > 128)
5409       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
5410 
5411   if (isAggregateTypeForABI(RetTy)) {
5412     // ELFv2 homogeneous aggregates are returned as array types.
5413     const Type *Base = nullptr;
5414     uint64_t Members = 0;
5415     if (Kind == ELFv2 &&
5416         isHomogeneousAggregate(RetTy, Base, Members)) {
5417       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5418       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5419       return ABIArgInfo::getDirect(CoerceTy);
5420     }
5421 
5422     // ELFv2 small aggregates are returned in up to two registers.
5423     uint64_t Bits = getContext().getTypeSize(RetTy);
5424     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
5425       if (Bits == 0)
5426         return ABIArgInfo::getIgnore();
5427 
5428       llvm::Type *CoerceTy;
5429       if (Bits > GPRBits) {
5430         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
5431         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
5432       } else
5433         CoerceTy =
5434             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5435       return ABIArgInfo::getDirect(CoerceTy);
5436     }
5437 
5438     // All other aggregates are returned indirectly.
5439     return getNaturalAlignIndirect(RetTy);
5440   }
5441 
5442   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
5443                                         : ABIArgInfo::getDirect());
5444 }
5445 
5446 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
5447 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5448                                       QualType Ty) const {
5449   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
5450   TypeInfo.Align = getParamTypeAlignment(Ty);
5451 
5452   CharUnits SlotSize = CharUnits::fromQuantity(8);
5453 
5454   // If we have a complex type and the base type is smaller than 8 bytes,
5455   // the ABI calls for the real and imaginary parts to be right-adjusted
5456   // in separate doublewords.  However, Clang expects us to produce a
5457   // pointer to a structure with the two parts packed tightly.  So generate
5458   // loads of the real and imaginary parts relative to the va_list pointer,
5459   // and store them to a temporary structure.
5460   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
5461     CharUnits EltSize = TypeInfo.Width / 2;
5462     if (EltSize < SlotSize)
5463       return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
5464   }
5465 
5466   // Otherwise, just use the general rule.
5467   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
5468                           TypeInfo, SlotSize, /*AllowHigher*/ true);
5469 }
5470 
5471 bool
5472 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
5473   CodeGen::CodeGenFunction &CGF,
5474   llvm::Value *Address) const {
5475   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5476                                      /*IsAIX*/ false);
5477 }
5478 
5479 bool
5480 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5481                                                 llvm::Value *Address) const {
5482   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5483                                      /*IsAIX*/ false);
5484 }
5485 
5486 //===----------------------------------------------------------------------===//
5487 // AArch64 ABI Implementation
5488 //===----------------------------------------------------------------------===//
5489 
5490 namespace {
5491 
5492 class AArch64ABIInfo : public SwiftABIInfo {
5493 public:
5494   enum ABIKind {
5495     AAPCS = 0,
5496     DarwinPCS,
5497     Win64
5498   };
5499 
5500 private:
5501   ABIKind Kind;
5502 
5503 public:
5504   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
5505     : SwiftABIInfo(CGT), Kind(Kind) {}
5506 
5507 private:
5508   ABIKind getABIKind() const { return Kind; }
5509   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5510 
5511   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5512   ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
5513                                   unsigned CallingConvention) const;
5514   ABIArgInfo coerceIllegalVector(QualType Ty) const;
5515   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5516   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5517                                          uint64_t Members) const override;
5518   bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
5519 
5520   bool isIllegalVectorType(QualType Ty) const;
5521 
5522   void computeInfo(CGFunctionInfo &FI) const override {
5523     if (!::classifyReturnType(getCXXABI(), FI, *this))
5524       FI.getReturnInfo() =
5525           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5526 
5527     for (auto &it : FI.arguments())
5528       it.info = classifyArgumentType(it.type, FI.isVariadic(),
5529                                      FI.getCallingConvention());
5530   }
5531 
5532   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5533                           CodeGenFunction &CGF) const;
5534 
5535   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5536                          CodeGenFunction &CGF) const;
5537 
5538   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5539                     QualType Ty) const override {
5540     llvm::Type *BaseTy = CGF.ConvertType(Ty);
5541     if (isa<llvm::ScalableVectorType>(BaseTy))
5542       llvm::report_fatal_error("Passing SVE types to variadic functions is "
5543                                "currently not supported");
5544 
5545     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5546                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5547                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5548   }
5549 
5550   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5551                       QualType Ty) const override;
5552 
5553   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5554                                     bool asReturnValue) const override {
5555     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5556   }
5557   bool isSwiftErrorInRegister() const override {
5558     return true;
5559   }
5560 
5561   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5562                                  unsigned elts) const override;
5563 
5564   bool allowBFloatArgsAndRet() const override {
5565     return getTarget().hasBFloat16Type();
5566   }
5567 };
5568 
5569 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5570 public:
5571   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5572       : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
5573 
5574   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5575     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5576   }
5577 
5578   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5579     return 31;
5580   }
5581 
5582   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5583 
5584   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5585                            CodeGen::CodeGenModule &CGM) const override {
5586     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5587     if (!FD)
5588       return;
5589 
5590     const auto *TA = FD->getAttr<TargetAttr>();
5591     if (TA == nullptr)
5592       return;
5593 
5594     ParsedTargetAttr Attr = TA->parse();
5595     if (Attr.BranchProtection.empty())
5596       return;
5597 
5598     TargetInfo::BranchProtectionInfo BPI;
5599     StringRef Error;
5600     (void)CGM.getTarget().validateBranchProtection(
5601         Attr.BranchProtection, Attr.Architecture, BPI, Error);
5602     assert(Error.empty());
5603 
5604     auto *Fn = cast<llvm::Function>(GV);
5605     static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
5606     Fn->addFnAttr("sign-return-address", SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
5607 
5608     if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
5609       Fn->addFnAttr("sign-return-address-key",
5610                     BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
5611                         ? "a_key"
5612                         : "b_key");
5613     }
5614 
5615     Fn->addFnAttr("branch-target-enforcement",
5616                   BPI.BranchTargetEnforcement ? "true" : "false");
5617   }
5618 
5619   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
5620                                 llvm::Type *Ty) const override {
5621     if (CGF.getTarget().hasFeature("ls64")) {
5622       auto *ST = dyn_cast<llvm::StructType>(Ty);
5623       if (ST && ST->getNumElements() == 1) {
5624         auto *AT = dyn_cast<llvm::ArrayType>(ST->getElementType(0));
5625         if (AT && AT->getNumElements() == 8 &&
5626             AT->getElementType()->isIntegerTy(64))
5627           return true;
5628       }
5629     }
5630     return TargetCodeGenInfo::isScalarizableAsmOperand(CGF, Ty);
5631   }
5632 };
5633 
5634 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5635 public:
5636   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5637       : AArch64TargetCodeGenInfo(CGT, K) {}
5638 
5639   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5640                            CodeGen::CodeGenModule &CGM) const override;
5641 
5642   void getDependentLibraryOption(llvm::StringRef Lib,
5643                                  llvm::SmallString<24> &Opt) const override {
5644     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5645   }
5646 
5647   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5648                                llvm::SmallString<32> &Opt) const override {
5649     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5650   }
5651 };
5652 
5653 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5654     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5655   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5656   if (GV->isDeclaration())
5657     return;
5658   addStackProbeTargetAttributes(D, GV, CGM);
5659 }
5660 }
5661 
5662 ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty) const {
5663   assert(Ty->isVectorType() && "expected vector type!");
5664 
5665   const auto *VT = Ty->castAs<VectorType>();
5666   if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
5667     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5668     assert(VT->getElementType()->castAs<BuiltinType>()->getKind() ==
5669                BuiltinType::UChar &&
5670            "unexpected builtin type for SVE predicate!");
5671     return ABIArgInfo::getDirect(llvm::ScalableVectorType::get(
5672         llvm::Type::getInt1Ty(getVMContext()), 16));
5673   }
5674 
5675   if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) {
5676     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5677 
5678     const auto *BT = VT->getElementType()->castAs<BuiltinType>();
5679     llvm::ScalableVectorType *ResType = nullptr;
5680     switch (BT->getKind()) {
5681     default:
5682       llvm_unreachable("unexpected builtin type for SVE vector!");
5683     case BuiltinType::SChar:
5684     case BuiltinType::UChar:
5685       ResType = llvm::ScalableVectorType::get(
5686           llvm::Type::getInt8Ty(getVMContext()), 16);
5687       break;
5688     case BuiltinType::Short:
5689     case BuiltinType::UShort:
5690       ResType = llvm::ScalableVectorType::get(
5691           llvm::Type::getInt16Ty(getVMContext()), 8);
5692       break;
5693     case BuiltinType::Int:
5694     case BuiltinType::UInt:
5695       ResType = llvm::ScalableVectorType::get(
5696           llvm::Type::getInt32Ty(getVMContext()), 4);
5697       break;
5698     case BuiltinType::Long:
5699     case BuiltinType::ULong:
5700       ResType = llvm::ScalableVectorType::get(
5701           llvm::Type::getInt64Ty(getVMContext()), 2);
5702       break;
5703     case BuiltinType::Half:
5704       ResType = llvm::ScalableVectorType::get(
5705           llvm::Type::getHalfTy(getVMContext()), 8);
5706       break;
5707     case BuiltinType::Float:
5708       ResType = llvm::ScalableVectorType::get(
5709           llvm::Type::getFloatTy(getVMContext()), 4);
5710       break;
5711     case BuiltinType::Double:
5712       ResType = llvm::ScalableVectorType::get(
5713           llvm::Type::getDoubleTy(getVMContext()), 2);
5714       break;
5715     case BuiltinType::BFloat16:
5716       ResType = llvm::ScalableVectorType::get(
5717           llvm::Type::getBFloatTy(getVMContext()), 8);
5718       break;
5719     }
5720     return ABIArgInfo::getDirect(ResType);
5721   }
5722 
5723   uint64_t Size = getContext().getTypeSize(Ty);
5724   // Android promotes <2 x i8> to i16, not i32
5725   if (isAndroid() && (Size <= 16)) {
5726     llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5727     return ABIArgInfo::getDirect(ResType);
5728   }
5729   if (Size <= 32) {
5730     llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5731     return ABIArgInfo::getDirect(ResType);
5732   }
5733   if (Size == 64) {
5734     auto *ResType =
5735         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5736     return ABIArgInfo::getDirect(ResType);
5737   }
5738   if (Size == 128) {
5739     auto *ResType =
5740         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5741     return ABIArgInfo::getDirect(ResType);
5742   }
5743   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5744 }
5745 
5746 ABIArgInfo
5747 AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadic,
5748                                      unsigned CallingConvention) const {
5749   Ty = useFirstFieldIfTransparentUnion(Ty);
5750 
5751   // Handle illegal vector types here.
5752   if (isIllegalVectorType(Ty))
5753     return coerceIllegalVector(Ty);
5754 
5755   if (!isAggregateTypeForABI(Ty)) {
5756     // Treat an enum type as its underlying type.
5757     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5758       Ty = EnumTy->getDecl()->getIntegerType();
5759 
5760     if (const auto *EIT = Ty->getAs<BitIntType>())
5761       if (EIT->getNumBits() > 128)
5762         return getNaturalAlignIndirect(Ty);
5763 
5764     return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
5765                 ? ABIArgInfo::getExtend(Ty)
5766                 : ABIArgInfo::getDirect());
5767   }
5768 
5769   // Structures with either a non-trivial destructor or a non-trivial
5770   // copy constructor are always indirect.
5771   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5772     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5773                                      CGCXXABI::RAA_DirectInMemory);
5774   }
5775 
5776   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5777   // elsewhere for GNU compatibility.
5778   uint64_t Size = getContext().getTypeSize(Ty);
5779   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5780   if (IsEmpty || Size == 0) {
5781     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5782       return ABIArgInfo::getIgnore();
5783 
5784     // GNU C mode. The only argument that gets ignored is an empty one with size
5785     // 0.
5786     if (IsEmpty && Size == 0)
5787       return ABIArgInfo::getIgnore();
5788     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5789   }
5790 
5791   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5792   const Type *Base = nullptr;
5793   uint64_t Members = 0;
5794   bool IsWin64 = Kind == Win64 || CallingConvention == llvm::CallingConv::Win64;
5795   bool IsWinVariadic = IsWin64 && IsVariadic;
5796   // In variadic functions on Windows, all composite types are treated alike,
5797   // no special handling of HFAs/HVAs.
5798   if (!IsWinVariadic && isHomogeneousAggregate(Ty, Base, Members)) {
5799     if (Kind != AArch64ABIInfo::AAPCS)
5800       return ABIArgInfo::getDirect(
5801           llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5802 
5803     // For alignment adjusted HFAs, cap the argument alignment to 16, leave it
5804     // default otherwise.
5805     unsigned Align =
5806         getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
5807     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
5808     Align = (Align > BaseAlign && Align >= 16) ? 16 : 0;
5809     return ABIArgInfo::getDirect(
5810         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members), 0,
5811         nullptr, true, Align);
5812   }
5813 
5814   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5815   if (Size <= 128) {
5816     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5817     // same size and alignment.
5818     if (getTarget().isRenderScriptTarget()) {
5819       return coerceToIntArray(Ty, getContext(), getVMContext());
5820     }
5821     unsigned Alignment;
5822     if (Kind == AArch64ABIInfo::AAPCS) {
5823       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5824       Alignment = Alignment < 128 ? 64 : 128;
5825     } else {
5826       Alignment = std::max(getContext().getTypeAlign(Ty),
5827                            (unsigned)getTarget().getPointerWidth(0));
5828     }
5829     Size = llvm::alignTo(Size, Alignment);
5830 
5831     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5832     // For aggregates with 16-byte alignment, we use i128.
5833     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5834     return ABIArgInfo::getDirect(
5835         Size == Alignment ? BaseTy
5836                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5837   }
5838 
5839   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5840 }
5841 
5842 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5843                                               bool IsVariadic) const {
5844   if (RetTy->isVoidType())
5845     return ABIArgInfo::getIgnore();
5846 
5847   if (const auto *VT = RetTy->getAs<VectorType>()) {
5848     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5849         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5850       return coerceIllegalVector(RetTy);
5851   }
5852 
5853   // Large vector types should be returned via memory.
5854   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5855     return getNaturalAlignIndirect(RetTy);
5856 
5857   if (!isAggregateTypeForABI(RetTy)) {
5858     // Treat an enum type as its underlying type.
5859     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5860       RetTy = EnumTy->getDecl()->getIntegerType();
5861 
5862     if (const auto *EIT = RetTy->getAs<BitIntType>())
5863       if (EIT->getNumBits() > 128)
5864         return getNaturalAlignIndirect(RetTy);
5865 
5866     return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
5867                 ? ABIArgInfo::getExtend(RetTy)
5868                 : ABIArgInfo::getDirect());
5869   }
5870 
5871   uint64_t Size = getContext().getTypeSize(RetTy);
5872   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5873     return ABIArgInfo::getIgnore();
5874 
5875   const Type *Base = nullptr;
5876   uint64_t Members = 0;
5877   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5878       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5879         IsVariadic))
5880     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5881     return ABIArgInfo::getDirect();
5882 
5883   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5884   if (Size <= 128) {
5885     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5886     // same size and alignment.
5887     if (getTarget().isRenderScriptTarget()) {
5888       return coerceToIntArray(RetTy, getContext(), getVMContext());
5889     }
5890 
5891     if (Size <= 64 && getDataLayout().isLittleEndian()) {
5892       // Composite types are returned in lower bits of a 64-bit register for LE,
5893       // and in higher bits for BE. However, integer types are always returned
5894       // in lower bits for both LE and BE, and they are not rounded up to
5895       // 64-bits. We can skip rounding up of composite types for LE, but not for
5896       // BE, otherwise composite types will be indistinguishable from integer
5897       // types.
5898       return ABIArgInfo::getDirect(
5899           llvm::IntegerType::get(getVMContext(), Size));
5900     }
5901 
5902     unsigned Alignment = getContext().getTypeAlign(RetTy);
5903     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5904 
5905     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5906     // For aggregates with 16-byte alignment, we use i128.
5907     if (Alignment < 128 && Size == 128) {
5908       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5909       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5910     }
5911     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5912   }
5913 
5914   return getNaturalAlignIndirect(RetTy);
5915 }
5916 
5917 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5918 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5919   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5920     // Check whether VT is a fixed-length SVE vector. These types are
5921     // represented as scalable vectors in function args/return and must be
5922     // coerced from fixed vectors.
5923     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5924         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5925       return true;
5926 
5927     // Check whether VT is legal.
5928     unsigned NumElements = VT->getNumElements();
5929     uint64_t Size = getContext().getTypeSize(VT);
5930     // NumElements should be power of 2.
5931     if (!llvm::isPowerOf2_32(NumElements))
5932       return true;
5933 
5934     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5935     // vectors for some reason.
5936     llvm::Triple Triple = getTarget().getTriple();
5937     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5938         Triple.isOSBinFormatMachO())
5939       return Size <= 32;
5940 
5941     return Size != 64 && (Size != 128 || NumElements == 1);
5942   }
5943   return false;
5944 }
5945 
5946 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5947                                                llvm::Type *eltTy,
5948                                                unsigned elts) const {
5949   if (!llvm::isPowerOf2_32(elts))
5950     return false;
5951   if (totalSize.getQuantity() != 8 &&
5952       (totalSize.getQuantity() != 16 || elts == 1))
5953     return false;
5954   return true;
5955 }
5956 
5957 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5958   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5959   // point type or a short-vector type. This is the same as the 32-bit ABI,
5960   // but with the difference that any floating-point type is allowed,
5961   // including __fp16.
5962   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5963     if (BT->isFloatingPoint())
5964       return true;
5965   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5966     unsigned VecSize = getContext().getTypeSize(VT);
5967     if (VecSize == 64 || VecSize == 128)
5968       return true;
5969   }
5970   return false;
5971 }
5972 
5973 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5974                                                        uint64_t Members) const {
5975   return Members <= 4;
5976 }
5977 
5978 bool AArch64ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate()
5979     const {
5980   // AAPCS64 says that the rule for whether something is a homogeneous
5981   // aggregate is applied to the output of the data layout decision. So
5982   // anything that doesn't affect the data layout also does not affect
5983   // homogeneity. In particular, zero-length bitfields don't stop a struct
5984   // being homogeneous.
5985   return true;
5986 }
5987 
5988 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5989                                        CodeGenFunction &CGF) const {
5990   ABIArgInfo AI = classifyArgumentType(Ty, /*IsVariadic=*/true,
5991                                        CGF.CurFnInfo->getCallingConvention());
5992   bool IsIndirect = AI.isIndirect();
5993 
5994   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5995   if (IsIndirect)
5996     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5997   else if (AI.getCoerceToType())
5998     BaseTy = AI.getCoerceToType();
5999 
6000   unsigned NumRegs = 1;
6001   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
6002     BaseTy = ArrTy->getElementType();
6003     NumRegs = ArrTy->getNumElements();
6004   }
6005   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
6006 
6007   // The AArch64 va_list type and handling is specified in the Procedure Call
6008   // Standard, section B.4:
6009   //
6010   // struct {
6011   //   void *__stack;
6012   //   void *__gr_top;
6013   //   void *__vr_top;
6014   //   int __gr_offs;
6015   //   int __vr_offs;
6016   // };
6017 
6018   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
6019   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6020   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
6021   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6022 
6023   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6024   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
6025 
6026   Address reg_offs_p = Address::invalid();
6027   llvm::Value *reg_offs = nullptr;
6028   int reg_top_index;
6029   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
6030   if (!IsFPR) {
6031     // 3 is the field number of __gr_offs
6032     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
6033     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
6034     reg_top_index = 1; // field number for __gr_top
6035     RegSize = llvm::alignTo(RegSize, 8);
6036   } else {
6037     // 4 is the field number of __vr_offs.
6038     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
6039     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
6040     reg_top_index = 2; // field number for __vr_top
6041     RegSize = 16 * NumRegs;
6042   }
6043 
6044   //=======================================
6045   // Find out where argument was passed
6046   //=======================================
6047 
6048   // If reg_offs >= 0 we're already using the stack for this type of
6049   // argument. We don't want to keep updating reg_offs (in case it overflows,
6050   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
6051   // whatever they get).
6052   llvm::Value *UsingStack = nullptr;
6053   UsingStack = CGF.Builder.CreateICmpSGE(
6054       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
6055 
6056   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
6057 
6058   // Otherwise, at least some kind of argument could go in these registers, the
6059   // question is whether this particular type is too big.
6060   CGF.EmitBlock(MaybeRegBlock);
6061 
6062   // Integer arguments may need to correct register alignment (for example a
6063   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
6064   // align __gr_offs to calculate the potential address.
6065   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
6066     int Align = TyAlign.getQuantity();
6067 
6068     reg_offs = CGF.Builder.CreateAdd(
6069         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
6070         "align_regoffs");
6071     reg_offs = CGF.Builder.CreateAnd(
6072         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
6073         "aligned_regoffs");
6074   }
6075 
6076   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
6077   // The fact that this is done unconditionally reflects the fact that
6078   // allocating an argument to the stack also uses up all the remaining
6079   // registers of the appropriate kind.
6080   llvm::Value *NewOffset = nullptr;
6081   NewOffset = CGF.Builder.CreateAdd(
6082       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
6083   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
6084 
6085   // Now we're in a position to decide whether this argument really was in
6086   // registers or not.
6087   llvm::Value *InRegs = nullptr;
6088   InRegs = CGF.Builder.CreateICmpSLE(
6089       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
6090 
6091   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
6092 
6093   //=======================================
6094   // Argument was in registers
6095   //=======================================
6096 
6097   // Now we emit the code for if the argument was originally passed in
6098   // registers. First start the appropriate block:
6099   CGF.EmitBlock(InRegBlock);
6100 
6101   llvm::Value *reg_top = nullptr;
6102   Address reg_top_p =
6103       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
6104   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
6105   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, reg_top, reg_offs),
6106                    CGF.Int8Ty, CharUnits::fromQuantity(IsFPR ? 16 : 8));
6107   Address RegAddr = Address::invalid();
6108   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty), *ElementTy = MemTy;
6109 
6110   if (IsIndirect) {
6111     // If it's been passed indirectly (actually a struct), whatever we find from
6112     // stored registers or on the stack will actually be a struct **.
6113     MemTy = llvm::PointerType::getUnqual(MemTy);
6114   }
6115 
6116   const Type *Base = nullptr;
6117   uint64_t NumMembers = 0;
6118   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
6119   if (IsHFA && NumMembers > 1) {
6120     // Homogeneous aggregates passed in registers will have their elements split
6121     // and stored 16-bytes apart regardless of size (they're notionally in qN,
6122     // qN+1, ...). We reload and store into a temporary local variable
6123     // contiguously.
6124     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
6125     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
6126     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
6127     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
6128     Address Tmp = CGF.CreateTempAlloca(HFATy,
6129                                        std::max(TyAlign, BaseTyInfo.Align));
6130 
6131     // On big-endian platforms, the value will be right-aligned in its slot.
6132     int Offset = 0;
6133     if (CGF.CGM.getDataLayout().isBigEndian() &&
6134         BaseTyInfo.Width.getQuantity() < 16)
6135       Offset = 16 - BaseTyInfo.Width.getQuantity();
6136 
6137     for (unsigned i = 0; i < NumMembers; ++i) {
6138       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
6139       Address LoadAddr =
6140         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
6141       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
6142 
6143       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
6144 
6145       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
6146       CGF.Builder.CreateStore(Elem, StoreAddr);
6147     }
6148 
6149     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
6150   } else {
6151     // Otherwise the object is contiguous in memory.
6152 
6153     // It might be right-aligned in its slot.
6154     CharUnits SlotSize = BaseAddr.getAlignment();
6155     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
6156         (IsHFA || !isAggregateTypeForABI(Ty)) &&
6157         TySize < SlotSize) {
6158       CharUnits Offset = SlotSize - TySize;
6159       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
6160     }
6161 
6162     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
6163   }
6164 
6165   CGF.EmitBranch(ContBlock);
6166 
6167   //=======================================
6168   // Argument was on the stack
6169   //=======================================
6170   CGF.EmitBlock(OnStackBlock);
6171 
6172   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
6173   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
6174 
6175   // Again, stack arguments may need realignment. In this case both integer and
6176   // floating-point ones might be affected.
6177   if (!IsIndirect && TyAlign.getQuantity() > 8) {
6178     int Align = TyAlign.getQuantity();
6179 
6180     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
6181 
6182     OnStackPtr = CGF.Builder.CreateAdd(
6183         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
6184         "align_stack");
6185     OnStackPtr = CGF.Builder.CreateAnd(
6186         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
6187         "align_stack");
6188 
6189     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
6190   }
6191   Address OnStackAddr = Address(OnStackPtr, CGF.Int8Ty,
6192                                 std::max(CharUnits::fromQuantity(8), TyAlign));
6193 
6194   // All stack slots are multiples of 8 bytes.
6195   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
6196   CharUnits StackSize;
6197   if (IsIndirect)
6198     StackSize = StackSlotSize;
6199   else
6200     StackSize = TySize.alignTo(StackSlotSize);
6201 
6202   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
6203   llvm::Value *NewStack = CGF.Builder.CreateInBoundsGEP(
6204       CGF.Int8Ty, OnStackPtr, StackSizeC, "new_stack");
6205 
6206   // Write the new value of __stack for the next call to va_arg
6207   CGF.Builder.CreateStore(NewStack, stack_p);
6208 
6209   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
6210       TySize < StackSlotSize) {
6211     CharUnits Offset = StackSlotSize - TySize;
6212     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
6213   }
6214 
6215   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
6216 
6217   CGF.EmitBranch(ContBlock);
6218 
6219   //=======================================
6220   // Tidy up
6221   //=======================================
6222   CGF.EmitBlock(ContBlock);
6223 
6224   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, OnStackAddr,
6225                                  OnStackBlock, "vaargs.addr");
6226 
6227   if (IsIndirect)
6228     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), ElementTy,
6229                    TyAlign);
6230 
6231   return ResAddr;
6232 }
6233 
6234 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
6235                                         CodeGenFunction &CGF) const {
6236   // The backend's lowering doesn't support va_arg for aggregates or
6237   // illegal vector types.  Lower VAArg here for these cases and use
6238   // the LLVM va_arg instruction for everything else.
6239   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
6240     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
6241 
6242   uint64_t PointerSize = getTarget().getPointerWidth(0) / 8;
6243   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6244 
6245   // Empty records are ignored for parameter passing purposes.
6246   if (isEmptyRecord(getContext(), Ty, true)) {
6247     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"),
6248                            getVAListElementType(CGF), SlotSize);
6249     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6250     return Addr;
6251   }
6252 
6253   // The size of the actual thing passed, which might end up just
6254   // being a pointer for indirect types.
6255   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6256 
6257   // Arguments bigger than 16 bytes which aren't homogeneous
6258   // aggregates should be passed indirectly.
6259   bool IsIndirect = false;
6260   if (TyInfo.Width.getQuantity() > 16) {
6261     const Type *Base = nullptr;
6262     uint64_t Members = 0;
6263     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
6264   }
6265 
6266   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6267                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
6268 }
6269 
6270 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
6271                                     QualType Ty) const {
6272   bool IsIndirect = false;
6273 
6274   // Composites larger than 16 bytes are passed by reference.
6275   if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) > 128)
6276     IsIndirect = true;
6277 
6278   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6279                           CGF.getContext().getTypeInfoInChars(Ty),
6280                           CharUnits::fromQuantity(8),
6281                           /*allowHigherAlign*/ false);
6282 }
6283 
6284 //===----------------------------------------------------------------------===//
6285 // ARM ABI Implementation
6286 //===----------------------------------------------------------------------===//
6287 
6288 namespace {
6289 
6290 class ARMABIInfo : public SwiftABIInfo {
6291 public:
6292   enum ABIKind {
6293     APCS = 0,
6294     AAPCS = 1,
6295     AAPCS_VFP = 2,
6296     AAPCS16_VFP = 3,
6297   };
6298 
6299 private:
6300   ABIKind Kind;
6301   bool IsFloatABISoftFP;
6302 
6303 public:
6304   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
6305       : SwiftABIInfo(CGT), Kind(_Kind) {
6306     setCCs();
6307     IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
6308         CGT.getCodeGenOpts().FloatABI == ""; // default
6309   }
6310 
6311   bool isEABI() const {
6312     switch (getTarget().getTriple().getEnvironment()) {
6313     case llvm::Triple::Android:
6314     case llvm::Triple::EABI:
6315     case llvm::Triple::EABIHF:
6316     case llvm::Triple::GNUEABI:
6317     case llvm::Triple::GNUEABIHF:
6318     case llvm::Triple::MuslEABI:
6319     case llvm::Triple::MuslEABIHF:
6320       return true;
6321     default:
6322       return false;
6323     }
6324   }
6325 
6326   bool isEABIHF() const {
6327     switch (getTarget().getTriple().getEnvironment()) {
6328     case llvm::Triple::EABIHF:
6329     case llvm::Triple::GNUEABIHF:
6330     case llvm::Triple::MuslEABIHF:
6331       return true;
6332     default:
6333       return false;
6334     }
6335   }
6336 
6337   ABIKind getABIKind() const { return Kind; }
6338 
6339   bool allowBFloatArgsAndRet() const override {
6340     return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
6341   }
6342 
6343 private:
6344   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
6345                                 unsigned functionCallConv) const;
6346   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
6347                                   unsigned functionCallConv) const;
6348   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
6349                                           uint64_t Members) const;
6350   ABIArgInfo coerceIllegalVector(QualType Ty) const;
6351   bool isIllegalVectorType(QualType Ty) const;
6352   bool containsAnyFP16Vectors(QualType Ty) const;
6353 
6354   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
6355   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
6356                                          uint64_t Members) const override;
6357   bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
6358 
6359   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
6360 
6361   void computeInfo(CGFunctionInfo &FI) const override;
6362 
6363   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6364                     QualType Ty) const override;
6365 
6366   llvm::CallingConv::ID getLLVMDefaultCC() const;
6367   llvm::CallingConv::ID getABIDefaultCC() const;
6368   void setCCs();
6369 
6370   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
6371                                     bool asReturnValue) const override {
6372     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6373   }
6374   bool isSwiftErrorInRegister() const override {
6375     return true;
6376   }
6377   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
6378                                  unsigned elts) const override;
6379 };
6380 
6381 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
6382 public:
6383   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6384       : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {}
6385 
6386   const ARMABIInfo &getABIInfo() const {
6387     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
6388   }
6389 
6390   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6391     return 13;
6392   }
6393 
6394   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
6395     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
6396   }
6397 
6398   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6399                                llvm::Value *Address) const override {
6400     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6401 
6402     // 0-15 are the 16 integer registers.
6403     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
6404     return false;
6405   }
6406 
6407   unsigned getSizeOfUnwindException() const override {
6408     if (getABIInfo().isEABI()) return 88;
6409     return TargetCodeGenInfo::getSizeOfUnwindException();
6410   }
6411 
6412   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6413                            CodeGen::CodeGenModule &CGM) const override {
6414     if (GV->isDeclaration())
6415       return;
6416     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6417     if (!FD)
6418       return;
6419     auto *Fn = cast<llvm::Function>(GV);
6420 
6421     if (const auto *TA = FD->getAttr<TargetAttr>()) {
6422       ParsedTargetAttr Attr = TA->parse();
6423       if (!Attr.BranchProtection.empty()) {
6424         TargetInfo::BranchProtectionInfo BPI;
6425         StringRef DiagMsg;
6426         StringRef Arch = Attr.Architecture.empty()
6427                              ? CGM.getTarget().getTargetOpts().CPU
6428                              : Attr.Architecture;
6429         if (!CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
6430                                                       Arch, BPI, DiagMsg)) {
6431           CGM.getDiags().Report(
6432               D->getLocation(),
6433               diag::warn_target_unsupported_branch_protection_attribute)
6434               << Arch;
6435         } else {
6436           static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
6437           assert(static_cast<unsigned>(BPI.SignReturnAddr) <= 2 &&
6438                  "Unexpected SignReturnAddressScopeKind");
6439           Fn->addFnAttr(
6440               "sign-return-address",
6441               SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
6442 
6443           Fn->addFnAttr("branch-target-enforcement",
6444                         BPI.BranchTargetEnforcement ? "true" : "false");
6445         }
6446       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
6447                  CGM.getLangOpts().hasSignReturnAddress()) {
6448         // If the Branch Protection attribute is missing, validate the target
6449         // Architecture attribute against Branch Protection command line
6450         // settings.
6451         if (!CGM.getTarget().isBranchProtectionSupportedArch(Attr.Architecture))
6452           CGM.getDiags().Report(
6453               D->getLocation(),
6454               diag::warn_target_unsupported_branch_protection_attribute)
6455               << Attr.Architecture;
6456       }
6457     }
6458 
6459     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
6460     if (!Attr)
6461       return;
6462 
6463     const char *Kind;
6464     switch (Attr->getInterrupt()) {
6465     case ARMInterruptAttr::Generic: Kind = ""; break;
6466     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
6467     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
6468     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
6469     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
6470     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
6471     }
6472 
6473     Fn->addFnAttr("interrupt", Kind);
6474 
6475     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
6476     if (ABI == ARMABIInfo::APCS)
6477       return;
6478 
6479     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
6480     // however this is not necessarily true on taking any interrupt. Instruct
6481     // the backend to perform a realignment as part of the function prologue.
6482     llvm::AttrBuilder B(Fn->getContext());
6483     B.addStackAlignmentAttr(8);
6484     Fn->addFnAttrs(B);
6485   }
6486 };
6487 
6488 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
6489 public:
6490   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6491       : ARMTargetCodeGenInfo(CGT, K) {}
6492 
6493   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6494                            CodeGen::CodeGenModule &CGM) const override;
6495 
6496   void getDependentLibraryOption(llvm::StringRef Lib,
6497                                  llvm::SmallString<24> &Opt) const override {
6498     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
6499   }
6500 
6501   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
6502                                llvm::SmallString<32> &Opt) const override {
6503     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
6504   }
6505 };
6506 
6507 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
6508     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
6509   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
6510   if (GV->isDeclaration())
6511     return;
6512   addStackProbeTargetAttributes(D, GV, CGM);
6513 }
6514 }
6515 
6516 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
6517   if (!::classifyReturnType(getCXXABI(), FI, *this))
6518     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
6519                                             FI.getCallingConvention());
6520 
6521   for (auto &I : FI.arguments())
6522     I.info = classifyArgumentType(I.type, FI.isVariadic(),
6523                                   FI.getCallingConvention());
6524 
6525 
6526   // Always honor user-specified calling convention.
6527   if (FI.getCallingConvention() != llvm::CallingConv::C)
6528     return;
6529 
6530   llvm::CallingConv::ID cc = getRuntimeCC();
6531   if (cc != llvm::CallingConv::C)
6532     FI.setEffectiveCallingConvention(cc);
6533 }
6534 
6535 /// Return the default calling convention that LLVM will use.
6536 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
6537   // The default calling convention that LLVM will infer.
6538   if (isEABIHF() || getTarget().getTriple().isWatchABI())
6539     return llvm::CallingConv::ARM_AAPCS_VFP;
6540   else if (isEABI())
6541     return llvm::CallingConv::ARM_AAPCS;
6542   else
6543     return llvm::CallingConv::ARM_APCS;
6544 }
6545 
6546 /// Return the calling convention that our ABI would like us to use
6547 /// as the C calling convention.
6548 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
6549   switch (getABIKind()) {
6550   case APCS: return llvm::CallingConv::ARM_APCS;
6551   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
6552   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6553   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6554   }
6555   llvm_unreachable("bad ABI kind");
6556 }
6557 
6558 void ARMABIInfo::setCCs() {
6559   assert(getRuntimeCC() == llvm::CallingConv::C);
6560 
6561   // Don't muddy up the IR with a ton of explicit annotations if
6562   // they'd just match what LLVM will infer from the triple.
6563   llvm::CallingConv::ID abiCC = getABIDefaultCC();
6564   if (abiCC != getLLVMDefaultCC())
6565     RuntimeCC = abiCC;
6566 }
6567 
6568 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
6569   uint64_t Size = getContext().getTypeSize(Ty);
6570   if (Size <= 32) {
6571     llvm::Type *ResType =
6572         llvm::Type::getInt32Ty(getVMContext());
6573     return ABIArgInfo::getDirect(ResType);
6574   }
6575   if (Size == 64 || Size == 128) {
6576     auto *ResType = llvm::FixedVectorType::get(
6577         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6578     return ABIArgInfo::getDirect(ResType);
6579   }
6580   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6581 }
6582 
6583 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
6584                                                     const Type *Base,
6585                                                     uint64_t Members) const {
6586   assert(Base && "Base class should be set for homogeneous aggregate");
6587   // Base can be a floating-point or a vector.
6588   if (const VectorType *VT = Base->getAs<VectorType>()) {
6589     // FP16 vectors should be converted to integer vectors
6590     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
6591       uint64_t Size = getContext().getTypeSize(VT);
6592       auto *NewVecTy = llvm::FixedVectorType::get(
6593           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6594       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
6595       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6596     }
6597   }
6598   unsigned Align = 0;
6599   if (getABIKind() == ARMABIInfo::AAPCS ||
6600       getABIKind() == ARMABIInfo::AAPCS_VFP) {
6601     // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
6602     // default otherwise.
6603     Align = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6604     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
6605     Align = (Align > BaseAlign && Align >= 8) ? 8 : 0;
6606   }
6607   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align);
6608 }
6609 
6610 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
6611                                             unsigned functionCallConv) const {
6612   // 6.1.2.1 The following argument types are VFP CPRCs:
6613   //   A single-precision floating-point type (including promoted
6614   //   half-precision types); A double-precision floating-point type;
6615   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
6616   //   with a Base Type of a single- or double-precision floating-point type,
6617   //   64-bit containerized vectors or 128-bit containerized vectors with one
6618   //   to four Elements.
6619   // Variadic functions should always marshal to the base standard.
6620   bool IsAAPCS_VFP =
6621       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
6622 
6623   Ty = useFirstFieldIfTransparentUnion(Ty);
6624 
6625   // Handle illegal vector types here.
6626   if (isIllegalVectorType(Ty))
6627     return coerceIllegalVector(Ty);
6628 
6629   if (!isAggregateTypeForABI(Ty)) {
6630     // Treat an enum type as its underlying type.
6631     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
6632       Ty = EnumTy->getDecl()->getIntegerType();
6633     }
6634 
6635     if (const auto *EIT = Ty->getAs<BitIntType>())
6636       if (EIT->getNumBits() > 64)
6637         return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6638 
6639     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
6640                                               : ABIArgInfo::getDirect());
6641   }
6642 
6643   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6644     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6645   }
6646 
6647   // Ignore empty records.
6648   if (isEmptyRecord(getContext(), Ty, true))
6649     return ABIArgInfo::getIgnore();
6650 
6651   if (IsAAPCS_VFP) {
6652     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
6653     // into VFP registers.
6654     const Type *Base = nullptr;
6655     uint64_t Members = 0;
6656     if (isHomogeneousAggregate(Ty, Base, Members))
6657       return classifyHomogeneousAggregate(Ty, Base, Members);
6658   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6659     // WatchOS does have homogeneous aggregates. Note that we intentionally use
6660     // this convention even for a variadic function: the backend will use GPRs
6661     // if needed.
6662     const Type *Base = nullptr;
6663     uint64_t Members = 0;
6664     if (isHomogeneousAggregate(Ty, Base, Members)) {
6665       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
6666       llvm::Type *Ty =
6667         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
6668       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6669     }
6670   }
6671 
6672   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6673       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
6674     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
6675     // bigger than 128-bits, they get placed in space allocated by the caller,
6676     // and a pointer is passed.
6677     return ABIArgInfo::getIndirect(
6678         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
6679   }
6680 
6681   // Support byval for ARM.
6682   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
6683   // most 8-byte. We realign the indirect argument if type alignment is bigger
6684   // than ABI alignment.
6685   uint64_t ABIAlign = 4;
6686   uint64_t TyAlign;
6687   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6688       getABIKind() == ARMABIInfo::AAPCS) {
6689     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6690     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
6691   } else {
6692     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
6693   }
6694   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
6695     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
6696     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
6697                                    /*ByVal=*/true,
6698                                    /*Realign=*/TyAlign > ABIAlign);
6699   }
6700 
6701   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
6702   // same size and alignment.
6703   if (getTarget().isRenderScriptTarget()) {
6704     return coerceToIntArray(Ty, getContext(), getVMContext());
6705   }
6706 
6707   // Otherwise, pass by coercing to a structure of the appropriate size.
6708   llvm::Type* ElemTy;
6709   unsigned SizeRegs;
6710   // FIXME: Try to match the types of the arguments more accurately where
6711   // we can.
6712   if (TyAlign <= 4) {
6713     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6714     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6715   } else {
6716     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6717     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6718   }
6719 
6720   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6721 }
6722 
6723 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6724                               llvm::LLVMContext &VMContext) {
6725   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6726   // is called integer-like if its size is less than or equal to one word, and
6727   // the offset of each of its addressable sub-fields is zero.
6728 
6729   uint64_t Size = Context.getTypeSize(Ty);
6730 
6731   // Check that the type fits in a word.
6732   if (Size > 32)
6733     return false;
6734 
6735   // FIXME: Handle vector types!
6736   if (Ty->isVectorType())
6737     return false;
6738 
6739   // Float types are never treated as "integer like".
6740   if (Ty->isRealFloatingType())
6741     return false;
6742 
6743   // If this is a builtin or pointer type then it is ok.
6744   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6745     return true;
6746 
6747   // Small complex integer types are "integer like".
6748   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6749     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6750 
6751   // Single element and zero sized arrays should be allowed, by the definition
6752   // above, but they are not.
6753 
6754   // Otherwise, it must be a record type.
6755   const RecordType *RT = Ty->getAs<RecordType>();
6756   if (!RT) return false;
6757 
6758   // Ignore records with flexible arrays.
6759   const RecordDecl *RD = RT->getDecl();
6760   if (RD->hasFlexibleArrayMember())
6761     return false;
6762 
6763   // Check that all sub-fields are at offset 0, and are themselves "integer
6764   // like".
6765   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6766 
6767   bool HadField = false;
6768   unsigned idx = 0;
6769   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6770        i != e; ++i, ++idx) {
6771     const FieldDecl *FD = *i;
6772 
6773     // Bit-fields are not addressable, we only need to verify they are "integer
6774     // like". We still have to disallow a subsequent non-bitfield, for example:
6775     //   struct { int : 0; int x }
6776     // is non-integer like according to gcc.
6777     if (FD->isBitField()) {
6778       if (!RD->isUnion())
6779         HadField = true;
6780 
6781       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6782         return false;
6783 
6784       continue;
6785     }
6786 
6787     // Check if this field is at offset 0.
6788     if (Layout.getFieldOffset(idx) != 0)
6789       return false;
6790 
6791     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6792       return false;
6793 
6794     // Only allow at most one field in a structure. This doesn't match the
6795     // wording above, but follows gcc in situations with a field following an
6796     // empty structure.
6797     if (!RD->isUnion()) {
6798       if (HadField)
6799         return false;
6800 
6801       HadField = true;
6802     }
6803   }
6804 
6805   return true;
6806 }
6807 
6808 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6809                                           unsigned functionCallConv) const {
6810 
6811   // Variadic functions should always marshal to the base standard.
6812   bool IsAAPCS_VFP =
6813       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6814 
6815   if (RetTy->isVoidType())
6816     return ABIArgInfo::getIgnore();
6817 
6818   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6819     // Large vector types should be returned via memory.
6820     if (getContext().getTypeSize(RetTy) > 128)
6821       return getNaturalAlignIndirect(RetTy);
6822     // TODO: FP16/BF16 vectors should be converted to integer vectors
6823     // This check is similar  to isIllegalVectorType - refactor?
6824     if ((!getTarget().hasLegalHalfType() &&
6825         (VT->getElementType()->isFloat16Type() ||
6826          VT->getElementType()->isHalfType())) ||
6827         (IsFloatABISoftFP &&
6828          VT->getElementType()->isBFloat16Type()))
6829       return coerceIllegalVector(RetTy);
6830   }
6831 
6832   if (!isAggregateTypeForABI(RetTy)) {
6833     // Treat an enum type as its underlying type.
6834     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6835       RetTy = EnumTy->getDecl()->getIntegerType();
6836 
6837     if (const auto *EIT = RetTy->getAs<BitIntType>())
6838       if (EIT->getNumBits() > 64)
6839         return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
6840 
6841     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6842                                                 : ABIArgInfo::getDirect();
6843   }
6844 
6845   // Are we following APCS?
6846   if (getABIKind() == APCS) {
6847     if (isEmptyRecord(getContext(), RetTy, false))
6848       return ABIArgInfo::getIgnore();
6849 
6850     // Complex types are all returned as packed integers.
6851     //
6852     // FIXME: Consider using 2 x vector types if the back end handles them
6853     // correctly.
6854     if (RetTy->isAnyComplexType())
6855       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6856           getVMContext(), getContext().getTypeSize(RetTy)));
6857 
6858     // Integer like structures are returned in r0.
6859     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6860       // Return in the smallest viable integer type.
6861       uint64_t Size = getContext().getTypeSize(RetTy);
6862       if (Size <= 8)
6863         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6864       if (Size <= 16)
6865         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6866       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6867     }
6868 
6869     // Otherwise return in memory.
6870     return getNaturalAlignIndirect(RetTy);
6871   }
6872 
6873   // Otherwise this is an AAPCS variant.
6874 
6875   if (isEmptyRecord(getContext(), RetTy, true))
6876     return ABIArgInfo::getIgnore();
6877 
6878   // Check for homogeneous aggregates with AAPCS-VFP.
6879   if (IsAAPCS_VFP) {
6880     const Type *Base = nullptr;
6881     uint64_t Members = 0;
6882     if (isHomogeneousAggregate(RetTy, Base, Members))
6883       return classifyHomogeneousAggregate(RetTy, Base, Members);
6884   }
6885 
6886   // Aggregates <= 4 bytes are returned in r0; other aggregates
6887   // are returned indirectly.
6888   uint64_t Size = getContext().getTypeSize(RetTy);
6889   if (Size <= 32) {
6890     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6891     // same size and alignment.
6892     if (getTarget().isRenderScriptTarget()) {
6893       return coerceToIntArray(RetTy, getContext(), getVMContext());
6894     }
6895     if (getDataLayout().isBigEndian())
6896       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6897       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6898 
6899     // Return in the smallest viable integer type.
6900     if (Size <= 8)
6901       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6902     if (Size <= 16)
6903       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6904     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6905   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6906     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6907     llvm::Type *CoerceTy =
6908         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6909     return ABIArgInfo::getDirect(CoerceTy);
6910   }
6911 
6912   return getNaturalAlignIndirect(RetTy);
6913 }
6914 
6915 /// isIllegalVector - check whether Ty is an illegal vector type.
6916 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6917   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6918     // On targets that don't support half, fp16 or bfloat, they are expanded
6919     // into float, and we don't want the ABI to depend on whether or not they
6920     // are supported in hardware. Thus return false to coerce vectors of these
6921     // types into integer vectors.
6922     // We do not depend on hasLegalHalfType for bfloat as it is a
6923     // separate IR type.
6924     if ((!getTarget().hasLegalHalfType() &&
6925         (VT->getElementType()->isFloat16Type() ||
6926          VT->getElementType()->isHalfType())) ||
6927         (IsFloatABISoftFP &&
6928          VT->getElementType()->isBFloat16Type()))
6929       return true;
6930     if (isAndroid()) {
6931       // Android shipped using Clang 3.1, which supported a slightly different
6932       // vector ABI. The primary differences were that 3-element vector types
6933       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6934       // accepts that legacy behavior for Android only.
6935       // Check whether VT is legal.
6936       unsigned NumElements = VT->getNumElements();
6937       // NumElements should be power of 2 or equal to 3.
6938       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6939         return true;
6940     } else {
6941       // Check whether VT is legal.
6942       unsigned NumElements = VT->getNumElements();
6943       uint64_t Size = getContext().getTypeSize(VT);
6944       // NumElements should be power of 2.
6945       if (!llvm::isPowerOf2_32(NumElements))
6946         return true;
6947       // Size should be greater than 32 bits.
6948       return Size <= 32;
6949     }
6950   }
6951   return false;
6952 }
6953 
6954 /// Return true if a type contains any 16-bit floating point vectors
6955 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6956   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6957     uint64_t NElements = AT->getSize().getZExtValue();
6958     if (NElements == 0)
6959       return false;
6960     return containsAnyFP16Vectors(AT->getElementType());
6961   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6962     const RecordDecl *RD = RT->getDecl();
6963 
6964     // If this is a C++ record, check the bases first.
6965     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6966       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6967             return containsAnyFP16Vectors(B.getType());
6968           }))
6969         return true;
6970 
6971     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6972           return FD && containsAnyFP16Vectors(FD->getType());
6973         }))
6974       return true;
6975 
6976     return false;
6977   } else {
6978     if (const VectorType *VT = Ty->getAs<VectorType>())
6979       return (VT->getElementType()->isFloat16Type() ||
6980               VT->getElementType()->isBFloat16Type() ||
6981               VT->getElementType()->isHalfType());
6982     return false;
6983   }
6984 }
6985 
6986 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6987                                            llvm::Type *eltTy,
6988                                            unsigned numElts) const {
6989   if (!llvm::isPowerOf2_32(numElts))
6990     return false;
6991   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6992   if (size > 64)
6993     return false;
6994   if (vectorSize.getQuantity() != 8 &&
6995       (vectorSize.getQuantity() != 16 || numElts == 1))
6996     return false;
6997   return true;
6998 }
6999 
7000 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7001   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
7002   // double, or 64-bit or 128-bit vectors.
7003   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
7004     if (BT->getKind() == BuiltinType::Float ||
7005         BT->getKind() == BuiltinType::Double ||
7006         BT->getKind() == BuiltinType::LongDouble)
7007       return true;
7008   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
7009     unsigned VecSize = getContext().getTypeSize(VT);
7010     if (VecSize == 64 || VecSize == 128)
7011       return true;
7012   }
7013   return false;
7014 }
7015 
7016 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
7017                                                    uint64_t Members) const {
7018   return Members <= 4;
7019 }
7020 
7021 bool ARMABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
7022   // AAPCS32 says that the rule for whether something is a homogeneous
7023   // aggregate is applied to the output of the data layout decision. So
7024   // anything that doesn't affect the data layout also does not affect
7025   // homogeneity. In particular, zero-length bitfields don't stop a struct
7026   // being homogeneous.
7027   return true;
7028 }
7029 
7030 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
7031                                         bool acceptHalf) const {
7032   // Give precedence to user-specified calling conventions.
7033   if (callConvention != llvm::CallingConv::C)
7034     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
7035   else
7036     return (getABIKind() == AAPCS_VFP) ||
7037            (acceptHalf && (getABIKind() == AAPCS16_VFP));
7038 }
7039 
7040 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7041                               QualType Ty) const {
7042   CharUnits SlotSize = CharUnits::fromQuantity(4);
7043 
7044   // Empty records are ignored for parameter passing purposes.
7045   if (isEmptyRecord(getContext(), Ty, true)) {
7046     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
7047                            getVAListElementType(CGF), SlotSize);
7048     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
7049     return Addr;
7050   }
7051 
7052   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
7053   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
7054 
7055   // Use indirect if size of the illegal vector is bigger than 16 bytes.
7056   bool IsIndirect = false;
7057   const Type *Base = nullptr;
7058   uint64_t Members = 0;
7059   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
7060     IsIndirect = true;
7061 
7062   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
7063   // allocated by the caller.
7064   } else if (TySize > CharUnits::fromQuantity(16) &&
7065              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
7066              !isHomogeneousAggregate(Ty, Base, Members)) {
7067     IsIndirect = true;
7068 
7069   // Otherwise, bound the type's ABI alignment.
7070   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
7071   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
7072   // Our callers should be prepared to handle an under-aligned address.
7073   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
7074              getABIKind() == ARMABIInfo::AAPCS) {
7075     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
7076     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
7077   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
7078     // ARMv7k allows type alignment up to 16 bytes.
7079     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
7080     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
7081   } else {
7082     TyAlignForABI = CharUnits::fromQuantity(4);
7083   }
7084 
7085   TypeInfoChars TyInfo(TySize, TyAlignForABI, AlignRequirementKind::None);
7086   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
7087                           SlotSize, /*AllowHigherAlign*/ true);
7088 }
7089 
7090 //===----------------------------------------------------------------------===//
7091 // NVPTX ABI Implementation
7092 //===----------------------------------------------------------------------===//
7093 
7094 namespace {
7095 
7096 class NVPTXTargetCodeGenInfo;
7097 
7098 class NVPTXABIInfo : public ABIInfo {
7099   NVPTXTargetCodeGenInfo &CGInfo;
7100 
7101 public:
7102   NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
7103       : ABIInfo(CGT), CGInfo(Info) {}
7104 
7105   ABIArgInfo classifyReturnType(QualType RetTy) const;
7106   ABIArgInfo classifyArgumentType(QualType Ty) const;
7107 
7108   void computeInfo(CGFunctionInfo &FI) const override;
7109   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7110                     QualType Ty) const override;
7111   bool isUnsupportedType(QualType T) const;
7112   ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
7113 };
7114 
7115 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
7116 public:
7117   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
7118       : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
7119 
7120   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7121                            CodeGen::CodeGenModule &M) const override;
7122   bool shouldEmitStaticExternCAliases() const override;
7123 
7124   llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
7125     // On the device side, surface reference is represented as an object handle
7126     // in 64-bit integer.
7127     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7128   }
7129 
7130   llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
7131     // On the device side, texture reference is represented as an object handle
7132     // in 64-bit integer.
7133     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7134   }
7135 
7136   bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7137                                               LValue Src) const override {
7138     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7139     return true;
7140   }
7141 
7142   bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7143                                               LValue Src) const override {
7144     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7145     return true;
7146   }
7147 
7148 private:
7149   // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
7150   // resulting MDNode to the nvvm.annotations MDNode.
7151   static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
7152                               int Operand);
7153 
7154   static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7155                                            LValue Src) {
7156     llvm::Value *Handle = nullptr;
7157     llvm::Constant *C =
7158         llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
7159     // Lookup `addrspacecast` through the constant pointer if any.
7160     if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
7161       C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
7162     if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
7163       // Load the handle from the specific global variable using
7164       // `nvvm.texsurf.handle.internal` intrinsic.
7165       Handle = CGF.EmitRuntimeCall(
7166           CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
7167                                {GV->getType()}),
7168           {GV}, "texsurf_handle");
7169     } else
7170       Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
7171     CGF.EmitStoreOfScalar(Handle, Dst);
7172   }
7173 };
7174 
7175 /// Checks if the type is unsupported directly by the current target.
7176 bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
7177   ASTContext &Context = getContext();
7178   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
7179     return true;
7180   if (!Context.getTargetInfo().hasFloat128Type() &&
7181       (T->isFloat128Type() ||
7182        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
7183     return true;
7184   if (const auto *EIT = T->getAs<BitIntType>())
7185     return EIT->getNumBits() >
7186            (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
7187   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
7188       Context.getTypeSize(T) > 64U)
7189     return true;
7190   if (const auto *AT = T->getAsArrayTypeUnsafe())
7191     return isUnsupportedType(AT->getElementType());
7192   const auto *RT = T->getAs<RecordType>();
7193   if (!RT)
7194     return false;
7195   const RecordDecl *RD = RT->getDecl();
7196 
7197   // If this is a C++ record, check the bases first.
7198   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7199     for (const CXXBaseSpecifier &I : CXXRD->bases())
7200       if (isUnsupportedType(I.getType()))
7201         return true;
7202 
7203   for (const FieldDecl *I : RD->fields())
7204     if (isUnsupportedType(I->getType()))
7205       return true;
7206   return false;
7207 }
7208 
7209 /// Coerce the given type into an array with maximum allowed size of elements.
7210 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
7211                                                    unsigned MaxSize) const {
7212   // Alignment and Size are measured in bits.
7213   const uint64_t Size = getContext().getTypeSize(Ty);
7214   const uint64_t Alignment = getContext().getTypeAlign(Ty);
7215   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
7216   llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
7217   const uint64_t NumElements = (Size + Div - 1) / Div;
7218   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
7219 }
7220 
7221 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
7222   if (RetTy->isVoidType())
7223     return ABIArgInfo::getIgnore();
7224 
7225   if (getContext().getLangOpts().OpenMP &&
7226       getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
7227     return coerceToIntArrayWithLimit(RetTy, 64);
7228 
7229   // note: this is different from default ABI
7230   if (!RetTy->isScalarType())
7231     return ABIArgInfo::getDirect();
7232 
7233   // Treat an enum type as its underlying type.
7234   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7235     RetTy = EnumTy->getDecl()->getIntegerType();
7236 
7237   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7238                                                : ABIArgInfo::getDirect());
7239 }
7240 
7241 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
7242   // Treat an enum type as its underlying type.
7243   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7244     Ty = EnumTy->getDecl()->getIntegerType();
7245 
7246   // Return aggregates type as indirect by value
7247   if (isAggregateTypeForABI(Ty)) {
7248     // Under CUDA device compilation, tex/surf builtin types are replaced with
7249     // object types and passed directly.
7250     if (getContext().getLangOpts().CUDAIsDevice) {
7251       if (Ty->isCUDADeviceBuiltinSurfaceType())
7252         return ABIArgInfo::getDirect(
7253             CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
7254       if (Ty->isCUDADeviceBuiltinTextureType())
7255         return ABIArgInfo::getDirect(
7256             CGInfo.getCUDADeviceBuiltinTextureDeviceType());
7257     }
7258     return getNaturalAlignIndirect(Ty, /* byval */ true);
7259   }
7260 
7261   if (const auto *EIT = Ty->getAs<BitIntType>()) {
7262     if ((EIT->getNumBits() > 128) ||
7263         (!getContext().getTargetInfo().hasInt128Type() &&
7264          EIT->getNumBits() > 64))
7265       return getNaturalAlignIndirect(Ty, /* byval */ true);
7266   }
7267 
7268   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
7269                                             : ABIArgInfo::getDirect());
7270 }
7271 
7272 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
7273   if (!getCXXABI().classifyReturnType(FI))
7274     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7275   for (auto &I : FI.arguments())
7276     I.info = classifyArgumentType(I.type);
7277 
7278   // Always honor user-specified calling convention.
7279   if (FI.getCallingConvention() != llvm::CallingConv::C)
7280     return;
7281 
7282   FI.setEffectiveCallingConvention(getRuntimeCC());
7283 }
7284 
7285 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7286                                 QualType Ty) const {
7287   llvm_unreachable("NVPTX does not support varargs");
7288 }
7289 
7290 void NVPTXTargetCodeGenInfo::setTargetAttributes(
7291     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7292   if (GV->isDeclaration())
7293     return;
7294   const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
7295   if (VD) {
7296     if (M.getLangOpts().CUDA) {
7297       if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
7298         addNVVMMetadata(GV, "surface", 1);
7299       else if (VD->getType()->isCUDADeviceBuiltinTextureType())
7300         addNVVMMetadata(GV, "texture", 1);
7301       return;
7302     }
7303   }
7304 
7305   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7306   if (!FD) return;
7307 
7308   llvm::Function *F = cast<llvm::Function>(GV);
7309 
7310   // Perform special handling in OpenCL mode
7311   if (M.getLangOpts().OpenCL) {
7312     // Use OpenCL function attributes to check for kernel functions
7313     // By default, all functions are device functions
7314     if (FD->hasAttr<OpenCLKernelAttr>()) {
7315       // OpenCL __kernel functions get kernel metadata
7316       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7317       addNVVMMetadata(F, "kernel", 1);
7318       // And kernel functions are not subject to inlining
7319       F->addFnAttr(llvm::Attribute::NoInline);
7320     }
7321   }
7322 
7323   // Perform special handling in CUDA mode.
7324   if (M.getLangOpts().CUDA) {
7325     // CUDA __global__ functions get a kernel metadata entry.  Since
7326     // __global__ functions cannot be called from the device, we do not
7327     // need to set the noinline attribute.
7328     if (FD->hasAttr<CUDAGlobalAttr>()) {
7329       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7330       addNVVMMetadata(F, "kernel", 1);
7331     }
7332     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
7333       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
7334       llvm::APSInt MaxThreads(32);
7335       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
7336       if (MaxThreads > 0)
7337         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
7338 
7339       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
7340       // not specified in __launch_bounds__ or if the user specified a 0 value,
7341       // we don't have to add a PTX directive.
7342       if (Attr->getMinBlocks()) {
7343         llvm::APSInt MinBlocks(32);
7344         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
7345         if (MinBlocks > 0)
7346           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
7347           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
7348       }
7349     }
7350   }
7351 }
7352 
7353 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
7354                                              StringRef Name, int Operand) {
7355   llvm::Module *M = GV->getParent();
7356   llvm::LLVMContext &Ctx = M->getContext();
7357 
7358   // Get "nvvm.annotations" metadata node
7359   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
7360 
7361   llvm::Metadata *MDVals[] = {
7362       llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
7363       llvm::ConstantAsMetadata::get(
7364           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
7365   // Append metadata to nvvm.annotations
7366   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7367 }
7368 
7369 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
7370   return false;
7371 }
7372 }
7373 
7374 //===----------------------------------------------------------------------===//
7375 // SystemZ ABI Implementation
7376 //===----------------------------------------------------------------------===//
7377 
7378 namespace {
7379 
7380 class SystemZABIInfo : public SwiftABIInfo {
7381   bool HasVector;
7382   bool IsSoftFloatABI;
7383 
7384 public:
7385   SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
7386     : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
7387 
7388   bool isPromotableIntegerTypeForABI(QualType Ty) const;
7389   bool isCompoundType(QualType Ty) const;
7390   bool isVectorArgumentType(QualType Ty) const;
7391   bool isFPArgumentType(QualType Ty) const;
7392   QualType GetSingleElementType(QualType Ty) const;
7393 
7394   ABIArgInfo classifyReturnType(QualType RetTy) const;
7395   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
7396 
7397   void computeInfo(CGFunctionInfo &FI) const override {
7398     if (!getCXXABI().classifyReturnType(FI))
7399       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7400     for (auto &I : FI.arguments())
7401       I.info = classifyArgumentType(I.type);
7402   }
7403 
7404   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7405                     QualType Ty) const override;
7406 
7407   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
7408                                     bool asReturnValue) const override {
7409     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
7410   }
7411   bool isSwiftErrorInRegister() const override {
7412     return false;
7413   }
7414 };
7415 
7416 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
7417 public:
7418   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
7419       : TargetCodeGenInfo(
7420             std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {}
7421 
7422   llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID,
7423                           CGBuilderTy &Builder,
7424                           CodeGenModule &CGM) const override {
7425     assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
7426     // Only use TDC in constrained FP mode.
7427     if (!Builder.getIsFPConstrained())
7428       return nullptr;
7429 
7430     llvm::Type *Ty = V->getType();
7431     if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) {
7432       llvm::Module &M = CGM.getModule();
7433       auto &Ctx = M.getContext();
7434       llvm::Function *TDCFunc =
7435           llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty);
7436       unsigned TDCBits = 0;
7437       switch (BuiltinID) {
7438       case Builtin::BI__builtin_isnan:
7439         TDCBits = 0xf;
7440         break;
7441       case Builtin::BIfinite:
7442       case Builtin::BI__finite:
7443       case Builtin::BIfinitef:
7444       case Builtin::BI__finitef:
7445       case Builtin::BIfinitel:
7446       case Builtin::BI__finitel:
7447       case Builtin::BI__builtin_isfinite:
7448         TDCBits = 0xfc0;
7449         break;
7450       case Builtin::BI__builtin_isinf:
7451         TDCBits = 0x30;
7452         break;
7453       default:
7454         break;
7455       }
7456       if (TDCBits)
7457         return Builder.CreateCall(
7458             TDCFunc,
7459             {V, llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), TDCBits)});
7460     }
7461     return nullptr;
7462   }
7463 };
7464 }
7465 
7466 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
7467   // Treat an enum type as its underlying type.
7468   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7469     Ty = EnumTy->getDecl()->getIntegerType();
7470 
7471   // Promotable integer types are required to be promoted by the ABI.
7472   if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
7473     return true;
7474 
7475   if (const auto *EIT = Ty->getAs<BitIntType>())
7476     if (EIT->getNumBits() < 64)
7477       return true;
7478 
7479   // 32-bit values must also be promoted.
7480   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7481     switch (BT->getKind()) {
7482     case BuiltinType::Int:
7483     case BuiltinType::UInt:
7484       return true;
7485     default:
7486       return false;
7487     }
7488   return false;
7489 }
7490 
7491 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
7492   return (Ty->isAnyComplexType() ||
7493           Ty->isVectorType() ||
7494           isAggregateTypeForABI(Ty));
7495 }
7496 
7497 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
7498   return (HasVector &&
7499           Ty->isVectorType() &&
7500           getContext().getTypeSize(Ty) <= 128);
7501 }
7502 
7503 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
7504   if (IsSoftFloatABI)
7505     return false;
7506 
7507   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7508     switch (BT->getKind()) {
7509     case BuiltinType::Float:
7510     case BuiltinType::Double:
7511       return true;
7512     default:
7513       return false;
7514     }
7515 
7516   return false;
7517 }
7518 
7519 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
7520   const RecordType *RT = Ty->getAs<RecordType>();
7521 
7522   if (RT && RT->isStructureOrClassType()) {
7523     const RecordDecl *RD = RT->getDecl();
7524     QualType Found;
7525 
7526     // If this is a C++ record, check the bases first.
7527     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7528       for (const auto &I : CXXRD->bases()) {
7529         QualType Base = I.getType();
7530 
7531         // Empty bases don't affect things either way.
7532         if (isEmptyRecord(getContext(), Base, true))
7533           continue;
7534 
7535         if (!Found.isNull())
7536           return Ty;
7537         Found = GetSingleElementType(Base);
7538       }
7539 
7540     // Check the fields.
7541     for (const auto *FD : RD->fields()) {
7542       // Unlike isSingleElementStruct(), empty structure and array fields
7543       // do count.  So do anonymous bitfields that aren't zero-sized.
7544 
7545       // Like isSingleElementStruct(), ignore C++20 empty data members.
7546       if (FD->hasAttr<NoUniqueAddressAttr>() &&
7547           isEmptyRecord(getContext(), FD->getType(), true))
7548         continue;
7549 
7550       // Unlike isSingleElementStruct(), arrays do not count.
7551       // Nested structures still do though.
7552       if (!Found.isNull())
7553         return Ty;
7554       Found = GetSingleElementType(FD->getType());
7555     }
7556 
7557     // Unlike isSingleElementStruct(), trailing padding is allowed.
7558     // An 8-byte aligned struct s { float f; } is passed as a double.
7559     if (!Found.isNull())
7560       return Found;
7561   }
7562 
7563   return Ty;
7564 }
7565 
7566 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7567                                   QualType Ty) const {
7568   // Assume that va_list type is correct; should be pointer to LLVM type:
7569   // struct {
7570   //   i64 __gpr;
7571   //   i64 __fpr;
7572   //   i8 *__overflow_arg_area;
7573   //   i8 *__reg_save_area;
7574   // };
7575 
7576   // Every non-vector argument occupies 8 bytes and is passed by preference
7577   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
7578   // always passed on the stack.
7579   Ty = getContext().getCanonicalType(Ty);
7580   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7581   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
7582   llvm::Type *DirectTy = ArgTy;
7583   ABIArgInfo AI = classifyArgumentType(Ty);
7584   bool IsIndirect = AI.isIndirect();
7585   bool InFPRs = false;
7586   bool IsVector = false;
7587   CharUnits UnpaddedSize;
7588   CharUnits DirectAlign;
7589   if (IsIndirect) {
7590     DirectTy = llvm::PointerType::getUnqual(DirectTy);
7591     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
7592   } else {
7593     if (AI.getCoerceToType())
7594       ArgTy = AI.getCoerceToType();
7595     InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
7596     IsVector = ArgTy->isVectorTy();
7597     UnpaddedSize = TyInfo.Width;
7598     DirectAlign = TyInfo.Align;
7599   }
7600   CharUnits PaddedSize = CharUnits::fromQuantity(8);
7601   if (IsVector && UnpaddedSize > PaddedSize)
7602     PaddedSize = CharUnits::fromQuantity(16);
7603   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
7604 
7605   CharUnits Padding = (PaddedSize - UnpaddedSize);
7606 
7607   llvm::Type *IndexTy = CGF.Int64Ty;
7608   llvm::Value *PaddedSizeV =
7609     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
7610 
7611   if (IsVector) {
7612     // Work out the address of a vector argument on the stack.
7613     // Vector arguments are always passed in the high bits of a
7614     // single (8 byte) or double (16 byte) stack slot.
7615     Address OverflowArgAreaPtr =
7616         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7617     Address OverflowArgArea =
7618         Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7619                 CGF.Int8Ty, TyInfo.Align);
7620     Address MemAddr =
7621         CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
7622 
7623     // Update overflow_arg_area_ptr pointer
7624     llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP(
7625         OverflowArgArea.getElementType(), OverflowArgArea.getPointer(),
7626         PaddedSizeV, "overflow_arg_area");
7627     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7628 
7629     return MemAddr;
7630   }
7631 
7632   assert(PaddedSize.getQuantity() == 8);
7633 
7634   unsigned MaxRegs, RegCountField, RegSaveIndex;
7635   CharUnits RegPadding;
7636   if (InFPRs) {
7637     MaxRegs = 4; // Maximum of 4 FPR arguments
7638     RegCountField = 1; // __fpr
7639     RegSaveIndex = 16; // save offset for f0
7640     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
7641   } else {
7642     MaxRegs = 5; // Maximum of 5 GPR arguments
7643     RegCountField = 0; // __gpr
7644     RegSaveIndex = 2; // save offset for r2
7645     RegPadding = Padding; // values are passed in the low bits of a GPR
7646   }
7647 
7648   Address RegCountPtr =
7649       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
7650   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
7651   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
7652   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
7653                                                  "fits_in_regs");
7654 
7655   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
7656   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
7657   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
7658   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
7659 
7660   // Emit code to load the value if it was passed in registers.
7661   CGF.EmitBlock(InRegBlock);
7662 
7663   // Work out the address of an argument register.
7664   llvm::Value *ScaledRegCount =
7665     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
7666   llvm::Value *RegBase =
7667     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
7668                                       + RegPadding.getQuantity());
7669   llvm::Value *RegOffset =
7670     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
7671   Address RegSaveAreaPtr =
7672       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
7673   llvm::Value *RegSaveArea =
7674       CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
7675   Address RawRegAddr(
7676       CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, RegOffset, "raw_reg_addr"),
7677       CGF.Int8Ty, PaddedSize);
7678   Address RegAddr =
7679       CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
7680 
7681   // Update the register count
7682   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
7683   llvm::Value *NewRegCount =
7684     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
7685   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
7686   CGF.EmitBranch(ContBlock);
7687 
7688   // Emit code to load the value if it was passed in memory.
7689   CGF.EmitBlock(InMemBlock);
7690 
7691   // Work out the address of a stack argument.
7692   Address OverflowArgAreaPtr =
7693       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7694   Address OverflowArgArea =
7695       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7696               CGF.Int8Ty, PaddedSize);
7697   Address RawMemAddr =
7698       CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
7699   Address MemAddr =
7700     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
7701 
7702   // Update overflow_arg_area_ptr pointer
7703   llvm::Value *NewOverflowArgArea =
7704     CGF.Builder.CreateGEP(OverflowArgArea.getElementType(),
7705                           OverflowArgArea.getPointer(), PaddedSizeV,
7706                           "overflow_arg_area");
7707   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7708   CGF.EmitBranch(ContBlock);
7709 
7710   // Return the appropriate result.
7711   CGF.EmitBlock(ContBlock);
7712   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
7713                                  "va_arg.addr");
7714 
7715   if (IsIndirect)
7716     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), ArgTy,
7717                       TyInfo.Align);
7718 
7719   return ResAddr;
7720 }
7721 
7722 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
7723   if (RetTy->isVoidType())
7724     return ABIArgInfo::getIgnore();
7725   if (isVectorArgumentType(RetTy))
7726     return ABIArgInfo::getDirect();
7727   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
7728     return getNaturalAlignIndirect(RetTy);
7729   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7730                                                : ABIArgInfo::getDirect());
7731 }
7732 
7733 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
7734   // Handle the generic C++ ABI.
7735   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7736     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7737 
7738   // Integers and enums are extended to full register width.
7739   if (isPromotableIntegerTypeForABI(Ty))
7740     return ABIArgInfo::getExtend(Ty);
7741 
7742   // Handle vector types and vector-like structure types.  Note that
7743   // as opposed to float-like structure types, we do not allow any
7744   // padding for vector-like structures, so verify the sizes match.
7745   uint64_t Size = getContext().getTypeSize(Ty);
7746   QualType SingleElementTy = GetSingleElementType(Ty);
7747   if (isVectorArgumentType(SingleElementTy) &&
7748       getContext().getTypeSize(SingleElementTy) == Size)
7749     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
7750 
7751   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
7752   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
7753     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7754 
7755   // Handle small structures.
7756   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7757     // Structures with flexible arrays have variable length, so really
7758     // fail the size test above.
7759     const RecordDecl *RD = RT->getDecl();
7760     if (RD->hasFlexibleArrayMember())
7761       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7762 
7763     // The structure is passed as an unextended integer, a float, or a double.
7764     llvm::Type *PassTy;
7765     if (isFPArgumentType(SingleElementTy)) {
7766       assert(Size == 32 || Size == 64);
7767       if (Size == 32)
7768         PassTy = llvm::Type::getFloatTy(getVMContext());
7769       else
7770         PassTy = llvm::Type::getDoubleTy(getVMContext());
7771     } else
7772       PassTy = llvm::IntegerType::get(getVMContext(), Size);
7773     return ABIArgInfo::getDirect(PassTy);
7774   }
7775 
7776   // Non-structure compounds are passed indirectly.
7777   if (isCompoundType(Ty))
7778     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7779 
7780   return ABIArgInfo::getDirect(nullptr);
7781 }
7782 
7783 //===----------------------------------------------------------------------===//
7784 // MSP430 ABI Implementation
7785 //===----------------------------------------------------------------------===//
7786 
7787 namespace {
7788 
7789 class MSP430ABIInfo : public DefaultABIInfo {
7790   static ABIArgInfo complexArgInfo() {
7791     ABIArgInfo Info = ABIArgInfo::getDirect();
7792     Info.setCanBeFlattened(false);
7793     return Info;
7794   }
7795 
7796 public:
7797   MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7798 
7799   ABIArgInfo classifyReturnType(QualType RetTy) const {
7800     if (RetTy->isAnyComplexType())
7801       return complexArgInfo();
7802 
7803     return DefaultABIInfo::classifyReturnType(RetTy);
7804   }
7805 
7806   ABIArgInfo classifyArgumentType(QualType RetTy) const {
7807     if (RetTy->isAnyComplexType())
7808       return complexArgInfo();
7809 
7810     return DefaultABIInfo::classifyArgumentType(RetTy);
7811   }
7812 
7813   // Just copy the original implementations because
7814   // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
7815   void computeInfo(CGFunctionInfo &FI) const override {
7816     if (!getCXXABI().classifyReturnType(FI))
7817       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7818     for (auto &I : FI.arguments())
7819       I.info = classifyArgumentType(I.type);
7820   }
7821 
7822   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7823                     QualType Ty) const override {
7824     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
7825   }
7826 };
7827 
7828 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7829 public:
7830   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7831       : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
7832   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7833                            CodeGen::CodeGenModule &M) const override;
7834 };
7835 
7836 }
7837 
7838 void MSP430TargetCodeGenInfo::setTargetAttributes(
7839     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7840   if (GV->isDeclaration())
7841     return;
7842   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7843     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7844     if (!InterruptAttr)
7845       return;
7846 
7847     // Handle 'interrupt' attribute:
7848     llvm::Function *F = cast<llvm::Function>(GV);
7849 
7850     // Step 1: Set ISR calling convention.
7851     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7852 
7853     // Step 2: Add attributes goodness.
7854     F->addFnAttr(llvm::Attribute::NoInline);
7855     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7856   }
7857 }
7858 
7859 //===----------------------------------------------------------------------===//
7860 // MIPS ABI Implementation.  This works for both little-endian and
7861 // big-endian variants.
7862 //===----------------------------------------------------------------------===//
7863 
7864 namespace {
7865 class MipsABIInfo : public ABIInfo {
7866   bool IsO32;
7867   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7868   void CoerceToIntArgs(uint64_t TySize,
7869                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7870   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7871   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7872   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7873 public:
7874   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7875     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7876     StackAlignInBytes(IsO32 ? 8 : 16) {}
7877 
7878   ABIArgInfo classifyReturnType(QualType RetTy) const;
7879   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7880   void computeInfo(CGFunctionInfo &FI) const override;
7881   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7882                     QualType Ty) const override;
7883   ABIArgInfo extendType(QualType Ty) const;
7884 };
7885 
7886 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7887   unsigned SizeOfUnwindException;
7888 public:
7889   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7890       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7891         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7892 
7893   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7894     return 29;
7895   }
7896 
7897   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7898                            CodeGen::CodeGenModule &CGM) const override {
7899     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7900     if (!FD) return;
7901     llvm::Function *Fn = cast<llvm::Function>(GV);
7902 
7903     if (FD->hasAttr<MipsLongCallAttr>())
7904       Fn->addFnAttr("long-call");
7905     else if (FD->hasAttr<MipsShortCallAttr>())
7906       Fn->addFnAttr("short-call");
7907 
7908     // Other attributes do not have a meaning for declarations.
7909     if (GV->isDeclaration())
7910       return;
7911 
7912     if (FD->hasAttr<Mips16Attr>()) {
7913       Fn->addFnAttr("mips16");
7914     }
7915     else if (FD->hasAttr<NoMips16Attr>()) {
7916       Fn->addFnAttr("nomips16");
7917     }
7918 
7919     if (FD->hasAttr<MicroMipsAttr>())
7920       Fn->addFnAttr("micromips");
7921     else if (FD->hasAttr<NoMicroMipsAttr>())
7922       Fn->addFnAttr("nomicromips");
7923 
7924     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7925     if (!Attr)
7926       return;
7927 
7928     const char *Kind;
7929     switch (Attr->getInterrupt()) {
7930     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7931     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7932     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7933     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7934     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7935     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7936     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7937     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7938     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7939     }
7940 
7941     Fn->addFnAttr("interrupt", Kind);
7942 
7943   }
7944 
7945   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7946                                llvm::Value *Address) const override;
7947 
7948   unsigned getSizeOfUnwindException() const override {
7949     return SizeOfUnwindException;
7950   }
7951 };
7952 }
7953 
7954 void MipsABIInfo::CoerceToIntArgs(
7955     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7956   llvm::IntegerType *IntTy =
7957     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7958 
7959   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7960   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7961     ArgList.push_back(IntTy);
7962 
7963   // If necessary, add one more integer type to ArgList.
7964   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7965 
7966   if (R)
7967     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7968 }
7969 
7970 // In N32/64, an aligned double precision floating point field is passed in
7971 // a register.
7972 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7973   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7974 
7975   if (IsO32) {
7976     CoerceToIntArgs(TySize, ArgList);
7977     return llvm::StructType::get(getVMContext(), ArgList);
7978   }
7979 
7980   if (Ty->isComplexType())
7981     return CGT.ConvertType(Ty);
7982 
7983   const RecordType *RT = Ty->getAs<RecordType>();
7984 
7985   // Unions/vectors are passed in integer registers.
7986   if (!RT || !RT->isStructureOrClassType()) {
7987     CoerceToIntArgs(TySize, ArgList);
7988     return llvm::StructType::get(getVMContext(), ArgList);
7989   }
7990 
7991   const RecordDecl *RD = RT->getDecl();
7992   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7993   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7994 
7995   uint64_t LastOffset = 0;
7996   unsigned idx = 0;
7997   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7998 
7999   // Iterate over fields in the struct/class and check if there are any aligned
8000   // double fields.
8001   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
8002        i != e; ++i, ++idx) {
8003     const QualType Ty = i->getType();
8004     const BuiltinType *BT = Ty->getAs<BuiltinType>();
8005 
8006     if (!BT || BT->getKind() != BuiltinType::Double)
8007       continue;
8008 
8009     uint64_t Offset = Layout.getFieldOffset(idx);
8010     if (Offset % 64) // Ignore doubles that are not aligned.
8011       continue;
8012 
8013     // Add ((Offset - LastOffset) / 64) args of type i64.
8014     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
8015       ArgList.push_back(I64);
8016 
8017     // Add double type.
8018     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
8019     LastOffset = Offset + 64;
8020   }
8021 
8022   CoerceToIntArgs(TySize - LastOffset, IntArgList);
8023   ArgList.append(IntArgList.begin(), IntArgList.end());
8024 
8025   return llvm::StructType::get(getVMContext(), ArgList);
8026 }
8027 
8028 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
8029                                         uint64_t Offset) const {
8030   if (OrigOffset + MinABIStackAlignInBytes > Offset)
8031     return nullptr;
8032 
8033   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
8034 }
8035 
8036 ABIArgInfo
8037 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
8038   Ty = useFirstFieldIfTransparentUnion(Ty);
8039 
8040   uint64_t OrigOffset = Offset;
8041   uint64_t TySize = getContext().getTypeSize(Ty);
8042   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
8043 
8044   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
8045                    (uint64_t)StackAlignInBytes);
8046   unsigned CurrOffset = llvm::alignTo(Offset, Align);
8047   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
8048 
8049   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
8050     // Ignore empty aggregates.
8051     if (TySize == 0)
8052       return ABIArgInfo::getIgnore();
8053 
8054     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
8055       Offset = OrigOffset + MinABIStackAlignInBytes;
8056       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8057     }
8058 
8059     // If we have reached here, aggregates are passed directly by coercing to
8060     // another structure type. Padding is inserted if the offset of the
8061     // aggregate is unaligned.
8062     ABIArgInfo ArgInfo =
8063         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
8064                               getPaddingType(OrigOffset, CurrOffset));
8065     ArgInfo.setInReg(true);
8066     return ArgInfo;
8067   }
8068 
8069   // Treat an enum type as its underlying type.
8070   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8071     Ty = EnumTy->getDecl()->getIntegerType();
8072 
8073   // Make sure we pass indirectly things that are too large.
8074   if (const auto *EIT = Ty->getAs<BitIntType>())
8075     if (EIT->getNumBits() > 128 ||
8076         (EIT->getNumBits() > 64 &&
8077          !getContext().getTargetInfo().hasInt128Type()))
8078       return getNaturalAlignIndirect(Ty);
8079 
8080   // All integral types are promoted to the GPR width.
8081   if (Ty->isIntegralOrEnumerationType())
8082     return extendType(Ty);
8083 
8084   return ABIArgInfo::getDirect(
8085       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
8086 }
8087 
8088 llvm::Type*
8089 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
8090   const RecordType *RT = RetTy->getAs<RecordType>();
8091   SmallVector<llvm::Type*, 8> RTList;
8092 
8093   if (RT && RT->isStructureOrClassType()) {
8094     const RecordDecl *RD = RT->getDecl();
8095     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
8096     unsigned FieldCnt = Layout.getFieldCount();
8097 
8098     // N32/64 returns struct/classes in floating point registers if the
8099     // following conditions are met:
8100     // 1. The size of the struct/class is no larger than 128-bit.
8101     // 2. The struct/class has one or two fields all of which are floating
8102     //    point types.
8103     // 3. The offset of the first field is zero (this follows what gcc does).
8104     //
8105     // Any other composite results are returned in integer registers.
8106     //
8107     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
8108       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
8109       for (; b != e; ++b) {
8110         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
8111 
8112         if (!BT || !BT->isFloatingPoint())
8113           break;
8114 
8115         RTList.push_back(CGT.ConvertType(b->getType()));
8116       }
8117 
8118       if (b == e)
8119         return llvm::StructType::get(getVMContext(), RTList,
8120                                      RD->hasAttr<PackedAttr>());
8121 
8122       RTList.clear();
8123     }
8124   }
8125 
8126   CoerceToIntArgs(Size, RTList);
8127   return llvm::StructType::get(getVMContext(), RTList);
8128 }
8129 
8130 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
8131   uint64_t Size = getContext().getTypeSize(RetTy);
8132 
8133   if (RetTy->isVoidType())
8134     return ABIArgInfo::getIgnore();
8135 
8136   // O32 doesn't treat zero-sized structs differently from other structs.
8137   // However, N32/N64 ignores zero sized return values.
8138   if (!IsO32 && Size == 0)
8139     return ABIArgInfo::getIgnore();
8140 
8141   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
8142     if (Size <= 128) {
8143       if (RetTy->isAnyComplexType())
8144         return ABIArgInfo::getDirect();
8145 
8146       // O32 returns integer vectors in registers and N32/N64 returns all small
8147       // aggregates in registers.
8148       if (!IsO32 ||
8149           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
8150         ABIArgInfo ArgInfo =
8151             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
8152         ArgInfo.setInReg(true);
8153         return ArgInfo;
8154       }
8155     }
8156 
8157     return getNaturalAlignIndirect(RetTy);
8158   }
8159 
8160   // Treat an enum type as its underlying type.
8161   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8162     RetTy = EnumTy->getDecl()->getIntegerType();
8163 
8164   // Make sure we pass indirectly things that are too large.
8165   if (const auto *EIT = RetTy->getAs<BitIntType>())
8166     if (EIT->getNumBits() > 128 ||
8167         (EIT->getNumBits() > 64 &&
8168          !getContext().getTargetInfo().hasInt128Type()))
8169       return getNaturalAlignIndirect(RetTy);
8170 
8171   if (isPromotableIntegerTypeForABI(RetTy))
8172     return ABIArgInfo::getExtend(RetTy);
8173 
8174   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
8175       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
8176     return ABIArgInfo::getSignExtend(RetTy);
8177 
8178   return ABIArgInfo::getDirect();
8179 }
8180 
8181 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
8182   ABIArgInfo &RetInfo = FI.getReturnInfo();
8183   if (!getCXXABI().classifyReturnType(FI))
8184     RetInfo = classifyReturnType(FI.getReturnType());
8185 
8186   // Check if a pointer to an aggregate is passed as a hidden argument.
8187   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
8188 
8189   for (auto &I : FI.arguments())
8190     I.info = classifyArgumentType(I.type, Offset);
8191 }
8192 
8193 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8194                                QualType OrigTy) const {
8195   QualType Ty = OrigTy;
8196 
8197   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
8198   // Pointers are also promoted in the same way but this only matters for N32.
8199   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
8200   unsigned PtrWidth = getTarget().getPointerWidth(0);
8201   bool DidPromote = false;
8202   if ((Ty->isIntegerType() &&
8203           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
8204       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
8205     DidPromote = true;
8206     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
8207                                             Ty->isSignedIntegerType());
8208   }
8209 
8210   auto TyInfo = getContext().getTypeInfoInChars(Ty);
8211 
8212   // The alignment of things in the argument area is never larger than
8213   // StackAlignInBytes.
8214   TyInfo.Align =
8215     std::min(TyInfo.Align, CharUnits::fromQuantity(StackAlignInBytes));
8216 
8217   // MinABIStackAlignInBytes is the size of argument slots on the stack.
8218   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
8219 
8220   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
8221                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
8222 
8223 
8224   // If there was a promotion, "unpromote" into a temporary.
8225   // TODO: can we just use a pointer into a subset of the original slot?
8226   if (DidPromote) {
8227     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
8228     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
8229 
8230     // Truncate down to the right width.
8231     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
8232                                                  : CGF.IntPtrTy);
8233     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
8234     if (OrigTy->isPointerType())
8235       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
8236 
8237     CGF.Builder.CreateStore(V, Temp);
8238     Addr = Temp;
8239   }
8240 
8241   return Addr;
8242 }
8243 
8244 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
8245   int TySize = getContext().getTypeSize(Ty);
8246 
8247   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
8248   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
8249     return ABIArgInfo::getSignExtend(Ty);
8250 
8251   return ABIArgInfo::getExtend(Ty);
8252 }
8253 
8254 bool
8255 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8256                                                llvm::Value *Address) const {
8257   // This information comes from gcc's implementation, which seems to
8258   // as canonical as it gets.
8259 
8260   // Everything on MIPS is 4 bytes.  Double-precision FP registers
8261   // are aliased to pairs of single-precision FP registers.
8262   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8263 
8264   // 0-31 are the general purpose registers, $0 - $31.
8265   // 32-63 are the floating-point registers, $f0 - $f31.
8266   // 64 and 65 are the multiply/divide registers, $hi and $lo.
8267   // 66 is the (notional, I think) register for signal-handler return.
8268   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
8269 
8270   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
8271   // They are one bit wide and ignored here.
8272 
8273   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
8274   // (coprocessor 1 is the FP unit)
8275   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
8276   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
8277   // 176-181 are the DSP accumulator registers.
8278   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
8279   return false;
8280 }
8281 
8282 //===----------------------------------------------------------------------===//
8283 // M68k ABI Implementation
8284 //===----------------------------------------------------------------------===//
8285 
8286 namespace {
8287 
8288 class M68kTargetCodeGenInfo : public TargetCodeGenInfo {
8289 public:
8290   M68kTargetCodeGenInfo(CodeGenTypes &CGT)
8291       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
8292   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8293                            CodeGen::CodeGenModule &M) const override;
8294 };
8295 
8296 } // namespace
8297 
8298 void M68kTargetCodeGenInfo::setTargetAttributes(
8299     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8300   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
8301     if (const auto *attr = FD->getAttr<M68kInterruptAttr>()) {
8302       // Handle 'interrupt' attribute:
8303       llvm::Function *F = cast<llvm::Function>(GV);
8304 
8305       // Step 1: Set ISR calling convention.
8306       F->setCallingConv(llvm::CallingConv::M68k_INTR);
8307 
8308       // Step 2: Add attributes goodness.
8309       F->addFnAttr(llvm::Attribute::NoInline);
8310 
8311       // Step 3: Emit ISR vector alias.
8312       unsigned Num = attr->getNumber() / 2;
8313       llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
8314                                 "__isr_" + Twine(Num), F);
8315     }
8316   }
8317 }
8318 
8319 //===----------------------------------------------------------------------===//
8320 // AVR ABI Implementation. Documented at
8321 // https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
8322 // https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
8323 //===----------------------------------------------------------------------===//
8324 
8325 namespace {
8326 class AVRABIInfo : public DefaultABIInfo {
8327 private:
8328   // The total amount of registers can be used to pass parameters. It is 18 on
8329   // AVR, or 6 on AVRTiny.
8330   const unsigned ParamRegs;
8331   // The total amount of registers can be used to pass return value. It is 8 on
8332   // AVR, or 4 on AVRTiny.
8333   const unsigned RetRegs;
8334 
8335 public:
8336   AVRABIInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
8337       : DefaultABIInfo(CGT), ParamRegs(NPR), RetRegs(NRR) {}
8338 
8339   ABIArgInfo classifyReturnType(QualType Ty, bool &LargeRet) const {
8340     if (isAggregateTypeForABI(Ty)) {
8341       // On AVR, a return struct with size less than or equals to 8 bytes is
8342       // returned directly via registers R18-R25. On AVRTiny, a return struct
8343       // with size less than or equals to 4 bytes is returned directly via
8344       // registers R22-R25.
8345       if (getContext().getTypeSize(Ty) <= RetRegs * 8)
8346         return ABIArgInfo::getDirect();
8347       // A return struct with larger size is returned via a stack
8348       // slot, along with a pointer to it as the function's implicit argument.
8349       LargeRet = true;
8350       return getNaturalAlignIndirect(Ty);
8351     }
8352     // Otherwise we follow the default way which is compatible.
8353     return DefaultABIInfo::classifyReturnType(Ty);
8354   }
8355 
8356   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegs) const {
8357     unsigned TySize = getContext().getTypeSize(Ty);
8358 
8359     // An int8 type argument always costs two registers like an int16.
8360     if (TySize == 8 && NumRegs >= 2) {
8361       NumRegs -= 2;
8362       return ABIArgInfo::getExtend(Ty);
8363     }
8364 
8365     // If the argument size is an odd number of bytes, round up the size
8366     // to the next even number.
8367     TySize = llvm::alignTo(TySize, 16);
8368 
8369     // Any type including an array/struct type can be passed in rgisters,
8370     // if there are enough registers left.
8371     if (TySize <= NumRegs * 8) {
8372       NumRegs -= TySize / 8;
8373       return ABIArgInfo::getDirect();
8374     }
8375 
8376     // An argument is passed either completely in registers or completely in
8377     // memory. Since there are not enough registers left, current argument
8378     // and all other unprocessed arguments should be passed in memory.
8379     // However we still need to return `ABIArgInfo::getDirect()` other than
8380     // `ABIInfo::getNaturalAlignIndirect(Ty)`, otherwise an extra stack slot
8381     // will be allocated, so the stack frame layout will be incompatible with
8382     // avr-gcc.
8383     NumRegs = 0;
8384     return ABIArgInfo::getDirect();
8385   }
8386 
8387   void computeInfo(CGFunctionInfo &FI) const override {
8388     // Decide the return type.
8389     bool LargeRet = false;
8390     if (!getCXXABI().classifyReturnType(FI))
8391       FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), LargeRet);
8392 
8393     // Decide each argument type. The total number of registers can be used for
8394     // arguments depends on several factors:
8395     // 1. Arguments of varargs functions are passed on the stack. This applies
8396     //    even to the named arguments. So no register can be used.
8397     // 2. Total 18 registers can be used on avr and 6 ones on avrtiny.
8398     // 3. If the return type is a struct with too large size, two registers
8399     //    (out of 18/6) will be cost as an implicit pointer argument.
8400     unsigned NumRegs = ParamRegs;
8401     if (FI.isVariadic())
8402       NumRegs = 0;
8403     else if (LargeRet)
8404       NumRegs -= 2;
8405     for (auto &I : FI.arguments())
8406       I.info = classifyArgumentType(I.type, NumRegs);
8407   }
8408 };
8409 
8410 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
8411 public:
8412   AVRTargetCodeGenInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
8413       : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT, NPR, NRR)) {}
8414 
8415   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8416                                   const VarDecl *D) const override {
8417     // Check if global/static variable is defined in address space
8418     // 1~6 (__flash, __flash1, __flash2, __flash3, __flash4, __flash5)
8419     // but not constant.
8420     if (D) {
8421       LangAS AS = D->getType().getAddressSpace();
8422       if (isTargetAddressSpace(AS) && 1 <= toTargetAddressSpace(AS) &&
8423           toTargetAddressSpace(AS) <= 6 && !D->getType().isConstQualified())
8424         CGM.getDiags().Report(D->getLocation(),
8425                               diag::err_verify_nonconst_addrspace)
8426             << "__flash*";
8427     }
8428     return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
8429   }
8430 
8431   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8432                            CodeGen::CodeGenModule &CGM) const override {
8433     if (GV->isDeclaration())
8434       return;
8435     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
8436     if (!FD) return;
8437     auto *Fn = cast<llvm::Function>(GV);
8438 
8439     if (FD->getAttr<AVRInterruptAttr>())
8440       Fn->addFnAttr("interrupt");
8441 
8442     if (FD->getAttr<AVRSignalAttr>())
8443       Fn->addFnAttr("signal");
8444   }
8445 };
8446 }
8447 
8448 //===----------------------------------------------------------------------===//
8449 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
8450 // Currently subclassed only to implement custom OpenCL C function attribute
8451 // handling.
8452 //===----------------------------------------------------------------------===//
8453 
8454 namespace {
8455 
8456 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
8457 public:
8458   TCETargetCodeGenInfo(CodeGenTypes &CGT)
8459     : DefaultTargetCodeGenInfo(CGT) {}
8460 
8461   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8462                            CodeGen::CodeGenModule &M) const override;
8463 };
8464 
8465 void TCETargetCodeGenInfo::setTargetAttributes(
8466     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8467   if (GV->isDeclaration())
8468     return;
8469   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8470   if (!FD) return;
8471 
8472   llvm::Function *F = cast<llvm::Function>(GV);
8473 
8474   if (M.getLangOpts().OpenCL) {
8475     if (FD->hasAttr<OpenCLKernelAttr>()) {
8476       // OpenCL C Kernel functions are not subject to inlining
8477       F->addFnAttr(llvm::Attribute::NoInline);
8478       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
8479       if (Attr) {
8480         // Convert the reqd_work_group_size() attributes to metadata.
8481         llvm::LLVMContext &Context = F->getContext();
8482         llvm::NamedMDNode *OpenCLMetadata =
8483             M.getModule().getOrInsertNamedMetadata(
8484                 "opencl.kernel_wg_size_info");
8485 
8486         SmallVector<llvm::Metadata *, 5> Operands;
8487         Operands.push_back(llvm::ConstantAsMetadata::get(F));
8488 
8489         Operands.push_back(
8490             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8491                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8492         Operands.push_back(
8493             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8494                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8495         Operands.push_back(
8496             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8497                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8498 
8499         // Add a boolean constant operand for "required" (true) or "hint"
8500         // (false) for implementing the work_group_size_hint attr later.
8501         // Currently always true as the hint is not yet implemented.
8502         Operands.push_back(
8503             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8504         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8505       }
8506     }
8507   }
8508 }
8509 
8510 }
8511 
8512 //===----------------------------------------------------------------------===//
8513 // Hexagon ABI Implementation
8514 //===----------------------------------------------------------------------===//
8515 
8516 namespace {
8517 
8518 class HexagonABIInfo : public DefaultABIInfo {
8519 public:
8520   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8521 
8522 private:
8523   ABIArgInfo classifyReturnType(QualType RetTy) const;
8524   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8525   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8526 
8527   void computeInfo(CGFunctionInfo &FI) const override;
8528 
8529   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8530                     QualType Ty) const override;
8531   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8532                               QualType Ty) const;
8533   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8534                               QualType Ty) const;
8535   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8536                                    QualType Ty) const;
8537 };
8538 
8539 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8540 public:
8541   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8542       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8543 
8544   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8545     return 29;
8546   }
8547 
8548   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8549                            CodeGen::CodeGenModule &GCM) const override {
8550     if (GV->isDeclaration())
8551       return;
8552     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8553     if (!FD)
8554       return;
8555   }
8556 };
8557 
8558 } // namespace
8559 
8560 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8561   unsigned RegsLeft = 6;
8562   if (!getCXXABI().classifyReturnType(FI))
8563     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8564   for (auto &I : FI.arguments())
8565     I.info = classifyArgumentType(I.type, &RegsLeft);
8566 }
8567 
8568 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8569   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8570                        " through registers");
8571 
8572   if (*RegsLeft == 0)
8573     return false;
8574 
8575   if (Size <= 32) {
8576     (*RegsLeft)--;
8577     return true;
8578   }
8579 
8580   if (2 <= (*RegsLeft & (~1U))) {
8581     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8582     return true;
8583   }
8584 
8585   // Next available register was r5 but candidate was greater than 32-bits so it
8586   // has to go on the stack. However we still consume r5
8587   if (*RegsLeft == 1)
8588     *RegsLeft = 0;
8589 
8590   return false;
8591 }
8592 
8593 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8594                                                 unsigned *RegsLeft) const {
8595   if (!isAggregateTypeForABI(Ty)) {
8596     // Treat an enum type as its underlying type.
8597     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8598       Ty = EnumTy->getDecl()->getIntegerType();
8599 
8600     uint64_t Size = getContext().getTypeSize(Ty);
8601     if (Size <= 64)
8602       HexagonAdjustRegsLeft(Size, RegsLeft);
8603 
8604     if (Size > 64 && Ty->isBitIntType())
8605       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8606 
8607     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8608                                              : ABIArgInfo::getDirect();
8609   }
8610 
8611   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8612     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8613 
8614   // Ignore empty records.
8615   if (isEmptyRecord(getContext(), Ty, true))
8616     return ABIArgInfo::getIgnore();
8617 
8618   uint64_t Size = getContext().getTypeSize(Ty);
8619   unsigned Align = getContext().getTypeAlign(Ty);
8620 
8621   if (Size > 64)
8622     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8623 
8624   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8625     Align = Size <= 32 ? 32 : 64;
8626   if (Size <= Align) {
8627     // Pass in the smallest viable integer type.
8628     if (!llvm::isPowerOf2_64(Size))
8629       Size = llvm::NextPowerOf2(Size);
8630     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8631   }
8632   return DefaultABIInfo::classifyArgumentType(Ty);
8633 }
8634 
8635 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8636   if (RetTy->isVoidType())
8637     return ABIArgInfo::getIgnore();
8638 
8639   const TargetInfo &T = CGT.getTarget();
8640   uint64_t Size = getContext().getTypeSize(RetTy);
8641 
8642   if (RetTy->getAs<VectorType>()) {
8643     // HVX vectors are returned in vector registers or register pairs.
8644     if (T.hasFeature("hvx")) {
8645       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8646       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8647       if (Size == VecSize || Size == 2*VecSize)
8648         return ABIArgInfo::getDirectInReg();
8649     }
8650     // Large vector types should be returned via memory.
8651     if (Size > 64)
8652       return getNaturalAlignIndirect(RetTy);
8653   }
8654 
8655   if (!isAggregateTypeForABI(RetTy)) {
8656     // Treat an enum type as its underlying type.
8657     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8658       RetTy = EnumTy->getDecl()->getIntegerType();
8659 
8660     if (Size > 64 && RetTy->isBitIntType())
8661       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8662 
8663     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8664                                                 : ABIArgInfo::getDirect();
8665   }
8666 
8667   if (isEmptyRecord(getContext(), RetTy, true))
8668     return ABIArgInfo::getIgnore();
8669 
8670   // Aggregates <= 8 bytes are returned in registers, other aggregates
8671   // are returned indirectly.
8672   if (Size <= 64) {
8673     // Return in the smallest viable integer type.
8674     if (!llvm::isPowerOf2_64(Size))
8675       Size = llvm::NextPowerOf2(Size);
8676     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8677   }
8678   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8679 }
8680 
8681 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8682                                             Address VAListAddr,
8683                                             QualType Ty) const {
8684   // Load the overflow area pointer.
8685   Address __overflow_area_pointer_p =
8686       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8687   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8688       __overflow_area_pointer_p, "__overflow_area_pointer");
8689 
8690   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8691   if (Align > 4) {
8692     // Alignment should be a power of 2.
8693     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8694 
8695     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8696     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8697 
8698     // Add offset to the current pointer to access the argument.
8699     __overflow_area_pointer =
8700         CGF.Builder.CreateGEP(CGF.Int8Ty, __overflow_area_pointer, Offset);
8701     llvm::Value *AsInt =
8702         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8703 
8704     // Create a mask which should be "AND"ed
8705     // with (overflow_arg_area + align - 1)
8706     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8707     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8708         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8709         "__overflow_area_pointer.align");
8710   }
8711 
8712   // Get the type of the argument from memory and bitcast
8713   // overflow area pointer to the argument type.
8714   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8715   Address AddrTyped = CGF.Builder.CreateElementBitCast(
8716       Address(__overflow_area_pointer, CGF.Int8Ty,
8717               CharUnits::fromQuantity(Align)),
8718       PTy);
8719 
8720   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8721   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8722 
8723   __overflow_area_pointer = CGF.Builder.CreateGEP(
8724       CGF.Int8Ty, __overflow_area_pointer,
8725       llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8726       "__overflow_area_pointer.next");
8727   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8728 
8729   return AddrTyped;
8730 }
8731 
8732 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8733                                             Address VAListAddr,
8734                                             QualType Ty) const {
8735   // FIXME: Need to handle alignment
8736   llvm::Type *BP = CGF.Int8PtrTy;
8737   CGBuilderTy &Builder = CGF.Builder;
8738   Address VAListAddrAsBPP = Builder.CreateElementBitCast(VAListAddr, BP, "ap");
8739   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8740   // Handle address alignment for type alignment > 32 bits
8741   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8742   if (TyAlign > 4) {
8743     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8744     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8745     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8746     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8747     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8748   }
8749   Address AddrTyped = Builder.CreateElementBitCast(
8750       Address(Addr, CGF.Int8Ty, CharUnits::fromQuantity(TyAlign)),
8751       CGF.ConvertType(Ty));
8752 
8753   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8754   llvm::Value *NextAddr = Builder.CreateGEP(
8755       CGF.Int8Ty, Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8756   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8757 
8758   return AddrTyped;
8759 }
8760 
8761 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8762                                                  Address VAListAddr,
8763                                                  QualType Ty) const {
8764   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8765 
8766   if (ArgSize > 8)
8767     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8768 
8769   // Here we have check if the argument is in register area or
8770   // in overflow area.
8771   // If the saved register area pointer + argsize rounded up to alignment >
8772   // saved register area end pointer, argument is in overflow area.
8773   unsigned RegsLeft = 6;
8774   Ty = CGF.getContext().getCanonicalType(Ty);
8775   (void)classifyArgumentType(Ty, &RegsLeft);
8776 
8777   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8778   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8779   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8780   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8781 
8782   // Get rounded size of the argument.GCC does not allow vararg of
8783   // size < 4 bytes. We follow the same logic here.
8784   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8785   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8786 
8787   // Argument may be in saved register area
8788   CGF.EmitBlock(MaybeRegBlock);
8789 
8790   // Load the current saved register area pointer.
8791   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8792       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8793   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8794       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8795 
8796   // Load the saved register area end pointer.
8797   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8798       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8799   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8800       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8801 
8802   // If the size of argument is > 4 bytes, check if the stack
8803   // location is aligned to 8 bytes
8804   if (ArgAlign > 4) {
8805 
8806     llvm::Value *__current_saved_reg_area_pointer_int =
8807         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8808                                    CGF.Int32Ty);
8809 
8810     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8811         __current_saved_reg_area_pointer_int,
8812         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8813         "align_current_saved_reg_area_pointer");
8814 
8815     __current_saved_reg_area_pointer_int =
8816         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8817                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8818                               "align_current_saved_reg_area_pointer");
8819 
8820     __current_saved_reg_area_pointer =
8821         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8822                                    __current_saved_reg_area_pointer->getType(),
8823                                    "align_current_saved_reg_area_pointer");
8824   }
8825 
8826   llvm::Value *__new_saved_reg_area_pointer =
8827       CGF.Builder.CreateGEP(CGF.Int8Ty, __current_saved_reg_area_pointer,
8828                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8829                             "__new_saved_reg_area_pointer");
8830 
8831   llvm::Value *UsingStack = nullptr;
8832   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8833                                          __saved_reg_area_end_pointer);
8834 
8835   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8836 
8837   // Argument in saved register area
8838   // Implement the block where argument is in register saved area
8839   CGF.EmitBlock(InRegBlock);
8840 
8841   llvm::Type *PTy = CGF.ConvertType(Ty);
8842   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8843       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8844 
8845   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8846                           __current_saved_reg_area_pointer_p);
8847 
8848   CGF.EmitBranch(ContBlock);
8849 
8850   // Argument in overflow area
8851   // Implement the block where the argument is in overflow area.
8852   CGF.EmitBlock(OnStackBlock);
8853 
8854   // Load the overflow area pointer
8855   Address __overflow_area_pointer_p =
8856       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8857   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8858       __overflow_area_pointer_p, "__overflow_area_pointer");
8859 
8860   // Align the overflow area pointer according to the alignment of the argument
8861   if (ArgAlign > 4) {
8862     llvm::Value *__overflow_area_pointer_int =
8863         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8864 
8865     __overflow_area_pointer_int =
8866         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8867                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8868                               "align_overflow_area_pointer");
8869 
8870     __overflow_area_pointer_int =
8871         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8872                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8873                               "align_overflow_area_pointer");
8874 
8875     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8876         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8877         "align_overflow_area_pointer");
8878   }
8879 
8880   // Get the pointer for next argument in overflow area and store it
8881   // to overflow area pointer.
8882   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8883       CGF.Int8Ty, __overflow_area_pointer,
8884       llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8885       "__overflow_area_pointer.next");
8886 
8887   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8888                           __overflow_area_pointer_p);
8889 
8890   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8891                           __current_saved_reg_area_pointer_p);
8892 
8893   // Bitcast the overflow area pointer to the type of argument.
8894   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8895   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8896       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8897 
8898   CGF.EmitBranch(ContBlock);
8899 
8900   // Get the correct pointer to load the variable argument
8901   // Implement the ContBlock
8902   CGF.EmitBlock(ContBlock);
8903 
8904   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
8905   llvm::Type *MemPTy = llvm::PointerType::getUnqual(MemTy);
8906   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
8907   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
8908   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
8909 
8910   return Address(ArgAddr, MemTy, CharUnits::fromQuantity(ArgAlign));
8911 }
8912 
8913 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8914                                   QualType Ty) const {
8915 
8916   if (getTarget().getTriple().isMusl())
8917     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
8918 
8919   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
8920 }
8921 
8922 //===----------------------------------------------------------------------===//
8923 // Lanai ABI Implementation
8924 //===----------------------------------------------------------------------===//
8925 
8926 namespace {
8927 class LanaiABIInfo : public DefaultABIInfo {
8928 public:
8929   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8930 
8931   bool shouldUseInReg(QualType Ty, CCState &State) const;
8932 
8933   void computeInfo(CGFunctionInfo &FI) const override {
8934     CCState State(FI);
8935     // Lanai uses 4 registers to pass arguments unless the function has the
8936     // regparm attribute set.
8937     if (FI.getHasRegParm()) {
8938       State.FreeRegs = FI.getRegParm();
8939     } else {
8940       State.FreeRegs = 4;
8941     }
8942 
8943     if (!getCXXABI().classifyReturnType(FI))
8944       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8945     for (auto &I : FI.arguments())
8946       I.info = classifyArgumentType(I.type, State);
8947   }
8948 
8949   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
8950   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
8951 };
8952 } // end anonymous namespace
8953 
8954 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
8955   unsigned Size = getContext().getTypeSize(Ty);
8956   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
8957 
8958   if (SizeInRegs == 0)
8959     return false;
8960 
8961   if (SizeInRegs > State.FreeRegs) {
8962     State.FreeRegs = 0;
8963     return false;
8964   }
8965 
8966   State.FreeRegs -= SizeInRegs;
8967 
8968   return true;
8969 }
8970 
8971 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
8972                                            CCState &State) const {
8973   if (!ByVal) {
8974     if (State.FreeRegs) {
8975       --State.FreeRegs; // Non-byval indirects just use one pointer.
8976       return getNaturalAlignIndirectInReg(Ty);
8977     }
8978     return getNaturalAlignIndirect(Ty, false);
8979   }
8980 
8981   // Compute the byval alignment.
8982   const unsigned MinABIStackAlignInBytes = 4;
8983   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8984   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8985                                  /*Realign=*/TypeAlign >
8986                                      MinABIStackAlignInBytes);
8987 }
8988 
8989 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
8990                                               CCState &State) const {
8991   // Check with the C++ ABI first.
8992   const RecordType *RT = Ty->getAs<RecordType>();
8993   if (RT) {
8994     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8995     if (RAA == CGCXXABI::RAA_Indirect) {
8996       return getIndirectResult(Ty, /*ByVal=*/false, State);
8997     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
8998       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8999     }
9000   }
9001 
9002   if (isAggregateTypeForABI(Ty)) {
9003     // Structures with flexible arrays are always indirect.
9004     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9005       return getIndirectResult(Ty, /*ByVal=*/true, State);
9006 
9007     // Ignore empty structs/unions.
9008     if (isEmptyRecord(getContext(), Ty, true))
9009       return ABIArgInfo::getIgnore();
9010 
9011     llvm::LLVMContext &LLVMContext = getVMContext();
9012     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
9013     if (SizeInRegs <= State.FreeRegs) {
9014       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9015       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9016       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9017       State.FreeRegs -= SizeInRegs;
9018       return ABIArgInfo::getDirectInReg(Result);
9019     } else {
9020       State.FreeRegs = 0;
9021     }
9022     return getIndirectResult(Ty, true, State);
9023   }
9024 
9025   // Treat an enum type as its underlying type.
9026   if (const auto *EnumTy = Ty->getAs<EnumType>())
9027     Ty = EnumTy->getDecl()->getIntegerType();
9028 
9029   bool InReg = shouldUseInReg(Ty, State);
9030 
9031   // Don't pass >64 bit integers in registers.
9032   if (const auto *EIT = Ty->getAs<BitIntType>())
9033     if (EIT->getNumBits() > 64)
9034       return getIndirectResult(Ty, /*ByVal=*/true, State);
9035 
9036   if (isPromotableIntegerTypeForABI(Ty)) {
9037     if (InReg)
9038       return ABIArgInfo::getDirectInReg();
9039     return ABIArgInfo::getExtend(Ty);
9040   }
9041   if (InReg)
9042     return ABIArgInfo::getDirectInReg();
9043   return ABIArgInfo::getDirect();
9044 }
9045 
9046 namespace {
9047 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
9048 public:
9049   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
9050       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
9051 };
9052 }
9053 
9054 //===----------------------------------------------------------------------===//
9055 // AMDGPU ABI Implementation
9056 //===----------------------------------------------------------------------===//
9057 
9058 namespace {
9059 
9060 class AMDGPUABIInfo final : public DefaultABIInfo {
9061 private:
9062   static const unsigned MaxNumRegsForArgsRet = 16;
9063 
9064   unsigned numRegsForType(QualType Ty) const;
9065 
9066   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
9067   bool isHomogeneousAggregateSmallEnough(const Type *Base,
9068                                          uint64_t Members) const override;
9069 
9070   // Coerce HIP scalar pointer arguments from generic pointers to global ones.
9071   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
9072                                        unsigned ToAS) const {
9073     // Single value types.
9074     auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
9075     if (PtrTy && PtrTy->getAddressSpace() == FromAS)
9076       return llvm::PointerType::getWithSamePointeeType(PtrTy, ToAS);
9077     return Ty;
9078   }
9079 
9080 public:
9081   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
9082     DefaultABIInfo(CGT) {}
9083 
9084   ABIArgInfo classifyReturnType(QualType RetTy) const;
9085   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
9086   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
9087 
9088   void computeInfo(CGFunctionInfo &FI) const override;
9089   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9090                     QualType Ty) const override;
9091 };
9092 
9093 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
9094   return true;
9095 }
9096 
9097 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
9098   const Type *Base, uint64_t Members) const {
9099   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
9100 
9101   // Homogeneous Aggregates may occupy at most 16 registers.
9102   return Members * NumRegs <= MaxNumRegsForArgsRet;
9103 }
9104 
9105 /// Estimate number of registers the type will use when passed in registers.
9106 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
9107   unsigned NumRegs = 0;
9108 
9109   if (const VectorType *VT = Ty->getAs<VectorType>()) {
9110     // Compute from the number of elements. The reported size is based on the
9111     // in-memory size, which includes the padding 4th element for 3-vectors.
9112     QualType EltTy = VT->getElementType();
9113     unsigned EltSize = getContext().getTypeSize(EltTy);
9114 
9115     // 16-bit element vectors should be passed as packed.
9116     if (EltSize == 16)
9117       return (VT->getNumElements() + 1) / 2;
9118 
9119     unsigned EltNumRegs = (EltSize + 31) / 32;
9120     return EltNumRegs * VT->getNumElements();
9121   }
9122 
9123   if (const RecordType *RT = Ty->getAs<RecordType>()) {
9124     const RecordDecl *RD = RT->getDecl();
9125     assert(!RD->hasFlexibleArrayMember());
9126 
9127     for (const FieldDecl *Field : RD->fields()) {
9128       QualType FieldTy = Field->getType();
9129       NumRegs += numRegsForType(FieldTy);
9130     }
9131 
9132     return NumRegs;
9133   }
9134 
9135   return (getContext().getTypeSize(Ty) + 31) / 32;
9136 }
9137 
9138 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
9139   llvm::CallingConv::ID CC = FI.getCallingConvention();
9140 
9141   if (!getCXXABI().classifyReturnType(FI))
9142     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9143 
9144   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
9145   for (auto &Arg : FI.arguments()) {
9146     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
9147       Arg.info = classifyKernelArgumentType(Arg.type);
9148     } else {
9149       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
9150     }
9151   }
9152 }
9153 
9154 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9155                                  QualType Ty) const {
9156   llvm_unreachable("AMDGPU does not support varargs");
9157 }
9158 
9159 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
9160   if (isAggregateTypeForABI(RetTy)) {
9161     // Records with non-trivial destructors/copy-constructors should not be
9162     // returned by value.
9163     if (!getRecordArgABI(RetTy, getCXXABI())) {
9164       // Ignore empty structs/unions.
9165       if (isEmptyRecord(getContext(), RetTy, true))
9166         return ABIArgInfo::getIgnore();
9167 
9168       // Lower single-element structs to just return a regular value.
9169       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
9170         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9171 
9172       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
9173         const RecordDecl *RD = RT->getDecl();
9174         if (RD->hasFlexibleArrayMember())
9175           return DefaultABIInfo::classifyReturnType(RetTy);
9176       }
9177 
9178       // Pack aggregates <= 4 bytes into single VGPR or pair.
9179       uint64_t Size = getContext().getTypeSize(RetTy);
9180       if (Size <= 16)
9181         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9182 
9183       if (Size <= 32)
9184         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9185 
9186       if (Size <= 64) {
9187         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9188         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9189       }
9190 
9191       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
9192         return ABIArgInfo::getDirect();
9193     }
9194   }
9195 
9196   // Otherwise just do the default thing.
9197   return DefaultABIInfo::classifyReturnType(RetTy);
9198 }
9199 
9200 /// For kernels all parameters are really passed in a special buffer. It doesn't
9201 /// make sense to pass anything byval, so everything must be direct.
9202 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
9203   Ty = useFirstFieldIfTransparentUnion(Ty);
9204 
9205   // TODO: Can we omit empty structs?
9206 
9207   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9208     Ty = QualType(SeltTy, 0);
9209 
9210   llvm::Type *OrigLTy = CGT.ConvertType(Ty);
9211   llvm::Type *LTy = OrigLTy;
9212   if (getContext().getLangOpts().HIP) {
9213     LTy = coerceKernelArgumentType(
9214         OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
9215         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
9216   }
9217 
9218   // FIXME: Should also use this for OpenCL, but it requires addressing the
9219   // problem of kernels being called.
9220   //
9221   // FIXME: This doesn't apply the optimization of coercing pointers in structs
9222   // to global address space when using byref. This would require implementing a
9223   // new kind of coercion of the in-memory type when for indirect arguments.
9224   if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
9225       isAggregateTypeForABI(Ty)) {
9226     return ABIArgInfo::getIndirectAliased(
9227         getContext().getTypeAlignInChars(Ty),
9228         getContext().getTargetAddressSpace(LangAS::opencl_constant),
9229         false /*Realign*/, nullptr /*Padding*/);
9230   }
9231 
9232   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
9233   // individual elements, which confuses the Clover OpenCL backend; therefore we
9234   // have to set it to false here. Other args of getDirect() are just defaults.
9235   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
9236 }
9237 
9238 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
9239                                                unsigned &NumRegsLeft) const {
9240   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
9241 
9242   Ty = useFirstFieldIfTransparentUnion(Ty);
9243 
9244   if (isAggregateTypeForABI(Ty)) {
9245     // Records with non-trivial destructors/copy-constructors should not be
9246     // passed by value.
9247     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
9248       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9249 
9250     // Ignore empty structs/unions.
9251     if (isEmptyRecord(getContext(), Ty, true))
9252       return ABIArgInfo::getIgnore();
9253 
9254     // Lower single-element structs to just pass a regular value. TODO: We
9255     // could do reasonable-size multiple-element structs too, using getExpand(),
9256     // though watch out for things like bitfields.
9257     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9258       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9259 
9260     if (const RecordType *RT = Ty->getAs<RecordType>()) {
9261       const RecordDecl *RD = RT->getDecl();
9262       if (RD->hasFlexibleArrayMember())
9263         return DefaultABIInfo::classifyArgumentType(Ty);
9264     }
9265 
9266     // Pack aggregates <= 8 bytes into single VGPR or pair.
9267     uint64_t Size = getContext().getTypeSize(Ty);
9268     if (Size <= 64) {
9269       unsigned NumRegs = (Size + 31) / 32;
9270       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
9271 
9272       if (Size <= 16)
9273         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9274 
9275       if (Size <= 32)
9276         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9277 
9278       // XXX: Should this be i64 instead, and should the limit increase?
9279       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9280       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9281     }
9282 
9283     if (NumRegsLeft > 0) {
9284       unsigned NumRegs = numRegsForType(Ty);
9285       if (NumRegsLeft >= NumRegs) {
9286         NumRegsLeft -= NumRegs;
9287         return ABIArgInfo::getDirect();
9288       }
9289     }
9290   }
9291 
9292   // Otherwise just do the default thing.
9293   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
9294   if (!ArgInfo.isIndirect()) {
9295     unsigned NumRegs = numRegsForType(Ty);
9296     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
9297   }
9298 
9299   return ArgInfo;
9300 }
9301 
9302 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
9303 public:
9304   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
9305       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
9306 
9307   void setFunctionDeclAttributes(const FunctionDecl *FD, llvm::Function *F,
9308                                  CodeGenModule &CGM) const;
9309 
9310   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
9311                            CodeGen::CodeGenModule &M) const override;
9312   unsigned getOpenCLKernelCallingConv() const override;
9313 
9314   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
9315       llvm::PointerType *T, QualType QT) const override;
9316 
9317   LangAS getASTAllocaAddressSpace() const override {
9318     return getLangASFromTargetAS(
9319         getABIInfo().getDataLayout().getAllocaAddrSpace());
9320   }
9321   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
9322                                   const VarDecl *D) const override;
9323   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
9324                                          SyncScope Scope,
9325                                          llvm::AtomicOrdering Ordering,
9326                                          llvm::LLVMContext &Ctx) const override;
9327   llvm::Function *
9328   createEnqueuedBlockKernel(CodeGenFunction &CGF,
9329                             llvm::Function *BlockInvokeFunc,
9330                             llvm::Type *BlockTy) const override;
9331   bool shouldEmitStaticExternCAliases() const override;
9332   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
9333 };
9334 }
9335 
9336 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
9337                                               llvm::GlobalValue *GV) {
9338   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
9339     return false;
9340 
9341   return D->hasAttr<OpenCLKernelAttr>() ||
9342          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
9343          (isa<VarDecl>(D) &&
9344           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
9345            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
9346            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
9347 }
9348 
9349 void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
9350     const FunctionDecl *FD, llvm::Function *F, CodeGenModule &M) const {
9351   const auto *ReqdWGS =
9352       M.getLangOpts().OpenCL ? FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
9353   const bool IsOpenCLKernel =
9354       M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>();
9355   const bool IsHIPKernel = M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>();
9356 
9357   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
9358   if (ReqdWGS || FlatWGS) {
9359     unsigned Min = 0;
9360     unsigned Max = 0;
9361     if (FlatWGS) {
9362       Min = FlatWGS->getMin()
9363                 ->EvaluateKnownConstInt(M.getContext())
9364                 .getExtValue();
9365       Max = FlatWGS->getMax()
9366                 ->EvaluateKnownConstInt(M.getContext())
9367                 .getExtValue();
9368     }
9369     if (ReqdWGS && Min == 0 && Max == 0)
9370       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
9371 
9372     if (Min != 0) {
9373       assert(Min <= Max && "Min must be less than or equal Max");
9374 
9375       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
9376       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9377     } else
9378       assert(Max == 0 && "Max must be zero");
9379   } else if (IsOpenCLKernel || IsHIPKernel) {
9380     // By default, restrict the maximum size to a value specified by
9381     // --gpu-max-threads-per-block=n or its default value for HIP.
9382     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
9383     const unsigned DefaultMaxWorkGroupSize =
9384         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
9385                        : M.getLangOpts().GPUMaxThreadsPerBlock;
9386     std::string AttrVal =
9387         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
9388     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9389   }
9390 
9391   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
9392     unsigned Min =
9393         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
9394     unsigned Max = Attr->getMax() ? Attr->getMax()
9395                                         ->EvaluateKnownConstInt(M.getContext())
9396                                         .getExtValue()
9397                                   : 0;
9398 
9399     if (Min != 0) {
9400       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
9401 
9402       std::string AttrVal = llvm::utostr(Min);
9403       if (Max != 0)
9404         AttrVal = AttrVal + "," + llvm::utostr(Max);
9405       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
9406     } else
9407       assert(Max == 0 && "Max must be zero");
9408   }
9409 
9410   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
9411     unsigned NumSGPR = Attr->getNumSGPR();
9412 
9413     if (NumSGPR != 0)
9414       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
9415   }
9416 
9417   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
9418     uint32_t NumVGPR = Attr->getNumVGPR();
9419 
9420     if (NumVGPR != 0)
9421       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
9422   }
9423 }
9424 
9425 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
9426     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
9427   if (requiresAMDGPUProtectedVisibility(D, GV)) {
9428     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
9429     GV->setDSOLocal(true);
9430   }
9431 
9432   if (GV->isDeclaration())
9433     return;
9434 
9435   llvm::Function *F = dyn_cast<llvm::Function>(GV);
9436   if (!F)
9437     return;
9438 
9439   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
9440   if (FD)
9441     setFunctionDeclAttributes(FD, F, M);
9442 
9443   const bool IsHIPKernel =
9444       M.getLangOpts().HIP && FD && FD->hasAttr<CUDAGlobalAttr>();
9445 
9446   if (IsHIPKernel)
9447     F->addFnAttr("uniform-work-group-size", "true");
9448 
9449   if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics())
9450     F->addFnAttr("amdgpu-unsafe-fp-atomics", "true");
9451 
9452   if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
9453     F->addFnAttr("amdgpu-ieee", "false");
9454 }
9455 
9456 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9457   return llvm::CallingConv::AMDGPU_KERNEL;
9458 }
9459 
9460 // Currently LLVM assumes null pointers always have value 0,
9461 // which results in incorrectly transformed IR. Therefore, instead of
9462 // emitting null pointers in private and local address spaces, a null
9463 // pointer in generic address space is emitted which is casted to a
9464 // pointer in local or private address space.
9465 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
9466     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
9467     QualType QT) const {
9468   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
9469     return llvm::ConstantPointerNull::get(PT);
9470 
9471   auto &Ctx = CGM.getContext();
9472   auto NPT = llvm::PointerType::getWithSamePointeeType(
9473       PT, Ctx.getTargetAddressSpace(LangAS::opencl_generic));
9474   return llvm::ConstantExpr::getAddrSpaceCast(
9475       llvm::ConstantPointerNull::get(NPT), PT);
9476 }
9477 
9478 LangAS
9479 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
9480                                                   const VarDecl *D) const {
9481   assert(!CGM.getLangOpts().OpenCL &&
9482          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
9483          "Address space agnostic languages only");
9484   LangAS DefaultGlobalAS = getLangASFromTargetAS(
9485       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
9486   if (!D)
9487     return DefaultGlobalAS;
9488 
9489   LangAS AddrSpace = D->getType().getAddressSpace();
9490   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9491   if (AddrSpace != LangAS::Default)
9492     return AddrSpace;
9493 
9494   // Only promote to address space 4 if VarDecl has constant initialization.
9495   if (CGM.isTypeConstant(D->getType(), false) &&
9496       D->hasConstantInitialization()) {
9497     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9498       return *ConstAS;
9499   }
9500   return DefaultGlobalAS;
9501 }
9502 
9503 llvm::SyncScope::ID
9504 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9505                                             SyncScope Scope,
9506                                             llvm::AtomicOrdering Ordering,
9507                                             llvm::LLVMContext &Ctx) const {
9508   std::string Name;
9509   switch (Scope) {
9510   case SyncScope::HIPSingleThread:
9511     Name = "singlethread";
9512     break;
9513   case SyncScope::HIPWavefront:
9514   case SyncScope::OpenCLSubGroup:
9515     Name = "wavefront";
9516     break;
9517   case SyncScope::HIPWorkgroup:
9518   case SyncScope::OpenCLWorkGroup:
9519     Name = "workgroup";
9520     break;
9521   case SyncScope::HIPAgent:
9522   case SyncScope::OpenCLDevice:
9523     Name = "agent";
9524     break;
9525   case SyncScope::HIPSystem:
9526   case SyncScope::OpenCLAllSVMDevices:
9527     Name = "";
9528     break;
9529   }
9530 
9531   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9532     if (!Name.empty())
9533       Name = Twine(Twine(Name) + Twine("-")).str();
9534 
9535     Name = Twine(Twine(Name) + Twine("one-as")).str();
9536   }
9537 
9538   return Ctx.getOrInsertSyncScopeID(Name);
9539 }
9540 
9541 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9542   return false;
9543 }
9544 
9545 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9546     const FunctionType *&FT) const {
9547   FT = getABIInfo().getContext().adjustFunctionType(
9548       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9549 }
9550 
9551 //===----------------------------------------------------------------------===//
9552 // SPARC v8 ABI Implementation.
9553 // Based on the SPARC Compliance Definition version 2.4.1.
9554 //
9555 // Ensures that complex values are passed in registers.
9556 //
9557 namespace {
9558 class SparcV8ABIInfo : public DefaultABIInfo {
9559 public:
9560   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9561 
9562 private:
9563   ABIArgInfo classifyReturnType(QualType RetTy) const;
9564   void computeInfo(CGFunctionInfo &FI) const override;
9565 };
9566 } // end anonymous namespace
9567 
9568 
9569 ABIArgInfo
9570 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9571   if (Ty->isAnyComplexType()) {
9572     return ABIArgInfo::getDirect();
9573   }
9574   else {
9575     return DefaultABIInfo::classifyReturnType(Ty);
9576   }
9577 }
9578 
9579 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9580 
9581   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9582   for (auto &Arg : FI.arguments())
9583     Arg.info = classifyArgumentType(Arg.type);
9584 }
9585 
9586 namespace {
9587 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9588 public:
9589   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9590       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9591 
9592   llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9593                                    llvm::Value *Address) const override {
9594     int Offset;
9595     if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
9596       Offset = 12;
9597     else
9598       Offset = 8;
9599     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9600                                  llvm::ConstantInt::get(CGF.Int32Ty, Offset));
9601   }
9602 
9603   llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9604                                    llvm::Value *Address) const override {
9605     int Offset;
9606     if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
9607       Offset = -12;
9608     else
9609       Offset = -8;
9610     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9611                                  llvm::ConstantInt::get(CGF.Int32Ty, Offset));
9612   }
9613 };
9614 } // end anonymous namespace
9615 
9616 //===----------------------------------------------------------------------===//
9617 // SPARC v9 ABI Implementation.
9618 // Based on the SPARC Compliance Definition version 2.4.1.
9619 //
9620 // Function arguments a mapped to a nominal "parameter array" and promoted to
9621 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9622 // the array, structs larger than 16 bytes are passed indirectly.
9623 //
9624 // One case requires special care:
9625 //
9626 //   struct mixed {
9627 //     int i;
9628 //     float f;
9629 //   };
9630 //
9631 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9632 // parameter array, but the int is passed in an integer register, and the float
9633 // is passed in a floating point register. This is represented as two arguments
9634 // with the LLVM IR inreg attribute:
9635 //
9636 //   declare void f(i32 inreg %i, float inreg %f)
9637 //
9638 // The code generator will only allocate 4 bytes from the parameter array for
9639 // the inreg arguments. All other arguments are allocated a multiple of 8
9640 // bytes.
9641 //
9642 namespace {
9643 class SparcV9ABIInfo : public ABIInfo {
9644 public:
9645   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9646 
9647 private:
9648   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9649   void computeInfo(CGFunctionInfo &FI) const override;
9650   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9651                     QualType Ty) const override;
9652 
9653   // Coercion type builder for structs passed in registers. The coercion type
9654   // serves two purposes:
9655   //
9656   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9657   //    in registers.
9658   // 2. Expose aligned floating point elements as first-level elements, so the
9659   //    code generator knows to pass them in floating point registers.
9660   //
9661   // We also compute the InReg flag which indicates that the struct contains
9662   // aligned 32-bit floats.
9663   //
9664   struct CoerceBuilder {
9665     llvm::LLVMContext &Context;
9666     const llvm::DataLayout &DL;
9667     SmallVector<llvm::Type*, 8> Elems;
9668     uint64_t Size;
9669     bool InReg;
9670 
9671     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9672       : Context(c), DL(dl), Size(0), InReg(false) {}
9673 
9674     // Pad Elems with integers until Size is ToSize.
9675     void pad(uint64_t ToSize) {
9676       assert(ToSize >= Size && "Cannot remove elements");
9677       if (ToSize == Size)
9678         return;
9679 
9680       // Finish the current 64-bit word.
9681       uint64_t Aligned = llvm::alignTo(Size, 64);
9682       if (Aligned > Size && Aligned <= ToSize) {
9683         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9684         Size = Aligned;
9685       }
9686 
9687       // Add whole 64-bit words.
9688       while (Size + 64 <= ToSize) {
9689         Elems.push_back(llvm::Type::getInt64Ty(Context));
9690         Size += 64;
9691       }
9692 
9693       // Final in-word padding.
9694       if (Size < ToSize) {
9695         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9696         Size = ToSize;
9697       }
9698     }
9699 
9700     // Add a floating point element at Offset.
9701     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9702       // Unaligned floats are treated as integers.
9703       if (Offset % Bits)
9704         return;
9705       // The InReg flag is only required if there are any floats < 64 bits.
9706       if (Bits < 64)
9707         InReg = true;
9708       pad(Offset);
9709       Elems.push_back(Ty);
9710       Size = Offset + Bits;
9711     }
9712 
9713     // Add a struct type to the coercion type, starting at Offset (in bits).
9714     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9715       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9716       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9717         llvm::Type *ElemTy = StrTy->getElementType(i);
9718         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9719         switch (ElemTy->getTypeID()) {
9720         case llvm::Type::StructTyID:
9721           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9722           break;
9723         case llvm::Type::FloatTyID:
9724           addFloat(ElemOffset, ElemTy, 32);
9725           break;
9726         case llvm::Type::DoubleTyID:
9727           addFloat(ElemOffset, ElemTy, 64);
9728           break;
9729         case llvm::Type::FP128TyID:
9730           addFloat(ElemOffset, ElemTy, 128);
9731           break;
9732         case llvm::Type::PointerTyID:
9733           if (ElemOffset % 64 == 0) {
9734             pad(ElemOffset);
9735             Elems.push_back(ElemTy);
9736             Size += 64;
9737           }
9738           break;
9739         default:
9740           break;
9741         }
9742       }
9743     }
9744 
9745     // Check if Ty is a usable substitute for the coercion type.
9746     bool isUsableType(llvm::StructType *Ty) const {
9747       return llvm::makeArrayRef(Elems) == Ty->elements();
9748     }
9749 
9750     // Get the coercion type as a literal struct type.
9751     llvm::Type *getType() const {
9752       if (Elems.size() == 1)
9753         return Elems.front();
9754       else
9755         return llvm::StructType::get(Context, Elems);
9756     }
9757   };
9758 };
9759 } // end anonymous namespace
9760 
9761 ABIArgInfo
9762 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9763   if (Ty->isVoidType())
9764     return ABIArgInfo::getIgnore();
9765 
9766   uint64_t Size = getContext().getTypeSize(Ty);
9767 
9768   // Anything too big to fit in registers is passed with an explicit indirect
9769   // pointer / sret pointer.
9770   if (Size > SizeLimit)
9771     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9772 
9773   // Treat an enum type as its underlying type.
9774   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9775     Ty = EnumTy->getDecl()->getIntegerType();
9776 
9777   // Integer types smaller than a register are extended.
9778   if (Size < 64 && Ty->isIntegerType())
9779     return ABIArgInfo::getExtend(Ty);
9780 
9781   if (const auto *EIT = Ty->getAs<BitIntType>())
9782     if (EIT->getNumBits() < 64)
9783       return ABIArgInfo::getExtend(Ty);
9784 
9785   // Other non-aggregates go in registers.
9786   if (!isAggregateTypeForABI(Ty))
9787     return ABIArgInfo::getDirect();
9788 
9789   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9790   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9791   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9792     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9793 
9794   // This is a small aggregate type that should be passed in registers.
9795   // Build a coercion type from the LLVM struct type.
9796   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9797   if (!StrTy)
9798     return ABIArgInfo::getDirect();
9799 
9800   CoerceBuilder CB(getVMContext(), getDataLayout());
9801   CB.addStruct(0, StrTy);
9802   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9803 
9804   // Try to use the original type for coercion.
9805   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9806 
9807   if (CB.InReg)
9808     return ABIArgInfo::getDirectInReg(CoerceTy);
9809   else
9810     return ABIArgInfo::getDirect(CoerceTy);
9811 }
9812 
9813 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9814                                   QualType Ty) const {
9815   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9816   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9817   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9818     AI.setCoerceToType(ArgTy);
9819 
9820   CharUnits SlotSize = CharUnits::fromQuantity(8);
9821 
9822   CGBuilderTy &Builder = CGF.Builder;
9823   Address Addr = Address(Builder.CreateLoad(VAListAddr, "ap.cur"),
9824                          getVAListElementType(CGF), SlotSize);
9825   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9826 
9827   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9828 
9829   Address ArgAddr = Address::invalid();
9830   CharUnits Stride;
9831   switch (AI.getKind()) {
9832   case ABIArgInfo::Expand:
9833   case ABIArgInfo::CoerceAndExpand:
9834   case ABIArgInfo::InAlloca:
9835     llvm_unreachable("Unsupported ABI kind for va_arg");
9836 
9837   case ABIArgInfo::Extend: {
9838     Stride = SlotSize;
9839     CharUnits Offset = SlotSize - TypeInfo.Width;
9840     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9841     break;
9842   }
9843 
9844   case ABIArgInfo::Direct: {
9845     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9846     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9847     ArgAddr = Addr;
9848     break;
9849   }
9850 
9851   case ABIArgInfo::Indirect:
9852   case ABIArgInfo::IndirectAliased:
9853     Stride = SlotSize;
9854     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9855     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), ArgTy,
9856                       TypeInfo.Align);
9857     break;
9858 
9859   case ABIArgInfo::Ignore:
9860     return Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeInfo.Align);
9861   }
9862 
9863   // Update VAList.
9864   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9865   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9866 
9867   return Builder.CreateElementBitCast(ArgAddr, ArgTy, "arg.addr");
9868 }
9869 
9870 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9871   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9872   for (auto &I : FI.arguments())
9873     I.info = classifyType(I.type, 16 * 8);
9874 }
9875 
9876 namespace {
9877 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9878 public:
9879   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9880       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9881 
9882   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9883     return 14;
9884   }
9885 
9886   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9887                                llvm::Value *Address) const override;
9888 
9889   llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9890                                    llvm::Value *Address) const override {
9891     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9892                                  llvm::ConstantInt::get(CGF.Int32Ty, 8));
9893   }
9894 
9895   llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9896                                    llvm::Value *Address) const override {
9897     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9898                                  llvm::ConstantInt::get(CGF.Int32Ty, -8));
9899   }
9900 };
9901 } // end anonymous namespace
9902 
9903 bool
9904 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9905                                                 llvm::Value *Address) const {
9906   // This is calculated from the LLVM and GCC tables and verified
9907   // against gcc output.  AFAIK all ABIs use the same encoding.
9908 
9909   CodeGen::CGBuilderTy &Builder = CGF.Builder;
9910 
9911   llvm::IntegerType *i8 = CGF.Int8Ty;
9912   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
9913   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
9914 
9915   // 0-31: the 8-byte general-purpose registers
9916   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
9917 
9918   // 32-63: f0-31, the 4-byte floating-point registers
9919   AssignToArrayRange(Builder, Address, Four8, 32, 63);
9920 
9921   //   Y   = 64
9922   //   PSR = 65
9923   //   WIM = 66
9924   //   TBR = 67
9925   //   PC  = 68
9926   //   NPC = 69
9927   //   FSR = 70
9928   //   CSR = 71
9929   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
9930 
9931   // 72-87: d0-15, the 8-byte floating-point registers
9932   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
9933 
9934   return false;
9935 }
9936 
9937 // ARC ABI implementation.
9938 namespace {
9939 
9940 class ARCABIInfo : public DefaultABIInfo {
9941 public:
9942   using DefaultABIInfo::DefaultABIInfo;
9943 
9944 private:
9945   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9946                     QualType Ty) const override;
9947 
9948   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
9949     if (!State.FreeRegs)
9950       return;
9951     if (Info.isIndirect() && Info.getInReg())
9952       State.FreeRegs--;
9953     else if (Info.isDirect() && Info.getInReg()) {
9954       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
9955       if (sz < State.FreeRegs)
9956         State.FreeRegs -= sz;
9957       else
9958         State.FreeRegs = 0;
9959     }
9960   }
9961 
9962   void computeInfo(CGFunctionInfo &FI) const override {
9963     CCState State(FI);
9964     // ARC uses 8 registers to pass arguments.
9965     State.FreeRegs = 8;
9966 
9967     if (!getCXXABI().classifyReturnType(FI))
9968       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9969     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
9970     for (auto &I : FI.arguments()) {
9971       I.info = classifyArgumentType(I.type, State.FreeRegs);
9972       updateState(I.info, I.type, State);
9973     }
9974   }
9975 
9976   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
9977   ABIArgInfo getIndirectByValue(QualType Ty) const;
9978   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
9979   ABIArgInfo classifyReturnType(QualType RetTy) const;
9980 };
9981 
9982 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
9983 public:
9984   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
9985       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
9986 };
9987 
9988 
9989 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
9990   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
9991                        getNaturalAlignIndirect(Ty, false);
9992 }
9993 
9994 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
9995   // Compute the byval alignment.
9996   const unsigned MinABIStackAlignInBytes = 4;
9997   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9998   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9999                                  TypeAlign > MinABIStackAlignInBytes);
10000 }
10001 
10002 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10003                               QualType Ty) const {
10004   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
10005                           getContext().getTypeInfoInChars(Ty),
10006                           CharUnits::fromQuantity(4), true);
10007 }
10008 
10009 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
10010                                             uint8_t FreeRegs) const {
10011   // Handle the generic C++ ABI.
10012   const RecordType *RT = Ty->getAs<RecordType>();
10013   if (RT) {
10014     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
10015     if (RAA == CGCXXABI::RAA_Indirect)
10016       return getIndirectByRef(Ty, FreeRegs > 0);
10017 
10018     if (RAA == CGCXXABI::RAA_DirectInMemory)
10019       return getIndirectByValue(Ty);
10020   }
10021 
10022   // Treat an enum type as its underlying type.
10023   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10024     Ty = EnumTy->getDecl()->getIntegerType();
10025 
10026   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
10027 
10028   if (isAggregateTypeForABI(Ty)) {
10029     // Structures with flexible arrays are always indirect.
10030     if (RT && RT->getDecl()->hasFlexibleArrayMember())
10031       return getIndirectByValue(Ty);
10032 
10033     // Ignore empty structs/unions.
10034     if (isEmptyRecord(getContext(), Ty, true))
10035       return ABIArgInfo::getIgnore();
10036 
10037     llvm::LLVMContext &LLVMContext = getVMContext();
10038 
10039     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
10040     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
10041     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
10042 
10043     return FreeRegs >= SizeInRegs ?
10044         ABIArgInfo::getDirectInReg(Result) :
10045         ABIArgInfo::getDirect(Result, 0, nullptr, false);
10046   }
10047 
10048   if (const auto *EIT = Ty->getAs<BitIntType>())
10049     if (EIT->getNumBits() > 64)
10050       return getIndirectByValue(Ty);
10051 
10052   return isPromotableIntegerTypeForABI(Ty)
10053              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
10054                                        : ABIArgInfo::getExtend(Ty))
10055              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
10056                                        : ABIArgInfo::getDirect());
10057 }
10058 
10059 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
10060   if (RetTy->isAnyComplexType())
10061     return ABIArgInfo::getDirectInReg();
10062 
10063   // Arguments of size > 4 registers are indirect.
10064   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
10065   if (RetSize > 4)
10066     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
10067 
10068   return DefaultABIInfo::classifyReturnType(RetTy);
10069 }
10070 
10071 } // End anonymous namespace.
10072 
10073 //===----------------------------------------------------------------------===//
10074 // XCore ABI Implementation
10075 //===----------------------------------------------------------------------===//
10076 
10077 namespace {
10078 
10079 /// A SmallStringEnc instance is used to build up the TypeString by passing
10080 /// it by reference between functions that append to it.
10081 typedef llvm::SmallString<128> SmallStringEnc;
10082 
10083 /// TypeStringCache caches the meta encodings of Types.
10084 ///
10085 /// The reason for caching TypeStrings is two fold:
10086 ///   1. To cache a type's encoding for later uses;
10087 ///   2. As a means to break recursive member type inclusion.
10088 ///
10089 /// A cache Entry can have a Status of:
10090 ///   NonRecursive:   The type encoding is not recursive;
10091 ///   Recursive:      The type encoding is recursive;
10092 ///   Incomplete:     An incomplete TypeString;
10093 ///   IncompleteUsed: An incomplete TypeString that has been used in a
10094 ///                   Recursive type encoding.
10095 ///
10096 /// A NonRecursive entry will have all of its sub-members expanded as fully
10097 /// as possible. Whilst it may contain types which are recursive, the type
10098 /// itself is not recursive and thus its encoding may be safely used whenever
10099 /// the type is encountered.
10100 ///
10101 /// A Recursive entry will have all of its sub-members expanded as fully as
10102 /// possible. The type itself is recursive and it may contain other types which
10103 /// are recursive. The Recursive encoding must not be used during the expansion
10104 /// of a recursive type's recursive branch. For simplicity the code uses
10105 /// IncompleteCount to reject all usage of Recursive encodings for member types.
10106 ///
10107 /// An Incomplete entry is always a RecordType and only encodes its
10108 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
10109 /// are placed into the cache during type expansion as a means to identify and
10110 /// handle recursive inclusion of types as sub-members. If there is recursion
10111 /// the entry becomes IncompleteUsed.
10112 ///
10113 /// During the expansion of a RecordType's members:
10114 ///
10115 ///   If the cache contains a NonRecursive encoding for the member type, the
10116 ///   cached encoding is used;
10117 ///
10118 ///   If the cache contains a Recursive encoding for the member type, the
10119 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
10120 ///
10121 ///   If the member is a RecordType, an Incomplete encoding is placed into the
10122 ///   cache to break potential recursive inclusion of itself as a sub-member;
10123 ///
10124 ///   Once a member RecordType has been expanded, its temporary incomplete
10125 ///   entry is removed from the cache. If a Recursive encoding was swapped out
10126 ///   it is swapped back in;
10127 ///
10128 ///   If an incomplete entry is used to expand a sub-member, the incomplete
10129 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
10130 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
10131 ///
10132 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
10133 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
10134 ///   Else the member is part of a recursive type and thus the recursion has
10135 ///   been exited too soon for the encoding to be correct for the member.
10136 ///
10137 class TypeStringCache {
10138   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
10139   struct Entry {
10140     std::string Str;     // The encoded TypeString for the type.
10141     enum Status State;   // Information about the encoding in 'Str'.
10142     std::string Swapped; // A temporary place holder for a Recursive encoding
10143                          // during the expansion of RecordType's members.
10144   };
10145   std::map<const IdentifierInfo *, struct Entry> Map;
10146   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
10147   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
10148 public:
10149   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
10150   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
10151   bool removeIncomplete(const IdentifierInfo *ID);
10152   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
10153                      bool IsRecursive);
10154   StringRef lookupStr(const IdentifierInfo *ID);
10155 };
10156 
10157 /// TypeString encodings for enum & union fields must be order.
10158 /// FieldEncoding is a helper for this ordering process.
10159 class FieldEncoding {
10160   bool HasName;
10161   std::string Enc;
10162 public:
10163   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
10164   StringRef str() { return Enc; }
10165   bool operator<(const FieldEncoding &rhs) const {
10166     if (HasName != rhs.HasName) return HasName;
10167     return Enc < rhs.Enc;
10168   }
10169 };
10170 
10171 class XCoreABIInfo : public DefaultABIInfo {
10172 public:
10173   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
10174   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10175                     QualType Ty) const override;
10176 };
10177 
10178 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
10179   mutable TypeStringCache TSC;
10180   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
10181                     const CodeGen::CodeGenModule &M) const;
10182 
10183 public:
10184   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
10185       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
10186   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
10187                           const llvm::MapVector<GlobalDecl, StringRef>
10188                               &MangledDeclNames) const override;
10189 };
10190 
10191 } // End anonymous namespace.
10192 
10193 // TODO: this implementation is likely now redundant with the default
10194 // EmitVAArg.
10195 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10196                                 QualType Ty) const {
10197   CGBuilderTy &Builder = CGF.Builder;
10198 
10199   // Get the VAList.
10200   CharUnits SlotSize = CharUnits::fromQuantity(4);
10201   Address AP = Address(Builder.CreateLoad(VAListAddr),
10202                        getVAListElementType(CGF), SlotSize);
10203 
10204   // Handle the argument.
10205   ABIArgInfo AI = classifyArgumentType(Ty);
10206   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
10207   llvm::Type *ArgTy = CGT.ConvertType(Ty);
10208   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
10209     AI.setCoerceToType(ArgTy);
10210   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
10211 
10212   Address Val = Address::invalid();
10213   CharUnits ArgSize = CharUnits::Zero();
10214   switch (AI.getKind()) {
10215   case ABIArgInfo::Expand:
10216   case ABIArgInfo::CoerceAndExpand:
10217   case ABIArgInfo::InAlloca:
10218     llvm_unreachable("Unsupported ABI kind for va_arg");
10219   case ABIArgInfo::Ignore:
10220     Val = Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeAlign);
10221     ArgSize = CharUnits::Zero();
10222     break;
10223   case ABIArgInfo::Extend:
10224   case ABIArgInfo::Direct:
10225     Val = Builder.CreateElementBitCast(AP, ArgTy);
10226     ArgSize = CharUnits::fromQuantity(
10227         getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
10228     ArgSize = ArgSize.alignTo(SlotSize);
10229     break;
10230   case ABIArgInfo::Indirect:
10231   case ABIArgInfo::IndirectAliased:
10232     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
10233     Val = Address(Builder.CreateLoad(Val), ArgTy, TypeAlign);
10234     ArgSize = SlotSize;
10235     break;
10236   }
10237 
10238   // Increment the VAList.
10239   if (!ArgSize.isZero()) {
10240     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
10241     Builder.CreateStore(APN.getPointer(), VAListAddr);
10242   }
10243 
10244   return Val;
10245 }
10246 
10247 /// During the expansion of a RecordType, an incomplete TypeString is placed
10248 /// into the cache as a means to identify and break recursion.
10249 /// If there is a Recursive encoding in the cache, it is swapped out and will
10250 /// be reinserted by removeIncomplete().
10251 /// All other types of encoding should have been used rather than arriving here.
10252 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
10253                                     std::string StubEnc) {
10254   if (!ID)
10255     return;
10256   Entry &E = Map[ID];
10257   assert( (E.Str.empty() || E.State == Recursive) &&
10258          "Incorrectly use of addIncomplete");
10259   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
10260   E.Swapped.swap(E.Str); // swap out the Recursive
10261   E.Str.swap(StubEnc);
10262   E.State = Incomplete;
10263   ++IncompleteCount;
10264 }
10265 
10266 /// Once the RecordType has been expanded, the temporary incomplete TypeString
10267 /// must be removed from the cache.
10268 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
10269 /// Returns true if the RecordType was defined recursively.
10270 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
10271   if (!ID)
10272     return false;
10273   auto I = Map.find(ID);
10274   assert(I != Map.end() && "Entry not present");
10275   Entry &E = I->second;
10276   assert( (E.State == Incomplete ||
10277            E.State == IncompleteUsed) &&
10278          "Entry must be an incomplete type");
10279   bool IsRecursive = false;
10280   if (E.State == IncompleteUsed) {
10281     // We made use of our Incomplete encoding, thus we are recursive.
10282     IsRecursive = true;
10283     --IncompleteUsedCount;
10284   }
10285   if (E.Swapped.empty())
10286     Map.erase(I);
10287   else {
10288     // Swap the Recursive back.
10289     E.Swapped.swap(E.Str);
10290     E.Swapped.clear();
10291     E.State = Recursive;
10292   }
10293   --IncompleteCount;
10294   return IsRecursive;
10295 }
10296 
10297 /// Add the encoded TypeString to the cache only if it is NonRecursive or
10298 /// Recursive (viz: all sub-members were expanded as fully as possible).
10299 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
10300                                     bool IsRecursive) {
10301   if (!ID || IncompleteUsedCount)
10302     return; // No key or it is is an incomplete sub-type so don't add.
10303   Entry &E = Map[ID];
10304   if (IsRecursive && !E.Str.empty()) {
10305     assert(E.State==Recursive && E.Str.size() == Str.size() &&
10306            "This is not the same Recursive entry");
10307     // The parent container was not recursive after all, so we could have used
10308     // this Recursive sub-member entry after all, but we assumed the worse when
10309     // we started viz: IncompleteCount!=0.
10310     return;
10311   }
10312   assert(E.Str.empty() && "Entry already present");
10313   E.Str = Str.str();
10314   E.State = IsRecursive? Recursive : NonRecursive;
10315 }
10316 
10317 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
10318 /// are recursively expanding a type (IncompleteCount != 0) and the cached
10319 /// encoding is Recursive, return an empty StringRef.
10320 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
10321   if (!ID)
10322     return StringRef();   // We have no key.
10323   auto I = Map.find(ID);
10324   if (I == Map.end())
10325     return StringRef();   // We have no encoding.
10326   Entry &E = I->second;
10327   if (E.State == Recursive && IncompleteCount)
10328     return StringRef();   // We don't use Recursive encodings for member types.
10329 
10330   if (E.State == Incomplete) {
10331     // The incomplete type is being used to break out of recursion.
10332     E.State = IncompleteUsed;
10333     ++IncompleteUsedCount;
10334   }
10335   return E.Str;
10336 }
10337 
10338 /// The XCore ABI includes a type information section that communicates symbol
10339 /// type information to the linker. The linker uses this information to verify
10340 /// safety/correctness of things such as array bound and pointers et al.
10341 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
10342 /// This type information (TypeString) is emitted into meta data for all global
10343 /// symbols: definitions, declarations, functions & variables.
10344 ///
10345 /// The TypeString carries type, qualifier, name, size & value details.
10346 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
10347 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
10348 /// The output is tested by test/CodeGen/xcore-stringtype.c.
10349 ///
10350 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10351                           const CodeGen::CodeGenModule &CGM,
10352                           TypeStringCache &TSC);
10353 
10354 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
10355 void XCoreTargetCodeGenInfo::emitTargetMD(
10356     const Decl *D, llvm::GlobalValue *GV,
10357     const CodeGen::CodeGenModule &CGM) const {
10358   SmallStringEnc Enc;
10359   if (getTypeString(Enc, D, CGM, TSC)) {
10360     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
10361     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
10362                                 llvm::MDString::get(Ctx, Enc.str())};
10363     llvm::NamedMDNode *MD =
10364       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
10365     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
10366   }
10367 }
10368 
10369 void XCoreTargetCodeGenInfo::emitTargetMetadata(
10370     CodeGen::CodeGenModule &CGM,
10371     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
10372   // Warning, new MangledDeclNames may be appended within this loop.
10373   // We rely on MapVector insertions adding new elements to the end
10374   // of the container.
10375   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
10376     auto Val = *(MangledDeclNames.begin() + I);
10377     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
10378     if (GV) {
10379       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
10380       emitTargetMD(D, GV, CGM);
10381     }
10382   }
10383 }
10384 
10385 //===----------------------------------------------------------------------===//
10386 // Base ABI and target codegen info implementation common between SPIR and
10387 // SPIR-V.
10388 //===----------------------------------------------------------------------===//
10389 
10390 namespace {
10391 class CommonSPIRABIInfo : public DefaultABIInfo {
10392 public:
10393   CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
10394 
10395 private:
10396   void setCCs();
10397 };
10398 
10399 class SPIRVABIInfo : public CommonSPIRABIInfo {
10400 public:
10401   SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
10402   void computeInfo(CGFunctionInfo &FI) const override;
10403 
10404 private:
10405   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
10406 };
10407 } // end anonymous namespace
10408 namespace {
10409 class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
10410 public:
10411   CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10412       : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
10413   CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
10414       : TargetCodeGenInfo(std::move(ABIInfo)) {}
10415 
10416   LangAS getASTAllocaAddressSpace() const override {
10417     return getLangASFromTargetAS(
10418         getABIInfo().getDataLayout().getAllocaAddrSpace());
10419   }
10420 
10421   unsigned getOpenCLKernelCallingConv() const override;
10422 };
10423 class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
10424 public:
10425   SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10426       : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
10427   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
10428 };
10429 } // End anonymous namespace.
10430 
10431 void CommonSPIRABIInfo::setCCs() {
10432   assert(getRuntimeCC() == llvm::CallingConv::C);
10433   RuntimeCC = llvm::CallingConv::SPIR_FUNC;
10434 }
10435 
10436 ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
10437   if (getContext().getLangOpts().CUDAIsDevice) {
10438     // Coerce pointer arguments with default address space to CrossWorkGroup
10439     // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
10440     // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
10441     llvm::Type *LTy = CGT.ConvertType(Ty);
10442     auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
10443     auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
10444     auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
10445     if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
10446       LTy = llvm::PointerType::getWithSamePointeeType(PtrTy, GlobalAS);
10447       return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
10448     }
10449   }
10450   return classifyArgumentType(Ty);
10451 }
10452 
10453 void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10454   // The logic is same as in DefaultABIInfo with an exception on the kernel
10455   // arguments handling.
10456   llvm::CallingConv::ID CC = FI.getCallingConvention();
10457 
10458   if (!getCXXABI().classifyReturnType(FI))
10459     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10460 
10461   for (auto &I : FI.arguments()) {
10462     if (CC == llvm::CallingConv::SPIR_KERNEL) {
10463       I.info = classifyKernelArgumentType(I.type);
10464     } else {
10465       I.info = classifyArgumentType(I.type);
10466     }
10467   }
10468 }
10469 
10470 namespace clang {
10471 namespace CodeGen {
10472 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
10473   if (CGM.getTarget().getTriple().isSPIRV())
10474     SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
10475   else
10476     CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
10477 }
10478 }
10479 }
10480 
10481 unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
10482   return llvm::CallingConv::SPIR_KERNEL;
10483 }
10484 
10485 void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
10486     const FunctionType *&FT) const {
10487   // Convert HIP kernels to SPIR-V kernels.
10488   if (getABIInfo().getContext().getLangOpts().HIP) {
10489     FT = getABIInfo().getContext().adjustFunctionType(
10490         FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
10491     return;
10492   }
10493 }
10494 
10495 static bool appendType(SmallStringEnc &Enc, QualType QType,
10496                        const CodeGen::CodeGenModule &CGM,
10497                        TypeStringCache &TSC);
10498 
10499 /// Helper function for appendRecordType().
10500 /// Builds a SmallVector containing the encoded field types in declaration
10501 /// order.
10502 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
10503                              const RecordDecl *RD,
10504                              const CodeGen::CodeGenModule &CGM,
10505                              TypeStringCache &TSC) {
10506   for (const auto *Field : RD->fields()) {
10507     SmallStringEnc Enc;
10508     Enc += "m(";
10509     Enc += Field->getName();
10510     Enc += "){";
10511     if (Field->isBitField()) {
10512       Enc += "b(";
10513       llvm::raw_svector_ostream OS(Enc);
10514       OS << Field->getBitWidthValue(CGM.getContext());
10515       Enc += ':';
10516     }
10517     if (!appendType(Enc, Field->getType(), CGM, TSC))
10518       return false;
10519     if (Field->isBitField())
10520       Enc += ')';
10521     Enc += '}';
10522     FE.emplace_back(!Field->getName().empty(), Enc);
10523   }
10524   return true;
10525 }
10526 
10527 /// Appends structure and union types to Enc and adds encoding to cache.
10528 /// Recursively calls appendType (via extractFieldType) for each field.
10529 /// Union types have their fields ordered according to the ABI.
10530 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
10531                              const CodeGen::CodeGenModule &CGM,
10532                              TypeStringCache &TSC, const IdentifierInfo *ID) {
10533   // Append the cached TypeString if we have one.
10534   StringRef TypeString = TSC.lookupStr(ID);
10535   if (!TypeString.empty()) {
10536     Enc += TypeString;
10537     return true;
10538   }
10539 
10540   // Start to emit an incomplete TypeString.
10541   size_t Start = Enc.size();
10542   Enc += (RT->isUnionType()? 'u' : 's');
10543   Enc += '(';
10544   if (ID)
10545     Enc += ID->getName();
10546   Enc += "){";
10547 
10548   // We collect all encoded fields and order as necessary.
10549   bool IsRecursive = false;
10550   const RecordDecl *RD = RT->getDecl()->getDefinition();
10551   if (RD && !RD->field_empty()) {
10552     // An incomplete TypeString stub is placed in the cache for this RecordType
10553     // so that recursive calls to this RecordType will use it whilst building a
10554     // complete TypeString for this RecordType.
10555     SmallVector<FieldEncoding, 16> FE;
10556     std::string StubEnc(Enc.substr(Start).str());
10557     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
10558     TSC.addIncomplete(ID, std::move(StubEnc));
10559     if (!extractFieldType(FE, RD, CGM, TSC)) {
10560       (void) TSC.removeIncomplete(ID);
10561       return false;
10562     }
10563     IsRecursive = TSC.removeIncomplete(ID);
10564     // The ABI requires unions to be sorted but not structures.
10565     // See FieldEncoding::operator< for sort algorithm.
10566     if (RT->isUnionType())
10567       llvm::sort(FE);
10568     // We can now complete the TypeString.
10569     unsigned E = FE.size();
10570     for (unsigned I = 0; I != E; ++I) {
10571       if (I)
10572         Enc += ',';
10573       Enc += FE[I].str();
10574     }
10575   }
10576   Enc += '}';
10577   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
10578   return true;
10579 }
10580 
10581 /// Appends enum types to Enc and adds the encoding to the cache.
10582 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
10583                            TypeStringCache &TSC,
10584                            const IdentifierInfo *ID) {
10585   // Append the cached TypeString if we have one.
10586   StringRef TypeString = TSC.lookupStr(ID);
10587   if (!TypeString.empty()) {
10588     Enc += TypeString;
10589     return true;
10590   }
10591 
10592   size_t Start = Enc.size();
10593   Enc += "e(";
10594   if (ID)
10595     Enc += ID->getName();
10596   Enc += "){";
10597 
10598   // We collect all encoded enumerations and order them alphanumerically.
10599   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
10600     SmallVector<FieldEncoding, 16> FE;
10601     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
10602          ++I) {
10603       SmallStringEnc EnumEnc;
10604       EnumEnc += "m(";
10605       EnumEnc += I->getName();
10606       EnumEnc += "){";
10607       I->getInitVal().toString(EnumEnc);
10608       EnumEnc += '}';
10609       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
10610     }
10611     llvm::sort(FE);
10612     unsigned E = FE.size();
10613     for (unsigned I = 0; I != E; ++I) {
10614       if (I)
10615         Enc += ',';
10616       Enc += FE[I].str();
10617     }
10618   }
10619   Enc += '}';
10620   TSC.addIfComplete(ID, Enc.substr(Start), false);
10621   return true;
10622 }
10623 
10624 /// Appends type's qualifier to Enc.
10625 /// This is done prior to appending the type's encoding.
10626 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10627   // Qualifiers are emitted in alphabetical order.
10628   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10629   int Lookup = 0;
10630   if (QT.isConstQualified())
10631     Lookup += 1<<0;
10632   if (QT.isRestrictQualified())
10633     Lookup += 1<<1;
10634   if (QT.isVolatileQualified())
10635     Lookup += 1<<2;
10636   Enc += Table[Lookup];
10637 }
10638 
10639 /// Appends built-in types to Enc.
10640 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10641   const char *EncType;
10642   switch (BT->getKind()) {
10643     case BuiltinType::Void:
10644       EncType = "0";
10645       break;
10646     case BuiltinType::Bool:
10647       EncType = "b";
10648       break;
10649     case BuiltinType::Char_U:
10650       EncType = "uc";
10651       break;
10652     case BuiltinType::UChar:
10653       EncType = "uc";
10654       break;
10655     case BuiltinType::SChar:
10656       EncType = "sc";
10657       break;
10658     case BuiltinType::UShort:
10659       EncType = "us";
10660       break;
10661     case BuiltinType::Short:
10662       EncType = "ss";
10663       break;
10664     case BuiltinType::UInt:
10665       EncType = "ui";
10666       break;
10667     case BuiltinType::Int:
10668       EncType = "si";
10669       break;
10670     case BuiltinType::ULong:
10671       EncType = "ul";
10672       break;
10673     case BuiltinType::Long:
10674       EncType = "sl";
10675       break;
10676     case BuiltinType::ULongLong:
10677       EncType = "ull";
10678       break;
10679     case BuiltinType::LongLong:
10680       EncType = "sll";
10681       break;
10682     case BuiltinType::Float:
10683       EncType = "ft";
10684       break;
10685     case BuiltinType::Double:
10686       EncType = "d";
10687       break;
10688     case BuiltinType::LongDouble:
10689       EncType = "ld";
10690       break;
10691     default:
10692       return false;
10693   }
10694   Enc += EncType;
10695   return true;
10696 }
10697 
10698 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10699 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10700                               const CodeGen::CodeGenModule &CGM,
10701                               TypeStringCache &TSC) {
10702   Enc += "p(";
10703   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10704     return false;
10705   Enc += ')';
10706   return true;
10707 }
10708 
10709 /// Appends array encoding to Enc before calling appendType for the element.
10710 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10711                             const ArrayType *AT,
10712                             const CodeGen::CodeGenModule &CGM,
10713                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10714   if (AT->getSizeModifier() != ArrayType::Normal)
10715     return false;
10716   Enc += "a(";
10717   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10718     CAT->getSize().toStringUnsigned(Enc);
10719   else
10720     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10721   Enc += ':';
10722   // The Qualifiers should be attached to the type rather than the array.
10723   appendQualifier(Enc, QT);
10724   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10725     return false;
10726   Enc += ')';
10727   return true;
10728 }
10729 
10730 /// Appends a function encoding to Enc, calling appendType for the return type
10731 /// and the arguments.
10732 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10733                              const CodeGen::CodeGenModule &CGM,
10734                              TypeStringCache &TSC) {
10735   Enc += "f{";
10736   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10737     return false;
10738   Enc += "}(";
10739   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10740     // N.B. we are only interested in the adjusted param types.
10741     auto I = FPT->param_type_begin();
10742     auto E = FPT->param_type_end();
10743     if (I != E) {
10744       do {
10745         if (!appendType(Enc, *I, CGM, TSC))
10746           return false;
10747         ++I;
10748         if (I != E)
10749           Enc += ',';
10750       } while (I != E);
10751       if (FPT->isVariadic())
10752         Enc += ",va";
10753     } else {
10754       if (FPT->isVariadic())
10755         Enc += "va";
10756       else
10757         Enc += '0';
10758     }
10759   }
10760   Enc += ')';
10761   return true;
10762 }
10763 
10764 /// Handles the type's qualifier before dispatching a call to handle specific
10765 /// type encodings.
10766 static bool appendType(SmallStringEnc &Enc, QualType QType,
10767                        const CodeGen::CodeGenModule &CGM,
10768                        TypeStringCache &TSC) {
10769 
10770   QualType QT = QType.getCanonicalType();
10771 
10772   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10773     // The Qualifiers should be attached to the type rather than the array.
10774     // Thus we don't call appendQualifier() here.
10775     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10776 
10777   appendQualifier(Enc, QT);
10778 
10779   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10780     return appendBuiltinType(Enc, BT);
10781 
10782   if (const PointerType *PT = QT->getAs<PointerType>())
10783     return appendPointerType(Enc, PT, CGM, TSC);
10784 
10785   if (const EnumType *ET = QT->getAs<EnumType>())
10786     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10787 
10788   if (const RecordType *RT = QT->getAsStructureType())
10789     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10790 
10791   if (const RecordType *RT = QT->getAsUnionType())
10792     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10793 
10794   if (const FunctionType *FT = QT->getAs<FunctionType>())
10795     return appendFunctionType(Enc, FT, CGM, TSC);
10796 
10797   return false;
10798 }
10799 
10800 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10801                           const CodeGen::CodeGenModule &CGM,
10802                           TypeStringCache &TSC) {
10803   if (!D)
10804     return false;
10805 
10806   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10807     if (FD->getLanguageLinkage() != CLanguageLinkage)
10808       return false;
10809     return appendType(Enc, FD->getType(), CGM, TSC);
10810   }
10811 
10812   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10813     if (VD->getLanguageLinkage() != CLanguageLinkage)
10814       return false;
10815     QualType QT = VD->getType().getCanonicalType();
10816     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10817       // Global ArrayTypes are given a size of '*' if the size is unknown.
10818       // The Qualifiers should be attached to the type rather than the array.
10819       // Thus we don't call appendQualifier() here.
10820       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10821     }
10822     return appendType(Enc, QT, CGM, TSC);
10823   }
10824   return false;
10825 }
10826 
10827 //===----------------------------------------------------------------------===//
10828 // RISCV ABI Implementation
10829 //===----------------------------------------------------------------------===//
10830 
10831 namespace {
10832 class RISCVABIInfo : public DefaultABIInfo {
10833 private:
10834   // Size of the integer ('x') registers in bits.
10835   unsigned XLen;
10836   // Size of the floating point ('f') registers in bits. Note that the target
10837   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10838   // with soft float ABI has FLen==0).
10839   unsigned FLen;
10840   static const int NumArgGPRs = 8;
10841   static const int NumArgFPRs = 8;
10842   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10843                                       llvm::Type *&Field1Ty,
10844                                       CharUnits &Field1Off,
10845                                       llvm::Type *&Field2Ty,
10846                                       CharUnits &Field2Off) const;
10847 
10848 public:
10849   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10850       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10851 
10852   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10853   // non-virtual, but computeInfo is virtual, so we overload it.
10854   void computeInfo(CGFunctionInfo &FI) const override;
10855 
10856   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10857                                   int &ArgFPRsLeft) const;
10858   ABIArgInfo classifyReturnType(QualType RetTy) const;
10859 
10860   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10861                     QualType Ty) const override;
10862 
10863   ABIArgInfo extendType(QualType Ty) const;
10864 
10865   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10866                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10867                                 CharUnits &Field2Off, int &NeededArgGPRs,
10868                                 int &NeededArgFPRs) const;
10869   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10870                                                CharUnits Field1Off,
10871                                                llvm::Type *Field2Ty,
10872                                                CharUnits Field2Off) const;
10873 };
10874 } // end anonymous namespace
10875 
10876 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10877   QualType RetTy = FI.getReturnType();
10878   if (!getCXXABI().classifyReturnType(FI))
10879     FI.getReturnInfo() = classifyReturnType(RetTy);
10880 
10881   // IsRetIndirect is true if classifyArgumentType indicated the value should
10882   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10883   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10884   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10885   // list and pass indirectly on RV32.
10886   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
10887   if (!IsRetIndirect && RetTy->isScalarType() &&
10888       getContext().getTypeSize(RetTy) > (2 * XLen)) {
10889     if (RetTy->isComplexType() && FLen) {
10890       QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
10891       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
10892     } else {
10893       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
10894       IsRetIndirect = true;
10895     }
10896   }
10897 
10898   // We must track the number of GPRs used in order to conform to the RISC-V
10899   // ABI, as integer scalars passed in registers should have signext/zeroext
10900   // when promoted, but are anyext if passed on the stack. As GPR usage is
10901   // different for variadic arguments, we must also track whether we are
10902   // examining a vararg or not.
10903   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
10904   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
10905   int NumFixedArgs = FI.getNumRequiredArgs();
10906 
10907   int ArgNum = 0;
10908   for (auto &ArgInfo : FI.arguments()) {
10909     bool IsFixed = ArgNum < NumFixedArgs;
10910     ArgInfo.info =
10911         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
10912     ArgNum++;
10913   }
10914 }
10915 
10916 // Returns true if the struct is a potential candidate for the floating point
10917 // calling convention. If this function returns true, the caller is
10918 // responsible for checking that if there is only a single field then that
10919 // field is a float.
10920 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10921                                                   llvm::Type *&Field1Ty,
10922                                                   CharUnits &Field1Off,
10923                                                   llvm::Type *&Field2Ty,
10924                                                   CharUnits &Field2Off) const {
10925   bool IsInt = Ty->isIntegralOrEnumerationType();
10926   bool IsFloat = Ty->isRealFloatingType();
10927 
10928   if (IsInt || IsFloat) {
10929     uint64_t Size = getContext().getTypeSize(Ty);
10930     if (IsInt && Size > XLen)
10931       return false;
10932     // Can't be eligible if larger than the FP registers. Half precision isn't
10933     // currently supported on RISC-V and the ABI hasn't been confirmed, so
10934     // default to the integer ABI in that case.
10935     if (IsFloat && (Size > FLen || Size < 32))
10936       return false;
10937     // Can't be eligible if an integer type was already found (int+int pairs
10938     // are not eligible).
10939     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
10940       return false;
10941     if (!Field1Ty) {
10942       Field1Ty = CGT.ConvertType(Ty);
10943       Field1Off = CurOff;
10944       return true;
10945     }
10946     if (!Field2Ty) {
10947       Field2Ty = CGT.ConvertType(Ty);
10948       Field2Off = CurOff;
10949       return true;
10950     }
10951     return false;
10952   }
10953 
10954   if (auto CTy = Ty->getAs<ComplexType>()) {
10955     if (Field1Ty)
10956       return false;
10957     QualType EltTy = CTy->getElementType();
10958     if (getContext().getTypeSize(EltTy) > FLen)
10959       return false;
10960     Field1Ty = CGT.ConvertType(EltTy);
10961     Field1Off = CurOff;
10962     Field2Ty = Field1Ty;
10963     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
10964     return true;
10965   }
10966 
10967   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
10968     uint64_t ArraySize = ATy->getSize().getZExtValue();
10969     QualType EltTy = ATy->getElementType();
10970     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
10971     for (uint64_t i = 0; i < ArraySize; ++i) {
10972       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
10973                                                 Field1Off, Field2Ty, Field2Off);
10974       if (!Ret)
10975         return false;
10976       CurOff += EltSize;
10977     }
10978     return true;
10979   }
10980 
10981   if (const auto *RTy = Ty->getAs<RecordType>()) {
10982     // Structures with either a non-trivial destructor or a non-trivial
10983     // copy constructor are not eligible for the FP calling convention.
10984     if (getRecordArgABI(Ty, CGT.getCXXABI()))
10985       return false;
10986     if (isEmptyRecord(getContext(), Ty, true))
10987       return true;
10988     const RecordDecl *RD = RTy->getDecl();
10989     // Unions aren't eligible unless they're empty (which is caught above).
10990     if (RD->isUnion())
10991       return false;
10992     int ZeroWidthBitFieldCount = 0;
10993     for (const FieldDecl *FD : RD->fields()) {
10994       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
10995       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
10996       QualType QTy = FD->getType();
10997       if (FD->isBitField()) {
10998         unsigned BitWidth = FD->getBitWidthValue(getContext());
10999         // Allow a bitfield with a type greater than XLen as long as the
11000         // bitwidth is XLen or less.
11001         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
11002           QTy = getContext().getIntTypeForBitwidth(XLen, false);
11003         if (BitWidth == 0) {
11004           ZeroWidthBitFieldCount++;
11005           continue;
11006         }
11007       }
11008 
11009       bool Ret = detectFPCCEligibleStructHelper(
11010           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
11011           Field1Ty, Field1Off, Field2Ty, Field2Off);
11012       if (!Ret)
11013         return false;
11014 
11015       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
11016       // or int+fp structs, but are ignored for a struct with an fp field and
11017       // any number of zero-width bitfields.
11018       if (Field2Ty && ZeroWidthBitFieldCount > 0)
11019         return false;
11020     }
11021     return Field1Ty != nullptr;
11022   }
11023 
11024   return false;
11025 }
11026 
11027 // Determine if a struct is eligible for passing according to the floating
11028 // point calling convention (i.e., when flattened it contains a single fp
11029 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
11030 // NeededArgGPRs are incremented appropriately.
11031 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
11032                                             CharUnits &Field1Off,
11033                                             llvm::Type *&Field2Ty,
11034                                             CharUnits &Field2Off,
11035                                             int &NeededArgGPRs,
11036                                             int &NeededArgFPRs) const {
11037   Field1Ty = nullptr;
11038   Field2Ty = nullptr;
11039   NeededArgGPRs = 0;
11040   NeededArgFPRs = 0;
11041   bool IsCandidate = detectFPCCEligibleStructHelper(
11042       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
11043   // Not really a candidate if we have a single int but no float.
11044   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
11045     return false;
11046   if (!IsCandidate)
11047     return false;
11048   if (Field1Ty && Field1Ty->isFloatingPointTy())
11049     NeededArgFPRs++;
11050   else if (Field1Ty)
11051     NeededArgGPRs++;
11052   if (Field2Ty && Field2Ty->isFloatingPointTy())
11053     NeededArgFPRs++;
11054   else if (Field2Ty)
11055     NeededArgGPRs++;
11056   return true;
11057 }
11058 
11059 // Call getCoerceAndExpand for the two-element flattened struct described by
11060 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
11061 // appropriate coerceToType and unpaddedCoerceToType.
11062 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
11063     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
11064     CharUnits Field2Off) const {
11065   SmallVector<llvm::Type *, 3> CoerceElts;
11066   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
11067   if (!Field1Off.isZero())
11068     CoerceElts.push_back(llvm::ArrayType::get(
11069         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
11070 
11071   CoerceElts.push_back(Field1Ty);
11072   UnpaddedCoerceElts.push_back(Field1Ty);
11073 
11074   if (!Field2Ty) {
11075     return ABIArgInfo::getCoerceAndExpand(
11076         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
11077         UnpaddedCoerceElts[0]);
11078   }
11079 
11080   CharUnits Field2Align =
11081       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
11082   CharUnits Field1End = Field1Off +
11083       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
11084   CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
11085 
11086   CharUnits Padding = CharUnits::Zero();
11087   if (Field2Off > Field2OffNoPadNoPack)
11088     Padding = Field2Off - Field2OffNoPadNoPack;
11089   else if (Field2Off != Field2Align && Field2Off > Field1End)
11090     Padding = Field2Off - Field1End;
11091 
11092   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
11093 
11094   if (!Padding.isZero())
11095     CoerceElts.push_back(llvm::ArrayType::get(
11096         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
11097 
11098   CoerceElts.push_back(Field2Ty);
11099   UnpaddedCoerceElts.push_back(Field2Ty);
11100 
11101   auto CoerceToType =
11102       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
11103   auto UnpaddedCoerceToType =
11104       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
11105 
11106   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
11107 }
11108 
11109 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
11110                                               int &ArgGPRsLeft,
11111                                               int &ArgFPRsLeft) const {
11112   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
11113   Ty = useFirstFieldIfTransparentUnion(Ty);
11114 
11115   // Structures with either a non-trivial destructor or a non-trivial
11116   // copy constructor are always passed indirectly.
11117   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
11118     if (ArgGPRsLeft)
11119       ArgGPRsLeft -= 1;
11120     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
11121                                            CGCXXABI::RAA_DirectInMemory);
11122   }
11123 
11124   // Ignore empty structs/unions.
11125   if (isEmptyRecord(getContext(), Ty, true))
11126     return ABIArgInfo::getIgnore();
11127 
11128   uint64_t Size = getContext().getTypeSize(Ty);
11129 
11130   // Pass floating point values via FPRs if possible.
11131   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
11132       FLen >= Size && ArgFPRsLeft) {
11133     ArgFPRsLeft--;
11134     return ABIArgInfo::getDirect();
11135   }
11136 
11137   // Complex types for the hard float ABI must be passed direct rather than
11138   // using CoerceAndExpand.
11139   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
11140     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
11141     if (getContext().getTypeSize(EltTy) <= FLen) {
11142       ArgFPRsLeft -= 2;
11143       return ABIArgInfo::getDirect();
11144     }
11145   }
11146 
11147   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
11148     llvm::Type *Field1Ty = nullptr;
11149     llvm::Type *Field2Ty = nullptr;
11150     CharUnits Field1Off = CharUnits::Zero();
11151     CharUnits Field2Off = CharUnits::Zero();
11152     int NeededArgGPRs = 0;
11153     int NeededArgFPRs = 0;
11154     bool IsCandidate =
11155         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
11156                                  NeededArgGPRs, NeededArgFPRs);
11157     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
11158         NeededArgFPRs <= ArgFPRsLeft) {
11159       ArgGPRsLeft -= NeededArgGPRs;
11160       ArgFPRsLeft -= NeededArgFPRs;
11161       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
11162                                                Field2Off);
11163     }
11164   }
11165 
11166   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
11167   bool MustUseStack = false;
11168   // Determine the number of GPRs needed to pass the current argument
11169   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
11170   // register pairs, so may consume 3 registers.
11171   int NeededArgGPRs = 1;
11172   if (!IsFixed && NeededAlign == 2 * XLen)
11173     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
11174   else if (Size > XLen && Size <= 2 * XLen)
11175     NeededArgGPRs = 2;
11176 
11177   if (NeededArgGPRs > ArgGPRsLeft) {
11178     MustUseStack = true;
11179     NeededArgGPRs = ArgGPRsLeft;
11180   }
11181 
11182   ArgGPRsLeft -= NeededArgGPRs;
11183 
11184   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
11185     // Treat an enum type as its underlying type.
11186     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
11187       Ty = EnumTy->getDecl()->getIntegerType();
11188 
11189     // All integral types are promoted to XLen width, unless passed on the
11190     // stack.
11191     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
11192       return extendType(Ty);
11193     }
11194 
11195     if (const auto *EIT = Ty->getAs<BitIntType>()) {
11196       if (EIT->getNumBits() < XLen && !MustUseStack)
11197         return extendType(Ty);
11198       if (EIT->getNumBits() > 128 ||
11199           (!getContext().getTargetInfo().hasInt128Type() &&
11200            EIT->getNumBits() > 64))
11201         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11202     }
11203 
11204     return ABIArgInfo::getDirect();
11205   }
11206 
11207   // Aggregates which are <= 2*XLen will be passed in registers if possible,
11208   // so coerce to integers.
11209   if (Size <= 2 * XLen) {
11210     unsigned Alignment = getContext().getTypeAlign(Ty);
11211 
11212     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
11213     // required, and a 2-element XLen array if only XLen alignment is required.
11214     if (Size <= XLen) {
11215       return ABIArgInfo::getDirect(
11216           llvm::IntegerType::get(getVMContext(), XLen));
11217     } else if (Alignment == 2 * XLen) {
11218       return ABIArgInfo::getDirect(
11219           llvm::IntegerType::get(getVMContext(), 2 * XLen));
11220     } else {
11221       return ABIArgInfo::getDirect(llvm::ArrayType::get(
11222           llvm::IntegerType::get(getVMContext(), XLen), 2));
11223     }
11224   }
11225   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11226 }
11227 
11228 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
11229   if (RetTy->isVoidType())
11230     return ABIArgInfo::getIgnore();
11231 
11232   int ArgGPRsLeft = 2;
11233   int ArgFPRsLeft = FLen ? 2 : 0;
11234 
11235   // The rules for return and argument types are the same, so defer to
11236   // classifyArgumentType.
11237   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
11238                               ArgFPRsLeft);
11239 }
11240 
11241 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11242                                 QualType Ty) const {
11243   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
11244 
11245   // Empty records are ignored for parameter passing purposes.
11246   if (isEmptyRecord(getContext(), Ty, true)) {
11247     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
11248                            getVAListElementType(CGF), SlotSize);
11249     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
11250     return Addr;
11251   }
11252 
11253   auto TInfo = getContext().getTypeInfoInChars(Ty);
11254 
11255   // Arguments bigger than 2*Xlen bytes are passed indirectly.
11256   bool IsIndirect = TInfo.Width > 2 * SlotSize;
11257 
11258   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo,
11259                           SlotSize, /*AllowHigherAlign=*/true);
11260 }
11261 
11262 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
11263   int TySize = getContext().getTypeSize(Ty);
11264   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
11265   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
11266     return ABIArgInfo::getSignExtend(Ty);
11267   return ABIArgInfo::getExtend(Ty);
11268 }
11269 
11270 namespace {
11271 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
11272 public:
11273   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
11274                          unsigned FLen)
11275       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
11276 
11277   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
11278                            CodeGen::CodeGenModule &CGM) const override {
11279     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
11280     if (!FD) return;
11281 
11282     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
11283     if (!Attr)
11284       return;
11285 
11286     const char *Kind;
11287     switch (Attr->getInterrupt()) {
11288     case RISCVInterruptAttr::user: Kind = "user"; break;
11289     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
11290     case RISCVInterruptAttr::machine: Kind = "machine"; break;
11291     }
11292 
11293     auto *Fn = cast<llvm::Function>(GV);
11294 
11295     Fn->addFnAttr("interrupt", Kind);
11296   }
11297 };
11298 } // namespace
11299 
11300 //===----------------------------------------------------------------------===//
11301 // VE ABI Implementation.
11302 //
11303 namespace {
11304 class VEABIInfo : public DefaultABIInfo {
11305 public:
11306   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
11307 
11308 private:
11309   ABIArgInfo classifyReturnType(QualType RetTy) const;
11310   ABIArgInfo classifyArgumentType(QualType RetTy) const;
11311   void computeInfo(CGFunctionInfo &FI) const override;
11312 };
11313 } // end anonymous namespace
11314 
11315 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
11316   if (Ty->isAnyComplexType())
11317     return ABIArgInfo::getDirect();
11318   uint64_t Size = getContext().getTypeSize(Ty);
11319   if (Size < 64 && Ty->isIntegerType())
11320     return ABIArgInfo::getExtend(Ty);
11321   return DefaultABIInfo::classifyReturnType(Ty);
11322 }
11323 
11324 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
11325   if (Ty->isAnyComplexType())
11326     return ABIArgInfo::getDirect();
11327   uint64_t Size = getContext().getTypeSize(Ty);
11328   if (Size < 64 && Ty->isIntegerType())
11329     return ABIArgInfo::getExtend(Ty);
11330   return DefaultABIInfo::classifyArgumentType(Ty);
11331 }
11332 
11333 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
11334   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
11335   for (auto &Arg : FI.arguments())
11336     Arg.info = classifyArgumentType(Arg.type);
11337 }
11338 
11339 namespace {
11340 class VETargetCodeGenInfo : public TargetCodeGenInfo {
11341 public:
11342   VETargetCodeGenInfo(CodeGenTypes &CGT)
11343       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
11344   // VE ABI requires the arguments of variadic and prototype-less functions
11345   // are passed in both registers and memory.
11346   bool isNoProtoCallVariadic(const CallArgList &args,
11347                              const FunctionNoProtoType *fnType) const override {
11348     return true;
11349   }
11350 };
11351 } // end anonymous namespace
11352 
11353 //===----------------------------------------------------------------------===//
11354 // CSKY ABI Implementation
11355 //===----------------------------------------------------------------------===//
11356 namespace {
11357 class CSKYABIInfo : public DefaultABIInfo {
11358   static const int NumArgGPRs = 4;
11359   static const int NumArgFPRs = 4;
11360 
11361   static const unsigned XLen = 32;
11362   unsigned FLen;
11363 
11364 public:
11365   CSKYABIInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
11366       : DefaultABIInfo(CGT), FLen(FLen) {}
11367 
11368   void computeInfo(CGFunctionInfo &FI) const override;
11369   ABIArgInfo classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
11370                                   int &ArgFPRsLeft,
11371                                   bool isReturnType = false) const;
11372   ABIArgInfo classifyReturnType(QualType RetTy) const;
11373 
11374   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11375                     QualType Ty) const override;
11376 };
11377 
11378 } // end anonymous namespace
11379 
11380 void CSKYABIInfo::computeInfo(CGFunctionInfo &FI) const {
11381   QualType RetTy = FI.getReturnType();
11382   if (!getCXXABI().classifyReturnType(FI))
11383     FI.getReturnInfo() = classifyReturnType(RetTy);
11384 
11385   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
11386 
11387   // We must track the number of GPRs used in order to conform to the CSKY
11388   // ABI, as integer scalars passed in registers should have signext/zeroext
11389   // when promoted.
11390   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
11391   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
11392 
11393   for (auto &ArgInfo : FI.arguments()) {
11394     ArgInfo.info = classifyArgumentType(ArgInfo.type, ArgGPRsLeft, ArgFPRsLeft);
11395   }
11396 }
11397 
11398 Address CSKYABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11399                                QualType Ty) const {
11400   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
11401 
11402   // Empty records are ignored for parameter passing purposes.
11403   if (isEmptyRecord(getContext(), Ty, true)) {
11404     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
11405                            getVAListElementType(CGF), SlotSize);
11406     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
11407     return Addr;
11408   }
11409 
11410   auto TInfo = getContext().getTypeInfoInChars(Ty);
11411 
11412   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, false, TInfo, SlotSize,
11413                           /*AllowHigherAlign=*/true);
11414 }
11415 
11416 ABIArgInfo CSKYABIInfo::classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
11417                                              int &ArgFPRsLeft,
11418                                              bool isReturnType) const {
11419   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
11420   Ty = useFirstFieldIfTransparentUnion(Ty);
11421 
11422   // Structures with either a non-trivial destructor or a non-trivial
11423   // copy constructor are always passed indirectly.
11424   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
11425     if (ArgGPRsLeft)
11426       ArgGPRsLeft -= 1;
11427     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
11428                                            CGCXXABI::RAA_DirectInMemory);
11429   }
11430 
11431   // Ignore empty structs/unions.
11432   if (isEmptyRecord(getContext(), Ty, true))
11433     return ABIArgInfo::getIgnore();
11434 
11435   if (!Ty->getAsUnionType())
11436     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
11437       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
11438 
11439   uint64_t Size = getContext().getTypeSize(Ty);
11440   // Pass floating point values via FPRs if possible.
11441   if (Ty->isFloatingType() && !Ty->isComplexType() && FLen >= Size &&
11442       ArgFPRsLeft) {
11443     ArgFPRsLeft--;
11444     return ABIArgInfo::getDirect();
11445   }
11446 
11447   // Complex types for the hard float ABI must be passed direct rather than
11448   // using CoerceAndExpand.
11449   if (Ty->isComplexType() && FLen && !isReturnType) {
11450     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
11451     if (getContext().getTypeSize(EltTy) <= FLen) {
11452       ArgFPRsLeft -= 2;
11453       return ABIArgInfo::getDirect();
11454     }
11455   }
11456 
11457   if (!isAggregateTypeForABI(Ty)) {
11458     // Treat an enum type as its underlying type.
11459     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
11460       Ty = EnumTy->getDecl()->getIntegerType();
11461 
11462     // All integral types are promoted to XLen width, unless passed on the
11463     // stack.
11464     if (Size < XLen && Ty->isIntegralOrEnumerationType())
11465       return ABIArgInfo::getExtend(Ty);
11466 
11467     if (const auto *EIT = Ty->getAs<BitIntType>()) {
11468       if (EIT->getNumBits() < XLen)
11469         return ABIArgInfo::getExtend(Ty);
11470     }
11471 
11472     return ABIArgInfo::getDirect();
11473   }
11474 
11475   // For argument type, the first 4*XLen parts of aggregate will be passed
11476   // in registers, and the rest will be passed in stack.
11477   // So we can coerce to integers directly and let backend handle it correctly.
11478   // For return type, aggregate which <= 2*XLen will be returned in registers.
11479   // Otherwise, aggregate will be returned indirectly.
11480   if (!isReturnType || (isReturnType && Size <= 2 * XLen)) {
11481     if (Size <= XLen) {
11482       return ABIArgInfo::getDirect(
11483           llvm::IntegerType::get(getVMContext(), XLen));
11484     } else {
11485       return ABIArgInfo::getDirect(llvm::ArrayType::get(
11486           llvm::IntegerType::get(getVMContext(), XLen), (Size + 31) / XLen));
11487     }
11488   }
11489   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11490 }
11491 
11492 ABIArgInfo CSKYABIInfo::classifyReturnType(QualType RetTy) const {
11493   if (RetTy->isVoidType())
11494     return ABIArgInfo::getIgnore();
11495 
11496   int ArgGPRsLeft = 2;
11497   int ArgFPRsLeft = FLen ? 1 : 0;
11498 
11499   // The rules for return and argument types are the same, so defer to
11500   // classifyArgumentType.
11501   return classifyArgumentType(RetTy, ArgGPRsLeft, ArgFPRsLeft, true);
11502 }
11503 
11504 namespace {
11505 class CSKYTargetCodeGenInfo : public TargetCodeGenInfo {
11506 public:
11507   CSKYTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
11508       : TargetCodeGenInfo(std::make_unique<CSKYABIInfo>(CGT, FLen)) {}
11509 };
11510 } // end anonymous namespace
11511 
11512 //===----------------------------------------------------------------------===//
11513 // Driver code
11514 //===----------------------------------------------------------------------===//
11515 
11516 bool CodeGenModule::supportsCOMDAT() const {
11517   return getTriple().supportsCOMDAT();
11518 }
11519 
11520 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
11521   if (TheTargetCodeGenInfo)
11522     return *TheTargetCodeGenInfo;
11523 
11524   // Helper to set the unique_ptr while still keeping the return value.
11525   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
11526     this->TheTargetCodeGenInfo.reset(P);
11527     return *P;
11528   };
11529 
11530   const llvm::Triple &Triple = getTarget().getTriple();
11531   switch (Triple.getArch()) {
11532   default:
11533     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
11534 
11535   case llvm::Triple::le32:
11536     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11537   case llvm::Triple::m68k:
11538     return SetCGInfo(new M68kTargetCodeGenInfo(Types));
11539   case llvm::Triple::mips:
11540   case llvm::Triple::mipsel:
11541     if (Triple.getOS() == llvm::Triple::NaCl)
11542       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
11543     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
11544 
11545   case llvm::Triple::mips64:
11546   case llvm::Triple::mips64el:
11547     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
11548 
11549   case llvm::Triple::avr: {
11550     // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
11551     // on avrtiny. For passing return value, R18~R25 are used on avr, and
11552     // R22~R25 are used on avrtiny.
11553     unsigned NPR = getTarget().getABI() == "avrtiny" ? 6 : 18;
11554     unsigned NRR = getTarget().getABI() == "avrtiny" ? 4 : 8;
11555     return SetCGInfo(new AVRTargetCodeGenInfo(Types, NPR, NRR));
11556   }
11557 
11558   case llvm::Triple::aarch64:
11559   case llvm::Triple::aarch64_32:
11560   case llvm::Triple::aarch64_be: {
11561     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
11562     if (getTarget().getABI() == "darwinpcs")
11563       Kind = AArch64ABIInfo::DarwinPCS;
11564     else if (Triple.isOSWindows())
11565       return SetCGInfo(
11566           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
11567 
11568     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
11569   }
11570 
11571   case llvm::Triple::wasm32:
11572   case llvm::Triple::wasm64: {
11573     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
11574     if (getTarget().getABI() == "experimental-mv")
11575       Kind = WebAssemblyABIInfo::ExperimentalMV;
11576     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
11577   }
11578 
11579   case llvm::Triple::arm:
11580   case llvm::Triple::armeb:
11581   case llvm::Triple::thumb:
11582   case llvm::Triple::thumbeb: {
11583     if (Triple.getOS() == llvm::Triple::Win32) {
11584       return SetCGInfo(
11585           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
11586     }
11587 
11588     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
11589     StringRef ABIStr = getTarget().getABI();
11590     if (ABIStr == "apcs-gnu")
11591       Kind = ARMABIInfo::APCS;
11592     else if (ABIStr == "aapcs16")
11593       Kind = ARMABIInfo::AAPCS16_VFP;
11594     else if (CodeGenOpts.FloatABI == "hard" ||
11595              (CodeGenOpts.FloatABI != "soft" &&
11596               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
11597                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
11598                Triple.getEnvironment() == llvm::Triple::EABIHF)))
11599       Kind = ARMABIInfo::AAPCS_VFP;
11600 
11601     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
11602   }
11603 
11604   case llvm::Triple::ppc: {
11605     if (Triple.isOSAIX())
11606       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
11607 
11608     bool IsSoftFloat =
11609         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
11610     bool RetSmallStructInRegABI =
11611         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11612     return SetCGInfo(
11613         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11614   }
11615   case llvm::Triple::ppcle: {
11616     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11617     bool RetSmallStructInRegABI =
11618         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11619     return SetCGInfo(
11620         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
11621   }
11622   case llvm::Triple::ppc64:
11623     if (Triple.isOSAIX())
11624       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
11625 
11626     if (Triple.isOSBinFormatELF()) {
11627       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
11628       if (getTarget().getABI() == "elfv2")
11629         Kind = PPC64_SVR4_ABIInfo::ELFv2;
11630       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11631 
11632       return SetCGInfo(
11633           new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11634     }
11635     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
11636   case llvm::Triple::ppc64le: {
11637     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
11638     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
11639     if (getTarget().getABI() == "elfv1")
11640       Kind = PPC64_SVR4_ABIInfo::ELFv1;
11641     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
11642 
11643     return SetCGInfo(
11644         new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
11645   }
11646 
11647   case llvm::Triple::nvptx:
11648   case llvm::Triple::nvptx64:
11649     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
11650 
11651   case llvm::Triple::msp430:
11652     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
11653 
11654   case llvm::Triple::riscv32:
11655   case llvm::Triple::riscv64: {
11656     StringRef ABIStr = getTarget().getABI();
11657     unsigned XLen = getTarget().getPointerWidth(0);
11658     unsigned ABIFLen = 0;
11659     if (ABIStr.endswith("f"))
11660       ABIFLen = 32;
11661     else if (ABIStr.endswith("d"))
11662       ABIFLen = 64;
11663     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
11664   }
11665 
11666   case llvm::Triple::systemz: {
11667     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
11668     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
11669     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
11670   }
11671 
11672   case llvm::Triple::tce:
11673   case llvm::Triple::tcele:
11674     return SetCGInfo(new TCETargetCodeGenInfo(Types));
11675 
11676   case llvm::Triple::x86: {
11677     bool IsDarwinVectorABI = Triple.isOSDarwin();
11678     bool RetSmallStructInRegABI =
11679         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
11680     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
11681 
11682     if (Triple.getOS() == llvm::Triple::Win32) {
11683       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
11684           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11685           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
11686     } else {
11687       return SetCGInfo(new X86_32TargetCodeGenInfo(
11688           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
11689           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
11690           CodeGenOpts.FloatABI == "soft"));
11691     }
11692   }
11693 
11694   case llvm::Triple::x86_64: {
11695     StringRef ABI = getTarget().getABI();
11696     X86AVXABILevel AVXLevel =
11697         (ABI == "avx512"
11698              ? X86AVXABILevel::AVX512
11699              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
11700 
11701     switch (Triple.getOS()) {
11702     case llvm::Triple::Win32:
11703       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
11704     default:
11705       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
11706     }
11707   }
11708   case llvm::Triple::hexagon:
11709     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
11710   case llvm::Triple::lanai:
11711     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
11712   case llvm::Triple::r600:
11713     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11714   case llvm::Triple::amdgcn:
11715     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
11716   case llvm::Triple::sparc:
11717     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
11718   case llvm::Triple::sparcv9:
11719     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
11720   case llvm::Triple::xcore:
11721     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
11722   case llvm::Triple::arc:
11723     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
11724   case llvm::Triple::spir:
11725   case llvm::Triple::spir64:
11726     return SetCGInfo(new CommonSPIRTargetCodeGenInfo(Types));
11727   case llvm::Triple::spirv32:
11728   case llvm::Triple::spirv64:
11729     return SetCGInfo(new SPIRVTargetCodeGenInfo(Types));
11730   case llvm::Triple::ve:
11731     return SetCGInfo(new VETargetCodeGenInfo(Types));
11732   case llvm::Triple::csky: {
11733     bool IsSoftFloat = !getTarget().hasFeature("hard-float-abi");
11734     bool hasFP64 = getTarget().hasFeature("fpuv2_df") ||
11735                    getTarget().hasFeature("fpuv3_df");
11736     return SetCGInfo(new CSKYTargetCodeGenInfo(Types, IsSoftFloat ? 0
11737                                                       : hasFP64   ? 64
11738                                                                   : 32));
11739   }
11740   }
11741 }
11742 
11743 /// Create an OpenCL kernel for an enqueued block.
11744 ///
11745 /// The kernel has the same function type as the block invoke function. Its
11746 /// name is the name of the block invoke function postfixed with "_kernel".
11747 /// It simply calls the block invoke function then returns.
11748 llvm::Function *
11749 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
11750                                              llvm::Function *Invoke,
11751                                              llvm::Type *BlockTy) const {
11752   auto *InvokeFT = Invoke->getFunctionType();
11753   auto &C = CGF.getLLVMContext();
11754   std::string Name = Invoke->getName().str() + "_kernel";
11755   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C),
11756                                      InvokeFT->params(), false);
11757   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::ExternalLinkage, Name,
11758                                    &CGF.CGM.getModule());
11759   auto IP = CGF.Builder.saveIP();
11760   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11761   auto &Builder = CGF.Builder;
11762   Builder.SetInsertPoint(BB);
11763   llvm::SmallVector<llvm::Value *, 2> Args(llvm::make_pointer_range(F->args()));
11764   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11765   call->setCallingConv(Invoke->getCallingConv());
11766   Builder.CreateRetVoid();
11767   Builder.restoreIP(IP);
11768   return F;
11769 }
11770 
11771 /// Create an OpenCL kernel for an enqueued block.
11772 ///
11773 /// The type of the first argument (the block literal) is the struct type
11774 /// of the block literal instead of a pointer type. The first argument
11775 /// (block literal) is passed directly by value to the kernel. The kernel
11776 /// allocates the same type of struct on stack and stores the block literal
11777 /// to it and passes its pointer to the block invoke function. The kernel
11778 /// has "enqueued-block" function attribute and kernel argument metadata.
11779 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
11780     CodeGenFunction &CGF, llvm::Function *Invoke,
11781     llvm::Type *BlockTy) const {
11782   auto &Builder = CGF.Builder;
11783   auto &C = CGF.getLLVMContext();
11784 
11785   auto *InvokeFT = Invoke->getFunctionType();
11786   llvm::SmallVector<llvm::Type *, 2> ArgTys;
11787   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
11788   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
11789   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
11790   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
11791   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
11792   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
11793 
11794   ArgTys.push_back(BlockTy);
11795   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11796   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
11797   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
11798   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11799   AccessQuals.push_back(llvm::MDString::get(C, "none"));
11800   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
11801   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
11802     ArgTys.push_back(InvokeFT->getParamType(I));
11803     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
11804     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
11805     AccessQuals.push_back(llvm::MDString::get(C, "none"));
11806     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
11807     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
11808     ArgNames.push_back(
11809         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
11810   }
11811   std::string Name = Invoke->getName().str() + "_kernel";
11812   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
11813   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
11814                                    &CGF.CGM.getModule());
11815   F->addFnAttr("enqueued-block");
11816   auto IP = CGF.Builder.saveIP();
11817   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
11818   Builder.SetInsertPoint(BB);
11819   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
11820   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
11821   BlockPtr->setAlignment(BlockAlign);
11822   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
11823   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
11824   llvm::SmallVector<llvm::Value *, 2> Args;
11825   Args.push_back(Cast);
11826   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
11827     Args.push_back(I);
11828   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
11829   call->setCallingConv(Invoke->getCallingConv());
11830   Builder.CreateRetVoid();
11831   Builder.restoreIP(IP);
11832 
11833   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
11834   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
11835   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
11836   F->setMetadata("kernel_arg_base_type",
11837                  llvm::MDNode::get(C, ArgBaseTypeNames));
11838   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
11839   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
11840     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
11841 
11842   return F;
11843 }
11844