xref: /freebsd-src/contrib/llvm-project/clang/lib/CodeGen/TargetInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 "llvm/ADT/SmallBitVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/IntrinsicsNVPTX.h"
33 #include "llvm/IR/IntrinsicsS390.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 
39 using namespace clang;
40 using namespace CodeGen;
41 
42 // Helper for coercing an aggregate argument or return value into an integer
43 // array of the same size (including padding) and alignment.  This alternate
44 // coercion happens only for the RenderScript ABI and can be removed after
45 // runtimes that rely on it are no longer supported.
46 //
47 // RenderScript assumes that the size of the argument / return value in the IR
48 // is the same as the size of the corresponding qualified type. This helper
49 // coerces the aggregate type into an array of the same size (including
50 // padding).  This coercion is used in lieu of expansion of struct members or
51 // other canonical coercions that return a coerced-type of larger size.
52 //
53 // Ty          - The argument / return value type
54 // Context     - The associated ASTContext
55 // LLVMContext - The associated LLVMContext
56 static ABIArgInfo coerceToIntArray(QualType Ty,
57                                    ASTContext &Context,
58                                    llvm::LLVMContext &LLVMContext) {
59   // Alignment and Size are measured in bits.
60   const uint64_t Size = Context.getTypeSize(Ty);
61   const uint64_t Alignment = Context.getTypeAlign(Ty);
62   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
63   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
64   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
65 }
66 
67 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
68                                llvm::Value *Array,
69                                llvm::Value *Value,
70                                unsigned FirstIndex,
71                                unsigned LastIndex) {
72   // Alternatively, we could emit this as a loop in the source.
73   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
74     llvm::Value *Cell =
75         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
76     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
77   }
78 }
79 
80 static bool isAggregateTypeForABI(QualType T) {
81   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
82          T->isMemberFunctionPointerType();
83 }
84 
85 ABIArgInfo ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByVal,
86                                             bool Realign,
87                                             llvm::Type *Padding) const {
88   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), ByVal,
89                                  Realign, Padding);
90 }
91 
92 ABIArgInfo
93 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
94   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
95                                       /*ByVal*/ false, Realign);
96 }
97 
98 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
99                              QualType Ty) const {
100   return Address::invalid();
101 }
102 
103 static llvm::Type *getVAListElementType(CodeGenFunction &CGF) {
104   return CGF.ConvertTypeForMem(
105       CGF.getContext().getBuiltinVaListType()->getPointeeType());
106 }
107 
108 bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
109   if (getContext().isPromotableIntegerType(Ty))
110     return true;
111 
112   if (const auto *EIT = Ty->getAs<BitIntType>())
113     if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
114       return true;
115 
116   return false;
117 }
118 
119 ABIInfo::~ABIInfo() = default;
120 
121 SwiftABIInfo::~SwiftABIInfo() = default;
122 
123 /// Does the given lowering require more than the given number of
124 /// registers when expanded?
125 ///
126 /// This is intended to be the basis of a reasonable basic implementation
127 /// of should{Pass,Return}IndirectlyForSwift.
128 ///
129 /// For most targets, a limit of four total registers is reasonable; this
130 /// limits the amount of code required in order to move around the value
131 /// in case it wasn't produced immediately prior to the call by the caller
132 /// (or wasn't produced in exactly the right registers) or isn't used
133 /// immediately within the callee.  But some targets may need to further
134 /// limit the register count due to an inability to support that many
135 /// return registers.
136 static bool occupiesMoreThan(CodeGenTypes &cgt,
137                              ArrayRef<llvm::Type*> scalarTypes,
138                              unsigned maxAllRegisters) {
139   unsigned intCount = 0, fpCount = 0;
140   for (llvm::Type *type : scalarTypes) {
141     if (type->isPointerTy()) {
142       intCount++;
143     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
144       auto ptrWidth = cgt.getTarget().getPointerWidth(LangAS::Default);
145       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
146     } else {
147       assert(type->isVectorTy() || type->isFloatingPointTy());
148       fpCount++;
149     }
150   }
151 
152   return (intCount + fpCount > maxAllRegisters);
153 }
154 
155 bool SwiftABIInfo::shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys,
156                                         bool AsReturnValue) const {
157   return occupiesMoreThan(CGT, ComponentTys, /*total=*/4);
158 }
159 
160 bool SwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
161                                      unsigned NumElts) const {
162   // The default implementation of this assumes that the target guarantees
163   // 128-bit SIMD support but nothing more.
164   return (VectorSize.getQuantity() > 8 && VectorSize.getQuantity() <= 16);
165 }
166 
167 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
168                                               CGCXXABI &CXXABI) {
169   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
170   if (!RD) {
171     if (!RT->getDecl()->canPassInRegisters())
172       return CGCXXABI::RAA_Indirect;
173     return CGCXXABI::RAA_Default;
174   }
175   return CXXABI.getRecordArgABI(RD);
176 }
177 
178 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
179                                               CGCXXABI &CXXABI) {
180   const RecordType *RT = T->getAs<RecordType>();
181   if (!RT)
182     return CGCXXABI::RAA_Default;
183   return getRecordArgABI(RT, CXXABI);
184 }
185 
186 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
187                                const ABIInfo &Info) {
188   QualType Ty = FI.getReturnType();
189 
190   if (const auto *RT = Ty->getAs<RecordType>())
191     if (!isa<CXXRecordDecl>(RT->getDecl()) &&
192         !RT->getDecl()->canPassInRegisters()) {
193       FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
194       return true;
195     }
196 
197   return CXXABI.classifyReturnType(FI);
198 }
199 
200 /// Pass transparent unions as if they were the type of the first element. Sema
201 /// should ensure that all elements of the union have the same "machine type".
202 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
203   if (const RecordType *UT = Ty->getAsUnionType()) {
204     const RecordDecl *UD = UT->getDecl();
205     if (UD->hasAttr<TransparentUnionAttr>()) {
206       assert(!UD->field_empty() && "sema created an empty transparent union");
207       return UD->field_begin()->getType();
208     }
209   }
210   return Ty;
211 }
212 
213 CGCXXABI &ABIInfo::getCXXABI() const {
214   return CGT.getCXXABI();
215 }
216 
217 ASTContext &ABIInfo::getContext() const {
218   return CGT.getContext();
219 }
220 
221 llvm::LLVMContext &ABIInfo::getVMContext() const {
222   return CGT.getLLVMContext();
223 }
224 
225 const llvm::DataLayout &ABIInfo::getDataLayout() const {
226   return CGT.getDataLayout();
227 }
228 
229 const TargetInfo &ABIInfo::getTarget() const {
230   return CGT.getTarget();
231 }
232 
233 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
234   return CGT.getCodeGenOpts();
235 }
236 
237 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
238 
239 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
240   return false;
241 }
242 
243 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
244                                                 uint64_t Members) const {
245   return false;
246 }
247 
248 bool ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
249   // For compatibility with GCC, ignore empty bitfields in C++ mode.
250   return getContext().getLangOpts().CPlusPlus;
251 }
252 
253 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
254   raw_ostream &OS = llvm::errs();
255   OS << "(ABIArgInfo Kind=";
256   switch (TheKind) {
257   case Direct:
258     OS << "Direct Type=";
259     if (llvm::Type *Ty = getCoerceToType())
260       Ty->print(OS);
261     else
262       OS << "null";
263     break;
264   case Extend:
265     OS << "Extend";
266     break;
267   case Ignore:
268     OS << "Ignore";
269     break;
270   case InAlloca:
271     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
272     break;
273   case Indirect:
274     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
275        << " ByVal=" << getIndirectByVal()
276        << " Realign=" << getIndirectRealign();
277     break;
278   case IndirectAliased:
279     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
280        << " AadrSpace=" << getIndirectAddrSpace()
281        << " Realign=" << getIndirectRealign();
282     break;
283   case Expand:
284     OS << "Expand";
285     break;
286   case CoerceAndExpand:
287     OS << "CoerceAndExpand Type=";
288     getCoerceAndExpandType()->print(OS);
289     break;
290   }
291   OS << ")\n";
292 }
293 
294 // Dynamically round a pointer up to a multiple of the given alignment.
295 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
296                                                   llvm::Value *Ptr,
297                                                   CharUnits Align) {
298   llvm::Value *PtrAsInt = Ptr;
299   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
300   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
301   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
302         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
303   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
304            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
305   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
306                                         Ptr->getType(),
307                                         Ptr->getName() + ".aligned");
308   return PtrAsInt;
309 }
310 
311 /// Emit va_arg for a platform using the common void* representation,
312 /// where arguments are simply emitted in an array of slots on the stack.
313 ///
314 /// This version implements the core direct-value passing rules.
315 ///
316 /// \param SlotSize - The size and alignment of a stack slot.
317 ///   Each argument will be allocated to a multiple of this number of
318 ///   slots, and all the slots will be aligned to this value.
319 /// \param AllowHigherAlign - The slot alignment is not a cap;
320 ///   an argument type with an alignment greater than the slot size
321 ///   will be emitted on a higher-alignment address, potentially
322 ///   leaving one or more empty slots behind as padding.  If this
323 ///   is false, the returned address might be less-aligned than
324 ///   DirectAlign.
325 /// \param ForceRightAdjust - Default is false. On big-endian platform and
326 ///   if the argument is smaller than a slot, set this flag will force
327 ///   right-adjust the argument in its slot irrespective of the type.
328 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
329                                       Address VAListAddr,
330                                       llvm::Type *DirectTy,
331                                       CharUnits DirectSize,
332                                       CharUnits DirectAlign,
333                                       CharUnits SlotSize,
334                                       bool AllowHigherAlign,
335                                       bool ForceRightAdjust = false) {
336   // Cast the element type to i8* if necessary.  Some platforms define
337   // va_list as a struct containing an i8* instead of just an i8*.
338   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
339     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
340 
341   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
342 
343   // If the CC aligns values higher than the slot size, do so if needed.
344   Address Addr = Address::invalid();
345   if (AllowHigherAlign && DirectAlign > SlotSize) {
346     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
347                    CGF.Int8Ty, DirectAlign);
348   } else {
349     Addr = Address(Ptr, CGF.Int8Ty, SlotSize);
350   }
351 
352   // Advance the pointer past the argument, then store that back.
353   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
354   Address NextPtr =
355       CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
356   CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
357 
358   // If the argument is smaller than a slot, and this is a big-endian
359   // target, the argument will be right-adjusted in its slot.
360   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
361       (!DirectTy->isStructTy() || ForceRightAdjust)) {
362     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
363   }
364 
365   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
366   return Addr;
367 }
368 
369 /// Emit va_arg for a platform using the common void* representation,
370 /// where arguments are simply emitted in an array of slots on the stack.
371 ///
372 /// \param IsIndirect - Values of this type are passed indirectly.
373 /// \param ValueInfo - The size and alignment of this type, generally
374 ///   computed with getContext().getTypeInfoInChars(ValueTy).
375 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
376 ///   Each argument will be allocated to a multiple of this number of
377 ///   slots, and all the slots will be aligned to this value.
378 /// \param AllowHigherAlign - The slot alignment is not a cap;
379 ///   an argument type with an alignment greater than the slot size
380 ///   will be emitted on a higher-alignment address, potentially
381 ///   leaving one or more empty slots behind as padding.
382 /// \param ForceRightAdjust - Default is false. On big-endian platform and
383 ///   if the argument is smaller than a slot, set this flag will force
384 ///   right-adjust the argument in its slot irrespective of the type.
385 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
386                                 QualType ValueTy, bool IsIndirect,
387                                 TypeInfoChars ValueInfo,
388                                 CharUnits SlotSizeAndAlign,
389                                 bool AllowHigherAlign,
390                                 bool ForceRightAdjust = false) {
391   // The size and alignment of the value that was passed directly.
392   CharUnits DirectSize, DirectAlign;
393   if (IsIndirect) {
394     DirectSize = CGF.getPointerSize();
395     DirectAlign = CGF.getPointerAlign();
396   } else {
397     DirectSize = ValueInfo.Width;
398     DirectAlign = ValueInfo.Align;
399   }
400 
401   // Cast the address we've calculated to the right type.
402   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy), *ElementTy = DirectTy;
403   if (IsIndirect)
404     DirectTy = DirectTy->getPointerTo(0);
405 
406   Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, DirectSize,
407                                         DirectAlign, SlotSizeAndAlign,
408                                         AllowHigherAlign, ForceRightAdjust);
409 
410   if (IsIndirect) {
411     Addr = Address(CGF.Builder.CreateLoad(Addr), ElementTy, ValueInfo.Align);
412   }
413 
414   return Addr;
415 }
416 
417 static Address complexTempStructure(CodeGenFunction &CGF, Address VAListAddr,
418                                     QualType Ty, CharUnits SlotSize,
419                                     CharUnits EltSize, const ComplexType *CTy) {
420   Address Addr =
421       emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, SlotSize * 2,
422                              SlotSize, SlotSize, /*AllowHigher*/ true);
423 
424   Address RealAddr = Addr;
425   Address ImagAddr = RealAddr;
426   if (CGF.CGM.getDataLayout().isBigEndian()) {
427     RealAddr =
428         CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize - EltSize);
429     ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
430                                                       2 * SlotSize - EltSize);
431   } else {
432     ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
433   }
434 
435   llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
436   RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
437   ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
438   llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
439   llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
440 
441   Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
442   CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
443                          /*init*/ true);
444   return Temp;
445 }
446 
447 static Address emitMergePHI(CodeGenFunction &CGF,
448                             Address Addr1, llvm::BasicBlock *Block1,
449                             Address Addr2, llvm::BasicBlock *Block2,
450                             const llvm::Twine &Name = "") {
451   assert(Addr1.getType() == Addr2.getType());
452   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
453   PHI->addIncoming(Addr1.getPointer(), Block1);
454   PHI->addIncoming(Addr2.getPointer(), Block2);
455   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
456   return Address(PHI, Addr1.getElementType(), Align);
457 }
458 
459 TargetCodeGenInfo::TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info)
460     : Info(std::move(Info)) {}
461 
462 TargetCodeGenInfo::~TargetCodeGenInfo() = default;
463 
464 // If someone can figure out a general rule for this, that would be great.
465 // It's probably just doomed to be platform-dependent, though.
466 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
467   // Verified for:
468   //   x86-64     FreeBSD, Linux, Darwin
469   //   x86-32     FreeBSD, Linux, Darwin
470   //   PowerPC    Linux, Darwin
471   //   ARM        Darwin (*not* EABI)
472   //   AArch64    Linux
473   return 32;
474 }
475 
476 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
477                                      const FunctionNoProtoType *fnType) const {
478   // The following conventions are known to require this to be false:
479   //   x86_stdcall
480   //   MIPS
481   // For everything else, we just prefer false unless we opt out.
482   return false;
483 }
484 
485 void
486 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
487                                              llvm::SmallString<24> &Opt) const {
488   // This assumes the user is passing a library name like "rt" instead of a
489   // filename like "librt.a/so", and that they don't care whether it's static or
490   // dynamic.
491   Opt = "-l";
492   Opt += Lib;
493 }
494 
495 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
496   // OpenCL kernels are called via an explicit runtime API with arguments
497   // set with clSetKernelArg(), not as normal sub-functions.
498   // Return SPIR_KERNEL by default as the kernel calling convention to
499   // ensure the fingerprint is fixed such way that each OpenCL argument
500   // gets one matching argument in the produced kernel function argument
501   // list to enable feasible implementation of clSetKernelArg() with
502   // aggregates etc. In case we would use the default C calling conv here,
503   // clSetKernelArg() might break depending on the target-specific
504   // conventions; different targets might split structs passed as values
505   // to multiple function arguments etc.
506   return llvm::CallingConv::SPIR_KERNEL;
507 }
508 
509 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
510     llvm::PointerType *T, QualType QT) const {
511   return llvm::ConstantPointerNull::get(T);
512 }
513 
514 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
515                                                    const VarDecl *D) const {
516   assert(!CGM.getLangOpts().OpenCL &&
517          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
518          "Address space agnostic languages only");
519   return D ? D->getType().getAddressSpace() : LangAS::Default;
520 }
521 
522 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
523     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
524     LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
525   // Since target may map different address spaces in AST to the same address
526   // space, an address space conversion may end up as a bitcast.
527   if (auto *C = dyn_cast<llvm::Constant>(Src))
528     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
529   // Try to preserve the source's name to make IR more readable.
530   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
531       Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
532 }
533 
534 llvm::Constant *
535 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
536                                         LangAS SrcAddr, LangAS DestAddr,
537                                         llvm::Type *DestTy) const {
538   // Since target may map different address spaces in AST to the same address
539   // space, an address space conversion may end up as a bitcast.
540   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
541 }
542 
543 llvm::SyncScope::ID
544 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
545                                       SyncScope Scope,
546                                       llvm::AtomicOrdering Ordering,
547                                       llvm::LLVMContext &Ctx) const {
548   return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
549 }
550 
551 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
552 
553 /// isEmptyField - Return true iff a the field is "empty", that is it
554 /// is an unnamed bit-field or an (array of) empty record(s).
555 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
556                          bool AllowArrays) {
557   if (FD->isUnnamedBitfield())
558     return true;
559 
560   QualType FT = FD->getType();
561 
562   // Constant arrays of empty records count as empty, strip them off.
563   // Constant arrays of zero length always count as empty.
564   bool WasArray = false;
565   if (AllowArrays)
566     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
567       if (AT->getSize() == 0)
568         return true;
569       FT = AT->getElementType();
570       // The [[no_unique_address]] special case below does not apply to
571       // arrays of C++ empty records, so we need to remember this fact.
572       WasArray = true;
573     }
574 
575   const RecordType *RT = FT->getAs<RecordType>();
576   if (!RT)
577     return false;
578 
579   // C++ record fields are never empty, at least in the Itanium ABI.
580   //
581   // FIXME: We should use a predicate for whether this behavior is true in the
582   // current ABI.
583   //
584   // The exception to the above rule are fields marked with the
585   // [[no_unique_address]] attribute (since C++20).  Those do count as empty
586   // according to the Itanium ABI.  The exception applies only to records,
587   // not arrays of records, so we must also check whether we stripped off an
588   // array type above.
589   if (isa<CXXRecordDecl>(RT->getDecl()) &&
590       (WasArray || !FD->hasAttr<NoUniqueAddressAttr>()))
591     return false;
592 
593   return isEmptyRecord(Context, FT, AllowArrays);
594 }
595 
596 /// isEmptyRecord - Return true iff a structure contains only empty
597 /// fields. Note that a structure with a flexible array member is not
598 /// considered empty.
599 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
600   const RecordType *RT = T->getAs<RecordType>();
601   if (!RT)
602     return false;
603   const RecordDecl *RD = RT->getDecl();
604   if (RD->hasFlexibleArrayMember())
605     return false;
606 
607   // If this is a C++ record, check the bases first.
608   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
609     for (const auto &I : CXXRD->bases())
610       if (!isEmptyRecord(Context, I.getType(), true))
611         return false;
612 
613   for (const auto *I : RD->fields())
614     if (!isEmptyField(Context, I, AllowArrays))
615       return false;
616   return true;
617 }
618 
619 /// isSingleElementStruct - Determine if a structure is a "single
620 /// element struct", i.e. it has exactly one non-empty field or
621 /// exactly one field which is itself a single element
622 /// struct. Structures with flexible array members are never
623 /// considered single element structs.
624 ///
625 /// \return The field declaration for the single non-empty field, if
626 /// it exists.
627 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
628   const RecordType *RT = T->getAs<RecordType>();
629   if (!RT)
630     return nullptr;
631 
632   const RecordDecl *RD = RT->getDecl();
633   if (RD->hasFlexibleArrayMember())
634     return nullptr;
635 
636   const Type *Found = nullptr;
637 
638   // If this is a C++ record, check the bases first.
639   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
640     for (const auto &I : CXXRD->bases()) {
641       // Ignore empty records.
642       if (isEmptyRecord(Context, I.getType(), true))
643         continue;
644 
645       // If we already found an element then this isn't a single-element struct.
646       if (Found)
647         return nullptr;
648 
649       // If this is non-empty and not a single element struct, the composite
650       // cannot be a single element struct.
651       Found = isSingleElementStruct(I.getType(), Context);
652       if (!Found)
653         return nullptr;
654     }
655   }
656 
657   // Check for single element.
658   for (const auto *FD : RD->fields()) {
659     QualType FT = FD->getType();
660 
661     // Ignore empty fields.
662     if (isEmptyField(Context, FD, true))
663       continue;
664 
665     // If we already found an element then this isn't a single-element
666     // struct.
667     if (Found)
668       return nullptr;
669 
670     // Treat single element arrays as the element.
671     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
672       if (AT->getSize().getZExtValue() != 1)
673         break;
674       FT = AT->getElementType();
675     }
676 
677     if (!isAggregateTypeForABI(FT)) {
678       Found = FT.getTypePtr();
679     } else {
680       Found = isSingleElementStruct(FT, Context);
681       if (!Found)
682         return nullptr;
683     }
684   }
685 
686   // We don't consider a struct a single-element struct if it has
687   // padding beyond the element type.
688   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
689     return nullptr;
690 
691   return Found;
692 }
693 
694 namespace {
695 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
696                        const ABIArgInfo &AI) {
697   // This default implementation defers to the llvm backend's va_arg
698   // instruction. It can handle only passing arguments directly
699   // (typically only handled in the backend for primitive types), or
700   // aggregates passed indirectly by pointer (NOTE: if the "byval"
701   // flag has ABI impact in the callee, this implementation cannot
702   // work.)
703 
704   // Only a few cases are covered here at the moment -- those needed
705   // by the default abi.
706   llvm::Value *Val;
707 
708   if (AI.isIndirect()) {
709     assert(!AI.getPaddingType() &&
710            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
711     assert(
712         !AI.getIndirectRealign() &&
713         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
714 
715     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
716     CharUnits TyAlignForABI = TyInfo.Align;
717 
718     llvm::Type *ElementTy = CGF.ConvertTypeForMem(Ty);
719     llvm::Type *BaseTy = llvm::PointerType::getUnqual(ElementTy);
720     llvm::Value *Addr =
721         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
722     return Address(Addr, ElementTy, TyAlignForABI);
723   } else {
724     assert((AI.isDirect() || AI.isExtend()) &&
725            "Unexpected ArgInfo Kind in generic VAArg emitter!");
726 
727     assert(!AI.getInReg() &&
728            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
729     assert(!AI.getPaddingType() &&
730            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
731     assert(!AI.getDirectOffset() &&
732            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
733     assert(!AI.getCoerceToType() &&
734            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
735 
736     Address Temp = CGF.CreateMemTemp(Ty, "varet");
737     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(),
738                                   CGF.ConvertTypeForMem(Ty));
739     CGF.Builder.CreateStore(Val, Temp);
740     return Temp;
741   }
742 }
743 
744 /// DefaultABIInfo - The default implementation for ABI specific
745 /// details. This implementation provides information which results in
746 /// self-consistent and sensible LLVM IR generation, but does not
747 /// conform to any particular ABI.
748 class DefaultABIInfo : public ABIInfo {
749 public:
750   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
751 
752   ABIArgInfo classifyReturnType(QualType RetTy) const;
753   ABIArgInfo classifyArgumentType(QualType RetTy) const;
754 
755   void computeInfo(CGFunctionInfo &FI) const override {
756     if (!getCXXABI().classifyReturnType(FI))
757       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
758     for (auto &I : FI.arguments())
759       I.info = classifyArgumentType(I.type);
760   }
761 
762   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
763                     QualType Ty) const override {
764     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
765   }
766 };
767 
768 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
769 public:
770   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
771       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
772 };
773 
774 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
775   Ty = useFirstFieldIfTransparentUnion(Ty);
776 
777   if (isAggregateTypeForABI(Ty)) {
778     // Records with non-trivial destructors/copy-constructors should not be
779     // passed by value.
780     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
781       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
782 
783     return getNaturalAlignIndirect(Ty);
784   }
785 
786   // Treat an enum type as its underlying type.
787   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
788     Ty = EnumTy->getDecl()->getIntegerType();
789 
790   ASTContext &Context = getContext();
791   if (const auto *EIT = Ty->getAs<BitIntType>())
792     if (EIT->getNumBits() >
793         Context.getTypeSize(Context.getTargetInfo().hasInt128Type()
794                                 ? Context.Int128Ty
795                                 : Context.LongLongTy))
796       return getNaturalAlignIndirect(Ty);
797 
798   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
799                                             : ABIArgInfo::getDirect());
800 }
801 
802 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
803   if (RetTy->isVoidType())
804     return ABIArgInfo::getIgnore();
805 
806   if (isAggregateTypeForABI(RetTy))
807     return getNaturalAlignIndirect(RetTy);
808 
809   // Treat an enum type as its underlying type.
810   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
811     RetTy = EnumTy->getDecl()->getIntegerType();
812 
813   if (const auto *EIT = RetTy->getAs<BitIntType>())
814     if (EIT->getNumBits() >
815         getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
816                                      ? getContext().Int128Ty
817                                      : getContext().LongLongTy))
818       return getNaturalAlignIndirect(RetTy);
819 
820   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
821                                                : ABIArgInfo::getDirect());
822 }
823 
824 //===----------------------------------------------------------------------===//
825 // WebAssembly ABI Implementation
826 //
827 // This is a very simple ABI that relies a lot on DefaultABIInfo.
828 //===----------------------------------------------------------------------===//
829 
830 class WebAssemblyABIInfo final : public ABIInfo {
831 public:
832   enum ABIKind {
833     MVP = 0,
834     ExperimentalMV = 1,
835   };
836 
837 private:
838   DefaultABIInfo defaultInfo;
839   ABIKind Kind;
840 
841 public:
842   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
843       : ABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {}
844 
845 private:
846   ABIArgInfo classifyReturnType(QualType RetTy) const;
847   ABIArgInfo classifyArgumentType(QualType Ty) const;
848 
849   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
850   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
851   // overload them.
852   void computeInfo(CGFunctionInfo &FI) const override {
853     if (!getCXXABI().classifyReturnType(FI))
854       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
855     for (auto &Arg : FI.arguments())
856       Arg.info = classifyArgumentType(Arg.type);
857   }
858 
859   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
860                     QualType Ty) const override;
861 };
862 
863 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
864 public:
865   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
866                                         WebAssemblyABIInfo::ABIKind K)
867       : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {
868     SwiftInfo =
869         std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
870   }
871 
872   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
873                            CodeGen::CodeGenModule &CGM) const override {
874     TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
875     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
876       if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
877         llvm::Function *Fn = cast<llvm::Function>(GV);
878         llvm::AttrBuilder B(GV->getContext());
879         B.addAttribute("wasm-import-module", Attr->getImportModule());
880         Fn->addFnAttrs(B);
881       }
882       if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
883         llvm::Function *Fn = cast<llvm::Function>(GV);
884         llvm::AttrBuilder B(GV->getContext());
885         B.addAttribute("wasm-import-name", Attr->getImportName());
886         Fn->addFnAttrs(B);
887       }
888       if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
889         llvm::Function *Fn = cast<llvm::Function>(GV);
890         llvm::AttrBuilder B(GV->getContext());
891         B.addAttribute("wasm-export-name", Attr->getExportName());
892         Fn->addFnAttrs(B);
893       }
894     }
895 
896     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
897       llvm::Function *Fn = cast<llvm::Function>(GV);
898       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
899         Fn->addFnAttr("no-prototype");
900     }
901   }
902 };
903 
904 /// Classify argument of given type \p Ty.
905 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
906   Ty = useFirstFieldIfTransparentUnion(Ty);
907 
908   if (isAggregateTypeForABI(Ty)) {
909     // Records with non-trivial destructors/copy-constructors should not be
910     // passed by value.
911     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
912       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
913     // Ignore empty structs/unions.
914     if (isEmptyRecord(getContext(), Ty, true))
915       return ABIArgInfo::getIgnore();
916     // Lower single-element structs to just pass a regular value. TODO: We
917     // could do reasonable-size multiple-element structs too, using getExpand(),
918     // though watch out for things like bitfields.
919     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
920       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
921     // For the experimental multivalue ABI, fully expand all other aggregates
922     if (Kind == ABIKind::ExperimentalMV) {
923       const RecordType *RT = Ty->getAs<RecordType>();
924       assert(RT);
925       bool HasBitField = false;
926       for (auto *Field : RT->getDecl()->fields()) {
927         if (Field->isBitField()) {
928           HasBitField = true;
929           break;
930         }
931       }
932       if (!HasBitField)
933         return ABIArgInfo::getExpand();
934     }
935   }
936 
937   // Otherwise just do the default thing.
938   return defaultInfo.classifyArgumentType(Ty);
939 }
940 
941 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
942   if (isAggregateTypeForABI(RetTy)) {
943     // Records with non-trivial destructors/copy-constructors should not be
944     // returned by value.
945     if (!getRecordArgABI(RetTy, getCXXABI())) {
946       // Ignore empty structs/unions.
947       if (isEmptyRecord(getContext(), RetTy, true))
948         return ABIArgInfo::getIgnore();
949       // Lower single-element structs to just return a regular value. TODO: We
950       // could do reasonable-size multiple-element structs too, using
951       // ABIArgInfo::getDirect().
952       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
953         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
954       // For the experimental multivalue ABI, return all other aggregates
955       if (Kind == ABIKind::ExperimentalMV)
956         return ABIArgInfo::getDirect();
957     }
958   }
959 
960   // Otherwise just do the default thing.
961   return defaultInfo.classifyReturnType(RetTy);
962 }
963 
964 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
965                                       QualType Ty) const {
966   bool IsIndirect = isAggregateTypeForABI(Ty) &&
967                     !isEmptyRecord(getContext(), Ty, true) &&
968                     !isSingleElementStruct(Ty, getContext());
969   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
970                           getContext().getTypeInfoInChars(Ty),
971                           CharUnits::fromQuantity(4),
972                           /*AllowHigherAlign=*/true);
973 }
974 
975 //===----------------------------------------------------------------------===//
976 // le32/PNaCl bitcode ABI Implementation
977 //
978 // This is a simplified version of the x86_32 ABI.  Arguments and return values
979 // are always passed on the stack.
980 //===----------------------------------------------------------------------===//
981 
982 class PNaClABIInfo : public ABIInfo {
983  public:
984   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
985 
986   ABIArgInfo classifyReturnType(QualType RetTy) const;
987   ABIArgInfo classifyArgumentType(QualType RetTy) const;
988 
989   void computeInfo(CGFunctionInfo &FI) const override;
990   Address EmitVAArg(CodeGenFunction &CGF,
991                     Address VAListAddr, QualType Ty) const override;
992 };
993 
994 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
995  public:
996    PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
997        : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {}
998 };
999 
1000 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
1001   if (!getCXXABI().classifyReturnType(FI))
1002     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
1003 
1004   for (auto &I : FI.arguments())
1005     I.info = classifyArgumentType(I.type);
1006 }
1007 
1008 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1009                                 QualType Ty) const {
1010   // The PNaCL ABI is a bit odd, in that varargs don't use normal
1011   // function classification. Structs get passed directly for varargs
1012   // functions, through a rewriting transform in
1013   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
1014   // this target to actually support a va_arg instructions with an
1015   // aggregate type, unlike other targets.
1016   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
1017 }
1018 
1019 /// Classify argument of given type \p Ty.
1020 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
1021   if (isAggregateTypeForABI(Ty)) {
1022     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
1023       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
1024     return getNaturalAlignIndirect(Ty);
1025   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
1026     // Treat an enum type as its underlying type.
1027     Ty = EnumTy->getDecl()->getIntegerType();
1028   } else if (Ty->isFloatingType()) {
1029     // Floating-point types don't go inreg.
1030     return ABIArgInfo::getDirect();
1031   } else if (const auto *EIT = Ty->getAs<BitIntType>()) {
1032     // Treat bit-precise integers as integers if <= 64, otherwise pass
1033     // indirectly.
1034     if (EIT->getNumBits() > 64)
1035       return getNaturalAlignIndirect(Ty);
1036     return ABIArgInfo::getDirect();
1037   }
1038 
1039   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
1040                                             : ABIArgInfo::getDirect());
1041 }
1042 
1043 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
1044   if (RetTy->isVoidType())
1045     return ABIArgInfo::getIgnore();
1046 
1047   // In the PNaCl ABI we always return records/structures on the stack.
1048   if (isAggregateTypeForABI(RetTy))
1049     return getNaturalAlignIndirect(RetTy);
1050 
1051   // Treat bit-precise integers as integers if <= 64, otherwise pass indirectly.
1052   if (const auto *EIT = RetTy->getAs<BitIntType>()) {
1053     if (EIT->getNumBits() > 64)
1054       return getNaturalAlignIndirect(RetTy);
1055     return ABIArgInfo::getDirect();
1056   }
1057 
1058   // Treat an enum type as its underlying type.
1059   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1060     RetTy = EnumTy->getDecl()->getIntegerType();
1061 
1062   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1063                                                : ABIArgInfo::getDirect());
1064 }
1065 
1066 /// IsX86_MMXType - Return true if this is an MMX type.
1067 bool IsX86_MMXType(llvm::Type *IRType) {
1068   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
1069   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
1070     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
1071     IRType->getScalarSizeInBits() != 64;
1072 }
1073 
1074 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1075                                           StringRef Constraint,
1076                                           llvm::Type* Ty) {
1077   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
1078                      .Cases("y", "&y", "^Ym", true)
1079                      .Default(false);
1080   if (IsMMXCons && Ty->isVectorTy()) {
1081     if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedValue() !=
1082         64) {
1083       // Invalid MMX constraint
1084       return nullptr;
1085     }
1086 
1087     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
1088   }
1089 
1090   // No operation needed
1091   return Ty;
1092 }
1093 
1094 /// Returns true if this type can be passed in SSE registers with the
1095 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1096 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
1097   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1098     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
1099       if (BT->getKind() == BuiltinType::LongDouble) {
1100         if (&Context.getTargetInfo().getLongDoubleFormat() ==
1101             &llvm::APFloat::x87DoubleExtended())
1102           return false;
1103       }
1104       return true;
1105     }
1106   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
1107     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
1108     // registers specially.
1109     unsigned VecSize = Context.getTypeSize(VT);
1110     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
1111       return true;
1112   }
1113   return false;
1114 }
1115 
1116 /// Returns true if this aggregate is small enough to be passed in SSE registers
1117 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
1118 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
1119   return NumMembers <= 4;
1120 }
1121 
1122 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
1123 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
1124   auto AI = ABIArgInfo::getDirect(T);
1125   AI.setInReg(true);
1126   AI.setCanBeFlattened(false);
1127   return AI;
1128 }
1129 
1130 //===----------------------------------------------------------------------===//
1131 // X86-32 ABI Implementation
1132 //===----------------------------------------------------------------------===//
1133 
1134 /// Similar to llvm::CCState, but for Clang.
1135 struct CCState {
1136   CCState(CGFunctionInfo &FI)
1137       : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {}
1138 
1139   llvm::SmallBitVector IsPreassigned;
1140   unsigned CC = CallingConv::CC_C;
1141   unsigned FreeRegs = 0;
1142   unsigned FreeSSERegs = 0;
1143 };
1144 
1145 /// X86_32ABIInfo - The X86-32 ABI information.
1146 class X86_32ABIInfo : public ABIInfo {
1147   enum Class {
1148     Integer,
1149     Float
1150   };
1151 
1152   static const unsigned MinABIStackAlignInBytes = 4;
1153 
1154   bool IsDarwinVectorABI;
1155   bool IsRetSmallStructInRegABI;
1156   bool IsWin32StructABI;
1157   bool IsSoftFloatABI;
1158   bool IsMCUABI;
1159   bool IsLinuxABI;
1160   unsigned DefaultNumRegisterParameters;
1161 
1162   static bool isRegisterSize(unsigned Size) {
1163     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
1164   }
1165 
1166   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1167     // FIXME: Assumes vectorcall is in use.
1168     return isX86VectorTypeForVectorCall(getContext(), Ty);
1169   }
1170 
1171   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1172                                          uint64_t NumMembers) const override {
1173     // FIXME: Assumes vectorcall is in use.
1174     return isX86VectorCallAggregateSmallEnough(NumMembers);
1175   }
1176 
1177   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
1178 
1179   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1180   /// such that the argument will be passed in memory.
1181   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
1182 
1183   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
1184 
1185   /// Return the alignment to use for the given type on the stack.
1186   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
1187 
1188   Class classify(QualType Ty) const;
1189   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
1190   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
1191 
1192   /// Updates the number of available free registers, returns
1193   /// true if any registers were allocated.
1194   bool updateFreeRegs(QualType Ty, CCState &State) const;
1195 
1196   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1197                                 bool &NeedsPadding) const;
1198   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1199 
1200   bool canExpandIndirectArgument(QualType Ty) const;
1201 
1202   /// Rewrite the function info so that all memory arguments use
1203   /// inalloca.
1204   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1205 
1206   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1207                            CharUnits &StackOffset, ABIArgInfo &Info,
1208                            QualType Type) const;
1209   void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const;
1210 
1211 public:
1212 
1213   void computeInfo(CGFunctionInfo &FI) const override;
1214   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1215                     QualType Ty) const override;
1216 
1217   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1218                 bool RetSmallStructInRegABI, bool Win32StructABI,
1219                 unsigned NumRegisterParameters, bool SoftFloatABI)
1220       : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1221         IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1222         IsWin32StructABI(Win32StructABI), IsSoftFloatABI(SoftFloatABI),
1223         IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1224         IsLinuxABI(CGT.getTarget().getTriple().isOSLinux() ||
1225                    CGT.getTarget().getTriple().isOSCygMing()),
1226         DefaultNumRegisterParameters(NumRegisterParameters) {}
1227 };
1228 
1229 class X86_32SwiftABIInfo : public SwiftABIInfo {
1230 public:
1231   explicit X86_32SwiftABIInfo(CodeGenTypes &CGT)
1232       : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/false) {}
1233 
1234   bool shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys,
1235                             bool AsReturnValue) const override {
1236     // LLVM's x86-32 lowering currently only assigns up to three
1237     // integer registers and three fp registers.  Oddly, it'll use up to
1238     // four vector registers for vectors, but those can overlap with the
1239     // scalar registers.
1240     return occupiesMoreThan(CGT, ComponentTys, /*total=*/3);
1241   }
1242 };
1243 
1244 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1245 public:
1246   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1247                           bool RetSmallStructInRegABI, bool Win32StructABI,
1248                           unsigned NumRegisterParameters, bool SoftFloatABI)
1249       : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>(
1250             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1251             NumRegisterParameters, SoftFloatABI)) {
1252     SwiftInfo = std::make_unique<X86_32SwiftABIInfo>(CGT);
1253   }
1254 
1255   static bool isStructReturnInRegABI(
1256       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1257 
1258   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1259                            CodeGen::CodeGenModule &CGM) const override;
1260 
1261   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1262     // Darwin uses different dwarf register numbers for EH.
1263     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1264     return 4;
1265   }
1266 
1267   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1268                                llvm::Value *Address) const override;
1269 
1270   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1271                                   StringRef Constraint,
1272                                   llvm::Type* Ty) const override {
1273     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1274   }
1275 
1276   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1277                                 std::string &Constraints,
1278                                 std::vector<llvm::Type *> &ResultRegTypes,
1279                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1280                                 std::vector<LValue> &ResultRegDests,
1281                                 std::string &AsmString,
1282                                 unsigned NumOutputs) const override;
1283 
1284   llvm::Constant *
1285   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1286     unsigned Sig = (0xeb << 0) |  // jmp rel8
1287                    (0x06 << 8) |  //           .+0x08
1288                    ('v' << 16) |
1289                    ('2' << 24);
1290     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1291   }
1292 
1293   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1294     return "movl\t%ebp, %ebp"
1295            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1296   }
1297 };
1298 
1299 }
1300 
1301 /// Rewrite input constraint references after adding some output constraints.
1302 /// In the case where there is one output and one input and we add one output,
1303 /// we need to replace all operand references greater than or equal to 1:
1304 ///     mov $0, $1
1305 ///     mov eax, $1
1306 /// The result will be:
1307 ///     mov $0, $2
1308 ///     mov eax, $2
1309 static void rewriteInputConstraintReferences(unsigned FirstIn,
1310                                              unsigned NumNewOuts,
1311                                              std::string &AsmString) {
1312   std::string Buf;
1313   llvm::raw_string_ostream OS(Buf);
1314   size_t Pos = 0;
1315   while (Pos < AsmString.size()) {
1316     size_t DollarStart = AsmString.find('$', Pos);
1317     if (DollarStart == std::string::npos)
1318       DollarStart = AsmString.size();
1319     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1320     if (DollarEnd == std::string::npos)
1321       DollarEnd = AsmString.size();
1322     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1323     Pos = DollarEnd;
1324     size_t NumDollars = DollarEnd - DollarStart;
1325     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1326       // We have an operand reference.
1327       size_t DigitStart = Pos;
1328       if (AsmString[DigitStart] == '{') {
1329         OS << '{';
1330         ++DigitStart;
1331       }
1332       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1333       if (DigitEnd == std::string::npos)
1334         DigitEnd = AsmString.size();
1335       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1336       unsigned OperandIndex;
1337       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1338         if (OperandIndex >= FirstIn)
1339           OperandIndex += NumNewOuts;
1340         OS << OperandIndex;
1341       } else {
1342         OS << OperandStr;
1343       }
1344       Pos = DigitEnd;
1345     }
1346   }
1347   AsmString = std::move(OS.str());
1348 }
1349 
1350 /// Add output constraints for EAX:EDX because they are return registers.
1351 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1352     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1353     std::vector<llvm::Type *> &ResultRegTypes,
1354     std::vector<llvm::Type *> &ResultTruncRegTypes,
1355     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1356     unsigned NumOutputs) const {
1357   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1358 
1359   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1360   // larger.
1361   if (!Constraints.empty())
1362     Constraints += ',';
1363   if (RetWidth <= 32) {
1364     Constraints += "={eax}";
1365     ResultRegTypes.push_back(CGF.Int32Ty);
1366   } else {
1367     // Use the 'A' constraint for EAX:EDX.
1368     Constraints += "=A";
1369     ResultRegTypes.push_back(CGF.Int64Ty);
1370   }
1371 
1372   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1373   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1374   ResultTruncRegTypes.push_back(CoerceTy);
1375 
1376   // Coerce the integer by bitcasting the return slot pointer.
1377   ReturnSlot.setAddress(
1378       CGF.Builder.CreateElementBitCast(ReturnSlot.getAddress(CGF), CoerceTy));
1379   ResultRegDests.push_back(ReturnSlot);
1380 
1381   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1382 }
1383 
1384 /// shouldReturnTypeInRegister - Determine if the given type should be
1385 /// returned in a register (for the Darwin and MCU ABI).
1386 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1387                                                ASTContext &Context) const {
1388   uint64_t Size = Context.getTypeSize(Ty);
1389 
1390   // For i386, type must be register sized.
1391   // For the MCU ABI, it only needs to be <= 8-byte
1392   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1393    return false;
1394 
1395   if (Ty->isVectorType()) {
1396     // 64- and 128- bit vectors inside structures are not returned in
1397     // registers.
1398     if (Size == 64 || Size == 128)
1399       return false;
1400 
1401     return true;
1402   }
1403 
1404   // If this is a builtin, pointer, enum, complex type, member pointer, or
1405   // member function pointer it is ok.
1406   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1407       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1408       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1409     return true;
1410 
1411   // Arrays are treated like records.
1412   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1413     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1414 
1415   // Otherwise, it must be a record type.
1416   const RecordType *RT = Ty->getAs<RecordType>();
1417   if (!RT) return false;
1418 
1419   // FIXME: Traverse bases here too.
1420 
1421   // Structure types are passed in register if all fields would be
1422   // passed in a register.
1423   for (const auto *FD : RT->getDecl()->fields()) {
1424     // Empty fields are ignored.
1425     if (isEmptyField(Context, FD, true))
1426       continue;
1427 
1428     // Check fields recursively.
1429     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1430       return false;
1431   }
1432   return true;
1433 }
1434 
1435 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1436   // Treat complex types as the element type.
1437   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1438     Ty = CTy->getElementType();
1439 
1440   // Check for a type which we know has a simple scalar argument-passing
1441   // convention without any padding.  (We're specifically looking for 32
1442   // and 64-bit integer and integer-equivalents, float, and double.)
1443   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1444       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1445     return false;
1446 
1447   uint64_t Size = Context.getTypeSize(Ty);
1448   return Size == 32 || Size == 64;
1449 }
1450 
1451 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1452                           uint64_t &Size) {
1453   for (const auto *FD : RD->fields()) {
1454     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1455     // argument is smaller than 32-bits, expanding the struct will create
1456     // alignment padding.
1457     if (!is32Or64BitBasicType(FD->getType(), Context))
1458       return false;
1459 
1460     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1461     // how to expand them yet, and the predicate for telling if a bitfield still
1462     // counts as "basic" is more complicated than what we were doing previously.
1463     if (FD->isBitField())
1464       return false;
1465 
1466     Size += Context.getTypeSize(FD->getType());
1467   }
1468   return true;
1469 }
1470 
1471 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1472                                  uint64_t &Size) {
1473   // Don't do this if there are any non-empty bases.
1474   for (const CXXBaseSpecifier &Base : RD->bases()) {
1475     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1476                               Size))
1477       return false;
1478   }
1479   if (!addFieldSizes(Context, RD, Size))
1480     return false;
1481   return true;
1482 }
1483 
1484 /// Test whether an argument type which is to be passed indirectly (on the
1485 /// stack) would have the equivalent layout if it was expanded into separate
1486 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1487 /// optimizations.
1488 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1489   // We can only expand structure types.
1490   const RecordType *RT = Ty->getAs<RecordType>();
1491   if (!RT)
1492     return false;
1493   const RecordDecl *RD = RT->getDecl();
1494   uint64_t Size = 0;
1495   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1496     if (!IsWin32StructABI) {
1497       // On non-Windows, we have to conservatively match our old bitcode
1498       // prototypes in order to be ABI-compatible at the bitcode level.
1499       if (!CXXRD->isCLike())
1500         return false;
1501     } else {
1502       // Don't do this for dynamic classes.
1503       if (CXXRD->isDynamicClass())
1504         return false;
1505     }
1506     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1507       return false;
1508   } else {
1509     if (!addFieldSizes(getContext(), RD, Size))
1510       return false;
1511   }
1512 
1513   // We can do this if there was no alignment padding.
1514   return Size == getContext().getTypeSize(Ty);
1515 }
1516 
1517 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1518   // If the return value is indirect, then the hidden argument is consuming one
1519   // integer register.
1520   if (State.FreeRegs) {
1521     --State.FreeRegs;
1522     if (!IsMCUABI)
1523       return getNaturalAlignIndirectInReg(RetTy);
1524   }
1525   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1526 }
1527 
1528 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1529                                              CCState &State) const {
1530   if (RetTy->isVoidType())
1531     return ABIArgInfo::getIgnore();
1532 
1533   const Type *Base = nullptr;
1534   uint64_t NumElts = 0;
1535   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1536        State.CC == llvm::CallingConv::X86_RegCall) &&
1537       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1538     // The LLVM struct type for such an aggregate should lower properly.
1539     return ABIArgInfo::getDirect();
1540   }
1541 
1542   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1543     // On Darwin, some vectors are returned in registers.
1544     if (IsDarwinVectorABI) {
1545       uint64_t Size = getContext().getTypeSize(RetTy);
1546 
1547       // 128-bit vectors are a special case; they are returned in
1548       // registers and we need to make sure to pick a type the LLVM
1549       // backend will like.
1550       if (Size == 128)
1551         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1552             llvm::Type::getInt64Ty(getVMContext()), 2));
1553 
1554       // Always return in register if it fits in a general purpose
1555       // register, or if it is 64 bits and has a single element.
1556       if ((Size == 8 || Size == 16 || Size == 32) ||
1557           (Size == 64 && VT->getNumElements() == 1))
1558         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1559                                                             Size));
1560 
1561       return getIndirectReturnResult(RetTy, State);
1562     }
1563 
1564     return ABIArgInfo::getDirect();
1565   }
1566 
1567   if (isAggregateTypeForABI(RetTy)) {
1568     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1569       // Structures with flexible arrays are always indirect.
1570       if (RT->getDecl()->hasFlexibleArrayMember())
1571         return getIndirectReturnResult(RetTy, State);
1572     }
1573 
1574     // If specified, structs and unions are always indirect.
1575     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1576       return getIndirectReturnResult(RetTy, State);
1577 
1578     // Ignore empty structs/unions.
1579     if (isEmptyRecord(getContext(), RetTy, true))
1580       return ABIArgInfo::getIgnore();
1581 
1582     // Return complex of _Float16 as <2 x half> so the backend will use xmm0.
1583     if (const ComplexType *CT = RetTy->getAs<ComplexType>()) {
1584       QualType ET = getContext().getCanonicalType(CT->getElementType());
1585       if (ET->isFloat16Type())
1586         return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
1587             llvm::Type::getHalfTy(getVMContext()), 2));
1588     }
1589 
1590     // Small structures which are register sized are generally returned
1591     // in a register.
1592     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1593       uint64_t Size = getContext().getTypeSize(RetTy);
1594 
1595       // As a special-case, if the struct is a "single-element" struct, and
1596       // the field is of type "float" or "double", return it in a
1597       // floating-point register. (MSVC does not apply this special case.)
1598       // We apply a similar transformation for pointer types to improve the
1599       // quality of the generated IR.
1600       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1601         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1602             || SeltTy->hasPointerRepresentation())
1603           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1604 
1605       // FIXME: We should be able to narrow this integer in cases with dead
1606       // padding.
1607       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1608     }
1609 
1610     return getIndirectReturnResult(RetTy, State);
1611   }
1612 
1613   // Treat an enum type as its underlying type.
1614   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1615     RetTy = EnumTy->getDecl()->getIntegerType();
1616 
1617   if (const auto *EIT = RetTy->getAs<BitIntType>())
1618     if (EIT->getNumBits() > 64)
1619       return getIndirectReturnResult(RetTy, State);
1620 
1621   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
1622                                                : ABIArgInfo::getDirect());
1623 }
1624 
1625 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) {
1626   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1627 }
1628 
1629 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) {
1630   const RecordType *RT = Ty->getAs<RecordType>();
1631   if (!RT)
1632     return false;
1633   const RecordDecl *RD = RT->getDecl();
1634 
1635   // If this is a C++ record, check the bases first.
1636   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1637     for (const auto &I : CXXRD->bases())
1638       if (!isRecordWithSIMDVectorType(Context, I.getType()))
1639         return false;
1640 
1641   for (const auto *i : RD->fields()) {
1642     QualType FT = i->getType();
1643 
1644     if (isSIMDVectorType(Context, FT))
1645       return true;
1646 
1647     if (isRecordWithSIMDVectorType(Context, FT))
1648       return true;
1649   }
1650 
1651   return false;
1652 }
1653 
1654 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1655                                                  unsigned Align) const {
1656   // Otherwise, if the alignment is less than or equal to the minimum ABI
1657   // alignment, just use the default; the backend will handle this.
1658   if (Align <= MinABIStackAlignInBytes)
1659     return 0; // Use default alignment.
1660 
1661   if (IsLinuxABI) {
1662     // Exclude other System V OS (e.g Darwin, PS4 and FreeBSD) since we don't
1663     // want to spend any effort dealing with the ramifications of ABI breaks.
1664     //
1665     // If the vector type is __m128/__m256/__m512, return the default alignment.
1666     if (Ty->isVectorType() && (Align == 16 || Align == 32 || Align == 64))
1667       return Align;
1668   }
1669   // On non-Darwin, the stack type alignment is always 4.
1670   if (!IsDarwinVectorABI) {
1671     // Set explicit alignment, since we may need to realign the top.
1672     return MinABIStackAlignInBytes;
1673   }
1674 
1675   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1676   if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) ||
1677                       isRecordWithSIMDVectorType(getContext(), Ty)))
1678     return 16;
1679 
1680   return MinABIStackAlignInBytes;
1681 }
1682 
1683 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1684                                             CCState &State) const {
1685   if (!ByVal) {
1686     if (State.FreeRegs) {
1687       --State.FreeRegs; // Non-byval indirects just use one pointer.
1688       if (!IsMCUABI)
1689         return getNaturalAlignIndirectInReg(Ty);
1690     }
1691     return getNaturalAlignIndirect(Ty, false);
1692   }
1693 
1694   // Compute the byval alignment.
1695   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1696   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1697   if (StackAlign == 0)
1698     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1699 
1700   // If the stack alignment is less than the type alignment, realign the
1701   // argument.
1702   bool Realign = TypeAlign > StackAlign;
1703   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1704                                  /*ByVal=*/true, Realign);
1705 }
1706 
1707 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1708   const Type *T = isSingleElementStruct(Ty, getContext());
1709   if (!T)
1710     T = Ty.getTypePtr();
1711 
1712   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1713     BuiltinType::Kind K = BT->getKind();
1714     if (K == BuiltinType::Float || K == BuiltinType::Double)
1715       return Float;
1716   }
1717   return Integer;
1718 }
1719 
1720 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1721   if (!IsSoftFloatABI) {
1722     Class C = classify(Ty);
1723     if (C == Float)
1724       return false;
1725   }
1726 
1727   unsigned Size = getContext().getTypeSize(Ty);
1728   unsigned SizeInRegs = (Size + 31) / 32;
1729 
1730   if (SizeInRegs == 0)
1731     return false;
1732 
1733   if (!IsMCUABI) {
1734     if (SizeInRegs > State.FreeRegs) {
1735       State.FreeRegs = 0;
1736       return false;
1737     }
1738   } else {
1739     // The MCU psABI allows passing parameters in-reg even if there are
1740     // earlier parameters that are passed on the stack. Also,
1741     // it does not allow passing >8-byte structs in-register,
1742     // even if there are 3 free registers available.
1743     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1744       return false;
1745   }
1746 
1747   State.FreeRegs -= SizeInRegs;
1748   return true;
1749 }
1750 
1751 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1752                                              bool &InReg,
1753                                              bool &NeedsPadding) const {
1754   // On Windows, aggregates other than HFAs are never passed in registers, and
1755   // they do not consume register slots. Homogenous floating-point aggregates
1756   // (HFAs) have already been dealt with at this point.
1757   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1758     return false;
1759 
1760   NeedsPadding = false;
1761   InReg = !IsMCUABI;
1762 
1763   if (!updateFreeRegs(Ty, State))
1764     return false;
1765 
1766   if (IsMCUABI)
1767     return true;
1768 
1769   if (State.CC == llvm::CallingConv::X86_FastCall ||
1770       State.CC == llvm::CallingConv::X86_VectorCall ||
1771       State.CC == llvm::CallingConv::X86_RegCall) {
1772     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1773       NeedsPadding = true;
1774 
1775     return false;
1776   }
1777 
1778   return true;
1779 }
1780 
1781 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1782   bool IsPtrOrInt = (getContext().getTypeSize(Ty) <= 32) &&
1783                     (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1784                      Ty->isReferenceType());
1785 
1786   if (!IsPtrOrInt && (State.CC == llvm::CallingConv::X86_FastCall ||
1787                       State.CC == llvm::CallingConv::X86_VectorCall))
1788     return false;
1789 
1790   if (!updateFreeRegs(Ty, State))
1791     return false;
1792 
1793   if (!IsPtrOrInt && State.CC == llvm::CallingConv::X86_RegCall)
1794     return false;
1795 
1796   // Return true to apply inreg to all legal parameters except for MCU targets.
1797   return !IsMCUABI;
1798 }
1799 
1800 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const {
1801   // Vectorcall x86 works subtly different than in x64, so the format is
1802   // a bit different than the x64 version.  First, all vector types (not HVAs)
1803   // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers.
1804   // This differs from the x64 implementation, where the first 6 by INDEX get
1805   // registers.
1806   // In the second pass over the arguments, HVAs are passed in the remaining
1807   // vector registers if possible, or indirectly by address. The address will be
1808   // passed in ECX/EDX if available. Any other arguments are passed according to
1809   // the usual fastcall rules.
1810   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
1811   for (int I = 0, E = Args.size(); I < E; ++I) {
1812     const Type *Base = nullptr;
1813     uint64_t NumElts = 0;
1814     const QualType &Ty = Args[I].type;
1815     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1816         isHomogeneousAggregate(Ty, Base, NumElts)) {
1817       if (State.FreeSSERegs >= NumElts) {
1818         State.FreeSSERegs -= NumElts;
1819         Args[I].info = ABIArgInfo::getDirectInReg();
1820         State.IsPreassigned.set(I);
1821       }
1822     }
1823   }
1824 }
1825 
1826 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1827                                                CCState &State) const {
1828   // FIXME: Set alignment on indirect arguments.
1829   bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall;
1830   bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall;
1831   bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall;
1832 
1833   Ty = useFirstFieldIfTransparentUnion(Ty);
1834   TypeInfo TI = getContext().getTypeInfo(Ty);
1835 
1836   // Check with the C++ ABI first.
1837   const RecordType *RT = Ty->getAs<RecordType>();
1838   if (RT) {
1839     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1840     if (RAA == CGCXXABI::RAA_Indirect) {
1841       return getIndirectResult(Ty, false, State);
1842     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1843       // The field index doesn't matter, we'll fix it up later.
1844       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1845     }
1846   }
1847 
1848   // Regcall uses the concept of a homogenous vector aggregate, similar
1849   // to other targets.
1850   const Type *Base = nullptr;
1851   uint64_t NumElts = 0;
1852   if ((IsRegCall || IsVectorCall) &&
1853       isHomogeneousAggregate(Ty, Base, NumElts)) {
1854     if (State.FreeSSERegs >= NumElts) {
1855       State.FreeSSERegs -= NumElts;
1856 
1857       // Vectorcall passes HVAs directly and does not flatten them, but regcall
1858       // does.
1859       if (IsVectorCall)
1860         return getDirectX86Hva();
1861 
1862       if (Ty->isBuiltinType() || Ty->isVectorType())
1863         return ABIArgInfo::getDirect();
1864       return ABIArgInfo::getExpand();
1865     }
1866     return getIndirectResult(Ty, /*ByVal=*/false, State);
1867   }
1868 
1869   if (isAggregateTypeForABI(Ty)) {
1870     // Structures with flexible arrays are always indirect.
1871     // FIXME: This should not be byval!
1872     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1873       return getIndirectResult(Ty, true, State);
1874 
1875     // Ignore empty structs/unions on non-Windows.
1876     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1877       return ABIArgInfo::getIgnore();
1878 
1879     llvm::LLVMContext &LLVMContext = getVMContext();
1880     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1881     bool NeedsPadding = false;
1882     bool InReg;
1883     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1884       unsigned SizeInRegs = (TI.Width + 31) / 32;
1885       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1886       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1887       if (InReg)
1888         return ABIArgInfo::getDirectInReg(Result);
1889       else
1890         return ABIArgInfo::getDirect(Result);
1891     }
1892     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1893 
1894     // Pass over-aligned aggregates on Windows indirectly. This behavior was
1895     // added in MSVC 2015.
1896     if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32)
1897       return getIndirectResult(Ty, /*ByVal=*/false, State);
1898 
1899     // Expand small (<= 128-bit) record types when we know that the stack layout
1900     // of those arguments will match the struct. This is important because the
1901     // LLVM backend isn't smart enough to remove byval, which inhibits many
1902     // optimizations.
1903     // Don't do this for the MCU if there are still free integer registers
1904     // (see X86_64 ABI for full explanation).
1905     if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) &&
1906         canExpandIndirectArgument(Ty))
1907       return ABIArgInfo::getExpandWithPadding(
1908           IsFastCall || IsVectorCall || IsRegCall, PaddingType);
1909 
1910     return getIndirectResult(Ty, true, State);
1911   }
1912 
1913   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1914     // On Windows, vectors are passed directly if registers are available, or
1915     // indirectly if not. This avoids the need to align argument memory. Pass
1916     // user-defined vector types larger than 512 bits indirectly for simplicity.
1917     if (IsWin32StructABI) {
1918       if (TI.Width <= 512 && State.FreeSSERegs > 0) {
1919         --State.FreeSSERegs;
1920         return ABIArgInfo::getDirectInReg();
1921       }
1922       return getIndirectResult(Ty, /*ByVal=*/false, State);
1923     }
1924 
1925     // On Darwin, some vectors are passed in memory, we handle this by passing
1926     // it as an i8/i16/i32/i64.
1927     if (IsDarwinVectorABI) {
1928       if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) ||
1929           (TI.Width == 64 && VT->getNumElements() == 1))
1930         return ABIArgInfo::getDirect(
1931             llvm::IntegerType::get(getVMContext(), TI.Width));
1932     }
1933 
1934     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1935       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1936 
1937     return ABIArgInfo::getDirect();
1938   }
1939 
1940 
1941   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1942     Ty = EnumTy->getDecl()->getIntegerType();
1943 
1944   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1945 
1946   if (isPromotableIntegerTypeForABI(Ty)) {
1947     if (InReg)
1948       return ABIArgInfo::getExtendInReg(Ty);
1949     return ABIArgInfo::getExtend(Ty);
1950   }
1951 
1952   if (const auto *EIT = Ty->getAs<BitIntType>()) {
1953     if (EIT->getNumBits() <= 64) {
1954       if (InReg)
1955         return ABIArgInfo::getDirectInReg();
1956       return ABIArgInfo::getDirect();
1957     }
1958     return getIndirectResult(Ty, /*ByVal=*/false, State);
1959   }
1960 
1961   if (InReg)
1962     return ABIArgInfo::getDirectInReg();
1963   return ABIArgInfo::getDirect();
1964 }
1965 
1966 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1967   CCState State(FI);
1968   if (IsMCUABI)
1969     State.FreeRegs = 3;
1970   else if (State.CC == llvm::CallingConv::X86_FastCall) {
1971     State.FreeRegs = 2;
1972     State.FreeSSERegs = 3;
1973   } else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1974     State.FreeRegs = 2;
1975     State.FreeSSERegs = 6;
1976   } else if (FI.getHasRegParm())
1977     State.FreeRegs = FI.getRegParm();
1978   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1979     State.FreeRegs = 5;
1980     State.FreeSSERegs = 8;
1981   } else if (IsWin32StructABI) {
1982     // Since MSVC 2015, the first three SSE vectors have been passed in
1983     // registers. The rest are passed indirectly.
1984     State.FreeRegs = DefaultNumRegisterParameters;
1985     State.FreeSSERegs = 3;
1986   } else
1987     State.FreeRegs = DefaultNumRegisterParameters;
1988 
1989   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
1990     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1991   } else if (FI.getReturnInfo().isIndirect()) {
1992     // The C++ ABI is not aware of register usage, so we have to check if the
1993     // return value was sret and put it in a register ourselves if appropriate.
1994     if (State.FreeRegs) {
1995       --State.FreeRegs;  // The sret parameter consumes a register.
1996       if (!IsMCUABI)
1997         FI.getReturnInfo().setInReg(true);
1998     }
1999   }
2000 
2001   // The chain argument effectively gives us another free register.
2002   if (FI.isChainCall())
2003     ++State.FreeRegs;
2004 
2005   // For vectorcall, do a first pass over the arguments, assigning FP and vector
2006   // arguments to XMM registers as available.
2007   if (State.CC == llvm::CallingConv::X86_VectorCall)
2008     runVectorCallFirstPass(FI, State);
2009 
2010   bool UsedInAlloca = false;
2011   MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
2012   for (int I = 0, E = Args.size(); I < E; ++I) {
2013     // Skip arguments that have already been assigned.
2014     if (State.IsPreassigned.test(I))
2015       continue;
2016 
2017     Args[I].info = classifyArgumentType(Args[I].type, State);
2018     UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca);
2019   }
2020 
2021   // If we needed to use inalloca for any argument, do a second pass and rewrite
2022   // all the memory arguments to use inalloca.
2023   if (UsedInAlloca)
2024     rewriteWithInAlloca(FI);
2025 }
2026 
2027 void
2028 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
2029                                    CharUnits &StackOffset, ABIArgInfo &Info,
2030                                    QualType Type) const {
2031   // Arguments are always 4-byte-aligned.
2032   CharUnits WordSize = CharUnits::fromQuantity(4);
2033   assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct");
2034 
2035   // sret pointers and indirect things will require an extra pointer
2036   // indirection, unless they are byval. Most things are byval, and will not
2037   // require this indirection.
2038   bool IsIndirect = false;
2039   if (Info.isIndirect() && !Info.getIndirectByVal())
2040     IsIndirect = true;
2041   Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect);
2042   llvm::Type *LLTy = CGT.ConvertTypeForMem(Type);
2043   if (IsIndirect)
2044     LLTy = LLTy->getPointerTo(0);
2045   FrameFields.push_back(LLTy);
2046   StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type);
2047 
2048   // Insert padding bytes to respect alignment.
2049   CharUnits FieldEnd = StackOffset;
2050   StackOffset = FieldEnd.alignTo(WordSize);
2051   if (StackOffset != FieldEnd) {
2052     CharUnits NumBytes = StackOffset - FieldEnd;
2053     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
2054     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
2055     FrameFields.push_back(Ty);
2056   }
2057 }
2058 
2059 static bool isArgInAlloca(const ABIArgInfo &Info) {
2060   // Leave ignored and inreg arguments alone.
2061   switch (Info.getKind()) {
2062   case ABIArgInfo::InAlloca:
2063     return true;
2064   case ABIArgInfo::Ignore:
2065   case ABIArgInfo::IndirectAliased:
2066     return false;
2067   case ABIArgInfo::Indirect:
2068   case ABIArgInfo::Direct:
2069   case ABIArgInfo::Extend:
2070     return !Info.getInReg();
2071   case ABIArgInfo::Expand:
2072   case ABIArgInfo::CoerceAndExpand:
2073     // These are aggregate types which are never passed in registers when
2074     // inalloca is involved.
2075     return true;
2076   }
2077   llvm_unreachable("invalid enum");
2078 }
2079 
2080 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
2081   assert(IsWin32StructABI && "inalloca only supported on win32");
2082 
2083   // Build a packed struct type for all of the arguments in memory.
2084   SmallVector<llvm::Type *, 6> FrameFields;
2085 
2086   // The stack alignment is always 4.
2087   CharUnits StackAlign = CharUnits::fromQuantity(4);
2088 
2089   CharUnits StackOffset;
2090   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
2091 
2092   // Put 'this' into the struct before 'sret', if necessary.
2093   bool IsThisCall =
2094       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
2095   ABIArgInfo &Ret = FI.getReturnInfo();
2096   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
2097       isArgInAlloca(I->info)) {
2098     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2099     ++I;
2100   }
2101 
2102   // Put the sret parameter into the inalloca struct if it's in memory.
2103   if (Ret.isIndirect() && !Ret.getInReg()) {
2104     addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType());
2105     // On Windows, the hidden sret parameter is always returned in eax.
2106     Ret.setInAllocaSRet(IsWin32StructABI);
2107   }
2108 
2109   // Skip the 'this' parameter in ecx.
2110   if (IsThisCall)
2111     ++I;
2112 
2113   // Put arguments passed in memory into the struct.
2114   for (; I != E; ++I) {
2115     if (isArgInAlloca(I->info))
2116       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
2117   }
2118 
2119   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
2120                                         /*isPacked=*/true),
2121                   StackAlign);
2122 }
2123 
2124 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
2125                                  Address VAListAddr, QualType Ty) const {
2126 
2127   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
2128 
2129   // x86-32 changes the alignment of certain arguments on the stack.
2130   //
2131   // Just messing with TypeInfo like this works because we never pass
2132   // anything indirectly.
2133   TypeInfo.Align = CharUnits::fromQuantity(
2134                 getTypeStackAlignInBytes(Ty, TypeInfo.Align.getQuantity()));
2135 
2136   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
2137                           TypeInfo, CharUnits::fromQuantity(4),
2138                           /*AllowHigherAlign*/ true);
2139 }
2140 
2141 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
2142     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
2143   assert(Triple.getArch() == llvm::Triple::x86);
2144 
2145   switch (Opts.getStructReturnConvention()) {
2146   case CodeGenOptions::SRCK_Default:
2147     break;
2148   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
2149     return false;
2150   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
2151     return true;
2152   }
2153 
2154   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
2155     return true;
2156 
2157   switch (Triple.getOS()) {
2158   case llvm::Triple::DragonFly:
2159   case llvm::Triple::FreeBSD:
2160   case llvm::Triple::OpenBSD:
2161   case llvm::Triple::Win32:
2162     return true;
2163   default:
2164     return false;
2165   }
2166 }
2167 
2168 static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
2169                                  CodeGen::CodeGenModule &CGM) {
2170   if (!FD->hasAttr<AnyX86InterruptAttr>())
2171     return;
2172 
2173   llvm::Function *Fn = cast<llvm::Function>(GV);
2174   Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2175   if (FD->getNumParams() == 0)
2176     return;
2177 
2178   auto PtrTy = cast<PointerType>(FD->getParamDecl(0)->getType());
2179   llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
2180   llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
2181     Fn->getContext(), ByValTy);
2182   Fn->addParamAttr(0, NewAttr);
2183 }
2184 
2185 void X86_32TargetCodeGenInfo::setTargetAttributes(
2186     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2187   if (GV->isDeclaration())
2188     return;
2189   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2190     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2191       llvm::Function *Fn = cast<llvm::Function>(GV);
2192       Fn->addFnAttr("stackrealign");
2193     }
2194 
2195     addX86InterruptAttrs(FD, GV, CGM);
2196   }
2197 }
2198 
2199 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
2200                                                CodeGen::CodeGenFunction &CGF,
2201                                                llvm::Value *Address) const {
2202   CodeGen::CGBuilderTy &Builder = CGF.Builder;
2203 
2204   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
2205 
2206   // 0-7 are the eight integer registers;  the order is different
2207   //   on Darwin (for EH), but the range is the same.
2208   // 8 is %eip.
2209   AssignToArrayRange(Builder, Address, Four8, 0, 8);
2210 
2211   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
2212     // 12-16 are st(0..4).  Not sure why we stop at 4.
2213     // These have size 16, which is sizeof(long double) on
2214     // platforms with 8-byte alignment for that type.
2215     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
2216     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
2217 
2218   } else {
2219     // 9 is %eflags, which doesn't get a size on Darwin for some
2220     // reason.
2221     Builder.CreateAlignedStore(
2222         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
2223                                CharUnits::One());
2224 
2225     // 11-16 are st(0..5).  Not sure why we stop at 5.
2226     // These have size 12, which is sizeof(long double) on
2227     // platforms with 4-byte alignment for that type.
2228     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
2229     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
2230   }
2231 
2232   return false;
2233 }
2234 
2235 //===----------------------------------------------------------------------===//
2236 // X86-64 ABI Implementation
2237 //===----------------------------------------------------------------------===//
2238 
2239 
2240 namespace {
2241 /// The AVX ABI level for X86 targets.
2242 enum class X86AVXABILevel {
2243   None,
2244   AVX,
2245   AVX512
2246 };
2247 
2248 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2249 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2250   switch (AVXLevel) {
2251   case X86AVXABILevel::AVX512:
2252     return 512;
2253   case X86AVXABILevel::AVX:
2254     return 256;
2255   case X86AVXABILevel::None:
2256     return 128;
2257   }
2258   llvm_unreachable("Unknown AVXLevel");
2259 }
2260 
2261 /// X86_64ABIInfo - The X86_64 ABI information.
2262 class X86_64ABIInfo : public ABIInfo {
2263   enum Class {
2264     Integer = 0,
2265     SSE,
2266     SSEUp,
2267     X87,
2268     X87Up,
2269     ComplexX87,
2270     NoClass,
2271     Memory
2272   };
2273 
2274   /// merge - Implement the X86_64 ABI merging algorithm.
2275   ///
2276   /// Merge an accumulating classification \arg Accum with a field
2277   /// classification \arg Field.
2278   ///
2279   /// \param Accum - The accumulating classification. This should
2280   /// always be either NoClass or the result of a previous merge
2281   /// call. In addition, this should never be Memory (the caller
2282   /// should just return Memory for the aggregate).
2283   static Class merge(Class Accum, Class Field);
2284 
2285   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2286   ///
2287   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2288   /// final MEMORY or SSE classes when necessary.
2289   ///
2290   /// \param AggregateSize - The size of the current aggregate in
2291   /// the classification process.
2292   ///
2293   /// \param Lo - The classification for the parts of the type
2294   /// residing in the low word of the containing object.
2295   ///
2296   /// \param Hi - The classification for the parts of the type
2297   /// residing in the higher words of the containing object.
2298   ///
2299   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2300 
2301   /// classify - Determine the x86_64 register classes in which the
2302   /// given type T should be passed.
2303   ///
2304   /// \param Lo - The classification for the parts of the type
2305   /// residing in the low word of the containing object.
2306   ///
2307   /// \param Hi - The classification for the parts of the type
2308   /// residing in the high word of the containing object.
2309   ///
2310   /// \param OffsetBase - The bit offset of this type in the
2311   /// containing object.  Some parameters are classified different
2312   /// depending on whether they straddle an eightbyte boundary.
2313   ///
2314   /// \param isNamedArg - Whether the argument in question is a "named"
2315   /// argument, as used in AMD64-ABI 3.5.7.
2316   ///
2317   /// \param IsRegCall - Whether the calling conversion is regcall.
2318   ///
2319   /// If a word is unused its result will be NoClass; if a type should
2320   /// be passed in Memory then at least the classification of \arg Lo
2321   /// will be Memory.
2322   ///
2323   /// The \arg Lo class will be NoClass iff the argument is ignored.
2324   ///
2325   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2326   /// also be ComplexX87.
2327   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2328                 bool isNamedArg, bool IsRegCall = false) const;
2329 
2330   llvm::Type *GetByteVectorType(QualType Ty) const;
2331   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2332                                  unsigned IROffset, QualType SourceTy,
2333                                  unsigned SourceOffset) const;
2334   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2335                                      unsigned IROffset, QualType SourceTy,
2336                                      unsigned SourceOffset) const;
2337 
2338   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2339   /// such that the argument will be returned in memory.
2340   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2341 
2342   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2343   /// such that the argument will be passed in memory.
2344   ///
2345   /// \param freeIntRegs - The number of free integer registers remaining
2346   /// available.
2347   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2348 
2349   ABIArgInfo classifyReturnType(QualType RetTy) const;
2350 
2351   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2352                                   unsigned &neededInt, unsigned &neededSSE,
2353                                   bool isNamedArg,
2354                                   bool IsRegCall = false) const;
2355 
2356   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2357                                        unsigned &NeededSSE,
2358                                        unsigned &MaxVectorWidth) const;
2359 
2360   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2361                                            unsigned &NeededSSE,
2362                                            unsigned &MaxVectorWidth) const;
2363 
2364   bool IsIllegalVectorType(QualType Ty) const;
2365 
2366   /// The 0.98 ABI revision clarified a lot of ambiguities,
2367   /// unfortunately in ways that were not always consistent with
2368   /// certain previous compilers.  In particular, platforms which
2369   /// required strict binary compatibility with older versions of GCC
2370   /// may need to exempt themselves.
2371   bool honorsRevision0_98() const {
2372     return !getTarget().getTriple().isOSDarwin();
2373   }
2374 
2375   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2376   /// classify it as INTEGER (for compatibility with older clang compilers).
2377   bool classifyIntegerMMXAsSSE() const {
2378     // Clang <= 3.8 did not do this.
2379     if (getContext().getLangOpts().getClangABICompat() <=
2380         LangOptions::ClangABI::Ver3_8)
2381       return false;
2382 
2383     const llvm::Triple &Triple = getTarget().getTriple();
2384     if (Triple.isOSDarwin() || Triple.isPS())
2385       return false;
2386     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2387       return false;
2388     return true;
2389   }
2390 
2391   // GCC classifies vectors of __int128 as memory.
2392   bool passInt128VectorsInMem() const {
2393     // Clang <= 9.0 did not do this.
2394     if (getContext().getLangOpts().getClangABICompat() <=
2395         LangOptions::ClangABI::Ver9)
2396       return false;
2397 
2398     const llvm::Triple &T = getTarget().getTriple();
2399     return T.isOSLinux() || T.isOSNetBSD();
2400   }
2401 
2402   X86AVXABILevel AVXLevel;
2403   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2404   // 64-bit hardware.
2405   bool Has64BitPointers;
2406 
2407 public:
2408   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2409       : ABIInfo(CGT), AVXLevel(AVXLevel),
2410         Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {}
2411 
2412   bool isPassedUsingAVXType(QualType type) const {
2413     unsigned neededInt, neededSSE;
2414     // The freeIntRegs argument doesn't matter here.
2415     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2416                                            /*isNamedArg*/true);
2417     if (info.isDirect()) {
2418       llvm::Type *ty = info.getCoerceToType();
2419       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2420         return vectorTy->getPrimitiveSizeInBits().getFixedValue() > 128;
2421     }
2422     return false;
2423   }
2424 
2425   void computeInfo(CGFunctionInfo &FI) const override;
2426 
2427   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2428                     QualType Ty) const override;
2429   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2430                       QualType Ty) const override;
2431 
2432   bool has64BitPointers() const {
2433     return Has64BitPointers;
2434   }
2435 };
2436 
2437 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2438 class WinX86_64ABIInfo : public ABIInfo {
2439 public:
2440   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2441       : ABIInfo(CGT), AVXLevel(AVXLevel),
2442         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2443 
2444   void computeInfo(CGFunctionInfo &FI) const override;
2445 
2446   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2447                     QualType Ty) const override;
2448 
2449   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2450     // FIXME: Assumes vectorcall is in use.
2451     return isX86VectorTypeForVectorCall(getContext(), Ty);
2452   }
2453 
2454   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2455                                          uint64_t NumMembers) const override {
2456     // FIXME: Assumes vectorcall is in use.
2457     return isX86VectorCallAggregateSmallEnough(NumMembers);
2458   }
2459 
2460 private:
2461   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2462                       bool IsVectorCall, bool IsRegCall) const;
2463   ABIArgInfo reclassifyHvaArgForVectorCall(QualType Ty, unsigned &FreeSSERegs,
2464                                            const ABIArgInfo &current) const;
2465 
2466   X86AVXABILevel AVXLevel;
2467 
2468   bool IsMingw64;
2469 };
2470 
2471 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2472 public:
2473   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2474       : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {
2475     SwiftInfo =
2476         std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/true);
2477   }
2478 
2479   const X86_64ABIInfo &getABIInfo() const {
2480     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2481   }
2482 
2483   /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
2484   /// autoreleaseRV/retainRV and autoreleaseRV/unsafeClaimRV optimizations.
2485   bool markARCOptimizedReturnCallsAsNoTail() const override { return true; }
2486 
2487   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2488     return 7;
2489   }
2490 
2491   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2492                                llvm::Value *Address) const override {
2493     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2494 
2495     // 0-15 are the 16 integer registers.
2496     // 16 is %rip.
2497     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2498     return false;
2499   }
2500 
2501   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2502                                   StringRef Constraint,
2503                                   llvm::Type* Ty) const override {
2504     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2505   }
2506 
2507   bool isNoProtoCallVariadic(const CallArgList &args,
2508                              const FunctionNoProtoType *fnType) const override {
2509     // The default CC on x86-64 sets %al to the number of SSA
2510     // registers used, and GCC sets this when calling an unprototyped
2511     // function, so we override the default behavior.  However, don't do
2512     // that when AVX types are involved: the ABI explicitly states it is
2513     // undefined, and it doesn't work in practice because of how the ABI
2514     // defines varargs anyway.
2515     if (fnType->getCallConv() == CC_C) {
2516       bool HasAVXType = false;
2517       for (CallArgList::const_iterator
2518              it = args.begin(), ie = args.end(); it != ie; ++it) {
2519         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2520           HasAVXType = true;
2521           break;
2522         }
2523       }
2524 
2525       if (!HasAVXType)
2526         return true;
2527     }
2528 
2529     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2530   }
2531 
2532   llvm::Constant *
2533   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2534     unsigned Sig = (0xeb << 0) | // jmp rel8
2535                    (0x06 << 8) | //           .+0x08
2536                    ('v' << 16) |
2537                    ('2' << 24);
2538     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2539   }
2540 
2541   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2542                            CodeGen::CodeGenModule &CGM) const override {
2543     if (GV->isDeclaration())
2544       return;
2545     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2546       if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2547         llvm::Function *Fn = cast<llvm::Function>(GV);
2548         Fn->addFnAttr("stackrealign");
2549       }
2550 
2551       addX86InterruptAttrs(FD, GV, CGM);
2552     }
2553   }
2554 
2555   void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
2556                             const FunctionDecl *Caller,
2557                             const FunctionDecl *Callee,
2558                             const CallArgList &Args) const override;
2559 };
2560 
2561 static void initFeatureMaps(const ASTContext &Ctx,
2562                             llvm::StringMap<bool> &CallerMap,
2563                             const FunctionDecl *Caller,
2564                             llvm::StringMap<bool> &CalleeMap,
2565                             const FunctionDecl *Callee) {
2566   if (CalleeMap.empty() && CallerMap.empty()) {
2567     // The caller is potentially nullptr in the case where the call isn't in a
2568     // function.  In this case, the getFunctionFeatureMap ensures we just get
2569     // the TU level setting (since it cannot be modified by 'target'..
2570     Ctx.getFunctionFeatureMap(CallerMap, Caller);
2571     Ctx.getFunctionFeatureMap(CalleeMap, Callee);
2572   }
2573 }
2574 
2575 static bool checkAVXParamFeature(DiagnosticsEngine &Diag,
2576                                  SourceLocation CallLoc,
2577                                  const llvm::StringMap<bool> &CallerMap,
2578                                  const llvm::StringMap<bool> &CalleeMap,
2579                                  QualType Ty, StringRef Feature,
2580                                  bool IsArgument) {
2581   bool CallerHasFeat = CallerMap.lookup(Feature);
2582   bool CalleeHasFeat = CalleeMap.lookup(Feature);
2583   if (!CallerHasFeat && !CalleeHasFeat)
2584     return Diag.Report(CallLoc, diag::warn_avx_calling_convention)
2585            << IsArgument << Ty << Feature;
2586 
2587   // Mixing calling conventions here is very clearly an error.
2588   if (!CallerHasFeat || !CalleeHasFeat)
2589     return Diag.Report(CallLoc, diag::err_avx_calling_convention)
2590            << IsArgument << Ty << Feature;
2591 
2592   // Else, both caller and callee have the required feature, so there is no need
2593   // to diagnose.
2594   return false;
2595 }
2596 
2597 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx,
2598                           SourceLocation CallLoc,
2599                           const llvm::StringMap<bool> &CallerMap,
2600                           const llvm::StringMap<bool> &CalleeMap, QualType Ty,
2601                           bool IsArgument) {
2602   uint64_t Size = Ctx.getTypeSize(Ty);
2603   if (Size > 256)
2604     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty,
2605                                 "avx512f", IsArgument);
2606 
2607   if (Size > 128)
2608     return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx",
2609                                 IsArgument);
2610 
2611   return false;
2612 }
2613 
2614 void X86_64TargetCodeGenInfo::checkFunctionCallABI(
2615     CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller,
2616     const FunctionDecl *Callee, const CallArgList &Args) const {
2617   llvm::StringMap<bool> CallerMap;
2618   llvm::StringMap<bool> CalleeMap;
2619   unsigned ArgIndex = 0;
2620 
2621   // We need to loop through the actual call arguments rather than the
2622   // function's parameters, in case this variadic.
2623   for (const CallArg &Arg : Args) {
2624     // The "avx" feature changes how vectors >128 in size are passed. "avx512f"
2625     // additionally changes how vectors >256 in size are passed. Like GCC, we
2626     // warn when a function is called with an argument where this will change.
2627     // Unlike GCC, we also error when it is an obvious ABI mismatch, that is,
2628     // the caller and callee features are mismatched.
2629     // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can
2630     // change its ABI with attribute-target after this call.
2631     if (Arg.getType()->isVectorType() &&
2632         CGM.getContext().getTypeSize(Arg.getType()) > 128) {
2633       initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2634       QualType Ty = Arg.getType();
2635       // The CallArg seems to have desugared the type already, so for clearer
2636       // diagnostics, replace it with the type in the FunctionDecl if possible.
2637       if (ArgIndex < Callee->getNumParams())
2638         Ty = Callee->getParamDecl(ArgIndex)->getType();
2639 
2640       if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2641                         CalleeMap, Ty, /*IsArgument*/ true))
2642         return;
2643     }
2644     ++ArgIndex;
2645   }
2646 
2647   // Check return always, as we don't have a good way of knowing in codegen
2648   // whether this value is used, tail-called, etc.
2649   if (Callee->getReturnType()->isVectorType() &&
2650       CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) {
2651     initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
2652     checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
2653                   CalleeMap, Callee->getReturnType(),
2654                   /*IsArgument*/ false);
2655   }
2656 }
2657 
2658 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2659   // If the argument does not end in .lib, automatically add the suffix.
2660   // If the argument contains a space, enclose it in quotes.
2661   // This matches the behavior of MSVC.
2662   bool Quote = Lib.contains(' ');
2663   std::string ArgStr = Quote ? "\"" : "";
2664   ArgStr += Lib;
2665   if (!Lib.endswith_insensitive(".lib") && !Lib.endswith_insensitive(".a"))
2666     ArgStr += ".lib";
2667   ArgStr += Quote ? "\"" : "";
2668   return ArgStr;
2669 }
2670 
2671 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2672 public:
2673   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2674         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2675         unsigned NumRegisterParameters)
2676     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2677         Win32StructABI, NumRegisterParameters, false) {}
2678 
2679   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2680                            CodeGen::CodeGenModule &CGM) const override;
2681 
2682   void getDependentLibraryOption(llvm::StringRef Lib,
2683                                  llvm::SmallString<24> &Opt) const override {
2684     Opt = "/DEFAULTLIB:";
2685     Opt += qualifyWindowsLibrary(Lib);
2686   }
2687 
2688   void getDetectMismatchOption(llvm::StringRef Name,
2689                                llvm::StringRef Value,
2690                                llvm::SmallString<32> &Opt) const override {
2691     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2692   }
2693 };
2694 
2695 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2696                                           CodeGen::CodeGenModule &CGM) {
2697   if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
2698 
2699     if (CGM.getCodeGenOpts().StackProbeSize != 4096)
2700       Fn->addFnAttr("stack-probe-size",
2701                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2702     if (CGM.getCodeGenOpts().NoStackArgProbe)
2703       Fn->addFnAttr("no-stack-arg-probe");
2704   }
2705 }
2706 
2707 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2708     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2709   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2710   if (GV->isDeclaration())
2711     return;
2712   addStackProbeTargetAttributes(D, GV, CGM);
2713 }
2714 
2715 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2716 public:
2717   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2718                              X86AVXABILevel AVXLevel)
2719       : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {
2720     SwiftInfo =
2721         std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/true);
2722   }
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 || k == BuiltinType::BFloat16) {
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                ET->isBFloat16Type()) {
3004       Current = SSE;
3005     } else if (ET == getContext().DoubleTy) {
3006       Lo = Hi = SSE;
3007     } else if (ET == getContext().LongDoubleTy) {
3008       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3009       if (LDF == &llvm::APFloat::IEEEquad())
3010         Current = Memory;
3011       else if (LDF == &llvm::APFloat::x87DoubleExtended())
3012         Current = ComplexX87;
3013       else if (LDF == &llvm::APFloat::IEEEdouble())
3014         Lo = Hi = SSE;
3015       else
3016         llvm_unreachable("unexpected long double representation!");
3017     }
3018 
3019     // If this complex type crosses an eightbyte boundary then it
3020     // should be split.
3021     uint64_t EB_Real = (OffsetBase) / 64;
3022     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
3023     if (Hi == NoClass && EB_Real != EB_Imag)
3024       Hi = Lo;
3025 
3026     return;
3027   }
3028 
3029   if (const auto *EITy = Ty->getAs<BitIntType>()) {
3030     if (EITy->getNumBits() <= 64)
3031       Current = Integer;
3032     else if (EITy->getNumBits() <= 128)
3033       Lo = Hi = Integer;
3034     // Larger values need to get passed in memory.
3035     return;
3036   }
3037 
3038   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3039     // Arrays are treated like structures.
3040 
3041     uint64_t Size = getContext().getTypeSize(Ty);
3042 
3043     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3044     // than eight eightbytes, ..., it has class MEMORY.
3045     // regcall ABI doesn't have limitation to an object. The only limitation
3046     // is the free registers, which will be checked in computeInfo.
3047     if (!IsRegCall && Size > 512)
3048       return;
3049 
3050     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
3051     // fields, it has class MEMORY.
3052     //
3053     // Only need to check alignment of array base.
3054     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
3055       return;
3056 
3057     // Otherwise implement simplified merge. We could be smarter about
3058     // this, but it isn't worth it and would be harder to verify.
3059     Current = NoClass;
3060     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
3061     uint64_t ArraySize = AT->getSize().getZExtValue();
3062 
3063     // The only case a 256-bit wide vector could be used is when the array
3064     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
3065     // to work for sizes wider than 128, early check and fallback to memory.
3066     //
3067     if (Size > 128 &&
3068         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
3069       return;
3070 
3071     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
3072       Class FieldLo, FieldHi;
3073       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
3074       Lo = merge(Lo, FieldLo);
3075       Hi = merge(Hi, FieldHi);
3076       if (Lo == Memory || Hi == Memory)
3077         break;
3078     }
3079 
3080     postMerge(Size, Lo, Hi);
3081     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
3082     return;
3083   }
3084 
3085   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3086     uint64_t Size = getContext().getTypeSize(Ty);
3087 
3088     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
3089     // than eight eightbytes, ..., it has class MEMORY.
3090     if (Size > 512)
3091       return;
3092 
3093     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
3094     // copy constructor or a non-trivial destructor, it is passed by invisible
3095     // reference.
3096     if (getRecordArgABI(RT, getCXXABI()))
3097       return;
3098 
3099     const RecordDecl *RD = RT->getDecl();
3100 
3101     // Assume variable sized types are passed in memory.
3102     if (RD->hasFlexibleArrayMember())
3103       return;
3104 
3105     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3106 
3107     // Reset Lo class, this will be recomputed.
3108     Current = NoClass;
3109 
3110     // If this is a C++ record, classify the bases first.
3111     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3112       for (const auto &I : CXXRD->bases()) {
3113         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3114                "Unexpected base class!");
3115         const auto *Base =
3116             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3117 
3118         // Classify this field.
3119         //
3120         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
3121         // single eightbyte, each is classified separately. Each eightbyte gets
3122         // initialized to class NO_CLASS.
3123         Class FieldLo, FieldHi;
3124         uint64_t Offset =
3125           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
3126         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
3127         Lo = merge(Lo, FieldLo);
3128         Hi = merge(Hi, FieldHi);
3129         if (Lo == Memory || Hi == Memory) {
3130           postMerge(Size, Lo, Hi);
3131           return;
3132         }
3133       }
3134     }
3135 
3136     // Classify the fields one at a time, merging the results.
3137     unsigned idx = 0;
3138     bool UseClang11Compat = getContext().getLangOpts().getClangABICompat() <=
3139                                 LangOptions::ClangABI::Ver11 ||
3140                             getContext().getTargetInfo().getTriple().isPS();
3141     bool IsUnion = RT->isUnionType() && !UseClang11Compat;
3142 
3143     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3144            i != e; ++i, ++idx) {
3145       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3146       bool BitField = i->isBitField();
3147 
3148       // Ignore padding bit-fields.
3149       if (BitField && i->isUnnamedBitfield())
3150         continue;
3151 
3152       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
3153       // eight eightbytes, or it contains unaligned fields, it has class MEMORY.
3154       //
3155       // The only case a 256-bit or a 512-bit wide vector could be used is when
3156       // the struct contains a single 256-bit or 512-bit element. Early check
3157       // and fallback to memory.
3158       //
3159       // FIXME: Extended the Lo and Hi logic properly to work for size wider
3160       // than 128.
3161       if (Size > 128 &&
3162           ((!IsUnion && Size != getContext().getTypeSize(i->getType())) ||
3163            Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
3164         Lo = Memory;
3165         postMerge(Size, Lo, Hi);
3166         return;
3167       }
3168       // Note, skip this test for bit-fields, see below.
3169       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
3170         Lo = Memory;
3171         postMerge(Size, Lo, Hi);
3172         return;
3173       }
3174 
3175       // Classify this field.
3176       //
3177       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
3178       // exceeds a single eightbyte, each is classified
3179       // separately. Each eightbyte gets initialized to class
3180       // NO_CLASS.
3181       Class FieldLo, FieldHi;
3182 
3183       // Bit-fields require special handling, they do not force the
3184       // structure to be passed in memory even if unaligned, and
3185       // therefore they can straddle an eightbyte.
3186       if (BitField) {
3187         assert(!i->isUnnamedBitfield());
3188         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
3189         uint64_t Size = i->getBitWidthValue(getContext());
3190 
3191         uint64_t EB_Lo = Offset / 64;
3192         uint64_t EB_Hi = (Offset + Size - 1) / 64;
3193 
3194         if (EB_Lo) {
3195           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
3196           FieldLo = NoClass;
3197           FieldHi = Integer;
3198         } else {
3199           FieldLo = Integer;
3200           FieldHi = EB_Hi ? Integer : NoClass;
3201         }
3202       } else
3203         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
3204       Lo = merge(Lo, FieldLo);
3205       Hi = merge(Hi, FieldHi);
3206       if (Lo == Memory || Hi == Memory)
3207         break;
3208     }
3209 
3210     postMerge(Size, Lo, Hi);
3211   }
3212 }
3213 
3214 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
3215   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3216   // place naturally.
3217   if (!isAggregateTypeForABI(Ty)) {
3218     // Treat an enum type as its underlying type.
3219     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3220       Ty = EnumTy->getDecl()->getIntegerType();
3221 
3222     if (Ty->isBitIntType())
3223       return getNaturalAlignIndirect(Ty);
3224 
3225     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3226                                               : ABIArgInfo::getDirect());
3227   }
3228 
3229   return getNaturalAlignIndirect(Ty);
3230 }
3231 
3232 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
3233   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
3234     uint64_t Size = getContext().getTypeSize(VecTy);
3235     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
3236     if (Size <= 64 || Size > LargestVector)
3237       return true;
3238     QualType EltTy = VecTy->getElementType();
3239     if (passInt128VectorsInMem() &&
3240         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
3241          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
3242       return true;
3243   }
3244 
3245   return false;
3246 }
3247 
3248 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
3249                                             unsigned freeIntRegs) const {
3250   // If this is a scalar LLVM value then assume LLVM will pass it in the right
3251   // place naturally.
3252   //
3253   // This assumption is optimistic, as there could be free registers available
3254   // when we need to pass this argument in memory, and LLVM could try to pass
3255   // the argument in the free register. This does not seem to happen currently,
3256   // but this code would be much safer if we could mark the argument with
3257   // 'onstack'. See PR12193.
3258   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
3259       !Ty->isBitIntType()) {
3260     // Treat an enum type as its underlying type.
3261     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3262       Ty = EnumTy->getDecl()->getIntegerType();
3263 
3264     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
3265                                               : ABIArgInfo::getDirect());
3266   }
3267 
3268   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3269     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3270 
3271   // Compute the byval alignment. We specify the alignment of the byval in all
3272   // cases so that the mid-level optimizer knows the alignment of the byval.
3273   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
3274 
3275   // Attempt to avoid passing indirect results using byval when possible. This
3276   // is important for good codegen.
3277   //
3278   // We do this by coercing the value into a scalar type which the backend can
3279   // handle naturally (i.e., without using byval).
3280   //
3281   // For simplicity, we currently only do this when we have exhausted all of the
3282   // free integer registers. Doing this when there are free integer registers
3283   // would require more care, as we would have to ensure that the coerced value
3284   // did not claim the unused register. That would require either reording the
3285   // arguments to the function (so that any subsequent inreg values came first),
3286   // or only doing this optimization when there were no following arguments that
3287   // might be inreg.
3288   //
3289   // We currently expect it to be rare (particularly in well written code) for
3290   // arguments to be passed on the stack when there are still free integer
3291   // registers available (this would typically imply large structs being passed
3292   // by value), so this seems like a fair tradeoff for now.
3293   //
3294   // We can revisit this if the backend grows support for 'onstack' parameter
3295   // attributes. See PR12193.
3296   if (freeIntRegs == 0) {
3297     uint64_t Size = getContext().getTypeSize(Ty);
3298 
3299     // If this type fits in an eightbyte, coerce it into the matching integral
3300     // type, which will end up on the stack (with alignment 8).
3301     if (Align == 8 && Size <= 64)
3302       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3303                                                           Size));
3304   }
3305 
3306   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
3307 }
3308 
3309 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3310 /// register. Pick an LLVM IR type that will be passed as a vector register.
3311 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3312   // Wrapper structs/arrays that only contain vectors are passed just like
3313   // vectors; strip them off if present.
3314   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3315     Ty = QualType(InnerTy, 0);
3316 
3317   llvm::Type *IRType = CGT.ConvertType(Ty);
3318   if (isa<llvm::VectorType>(IRType)) {
3319     // Don't pass vXi128 vectors in their native type, the backend can't
3320     // legalize them.
3321     if (passInt128VectorsInMem() &&
3322         cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
3323       // Use a vXi64 vector.
3324       uint64_t Size = getContext().getTypeSize(Ty);
3325       return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3326                                         Size / 64);
3327     }
3328 
3329     return IRType;
3330   }
3331 
3332   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3333     return IRType;
3334 
3335   // We couldn't find the preferred IR vector type for 'Ty'.
3336   uint64_t Size = getContext().getTypeSize(Ty);
3337   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3338 
3339 
3340   // Return a LLVM IR vector type based on the size of 'Ty'.
3341   return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3342                                     Size / 64);
3343 }
3344 
3345 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3346 /// is known to either be off the end of the specified type or being in
3347 /// alignment padding.  The user type specified is known to be at most 128 bits
3348 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3349 /// classification that put one of the two halves in the INTEGER class.
3350 ///
3351 /// It is conservatively correct to return false.
3352 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3353                                   unsigned EndBit, ASTContext &Context) {
3354   // If the bytes being queried are off the end of the type, there is no user
3355   // data hiding here.  This handles analysis of builtins, vectors and other
3356   // types that don't contain interesting padding.
3357   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3358   if (TySize <= StartBit)
3359     return true;
3360 
3361   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3362     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3363     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3364 
3365     // Check each element to see if the element overlaps with the queried range.
3366     for (unsigned i = 0; i != NumElts; ++i) {
3367       // If the element is after the span we care about, then we're done..
3368       unsigned EltOffset = i*EltSize;
3369       if (EltOffset >= EndBit) break;
3370 
3371       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3372       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3373                                  EndBit-EltOffset, Context))
3374         return false;
3375     }
3376     // If it overlaps no elements, then it is safe to process as padding.
3377     return true;
3378   }
3379 
3380   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3381     const RecordDecl *RD = RT->getDecl();
3382     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3383 
3384     // If this is a C++ record, check the bases first.
3385     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3386       for (const auto &I : CXXRD->bases()) {
3387         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3388                "Unexpected base class!");
3389         const auto *Base =
3390             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3391 
3392         // If the base is after the span we care about, ignore it.
3393         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3394         if (BaseOffset >= EndBit) continue;
3395 
3396         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3397         if (!BitsContainNoUserData(I.getType(), BaseStart,
3398                                    EndBit-BaseOffset, Context))
3399           return false;
3400       }
3401     }
3402 
3403     // Verify that no field has data that overlaps the region of interest.  Yes
3404     // this could be sped up a lot by being smarter about queried fields,
3405     // however we're only looking at structs up to 16 bytes, so we don't care
3406     // much.
3407     unsigned idx = 0;
3408     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3409          i != e; ++i, ++idx) {
3410       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3411 
3412       // If we found a field after the region we care about, then we're done.
3413       if (FieldOffset >= EndBit) break;
3414 
3415       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3416       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3417                                  Context))
3418         return false;
3419     }
3420 
3421     // If nothing in this record overlapped the area of interest, then we're
3422     // clean.
3423     return true;
3424   }
3425 
3426   return false;
3427 }
3428 
3429 /// getFPTypeAtOffset - Return a floating point type at the specified offset.
3430 static llvm::Type *getFPTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3431                                      const llvm::DataLayout &TD) {
3432   if (IROffset == 0 && IRType->isFloatingPointTy())
3433     return IRType;
3434 
3435   // If this is a struct, recurse into the field at the specified offset.
3436   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3437     if (!STy->getNumContainedTypes())
3438       return nullptr;
3439 
3440     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3441     unsigned Elt = SL->getElementContainingOffset(IROffset);
3442     IROffset -= SL->getElementOffset(Elt);
3443     return getFPTypeAtOffset(STy->getElementType(Elt), IROffset, TD);
3444   }
3445 
3446   // If this is an array, recurse into the field at the specified offset.
3447   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3448     llvm::Type *EltTy = ATy->getElementType();
3449     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3450     IROffset -= IROffset / EltSize * EltSize;
3451     return getFPTypeAtOffset(EltTy, IROffset, TD);
3452   }
3453 
3454   return nullptr;
3455 }
3456 
3457 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3458 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3459 llvm::Type *X86_64ABIInfo::
3460 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3461                    QualType SourceTy, unsigned SourceOffset) const {
3462   const llvm::DataLayout &TD = getDataLayout();
3463   unsigned SourceSize =
3464       (unsigned)getContext().getTypeSize(SourceTy) / 8 - SourceOffset;
3465   llvm::Type *T0 = getFPTypeAtOffset(IRType, IROffset, TD);
3466   if (!T0 || T0->isDoubleTy())
3467     return llvm::Type::getDoubleTy(getVMContext());
3468 
3469   // Get the adjacent FP type.
3470   llvm::Type *T1 = nullptr;
3471   unsigned T0Size = TD.getTypeAllocSize(T0);
3472   if (SourceSize > T0Size)
3473       T1 = getFPTypeAtOffset(IRType, IROffset + T0Size, TD);
3474   if (T1 == nullptr) {
3475     // Check if IRType is a half/bfloat + float. float type will be in IROffset+4 due
3476     // to its alignment.
3477     if (T0->is16bitFPTy() && SourceSize > 4)
3478       T1 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
3479     // If we can't get a second FP type, return a simple half or float.
3480     // avx512fp16-abi.c:pr51813_2 shows it works to return float for
3481     // {float, i8} too.
3482     if (T1 == nullptr)
3483       return T0;
3484   }
3485 
3486   if (T0->isFloatTy() && T1->isFloatTy())
3487     return llvm::FixedVectorType::get(T0, 2);
3488 
3489   if (T0->is16bitFPTy() && T1->is16bitFPTy()) {
3490     llvm::Type *T2 = nullptr;
3491     if (SourceSize > 4)
3492       T2 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
3493     if (T2 == nullptr)
3494       return llvm::FixedVectorType::get(T0, 2);
3495     return llvm::FixedVectorType::get(T0, 4);
3496   }
3497 
3498   if (T0->is16bitFPTy() || T1->is16bitFPTy())
3499     return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()), 4);
3500 
3501   return llvm::Type::getDoubleTy(getVMContext());
3502 }
3503 
3504 
3505 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3506 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3507 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3508 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3509 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3510 /// etc).
3511 ///
3512 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3513 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3514 /// the 8-byte value references.  PrefType may be null.
3515 ///
3516 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3517 /// an offset into this that we're processing (which is always either 0 or 8).
3518 ///
3519 llvm::Type *X86_64ABIInfo::
3520 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3521                        QualType SourceTy, unsigned SourceOffset) const {
3522   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3523   // returning an 8-byte unit starting with it.  See if we can safely use it.
3524   if (IROffset == 0) {
3525     // Pointers and int64's always fill the 8-byte unit.
3526     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3527         IRType->isIntegerTy(64))
3528       return IRType;
3529 
3530     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3531     // goodness in the source type is just tail padding.  This is allowed to
3532     // kick in for struct {double,int} on the int, but not on
3533     // struct{double,int,int} because we wouldn't return the second int.  We
3534     // have to do this analysis on the source type because we can't depend on
3535     // unions being lowered a specific way etc.
3536     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3537         IRType->isIntegerTy(32) ||
3538         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3539       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3540           cast<llvm::IntegerType>(IRType)->getBitWidth();
3541 
3542       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3543                                 SourceOffset*8+64, getContext()))
3544         return IRType;
3545     }
3546   }
3547 
3548   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3549     // If this is a struct, recurse into the field at the specified offset.
3550     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3551     if (IROffset < SL->getSizeInBytes()) {
3552       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3553       IROffset -= SL->getElementOffset(FieldIdx);
3554 
3555       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3556                                     SourceTy, SourceOffset);
3557     }
3558   }
3559 
3560   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3561     llvm::Type *EltTy = ATy->getElementType();
3562     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3563     unsigned EltOffset = IROffset/EltSize*EltSize;
3564     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3565                                   SourceOffset);
3566   }
3567 
3568   // Okay, we don't have any better idea of what to pass, so we pass this in an
3569   // integer register that isn't too big to fit the rest of the struct.
3570   unsigned TySizeInBytes =
3571     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3572 
3573   assert(TySizeInBytes != SourceOffset && "Empty field?");
3574 
3575   // It is always safe to classify this as an integer type up to i64 that
3576   // isn't larger than the structure.
3577   return llvm::IntegerType::get(getVMContext(),
3578                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3579 }
3580 
3581 
3582 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3583 /// be used as elements of a two register pair to pass or return, return a
3584 /// first class aggregate to represent them.  For example, if the low part of
3585 /// a by-value argument should be passed as i32* and the high part as float,
3586 /// return {i32*, float}.
3587 static llvm::Type *
3588 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3589                            const llvm::DataLayout &TD) {
3590   // In order to correctly satisfy the ABI, we need to the high part to start
3591   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3592   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3593   // the second element at offset 8.  Check for this:
3594   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3595   llvm::Align HiAlign = TD.getABITypeAlign(Hi);
3596   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3597   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3598 
3599   // To handle this, we have to increase the size of the low part so that the
3600   // second element will start at an 8 byte offset.  We can't increase the size
3601   // of the second element because it might make us access off the end of the
3602   // struct.
3603   if (HiStart != 8) {
3604     // There are usually two sorts of types the ABI generation code can produce
3605     // for the low part of a pair that aren't 8 bytes in size: half, float or
3606     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3607     // NaCl).
3608     // Promote these to a larger type.
3609     if (Lo->isHalfTy() || Lo->isFloatTy())
3610       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3611     else {
3612       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3613              && "Invalid/unknown lo type");
3614       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3615     }
3616   }
3617 
3618   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3619 
3620   // Verify that the second element is at an 8-byte offset.
3621   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3622          "Invalid x86-64 argument pair!");
3623   return Result;
3624 }
3625 
3626 ABIArgInfo X86_64ABIInfo::
3627 classifyReturnType(QualType RetTy) const {
3628   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3629   // classification algorithm.
3630   X86_64ABIInfo::Class Lo, Hi;
3631   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3632 
3633   // Check some invariants.
3634   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3635   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3636 
3637   llvm::Type *ResType = nullptr;
3638   switch (Lo) {
3639   case NoClass:
3640     if (Hi == NoClass)
3641       return ABIArgInfo::getIgnore();
3642     // If the low part is just padding, it takes no register, leave ResType
3643     // null.
3644     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3645            "Unknown missing lo part");
3646     break;
3647 
3648   case SSEUp:
3649   case X87Up:
3650     llvm_unreachable("Invalid classification for lo word.");
3651 
3652     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3653     // hidden argument.
3654   case Memory:
3655     return getIndirectReturnResult(RetTy);
3656 
3657     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3658     // available register of the sequence %rax, %rdx is used.
3659   case Integer:
3660     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3661 
3662     // If we have a sign or zero extended integer, make sure to return Extend
3663     // so that the parameter gets the right LLVM IR attributes.
3664     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3665       // Treat an enum type as its underlying type.
3666       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3667         RetTy = EnumTy->getDecl()->getIntegerType();
3668 
3669       if (RetTy->isIntegralOrEnumerationType() &&
3670           isPromotableIntegerTypeForABI(RetTy))
3671         return ABIArgInfo::getExtend(RetTy);
3672     }
3673     break;
3674 
3675     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3676     // available SSE register of the sequence %xmm0, %xmm1 is used.
3677   case SSE:
3678     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3679     break;
3680 
3681     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3682     // returned on the X87 stack in %st0 as 80-bit x87 number.
3683   case X87:
3684     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3685     break;
3686 
3687     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3688     // part of the value is returned in %st0 and the imaginary part in
3689     // %st1.
3690   case ComplexX87:
3691     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3692     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3693                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3694     break;
3695   }
3696 
3697   llvm::Type *HighPart = nullptr;
3698   switch (Hi) {
3699     // Memory was handled previously and X87 should
3700     // never occur as a hi class.
3701   case Memory:
3702   case X87:
3703     llvm_unreachable("Invalid classification for hi word.");
3704 
3705   case ComplexX87: // Previously handled.
3706   case NoClass:
3707     break;
3708 
3709   case Integer:
3710     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3711     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3712       return ABIArgInfo::getDirect(HighPart, 8);
3713     break;
3714   case SSE:
3715     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3716     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3717       return ABIArgInfo::getDirect(HighPart, 8);
3718     break;
3719 
3720     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3721     // is passed in the next available eightbyte chunk if the last used
3722     // vector register.
3723     //
3724     // SSEUP should always be preceded by SSE, just widen.
3725   case SSEUp:
3726     assert(Lo == SSE && "Unexpected SSEUp classification.");
3727     ResType = GetByteVectorType(RetTy);
3728     break;
3729 
3730     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3731     // returned together with the previous X87 value in %st0.
3732   case X87Up:
3733     // If X87Up is preceded by X87, we don't need to do
3734     // anything. However, in some cases with unions it may not be
3735     // preceded by X87. In such situations we follow gcc and pass the
3736     // extra bits in an SSE reg.
3737     if (Lo != X87) {
3738       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3739       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3740         return ABIArgInfo::getDirect(HighPart, 8);
3741     }
3742     break;
3743   }
3744 
3745   // If a high part was specified, merge it together with the low part.  It is
3746   // known to pass in the high eightbyte of the result.  We do this by forming a
3747   // first class struct aggregate with the high and low part: {low, high}
3748   if (HighPart)
3749     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3750 
3751   return ABIArgInfo::getDirect(ResType);
3752 }
3753 
3754 ABIArgInfo
3755 X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned freeIntRegs,
3756                                     unsigned &neededInt, unsigned &neededSSE,
3757                                     bool isNamedArg, bool IsRegCall) const {
3758   Ty = useFirstFieldIfTransparentUnion(Ty);
3759 
3760   X86_64ABIInfo::Class Lo, Hi;
3761   classify(Ty, 0, Lo, Hi, isNamedArg, IsRegCall);
3762 
3763   // Check some invariants.
3764   // FIXME: Enforce these by construction.
3765   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3766   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3767 
3768   neededInt = 0;
3769   neededSSE = 0;
3770   llvm::Type *ResType = nullptr;
3771   switch (Lo) {
3772   case NoClass:
3773     if (Hi == NoClass)
3774       return ABIArgInfo::getIgnore();
3775     // If the low part is just padding, it takes no register, leave ResType
3776     // null.
3777     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3778            "Unknown missing lo part");
3779     break;
3780 
3781     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3782     // on the stack.
3783   case Memory:
3784 
3785     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3786     // COMPLEX_X87, it is passed in memory.
3787   case X87:
3788   case ComplexX87:
3789     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3790       ++neededInt;
3791     return getIndirectResult(Ty, freeIntRegs);
3792 
3793   case SSEUp:
3794   case X87Up:
3795     llvm_unreachable("Invalid classification for lo word.");
3796 
3797     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3798     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3799     // and %r9 is used.
3800   case Integer:
3801     ++neededInt;
3802 
3803     // Pick an 8-byte type based on the preferred type.
3804     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3805 
3806     // If we have a sign or zero extended integer, make sure to return Extend
3807     // so that the parameter gets the right LLVM IR attributes.
3808     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3809       // Treat an enum type as its underlying type.
3810       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3811         Ty = EnumTy->getDecl()->getIntegerType();
3812 
3813       if (Ty->isIntegralOrEnumerationType() &&
3814           isPromotableIntegerTypeForABI(Ty))
3815         return ABIArgInfo::getExtend(Ty);
3816     }
3817 
3818     break;
3819 
3820     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3821     // available SSE register is used, the registers are taken in the
3822     // order from %xmm0 to %xmm7.
3823   case SSE: {
3824     llvm::Type *IRType = CGT.ConvertType(Ty);
3825     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3826     ++neededSSE;
3827     break;
3828   }
3829   }
3830 
3831   llvm::Type *HighPart = nullptr;
3832   switch (Hi) {
3833     // Memory was handled previously, ComplexX87 and X87 should
3834     // never occur as hi classes, and X87Up must be preceded by X87,
3835     // which is passed in memory.
3836   case Memory:
3837   case X87:
3838   case ComplexX87:
3839     llvm_unreachable("Invalid classification for hi word.");
3840 
3841   case NoClass: break;
3842 
3843   case Integer:
3844     ++neededInt;
3845     // Pick an 8-byte type based on the preferred type.
3846     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3847 
3848     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3849       return ABIArgInfo::getDirect(HighPart, 8);
3850     break;
3851 
3852     // X87Up generally doesn't occur here (long double is passed in
3853     // memory), except in situations involving unions.
3854   case X87Up:
3855   case SSE:
3856     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3857 
3858     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3859       return ABIArgInfo::getDirect(HighPart, 8);
3860 
3861     ++neededSSE;
3862     break;
3863 
3864     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3865     // eightbyte is passed in the upper half of the last used SSE
3866     // register.  This only happens when 128-bit vectors are passed.
3867   case SSEUp:
3868     assert(Lo == SSE && "Unexpected SSEUp classification");
3869     ResType = GetByteVectorType(Ty);
3870     break;
3871   }
3872 
3873   // If a high part was specified, merge it together with the low part.  It is
3874   // known to pass in the high eightbyte of the result.  We do this by forming a
3875   // first class struct aggregate with the high and low part: {low, high}
3876   if (HighPart)
3877     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3878 
3879   return ABIArgInfo::getDirect(ResType);
3880 }
3881 
3882 ABIArgInfo
3883 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3884                                              unsigned &NeededSSE,
3885                                              unsigned &MaxVectorWidth) const {
3886   auto RT = Ty->getAs<RecordType>();
3887   assert(RT && "classifyRegCallStructType only valid with struct types");
3888 
3889   if (RT->getDecl()->hasFlexibleArrayMember())
3890     return getIndirectReturnResult(Ty);
3891 
3892   // Sum up bases
3893   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3894     if (CXXRD->isDynamicClass()) {
3895       NeededInt = NeededSSE = 0;
3896       return getIndirectReturnResult(Ty);
3897     }
3898 
3899     for (const auto &I : CXXRD->bases())
3900       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE,
3901                                         MaxVectorWidth)
3902               .isIndirect()) {
3903         NeededInt = NeededSSE = 0;
3904         return getIndirectReturnResult(Ty);
3905       }
3906   }
3907 
3908   // Sum up members
3909   for (const auto *FD : RT->getDecl()->fields()) {
3910     QualType MTy = FD->getType();
3911     if (MTy->isRecordType() && !MTy->isUnionType()) {
3912       if (classifyRegCallStructTypeImpl(MTy, NeededInt, NeededSSE,
3913                                         MaxVectorWidth)
3914               .isIndirect()) {
3915         NeededInt = NeededSSE = 0;
3916         return getIndirectReturnResult(Ty);
3917       }
3918     } else {
3919       unsigned LocalNeededInt, LocalNeededSSE;
3920       if (classifyArgumentType(MTy, UINT_MAX, LocalNeededInt, LocalNeededSSE,
3921                                true, true)
3922               .isIndirect()) {
3923         NeededInt = NeededSSE = 0;
3924         return getIndirectReturnResult(Ty);
3925       }
3926       if (const auto *AT = getContext().getAsConstantArrayType(MTy))
3927         MTy = AT->getElementType();
3928       if (const auto *VT = MTy->getAs<VectorType>())
3929         if (getContext().getTypeSize(VT) > MaxVectorWidth)
3930           MaxVectorWidth = getContext().getTypeSize(VT);
3931       NeededInt += LocalNeededInt;
3932       NeededSSE += LocalNeededSSE;
3933     }
3934   }
3935 
3936   return ABIArgInfo::getDirect();
3937 }
3938 
3939 ABIArgInfo
3940 X86_64ABIInfo::classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
3941                                          unsigned &NeededSSE,
3942                                          unsigned &MaxVectorWidth) const {
3943 
3944   NeededInt = 0;
3945   NeededSSE = 0;
3946   MaxVectorWidth = 0;
3947 
3948   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE,
3949                                        MaxVectorWidth);
3950 }
3951 
3952 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3953 
3954   const unsigned CallingConv = FI.getCallingConvention();
3955   // It is possible to force Win64 calling convention on any x86_64 target by
3956   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3957   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3958   if (CallingConv == llvm::CallingConv::Win64) {
3959     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3960     Win64ABIInfo.computeInfo(FI);
3961     return;
3962   }
3963 
3964   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3965 
3966   // Keep track of the number of assigned registers.
3967   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3968   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3969   unsigned NeededInt = 0, NeededSSE = 0, MaxVectorWidth = 0;
3970 
3971   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3972     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3973         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3974       FI.getReturnInfo() = classifyRegCallStructType(
3975           FI.getReturnType(), NeededInt, NeededSSE, MaxVectorWidth);
3976       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3977         FreeIntRegs -= NeededInt;
3978         FreeSSERegs -= NeededSSE;
3979       } else {
3980         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3981       }
3982     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
3983                getContext().getCanonicalType(FI.getReturnType()
3984                                                  ->getAs<ComplexType>()
3985                                                  ->getElementType()) ==
3986                    getContext().LongDoubleTy)
3987       // Complex Long Double Type is passed in Memory when Regcall
3988       // calling convention is used.
3989       FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3990     else
3991       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3992   }
3993 
3994   // If the return value is indirect, then the hidden argument is consuming one
3995   // integer register.
3996   if (FI.getReturnInfo().isIndirect())
3997     --FreeIntRegs;
3998   else if (NeededSSE && MaxVectorWidth > 0)
3999     FI.setMaxVectorWidth(MaxVectorWidth);
4000 
4001   // The chain argument effectively gives us another free register.
4002   if (FI.isChainCall())
4003     ++FreeIntRegs;
4004 
4005   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
4006   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
4007   // get assigned (in left-to-right order) for passing as follows...
4008   unsigned ArgNo = 0;
4009   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4010        it != ie; ++it, ++ArgNo) {
4011     bool IsNamedArg = ArgNo < NumRequiredArgs;
4012 
4013     if (IsRegCall && it->type->isStructureOrClassType())
4014       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE,
4015                                            MaxVectorWidth);
4016     else
4017       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
4018                                       NeededSSE, IsNamedArg);
4019 
4020     // AMD64-ABI 3.2.3p3: If there are no registers available for any
4021     // eightbyte of an argument, the whole argument is passed on the
4022     // stack. If registers have already been assigned for some
4023     // eightbytes of such an argument, the assignments get reverted.
4024     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
4025       FreeIntRegs -= NeededInt;
4026       FreeSSERegs -= NeededSSE;
4027       if (MaxVectorWidth > FI.getMaxVectorWidth())
4028         FI.setMaxVectorWidth(MaxVectorWidth);
4029     } else {
4030       it->info = getIndirectResult(it->type, FreeIntRegs);
4031     }
4032   }
4033 }
4034 
4035 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
4036                                          Address VAListAddr, QualType Ty) {
4037   Address overflow_arg_area_p =
4038       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
4039   llvm::Value *overflow_arg_area =
4040     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
4041 
4042   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
4043   // byte boundary if alignment needed by type exceeds 8 byte boundary.
4044   // It isn't stated explicitly in the standard, but in practice we use
4045   // alignment greater than 16 where necessary.
4046   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4047   if (Align > CharUnits::fromQuantity(8)) {
4048     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
4049                                                       Align);
4050   }
4051 
4052   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
4053   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4054   llvm::Value *Res =
4055     CGF.Builder.CreateBitCast(overflow_arg_area,
4056                               llvm::PointerType::getUnqual(LTy));
4057 
4058   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
4059   // l->overflow_arg_area + sizeof(type).
4060   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
4061   // an 8 byte boundary.
4062 
4063   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
4064   llvm::Value *Offset =
4065       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
4066   overflow_arg_area = CGF.Builder.CreateGEP(CGF.Int8Ty, overflow_arg_area,
4067                                             Offset, "overflow_arg_area.next");
4068   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
4069 
4070   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
4071   return Address(Res, LTy, Align);
4072 }
4073 
4074 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4075                                  QualType Ty) const {
4076   // Assume that va_list type is correct; should be pointer to LLVM type:
4077   // struct {
4078   //   i32 gp_offset;
4079   //   i32 fp_offset;
4080   //   i8* overflow_arg_area;
4081   //   i8* reg_save_area;
4082   // };
4083   unsigned neededInt, neededSSE;
4084 
4085   Ty = getContext().getCanonicalType(Ty);
4086   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
4087                                        /*isNamedArg*/false);
4088 
4089   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
4090   // in the registers. If not go to step 7.
4091   if (!neededInt && !neededSSE)
4092     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4093 
4094   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
4095   // general purpose registers needed to pass type and num_fp to hold
4096   // the number of floating point registers needed.
4097 
4098   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
4099   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
4100   // l->fp_offset > 304 - num_fp * 16 go to step 7.
4101   //
4102   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
4103   // register save space).
4104 
4105   llvm::Value *InRegs = nullptr;
4106   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
4107   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
4108   if (neededInt) {
4109     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
4110     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
4111     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
4112     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
4113   }
4114 
4115   if (neededSSE) {
4116     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
4117     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
4118     llvm::Value *FitsInFP =
4119       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
4120     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
4121     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
4122   }
4123 
4124   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4125   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4126   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4127   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4128 
4129   // Emit code to load the value if it was passed in registers.
4130 
4131   CGF.EmitBlock(InRegBlock);
4132 
4133   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
4134   // an offset of l->gp_offset and/or l->fp_offset. This may require
4135   // copying to a temporary location in case the parameter is passed
4136   // in different register classes or requires an alignment greater
4137   // than 8 for general purpose registers and 16 for XMM registers.
4138   //
4139   // FIXME: This really results in shameful code when we end up needing to
4140   // collect arguments from different places; often what should result in a
4141   // simple assembling of a structure from scattered addresses has many more
4142   // loads than necessary. Can we clean this up?
4143   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
4144   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
4145       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
4146 
4147   Address RegAddr = Address::invalid();
4148   if (neededInt && neededSSE) {
4149     // FIXME: Cleanup.
4150     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
4151     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
4152     Address Tmp = CGF.CreateMemTemp(Ty);
4153     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4154     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
4155     llvm::Type *TyLo = ST->getElementType(0);
4156     llvm::Type *TyHi = ST->getElementType(1);
4157     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
4158            "Unexpected ABI info for mixed regs");
4159     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
4160     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
4161     llvm::Value *GPAddr =
4162         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset);
4163     llvm::Value *FPAddr =
4164         CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset);
4165     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
4166     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
4167 
4168     // Copy the first element.
4169     // FIXME: Our choice of alignment here and below is probably pessimistic.
4170     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
4171         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
4172         CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyLo)));
4173     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4174 
4175     // Copy the second element.
4176     V = CGF.Builder.CreateAlignedLoad(
4177         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
4178         CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyHi)));
4179     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4180 
4181     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4182   } else if (neededInt) {
4183     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset),
4184                       CGF.Int8Ty, CharUnits::fromQuantity(8));
4185     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4186 
4187     // Copy to a temporary if necessary to ensure the appropriate alignment.
4188     auto TInfo = getContext().getTypeInfoInChars(Ty);
4189     uint64_t TySize = TInfo.Width.getQuantity();
4190     CharUnits TyAlign = TInfo.Align;
4191 
4192     // Copy into a temporary if the type is more aligned than the
4193     // register save area.
4194     if (TyAlign.getQuantity() > 8) {
4195       Address Tmp = CGF.CreateMemTemp(Ty);
4196       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
4197       RegAddr = Tmp;
4198     }
4199 
4200   } else if (neededSSE == 1) {
4201     RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset),
4202                       CGF.Int8Ty, CharUnits::fromQuantity(16));
4203     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
4204   } else {
4205     assert(neededSSE == 2 && "Invalid number of needed registers!");
4206     // SSE registers are spaced 16 bytes apart in the register save
4207     // area, we need to collect the two eightbytes together.
4208     // The ABI isn't explicit about this, but it seems reasonable
4209     // to assume that the slots are 16-byte aligned, since the stack is
4210     // naturally 16-byte aligned and the prologue is expected to store
4211     // all the SSE registers to the RSA.
4212     Address RegAddrLo = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea,
4213                                                       fp_offset),
4214                                 CGF.Int8Ty, CharUnits::fromQuantity(16));
4215     Address RegAddrHi =
4216       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
4217                                              CharUnits::fromQuantity(16));
4218     llvm::Type *ST = AI.canHaveCoerceToType()
4219                          ? AI.getCoerceToType()
4220                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
4221     llvm::Value *V;
4222     Address Tmp = CGF.CreateMemTemp(Ty);
4223     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
4224     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4225         RegAddrLo, ST->getStructElementType(0)));
4226     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
4227     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
4228         RegAddrHi, ST->getStructElementType(1)));
4229     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
4230 
4231     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
4232   }
4233 
4234   // AMD64-ABI 3.5.7p5: Step 5. Set:
4235   // l->gp_offset = l->gp_offset + num_gp * 8
4236   // l->fp_offset = l->fp_offset + num_fp * 16.
4237   if (neededInt) {
4238     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
4239     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
4240                             gp_offset_p);
4241   }
4242   if (neededSSE) {
4243     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
4244     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
4245                             fp_offset_p);
4246   }
4247   CGF.EmitBranch(ContBlock);
4248 
4249   // Emit code to load the value if it was passed in memory.
4250 
4251   CGF.EmitBlock(InMemBlock);
4252   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
4253 
4254   // Return the appropriate result.
4255 
4256   CGF.EmitBlock(ContBlock);
4257   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
4258                                  "vaarg.addr");
4259   return ResAddr;
4260 }
4261 
4262 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4263                                    QualType Ty) const {
4264   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4265   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4266   uint64_t Width = getContext().getTypeSize(Ty);
4267   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4268 
4269   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4270                           CGF.getContext().getTypeInfoInChars(Ty),
4271                           CharUnits::fromQuantity(8),
4272                           /*allowHigherAlign*/ false);
4273 }
4274 
4275 ABIArgInfo WinX86_64ABIInfo::reclassifyHvaArgForVectorCall(
4276     QualType Ty, unsigned &FreeSSERegs, const ABIArgInfo &current) const {
4277   const Type *Base = nullptr;
4278   uint64_t NumElts = 0;
4279 
4280   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
4281       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
4282     FreeSSERegs -= NumElts;
4283     return getDirectX86Hva();
4284   }
4285   return current;
4286 }
4287 
4288 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
4289                                       bool IsReturnType, bool IsVectorCall,
4290                                       bool IsRegCall) const {
4291 
4292   if (Ty->isVoidType())
4293     return ABIArgInfo::getIgnore();
4294 
4295   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4296     Ty = EnumTy->getDecl()->getIntegerType();
4297 
4298   TypeInfo Info = getContext().getTypeInfo(Ty);
4299   uint64_t Width = Info.Width;
4300   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
4301 
4302   const RecordType *RT = Ty->getAs<RecordType>();
4303   if (RT) {
4304     if (!IsReturnType) {
4305       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
4306         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4307     }
4308 
4309     if (RT->getDecl()->hasFlexibleArrayMember())
4310       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4311 
4312   }
4313 
4314   const Type *Base = nullptr;
4315   uint64_t NumElts = 0;
4316   // vectorcall adds the concept of a homogenous vector aggregate, similar to
4317   // other targets.
4318   if ((IsVectorCall || IsRegCall) &&
4319       isHomogeneousAggregate(Ty, Base, NumElts)) {
4320     if (IsRegCall) {
4321       if (FreeSSERegs >= NumElts) {
4322         FreeSSERegs -= NumElts;
4323         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
4324           return ABIArgInfo::getDirect();
4325         return ABIArgInfo::getExpand();
4326       }
4327       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4328     } else if (IsVectorCall) {
4329       if (FreeSSERegs >= NumElts &&
4330           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
4331         FreeSSERegs -= NumElts;
4332         return ABIArgInfo::getDirect();
4333       } else if (IsReturnType) {
4334         return ABIArgInfo::getExpand();
4335       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
4336         // HVAs are delayed and reclassified in the 2nd step.
4337         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4338       }
4339     }
4340   }
4341 
4342   if (Ty->isMemberPointerType()) {
4343     // If the member pointer is represented by an LLVM int or ptr, pass it
4344     // directly.
4345     llvm::Type *LLTy = CGT.ConvertType(Ty);
4346     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
4347       return ABIArgInfo::getDirect();
4348   }
4349 
4350   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
4351     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4352     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4353     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4354       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4355 
4356     // Otherwise, coerce it to a small integer.
4357     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4358   }
4359 
4360   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4361     switch (BT->getKind()) {
4362     case BuiltinType::Bool:
4363       // Bool type is always extended to the ABI, other builtin types are not
4364       // extended.
4365       return ABIArgInfo::getExtend(Ty);
4366 
4367     case BuiltinType::LongDouble:
4368       // Mingw64 GCC uses the old 80 bit extended precision floating point
4369       // unit. It passes them indirectly through memory.
4370       if (IsMingw64) {
4371         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4372         if (LDF == &llvm::APFloat::x87DoubleExtended())
4373           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4374       }
4375       break;
4376 
4377     case BuiltinType::Int128:
4378     case BuiltinType::UInt128:
4379       // If it's a parameter type, the normal ABI rule is that arguments larger
4380       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4381       // even though it isn't particularly efficient.
4382       if (!IsReturnType)
4383         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4384 
4385       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4386       // Clang matches them for compatibility.
4387       return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
4388           llvm::Type::getInt64Ty(getVMContext()), 2));
4389 
4390     default:
4391       break;
4392     }
4393   }
4394 
4395   if (Ty->isBitIntType()) {
4396     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4397     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4398     // However, non-power-of-two bit-precise integers will be passed as 1, 2, 4,
4399     // or 8 bytes anyway as long is it fits in them, so we don't have to check
4400     // the power of 2.
4401     if (Width <= 64)
4402       return ABIArgInfo::getDirect();
4403     return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4404   }
4405 
4406   return ABIArgInfo::getDirect();
4407 }
4408 
4409 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4410   const unsigned CC = FI.getCallingConvention();
4411   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4412   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4413 
4414   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4415   // classification rules.
4416   if (CC == llvm::CallingConv::X86_64_SysV) {
4417     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4418     SysVABIInfo.computeInfo(FI);
4419     return;
4420   }
4421 
4422   unsigned FreeSSERegs = 0;
4423   if (IsVectorCall) {
4424     // We can use up to 4 SSE return registers with vectorcall.
4425     FreeSSERegs = 4;
4426   } else if (IsRegCall) {
4427     // RegCall gives us 16 SSE registers.
4428     FreeSSERegs = 16;
4429   }
4430 
4431   if (!getCXXABI().classifyReturnType(FI))
4432     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4433                                   IsVectorCall, IsRegCall);
4434 
4435   if (IsVectorCall) {
4436     // We can use up to 6 SSE register parameters with vectorcall.
4437     FreeSSERegs = 6;
4438   } else if (IsRegCall) {
4439     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4440     FreeSSERegs = 16;
4441   }
4442 
4443   unsigned ArgNum = 0;
4444   unsigned ZeroSSERegs = 0;
4445   for (auto &I : FI.arguments()) {
4446     // Vectorcall in x64 only permits the first 6 arguments to be passed as
4447     // XMM/YMM registers. After the sixth argument, pretend no vector
4448     // registers are left.
4449     unsigned *MaybeFreeSSERegs =
4450         (IsVectorCall && ArgNum >= 6) ? &ZeroSSERegs : &FreeSSERegs;
4451     I.info =
4452         classify(I.type, *MaybeFreeSSERegs, false, IsVectorCall, IsRegCall);
4453     ++ArgNum;
4454   }
4455 
4456   if (IsVectorCall) {
4457     // For vectorcall, assign aggregate HVAs to any free vector registers in a
4458     // second pass.
4459     for (auto &I : FI.arguments())
4460       I.info = reclassifyHvaArgForVectorCall(I.type, FreeSSERegs, I.info);
4461   }
4462 }
4463 
4464 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4465                                     QualType Ty) const {
4466   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4467   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4468   uint64_t Width = getContext().getTypeSize(Ty);
4469   bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4470 
4471   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4472                           CGF.getContext().getTypeInfoInChars(Ty),
4473                           CharUnits::fromQuantity(8),
4474                           /*allowHigherAlign*/ false);
4475 }
4476 
4477 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4478                                         llvm::Value *Address, bool Is64Bit,
4479                                         bool IsAIX) {
4480   // This is calculated from the LLVM and GCC tables and verified
4481   // against gcc output.  AFAIK all PPC ABIs use the same encoding.
4482 
4483   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4484 
4485   llvm::IntegerType *i8 = CGF.Int8Ty;
4486   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4487   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4488   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4489 
4490   // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
4491   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
4492 
4493   // 32-63: fp0-31, the 8-byte floating-point registers
4494   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4495 
4496   // 64-67 are various 4-byte or 8-byte special-purpose registers:
4497   // 64: mq
4498   // 65: lr
4499   // 66: ctr
4500   // 67: ap
4501   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
4502 
4503   // 68-76 are various 4-byte special-purpose registers:
4504   // 68-75 cr0-7
4505   // 76: xer
4506   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4507 
4508   // 77-108: v0-31, the 16-byte vector registers
4509   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4510 
4511   // 109: vrsave
4512   // 110: vscr
4513   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
4514 
4515   // AIX does not utilize the rest of the registers.
4516   if (IsAIX)
4517     return false;
4518 
4519   // 111: spe_acc
4520   // 112: spefscr
4521   // 113: sfp
4522   AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
4523 
4524   if (!Is64Bit)
4525     return false;
4526 
4527   // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
4528   // or above CPU.
4529   // 64-bit only registers:
4530   // 114: tfhar
4531   // 115: tfiar
4532   // 116: texasr
4533   AssignToArrayRange(Builder, Address, Eight8, 114, 116);
4534 
4535   return false;
4536 }
4537 
4538 // AIX
4539 namespace {
4540 /// AIXABIInfo - The AIX XCOFF ABI information.
4541 class AIXABIInfo : public ABIInfo {
4542   const bool Is64Bit;
4543   const unsigned PtrByteSize;
4544   CharUnits getParamTypeAlignment(QualType Ty) const;
4545 
4546 public:
4547   AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4548       : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
4549 
4550   bool isPromotableTypeForABI(QualType Ty) const;
4551 
4552   ABIArgInfo classifyReturnType(QualType RetTy) const;
4553   ABIArgInfo classifyArgumentType(QualType Ty) const;
4554 
4555   void computeInfo(CGFunctionInfo &FI) const override {
4556     if (!getCXXABI().classifyReturnType(FI))
4557       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4558 
4559     for (auto &I : FI.arguments())
4560       I.info = classifyArgumentType(I.type);
4561   }
4562 
4563   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4564                     QualType Ty) const override;
4565 };
4566 
4567 class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
4568   const bool Is64Bit;
4569 
4570 public:
4571   AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
4572       : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
4573         Is64Bit(Is64Bit) {}
4574   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4575     return 1; // r1 is the dedicated stack pointer
4576   }
4577 
4578   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4579                                llvm::Value *Address) const override;
4580 };
4581 } // namespace
4582 
4583 // Return true if the ABI requires Ty to be passed sign- or zero-
4584 // extended to 32/64 bits.
4585 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
4586   // Treat an enum type as its underlying type.
4587   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4588     Ty = EnumTy->getDecl()->getIntegerType();
4589 
4590   // Promotable integer types are required to be promoted by the ABI.
4591   if (getContext().isPromotableIntegerType(Ty))
4592     return true;
4593 
4594   if (!Is64Bit)
4595     return false;
4596 
4597   // For 64 bit mode, in addition to the usual promotable integer types, we also
4598   // need to extend all 32-bit types, since the ABI requires promotion to 64
4599   // bits.
4600   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4601     switch (BT->getKind()) {
4602     case BuiltinType::Int:
4603     case BuiltinType::UInt:
4604       return true;
4605     default:
4606       break;
4607     }
4608 
4609   return false;
4610 }
4611 
4612 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
4613   if (RetTy->isAnyComplexType())
4614     return ABIArgInfo::getDirect();
4615 
4616   if (RetTy->isVectorType())
4617     return ABIArgInfo::getDirect();
4618 
4619   if (RetTy->isVoidType())
4620     return ABIArgInfo::getIgnore();
4621 
4622   if (isAggregateTypeForABI(RetTy))
4623     return getNaturalAlignIndirect(RetTy);
4624 
4625   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4626                                         : ABIArgInfo::getDirect());
4627 }
4628 
4629 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
4630   Ty = useFirstFieldIfTransparentUnion(Ty);
4631 
4632   if (Ty->isAnyComplexType())
4633     return ABIArgInfo::getDirect();
4634 
4635   if (Ty->isVectorType())
4636     return ABIArgInfo::getDirect();
4637 
4638   if (isAggregateTypeForABI(Ty)) {
4639     // Records with non-trivial destructors/copy-constructors should not be
4640     // passed by value.
4641     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4642       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4643 
4644     CharUnits CCAlign = getParamTypeAlignment(Ty);
4645     CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
4646 
4647     return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
4648                                    /*Realign*/ TyAlign > CCAlign);
4649   }
4650 
4651   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4652                                      : ABIArgInfo::getDirect());
4653 }
4654 
4655 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
4656   // Complex types are passed just like their elements.
4657   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4658     Ty = CTy->getElementType();
4659 
4660   if (Ty->isVectorType())
4661     return CharUnits::fromQuantity(16);
4662 
4663   // If the structure contains a vector type, the alignment is 16.
4664   if (isRecordWithSIMDVectorType(getContext(), Ty))
4665     return CharUnits::fromQuantity(16);
4666 
4667   return CharUnits::fromQuantity(PtrByteSize);
4668 }
4669 
4670 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4671                               QualType Ty) const {
4672 
4673   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4674   TypeInfo.Align = getParamTypeAlignment(Ty);
4675 
4676   CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
4677 
4678   // If we have a complex type and the base type is smaller than the register
4679   // size, the ABI calls for the real and imaginary parts to be right-adjusted
4680   // in separate words in 32bit mode or doublewords in 64bit mode. However,
4681   // Clang expects us to produce a pointer to a structure with the two parts
4682   // packed tightly. So generate loads of the real and imaginary parts relative
4683   // to the va_list pointer, and store them to a temporary structure. We do the
4684   // same as the PPC64ABI here.
4685   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4686     CharUnits EltSize = TypeInfo.Width / 2;
4687     if (EltSize < SlotSize)
4688       return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
4689   }
4690 
4691   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
4692                           SlotSize, /*AllowHigher*/ true);
4693 }
4694 
4695 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
4696     CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
4697   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
4698 }
4699 
4700 // PowerPC-32
4701 namespace {
4702 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4703 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4704   bool IsSoftFloatABI;
4705   bool IsRetSmallStructInRegABI;
4706 
4707   CharUnits getParamTypeAlignment(QualType Ty) const;
4708 
4709 public:
4710   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
4711                      bool RetSmallStructInRegABI)
4712       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
4713         IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
4714 
4715   ABIArgInfo classifyReturnType(QualType RetTy) const;
4716 
4717   void computeInfo(CGFunctionInfo &FI) const override {
4718     if (!getCXXABI().classifyReturnType(FI))
4719       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4720     for (auto &I : FI.arguments())
4721       I.info = classifyArgumentType(I.type);
4722   }
4723 
4724   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4725                     QualType Ty) const override;
4726 };
4727 
4728 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4729 public:
4730   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
4731                          bool RetSmallStructInRegABI)
4732       : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
4733             CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
4734 
4735   static bool isStructReturnInRegABI(const llvm::Triple &Triple,
4736                                      const CodeGenOptions &Opts);
4737 
4738   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4739     // This is recovered from gcc output.
4740     return 1; // r1 is the dedicated stack pointer
4741   }
4742 
4743   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4744                                llvm::Value *Address) const override;
4745 };
4746 }
4747 
4748 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4749   // Complex types are passed just like their elements.
4750   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4751     Ty = CTy->getElementType();
4752 
4753   if (Ty->isVectorType())
4754     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4755                                                                        : 4);
4756 
4757   // For single-element float/vector structs, we consider the whole type
4758   // to have the same alignment requirements as its single element.
4759   const Type *AlignTy = nullptr;
4760   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4761     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4762     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4763         (BT && BT->isFloatingPoint()))
4764       AlignTy = EltType;
4765   }
4766 
4767   if (AlignTy)
4768     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4769   return CharUnits::fromQuantity(4);
4770 }
4771 
4772 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4773   uint64_t Size;
4774 
4775   // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
4776   if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
4777       (Size = getContext().getTypeSize(RetTy)) <= 64) {
4778     // System V ABI (1995), page 3-22, specified:
4779     // > A structure or union whose size is less than or equal to 8 bytes
4780     // > shall be returned in r3 and r4, as if it were first stored in the
4781     // > 8-byte aligned memory area and then the low addressed word were
4782     // > loaded into r3 and the high-addressed word into r4.  Bits beyond
4783     // > the last member of the structure or union are not defined.
4784     //
4785     // GCC for big-endian PPC32 inserts the pad before the first member,
4786     // not "beyond the last member" of the struct.  To stay compatible
4787     // with GCC, we coerce the struct to an integer of the same size.
4788     // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
4789     if (Size == 0)
4790       return ABIArgInfo::getIgnore();
4791     else {
4792       llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
4793       return ABIArgInfo::getDirect(CoerceTy);
4794     }
4795   }
4796 
4797   return DefaultABIInfo::classifyReturnType(RetTy);
4798 }
4799 
4800 // TODO: this implementation is now likely redundant with
4801 // DefaultABIInfo::EmitVAArg.
4802 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4803                                       QualType Ty) const {
4804   if (getTarget().getTriple().isOSDarwin()) {
4805     auto TI = getContext().getTypeInfoInChars(Ty);
4806     TI.Align = getParamTypeAlignment(Ty);
4807 
4808     CharUnits SlotSize = CharUnits::fromQuantity(4);
4809     return emitVoidPtrVAArg(CGF, VAList, Ty,
4810                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4811                             /*AllowHigherAlign=*/true);
4812   }
4813 
4814   const unsigned OverflowLimit = 8;
4815   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4816     // TODO: Implement this. For now ignore.
4817     (void)CTy;
4818     return Address::invalid(); // FIXME?
4819   }
4820 
4821   // struct __va_list_tag {
4822   //   unsigned char gpr;
4823   //   unsigned char fpr;
4824   //   unsigned short reserved;
4825   //   void *overflow_arg_area;
4826   //   void *reg_save_area;
4827   // };
4828 
4829   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4830   bool isInt = !Ty->isFloatingType();
4831   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4832 
4833   // All aggregates are passed indirectly?  That doesn't seem consistent
4834   // with the argument-lowering code.
4835   bool isIndirect = isAggregateTypeForABI(Ty);
4836 
4837   CGBuilderTy &Builder = CGF.Builder;
4838 
4839   // The calling convention either uses 1-2 GPRs or 1 FPR.
4840   Address NumRegsAddr = Address::invalid();
4841   if (isInt || IsSoftFloatABI) {
4842     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4843   } else {
4844     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4845   }
4846 
4847   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4848 
4849   // "Align" the register count when TY is i64.
4850   if (isI64 || (isF64 && IsSoftFloatABI)) {
4851     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4852     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4853   }
4854 
4855   llvm::Value *CC =
4856       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4857 
4858   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4859   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4860   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4861 
4862   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4863 
4864   llvm::Type *DirectTy = CGF.ConvertType(Ty), *ElementTy = DirectTy;
4865   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4866 
4867   // Case 1: consume registers.
4868   Address RegAddr = Address::invalid();
4869   {
4870     CGF.EmitBlock(UsingRegs);
4871 
4872     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4873     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), CGF.Int8Ty,
4874                       CharUnits::fromQuantity(8));
4875     assert(RegAddr.getElementType() == CGF.Int8Ty);
4876 
4877     // Floating-point registers start after the general-purpose registers.
4878     if (!(isInt || IsSoftFloatABI)) {
4879       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4880                                                    CharUnits::fromQuantity(32));
4881     }
4882 
4883     // Get the address of the saved value by scaling the number of
4884     // registers we've used by the number of
4885     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4886     llvm::Value *RegOffset =
4887         Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4888     RegAddr = Address(
4889         Builder.CreateInBoundsGEP(CGF.Int8Ty, RegAddr.getPointer(), RegOffset),
4890         CGF.Int8Ty, RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4891     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4892 
4893     // Increase the used-register count.
4894     NumRegs =
4895       Builder.CreateAdd(NumRegs,
4896                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4897     Builder.CreateStore(NumRegs, NumRegsAddr);
4898 
4899     CGF.EmitBranch(Cont);
4900   }
4901 
4902   // Case 2: consume space in the overflow area.
4903   Address MemAddr = Address::invalid();
4904   {
4905     CGF.EmitBlock(UsingOverflow);
4906 
4907     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4908 
4909     // Everything in the overflow area is rounded up to a size of at least 4.
4910     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4911 
4912     CharUnits Size;
4913     if (!isIndirect) {
4914       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4915       Size = TypeInfo.Width.alignTo(OverflowAreaAlign);
4916     } else {
4917       Size = CGF.getPointerSize();
4918     }
4919 
4920     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4921     Address OverflowArea =
4922         Address(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), CGF.Int8Ty,
4923                 OverflowAreaAlign);
4924     // Round up address of argument to alignment
4925     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4926     if (Align > OverflowAreaAlign) {
4927       llvm::Value *Ptr = OverflowArea.getPointer();
4928       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4929                              OverflowArea.getElementType(), Align);
4930     }
4931 
4932     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4933 
4934     // Increase the overflow area.
4935     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4936     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4937     CGF.EmitBranch(Cont);
4938   }
4939 
4940   CGF.EmitBlock(Cont);
4941 
4942   // Merge the cases with a phi.
4943   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4944                                 "vaarg.addr");
4945 
4946   // Load the pointer if the argument was passed indirectly.
4947   if (isIndirect) {
4948     Result = Address(Builder.CreateLoad(Result, "aggr"), ElementTy,
4949                      getContext().getTypeAlignInChars(Ty));
4950   }
4951 
4952   return Result;
4953 }
4954 
4955 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
4956     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4957   assert(Triple.isPPC32());
4958 
4959   switch (Opts.getStructReturnConvention()) {
4960   case CodeGenOptions::SRCK_Default:
4961     break;
4962   case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
4963     return false;
4964   case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
4965     return true;
4966   }
4967 
4968   if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
4969     return true;
4970 
4971   return false;
4972 }
4973 
4974 bool
4975 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4976                                                 llvm::Value *Address) const {
4977   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
4978                                      /*IsAIX*/ false);
4979 }
4980 
4981 // PowerPC-64
4982 
4983 namespace {
4984 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4985 class PPC64_SVR4_ABIInfo : public ABIInfo {
4986 public:
4987   enum ABIKind {
4988     ELFv1 = 0,
4989     ELFv2
4990   };
4991 
4992 private:
4993   static const unsigned GPRBits = 64;
4994   ABIKind Kind;
4995   bool IsSoftFloatABI;
4996 
4997 public:
4998   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind,
4999                      bool SoftFloatABI)
5000       : ABIInfo(CGT), Kind(Kind), IsSoftFloatABI(SoftFloatABI) {}
5001 
5002   bool isPromotableTypeForABI(QualType Ty) const;
5003   CharUnits getParamTypeAlignment(QualType Ty) const;
5004 
5005   ABIArgInfo classifyReturnType(QualType RetTy) const;
5006   ABIArgInfo classifyArgumentType(QualType Ty) const;
5007 
5008   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5009   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5010                                          uint64_t Members) const override;
5011 
5012   // TODO: We can add more logic to computeInfo to improve performance.
5013   // Example: For aggregate arguments that fit in a register, we could
5014   // use getDirectInReg (as is done below for structs containing a single
5015   // floating-point value) to avoid pushing them to memory on function
5016   // entry.  This would require changing the logic in PPCISelLowering
5017   // when lowering the parameters in the caller and args in the callee.
5018   void computeInfo(CGFunctionInfo &FI) const override {
5019     if (!getCXXABI().classifyReturnType(FI))
5020       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5021     for (auto &I : FI.arguments()) {
5022       // We rely on the default argument classification for the most part.
5023       // One exception:  An aggregate containing a single floating-point
5024       // or vector item must be passed in a register if one is available.
5025       const Type *T = isSingleElementStruct(I.type, getContext());
5026       if (T) {
5027         const BuiltinType *BT = T->getAs<BuiltinType>();
5028         if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
5029             (BT && BT->isFloatingPoint())) {
5030           QualType QT(T, 0);
5031           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
5032           continue;
5033         }
5034       }
5035       I.info = classifyArgumentType(I.type);
5036     }
5037   }
5038 
5039   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5040                     QualType Ty) const override;
5041 };
5042 
5043 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
5044 
5045 public:
5046   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
5047                                PPC64_SVR4_ABIInfo::ABIKind Kind,
5048                                bool SoftFloatABI)
5049       : TargetCodeGenInfo(
5050             std::make_unique<PPC64_SVR4_ABIInfo>(CGT, Kind, SoftFloatABI)) {
5051     SwiftInfo =
5052         std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
5053   }
5054 
5055   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5056     // This is recovered from gcc output.
5057     return 1; // r1 is the dedicated stack pointer
5058   }
5059 
5060   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5061                                llvm::Value *Address) const override;
5062 };
5063 
5064 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
5065 public:
5066   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
5067 
5068   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5069     // This is recovered from gcc output.
5070     return 1; // r1 is the dedicated stack pointer
5071   }
5072 
5073   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5074                                llvm::Value *Address) const override;
5075 };
5076 
5077 }
5078 
5079 // Return true if the ABI requires Ty to be passed sign- or zero-
5080 // extended to 64 bits.
5081 bool
5082 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
5083   // Treat an enum type as its underlying type.
5084   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5085     Ty = EnumTy->getDecl()->getIntegerType();
5086 
5087   // Promotable integer types are required to be promoted by the ABI.
5088   if (isPromotableIntegerTypeForABI(Ty))
5089     return true;
5090 
5091   // In addition to the usual promotable integer types, we also need to
5092   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
5093   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5094     switch (BT->getKind()) {
5095     case BuiltinType::Int:
5096     case BuiltinType::UInt:
5097       return true;
5098     default:
5099       break;
5100     }
5101 
5102   if (const auto *EIT = Ty->getAs<BitIntType>())
5103     if (EIT->getNumBits() < 64)
5104       return true;
5105 
5106   return false;
5107 }
5108 
5109 /// isAlignedParamType - Determine whether a type requires 16-byte or
5110 /// higher alignment in the parameter area.  Always returns at least 8.
5111 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
5112   // Complex types are passed just like their elements.
5113   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
5114     Ty = CTy->getElementType();
5115 
5116   auto FloatUsesVector = [this](QualType Ty){
5117     return Ty->isRealFloatingType() && &getContext().getFloatTypeSemantics(
5118                                            Ty) == &llvm::APFloat::IEEEquad();
5119   };
5120 
5121   // Only vector types of size 16 bytes need alignment (larger types are
5122   // passed via reference, smaller types are not aligned).
5123   if (Ty->isVectorType()) {
5124     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5125   } else if (FloatUsesVector(Ty)) {
5126     // According to ABI document section 'Optional Save Areas': If extended
5127     // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
5128     // format are supported, map them to a single quadword, quadword aligned.
5129     return CharUnits::fromQuantity(16);
5130   }
5131 
5132   // For single-element float/vector structs, we consider the whole type
5133   // to have the same alignment requirements as its single element.
5134   const Type *AlignAsType = nullptr;
5135   const Type *EltType = isSingleElementStruct(Ty, getContext());
5136   if (EltType) {
5137     const BuiltinType *BT = EltType->getAs<BuiltinType>();
5138     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
5139         (BT && BT->isFloatingPoint()))
5140       AlignAsType = EltType;
5141   }
5142 
5143   // Likewise for ELFv2 homogeneous aggregates.
5144   const Type *Base = nullptr;
5145   uint64_t Members = 0;
5146   if (!AlignAsType && Kind == ELFv2 &&
5147       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
5148     AlignAsType = Base;
5149 
5150   // With special case aggregates, only vector base types need alignment.
5151   if (AlignAsType) {
5152     bool UsesVector = AlignAsType->isVectorType() ||
5153                       FloatUsesVector(QualType(AlignAsType, 0));
5154     return CharUnits::fromQuantity(UsesVector ? 16 : 8);
5155   }
5156 
5157   // Otherwise, we only need alignment for any aggregate type that
5158   // has an alignment requirement of >= 16 bytes.
5159   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
5160     return CharUnits::fromQuantity(16);
5161   }
5162 
5163   return CharUnits::fromQuantity(8);
5164 }
5165 
5166 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
5167 /// aggregate.  Base is set to the base element type, and Members is set
5168 /// to the number of base elements.
5169 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
5170                                      uint64_t &Members) const {
5171   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
5172     uint64_t NElements = AT->getSize().getZExtValue();
5173     if (NElements == 0)
5174       return false;
5175     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
5176       return false;
5177     Members *= NElements;
5178   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
5179     const RecordDecl *RD = RT->getDecl();
5180     if (RD->hasFlexibleArrayMember())
5181       return false;
5182 
5183     Members = 0;
5184 
5185     // If this is a C++ record, check the properties of the record such as
5186     // bases and ABI specific restrictions
5187     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5188       if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
5189         return false;
5190 
5191       for (const auto &I : CXXRD->bases()) {
5192         // Ignore empty records.
5193         if (isEmptyRecord(getContext(), I.getType(), true))
5194           continue;
5195 
5196         uint64_t FldMembers;
5197         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
5198           return false;
5199 
5200         Members += FldMembers;
5201       }
5202     }
5203 
5204     for (const auto *FD : RD->fields()) {
5205       // Ignore (non-zero arrays of) empty records.
5206       QualType FT = FD->getType();
5207       while (const ConstantArrayType *AT =
5208              getContext().getAsConstantArrayType(FT)) {
5209         if (AT->getSize().getZExtValue() == 0)
5210           return false;
5211         FT = AT->getElementType();
5212       }
5213       if (isEmptyRecord(getContext(), FT, true))
5214         continue;
5215 
5216       if (isZeroLengthBitfieldPermittedInHomogeneousAggregate() &&
5217           FD->isZeroLengthBitField(getContext()))
5218         continue;
5219 
5220       uint64_t FldMembers;
5221       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
5222         return false;
5223 
5224       Members = (RD->isUnion() ?
5225                  std::max(Members, FldMembers) : Members + FldMembers);
5226     }
5227 
5228     if (!Base)
5229       return false;
5230 
5231     // Ensure there is no padding.
5232     if (getContext().getTypeSize(Base) * Members !=
5233         getContext().getTypeSize(Ty))
5234       return false;
5235   } else {
5236     Members = 1;
5237     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
5238       Members = 2;
5239       Ty = CT->getElementType();
5240     }
5241 
5242     // Most ABIs only support float, double, and some vector type widths.
5243     if (!isHomogeneousAggregateBaseType(Ty))
5244       return false;
5245 
5246     // The base type must be the same for all members.  Types that
5247     // agree in both total size and mode (float vs. vector) are
5248     // treated as being equivalent here.
5249     const Type *TyPtr = Ty.getTypePtr();
5250     if (!Base) {
5251       Base = TyPtr;
5252       // If it's a non-power-of-2 vector, its size is already a power-of-2,
5253       // so make sure to widen it explicitly.
5254       if (const VectorType *VT = Base->getAs<VectorType>()) {
5255         QualType EltTy = VT->getElementType();
5256         unsigned NumElements =
5257             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
5258         Base = getContext()
5259                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
5260                    .getTypePtr();
5261       }
5262     }
5263 
5264     if (Base->isVectorType() != TyPtr->isVectorType() ||
5265         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
5266       return false;
5267   }
5268   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
5269 }
5270 
5271 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5272   // Homogeneous aggregates for ELFv2 must have base types of float,
5273   // double, long double, or 128-bit vectors.
5274   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5275     if (BT->getKind() == BuiltinType::Float ||
5276         BT->getKind() == BuiltinType::Double ||
5277         BT->getKind() == BuiltinType::LongDouble ||
5278         BT->getKind() == BuiltinType::Ibm128 ||
5279         (getContext().getTargetInfo().hasFloat128Type() &&
5280          (BT->getKind() == BuiltinType::Float128))) {
5281       if (IsSoftFloatABI)
5282         return false;
5283       return true;
5284     }
5285   }
5286   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5287     if (getContext().getTypeSize(VT) == 128)
5288       return true;
5289   }
5290   return false;
5291 }
5292 
5293 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
5294     const Type *Base, uint64_t Members) const {
5295   // Vector and fp128 types require one register, other floating point types
5296   // require one or two registers depending on their size.
5297   uint32_t NumRegs =
5298       ((getContext().getTargetInfo().hasFloat128Type() &&
5299           Base->isFloat128Type()) ||
5300         Base->isVectorType()) ? 1
5301                               : (getContext().getTypeSize(Base) + 63) / 64;
5302 
5303   // Homogeneous Aggregates may occupy at most 8 registers.
5304   return Members * NumRegs <= 8;
5305 }
5306 
5307 ABIArgInfo
5308 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
5309   Ty = useFirstFieldIfTransparentUnion(Ty);
5310 
5311   if (Ty->isAnyComplexType())
5312     return ABIArgInfo::getDirect();
5313 
5314   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
5315   // or via reference (larger than 16 bytes).
5316   if (Ty->isVectorType()) {
5317     uint64_t Size = getContext().getTypeSize(Ty);
5318     if (Size > 128)
5319       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5320     else if (Size < 128) {
5321       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5322       return ABIArgInfo::getDirect(CoerceTy);
5323     }
5324   }
5325 
5326   if (const auto *EIT = Ty->getAs<BitIntType>())
5327     if (EIT->getNumBits() > 128)
5328       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
5329 
5330   if (isAggregateTypeForABI(Ty)) {
5331     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5332       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5333 
5334     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
5335     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5336 
5337     // ELFv2 homogeneous aggregates are passed as array types.
5338     const Type *Base = nullptr;
5339     uint64_t Members = 0;
5340     if (Kind == ELFv2 &&
5341         isHomogeneousAggregate(Ty, Base, Members)) {
5342       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5343       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5344       return ABIArgInfo::getDirect(CoerceTy);
5345     }
5346 
5347     // If an aggregate may end up fully in registers, we do not
5348     // use the ByVal method, but pass the aggregate as array.
5349     // This is usually beneficial since we avoid forcing the
5350     // back-end to store the argument to memory.
5351     uint64_t Bits = getContext().getTypeSize(Ty);
5352     if (Bits > 0 && Bits <= 8 * GPRBits) {
5353       llvm::Type *CoerceTy;
5354 
5355       // Types up to 8 bytes are passed as integer type (which will be
5356       // properly aligned in the argument save area doubleword).
5357       if (Bits <= GPRBits)
5358         CoerceTy =
5359             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5360       // Larger types are passed as arrays, with the base type selected
5361       // according to the required alignment in the save area.
5362       else {
5363         uint64_t RegBits = ABIAlign * 8;
5364         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
5365         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
5366         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
5367       }
5368 
5369       return ABIArgInfo::getDirect(CoerceTy);
5370     }
5371 
5372     // All other aggregates are passed ByVal.
5373     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5374                                    /*ByVal=*/true,
5375                                    /*Realign=*/TyAlign > ABIAlign);
5376   }
5377 
5378   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
5379                                      : ABIArgInfo::getDirect());
5380 }
5381 
5382 ABIArgInfo
5383 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
5384   if (RetTy->isVoidType())
5385     return ABIArgInfo::getIgnore();
5386 
5387   if (RetTy->isAnyComplexType())
5388     return ABIArgInfo::getDirect();
5389 
5390   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
5391   // or via reference (larger than 16 bytes).
5392   if (RetTy->isVectorType()) {
5393     uint64_t Size = getContext().getTypeSize(RetTy);
5394     if (Size > 128)
5395       return getNaturalAlignIndirect(RetTy);
5396     else if (Size < 128) {
5397       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
5398       return ABIArgInfo::getDirect(CoerceTy);
5399     }
5400   }
5401 
5402   if (const auto *EIT = RetTy->getAs<BitIntType>())
5403     if (EIT->getNumBits() > 128)
5404       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
5405 
5406   if (isAggregateTypeForABI(RetTy)) {
5407     // ELFv2 homogeneous aggregates are returned as array types.
5408     const Type *Base = nullptr;
5409     uint64_t Members = 0;
5410     if (Kind == ELFv2 &&
5411         isHomogeneousAggregate(RetTy, Base, Members)) {
5412       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
5413       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
5414       return ABIArgInfo::getDirect(CoerceTy);
5415     }
5416 
5417     // ELFv2 small aggregates are returned in up to two registers.
5418     uint64_t Bits = getContext().getTypeSize(RetTy);
5419     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
5420       if (Bits == 0)
5421         return ABIArgInfo::getIgnore();
5422 
5423       llvm::Type *CoerceTy;
5424       if (Bits > GPRBits) {
5425         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
5426         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
5427       } else
5428         CoerceTy =
5429             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
5430       return ABIArgInfo::getDirect(CoerceTy);
5431     }
5432 
5433     // All other aggregates are returned indirectly.
5434     return getNaturalAlignIndirect(RetTy);
5435   }
5436 
5437   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
5438                                         : ABIArgInfo::getDirect());
5439 }
5440 
5441 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
5442 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5443                                       QualType Ty) const {
5444   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
5445   TypeInfo.Align = getParamTypeAlignment(Ty);
5446 
5447   CharUnits SlotSize = CharUnits::fromQuantity(8);
5448 
5449   // If we have a complex type and the base type is smaller than 8 bytes,
5450   // the ABI calls for the real and imaginary parts to be right-adjusted
5451   // in separate doublewords.  However, Clang expects us to produce a
5452   // pointer to a structure with the two parts packed tightly.  So generate
5453   // loads of the real and imaginary parts relative to the va_list pointer,
5454   // and store them to a temporary structure.
5455   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
5456     CharUnits EltSize = TypeInfo.Width / 2;
5457     if (EltSize < SlotSize)
5458       return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
5459   }
5460 
5461   // Otherwise, just use the general rule.
5462   //
5463   // The PPC64 ABI passes some arguments in integer registers, even to variadic
5464   // functions. To allow va_list to use the simple "void*" representation,
5465   // variadic calls allocate space in the argument area for the integer argument
5466   // registers, and variadic functions spill their integer argument registers to
5467   // this area in their prologues. When aggregates smaller than a register are
5468   // passed this way, they are passed in the least significant bits of the
5469   // register, which means that after spilling on big-endian targets they will
5470   // be right-aligned in their argument slot. This is uncommon; for a variety of
5471   // reasons, other big-endian targets don't end up right-aligning aggregate
5472   // types this way, and so right-alignment only applies to fundamental types.
5473   // So on PPC64, we must force the use of right-alignment even for aggregates.
5474   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
5475                           SlotSize, /*AllowHigher*/ true,
5476                           /*ForceRightAdjust*/ true);
5477 }
5478 
5479 bool
5480 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
5481   CodeGen::CodeGenFunction &CGF,
5482   llvm::Value *Address) const {
5483   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5484                                      /*IsAIX*/ false);
5485 }
5486 
5487 bool
5488 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5489                                                 llvm::Value *Address) const {
5490   return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
5491                                      /*IsAIX*/ false);
5492 }
5493 
5494 //===----------------------------------------------------------------------===//
5495 // AArch64 ABI Implementation
5496 //===----------------------------------------------------------------------===//
5497 
5498 namespace {
5499 
5500 class AArch64ABIInfo : public ABIInfo {
5501 public:
5502   enum ABIKind {
5503     AAPCS = 0,
5504     DarwinPCS,
5505     Win64
5506   };
5507 
5508 private:
5509   ABIKind Kind;
5510 
5511 public:
5512   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
5513 
5514 private:
5515   ABIKind getABIKind() const { return Kind; }
5516   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5517 
5518   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5519   ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
5520                                   unsigned CallingConvention) const;
5521   ABIArgInfo coerceIllegalVector(QualType Ty) const;
5522   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5523   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5524                                          uint64_t Members) const override;
5525   bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
5526 
5527   bool isIllegalVectorType(QualType Ty) const;
5528 
5529   void computeInfo(CGFunctionInfo &FI) const override {
5530     if (!::classifyReturnType(getCXXABI(), FI, *this))
5531       FI.getReturnInfo() =
5532           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5533 
5534     for (auto &it : FI.arguments())
5535       it.info = classifyArgumentType(it.type, FI.isVariadic(),
5536                                      FI.getCallingConvention());
5537   }
5538 
5539   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5540                           CodeGenFunction &CGF) const;
5541 
5542   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5543                          CodeGenFunction &CGF) const;
5544 
5545   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5546                     QualType Ty) const override {
5547     llvm::Type *BaseTy = CGF.ConvertType(Ty);
5548     if (isa<llvm::ScalableVectorType>(BaseTy))
5549       llvm::report_fatal_error("Passing SVE types to variadic functions is "
5550                                "currently not supported");
5551 
5552     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5553                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5554                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5555   }
5556 
5557   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5558                       QualType Ty) const override;
5559 
5560   bool allowBFloatArgsAndRet() const override {
5561     return getTarget().hasBFloat16Type();
5562   }
5563 };
5564 
5565 class AArch64SwiftABIInfo : public SwiftABIInfo {
5566 public:
5567   explicit AArch64SwiftABIInfo(CodeGenTypes &CGT)
5568       : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/true) {}
5569 
5570   bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
5571                          unsigned NumElts) const override;
5572 };
5573 
5574 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5575 public:
5576   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5577       : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {
5578     SwiftInfo = std::make_unique<AArch64SwiftABIInfo>(CGT);
5579   }
5580 
5581   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5582     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5583   }
5584 
5585   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5586     return 31;
5587   }
5588 
5589   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5590 
5591   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5592                            CodeGen::CodeGenModule &CGM) const override {
5593     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5594     if (!FD)
5595       return;
5596 
5597     const auto *TA = FD->getAttr<TargetAttr>();
5598     if (TA == nullptr)
5599       return;
5600 
5601     ParsedTargetAttr Attr =
5602         CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
5603     if (Attr.BranchProtection.empty())
5604       return;
5605 
5606     TargetInfo::BranchProtectionInfo BPI;
5607     StringRef Error;
5608     (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
5609                                                    Attr.CPU, BPI, Error);
5610     assert(Error.empty());
5611 
5612     auto *Fn = cast<llvm::Function>(GV);
5613     static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
5614     Fn->addFnAttr("sign-return-address", SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
5615 
5616     if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
5617       Fn->addFnAttr("sign-return-address-key",
5618                     BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
5619                         ? "a_key"
5620                         : "b_key");
5621     }
5622 
5623     Fn->addFnAttr("branch-target-enforcement",
5624                   BPI.BranchTargetEnforcement ? "true" : "false");
5625   }
5626 
5627   bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
5628                                 llvm::Type *Ty) const override {
5629     if (CGF.getTarget().hasFeature("ls64")) {
5630       auto *ST = dyn_cast<llvm::StructType>(Ty);
5631       if (ST && ST->getNumElements() == 1) {
5632         auto *AT = dyn_cast<llvm::ArrayType>(ST->getElementType(0));
5633         if (AT && AT->getNumElements() == 8 &&
5634             AT->getElementType()->isIntegerTy(64))
5635           return true;
5636       }
5637     }
5638     return TargetCodeGenInfo::isScalarizableAsmOperand(CGF, Ty);
5639   }
5640 };
5641 
5642 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5643 public:
5644   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5645       : AArch64TargetCodeGenInfo(CGT, K) {}
5646 
5647   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5648                            CodeGen::CodeGenModule &CGM) const override;
5649 
5650   void getDependentLibraryOption(llvm::StringRef Lib,
5651                                  llvm::SmallString<24> &Opt) const override {
5652     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5653   }
5654 
5655   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5656                                llvm::SmallString<32> &Opt) const override {
5657     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5658   }
5659 };
5660 
5661 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5662     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5663   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5664   if (GV->isDeclaration())
5665     return;
5666   addStackProbeTargetAttributes(D, GV, CGM);
5667 }
5668 }
5669 
5670 ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty) const {
5671   assert(Ty->isVectorType() && "expected vector type!");
5672 
5673   const auto *VT = Ty->castAs<VectorType>();
5674   if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
5675     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5676     assert(VT->getElementType()->castAs<BuiltinType>()->getKind() ==
5677                BuiltinType::UChar &&
5678            "unexpected builtin type for SVE predicate!");
5679     return ABIArgInfo::getDirect(llvm::ScalableVectorType::get(
5680         llvm::Type::getInt1Ty(getVMContext()), 16));
5681   }
5682 
5683   if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) {
5684     assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
5685 
5686     const auto *BT = VT->getElementType()->castAs<BuiltinType>();
5687     llvm::ScalableVectorType *ResType = nullptr;
5688     switch (BT->getKind()) {
5689     default:
5690       llvm_unreachable("unexpected builtin type for SVE vector!");
5691     case BuiltinType::SChar:
5692     case BuiltinType::UChar:
5693       ResType = llvm::ScalableVectorType::get(
5694           llvm::Type::getInt8Ty(getVMContext()), 16);
5695       break;
5696     case BuiltinType::Short:
5697     case BuiltinType::UShort:
5698       ResType = llvm::ScalableVectorType::get(
5699           llvm::Type::getInt16Ty(getVMContext()), 8);
5700       break;
5701     case BuiltinType::Int:
5702     case BuiltinType::UInt:
5703       ResType = llvm::ScalableVectorType::get(
5704           llvm::Type::getInt32Ty(getVMContext()), 4);
5705       break;
5706     case BuiltinType::Long:
5707     case BuiltinType::ULong:
5708       ResType = llvm::ScalableVectorType::get(
5709           llvm::Type::getInt64Ty(getVMContext()), 2);
5710       break;
5711     case BuiltinType::Half:
5712       ResType = llvm::ScalableVectorType::get(
5713           llvm::Type::getHalfTy(getVMContext()), 8);
5714       break;
5715     case BuiltinType::Float:
5716       ResType = llvm::ScalableVectorType::get(
5717           llvm::Type::getFloatTy(getVMContext()), 4);
5718       break;
5719     case BuiltinType::Double:
5720       ResType = llvm::ScalableVectorType::get(
5721           llvm::Type::getDoubleTy(getVMContext()), 2);
5722       break;
5723     case BuiltinType::BFloat16:
5724       ResType = llvm::ScalableVectorType::get(
5725           llvm::Type::getBFloatTy(getVMContext()), 8);
5726       break;
5727     }
5728     return ABIArgInfo::getDirect(ResType);
5729   }
5730 
5731   uint64_t Size = getContext().getTypeSize(Ty);
5732   // Android promotes <2 x i8> to i16, not i32
5733   if (isAndroid() && (Size <= 16)) {
5734     llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5735     return ABIArgInfo::getDirect(ResType);
5736   }
5737   if (Size <= 32) {
5738     llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5739     return ABIArgInfo::getDirect(ResType);
5740   }
5741   if (Size == 64) {
5742     auto *ResType =
5743         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5744     return ABIArgInfo::getDirect(ResType);
5745   }
5746   if (Size == 128) {
5747     auto *ResType =
5748         llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5749     return ABIArgInfo::getDirect(ResType);
5750   }
5751   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5752 }
5753 
5754 ABIArgInfo
5755 AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadic,
5756                                      unsigned CallingConvention) const {
5757   Ty = useFirstFieldIfTransparentUnion(Ty);
5758 
5759   // Handle illegal vector types here.
5760   if (isIllegalVectorType(Ty))
5761     return coerceIllegalVector(Ty);
5762 
5763   if (!isAggregateTypeForABI(Ty)) {
5764     // Treat an enum type as its underlying type.
5765     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5766       Ty = EnumTy->getDecl()->getIntegerType();
5767 
5768     if (const auto *EIT = Ty->getAs<BitIntType>())
5769       if (EIT->getNumBits() > 128)
5770         return getNaturalAlignIndirect(Ty);
5771 
5772     return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
5773                 ? ABIArgInfo::getExtend(Ty)
5774                 : ABIArgInfo::getDirect());
5775   }
5776 
5777   // Structures with either a non-trivial destructor or a non-trivial
5778   // copy constructor are always indirect.
5779   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5780     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5781                                      CGCXXABI::RAA_DirectInMemory);
5782   }
5783 
5784   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5785   // elsewhere for GNU compatibility.
5786   uint64_t Size = getContext().getTypeSize(Ty);
5787   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5788   if (IsEmpty || Size == 0) {
5789     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5790       return ABIArgInfo::getIgnore();
5791 
5792     // GNU C mode. The only argument that gets ignored is an empty one with size
5793     // 0.
5794     if (IsEmpty && Size == 0)
5795       return ABIArgInfo::getIgnore();
5796     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5797   }
5798 
5799   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5800   const Type *Base = nullptr;
5801   uint64_t Members = 0;
5802   bool IsWin64 = Kind == Win64 || CallingConvention == llvm::CallingConv::Win64;
5803   bool IsWinVariadic = IsWin64 && IsVariadic;
5804   // In variadic functions on Windows, all composite types are treated alike,
5805   // no special handling of HFAs/HVAs.
5806   if (!IsWinVariadic && isHomogeneousAggregate(Ty, Base, Members)) {
5807     if (Kind != AArch64ABIInfo::AAPCS)
5808       return ABIArgInfo::getDirect(
5809           llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5810 
5811     // For alignment adjusted HFAs, cap the argument alignment to 16, leave it
5812     // default otherwise.
5813     unsigned Align =
5814         getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
5815     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
5816     Align = (Align > BaseAlign && Align >= 16) ? 16 : 0;
5817     return ABIArgInfo::getDirect(
5818         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members), 0,
5819         nullptr, true, Align);
5820   }
5821 
5822   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5823   if (Size <= 128) {
5824     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5825     // same size and alignment.
5826     if (getTarget().isRenderScriptTarget()) {
5827       return coerceToIntArray(Ty, getContext(), getVMContext());
5828     }
5829     unsigned Alignment;
5830     if (Kind == AArch64ABIInfo::AAPCS) {
5831       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5832       Alignment = Alignment < 128 ? 64 : 128;
5833     } else {
5834       Alignment =
5835           std::max(getContext().getTypeAlign(Ty),
5836                    (unsigned)getTarget().getPointerWidth(LangAS::Default));
5837     }
5838     Size = llvm::alignTo(Size, Alignment);
5839 
5840     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5841     // For aggregates with 16-byte alignment, we use i128.
5842     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5843     return ABIArgInfo::getDirect(
5844         Size == Alignment ? BaseTy
5845                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5846   }
5847 
5848   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5849 }
5850 
5851 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5852                                               bool IsVariadic) const {
5853   if (RetTy->isVoidType())
5854     return ABIArgInfo::getIgnore();
5855 
5856   if (const auto *VT = RetTy->getAs<VectorType>()) {
5857     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5858         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5859       return coerceIllegalVector(RetTy);
5860   }
5861 
5862   // Large vector types should be returned via memory.
5863   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5864     return getNaturalAlignIndirect(RetTy);
5865 
5866   if (!isAggregateTypeForABI(RetTy)) {
5867     // Treat an enum type as its underlying type.
5868     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5869       RetTy = EnumTy->getDecl()->getIntegerType();
5870 
5871     if (const auto *EIT = RetTy->getAs<BitIntType>())
5872       if (EIT->getNumBits() > 128)
5873         return getNaturalAlignIndirect(RetTy);
5874 
5875     return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
5876                 ? ABIArgInfo::getExtend(RetTy)
5877                 : ABIArgInfo::getDirect());
5878   }
5879 
5880   uint64_t Size = getContext().getTypeSize(RetTy);
5881   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5882     return ABIArgInfo::getIgnore();
5883 
5884   const Type *Base = nullptr;
5885   uint64_t Members = 0;
5886   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5887       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5888         IsVariadic))
5889     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5890     return ABIArgInfo::getDirect();
5891 
5892   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5893   if (Size <= 128) {
5894     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5895     // same size and alignment.
5896     if (getTarget().isRenderScriptTarget()) {
5897       return coerceToIntArray(RetTy, getContext(), getVMContext());
5898     }
5899 
5900     if (Size <= 64 && getDataLayout().isLittleEndian()) {
5901       // Composite types are returned in lower bits of a 64-bit register for LE,
5902       // and in higher bits for BE. However, integer types are always returned
5903       // in lower bits for both LE and BE, and they are not rounded up to
5904       // 64-bits. We can skip rounding up of composite types for LE, but not for
5905       // BE, otherwise composite types will be indistinguishable from integer
5906       // types.
5907       return ABIArgInfo::getDirect(
5908           llvm::IntegerType::get(getVMContext(), Size));
5909     }
5910 
5911     unsigned Alignment = getContext().getTypeAlign(RetTy);
5912     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5913 
5914     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5915     // For aggregates with 16-byte alignment, we use i128.
5916     if (Alignment < 128 && Size == 128) {
5917       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5918       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5919     }
5920     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5921   }
5922 
5923   return getNaturalAlignIndirect(RetTy);
5924 }
5925 
5926 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5927 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5928   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5929     // Check whether VT is a fixed-length SVE vector. These types are
5930     // represented as scalable vectors in function args/return and must be
5931     // coerced from fixed vectors.
5932     if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
5933         VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
5934       return true;
5935 
5936     // Check whether VT is legal.
5937     unsigned NumElements = VT->getNumElements();
5938     uint64_t Size = getContext().getTypeSize(VT);
5939     // NumElements should be power of 2.
5940     if (!llvm::isPowerOf2_32(NumElements))
5941       return true;
5942 
5943     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5944     // vectors for some reason.
5945     llvm::Triple Triple = getTarget().getTriple();
5946     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5947         Triple.isOSBinFormatMachO())
5948       return Size <= 32;
5949 
5950     return Size != 64 && (Size != 128 || NumElements == 1);
5951   }
5952   return false;
5953 }
5954 
5955 bool AArch64SwiftABIInfo::isLegalVectorType(CharUnits VectorSize,
5956                                             llvm::Type *EltTy,
5957                                             unsigned NumElts) const {
5958   if (!llvm::isPowerOf2_32(NumElts))
5959     return false;
5960   if (VectorSize.getQuantity() != 8 &&
5961       (VectorSize.getQuantity() != 16 || NumElts == 1))
5962     return false;
5963   return true;
5964 }
5965 
5966 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5967   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5968   // point type or a short-vector type. This is the same as the 32-bit ABI,
5969   // but with the difference that any floating-point type is allowed,
5970   // including __fp16.
5971   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5972     if (BT->isFloatingPoint())
5973       return true;
5974   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5975     unsigned VecSize = getContext().getTypeSize(VT);
5976     if (VecSize == 64 || VecSize == 128)
5977       return true;
5978   }
5979   return false;
5980 }
5981 
5982 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5983                                                        uint64_t Members) const {
5984   return Members <= 4;
5985 }
5986 
5987 bool AArch64ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate()
5988     const {
5989   // AAPCS64 says that the rule for whether something is a homogeneous
5990   // aggregate is applied to the output of the data layout decision. So
5991   // anything that doesn't affect the data layout also does not affect
5992   // homogeneity. In particular, zero-length bitfields don't stop a struct
5993   // being homogeneous.
5994   return true;
5995 }
5996 
5997 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5998                                        CodeGenFunction &CGF) const {
5999   ABIArgInfo AI = classifyArgumentType(Ty, /*IsVariadic=*/true,
6000                                        CGF.CurFnInfo->getCallingConvention());
6001   // Empty records are ignored for parameter passing purposes.
6002   if (AI.isIgnore()) {
6003     uint64_t PointerSize = getTarget().getPointerWidth(LangAS::Default) / 8;
6004     CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6005     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
6006     auto *Load = CGF.Builder.CreateLoad(VAListAddr);
6007     Address Addr = Address(Load, CGF.Int8Ty, SlotSize);
6008     return CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6009   }
6010 
6011   bool IsIndirect = AI.isIndirect();
6012 
6013   llvm::Type *BaseTy = CGF.ConvertType(Ty);
6014   if (IsIndirect)
6015     BaseTy = llvm::PointerType::getUnqual(BaseTy);
6016   else if (AI.getCoerceToType())
6017     BaseTy = AI.getCoerceToType();
6018 
6019   unsigned NumRegs = 1;
6020   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
6021     BaseTy = ArrTy->getElementType();
6022     NumRegs = ArrTy->getNumElements();
6023   }
6024   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
6025 
6026   // The AArch64 va_list type and handling is specified in the Procedure Call
6027   // Standard, section B.4:
6028   //
6029   // struct {
6030   //   void *__stack;
6031   //   void *__gr_top;
6032   //   void *__vr_top;
6033   //   int __gr_offs;
6034   //   int __vr_offs;
6035   // };
6036 
6037   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
6038   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6039   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
6040   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6041 
6042   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6043   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
6044 
6045   Address reg_offs_p = Address::invalid();
6046   llvm::Value *reg_offs = nullptr;
6047   int reg_top_index;
6048   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
6049   if (!IsFPR) {
6050     // 3 is the field number of __gr_offs
6051     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
6052     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
6053     reg_top_index = 1; // field number for __gr_top
6054     RegSize = llvm::alignTo(RegSize, 8);
6055   } else {
6056     // 4 is the field number of __vr_offs.
6057     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
6058     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
6059     reg_top_index = 2; // field number for __vr_top
6060     RegSize = 16 * NumRegs;
6061   }
6062 
6063   //=======================================
6064   // Find out where argument was passed
6065   //=======================================
6066 
6067   // If reg_offs >= 0 we're already using the stack for this type of
6068   // argument. We don't want to keep updating reg_offs (in case it overflows,
6069   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
6070   // whatever they get).
6071   llvm::Value *UsingStack = nullptr;
6072   UsingStack = CGF.Builder.CreateICmpSGE(
6073       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
6074 
6075   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
6076 
6077   // Otherwise, at least some kind of argument could go in these registers, the
6078   // question is whether this particular type is too big.
6079   CGF.EmitBlock(MaybeRegBlock);
6080 
6081   // Integer arguments may need to correct register alignment (for example a
6082   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
6083   // align __gr_offs to calculate the potential address.
6084   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
6085     int Align = TyAlign.getQuantity();
6086 
6087     reg_offs = CGF.Builder.CreateAdd(
6088         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
6089         "align_regoffs");
6090     reg_offs = CGF.Builder.CreateAnd(
6091         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
6092         "aligned_regoffs");
6093   }
6094 
6095   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
6096   // The fact that this is done unconditionally reflects the fact that
6097   // allocating an argument to the stack also uses up all the remaining
6098   // registers of the appropriate kind.
6099   llvm::Value *NewOffset = nullptr;
6100   NewOffset = CGF.Builder.CreateAdd(
6101       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
6102   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
6103 
6104   // Now we're in a position to decide whether this argument really was in
6105   // registers or not.
6106   llvm::Value *InRegs = nullptr;
6107   InRegs = CGF.Builder.CreateICmpSLE(
6108       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
6109 
6110   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
6111 
6112   //=======================================
6113   // Argument was in registers
6114   //=======================================
6115 
6116   // Now we emit the code for if the argument was originally passed in
6117   // registers. First start the appropriate block:
6118   CGF.EmitBlock(InRegBlock);
6119 
6120   llvm::Value *reg_top = nullptr;
6121   Address reg_top_p =
6122       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
6123   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
6124   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, reg_top, reg_offs),
6125                    CGF.Int8Ty, CharUnits::fromQuantity(IsFPR ? 16 : 8));
6126   Address RegAddr = Address::invalid();
6127   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty), *ElementTy = MemTy;
6128 
6129   if (IsIndirect) {
6130     // If it's been passed indirectly (actually a struct), whatever we find from
6131     // stored registers or on the stack will actually be a struct **.
6132     MemTy = llvm::PointerType::getUnqual(MemTy);
6133   }
6134 
6135   const Type *Base = nullptr;
6136   uint64_t NumMembers = 0;
6137   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
6138   if (IsHFA && NumMembers > 1) {
6139     // Homogeneous aggregates passed in registers will have their elements split
6140     // and stored 16-bytes apart regardless of size (they're notionally in qN,
6141     // qN+1, ...). We reload and store into a temporary local variable
6142     // contiguously.
6143     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
6144     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
6145     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
6146     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
6147     Address Tmp = CGF.CreateTempAlloca(HFATy,
6148                                        std::max(TyAlign, BaseTyInfo.Align));
6149 
6150     // On big-endian platforms, the value will be right-aligned in its slot.
6151     int Offset = 0;
6152     if (CGF.CGM.getDataLayout().isBigEndian() &&
6153         BaseTyInfo.Width.getQuantity() < 16)
6154       Offset = 16 - BaseTyInfo.Width.getQuantity();
6155 
6156     for (unsigned i = 0; i < NumMembers; ++i) {
6157       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
6158       Address LoadAddr =
6159         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
6160       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
6161 
6162       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
6163 
6164       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
6165       CGF.Builder.CreateStore(Elem, StoreAddr);
6166     }
6167 
6168     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
6169   } else {
6170     // Otherwise the object is contiguous in memory.
6171 
6172     // It might be right-aligned in its slot.
6173     CharUnits SlotSize = BaseAddr.getAlignment();
6174     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
6175         (IsHFA || !isAggregateTypeForABI(Ty)) &&
6176         TySize < SlotSize) {
6177       CharUnits Offset = SlotSize - TySize;
6178       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
6179     }
6180 
6181     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
6182   }
6183 
6184   CGF.EmitBranch(ContBlock);
6185 
6186   //=======================================
6187   // Argument was on the stack
6188   //=======================================
6189   CGF.EmitBlock(OnStackBlock);
6190 
6191   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
6192   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
6193 
6194   // Again, stack arguments may need realignment. In this case both integer and
6195   // floating-point ones might be affected.
6196   if (!IsIndirect && TyAlign.getQuantity() > 8) {
6197     int Align = TyAlign.getQuantity();
6198 
6199     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
6200 
6201     OnStackPtr = CGF.Builder.CreateAdd(
6202         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
6203         "align_stack");
6204     OnStackPtr = CGF.Builder.CreateAnd(
6205         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
6206         "align_stack");
6207 
6208     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
6209   }
6210   Address OnStackAddr = Address(OnStackPtr, CGF.Int8Ty,
6211                                 std::max(CharUnits::fromQuantity(8), TyAlign));
6212 
6213   // All stack slots are multiples of 8 bytes.
6214   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
6215   CharUnits StackSize;
6216   if (IsIndirect)
6217     StackSize = StackSlotSize;
6218   else
6219     StackSize = TySize.alignTo(StackSlotSize);
6220 
6221   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
6222   llvm::Value *NewStack = CGF.Builder.CreateInBoundsGEP(
6223       CGF.Int8Ty, OnStackPtr, StackSizeC, "new_stack");
6224 
6225   // Write the new value of __stack for the next call to va_arg
6226   CGF.Builder.CreateStore(NewStack, stack_p);
6227 
6228   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
6229       TySize < StackSlotSize) {
6230     CharUnits Offset = StackSlotSize - TySize;
6231     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
6232   }
6233 
6234   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
6235 
6236   CGF.EmitBranch(ContBlock);
6237 
6238   //=======================================
6239   // Tidy up
6240   //=======================================
6241   CGF.EmitBlock(ContBlock);
6242 
6243   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, OnStackAddr,
6244                                  OnStackBlock, "vaargs.addr");
6245 
6246   if (IsIndirect)
6247     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), ElementTy,
6248                    TyAlign);
6249 
6250   return ResAddr;
6251 }
6252 
6253 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
6254                                         CodeGenFunction &CGF) const {
6255   // The backend's lowering doesn't support va_arg for aggregates or
6256   // illegal vector types.  Lower VAArg here for these cases and use
6257   // the LLVM va_arg instruction for everything else.
6258   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
6259     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
6260 
6261   uint64_t PointerSize = getTarget().getPointerWidth(LangAS::Default) / 8;
6262   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
6263 
6264   // Empty records are ignored for parameter passing purposes.
6265   if (isEmptyRecord(getContext(), Ty, true)) {
6266     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"),
6267                            getVAListElementType(CGF), SlotSize);
6268     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6269     return Addr;
6270   }
6271 
6272   // The size of the actual thing passed, which might end up just
6273   // being a pointer for indirect types.
6274   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6275 
6276   // Arguments bigger than 16 bytes which aren't homogeneous
6277   // aggregates should be passed indirectly.
6278   bool IsIndirect = false;
6279   if (TyInfo.Width.getQuantity() > 16) {
6280     const Type *Base = nullptr;
6281     uint64_t Members = 0;
6282     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
6283   }
6284 
6285   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6286                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
6287 }
6288 
6289 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
6290                                     QualType Ty) const {
6291   bool IsIndirect = false;
6292 
6293   // Composites larger than 16 bytes are passed by reference.
6294   if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) > 128)
6295     IsIndirect = true;
6296 
6297   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
6298                           CGF.getContext().getTypeInfoInChars(Ty),
6299                           CharUnits::fromQuantity(8),
6300                           /*allowHigherAlign*/ false);
6301 }
6302 
6303 //===----------------------------------------------------------------------===//
6304 // ARM ABI Implementation
6305 //===----------------------------------------------------------------------===//
6306 
6307 namespace {
6308 
6309 class ARMABIInfo : public ABIInfo {
6310 public:
6311   enum ABIKind {
6312     APCS = 0,
6313     AAPCS = 1,
6314     AAPCS_VFP = 2,
6315     AAPCS16_VFP = 3,
6316   };
6317 
6318 private:
6319   ABIKind Kind;
6320   bool IsFloatABISoftFP;
6321 
6322 public:
6323   ARMABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {
6324     setCCs();
6325     IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
6326         CGT.getCodeGenOpts().FloatABI == ""; // default
6327   }
6328 
6329   bool isEABI() const {
6330     switch (getTarget().getTriple().getEnvironment()) {
6331     case llvm::Triple::Android:
6332     case llvm::Triple::EABI:
6333     case llvm::Triple::EABIHF:
6334     case llvm::Triple::GNUEABI:
6335     case llvm::Triple::GNUEABIHF:
6336     case llvm::Triple::MuslEABI:
6337     case llvm::Triple::MuslEABIHF:
6338       return true;
6339     default:
6340       return false;
6341     }
6342   }
6343 
6344   bool isEABIHF() const {
6345     switch (getTarget().getTriple().getEnvironment()) {
6346     case llvm::Triple::EABIHF:
6347     case llvm::Triple::GNUEABIHF:
6348     case llvm::Triple::MuslEABIHF:
6349       return true;
6350     default:
6351       return false;
6352     }
6353   }
6354 
6355   ABIKind getABIKind() const { return Kind; }
6356 
6357   bool allowBFloatArgsAndRet() const override {
6358     return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
6359   }
6360 
6361 private:
6362   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
6363                                 unsigned functionCallConv) const;
6364   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
6365                                   unsigned functionCallConv) const;
6366   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
6367                                           uint64_t Members) const;
6368   ABIArgInfo coerceIllegalVector(QualType Ty) const;
6369   bool isIllegalVectorType(QualType Ty) const;
6370   bool containsAnyFP16Vectors(QualType Ty) const;
6371 
6372   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
6373   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
6374                                          uint64_t Members) const override;
6375   bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
6376 
6377   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
6378 
6379   void computeInfo(CGFunctionInfo &FI) const override;
6380 
6381   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6382                     QualType Ty) const override;
6383 
6384   llvm::CallingConv::ID getLLVMDefaultCC() const;
6385   llvm::CallingConv::ID getABIDefaultCC() const;
6386   void setCCs();
6387 };
6388 
6389 class ARMSwiftABIInfo : public SwiftABIInfo {
6390 public:
6391   explicit ARMSwiftABIInfo(CodeGenTypes &CGT)
6392       : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/true) {}
6393 
6394   bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
6395                          unsigned NumElts) const override;
6396 };
6397 
6398 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
6399 public:
6400   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6401       : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {
6402     SwiftInfo = std::make_unique<ARMSwiftABIInfo>(CGT);
6403   }
6404 
6405   const ARMABIInfo &getABIInfo() const {
6406     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
6407   }
6408 
6409   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6410     return 13;
6411   }
6412 
6413   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
6414     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
6415   }
6416 
6417   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6418                                llvm::Value *Address) const override {
6419     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6420 
6421     // 0-15 are the 16 integer registers.
6422     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
6423     return false;
6424   }
6425 
6426   unsigned getSizeOfUnwindException() const override {
6427     if (getABIInfo().isEABI()) return 88;
6428     return TargetCodeGenInfo::getSizeOfUnwindException();
6429   }
6430 
6431   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6432                            CodeGen::CodeGenModule &CGM) const override {
6433     if (GV->isDeclaration())
6434       return;
6435     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6436     if (!FD)
6437       return;
6438     auto *Fn = cast<llvm::Function>(GV);
6439 
6440     if (const auto *TA = FD->getAttr<TargetAttr>()) {
6441       ParsedTargetAttr Attr =
6442           CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
6443       if (!Attr.BranchProtection.empty()) {
6444         TargetInfo::BranchProtectionInfo BPI;
6445         StringRef DiagMsg;
6446         StringRef Arch =
6447             Attr.CPU.empty() ? CGM.getTarget().getTargetOpts().CPU : Attr.CPU;
6448         if (!CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
6449                                                       Arch, BPI, DiagMsg)) {
6450           CGM.getDiags().Report(
6451               D->getLocation(),
6452               diag::warn_target_unsupported_branch_protection_attribute)
6453               << Arch;
6454         } else {
6455           static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
6456           assert(static_cast<unsigned>(BPI.SignReturnAddr) <= 2 &&
6457                  "Unexpected SignReturnAddressScopeKind");
6458           Fn->addFnAttr(
6459               "sign-return-address",
6460               SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
6461 
6462           Fn->addFnAttr("branch-target-enforcement",
6463                         BPI.BranchTargetEnforcement ? "true" : "false");
6464         }
6465       } else if (CGM.getLangOpts().BranchTargetEnforcement ||
6466                  CGM.getLangOpts().hasSignReturnAddress()) {
6467         // If the Branch Protection attribute is missing, validate the target
6468         // Architecture attribute against Branch Protection command line
6469         // settings.
6470         if (!CGM.getTarget().isBranchProtectionSupportedArch(Attr.CPU))
6471           CGM.getDiags().Report(
6472               D->getLocation(),
6473               diag::warn_target_unsupported_branch_protection_attribute)
6474               << Attr.CPU;
6475       }
6476     }
6477 
6478     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
6479     if (!Attr)
6480       return;
6481 
6482     const char *Kind;
6483     switch (Attr->getInterrupt()) {
6484     case ARMInterruptAttr::Generic: Kind = ""; break;
6485     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
6486     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
6487     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
6488     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
6489     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
6490     }
6491 
6492     Fn->addFnAttr("interrupt", Kind);
6493 
6494     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
6495     if (ABI == ARMABIInfo::APCS)
6496       return;
6497 
6498     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
6499     // however this is not necessarily true on taking any interrupt. Instruct
6500     // the backend to perform a realignment as part of the function prologue.
6501     llvm::AttrBuilder B(Fn->getContext());
6502     B.addStackAlignmentAttr(8);
6503     Fn->addFnAttrs(B);
6504   }
6505 };
6506 
6507 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
6508 public:
6509   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
6510       : ARMTargetCodeGenInfo(CGT, K) {}
6511 
6512   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6513                            CodeGen::CodeGenModule &CGM) const override;
6514 
6515   void getDependentLibraryOption(llvm::StringRef Lib,
6516                                  llvm::SmallString<24> &Opt) const override {
6517     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
6518   }
6519 
6520   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
6521                                llvm::SmallString<32> &Opt) const override {
6522     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
6523   }
6524 };
6525 
6526 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
6527     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
6528   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
6529   if (GV->isDeclaration())
6530     return;
6531   addStackProbeTargetAttributes(D, GV, CGM);
6532 }
6533 }
6534 
6535 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
6536   if (!::classifyReturnType(getCXXABI(), FI, *this))
6537     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
6538                                             FI.getCallingConvention());
6539 
6540   for (auto &I : FI.arguments())
6541     I.info = classifyArgumentType(I.type, FI.isVariadic(),
6542                                   FI.getCallingConvention());
6543 
6544 
6545   // Always honor user-specified calling convention.
6546   if (FI.getCallingConvention() != llvm::CallingConv::C)
6547     return;
6548 
6549   llvm::CallingConv::ID cc = getRuntimeCC();
6550   if (cc != llvm::CallingConv::C)
6551     FI.setEffectiveCallingConvention(cc);
6552 }
6553 
6554 /// Return the default calling convention that LLVM will use.
6555 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
6556   // The default calling convention that LLVM will infer.
6557   if (isEABIHF() || getTarget().getTriple().isWatchABI())
6558     return llvm::CallingConv::ARM_AAPCS_VFP;
6559   else if (isEABI())
6560     return llvm::CallingConv::ARM_AAPCS;
6561   else
6562     return llvm::CallingConv::ARM_APCS;
6563 }
6564 
6565 /// Return the calling convention that our ABI would like us to use
6566 /// as the C calling convention.
6567 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
6568   switch (getABIKind()) {
6569   case APCS: return llvm::CallingConv::ARM_APCS;
6570   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
6571   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6572   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
6573   }
6574   llvm_unreachable("bad ABI kind");
6575 }
6576 
6577 void ARMABIInfo::setCCs() {
6578   assert(getRuntimeCC() == llvm::CallingConv::C);
6579 
6580   // Don't muddy up the IR with a ton of explicit annotations if
6581   // they'd just match what LLVM will infer from the triple.
6582   llvm::CallingConv::ID abiCC = getABIDefaultCC();
6583   if (abiCC != getLLVMDefaultCC())
6584     RuntimeCC = abiCC;
6585 }
6586 
6587 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
6588   uint64_t Size = getContext().getTypeSize(Ty);
6589   if (Size <= 32) {
6590     llvm::Type *ResType =
6591         llvm::Type::getInt32Ty(getVMContext());
6592     return ABIArgInfo::getDirect(ResType);
6593   }
6594   if (Size == 64 || Size == 128) {
6595     auto *ResType = llvm::FixedVectorType::get(
6596         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6597     return ABIArgInfo::getDirect(ResType);
6598   }
6599   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6600 }
6601 
6602 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
6603                                                     const Type *Base,
6604                                                     uint64_t Members) const {
6605   assert(Base && "Base class should be set for homogeneous aggregate");
6606   // Base can be a floating-point or a vector.
6607   if (const VectorType *VT = Base->getAs<VectorType>()) {
6608     // FP16 vectors should be converted to integer vectors
6609     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
6610       uint64_t Size = getContext().getTypeSize(VT);
6611       auto *NewVecTy = llvm::FixedVectorType::get(
6612           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
6613       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
6614       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6615     }
6616   }
6617   unsigned Align = 0;
6618   if (getABIKind() == ARMABIInfo::AAPCS ||
6619       getABIKind() == ARMABIInfo::AAPCS_VFP) {
6620     // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
6621     // default otherwise.
6622     Align = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6623     unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
6624     Align = (Align > BaseAlign && Align >= 8) ? 8 : 0;
6625   }
6626   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align);
6627 }
6628 
6629 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
6630                                             unsigned functionCallConv) const {
6631   // 6.1.2.1 The following argument types are VFP CPRCs:
6632   //   A single-precision floating-point type (including promoted
6633   //   half-precision types); A double-precision floating-point type;
6634   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
6635   //   with a Base Type of a single- or double-precision floating-point type,
6636   //   64-bit containerized vectors or 128-bit containerized vectors with one
6637   //   to four Elements.
6638   // Variadic functions should always marshal to the base standard.
6639   bool IsAAPCS_VFP =
6640       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
6641 
6642   Ty = useFirstFieldIfTransparentUnion(Ty);
6643 
6644   // Handle illegal vector types here.
6645   if (isIllegalVectorType(Ty))
6646     return coerceIllegalVector(Ty);
6647 
6648   if (!isAggregateTypeForABI(Ty)) {
6649     // Treat an enum type as its underlying type.
6650     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
6651       Ty = EnumTy->getDecl()->getIntegerType();
6652     }
6653 
6654     if (const auto *EIT = Ty->getAs<BitIntType>())
6655       if (EIT->getNumBits() > 64)
6656         return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6657 
6658     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
6659                                               : ABIArgInfo::getDirect());
6660   }
6661 
6662   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6663     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6664   }
6665 
6666   // Ignore empty records.
6667   if (isEmptyRecord(getContext(), Ty, true))
6668     return ABIArgInfo::getIgnore();
6669 
6670   if (IsAAPCS_VFP) {
6671     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
6672     // into VFP registers.
6673     const Type *Base = nullptr;
6674     uint64_t Members = 0;
6675     if (isHomogeneousAggregate(Ty, Base, Members))
6676       return classifyHomogeneousAggregate(Ty, Base, Members);
6677   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6678     // WatchOS does have homogeneous aggregates. Note that we intentionally use
6679     // this convention even for a variadic function: the backend will use GPRs
6680     // if needed.
6681     const Type *Base = nullptr;
6682     uint64_t Members = 0;
6683     if (isHomogeneousAggregate(Ty, Base, Members)) {
6684       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
6685       llvm::Type *Ty =
6686         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
6687       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
6688     }
6689   }
6690 
6691   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6692       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
6693     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
6694     // bigger than 128-bits, they get placed in space allocated by the caller,
6695     // and a pointer is passed.
6696     return ABIArgInfo::getIndirect(
6697         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
6698   }
6699 
6700   // Support byval for ARM.
6701   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
6702   // most 8-byte. We realign the indirect argument if type alignment is bigger
6703   // than ABI alignment.
6704   uint64_t ABIAlign = 4;
6705   uint64_t TyAlign;
6706   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6707       getABIKind() == ARMABIInfo::AAPCS) {
6708     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
6709     ABIAlign = std::clamp(TyAlign, (uint64_t)4, (uint64_t)8);
6710   } else {
6711     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
6712   }
6713   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
6714     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
6715     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
6716                                    /*ByVal=*/true,
6717                                    /*Realign=*/TyAlign > ABIAlign);
6718   }
6719 
6720   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
6721   // same size and alignment.
6722   if (getTarget().isRenderScriptTarget()) {
6723     return coerceToIntArray(Ty, getContext(), getVMContext());
6724   }
6725 
6726   // Otherwise, pass by coercing to a structure of the appropriate size.
6727   llvm::Type* ElemTy;
6728   unsigned SizeRegs;
6729   // FIXME: Try to match the types of the arguments more accurately where
6730   // we can.
6731   if (TyAlign <= 4) {
6732     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6733     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6734   } else {
6735     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6736     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6737   }
6738 
6739   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6740 }
6741 
6742 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6743                               llvm::LLVMContext &VMContext) {
6744   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6745   // is called integer-like if its size is less than or equal to one word, and
6746   // the offset of each of its addressable sub-fields is zero.
6747 
6748   uint64_t Size = Context.getTypeSize(Ty);
6749 
6750   // Check that the type fits in a word.
6751   if (Size > 32)
6752     return false;
6753 
6754   // FIXME: Handle vector types!
6755   if (Ty->isVectorType())
6756     return false;
6757 
6758   // Float types are never treated as "integer like".
6759   if (Ty->isRealFloatingType())
6760     return false;
6761 
6762   // If this is a builtin or pointer type then it is ok.
6763   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6764     return true;
6765 
6766   // Small complex integer types are "integer like".
6767   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6768     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6769 
6770   // Single element and zero sized arrays should be allowed, by the definition
6771   // above, but they are not.
6772 
6773   // Otherwise, it must be a record type.
6774   const RecordType *RT = Ty->getAs<RecordType>();
6775   if (!RT) return false;
6776 
6777   // Ignore records with flexible arrays.
6778   const RecordDecl *RD = RT->getDecl();
6779   if (RD->hasFlexibleArrayMember())
6780     return false;
6781 
6782   // Check that all sub-fields are at offset 0, and are themselves "integer
6783   // like".
6784   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6785 
6786   bool HadField = false;
6787   unsigned idx = 0;
6788   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6789        i != e; ++i, ++idx) {
6790     const FieldDecl *FD = *i;
6791 
6792     // Bit-fields are not addressable, we only need to verify they are "integer
6793     // like". We still have to disallow a subsequent non-bitfield, for example:
6794     //   struct { int : 0; int x }
6795     // is non-integer like according to gcc.
6796     if (FD->isBitField()) {
6797       if (!RD->isUnion())
6798         HadField = true;
6799 
6800       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6801         return false;
6802 
6803       continue;
6804     }
6805 
6806     // Check if this field is at offset 0.
6807     if (Layout.getFieldOffset(idx) != 0)
6808       return false;
6809 
6810     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6811       return false;
6812 
6813     // Only allow at most one field in a structure. This doesn't match the
6814     // wording above, but follows gcc in situations with a field following an
6815     // empty structure.
6816     if (!RD->isUnion()) {
6817       if (HadField)
6818         return false;
6819 
6820       HadField = true;
6821     }
6822   }
6823 
6824   return true;
6825 }
6826 
6827 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6828                                           unsigned functionCallConv) const {
6829 
6830   // Variadic functions should always marshal to the base standard.
6831   bool IsAAPCS_VFP =
6832       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6833 
6834   if (RetTy->isVoidType())
6835     return ABIArgInfo::getIgnore();
6836 
6837   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6838     // Large vector types should be returned via memory.
6839     if (getContext().getTypeSize(RetTy) > 128)
6840       return getNaturalAlignIndirect(RetTy);
6841     // TODO: FP16/BF16 vectors should be converted to integer vectors
6842     // This check is similar  to isIllegalVectorType - refactor?
6843     if ((!getTarget().hasLegalHalfType() &&
6844         (VT->getElementType()->isFloat16Type() ||
6845          VT->getElementType()->isHalfType())) ||
6846         (IsFloatABISoftFP &&
6847          VT->getElementType()->isBFloat16Type()))
6848       return coerceIllegalVector(RetTy);
6849   }
6850 
6851   if (!isAggregateTypeForABI(RetTy)) {
6852     // Treat an enum type as its underlying type.
6853     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6854       RetTy = EnumTy->getDecl()->getIntegerType();
6855 
6856     if (const auto *EIT = RetTy->getAs<BitIntType>())
6857       if (EIT->getNumBits() > 64)
6858         return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
6859 
6860     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
6861                                                 : ABIArgInfo::getDirect();
6862   }
6863 
6864   // Are we following APCS?
6865   if (getABIKind() == APCS) {
6866     if (isEmptyRecord(getContext(), RetTy, false))
6867       return ABIArgInfo::getIgnore();
6868 
6869     // Complex types are all returned as packed integers.
6870     //
6871     // FIXME: Consider using 2 x vector types if the back end handles them
6872     // correctly.
6873     if (RetTy->isAnyComplexType())
6874       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6875           getVMContext(), getContext().getTypeSize(RetTy)));
6876 
6877     // Integer like structures are returned in r0.
6878     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6879       // Return in the smallest viable integer type.
6880       uint64_t Size = getContext().getTypeSize(RetTy);
6881       if (Size <= 8)
6882         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6883       if (Size <= 16)
6884         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6885       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6886     }
6887 
6888     // Otherwise return in memory.
6889     return getNaturalAlignIndirect(RetTy);
6890   }
6891 
6892   // Otherwise this is an AAPCS variant.
6893 
6894   if (isEmptyRecord(getContext(), RetTy, true))
6895     return ABIArgInfo::getIgnore();
6896 
6897   // Check for homogeneous aggregates with AAPCS-VFP.
6898   if (IsAAPCS_VFP) {
6899     const Type *Base = nullptr;
6900     uint64_t Members = 0;
6901     if (isHomogeneousAggregate(RetTy, Base, Members))
6902       return classifyHomogeneousAggregate(RetTy, Base, Members);
6903   }
6904 
6905   // Aggregates <= 4 bytes are returned in r0; other aggregates
6906   // are returned indirectly.
6907   uint64_t Size = getContext().getTypeSize(RetTy);
6908   if (Size <= 32) {
6909     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6910     // same size and alignment.
6911     if (getTarget().isRenderScriptTarget()) {
6912       return coerceToIntArray(RetTy, getContext(), getVMContext());
6913     }
6914     if (getDataLayout().isBigEndian())
6915       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6916       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6917 
6918     // Return in the smallest viable integer type.
6919     if (Size <= 8)
6920       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6921     if (Size <= 16)
6922       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6923     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6924   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6925     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6926     llvm::Type *CoerceTy =
6927         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6928     return ABIArgInfo::getDirect(CoerceTy);
6929   }
6930 
6931   return getNaturalAlignIndirect(RetTy);
6932 }
6933 
6934 /// isIllegalVector - check whether Ty is an illegal vector type.
6935 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6936   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6937     // On targets that don't support half, fp16 or bfloat, they are expanded
6938     // into float, and we don't want the ABI to depend on whether or not they
6939     // are supported in hardware. Thus return false to coerce vectors of these
6940     // types into integer vectors.
6941     // We do not depend on hasLegalHalfType for bfloat as it is a
6942     // separate IR type.
6943     if ((!getTarget().hasLegalHalfType() &&
6944         (VT->getElementType()->isFloat16Type() ||
6945          VT->getElementType()->isHalfType())) ||
6946         (IsFloatABISoftFP &&
6947          VT->getElementType()->isBFloat16Type()))
6948       return true;
6949     if (isAndroid()) {
6950       // Android shipped using Clang 3.1, which supported a slightly different
6951       // vector ABI. The primary differences were that 3-element vector types
6952       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6953       // accepts that legacy behavior for Android only.
6954       // Check whether VT is legal.
6955       unsigned NumElements = VT->getNumElements();
6956       // NumElements should be power of 2 or equal to 3.
6957       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6958         return true;
6959     } else {
6960       // Check whether VT is legal.
6961       unsigned NumElements = VT->getNumElements();
6962       uint64_t Size = getContext().getTypeSize(VT);
6963       // NumElements should be power of 2.
6964       if (!llvm::isPowerOf2_32(NumElements))
6965         return true;
6966       // Size should be greater than 32 bits.
6967       return Size <= 32;
6968     }
6969   }
6970   return false;
6971 }
6972 
6973 /// Return true if a type contains any 16-bit floating point vectors
6974 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6975   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6976     uint64_t NElements = AT->getSize().getZExtValue();
6977     if (NElements == 0)
6978       return false;
6979     return containsAnyFP16Vectors(AT->getElementType());
6980   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6981     const RecordDecl *RD = RT->getDecl();
6982 
6983     // If this is a C++ record, check the bases first.
6984     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6985       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6986             return containsAnyFP16Vectors(B.getType());
6987           }))
6988         return true;
6989 
6990     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6991           return FD && containsAnyFP16Vectors(FD->getType());
6992         }))
6993       return true;
6994 
6995     return false;
6996   } else {
6997     if (const VectorType *VT = Ty->getAs<VectorType>())
6998       return (VT->getElementType()->isFloat16Type() ||
6999               VT->getElementType()->isBFloat16Type() ||
7000               VT->getElementType()->isHalfType());
7001     return false;
7002   }
7003 }
7004 
7005 bool ARMSwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
7006                                         unsigned NumElts) const {
7007   if (!llvm::isPowerOf2_32(NumElts))
7008     return false;
7009   unsigned size = CGT.getDataLayout().getTypeStoreSizeInBits(EltTy);
7010   if (size > 64)
7011     return false;
7012   if (VectorSize.getQuantity() != 8 &&
7013       (VectorSize.getQuantity() != 16 || NumElts == 1))
7014     return false;
7015   return true;
7016 }
7017 
7018 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7019   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
7020   // double, or 64-bit or 128-bit vectors.
7021   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
7022     if (BT->getKind() == BuiltinType::Float ||
7023         BT->getKind() == BuiltinType::Double ||
7024         BT->getKind() == BuiltinType::LongDouble)
7025       return true;
7026   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
7027     unsigned VecSize = getContext().getTypeSize(VT);
7028     if (VecSize == 64 || VecSize == 128)
7029       return true;
7030   }
7031   return false;
7032 }
7033 
7034 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
7035                                                    uint64_t Members) const {
7036   return Members <= 4;
7037 }
7038 
7039 bool ARMABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
7040   // AAPCS32 says that the rule for whether something is a homogeneous
7041   // aggregate is applied to the output of the data layout decision. So
7042   // anything that doesn't affect the data layout also does not affect
7043   // homogeneity. In particular, zero-length bitfields don't stop a struct
7044   // being homogeneous.
7045   return true;
7046 }
7047 
7048 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
7049                                         bool acceptHalf) const {
7050   // Give precedence to user-specified calling conventions.
7051   if (callConvention != llvm::CallingConv::C)
7052     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
7053   else
7054     return (getABIKind() == AAPCS_VFP) ||
7055            (acceptHalf && (getABIKind() == AAPCS16_VFP));
7056 }
7057 
7058 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7059                               QualType Ty) const {
7060   CharUnits SlotSize = CharUnits::fromQuantity(4);
7061 
7062   // Empty records are ignored for parameter passing purposes.
7063   if (isEmptyRecord(getContext(), Ty, true)) {
7064     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
7065     auto *Load = CGF.Builder.CreateLoad(VAListAddr);
7066     Address Addr = Address(Load, CGF.Int8Ty, SlotSize);
7067     return CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
7068   }
7069 
7070   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
7071   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
7072 
7073   // Use indirect if size of the illegal vector is bigger than 16 bytes.
7074   bool IsIndirect = false;
7075   const Type *Base = nullptr;
7076   uint64_t Members = 0;
7077   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
7078     IsIndirect = true;
7079 
7080   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
7081   // allocated by the caller.
7082   } else if (TySize > CharUnits::fromQuantity(16) &&
7083              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
7084              !isHomogeneousAggregate(Ty, Base, Members)) {
7085     IsIndirect = true;
7086 
7087   // Otherwise, bound the type's ABI alignment.
7088   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
7089   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
7090   // Our callers should be prepared to handle an under-aligned address.
7091   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
7092              getABIKind() == ARMABIInfo::AAPCS) {
7093     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
7094     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
7095   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
7096     // ARMv7k allows type alignment up to 16 bytes.
7097     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
7098     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
7099   } else {
7100     TyAlignForABI = CharUnits::fromQuantity(4);
7101   }
7102 
7103   TypeInfoChars TyInfo(TySize, TyAlignForABI, AlignRequirementKind::None);
7104   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
7105                           SlotSize, /*AllowHigherAlign*/ true);
7106 }
7107 
7108 //===----------------------------------------------------------------------===//
7109 // NVPTX ABI Implementation
7110 //===----------------------------------------------------------------------===//
7111 
7112 namespace {
7113 
7114 class NVPTXTargetCodeGenInfo;
7115 
7116 class NVPTXABIInfo : public ABIInfo {
7117   NVPTXTargetCodeGenInfo &CGInfo;
7118 
7119 public:
7120   NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
7121       : ABIInfo(CGT), CGInfo(Info) {}
7122 
7123   ABIArgInfo classifyReturnType(QualType RetTy) const;
7124   ABIArgInfo classifyArgumentType(QualType Ty) const;
7125 
7126   void computeInfo(CGFunctionInfo &FI) const override;
7127   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7128                     QualType Ty) const override;
7129   bool isUnsupportedType(QualType T) const;
7130   ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
7131 };
7132 
7133 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
7134 public:
7135   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
7136       : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
7137 
7138   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7139                            CodeGen::CodeGenModule &M) const override;
7140   bool shouldEmitStaticExternCAliases() const override;
7141 
7142   llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
7143     // On the device side, surface reference is represented as an object handle
7144     // in 64-bit integer.
7145     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7146   }
7147 
7148   llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
7149     // On the device side, texture reference is represented as an object handle
7150     // in 64-bit integer.
7151     return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
7152   }
7153 
7154   bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7155                                               LValue Src) const override {
7156     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7157     return true;
7158   }
7159 
7160   bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7161                                               LValue Src) const override {
7162     emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
7163     return true;
7164   }
7165 
7166 private:
7167   // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
7168   // resulting MDNode to the nvvm.annotations MDNode.
7169   static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
7170                               int Operand);
7171 
7172   static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
7173                                            LValue Src) {
7174     llvm::Value *Handle = nullptr;
7175     llvm::Constant *C =
7176         llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
7177     // Lookup `addrspacecast` through the constant pointer if any.
7178     if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
7179       C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
7180     if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
7181       // Load the handle from the specific global variable using
7182       // `nvvm.texsurf.handle.internal` intrinsic.
7183       Handle = CGF.EmitRuntimeCall(
7184           CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
7185                                {GV->getType()}),
7186           {GV}, "texsurf_handle");
7187     } else
7188       Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
7189     CGF.EmitStoreOfScalar(Handle, Dst);
7190   }
7191 };
7192 
7193 /// Checks if the type is unsupported directly by the current target.
7194 bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
7195   ASTContext &Context = getContext();
7196   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
7197     return true;
7198   if (!Context.getTargetInfo().hasFloat128Type() &&
7199       (T->isFloat128Type() ||
7200        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
7201     return true;
7202   if (const auto *EIT = T->getAs<BitIntType>())
7203     return EIT->getNumBits() >
7204            (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
7205   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
7206       Context.getTypeSize(T) > 64U)
7207     return true;
7208   if (const auto *AT = T->getAsArrayTypeUnsafe())
7209     return isUnsupportedType(AT->getElementType());
7210   const auto *RT = T->getAs<RecordType>();
7211   if (!RT)
7212     return false;
7213   const RecordDecl *RD = RT->getDecl();
7214 
7215   // If this is a C++ record, check the bases first.
7216   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7217     for (const CXXBaseSpecifier &I : CXXRD->bases())
7218       if (isUnsupportedType(I.getType()))
7219         return true;
7220 
7221   for (const FieldDecl *I : RD->fields())
7222     if (isUnsupportedType(I->getType()))
7223       return true;
7224   return false;
7225 }
7226 
7227 /// Coerce the given type into an array with maximum allowed size of elements.
7228 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
7229                                                    unsigned MaxSize) const {
7230   // Alignment and Size are measured in bits.
7231   const uint64_t Size = getContext().getTypeSize(Ty);
7232   const uint64_t Alignment = getContext().getTypeAlign(Ty);
7233   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
7234   llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
7235   const uint64_t NumElements = (Size + Div - 1) / Div;
7236   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
7237 }
7238 
7239 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
7240   if (RetTy->isVoidType())
7241     return ABIArgInfo::getIgnore();
7242 
7243   if (getContext().getLangOpts().OpenMP &&
7244       getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
7245     return coerceToIntArrayWithLimit(RetTy, 64);
7246 
7247   // note: this is different from default ABI
7248   if (!RetTy->isScalarType())
7249     return ABIArgInfo::getDirect();
7250 
7251   // Treat an enum type as its underlying type.
7252   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7253     RetTy = EnumTy->getDecl()->getIntegerType();
7254 
7255   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7256                                                : ABIArgInfo::getDirect());
7257 }
7258 
7259 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
7260   // Treat an enum type as its underlying type.
7261   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7262     Ty = EnumTy->getDecl()->getIntegerType();
7263 
7264   // Return aggregates type as indirect by value
7265   if (isAggregateTypeForABI(Ty)) {
7266     // Under CUDA device compilation, tex/surf builtin types are replaced with
7267     // object types and passed directly.
7268     if (getContext().getLangOpts().CUDAIsDevice) {
7269       if (Ty->isCUDADeviceBuiltinSurfaceType())
7270         return ABIArgInfo::getDirect(
7271             CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
7272       if (Ty->isCUDADeviceBuiltinTextureType())
7273         return ABIArgInfo::getDirect(
7274             CGInfo.getCUDADeviceBuiltinTextureDeviceType());
7275     }
7276     return getNaturalAlignIndirect(Ty, /* byval */ true);
7277   }
7278 
7279   if (const auto *EIT = Ty->getAs<BitIntType>()) {
7280     if ((EIT->getNumBits() > 128) ||
7281         (!getContext().getTargetInfo().hasInt128Type() &&
7282          EIT->getNumBits() > 64))
7283       return getNaturalAlignIndirect(Ty, /* byval */ true);
7284   }
7285 
7286   return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
7287                                             : ABIArgInfo::getDirect());
7288 }
7289 
7290 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
7291   if (!getCXXABI().classifyReturnType(FI))
7292     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7293   for (auto &I : FI.arguments())
7294     I.info = classifyArgumentType(I.type);
7295 
7296   // Always honor user-specified calling convention.
7297   if (FI.getCallingConvention() != llvm::CallingConv::C)
7298     return;
7299 
7300   FI.setEffectiveCallingConvention(getRuntimeCC());
7301 }
7302 
7303 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7304                                 QualType Ty) const {
7305   llvm_unreachable("NVPTX does not support varargs");
7306 }
7307 
7308 void NVPTXTargetCodeGenInfo::setTargetAttributes(
7309     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7310   if (GV->isDeclaration())
7311     return;
7312   const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
7313   if (VD) {
7314     if (M.getLangOpts().CUDA) {
7315       if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
7316         addNVVMMetadata(GV, "surface", 1);
7317       else if (VD->getType()->isCUDADeviceBuiltinTextureType())
7318         addNVVMMetadata(GV, "texture", 1);
7319       return;
7320     }
7321   }
7322 
7323   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7324   if (!FD) return;
7325 
7326   llvm::Function *F = cast<llvm::Function>(GV);
7327 
7328   // Perform special handling in OpenCL mode
7329   if (M.getLangOpts().OpenCL) {
7330     // Use OpenCL function attributes to check for kernel functions
7331     // By default, all functions are device functions
7332     if (FD->hasAttr<OpenCLKernelAttr>()) {
7333       // OpenCL __kernel functions get kernel metadata
7334       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7335       addNVVMMetadata(F, "kernel", 1);
7336       // And kernel functions are not subject to inlining
7337       F->addFnAttr(llvm::Attribute::NoInline);
7338     }
7339   }
7340 
7341   // Perform special handling in CUDA mode.
7342   if (M.getLangOpts().CUDA) {
7343     // CUDA __global__ functions get a kernel metadata entry.  Since
7344     // __global__ functions cannot be called from the device, we do not
7345     // need to set the noinline attribute.
7346     if (FD->hasAttr<CUDAGlobalAttr>()) {
7347       // Create !{<func-ref>, metadata !"kernel", i32 1} node
7348       addNVVMMetadata(F, "kernel", 1);
7349     }
7350     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
7351       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
7352       llvm::APSInt MaxThreads(32);
7353       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
7354       if (MaxThreads > 0)
7355         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
7356 
7357       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
7358       // not specified in __launch_bounds__ or if the user specified a 0 value,
7359       // we don't have to add a PTX directive.
7360       if (Attr->getMinBlocks()) {
7361         llvm::APSInt MinBlocks(32);
7362         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
7363         if (MinBlocks > 0)
7364           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
7365           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
7366       }
7367     }
7368   }
7369 }
7370 
7371 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
7372                                              StringRef Name, int Operand) {
7373   llvm::Module *M = GV->getParent();
7374   llvm::LLVMContext &Ctx = M->getContext();
7375 
7376   // Get "nvvm.annotations" metadata node
7377   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
7378 
7379   llvm::Metadata *MDVals[] = {
7380       llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
7381       llvm::ConstantAsMetadata::get(
7382           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
7383   // Append metadata to nvvm.annotations
7384   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7385 }
7386 
7387 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
7388   return false;
7389 }
7390 }
7391 
7392 //===----------------------------------------------------------------------===//
7393 // SystemZ ABI Implementation
7394 //===----------------------------------------------------------------------===//
7395 
7396 namespace {
7397 
7398 class SystemZABIInfo : public ABIInfo {
7399   bool HasVector;
7400   bool IsSoftFloatABI;
7401 
7402 public:
7403   SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
7404       : ABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
7405 
7406   bool isPromotableIntegerTypeForABI(QualType Ty) const;
7407   bool isCompoundType(QualType Ty) const;
7408   bool isVectorArgumentType(QualType Ty) const;
7409   bool isFPArgumentType(QualType Ty) const;
7410   QualType GetSingleElementType(QualType Ty) const;
7411 
7412   ABIArgInfo classifyReturnType(QualType RetTy) const;
7413   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
7414 
7415   void computeInfo(CGFunctionInfo &FI) const override;
7416   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7417                     QualType Ty) const override;
7418 };
7419 
7420 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
7421   // These are used for speeding up the search for a visible vector ABI.
7422   mutable bool HasVisibleVecABIFlag = false;
7423   mutable std::set<const Type *> SeenTypes;
7424 
7425   // Returns true (the first time) if Ty is or found to make use of a vector
7426   // type (e.g. as a function argument).
7427   bool isVectorTypeBased(const Type *Ty) const;
7428 
7429 public:
7430   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
7431       : TargetCodeGenInfo(
7432             std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {
7433     SwiftInfo =
7434         std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
7435   }
7436 
7437   // The vector ABI is different when the vector facility is present and when
7438   // a module e.g. defines an externally visible vector variable, a flag
7439   // indicating a visible vector ABI is added. Eventually this will result in
7440   // a GNU attribute indicating the vector ABI of the module.  Ty is the type
7441   // of a variable or function parameter that is globally visible.
7442   void handleExternallyVisibleObjABI(const Type *Ty,
7443                                      CodeGen::CodeGenModule &M) const {
7444     if (!HasVisibleVecABIFlag && isVectorTypeBased(Ty)) {
7445       M.getModule().addModuleFlag(llvm::Module::Warning,
7446                                   "s390x-visible-vector-ABI", 1);
7447       HasVisibleVecABIFlag = true;
7448     }
7449   }
7450 
7451   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7452                            CodeGen::CodeGenModule &M) const override {
7453     if (!D)
7454       return;
7455 
7456     // Check if the vector ABI becomes visible by an externally visible
7457     // variable or function.
7458     if (const auto *VD = dyn_cast<VarDecl>(D)) {
7459       if (VD->isExternallyVisible())
7460         handleExternallyVisibleObjABI(VD->getType().getTypePtr(), M);
7461     }
7462     else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7463       if (FD->isExternallyVisible())
7464         handleExternallyVisibleObjABI(FD->getType().getTypePtr(), M);
7465     }
7466   }
7467 
7468   llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID,
7469                           CGBuilderTy &Builder,
7470                           CodeGenModule &CGM) const override {
7471     assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
7472     // Only use TDC in constrained FP mode.
7473     if (!Builder.getIsFPConstrained())
7474       return nullptr;
7475 
7476     llvm::Type *Ty = V->getType();
7477     if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) {
7478       llvm::Module &M = CGM.getModule();
7479       auto &Ctx = M.getContext();
7480       llvm::Function *TDCFunc =
7481           llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty);
7482       unsigned TDCBits = 0;
7483       switch (BuiltinID) {
7484       case Builtin::BI__builtin_isnan:
7485         TDCBits = 0xf;
7486         break;
7487       case Builtin::BIfinite:
7488       case Builtin::BI__finite:
7489       case Builtin::BIfinitef:
7490       case Builtin::BI__finitef:
7491       case Builtin::BIfinitel:
7492       case Builtin::BI__finitel:
7493       case Builtin::BI__builtin_isfinite:
7494         TDCBits = 0xfc0;
7495         break;
7496       case Builtin::BI__builtin_isinf:
7497         TDCBits = 0x30;
7498         break;
7499       default:
7500         break;
7501       }
7502       if (TDCBits)
7503         return Builder.CreateCall(
7504             TDCFunc,
7505             {V, llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), TDCBits)});
7506     }
7507     return nullptr;
7508   }
7509 };
7510 }
7511 
7512 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
7513   // Treat an enum type as its underlying type.
7514   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7515     Ty = EnumTy->getDecl()->getIntegerType();
7516 
7517   // Promotable integer types are required to be promoted by the ABI.
7518   if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
7519     return true;
7520 
7521   if (const auto *EIT = Ty->getAs<BitIntType>())
7522     if (EIT->getNumBits() < 64)
7523       return true;
7524 
7525   // 32-bit values must also be promoted.
7526   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7527     switch (BT->getKind()) {
7528     case BuiltinType::Int:
7529     case BuiltinType::UInt:
7530       return true;
7531     default:
7532       return false;
7533     }
7534   return false;
7535 }
7536 
7537 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
7538   return (Ty->isAnyComplexType() ||
7539           Ty->isVectorType() ||
7540           isAggregateTypeForABI(Ty));
7541 }
7542 
7543 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
7544   return (HasVector &&
7545           Ty->isVectorType() &&
7546           getContext().getTypeSize(Ty) <= 128);
7547 }
7548 
7549 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
7550   if (IsSoftFloatABI)
7551     return false;
7552 
7553   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
7554     switch (BT->getKind()) {
7555     case BuiltinType::Float:
7556     case BuiltinType::Double:
7557       return true;
7558     default:
7559       return false;
7560     }
7561 
7562   return false;
7563 }
7564 
7565 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
7566   const RecordType *RT = Ty->getAs<RecordType>();
7567 
7568   if (RT && RT->isStructureOrClassType()) {
7569     const RecordDecl *RD = RT->getDecl();
7570     QualType Found;
7571 
7572     // If this is a C++ record, check the bases first.
7573     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7574       for (const auto &I : CXXRD->bases()) {
7575         QualType Base = I.getType();
7576 
7577         // Empty bases don't affect things either way.
7578         if (isEmptyRecord(getContext(), Base, true))
7579           continue;
7580 
7581         if (!Found.isNull())
7582           return Ty;
7583         Found = GetSingleElementType(Base);
7584       }
7585 
7586     // Check the fields.
7587     for (const auto *FD : RD->fields()) {
7588       // Unlike isSingleElementStruct(), empty structure and array fields
7589       // do count.  So do anonymous bitfields that aren't zero-sized.
7590 
7591       // Like isSingleElementStruct(), ignore C++20 empty data members.
7592       if (FD->hasAttr<NoUniqueAddressAttr>() &&
7593           isEmptyRecord(getContext(), FD->getType(), true))
7594         continue;
7595 
7596       // Unlike isSingleElementStruct(), arrays do not count.
7597       // Nested structures still do though.
7598       if (!Found.isNull())
7599         return Ty;
7600       Found = GetSingleElementType(FD->getType());
7601     }
7602 
7603     // Unlike isSingleElementStruct(), trailing padding is allowed.
7604     // An 8-byte aligned struct s { float f; } is passed as a double.
7605     if (!Found.isNull())
7606       return Found;
7607   }
7608 
7609   return Ty;
7610 }
7611 
7612 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7613                                   QualType Ty) const {
7614   // Assume that va_list type is correct; should be pointer to LLVM type:
7615   // struct {
7616   //   i64 __gpr;
7617   //   i64 __fpr;
7618   //   i8 *__overflow_arg_area;
7619   //   i8 *__reg_save_area;
7620   // };
7621 
7622   // Every non-vector argument occupies 8 bytes and is passed by preference
7623   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
7624   // always passed on the stack.
7625   const SystemZTargetCodeGenInfo &SZCGI =
7626       static_cast<const SystemZTargetCodeGenInfo &>(
7627           CGT.getCGM().getTargetCodeGenInfo());
7628   Ty = getContext().getCanonicalType(Ty);
7629   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7630   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
7631   llvm::Type *DirectTy = ArgTy;
7632   ABIArgInfo AI = classifyArgumentType(Ty);
7633   bool IsIndirect = AI.isIndirect();
7634   bool InFPRs = false;
7635   bool IsVector = false;
7636   CharUnits UnpaddedSize;
7637   CharUnits DirectAlign;
7638   SZCGI.handleExternallyVisibleObjABI(Ty.getTypePtr(), CGT.getCGM());
7639   if (IsIndirect) {
7640     DirectTy = llvm::PointerType::getUnqual(DirectTy);
7641     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
7642   } else {
7643     if (AI.getCoerceToType())
7644       ArgTy = AI.getCoerceToType();
7645     InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
7646     IsVector = ArgTy->isVectorTy();
7647     UnpaddedSize = TyInfo.Width;
7648     DirectAlign = TyInfo.Align;
7649   }
7650   CharUnits PaddedSize = CharUnits::fromQuantity(8);
7651   if (IsVector && UnpaddedSize > PaddedSize)
7652     PaddedSize = CharUnits::fromQuantity(16);
7653   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
7654 
7655   CharUnits Padding = (PaddedSize - UnpaddedSize);
7656 
7657   llvm::Type *IndexTy = CGF.Int64Ty;
7658   llvm::Value *PaddedSizeV =
7659     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
7660 
7661   if (IsVector) {
7662     // Work out the address of a vector argument on the stack.
7663     // Vector arguments are always passed in the high bits of a
7664     // single (8 byte) or double (16 byte) stack slot.
7665     Address OverflowArgAreaPtr =
7666         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7667     Address OverflowArgArea =
7668         Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7669                 CGF.Int8Ty, TyInfo.Align);
7670     Address MemAddr =
7671         CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
7672 
7673     // Update overflow_arg_area_ptr pointer
7674     llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP(
7675         OverflowArgArea.getElementType(), OverflowArgArea.getPointer(),
7676         PaddedSizeV, "overflow_arg_area");
7677     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7678 
7679     return MemAddr;
7680   }
7681 
7682   assert(PaddedSize.getQuantity() == 8);
7683 
7684   unsigned MaxRegs, RegCountField, RegSaveIndex;
7685   CharUnits RegPadding;
7686   if (InFPRs) {
7687     MaxRegs = 4; // Maximum of 4 FPR arguments
7688     RegCountField = 1; // __fpr
7689     RegSaveIndex = 16; // save offset for f0
7690     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
7691   } else {
7692     MaxRegs = 5; // Maximum of 5 GPR arguments
7693     RegCountField = 0; // __gpr
7694     RegSaveIndex = 2; // save offset for r2
7695     RegPadding = Padding; // values are passed in the low bits of a GPR
7696   }
7697 
7698   Address RegCountPtr =
7699       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
7700   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
7701   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
7702   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
7703                                                  "fits_in_regs");
7704 
7705   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
7706   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
7707   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
7708   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
7709 
7710   // Emit code to load the value if it was passed in registers.
7711   CGF.EmitBlock(InRegBlock);
7712 
7713   // Work out the address of an argument register.
7714   llvm::Value *ScaledRegCount =
7715     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
7716   llvm::Value *RegBase =
7717     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
7718                                       + RegPadding.getQuantity());
7719   llvm::Value *RegOffset =
7720     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
7721   Address RegSaveAreaPtr =
7722       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
7723   llvm::Value *RegSaveArea =
7724       CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
7725   Address RawRegAddr(
7726       CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, RegOffset, "raw_reg_addr"),
7727       CGF.Int8Ty, PaddedSize);
7728   Address RegAddr =
7729       CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
7730 
7731   // Update the register count
7732   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
7733   llvm::Value *NewRegCount =
7734     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
7735   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
7736   CGF.EmitBranch(ContBlock);
7737 
7738   // Emit code to load the value if it was passed in memory.
7739   CGF.EmitBlock(InMemBlock);
7740 
7741   // Work out the address of a stack argument.
7742   Address OverflowArgAreaPtr =
7743       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
7744   Address OverflowArgArea =
7745       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
7746               CGF.Int8Ty, PaddedSize);
7747   Address RawMemAddr =
7748       CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
7749   Address MemAddr =
7750     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
7751 
7752   // Update overflow_arg_area_ptr pointer
7753   llvm::Value *NewOverflowArgArea =
7754     CGF.Builder.CreateGEP(OverflowArgArea.getElementType(),
7755                           OverflowArgArea.getPointer(), PaddedSizeV,
7756                           "overflow_arg_area");
7757   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
7758   CGF.EmitBranch(ContBlock);
7759 
7760   // Return the appropriate result.
7761   CGF.EmitBlock(ContBlock);
7762   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
7763                                  "va_arg.addr");
7764 
7765   if (IsIndirect)
7766     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), ArgTy,
7767                       TyInfo.Align);
7768 
7769   return ResAddr;
7770 }
7771 
7772 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
7773   if (RetTy->isVoidType())
7774     return ABIArgInfo::getIgnore();
7775   if (isVectorArgumentType(RetTy))
7776     return ABIArgInfo::getDirect();
7777   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
7778     return getNaturalAlignIndirect(RetTy);
7779   return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
7780                                                : ABIArgInfo::getDirect());
7781 }
7782 
7783 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
7784   // Handle the generic C++ ABI.
7785   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7786     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7787 
7788   // Integers and enums are extended to full register width.
7789   if (isPromotableIntegerTypeForABI(Ty))
7790     return ABIArgInfo::getExtend(Ty);
7791 
7792   // Handle vector types and vector-like structure types.  Note that
7793   // as opposed to float-like structure types, we do not allow any
7794   // padding for vector-like structures, so verify the sizes match.
7795   uint64_t Size = getContext().getTypeSize(Ty);
7796   QualType SingleElementTy = GetSingleElementType(Ty);
7797   if (isVectorArgumentType(SingleElementTy) &&
7798       getContext().getTypeSize(SingleElementTy) == Size)
7799     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
7800 
7801   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
7802   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
7803     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7804 
7805   // Handle small structures.
7806   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7807     // Structures with flexible arrays have variable length, so really
7808     // fail the size test above.
7809     const RecordDecl *RD = RT->getDecl();
7810     if (RD->hasFlexibleArrayMember())
7811       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7812 
7813     // The structure is passed as an unextended integer, a float, or a double.
7814     llvm::Type *PassTy;
7815     if (isFPArgumentType(SingleElementTy)) {
7816       assert(Size == 32 || Size == 64);
7817       if (Size == 32)
7818         PassTy = llvm::Type::getFloatTy(getVMContext());
7819       else
7820         PassTy = llvm::Type::getDoubleTy(getVMContext());
7821     } else
7822       PassTy = llvm::IntegerType::get(getVMContext(), Size);
7823     return ABIArgInfo::getDirect(PassTy);
7824   }
7825 
7826   // Non-structure compounds are passed indirectly.
7827   if (isCompoundType(Ty))
7828     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7829 
7830   return ABIArgInfo::getDirect(nullptr);
7831 }
7832 
7833 void SystemZABIInfo::computeInfo(CGFunctionInfo &FI) const {
7834   const SystemZTargetCodeGenInfo &SZCGI =
7835       static_cast<const SystemZTargetCodeGenInfo &>(
7836           CGT.getCGM().getTargetCodeGenInfo());
7837   if (!getCXXABI().classifyReturnType(FI))
7838     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7839   unsigned Idx = 0;
7840   for (auto &I : FI.arguments()) {
7841     I.info = classifyArgumentType(I.type);
7842     if (FI.isVariadic() && Idx++ >= FI.getNumRequiredArgs())
7843       // Check if a vararg vector argument is passed, in which case the
7844       // vector ABI becomes visible as the va_list could be passed on to
7845       // other functions.
7846       SZCGI.handleExternallyVisibleObjABI(I.type.getTypePtr(), CGT.getCGM());
7847   }
7848 }
7849 
7850 bool SystemZTargetCodeGenInfo::isVectorTypeBased(const Type *Ty) const {
7851   while (Ty->isPointerType() || Ty->isArrayType())
7852     Ty = Ty->getPointeeOrArrayElementType();
7853   if (!SeenTypes.insert(Ty).second)
7854     return false;
7855   if (Ty->isVectorType())
7856     return true;
7857   if (const auto *RecordTy = Ty->getAs<RecordType>()) {
7858     const RecordDecl *RD = RecordTy->getDecl();
7859     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
7860       if (CXXRD->hasDefinition())
7861         for (const auto &I : CXXRD->bases())
7862           if (isVectorTypeBased(I.getType().getTypePtr()))
7863             return true;
7864     for (const auto *FD : RD->fields())
7865       if (isVectorTypeBased(FD->getType().getTypePtr()))
7866         return true;
7867   }
7868   if (const auto *FT = Ty->getAs<FunctionType>())
7869     if (isVectorTypeBased(FT->getReturnType().getTypePtr()))
7870       return true;
7871   if (const FunctionProtoType *Proto = Ty->getAs<FunctionProtoType>())
7872     for (auto ParamType : Proto->getParamTypes())
7873       if (isVectorTypeBased(ParamType.getTypePtr()))
7874         return true;
7875   return false;
7876 }
7877 
7878 //===----------------------------------------------------------------------===//
7879 // MSP430 ABI Implementation
7880 //===----------------------------------------------------------------------===//
7881 
7882 namespace {
7883 
7884 class MSP430ABIInfo : public DefaultABIInfo {
7885   static ABIArgInfo complexArgInfo() {
7886     ABIArgInfo Info = ABIArgInfo::getDirect();
7887     Info.setCanBeFlattened(false);
7888     return Info;
7889   }
7890 
7891 public:
7892   MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7893 
7894   ABIArgInfo classifyReturnType(QualType RetTy) const {
7895     if (RetTy->isAnyComplexType())
7896       return complexArgInfo();
7897 
7898     return DefaultABIInfo::classifyReturnType(RetTy);
7899   }
7900 
7901   ABIArgInfo classifyArgumentType(QualType RetTy) const {
7902     if (RetTy->isAnyComplexType())
7903       return complexArgInfo();
7904 
7905     return DefaultABIInfo::classifyArgumentType(RetTy);
7906   }
7907 
7908   // Just copy the original implementations because
7909   // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
7910   void computeInfo(CGFunctionInfo &FI) const override {
7911     if (!getCXXABI().classifyReturnType(FI))
7912       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7913     for (auto &I : FI.arguments())
7914       I.info = classifyArgumentType(I.type);
7915   }
7916 
7917   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7918                     QualType Ty) const override {
7919     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
7920   }
7921 };
7922 
7923 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
7924 public:
7925   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
7926       : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
7927   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7928                            CodeGen::CodeGenModule &M) const override;
7929 };
7930 
7931 }
7932 
7933 void MSP430TargetCodeGenInfo::setTargetAttributes(
7934     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7935   if (GV->isDeclaration())
7936     return;
7937   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
7938     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
7939     if (!InterruptAttr)
7940       return;
7941 
7942     // Handle 'interrupt' attribute:
7943     llvm::Function *F = cast<llvm::Function>(GV);
7944 
7945     // Step 1: Set ISR calling convention.
7946     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
7947 
7948     // Step 2: Add attributes goodness.
7949     F->addFnAttr(llvm::Attribute::NoInline);
7950     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
7951   }
7952 }
7953 
7954 //===----------------------------------------------------------------------===//
7955 // MIPS ABI Implementation.  This works for both little-endian and
7956 // big-endian variants.
7957 //===----------------------------------------------------------------------===//
7958 
7959 namespace {
7960 class MipsABIInfo : public ABIInfo {
7961   bool IsO32;
7962   const unsigned MinABIStackAlignInBytes, StackAlignInBytes;
7963   void CoerceToIntArgs(uint64_t TySize,
7964                        SmallVectorImpl<llvm::Type *> &ArgList) const;
7965   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
7966   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
7967   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
7968 public:
7969   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
7970     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
7971     StackAlignInBytes(IsO32 ? 8 : 16) {}
7972 
7973   ABIArgInfo classifyReturnType(QualType RetTy) const;
7974   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
7975   void computeInfo(CGFunctionInfo &FI) const override;
7976   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7977                     QualType Ty) const override;
7978   ABIArgInfo extendType(QualType Ty) const;
7979 };
7980 
7981 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
7982   unsigned SizeOfUnwindException;
7983 public:
7984   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
7985       : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
7986         SizeOfUnwindException(IsO32 ? 24 : 32) {}
7987 
7988   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
7989     return 29;
7990   }
7991 
7992   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7993                            CodeGen::CodeGenModule &CGM) const override {
7994     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7995     if (!FD) return;
7996     llvm::Function *Fn = cast<llvm::Function>(GV);
7997 
7998     if (FD->hasAttr<MipsLongCallAttr>())
7999       Fn->addFnAttr("long-call");
8000     else if (FD->hasAttr<MipsShortCallAttr>())
8001       Fn->addFnAttr("short-call");
8002 
8003     // Other attributes do not have a meaning for declarations.
8004     if (GV->isDeclaration())
8005       return;
8006 
8007     if (FD->hasAttr<Mips16Attr>()) {
8008       Fn->addFnAttr("mips16");
8009     }
8010     else if (FD->hasAttr<NoMips16Attr>()) {
8011       Fn->addFnAttr("nomips16");
8012     }
8013 
8014     if (FD->hasAttr<MicroMipsAttr>())
8015       Fn->addFnAttr("micromips");
8016     else if (FD->hasAttr<NoMicroMipsAttr>())
8017       Fn->addFnAttr("nomicromips");
8018 
8019     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
8020     if (!Attr)
8021       return;
8022 
8023     const char *Kind;
8024     switch (Attr->getInterrupt()) {
8025     case MipsInterruptAttr::eic:     Kind = "eic"; break;
8026     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
8027     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
8028     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
8029     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
8030     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
8031     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
8032     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
8033     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
8034     }
8035 
8036     Fn->addFnAttr("interrupt", Kind);
8037 
8038   }
8039 
8040   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8041                                llvm::Value *Address) const override;
8042 
8043   unsigned getSizeOfUnwindException() const override {
8044     return SizeOfUnwindException;
8045   }
8046 };
8047 }
8048 
8049 void MipsABIInfo::CoerceToIntArgs(
8050     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
8051   llvm::IntegerType *IntTy =
8052     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
8053 
8054   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
8055   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
8056     ArgList.push_back(IntTy);
8057 
8058   // If necessary, add one more integer type to ArgList.
8059   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
8060 
8061   if (R)
8062     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
8063 }
8064 
8065 // In N32/64, an aligned double precision floating point field is passed in
8066 // a register.
8067 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
8068   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
8069 
8070   if (IsO32) {
8071     CoerceToIntArgs(TySize, ArgList);
8072     return llvm::StructType::get(getVMContext(), ArgList);
8073   }
8074 
8075   if (Ty->isComplexType())
8076     return CGT.ConvertType(Ty);
8077 
8078   const RecordType *RT = Ty->getAs<RecordType>();
8079 
8080   // Unions/vectors are passed in integer registers.
8081   if (!RT || !RT->isStructureOrClassType()) {
8082     CoerceToIntArgs(TySize, ArgList);
8083     return llvm::StructType::get(getVMContext(), ArgList);
8084   }
8085 
8086   const RecordDecl *RD = RT->getDecl();
8087   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
8088   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
8089 
8090   uint64_t LastOffset = 0;
8091   unsigned idx = 0;
8092   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
8093 
8094   // Iterate over fields in the struct/class and check if there are any aligned
8095   // double fields.
8096   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
8097        i != e; ++i, ++idx) {
8098     const QualType Ty = i->getType();
8099     const BuiltinType *BT = Ty->getAs<BuiltinType>();
8100 
8101     if (!BT || BT->getKind() != BuiltinType::Double)
8102       continue;
8103 
8104     uint64_t Offset = Layout.getFieldOffset(idx);
8105     if (Offset % 64) // Ignore doubles that are not aligned.
8106       continue;
8107 
8108     // Add ((Offset - LastOffset) / 64) args of type i64.
8109     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
8110       ArgList.push_back(I64);
8111 
8112     // Add double type.
8113     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
8114     LastOffset = Offset + 64;
8115   }
8116 
8117   CoerceToIntArgs(TySize - LastOffset, IntArgList);
8118   ArgList.append(IntArgList.begin(), IntArgList.end());
8119 
8120   return llvm::StructType::get(getVMContext(), ArgList);
8121 }
8122 
8123 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
8124                                         uint64_t Offset) const {
8125   if (OrigOffset + MinABIStackAlignInBytes > Offset)
8126     return nullptr;
8127 
8128   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
8129 }
8130 
8131 ABIArgInfo
8132 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
8133   Ty = useFirstFieldIfTransparentUnion(Ty);
8134 
8135   uint64_t OrigOffset = Offset;
8136   uint64_t TySize = getContext().getTypeSize(Ty);
8137   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
8138 
8139   Align = std::clamp(Align, (uint64_t)MinABIStackAlignInBytes,
8140                      (uint64_t)StackAlignInBytes);
8141   unsigned CurrOffset = llvm::alignTo(Offset, Align);
8142   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
8143 
8144   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
8145     // Ignore empty aggregates.
8146     if (TySize == 0)
8147       return ABIArgInfo::getIgnore();
8148 
8149     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
8150       Offset = OrigOffset + MinABIStackAlignInBytes;
8151       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8152     }
8153 
8154     // If we have reached here, aggregates are passed directly by coercing to
8155     // another structure type. Padding is inserted if the offset of the
8156     // aggregate is unaligned.
8157     ABIArgInfo ArgInfo =
8158         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
8159                               getPaddingType(OrigOffset, CurrOffset));
8160     ArgInfo.setInReg(true);
8161     return ArgInfo;
8162   }
8163 
8164   // Treat an enum type as its underlying type.
8165   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8166     Ty = EnumTy->getDecl()->getIntegerType();
8167 
8168   // Make sure we pass indirectly things that are too large.
8169   if (const auto *EIT = Ty->getAs<BitIntType>())
8170     if (EIT->getNumBits() > 128 ||
8171         (EIT->getNumBits() > 64 &&
8172          !getContext().getTargetInfo().hasInt128Type()))
8173       return getNaturalAlignIndirect(Ty);
8174 
8175   // All integral types are promoted to the GPR width.
8176   if (Ty->isIntegralOrEnumerationType())
8177     return extendType(Ty);
8178 
8179   return ABIArgInfo::getDirect(
8180       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
8181 }
8182 
8183 llvm::Type*
8184 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
8185   const RecordType *RT = RetTy->getAs<RecordType>();
8186   SmallVector<llvm::Type*, 8> RTList;
8187 
8188   if (RT && RT->isStructureOrClassType()) {
8189     const RecordDecl *RD = RT->getDecl();
8190     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
8191     unsigned FieldCnt = Layout.getFieldCount();
8192 
8193     // N32/64 returns struct/classes in floating point registers if the
8194     // following conditions are met:
8195     // 1. The size of the struct/class is no larger than 128-bit.
8196     // 2. The struct/class has one or two fields all of which are floating
8197     //    point types.
8198     // 3. The offset of the first field is zero (this follows what gcc does).
8199     //
8200     // Any other composite results are returned in integer registers.
8201     //
8202     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
8203       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
8204       for (; b != e; ++b) {
8205         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
8206 
8207         if (!BT || !BT->isFloatingPoint())
8208           break;
8209 
8210         RTList.push_back(CGT.ConvertType(b->getType()));
8211       }
8212 
8213       if (b == e)
8214         return llvm::StructType::get(getVMContext(), RTList,
8215                                      RD->hasAttr<PackedAttr>());
8216 
8217       RTList.clear();
8218     }
8219   }
8220 
8221   CoerceToIntArgs(Size, RTList);
8222   return llvm::StructType::get(getVMContext(), RTList);
8223 }
8224 
8225 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
8226   uint64_t Size = getContext().getTypeSize(RetTy);
8227 
8228   if (RetTy->isVoidType())
8229     return ABIArgInfo::getIgnore();
8230 
8231   // O32 doesn't treat zero-sized structs differently from other structs.
8232   // However, N32/N64 ignores zero sized return values.
8233   if (!IsO32 && Size == 0)
8234     return ABIArgInfo::getIgnore();
8235 
8236   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
8237     if (Size <= 128) {
8238       if (RetTy->isAnyComplexType())
8239         return ABIArgInfo::getDirect();
8240 
8241       // O32 returns integer vectors in registers and N32/N64 returns all small
8242       // aggregates in registers.
8243       if (!IsO32 ||
8244           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
8245         ABIArgInfo ArgInfo =
8246             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
8247         ArgInfo.setInReg(true);
8248         return ArgInfo;
8249       }
8250     }
8251 
8252     return getNaturalAlignIndirect(RetTy);
8253   }
8254 
8255   // Treat an enum type as its underlying type.
8256   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8257     RetTy = EnumTy->getDecl()->getIntegerType();
8258 
8259   // Make sure we pass indirectly things that are too large.
8260   if (const auto *EIT = RetTy->getAs<BitIntType>())
8261     if (EIT->getNumBits() > 128 ||
8262         (EIT->getNumBits() > 64 &&
8263          !getContext().getTargetInfo().hasInt128Type()))
8264       return getNaturalAlignIndirect(RetTy);
8265 
8266   if (isPromotableIntegerTypeForABI(RetTy))
8267     return ABIArgInfo::getExtend(RetTy);
8268 
8269   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
8270       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
8271     return ABIArgInfo::getSignExtend(RetTy);
8272 
8273   return ABIArgInfo::getDirect();
8274 }
8275 
8276 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
8277   ABIArgInfo &RetInfo = FI.getReturnInfo();
8278   if (!getCXXABI().classifyReturnType(FI))
8279     RetInfo = classifyReturnType(FI.getReturnType());
8280 
8281   // Check if a pointer to an aggregate is passed as a hidden argument.
8282   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
8283 
8284   for (auto &I : FI.arguments())
8285     I.info = classifyArgumentType(I.type, Offset);
8286 }
8287 
8288 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8289                                QualType OrigTy) const {
8290   QualType Ty = OrigTy;
8291 
8292   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
8293   // Pointers are also promoted in the same way but this only matters for N32.
8294   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
8295   unsigned PtrWidth = getTarget().getPointerWidth(LangAS::Default);
8296   bool DidPromote = false;
8297   if ((Ty->isIntegerType() &&
8298           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
8299       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
8300     DidPromote = true;
8301     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
8302                                             Ty->isSignedIntegerType());
8303   }
8304 
8305   auto TyInfo = getContext().getTypeInfoInChars(Ty);
8306 
8307   // The alignment of things in the argument area is never larger than
8308   // StackAlignInBytes.
8309   TyInfo.Align =
8310     std::min(TyInfo.Align, CharUnits::fromQuantity(StackAlignInBytes));
8311 
8312   // MinABIStackAlignInBytes is the size of argument slots on the stack.
8313   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
8314 
8315   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
8316                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
8317 
8318 
8319   // If there was a promotion, "unpromote" into a temporary.
8320   // TODO: can we just use a pointer into a subset of the original slot?
8321   if (DidPromote) {
8322     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
8323     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
8324 
8325     // Truncate down to the right width.
8326     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
8327                                                  : CGF.IntPtrTy);
8328     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
8329     if (OrigTy->isPointerType())
8330       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
8331 
8332     CGF.Builder.CreateStore(V, Temp);
8333     Addr = Temp;
8334   }
8335 
8336   return Addr;
8337 }
8338 
8339 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
8340   int TySize = getContext().getTypeSize(Ty);
8341 
8342   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
8343   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
8344     return ABIArgInfo::getSignExtend(Ty);
8345 
8346   return ABIArgInfo::getExtend(Ty);
8347 }
8348 
8349 bool
8350 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8351                                                llvm::Value *Address) const {
8352   // This information comes from gcc's implementation, which seems to
8353   // as canonical as it gets.
8354 
8355   // Everything on MIPS is 4 bytes.  Double-precision FP registers
8356   // are aliased to pairs of single-precision FP registers.
8357   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8358 
8359   // 0-31 are the general purpose registers, $0 - $31.
8360   // 32-63 are the floating-point registers, $f0 - $f31.
8361   // 64 and 65 are the multiply/divide registers, $hi and $lo.
8362   // 66 is the (notional, I think) register for signal-handler return.
8363   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
8364 
8365   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
8366   // They are one bit wide and ignored here.
8367 
8368   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
8369   // (coprocessor 1 is the FP unit)
8370   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
8371   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
8372   // 176-181 are the DSP accumulator registers.
8373   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
8374   return false;
8375 }
8376 
8377 //===----------------------------------------------------------------------===//
8378 // M68k ABI Implementation
8379 //===----------------------------------------------------------------------===//
8380 
8381 namespace {
8382 
8383 class M68kTargetCodeGenInfo : public TargetCodeGenInfo {
8384 public:
8385   M68kTargetCodeGenInfo(CodeGenTypes &CGT)
8386       : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
8387   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8388                            CodeGen::CodeGenModule &M) const override;
8389 };
8390 
8391 } // namespace
8392 
8393 void M68kTargetCodeGenInfo::setTargetAttributes(
8394     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8395   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
8396     if (const auto *attr = FD->getAttr<M68kInterruptAttr>()) {
8397       // Handle 'interrupt' attribute:
8398       llvm::Function *F = cast<llvm::Function>(GV);
8399 
8400       // Step 1: Set ISR calling convention.
8401       F->setCallingConv(llvm::CallingConv::M68k_INTR);
8402 
8403       // Step 2: Add attributes goodness.
8404       F->addFnAttr(llvm::Attribute::NoInline);
8405 
8406       // Step 3: Emit ISR vector alias.
8407       unsigned Num = attr->getNumber() / 2;
8408       llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
8409                                 "__isr_" + Twine(Num), F);
8410     }
8411   }
8412 }
8413 
8414 //===----------------------------------------------------------------------===//
8415 // AVR ABI Implementation. Documented at
8416 // https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
8417 // https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
8418 //===----------------------------------------------------------------------===//
8419 
8420 namespace {
8421 class AVRABIInfo : public DefaultABIInfo {
8422 private:
8423   // The total amount of registers can be used to pass parameters. It is 18 on
8424   // AVR, or 6 on AVRTiny.
8425   const unsigned ParamRegs;
8426   // The total amount of registers can be used to pass return value. It is 8 on
8427   // AVR, or 4 on AVRTiny.
8428   const unsigned RetRegs;
8429 
8430 public:
8431   AVRABIInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
8432       : DefaultABIInfo(CGT), ParamRegs(NPR), RetRegs(NRR) {}
8433 
8434   ABIArgInfo classifyReturnType(QualType Ty, bool &LargeRet) const {
8435     // On AVR, a return struct with size less than or equals to 8 bytes is
8436     // returned directly via registers R18-R25. On AVRTiny, a return struct
8437     // with size less than or equals to 4 bytes is returned directly via
8438     // registers R22-R25.
8439     if (isAggregateTypeForABI(Ty) &&
8440         getContext().getTypeSize(Ty) <= RetRegs * 8)
8441       return ABIArgInfo::getDirect();
8442     // A return value (struct or scalar) with larger size is returned via a
8443     // stack slot, along with a pointer as the function's implicit argument.
8444     if (getContext().getTypeSize(Ty) > RetRegs * 8) {
8445       LargeRet = true;
8446       return getNaturalAlignIndirect(Ty);
8447     }
8448     // An i8 return value should not be extended to i16, since AVR has 8-bit
8449     // registers.
8450     if (Ty->isIntegralOrEnumerationType() && getContext().getTypeSize(Ty) <= 8)
8451       return ABIArgInfo::getDirect();
8452     // Otherwise we follow the default way which is compatible.
8453     return DefaultABIInfo::classifyReturnType(Ty);
8454   }
8455 
8456   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegs) const {
8457     unsigned TySize = getContext().getTypeSize(Ty);
8458 
8459     // An int8 type argument always costs two registers like an int16.
8460     if (TySize == 8 && NumRegs >= 2) {
8461       NumRegs -= 2;
8462       return ABIArgInfo::getExtend(Ty);
8463     }
8464 
8465     // If the argument size is an odd number of bytes, round up the size
8466     // to the next even number.
8467     TySize = llvm::alignTo(TySize, 16);
8468 
8469     // Any type including an array/struct type can be passed in rgisters,
8470     // if there are enough registers left.
8471     if (TySize <= NumRegs * 8) {
8472       NumRegs -= TySize / 8;
8473       return ABIArgInfo::getDirect();
8474     }
8475 
8476     // An argument is passed either completely in registers or completely in
8477     // memory. Since there are not enough registers left, current argument
8478     // and all other unprocessed arguments should be passed in memory.
8479     // However we still need to return `ABIArgInfo::getDirect()` other than
8480     // `ABIInfo::getNaturalAlignIndirect(Ty)`, otherwise an extra stack slot
8481     // will be allocated, so the stack frame layout will be incompatible with
8482     // avr-gcc.
8483     NumRegs = 0;
8484     return ABIArgInfo::getDirect();
8485   }
8486 
8487   void computeInfo(CGFunctionInfo &FI) const override {
8488     // Decide the return type.
8489     bool LargeRet = false;
8490     if (!getCXXABI().classifyReturnType(FI))
8491       FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), LargeRet);
8492 
8493     // Decide each argument type. The total number of registers can be used for
8494     // arguments depends on several factors:
8495     // 1. Arguments of varargs functions are passed on the stack. This applies
8496     //    even to the named arguments. So no register can be used.
8497     // 2. Total 18 registers can be used on avr and 6 ones on avrtiny.
8498     // 3. If the return type is a struct with too large size, two registers
8499     //    (out of 18/6) will be cost as an implicit pointer argument.
8500     unsigned NumRegs = ParamRegs;
8501     if (FI.isVariadic())
8502       NumRegs = 0;
8503     else if (LargeRet)
8504       NumRegs -= 2;
8505     for (auto &I : FI.arguments())
8506       I.info = classifyArgumentType(I.type, NumRegs);
8507   }
8508 };
8509 
8510 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
8511 public:
8512   AVRTargetCodeGenInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
8513       : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT, NPR, NRR)) {}
8514 
8515   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
8516                                   const VarDecl *D) const override {
8517     // Check if global/static variable is defined in address space
8518     // 1~6 (__flash, __flash1, __flash2, __flash3, __flash4, __flash5)
8519     // but not constant.
8520     if (D) {
8521       LangAS AS = D->getType().getAddressSpace();
8522       if (isTargetAddressSpace(AS) && 1 <= toTargetAddressSpace(AS) &&
8523           toTargetAddressSpace(AS) <= 6 && !D->getType().isConstQualified())
8524         CGM.getDiags().Report(D->getLocation(),
8525                               diag::err_verify_nonconst_addrspace)
8526             << "__flash*";
8527     }
8528     return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
8529   }
8530 
8531   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8532                            CodeGen::CodeGenModule &CGM) const override {
8533     if (GV->isDeclaration())
8534       return;
8535     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
8536     if (!FD) return;
8537     auto *Fn = cast<llvm::Function>(GV);
8538 
8539     if (FD->getAttr<AVRInterruptAttr>())
8540       Fn->addFnAttr("interrupt");
8541 
8542     if (FD->getAttr<AVRSignalAttr>())
8543       Fn->addFnAttr("signal");
8544   }
8545 };
8546 }
8547 
8548 //===----------------------------------------------------------------------===//
8549 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
8550 // Currently subclassed only to implement custom OpenCL C function attribute
8551 // handling.
8552 //===----------------------------------------------------------------------===//
8553 
8554 namespace {
8555 
8556 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
8557 public:
8558   TCETargetCodeGenInfo(CodeGenTypes &CGT)
8559     : DefaultTargetCodeGenInfo(CGT) {}
8560 
8561   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8562                            CodeGen::CodeGenModule &M) const override;
8563 };
8564 
8565 void TCETargetCodeGenInfo::setTargetAttributes(
8566     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8567   if (GV->isDeclaration())
8568     return;
8569   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8570   if (!FD) return;
8571 
8572   llvm::Function *F = cast<llvm::Function>(GV);
8573 
8574   if (M.getLangOpts().OpenCL) {
8575     if (FD->hasAttr<OpenCLKernelAttr>()) {
8576       // OpenCL C Kernel functions are not subject to inlining
8577       F->addFnAttr(llvm::Attribute::NoInline);
8578       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
8579       if (Attr) {
8580         // Convert the reqd_work_group_size() attributes to metadata.
8581         llvm::LLVMContext &Context = F->getContext();
8582         llvm::NamedMDNode *OpenCLMetadata =
8583             M.getModule().getOrInsertNamedMetadata(
8584                 "opencl.kernel_wg_size_info");
8585 
8586         SmallVector<llvm::Metadata *, 5> Operands;
8587         Operands.push_back(llvm::ConstantAsMetadata::get(F));
8588 
8589         Operands.push_back(
8590             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8591                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8592         Operands.push_back(
8593             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8594                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8595         Operands.push_back(
8596             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8597                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8598 
8599         // Add a boolean constant operand for "required" (true) or "hint"
8600         // (false) for implementing the work_group_size_hint attr later.
8601         // Currently always true as the hint is not yet implemented.
8602         Operands.push_back(
8603             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
8604         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
8605       }
8606     }
8607   }
8608 }
8609 
8610 }
8611 
8612 //===----------------------------------------------------------------------===//
8613 // Hexagon ABI Implementation
8614 //===----------------------------------------------------------------------===//
8615 
8616 namespace {
8617 
8618 class HexagonABIInfo : public DefaultABIInfo {
8619 public:
8620   HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8621 
8622 private:
8623   ABIArgInfo classifyReturnType(QualType RetTy) const;
8624   ABIArgInfo classifyArgumentType(QualType RetTy) const;
8625   ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
8626 
8627   void computeInfo(CGFunctionInfo &FI) const override;
8628 
8629   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8630                     QualType Ty) const override;
8631   Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
8632                               QualType Ty) const;
8633   Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
8634                               QualType Ty) const;
8635   Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
8636                                    QualType Ty) const;
8637 };
8638 
8639 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
8640 public:
8641   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
8642       : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
8643 
8644   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8645     return 29;
8646   }
8647 
8648   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
8649                            CodeGen::CodeGenModule &GCM) const override {
8650     if (GV->isDeclaration())
8651       return;
8652     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8653     if (!FD)
8654       return;
8655   }
8656 };
8657 
8658 } // namespace
8659 
8660 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
8661   unsigned RegsLeft = 6;
8662   if (!getCXXABI().classifyReturnType(FI))
8663     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8664   for (auto &I : FI.arguments())
8665     I.info = classifyArgumentType(I.type, &RegsLeft);
8666 }
8667 
8668 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
8669   assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
8670                        " through registers");
8671 
8672   if (*RegsLeft == 0)
8673     return false;
8674 
8675   if (Size <= 32) {
8676     (*RegsLeft)--;
8677     return true;
8678   }
8679 
8680   if (2 <= (*RegsLeft & (~1U))) {
8681     *RegsLeft = (*RegsLeft & (~1U)) - 2;
8682     return true;
8683   }
8684 
8685   // Next available register was r5 but candidate was greater than 32-bits so it
8686   // has to go on the stack. However we still consume r5
8687   if (*RegsLeft == 1)
8688     *RegsLeft = 0;
8689 
8690   return false;
8691 }
8692 
8693 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
8694                                                 unsigned *RegsLeft) const {
8695   if (!isAggregateTypeForABI(Ty)) {
8696     // Treat an enum type as its underlying type.
8697     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8698       Ty = EnumTy->getDecl()->getIntegerType();
8699 
8700     uint64_t Size = getContext().getTypeSize(Ty);
8701     if (Size <= 64)
8702       HexagonAdjustRegsLeft(Size, RegsLeft);
8703 
8704     if (Size > 64 && Ty->isBitIntType())
8705       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8706 
8707     return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
8708                                              : ABIArgInfo::getDirect();
8709   }
8710 
8711   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8712     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8713 
8714   // Ignore empty records.
8715   if (isEmptyRecord(getContext(), Ty, true))
8716     return ABIArgInfo::getIgnore();
8717 
8718   uint64_t Size = getContext().getTypeSize(Ty);
8719   unsigned Align = getContext().getTypeAlign(Ty);
8720 
8721   if (Size > 64)
8722     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
8723 
8724   if (HexagonAdjustRegsLeft(Size, RegsLeft))
8725     Align = Size <= 32 ? 32 : 64;
8726   if (Size <= Align) {
8727     // Pass in the smallest viable integer type.
8728     if (!llvm::isPowerOf2_64(Size))
8729       Size = llvm::NextPowerOf2(Size);
8730     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8731   }
8732   return DefaultABIInfo::classifyArgumentType(Ty);
8733 }
8734 
8735 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
8736   if (RetTy->isVoidType())
8737     return ABIArgInfo::getIgnore();
8738 
8739   const TargetInfo &T = CGT.getTarget();
8740   uint64_t Size = getContext().getTypeSize(RetTy);
8741 
8742   if (RetTy->getAs<VectorType>()) {
8743     // HVX vectors are returned in vector registers or register pairs.
8744     if (T.hasFeature("hvx")) {
8745       assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
8746       uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
8747       if (Size == VecSize || Size == 2*VecSize)
8748         return ABIArgInfo::getDirectInReg();
8749     }
8750     // Large vector types should be returned via memory.
8751     if (Size > 64)
8752       return getNaturalAlignIndirect(RetTy);
8753   }
8754 
8755   if (!isAggregateTypeForABI(RetTy)) {
8756     // Treat an enum type as its underlying type.
8757     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
8758       RetTy = EnumTy->getDecl()->getIntegerType();
8759 
8760     if (Size > 64 && RetTy->isBitIntType())
8761       return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
8762 
8763     return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
8764                                                 : ABIArgInfo::getDirect();
8765   }
8766 
8767   if (isEmptyRecord(getContext(), RetTy, true))
8768     return ABIArgInfo::getIgnore();
8769 
8770   // Aggregates <= 8 bytes are returned in registers, other aggregates
8771   // are returned indirectly.
8772   if (Size <= 64) {
8773     // Return in the smallest viable integer type.
8774     if (!llvm::isPowerOf2_64(Size))
8775       Size = llvm::NextPowerOf2(Size);
8776     return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
8777   }
8778   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
8779 }
8780 
8781 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
8782                                             Address VAListAddr,
8783                                             QualType Ty) const {
8784   // Load the overflow area pointer.
8785   Address __overflow_area_pointer_p =
8786       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8787   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8788       __overflow_area_pointer_p, "__overflow_area_pointer");
8789 
8790   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
8791   if (Align > 4) {
8792     // Alignment should be a power of 2.
8793     assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
8794 
8795     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
8796     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
8797 
8798     // Add offset to the current pointer to access the argument.
8799     __overflow_area_pointer =
8800         CGF.Builder.CreateGEP(CGF.Int8Ty, __overflow_area_pointer, Offset);
8801     llvm::Value *AsInt =
8802         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8803 
8804     // Create a mask which should be "AND"ed
8805     // with (overflow_arg_area + align - 1)
8806     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
8807     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8808         CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
8809         "__overflow_area_pointer.align");
8810   }
8811 
8812   // Get the type of the argument from memory and bitcast
8813   // overflow area pointer to the argument type.
8814   llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
8815   Address AddrTyped = CGF.Builder.CreateElementBitCast(
8816       Address(__overflow_area_pointer, CGF.Int8Ty,
8817               CharUnits::fromQuantity(Align)),
8818       PTy);
8819 
8820   // Round up to the minimum stack alignment for varargs which is 4 bytes.
8821   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8822 
8823   __overflow_area_pointer = CGF.Builder.CreateGEP(
8824       CGF.Int8Ty, __overflow_area_pointer,
8825       llvm::ConstantInt::get(CGF.Int32Ty, Offset),
8826       "__overflow_area_pointer.next");
8827   CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
8828 
8829   return AddrTyped;
8830 }
8831 
8832 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
8833                                             Address VAListAddr,
8834                                             QualType Ty) const {
8835   // FIXME: Need to handle alignment
8836   llvm::Type *BP = CGF.Int8PtrTy;
8837   CGBuilderTy &Builder = CGF.Builder;
8838   Address VAListAddrAsBPP = Builder.CreateElementBitCast(VAListAddr, BP, "ap");
8839   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8840   // Handle address alignment for type alignment > 32 bits
8841   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
8842   if (TyAlign > 4) {
8843     assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
8844     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
8845     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
8846     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
8847     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
8848   }
8849   Address AddrTyped = Builder.CreateElementBitCast(
8850       Address(Addr, CGF.Int8Ty, CharUnits::fromQuantity(TyAlign)),
8851       CGF.ConvertType(Ty));
8852 
8853   uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
8854   llvm::Value *NextAddr = Builder.CreateGEP(
8855       CGF.Int8Ty, Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
8856   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
8857 
8858   return AddrTyped;
8859 }
8860 
8861 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
8862                                                  Address VAListAddr,
8863                                                  QualType Ty) const {
8864   int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
8865 
8866   if (ArgSize > 8)
8867     return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
8868 
8869   // Here we have check if the argument is in register area or
8870   // in overflow area.
8871   // If the saved register area pointer + argsize rounded up to alignment >
8872   // saved register area end pointer, argument is in overflow area.
8873   unsigned RegsLeft = 6;
8874   Ty = CGF.getContext().getCanonicalType(Ty);
8875   (void)classifyArgumentType(Ty, &RegsLeft);
8876 
8877   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
8878   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
8879   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
8880   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
8881 
8882   // Get rounded size of the argument.GCC does not allow vararg of
8883   // size < 4 bytes. We follow the same logic here.
8884   ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8885   int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
8886 
8887   // Argument may be in saved register area
8888   CGF.EmitBlock(MaybeRegBlock);
8889 
8890   // Load the current saved register area pointer.
8891   Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
8892       VAListAddr, 0, "__current_saved_reg_area_pointer_p");
8893   llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
8894       __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
8895 
8896   // Load the saved register area end pointer.
8897   Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
8898       VAListAddr, 1, "__saved_reg_area_end_pointer_p");
8899   llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
8900       __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
8901 
8902   // If the size of argument is > 4 bytes, check if the stack
8903   // location is aligned to 8 bytes
8904   if (ArgAlign > 4) {
8905 
8906     llvm::Value *__current_saved_reg_area_pointer_int =
8907         CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
8908                                    CGF.Int32Ty);
8909 
8910     __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
8911         __current_saved_reg_area_pointer_int,
8912         llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
8913         "align_current_saved_reg_area_pointer");
8914 
8915     __current_saved_reg_area_pointer_int =
8916         CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
8917                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8918                               "align_current_saved_reg_area_pointer");
8919 
8920     __current_saved_reg_area_pointer =
8921         CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
8922                                    __current_saved_reg_area_pointer->getType(),
8923                                    "align_current_saved_reg_area_pointer");
8924   }
8925 
8926   llvm::Value *__new_saved_reg_area_pointer =
8927       CGF.Builder.CreateGEP(CGF.Int8Ty, __current_saved_reg_area_pointer,
8928                             llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8929                             "__new_saved_reg_area_pointer");
8930 
8931   llvm::Value *UsingStack = nullptr;
8932   UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
8933                                          __saved_reg_area_end_pointer);
8934 
8935   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
8936 
8937   // Argument in saved register area
8938   // Implement the block where argument is in register saved area
8939   CGF.EmitBlock(InRegBlock);
8940 
8941   llvm::Type *PTy = CGF.ConvertType(Ty);
8942   llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
8943       __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
8944 
8945   CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
8946                           __current_saved_reg_area_pointer_p);
8947 
8948   CGF.EmitBranch(ContBlock);
8949 
8950   // Argument in overflow area
8951   // Implement the block where the argument is in overflow area.
8952   CGF.EmitBlock(OnStackBlock);
8953 
8954   // Load the overflow area pointer
8955   Address __overflow_area_pointer_p =
8956       CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
8957   llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
8958       __overflow_area_pointer_p, "__overflow_area_pointer");
8959 
8960   // Align the overflow area pointer according to the alignment of the argument
8961   if (ArgAlign > 4) {
8962     llvm::Value *__overflow_area_pointer_int =
8963         CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
8964 
8965     __overflow_area_pointer_int =
8966         CGF.Builder.CreateAdd(__overflow_area_pointer_int,
8967                               llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
8968                               "align_overflow_area_pointer");
8969 
8970     __overflow_area_pointer_int =
8971         CGF.Builder.CreateAnd(__overflow_area_pointer_int,
8972                               llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
8973                               "align_overflow_area_pointer");
8974 
8975     __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
8976         __overflow_area_pointer_int, __overflow_area_pointer->getType(),
8977         "align_overflow_area_pointer");
8978   }
8979 
8980   // Get the pointer for next argument in overflow area and store it
8981   // to overflow area pointer.
8982   llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
8983       CGF.Int8Ty, __overflow_area_pointer,
8984       llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
8985       "__overflow_area_pointer.next");
8986 
8987   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8988                           __overflow_area_pointer_p);
8989 
8990   CGF.Builder.CreateStore(__new_overflow_area_pointer,
8991                           __current_saved_reg_area_pointer_p);
8992 
8993   // Bitcast the overflow area pointer to the type of argument.
8994   llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
8995   llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
8996       __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
8997 
8998   CGF.EmitBranch(ContBlock);
8999 
9000   // Get the correct pointer to load the variable argument
9001   // Implement the ContBlock
9002   CGF.EmitBlock(ContBlock);
9003 
9004   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
9005   llvm::Type *MemPTy = llvm::PointerType::getUnqual(MemTy);
9006   llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
9007   ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
9008   ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
9009 
9010   return Address(ArgAddr, MemTy, CharUnits::fromQuantity(ArgAlign));
9011 }
9012 
9013 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9014                                   QualType Ty) const {
9015 
9016   if (getTarget().getTriple().isMusl())
9017     return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
9018 
9019   return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
9020 }
9021 
9022 //===----------------------------------------------------------------------===//
9023 // Lanai ABI Implementation
9024 //===----------------------------------------------------------------------===//
9025 
9026 namespace {
9027 class LanaiABIInfo : public DefaultABIInfo {
9028 public:
9029   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9030 
9031   bool shouldUseInReg(QualType Ty, CCState &State) const;
9032 
9033   void computeInfo(CGFunctionInfo &FI) const override {
9034     CCState State(FI);
9035     // Lanai uses 4 registers to pass arguments unless the function has the
9036     // regparm attribute set.
9037     if (FI.getHasRegParm()) {
9038       State.FreeRegs = FI.getRegParm();
9039     } else {
9040       State.FreeRegs = 4;
9041     }
9042 
9043     if (!getCXXABI().classifyReturnType(FI))
9044       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9045     for (auto &I : FI.arguments())
9046       I.info = classifyArgumentType(I.type, State);
9047   }
9048 
9049   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
9050   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
9051 };
9052 } // end anonymous namespace
9053 
9054 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
9055   unsigned Size = getContext().getTypeSize(Ty);
9056   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
9057 
9058   if (SizeInRegs == 0)
9059     return false;
9060 
9061   if (SizeInRegs > State.FreeRegs) {
9062     State.FreeRegs = 0;
9063     return false;
9064   }
9065 
9066   State.FreeRegs -= SizeInRegs;
9067 
9068   return true;
9069 }
9070 
9071 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
9072                                            CCState &State) const {
9073   if (!ByVal) {
9074     if (State.FreeRegs) {
9075       --State.FreeRegs; // Non-byval indirects just use one pointer.
9076       return getNaturalAlignIndirectInReg(Ty);
9077     }
9078     return getNaturalAlignIndirect(Ty, false);
9079   }
9080 
9081   // Compute the byval alignment.
9082   const unsigned MinABIStackAlignInBytes = 4;
9083   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
9084   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
9085                                  /*Realign=*/TypeAlign >
9086                                      MinABIStackAlignInBytes);
9087 }
9088 
9089 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
9090                                               CCState &State) const {
9091   // Check with the C++ ABI first.
9092   const RecordType *RT = Ty->getAs<RecordType>();
9093   if (RT) {
9094     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
9095     if (RAA == CGCXXABI::RAA_Indirect) {
9096       return getIndirectResult(Ty, /*ByVal=*/false, State);
9097     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
9098       return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
9099     }
9100   }
9101 
9102   if (isAggregateTypeForABI(Ty)) {
9103     // Structures with flexible arrays are always indirect.
9104     if (RT && RT->getDecl()->hasFlexibleArrayMember())
9105       return getIndirectResult(Ty, /*ByVal=*/true, State);
9106 
9107     // Ignore empty structs/unions.
9108     if (isEmptyRecord(getContext(), Ty, true))
9109       return ABIArgInfo::getIgnore();
9110 
9111     llvm::LLVMContext &LLVMContext = getVMContext();
9112     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
9113     if (SizeInRegs <= State.FreeRegs) {
9114       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
9115       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
9116       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
9117       State.FreeRegs -= SizeInRegs;
9118       return ABIArgInfo::getDirectInReg(Result);
9119     } else {
9120       State.FreeRegs = 0;
9121     }
9122     return getIndirectResult(Ty, true, State);
9123   }
9124 
9125   // Treat an enum type as its underlying type.
9126   if (const auto *EnumTy = Ty->getAs<EnumType>())
9127     Ty = EnumTy->getDecl()->getIntegerType();
9128 
9129   bool InReg = shouldUseInReg(Ty, State);
9130 
9131   // Don't pass >64 bit integers in registers.
9132   if (const auto *EIT = Ty->getAs<BitIntType>())
9133     if (EIT->getNumBits() > 64)
9134       return getIndirectResult(Ty, /*ByVal=*/true, State);
9135 
9136   if (isPromotableIntegerTypeForABI(Ty)) {
9137     if (InReg)
9138       return ABIArgInfo::getDirectInReg();
9139     return ABIArgInfo::getExtend(Ty);
9140   }
9141   if (InReg)
9142     return ABIArgInfo::getDirectInReg();
9143   return ABIArgInfo::getDirect();
9144 }
9145 
9146 namespace {
9147 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
9148 public:
9149   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
9150       : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
9151 };
9152 }
9153 
9154 //===----------------------------------------------------------------------===//
9155 // AMDGPU ABI Implementation
9156 //===----------------------------------------------------------------------===//
9157 
9158 namespace {
9159 
9160 class AMDGPUABIInfo final : public DefaultABIInfo {
9161 private:
9162   static const unsigned MaxNumRegsForArgsRet = 16;
9163 
9164   unsigned numRegsForType(QualType Ty) const;
9165 
9166   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
9167   bool isHomogeneousAggregateSmallEnough(const Type *Base,
9168                                          uint64_t Members) const override;
9169 
9170   // Coerce HIP scalar pointer arguments from generic pointers to global ones.
9171   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
9172                                        unsigned ToAS) const {
9173     // Single value types.
9174     auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
9175     if (PtrTy && PtrTy->getAddressSpace() == FromAS)
9176       return llvm::PointerType::getWithSamePointeeType(PtrTy, ToAS);
9177     return Ty;
9178   }
9179 
9180 public:
9181   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
9182     DefaultABIInfo(CGT) {}
9183 
9184   ABIArgInfo classifyReturnType(QualType RetTy) const;
9185   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
9186   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
9187 
9188   void computeInfo(CGFunctionInfo &FI) const override;
9189   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9190                     QualType Ty) const override;
9191 };
9192 
9193 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
9194   return true;
9195 }
9196 
9197 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
9198   const Type *Base, uint64_t Members) const {
9199   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
9200 
9201   // Homogeneous Aggregates may occupy at most 16 registers.
9202   return Members * NumRegs <= MaxNumRegsForArgsRet;
9203 }
9204 
9205 /// Estimate number of registers the type will use when passed in registers.
9206 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
9207   unsigned NumRegs = 0;
9208 
9209   if (const VectorType *VT = Ty->getAs<VectorType>()) {
9210     // Compute from the number of elements. The reported size is based on the
9211     // in-memory size, which includes the padding 4th element for 3-vectors.
9212     QualType EltTy = VT->getElementType();
9213     unsigned EltSize = getContext().getTypeSize(EltTy);
9214 
9215     // 16-bit element vectors should be passed as packed.
9216     if (EltSize == 16)
9217       return (VT->getNumElements() + 1) / 2;
9218 
9219     unsigned EltNumRegs = (EltSize + 31) / 32;
9220     return EltNumRegs * VT->getNumElements();
9221   }
9222 
9223   if (const RecordType *RT = Ty->getAs<RecordType>()) {
9224     const RecordDecl *RD = RT->getDecl();
9225     assert(!RD->hasFlexibleArrayMember());
9226 
9227     for (const FieldDecl *Field : RD->fields()) {
9228       QualType FieldTy = Field->getType();
9229       NumRegs += numRegsForType(FieldTy);
9230     }
9231 
9232     return NumRegs;
9233   }
9234 
9235   return (getContext().getTypeSize(Ty) + 31) / 32;
9236 }
9237 
9238 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
9239   llvm::CallingConv::ID CC = FI.getCallingConvention();
9240 
9241   if (!getCXXABI().classifyReturnType(FI))
9242     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9243 
9244   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
9245   for (auto &Arg : FI.arguments()) {
9246     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
9247       Arg.info = classifyKernelArgumentType(Arg.type);
9248     } else {
9249       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
9250     }
9251   }
9252 }
9253 
9254 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9255                                  QualType Ty) const {
9256   llvm_unreachable("AMDGPU does not support varargs");
9257 }
9258 
9259 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
9260   if (isAggregateTypeForABI(RetTy)) {
9261     // Records with non-trivial destructors/copy-constructors should not be
9262     // returned by value.
9263     if (!getRecordArgABI(RetTy, getCXXABI())) {
9264       // Ignore empty structs/unions.
9265       if (isEmptyRecord(getContext(), RetTy, true))
9266         return ABIArgInfo::getIgnore();
9267 
9268       // Lower single-element structs to just return a regular value.
9269       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
9270         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9271 
9272       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
9273         const RecordDecl *RD = RT->getDecl();
9274         if (RD->hasFlexibleArrayMember())
9275           return DefaultABIInfo::classifyReturnType(RetTy);
9276       }
9277 
9278       // Pack aggregates <= 4 bytes into single VGPR or pair.
9279       uint64_t Size = getContext().getTypeSize(RetTy);
9280       if (Size <= 16)
9281         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9282 
9283       if (Size <= 32)
9284         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9285 
9286       if (Size <= 64) {
9287         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9288         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9289       }
9290 
9291       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
9292         return ABIArgInfo::getDirect();
9293     }
9294   }
9295 
9296   // Otherwise just do the default thing.
9297   return DefaultABIInfo::classifyReturnType(RetTy);
9298 }
9299 
9300 /// For kernels all parameters are really passed in a special buffer. It doesn't
9301 /// make sense to pass anything byval, so everything must be direct.
9302 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
9303   Ty = useFirstFieldIfTransparentUnion(Ty);
9304 
9305   // TODO: Can we omit empty structs?
9306 
9307   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9308     Ty = QualType(SeltTy, 0);
9309 
9310   llvm::Type *OrigLTy = CGT.ConvertType(Ty);
9311   llvm::Type *LTy = OrigLTy;
9312   if (getContext().getLangOpts().HIP) {
9313     LTy = coerceKernelArgumentType(
9314         OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
9315         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
9316   }
9317 
9318   // FIXME: Should also use this for OpenCL, but it requires addressing the
9319   // problem of kernels being called.
9320   //
9321   // FIXME: This doesn't apply the optimization of coercing pointers in structs
9322   // to global address space when using byref. This would require implementing a
9323   // new kind of coercion of the in-memory type when for indirect arguments.
9324   if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
9325       isAggregateTypeForABI(Ty)) {
9326     return ABIArgInfo::getIndirectAliased(
9327         getContext().getTypeAlignInChars(Ty),
9328         getContext().getTargetAddressSpace(LangAS::opencl_constant),
9329         false /*Realign*/, nullptr /*Padding*/);
9330   }
9331 
9332   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
9333   // individual elements, which confuses the Clover OpenCL backend; therefore we
9334   // have to set it to false here. Other args of getDirect() are just defaults.
9335   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
9336 }
9337 
9338 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
9339                                                unsigned &NumRegsLeft) const {
9340   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
9341 
9342   Ty = useFirstFieldIfTransparentUnion(Ty);
9343 
9344   if (isAggregateTypeForABI(Ty)) {
9345     // Records with non-trivial destructors/copy-constructors should not be
9346     // passed by value.
9347     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
9348       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9349 
9350     // Ignore empty structs/unions.
9351     if (isEmptyRecord(getContext(), Ty, true))
9352       return ABIArgInfo::getIgnore();
9353 
9354     // Lower single-element structs to just pass a regular value. TODO: We
9355     // could do reasonable-size multiple-element structs too, using getExpand(),
9356     // though watch out for things like bitfields.
9357     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
9358       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
9359 
9360     if (const RecordType *RT = Ty->getAs<RecordType>()) {
9361       const RecordDecl *RD = RT->getDecl();
9362       if (RD->hasFlexibleArrayMember())
9363         return DefaultABIInfo::classifyArgumentType(Ty);
9364     }
9365 
9366     // Pack aggregates <= 8 bytes into single VGPR or pair.
9367     uint64_t Size = getContext().getTypeSize(Ty);
9368     if (Size <= 64) {
9369       unsigned NumRegs = (Size + 31) / 32;
9370       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
9371 
9372       if (Size <= 16)
9373         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
9374 
9375       if (Size <= 32)
9376         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
9377 
9378       // XXX: Should this be i64 instead, and should the limit increase?
9379       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
9380       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
9381     }
9382 
9383     if (NumRegsLeft > 0) {
9384       unsigned NumRegs = numRegsForType(Ty);
9385       if (NumRegsLeft >= NumRegs) {
9386         NumRegsLeft -= NumRegs;
9387         return ABIArgInfo::getDirect();
9388       }
9389     }
9390   }
9391 
9392   // Otherwise just do the default thing.
9393   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
9394   if (!ArgInfo.isIndirect()) {
9395     unsigned NumRegs = numRegsForType(Ty);
9396     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
9397   }
9398 
9399   return ArgInfo;
9400 }
9401 
9402 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
9403 public:
9404   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
9405       : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
9406 
9407   void setFunctionDeclAttributes(const FunctionDecl *FD, llvm::Function *F,
9408                                  CodeGenModule &CGM) const;
9409 
9410   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
9411                            CodeGen::CodeGenModule &M) const override;
9412   unsigned getOpenCLKernelCallingConv() const override;
9413 
9414   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
9415       llvm::PointerType *T, QualType QT) const override;
9416 
9417   LangAS getASTAllocaAddressSpace() const override {
9418     return getLangASFromTargetAS(
9419         getABIInfo().getDataLayout().getAllocaAddrSpace());
9420   }
9421   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
9422                                   const VarDecl *D) const override;
9423   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
9424                                          SyncScope Scope,
9425                                          llvm::AtomicOrdering Ordering,
9426                                          llvm::LLVMContext &Ctx) const override;
9427   llvm::Function *
9428   createEnqueuedBlockKernel(CodeGenFunction &CGF,
9429                             llvm::Function *BlockInvokeFunc,
9430                             llvm::Type *BlockTy) const override;
9431   bool shouldEmitStaticExternCAliases() const override;
9432   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
9433 };
9434 }
9435 
9436 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
9437                                               llvm::GlobalValue *GV) {
9438   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
9439     return false;
9440 
9441   return D->hasAttr<OpenCLKernelAttr>() ||
9442          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
9443          (isa<VarDecl>(D) &&
9444           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
9445            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
9446            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
9447 }
9448 
9449 void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
9450     const FunctionDecl *FD, llvm::Function *F, CodeGenModule &M) const {
9451   const auto *ReqdWGS =
9452       M.getLangOpts().OpenCL ? FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
9453   const bool IsOpenCLKernel =
9454       M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>();
9455   const bool IsHIPKernel = M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>();
9456 
9457   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
9458   if (ReqdWGS || FlatWGS) {
9459     unsigned Min = 0;
9460     unsigned Max = 0;
9461     if (FlatWGS) {
9462       Min = FlatWGS->getMin()
9463                 ->EvaluateKnownConstInt(M.getContext())
9464                 .getExtValue();
9465       Max = FlatWGS->getMax()
9466                 ->EvaluateKnownConstInt(M.getContext())
9467                 .getExtValue();
9468     }
9469     if (ReqdWGS && Min == 0 && Max == 0)
9470       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
9471 
9472     if (Min != 0) {
9473       assert(Min <= Max && "Min must be less than or equal Max");
9474 
9475       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
9476       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9477     } else
9478       assert(Max == 0 && "Max must be zero");
9479   } else if (IsOpenCLKernel || IsHIPKernel) {
9480     // By default, restrict the maximum size to a value specified by
9481     // --gpu-max-threads-per-block=n or its default value for HIP.
9482     const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
9483     const unsigned DefaultMaxWorkGroupSize =
9484         IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
9485                        : M.getLangOpts().GPUMaxThreadsPerBlock;
9486     std::string AttrVal =
9487         std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
9488     F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
9489   }
9490 
9491   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
9492     unsigned Min =
9493         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
9494     unsigned Max = Attr->getMax() ? Attr->getMax()
9495                                         ->EvaluateKnownConstInt(M.getContext())
9496                                         .getExtValue()
9497                                   : 0;
9498 
9499     if (Min != 0) {
9500       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
9501 
9502       std::string AttrVal = llvm::utostr(Min);
9503       if (Max != 0)
9504         AttrVal = AttrVal + "," + llvm::utostr(Max);
9505       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
9506     } else
9507       assert(Max == 0 && "Max must be zero");
9508   }
9509 
9510   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
9511     unsigned NumSGPR = Attr->getNumSGPR();
9512 
9513     if (NumSGPR != 0)
9514       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
9515   }
9516 
9517   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
9518     uint32_t NumVGPR = Attr->getNumVGPR();
9519 
9520     if (NumVGPR != 0)
9521       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
9522   }
9523 }
9524 
9525 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
9526     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
9527   if (requiresAMDGPUProtectedVisibility(D, GV)) {
9528     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
9529     GV->setDSOLocal(true);
9530   }
9531 
9532   if (GV->isDeclaration())
9533     return;
9534 
9535   llvm::Function *F = dyn_cast<llvm::Function>(GV);
9536   if (!F)
9537     return;
9538 
9539   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
9540   if (FD)
9541     setFunctionDeclAttributes(FD, F, M);
9542 
9543   const bool IsHIPKernel =
9544       M.getLangOpts().HIP && FD && FD->hasAttr<CUDAGlobalAttr>();
9545   const bool IsOpenMPkernel =
9546       M.getLangOpts().OpenMPIsDevice &&
9547       (F->getCallingConv() == llvm::CallingConv::AMDGPU_KERNEL);
9548 
9549   // TODO: This should be moved to language specific attributes instead.
9550   if (IsHIPKernel || IsOpenMPkernel)
9551     F->addFnAttr("uniform-work-group-size", "true");
9552 
9553   if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics())
9554     F->addFnAttr("amdgpu-unsafe-fp-atomics", "true");
9555 
9556   if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
9557     F->addFnAttr("amdgpu-ieee", "false");
9558 }
9559 
9560 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
9561   return llvm::CallingConv::AMDGPU_KERNEL;
9562 }
9563 
9564 // Currently LLVM assumes null pointers always have value 0,
9565 // which results in incorrectly transformed IR. Therefore, instead of
9566 // emitting null pointers in private and local address spaces, a null
9567 // pointer in generic address space is emitted which is casted to a
9568 // pointer in local or private address space.
9569 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
9570     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
9571     QualType QT) const {
9572   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
9573     return llvm::ConstantPointerNull::get(PT);
9574 
9575   auto &Ctx = CGM.getContext();
9576   auto NPT = llvm::PointerType::getWithSamePointeeType(
9577       PT, Ctx.getTargetAddressSpace(LangAS::opencl_generic));
9578   return llvm::ConstantExpr::getAddrSpaceCast(
9579       llvm::ConstantPointerNull::get(NPT), PT);
9580 }
9581 
9582 LangAS
9583 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
9584                                                   const VarDecl *D) const {
9585   assert(!CGM.getLangOpts().OpenCL &&
9586          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
9587          "Address space agnostic languages only");
9588   LangAS DefaultGlobalAS = getLangASFromTargetAS(
9589       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
9590   if (!D)
9591     return DefaultGlobalAS;
9592 
9593   LangAS AddrSpace = D->getType().getAddressSpace();
9594   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
9595   if (AddrSpace != LangAS::Default)
9596     return AddrSpace;
9597 
9598   // Only promote to address space 4 if VarDecl has constant initialization.
9599   if (CGM.isTypeConstant(D->getType(), false) &&
9600       D->hasConstantInitialization()) {
9601     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
9602       return *ConstAS;
9603   }
9604   return DefaultGlobalAS;
9605 }
9606 
9607 llvm::SyncScope::ID
9608 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
9609                                             SyncScope Scope,
9610                                             llvm::AtomicOrdering Ordering,
9611                                             llvm::LLVMContext &Ctx) const {
9612   std::string Name;
9613   switch (Scope) {
9614   case SyncScope::HIPSingleThread:
9615     Name = "singlethread";
9616     break;
9617   case SyncScope::HIPWavefront:
9618   case SyncScope::OpenCLSubGroup:
9619     Name = "wavefront";
9620     break;
9621   case SyncScope::HIPWorkgroup:
9622   case SyncScope::OpenCLWorkGroup:
9623     Name = "workgroup";
9624     break;
9625   case SyncScope::HIPAgent:
9626   case SyncScope::OpenCLDevice:
9627     Name = "agent";
9628     break;
9629   case SyncScope::HIPSystem:
9630   case SyncScope::OpenCLAllSVMDevices:
9631     Name = "";
9632     break;
9633   }
9634 
9635   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
9636     if (!Name.empty())
9637       Name = Twine(Twine(Name) + Twine("-")).str();
9638 
9639     Name = Twine(Twine(Name) + Twine("one-as")).str();
9640   }
9641 
9642   return Ctx.getOrInsertSyncScopeID(Name);
9643 }
9644 
9645 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
9646   return false;
9647 }
9648 
9649 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
9650     const FunctionType *&FT) const {
9651   FT = getABIInfo().getContext().adjustFunctionType(
9652       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
9653 }
9654 
9655 //===----------------------------------------------------------------------===//
9656 // SPARC v8 ABI Implementation.
9657 // Based on the SPARC Compliance Definition version 2.4.1.
9658 //
9659 // Ensures that complex values are passed in registers.
9660 //
9661 namespace {
9662 class SparcV8ABIInfo : public DefaultABIInfo {
9663 public:
9664   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
9665 
9666 private:
9667   ABIArgInfo classifyReturnType(QualType RetTy) const;
9668   void computeInfo(CGFunctionInfo &FI) const override;
9669 };
9670 } // end anonymous namespace
9671 
9672 
9673 ABIArgInfo
9674 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
9675   if (Ty->isAnyComplexType()) {
9676     return ABIArgInfo::getDirect();
9677   }
9678   else {
9679     return DefaultABIInfo::classifyReturnType(Ty);
9680   }
9681 }
9682 
9683 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9684 
9685   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
9686   for (auto &Arg : FI.arguments())
9687     Arg.info = classifyArgumentType(Arg.type);
9688 }
9689 
9690 namespace {
9691 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
9692 public:
9693   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
9694       : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
9695 
9696   llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9697                                    llvm::Value *Address) const override {
9698     int Offset;
9699     if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
9700       Offset = 12;
9701     else
9702       Offset = 8;
9703     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9704                                  llvm::ConstantInt::get(CGF.Int32Ty, Offset));
9705   }
9706 
9707   llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9708                                    llvm::Value *Address) const override {
9709     int Offset;
9710     if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
9711       Offset = -12;
9712     else
9713       Offset = -8;
9714     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9715                                  llvm::ConstantInt::get(CGF.Int32Ty, Offset));
9716   }
9717 };
9718 } // end anonymous namespace
9719 
9720 //===----------------------------------------------------------------------===//
9721 // SPARC v9 ABI Implementation.
9722 // Based on the SPARC Compliance Definition version 2.4.1.
9723 //
9724 // Function arguments a mapped to a nominal "parameter array" and promoted to
9725 // registers depending on their type. Each argument occupies 8 or 16 bytes in
9726 // the array, structs larger than 16 bytes are passed indirectly.
9727 //
9728 // One case requires special care:
9729 //
9730 //   struct mixed {
9731 //     int i;
9732 //     float f;
9733 //   };
9734 //
9735 // When a struct mixed is passed by value, it only occupies 8 bytes in the
9736 // parameter array, but the int is passed in an integer register, and the float
9737 // is passed in a floating point register. This is represented as two arguments
9738 // with the LLVM IR inreg attribute:
9739 //
9740 //   declare void f(i32 inreg %i, float inreg %f)
9741 //
9742 // The code generator will only allocate 4 bytes from the parameter array for
9743 // the inreg arguments. All other arguments are allocated a multiple of 8
9744 // bytes.
9745 //
9746 namespace {
9747 class SparcV9ABIInfo : public ABIInfo {
9748 public:
9749   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
9750 
9751 private:
9752   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
9753   void computeInfo(CGFunctionInfo &FI) const override;
9754   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9755                     QualType Ty) const override;
9756 
9757   // Coercion type builder for structs passed in registers. The coercion type
9758   // serves two purposes:
9759   //
9760   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
9761   //    in registers.
9762   // 2. Expose aligned floating point elements as first-level elements, so the
9763   //    code generator knows to pass them in floating point registers.
9764   //
9765   // We also compute the InReg flag which indicates that the struct contains
9766   // aligned 32-bit floats.
9767   //
9768   struct CoerceBuilder {
9769     llvm::LLVMContext &Context;
9770     const llvm::DataLayout &DL;
9771     SmallVector<llvm::Type*, 8> Elems;
9772     uint64_t Size;
9773     bool InReg;
9774 
9775     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
9776       : Context(c), DL(dl), Size(0), InReg(false) {}
9777 
9778     // Pad Elems with integers until Size is ToSize.
9779     void pad(uint64_t ToSize) {
9780       assert(ToSize >= Size && "Cannot remove elements");
9781       if (ToSize == Size)
9782         return;
9783 
9784       // Finish the current 64-bit word.
9785       uint64_t Aligned = llvm::alignTo(Size, 64);
9786       if (Aligned > Size && Aligned <= ToSize) {
9787         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
9788         Size = Aligned;
9789       }
9790 
9791       // Add whole 64-bit words.
9792       while (Size + 64 <= ToSize) {
9793         Elems.push_back(llvm::Type::getInt64Ty(Context));
9794         Size += 64;
9795       }
9796 
9797       // Final in-word padding.
9798       if (Size < ToSize) {
9799         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
9800         Size = ToSize;
9801       }
9802     }
9803 
9804     // Add a floating point element at Offset.
9805     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
9806       // Unaligned floats are treated as integers.
9807       if (Offset % Bits)
9808         return;
9809       // The InReg flag is only required if there are any floats < 64 bits.
9810       if (Bits < 64)
9811         InReg = true;
9812       pad(Offset);
9813       Elems.push_back(Ty);
9814       Size = Offset + Bits;
9815     }
9816 
9817     // Add a struct type to the coercion type, starting at Offset (in bits).
9818     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
9819       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
9820       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
9821         llvm::Type *ElemTy = StrTy->getElementType(i);
9822         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
9823         switch (ElemTy->getTypeID()) {
9824         case llvm::Type::StructTyID:
9825           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
9826           break;
9827         case llvm::Type::FloatTyID:
9828           addFloat(ElemOffset, ElemTy, 32);
9829           break;
9830         case llvm::Type::DoubleTyID:
9831           addFloat(ElemOffset, ElemTy, 64);
9832           break;
9833         case llvm::Type::FP128TyID:
9834           addFloat(ElemOffset, ElemTy, 128);
9835           break;
9836         case llvm::Type::PointerTyID:
9837           if (ElemOffset % 64 == 0) {
9838             pad(ElemOffset);
9839             Elems.push_back(ElemTy);
9840             Size += 64;
9841           }
9842           break;
9843         default:
9844           break;
9845         }
9846       }
9847     }
9848 
9849     // Check if Ty is a usable substitute for the coercion type.
9850     bool isUsableType(llvm::StructType *Ty) const {
9851       return llvm::ArrayRef(Elems) == Ty->elements();
9852     }
9853 
9854     // Get the coercion type as a literal struct type.
9855     llvm::Type *getType() const {
9856       if (Elems.size() == 1)
9857         return Elems.front();
9858       else
9859         return llvm::StructType::get(Context, Elems);
9860     }
9861   };
9862 };
9863 } // end anonymous namespace
9864 
9865 ABIArgInfo
9866 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
9867   if (Ty->isVoidType())
9868     return ABIArgInfo::getIgnore();
9869 
9870   uint64_t Size = getContext().getTypeSize(Ty);
9871 
9872   // Anything too big to fit in registers is passed with an explicit indirect
9873   // pointer / sret pointer.
9874   if (Size > SizeLimit)
9875     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9876 
9877   // Treat an enum type as its underlying type.
9878   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9879     Ty = EnumTy->getDecl()->getIntegerType();
9880 
9881   // Integer types smaller than a register are extended.
9882   if (Size < 64 && Ty->isIntegerType())
9883     return ABIArgInfo::getExtend(Ty);
9884 
9885   if (const auto *EIT = Ty->getAs<BitIntType>())
9886     if (EIT->getNumBits() < 64)
9887       return ABIArgInfo::getExtend(Ty);
9888 
9889   // Other non-aggregates go in registers.
9890   if (!isAggregateTypeForABI(Ty))
9891     return ABIArgInfo::getDirect();
9892 
9893   // If a C++ object has either a non-trivial copy constructor or a non-trivial
9894   // destructor, it is passed with an explicit indirect pointer / sret pointer.
9895   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
9896     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
9897 
9898   // This is a small aggregate type that should be passed in registers.
9899   // Build a coercion type from the LLVM struct type.
9900   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
9901   if (!StrTy)
9902     return ABIArgInfo::getDirect();
9903 
9904   CoerceBuilder CB(getVMContext(), getDataLayout());
9905   CB.addStruct(0, StrTy);
9906   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
9907 
9908   // Try to use the original type for coercion.
9909   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
9910 
9911   if (CB.InReg)
9912     return ABIArgInfo::getDirectInReg(CoerceTy);
9913   else
9914     return ABIArgInfo::getDirect(CoerceTy);
9915 }
9916 
9917 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9918                                   QualType Ty) const {
9919   ABIArgInfo AI = classifyType(Ty, 16 * 8);
9920   llvm::Type *ArgTy = CGT.ConvertType(Ty);
9921   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
9922     AI.setCoerceToType(ArgTy);
9923 
9924   CharUnits SlotSize = CharUnits::fromQuantity(8);
9925 
9926   CGBuilderTy &Builder = CGF.Builder;
9927   Address Addr = Address(Builder.CreateLoad(VAListAddr, "ap.cur"),
9928                          getVAListElementType(CGF), SlotSize);
9929   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
9930 
9931   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
9932 
9933   Address ArgAddr = Address::invalid();
9934   CharUnits Stride;
9935   switch (AI.getKind()) {
9936   case ABIArgInfo::Expand:
9937   case ABIArgInfo::CoerceAndExpand:
9938   case ABIArgInfo::InAlloca:
9939     llvm_unreachable("Unsupported ABI kind for va_arg");
9940 
9941   case ABIArgInfo::Extend: {
9942     Stride = SlotSize;
9943     CharUnits Offset = SlotSize - TypeInfo.Width;
9944     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
9945     break;
9946   }
9947 
9948   case ABIArgInfo::Direct: {
9949     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
9950     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
9951     ArgAddr = Addr;
9952     break;
9953   }
9954 
9955   case ABIArgInfo::Indirect:
9956   case ABIArgInfo::IndirectAliased:
9957     Stride = SlotSize;
9958     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
9959     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), ArgTy,
9960                       TypeInfo.Align);
9961     break;
9962 
9963   case ABIArgInfo::Ignore:
9964     return Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeInfo.Align);
9965   }
9966 
9967   // Update VAList.
9968   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
9969   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
9970 
9971   return Builder.CreateElementBitCast(ArgAddr, ArgTy, "arg.addr");
9972 }
9973 
9974 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
9975   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
9976   for (auto &I : FI.arguments())
9977     I.info = classifyType(I.type, 16 * 8);
9978 }
9979 
9980 namespace {
9981 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
9982 public:
9983   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
9984       : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
9985 
9986   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
9987     return 14;
9988   }
9989 
9990   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
9991                                llvm::Value *Address) const override;
9992 
9993   llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
9994                                    llvm::Value *Address) const override {
9995     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
9996                                  llvm::ConstantInt::get(CGF.Int32Ty, 8));
9997   }
9998 
9999   llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
10000                                    llvm::Value *Address) const override {
10001     return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
10002                                  llvm::ConstantInt::get(CGF.Int32Ty, -8));
10003   }
10004 };
10005 } // end anonymous namespace
10006 
10007 bool
10008 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
10009                                                 llvm::Value *Address) const {
10010   // This is calculated from the LLVM and GCC tables and verified
10011   // against gcc output.  AFAIK all ABIs use the same encoding.
10012 
10013   CodeGen::CGBuilderTy &Builder = CGF.Builder;
10014 
10015   llvm::IntegerType *i8 = CGF.Int8Ty;
10016   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
10017   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
10018 
10019   // 0-31: the 8-byte general-purpose registers
10020   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
10021 
10022   // 32-63: f0-31, the 4-byte floating-point registers
10023   AssignToArrayRange(Builder, Address, Four8, 32, 63);
10024 
10025   //   Y   = 64
10026   //   PSR = 65
10027   //   WIM = 66
10028   //   TBR = 67
10029   //   PC  = 68
10030   //   NPC = 69
10031   //   FSR = 70
10032   //   CSR = 71
10033   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
10034 
10035   // 72-87: d0-15, the 8-byte floating-point registers
10036   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
10037 
10038   return false;
10039 }
10040 
10041 // ARC ABI implementation.
10042 namespace {
10043 
10044 class ARCABIInfo : public DefaultABIInfo {
10045 public:
10046   using DefaultABIInfo::DefaultABIInfo;
10047 
10048 private:
10049   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10050                     QualType Ty) const override;
10051 
10052   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
10053     if (!State.FreeRegs)
10054       return;
10055     if (Info.isIndirect() && Info.getInReg())
10056       State.FreeRegs--;
10057     else if (Info.isDirect() && Info.getInReg()) {
10058       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
10059       if (sz < State.FreeRegs)
10060         State.FreeRegs -= sz;
10061       else
10062         State.FreeRegs = 0;
10063     }
10064   }
10065 
10066   void computeInfo(CGFunctionInfo &FI) const override {
10067     CCState State(FI);
10068     // ARC uses 8 registers to pass arguments.
10069     State.FreeRegs = 8;
10070 
10071     if (!getCXXABI().classifyReturnType(FI))
10072       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10073     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
10074     for (auto &I : FI.arguments()) {
10075       I.info = classifyArgumentType(I.type, State.FreeRegs);
10076       updateState(I.info, I.type, State);
10077     }
10078   }
10079 
10080   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
10081   ABIArgInfo getIndirectByValue(QualType Ty) const;
10082   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
10083   ABIArgInfo classifyReturnType(QualType RetTy) const;
10084 };
10085 
10086 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
10087 public:
10088   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
10089       : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
10090 };
10091 
10092 
10093 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
10094   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
10095                        getNaturalAlignIndirect(Ty, false);
10096 }
10097 
10098 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
10099   // Compute the byval alignment.
10100   const unsigned MinABIStackAlignInBytes = 4;
10101   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
10102   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
10103                                  TypeAlign > MinABIStackAlignInBytes);
10104 }
10105 
10106 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10107                               QualType Ty) const {
10108   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
10109                           getContext().getTypeInfoInChars(Ty),
10110                           CharUnits::fromQuantity(4), true);
10111 }
10112 
10113 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
10114                                             uint8_t FreeRegs) const {
10115   // Handle the generic C++ ABI.
10116   const RecordType *RT = Ty->getAs<RecordType>();
10117   if (RT) {
10118     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
10119     if (RAA == CGCXXABI::RAA_Indirect)
10120       return getIndirectByRef(Ty, FreeRegs > 0);
10121 
10122     if (RAA == CGCXXABI::RAA_DirectInMemory)
10123       return getIndirectByValue(Ty);
10124   }
10125 
10126   // Treat an enum type as its underlying type.
10127   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
10128     Ty = EnumTy->getDecl()->getIntegerType();
10129 
10130   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
10131 
10132   if (isAggregateTypeForABI(Ty)) {
10133     // Structures with flexible arrays are always indirect.
10134     if (RT && RT->getDecl()->hasFlexibleArrayMember())
10135       return getIndirectByValue(Ty);
10136 
10137     // Ignore empty structs/unions.
10138     if (isEmptyRecord(getContext(), Ty, true))
10139       return ABIArgInfo::getIgnore();
10140 
10141     llvm::LLVMContext &LLVMContext = getVMContext();
10142 
10143     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
10144     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
10145     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
10146 
10147     return FreeRegs >= SizeInRegs ?
10148         ABIArgInfo::getDirectInReg(Result) :
10149         ABIArgInfo::getDirect(Result, 0, nullptr, false);
10150   }
10151 
10152   if (const auto *EIT = Ty->getAs<BitIntType>())
10153     if (EIT->getNumBits() > 64)
10154       return getIndirectByValue(Ty);
10155 
10156   return isPromotableIntegerTypeForABI(Ty)
10157              ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
10158                                        : ABIArgInfo::getExtend(Ty))
10159              : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
10160                                        : ABIArgInfo::getDirect());
10161 }
10162 
10163 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
10164   if (RetTy->isAnyComplexType())
10165     return ABIArgInfo::getDirectInReg();
10166 
10167   // Arguments of size > 4 registers are indirect.
10168   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
10169   if (RetSize > 4)
10170     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
10171 
10172   return DefaultABIInfo::classifyReturnType(RetTy);
10173 }
10174 
10175 } // End anonymous namespace.
10176 
10177 //===----------------------------------------------------------------------===//
10178 // XCore ABI Implementation
10179 //===----------------------------------------------------------------------===//
10180 
10181 namespace {
10182 
10183 /// A SmallStringEnc instance is used to build up the TypeString by passing
10184 /// it by reference between functions that append to it.
10185 typedef llvm::SmallString<128> SmallStringEnc;
10186 
10187 /// TypeStringCache caches the meta encodings of Types.
10188 ///
10189 /// The reason for caching TypeStrings is two fold:
10190 ///   1. To cache a type's encoding for later uses;
10191 ///   2. As a means to break recursive member type inclusion.
10192 ///
10193 /// A cache Entry can have a Status of:
10194 ///   NonRecursive:   The type encoding is not recursive;
10195 ///   Recursive:      The type encoding is recursive;
10196 ///   Incomplete:     An incomplete TypeString;
10197 ///   IncompleteUsed: An incomplete TypeString that has been used in a
10198 ///                   Recursive type encoding.
10199 ///
10200 /// A NonRecursive entry will have all of its sub-members expanded as fully
10201 /// as possible. Whilst it may contain types which are recursive, the type
10202 /// itself is not recursive and thus its encoding may be safely used whenever
10203 /// the type is encountered.
10204 ///
10205 /// A Recursive entry will have all of its sub-members expanded as fully as
10206 /// possible. The type itself is recursive and it may contain other types which
10207 /// are recursive. The Recursive encoding must not be used during the expansion
10208 /// of a recursive type's recursive branch. For simplicity the code uses
10209 /// IncompleteCount to reject all usage of Recursive encodings for member types.
10210 ///
10211 /// An Incomplete entry is always a RecordType and only encodes its
10212 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
10213 /// are placed into the cache during type expansion as a means to identify and
10214 /// handle recursive inclusion of types as sub-members. If there is recursion
10215 /// the entry becomes IncompleteUsed.
10216 ///
10217 /// During the expansion of a RecordType's members:
10218 ///
10219 ///   If the cache contains a NonRecursive encoding for the member type, the
10220 ///   cached encoding is used;
10221 ///
10222 ///   If the cache contains a Recursive encoding for the member type, the
10223 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
10224 ///
10225 ///   If the member is a RecordType, an Incomplete encoding is placed into the
10226 ///   cache to break potential recursive inclusion of itself as a sub-member;
10227 ///
10228 ///   Once a member RecordType has been expanded, its temporary incomplete
10229 ///   entry is removed from the cache. If a Recursive encoding was swapped out
10230 ///   it is swapped back in;
10231 ///
10232 ///   If an incomplete entry is used to expand a sub-member, the incomplete
10233 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
10234 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
10235 ///
10236 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
10237 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
10238 ///   Else the member is part of a recursive type and thus the recursion has
10239 ///   been exited too soon for the encoding to be correct for the member.
10240 ///
10241 class TypeStringCache {
10242   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
10243   struct Entry {
10244     std::string Str;     // The encoded TypeString for the type.
10245     enum Status State;   // Information about the encoding in 'Str'.
10246     std::string Swapped; // A temporary place holder for a Recursive encoding
10247                          // during the expansion of RecordType's members.
10248   };
10249   std::map<const IdentifierInfo *, struct Entry> Map;
10250   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
10251   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
10252 public:
10253   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
10254   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
10255   bool removeIncomplete(const IdentifierInfo *ID);
10256   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
10257                      bool IsRecursive);
10258   StringRef lookupStr(const IdentifierInfo *ID);
10259 };
10260 
10261 /// TypeString encodings for enum & union fields must be order.
10262 /// FieldEncoding is a helper for this ordering process.
10263 class FieldEncoding {
10264   bool HasName;
10265   std::string Enc;
10266 public:
10267   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
10268   StringRef str() { return Enc; }
10269   bool operator<(const FieldEncoding &rhs) const {
10270     if (HasName != rhs.HasName) return HasName;
10271     return Enc < rhs.Enc;
10272   }
10273 };
10274 
10275 class XCoreABIInfo : public DefaultABIInfo {
10276 public:
10277   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
10278   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10279                     QualType Ty) const override;
10280 };
10281 
10282 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
10283   mutable TypeStringCache TSC;
10284   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
10285                     const CodeGen::CodeGenModule &M) const;
10286 
10287 public:
10288   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
10289       : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
10290   void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
10291                           const llvm::MapVector<GlobalDecl, StringRef>
10292                               &MangledDeclNames) const override;
10293 };
10294 
10295 } // End anonymous namespace.
10296 
10297 // TODO: this implementation is likely now redundant with the default
10298 // EmitVAArg.
10299 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10300                                 QualType Ty) const {
10301   CGBuilderTy &Builder = CGF.Builder;
10302 
10303   // Get the VAList.
10304   CharUnits SlotSize = CharUnits::fromQuantity(4);
10305   Address AP = Address(Builder.CreateLoad(VAListAddr),
10306                        getVAListElementType(CGF), SlotSize);
10307 
10308   // Handle the argument.
10309   ABIArgInfo AI = classifyArgumentType(Ty);
10310   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
10311   llvm::Type *ArgTy = CGT.ConvertType(Ty);
10312   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
10313     AI.setCoerceToType(ArgTy);
10314   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
10315 
10316   Address Val = Address::invalid();
10317   CharUnits ArgSize = CharUnits::Zero();
10318   switch (AI.getKind()) {
10319   case ABIArgInfo::Expand:
10320   case ABIArgInfo::CoerceAndExpand:
10321   case ABIArgInfo::InAlloca:
10322     llvm_unreachable("Unsupported ABI kind for va_arg");
10323   case ABIArgInfo::Ignore:
10324     Val = Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeAlign);
10325     ArgSize = CharUnits::Zero();
10326     break;
10327   case ABIArgInfo::Extend:
10328   case ABIArgInfo::Direct:
10329     Val = Builder.CreateElementBitCast(AP, ArgTy);
10330     ArgSize = CharUnits::fromQuantity(
10331         getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
10332     ArgSize = ArgSize.alignTo(SlotSize);
10333     break;
10334   case ABIArgInfo::Indirect:
10335   case ABIArgInfo::IndirectAliased:
10336     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
10337     Val = Address(Builder.CreateLoad(Val), ArgTy, TypeAlign);
10338     ArgSize = SlotSize;
10339     break;
10340   }
10341 
10342   // Increment the VAList.
10343   if (!ArgSize.isZero()) {
10344     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
10345     Builder.CreateStore(APN.getPointer(), VAListAddr);
10346   }
10347 
10348   return Val;
10349 }
10350 
10351 /// During the expansion of a RecordType, an incomplete TypeString is placed
10352 /// into the cache as a means to identify and break recursion.
10353 /// If there is a Recursive encoding in the cache, it is swapped out and will
10354 /// be reinserted by removeIncomplete().
10355 /// All other types of encoding should have been used rather than arriving here.
10356 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
10357                                     std::string StubEnc) {
10358   if (!ID)
10359     return;
10360   Entry &E = Map[ID];
10361   assert( (E.Str.empty() || E.State == Recursive) &&
10362          "Incorrectly use of addIncomplete");
10363   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
10364   E.Swapped.swap(E.Str); // swap out the Recursive
10365   E.Str.swap(StubEnc);
10366   E.State = Incomplete;
10367   ++IncompleteCount;
10368 }
10369 
10370 /// Once the RecordType has been expanded, the temporary incomplete TypeString
10371 /// must be removed from the cache.
10372 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
10373 /// Returns true if the RecordType was defined recursively.
10374 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
10375   if (!ID)
10376     return false;
10377   auto I = Map.find(ID);
10378   assert(I != Map.end() && "Entry not present");
10379   Entry &E = I->second;
10380   assert( (E.State == Incomplete ||
10381            E.State == IncompleteUsed) &&
10382          "Entry must be an incomplete type");
10383   bool IsRecursive = false;
10384   if (E.State == IncompleteUsed) {
10385     // We made use of our Incomplete encoding, thus we are recursive.
10386     IsRecursive = true;
10387     --IncompleteUsedCount;
10388   }
10389   if (E.Swapped.empty())
10390     Map.erase(I);
10391   else {
10392     // Swap the Recursive back.
10393     E.Swapped.swap(E.Str);
10394     E.Swapped.clear();
10395     E.State = Recursive;
10396   }
10397   --IncompleteCount;
10398   return IsRecursive;
10399 }
10400 
10401 /// Add the encoded TypeString to the cache only if it is NonRecursive or
10402 /// Recursive (viz: all sub-members were expanded as fully as possible).
10403 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
10404                                     bool IsRecursive) {
10405   if (!ID || IncompleteUsedCount)
10406     return; // No key or it is an incomplete sub-type so don't add.
10407   Entry &E = Map[ID];
10408   if (IsRecursive && !E.Str.empty()) {
10409     assert(E.State==Recursive && E.Str.size() == Str.size() &&
10410            "This is not the same Recursive entry");
10411     // The parent container was not recursive after all, so we could have used
10412     // this Recursive sub-member entry after all, but we assumed the worse when
10413     // we started viz: IncompleteCount!=0.
10414     return;
10415   }
10416   assert(E.Str.empty() && "Entry already present");
10417   E.Str = Str.str();
10418   E.State = IsRecursive? Recursive : NonRecursive;
10419 }
10420 
10421 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
10422 /// are recursively expanding a type (IncompleteCount != 0) and the cached
10423 /// encoding is Recursive, return an empty StringRef.
10424 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
10425   if (!ID)
10426     return StringRef();   // We have no key.
10427   auto I = Map.find(ID);
10428   if (I == Map.end())
10429     return StringRef();   // We have no encoding.
10430   Entry &E = I->second;
10431   if (E.State == Recursive && IncompleteCount)
10432     return StringRef();   // We don't use Recursive encodings for member types.
10433 
10434   if (E.State == Incomplete) {
10435     // The incomplete type is being used to break out of recursion.
10436     E.State = IncompleteUsed;
10437     ++IncompleteUsedCount;
10438   }
10439   return E.Str;
10440 }
10441 
10442 /// The XCore ABI includes a type information section that communicates symbol
10443 /// type information to the linker. The linker uses this information to verify
10444 /// safety/correctness of things such as array bound and pointers et al.
10445 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
10446 /// This type information (TypeString) is emitted into meta data for all global
10447 /// symbols: definitions, declarations, functions & variables.
10448 ///
10449 /// The TypeString carries type, qualifier, name, size & value details.
10450 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
10451 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
10452 /// The output is tested by test/CodeGen/xcore-stringtype.c.
10453 ///
10454 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10455                           const CodeGen::CodeGenModule &CGM,
10456                           TypeStringCache &TSC);
10457 
10458 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
10459 void XCoreTargetCodeGenInfo::emitTargetMD(
10460     const Decl *D, llvm::GlobalValue *GV,
10461     const CodeGen::CodeGenModule &CGM) const {
10462   SmallStringEnc Enc;
10463   if (getTypeString(Enc, D, CGM, TSC)) {
10464     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
10465     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
10466                                 llvm::MDString::get(Ctx, Enc.str())};
10467     llvm::NamedMDNode *MD =
10468       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
10469     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
10470   }
10471 }
10472 
10473 void XCoreTargetCodeGenInfo::emitTargetMetadata(
10474     CodeGen::CodeGenModule &CGM,
10475     const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
10476   // Warning, new MangledDeclNames may be appended within this loop.
10477   // We rely on MapVector insertions adding new elements to the end
10478   // of the container.
10479   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
10480     auto Val = *(MangledDeclNames.begin() + I);
10481     llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
10482     if (GV) {
10483       const Decl *D = Val.first.getDecl()->getMostRecentDecl();
10484       emitTargetMD(D, GV, CGM);
10485     }
10486   }
10487 }
10488 
10489 //===----------------------------------------------------------------------===//
10490 // Base ABI and target codegen info implementation common between SPIR and
10491 // SPIR-V.
10492 //===----------------------------------------------------------------------===//
10493 
10494 namespace {
10495 class CommonSPIRABIInfo : public DefaultABIInfo {
10496 public:
10497   CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
10498 
10499 private:
10500   void setCCs();
10501 };
10502 
10503 class SPIRVABIInfo : public CommonSPIRABIInfo {
10504 public:
10505   SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
10506   void computeInfo(CGFunctionInfo &FI) const override;
10507 
10508 private:
10509   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
10510 };
10511 } // end anonymous namespace
10512 namespace {
10513 class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
10514 public:
10515   CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10516       : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
10517   CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
10518       : TargetCodeGenInfo(std::move(ABIInfo)) {}
10519 
10520   LangAS getASTAllocaAddressSpace() const override {
10521     return getLangASFromTargetAS(
10522         getABIInfo().getDataLayout().getAllocaAddrSpace());
10523   }
10524 
10525   unsigned getOpenCLKernelCallingConv() const override;
10526 };
10527 class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
10528 public:
10529   SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
10530       : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
10531   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
10532 };
10533 } // End anonymous namespace.
10534 
10535 void CommonSPIRABIInfo::setCCs() {
10536   assert(getRuntimeCC() == llvm::CallingConv::C);
10537   RuntimeCC = llvm::CallingConv::SPIR_FUNC;
10538 }
10539 
10540 ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
10541   if (getContext().getLangOpts().CUDAIsDevice) {
10542     // Coerce pointer arguments with default address space to CrossWorkGroup
10543     // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
10544     // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
10545     llvm::Type *LTy = CGT.ConvertType(Ty);
10546     auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
10547     auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
10548     auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
10549     if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
10550       LTy = llvm::PointerType::getWithSamePointeeType(PtrTy, GlobalAS);
10551       return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
10552     }
10553 
10554     // Force copying aggregate type in kernel arguments by value when
10555     // compiling CUDA targeting SPIR-V. This is required for the object
10556     // copied to be valid on the device.
10557     // This behavior follows the CUDA spec
10558     // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
10559     // and matches the NVPTX implementation.
10560     if (isAggregateTypeForABI(Ty))
10561       return getNaturalAlignIndirect(Ty, /* byval */ true);
10562   }
10563   return classifyArgumentType(Ty);
10564 }
10565 
10566 void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10567   // The logic is same as in DefaultABIInfo with an exception on the kernel
10568   // arguments handling.
10569   llvm::CallingConv::ID CC = FI.getCallingConvention();
10570 
10571   if (!getCXXABI().classifyReturnType(FI))
10572     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
10573 
10574   for (auto &I : FI.arguments()) {
10575     if (CC == llvm::CallingConv::SPIR_KERNEL) {
10576       I.info = classifyKernelArgumentType(I.type);
10577     } else {
10578       I.info = classifyArgumentType(I.type);
10579     }
10580   }
10581 }
10582 
10583 namespace clang {
10584 namespace CodeGen {
10585 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
10586   if (CGM.getTarget().getTriple().isSPIRV())
10587     SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
10588   else
10589     CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
10590 }
10591 }
10592 }
10593 
10594 unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
10595   return llvm::CallingConv::SPIR_KERNEL;
10596 }
10597 
10598 void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
10599     const FunctionType *&FT) const {
10600   // Convert HIP kernels to SPIR-V kernels.
10601   if (getABIInfo().getContext().getLangOpts().HIP) {
10602     FT = getABIInfo().getContext().adjustFunctionType(
10603         FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
10604     return;
10605   }
10606 }
10607 
10608 static bool appendType(SmallStringEnc &Enc, QualType QType,
10609                        const CodeGen::CodeGenModule &CGM,
10610                        TypeStringCache &TSC);
10611 
10612 /// Helper function for appendRecordType().
10613 /// Builds a SmallVector containing the encoded field types in declaration
10614 /// order.
10615 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
10616                              const RecordDecl *RD,
10617                              const CodeGen::CodeGenModule &CGM,
10618                              TypeStringCache &TSC) {
10619   for (const auto *Field : RD->fields()) {
10620     SmallStringEnc Enc;
10621     Enc += "m(";
10622     Enc += Field->getName();
10623     Enc += "){";
10624     if (Field->isBitField()) {
10625       Enc += "b(";
10626       llvm::raw_svector_ostream OS(Enc);
10627       OS << Field->getBitWidthValue(CGM.getContext());
10628       Enc += ':';
10629     }
10630     if (!appendType(Enc, Field->getType(), CGM, TSC))
10631       return false;
10632     if (Field->isBitField())
10633       Enc += ')';
10634     Enc += '}';
10635     FE.emplace_back(!Field->getName().empty(), Enc);
10636   }
10637   return true;
10638 }
10639 
10640 /// Appends structure and union types to Enc and adds encoding to cache.
10641 /// Recursively calls appendType (via extractFieldType) for each field.
10642 /// Union types have their fields ordered according to the ABI.
10643 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
10644                              const CodeGen::CodeGenModule &CGM,
10645                              TypeStringCache &TSC, const IdentifierInfo *ID) {
10646   // Append the cached TypeString if we have one.
10647   StringRef TypeString = TSC.lookupStr(ID);
10648   if (!TypeString.empty()) {
10649     Enc += TypeString;
10650     return true;
10651   }
10652 
10653   // Start to emit an incomplete TypeString.
10654   size_t Start = Enc.size();
10655   Enc += (RT->isUnionType()? 'u' : 's');
10656   Enc += '(';
10657   if (ID)
10658     Enc += ID->getName();
10659   Enc += "){";
10660 
10661   // We collect all encoded fields and order as necessary.
10662   bool IsRecursive = false;
10663   const RecordDecl *RD = RT->getDecl()->getDefinition();
10664   if (RD && !RD->field_empty()) {
10665     // An incomplete TypeString stub is placed in the cache for this RecordType
10666     // so that recursive calls to this RecordType will use it whilst building a
10667     // complete TypeString for this RecordType.
10668     SmallVector<FieldEncoding, 16> FE;
10669     std::string StubEnc(Enc.substr(Start).str());
10670     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
10671     TSC.addIncomplete(ID, std::move(StubEnc));
10672     if (!extractFieldType(FE, RD, CGM, TSC)) {
10673       (void) TSC.removeIncomplete(ID);
10674       return false;
10675     }
10676     IsRecursive = TSC.removeIncomplete(ID);
10677     // The ABI requires unions to be sorted but not structures.
10678     // See FieldEncoding::operator< for sort algorithm.
10679     if (RT->isUnionType())
10680       llvm::sort(FE);
10681     // We can now complete the TypeString.
10682     unsigned E = FE.size();
10683     for (unsigned I = 0; I != E; ++I) {
10684       if (I)
10685         Enc += ',';
10686       Enc += FE[I].str();
10687     }
10688   }
10689   Enc += '}';
10690   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
10691   return true;
10692 }
10693 
10694 /// Appends enum types to Enc and adds the encoding to the cache.
10695 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
10696                            TypeStringCache &TSC,
10697                            const IdentifierInfo *ID) {
10698   // Append the cached TypeString if we have one.
10699   StringRef TypeString = TSC.lookupStr(ID);
10700   if (!TypeString.empty()) {
10701     Enc += TypeString;
10702     return true;
10703   }
10704 
10705   size_t Start = Enc.size();
10706   Enc += "e(";
10707   if (ID)
10708     Enc += ID->getName();
10709   Enc += "){";
10710 
10711   // We collect all encoded enumerations and order them alphanumerically.
10712   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
10713     SmallVector<FieldEncoding, 16> FE;
10714     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
10715          ++I) {
10716       SmallStringEnc EnumEnc;
10717       EnumEnc += "m(";
10718       EnumEnc += I->getName();
10719       EnumEnc += "){";
10720       I->getInitVal().toString(EnumEnc);
10721       EnumEnc += '}';
10722       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
10723     }
10724     llvm::sort(FE);
10725     unsigned E = FE.size();
10726     for (unsigned I = 0; I != E; ++I) {
10727       if (I)
10728         Enc += ',';
10729       Enc += FE[I].str();
10730     }
10731   }
10732   Enc += '}';
10733   TSC.addIfComplete(ID, Enc.substr(Start), false);
10734   return true;
10735 }
10736 
10737 /// Appends type's qualifier to Enc.
10738 /// This is done prior to appending the type's encoding.
10739 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
10740   // Qualifiers are emitted in alphabetical order.
10741   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
10742   int Lookup = 0;
10743   if (QT.isConstQualified())
10744     Lookup += 1<<0;
10745   if (QT.isRestrictQualified())
10746     Lookup += 1<<1;
10747   if (QT.isVolatileQualified())
10748     Lookup += 1<<2;
10749   Enc += Table[Lookup];
10750 }
10751 
10752 /// Appends built-in types to Enc.
10753 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
10754   const char *EncType;
10755   switch (BT->getKind()) {
10756     case BuiltinType::Void:
10757       EncType = "0";
10758       break;
10759     case BuiltinType::Bool:
10760       EncType = "b";
10761       break;
10762     case BuiltinType::Char_U:
10763       EncType = "uc";
10764       break;
10765     case BuiltinType::UChar:
10766       EncType = "uc";
10767       break;
10768     case BuiltinType::SChar:
10769       EncType = "sc";
10770       break;
10771     case BuiltinType::UShort:
10772       EncType = "us";
10773       break;
10774     case BuiltinType::Short:
10775       EncType = "ss";
10776       break;
10777     case BuiltinType::UInt:
10778       EncType = "ui";
10779       break;
10780     case BuiltinType::Int:
10781       EncType = "si";
10782       break;
10783     case BuiltinType::ULong:
10784       EncType = "ul";
10785       break;
10786     case BuiltinType::Long:
10787       EncType = "sl";
10788       break;
10789     case BuiltinType::ULongLong:
10790       EncType = "ull";
10791       break;
10792     case BuiltinType::LongLong:
10793       EncType = "sll";
10794       break;
10795     case BuiltinType::Float:
10796       EncType = "ft";
10797       break;
10798     case BuiltinType::Double:
10799       EncType = "d";
10800       break;
10801     case BuiltinType::LongDouble:
10802       EncType = "ld";
10803       break;
10804     default:
10805       return false;
10806   }
10807   Enc += EncType;
10808   return true;
10809 }
10810 
10811 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
10812 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
10813                               const CodeGen::CodeGenModule &CGM,
10814                               TypeStringCache &TSC) {
10815   Enc += "p(";
10816   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
10817     return false;
10818   Enc += ')';
10819   return true;
10820 }
10821 
10822 /// Appends array encoding to Enc before calling appendType for the element.
10823 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
10824                             const ArrayType *AT,
10825                             const CodeGen::CodeGenModule &CGM,
10826                             TypeStringCache &TSC, StringRef NoSizeEnc) {
10827   if (AT->getSizeModifier() != ArrayType::Normal)
10828     return false;
10829   Enc += "a(";
10830   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
10831     CAT->getSize().toStringUnsigned(Enc);
10832   else
10833     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
10834   Enc += ':';
10835   // The Qualifiers should be attached to the type rather than the array.
10836   appendQualifier(Enc, QT);
10837   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
10838     return false;
10839   Enc += ')';
10840   return true;
10841 }
10842 
10843 /// Appends a function encoding to Enc, calling appendType for the return type
10844 /// and the arguments.
10845 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
10846                              const CodeGen::CodeGenModule &CGM,
10847                              TypeStringCache &TSC) {
10848   Enc += "f{";
10849   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
10850     return false;
10851   Enc += "}(";
10852   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
10853     // N.B. we are only interested in the adjusted param types.
10854     auto I = FPT->param_type_begin();
10855     auto E = FPT->param_type_end();
10856     if (I != E) {
10857       do {
10858         if (!appendType(Enc, *I, CGM, TSC))
10859           return false;
10860         ++I;
10861         if (I != E)
10862           Enc += ',';
10863       } while (I != E);
10864       if (FPT->isVariadic())
10865         Enc += ",va";
10866     } else {
10867       if (FPT->isVariadic())
10868         Enc += "va";
10869       else
10870         Enc += '0';
10871     }
10872   }
10873   Enc += ')';
10874   return true;
10875 }
10876 
10877 /// Handles the type's qualifier before dispatching a call to handle specific
10878 /// type encodings.
10879 static bool appendType(SmallStringEnc &Enc, QualType QType,
10880                        const CodeGen::CodeGenModule &CGM,
10881                        TypeStringCache &TSC) {
10882 
10883   QualType QT = QType.getCanonicalType();
10884 
10885   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
10886     // The Qualifiers should be attached to the type rather than the array.
10887     // Thus we don't call appendQualifier() here.
10888     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
10889 
10890   appendQualifier(Enc, QT);
10891 
10892   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
10893     return appendBuiltinType(Enc, BT);
10894 
10895   if (const PointerType *PT = QT->getAs<PointerType>())
10896     return appendPointerType(Enc, PT, CGM, TSC);
10897 
10898   if (const EnumType *ET = QT->getAs<EnumType>())
10899     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
10900 
10901   if (const RecordType *RT = QT->getAsStructureType())
10902     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10903 
10904   if (const RecordType *RT = QT->getAsUnionType())
10905     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
10906 
10907   if (const FunctionType *FT = QT->getAs<FunctionType>())
10908     return appendFunctionType(Enc, FT, CGM, TSC);
10909 
10910   return false;
10911 }
10912 
10913 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
10914                           const CodeGen::CodeGenModule &CGM,
10915                           TypeStringCache &TSC) {
10916   if (!D)
10917     return false;
10918 
10919   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
10920     if (FD->getLanguageLinkage() != CLanguageLinkage)
10921       return false;
10922     return appendType(Enc, FD->getType(), CGM, TSC);
10923   }
10924 
10925   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
10926     if (VD->getLanguageLinkage() != CLanguageLinkage)
10927       return false;
10928     QualType QT = VD->getType().getCanonicalType();
10929     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
10930       // Global ArrayTypes are given a size of '*' if the size is unknown.
10931       // The Qualifiers should be attached to the type rather than the array.
10932       // Thus we don't call appendQualifier() here.
10933       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
10934     }
10935     return appendType(Enc, QT, CGM, TSC);
10936   }
10937   return false;
10938 }
10939 
10940 //===----------------------------------------------------------------------===//
10941 // RISCV ABI Implementation
10942 //===----------------------------------------------------------------------===//
10943 
10944 namespace {
10945 class RISCVABIInfo : public DefaultABIInfo {
10946 private:
10947   // Size of the integer ('x') registers in bits.
10948   unsigned XLen;
10949   // Size of the floating point ('f') registers in bits. Note that the target
10950   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
10951   // with soft float ABI has FLen==0).
10952   unsigned FLen;
10953   static const int NumArgGPRs = 8;
10954   static const int NumArgFPRs = 8;
10955   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
10956                                       llvm::Type *&Field1Ty,
10957                                       CharUnits &Field1Off,
10958                                       llvm::Type *&Field2Ty,
10959                                       CharUnits &Field2Off) const;
10960 
10961 public:
10962   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
10963       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
10964 
10965   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
10966   // non-virtual, but computeInfo is virtual, so we overload it.
10967   void computeInfo(CGFunctionInfo &FI) const override;
10968 
10969   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
10970                                   int &ArgFPRsLeft) const;
10971   ABIArgInfo classifyReturnType(QualType RetTy) const;
10972 
10973   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
10974                     QualType Ty) const override;
10975 
10976   ABIArgInfo extendType(QualType Ty) const;
10977 
10978   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
10979                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
10980                                 CharUnits &Field2Off, int &NeededArgGPRs,
10981                                 int &NeededArgFPRs) const;
10982   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
10983                                                CharUnits Field1Off,
10984                                                llvm::Type *Field2Ty,
10985                                                CharUnits Field2Off) const;
10986 };
10987 } // end anonymous namespace
10988 
10989 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
10990   QualType RetTy = FI.getReturnType();
10991   if (!getCXXABI().classifyReturnType(FI))
10992     FI.getReturnInfo() = classifyReturnType(RetTy);
10993 
10994   // IsRetIndirect is true if classifyArgumentType indicated the value should
10995   // be passed indirect, or if the type size is a scalar greater than 2*XLen
10996   // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
10997   // in LLVM IR, relying on the backend lowering code to rewrite the argument
10998   // list and pass indirectly on RV32.
10999   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
11000   if (!IsRetIndirect && RetTy->isScalarType() &&
11001       getContext().getTypeSize(RetTy) > (2 * XLen)) {
11002     if (RetTy->isComplexType() && FLen) {
11003       QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
11004       IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
11005     } else {
11006       // This is a normal scalar > 2*XLen, such as fp128 on RV32.
11007       IsRetIndirect = true;
11008     }
11009   }
11010 
11011   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
11012   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
11013   int NumFixedArgs = FI.getNumRequiredArgs();
11014 
11015   int ArgNum = 0;
11016   for (auto &ArgInfo : FI.arguments()) {
11017     bool IsFixed = ArgNum < NumFixedArgs;
11018     ArgInfo.info =
11019         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
11020     ArgNum++;
11021   }
11022 }
11023 
11024 // Returns true if the struct is a potential candidate for the floating point
11025 // calling convention. If this function returns true, the caller is
11026 // responsible for checking that if there is only a single field then that
11027 // field is a float.
11028 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
11029                                                   llvm::Type *&Field1Ty,
11030                                                   CharUnits &Field1Off,
11031                                                   llvm::Type *&Field2Ty,
11032                                                   CharUnits &Field2Off) const {
11033   bool IsInt = Ty->isIntegralOrEnumerationType();
11034   bool IsFloat = Ty->isRealFloatingType();
11035 
11036   if (IsInt || IsFloat) {
11037     uint64_t Size = getContext().getTypeSize(Ty);
11038     if (IsInt && Size > XLen)
11039       return false;
11040     // Can't be eligible if larger than the FP registers. Half precision isn't
11041     // currently supported on RISC-V and the ABI hasn't been confirmed, so
11042     // default to the integer ABI in that case.
11043     if (IsFloat && (Size > FLen || Size < 32))
11044       return false;
11045     // Can't be eligible if an integer type was already found (int+int pairs
11046     // are not eligible).
11047     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
11048       return false;
11049     if (!Field1Ty) {
11050       Field1Ty = CGT.ConvertType(Ty);
11051       Field1Off = CurOff;
11052       return true;
11053     }
11054     if (!Field2Ty) {
11055       Field2Ty = CGT.ConvertType(Ty);
11056       Field2Off = CurOff;
11057       return true;
11058     }
11059     return false;
11060   }
11061 
11062   if (auto CTy = Ty->getAs<ComplexType>()) {
11063     if (Field1Ty)
11064       return false;
11065     QualType EltTy = CTy->getElementType();
11066     if (getContext().getTypeSize(EltTy) > FLen)
11067       return false;
11068     Field1Ty = CGT.ConvertType(EltTy);
11069     Field1Off = CurOff;
11070     Field2Ty = Field1Ty;
11071     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
11072     return true;
11073   }
11074 
11075   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
11076     uint64_t ArraySize = ATy->getSize().getZExtValue();
11077     QualType EltTy = ATy->getElementType();
11078     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
11079     for (uint64_t i = 0; i < ArraySize; ++i) {
11080       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
11081                                                 Field1Off, Field2Ty, Field2Off);
11082       if (!Ret)
11083         return false;
11084       CurOff += EltSize;
11085     }
11086     return true;
11087   }
11088 
11089   if (const auto *RTy = Ty->getAs<RecordType>()) {
11090     // Structures with either a non-trivial destructor or a non-trivial
11091     // copy constructor are not eligible for the FP calling convention.
11092     if (getRecordArgABI(Ty, CGT.getCXXABI()))
11093       return false;
11094     if (isEmptyRecord(getContext(), Ty, true))
11095       return true;
11096     const RecordDecl *RD = RTy->getDecl();
11097     // Unions aren't eligible unless they're empty (which is caught above).
11098     if (RD->isUnion())
11099       return false;
11100     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
11101     // If this is a C++ record, check the bases first.
11102     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
11103       for (const CXXBaseSpecifier &B : CXXRD->bases()) {
11104         const auto *BDecl =
11105             cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
11106         CharUnits BaseOff = Layout.getBaseClassOffset(BDecl);
11107         bool Ret = detectFPCCEligibleStructHelper(B.getType(), CurOff + BaseOff,
11108                                                   Field1Ty, Field1Off, Field2Ty,
11109                                                   Field2Off);
11110         if (!Ret)
11111           return false;
11112       }
11113     }
11114     int ZeroWidthBitFieldCount = 0;
11115     for (const FieldDecl *FD : RD->fields()) {
11116       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
11117       QualType QTy = FD->getType();
11118       if (FD->isBitField()) {
11119         unsigned BitWidth = FD->getBitWidthValue(getContext());
11120         // Allow a bitfield with a type greater than XLen as long as the
11121         // bitwidth is XLen or less.
11122         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
11123           QTy = getContext().getIntTypeForBitwidth(XLen, false);
11124         if (BitWidth == 0) {
11125           ZeroWidthBitFieldCount++;
11126           continue;
11127         }
11128       }
11129 
11130       bool Ret = detectFPCCEligibleStructHelper(
11131           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
11132           Field1Ty, Field1Off, Field2Ty, Field2Off);
11133       if (!Ret)
11134         return false;
11135 
11136       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
11137       // or int+fp structs, but are ignored for a struct with an fp field and
11138       // any number of zero-width bitfields.
11139       if (Field2Ty && ZeroWidthBitFieldCount > 0)
11140         return false;
11141     }
11142     return Field1Ty != nullptr;
11143   }
11144 
11145   return false;
11146 }
11147 
11148 // Determine if a struct is eligible for passing according to the floating
11149 // point calling convention (i.e., when flattened it contains a single fp
11150 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
11151 // NeededArgGPRs are incremented appropriately.
11152 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
11153                                             CharUnits &Field1Off,
11154                                             llvm::Type *&Field2Ty,
11155                                             CharUnits &Field2Off,
11156                                             int &NeededArgGPRs,
11157                                             int &NeededArgFPRs) const {
11158   Field1Ty = nullptr;
11159   Field2Ty = nullptr;
11160   NeededArgGPRs = 0;
11161   NeededArgFPRs = 0;
11162   bool IsCandidate = detectFPCCEligibleStructHelper(
11163       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
11164   // Not really a candidate if we have a single int but no float.
11165   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
11166     return false;
11167   if (!IsCandidate)
11168     return false;
11169   if (Field1Ty && Field1Ty->isFloatingPointTy())
11170     NeededArgFPRs++;
11171   else if (Field1Ty)
11172     NeededArgGPRs++;
11173   if (Field2Ty && Field2Ty->isFloatingPointTy())
11174     NeededArgFPRs++;
11175   else if (Field2Ty)
11176     NeededArgGPRs++;
11177   return true;
11178 }
11179 
11180 // Call getCoerceAndExpand for the two-element flattened struct described by
11181 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
11182 // appropriate coerceToType and unpaddedCoerceToType.
11183 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
11184     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
11185     CharUnits Field2Off) const {
11186   SmallVector<llvm::Type *, 3> CoerceElts;
11187   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
11188   if (!Field1Off.isZero())
11189     CoerceElts.push_back(llvm::ArrayType::get(
11190         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
11191 
11192   CoerceElts.push_back(Field1Ty);
11193   UnpaddedCoerceElts.push_back(Field1Ty);
11194 
11195   if (!Field2Ty) {
11196     return ABIArgInfo::getCoerceAndExpand(
11197         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
11198         UnpaddedCoerceElts[0]);
11199   }
11200 
11201   CharUnits Field2Align =
11202       CharUnits::fromQuantity(getDataLayout().getABITypeAlign(Field2Ty));
11203   CharUnits Field1End = Field1Off +
11204       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
11205   CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
11206 
11207   CharUnits Padding = CharUnits::Zero();
11208   if (Field2Off > Field2OffNoPadNoPack)
11209     Padding = Field2Off - Field2OffNoPadNoPack;
11210   else if (Field2Off != Field2Align && Field2Off > Field1End)
11211     Padding = Field2Off - Field1End;
11212 
11213   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
11214 
11215   if (!Padding.isZero())
11216     CoerceElts.push_back(llvm::ArrayType::get(
11217         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
11218 
11219   CoerceElts.push_back(Field2Ty);
11220   UnpaddedCoerceElts.push_back(Field2Ty);
11221 
11222   auto CoerceToType =
11223       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
11224   auto UnpaddedCoerceToType =
11225       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
11226 
11227   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
11228 }
11229 
11230 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
11231                                               int &ArgGPRsLeft,
11232                                               int &ArgFPRsLeft) const {
11233   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
11234   Ty = useFirstFieldIfTransparentUnion(Ty);
11235 
11236   // Structures with either a non-trivial destructor or a non-trivial
11237   // copy constructor are always passed indirectly.
11238   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
11239     if (ArgGPRsLeft)
11240       ArgGPRsLeft -= 1;
11241     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
11242                                            CGCXXABI::RAA_DirectInMemory);
11243   }
11244 
11245   // Ignore empty structs/unions.
11246   if (isEmptyRecord(getContext(), Ty, true))
11247     return ABIArgInfo::getIgnore();
11248 
11249   uint64_t Size = getContext().getTypeSize(Ty);
11250 
11251   // Pass floating point values via FPRs if possible.
11252   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
11253       FLen >= Size && ArgFPRsLeft) {
11254     ArgFPRsLeft--;
11255     return ABIArgInfo::getDirect();
11256   }
11257 
11258   // Complex types for the hard float ABI must be passed direct rather than
11259   // using CoerceAndExpand.
11260   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
11261     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
11262     if (getContext().getTypeSize(EltTy) <= FLen) {
11263       ArgFPRsLeft -= 2;
11264       return ABIArgInfo::getDirect();
11265     }
11266   }
11267 
11268   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
11269     llvm::Type *Field1Ty = nullptr;
11270     llvm::Type *Field2Ty = nullptr;
11271     CharUnits Field1Off = CharUnits::Zero();
11272     CharUnits Field2Off = CharUnits::Zero();
11273     int NeededArgGPRs = 0;
11274     int NeededArgFPRs = 0;
11275     bool IsCandidate =
11276         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
11277                                  NeededArgGPRs, NeededArgFPRs);
11278     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
11279         NeededArgFPRs <= ArgFPRsLeft) {
11280       ArgGPRsLeft -= NeededArgGPRs;
11281       ArgFPRsLeft -= NeededArgFPRs;
11282       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
11283                                                Field2Off);
11284     }
11285   }
11286 
11287   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
11288   // Determine the number of GPRs needed to pass the current argument
11289   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
11290   // register pairs, so may consume 3 registers.
11291   int NeededArgGPRs = 1;
11292   if (!IsFixed && NeededAlign == 2 * XLen)
11293     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
11294   else if (Size > XLen && Size <= 2 * XLen)
11295     NeededArgGPRs = 2;
11296 
11297   if (NeededArgGPRs > ArgGPRsLeft) {
11298     NeededArgGPRs = ArgGPRsLeft;
11299   }
11300 
11301   ArgGPRsLeft -= NeededArgGPRs;
11302 
11303   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
11304     // Treat an enum type as its underlying type.
11305     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
11306       Ty = EnumTy->getDecl()->getIntegerType();
11307 
11308     // All integral types are promoted to XLen width
11309     if (Size < XLen && Ty->isIntegralOrEnumerationType()) {
11310       return extendType(Ty);
11311     }
11312 
11313     if (const auto *EIT = Ty->getAs<BitIntType>()) {
11314       if (EIT->getNumBits() < XLen)
11315         return extendType(Ty);
11316       if (EIT->getNumBits() > 128 ||
11317           (!getContext().getTargetInfo().hasInt128Type() &&
11318            EIT->getNumBits() > 64))
11319         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11320     }
11321 
11322     return ABIArgInfo::getDirect();
11323   }
11324 
11325   // Aggregates which are <= 2*XLen will be passed in registers if possible,
11326   // so coerce to integers.
11327   if (Size <= 2 * XLen) {
11328     unsigned Alignment = getContext().getTypeAlign(Ty);
11329 
11330     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
11331     // required, and a 2-element XLen array if only XLen alignment is required.
11332     if (Size <= XLen) {
11333       return ABIArgInfo::getDirect(
11334           llvm::IntegerType::get(getVMContext(), XLen));
11335     } else if (Alignment == 2 * XLen) {
11336       return ABIArgInfo::getDirect(
11337           llvm::IntegerType::get(getVMContext(), 2 * XLen));
11338     } else {
11339       return ABIArgInfo::getDirect(llvm::ArrayType::get(
11340           llvm::IntegerType::get(getVMContext(), XLen), 2));
11341     }
11342   }
11343   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11344 }
11345 
11346 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
11347   if (RetTy->isVoidType())
11348     return ABIArgInfo::getIgnore();
11349 
11350   int ArgGPRsLeft = 2;
11351   int ArgFPRsLeft = FLen ? 2 : 0;
11352 
11353   // The rules for return and argument types are the same, so defer to
11354   // classifyArgumentType.
11355   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
11356                               ArgFPRsLeft);
11357 }
11358 
11359 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11360                                 QualType Ty) const {
11361   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
11362 
11363   // Empty records are ignored for parameter passing purposes.
11364   if (isEmptyRecord(getContext(), Ty, true)) {
11365     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
11366                            getVAListElementType(CGF), SlotSize);
11367     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
11368     return Addr;
11369   }
11370 
11371   auto TInfo = getContext().getTypeInfoInChars(Ty);
11372 
11373   // Arguments bigger than 2*Xlen bytes are passed indirectly.
11374   bool IsIndirect = TInfo.Width > 2 * SlotSize;
11375 
11376   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo,
11377                           SlotSize, /*AllowHigherAlign=*/true);
11378 }
11379 
11380 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
11381   int TySize = getContext().getTypeSize(Ty);
11382   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
11383   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
11384     return ABIArgInfo::getSignExtend(Ty);
11385   return ABIArgInfo::getExtend(Ty);
11386 }
11387 
11388 namespace {
11389 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
11390 public:
11391   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
11392                          unsigned FLen)
11393       : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
11394 
11395   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
11396                            CodeGen::CodeGenModule &CGM) const override {
11397     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
11398     if (!FD) return;
11399 
11400     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
11401     if (!Attr)
11402       return;
11403 
11404     const char *Kind;
11405     switch (Attr->getInterrupt()) {
11406     case RISCVInterruptAttr::user: Kind = "user"; break;
11407     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
11408     case RISCVInterruptAttr::machine: Kind = "machine"; break;
11409     }
11410 
11411     auto *Fn = cast<llvm::Function>(GV);
11412 
11413     Fn->addFnAttr("interrupt", Kind);
11414   }
11415 };
11416 } // namespace
11417 
11418 //===----------------------------------------------------------------------===//
11419 // VE ABI Implementation.
11420 //
11421 namespace {
11422 class VEABIInfo : public DefaultABIInfo {
11423 public:
11424   VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
11425 
11426 private:
11427   ABIArgInfo classifyReturnType(QualType RetTy) const;
11428   ABIArgInfo classifyArgumentType(QualType RetTy) const;
11429   void computeInfo(CGFunctionInfo &FI) const override;
11430 };
11431 } // end anonymous namespace
11432 
11433 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
11434   if (Ty->isAnyComplexType())
11435     return ABIArgInfo::getDirect();
11436   uint64_t Size = getContext().getTypeSize(Ty);
11437   if (Size < 64 && Ty->isIntegerType())
11438     return ABIArgInfo::getExtend(Ty);
11439   return DefaultABIInfo::classifyReturnType(Ty);
11440 }
11441 
11442 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
11443   if (Ty->isAnyComplexType())
11444     return ABIArgInfo::getDirect();
11445   uint64_t Size = getContext().getTypeSize(Ty);
11446   if (Size < 64 && Ty->isIntegerType())
11447     return ABIArgInfo::getExtend(Ty);
11448   return DefaultABIInfo::classifyArgumentType(Ty);
11449 }
11450 
11451 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
11452   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
11453   for (auto &Arg : FI.arguments())
11454     Arg.info = classifyArgumentType(Arg.type);
11455 }
11456 
11457 namespace {
11458 class VETargetCodeGenInfo : public TargetCodeGenInfo {
11459 public:
11460   VETargetCodeGenInfo(CodeGenTypes &CGT)
11461       : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
11462   // VE ABI requires the arguments of variadic and prototype-less functions
11463   // are passed in both registers and memory.
11464   bool isNoProtoCallVariadic(const CallArgList &args,
11465                              const FunctionNoProtoType *fnType) const override {
11466     return true;
11467   }
11468 };
11469 } // end anonymous namespace
11470 
11471 //===----------------------------------------------------------------------===//
11472 // CSKY ABI Implementation
11473 //===----------------------------------------------------------------------===//
11474 namespace {
11475 class CSKYABIInfo : public DefaultABIInfo {
11476   static const int NumArgGPRs = 4;
11477   static const int NumArgFPRs = 4;
11478 
11479   static const unsigned XLen = 32;
11480   unsigned FLen;
11481 
11482 public:
11483   CSKYABIInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
11484       : DefaultABIInfo(CGT), FLen(FLen) {}
11485 
11486   void computeInfo(CGFunctionInfo &FI) const override;
11487   ABIArgInfo classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
11488                                   int &ArgFPRsLeft,
11489                                   bool isReturnType = false) const;
11490   ABIArgInfo classifyReturnType(QualType RetTy) const;
11491 
11492   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11493                     QualType Ty) const override;
11494 };
11495 
11496 } // end anonymous namespace
11497 
11498 void CSKYABIInfo::computeInfo(CGFunctionInfo &FI) const {
11499   QualType RetTy = FI.getReturnType();
11500   if (!getCXXABI().classifyReturnType(FI))
11501     FI.getReturnInfo() = classifyReturnType(RetTy);
11502 
11503   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
11504 
11505   // We must track the number of GPRs used in order to conform to the CSKY
11506   // ABI, as integer scalars passed in registers should have signext/zeroext
11507   // when promoted.
11508   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
11509   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
11510 
11511   for (auto &ArgInfo : FI.arguments()) {
11512     ArgInfo.info = classifyArgumentType(ArgInfo.type, ArgGPRsLeft, ArgFPRsLeft);
11513   }
11514 }
11515 
11516 Address CSKYABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11517                                QualType Ty) const {
11518   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
11519 
11520   // Empty records are ignored for parameter passing purposes.
11521   if (isEmptyRecord(getContext(), Ty, true)) {
11522     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
11523                            getVAListElementType(CGF), SlotSize);
11524     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
11525     return Addr;
11526   }
11527 
11528   auto TInfo = getContext().getTypeInfoInChars(Ty);
11529 
11530   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, false, TInfo, SlotSize,
11531                           /*AllowHigherAlign=*/true);
11532 }
11533 
11534 ABIArgInfo CSKYABIInfo::classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
11535                                              int &ArgFPRsLeft,
11536                                              bool isReturnType) const {
11537   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
11538   Ty = useFirstFieldIfTransparentUnion(Ty);
11539 
11540   // Structures with either a non-trivial destructor or a non-trivial
11541   // copy constructor are always passed indirectly.
11542   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
11543     if (ArgGPRsLeft)
11544       ArgGPRsLeft -= 1;
11545     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
11546                                            CGCXXABI::RAA_DirectInMemory);
11547   }
11548 
11549   // Ignore empty structs/unions.
11550   if (isEmptyRecord(getContext(), Ty, true))
11551     return ABIArgInfo::getIgnore();
11552 
11553   if (!Ty->getAsUnionType())
11554     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
11555       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
11556 
11557   uint64_t Size = getContext().getTypeSize(Ty);
11558   // Pass floating point values via FPRs if possible.
11559   if (Ty->isFloatingType() && !Ty->isComplexType() && FLen >= Size &&
11560       ArgFPRsLeft) {
11561     ArgFPRsLeft--;
11562     return ABIArgInfo::getDirect();
11563   }
11564 
11565   // Complex types for the hard float ABI must be passed direct rather than
11566   // using CoerceAndExpand.
11567   if (Ty->isComplexType() && FLen && !isReturnType) {
11568     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
11569     if (getContext().getTypeSize(EltTy) <= FLen) {
11570       ArgFPRsLeft -= 2;
11571       return ABIArgInfo::getDirect();
11572     }
11573   }
11574 
11575   if (!isAggregateTypeForABI(Ty)) {
11576     // Treat an enum type as its underlying type.
11577     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
11578       Ty = EnumTy->getDecl()->getIntegerType();
11579 
11580     // All integral types are promoted to XLen width, unless passed on the
11581     // stack.
11582     if (Size < XLen && Ty->isIntegralOrEnumerationType())
11583       return ABIArgInfo::getExtend(Ty);
11584 
11585     if (const auto *EIT = Ty->getAs<BitIntType>()) {
11586       if (EIT->getNumBits() < XLen)
11587         return ABIArgInfo::getExtend(Ty);
11588     }
11589 
11590     return ABIArgInfo::getDirect();
11591   }
11592 
11593   // For argument type, the first 4*XLen parts of aggregate will be passed
11594   // in registers, and the rest will be passed in stack.
11595   // So we can coerce to integers directly and let backend handle it correctly.
11596   // For return type, aggregate which <= 2*XLen will be returned in registers.
11597   // Otherwise, aggregate will be returned indirectly.
11598   if (!isReturnType || (isReturnType && Size <= 2 * XLen)) {
11599     if (Size <= XLen) {
11600       return ABIArgInfo::getDirect(
11601           llvm::IntegerType::get(getVMContext(), XLen));
11602     } else {
11603       return ABIArgInfo::getDirect(llvm::ArrayType::get(
11604           llvm::IntegerType::get(getVMContext(), XLen), (Size + 31) / XLen));
11605     }
11606   }
11607   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
11608 }
11609 
11610 ABIArgInfo CSKYABIInfo::classifyReturnType(QualType RetTy) const {
11611   if (RetTy->isVoidType())
11612     return ABIArgInfo::getIgnore();
11613 
11614   int ArgGPRsLeft = 2;
11615   int ArgFPRsLeft = FLen ? 1 : 0;
11616 
11617   // The rules for return and argument types are the same, so defer to
11618   // classifyArgumentType.
11619   return classifyArgumentType(RetTy, ArgGPRsLeft, ArgFPRsLeft, true);
11620 }
11621 
11622 namespace {
11623 class CSKYTargetCodeGenInfo : public TargetCodeGenInfo {
11624 public:
11625   CSKYTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
11626       : TargetCodeGenInfo(std::make_unique<CSKYABIInfo>(CGT, FLen)) {}
11627 };
11628 } // end anonymous namespace
11629 
11630 //===----------------------------------------------------------------------===//
11631 // BPF ABI Implementation
11632 //===----------------------------------------------------------------------===//
11633 
11634 namespace {
11635 
11636 class BPFABIInfo : public DefaultABIInfo {
11637 public:
11638   BPFABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
11639 
11640   ABIArgInfo classifyArgumentType(QualType Ty) const {
11641     Ty = useFirstFieldIfTransparentUnion(Ty);
11642 
11643     if (isAggregateTypeForABI(Ty)) {
11644       uint64_t Bits = getContext().getTypeSize(Ty);
11645       if (Bits == 0)
11646         return ABIArgInfo::getIgnore();
11647 
11648       // If the aggregate needs 1 or 2 registers, do not use reference.
11649       if (Bits <= 128) {
11650         llvm::Type *CoerceTy;
11651         if (Bits <= 64) {
11652           CoerceTy =
11653               llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
11654         } else {
11655           llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
11656           CoerceTy = llvm::ArrayType::get(RegTy, 2);
11657         }
11658         return ABIArgInfo::getDirect(CoerceTy);
11659       } else {
11660         return getNaturalAlignIndirect(Ty);
11661       }
11662     }
11663 
11664     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
11665       Ty = EnumTy->getDecl()->getIntegerType();
11666 
11667     ASTContext &Context = getContext();
11668     if (const auto *EIT = Ty->getAs<BitIntType>())
11669       if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
11670         return getNaturalAlignIndirect(Ty);
11671 
11672     return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
11673                                               : ABIArgInfo::getDirect());
11674   }
11675 
11676   ABIArgInfo classifyReturnType(QualType RetTy) const {
11677     if (RetTy->isVoidType())
11678       return ABIArgInfo::getIgnore();
11679 
11680     if (isAggregateTypeForABI(RetTy))
11681       return getNaturalAlignIndirect(RetTy);
11682 
11683     // Treat an enum type as its underlying type.
11684     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
11685       RetTy = EnumTy->getDecl()->getIntegerType();
11686 
11687     ASTContext &Context = getContext();
11688     if (const auto *EIT = RetTy->getAs<BitIntType>())
11689       if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
11690         return getNaturalAlignIndirect(RetTy);
11691 
11692     // Caller will do necessary sign/zero extension.
11693     return ABIArgInfo::getDirect();
11694   }
11695 
11696   void computeInfo(CGFunctionInfo &FI) const override {
11697     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
11698     for (auto &I : FI.arguments())
11699       I.info = classifyArgumentType(I.type);
11700   }
11701 
11702 };
11703 
11704 class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
11705 public:
11706   BPFTargetCodeGenInfo(CodeGenTypes &CGT)
11707       : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(CGT)) {}
11708 
11709   const BPFABIInfo &getABIInfo() const {
11710     return static_cast<const BPFABIInfo&>(TargetCodeGenInfo::getABIInfo());
11711   }
11712 };
11713 
11714 }
11715 
11716 // LoongArch ABI Implementation. Documented at
11717 // https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
11718 //
11719 //===----------------------------------------------------------------------===//
11720 
11721 namespace {
11722 class LoongArchABIInfo : public DefaultABIInfo {
11723 private:
11724   // Size of the integer ('r') registers in bits.
11725   unsigned GRLen;
11726   // Size of the floating point ('f') registers in bits.
11727   unsigned FRLen;
11728   // Number of general-purpose argument registers.
11729   static const int NumGARs = 8;
11730   // Number of floating-point argument registers.
11731   static const int NumFARs = 8;
11732   bool detectFARsEligibleStructHelper(QualType Ty, CharUnits CurOff,
11733                                       llvm::Type *&Field1Ty,
11734                                       CharUnits &Field1Off,
11735                                       llvm::Type *&Field2Ty,
11736                                       CharUnits &Field2Off) const;
11737 
11738 public:
11739   LoongArchABIInfo(CodeGen::CodeGenTypes &CGT, unsigned GRLen, unsigned FRLen)
11740       : DefaultABIInfo(CGT), GRLen(GRLen), FRLen(FRLen) {}
11741 
11742   void computeInfo(CGFunctionInfo &FI) const override;
11743 
11744   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &GARsLeft,
11745                                   int &FARsLeft) const;
11746   ABIArgInfo classifyReturnType(QualType RetTy) const;
11747 
11748   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
11749                     QualType Ty) const override;
11750 
11751   ABIArgInfo extendType(QualType Ty) const;
11752 
11753   bool detectFARsEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
11754                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
11755                                 CharUnits &Field2Off, int &NeededArgGPRs,
11756                                 int &NeededArgFPRs) const;
11757   ABIArgInfo coerceAndExpandFARsEligibleStruct(llvm::Type *Field1Ty,
11758                                                CharUnits Field1Off,
11759                                                llvm::Type *Field2Ty,
11760                                                CharUnits Field2Off) const;
11761 };
11762 } // end anonymous namespace
11763 
11764 void LoongArchABIInfo::computeInfo(CGFunctionInfo &FI) const {
11765   QualType RetTy = FI.getReturnType();
11766   if (!getCXXABI().classifyReturnType(FI))
11767     FI.getReturnInfo() = classifyReturnType(RetTy);
11768 
11769   // IsRetIndirect is true if classifyArgumentType indicated the value should
11770   // be passed indirect, or if the type size is a scalar greater than 2*GRLen
11771   // and not a complex type with elements <= FRLen. e.g. fp128 is passed direct
11772   // in LLVM IR, relying on the backend lowering code to rewrite the argument
11773   // list and pass indirectly on LA32.
11774   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
11775   if (!IsRetIndirect && RetTy->isScalarType() &&
11776       getContext().getTypeSize(RetTy) > (2 * GRLen)) {
11777     if (RetTy->isComplexType() && FRLen) {
11778       QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
11779       IsRetIndirect = getContext().getTypeSize(EltTy) > FRLen;
11780     } else {
11781       // This is a normal scalar > 2*GRLen, such as fp128 on LA32.
11782       IsRetIndirect = true;
11783     }
11784   }
11785 
11786   // We must track the number of GARs and FARs used in order to conform to the
11787   // LoongArch ABI. As GAR usage is different for variadic arguments, we must
11788   // also track whether we are examining a vararg or not.
11789   int GARsLeft = IsRetIndirect ? NumGARs - 1 : NumGARs;
11790   int FARsLeft = FRLen ? NumFARs : 0;
11791   int NumFixedArgs = FI.getNumRequiredArgs();
11792 
11793   int ArgNum = 0;
11794   for (auto &ArgInfo : FI.arguments()) {
11795     ArgInfo.info = classifyArgumentType(
11796         ArgInfo.type, /*IsFixed=*/ArgNum < NumFixedArgs, GARsLeft, FARsLeft);
11797     ArgNum++;
11798   }
11799 }
11800 
11801 // Returns true if the struct is a potential candidate to be passed in FARs (and
11802 // GARs). If this function returns true, the caller is responsible for checking
11803 // that if there is only a single field then that field is a float.
11804 bool LoongArchABIInfo::detectFARsEligibleStructHelper(
11805     QualType Ty, CharUnits CurOff, llvm::Type *&Field1Ty, CharUnits &Field1Off,
11806     llvm::Type *&Field2Ty, CharUnits &Field2Off) const {
11807   bool IsInt = Ty->isIntegralOrEnumerationType();
11808   bool IsFloat = Ty->isRealFloatingType();
11809 
11810   if (IsInt || IsFloat) {
11811     uint64_t Size = getContext().getTypeSize(Ty);
11812     if (IsInt && Size > GRLen)
11813       return false;
11814     // Can't be eligible if larger than the FP registers. Half precision isn't
11815     // currently supported on LoongArch and the ABI hasn't been confirmed, so
11816     // default to the integer ABI in that case.
11817     if (IsFloat && (Size > FRLen || Size < 32))
11818       return false;
11819     // Can't be eligible if an integer type was already found (int+int pairs
11820     // are not eligible).
11821     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
11822       return false;
11823     if (!Field1Ty) {
11824       Field1Ty = CGT.ConvertType(Ty);
11825       Field1Off = CurOff;
11826       return true;
11827     }
11828     if (!Field2Ty) {
11829       Field2Ty = CGT.ConvertType(Ty);
11830       Field2Off = CurOff;
11831       return true;
11832     }
11833     return false;
11834   }
11835 
11836   if (auto CTy = Ty->getAs<ComplexType>()) {
11837     if (Field1Ty)
11838       return false;
11839     QualType EltTy = CTy->getElementType();
11840     if (getContext().getTypeSize(EltTy) > FRLen)
11841       return false;
11842     Field1Ty = CGT.ConvertType(EltTy);
11843     Field1Off = CurOff;
11844     Field2Ty = Field1Ty;
11845     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
11846     return true;
11847   }
11848 
11849   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
11850     uint64_t ArraySize = ATy->getSize().getZExtValue();
11851     QualType EltTy = ATy->getElementType();
11852     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
11853     for (uint64_t i = 0; i < ArraySize; ++i) {
11854       if (!detectFARsEligibleStructHelper(EltTy, CurOff, Field1Ty, Field1Off,
11855                                           Field2Ty, Field2Off))
11856         return false;
11857       CurOff += EltSize;
11858     }
11859     return true;
11860   }
11861 
11862   if (const auto *RTy = Ty->getAs<RecordType>()) {
11863     // Structures with either a non-trivial destructor or a non-trivial
11864     // copy constructor are not eligible for the FP calling convention.
11865     if (getRecordArgABI(Ty, CGT.getCXXABI()))
11866       return false;
11867     if (isEmptyRecord(getContext(), Ty, true))
11868       return true;
11869     const RecordDecl *RD = RTy->getDecl();
11870     // Unions aren't eligible unless they're empty (which is caught above).
11871     if (RD->isUnion())
11872       return false;
11873     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
11874     // If this is a C++ record, check the bases first.
11875     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
11876       for (const CXXBaseSpecifier &B : CXXRD->bases()) {
11877         const auto *BDecl =
11878             cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
11879         if (!detectFARsEligibleStructHelper(
11880                 B.getType(), CurOff + Layout.getBaseClassOffset(BDecl),
11881                 Field1Ty, Field1Off, Field2Ty, Field2Off))
11882           return false;
11883       }
11884     }
11885     for (const FieldDecl *FD : RD->fields()) {
11886       QualType QTy = FD->getType();
11887       if (FD->isBitField()) {
11888         unsigned BitWidth = FD->getBitWidthValue(getContext());
11889         // Zero-width bitfields are ignored.
11890         if (BitWidth == 0)
11891           continue;
11892         // Allow a bitfield with a type greater than GRLen as long as the
11893         // bitwidth is GRLen or less.
11894         if (getContext().getTypeSize(QTy) > GRLen && BitWidth <= GRLen) {
11895           QTy = getContext().getIntTypeForBitwidth(GRLen, false);
11896         }
11897       }
11898 
11899       if (!detectFARsEligibleStructHelper(
11900               QTy,
11901               CurOff + getContext().toCharUnitsFromBits(
11902                            Layout.getFieldOffset(FD->getFieldIndex())),
11903               Field1Ty, Field1Off, Field2Ty, Field2Off))
11904         return false;
11905     }
11906     return Field1Ty != nullptr;
11907   }
11908 
11909   return false;
11910 }
11911 
11912 // Determine if a struct is eligible to be passed in FARs (and GARs) (i.e., when
11913 // flattened it contains a single fp value, fp+fp, or int+fp of appropriate
11914 // size). If so, NeededFARs and NeededGARs are incremented appropriately.
11915 bool LoongArchABIInfo::detectFARsEligibleStruct(
11916     QualType Ty, llvm::Type *&Field1Ty, CharUnits &Field1Off,
11917     llvm::Type *&Field2Ty, CharUnits &Field2Off, int &NeededGARs,
11918     int &NeededFARs) const {
11919   Field1Ty = nullptr;
11920   Field2Ty = nullptr;
11921   NeededGARs = 0;
11922   NeededFARs = 0;
11923   if (!detectFARsEligibleStructHelper(Ty, CharUnits::Zero(), Field1Ty,
11924                                       Field1Off, Field2Ty, Field2Off))
11925     return false;
11926   // Not really a candidate if we have a single int but no float.
11927   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
11928     return false;
11929   if (Field1Ty && Field1Ty->isFloatingPointTy())
11930     NeededFARs++;
11931   else if (Field1Ty)
11932     NeededGARs++;
11933   if (Field2Ty && Field2Ty->isFloatingPointTy())
11934     NeededFARs++;
11935   else if (Field2Ty)
11936     NeededGARs++;
11937   return true;
11938 }
11939 
11940 // Call getCoerceAndExpand for the two-element flattened struct described by
11941 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
11942 // appropriate coerceToType and unpaddedCoerceToType.
11943 ABIArgInfo LoongArchABIInfo::coerceAndExpandFARsEligibleStruct(
11944     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
11945     CharUnits Field2Off) const {
11946   SmallVector<llvm::Type *, 3> CoerceElts;
11947   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
11948   if (!Field1Off.isZero())
11949     CoerceElts.push_back(llvm::ArrayType::get(
11950         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
11951 
11952   CoerceElts.push_back(Field1Ty);
11953   UnpaddedCoerceElts.push_back(Field1Ty);
11954 
11955   if (!Field2Ty) {
11956     return ABIArgInfo::getCoerceAndExpand(
11957         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
11958         UnpaddedCoerceElts[0]);
11959   }
11960 
11961   CharUnits Field2Align =
11962       CharUnits::fromQuantity(getDataLayout().getABITypeAlign(Field2Ty));
11963   CharUnits Field1End =
11964       Field1Off +
11965       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
11966   CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
11967 
11968   CharUnits Padding = CharUnits::Zero();
11969   if (Field2Off > Field2OffNoPadNoPack)
11970     Padding = Field2Off - Field2OffNoPadNoPack;
11971   else if (Field2Off != Field2Align && Field2Off > Field1End)
11972     Padding = Field2Off - Field1End;
11973 
11974   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
11975 
11976   if (!Padding.isZero())
11977     CoerceElts.push_back(llvm::ArrayType::get(
11978         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
11979 
11980   CoerceElts.push_back(Field2Ty);
11981   UnpaddedCoerceElts.push_back(Field2Ty);
11982 
11983   return ABIArgInfo::getCoerceAndExpand(
11984       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked),
11985       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked));
11986 }
11987 
11988 ABIArgInfo LoongArchABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
11989                                                   int &GARsLeft,
11990                                                   int &FARsLeft) const {
11991   assert(GARsLeft <= NumGARs && "GAR tracking underflow");
11992   Ty = useFirstFieldIfTransparentUnion(Ty);
11993 
11994   // Structures with either a non-trivial destructor or a non-trivial
11995   // copy constructor are always passed indirectly.
11996   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
11997     if (GARsLeft)
11998       GARsLeft -= 1;
11999     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
12000                                            CGCXXABI::RAA_DirectInMemory);
12001   }
12002 
12003   // Ignore empty structs/unions.
12004   if (isEmptyRecord(getContext(), Ty, true))
12005     return ABIArgInfo::getIgnore();
12006 
12007   uint64_t Size = getContext().getTypeSize(Ty);
12008 
12009   // Pass floating point values via FARs if possible.
12010   if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
12011       FRLen >= Size && FARsLeft) {
12012     FARsLeft--;
12013     return ABIArgInfo::getDirect();
12014   }
12015 
12016   // Complex types for the *f or *d ABI must be passed directly rather than
12017   // using CoerceAndExpand.
12018   if (IsFixed && Ty->isComplexType() && FRLen && FARsLeft >= 2) {
12019     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
12020     if (getContext().getTypeSize(EltTy) <= FRLen) {
12021       FARsLeft -= 2;
12022       return ABIArgInfo::getDirect();
12023     }
12024   }
12025 
12026   if (IsFixed && FRLen && Ty->isStructureOrClassType()) {
12027     llvm::Type *Field1Ty = nullptr;
12028     llvm::Type *Field2Ty = nullptr;
12029     CharUnits Field1Off = CharUnits::Zero();
12030     CharUnits Field2Off = CharUnits::Zero();
12031     int NeededGARs = 0;
12032     int NeededFARs = 0;
12033     bool IsCandidate = detectFARsEligibleStruct(
12034         Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, NeededGARs, NeededFARs);
12035     if (IsCandidate && NeededGARs <= GARsLeft && NeededFARs <= FARsLeft) {
12036       GARsLeft -= NeededGARs;
12037       FARsLeft -= NeededFARs;
12038       return coerceAndExpandFARsEligibleStruct(Field1Ty, Field1Off, Field2Ty,
12039                                                Field2Off);
12040     }
12041   }
12042 
12043   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
12044   // Determine the number of GARs needed to pass the current argument
12045   // according to the ABI. 2*GRLen-aligned varargs are passed in "aligned"
12046   // register pairs, so may consume 3 registers.
12047   int NeededGARs = 1;
12048   if (!IsFixed && NeededAlign == 2 * GRLen)
12049     NeededGARs = 2 + (GARsLeft % 2);
12050   else if (Size > GRLen && Size <= 2 * GRLen)
12051     NeededGARs = 2;
12052 
12053   if (NeededGARs > GARsLeft)
12054     NeededGARs = GARsLeft;
12055 
12056   GARsLeft -= NeededGARs;
12057 
12058   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
12059     // Treat an enum type as its underlying type.
12060     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
12061       Ty = EnumTy->getDecl()->getIntegerType();
12062 
12063     // All integral types are promoted to GRLen width.
12064     if (Size < GRLen && Ty->isIntegralOrEnumerationType())
12065       return extendType(Ty);
12066 
12067     if (const auto *EIT = Ty->getAs<BitIntType>()) {
12068       if (EIT->getNumBits() < GRLen)
12069         return extendType(Ty);
12070       if (EIT->getNumBits() > 128 ||
12071           (!getContext().getTargetInfo().hasInt128Type() &&
12072            EIT->getNumBits() > 64))
12073         return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
12074     }
12075 
12076     return ABIArgInfo::getDirect();
12077   }
12078 
12079   // Aggregates which are <= 2*GRLen will be passed in registers if possible,
12080   // so coerce to integers.
12081   if (Size <= 2 * GRLen) {
12082     // Use a single GRLen int if possible, 2*GRLen if 2*GRLen alignment is
12083     // required, and a 2-element GRLen array if only GRLen alignment is
12084     // required.
12085     if (Size <= GRLen) {
12086       return ABIArgInfo::getDirect(
12087           llvm::IntegerType::get(getVMContext(), GRLen));
12088     }
12089     if (getContext().getTypeAlign(Ty) == 2 * GRLen) {
12090       return ABIArgInfo::getDirect(
12091           llvm::IntegerType::get(getVMContext(), 2 * GRLen));
12092     }
12093     return ABIArgInfo::getDirect(
12094         llvm::ArrayType::get(llvm::IntegerType::get(getVMContext(), GRLen), 2));
12095   }
12096   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
12097 }
12098 
12099 ABIArgInfo LoongArchABIInfo::classifyReturnType(QualType RetTy) const {
12100   if (RetTy->isVoidType())
12101     return ABIArgInfo::getIgnore();
12102   // The rules for return and argument types are the same, so defer to
12103   // classifyArgumentType.
12104   int GARsLeft = 2;
12105   int FARsLeft = FRLen ? 2 : 0;
12106   return classifyArgumentType(RetTy, /*IsFixed=*/true, GARsLeft, FARsLeft);
12107 }
12108 
12109 Address LoongArchABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
12110                                     QualType Ty) const {
12111   CharUnits SlotSize = CharUnits::fromQuantity(GRLen / 8);
12112 
12113   // Empty records are ignored for parameter passing purposes.
12114   if (isEmptyRecord(getContext(), Ty, true)) {
12115     Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
12116                            getVAListElementType(CGF), SlotSize);
12117     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
12118     return Addr;
12119   }
12120 
12121   auto TInfo = getContext().getTypeInfoInChars(Ty);
12122 
12123   // Arguments bigger than 2*GRLen bytes are passed indirectly.
12124   return emitVoidPtrVAArg(CGF, VAListAddr, Ty,
12125                           /*IsIndirect=*/TInfo.Width > 2 * SlotSize, TInfo,
12126                           SlotSize,
12127                           /*AllowHigherAlign=*/true);
12128 }
12129 
12130 ABIArgInfo LoongArchABIInfo::extendType(QualType Ty) const {
12131   int TySize = getContext().getTypeSize(Ty);
12132   // LA64 ABI requires unsigned 32 bit integers to be sign extended.
12133   if (GRLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
12134     return ABIArgInfo::getSignExtend(Ty);
12135   return ABIArgInfo::getExtend(Ty);
12136 }
12137 
12138 namespace {
12139 class LoongArchTargetCodeGenInfo : public TargetCodeGenInfo {
12140 public:
12141   LoongArchTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned GRLen,
12142                              unsigned FRLen)
12143       : TargetCodeGenInfo(
12144             std::make_unique<LoongArchABIInfo>(CGT, GRLen, FRLen)) {}
12145 };
12146 } // namespace
12147 
12148 //===----------------------------------------------------------------------===//
12149 // Driver code
12150 //===----------------------------------------------------------------------===//
12151 
12152 bool CodeGenModule::supportsCOMDAT() const {
12153   return getTriple().supportsCOMDAT();
12154 }
12155 
12156 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
12157   if (TheTargetCodeGenInfo)
12158     return *TheTargetCodeGenInfo;
12159 
12160   // Helper to set the unique_ptr while still keeping the return value.
12161   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
12162     this->TheTargetCodeGenInfo.reset(P);
12163     return *P;
12164   };
12165 
12166   const llvm::Triple &Triple = getTarget().getTriple();
12167   switch (Triple.getArch()) {
12168   default:
12169     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
12170 
12171   case llvm::Triple::le32:
12172     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
12173   case llvm::Triple::m68k:
12174     return SetCGInfo(new M68kTargetCodeGenInfo(Types));
12175   case llvm::Triple::mips:
12176   case llvm::Triple::mipsel:
12177     if (Triple.getOS() == llvm::Triple::NaCl)
12178       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
12179     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
12180 
12181   case llvm::Triple::mips64:
12182   case llvm::Triple::mips64el:
12183     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
12184 
12185   case llvm::Triple::avr: {
12186     // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
12187     // on avrtiny. For passing return value, R18~R25 are used on avr, and
12188     // R22~R25 are used on avrtiny.
12189     unsigned NPR = getTarget().getABI() == "avrtiny" ? 6 : 18;
12190     unsigned NRR = getTarget().getABI() == "avrtiny" ? 4 : 8;
12191     return SetCGInfo(new AVRTargetCodeGenInfo(Types, NPR, NRR));
12192   }
12193 
12194   case llvm::Triple::aarch64:
12195   case llvm::Triple::aarch64_32:
12196   case llvm::Triple::aarch64_be: {
12197     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
12198     if (getTarget().getABI() == "darwinpcs")
12199       Kind = AArch64ABIInfo::DarwinPCS;
12200     else if (Triple.isOSWindows())
12201       return SetCGInfo(
12202           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
12203 
12204     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
12205   }
12206 
12207   case llvm::Triple::wasm32:
12208   case llvm::Triple::wasm64: {
12209     WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
12210     if (getTarget().getABI() == "experimental-mv")
12211       Kind = WebAssemblyABIInfo::ExperimentalMV;
12212     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
12213   }
12214 
12215   case llvm::Triple::arm:
12216   case llvm::Triple::armeb:
12217   case llvm::Triple::thumb:
12218   case llvm::Triple::thumbeb: {
12219     if (Triple.getOS() == llvm::Triple::Win32) {
12220       return SetCGInfo(
12221           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
12222     }
12223 
12224     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
12225     StringRef ABIStr = getTarget().getABI();
12226     if (ABIStr == "apcs-gnu")
12227       Kind = ARMABIInfo::APCS;
12228     else if (ABIStr == "aapcs16")
12229       Kind = ARMABIInfo::AAPCS16_VFP;
12230     else if (CodeGenOpts.FloatABI == "hard" ||
12231              (CodeGenOpts.FloatABI != "soft" &&
12232               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
12233                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
12234                Triple.getEnvironment() == llvm::Triple::EABIHF)))
12235       Kind = ARMABIInfo::AAPCS_VFP;
12236 
12237     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
12238   }
12239 
12240   case llvm::Triple::ppc: {
12241     if (Triple.isOSAIX())
12242       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
12243 
12244     bool IsSoftFloat =
12245         CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
12246     bool RetSmallStructInRegABI =
12247         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
12248     return SetCGInfo(
12249         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
12250   }
12251   case llvm::Triple::ppcle: {
12252     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
12253     bool RetSmallStructInRegABI =
12254         PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
12255     return SetCGInfo(
12256         new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
12257   }
12258   case llvm::Triple::ppc64:
12259     if (Triple.isOSAIX())
12260       return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
12261 
12262     if (Triple.isOSBinFormatELF()) {
12263       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
12264       if (getTarget().getABI() == "elfv2")
12265         Kind = PPC64_SVR4_ABIInfo::ELFv2;
12266       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
12267 
12268       return SetCGInfo(
12269           new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
12270     }
12271     return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
12272   case llvm::Triple::ppc64le: {
12273     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
12274     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
12275     if (getTarget().getABI() == "elfv1")
12276       Kind = PPC64_SVR4_ABIInfo::ELFv1;
12277     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
12278 
12279     return SetCGInfo(
12280         new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
12281   }
12282 
12283   case llvm::Triple::nvptx:
12284   case llvm::Triple::nvptx64:
12285     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
12286 
12287   case llvm::Triple::msp430:
12288     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
12289 
12290   case llvm::Triple::riscv32:
12291   case llvm::Triple::riscv64: {
12292     StringRef ABIStr = getTarget().getABI();
12293     unsigned XLen = getTarget().getPointerWidth(LangAS::Default);
12294     unsigned ABIFLen = 0;
12295     if (ABIStr.endswith("f"))
12296       ABIFLen = 32;
12297     else if (ABIStr.endswith("d"))
12298       ABIFLen = 64;
12299     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
12300   }
12301 
12302   case llvm::Triple::systemz: {
12303     bool SoftFloat = CodeGenOpts.FloatABI == "soft";
12304     bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
12305     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
12306   }
12307 
12308   case llvm::Triple::tce:
12309   case llvm::Triple::tcele:
12310     return SetCGInfo(new TCETargetCodeGenInfo(Types));
12311 
12312   case llvm::Triple::x86: {
12313     bool IsDarwinVectorABI = Triple.isOSDarwin();
12314     bool RetSmallStructInRegABI =
12315         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
12316     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
12317 
12318     if (Triple.getOS() == llvm::Triple::Win32) {
12319       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
12320           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
12321           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
12322     } else {
12323       return SetCGInfo(new X86_32TargetCodeGenInfo(
12324           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
12325           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
12326           CodeGenOpts.FloatABI == "soft"));
12327     }
12328   }
12329 
12330   case llvm::Triple::x86_64: {
12331     StringRef ABI = getTarget().getABI();
12332     X86AVXABILevel AVXLevel =
12333         (ABI == "avx512"
12334              ? X86AVXABILevel::AVX512
12335              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
12336 
12337     switch (Triple.getOS()) {
12338     case llvm::Triple::Win32:
12339       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
12340     default:
12341       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
12342     }
12343   }
12344   case llvm::Triple::hexagon:
12345     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
12346   case llvm::Triple::lanai:
12347     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
12348   case llvm::Triple::r600:
12349     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
12350   case llvm::Triple::amdgcn:
12351     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
12352   case llvm::Triple::sparc:
12353     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
12354   case llvm::Triple::sparcv9:
12355     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
12356   case llvm::Triple::xcore:
12357     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
12358   case llvm::Triple::arc:
12359     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
12360   case llvm::Triple::spir:
12361   case llvm::Triple::spir64:
12362     return SetCGInfo(new CommonSPIRTargetCodeGenInfo(Types));
12363   case llvm::Triple::spirv32:
12364   case llvm::Triple::spirv64:
12365     return SetCGInfo(new SPIRVTargetCodeGenInfo(Types));
12366   case llvm::Triple::ve:
12367     return SetCGInfo(new VETargetCodeGenInfo(Types));
12368   case llvm::Triple::csky: {
12369     bool IsSoftFloat = !getTarget().hasFeature("hard-float-abi");
12370     bool hasFP64 = getTarget().hasFeature("fpuv2_df") ||
12371                    getTarget().hasFeature("fpuv3_df");
12372     return SetCGInfo(new CSKYTargetCodeGenInfo(Types, IsSoftFloat ? 0
12373                                                       : hasFP64   ? 64
12374                                                                   : 32));
12375   }
12376   case llvm::Triple::bpfeb:
12377   case llvm::Triple::bpfel:
12378     return SetCGInfo(new BPFTargetCodeGenInfo(Types));
12379   case llvm::Triple::loongarch32:
12380   case llvm::Triple::loongarch64: {
12381     StringRef ABIStr = getTarget().getABI();
12382     unsigned ABIFRLen = 0;
12383     if (ABIStr.endswith("f"))
12384       ABIFRLen = 32;
12385     else if (ABIStr.endswith("d"))
12386       ABIFRLen = 64;
12387     return SetCGInfo(new LoongArchTargetCodeGenInfo(
12388         Types, getTarget().getPointerWidth(LangAS::Default), ABIFRLen));
12389   }
12390   }
12391 }
12392 
12393 /// Create an OpenCL kernel for an enqueued block.
12394 ///
12395 /// The kernel has the same function type as the block invoke function. Its
12396 /// name is the name of the block invoke function postfixed with "_kernel".
12397 /// It simply calls the block invoke function then returns.
12398 llvm::Function *
12399 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
12400                                              llvm::Function *Invoke,
12401                                              llvm::Type *BlockTy) const {
12402   auto *InvokeFT = Invoke->getFunctionType();
12403   auto &C = CGF.getLLVMContext();
12404   std::string Name = Invoke->getName().str() + "_kernel";
12405   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C),
12406                                      InvokeFT->params(), false);
12407   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::ExternalLinkage, Name,
12408                                    &CGF.CGM.getModule());
12409   auto IP = CGF.Builder.saveIP();
12410   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
12411   auto &Builder = CGF.Builder;
12412   Builder.SetInsertPoint(BB);
12413   llvm::SmallVector<llvm::Value *, 2> Args(llvm::make_pointer_range(F->args()));
12414   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
12415   call->setCallingConv(Invoke->getCallingConv());
12416   Builder.CreateRetVoid();
12417   Builder.restoreIP(IP);
12418   return F;
12419 }
12420 
12421 /// Create an OpenCL kernel for an enqueued block.
12422 ///
12423 /// The type of the first argument (the block literal) is the struct type
12424 /// of the block literal instead of a pointer type. The first argument
12425 /// (block literal) is passed directly by value to the kernel. The kernel
12426 /// allocates the same type of struct on stack and stores the block literal
12427 /// to it and passes its pointer to the block invoke function. The kernel
12428 /// has "enqueued-block" function attribute and kernel argument metadata.
12429 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
12430     CodeGenFunction &CGF, llvm::Function *Invoke,
12431     llvm::Type *BlockTy) const {
12432   auto &Builder = CGF.Builder;
12433   auto &C = CGF.getLLVMContext();
12434 
12435   auto *InvokeFT = Invoke->getFunctionType();
12436   llvm::SmallVector<llvm::Type *, 2> ArgTys;
12437   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
12438   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
12439   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
12440   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
12441   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
12442   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
12443 
12444   ArgTys.push_back(BlockTy);
12445   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
12446   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
12447   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
12448   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
12449   AccessQuals.push_back(llvm::MDString::get(C, "none"));
12450   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
12451   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
12452     ArgTys.push_back(InvokeFT->getParamType(I));
12453     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
12454     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
12455     AccessQuals.push_back(llvm::MDString::get(C, "none"));
12456     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
12457     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
12458     ArgNames.push_back(
12459         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
12460   }
12461   std::string Name = Invoke->getName().str() + "_kernel";
12462   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
12463   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
12464                                    &CGF.CGM.getModule());
12465   F->addFnAttr("enqueued-block");
12466   auto IP = CGF.Builder.saveIP();
12467   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
12468   Builder.SetInsertPoint(BB);
12469   const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
12470   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
12471   BlockPtr->setAlignment(BlockAlign);
12472   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
12473   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
12474   llvm::SmallVector<llvm::Value *, 2> Args;
12475   Args.push_back(Cast);
12476   for (llvm::Argument &A : llvm::drop_begin(F->args()))
12477     Args.push_back(&A);
12478   llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
12479   call->setCallingConv(Invoke->getCallingConv());
12480   Builder.CreateRetVoid();
12481   Builder.restoreIP(IP);
12482 
12483   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
12484   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
12485   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
12486   F->setMetadata("kernel_arg_base_type",
12487                  llvm::MDNode::get(C, ArgBaseTypeNames));
12488   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
12489   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
12490     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
12491 
12492   return F;
12493 }
12494