1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "TargetInfo.h"
16 #include "ABIInfo.h"
17 #include "CGCXXABI.h"
18 #include "CGValue.h"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/CodeGen/CGFunctionInfo.h"
22 #include "clang/CodeGen/SwiftCallingConv.h"
23 #include "clang/Frontend/CodeGenOptions.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>    // std::sort
31 
32 using namespace clang;
33 using namespace CodeGen;
34 
35 // Helper for coercing an aggregate argument or return value into an integer
36 // array of the same size (including padding) and alignment.  This alternate
37 // coercion happens only for the RenderScript ABI and can be removed after
38 // runtimes that rely on it are no longer supported.
39 //
40 // RenderScript assumes that the size of the argument / return value in the IR
41 // is the same as the size of the corresponding qualified type. This helper
42 // coerces the aggregate type into an array of the same size (including
43 // padding).  This coercion is used in lieu of expansion of struct members or
44 // other canonical coercions that return a coerced-type of larger size.
45 //
46 // Ty          - The argument / return value type
47 // Context     - The associated ASTContext
48 // LLVMContext - The associated LLVMContext
49 static ABIArgInfo coerceToIntArray(QualType Ty,
50                                    ASTContext &Context,
51                                    llvm::LLVMContext &LLVMContext) {
52   // Alignment and Size are measured in bits.
53   const uint64_t Size = Context.getTypeSize(Ty);
54   const uint64_t Alignment = Context.getTypeAlign(Ty);
55   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
56   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
57   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
58 }
59 
60 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
61                                llvm::Value *Array,
62                                llvm::Value *Value,
63                                unsigned FirstIndex,
64                                unsigned LastIndex) {
65   // Alternatively, we could emit this as a loop in the source.
66   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
67     llvm::Value *Cell =
68         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
69     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
70   }
71 }
72 
73 static bool isAggregateTypeForABI(QualType T) {
74   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
75          T->isMemberFunctionPointerType();
76 }
77 
78 ABIArgInfo
79 ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
80                                  llvm::Type *Padding) const {
81   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
82                                  ByRef, Realign, Padding);
83 }
84 
85 ABIArgInfo
86 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
87   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
88                                       /*ByRef*/ false, Realign);
89 }
90 
91 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
92                              QualType Ty) const {
93   return Address::invalid();
94 }
95 
96 ABIInfo::~ABIInfo() {}
97 
98 /// Does the given lowering require more than the given number of
99 /// registers when expanded?
100 ///
101 /// This is intended to be the basis of a reasonable basic implementation
102 /// of should{Pass,Return}IndirectlyForSwift.
103 ///
104 /// For most targets, a limit of four total registers is reasonable; this
105 /// limits the amount of code required in order to move around the value
106 /// in case it wasn't produced immediately prior to the call by the caller
107 /// (or wasn't produced in exactly the right registers) or isn't used
108 /// immediately within the callee.  But some targets may need to further
109 /// limit the register count due to an inability to support that many
110 /// return registers.
111 static bool occupiesMoreThan(CodeGenTypes &cgt,
112                              ArrayRef<llvm::Type*> scalarTypes,
113                              unsigned maxAllRegisters) {
114   unsigned intCount = 0, fpCount = 0;
115   for (llvm::Type *type : scalarTypes) {
116     if (type->isPointerTy()) {
117       intCount++;
118     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
119       auto ptrWidth = cgt.getTarget().getPointerWidth(0);
120       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
121     } else {
122       assert(type->isVectorTy() || type->isFloatingPointTy());
123       fpCount++;
124     }
125   }
126 
127   return (intCount + fpCount > maxAllRegisters);
128 }
129 
130 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
131                                              llvm::Type *eltTy,
132                                              unsigned numElts) const {
133   // The default implementation of this assumes that the target guarantees
134   // 128-bit SIMD support but nothing more.
135   return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
136 }
137 
138 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
139                                               CGCXXABI &CXXABI) {
140   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
141   if (!RD)
142     return CGCXXABI::RAA_Default;
143   return CXXABI.getRecordArgABI(RD);
144 }
145 
146 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
147                                               CGCXXABI &CXXABI) {
148   const RecordType *RT = T->getAs<RecordType>();
149   if (!RT)
150     return CGCXXABI::RAA_Default;
151   return getRecordArgABI(RT, CXXABI);
152 }
153 
154 /// Pass transparent unions as if they were the type of the first element. Sema
155 /// should ensure that all elements of the union have the same "machine type".
156 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
157   if (const RecordType *UT = Ty->getAsUnionType()) {
158     const RecordDecl *UD = UT->getDecl();
159     if (UD->hasAttr<TransparentUnionAttr>()) {
160       assert(!UD->field_empty() && "sema created an empty transparent union");
161       return UD->field_begin()->getType();
162     }
163   }
164   return Ty;
165 }
166 
167 CGCXXABI &ABIInfo::getCXXABI() const {
168   return CGT.getCXXABI();
169 }
170 
171 ASTContext &ABIInfo::getContext() const {
172   return CGT.getContext();
173 }
174 
175 llvm::LLVMContext &ABIInfo::getVMContext() const {
176   return CGT.getLLVMContext();
177 }
178 
179 const llvm::DataLayout &ABIInfo::getDataLayout() const {
180   return CGT.getDataLayout();
181 }
182 
183 const TargetInfo &ABIInfo::getTarget() const {
184   return CGT.getTarget();
185 }
186 
187 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
188   return CGT.getCodeGenOpts();
189 }
190 
191 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
192 
193 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
194   return false;
195 }
196 
197 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
198                                                 uint64_t Members) const {
199   return false;
200 }
201 
202 bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
203   return false;
204 }
205 
206 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
207   raw_ostream &OS = llvm::errs();
208   OS << "(ABIArgInfo Kind=";
209   switch (TheKind) {
210   case Direct:
211     OS << "Direct Type=";
212     if (llvm::Type *Ty = getCoerceToType())
213       Ty->print(OS);
214     else
215       OS << "null";
216     break;
217   case Extend:
218     OS << "Extend";
219     break;
220   case Ignore:
221     OS << "Ignore";
222     break;
223   case InAlloca:
224     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
225     break;
226   case Indirect:
227     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
228        << " ByVal=" << getIndirectByVal()
229        << " Realign=" << getIndirectRealign();
230     break;
231   case Expand:
232     OS << "Expand";
233     break;
234   case CoerceAndExpand:
235     OS << "CoerceAndExpand Type=";
236     getCoerceAndExpandType()->print(OS);
237     break;
238   }
239   OS << ")\n";
240 }
241 
242 // Dynamically round a pointer up to a multiple of the given alignment.
243 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
244                                                   llvm::Value *Ptr,
245                                                   CharUnits Align) {
246   llvm::Value *PtrAsInt = Ptr;
247   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
248   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
249   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
250         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
251   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
252            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
253   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
254                                         Ptr->getType(),
255                                         Ptr->getName() + ".aligned");
256   return PtrAsInt;
257 }
258 
259 /// Emit va_arg for a platform using the common void* representation,
260 /// where arguments are simply emitted in an array of slots on the stack.
261 ///
262 /// This version implements the core direct-value passing rules.
263 ///
264 /// \param SlotSize - The size and alignment of a stack slot.
265 ///   Each argument will be allocated to a multiple of this number of
266 ///   slots, and all the slots will be aligned to this value.
267 /// \param AllowHigherAlign - The slot alignment is not a cap;
268 ///   an argument type with an alignment greater than the slot size
269 ///   will be emitted on a higher-alignment address, potentially
270 ///   leaving one or more empty slots behind as padding.  If this
271 ///   is false, the returned address might be less-aligned than
272 ///   DirectAlign.
273 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
274                                       Address VAListAddr,
275                                       llvm::Type *DirectTy,
276                                       CharUnits DirectSize,
277                                       CharUnits DirectAlign,
278                                       CharUnits SlotSize,
279                                       bool AllowHigherAlign) {
280   // Cast the element type to i8* if necessary.  Some platforms define
281   // va_list as a struct containing an i8* instead of just an i8*.
282   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
283     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
284 
285   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
286 
287   // If the CC aligns values higher than the slot size, do so if needed.
288   Address Addr = Address::invalid();
289   if (AllowHigherAlign && DirectAlign > SlotSize) {
290     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
291                                                  DirectAlign);
292   } else {
293     Addr = Address(Ptr, SlotSize);
294   }
295 
296   // Advance the pointer past the argument, then store that back.
297   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
298   llvm::Value *NextPtr =
299     CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
300                                            "argp.next");
301   CGF.Builder.CreateStore(NextPtr, VAListAddr);
302 
303   // If the argument is smaller than a slot, and this is a big-endian
304   // target, the argument will be right-adjusted in its slot.
305   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
306       !DirectTy->isStructTy()) {
307     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
308   }
309 
310   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
311   return Addr;
312 }
313 
314 /// Emit va_arg for a platform using the common void* representation,
315 /// where arguments are simply emitted in an array of slots on the stack.
316 ///
317 /// \param IsIndirect - Values of this type are passed indirectly.
318 /// \param ValueInfo - The size and alignment of this type, generally
319 ///   computed with getContext().getTypeInfoInChars(ValueTy).
320 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
321 ///   Each argument will be allocated to a multiple of this number of
322 ///   slots, and all the slots will be aligned to this value.
323 /// \param AllowHigherAlign - The slot alignment is not a cap;
324 ///   an argument type with an alignment greater than the slot size
325 ///   will be emitted on a higher-alignment address, potentially
326 ///   leaving one or more empty slots behind as padding.
327 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
328                                 QualType ValueTy, bool IsIndirect,
329                                 std::pair<CharUnits, CharUnits> ValueInfo,
330                                 CharUnits SlotSizeAndAlign,
331                                 bool AllowHigherAlign) {
332   // The size and alignment of the value that was passed directly.
333   CharUnits DirectSize, DirectAlign;
334   if (IsIndirect) {
335     DirectSize = CGF.getPointerSize();
336     DirectAlign = CGF.getPointerAlign();
337   } else {
338     DirectSize = ValueInfo.first;
339     DirectAlign = ValueInfo.second;
340   }
341 
342   // Cast the address we've calculated to the right type.
343   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
344   if (IsIndirect)
345     DirectTy = DirectTy->getPointerTo(0);
346 
347   Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
348                                         DirectSize, DirectAlign,
349                                         SlotSizeAndAlign,
350                                         AllowHigherAlign);
351 
352   if (IsIndirect) {
353     Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
354   }
355 
356   return Addr;
357 
358 }
359 
360 static Address emitMergePHI(CodeGenFunction &CGF,
361                             Address Addr1, llvm::BasicBlock *Block1,
362                             Address Addr2, llvm::BasicBlock *Block2,
363                             const llvm::Twine &Name = "") {
364   assert(Addr1.getType() == Addr2.getType());
365   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
366   PHI->addIncoming(Addr1.getPointer(), Block1);
367   PHI->addIncoming(Addr2.getPointer(), Block2);
368   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
369   return Address(PHI, Align);
370 }
371 
372 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
373 
374 // If someone can figure out a general rule for this, that would be great.
375 // It's probably just doomed to be platform-dependent, though.
376 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
377   // Verified for:
378   //   x86-64     FreeBSD, Linux, Darwin
379   //   x86-32     FreeBSD, Linux, Darwin
380   //   PowerPC    Linux, Darwin
381   //   ARM        Darwin (*not* EABI)
382   //   AArch64    Linux
383   return 32;
384 }
385 
386 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
387                                      const FunctionNoProtoType *fnType) const {
388   // The following conventions are known to require this to be false:
389   //   x86_stdcall
390   //   MIPS
391   // For everything else, we just prefer false unless we opt out.
392   return false;
393 }
394 
395 void
396 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
397                                              llvm::SmallString<24> &Opt) const {
398   // This assumes the user is passing a library name like "rt" instead of a
399   // filename like "librt.a/so", and that they don't care whether it's static or
400   // dynamic.
401   Opt = "-l";
402   Opt += Lib;
403 }
404 
405 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
406   // OpenCL kernels are called via an explicit runtime API with arguments
407   // set with clSetKernelArg(), not as normal sub-functions.
408   // Return SPIR_KERNEL by default as the kernel calling convention to
409   // ensure the fingerprint is fixed such way that each OpenCL argument
410   // gets one matching argument in the produced kernel function argument
411   // list to enable feasible implementation of clSetKernelArg() with
412   // aggregates etc. In case we would use the default C calling conv here,
413   // clSetKernelArg() might break depending on the target-specific
414   // conventions; different targets might split structs passed as values
415   // to multiple function arguments etc.
416   return llvm::CallingConv::SPIR_KERNEL;
417 }
418 
419 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
420     llvm::PointerType *T, QualType QT) const {
421   return llvm::ConstantPointerNull::get(T);
422 }
423 
424 unsigned TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
425                                                      const VarDecl *D) const {
426   assert(!CGM.getLangOpts().OpenCL &&
427          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
428          "Address space agnostic languages only");
429   return D ? D->getType().getAddressSpace()
430            : static_cast<unsigned>(LangAS::Default);
431 }
432 
433 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
434     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, unsigned SrcAddr,
435     unsigned DestAddr, llvm::Type *DestTy, bool isNonNull) const {
436   // Since target may map different address spaces in AST to the same address
437   // space, an address space conversion may end up as a bitcast.
438   if (auto *C = dyn_cast<llvm::Constant>(Src))
439     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
440   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy);
441 }
442 
443 llvm::Constant *
444 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
445                                         unsigned SrcAddr, unsigned DestAddr,
446                                         llvm::Type *DestTy) const {
447   // Since target may map different address spaces in AST to the same address
448   // space, an address space conversion may end up as a bitcast.
449   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
450 }
451 
452 llvm::SyncScope::ID
453 TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const {
454   return C.getOrInsertSyncScopeID(""); /* default sync scope */
455 }
456 
457 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
458 
459 /// isEmptyField - Return true iff a the field is "empty", that is it
460 /// is an unnamed bit-field or an (array of) empty record(s).
461 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
462                          bool AllowArrays) {
463   if (FD->isUnnamedBitfield())
464     return true;
465 
466   QualType FT = FD->getType();
467 
468   // Constant arrays of empty records count as empty, strip them off.
469   // Constant arrays of zero length always count as empty.
470   if (AllowArrays)
471     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
472       if (AT->getSize() == 0)
473         return true;
474       FT = AT->getElementType();
475     }
476 
477   const RecordType *RT = FT->getAs<RecordType>();
478   if (!RT)
479     return false;
480 
481   // C++ record fields are never empty, at least in the Itanium ABI.
482   //
483   // FIXME: We should use a predicate for whether this behavior is true in the
484   // current ABI.
485   if (isa<CXXRecordDecl>(RT->getDecl()))
486     return false;
487 
488   return isEmptyRecord(Context, FT, AllowArrays);
489 }
490 
491 /// isEmptyRecord - Return true iff a structure contains only empty
492 /// fields. Note that a structure with a flexible array member is not
493 /// considered empty.
494 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
495   const RecordType *RT = T->getAs<RecordType>();
496   if (!RT)
497     return false;
498   const RecordDecl *RD = RT->getDecl();
499   if (RD->hasFlexibleArrayMember())
500     return false;
501 
502   // If this is a C++ record, check the bases first.
503   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
504     for (const auto &I : CXXRD->bases())
505       if (!isEmptyRecord(Context, I.getType(), true))
506         return false;
507 
508   for (const auto *I : RD->fields())
509     if (!isEmptyField(Context, I, AllowArrays))
510       return false;
511   return true;
512 }
513 
514 /// isSingleElementStruct - Determine if a structure is a "single
515 /// element struct", i.e. it has exactly one non-empty field or
516 /// exactly one field which is itself a single element
517 /// struct. Structures with flexible array members are never
518 /// considered single element structs.
519 ///
520 /// \return The field declaration for the single non-empty field, if
521 /// it exists.
522 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
523   const RecordType *RT = T->getAs<RecordType>();
524   if (!RT)
525     return nullptr;
526 
527   const RecordDecl *RD = RT->getDecl();
528   if (RD->hasFlexibleArrayMember())
529     return nullptr;
530 
531   const Type *Found = nullptr;
532 
533   // If this is a C++ record, check the bases first.
534   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
535     for (const auto &I : CXXRD->bases()) {
536       // Ignore empty records.
537       if (isEmptyRecord(Context, I.getType(), true))
538         continue;
539 
540       // If we already found an element then this isn't a single-element struct.
541       if (Found)
542         return nullptr;
543 
544       // If this is non-empty and not a single element struct, the composite
545       // cannot be a single element struct.
546       Found = isSingleElementStruct(I.getType(), Context);
547       if (!Found)
548         return nullptr;
549     }
550   }
551 
552   // Check for single element.
553   for (const auto *FD : RD->fields()) {
554     QualType FT = FD->getType();
555 
556     // Ignore empty fields.
557     if (isEmptyField(Context, FD, true))
558       continue;
559 
560     // If we already found an element then this isn't a single-element
561     // struct.
562     if (Found)
563       return nullptr;
564 
565     // Treat single element arrays as the element.
566     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
567       if (AT->getSize().getZExtValue() != 1)
568         break;
569       FT = AT->getElementType();
570     }
571 
572     if (!isAggregateTypeForABI(FT)) {
573       Found = FT.getTypePtr();
574     } else {
575       Found = isSingleElementStruct(FT, Context);
576       if (!Found)
577         return nullptr;
578     }
579   }
580 
581   // We don't consider a struct a single-element struct if it has
582   // padding beyond the element type.
583   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
584     return nullptr;
585 
586   return Found;
587 }
588 
589 namespace {
590 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
591                        const ABIArgInfo &AI) {
592   // This default implementation defers to the llvm backend's va_arg
593   // instruction. It can handle only passing arguments directly
594   // (typically only handled in the backend for primitive types), or
595   // aggregates passed indirectly by pointer (NOTE: if the "byval"
596   // flag has ABI impact in the callee, this implementation cannot
597   // work.)
598 
599   // Only a few cases are covered here at the moment -- those needed
600   // by the default abi.
601   llvm::Value *Val;
602 
603   if (AI.isIndirect()) {
604     assert(!AI.getPaddingType() &&
605            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
606     assert(
607         !AI.getIndirectRealign() &&
608         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
609 
610     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
611     CharUnits TyAlignForABI = TyInfo.second;
612 
613     llvm::Type *BaseTy =
614         llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
615     llvm::Value *Addr =
616         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
617     return Address(Addr, TyAlignForABI);
618   } else {
619     assert((AI.isDirect() || AI.isExtend()) &&
620            "Unexpected ArgInfo Kind in generic VAArg emitter!");
621 
622     assert(!AI.getInReg() &&
623            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
624     assert(!AI.getPaddingType() &&
625            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
626     assert(!AI.getDirectOffset() &&
627            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
628     assert(!AI.getCoerceToType() &&
629            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
630 
631     Address Temp = CGF.CreateMemTemp(Ty, "varet");
632     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
633     CGF.Builder.CreateStore(Val, Temp);
634     return Temp;
635   }
636 }
637 
638 /// DefaultABIInfo - The default implementation for ABI specific
639 /// details. This implementation provides information which results in
640 /// self-consistent and sensible LLVM IR generation, but does not
641 /// conform to any particular ABI.
642 class DefaultABIInfo : public ABIInfo {
643 public:
644   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
645 
646   ABIArgInfo classifyReturnType(QualType RetTy) const;
647   ABIArgInfo classifyArgumentType(QualType RetTy) const;
648 
649   void computeInfo(CGFunctionInfo &FI) const override {
650     if (!getCXXABI().classifyReturnType(FI))
651       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
652     for (auto &I : FI.arguments())
653       I.info = classifyArgumentType(I.type);
654   }
655 
656   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
657                     QualType Ty) const override {
658     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
659   }
660 };
661 
662 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
663 public:
664   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
665     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
666 };
667 
668 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
669   Ty = useFirstFieldIfTransparentUnion(Ty);
670 
671   if (isAggregateTypeForABI(Ty)) {
672     // Records with non-trivial destructors/copy-constructors should not be
673     // passed by value.
674     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
675       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
676 
677     return getNaturalAlignIndirect(Ty);
678   }
679 
680   // Treat an enum type as its underlying type.
681   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
682     Ty = EnumTy->getDecl()->getIntegerType();
683 
684   return (Ty->isPromotableIntegerType() ?
685           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
686 }
687 
688 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
689   if (RetTy->isVoidType())
690     return ABIArgInfo::getIgnore();
691 
692   if (isAggregateTypeForABI(RetTy))
693     return getNaturalAlignIndirect(RetTy);
694 
695   // Treat an enum type as its underlying type.
696   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
697     RetTy = EnumTy->getDecl()->getIntegerType();
698 
699   return (RetTy->isPromotableIntegerType() ?
700           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
701 }
702 
703 //===----------------------------------------------------------------------===//
704 // WebAssembly ABI Implementation
705 //
706 // This is a very simple ABI that relies a lot on DefaultABIInfo.
707 //===----------------------------------------------------------------------===//
708 
709 class WebAssemblyABIInfo final : public DefaultABIInfo {
710 public:
711   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
712       : DefaultABIInfo(CGT) {}
713 
714 private:
715   ABIArgInfo classifyReturnType(QualType RetTy) const;
716   ABIArgInfo classifyArgumentType(QualType Ty) const;
717 
718   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
719   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
720   // overload them.
721   void computeInfo(CGFunctionInfo &FI) const override {
722     if (!getCXXABI().classifyReturnType(FI))
723       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
724     for (auto &Arg : FI.arguments())
725       Arg.info = classifyArgumentType(Arg.type);
726   }
727 
728   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
729                     QualType Ty) const override;
730 };
731 
732 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
733 public:
734   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
735       : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
736 };
737 
738 /// \brief Classify argument of given type \p Ty.
739 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
740   Ty = useFirstFieldIfTransparentUnion(Ty);
741 
742   if (isAggregateTypeForABI(Ty)) {
743     // Records with non-trivial destructors/copy-constructors should not be
744     // passed by value.
745     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
746       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
747     // Ignore empty structs/unions.
748     if (isEmptyRecord(getContext(), Ty, true))
749       return ABIArgInfo::getIgnore();
750     // Lower single-element structs to just pass a regular value. TODO: We
751     // could do reasonable-size multiple-element structs too, using getExpand(),
752     // though watch out for things like bitfields.
753     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
754       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
755   }
756 
757   // Otherwise just do the default thing.
758   return DefaultABIInfo::classifyArgumentType(Ty);
759 }
760 
761 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
762   if (isAggregateTypeForABI(RetTy)) {
763     // Records with non-trivial destructors/copy-constructors should not be
764     // returned by value.
765     if (!getRecordArgABI(RetTy, getCXXABI())) {
766       // Ignore empty structs/unions.
767       if (isEmptyRecord(getContext(), RetTy, true))
768         return ABIArgInfo::getIgnore();
769       // Lower single-element structs to just return a regular value. TODO: We
770       // could do reasonable-size multiple-element structs too, using
771       // ABIArgInfo::getDirect().
772       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
773         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
774     }
775   }
776 
777   // Otherwise just do the default thing.
778   return DefaultABIInfo::classifyReturnType(RetTy);
779 }
780 
781 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
782                                       QualType Ty) const {
783   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
784                           getContext().getTypeInfoInChars(Ty),
785                           CharUnits::fromQuantity(4),
786                           /*AllowHigherAlign=*/ true);
787 }
788 
789 //===----------------------------------------------------------------------===//
790 // le32/PNaCl bitcode ABI Implementation
791 //
792 // This is a simplified version of the x86_32 ABI.  Arguments and return values
793 // are always passed on the stack.
794 //===----------------------------------------------------------------------===//
795 
796 class PNaClABIInfo : public ABIInfo {
797  public:
798   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
799 
800   ABIArgInfo classifyReturnType(QualType RetTy) const;
801   ABIArgInfo classifyArgumentType(QualType RetTy) const;
802 
803   void computeInfo(CGFunctionInfo &FI) const override;
804   Address EmitVAArg(CodeGenFunction &CGF,
805                     Address VAListAddr, QualType Ty) const override;
806 };
807 
808 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
809  public:
810   PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
811     : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
812 };
813 
814 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
815   if (!getCXXABI().classifyReturnType(FI))
816     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
817 
818   for (auto &I : FI.arguments())
819     I.info = classifyArgumentType(I.type);
820 }
821 
822 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
823                                 QualType Ty) const {
824   // The PNaCL ABI is a bit odd, in that varargs don't use normal
825   // function classification. Structs get passed directly for varargs
826   // functions, through a rewriting transform in
827   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
828   // this target to actually support a va_arg instructions with an
829   // aggregate type, unlike other targets.
830   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
831 }
832 
833 /// \brief Classify argument of given type \p Ty.
834 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
835   if (isAggregateTypeForABI(Ty)) {
836     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
837       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
838     return getNaturalAlignIndirect(Ty);
839   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
840     // Treat an enum type as its underlying type.
841     Ty = EnumTy->getDecl()->getIntegerType();
842   } else if (Ty->isFloatingType()) {
843     // Floating-point types don't go inreg.
844     return ABIArgInfo::getDirect();
845   }
846 
847   return (Ty->isPromotableIntegerType() ?
848           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
849 }
850 
851 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
852   if (RetTy->isVoidType())
853     return ABIArgInfo::getIgnore();
854 
855   // In the PNaCl ABI we always return records/structures on the stack.
856   if (isAggregateTypeForABI(RetTy))
857     return getNaturalAlignIndirect(RetTy);
858 
859   // Treat an enum type as its underlying type.
860   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
861     RetTy = EnumTy->getDecl()->getIntegerType();
862 
863   return (RetTy->isPromotableIntegerType() ?
864           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
865 }
866 
867 /// IsX86_MMXType - Return true if this is an MMX type.
868 bool IsX86_MMXType(llvm::Type *IRType) {
869   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
870   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
871     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
872     IRType->getScalarSizeInBits() != 64;
873 }
874 
875 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
876                                           StringRef Constraint,
877                                           llvm::Type* Ty) {
878   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
879                      .Cases("y", "&y", "^Ym", true)
880                      .Default(false);
881   if (IsMMXCons && Ty->isVectorTy()) {
882     if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
883       // Invalid MMX constraint
884       return nullptr;
885     }
886 
887     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
888   }
889 
890   // No operation needed
891   return Ty;
892 }
893 
894 /// Returns true if this type can be passed in SSE registers with the
895 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
896 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
897   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
898     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
899       if (BT->getKind() == BuiltinType::LongDouble) {
900         if (&Context.getTargetInfo().getLongDoubleFormat() ==
901             &llvm::APFloat::x87DoubleExtended())
902           return false;
903       }
904       return true;
905     }
906   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
907     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
908     // registers specially.
909     unsigned VecSize = Context.getTypeSize(VT);
910     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
911       return true;
912   }
913   return false;
914 }
915 
916 /// Returns true if this aggregate is small enough to be passed in SSE registers
917 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
918 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
919   return NumMembers <= 4;
920 }
921 
922 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
923 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
924   auto AI = ABIArgInfo::getDirect(T);
925   AI.setInReg(true);
926   AI.setCanBeFlattened(false);
927   return AI;
928 }
929 
930 //===----------------------------------------------------------------------===//
931 // X86-32 ABI Implementation
932 //===----------------------------------------------------------------------===//
933 
934 /// \brief Similar to llvm::CCState, but for Clang.
935 struct CCState {
936   CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
937 
938   unsigned CC;
939   unsigned FreeRegs;
940   unsigned FreeSSERegs;
941 };
942 
943 enum {
944   // Vectorcall only allows the first 6 parameters to be passed in registers.
945   VectorcallMaxParamNumAsReg = 6
946 };
947 
948 /// X86_32ABIInfo - The X86-32 ABI information.
949 class X86_32ABIInfo : public SwiftABIInfo {
950   enum Class {
951     Integer,
952     Float
953   };
954 
955   static const unsigned MinABIStackAlignInBytes = 4;
956 
957   bool IsDarwinVectorABI;
958   bool IsRetSmallStructInRegABI;
959   bool IsWin32StructABI;
960   bool IsSoftFloatABI;
961   bool IsMCUABI;
962   unsigned DefaultNumRegisterParameters;
963 
964   static bool isRegisterSize(unsigned Size) {
965     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
966   }
967 
968   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
969     // FIXME: Assumes vectorcall is in use.
970     return isX86VectorTypeForVectorCall(getContext(), Ty);
971   }
972 
973   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
974                                          uint64_t NumMembers) const override {
975     // FIXME: Assumes vectorcall is in use.
976     return isX86VectorCallAggregateSmallEnough(NumMembers);
977   }
978 
979   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
980 
981   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
982   /// such that the argument will be passed in memory.
983   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
984 
985   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
986 
987   /// \brief Return the alignment to use for the given type on the stack.
988   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
989 
990   Class classify(QualType Ty) const;
991   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
992   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
993 
994   /// \brief Updates the number of available free registers, returns
995   /// true if any registers were allocated.
996   bool updateFreeRegs(QualType Ty, CCState &State) const;
997 
998   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
999                                 bool &NeedsPadding) const;
1000   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1001 
1002   bool canExpandIndirectArgument(QualType Ty) const;
1003 
1004   /// \brief Rewrite the function info so that all memory arguments use
1005   /// inalloca.
1006   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1007 
1008   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1009                            CharUnits &StackOffset, ABIArgInfo &Info,
1010                            QualType Type) const;
1011   void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1012                              bool &UsedInAlloca) const;
1013 
1014 public:
1015 
1016   void computeInfo(CGFunctionInfo &FI) const override;
1017   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1018                     QualType Ty) const override;
1019 
1020   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1021                 bool RetSmallStructInRegABI, bool Win32StructABI,
1022                 unsigned NumRegisterParameters, bool SoftFloatABI)
1023     : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1024       IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1025       IsWin32StructABI(Win32StructABI),
1026       IsSoftFloatABI(SoftFloatABI),
1027       IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1028       DefaultNumRegisterParameters(NumRegisterParameters) {}
1029 
1030   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
1031                                     ArrayRef<llvm::Type*> scalars,
1032                                     bool asReturnValue) const override {
1033     // LLVM's x86-32 lowering currently only assigns up to three
1034     // integer registers and three fp registers.  Oddly, it'll use up to
1035     // four vector registers for vectors, but those can overlap with the
1036     // scalar registers.
1037     return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1038   }
1039 
1040   bool isSwiftErrorInRegister() const override {
1041     // x86-32 lowering does not support passing swifterror in a register.
1042     return false;
1043   }
1044 };
1045 
1046 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1047 public:
1048   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1049                           bool RetSmallStructInRegABI, bool Win32StructABI,
1050                           unsigned NumRegisterParameters, bool SoftFloatABI)
1051       : TargetCodeGenInfo(new X86_32ABIInfo(
1052             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1053             NumRegisterParameters, SoftFloatABI)) {}
1054 
1055   static bool isStructReturnInRegABI(
1056       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1057 
1058   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1059                            CodeGen::CodeGenModule &CGM,
1060                            ForDefinition_t IsForDefinition) const override;
1061 
1062   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1063     // Darwin uses different dwarf register numbers for EH.
1064     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1065     return 4;
1066   }
1067 
1068   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1069                                llvm::Value *Address) const override;
1070 
1071   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1072                                   StringRef Constraint,
1073                                   llvm::Type* Ty) const override {
1074     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1075   }
1076 
1077   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1078                                 std::string &Constraints,
1079                                 std::vector<llvm::Type *> &ResultRegTypes,
1080                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1081                                 std::vector<LValue> &ResultRegDests,
1082                                 std::string &AsmString,
1083                                 unsigned NumOutputs) const override;
1084 
1085   llvm::Constant *
1086   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1087     unsigned Sig = (0xeb << 0) |  // jmp rel8
1088                    (0x06 << 8) |  //           .+0x08
1089                    ('F' << 16) |
1090                    ('T' << 24);
1091     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1092   }
1093 
1094   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1095     return "movl\t%ebp, %ebp"
1096            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1097   }
1098 };
1099 
1100 }
1101 
1102 /// Rewrite input constraint references after adding some output constraints.
1103 /// In the case where there is one output and one input and we add one output,
1104 /// we need to replace all operand references greater than or equal to 1:
1105 ///     mov $0, $1
1106 ///     mov eax, $1
1107 /// The result will be:
1108 ///     mov $0, $2
1109 ///     mov eax, $2
1110 static void rewriteInputConstraintReferences(unsigned FirstIn,
1111                                              unsigned NumNewOuts,
1112                                              std::string &AsmString) {
1113   std::string Buf;
1114   llvm::raw_string_ostream OS(Buf);
1115   size_t Pos = 0;
1116   while (Pos < AsmString.size()) {
1117     size_t DollarStart = AsmString.find('$', Pos);
1118     if (DollarStart == std::string::npos)
1119       DollarStart = AsmString.size();
1120     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1121     if (DollarEnd == std::string::npos)
1122       DollarEnd = AsmString.size();
1123     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1124     Pos = DollarEnd;
1125     size_t NumDollars = DollarEnd - DollarStart;
1126     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1127       // We have an operand reference.
1128       size_t DigitStart = Pos;
1129       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1130       if (DigitEnd == std::string::npos)
1131         DigitEnd = AsmString.size();
1132       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1133       unsigned OperandIndex;
1134       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1135         if (OperandIndex >= FirstIn)
1136           OperandIndex += NumNewOuts;
1137         OS << OperandIndex;
1138       } else {
1139         OS << OperandStr;
1140       }
1141       Pos = DigitEnd;
1142     }
1143   }
1144   AsmString = std::move(OS.str());
1145 }
1146 
1147 /// Add output constraints for EAX:EDX because they are return registers.
1148 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1149     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1150     std::vector<llvm::Type *> &ResultRegTypes,
1151     std::vector<llvm::Type *> &ResultTruncRegTypes,
1152     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1153     unsigned NumOutputs) const {
1154   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1155 
1156   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1157   // larger.
1158   if (!Constraints.empty())
1159     Constraints += ',';
1160   if (RetWidth <= 32) {
1161     Constraints += "={eax}";
1162     ResultRegTypes.push_back(CGF.Int32Ty);
1163   } else {
1164     // Use the 'A' constraint for EAX:EDX.
1165     Constraints += "=A";
1166     ResultRegTypes.push_back(CGF.Int64Ty);
1167   }
1168 
1169   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1170   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1171   ResultTruncRegTypes.push_back(CoerceTy);
1172 
1173   // Coerce the integer by bitcasting the return slot pointer.
1174   ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1175                                                   CoerceTy->getPointerTo()));
1176   ResultRegDests.push_back(ReturnSlot);
1177 
1178   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1179 }
1180 
1181 /// shouldReturnTypeInRegister - Determine if the given type should be
1182 /// returned in a register (for the Darwin and MCU ABI).
1183 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1184                                                ASTContext &Context) const {
1185   uint64_t Size = Context.getTypeSize(Ty);
1186 
1187   // For i386, type must be register sized.
1188   // For the MCU ABI, it only needs to be <= 8-byte
1189   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1190    return false;
1191 
1192   if (Ty->isVectorType()) {
1193     // 64- and 128- bit vectors inside structures are not returned in
1194     // registers.
1195     if (Size == 64 || Size == 128)
1196       return false;
1197 
1198     return true;
1199   }
1200 
1201   // If this is a builtin, pointer, enum, complex type, member pointer, or
1202   // member function pointer it is ok.
1203   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1204       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1205       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1206     return true;
1207 
1208   // Arrays are treated like records.
1209   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1210     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1211 
1212   // Otherwise, it must be a record type.
1213   const RecordType *RT = Ty->getAs<RecordType>();
1214   if (!RT) return false;
1215 
1216   // FIXME: Traverse bases here too.
1217 
1218   // Structure types are passed in register if all fields would be
1219   // passed in a register.
1220   for (const auto *FD : RT->getDecl()->fields()) {
1221     // Empty fields are ignored.
1222     if (isEmptyField(Context, FD, true))
1223       continue;
1224 
1225     // Check fields recursively.
1226     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1227       return false;
1228   }
1229   return true;
1230 }
1231 
1232 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1233   // Treat complex types as the element type.
1234   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1235     Ty = CTy->getElementType();
1236 
1237   // Check for a type which we know has a simple scalar argument-passing
1238   // convention without any padding.  (We're specifically looking for 32
1239   // and 64-bit integer and integer-equivalents, float, and double.)
1240   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1241       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1242     return false;
1243 
1244   uint64_t Size = Context.getTypeSize(Ty);
1245   return Size == 32 || Size == 64;
1246 }
1247 
1248 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1249                           uint64_t &Size) {
1250   for (const auto *FD : RD->fields()) {
1251     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1252     // argument is smaller than 32-bits, expanding the struct will create
1253     // alignment padding.
1254     if (!is32Or64BitBasicType(FD->getType(), Context))
1255       return false;
1256 
1257     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1258     // how to expand them yet, and the predicate for telling if a bitfield still
1259     // counts as "basic" is more complicated than what we were doing previously.
1260     if (FD->isBitField())
1261       return false;
1262 
1263     Size += Context.getTypeSize(FD->getType());
1264   }
1265   return true;
1266 }
1267 
1268 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1269                                  uint64_t &Size) {
1270   // Don't do this if there are any non-empty bases.
1271   for (const CXXBaseSpecifier &Base : RD->bases()) {
1272     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1273                               Size))
1274       return false;
1275   }
1276   if (!addFieldSizes(Context, RD, Size))
1277     return false;
1278   return true;
1279 }
1280 
1281 /// Test whether an argument type which is to be passed indirectly (on the
1282 /// stack) would have the equivalent layout if it was expanded into separate
1283 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1284 /// optimizations.
1285 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1286   // We can only expand structure types.
1287   const RecordType *RT = Ty->getAs<RecordType>();
1288   if (!RT)
1289     return false;
1290   const RecordDecl *RD = RT->getDecl();
1291   uint64_t Size = 0;
1292   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1293     if (!IsWin32StructABI) {
1294       // On non-Windows, we have to conservatively match our old bitcode
1295       // prototypes in order to be ABI-compatible at the bitcode level.
1296       if (!CXXRD->isCLike())
1297         return false;
1298     } else {
1299       // Don't do this for dynamic classes.
1300       if (CXXRD->isDynamicClass())
1301         return false;
1302     }
1303     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1304       return false;
1305   } else {
1306     if (!addFieldSizes(getContext(), RD, Size))
1307       return false;
1308   }
1309 
1310   // We can do this if there was no alignment padding.
1311   return Size == getContext().getTypeSize(Ty);
1312 }
1313 
1314 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1315   // If the return value is indirect, then the hidden argument is consuming one
1316   // integer register.
1317   if (State.FreeRegs) {
1318     --State.FreeRegs;
1319     if (!IsMCUABI)
1320       return getNaturalAlignIndirectInReg(RetTy);
1321   }
1322   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1323 }
1324 
1325 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1326                                              CCState &State) const {
1327   if (RetTy->isVoidType())
1328     return ABIArgInfo::getIgnore();
1329 
1330   const Type *Base = nullptr;
1331   uint64_t NumElts = 0;
1332   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1333        State.CC == llvm::CallingConv::X86_RegCall) &&
1334       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1335     // The LLVM struct type for such an aggregate should lower properly.
1336     return ABIArgInfo::getDirect();
1337   }
1338 
1339   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1340     // On Darwin, some vectors are returned in registers.
1341     if (IsDarwinVectorABI) {
1342       uint64_t Size = getContext().getTypeSize(RetTy);
1343 
1344       // 128-bit vectors are a special case; they are returned in
1345       // registers and we need to make sure to pick a type the LLVM
1346       // backend will like.
1347       if (Size == 128)
1348         return ABIArgInfo::getDirect(llvm::VectorType::get(
1349                   llvm::Type::getInt64Ty(getVMContext()), 2));
1350 
1351       // Always return in register if it fits in a general purpose
1352       // register, or if it is 64 bits and has a single element.
1353       if ((Size == 8 || Size == 16 || Size == 32) ||
1354           (Size == 64 && VT->getNumElements() == 1))
1355         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1356                                                             Size));
1357 
1358       return getIndirectReturnResult(RetTy, State);
1359     }
1360 
1361     return ABIArgInfo::getDirect();
1362   }
1363 
1364   if (isAggregateTypeForABI(RetTy)) {
1365     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1366       // Structures with flexible arrays are always indirect.
1367       if (RT->getDecl()->hasFlexibleArrayMember())
1368         return getIndirectReturnResult(RetTy, State);
1369     }
1370 
1371     // If specified, structs and unions are always indirect.
1372     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1373       return getIndirectReturnResult(RetTy, State);
1374 
1375     // Ignore empty structs/unions.
1376     if (isEmptyRecord(getContext(), RetTy, true))
1377       return ABIArgInfo::getIgnore();
1378 
1379     // Small structures which are register sized are generally returned
1380     // in a register.
1381     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1382       uint64_t Size = getContext().getTypeSize(RetTy);
1383 
1384       // As a special-case, if the struct is a "single-element" struct, and
1385       // the field is of type "float" or "double", return it in a
1386       // floating-point register. (MSVC does not apply this special case.)
1387       // We apply a similar transformation for pointer types to improve the
1388       // quality of the generated IR.
1389       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1390         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1391             || SeltTy->hasPointerRepresentation())
1392           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1393 
1394       // FIXME: We should be able to narrow this integer in cases with dead
1395       // padding.
1396       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1397     }
1398 
1399     return getIndirectReturnResult(RetTy, State);
1400   }
1401 
1402   // Treat an enum type as its underlying type.
1403   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1404     RetTy = EnumTy->getDecl()->getIntegerType();
1405 
1406   return (RetTy->isPromotableIntegerType() ?
1407           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1408 }
1409 
1410 static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1411   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1412 }
1413 
1414 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1415   const RecordType *RT = Ty->getAs<RecordType>();
1416   if (!RT)
1417     return 0;
1418   const RecordDecl *RD = RT->getDecl();
1419 
1420   // If this is a C++ record, check the bases first.
1421   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1422     for (const auto &I : CXXRD->bases())
1423       if (!isRecordWithSSEVectorType(Context, I.getType()))
1424         return false;
1425 
1426   for (const auto *i : RD->fields()) {
1427     QualType FT = i->getType();
1428 
1429     if (isSSEVectorType(Context, FT))
1430       return true;
1431 
1432     if (isRecordWithSSEVectorType(Context, FT))
1433       return true;
1434   }
1435 
1436   return false;
1437 }
1438 
1439 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1440                                                  unsigned Align) const {
1441   // Otherwise, if the alignment is less than or equal to the minimum ABI
1442   // alignment, just use the default; the backend will handle this.
1443   if (Align <= MinABIStackAlignInBytes)
1444     return 0; // Use default alignment.
1445 
1446   // On non-Darwin, the stack type alignment is always 4.
1447   if (!IsDarwinVectorABI) {
1448     // Set explicit alignment, since we may need to realign the top.
1449     return MinABIStackAlignInBytes;
1450   }
1451 
1452   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1453   if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1454                       isRecordWithSSEVectorType(getContext(), Ty)))
1455     return 16;
1456 
1457   return MinABIStackAlignInBytes;
1458 }
1459 
1460 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1461                                             CCState &State) const {
1462   if (!ByVal) {
1463     if (State.FreeRegs) {
1464       --State.FreeRegs; // Non-byval indirects just use one pointer.
1465       if (!IsMCUABI)
1466         return getNaturalAlignIndirectInReg(Ty);
1467     }
1468     return getNaturalAlignIndirect(Ty, false);
1469   }
1470 
1471   // Compute the byval alignment.
1472   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1473   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1474   if (StackAlign == 0)
1475     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1476 
1477   // If the stack alignment is less than the type alignment, realign the
1478   // argument.
1479   bool Realign = TypeAlign > StackAlign;
1480   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1481                                  /*ByVal=*/true, Realign);
1482 }
1483 
1484 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1485   const Type *T = isSingleElementStruct(Ty, getContext());
1486   if (!T)
1487     T = Ty.getTypePtr();
1488 
1489   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1490     BuiltinType::Kind K = BT->getKind();
1491     if (K == BuiltinType::Float || K == BuiltinType::Double)
1492       return Float;
1493   }
1494   return Integer;
1495 }
1496 
1497 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1498   if (!IsSoftFloatABI) {
1499     Class C = classify(Ty);
1500     if (C == Float)
1501       return false;
1502   }
1503 
1504   unsigned Size = getContext().getTypeSize(Ty);
1505   unsigned SizeInRegs = (Size + 31) / 32;
1506 
1507   if (SizeInRegs == 0)
1508     return false;
1509 
1510   if (!IsMCUABI) {
1511     if (SizeInRegs > State.FreeRegs) {
1512       State.FreeRegs = 0;
1513       return false;
1514     }
1515   } else {
1516     // The MCU psABI allows passing parameters in-reg even if there are
1517     // earlier parameters that are passed on the stack. Also,
1518     // it does not allow passing >8-byte structs in-register,
1519     // even if there are 3 free registers available.
1520     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1521       return false;
1522   }
1523 
1524   State.FreeRegs -= SizeInRegs;
1525   return true;
1526 }
1527 
1528 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1529                                              bool &InReg,
1530                                              bool &NeedsPadding) const {
1531   // On Windows, aggregates other than HFAs are never passed in registers, and
1532   // they do not consume register slots. Homogenous floating-point aggregates
1533   // (HFAs) have already been dealt with at this point.
1534   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1535     return false;
1536 
1537   NeedsPadding = false;
1538   InReg = !IsMCUABI;
1539 
1540   if (!updateFreeRegs(Ty, State))
1541     return false;
1542 
1543   if (IsMCUABI)
1544     return true;
1545 
1546   if (State.CC == llvm::CallingConv::X86_FastCall ||
1547       State.CC == llvm::CallingConv::X86_VectorCall ||
1548       State.CC == llvm::CallingConv::X86_RegCall) {
1549     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1550       NeedsPadding = true;
1551 
1552     return false;
1553   }
1554 
1555   return true;
1556 }
1557 
1558 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1559   if (!updateFreeRegs(Ty, State))
1560     return false;
1561 
1562   if (IsMCUABI)
1563     return false;
1564 
1565   if (State.CC == llvm::CallingConv::X86_FastCall ||
1566       State.CC == llvm::CallingConv::X86_VectorCall ||
1567       State.CC == llvm::CallingConv::X86_RegCall) {
1568     if (getContext().getTypeSize(Ty) > 32)
1569       return false;
1570 
1571     return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1572         Ty->isReferenceType());
1573   }
1574 
1575   return true;
1576 }
1577 
1578 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1579                                                CCState &State) const {
1580   // FIXME: Set alignment on indirect arguments.
1581 
1582   Ty = useFirstFieldIfTransparentUnion(Ty);
1583 
1584   // Check with the C++ ABI first.
1585   const RecordType *RT = Ty->getAs<RecordType>();
1586   if (RT) {
1587     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1588     if (RAA == CGCXXABI::RAA_Indirect) {
1589       return getIndirectResult(Ty, false, State);
1590     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1591       // The field index doesn't matter, we'll fix it up later.
1592       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1593     }
1594   }
1595 
1596   // Regcall uses the concept of a homogenous vector aggregate, similar
1597   // to other targets.
1598   const Type *Base = nullptr;
1599   uint64_t NumElts = 0;
1600   if (State.CC == llvm::CallingConv::X86_RegCall &&
1601       isHomogeneousAggregate(Ty, Base, NumElts)) {
1602 
1603     if (State.FreeSSERegs >= NumElts) {
1604       State.FreeSSERegs -= NumElts;
1605       if (Ty->isBuiltinType() || Ty->isVectorType())
1606         return ABIArgInfo::getDirect();
1607       return ABIArgInfo::getExpand();
1608     }
1609     return getIndirectResult(Ty, /*ByVal=*/false, State);
1610   }
1611 
1612   if (isAggregateTypeForABI(Ty)) {
1613     // Structures with flexible arrays are always indirect.
1614     // FIXME: This should not be byval!
1615     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1616       return getIndirectResult(Ty, true, State);
1617 
1618     // Ignore empty structs/unions on non-Windows.
1619     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1620       return ABIArgInfo::getIgnore();
1621 
1622     llvm::LLVMContext &LLVMContext = getVMContext();
1623     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1624     bool NeedsPadding = false;
1625     bool InReg;
1626     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1627       unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
1628       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1629       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1630       if (InReg)
1631         return ABIArgInfo::getDirectInReg(Result);
1632       else
1633         return ABIArgInfo::getDirect(Result);
1634     }
1635     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1636 
1637     // Expand small (<= 128-bit) record types when we know that the stack layout
1638     // of those arguments will match the struct. This is important because the
1639     // LLVM backend isn't smart enough to remove byval, which inhibits many
1640     // optimizations.
1641     // Don't do this for the MCU if there are still free integer registers
1642     // (see X86_64 ABI for full explanation).
1643     if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1644         (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
1645       return ABIArgInfo::getExpandWithPadding(
1646           State.CC == llvm::CallingConv::X86_FastCall ||
1647               State.CC == llvm::CallingConv::X86_VectorCall ||
1648               State.CC == llvm::CallingConv::X86_RegCall,
1649           PaddingType);
1650 
1651     return getIndirectResult(Ty, true, State);
1652   }
1653 
1654   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1655     // On Darwin, some vectors are passed in memory, we handle this by passing
1656     // it as an i8/i16/i32/i64.
1657     if (IsDarwinVectorABI) {
1658       uint64_t Size = getContext().getTypeSize(Ty);
1659       if ((Size == 8 || Size == 16 || Size == 32) ||
1660           (Size == 64 && VT->getNumElements() == 1))
1661         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1662                                                             Size));
1663     }
1664 
1665     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1666       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1667 
1668     return ABIArgInfo::getDirect();
1669   }
1670 
1671 
1672   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1673     Ty = EnumTy->getDecl()->getIntegerType();
1674 
1675   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1676 
1677   if (Ty->isPromotableIntegerType()) {
1678     if (InReg)
1679       return ABIArgInfo::getExtendInReg();
1680     return ABIArgInfo::getExtend();
1681   }
1682 
1683   if (InReg)
1684     return ABIArgInfo::getDirectInReg();
1685   return ABIArgInfo::getDirect();
1686 }
1687 
1688 void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1689                                           bool &UsedInAlloca) const {
1690   // Vectorcall x86 works subtly different than in x64, so the format is
1691   // a bit different than the x64 version.  First, all vector types (not HVAs)
1692   // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
1693   // This differs from the x64 implementation, where the first 6 by INDEX get
1694   // registers.
1695   // After that, integers AND HVAs are assigned Left to Right in the same pass.
1696   // Integers are passed as ECX/EDX if one is available (in order).  HVAs will
1697   // first take up the remaining YMM/XMM registers. If insufficient registers
1698   // remain but an integer register (ECX/EDX) is available, it will be passed
1699   // in that, else, on the stack.
1700   for (auto &I : FI.arguments()) {
1701     // First pass do all the vector types.
1702     const Type *Base = nullptr;
1703     uint64_t NumElts = 0;
1704     const QualType& Ty = I.type;
1705     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1706         isHomogeneousAggregate(Ty, Base, NumElts)) {
1707       if (State.FreeSSERegs >= NumElts) {
1708         State.FreeSSERegs -= NumElts;
1709         I.info = ABIArgInfo::getDirect();
1710       } else {
1711         I.info = classifyArgumentType(Ty, State);
1712       }
1713       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1714     }
1715   }
1716 
1717   for (auto &I : FI.arguments()) {
1718     // Second pass, do the rest!
1719     const Type *Base = nullptr;
1720     uint64_t NumElts = 0;
1721     const QualType& Ty = I.type;
1722     bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
1723 
1724     if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
1725       // Assign true HVAs (non vector/native FP types).
1726       if (State.FreeSSERegs >= NumElts) {
1727         State.FreeSSERegs -= NumElts;
1728         I.info = getDirectX86Hva();
1729       } else {
1730         I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
1731       }
1732     } else if (!IsHva) {
1733       // Assign all Non-HVAs, so this will exclude Vector/FP args.
1734       I.info = classifyArgumentType(Ty, State);
1735       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1736     }
1737   }
1738 }
1739 
1740 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1741   CCState State(FI.getCallingConvention());
1742   if (IsMCUABI)
1743     State.FreeRegs = 3;
1744   else if (State.CC == llvm::CallingConv::X86_FastCall)
1745     State.FreeRegs = 2;
1746   else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1747     State.FreeRegs = 2;
1748     State.FreeSSERegs = 6;
1749   } else if (FI.getHasRegParm())
1750     State.FreeRegs = FI.getRegParm();
1751   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1752     State.FreeRegs = 5;
1753     State.FreeSSERegs = 8;
1754   } else
1755     State.FreeRegs = DefaultNumRegisterParameters;
1756 
1757   if (!getCXXABI().classifyReturnType(FI)) {
1758     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1759   } else if (FI.getReturnInfo().isIndirect()) {
1760     // The C++ ABI is not aware of register usage, so we have to check if the
1761     // return value was sret and put it in a register ourselves if appropriate.
1762     if (State.FreeRegs) {
1763       --State.FreeRegs;  // The sret parameter consumes a register.
1764       if (!IsMCUABI)
1765         FI.getReturnInfo().setInReg(true);
1766     }
1767   }
1768 
1769   // The chain argument effectively gives us another free register.
1770   if (FI.isChainCall())
1771     ++State.FreeRegs;
1772 
1773   bool UsedInAlloca = false;
1774   if (State.CC == llvm::CallingConv::X86_VectorCall) {
1775     computeVectorCallArgs(FI, State, UsedInAlloca);
1776   } else {
1777     // If not vectorcall, revert to normal behavior.
1778     for (auto &I : FI.arguments()) {
1779       I.info = classifyArgumentType(I.type, State);
1780       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1781     }
1782   }
1783 
1784   // If we needed to use inalloca for any argument, do a second pass and rewrite
1785   // all the memory arguments to use inalloca.
1786   if (UsedInAlloca)
1787     rewriteWithInAlloca(FI);
1788 }
1789 
1790 void
1791 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1792                                    CharUnits &StackOffset, ABIArgInfo &Info,
1793                                    QualType Type) const {
1794   // Arguments are always 4-byte-aligned.
1795   CharUnits FieldAlign = CharUnits::fromQuantity(4);
1796 
1797   assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
1798   Info = ABIArgInfo::getInAlloca(FrameFields.size());
1799   FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1800   StackOffset += getContext().getTypeSizeInChars(Type);
1801 
1802   // Insert padding bytes to respect alignment.
1803   CharUnits FieldEnd = StackOffset;
1804   StackOffset = FieldEnd.alignTo(FieldAlign);
1805   if (StackOffset != FieldEnd) {
1806     CharUnits NumBytes = StackOffset - FieldEnd;
1807     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1808     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
1809     FrameFields.push_back(Ty);
1810   }
1811 }
1812 
1813 static bool isArgInAlloca(const ABIArgInfo &Info) {
1814   // Leave ignored and inreg arguments alone.
1815   switch (Info.getKind()) {
1816   case ABIArgInfo::InAlloca:
1817     return true;
1818   case ABIArgInfo::Indirect:
1819     assert(Info.getIndirectByVal());
1820     return true;
1821   case ABIArgInfo::Ignore:
1822     return false;
1823   case ABIArgInfo::Direct:
1824   case ABIArgInfo::Extend:
1825     if (Info.getInReg())
1826       return false;
1827     return true;
1828   case ABIArgInfo::Expand:
1829   case ABIArgInfo::CoerceAndExpand:
1830     // These are aggregate types which are never passed in registers when
1831     // inalloca is involved.
1832     return true;
1833   }
1834   llvm_unreachable("invalid enum");
1835 }
1836 
1837 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1838   assert(IsWin32StructABI && "inalloca only supported on win32");
1839 
1840   // Build a packed struct type for all of the arguments in memory.
1841   SmallVector<llvm::Type *, 6> FrameFields;
1842 
1843   // The stack alignment is always 4.
1844   CharUnits StackAlign = CharUnits::fromQuantity(4);
1845 
1846   CharUnits StackOffset;
1847   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1848 
1849   // Put 'this' into the struct before 'sret', if necessary.
1850   bool IsThisCall =
1851       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1852   ABIArgInfo &Ret = FI.getReturnInfo();
1853   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1854       isArgInAlloca(I->info)) {
1855     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1856     ++I;
1857   }
1858 
1859   // Put the sret parameter into the inalloca struct if it's in memory.
1860   if (Ret.isIndirect() && !Ret.getInReg()) {
1861     CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1862     addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1863     // On Windows, the hidden sret parameter is always returned in eax.
1864     Ret.setInAllocaSRet(IsWin32StructABI);
1865   }
1866 
1867   // Skip the 'this' parameter in ecx.
1868   if (IsThisCall)
1869     ++I;
1870 
1871   // Put arguments passed in memory into the struct.
1872   for (; I != E; ++I) {
1873     if (isArgInAlloca(I->info))
1874       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1875   }
1876 
1877   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1878                                         /*isPacked=*/true),
1879                   StackAlign);
1880 }
1881 
1882 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1883                                  Address VAListAddr, QualType Ty) const {
1884 
1885   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
1886 
1887   // x86-32 changes the alignment of certain arguments on the stack.
1888   //
1889   // Just messing with TypeInfo like this works because we never pass
1890   // anything indirectly.
1891   TypeInfo.second = CharUnits::fromQuantity(
1892                 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
1893 
1894   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1895                           TypeInfo, CharUnits::fromQuantity(4),
1896                           /*AllowHigherAlign*/ true);
1897 }
1898 
1899 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1900     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1901   assert(Triple.getArch() == llvm::Triple::x86);
1902 
1903   switch (Opts.getStructReturnConvention()) {
1904   case CodeGenOptions::SRCK_Default:
1905     break;
1906   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
1907     return false;
1908   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
1909     return true;
1910   }
1911 
1912   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
1913     return true;
1914 
1915   switch (Triple.getOS()) {
1916   case llvm::Triple::DragonFly:
1917   case llvm::Triple::FreeBSD:
1918   case llvm::Triple::OpenBSD:
1919   case llvm::Triple::Win32:
1920     return true;
1921   default:
1922     return false;
1923   }
1924 }
1925 
1926 void X86_32TargetCodeGenInfo::setTargetAttributes(
1927     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
1928     ForDefinition_t IsForDefinition) const {
1929   if (!IsForDefinition)
1930     return;
1931   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1932     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1933       // Get the LLVM function.
1934       llvm::Function *Fn = cast<llvm::Function>(GV);
1935 
1936       // Now add the 'alignstack' attribute with a value of 16.
1937       llvm::AttrBuilder B;
1938       B.addStackAlignmentAttr(16);
1939       Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
1940     }
1941     if (FD->hasAttr<AnyX86InterruptAttr>()) {
1942       llvm::Function *Fn = cast<llvm::Function>(GV);
1943       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1944     }
1945   }
1946 }
1947 
1948 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1949                                                CodeGen::CodeGenFunction &CGF,
1950                                                llvm::Value *Address) const {
1951   CodeGen::CGBuilderTy &Builder = CGF.Builder;
1952 
1953   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
1954 
1955   // 0-7 are the eight integer registers;  the order is different
1956   //   on Darwin (for EH), but the range is the same.
1957   // 8 is %eip.
1958   AssignToArrayRange(Builder, Address, Four8, 0, 8);
1959 
1960   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
1961     // 12-16 are st(0..4).  Not sure why we stop at 4.
1962     // These have size 16, which is sizeof(long double) on
1963     // platforms with 8-byte alignment for that type.
1964     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
1965     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
1966 
1967   } else {
1968     // 9 is %eflags, which doesn't get a size on Darwin for some
1969     // reason.
1970     Builder.CreateAlignedStore(
1971         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1972                                CharUnits::One());
1973 
1974     // 11-16 are st(0..5).  Not sure why we stop at 5.
1975     // These have size 12, which is sizeof(long double) on
1976     // platforms with 4-byte alignment for that type.
1977     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1978     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1979   }
1980 
1981   return false;
1982 }
1983 
1984 //===----------------------------------------------------------------------===//
1985 // X86-64 ABI Implementation
1986 //===----------------------------------------------------------------------===//
1987 
1988 
1989 namespace {
1990 /// The AVX ABI level for X86 targets.
1991 enum class X86AVXABILevel {
1992   None,
1993   AVX,
1994   AVX512
1995 };
1996 
1997 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1998 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1999   switch (AVXLevel) {
2000   case X86AVXABILevel::AVX512:
2001     return 512;
2002   case X86AVXABILevel::AVX:
2003     return 256;
2004   case X86AVXABILevel::None:
2005     return 128;
2006   }
2007   llvm_unreachable("Unknown AVXLevel");
2008 }
2009 
2010 /// X86_64ABIInfo - The X86_64 ABI information.
2011 class X86_64ABIInfo : public SwiftABIInfo {
2012   enum Class {
2013     Integer = 0,
2014     SSE,
2015     SSEUp,
2016     X87,
2017     X87Up,
2018     ComplexX87,
2019     NoClass,
2020     Memory
2021   };
2022 
2023   /// merge - Implement the X86_64 ABI merging algorithm.
2024   ///
2025   /// Merge an accumulating classification \arg Accum with a field
2026   /// classification \arg Field.
2027   ///
2028   /// \param Accum - The accumulating classification. This should
2029   /// always be either NoClass or the result of a previous merge
2030   /// call. In addition, this should never be Memory (the caller
2031   /// should just return Memory for the aggregate).
2032   static Class merge(Class Accum, Class Field);
2033 
2034   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2035   ///
2036   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2037   /// final MEMORY or SSE classes when necessary.
2038   ///
2039   /// \param AggregateSize - The size of the current aggregate in
2040   /// the classification process.
2041   ///
2042   /// \param Lo - The classification for the parts of the type
2043   /// residing in the low word of the containing object.
2044   ///
2045   /// \param Hi - The classification for the parts of the type
2046   /// residing in the higher words of the containing object.
2047   ///
2048   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2049 
2050   /// classify - Determine the x86_64 register classes in which the
2051   /// given type T should be passed.
2052   ///
2053   /// \param Lo - The classification for the parts of the type
2054   /// residing in the low word of the containing object.
2055   ///
2056   /// \param Hi - The classification for the parts of the type
2057   /// residing in the high word of the containing object.
2058   ///
2059   /// \param OffsetBase - The bit offset of this type in the
2060   /// containing object.  Some parameters are classified different
2061   /// depending on whether they straddle an eightbyte boundary.
2062   ///
2063   /// \param isNamedArg - Whether the argument in question is a "named"
2064   /// argument, as used in AMD64-ABI 3.5.7.
2065   ///
2066   /// If a word is unused its result will be NoClass; if a type should
2067   /// be passed in Memory then at least the classification of \arg Lo
2068   /// will be Memory.
2069   ///
2070   /// The \arg Lo class will be NoClass iff the argument is ignored.
2071   ///
2072   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2073   /// also be ComplexX87.
2074   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2075                 bool isNamedArg) const;
2076 
2077   llvm::Type *GetByteVectorType(QualType Ty) const;
2078   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2079                                  unsigned IROffset, QualType SourceTy,
2080                                  unsigned SourceOffset) const;
2081   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2082                                      unsigned IROffset, QualType SourceTy,
2083                                      unsigned SourceOffset) const;
2084 
2085   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2086   /// such that the argument will be returned in memory.
2087   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2088 
2089   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2090   /// such that the argument will be passed in memory.
2091   ///
2092   /// \param freeIntRegs - The number of free integer registers remaining
2093   /// available.
2094   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2095 
2096   ABIArgInfo classifyReturnType(QualType RetTy) const;
2097 
2098   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2099                                   unsigned &neededInt, unsigned &neededSSE,
2100                                   bool isNamedArg) const;
2101 
2102   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2103                                        unsigned &NeededSSE) const;
2104 
2105   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2106                                            unsigned &NeededSSE) const;
2107 
2108   bool IsIllegalVectorType(QualType Ty) const;
2109 
2110   /// The 0.98 ABI revision clarified a lot of ambiguities,
2111   /// unfortunately in ways that were not always consistent with
2112   /// certain previous compilers.  In particular, platforms which
2113   /// required strict binary compatibility with older versions of GCC
2114   /// may need to exempt themselves.
2115   bool honorsRevision0_98() const {
2116     return !getTarget().getTriple().isOSDarwin();
2117   }
2118 
2119   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2120   /// classify it as INTEGER (for compatibility with older clang compilers).
2121   bool classifyIntegerMMXAsSSE() const {
2122     // Clang <= 3.8 did not do this.
2123     if (getCodeGenOpts().getClangABICompat() <=
2124         CodeGenOptions::ClangABI::Ver3_8)
2125       return false;
2126 
2127     const llvm::Triple &Triple = getTarget().getTriple();
2128     if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2129       return false;
2130     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2131       return false;
2132     return true;
2133   }
2134 
2135   X86AVXABILevel AVXLevel;
2136   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2137   // 64-bit hardware.
2138   bool Has64BitPointers;
2139 
2140 public:
2141   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
2142       SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2143       Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
2144   }
2145 
2146   bool isPassedUsingAVXType(QualType type) const {
2147     unsigned neededInt, neededSSE;
2148     // The freeIntRegs argument doesn't matter here.
2149     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2150                                            /*isNamedArg*/true);
2151     if (info.isDirect()) {
2152       llvm::Type *ty = info.getCoerceToType();
2153       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2154         return (vectorTy->getBitWidth() > 128);
2155     }
2156     return false;
2157   }
2158 
2159   void computeInfo(CGFunctionInfo &FI) const override;
2160 
2161   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2162                     QualType Ty) const override;
2163   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2164                       QualType Ty) const override;
2165 
2166   bool has64BitPointers() const {
2167     return Has64BitPointers;
2168   }
2169 
2170   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2171                                     ArrayRef<llvm::Type*> scalars,
2172                                     bool asReturnValue) const override {
2173     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2174   }
2175   bool isSwiftErrorInRegister() const override {
2176     return true;
2177   }
2178 };
2179 
2180 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2181 class WinX86_64ABIInfo : public SwiftABIInfo {
2182 public:
2183   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
2184       : SwiftABIInfo(CGT),
2185         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2186 
2187   void computeInfo(CGFunctionInfo &FI) const override;
2188 
2189   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2190                     QualType Ty) const override;
2191 
2192   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2193     // FIXME: Assumes vectorcall is in use.
2194     return isX86VectorTypeForVectorCall(getContext(), Ty);
2195   }
2196 
2197   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2198                                          uint64_t NumMembers) const override {
2199     // FIXME: Assumes vectorcall is in use.
2200     return isX86VectorCallAggregateSmallEnough(NumMembers);
2201   }
2202 
2203   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2204                                     ArrayRef<llvm::Type *> scalars,
2205                                     bool asReturnValue) const override {
2206     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2207   }
2208 
2209   bool isSwiftErrorInRegister() const override {
2210     return true;
2211   }
2212 
2213 private:
2214   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2215                       bool IsVectorCall, bool IsRegCall) const;
2216   ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2217                                       const ABIArgInfo &current) const;
2218   void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2219                              bool IsVectorCall, bool IsRegCall) const;
2220 
2221     bool IsMingw64;
2222 };
2223 
2224 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2225 public:
2226   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2227       : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
2228 
2229   const X86_64ABIInfo &getABIInfo() const {
2230     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2231   }
2232 
2233   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2234     return 7;
2235   }
2236 
2237   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2238                                llvm::Value *Address) const override {
2239     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2240 
2241     // 0-15 are the 16 integer registers.
2242     // 16 is %rip.
2243     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2244     return false;
2245   }
2246 
2247   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2248                                   StringRef Constraint,
2249                                   llvm::Type* Ty) const override {
2250     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2251   }
2252 
2253   bool isNoProtoCallVariadic(const CallArgList &args,
2254                              const FunctionNoProtoType *fnType) const override {
2255     // The default CC on x86-64 sets %al to the number of SSA
2256     // registers used, and GCC sets this when calling an unprototyped
2257     // function, so we override the default behavior.  However, don't do
2258     // that when AVX types are involved: the ABI explicitly states it is
2259     // undefined, and it doesn't work in practice because of how the ABI
2260     // defines varargs anyway.
2261     if (fnType->getCallConv() == CC_C) {
2262       bool HasAVXType = false;
2263       for (CallArgList::const_iterator
2264              it = args.begin(), ie = args.end(); it != ie; ++it) {
2265         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2266           HasAVXType = true;
2267           break;
2268         }
2269       }
2270 
2271       if (!HasAVXType)
2272         return true;
2273     }
2274 
2275     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2276   }
2277 
2278   llvm::Constant *
2279   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2280     unsigned Sig;
2281     if (getABIInfo().has64BitPointers())
2282       Sig = (0xeb << 0) |  // jmp rel8
2283             (0x0a << 8) |  //           .+0x0c
2284             ('F' << 16) |
2285             ('T' << 24);
2286     else
2287       Sig = (0xeb << 0) |  // jmp rel8
2288             (0x06 << 8) |  //           .+0x08
2289             ('F' << 16) |
2290             ('T' << 24);
2291     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2292   }
2293 
2294   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2295                            CodeGen::CodeGenModule &CGM,
2296                            ForDefinition_t IsForDefinition) const override {
2297     if (!IsForDefinition)
2298       return;
2299     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2300       if (FD->hasAttr<AnyX86InterruptAttr>()) {
2301         llvm::Function *Fn = cast<llvm::Function>(GV);
2302         Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2303       }
2304     }
2305   }
2306 };
2307 
2308 class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2309 public:
2310   PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2311     : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
2312 
2313   void getDependentLibraryOption(llvm::StringRef Lib,
2314                                  llvm::SmallString<24> &Opt) const override {
2315     Opt = "\01";
2316     // If the argument contains a space, enclose it in quotes.
2317     if (Lib.find(" ") != StringRef::npos)
2318       Opt += "\"" + Lib.str() + "\"";
2319     else
2320       Opt += Lib;
2321   }
2322 };
2323 
2324 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2325   // If the argument does not end in .lib, automatically add the suffix.
2326   // If the argument contains a space, enclose it in quotes.
2327   // This matches the behavior of MSVC.
2328   bool Quote = (Lib.find(" ") != StringRef::npos);
2329   std::string ArgStr = Quote ? "\"" : "";
2330   ArgStr += Lib;
2331   if (!Lib.endswith_lower(".lib"))
2332     ArgStr += ".lib";
2333   ArgStr += Quote ? "\"" : "";
2334   return ArgStr;
2335 }
2336 
2337 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2338 public:
2339   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2340         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2341         unsigned NumRegisterParameters)
2342     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2343         Win32StructABI, NumRegisterParameters, false) {}
2344 
2345   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2346                            CodeGen::CodeGenModule &CGM,
2347                            ForDefinition_t IsForDefinition) const override;
2348 
2349   void getDependentLibraryOption(llvm::StringRef Lib,
2350                                  llvm::SmallString<24> &Opt) const override {
2351     Opt = "/DEFAULTLIB:";
2352     Opt += qualifyWindowsLibrary(Lib);
2353   }
2354 
2355   void getDetectMismatchOption(llvm::StringRef Name,
2356                                llvm::StringRef Value,
2357                                llvm::SmallString<32> &Opt) const override {
2358     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2359   }
2360 };
2361 
2362 static void addStackProbeSizeTargetAttribute(const Decl *D,
2363                                              llvm::GlobalValue *GV,
2364                                              CodeGen::CodeGenModule &CGM) {
2365   if (D && isa<FunctionDecl>(D)) {
2366     if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2367       llvm::Function *Fn = cast<llvm::Function>(GV);
2368 
2369       Fn->addFnAttr("stack-probe-size",
2370                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2371     }
2372   }
2373 }
2374 
2375 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2376     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
2377     ForDefinition_t IsForDefinition) const {
2378   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
2379   if (!IsForDefinition)
2380     return;
2381   addStackProbeSizeTargetAttribute(D, GV, CGM);
2382 }
2383 
2384 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2385 public:
2386   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2387                              X86AVXABILevel AVXLevel)
2388       : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
2389 
2390   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2391                            CodeGen::CodeGenModule &CGM,
2392                            ForDefinition_t IsForDefinition) const override;
2393 
2394   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2395     return 7;
2396   }
2397 
2398   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2399                                llvm::Value *Address) const override {
2400     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2401 
2402     // 0-15 are the 16 integer registers.
2403     // 16 is %rip.
2404     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2405     return false;
2406   }
2407 
2408   void getDependentLibraryOption(llvm::StringRef Lib,
2409                                  llvm::SmallString<24> &Opt) const override {
2410     Opt = "/DEFAULTLIB:";
2411     Opt += qualifyWindowsLibrary(Lib);
2412   }
2413 
2414   void getDetectMismatchOption(llvm::StringRef Name,
2415                                llvm::StringRef Value,
2416                                llvm::SmallString<32> &Opt) const override {
2417     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2418   }
2419 };
2420 
2421 void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2422     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
2423     ForDefinition_t IsForDefinition) const {
2424   TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
2425   if (!IsForDefinition)
2426     return;
2427   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2428     if (FD->hasAttr<AnyX86InterruptAttr>()) {
2429       llvm::Function *Fn = cast<llvm::Function>(GV);
2430       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2431     }
2432   }
2433 
2434   addStackProbeSizeTargetAttribute(D, GV, CGM);
2435 }
2436 }
2437 
2438 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2439                               Class &Hi) const {
2440   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2441   //
2442   // (a) If one of the classes is Memory, the whole argument is passed in
2443   //     memory.
2444   //
2445   // (b) If X87UP is not preceded by X87, the whole argument is passed in
2446   //     memory.
2447   //
2448   // (c) If the size of the aggregate exceeds two eightbytes and the first
2449   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2450   //     argument is passed in memory. NOTE: This is necessary to keep the
2451   //     ABI working for processors that don't support the __m256 type.
2452   //
2453   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2454   //
2455   // Some of these are enforced by the merging logic.  Others can arise
2456   // only with unions; for example:
2457   //   union { _Complex double; unsigned; }
2458   //
2459   // Note that clauses (b) and (c) were added in 0.98.
2460   //
2461   if (Hi == Memory)
2462     Lo = Memory;
2463   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2464     Lo = Memory;
2465   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2466     Lo = Memory;
2467   if (Hi == SSEUp && Lo != SSE)
2468     Hi = SSE;
2469 }
2470 
2471 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2472   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2473   // classified recursively so that always two fields are
2474   // considered. The resulting class is calculated according to
2475   // the classes of the fields in the eightbyte:
2476   //
2477   // (a) If both classes are equal, this is the resulting class.
2478   //
2479   // (b) If one of the classes is NO_CLASS, the resulting class is
2480   // the other class.
2481   //
2482   // (c) If one of the classes is MEMORY, the result is the MEMORY
2483   // class.
2484   //
2485   // (d) If one of the classes is INTEGER, the result is the
2486   // INTEGER.
2487   //
2488   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2489   // MEMORY is used as class.
2490   //
2491   // (f) Otherwise class SSE is used.
2492 
2493   // Accum should never be memory (we should have returned) or
2494   // ComplexX87 (because this cannot be passed in a structure).
2495   assert((Accum != Memory && Accum != ComplexX87) &&
2496          "Invalid accumulated classification during merge.");
2497   if (Accum == Field || Field == NoClass)
2498     return Accum;
2499   if (Field == Memory)
2500     return Memory;
2501   if (Accum == NoClass)
2502     return Field;
2503   if (Accum == Integer || Field == Integer)
2504     return Integer;
2505   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2506       Accum == X87 || Accum == X87Up)
2507     return Memory;
2508   return SSE;
2509 }
2510 
2511 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
2512                              Class &Lo, Class &Hi, bool isNamedArg) const {
2513   // FIXME: This code can be simplified by introducing a simple value class for
2514   // Class pairs with appropriate constructor methods for the various
2515   // situations.
2516 
2517   // FIXME: Some of the split computations are wrong; unaligned vectors
2518   // shouldn't be passed in registers for example, so there is no chance they
2519   // can straddle an eightbyte. Verify & simplify.
2520 
2521   Lo = Hi = NoClass;
2522 
2523   Class &Current = OffsetBase < 64 ? Lo : Hi;
2524   Current = Memory;
2525 
2526   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2527     BuiltinType::Kind k = BT->getKind();
2528 
2529     if (k == BuiltinType::Void) {
2530       Current = NoClass;
2531     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2532       Lo = Integer;
2533       Hi = Integer;
2534     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2535       Current = Integer;
2536     } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
2537       Current = SSE;
2538     } else if (k == BuiltinType::LongDouble) {
2539       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2540       if (LDF == &llvm::APFloat::IEEEquad()) {
2541         Lo = SSE;
2542         Hi = SSEUp;
2543       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2544         Lo = X87;
2545         Hi = X87Up;
2546       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2547         Current = SSE;
2548       } else
2549         llvm_unreachable("unexpected long double representation!");
2550     }
2551     // FIXME: _Decimal32 and _Decimal64 are SSE.
2552     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2553     return;
2554   }
2555 
2556   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2557     // Classify the underlying integer type.
2558     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2559     return;
2560   }
2561 
2562   if (Ty->hasPointerRepresentation()) {
2563     Current = Integer;
2564     return;
2565   }
2566 
2567   if (Ty->isMemberPointerType()) {
2568     if (Ty->isMemberFunctionPointerType()) {
2569       if (Has64BitPointers) {
2570         // If Has64BitPointers, this is an {i64, i64}, so classify both
2571         // Lo and Hi now.
2572         Lo = Hi = Integer;
2573       } else {
2574         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2575         // straddles an eightbyte boundary, Hi should be classified as well.
2576         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2577         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2578         if (EB_FuncPtr != EB_ThisAdj) {
2579           Lo = Hi = Integer;
2580         } else {
2581           Current = Integer;
2582         }
2583       }
2584     } else {
2585       Current = Integer;
2586     }
2587     return;
2588   }
2589 
2590   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2591     uint64_t Size = getContext().getTypeSize(VT);
2592     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2593       // gcc passes the following as integer:
2594       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2595       // 2 bytes - <2 x char>, <1 x short>
2596       // 1 byte  - <1 x char>
2597       Current = Integer;
2598 
2599       // If this type crosses an eightbyte boundary, it should be
2600       // split.
2601       uint64_t EB_Lo = (OffsetBase) / 64;
2602       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2603       if (EB_Lo != EB_Hi)
2604         Hi = Lo;
2605     } else if (Size == 64) {
2606       QualType ElementType = VT->getElementType();
2607 
2608       // gcc passes <1 x double> in memory. :(
2609       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2610         return;
2611 
2612       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2613       // pass them as integer.  For platforms where clang is the de facto
2614       // platform compiler, we must continue to use integer.
2615       if (!classifyIntegerMMXAsSSE() &&
2616           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2617            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2618            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2619            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2620         Current = Integer;
2621       else
2622         Current = SSE;
2623 
2624       // If this type crosses an eightbyte boundary, it should be
2625       // split.
2626       if (OffsetBase && OffsetBase != 64)
2627         Hi = Lo;
2628     } else if (Size == 128 ||
2629                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2630       // Arguments of 256-bits are split into four eightbyte chunks. The
2631       // least significant one belongs to class SSE and all the others to class
2632       // SSEUP. The original Lo and Hi design considers that types can't be
2633       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2634       // This design isn't correct for 256-bits, but since there're no cases
2635       // where the upper parts would need to be inspected, avoid adding
2636       // complexity and just consider Hi to match the 64-256 part.
2637       //
2638       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2639       // registers if they are "named", i.e. not part of the "..." of a
2640       // variadic function.
2641       //
2642       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2643       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2644       Lo = SSE;
2645       Hi = SSEUp;
2646     }
2647     return;
2648   }
2649 
2650   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2651     QualType ET = getContext().getCanonicalType(CT->getElementType());
2652 
2653     uint64_t Size = getContext().getTypeSize(Ty);
2654     if (ET->isIntegralOrEnumerationType()) {
2655       if (Size <= 64)
2656         Current = Integer;
2657       else if (Size <= 128)
2658         Lo = Hi = Integer;
2659     } else if (ET == getContext().FloatTy) {
2660       Current = SSE;
2661     } else if (ET == getContext().DoubleTy) {
2662       Lo = Hi = SSE;
2663     } else if (ET == getContext().LongDoubleTy) {
2664       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2665       if (LDF == &llvm::APFloat::IEEEquad())
2666         Current = Memory;
2667       else if (LDF == &llvm::APFloat::x87DoubleExtended())
2668         Current = ComplexX87;
2669       else if (LDF == &llvm::APFloat::IEEEdouble())
2670         Lo = Hi = SSE;
2671       else
2672         llvm_unreachable("unexpected long double representation!");
2673     }
2674 
2675     // If this complex type crosses an eightbyte boundary then it
2676     // should be split.
2677     uint64_t EB_Real = (OffsetBase) / 64;
2678     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2679     if (Hi == NoClass && EB_Real != EB_Imag)
2680       Hi = Lo;
2681 
2682     return;
2683   }
2684 
2685   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2686     // Arrays are treated like structures.
2687 
2688     uint64_t Size = getContext().getTypeSize(Ty);
2689 
2690     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2691     // than eight eightbytes, ..., it has class MEMORY.
2692     if (Size > 512)
2693       return;
2694 
2695     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2696     // fields, it has class MEMORY.
2697     //
2698     // Only need to check alignment of array base.
2699     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2700       return;
2701 
2702     // Otherwise implement simplified merge. We could be smarter about
2703     // this, but it isn't worth it and would be harder to verify.
2704     Current = NoClass;
2705     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
2706     uint64_t ArraySize = AT->getSize().getZExtValue();
2707 
2708     // The only case a 256-bit wide vector could be used is when the array
2709     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2710     // to work for sizes wider than 128, early check and fallback to memory.
2711     //
2712     if (Size > 128 &&
2713         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
2714       return;
2715 
2716     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2717       Class FieldLo, FieldHi;
2718       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
2719       Lo = merge(Lo, FieldLo);
2720       Hi = merge(Hi, FieldHi);
2721       if (Lo == Memory || Hi == Memory)
2722         break;
2723     }
2724 
2725     postMerge(Size, Lo, Hi);
2726     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
2727     return;
2728   }
2729 
2730   if (const RecordType *RT = Ty->getAs<RecordType>()) {
2731     uint64_t Size = getContext().getTypeSize(Ty);
2732 
2733     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2734     // than eight eightbytes, ..., it has class MEMORY.
2735     if (Size > 512)
2736       return;
2737 
2738     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2739     // copy constructor or a non-trivial destructor, it is passed by invisible
2740     // reference.
2741     if (getRecordArgABI(RT, getCXXABI()))
2742       return;
2743 
2744     const RecordDecl *RD = RT->getDecl();
2745 
2746     // Assume variable sized types are passed in memory.
2747     if (RD->hasFlexibleArrayMember())
2748       return;
2749 
2750     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2751 
2752     // Reset Lo class, this will be recomputed.
2753     Current = NoClass;
2754 
2755     // If this is a C++ record, classify the bases first.
2756     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2757       for (const auto &I : CXXRD->bases()) {
2758         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2759                "Unexpected base class!");
2760         const CXXRecordDecl *Base =
2761           cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2762 
2763         // Classify this field.
2764         //
2765         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2766         // single eightbyte, each is classified separately. Each eightbyte gets
2767         // initialized to class NO_CLASS.
2768         Class FieldLo, FieldHi;
2769         uint64_t Offset =
2770           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
2771         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
2772         Lo = merge(Lo, FieldLo);
2773         Hi = merge(Hi, FieldHi);
2774         if (Lo == Memory || Hi == Memory) {
2775           postMerge(Size, Lo, Hi);
2776           return;
2777         }
2778       }
2779     }
2780 
2781     // Classify the fields one at a time, merging the results.
2782     unsigned idx = 0;
2783     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2784            i != e; ++i, ++idx) {
2785       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2786       bool BitField = i->isBitField();
2787 
2788       // Ignore padding bit-fields.
2789       if (BitField && i->isUnnamedBitfield())
2790         continue;
2791 
2792       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2793       // four eightbytes, or it contains unaligned fields, it has class MEMORY.
2794       //
2795       // The only case a 256-bit wide vector could be used is when the struct
2796       // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2797       // to work for sizes wider than 128, early check and fallback to memory.
2798       //
2799       if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
2800                          Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
2801         Lo = Memory;
2802         postMerge(Size, Lo, Hi);
2803         return;
2804       }
2805       // Note, skip this test for bit-fields, see below.
2806       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
2807         Lo = Memory;
2808         postMerge(Size, Lo, Hi);
2809         return;
2810       }
2811 
2812       // Classify this field.
2813       //
2814       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2815       // exceeds a single eightbyte, each is classified
2816       // separately. Each eightbyte gets initialized to class
2817       // NO_CLASS.
2818       Class FieldLo, FieldHi;
2819 
2820       // Bit-fields require special handling, they do not force the
2821       // structure to be passed in memory even if unaligned, and
2822       // therefore they can straddle an eightbyte.
2823       if (BitField) {
2824         assert(!i->isUnnamedBitfield());
2825         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2826         uint64_t Size = i->getBitWidthValue(getContext());
2827 
2828         uint64_t EB_Lo = Offset / 64;
2829         uint64_t EB_Hi = (Offset + Size - 1) / 64;
2830 
2831         if (EB_Lo) {
2832           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2833           FieldLo = NoClass;
2834           FieldHi = Integer;
2835         } else {
2836           FieldLo = Integer;
2837           FieldHi = EB_Hi ? Integer : NoClass;
2838         }
2839       } else
2840         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
2841       Lo = merge(Lo, FieldLo);
2842       Hi = merge(Hi, FieldHi);
2843       if (Lo == Memory || Hi == Memory)
2844         break;
2845     }
2846 
2847     postMerge(Size, Lo, Hi);
2848   }
2849 }
2850 
2851 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
2852   // If this is a scalar LLVM value then assume LLVM will pass it in the right
2853   // place naturally.
2854   if (!isAggregateTypeForABI(Ty)) {
2855     // Treat an enum type as its underlying type.
2856     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2857       Ty = EnumTy->getDecl()->getIntegerType();
2858 
2859     return (Ty->isPromotableIntegerType() ?
2860             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2861   }
2862 
2863   return getNaturalAlignIndirect(Ty);
2864 }
2865 
2866 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2867   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2868     uint64_t Size = getContext().getTypeSize(VecTy);
2869     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
2870     if (Size <= 64 || Size > LargestVector)
2871       return true;
2872   }
2873 
2874   return false;
2875 }
2876 
2877 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2878                                             unsigned freeIntRegs) const {
2879   // If this is a scalar LLVM value then assume LLVM will pass it in the right
2880   // place naturally.
2881   //
2882   // This assumption is optimistic, as there could be free registers available
2883   // when we need to pass this argument in memory, and LLVM could try to pass
2884   // the argument in the free register. This does not seem to happen currently,
2885   // but this code would be much safer if we could mark the argument with
2886   // 'onstack'. See PR12193.
2887   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
2888     // Treat an enum type as its underlying type.
2889     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2890       Ty = EnumTy->getDecl()->getIntegerType();
2891 
2892     return (Ty->isPromotableIntegerType() ?
2893             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2894   }
2895 
2896   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
2897     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
2898 
2899   // Compute the byval alignment. We specify the alignment of the byval in all
2900   // cases so that the mid-level optimizer knows the alignment of the byval.
2901   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
2902 
2903   // Attempt to avoid passing indirect results using byval when possible. This
2904   // is important for good codegen.
2905   //
2906   // We do this by coercing the value into a scalar type which the backend can
2907   // handle naturally (i.e., without using byval).
2908   //
2909   // For simplicity, we currently only do this when we have exhausted all of the
2910   // free integer registers. Doing this when there are free integer registers
2911   // would require more care, as we would have to ensure that the coerced value
2912   // did not claim the unused register. That would require either reording the
2913   // arguments to the function (so that any subsequent inreg values came first),
2914   // or only doing this optimization when there were no following arguments that
2915   // might be inreg.
2916   //
2917   // We currently expect it to be rare (particularly in well written code) for
2918   // arguments to be passed on the stack when there are still free integer
2919   // registers available (this would typically imply large structs being passed
2920   // by value), so this seems like a fair tradeoff for now.
2921   //
2922   // We can revisit this if the backend grows support for 'onstack' parameter
2923   // attributes. See PR12193.
2924   if (freeIntRegs == 0) {
2925     uint64_t Size = getContext().getTypeSize(Ty);
2926 
2927     // If this type fits in an eightbyte, coerce it into the matching integral
2928     // type, which will end up on the stack (with alignment 8).
2929     if (Align == 8 && Size <= 64)
2930       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2931                                                           Size));
2932   }
2933 
2934   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
2935 }
2936 
2937 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
2938 /// register. Pick an LLVM IR type that will be passed as a vector register.
2939 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
2940   // Wrapper structs/arrays that only contain vectors are passed just like
2941   // vectors; strip them off if present.
2942   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2943     Ty = QualType(InnerTy, 0);
2944 
2945   llvm::Type *IRType = CGT.ConvertType(Ty);
2946   if (isa<llvm::VectorType>(IRType) ||
2947       IRType->getTypeID() == llvm::Type::FP128TyID)
2948     return IRType;
2949 
2950   // We couldn't find the preferred IR vector type for 'Ty'.
2951   uint64_t Size = getContext().getTypeSize(Ty);
2952   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
2953 
2954   // Return a LLVM IR vector type based on the size of 'Ty'.
2955   return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2956                                Size / 64);
2957 }
2958 
2959 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
2960 /// is known to either be off the end of the specified type or being in
2961 /// alignment padding.  The user type specified is known to be at most 128 bits
2962 /// in size, and have passed through X86_64ABIInfo::classify with a successful
2963 /// classification that put one of the two halves in the INTEGER class.
2964 ///
2965 /// It is conservatively correct to return false.
2966 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2967                                   unsigned EndBit, ASTContext &Context) {
2968   // If the bytes being queried are off the end of the type, there is no user
2969   // data hiding here.  This handles analysis of builtins, vectors and other
2970   // types that don't contain interesting padding.
2971   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2972   if (TySize <= StartBit)
2973     return true;
2974 
2975   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2976     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2977     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2978 
2979     // Check each element to see if the element overlaps with the queried range.
2980     for (unsigned i = 0; i != NumElts; ++i) {
2981       // If the element is after the span we care about, then we're done..
2982       unsigned EltOffset = i*EltSize;
2983       if (EltOffset >= EndBit) break;
2984 
2985       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2986       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2987                                  EndBit-EltOffset, Context))
2988         return false;
2989     }
2990     // If it overlaps no elements, then it is safe to process as padding.
2991     return true;
2992   }
2993 
2994   if (const RecordType *RT = Ty->getAs<RecordType>()) {
2995     const RecordDecl *RD = RT->getDecl();
2996     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2997 
2998     // If this is a C++ record, check the bases first.
2999     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3000       for (const auto &I : CXXRD->bases()) {
3001         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3002                "Unexpected base class!");
3003         const CXXRecordDecl *Base =
3004           cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
3005 
3006         // If the base is after the span we care about, ignore it.
3007         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3008         if (BaseOffset >= EndBit) continue;
3009 
3010         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3011         if (!BitsContainNoUserData(I.getType(), BaseStart,
3012                                    EndBit-BaseOffset, Context))
3013           return false;
3014       }
3015     }
3016 
3017     // Verify that no field has data that overlaps the region of interest.  Yes
3018     // this could be sped up a lot by being smarter about queried fields,
3019     // however we're only looking at structs up to 16 bytes, so we don't care
3020     // much.
3021     unsigned idx = 0;
3022     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3023          i != e; ++i, ++idx) {
3024       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3025 
3026       // If we found a field after the region we care about, then we're done.
3027       if (FieldOffset >= EndBit) break;
3028 
3029       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3030       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3031                                  Context))
3032         return false;
3033     }
3034 
3035     // If nothing in this record overlapped the area of interest, then we're
3036     // clean.
3037     return true;
3038   }
3039 
3040   return false;
3041 }
3042 
3043 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3044 /// float member at the specified offset.  For example, {int,{float}} has a
3045 /// float at offset 4.  It is conservatively correct for this routine to return
3046 /// false.
3047 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
3048                                   const llvm::DataLayout &TD) {
3049   // Base case if we find a float.
3050   if (IROffset == 0 && IRType->isFloatTy())
3051     return true;
3052 
3053   // If this is a struct, recurse into the field at the specified offset.
3054   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3055     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3056     unsigned Elt = SL->getElementContainingOffset(IROffset);
3057     IROffset -= SL->getElementOffset(Elt);
3058     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3059   }
3060 
3061   // If this is an array, recurse into the field at the specified offset.
3062   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3063     llvm::Type *EltTy = ATy->getElementType();
3064     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3065     IROffset -= IROffset/EltSize*EltSize;
3066     return ContainsFloatAtOffset(EltTy, IROffset, TD);
3067   }
3068 
3069   return false;
3070 }
3071 
3072 
3073 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3074 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3075 llvm::Type *X86_64ABIInfo::
3076 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3077                    QualType SourceTy, unsigned SourceOffset) const {
3078   // The only three choices we have are either double, <2 x float>, or float. We
3079   // pass as float if the last 4 bytes is just padding.  This happens for
3080   // structs that contain 3 floats.
3081   if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3082                             SourceOffset*8+64, getContext()))
3083     return llvm::Type::getFloatTy(getVMContext());
3084 
3085   // We want to pass as <2 x float> if the LLVM IR type contains a float at
3086   // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
3087   // case.
3088   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3089       ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
3090     return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
3091 
3092   return llvm::Type::getDoubleTy(getVMContext());
3093 }
3094 
3095 
3096 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3097 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3098 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3099 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3100 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3101 /// etc).
3102 ///
3103 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3104 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3105 /// the 8-byte value references.  PrefType may be null.
3106 ///
3107 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3108 /// an offset into this that we're processing (which is always either 0 or 8).
3109 ///
3110 llvm::Type *X86_64ABIInfo::
3111 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3112                        QualType SourceTy, unsigned SourceOffset) const {
3113   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3114   // returning an 8-byte unit starting with it.  See if we can safely use it.
3115   if (IROffset == 0) {
3116     // Pointers and int64's always fill the 8-byte unit.
3117     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3118         IRType->isIntegerTy(64))
3119       return IRType;
3120 
3121     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3122     // goodness in the source type is just tail padding.  This is allowed to
3123     // kick in for struct {double,int} on the int, but not on
3124     // struct{double,int,int} because we wouldn't return the second int.  We
3125     // have to do this analysis on the source type because we can't depend on
3126     // unions being lowered a specific way etc.
3127     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3128         IRType->isIntegerTy(32) ||
3129         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3130       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3131           cast<llvm::IntegerType>(IRType)->getBitWidth();
3132 
3133       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3134                                 SourceOffset*8+64, getContext()))
3135         return IRType;
3136     }
3137   }
3138 
3139   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3140     // If this is a struct, recurse into the field at the specified offset.
3141     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3142     if (IROffset < SL->getSizeInBytes()) {
3143       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3144       IROffset -= SL->getElementOffset(FieldIdx);
3145 
3146       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3147                                     SourceTy, SourceOffset);
3148     }
3149   }
3150 
3151   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3152     llvm::Type *EltTy = ATy->getElementType();
3153     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3154     unsigned EltOffset = IROffset/EltSize*EltSize;
3155     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3156                                   SourceOffset);
3157   }
3158 
3159   // Okay, we don't have any better idea of what to pass, so we pass this in an
3160   // integer register that isn't too big to fit the rest of the struct.
3161   unsigned TySizeInBytes =
3162     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3163 
3164   assert(TySizeInBytes != SourceOffset && "Empty field?");
3165 
3166   // It is always safe to classify this as an integer type up to i64 that
3167   // isn't larger than the structure.
3168   return llvm::IntegerType::get(getVMContext(),
3169                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3170 }
3171 
3172 
3173 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3174 /// be used as elements of a two register pair to pass or return, return a
3175 /// first class aggregate to represent them.  For example, if the low part of
3176 /// a by-value argument should be passed as i32* and the high part as float,
3177 /// return {i32*, float}.
3178 static llvm::Type *
3179 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3180                            const llvm::DataLayout &TD) {
3181   // In order to correctly satisfy the ABI, we need to the high part to start
3182   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3183   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3184   // the second element at offset 8.  Check for this:
3185   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3186   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3187   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3188   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3189 
3190   // To handle this, we have to increase the size of the low part so that the
3191   // second element will start at an 8 byte offset.  We can't increase the size
3192   // of the second element because it might make us access off the end of the
3193   // struct.
3194   if (HiStart != 8) {
3195     // There are usually two sorts of types the ABI generation code can produce
3196     // for the low part of a pair that aren't 8 bytes in size: float or
3197     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3198     // NaCl).
3199     // Promote these to a larger type.
3200     if (Lo->isFloatTy())
3201       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3202     else {
3203       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3204              && "Invalid/unknown lo type");
3205       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3206     }
3207   }
3208 
3209   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3210 
3211   // Verify that the second element is at an 8-byte offset.
3212   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3213          "Invalid x86-64 argument pair!");
3214   return Result;
3215 }
3216 
3217 ABIArgInfo X86_64ABIInfo::
3218 classifyReturnType(QualType RetTy) const {
3219   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3220   // classification algorithm.
3221   X86_64ABIInfo::Class Lo, Hi;
3222   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3223 
3224   // Check some invariants.
3225   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3226   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3227 
3228   llvm::Type *ResType = nullptr;
3229   switch (Lo) {
3230   case NoClass:
3231     if (Hi == NoClass)
3232       return ABIArgInfo::getIgnore();
3233     // If the low part is just padding, it takes no register, leave ResType
3234     // null.
3235     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3236            "Unknown missing lo part");
3237     break;
3238 
3239   case SSEUp:
3240   case X87Up:
3241     llvm_unreachable("Invalid classification for lo word.");
3242 
3243     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3244     // hidden argument.
3245   case Memory:
3246     return getIndirectReturnResult(RetTy);
3247 
3248     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3249     // available register of the sequence %rax, %rdx is used.
3250   case Integer:
3251     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3252 
3253     // If we have a sign or zero extended integer, make sure to return Extend
3254     // so that the parameter gets the right LLVM IR attributes.
3255     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3256       // Treat an enum type as its underlying type.
3257       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3258         RetTy = EnumTy->getDecl()->getIntegerType();
3259 
3260       if (RetTy->isIntegralOrEnumerationType() &&
3261           RetTy->isPromotableIntegerType())
3262         return ABIArgInfo::getExtend();
3263     }
3264     break;
3265 
3266     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3267     // available SSE register of the sequence %xmm0, %xmm1 is used.
3268   case SSE:
3269     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3270     break;
3271 
3272     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3273     // returned on the X87 stack in %st0 as 80-bit x87 number.
3274   case X87:
3275     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3276     break;
3277 
3278     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3279     // part of the value is returned in %st0 and the imaginary part in
3280     // %st1.
3281   case ComplexX87:
3282     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3283     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3284                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3285     break;
3286   }
3287 
3288   llvm::Type *HighPart = nullptr;
3289   switch (Hi) {
3290     // Memory was handled previously and X87 should
3291     // never occur as a hi class.
3292   case Memory:
3293   case X87:
3294     llvm_unreachable("Invalid classification for hi word.");
3295 
3296   case ComplexX87: // Previously handled.
3297   case NoClass:
3298     break;
3299 
3300   case Integer:
3301     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3302     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3303       return ABIArgInfo::getDirect(HighPart, 8);
3304     break;
3305   case SSE:
3306     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3307     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3308       return ABIArgInfo::getDirect(HighPart, 8);
3309     break;
3310 
3311     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3312     // is passed in the next available eightbyte chunk if the last used
3313     // vector register.
3314     //
3315     // SSEUP should always be preceded by SSE, just widen.
3316   case SSEUp:
3317     assert(Lo == SSE && "Unexpected SSEUp classification.");
3318     ResType = GetByteVectorType(RetTy);
3319     break;
3320 
3321     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3322     // returned together with the previous X87 value in %st0.
3323   case X87Up:
3324     // If X87Up is preceded by X87, we don't need to do
3325     // anything. However, in some cases with unions it may not be
3326     // preceded by X87. In such situations we follow gcc and pass the
3327     // extra bits in an SSE reg.
3328     if (Lo != X87) {
3329       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3330       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3331         return ABIArgInfo::getDirect(HighPart, 8);
3332     }
3333     break;
3334   }
3335 
3336   // If a high part was specified, merge it together with the low part.  It is
3337   // known to pass in the high eightbyte of the result.  We do this by forming a
3338   // first class struct aggregate with the high and low part: {low, high}
3339   if (HighPart)
3340     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3341 
3342   return ABIArgInfo::getDirect(ResType);
3343 }
3344 
3345 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
3346   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3347   bool isNamedArg)
3348   const
3349 {
3350   Ty = useFirstFieldIfTransparentUnion(Ty);
3351 
3352   X86_64ABIInfo::Class Lo, Hi;
3353   classify(Ty, 0, Lo, Hi, isNamedArg);
3354 
3355   // Check some invariants.
3356   // FIXME: Enforce these by construction.
3357   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3358   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3359 
3360   neededInt = 0;
3361   neededSSE = 0;
3362   llvm::Type *ResType = nullptr;
3363   switch (Lo) {
3364   case NoClass:
3365     if (Hi == NoClass)
3366       return ABIArgInfo::getIgnore();
3367     // If the low part is just padding, it takes no register, leave ResType
3368     // null.
3369     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3370            "Unknown missing lo part");
3371     break;
3372 
3373     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3374     // on the stack.
3375   case Memory:
3376 
3377     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3378     // COMPLEX_X87, it is passed in memory.
3379   case X87:
3380   case ComplexX87:
3381     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3382       ++neededInt;
3383     return getIndirectResult(Ty, freeIntRegs);
3384 
3385   case SSEUp:
3386   case X87Up:
3387     llvm_unreachable("Invalid classification for lo word.");
3388 
3389     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3390     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3391     // and %r9 is used.
3392   case Integer:
3393     ++neededInt;
3394 
3395     // Pick an 8-byte type based on the preferred type.
3396     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3397 
3398     // If we have a sign or zero extended integer, make sure to return Extend
3399     // so that the parameter gets the right LLVM IR attributes.
3400     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3401       // Treat an enum type as its underlying type.
3402       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3403         Ty = EnumTy->getDecl()->getIntegerType();
3404 
3405       if (Ty->isIntegralOrEnumerationType() &&
3406           Ty->isPromotableIntegerType())
3407         return ABIArgInfo::getExtend();
3408     }
3409 
3410     break;
3411 
3412     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3413     // available SSE register is used, the registers are taken in the
3414     // order from %xmm0 to %xmm7.
3415   case SSE: {
3416     llvm::Type *IRType = CGT.ConvertType(Ty);
3417     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3418     ++neededSSE;
3419     break;
3420   }
3421   }
3422 
3423   llvm::Type *HighPart = nullptr;
3424   switch (Hi) {
3425     // Memory was handled previously, ComplexX87 and X87 should
3426     // never occur as hi classes, and X87Up must be preceded by X87,
3427     // which is passed in memory.
3428   case Memory:
3429   case X87:
3430   case ComplexX87:
3431     llvm_unreachable("Invalid classification for hi word.");
3432 
3433   case NoClass: break;
3434 
3435   case Integer:
3436     ++neededInt;
3437     // Pick an 8-byte type based on the preferred type.
3438     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3439 
3440     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3441       return ABIArgInfo::getDirect(HighPart, 8);
3442     break;
3443 
3444     // X87Up generally doesn't occur here (long double is passed in
3445     // memory), except in situations involving unions.
3446   case X87Up:
3447   case SSE:
3448     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3449 
3450     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3451       return ABIArgInfo::getDirect(HighPart, 8);
3452 
3453     ++neededSSE;
3454     break;
3455 
3456     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3457     // eightbyte is passed in the upper half of the last used SSE
3458     // register.  This only happens when 128-bit vectors are passed.
3459   case SSEUp:
3460     assert(Lo == SSE && "Unexpected SSEUp classification");
3461     ResType = GetByteVectorType(Ty);
3462     break;
3463   }
3464 
3465   // If a high part was specified, merge it together with the low part.  It is
3466   // known to pass in the high eightbyte of the result.  We do this by forming a
3467   // first class struct aggregate with the high and low part: {low, high}
3468   if (HighPart)
3469     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3470 
3471   return ABIArgInfo::getDirect(ResType);
3472 }
3473 
3474 ABIArgInfo
3475 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3476                                              unsigned &NeededSSE) const {
3477   auto RT = Ty->getAs<RecordType>();
3478   assert(RT && "classifyRegCallStructType only valid with struct types");
3479 
3480   if (RT->getDecl()->hasFlexibleArrayMember())
3481     return getIndirectReturnResult(Ty);
3482 
3483   // Sum up bases
3484   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3485     if (CXXRD->isDynamicClass()) {
3486       NeededInt = NeededSSE = 0;
3487       return getIndirectReturnResult(Ty);
3488     }
3489 
3490     for (const auto &I : CXXRD->bases())
3491       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3492               .isIndirect()) {
3493         NeededInt = NeededSSE = 0;
3494         return getIndirectReturnResult(Ty);
3495       }
3496   }
3497 
3498   // Sum up members
3499   for (const auto *FD : RT->getDecl()->fields()) {
3500     if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3501       if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3502               .isIndirect()) {
3503         NeededInt = NeededSSE = 0;
3504         return getIndirectReturnResult(Ty);
3505       }
3506     } else {
3507       unsigned LocalNeededInt, LocalNeededSSE;
3508       if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3509                                LocalNeededSSE, true)
3510               .isIndirect()) {
3511         NeededInt = NeededSSE = 0;
3512         return getIndirectReturnResult(Ty);
3513       }
3514       NeededInt += LocalNeededInt;
3515       NeededSSE += LocalNeededSSE;
3516     }
3517   }
3518 
3519   return ABIArgInfo::getDirect();
3520 }
3521 
3522 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3523                                                     unsigned &NeededInt,
3524                                                     unsigned &NeededSSE) const {
3525 
3526   NeededInt = 0;
3527   NeededSSE = 0;
3528 
3529   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3530 }
3531 
3532 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3533 
3534   bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
3535 
3536   // Keep track of the number of assigned registers.
3537   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3538   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3539   unsigned NeededInt, NeededSSE;
3540 
3541   if (!getCXXABI().classifyReturnType(FI)) {
3542     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3543         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3544       FI.getReturnInfo() =
3545           classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3546       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3547         FreeIntRegs -= NeededInt;
3548         FreeSSERegs -= NeededSSE;
3549       } else {
3550         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3551       }
3552     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
3553       // Complex Long Double Type is passed in Memory when Regcall
3554       // calling convention is used.
3555       const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
3556       if (getContext().getCanonicalType(CT->getElementType()) ==
3557           getContext().LongDoubleTy)
3558         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3559     } else
3560       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3561   }
3562 
3563   // If the return value is indirect, then the hidden argument is consuming one
3564   // integer register.
3565   if (FI.getReturnInfo().isIndirect())
3566     --FreeIntRegs;
3567 
3568   // The chain argument effectively gives us another free register.
3569   if (FI.isChainCall())
3570     ++FreeIntRegs;
3571 
3572   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3573   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3574   // get assigned (in left-to-right order) for passing as follows...
3575   unsigned ArgNo = 0;
3576   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3577        it != ie; ++it, ++ArgNo) {
3578     bool IsNamedArg = ArgNo < NumRequiredArgs;
3579 
3580     if (IsRegCall && it->type->isStructureOrClassType())
3581       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3582     else
3583       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3584                                       NeededSSE, IsNamedArg);
3585 
3586     // AMD64-ABI 3.2.3p3: If there are no registers available for any
3587     // eightbyte of an argument, the whole argument is passed on the
3588     // stack. If registers have already been assigned for some
3589     // eightbytes of such an argument, the assignments get reverted.
3590     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3591       FreeIntRegs -= NeededInt;
3592       FreeSSERegs -= NeededSSE;
3593     } else {
3594       it->info = getIndirectResult(it->type, FreeIntRegs);
3595     }
3596   }
3597 }
3598 
3599 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3600                                          Address VAListAddr, QualType Ty) {
3601   Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3602       VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
3603   llvm::Value *overflow_arg_area =
3604     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3605 
3606   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3607   // byte boundary if alignment needed by type exceeds 8 byte boundary.
3608   // It isn't stated explicitly in the standard, but in practice we use
3609   // alignment greater than 16 where necessary.
3610   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3611   if (Align > CharUnits::fromQuantity(8)) {
3612     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3613                                                       Align);
3614   }
3615 
3616   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
3617   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3618   llvm::Value *Res =
3619     CGF.Builder.CreateBitCast(overflow_arg_area,
3620                               llvm::PointerType::getUnqual(LTy));
3621 
3622   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3623   // l->overflow_arg_area + sizeof(type).
3624   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3625   // an 8 byte boundary.
3626 
3627   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
3628   llvm::Value *Offset =
3629       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
3630   overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3631                                             "overflow_arg_area.next");
3632   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3633 
3634   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
3635   return Address(Res, Align);
3636 }
3637 
3638 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3639                                  QualType Ty) const {
3640   // Assume that va_list type is correct; should be pointer to LLVM type:
3641   // struct {
3642   //   i32 gp_offset;
3643   //   i32 fp_offset;
3644   //   i8* overflow_arg_area;
3645   //   i8* reg_save_area;
3646   // };
3647   unsigned neededInt, neededSSE;
3648 
3649   Ty = getContext().getCanonicalType(Ty);
3650   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
3651                                        /*isNamedArg*/false);
3652 
3653   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3654   // in the registers. If not go to step 7.
3655   if (!neededInt && !neededSSE)
3656     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3657 
3658   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3659   // general purpose registers needed to pass type and num_fp to hold
3660   // the number of floating point registers needed.
3661 
3662   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3663   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3664   // l->fp_offset > 304 - num_fp * 16 go to step 7.
3665   //
3666   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3667   // register save space).
3668 
3669   llvm::Value *InRegs = nullptr;
3670   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3671   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
3672   if (neededInt) {
3673     gp_offset_p =
3674         CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3675                                     "gp_offset_p");
3676     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
3677     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3678     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
3679   }
3680 
3681   if (neededSSE) {
3682     fp_offset_p =
3683         CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3684                                     "fp_offset_p");
3685     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3686     llvm::Value *FitsInFP =
3687       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3688     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
3689     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3690   }
3691 
3692   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3693   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3694   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3695   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3696 
3697   // Emit code to load the value if it was passed in registers.
3698 
3699   CGF.EmitBlock(InRegBlock);
3700 
3701   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3702   // an offset of l->gp_offset and/or l->fp_offset. This may require
3703   // copying to a temporary location in case the parameter is passed
3704   // in different register classes or requires an alignment greater
3705   // than 8 for general purpose registers and 16 for XMM registers.
3706   //
3707   // FIXME: This really results in shameful code when we end up needing to
3708   // collect arguments from different places; often what should result in a
3709   // simple assembling of a structure from scattered addresses has many more
3710   // loads than necessary. Can we clean this up?
3711   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3712   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3713       CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3714                                   "reg_save_area");
3715 
3716   Address RegAddr = Address::invalid();
3717   if (neededInt && neededSSE) {
3718     // FIXME: Cleanup.
3719     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
3720     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
3721     Address Tmp = CGF.CreateMemTemp(Ty);
3722     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3723     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
3724     llvm::Type *TyLo = ST->getElementType(0);
3725     llvm::Type *TyHi = ST->getElementType(1);
3726     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
3727            "Unexpected ABI info for mixed regs");
3728     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3729     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
3730     llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3731     llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
3732     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3733     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
3734 
3735     // Copy the first element.
3736     // FIXME: Our choice of alignment here and below is probably pessimistic.
3737     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
3738         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
3739         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
3740     CGF.Builder.CreateStore(V,
3741                     CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3742 
3743     // Copy the second element.
3744     V = CGF.Builder.CreateAlignedLoad(
3745         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
3746         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
3747     CharUnits Offset = CharUnits::fromQuantity(
3748                    getDataLayout().getStructLayout(ST)->getElementOffset(1));
3749     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3750 
3751     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3752   } else if (neededInt) {
3753     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3754                       CharUnits::fromQuantity(8));
3755     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3756 
3757     // Copy to a temporary if necessary to ensure the appropriate alignment.
3758     std::pair<CharUnits, CharUnits> SizeAlign =
3759         getContext().getTypeInfoInChars(Ty);
3760     uint64_t TySize = SizeAlign.first.getQuantity();
3761     CharUnits TyAlign = SizeAlign.second;
3762 
3763     // Copy into a temporary if the type is more aligned than the
3764     // register save area.
3765     if (TyAlign.getQuantity() > 8) {
3766       Address Tmp = CGF.CreateMemTemp(Ty);
3767       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
3768       RegAddr = Tmp;
3769     }
3770 
3771   } else if (neededSSE == 1) {
3772     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3773                       CharUnits::fromQuantity(16));
3774     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3775   } else {
3776     assert(neededSSE == 2 && "Invalid number of needed registers!");
3777     // SSE registers are spaced 16 bytes apart in the register save
3778     // area, we need to collect the two eightbytes together.
3779     // The ABI isn't explicit about this, but it seems reasonable
3780     // to assume that the slots are 16-byte aligned, since the stack is
3781     // naturally 16-byte aligned and the prologue is expected to store
3782     // all the SSE registers to the RSA.
3783     Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3784                                 CharUnits::fromQuantity(16));
3785     Address RegAddrHi =
3786       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3787                                              CharUnits::fromQuantity(16));
3788     llvm::Type *DoubleTy = CGF.DoubleTy;
3789     llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy);
3790     llvm::Value *V;
3791     Address Tmp = CGF.CreateMemTemp(Ty);
3792     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3793     V = CGF.Builder.CreateLoad(
3794                    CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3795     CGF.Builder.CreateStore(V,
3796                    CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3797     V = CGF.Builder.CreateLoad(
3798                    CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3799     CGF.Builder.CreateStore(V,
3800           CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3801 
3802     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3803   }
3804 
3805   // AMD64-ABI 3.5.7p5: Step 5. Set:
3806   // l->gp_offset = l->gp_offset + num_gp * 8
3807   // l->fp_offset = l->fp_offset + num_fp * 16.
3808   if (neededInt) {
3809     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
3810     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3811                             gp_offset_p);
3812   }
3813   if (neededSSE) {
3814     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
3815     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3816                             fp_offset_p);
3817   }
3818   CGF.EmitBranch(ContBlock);
3819 
3820   // Emit code to load the value if it was passed in memory.
3821 
3822   CGF.EmitBlock(InMemBlock);
3823   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3824 
3825   // Return the appropriate result.
3826 
3827   CGF.EmitBlock(ContBlock);
3828   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3829                                  "vaarg.addr");
3830   return ResAddr;
3831 }
3832 
3833 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3834                                    QualType Ty) const {
3835   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3836                           CGF.getContext().getTypeInfoInChars(Ty),
3837                           CharUnits::fromQuantity(8),
3838                           /*allowHigherAlign*/ false);
3839 }
3840 
3841 ABIArgInfo
3842 WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
3843                                     const ABIArgInfo &current) const {
3844   // Assumes vectorCall calling convention.
3845   const Type *Base = nullptr;
3846   uint64_t NumElts = 0;
3847 
3848   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
3849       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
3850     FreeSSERegs -= NumElts;
3851     return getDirectX86Hva();
3852   }
3853   return current;
3854 }
3855 
3856 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3857                                       bool IsReturnType, bool IsVectorCall,
3858                                       bool IsRegCall) const {
3859 
3860   if (Ty->isVoidType())
3861     return ABIArgInfo::getIgnore();
3862 
3863   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3864     Ty = EnumTy->getDecl()->getIntegerType();
3865 
3866   TypeInfo Info = getContext().getTypeInfo(Ty);
3867   uint64_t Width = Info.Width;
3868   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
3869 
3870   const RecordType *RT = Ty->getAs<RecordType>();
3871   if (RT) {
3872     if (!IsReturnType) {
3873       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
3874         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3875     }
3876 
3877     if (RT->getDecl()->hasFlexibleArrayMember())
3878       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3879 
3880   }
3881 
3882   const Type *Base = nullptr;
3883   uint64_t NumElts = 0;
3884   // vectorcall adds the concept of a homogenous vector aggregate, similar to
3885   // other targets.
3886   if ((IsVectorCall || IsRegCall) &&
3887       isHomogeneousAggregate(Ty, Base, NumElts)) {
3888     if (IsRegCall) {
3889       if (FreeSSERegs >= NumElts) {
3890         FreeSSERegs -= NumElts;
3891         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3892           return ABIArgInfo::getDirect();
3893         return ABIArgInfo::getExpand();
3894       }
3895       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3896     } else if (IsVectorCall) {
3897       if (FreeSSERegs >= NumElts &&
3898           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
3899         FreeSSERegs -= NumElts;
3900         return ABIArgInfo::getDirect();
3901       } else if (IsReturnType) {
3902         return ABIArgInfo::getExpand();
3903       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
3904         // HVAs are delayed and reclassified in the 2nd step.
3905         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3906       }
3907     }
3908   }
3909 
3910   if (Ty->isMemberPointerType()) {
3911     // If the member pointer is represented by an LLVM int or ptr, pass it
3912     // directly.
3913     llvm::Type *LLTy = CGT.ConvertType(Ty);
3914     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3915       return ABIArgInfo::getDirect();
3916   }
3917 
3918   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
3919     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3920     // not 1, 2, 4, or 8 bytes, must be passed by reference."
3921     if (Width > 64 || !llvm::isPowerOf2_64(Width))
3922       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3923 
3924     // Otherwise, coerce it to a small integer.
3925     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
3926   }
3927 
3928   // Bool type is always extended to the ABI, other builtin types are not
3929   // extended.
3930   const BuiltinType *BT = Ty->getAs<BuiltinType>();
3931   if (BT && BT->getKind() == BuiltinType::Bool)
3932     return ABIArgInfo::getExtend();
3933 
3934   // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3935   // passes them indirectly through memory.
3936   if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3937     const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3938     if (LDF == &llvm::APFloat::x87DoubleExtended())
3939       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3940   }
3941 
3942   return ABIArgInfo::getDirect();
3943 }
3944 
3945 void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
3946                                              unsigned FreeSSERegs,
3947                                              bool IsVectorCall,
3948                                              bool IsRegCall) const {
3949   unsigned Count = 0;
3950   for (auto &I : FI.arguments()) {
3951     // Vectorcall in x64 only permits the first 6 arguments to be passed
3952     // as XMM/YMM registers.
3953     if (Count < VectorcallMaxParamNumAsReg)
3954       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
3955     else {
3956       // Since these cannot be passed in registers, pretend no registers
3957       // are left.
3958       unsigned ZeroSSERegsAvail = 0;
3959       I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
3960                         IsVectorCall, IsRegCall);
3961     }
3962     ++Count;
3963   }
3964 
3965   for (auto &I : FI.arguments()) {
3966     I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
3967   }
3968 }
3969 
3970 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3971   bool IsVectorCall =
3972       FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
3973   bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
3974 
3975   unsigned FreeSSERegs = 0;
3976   if (IsVectorCall) {
3977     // We can use up to 4 SSE return registers with vectorcall.
3978     FreeSSERegs = 4;
3979   } else if (IsRegCall) {
3980     // RegCall gives us 16 SSE registers.
3981     FreeSSERegs = 16;
3982   }
3983 
3984   if (!getCXXABI().classifyReturnType(FI))
3985     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
3986                                   IsVectorCall, IsRegCall);
3987 
3988   if (IsVectorCall) {
3989     // We can use up to 6 SSE register parameters with vectorcall.
3990     FreeSSERegs = 6;
3991   } else if (IsRegCall) {
3992     // RegCall gives us 16 SSE registers, we can reuse the return registers.
3993     FreeSSERegs = 16;
3994   }
3995 
3996   if (IsVectorCall) {
3997     computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
3998   } else {
3999     for (auto &I : FI.arguments())
4000       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4001   }
4002 
4003 }
4004 
4005 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4006                                     QualType Ty) const {
4007 
4008   bool IsIndirect = false;
4009 
4010   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4011   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4012   if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
4013     uint64_t Width = getContext().getTypeSize(Ty);
4014     IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4015   }
4016 
4017   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4018                           CGF.getContext().getTypeInfoInChars(Ty),
4019                           CharUnits::fromQuantity(8),
4020                           /*allowHigherAlign*/ false);
4021 }
4022 
4023 // PowerPC-32
4024 namespace {
4025 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4026 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4027 bool IsSoftFloatABI;
4028 public:
4029   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
4030       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
4031 
4032   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4033                     QualType Ty) const override;
4034 };
4035 
4036 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4037 public:
4038   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
4039       : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
4040 
4041   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4042     // This is recovered from gcc output.
4043     return 1; // r1 is the dedicated stack pointer
4044   }
4045 
4046   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4047                                llvm::Value *Address) const override;
4048 };
4049 
4050 }
4051 
4052 // TODO: this implementation is now likely redundant with
4053 // DefaultABIInfo::EmitVAArg.
4054 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4055                                       QualType Ty) const {
4056   const unsigned OverflowLimit = 8;
4057   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4058     // TODO: Implement this. For now ignore.
4059     (void)CTy;
4060     return Address::invalid(); // FIXME?
4061   }
4062 
4063   // struct __va_list_tag {
4064   //   unsigned char gpr;
4065   //   unsigned char fpr;
4066   //   unsigned short reserved;
4067   //   void *overflow_arg_area;
4068   //   void *reg_save_area;
4069   // };
4070 
4071   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4072   bool isInt =
4073       Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
4074   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4075 
4076   // All aggregates are passed indirectly?  That doesn't seem consistent
4077   // with the argument-lowering code.
4078   bool isIndirect = Ty->isAggregateType();
4079 
4080   CGBuilderTy &Builder = CGF.Builder;
4081 
4082   // The calling convention either uses 1-2 GPRs or 1 FPR.
4083   Address NumRegsAddr = Address::invalid();
4084   if (isInt || IsSoftFloatABI) {
4085     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
4086   } else {
4087     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
4088   }
4089 
4090   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4091 
4092   // "Align" the register count when TY is i64.
4093   if (isI64 || (isF64 && IsSoftFloatABI)) {
4094     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4095     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4096   }
4097 
4098   llvm::Value *CC =
4099       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4100 
4101   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4102   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4103   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4104 
4105   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4106 
4107   llvm::Type *DirectTy = CGF.ConvertType(Ty);
4108   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4109 
4110   // Case 1: consume registers.
4111   Address RegAddr = Address::invalid();
4112   {
4113     CGF.EmitBlock(UsingRegs);
4114 
4115     Address RegSaveAreaPtr =
4116       Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
4117     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4118                       CharUnits::fromQuantity(8));
4119     assert(RegAddr.getElementType() == CGF.Int8Ty);
4120 
4121     // Floating-point registers start after the general-purpose registers.
4122     if (!(isInt || IsSoftFloatABI)) {
4123       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4124                                                    CharUnits::fromQuantity(32));
4125     }
4126 
4127     // Get the address of the saved value by scaling the number of
4128     // registers we've used by the number of
4129     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4130     llvm::Value *RegOffset =
4131       Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4132     RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4133                                             RegAddr.getPointer(), RegOffset),
4134                       RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4135     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4136 
4137     // Increase the used-register count.
4138     NumRegs =
4139       Builder.CreateAdd(NumRegs,
4140                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4141     Builder.CreateStore(NumRegs, NumRegsAddr);
4142 
4143     CGF.EmitBranch(Cont);
4144   }
4145 
4146   // Case 2: consume space in the overflow area.
4147   Address MemAddr = Address::invalid();
4148   {
4149     CGF.EmitBlock(UsingOverflow);
4150 
4151     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4152 
4153     // Everything in the overflow area is rounded up to a size of at least 4.
4154     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4155 
4156     CharUnits Size;
4157     if (!isIndirect) {
4158       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4159       Size = TypeInfo.first.alignTo(OverflowAreaAlign);
4160     } else {
4161       Size = CGF.getPointerSize();
4162     }
4163 
4164     Address OverflowAreaAddr =
4165       Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
4166     Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
4167                          OverflowAreaAlign);
4168     // Round up address of argument to alignment
4169     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4170     if (Align > OverflowAreaAlign) {
4171       llvm::Value *Ptr = OverflowArea.getPointer();
4172       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4173                                                            Align);
4174     }
4175 
4176     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4177 
4178     // Increase the overflow area.
4179     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4180     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4181     CGF.EmitBranch(Cont);
4182   }
4183 
4184   CGF.EmitBlock(Cont);
4185 
4186   // Merge the cases with a phi.
4187   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4188                                 "vaarg.addr");
4189 
4190   // Load the pointer if the argument was passed indirectly.
4191   if (isIndirect) {
4192     Result = Address(Builder.CreateLoad(Result, "aggr"),
4193                      getContext().getTypeAlignInChars(Ty));
4194   }
4195 
4196   return Result;
4197 }
4198 
4199 bool
4200 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4201                                                 llvm::Value *Address) const {
4202   // This is calculated from the LLVM and GCC tables and verified
4203   // against gcc output.  AFAIK all ABIs use the same encoding.
4204 
4205   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4206 
4207   llvm::IntegerType *i8 = CGF.Int8Ty;
4208   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4209   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4210   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4211 
4212   // 0-31: r0-31, the 4-byte general-purpose registers
4213   AssignToArrayRange(Builder, Address, Four8, 0, 31);
4214 
4215   // 32-63: fp0-31, the 8-byte floating-point registers
4216   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4217 
4218   // 64-76 are various 4-byte special-purpose registers:
4219   // 64: mq
4220   // 65: lr
4221   // 66: ctr
4222   // 67: ap
4223   // 68-75 cr0-7
4224   // 76: xer
4225   AssignToArrayRange(Builder, Address, Four8, 64, 76);
4226 
4227   // 77-108: v0-31, the 16-byte vector registers
4228   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4229 
4230   // 109: vrsave
4231   // 110: vscr
4232   // 111: spe_acc
4233   // 112: spefscr
4234   // 113: sfp
4235   AssignToArrayRange(Builder, Address, Four8, 109, 113);
4236 
4237   return false;
4238 }
4239 
4240 // PowerPC-64
4241 
4242 namespace {
4243 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4244 class PPC64_SVR4_ABIInfo : public ABIInfo {
4245 public:
4246   enum ABIKind {
4247     ELFv1 = 0,
4248     ELFv2
4249   };
4250 
4251 private:
4252   static const unsigned GPRBits = 64;
4253   ABIKind Kind;
4254   bool HasQPX;
4255   bool IsSoftFloatABI;
4256 
4257   // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4258   // will be passed in a QPX register.
4259   bool IsQPXVectorTy(const Type *Ty) const {
4260     if (!HasQPX)
4261       return false;
4262 
4263     if (const VectorType *VT = Ty->getAs<VectorType>()) {
4264       unsigned NumElements = VT->getNumElements();
4265       if (NumElements == 1)
4266         return false;
4267 
4268       if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4269         if (getContext().getTypeSize(Ty) <= 256)
4270           return true;
4271       } else if (VT->getElementType()->
4272                    isSpecificBuiltinType(BuiltinType::Float)) {
4273         if (getContext().getTypeSize(Ty) <= 128)
4274           return true;
4275       }
4276     }
4277 
4278     return false;
4279   }
4280 
4281   bool IsQPXVectorTy(QualType Ty) const {
4282     return IsQPXVectorTy(Ty.getTypePtr());
4283   }
4284 
4285 public:
4286   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4287                      bool SoftFloatABI)
4288       : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4289         IsSoftFloatABI(SoftFloatABI) {}
4290 
4291   bool isPromotableTypeForABI(QualType Ty) const;
4292   CharUnits getParamTypeAlignment(QualType Ty) const;
4293 
4294   ABIArgInfo classifyReturnType(QualType RetTy) const;
4295   ABIArgInfo classifyArgumentType(QualType Ty) const;
4296 
4297   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4298   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4299                                          uint64_t Members) const override;
4300 
4301   // TODO: We can add more logic to computeInfo to improve performance.
4302   // Example: For aggregate arguments that fit in a register, we could
4303   // use getDirectInReg (as is done below for structs containing a single
4304   // floating-point value) to avoid pushing them to memory on function
4305   // entry.  This would require changing the logic in PPCISelLowering
4306   // when lowering the parameters in the caller and args in the callee.
4307   void computeInfo(CGFunctionInfo &FI) const override {
4308     if (!getCXXABI().classifyReturnType(FI))
4309       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4310     for (auto &I : FI.arguments()) {
4311       // We rely on the default argument classification for the most part.
4312       // One exception:  An aggregate containing a single floating-point
4313       // or vector item must be passed in a register if one is available.
4314       const Type *T = isSingleElementStruct(I.type, getContext());
4315       if (T) {
4316         const BuiltinType *BT = T->getAs<BuiltinType>();
4317         if (IsQPXVectorTy(T) ||
4318             (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
4319             (BT && BT->isFloatingPoint())) {
4320           QualType QT(T, 0);
4321           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
4322           continue;
4323         }
4324       }
4325       I.info = classifyArgumentType(I.type);
4326     }
4327   }
4328 
4329   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4330                     QualType Ty) const override;
4331 };
4332 
4333 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
4334 
4335 public:
4336   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
4337                                PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4338                                bool SoftFloatABI)
4339       : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
4340                                                  SoftFloatABI)) {}
4341 
4342   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4343     // This is recovered from gcc output.
4344     return 1; // r1 is the dedicated stack pointer
4345   }
4346 
4347   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4348                                llvm::Value *Address) const override;
4349 };
4350 
4351 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4352 public:
4353   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4354 
4355   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4356     // This is recovered from gcc output.
4357     return 1; // r1 is the dedicated stack pointer
4358   }
4359 
4360   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4361                                llvm::Value *Address) const override;
4362 };
4363 
4364 }
4365 
4366 // Return true if the ABI requires Ty to be passed sign- or zero-
4367 // extended to 64 bits.
4368 bool
4369 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4370   // Treat an enum type as its underlying type.
4371   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4372     Ty = EnumTy->getDecl()->getIntegerType();
4373 
4374   // Promotable integer types are required to be promoted by the ABI.
4375   if (Ty->isPromotableIntegerType())
4376     return true;
4377 
4378   // In addition to the usual promotable integer types, we also need to
4379   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4380   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4381     switch (BT->getKind()) {
4382     case BuiltinType::Int:
4383     case BuiltinType::UInt:
4384       return true;
4385     default:
4386       break;
4387     }
4388 
4389   return false;
4390 }
4391 
4392 /// isAlignedParamType - Determine whether a type requires 16-byte or
4393 /// higher alignment in the parameter area.  Always returns at least 8.
4394 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4395   // Complex types are passed just like their elements.
4396   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4397     Ty = CTy->getElementType();
4398 
4399   // Only vector types of size 16 bytes need alignment (larger types are
4400   // passed via reference, smaller types are not aligned).
4401   if (IsQPXVectorTy(Ty)) {
4402     if (getContext().getTypeSize(Ty) > 128)
4403       return CharUnits::fromQuantity(32);
4404 
4405     return CharUnits::fromQuantity(16);
4406   } else if (Ty->isVectorType()) {
4407     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
4408   }
4409 
4410   // For single-element float/vector structs, we consider the whole type
4411   // to have the same alignment requirements as its single element.
4412   const Type *AlignAsType = nullptr;
4413   const Type *EltType = isSingleElementStruct(Ty, getContext());
4414   if (EltType) {
4415     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4416     if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
4417          getContext().getTypeSize(EltType) == 128) ||
4418         (BT && BT->isFloatingPoint()))
4419       AlignAsType = EltType;
4420   }
4421 
4422   // Likewise for ELFv2 homogeneous aggregates.
4423   const Type *Base = nullptr;
4424   uint64_t Members = 0;
4425   if (!AlignAsType && Kind == ELFv2 &&
4426       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4427     AlignAsType = Base;
4428 
4429   // With special case aggregates, only vector base types need alignment.
4430   if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4431     if (getContext().getTypeSize(AlignAsType) > 128)
4432       return CharUnits::fromQuantity(32);
4433 
4434     return CharUnits::fromQuantity(16);
4435   } else if (AlignAsType) {
4436     return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
4437   }
4438 
4439   // Otherwise, we only need alignment for any aggregate type that
4440   // has an alignment requirement of >= 16 bytes.
4441   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4442     if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
4443       return CharUnits::fromQuantity(32);
4444     return CharUnits::fromQuantity(16);
4445   }
4446 
4447   return CharUnits::fromQuantity(8);
4448 }
4449 
4450 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4451 /// aggregate.  Base is set to the base element type, and Members is set
4452 /// to the number of base elements.
4453 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4454                                      uint64_t &Members) const {
4455   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4456     uint64_t NElements = AT->getSize().getZExtValue();
4457     if (NElements == 0)
4458       return false;
4459     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4460       return false;
4461     Members *= NElements;
4462   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4463     const RecordDecl *RD = RT->getDecl();
4464     if (RD->hasFlexibleArrayMember())
4465       return false;
4466 
4467     Members = 0;
4468 
4469     // If this is a C++ record, check the bases first.
4470     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4471       for (const auto &I : CXXRD->bases()) {
4472         // Ignore empty records.
4473         if (isEmptyRecord(getContext(), I.getType(), true))
4474           continue;
4475 
4476         uint64_t FldMembers;
4477         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4478           return false;
4479 
4480         Members += FldMembers;
4481       }
4482     }
4483 
4484     for (const auto *FD : RD->fields()) {
4485       // Ignore (non-zero arrays of) empty records.
4486       QualType FT = FD->getType();
4487       while (const ConstantArrayType *AT =
4488              getContext().getAsConstantArrayType(FT)) {
4489         if (AT->getSize().getZExtValue() == 0)
4490           return false;
4491         FT = AT->getElementType();
4492       }
4493       if (isEmptyRecord(getContext(), FT, true))
4494         continue;
4495 
4496       // For compatibility with GCC, ignore empty bitfields in C++ mode.
4497       if (getContext().getLangOpts().CPlusPlus &&
4498           FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4499         continue;
4500 
4501       uint64_t FldMembers;
4502       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4503         return false;
4504 
4505       Members = (RD->isUnion() ?
4506                  std::max(Members, FldMembers) : Members + FldMembers);
4507     }
4508 
4509     if (!Base)
4510       return false;
4511 
4512     // Ensure there is no padding.
4513     if (getContext().getTypeSize(Base) * Members !=
4514         getContext().getTypeSize(Ty))
4515       return false;
4516   } else {
4517     Members = 1;
4518     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4519       Members = 2;
4520       Ty = CT->getElementType();
4521     }
4522 
4523     // Most ABIs only support float, double, and some vector type widths.
4524     if (!isHomogeneousAggregateBaseType(Ty))
4525       return false;
4526 
4527     // The base type must be the same for all members.  Types that
4528     // agree in both total size and mode (float vs. vector) are
4529     // treated as being equivalent here.
4530     const Type *TyPtr = Ty.getTypePtr();
4531     if (!Base) {
4532       Base = TyPtr;
4533       // If it's a non-power-of-2 vector, its size is already a power-of-2,
4534       // so make sure to widen it explicitly.
4535       if (const VectorType *VT = Base->getAs<VectorType>()) {
4536         QualType EltTy = VT->getElementType();
4537         unsigned NumElements =
4538             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4539         Base = getContext()
4540                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
4541                    .getTypePtr();
4542       }
4543     }
4544 
4545     if (Base->isVectorType() != TyPtr->isVectorType() ||
4546         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4547       return false;
4548   }
4549   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4550 }
4551 
4552 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4553   // Homogeneous aggregates for ELFv2 must have base types of float,
4554   // double, long double, or 128-bit vectors.
4555   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4556     if (BT->getKind() == BuiltinType::Float ||
4557         BT->getKind() == BuiltinType::Double ||
4558         BT->getKind() == BuiltinType::LongDouble) {
4559       if (IsSoftFloatABI)
4560         return false;
4561       return true;
4562     }
4563   }
4564   if (const VectorType *VT = Ty->getAs<VectorType>()) {
4565     if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
4566       return true;
4567   }
4568   return false;
4569 }
4570 
4571 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4572     const Type *Base, uint64_t Members) const {
4573   // Vector types require one register, floating point types require one
4574   // or two registers depending on their size.
4575   uint32_t NumRegs =
4576       Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
4577 
4578   // Homogeneous Aggregates may occupy at most 8 registers.
4579   return Members * NumRegs <= 8;
4580 }
4581 
4582 ABIArgInfo
4583 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
4584   Ty = useFirstFieldIfTransparentUnion(Ty);
4585 
4586   if (Ty->isAnyComplexType())
4587     return ABIArgInfo::getDirect();
4588 
4589   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4590   // or via reference (larger than 16 bytes).
4591   if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
4592     uint64_t Size = getContext().getTypeSize(Ty);
4593     if (Size > 128)
4594       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4595     else if (Size < 128) {
4596       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4597       return ABIArgInfo::getDirect(CoerceTy);
4598     }
4599   }
4600 
4601   if (isAggregateTypeForABI(Ty)) {
4602     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4603       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4604 
4605     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4606     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
4607 
4608     // ELFv2 homogeneous aggregates are passed as array types.
4609     const Type *Base = nullptr;
4610     uint64_t Members = 0;
4611     if (Kind == ELFv2 &&
4612         isHomogeneousAggregate(Ty, Base, Members)) {
4613       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4614       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4615       return ABIArgInfo::getDirect(CoerceTy);
4616     }
4617 
4618     // If an aggregate may end up fully in registers, we do not
4619     // use the ByVal method, but pass the aggregate as array.
4620     // This is usually beneficial since we avoid forcing the
4621     // back-end to store the argument to memory.
4622     uint64_t Bits = getContext().getTypeSize(Ty);
4623     if (Bits > 0 && Bits <= 8 * GPRBits) {
4624       llvm::Type *CoerceTy;
4625 
4626       // Types up to 8 bytes are passed as integer type (which will be
4627       // properly aligned in the argument save area doubleword).
4628       if (Bits <= GPRBits)
4629         CoerceTy =
4630             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
4631       // Larger types are passed as arrays, with the base type selected
4632       // according to the required alignment in the save area.
4633       else {
4634         uint64_t RegBits = ABIAlign * 8;
4635         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
4636         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4637         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4638       }
4639 
4640       return ABIArgInfo::getDirect(CoerceTy);
4641     }
4642 
4643     // All other aggregates are passed ByVal.
4644     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4645                                    /*ByVal=*/true,
4646                                    /*Realign=*/TyAlign > ABIAlign);
4647   }
4648 
4649   return (isPromotableTypeForABI(Ty) ?
4650           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4651 }
4652 
4653 ABIArgInfo
4654 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4655   if (RetTy->isVoidType())
4656     return ABIArgInfo::getIgnore();
4657 
4658   if (RetTy->isAnyComplexType())
4659     return ABIArgInfo::getDirect();
4660 
4661   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4662   // or via reference (larger than 16 bytes).
4663   if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
4664     uint64_t Size = getContext().getTypeSize(RetTy);
4665     if (Size > 128)
4666       return getNaturalAlignIndirect(RetTy);
4667     else if (Size < 128) {
4668       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4669       return ABIArgInfo::getDirect(CoerceTy);
4670     }
4671   }
4672 
4673   if (isAggregateTypeForABI(RetTy)) {
4674     // ELFv2 homogeneous aggregates are returned as array types.
4675     const Type *Base = nullptr;
4676     uint64_t Members = 0;
4677     if (Kind == ELFv2 &&
4678         isHomogeneousAggregate(RetTy, Base, Members)) {
4679       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4680       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4681       return ABIArgInfo::getDirect(CoerceTy);
4682     }
4683 
4684     // ELFv2 small aggregates are returned in up to two registers.
4685     uint64_t Bits = getContext().getTypeSize(RetTy);
4686     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4687       if (Bits == 0)
4688         return ABIArgInfo::getIgnore();
4689 
4690       llvm::Type *CoerceTy;
4691       if (Bits > GPRBits) {
4692         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
4693         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
4694       } else
4695         CoerceTy =
4696             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
4697       return ABIArgInfo::getDirect(CoerceTy);
4698     }
4699 
4700     // All other aggregates are returned indirectly.
4701     return getNaturalAlignIndirect(RetTy);
4702   }
4703 
4704   return (isPromotableTypeForABI(RetTy) ?
4705           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4706 }
4707 
4708 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
4709 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4710                                       QualType Ty) const {
4711   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4712   TypeInfo.second = getParamTypeAlignment(Ty);
4713 
4714   CharUnits SlotSize = CharUnits::fromQuantity(8);
4715 
4716   // If we have a complex type and the base type is smaller than 8 bytes,
4717   // the ABI calls for the real and imaginary parts to be right-adjusted
4718   // in separate doublewords.  However, Clang expects us to produce a
4719   // pointer to a structure with the two parts packed tightly.  So generate
4720   // loads of the real and imaginary parts relative to the va_list pointer,
4721   // and store them to a temporary structure.
4722   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4723     CharUnits EltSize = TypeInfo.first / 2;
4724     if (EltSize < SlotSize) {
4725       Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4726                                             SlotSize * 2, SlotSize,
4727                                             SlotSize, /*AllowHigher*/ true);
4728 
4729       Address RealAddr = Addr;
4730       Address ImagAddr = RealAddr;
4731       if (CGF.CGM.getDataLayout().isBigEndian()) {
4732         RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4733                                                           SlotSize - EltSize);
4734         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4735                                                       2 * SlotSize - EltSize);
4736       } else {
4737         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4738       }
4739 
4740       llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4741       RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4742       ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4743       llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4744       llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4745 
4746       Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4747       CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4748                              /*init*/ true);
4749       return Temp;
4750     }
4751   }
4752 
4753   // Otherwise, just use the general rule.
4754   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4755                           TypeInfo, SlotSize, /*AllowHigher*/ true);
4756 }
4757 
4758 static bool
4759 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4760                               llvm::Value *Address) {
4761   // This is calculated from the LLVM and GCC tables and verified
4762   // against gcc output.  AFAIK all ABIs use the same encoding.
4763 
4764   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4765 
4766   llvm::IntegerType *i8 = CGF.Int8Ty;
4767   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4768   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4769   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4770 
4771   // 0-31: r0-31, the 8-byte general-purpose registers
4772   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4773 
4774   // 32-63: fp0-31, the 8-byte floating-point registers
4775   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4776 
4777   // 64-67 are various 8-byte special-purpose registers:
4778   // 64: mq
4779   // 65: lr
4780   // 66: ctr
4781   // 67: ap
4782   AssignToArrayRange(Builder, Address, Eight8, 64, 67);
4783 
4784   // 68-76 are various 4-byte special-purpose registers:
4785   // 68-75 cr0-7
4786   // 76: xer
4787   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4788 
4789   // 77-108: v0-31, the 16-byte vector registers
4790   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4791 
4792   // 109: vrsave
4793   // 110: vscr
4794   // 111: spe_acc
4795   // 112: spefscr
4796   // 113: sfp
4797   // 114: tfhar
4798   // 115: tfiar
4799   // 116: texasr
4800   AssignToArrayRange(Builder, Address, Eight8, 109, 116);
4801 
4802   return false;
4803 }
4804 
4805 bool
4806 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4807   CodeGen::CodeGenFunction &CGF,
4808   llvm::Value *Address) const {
4809 
4810   return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4811 }
4812 
4813 bool
4814 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4815                                                 llvm::Value *Address) const {
4816 
4817   return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4818 }
4819 
4820 //===----------------------------------------------------------------------===//
4821 // AArch64 ABI Implementation
4822 //===----------------------------------------------------------------------===//
4823 
4824 namespace {
4825 
4826 class AArch64ABIInfo : public SwiftABIInfo {
4827 public:
4828   enum ABIKind {
4829     AAPCS = 0,
4830     DarwinPCS,
4831     Win64
4832   };
4833 
4834 private:
4835   ABIKind Kind;
4836 
4837 public:
4838   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4839     : SwiftABIInfo(CGT), Kind(Kind) {}
4840 
4841 private:
4842   ABIKind getABIKind() const { return Kind; }
4843   bool isDarwinPCS() const { return Kind == DarwinPCS; }
4844 
4845   ABIArgInfo classifyReturnType(QualType RetTy) const;
4846   ABIArgInfo classifyArgumentType(QualType RetTy) const;
4847   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4848   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4849                                          uint64_t Members) const override;
4850 
4851   bool isIllegalVectorType(QualType Ty) const;
4852 
4853   void computeInfo(CGFunctionInfo &FI) const override {
4854     if (!getCXXABI().classifyReturnType(FI))
4855       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4856 
4857     for (auto &it : FI.arguments())
4858       it.info = classifyArgumentType(it.type);
4859   }
4860 
4861   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4862                           CodeGenFunction &CGF) const;
4863 
4864   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4865                          CodeGenFunction &CGF) const;
4866 
4867   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4868                     QualType Ty) const override {
4869     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
4870                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4871                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4872   }
4873 
4874   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4875                       QualType Ty) const override;
4876 
4877   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
4878                                     ArrayRef<llvm::Type*> scalars,
4879                                     bool asReturnValue) const override {
4880     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4881   }
4882   bool isSwiftErrorInRegister() const override {
4883     return true;
4884   }
4885 
4886   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
4887                                  unsigned elts) const override;
4888 };
4889 
4890 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
4891 public:
4892   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4893       : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
4894 
4895   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
4896     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
4897   }
4898 
4899   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4900     return 31;
4901   }
4902 
4903   bool doesReturnSlotInterfereWithArgs() const override { return false; }
4904 };
4905 
4906 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
4907 public:
4908   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
4909       : AArch64TargetCodeGenInfo(CGT, K) {}
4910 
4911   void getDependentLibraryOption(llvm::StringRef Lib,
4912                                  llvm::SmallString<24> &Opt) const override {
4913     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
4914   }
4915 
4916   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
4917                                llvm::SmallString<32> &Opt) const override {
4918     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
4919   }
4920 };
4921 }
4922 
4923 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
4924   Ty = useFirstFieldIfTransparentUnion(Ty);
4925 
4926   // Handle illegal vector types here.
4927   if (isIllegalVectorType(Ty)) {
4928     uint64_t Size = getContext().getTypeSize(Ty);
4929     // Android promotes <2 x i8> to i16, not i32
4930     if (isAndroid() && (Size <= 16)) {
4931       llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4932       return ABIArgInfo::getDirect(ResType);
4933     }
4934     if (Size <= 32) {
4935       llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
4936       return ABIArgInfo::getDirect(ResType);
4937     }
4938     if (Size == 64) {
4939       llvm::Type *ResType =
4940           llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
4941       return ABIArgInfo::getDirect(ResType);
4942     }
4943     if (Size == 128) {
4944       llvm::Type *ResType =
4945           llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
4946       return ABIArgInfo::getDirect(ResType);
4947     }
4948     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4949   }
4950 
4951   if (!isAggregateTypeForABI(Ty)) {
4952     // Treat an enum type as its underlying type.
4953     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4954       Ty = EnumTy->getDecl()->getIntegerType();
4955 
4956     return (Ty->isPromotableIntegerType() && isDarwinPCS()
4957                 ? ABIArgInfo::getExtend()
4958                 : ABIArgInfo::getDirect());
4959   }
4960 
4961   // Structures with either a non-trivial destructor or a non-trivial
4962   // copy constructor are always indirect.
4963   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
4964     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4965                                      CGCXXABI::RAA_DirectInMemory);
4966   }
4967 
4968   // Empty records are always ignored on Darwin, but actually passed in C++ mode
4969   // elsewhere for GNU compatibility.
4970   uint64_t Size = getContext().getTypeSize(Ty);
4971   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
4972   if (IsEmpty || Size == 0) {
4973     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4974       return ABIArgInfo::getIgnore();
4975 
4976     // GNU C mode. The only argument that gets ignored is an empty one with size
4977     // 0.
4978     if (IsEmpty && Size == 0)
4979       return ABIArgInfo::getIgnore();
4980     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4981   }
4982 
4983   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
4984   const Type *Base = nullptr;
4985   uint64_t Members = 0;
4986   if (isHomogeneousAggregate(Ty, Base, Members)) {
4987     return ABIArgInfo::getDirect(
4988         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
4989   }
4990 
4991   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4992   if (Size <= 128) {
4993     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4994     // same size and alignment.
4995     if (getTarget().isRenderScriptTarget()) {
4996       return coerceToIntArray(Ty, getContext(), getVMContext());
4997     }
4998     unsigned Alignment = getContext().getTypeAlign(Ty);
4999     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5000 
5001     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5002     // For aggregates with 16-byte alignment, we use i128.
5003     if (Alignment < 128 && Size == 128) {
5004       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5005       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5006     }
5007     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5008   }
5009 
5010   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5011 }
5012 
5013 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
5014   if (RetTy->isVoidType())
5015     return ABIArgInfo::getIgnore();
5016 
5017   // Large vector types should be returned via memory.
5018   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5019     return getNaturalAlignIndirect(RetTy);
5020 
5021   if (!isAggregateTypeForABI(RetTy)) {
5022     // Treat an enum type as its underlying type.
5023     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5024       RetTy = EnumTy->getDecl()->getIntegerType();
5025 
5026     return (RetTy->isPromotableIntegerType() && isDarwinPCS()
5027                 ? ABIArgInfo::getExtend()
5028                 : ABIArgInfo::getDirect());
5029   }
5030 
5031   uint64_t Size = getContext().getTypeSize(RetTy);
5032   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5033     return ABIArgInfo::getIgnore();
5034 
5035   const Type *Base = nullptr;
5036   uint64_t Members = 0;
5037   if (isHomogeneousAggregate(RetTy, Base, Members))
5038     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5039     return ABIArgInfo::getDirect();
5040 
5041   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5042   if (Size <= 128) {
5043     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5044     // same size and alignment.
5045     if (getTarget().isRenderScriptTarget()) {
5046       return coerceToIntArray(RetTy, getContext(), getVMContext());
5047     }
5048     unsigned Alignment = getContext().getTypeAlign(RetTy);
5049     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5050 
5051     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5052     // For aggregates with 16-byte alignment, we use i128.
5053     if (Alignment < 128 && Size == 128) {
5054       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5055       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5056     }
5057     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5058   }
5059 
5060   return getNaturalAlignIndirect(RetTy);
5061 }
5062 
5063 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5064 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5065   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5066     // Check whether VT is legal.
5067     unsigned NumElements = VT->getNumElements();
5068     uint64_t Size = getContext().getTypeSize(VT);
5069     // NumElements should be power of 2.
5070     if (!llvm::isPowerOf2_32(NumElements))
5071       return true;
5072     return Size != 64 && (Size != 128 || NumElements == 1);
5073   }
5074   return false;
5075 }
5076 
5077 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5078                                                llvm::Type *eltTy,
5079                                                unsigned elts) const {
5080   if (!llvm::isPowerOf2_32(elts))
5081     return false;
5082   if (totalSize.getQuantity() != 8 &&
5083       (totalSize.getQuantity() != 16 || elts == 1))
5084     return false;
5085   return true;
5086 }
5087 
5088 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5089   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5090   // point type or a short-vector type. This is the same as the 32-bit ABI,
5091   // but with the difference that any floating-point type is allowed,
5092   // including __fp16.
5093   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5094     if (BT->isFloatingPoint())
5095       return true;
5096   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5097     unsigned VecSize = getContext().getTypeSize(VT);
5098     if (VecSize == 64 || VecSize == 128)
5099       return true;
5100   }
5101   return false;
5102 }
5103 
5104 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5105                                                        uint64_t Members) const {
5106   return Members <= 4;
5107 }
5108 
5109 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
5110                                             QualType Ty,
5111                                             CodeGenFunction &CGF) const {
5112   ABIArgInfo AI = classifyArgumentType(Ty);
5113   bool IsIndirect = AI.isIndirect();
5114 
5115   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5116   if (IsIndirect)
5117     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5118   else if (AI.getCoerceToType())
5119     BaseTy = AI.getCoerceToType();
5120 
5121   unsigned NumRegs = 1;
5122   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5123     BaseTy = ArrTy->getElementType();
5124     NumRegs = ArrTy->getNumElements();
5125   }
5126   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5127 
5128   // The AArch64 va_list type and handling is specified in the Procedure Call
5129   // Standard, section B.4:
5130   //
5131   // struct {
5132   //   void *__stack;
5133   //   void *__gr_top;
5134   //   void *__vr_top;
5135   //   int __gr_offs;
5136   //   int __vr_offs;
5137   // };
5138 
5139   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5140   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5141   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5142   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5143 
5144   auto TyInfo = getContext().getTypeInfoInChars(Ty);
5145   CharUnits TyAlign = TyInfo.second;
5146 
5147   Address reg_offs_p = Address::invalid();
5148   llvm::Value *reg_offs = nullptr;
5149   int reg_top_index;
5150   CharUnits reg_top_offset;
5151   int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
5152   if (!IsFPR) {
5153     // 3 is the field number of __gr_offs
5154     reg_offs_p =
5155         CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5156                                     "gr_offs_p");
5157     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5158     reg_top_index = 1; // field number for __gr_top
5159     reg_top_offset = CharUnits::fromQuantity(8);
5160     RegSize = llvm::alignTo(RegSize, 8);
5161   } else {
5162     // 4 is the field number of __vr_offs.
5163     reg_offs_p =
5164         CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
5165                                     "vr_offs_p");
5166     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5167     reg_top_index = 2; // field number for __vr_top
5168     reg_top_offset = CharUnits::fromQuantity(16);
5169     RegSize = 16 * NumRegs;
5170   }
5171 
5172   //=======================================
5173   // Find out where argument was passed
5174   //=======================================
5175 
5176   // If reg_offs >= 0 we're already using the stack for this type of
5177   // argument. We don't want to keep updating reg_offs (in case it overflows,
5178   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5179   // whatever they get).
5180   llvm::Value *UsingStack = nullptr;
5181   UsingStack = CGF.Builder.CreateICmpSGE(
5182       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5183 
5184   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5185 
5186   // Otherwise, at least some kind of argument could go in these registers, the
5187   // question is whether this particular type is too big.
5188   CGF.EmitBlock(MaybeRegBlock);
5189 
5190   // Integer arguments may need to correct register alignment (for example a
5191   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5192   // align __gr_offs to calculate the potential address.
5193   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5194     int Align = TyAlign.getQuantity();
5195 
5196     reg_offs = CGF.Builder.CreateAdd(
5197         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5198         "align_regoffs");
5199     reg_offs = CGF.Builder.CreateAnd(
5200         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5201         "aligned_regoffs");
5202   }
5203 
5204   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
5205   // The fact that this is done unconditionally reflects the fact that
5206   // allocating an argument to the stack also uses up all the remaining
5207   // registers of the appropriate kind.
5208   llvm::Value *NewOffset = nullptr;
5209   NewOffset = CGF.Builder.CreateAdd(
5210       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5211   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5212 
5213   // Now we're in a position to decide whether this argument really was in
5214   // registers or not.
5215   llvm::Value *InRegs = nullptr;
5216   InRegs = CGF.Builder.CreateICmpSLE(
5217       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5218 
5219   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5220 
5221   //=======================================
5222   // Argument was in registers
5223   //=======================================
5224 
5225   // Now we emit the code for if the argument was originally passed in
5226   // registers. First start the appropriate block:
5227   CGF.EmitBlock(InRegBlock);
5228 
5229   llvm::Value *reg_top = nullptr;
5230   Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
5231                                                   reg_top_offset, "reg_top_p");
5232   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
5233   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5234                    CharUnits::fromQuantity(IsFPR ? 16 : 8));
5235   Address RegAddr = Address::invalid();
5236   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
5237 
5238   if (IsIndirect) {
5239     // If it's been passed indirectly (actually a struct), whatever we find from
5240     // stored registers or on the stack will actually be a struct **.
5241     MemTy = llvm::PointerType::getUnqual(MemTy);
5242   }
5243 
5244   const Type *Base = nullptr;
5245   uint64_t NumMembers = 0;
5246   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
5247   if (IsHFA && NumMembers > 1) {
5248     // Homogeneous aggregates passed in registers will have their elements split
5249     // and stored 16-bytes apart regardless of size (they're notionally in qN,
5250     // qN+1, ...). We reload and store into a temporary local variable
5251     // contiguously.
5252     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
5253     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
5254     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5255     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
5256     Address Tmp = CGF.CreateTempAlloca(HFATy,
5257                                        std::max(TyAlign, BaseTyInfo.second));
5258 
5259     // On big-endian platforms, the value will be right-aligned in its slot.
5260     int Offset = 0;
5261     if (CGF.CGM.getDataLayout().isBigEndian() &&
5262         BaseTyInfo.first.getQuantity() < 16)
5263       Offset = 16 - BaseTyInfo.first.getQuantity();
5264 
5265     for (unsigned i = 0; i < NumMembers; ++i) {
5266       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5267       Address LoadAddr =
5268         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5269       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5270 
5271       Address StoreAddr =
5272         CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
5273 
5274       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5275       CGF.Builder.CreateStore(Elem, StoreAddr);
5276     }
5277 
5278     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
5279   } else {
5280     // Otherwise the object is contiguous in memory.
5281 
5282     // It might be right-aligned in its slot.
5283     CharUnits SlotSize = BaseAddr.getAlignment();
5284     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
5285         (IsHFA || !isAggregateTypeForABI(Ty)) &&
5286         TyInfo.first < SlotSize) {
5287       CharUnits Offset = SlotSize - TyInfo.first;
5288       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
5289     }
5290 
5291     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
5292   }
5293 
5294   CGF.EmitBranch(ContBlock);
5295 
5296   //=======================================
5297   // Argument was on the stack
5298   //=======================================
5299   CGF.EmitBlock(OnStackBlock);
5300 
5301   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
5302                                                 CharUnits::Zero(), "stack_p");
5303   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
5304 
5305   // Again, stack arguments may need realignment. In this case both integer and
5306   // floating-point ones might be affected.
5307   if (!IsIndirect && TyAlign.getQuantity() > 8) {
5308     int Align = TyAlign.getQuantity();
5309 
5310     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
5311 
5312     OnStackPtr = CGF.Builder.CreateAdd(
5313         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
5314         "align_stack");
5315     OnStackPtr = CGF.Builder.CreateAnd(
5316         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
5317         "align_stack");
5318 
5319     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
5320   }
5321   Address OnStackAddr(OnStackPtr,
5322                       std::max(CharUnits::fromQuantity(8), TyAlign));
5323 
5324   // All stack slots are multiples of 8 bytes.
5325   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5326   CharUnits StackSize;
5327   if (IsIndirect)
5328     StackSize = StackSlotSize;
5329   else
5330     StackSize = TyInfo.first.alignTo(StackSlotSize);
5331 
5332   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
5333   llvm::Value *NewStack =
5334       CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
5335 
5336   // Write the new value of __stack for the next call to va_arg
5337   CGF.Builder.CreateStore(NewStack, stack_p);
5338 
5339   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
5340       TyInfo.first < StackSlotSize) {
5341     CharUnits Offset = StackSlotSize - TyInfo.first;
5342     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
5343   }
5344 
5345   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
5346 
5347   CGF.EmitBranch(ContBlock);
5348 
5349   //=======================================
5350   // Tidy up
5351   //=======================================
5352   CGF.EmitBlock(ContBlock);
5353 
5354   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5355                                  OnStackAddr, OnStackBlock, "vaargs.addr");
5356 
5357   if (IsIndirect)
5358     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
5359                    TyInfo.second);
5360 
5361   return ResAddr;
5362 }
5363 
5364 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5365                                         CodeGenFunction &CGF) const {
5366   // The backend's lowering doesn't support va_arg for aggregates or
5367   // illegal vector types.  Lower VAArg here for these cases and use
5368   // the LLVM va_arg instruction for everything else.
5369   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
5370     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
5371 
5372   CharUnits SlotSize = CharUnits::fromQuantity(8);
5373 
5374   // Empty records are ignored for parameter passing purposes.
5375   if (isEmptyRecord(getContext(), Ty, true)) {
5376     Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
5377     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5378     return Addr;
5379   }
5380 
5381   // The size of the actual thing passed, which might end up just
5382   // being a pointer for indirect types.
5383   auto TyInfo = getContext().getTypeInfoInChars(Ty);
5384 
5385   // Arguments bigger than 16 bytes which aren't homogeneous
5386   // aggregates should be passed indirectly.
5387   bool IsIndirect = false;
5388   if (TyInfo.first.getQuantity() > 16) {
5389     const Type *Base = nullptr;
5390     uint64_t Members = 0;
5391     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
5392   }
5393 
5394   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
5395                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
5396 }
5397 
5398 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5399                                     QualType Ty) const {
5400   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
5401                           CGF.getContext().getTypeInfoInChars(Ty),
5402                           CharUnits::fromQuantity(8),
5403                           /*allowHigherAlign*/ false);
5404 }
5405 
5406 //===----------------------------------------------------------------------===//
5407 // ARM ABI Implementation
5408 //===----------------------------------------------------------------------===//
5409 
5410 namespace {
5411 
5412 class ARMABIInfo : public SwiftABIInfo {
5413 public:
5414   enum ABIKind {
5415     APCS = 0,
5416     AAPCS = 1,
5417     AAPCS_VFP = 2,
5418     AAPCS16_VFP = 3,
5419   };
5420 
5421 private:
5422   ABIKind Kind;
5423 
5424 public:
5425   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
5426       : SwiftABIInfo(CGT), Kind(_Kind) {
5427     setCCs();
5428   }
5429 
5430   bool isEABI() const {
5431     switch (getTarget().getTriple().getEnvironment()) {
5432     case llvm::Triple::Android:
5433     case llvm::Triple::EABI:
5434     case llvm::Triple::EABIHF:
5435     case llvm::Triple::GNUEABI:
5436     case llvm::Triple::GNUEABIHF:
5437     case llvm::Triple::MuslEABI:
5438     case llvm::Triple::MuslEABIHF:
5439       return true;
5440     default:
5441       return false;
5442     }
5443   }
5444 
5445   bool isEABIHF() const {
5446     switch (getTarget().getTriple().getEnvironment()) {
5447     case llvm::Triple::EABIHF:
5448     case llvm::Triple::GNUEABIHF:
5449     case llvm::Triple::MuslEABIHF:
5450       return true;
5451     default:
5452       return false;
5453     }
5454   }
5455 
5456   ABIKind getABIKind() const { return Kind; }
5457 
5458 private:
5459   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
5460   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
5461   bool isIllegalVectorType(QualType Ty) const;
5462 
5463   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5464   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5465                                          uint64_t Members) const override;
5466 
5467   void computeInfo(CGFunctionInfo &FI) const override;
5468 
5469   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5470                     QualType Ty) const override;
5471 
5472   llvm::CallingConv::ID getLLVMDefaultCC() const;
5473   llvm::CallingConv::ID getABIDefaultCC() const;
5474   void setCCs();
5475 
5476   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5477                                     ArrayRef<llvm::Type*> scalars,
5478                                     bool asReturnValue) const override {
5479     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5480   }
5481   bool isSwiftErrorInRegister() const override {
5482     return true;
5483   }
5484   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5485                                  unsigned elts) const override;
5486 };
5487 
5488 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5489 public:
5490   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5491     :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
5492 
5493   const ARMABIInfo &getABIInfo() const {
5494     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5495   }
5496 
5497   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5498     return 13;
5499   }
5500 
5501   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5502     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
5503   }
5504 
5505   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5506                                llvm::Value *Address) const override {
5507     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
5508 
5509     // 0-15 are the 16 integer registers.
5510     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
5511     return false;
5512   }
5513 
5514   unsigned getSizeOfUnwindException() const override {
5515     if (getABIInfo().isEABI()) return 88;
5516     return TargetCodeGenInfo::getSizeOfUnwindException();
5517   }
5518 
5519   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5520                            CodeGen::CodeGenModule &CGM,
5521                            ForDefinition_t IsForDefinition) const override {
5522     if (!IsForDefinition)
5523       return;
5524     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5525     if (!FD)
5526       return;
5527 
5528     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5529     if (!Attr)
5530       return;
5531 
5532     const char *Kind;
5533     switch (Attr->getInterrupt()) {
5534     case ARMInterruptAttr::Generic: Kind = ""; break;
5535     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
5536     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
5537     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
5538     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
5539     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
5540     }
5541 
5542     llvm::Function *Fn = cast<llvm::Function>(GV);
5543 
5544     Fn->addFnAttr("interrupt", Kind);
5545 
5546     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5547     if (ABI == ARMABIInfo::APCS)
5548       return;
5549 
5550     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5551     // however this is not necessarily true on taking any interrupt. Instruct
5552     // the backend to perform a realignment as part of the function prologue.
5553     llvm::AttrBuilder B;
5554     B.addStackAlignmentAttr(8);
5555     Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
5556   }
5557 };
5558 
5559 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
5560 public:
5561   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5562       : ARMTargetCodeGenInfo(CGT, K) {}
5563 
5564   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5565                            CodeGen::CodeGenModule &CGM,
5566                            ForDefinition_t IsForDefinition) const override;
5567 
5568   void getDependentLibraryOption(llvm::StringRef Lib,
5569                                  llvm::SmallString<24> &Opt) const override {
5570     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5571   }
5572 
5573   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5574                                llvm::SmallString<32> &Opt) const override {
5575     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5576   }
5577 };
5578 
5579 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
5580     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
5581     ForDefinition_t IsForDefinition) const {
5582   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
5583   if (!IsForDefinition)
5584     return;
5585   addStackProbeSizeTargetAttribute(D, GV, CGM);
5586 }
5587 }
5588 
5589 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
5590   if (!getCXXABI().classifyReturnType(FI))
5591     FI.getReturnInfo() =
5592         classifyReturnType(FI.getReturnType(), FI.isVariadic());
5593 
5594   for (auto &I : FI.arguments())
5595     I.info = classifyArgumentType(I.type, FI.isVariadic());
5596 
5597   // Always honor user-specified calling convention.
5598   if (FI.getCallingConvention() != llvm::CallingConv::C)
5599     return;
5600 
5601   llvm::CallingConv::ID cc = getRuntimeCC();
5602   if (cc != llvm::CallingConv::C)
5603     FI.setEffectiveCallingConvention(cc);
5604 }
5605 
5606 /// Return the default calling convention that LLVM will use.
5607 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5608   // The default calling convention that LLVM will infer.
5609   if (isEABIHF() || getTarget().getTriple().isWatchABI())
5610     return llvm::CallingConv::ARM_AAPCS_VFP;
5611   else if (isEABI())
5612     return llvm::CallingConv::ARM_AAPCS;
5613   else
5614     return llvm::CallingConv::ARM_APCS;
5615 }
5616 
5617 /// Return the calling convention that our ABI would like us to use
5618 /// as the C calling convention.
5619 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
5620   switch (getABIKind()) {
5621   case APCS: return llvm::CallingConv::ARM_APCS;
5622   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5623   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
5624   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
5625   }
5626   llvm_unreachable("bad ABI kind");
5627 }
5628 
5629 void ARMABIInfo::setCCs() {
5630   assert(getRuntimeCC() == llvm::CallingConv::C);
5631 
5632   // Don't muddy up the IR with a ton of explicit annotations if
5633   // they'd just match what LLVM will infer from the triple.
5634   llvm::CallingConv::ID abiCC = getABIDefaultCC();
5635   if (abiCC != getLLVMDefaultCC())
5636     RuntimeCC = abiCC;
5637 
5638   // AAPCS apparently requires runtime support functions to be soft-float, but
5639   // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5640   // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
5641 
5642   // The Run-time ABI for the ARM Architecture section 4.1.2 requires
5643   // AEABI-complying FP helper functions to use the base AAPCS.
5644   // These AEABI functions are expanded in the ARM llvm backend, all the builtin
5645   // support functions emitted by clang such as the _Complex helpers follow the
5646   // abiCC.
5647   if (abiCC != getLLVMDefaultCC())
5648       BuiltinCC = abiCC;
5649 }
5650 
5651 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5652                                             bool isVariadic) const {
5653   // 6.1.2.1 The following argument types are VFP CPRCs:
5654   //   A single-precision floating-point type (including promoted
5655   //   half-precision types); A double-precision floating-point type;
5656   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5657   //   with a Base Type of a single- or double-precision floating-point type,
5658   //   64-bit containerized vectors or 128-bit containerized vectors with one
5659   //   to four Elements.
5660   bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
5661 
5662   Ty = useFirstFieldIfTransparentUnion(Ty);
5663 
5664   // Handle illegal vector types here.
5665   if (isIllegalVectorType(Ty)) {
5666     uint64_t Size = getContext().getTypeSize(Ty);
5667     if (Size <= 32) {
5668       llvm::Type *ResType =
5669           llvm::Type::getInt32Ty(getVMContext());
5670       return ABIArgInfo::getDirect(ResType);
5671     }
5672     if (Size == 64) {
5673       llvm::Type *ResType = llvm::VectorType::get(
5674           llvm::Type::getInt32Ty(getVMContext()), 2);
5675       return ABIArgInfo::getDirect(ResType);
5676     }
5677     if (Size == 128) {
5678       llvm::Type *ResType = llvm::VectorType::get(
5679           llvm::Type::getInt32Ty(getVMContext()), 4);
5680       return ABIArgInfo::getDirect(ResType);
5681     }
5682     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5683   }
5684 
5685   // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5686   // unspecified. This is not done for OpenCL as it handles the half type
5687   // natively, and does not need to interwork with AAPCS code.
5688   if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
5689     llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5690       llvm::Type::getFloatTy(getVMContext()) :
5691       llvm::Type::getInt32Ty(getVMContext());
5692     return ABIArgInfo::getDirect(ResType);
5693   }
5694 
5695   if (!isAggregateTypeForABI(Ty)) {
5696     // Treat an enum type as its underlying type.
5697     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
5698       Ty = EnumTy->getDecl()->getIntegerType();
5699     }
5700 
5701     return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5702                                           : ABIArgInfo::getDirect());
5703   }
5704 
5705   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5706     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5707   }
5708 
5709   // Ignore empty records.
5710   if (isEmptyRecord(getContext(), Ty, true))
5711     return ABIArgInfo::getIgnore();
5712 
5713   if (IsEffectivelyAAPCS_VFP) {
5714     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5715     // into VFP registers.
5716     const Type *Base = nullptr;
5717     uint64_t Members = 0;
5718     if (isHomogeneousAggregate(Ty, Base, Members)) {
5719       assert(Base && "Base class should be set for homogeneous aggregate");
5720       // Base can be a floating-point or a vector.
5721       return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
5722     }
5723   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5724     // WatchOS does have homogeneous aggregates. Note that we intentionally use
5725     // this convention even for a variadic function: the backend will use GPRs
5726     // if needed.
5727     const Type *Base = nullptr;
5728     uint64_t Members = 0;
5729     if (isHomogeneousAggregate(Ty, Base, Members)) {
5730       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5731       llvm::Type *Ty =
5732         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5733       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5734     }
5735   }
5736 
5737   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5738       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5739     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5740     // bigger than 128-bits, they get placed in space allocated by the caller,
5741     // and a pointer is passed.
5742     return ABIArgInfo::getIndirect(
5743         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
5744   }
5745 
5746   // Support byval for ARM.
5747   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5748   // most 8-byte. We realign the indirect argument if type alignment is bigger
5749   // than ABI alignment.
5750   uint64_t ABIAlign = 4;
5751   uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5752   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5753        getABIKind() == ARMABIInfo::AAPCS)
5754     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
5755 
5756   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
5757     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
5758     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5759                                    /*ByVal=*/true,
5760                                    /*Realign=*/TyAlign > ABIAlign);
5761   }
5762 
5763   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5764   // same size and alignment.
5765   if (getTarget().isRenderScriptTarget()) {
5766     return coerceToIntArray(Ty, getContext(), getVMContext());
5767   }
5768 
5769   // Otherwise, pass by coercing to a structure of the appropriate size.
5770   llvm::Type* ElemTy;
5771   unsigned SizeRegs;
5772   // FIXME: Try to match the types of the arguments more accurately where
5773   // we can.
5774   if (getContext().getTypeAlign(Ty) <= 32) {
5775     ElemTy = llvm::Type::getInt32Ty(getVMContext());
5776     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
5777   } else {
5778     ElemTy = llvm::Type::getInt64Ty(getVMContext());
5779     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
5780   }
5781 
5782   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
5783 }
5784 
5785 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
5786                               llvm::LLVMContext &VMContext) {
5787   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5788   // is called integer-like if its size is less than or equal to one word, and
5789   // the offset of each of its addressable sub-fields is zero.
5790 
5791   uint64_t Size = Context.getTypeSize(Ty);
5792 
5793   // Check that the type fits in a word.
5794   if (Size > 32)
5795     return false;
5796 
5797   // FIXME: Handle vector types!
5798   if (Ty->isVectorType())
5799     return false;
5800 
5801   // Float types are never treated as "integer like".
5802   if (Ty->isRealFloatingType())
5803     return false;
5804 
5805   // If this is a builtin or pointer type then it is ok.
5806   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
5807     return true;
5808 
5809   // Small complex integer types are "integer like".
5810   if (const ComplexType *CT = Ty->getAs<ComplexType>())
5811     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
5812 
5813   // Single element and zero sized arrays should be allowed, by the definition
5814   // above, but they are not.
5815 
5816   // Otherwise, it must be a record type.
5817   const RecordType *RT = Ty->getAs<RecordType>();
5818   if (!RT) return false;
5819 
5820   // Ignore records with flexible arrays.
5821   const RecordDecl *RD = RT->getDecl();
5822   if (RD->hasFlexibleArrayMember())
5823     return false;
5824 
5825   // Check that all sub-fields are at offset 0, and are themselves "integer
5826   // like".
5827   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5828 
5829   bool HadField = false;
5830   unsigned idx = 0;
5831   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5832        i != e; ++i, ++idx) {
5833     const FieldDecl *FD = *i;
5834 
5835     // Bit-fields are not addressable, we only need to verify they are "integer
5836     // like". We still have to disallow a subsequent non-bitfield, for example:
5837     //   struct { int : 0; int x }
5838     // is non-integer like according to gcc.
5839     if (FD->isBitField()) {
5840       if (!RD->isUnion())
5841         HadField = true;
5842 
5843       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5844         return false;
5845 
5846       continue;
5847     }
5848 
5849     // Check if this field is at offset 0.
5850     if (Layout.getFieldOffset(idx) != 0)
5851       return false;
5852 
5853     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5854       return false;
5855 
5856     // Only allow at most one field in a structure. This doesn't match the
5857     // wording above, but follows gcc in situations with a field following an
5858     // empty structure.
5859     if (!RD->isUnion()) {
5860       if (HadField)
5861         return false;
5862 
5863       HadField = true;
5864     }
5865   }
5866 
5867   return true;
5868 }
5869 
5870 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5871                                           bool isVariadic) const {
5872   bool IsEffectivelyAAPCS_VFP =
5873       (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
5874 
5875   if (RetTy->isVoidType())
5876     return ABIArgInfo::getIgnore();
5877 
5878   // Large vector types should be returned via memory.
5879   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
5880     return getNaturalAlignIndirect(RetTy);
5881   }
5882 
5883   // __fp16 gets returned as if it were an int or float, but with the top 16
5884   // bits unspecified. This is not done for OpenCL as it handles the half type
5885   // natively, and does not need to interwork with AAPCS code.
5886   if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
5887     llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5888       llvm::Type::getFloatTy(getVMContext()) :
5889       llvm::Type::getInt32Ty(getVMContext());
5890     return ABIArgInfo::getDirect(ResType);
5891   }
5892 
5893   if (!isAggregateTypeForABI(RetTy)) {
5894     // Treat an enum type as its underlying type.
5895     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5896       RetTy = EnumTy->getDecl()->getIntegerType();
5897 
5898     return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5899                                             : ABIArgInfo::getDirect();
5900   }
5901 
5902   // Are we following APCS?
5903   if (getABIKind() == APCS) {
5904     if (isEmptyRecord(getContext(), RetTy, false))
5905       return ABIArgInfo::getIgnore();
5906 
5907     // Complex types are all returned as packed integers.
5908     //
5909     // FIXME: Consider using 2 x vector types if the back end handles them
5910     // correctly.
5911     if (RetTy->isAnyComplexType())
5912       return ABIArgInfo::getDirect(llvm::IntegerType::get(
5913           getVMContext(), getContext().getTypeSize(RetTy)));
5914 
5915     // Integer like structures are returned in r0.
5916     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
5917       // Return in the smallest viable integer type.
5918       uint64_t Size = getContext().getTypeSize(RetTy);
5919       if (Size <= 8)
5920         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5921       if (Size <= 16)
5922         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5923       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5924     }
5925 
5926     // Otherwise return in memory.
5927     return getNaturalAlignIndirect(RetTy);
5928   }
5929 
5930   // Otherwise this is an AAPCS variant.
5931 
5932   if (isEmptyRecord(getContext(), RetTy, true))
5933     return ABIArgInfo::getIgnore();
5934 
5935   // Check for homogeneous aggregates with AAPCS-VFP.
5936   if (IsEffectivelyAAPCS_VFP) {
5937     const Type *Base = nullptr;
5938     uint64_t Members = 0;
5939     if (isHomogeneousAggregate(RetTy, Base, Members)) {
5940       assert(Base && "Base class should be set for homogeneous aggregate");
5941       // Homogeneous Aggregates are returned directly.
5942       return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
5943     }
5944   }
5945 
5946   // Aggregates <= 4 bytes are returned in r0; other aggregates
5947   // are returned indirectly.
5948   uint64_t Size = getContext().getTypeSize(RetTy);
5949   if (Size <= 32) {
5950     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5951     // same size and alignment.
5952     if (getTarget().isRenderScriptTarget()) {
5953       return coerceToIntArray(RetTy, getContext(), getVMContext());
5954     }
5955     if (getDataLayout().isBigEndian())
5956       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
5957       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5958 
5959     // Return in the smallest viable integer type.
5960     if (Size <= 8)
5961       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5962     if (Size <= 16)
5963       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5964     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5965   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5966     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5967     llvm::Type *CoerceTy =
5968         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
5969     return ABIArgInfo::getDirect(CoerceTy);
5970   }
5971 
5972   return getNaturalAlignIndirect(RetTy);
5973 }
5974 
5975 /// isIllegalVector - check whether Ty is an illegal vector type.
5976 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
5977   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5978     if (isAndroid()) {
5979       // Android shipped using Clang 3.1, which supported a slightly different
5980       // vector ABI. The primary differences were that 3-element vector types
5981       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5982       // accepts that legacy behavior for Android only.
5983       // Check whether VT is legal.
5984       unsigned NumElements = VT->getNumElements();
5985       // NumElements should be power of 2 or equal to 3.
5986       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5987         return true;
5988     } else {
5989       // Check whether VT is legal.
5990       unsigned NumElements = VT->getNumElements();
5991       uint64_t Size = getContext().getTypeSize(VT);
5992       // NumElements should be power of 2.
5993       if (!llvm::isPowerOf2_32(NumElements))
5994         return true;
5995       // Size should be greater than 32 bits.
5996       return Size <= 32;
5997     }
5998   }
5999   return false;
6000 }
6001 
6002 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6003                                            llvm::Type *eltTy,
6004                                            unsigned numElts) const {
6005   if (!llvm::isPowerOf2_32(numElts))
6006     return false;
6007   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6008   if (size > 64)
6009     return false;
6010   if (vectorSize.getQuantity() != 8 &&
6011       (vectorSize.getQuantity() != 16 || numElts == 1))
6012     return false;
6013   return true;
6014 }
6015 
6016 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6017   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6018   // double, or 64-bit or 128-bit vectors.
6019   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6020     if (BT->getKind() == BuiltinType::Float ||
6021         BT->getKind() == BuiltinType::Double ||
6022         BT->getKind() == BuiltinType::LongDouble)
6023       return true;
6024   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6025     unsigned VecSize = getContext().getTypeSize(VT);
6026     if (VecSize == 64 || VecSize == 128)
6027       return true;
6028   }
6029   return false;
6030 }
6031 
6032 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6033                                                    uint64_t Members) const {
6034   return Members <= 4;
6035 }
6036 
6037 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6038                               QualType Ty) const {
6039   CharUnits SlotSize = CharUnits::fromQuantity(4);
6040 
6041   // Empty records are ignored for parameter passing purposes.
6042   if (isEmptyRecord(getContext(), Ty, true)) {
6043     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6044     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6045     return Addr;
6046   }
6047 
6048   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6049   CharUnits TyAlignForABI = TyInfo.second;
6050 
6051   // Use indirect if size of the illegal vector is bigger than 16 bytes.
6052   bool IsIndirect = false;
6053   const Type *Base = nullptr;
6054   uint64_t Members = 0;
6055   if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6056     IsIndirect = true;
6057 
6058   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6059   // allocated by the caller.
6060   } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
6061              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6062              !isHomogeneousAggregate(Ty, Base, Members)) {
6063     IsIndirect = true;
6064 
6065   // Otherwise, bound the type's ABI alignment.
6066   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6067   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
6068   // Our callers should be prepared to handle an under-aligned address.
6069   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6070              getABIKind() == ARMABIInfo::AAPCS) {
6071     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6072     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
6073   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6074     // ARMv7k allows type alignment up to 16 bytes.
6075     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6076     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
6077   } else {
6078     TyAlignForABI = CharUnits::fromQuantity(4);
6079   }
6080   TyInfo.second = TyAlignForABI;
6081 
6082   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6083                           SlotSize, /*AllowHigherAlign*/ true);
6084 }
6085 
6086 //===----------------------------------------------------------------------===//
6087 // NVPTX ABI Implementation
6088 //===----------------------------------------------------------------------===//
6089 
6090 namespace {
6091 
6092 class NVPTXABIInfo : public ABIInfo {
6093 public:
6094   NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6095 
6096   ABIArgInfo classifyReturnType(QualType RetTy) const;
6097   ABIArgInfo classifyArgumentType(QualType Ty) const;
6098 
6099   void computeInfo(CGFunctionInfo &FI) const override;
6100   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6101                     QualType Ty) const override;
6102 };
6103 
6104 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
6105 public:
6106   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6107     : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
6108 
6109   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6110                            CodeGen::CodeGenModule &M,
6111                            ForDefinition_t IsForDefinition) const override;
6112 
6113 private:
6114   // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
6115   // resulting MDNode to the nvvm.annotations MDNode.
6116   static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
6117 };
6118 
6119 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
6120   if (RetTy->isVoidType())
6121     return ABIArgInfo::getIgnore();
6122 
6123   // note: this is different from default ABI
6124   if (!RetTy->isScalarType())
6125     return ABIArgInfo::getDirect();
6126 
6127   // Treat an enum type as its underlying type.
6128   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6129     RetTy = EnumTy->getDecl()->getIntegerType();
6130 
6131   return (RetTy->isPromotableIntegerType() ?
6132           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6133 }
6134 
6135 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
6136   // Treat an enum type as its underlying type.
6137   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6138     Ty = EnumTy->getDecl()->getIntegerType();
6139 
6140   // Return aggregates type as indirect by value
6141   if (isAggregateTypeForABI(Ty))
6142     return getNaturalAlignIndirect(Ty, /* byval */ true);
6143 
6144   return (Ty->isPromotableIntegerType() ?
6145           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6146 }
6147 
6148 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
6149   if (!getCXXABI().classifyReturnType(FI))
6150     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6151   for (auto &I : FI.arguments())
6152     I.info = classifyArgumentType(I.type);
6153 
6154   // Always honor user-specified calling convention.
6155   if (FI.getCallingConvention() != llvm::CallingConv::C)
6156     return;
6157 
6158   FI.setEffectiveCallingConvention(getRuntimeCC());
6159 }
6160 
6161 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6162                                 QualType Ty) const {
6163   llvm_unreachable("NVPTX does not support varargs");
6164 }
6165 
6166 void NVPTXTargetCodeGenInfo::setTargetAttributes(
6167     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
6168     ForDefinition_t IsForDefinition) const {
6169   if (!IsForDefinition)
6170     return;
6171   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6172   if (!FD) return;
6173 
6174   llvm::Function *F = cast<llvm::Function>(GV);
6175 
6176   // Perform special handling in OpenCL mode
6177   if (M.getLangOpts().OpenCL) {
6178     // Use OpenCL function attributes to check for kernel functions
6179     // By default, all functions are device functions
6180     if (FD->hasAttr<OpenCLKernelAttr>()) {
6181       // OpenCL __kernel functions get kernel metadata
6182       // Create !{<func-ref>, metadata !"kernel", i32 1} node
6183       addNVVMMetadata(F, "kernel", 1);
6184       // And kernel functions are not subject to inlining
6185       F->addFnAttr(llvm::Attribute::NoInline);
6186     }
6187   }
6188 
6189   // Perform special handling in CUDA mode.
6190   if (M.getLangOpts().CUDA) {
6191     // CUDA __global__ functions get a kernel metadata entry.  Since
6192     // __global__ functions cannot be called from the device, we do not
6193     // need to set the noinline attribute.
6194     if (FD->hasAttr<CUDAGlobalAttr>()) {
6195       // Create !{<func-ref>, metadata !"kernel", i32 1} node
6196       addNVVMMetadata(F, "kernel", 1);
6197     }
6198     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
6199       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
6200       llvm::APSInt MaxThreads(32);
6201       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
6202       if (MaxThreads > 0)
6203         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
6204 
6205       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
6206       // not specified in __launch_bounds__ or if the user specified a 0 value,
6207       // we don't have to add a PTX directive.
6208       if (Attr->getMinBlocks()) {
6209         llvm::APSInt MinBlocks(32);
6210         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
6211         if (MinBlocks > 0)
6212           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
6213           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
6214       }
6215     }
6216   }
6217 }
6218 
6219 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
6220                                              int Operand) {
6221   llvm::Module *M = F->getParent();
6222   llvm::LLVMContext &Ctx = M->getContext();
6223 
6224   // Get "nvvm.annotations" metadata node
6225   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
6226 
6227   llvm::Metadata *MDVals[] = {
6228       llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
6229       llvm::ConstantAsMetadata::get(
6230           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
6231   // Append metadata to nvvm.annotations
6232   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6233 }
6234 }
6235 
6236 //===----------------------------------------------------------------------===//
6237 // SystemZ ABI Implementation
6238 //===----------------------------------------------------------------------===//
6239 
6240 namespace {
6241 
6242 class SystemZABIInfo : public SwiftABIInfo {
6243   bool HasVector;
6244 
6245 public:
6246   SystemZABIInfo(CodeGenTypes &CGT, bool HV)
6247     : SwiftABIInfo(CGT), HasVector(HV) {}
6248 
6249   bool isPromotableIntegerType(QualType Ty) const;
6250   bool isCompoundType(QualType Ty) const;
6251   bool isVectorArgumentType(QualType Ty) const;
6252   bool isFPArgumentType(QualType Ty) const;
6253   QualType GetSingleElementType(QualType Ty) const;
6254 
6255   ABIArgInfo classifyReturnType(QualType RetTy) const;
6256   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
6257 
6258   void computeInfo(CGFunctionInfo &FI) const override {
6259     if (!getCXXABI().classifyReturnType(FI))
6260       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6261     for (auto &I : FI.arguments())
6262       I.info = classifyArgumentType(I.type);
6263   }
6264 
6265   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6266                     QualType Ty) const override;
6267 
6268   bool shouldPassIndirectlyForSwift(CharUnits totalSize,
6269                                     ArrayRef<llvm::Type*> scalars,
6270                                     bool asReturnValue) const override {
6271     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6272   }
6273   bool isSwiftErrorInRegister() const override {
6274     return true;
6275   }
6276 };
6277 
6278 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
6279 public:
6280   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
6281     : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
6282 };
6283 
6284 }
6285 
6286 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
6287   // Treat an enum type as its underlying type.
6288   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6289     Ty = EnumTy->getDecl()->getIntegerType();
6290 
6291   // Promotable integer types are required to be promoted by the ABI.
6292   if (Ty->isPromotableIntegerType())
6293     return true;
6294 
6295   // 32-bit values must also be promoted.
6296   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6297     switch (BT->getKind()) {
6298     case BuiltinType::Int:
6299     case BuiltinType::UInt:
6300       return true;
6301     default:
6302       return false;
6303     }
6304   return false;
6305 }
6306 
6307 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
6308   return (Ty->isAnyComplexType() ||
6309           Ty->isVectorType() ||
6310           isAggregateTypeForABI(Ty));
6311 }
6312 
6313 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
6314   return (HasVector &&
6315           Ty->isVectorType() &&
6316           getContext().getTypeSize(Ty) <= 128);
6317 }
6318 
6319 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
6320   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6321     switch (BT->getKind()) {
6322     case BuiltinType::Float:
6323     case BuiltinType::Double:
6324       return true;
6325     default:
6326       return false;
6327     }
6328 
6329   return false;
6330 }
6331 
6332 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
6333   if (const RecordType *RT = Ty->getAsStructureType()) {
6334     const RecordDecl *RD = RT->getDecl();
6335     QualType Found;
6336 
6337     // If this is a C++ record, check the bases first.
6338     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6339       for (const auto &I : CXXRD->bases()) {
6340         QualType Base = I.getType();
6341 
6342         // Empty bases don't affect things either way.
6343         if (isEmptyRecord(getContext(), Base, true))
6344           continue;
6345 
6346         if (!Found.isNull())
6347           return Ty;
6348         Found = GetSingleElementType(Base);
6349       }
6350 
6351     // Check the fields.
6352     for (const auto *FD : RD->fields()) {
6353       // For compatibility with GCC, ignore empty bitfields in C++ mode.
6354       // Unlike isSingleElementStruct(), empty structure and array fields
6355       // do count.  So do anonymous bitfields that aren't zero-sized.
6356       if (getContext().getLangOpts().CPlusPlus &&
6357           FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
6358         continue;
6359 
6360       // Unlike isSingleElementStruct(), arrays do not count.
6361       // Nested structures still do though.
6362       if (!Found.isNull())
6363         return Ty;
6364       Found = GetSingleElementType(FD->getType());
6365     }
6366 
6367     // Unlike isSingleElementStruct(), trailing padding is allowed.
6368     // An 8-byte aligned struct s { float f; } is passed as a double.
6369     if (!Found.isNull())
6370       return Found;
6371   }
6372 
6373   return Ty;
6374 }
6375 
6376 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6377                                   QualType Ty) const {
6378   // Assume that va_list type is correct; should be pointer to LLVM type:
6379   // struct {
6380   //   i64 __gpr;
6381   //   i64 __fpr;
6382   //   i8 *__overflow_arg_area;
6383   //   i8 *__reg_save_area;
6384   // };
6385 
6386   // Every non-vector argument occupies 8 bytes and is passed by preference
6387   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
6388   // always passed on the stack.
6389   Ty = getContext().getCanonicalType(Ty);
6390   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6391   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
6392   llvm::Type *DirectTy = ArgTy;
6393   ABIArgInfo AI = classifyArgumentType(Ty);
6394   bool IsIndirect = AI.isIndirect();
6395   bool InFPRs = false;
6396   bool IsVector = false;
6397   CharUnits UnpaddedSize;
6398   CharUnits DirectAlign;
6399   if (IsIndirect) {
6400     DirectTy = llvm::PointerType::getUnqual(DirectTy);
6401     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
6402   } else {
6403     if (AI.getCoerceToType())
6404       ArgTy = AI.getCoerceToType();
6405     InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
6406     IsVector = ArgTy->isVectorTy();
6407     UnpaddedSize = TyInfo.first;
6408     DirectAlign = TyInfo.second;
6409   }
6410   CharUnits PaddedSize = CharUnits::fromQuantity(8);
6411   if (IsVector && UnpaddedSize > PaddedSize)
6412     PaddedSize = CharUnits::fromQuantity(16);
6413   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
6414 
6415   CharUnits Padding = (PaddedSize - UnpaddedSize);
6416 
6417   llvm::Type *IndexTy = CGF.Int64Ty;
6418   llvm::Value *PaddedSizeV =
6419     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
6420 
6421   if (IsVector) {
6422     // Work out the address of a vector argument on the stack.
6423     // Vector arguments are always passed in the high bits of a
6424     // single (8 byte) or double (16 byte) stack slot.
6425     Address OverflowArgAreaPtr =
6426       CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
6427                                   "overflow_arg_area_ptr");
6428     Address OverflowArgArea =
6429       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6430               TyInfo.second);
6431     Address MemAddr =
6432       CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
6433 
6434     // Update overflow_arg_area_ptr pointer
6435     llvm::Value *NewOverflowArgArea =
6436       CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6437                             "overflow_arg_area");
6438     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6439 
6440     return MemAddr;
6441   }
6442 
6443   assert(PaddedSize.getQuantity() == 8);
6444 
6445   unsigned MaxRegs, RegCountField, RegSaveIndex;
6446   CharUnits RegPadding;
6447   if (InFPRs) {
6448     MaxRegs = 4; // Maximum of 4 FPR arguments
6449     RegCountField = 1; // __fpr
6450     RegSaveIndex = 16; // save offset for f0
6451     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
6452   } else {
6453     MaxRegs = 5; // Maximum of 5 GPR arguments
6454     RegCountField = 0; // __gpr
6455     RegSaveIndex = 2; // save offset for r2
6456     RegPadding = Padding; // values are passed in the low bits of a GPR
6457   }
6458 
6459   Address RegCountPtr = CGF.Builder.CreateStructGEP(
6460       VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6461       "reg_count_ptr");
6462   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
6463   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6464   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
6465                                                  "fits_in_regs");
6466 
6467   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6468   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6469   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6470   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6471 
6472   // Emit code to load the value if it was passed in registers.
6473   CGF.EmitBlock(InRegBlock);
6474 
6475   // Work out the address of an argument register.
6476   llvm::Value *ScaledRegCount =
6477     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6478   llvm::Value *RegBase =
6479     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6480                                       + RegPadding.getQuantity());
6481   llvm::Value *RegOffset =
6482     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
6483   Address RegSaveAreaPtr =
6484       CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6485                                   "reg_save_area_ptr");
6486   llvm::Value *RegSaveArea =
6487     CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
6488   Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6489                                            "raw_reg_addr"),
6490                      PaddedSize);
6491   Address RegAddr =
6492     CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
6493 
6494   // Update the register count
6495   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6496   llvm::Value *NewRegCount =
6497     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6498   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6499   CGF.EmitBranch(ContBlock);
6500 
6501   // Emit code to load the value if it was passed in memory.
6502   CGF.EmitBlock(InMemBlock);
6503 
6504   // Work out the address of a stack argument.
6505   Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6506       VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6507   Address OverflowArgArea =
6508     Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6509             PaddedSize);
6510   Address RawMemAddr =
6511     CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6512   Address MemAddr =
6513     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
6514 
6515   // Update overflow_arg_area_ptr pointer
6516   llvm::Value *NewOverflowArgArea =
6517     CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6518                           "overflow_arg_area");
6519   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6520   CGF.EmitBranch(ContBlock);
6521 
6522   // Return the appropriate result.
6523   CGF.EmitBlock(ContBlock);
6524   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6525                                  MemAddr, InMemBlock, "va_arg.addr");
6526 
6527   if (IsIndirect)
6528     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6529                       TyInfo.second);
6530 
6531   return ResAddr;
6532 }
6533 
6534 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6535   if (RetTy->isVoidType())
6536     return ABIArgInfo::getIgnore();
6537   if (isVectorArgumentType(RetTy))
6538     return ABIArgInfo::getDirect();
6539   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
6540     return getNaturalAlignIndirect(RetTy);
6541   return (isPromotableIntegerType(RetTy) ?
6542           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6543 }
6544 
6545 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6546   // Handle the generic C++ ABI.
6547   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6548     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6549 
6550   // Integers and enums are extended to full register width.
6551   if (isPromotableIntegerType(Ty))
6552     return ABIArgInfo::getExtend();
6553 
6554   // Handle vector types and vector-like structure types.  Note that
6555   // as opposed to float-like structure types, we do not allow any
6556   // padding for vector-like structures, so verify the sizes match.
6557   uint64_t Size = getContext().getTypeSize(Ty);
6558   QualType SingleElementTy = GetSingleElementType(Ty);
6559   if (isVectorArgumentType(SingleElementTy) &&
6560       getContext().getTypeSize(SingleElementTy) == Size)
6561     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6562 
6563   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
6564   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
6565     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6566 
6567   // Handle small structures.
6568   if (const RecordType *RT = Ty->getAs<RecordType>()) {
6569     // Structures with flexible arrays have variable length, so really
6570     // fail the size test above.
6571     const RecordDecl *RD = RT->getDecl();
6572     if (RD->hasFlexibleArrayMember())
6573       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6574 
6575     // The structure is passed as an unextended integer, a float, or a double.
6576     llvm::Type *PassTy;
6577     if (isFPArgumentType(SingleElementTy)) {
6578       assert(Size == 32 || Size == 64);
6579       if (Size == 32)
6580         PassTy = llvm::Type::getFloatTy(getVMContext());
6581       else
6582         PassTy = llvm::Type::getDoubleTy(getVMContext());
6583     } else
6584       PassTy = llvm::IntegerType::get(getVMContext(), Size);
6585     return ABIArgInfo::getDirect(PassTy);
6586   }
6587 
6588   // Non-structure compounds are passed indirectly.
6589   if (isCompoundType(Ty))
6590     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6591 
6592   return ABIArgInfo::getDirect(nullptr);
6593 }
6594 
6595 //===----------------------------------------------------------------------===//
6596 // MSP430 ABI Implementation
6597 //===----------------------------------------------------------------------===//
6598 
6599 namespace {
6600 
6601 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6602 public:
6603   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6604     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
6605   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6606                            CodeGen::CodeGenModule &M,
6607                            ForDefinition_t IsForDefinition) const override;
6608 };
6609 
6610 }
6611 
6612 void MSP430TargetCodeGenInfo::setTargetAttributes(
6613     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
6614     ForDefinition_t IsForDefinition) const {
6615   if (!IsForDefinition)
6616     return;
6617   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
6618     if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6619       // Handle 'interrupt' attribute:
6620       llvm::Function *F = cast<llvm::Function>(GV);
6621 
6622       // Step 1: Set ISR calling convention.
6623       F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6624 
6625       // Step 2: Add attributes goodness.
6626       F->addFnAttr(llvm::Attribute::NoInline);
6627 
6628       // Step 3: Emit ISR vector alias.
6629       unsigned Num = attr->getNumber() / 2;
6630       llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6631                                 "__isr_" + Twine(Num), F);
6632     }
6633   }
6634 }
6635 
6636 //===----------------------------------------------------------------------===//
6637 // MIPS ABI Implementation.  This works for both little-endian and
6638 // big-endian variants.
6639 //===----------------------------------------------------------------------===//
6640 
6641 namespace {
6642 class MipsABIInfo : public ABIInfo {
6643   bool IsO32;
6644   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6645   void CoerceToIntArgs(uint64_t TySize,
6646                        SmallVectorImpl<llvm::Type *> &ArgList) const;
6647   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
6648   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
6649   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
6650 public:
6651   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
6652     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
6653     StackAlignInBytes(IsO32 ? 8 : 16) {}
6654 
6655   ABIArgInfo classifyReturnType(QualType RetTy) const;
6656   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
6657   void computeInfo(CGFunctionInfo &FI) const override;
6658   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6659                     QualType Ty) const override;
6660   bool shouldSignExtUnsignedType(QualType Ty) const override;
6661 };
6662 
6663 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
6664   unsigned SizeOfUnwindException;
6665 public:
6666   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6667     : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
6668       SizeOfUnwindException(IsO32 ? 24 : 32) {}
6669 
6670   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
6671     return 29;
6672   }
6673 
6674   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6675                            CodeGen::CodeGenModule &CGM,
6676                            ForDefinition_t IsForDefinition) const override {
6677     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6678     if (!FD) return;
6679     llvm::Function *Fn = cast<llvm::Function>(GV);
6680 
6681     if (FD->hasAttr<MipsLongCallAttr>())
6682       Fn->addFnAttr("long-call");
6683     else if (FD->hasAttr<MipsShortCallAttr>())
6684       Fn->addFnAttr("short-call");
6685 
6686     // Other attributes do not have a meaning for declarations.
6687     if (!IsForDefinition)
6688       return;
6689 
6690     if (FD->hasAttr<Mips16Attr>()) {
6691       Fn->addFnAttr("mips16");
6692     }
6693     else if (FD->hasAttr<NoMips16Attr>()) {
6694       Fn->addFnAttr("nomips16");
6695     }
6696 
6697     if (FD->hasAttr<MicroMipsAttr>())
6698       Fn->addFnAttr("micromips");
6699     else if (FD->hasAttr<NoMicroMipsAttr>())
6700       Fn->addFnAttr("nomicromips");
6701 
6702     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6703     if (!Attr)
6704       return;
6705 
6706     const char *Kind;
6707     switch (Attr->getInterrupt()) {
6708     case MipsInterruptAttr::eic:     Kind = "eic"; break;
6709     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
6710     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
6711     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
6712     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
6713     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
6714     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
6715     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
6716     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
6717     }
6718 
6719     Fn->addFnAttr("interrupt", Kind);
6720 
6721   }
6722 
6723   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6724                                llvm::Value *Address) const override;
6725 
6726   unsigned getSizeOfUnwindException() const override {
6727     return SizeOfUnwindException;
6728   }
6729 };
6730 }
6731 
6732 void MipsABIInfo::CoerceToIntArgs(
6733     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
6734   llvm::IntegerType *IntTy =
6735     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
6736 
6737   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6738   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6739     ArgList.push_back(IntTy);
6740 
6741   // If necessary, add one more integer type to ArgList.
6742   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6743 
6744   if (R)
6745     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
6746 }
6747 
6748 // In N32/64, an aligned double precision floating point field is passed in
6749 // a register.
6750 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
6751   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6752 
6753   if (IsO32) {
6754     CoerceToIntArgs(TySize, ArgList);
6755     return llvm::StructType::get(getVMContext(), ArgList);
6756   }
6757 
6758   if (Ty->isComplexType())
6759     return CGT.ConvertType(Ty);
6760 
6761   const RecordType *RT = Ty->getAs<RecordType>();
6762 
6763   // Unions/vectors are passed in integer registers.
6764   if (!RT || !RT->isStructureOrClassType()) {
6765     CoerceToIntArgs(TySize, ArgList);
6766     return llvm::StructType::get(getVMContext(), ArgList);
6767   }
6768 
6769   const RecordDecl *RD = RT->getDecl();
6770   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6771   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
6772 
6773   uint64_t LastOffset = 0;
6774   unsigned idx = 0;
6775   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6776 
6777   // Iterate over fields in the struct/class and check if there are any aligned
6778   // double fields.
6779   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6780        i != e; ++i, ++idx) {
6781     const QualType Ty = i->getType();
6782     const BuiltinType *BT = Ty->getAs<BuiltinType>();
6783 
6784     if (!BT || BT->getKind() != BuiltinType::Double)
6785       continue;
6786 
6787     uint64_t Offset = Layout.getFieldOffset(idx);
6788     if (Offset % 64) // Ignore doubles that are not aligned.
6789       continue;
6790 
6791     // Add ((Offset - LastOffset) / 64) args of type i64.
6792     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6793       ArgList.push_back(I64);
6794 
6795     // Add double type.
6796     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6797     LastOffset = Offset + 64;
6798   }
6799 
6800   CoerceToIntArgs(TySize - LastOffset, IntArgList);
6801   ArgList.append(IntArgList.begin(), IntArgList.end());
6802 
6803   return llvm::StructType::get(getVMContext(), ArgList);
6804 }
6805 
6806 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6807                                         uint64_t Offset) const {
6808   if (OrigOffset + MinABIStackAlignInBytes > Offset)
6809     return nullptr;
6810 
6811   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
6812 }
6813 
6814 ABIArgInfo
6815 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
6816   Ty = useFirstFieldIfTransparentUnion(Ty);
6817 
6818   uint64_t OrigOffset = Offset;
6819   uint64_t TySize = getContext().getTypeSize(Ty);
6820   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
6821 
6822   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6823                    (uint64_t)StackAlignInBytes);
6824   unsigned CurrOffset = llvm::alignTo(Offset, Align);
6825   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
6826 
6827   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
6828     // Ignore empty aggregates.
6829     if (TySize == 0)
6830       return ABIArgInfo::getIgnore();
6831 
6832     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6833       Offset = OrigOffset + MinABIStackAlignInBytes;
6834       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6835     }
6836 
6837     // If we have reached here, aggregates are passed directly by coercing to
6838     // another structure type. Padding is inserted if the offset of the
6839     // aggregate is unaligned.
6840     ABIArgInfo ArgInfo =
6841         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6842                               getPaddingType(OrigOffset, CurrOffset));
6843     ArgInfo.setInReg(true);
6844     return ArgInfo;
6845   }
6846 
6847   // Treat an enum type as its underlying type.
6848   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6849     Ty = EnumTy->getDecl()->getIntegerType();
6850 
6851   // All integral types are promoted to the GPR width.
6852   if (Ty->isIntegralOrEnumerationType())
6853     return ABIArgInfo::getExtend();
6854 
6855   return ABIArgInfo::getDirect(
6856       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
6857 }
6858 
6859 llvm::Type*
6860 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
6861   const RecordType *RT = RetTy->getAs<RecordType>();
6862   SmallVector<llvm::Type*, 8> RTList;
6863 
6864   if (RT && RT->isStructureOrClassType()) {
6865     const RecordDecl *RD = RT->getDecl();
6866     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6867     unsigned FieldCnt = Layout.getFieldCount();
6868 
6869     // N32/64 returns struct/classes in floating point registers if the
6870     // following conditions are met:
6871     // 1. The size of the struct/class is no larger than 128-bit.
6872     // 2. The struct/class has one or two fields all of which are floating
6873     //    point types.
6874     // 3. The offset of the first field is zero (this follows what gcc does).
6875     //
6876     // Any other composite results are returned in integer registers.
6877     //
6878     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6879       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6880       for (; b != e; ++b) {
6881         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
6882 
6883         if (!BT || !BT->isFloatingPoint())
6884           break;
6885 
6886         RTList.push_back(CGT.ConvertType(b->getType()));
6887       }
6888 
6889       if (b == e)
6890         return llvm::StructType::get(getVMContext(), RTList,
6891                                      RD->hasAttr<PackedAttr>());
6892 
6893       RTList.clear();
6894     }
6895   }
6896 
6897   CoerceToIntArgs(Size, RTList);
6898   return llvm::StructType::get(getVMContext(), RTList);
6899 }
6900 
6901 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
6902   uint64_t Size = getContext().getTypeSize(RetTy);
6903 
6904   if (RetTy->isVoidType())
6905     return ABIArgInfo::getIgnore();
6906 
6907   // O32 doesn't treat zero-sized structs differently from other structs.
6908   // However, N32/N64 ignores zero sized return values.
6909   if (!IsO32 && Size == 0)
6910     return ABIArgInfo::getIgnore();
6911 
6912   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
6913     if (Size <= 128) {
6914       if (RetTy->isAnyComplexType())
6915         return ABIArgInfo::getDirect();
6916 
6917       // O32 returns integer vectors in registers and N32/N64 returns all small
6918       // aggregates in registers.
6919       if (!IsO32 ||
6920           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6921         ABIArgInfo ArgInfo =
6922             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6923         ArgInfo.setInReg(true);
6924         return ArgInfo;
6925       }
6926     }
6927 
6928     return getNaturalAlignIndirect(RetTy);
6929   }
6930 
6931   // Treat an enum type as its underlying type.
6932   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6933     RetTy = EnumTy->getDecl()->getIntegerType();
6934 
6935   return (RetTy->isPromotableIntegerType() ?
6936           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6937 }
6938 
6939 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
6940   ABIArgInfo &RetInfo = FI.getReturnInfo();
6941   if (!getCXXABI().classifyReturnType(FI))
6942     RetInfo = classifyReturnType(FI.getReturnType());
6943 
6944   // Check if a pointer to an aggregate is passed as a hidden argument.
6945   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
6946 
6947   for (auto &I : FI.arguments())
6948     I.info = classifyArgumentType(I.type, Offset);
6949 }
6950 
6951 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6952                                QualType OrigTy) const {
6953   QualType Ty = OrigTy;
6954 
6955   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6956   // Pointers are also promoted in the same way but this only matters for N32.
6957   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
6958   unsigned PtrWidth = getTarget().getPointerWidth(0);
6959   bool DidPromote = false;
6960   if ((Ty->isIntegerType() &&
6961           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
6962       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
6963     DidPromote = true;
6964     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6965                                             Ty->isSignedIntegerType());
6966   }
6967 
6968   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6969 
6970   // The alignment of things in the argument area is never larger than
6971   // StackAlignInBytes.
6972   TyInfo.second =
6973     std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6974 
6975   // MinABIStackAlignInBytes is the size of argument slots on the stack.
6976   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6977 
6978   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6979                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6980 
6981 
6982   // If there was a promotion, "unpromote" into a temporary.
6983   // TODO: can we just use a pointer into a subset of the original slot?
6984   if (DidPromote) {
6985     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6986     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6987 
6988     // Truncate down to the right width.
6989     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6990                                                  : CGF.IntPtrTy);
6991     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6992     if (OrigTy->isPointerType())
6993       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6994 
6995     CGF.Builder.CreateStore(V, Temp);
6996     Addr = Temp;
6997   }
6998 
6999   return Addr;
7000 }
7001 
7002 bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
7003   int TySize = getContext().getTypeSize(Ty);
7004 
7005   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7006   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
7007     return true;
7008 
7009   return false;
7010 }
7011 
7012 bool
7013 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7014                                                llvm::Value *Address) const {
7015   // This information comes from gcc's implementation, which seems to
7016   // as canonical as it gets.
7017 
7018   // Everything on MIPS is 4 bytes.  Double-precision FP registers
7019   // are aliased to pairs of single-precision FP registers.
7020   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
7021 
7022   // 0-31 are the general purpose registers, $0 - $31.
7023   // 32-63 are the floating-point registers, $f0 - $f31.
7024   // 64 and 65 are the multiply/divide registers, $hi and $lo.
7025   // 66 is the (notional, I think) register for signal-handler return.
7026   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
7027 
7028   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7029   // They are one bit wide and ignored here.
7030 
7031   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7032   // (coprocessor 1 is the FP unit)
7033   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7034   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7035   // 176-181 are the DSP accumulator registers.
7036   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
7037   return false;
7038 }
7039 
7040 //===----------------------------------------------------------------------===//
7041 // AVR ABI Implementation.
7042 //===----------------------------------------------------------------------===//
7043 
7044 namespace {
7045 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7046 public:
7047   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7048     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
7049 
7050   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7051                            CodeGen::CodeGenModule &CGM,
7052                            ForDefinition_t IsForDefinition) const override {
7053     if (!IsForDefinition)
7054       return;
7055     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7056     if (!FD) return;
7057     auto *Fn = cast<llvm::Function>(GV);
7058 
7059     if (FD->getAttr<AVRInterruptAttr>())
7060       Fn->addFnAttr("interrupt");
7061 
7062     if (FD->getAttr<AVRSignalAttr>())
7063       Fn->addFnAttr("signal");
7064   }
7065 };
7066 }
7067 
7068 //===----------------------------------------------------------------------===//
7069 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
7070 // Currently subclassed only to implement custom OpenCL C function attribute
7071 // handling.
7072 //===----------------------------------------------------------------------===//
7073 
7074 namespace {
7075 
7076 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
7077 public:
7078   TCETargetCodeGenInfo(CodeGenTypes &CGT)
7079     : DefaultTargetCodeGenInfo(CGT) {}
7080 
7081   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7082                            CodeGen::CodeGenModule &M,
7083                            ForDefinition_t IsForDefinition) const override;
7084 };
7085 
7086 void TCETargetCodeGenInfo::setTargetAttributes(
7087     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
7088     ForDefinition_t IsForDefinition) const {
7089   if (!IsForDefinition)
7090     return;
7091   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7092   if (!FD) return;
7093 
7094   llvm::Function *F = cast<llvm::Function>(GV);
7095 
7096   if (M.getLangOpts().OpenCL) {
7097     if (FD->hasAttr<OpenCLKernelAttr>()) {
7098       // OpenCL C Kernel functions are not subject to inlining
7099       F->addFnAttr(llvm::Attribute::NoInline);
7100       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7101       if (Attr) {
7102         // Convert the reqd_work_group_size() attributes to metadata.
7103         llvm::LLVMContext &Context = F->getContext();
7104         llvm::NamedMDNode *OpenCLMetadata =
7105             M.getModule().getOrInsertNamedMetadata(
7106                 "opencl.kernel_wg_size_info");
7107 
7108         SmallVector<llvm::Metadata *, 5> Operands;
7109         Operands.push_back(llvm::ConstantAsMetadata::get(F));
7110 
7111         Operands.push_back(
7112             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7113                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
7114         Operands.push_back(
7115             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7116                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
7117         Operands.push_back(
7118             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7119                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
7120 
7121         // Add a boolean constant operand for "required" (true) or "hint"
7122         // (false) for implementing the work_group_size_hint attr later.
7123         // Currently always true as the hint is not yet implemented.
7124         Operands.push_back(
7125             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
7126         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
7127       }
7128     }
7129   }
7130 }
7131 
7132 }
7133 
7134 //===----------------------------------------------------------------------===//
7135 // Hexagon ABI Implementation
7136 //===----------------------------------------------------------------------===//
7137 
7138 namespace {
7139 
7140 class HexagonABIInfo : public ABIInfo {
7141 
7142 
7143 public:
7144   HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7145 
7146 private:
7147 
7148   ABIArgInfo classifyReturnType(QualType RetTy) const;
7149   ABIArgInfo classifyArgumentType(QualType RetTy) const;
7150 
7151   void computeInfo(CGFunctionInfo &FI) const override;
7152 
7153   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7154                     QualType Ty) const override;
7155 };
7156 
7157 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
7158 public:
7159   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
7160     :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
7161 
7162   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
7163     return 29;
7164   }
7165 };
7166 
7167 }
7168 
7169 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
7170   if (!getCXXABI().classifyReturnType(FI))
7171     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7172   for (auto &I : FI.arguments())
7173     I.info = classifyArgumentType(I.type);
7174 }
7175 
7176 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
7177   if (!isAggregateTypeForABI(Ty)) {
7178     // Treat an enum type as its underlying type.
7179     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7180       Ty = EnumTy->getDecl()->getIntegerType();
7181 
7182     return (Ty->isPromotableIntegerType() ?
7183             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7184   }
7185 
7186   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7187     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7188 
7189   // Ignore empty records.
7190   if (isEmptyRecord(getContext(), Ty, true))
7191     return ABIArgInfo::getIgnore();
7192 
7193   uint64_t Size = getContext().getTypeSize(Ty);
7194   if (Size > 64)
7195     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
7196     // Pass in the smallest viable integer type.
7197   else if (Size > 32)
7198       return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7199   else if (Size > 16)
7200       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7201   else if (Size > 8)
7202       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7203   else
7204       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7205 }
7206 
7207 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
7208   if (RetTy->isVoidType())
7209     return ABIArgInfo::getIgnore();
7210 
7211   // Large vector types should be returned via memory.
7212   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
7213     return getNaturalAlignIndirect(RetTy);
7214 
7215   if (!isAggregateTypeForABI(RetTy)) {
7216     // Treat an enum type as its underlying type.
7217     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7218       RetTy = EnumTy->getDecl()->getIntegerType();
7219 
7220     return (RetTy->isPromotableIntegerType() ?
7221             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7222   }
7223 
7224   if (isEmptyRecord(getContext(), RetTy, true))
7225     return ABIArgInfo::getIgnore();
7226 
7227   // Aggregates <= 8 bytes are returned in r0; other aggregates
7228   // are returned indirectly.
7229   uint64_t Size = getContext().getTypeSize(RetTy);
7230   if (Size <= 64) {
7231     // Return in the smallest viable integer type.
7232     if (Size <= 8)
7233       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7234     if (Size <= 16)
7235       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7236     if (Size <= 32)
7237       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7238     return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7239   }
7240 
7241   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
7242 }
7243 
7244 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7245                                   QualType Ty) const {
7246   // FIXME: Someone needs to audit that this handle alignment correctly.
7247   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7248                           getContext().getTypeInfoInChars(Ty),
7249                           CharUnits::fromQuantity(4),
7250                           /*AllowHigherAlign*/ true);
7251 }
7252 
7253 //===----------------------------------------------------------------------===//
7254 // Lanai ABI Implementation
7255 //===----------------------------------------------------------------------===//
7256 
7257 namespace {
7258 class LanaiABIInfo : public DefaultABIInfo {
7259 public:
7260   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7261 
7262   bool shouldUseInReg(QualType Ty, CCState &State) const;
7263 
7264   void computeInfo(CGFunctionInfo &FI) const override {
7265     CCState State(FI.getCallingConvention());
7266     // Lanai uses 4 registers to pass arguments unless the function has the
7267     // regparm attribute set.
7268     if (FI.getHasRegParm()) {
7269       State.FreeRegs = FI.getRegParm();
7270     } else {
7271       State.FreeRegs = 4;
7272     }
7273 
7274     if (!getCXXABI().classifyReturnType(FI))
7275       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7276     for (auto &I : FI.arguments())
7277       I.info = classifyArgumentType(I.type, State);
7278   }
7279 
7280   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
7281   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
7282 };
7283 } // end anonymous namespace
7284 
7285 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
7286   unsigned Size = getContext().getTypeSize(Ty);
7287   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
7288 
7289   if (SizeInRegs == 0)
7290     return false;
7291 
7292   if (SizeInRegs > State.FreeRegs) {
7293     State.FreeRegs = 0;
7294     return false;
7295   }
7296 
7297   State.FreeRegs -= SizeInRegs;
7298 
7299   return true;
7300 }
7301 
7302 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7303                                            CCState &State) const {
7304   if (!ByVal) {
7305     if (State.FreeRegs) {
7306       --State.FreeRegs; // Non-byval indirects just use one pointer.
7307       return getNaturalAlignIndirectInReg(Ty);
7308     }
7309     return getNaturalAlignIndirect(Ty, false);
7310   }
7311 
7312   // Compute the byval alignment.
7313   const unsigned MinABIStackAlignInBytes = 4;
7314   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
7315   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
7316                                  /*Realign=*/TypeAlign >
7317                                      MinABIStackAlignInBytes);
7318 }
7319 
7320 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
7321                                               CCState &State) const {
7322   // Check with the C++ ABI first.
7323   const RecordType *RT = Ty->getAs<RecordType>();
7324   if (RT) {
7325     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
7326     if (RAA == CGCXXABI::RAA_Indirect) {
7327       return getIndirectResult(Ty, /*ByVal=*/false, State);
7328     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
7329       return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
7330     }
7331   }
7332 
7333   if (isAggregateTypeForABI(Ty)) {
7334     // Structures with flexible arrays are always indirect.
7335     if (RT && RT->getDecl()->hasFlexibleArrayMember())
7336       return getIndirectResult(Ty, /*ByVal=*/true, State);
7337 
7338     // Ignore empty structs/unions.
7339     if (isEmptyRecord(getContext(), Ty, true))
7340       return ABIArgInfo::getIgnore();
7341 
7342     llvm::LLVMContext &LLVMContext = getVMContext();
7343     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
7344     if (SizeInRegs <= State.FreeRegs) {
7345       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
7346       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
7347       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
7348       State.FreeRegs -= SizeInRegs;
7349       return ABIArgInfo::getDirectInReg(Result);
7350     } else {
7351       State.FreeRegs = 0;
7352     }
7353     return getIndirectResult(Ty, true, State);
7354   }
7355 
7356   // Treat an enum type as its underlying type.
7357   if (const auto *EnumTy = Ty->getAs<EnumType>())
7358     Ty = EnumTy->getDecl()->getIntegerType();
7359 
7360   bool InReg = shouldUseInReg(Ty, State);
7361   if (Ty->isPromotableIntegerType()) {
7362     if (InReg)
7363       return ABIArgInfo::getDirectInReg();
7364     return ABIArgInfo::getExtend();
7365   }
7366   if (InReg)
7367     return ABIArgInfo::getDirectInReg();
7368   return ABIArgInfo::getDirect();
7369 }
7370 
7371 namespace {
7372 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
7373 public:
7374   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7375       : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
7376 };
7377 }
7378 
7379 //===----------------------------------------------------------------------===//
7380 // AMDGPU ABI Implementation
7381 //===----------------------------------------------------------------------===//
7382 
7383 namespace {
7384 
7385 class AMDGPUABIInfo final : public DefaultABIInfo {
7386 private:
7387   static const unsigned MaxNumRegsForArgsRet = 16;
7388 
7389   unsigned numRegsForType(QualType Ty) const;
7390 
7391   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
7392   bool isHomogeneousAggregateSmallEnough(const Type *Base,
7393                                          uint64_t Members) const override;
7394 
7395 public:
7396   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
7397     DefaultABIInfo(CGT) {}
7398 
7399   ABIArgInfo classifyReturnType(QualType RetTy) const;
7400   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
7401   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
7402 
7403   void computeInfo(CGFunctionInfo &FI) const override;
7404 };
7405 
7406 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7407   return true;
7408 }
7409 
7410 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
7411   const Type *Base, uint64_t Members) const {
7412   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
7413 
7414   // Homogeneous Aggregates may occupy at most 16 registers.
7415   return Members * NumRegs <= MaxNumRegsForArgsRet;
7416 }
7417 
7418 /// Estimate number of registers the type will use when passed in registers.
7419 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
7420   unsigned NumRegs = 0;
7421 
7422   if (const VectorType *VT = Ty->getAs<VectorType>()) {
7423     // Compute from the number of elements. The reported size is based on the
7424     // in-memory size, which includes the padding 4th element for 3-vectors.
7425     QualType EltTy = VT->getElementType();
7426     unsigned EltSize = getContext().getTypeSize(EltTy);
7427 
7428     // 16-bit element vectors should be passed as packed.
7429     if (EltSize == 16)
7430       return (VT->getNumElements() + 1) / 2;
7431 
7432     unsigned EltNumRegs = (EltSize + 31) / 32;
7433     return EltNumRegs * VT->getNumElements();
7434   }
7435 
7436   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7437     const RecordDecl *RD = RT->getDecl();
7438     assert(!RD->hasFlexibleArrayMember());
7439 
7440     for (const FieldDecl *Field : RD->fields()) {
7441       QualType FieldTy = Field->getType();
7442       NumRegs += numRegsForType(FieldTy);
7443     }
7444 
7445     return NumRegs;
7446   }
7447 
7448   return (getContext().getTypeSize(Ty) + 31) / 32;
7449 }
7450 
7451 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
7452   llvm::CallingConv::ID CC = FI.getCallingConvention();
7453 
7454   if (!getCXXABI().classifyReturnType(FI))
7455     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7456 
7457   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
7458   for (auto &Arg : FI.arguments()) {
7459     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
7460       Arg.info = classifyKernelArgumentType(Arg.type);
7461     } else {
7462       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
7463     }
7464   }
7465 }
7466 
7467 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
7468   if (isAggregateTypeForABI(RetTy)) {
7469     // Records with non-trivial destructors/copy-constructors should not be
7470     // returned by value.
7471     if (!getRecordArgABI(RetTy, getCXXABI())) {
7472       // Ignore empty structs/unions.
7473       if (isEmptyRecord(getContext(), RetTy, true))
7474         return ABIArgInfo::getIgnore();
7475 
7476       // Lower single-element structs to just return a regular value.
7477       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
7478         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7479 
7480       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
7481         const RecordDecl *RD = RT->getDecl();
7482         if (RD->hasFlexibleArrayMember())
7483           return DefaultABIInfo::classifyReturnType(RetTy);
7484       }
7485 
7486       // Pack aggregates <= 4 bytes into single VGPR or pair.
7487       uint64_t Size = getContext().getTypeSize(RetTy);
7488       if (Size <= 16)
7489         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7490 
7491       if (Size <= 32)
7492         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7493 
7494       if (Size <= 64) {
7495         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7496         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7497       }
7498 
7499       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
7500         return ABIArgInfo::getDirect();
7501     }
7502   }
7503 
7504   // Otherwise just do the default thing.
7505   return DefaultABIInfo::classifyReturnType(RetTy);
7506 }
7507 
7508 /// For kernels all parameters are really passed in a special buffer. It doesn't
7509 /// make sense to pass anything byval, so everything must be direct.
7510 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
7511   Ty = useFirstFieldIfTransparentUnion(Ty);
7512 
7513   // TODO: Can we omit empty structs?
7514 
7515   // Coerce single element structs to its element.
7516   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7517     return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7518 
7519   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
7520   // individual elements, which confuses the Clover OpenCL backend; therefore we
7521   // have to set it to false here. Other args of getDirect() are just defaults.
7522   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
7523 }
7524 
7525 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
7526                                                unsigned &NumRegsLeft) const {
7527   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
7528 
7529   Ty = useFirstFieldIfTransparentUnion(Ty);
7530 
7531   if (isAggregateTypeForABI(Ty)) {
7532     // Records with non-trivial destructors/copy-constructors should not be
7533     // passed by value.
7534     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
7535       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7536 
7537     // Ignore empty structs/unions.
7538     if (isEmptyRecord(getContext(), Ty, true))
7539       return ABIArgInfo::getIgnore();
7540 
7541     // Lower single-element structs to just pass a regular value. TODO: We
7542     // could do reasonable-size multiple-element structs too, using getExpand(),
7543     // though watch out for things like bitfields.
7544     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7545       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7546 
7547     if (const RecordType *RT = Ty->getAs<RecordType>()) {
7548       const RecordDecl *RD = RT->getDecl();
7549       if (RD->hasFlexibleArrayMember())
7550         return DefaultABIInfo::classifyArgumentType(Ty);
7551     }
7552 
7553     // Pack aggregates <= 8 bytes into single VGPR or pair.
7554     uint64_t Size = getContext().getTypeSize(Ty);
7555     if (Size <= 64) {
7556       unsigned NumRegs = (Size + 31) / 32;
7557       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
7558 
7559       if (Size <= 16)
7560         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7561 
7562       if (Size <= 32)
7563         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7564 
7565       // XXX: Should this be i64 instead, and should the limit increase?
7566       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7567       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7568     }
7569 
7570     if (NumRegsLeft > 0) {
7571       unsigned NumRegs = numRegsForType(Ty);
7572       if (NumRegsLeft >= NumRegs) {
7573         NumRegsLeft -= NumRegs;
7574         return ABIArgInfo::getDirect();
7575       }
7576     }
7577   }
7578 
7579   // Otherwise just do the default thing.
7580   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
7581   if (!ArgInfo.isIndirect()) {
7582     unsigned NumRegs = numRegsForType(Ty);
7583     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
7584   }
7585 
7586   return ArgInfo;
7587 }
7588 
7589 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
7590 public:
7591   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
7592     : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
7593   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7594                            CodeGen::CodeGenModule &M,
7595                            ForDefinition_t IsForDefinition) const override;
7596   unsigned getOpenCLKernelCallingConv() const override;
7597 
7598   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
7599       llvm::PointerType *T, QualType QT) const override;
7600 
7601   unsigned getASTAllocaAddressSpace() const override {
7602     return LangAS::FirstTargetAddressSpace +
7603            getABIInfo().getDataLayout().getAllocaAddrSpace();
7604   }
7605   unsigned getGlobalVarAddressSpace(CodeGenModule &CGM,
7606                                     const VarDecl *D) const override;
7607   llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
7608                                          llvm::LLVMContext &C) const override;
7609 };
7610 }
7611 
7612 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
7613     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
7614     ForDefinition_t IsForDefinition) const {
7615   if (!IsForDefinition)
7616     return;
7617   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7618   if (!FD)
7619     return;
7620 
7621   llvm::Function *F = cast<llvm::Function>(GV);
7622 
7623   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
7624     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
7625   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
7626   if (ReqdWGS || FlatWGS) {
7627     unsigned Min = FlatWGS ? FlatWGS->getMin() : 0;
7628     unsigned Max = FlatWGS ? FlatWGS->getMax() : 0;
7629     if (ReqdWGS && Min == 0 && Max == 0)
7630       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
7631 
7632     if (Min != 0) {
7633       assert(Min <= Max && "Min must be less than or equal Max");
7634 
7635       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
7636       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
7637     } else
7638       assert(Max == 0 && "Max must be zero");
7639   }
7640 
7641   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
7642     unsigned Min = Attr->getMin();
7643     unsigned Max = Attr->getMax();
7644 
7645     if (Min != 0) {
7646       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
7647 
7648       std::string AttrVal = llvm::utostr(Min);
7649       if (Max != 0)
7650         AttrVal = AttrVal + "," + llvm::utostr(Max);
7651       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
7652     } else
7653       assert(Max == 0 && "Max must be zero");
7654   }
7655 
7656   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
7657     unsigned NumSGPR = Attr->getNumSGPR();
7658 
7659     if (NumSGPR != 0)
7660       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
7661   }
7662 
7663   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
7664     uint32_t NumVGPR = Attr->getNumVGPR();
7665 
7666     if (NumVGPR != 0)
7667       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
7668   }
7669 }
7670 
7671 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7672   return llvm::CallingConv::AMDGPU_KERNEL;
7673 }
7674 
7675 // Currently LLVM assumes null pointers always have value 0,
7676 // which results in incorrectly transformed IR. Therefore, instead of
7677 // emitting null pointers in private and local address spaces, a null
7678 // pointer in generic address space is emitted which is casted to a
7679 // pointer in local or private address space.
7680 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
7681     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
7682     QualType QT) const {
7683   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
7684     return llvm::ConstantPointerNull::get(PT);
7685 
7686   auto &Ctx = CGM.getContext();
7687   auto NPT = llvm::PointerType::get(PT->getElementType(),
7688       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
7689   return llvm::ConstantExpr::getAddrSpaceCast(
7690       llvm::ConstantPointerNull::get(NPT), PT);
7691 }
7692 
7693 unsigned
7694 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
7695                                                   const VarDecl *D) const {
7696   assert(!CGM.getLangOpts().OpenCL &&
7697          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
7698          "Address space agnostic languages only");
7699   unsigned DefaultGlobalAS =
7700       LangAS::FirstTargetAddressSpace +
7701       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
7702   if (!D)
7703     return DefaultGlobalAS;
7704 
7705   unsigned AddrSpace = D->getType().getAddressSpace();
7706   assert(AddrSpace == LangAS::Default ||
7707          AddrSpace >= LangAS::FirstTargetAddressSpace);
7708   if (AddrSpace != LangAS::Default)
7709     return AddrSpace;
7710 
7711   if (CGM.isTypeConstant(D->getType(), false)) {
7712     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
7713       return ConstAS.getValue();
7714   }
7715   return DefaultGlobalAS;
7716 }
7717 
7718 llvm::SyncScope::ID
7719 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S,
7720                                             llvm::LLVMContext &C) const {
7721   StringRef Name;
7722   switch (S) {
7723   case SyncScope::OpenCLWorkGroup:
7724     Name = "workgroup";
7725     break;
7726   case SyncScope::OpenCLDevice:
7727     Name = "agent";
7728     break;
7729   case SyncScope::OpenCLAllSVMDevices:
7730     Name = "";
7731     break;
7732   case SyncScope::OpenCLSubGroup:
7733     Name = "subgroup";
7734   }
7735   return C.getOrInsertSyncScopeID(Name);
7736 }
7737 
7738 //===----------------------------------------------------------------------===//
7739 // SPARC v8 ABI Implementation.
7740 // Based on the SPARC Compliance Definition version 2.4.1.
7741 //
7742 // Ensures that complex values are passed in registers.
7743 //
7744 namespace {
7745 class SparcV8ABIInfo : public DefaultABIInfo {
7746 public:
7747   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7748 
7749 private:
7750   ABIArgInfo classifyReturnType(QualType RetTy) const;
7751   void computeInfo(CGFunctionInfo &FI) const override;
7752 };
7753 } // end anonymous namespace
7754 
7755 
7756 ABIArgInfo
7757 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
7758   if (Ty->isAnyComplexType()) {
7759     return ABIArgInfo::getDirect();
7760   }
7761   else {
7762     return DefaultABIInfo::classifyReturnType(Ty);
7763   }
7764 }
7765 
7766 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7767 
7768   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7769   for (auto &Arg : FI.arguments())
7770     Arg.info = classifyArgumentType(Arg.type);
7771 }
7772 
7773 namespace {
7774 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
7775 public:
7776   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
7777     : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
7778 };
7779 } // end anonymous namespace
7780 
7781 //===----------------------------------------------------------------------===//
7782 // SPARC v9 ABI Implementation.
7783 // Based on the SPARC Compliance Definition version 2.4.1.
7784 //
7785 // Function arguments a mapped to a nominal "parameter array" and promoted to
7786 // registers depending on their type. Each argument occupies 8 or 16 bytes in
7787 // the array, structs larger than 16 bytes are passed indirectly.
7788 //
7789 // One case requires special care:
7790 //
7791 //   struct mixed {
7792 //     int i;
7793 //     float f;
7794 //   };
7795 //
7796 // When a struct mixed is passed by value, it only occupies 8 bytes in the
7797 // parameter array, but the int is passed in an integer register, and the float
7798 // is passed in a floating point register. This is represented as two arguments
7799 // with the LLVM IR inreg attribute:
7800 //
7801 //   declare void f(i32 inreg %i, float inreg %f)
7802 //
7803 // The code generator will only allocate 4 bytes from the parameter array for
7804 // the inreg arguments. All other arguments are allocated a multiple of 8
7805 // bytes.
7806 //
7807 namespace {
7808 class SparcV9ABIInfo : public ABIInfo {
7809 public:
7810   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7811 
7812 private:
7813   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
7814   void computeInfo(CGFunctionInfo &FI) const override;
7815   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7816                     QualType Ty) const override;
7817 
7818   // Coercion type builder for structs passed in registers. The coercion type
7819   // serves two purposes:
7820   //
7821   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
7822   //    in registers.
7823   // 2. Expose aligned floating point elements as first-level elements, so the
7824   //    code generator knows to pass them in floating point registers.
7825   //
7826   // We also compute the InReg flag which indicates that the struct contains
7827   // aligned 32-bit floats.
7828   //
7829   struct CoerceBuilder {
7830     llvm::LLVMContext &Context;
7831     const llvm::DataLayout &DL;
7832     SmallVector<llvm::Type*, 8> Elems;
7833     uint64_t Size;
7834     bool InReg;
7835 
7836     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7837       : Context(c), DL(dl), Size(0), InReg(false) {}
7838 
7839     // Pad Elems with integers until Size is ToSize.
7840     void pad(uint64_t ToSize) {
7841       assert(ToSize >= Size && "Cannot remove elements");
7842       if (ToSize == Size)
7843         return;
7844 
7845       // Finish the current 64-bit word.
7846       uint64_t Aligned = llvm::alignTo(Size, 64);
7847       if (Aligned > Size && Aligned <= ToSize) {
7848         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7849         Size = Aligned;
7850       }
7851 
7852       // Add whole 64-bit words.
7853       while (Size + 64 <= ToSize) {
7854         Elems.push_back(llvm::Type::getInt64Ty(Context));
7855         Size += 64;
7856       }
7857 
7858       // Final in-word padding.
7859       if (Size < ToSize) {
7860         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7861         Size = ToSize;
7862       }
7863     }
7864 
7865     // Add a floating point element at Offset.
7866     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7867       // Unaligned floats are treated as integers.
7868       if (Offset % Bits)
7869         return;
7870       // The InReg flag is only required if there are any floats < 64 bits.
7871       if (Bits < 64)
7872         InReg = true;
7873       pad(Offset);
7874       Elems.push_back(Ty);
7875       Size = Offset + Bits;
7876     }
7877 
7878     // Add a struct type to the coercion type, starting at Offset (in bits).
7879     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7880       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7881       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7882         llvm::Type *ElemTy = StrTy->getElementType(i);
7883         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7884         switch (ElemTy->getTypeID()) {
7885         case llvm::Type::StructTyID:
7886           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7887           break;
7888         case llvm::Type::FloatTyID:
7889           addFloat(ElemOffset, ElemTy, 32);
7890           break;
7891         case llvm::Type::DoubleTyID:
7892           addFloat(ElemOffset, ElemTy, 64);
7893           break;
7894         case llvm::Type::FP128TyID:
7895           addFloat(ElemOffset, ElemTy, 128);
7896           break;
7897         case llvm::Type::PointerTyID:
7898           if (ElemOffset % 64 == 0) {
7899             pad(ElemOffset);
7900             Elems.push_back(ElemTy);
7901             Size += 64;
7902           }
7903           break;
7904         default:
7905           break;
7906         }
7907       }
7908     }
7909 
7910     // Check if Ty is a usable substitute for the coercion type.
7911     bool isUsableType(llvm::StructType *Ty) const {
7912       return llvm::makeArrayRef(Elems) == Ty->elements();
7913     }
7914 
7915     // Get the coercion type as a literal struct type.
7916     llvm::Type *getType() const {
7917       if (Elems.size() == 1)
7918         return Elems.front();
7919       else
7920         return llvm::StructType::get(Context, Elems);
7921     }
7922   };
7923 };
7924 } // end anonymous namespace
7925 
7926 ABIArgInfo
7927 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7928   if (Ty->isVoidType())
7929     return ABIArgInfo::getIgnore();
7930 
7931   uint64_t Size = getContext().getTypeSize(Ty);
7932 
7933   // Anything too big to fit in registers is passed with an explicit indirect
7934   // pointer / sret pointer.
7935   if (Size > SizeLimit)
7936     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
7937 
7938   // Treat an enum type as its underlying type.
7939   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7940     Ty = EnumTy->getDecl()->getIntegerType();
7941 
7942   // Integer types smaller than a register are extended.
7943   if (Size < 64 && Ty->isIntegerType())
7944     return ABIArgInfo::getExtend();
7945 
7946   // Other non-aggregates go in registers.
7947   if (!isAggregateTypeForABI(Ty))
7948     return ABIArgInfo::getDirect();
7949 
7950   // If a C++ object has either a non-trivial copy constructor or a non-trivial
7951   // destructor, it is passed with an explicit indirect pointer / sret pointer.
7952   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7953     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7954 
7955   // This is a small aggregate type that should be passed in registers.
7956   // Build a coercion type from the LLVM struct type.
7957   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7958   if (!StrTy)
7959     return ABIArgInfo::getDirect();
7960 
7961   CoerceBuilder CB(getVMContext(), getDataLayout());
7962   CB.addStruct(0, StrTy);
7963   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
7964 
7965   // Try to use the original type for coercion.
7966   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7967 
7968   if (CB.InReg)
7969     return ABIArgInfo::getDirectInReg(CoerceTy);
7970   else
7971     return ABIArgInfo::getDirect(CoerceTy);
7972 }
7973 
7974 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7975                                   QualType Ty) const {
7976   ABIArgInfo AI = classifyType(Ty, 16 * 8);
7977   llvm::Type *ArgTy = CGT.ConvertType(Ty);
7978   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7979     AI.setCoerceToType(ArgTy);
7980 
7981   CharUnits SlotSize = CharUnits::fromQuantity(8);
7982 
7983   CGBuilderTy &Builder = CGF.Builder;
7984   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
7985   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
7986 
7987   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
7988 
7989   Address ArgAddr = Address::invalid();
7990   CharUnits Stride;
7991   switch (AI.getKind()) {
7992   case ABIArgInfo::Expand:
7993   case ABIArgInfo::CoerceAndExpand:
7994   case ABIArgInfo::InAlloca:
7995     llvm_unreachable("Unsupported ABI kind for va_arg");
7996 
7997   case ABIArgInfo::Extend: {
7998     Stride = SlotSize;
7999     CharUnits Offset = SlotSize - TypeInfo.first;
8000     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
8001     break;
8002   }
8003 
8004   case ABIArgInfo::Direct: {
8005     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
8006     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
8007     ArgAddr = Addr;
8008     break;
8009   }
8010 
8011   case ABIArgInfo::Indirect:
8012     Stride = SlotSize;
8013     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
8014     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
8015                       TypeInfo.second);
8016     break;
8017 
8018   case ABIArgInfo::Ignore:
8019     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
8020   }
8021 
8022   // Update VAList.
8023   llvm::Value *NextPtr =
8024     Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
8025   Builder.CreateStore(NextPtr, VAListAddr);
8026 
8027   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
8028 }
8029 
8030 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
8031   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
8032   for (auto &I : FI.arguments())
8033     I.info = classifyType(I.type, 16 * 8);
8034 }
8035 
8036 namespace {
8037 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
8038 public:
8039   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
8040     : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
8041 
8042   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8043     return 14;
8044   }
8045 
8046   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8047                                llvm::Value *Address) const override;
8048 };
8049 } // end anonymous namespace
8050 
8051 bool
8052 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8053                                                 llvm::Value *Address) const {
8054   // This is calculated from the LLVM and GCC tables and verified
8055   // against gcc output.  AFAIK all ABIs use the same encoding.
8056 
8057   CodeGen::CGBuilderTy &Builder = CGF.Builder;
8058 
8059   llvm::IntegerType *i8 = CGF.Int8Ty;
8060   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
8061   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
8062 
8063   // 0-31: the 8-byte general-purpose registers
8064   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
8065 
8066   // 32-63: f0-31, the 4-byte floating-point registers
8067   AssignToArrayRange(Builder, Address, Four8, 32, 63);
8068 
8069   //   Y   = 64
8070   //   PSR = 65
8071   //   WIM = 66
8072   //   TBR = 67
8073   //   PC  = 68
8074   //   NPC = 69
8075   //   FSR = 70
8076   //   CSR = 71
8077   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
8078 
8079   // 72-87: d0-15, the 8-byte floating-point registers
8080   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
8081 
8082   return false;
8083 }
8084 
8085 
8086 //===----------------------------------------------------------------------===//
8087 // XCore ABI Implementation
8088 //===----------------------------------------------------------------------===//
8089 
8090 namespace {
8091 
8092 /// A SmallStringEnc instance is used to build up the TypeString by passing
8093 /// it by reference between functions that append to it.
8094 typedef llvm::SmallString<128> SmallStringEnc;
8095 
8096 /// TypeStringCache caches the meta encodings of Types.
8097 ///
8098 /// The reason for caching TypeStrings is two fold:
8099 ///   1. To cache a type's encoding for later uses;
8100 ///   2. As a means to break recursive member type inclusion.
8101 ///
8102 /// A cache Entry can have a Status of:
8103 ///   NonRecursive:   The type encoding is not recursive;
8104 ///   Recursive:      The type encoding is recursive;
8105 ///   Incomplete:     An incomplete TypeString;
8106 ///   IncompleteUsed: An incomplete TypeString that has been used in a
8107 ///                   Recursive type encoding.
8108 ///
8109 /// A NonRecursive entry will have all of its sub-members expanded as fully
8110 /// as possible. Whilst it may contain types which are recursive, the type
8111 /// itself is not recursive and thus its encoding may be safely used whenever
8112 /// the type is encountered.
8113 ///
8114 /// A Recursive entry will have all of its sub-members expanded as fully as
8115 /// possible. The type itself is recursive and it may contain other types which
8116 /// are recursive. The Recursive encoding must not be used during the expansion
8117 /// of a recursive type's recursive branch. For simplicity the code uses
8118 /// IncompleteCount to reject all usage of Recursive encodings for member types.
8119 ///
8120 /// An Incomplete entry is always a RecordType and only encodes its
8121 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
8122 /// are placed into the cache during type expansion as a means to identify and
8123 /// handle recursive inclusion of types as sub-members. If there is recursion
8124 /// the entry becomes IncompleteUsed.
8125 ///
8126 /// During the expansion of a RecordType's members:
8127 ///
8128 ///   If the cache contains a NonRecursive encoding for the member type, the
8129 ///   cached encoding is used;
8130 ///
8131 ///   If the cache contains a Recursive encoding for the member type, the
8132 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
8133 ///
8134 ///   If the member is a RecordType, an Incomplete encoding is placed into the
8135 ///   cache to break potential recursive inclusion of itself as a sub-member;
8136 ///
8137 ///   Once a member RecordType has been expanded, its temporary incomplete
8138 ///   entry is removed from the cache. If a Recursive encoding was swapped out
8139 ///   it is swapped back in;
8140 ///
8141 ///   If an incomplete entry is used to expand a sub-member, the incomplete
8142 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
8143 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
8144 ///
8145 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
8146 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
8147 ///   Else the member is part of a recursive type and thus the recursion has
8148 ///   been exited too soon for the encoding to be correct for the member.
8149 ///
8150 class TypeStringCache {
8151   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
8152   struct Entry {
8153     std::string Str;     // The encoded TypeString for the type.
8154     enum Status State;   // Information about the encoding in 'Str'.
8155     std::string Swapped; // A temporary place holder for a Recursive encoding
8156                          // during the expansion of RecordType's members.
8157   };
8158   std::map<const IdentifierInfo *, struct Entry> Map;
8159   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
8160   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
8161 public:
8162   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
8163   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
8164   bool removeIncomplete(const IdentifierInfo *ID);
8165   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
8166                      bool IsRecursive);
8167   StringRef lookupStr(const IdentifierInfo *ID);
8168 };
8169 
8170 /// TypeString encodings for enum & union fields must be order.
8171 /// FieldEncoding is a helper for this ordering process.
8172 class FieldEncoding {
8173   bool HasName;
8174   std::string Enc;
8175 public:
8176   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
8177   StringRef str() { return Enc; }
8178   bool operator<(const FieldEncoding &rhs) const {
8179     if (HasName != rhs.HasName) return HasName;
8180     return Enc < rhs.Enc;
8181   }
8182 };
8183 
8184 class XCoreABIInfo : public DefaultABIInfo {
8185 public:
8186   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8187   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8188                     QualType Ty) const override;
8189 };
8190 
8191 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
8192   mutable TypeStringCache TSC;
8193 public:
8194   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
8195     :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
8196   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8197                     CodeGen::CodeGenModule &M) const override;
8198 };
8199 
8200 } // End anonymous namespace.
8201 
8202 // TODO: this implementation is likely now redundant with the default
8203 // EmitVAArg.
8204 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8205                                 QualType Ty) const {
8206   CGBuilderTy &Builder = CGF.Builder;
8207 
8208   // Get the VAList.
8209   CharUnits SlotSize = CharUnits::fromQuantity(4);
8210   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
8211 
8212   // Handle the argument.
8213   ABIArgInfo AI = classifyArgumentType(Ty);
8214   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
8215   llvm::Type *ArgTy = CGT.ConvertType(Ty);
8216   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8217     AI.setCoerceToType(ArgTy);
8218   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
8219 
8220   Address Val = Address::invalid();
8221   CharUnits ArgSize = CharUnits::Zero();
8222   switch (AI.getKind()) {
8223   case ABIArgInfo::Expand:
8224   case ABIArgInfo::CoerceAndExpand:
8225   case ABIArgInfo::InAlloca:
8226     llvm_unreachable("Unsupported ABI kind for va_arg");
8227   case ABIArgInfo::Ignore:
8228     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
8229     ArgSize = CharUnits::Zero();
8230     break;
8231   case ABIArgInfo::Extend:
8232   case ABIArgInfo::Direct:
8233     Val = Builder.CreateBitCast(AP, ArgPtrTy);
8234     ArgSize = CharUnits::fromQuantity(
8235                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
8236     ArgSize = ArgSize.alignTo(SlotSize);
8237     break;
8238   case ABIArgInfo::Indirect:
8239     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
8240     Val = Address(Builder.CreateLoad(Val), TypeAlign);
8241     ArgSize = SlotSize;
8242     break;
8243   }
8244 
8245   // Increment the VAList.
8246   if (!ArgSize.isZero()) {
8247     llvm::Value *APN =
8248       Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
8249     Builder.CreateStore(APN, VAListAddr);
8250   }
8251 
8252   return Val;
8253 }
8254 
8255 /// During the expansion of a RecordType, an incomplete TypeString is placed
8256 /// into the cache as a means to identify and break recursion.
8257 /// If there is a Recursive encoding in the cache, it is swapped out and will
8258 /// be reinserted by removeIncomplete().
8259 /// All other types of encoding should have been used rather than arriving here.
8260 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
8261                                     std::string StubEnc) {
8262   if (!ID)
8263     return;
8264   Entry &E = Map[ID];
8265   assert( (E.Str.empty() || E.State == Recursive) &&
8266          "Incorrectly use of addIncomplete");
8267   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
8268   E.Swapped.swap(E.Str); // swap out the Recursive
8269   E.Str.swap(StubEnc);
8270   E.State = Incomplete;
8271   ++IncompleteCount;
8272 }
8273 
8274 /// Once the RecordType has been expanded, the temporary incomplete TypeString
8275 /// must be removed from the cache.
8276 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
8277 /// Returns true if the RecordType was defined recursively.
8278 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
8279   if (!ID)
8280     return false;
8281   auto I = Map.find(ID);
8282   assert(I != Map.end() && "Entry not present");
8283   Entry &E = I->second;
8284   assert( (E.State == Incomplete ||
8285            E.State == IncompleteUsed) &&
8286          "Entry must be an incomplete type");
8287   bool IsRecursive = false;
8288   if (E.State == IncompleteUsed) {
8289     // We made use of our Incomplete encoding, thus we are recursive.
8290     IsRecursive = true;
8291     --IncompleteUsedCount;
8292   }
8293   if (E.Swapped.empty())
8294     Map.erase(I);
8295   else {
8296     // Swap the Recursive back.
8297     E.Swapped.swap(E.Str);
8298     E.Swapped.clear();
8299     E.State = Recursive;
8300   }
8301   --IncompleteCount;
8302   return IsRecursive;
8303 }
8304 
8305 /// Add the encoded TypeString to the cache only if it is NonRecursive or
8306 /// Recursive (viz: all sub-members were expanded as fully as possible).
8307 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
8308                                     bool IsRecursive) {
8309   if (!ID || IncompleteUsedCount)
8310     return; // No key or it is is an incomplete sub-type so don't add.
8311   Entry &E = Map[ID];
8312   if (IsRecursive && !E.Str.empty()) {
8313     assert(E.State==Recursive && E.Str.size() == Str.size() &&
8314            "This is not the same Recursive entry");
8315     // The parent container was not recursive after all, so we could have used
8316     // this Recursive sub-member entry after all, but we assumed the worse when
8317     // we started viz: IncompleteCount!=0.
8318     return;
8319   }
8320   assert(E.Str.empty() && "Entry already present");
8321   E.Str = Str.str();
8322   E.State = IsRecursive? Recursive : NonRecursive;
8323 }
8324 
8325 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
8326 /// are recursively expanding a type (IncompleteCount != 0) and the cached
8327 /// encoding is Recursive, return an empty StringRef.
8328 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
8329   if (!ID)
8330     return StringRef();   // We have no key.
8331   auto I = Map.find(ID);
8332   if (I == Map.end())
8333     return StringRef();   // We have no encoding.
8334   Entry &E = I->second;
8335   if (E.State == Recursive && IncompleteCount)
8336     return StringRef();   // We don't use Recursive encodings for member types.
8337 
8338   if (E.State == Incomplete) {
8339     // The incomplete type is being used to break out of recursion.
8340     E.State = IncompleteUsed;
8341     ++IncompleteUsedCount;
8342   }
8343   return E.Str;
8344 }
8345 
8346 /// The XCore ABI includes a type information section that communicates symbol
8347 /// type information to the linker. The linker uses this information to verify
8348 /// safety/correctness of things such as array bound and pointers et al.
8349 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
8350 /// This type information (TypeString) is emitted into meta data for all global
8351 /// symbols: definitions, declarations, functions & variables.
8352 ///
8353 /// The TypeString carries type, qualifier, name, size & value details.
8354 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
8355 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
8356 /// The output is tested by test/CodeGen/xcore-stringtype.c.
8357 ///
8358 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8359                           CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
8360 
8361 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
8362 void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8363                                           CodeGen::CodeGenModule &CGM) const {
8364   SmallStringEnc Enc;
8365   if (getTypeString(Enc, D, CGM, TSC)) {
8366     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
8367     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
8368                                 llvm::MDString::get(Ctx, Enc.str())};
8369     llvm::NamedMDNode *MD =
8370       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
8371     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
8372   }
8373 }
8374 
8375 //===----------------------------------------------------------------------===//
8376 // SPIR ABI Implementation
8377 //===----------------------------------------------------------------------===//
8378 
8379 namespace {
8380 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
8381 public:
8382   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8383     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
8384   unsigned getOpenCLKernelCallingConv() const override;
8385 };
8386 
8387 } // End anonymous namespace.
8388 
8389 namespace clang {
8390 namespace CodeGen {
8391 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8392   DefaultABIInfo SPIRABI(CGM.getTypes());
8393   SPIRABI.computeInfo(FI);
8394 }
8395 }
8396 }
8397 
8398 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8399   return llvm::CallingConv::SPIR_KERNEL;
8400 }
8401 
8402 static bool appendType(SmallStringEnc &Enc, QualType QType,
8403                        const CodeGen::CodeGenModule &CGM,
8404                        TypeStringCache &TSC);
8405 
8406 /// Helper function for appendRecordType().
8407 /// Builds a SmallVector containing the encoded field types in declaration
8408 /// order.
8409 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
8410                              const RecordDecl *RD,
8411                              const CodeGen::CodeGenModule &CGM,
8412                              TypeStringCache &TSC) {
8413   for (const auto *Field : RD->fields()) {
8414     SmallStringEnc Enc;
8415     Enc += "m(";
8416     Enc += Field->getName();
8417     Enc += "){";
8418     if (Field->isBitField()) {
8419       Enc += "b(";
8420       llvm::raw_svector_ostream OS(Enc);
8421       OS << Field->getBitWidthValue(CGM.getContext());
8422       Enc += ':';
8423     }
8424     if (!appendType(Enc, Field->getType(), CGM, TSC))
8425       return false;
8426     if (Field->isBitField())
8427       Enc += ')';
8428     Enc += '}';
8429     FE.emplace_back(!Field->getName().empty(), Enc);
8430   }
8431   return true;
8432 }
8433 
8434 /// Appends structure and union types to Enc and adds encoding to cache.
8435 /// Recursively calls appendType (via extractFieldType) for each field.
8436 /// Union types have their fields ordered according to the ABI.
8437 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
8438                              const CodeGen::CodeGenModule &CGM,
8439                              TypeStringCache &TSC, const IdentifierInfo *ID) {
8440   // Append the cached TypeString if we have one.
8441   StringRef TypeString = TSC.lookupStr(ID);
8442   if (!TypeString.empty()) {
8443     Enc += TypeString;
8444     return true;
8445   }
8446 
8447   // Start to emit an incomplete TypeString.
8448   size_t Start = Enc.size();
8449   Enc += (RT->isUnionType()? 'u' : 's');
8450   Enc += '(';
8451   if (ID)
8452     Enc += ID->getName();
8453   Enc += "){";
8454 
8455   // We collect all encoded fields and order as necessary.
8456   bool IsRecursive = false;
8457   const RecordDecl *RD = RT->getDecl()->getDefinition();
8458   if (RD && !RD->field_empty()) {
8459     // An incomplete TypeString stub is placed in the cache for this RecordType
8460     // so that recursive calls to this RecordType will use it whilst building a
8461     // complete TypeString for this RecordType.
8462     SmallVector<FieldEncoding, 16> FE;
8463     std::string StubEnc(Enc.substr(Start).str());
8464     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
8465     TSC.addIncomplete(ID, std::move(StubEnc));
8466     if (!extractFieldType(FE, RD, CGM, TSC)) {
8467       (void) TSC.removeIncomplete(ID);
8468       return false;
8469     }
8470     IsRecursive = TSC.removeIncomplete(ID);
8471     // The ABI requires unions to be sorted but not structures.
8472     // See FieldEncoding::operator< for sort algorithm.
8473     if (RT->isUnionType())
8474       std::sort(FE.begin(), FE.end());
8475     // We can now complete the TypeString.
8476     unsigned E = FE.size();
8477     for (unsigned I = 0; I != E; ++I) {
8478       if (I)
8479         Enc += ',';
8480       Enc += FE[I].str();
8481     }
8482   }
8483   Enc += '}';
8484   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
8485   return true;
8486 }
8487 
8488 /// Appends enum types to Enc and adds the encoding to the cache.
8489 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
8490                            TypeStringCache &TSC,
8491                            const IdentifierInfo *ID) {
8492   // Append the cached TypeString if we have one.
8493   StringRef TypeString = TSC.lookupStr(ID);
8494   if (!TypeString.empty()) {
8495     Enc += TypeString;
8496     return true;
8497   }
8498 
8499   size_t Start = Enc.size();
8500   Enc += "e(";
8501   if (ID)
8502     Enc += ID->getName();
8503   Enc += "){";
8504 
8505   // We collect all encoded enumerations and order them alphanumerically.
8506   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
8507     SmallVector<FieldEncoding, 16> FE;
8508     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
8509          ++I) {
8510       SmallStringEnc EnumEnc;
8511       EnumEnc += "m(";
8512       EnumEnc += I->getName();
8513       EnumEnc += "){";
8514       I->getInitVal().toString(EnumEnc);
8515       EnumEnc += '}';
8516       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
8517     }
8518     std::sort(FE.begin(), FE.end());
8519     unsigned E = FE.size();
8520     for (unsigned I = 0; I != E; ++I) {
8521       if (I)
8522         Enc += ',';
8523       Enc += FE[I].str();
8524     }
8525   }
8526   Enc += '}';
8527   TSC.addIfComplete(ID, Enc.substr(Start), false);
8528   return true;
8529 }
8530 
8531 /// Appends type's qualifier to Enc.
8532 /// This is done prior to appending the type's encoding.
8533 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
8534   // Qualifiers are emitted in alphabetical order.
8535   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
8536   int Lookup = 0;
8537   if (QT.isConstQualified())
8538     Lookup += 1<<0;
8539   if (QT.isRestrictQualified())
8540     Lookup += 1<<1;
8541   if (QT.isVolatileQualified())
8542     Lookup += 1<<2;
8543   Enc += Table[Lookup];
8544 }
8545 
8546 /// Appends built-in types to Enc.
8547 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
8548   const char *EncType;
8549   switch (BT->getKind()) {
8550     case BuiltinType::Void:
8551       EncType = "0";
8552       break;
8553     case BuiltinType::Bool:
8554       EncType = "b";
8555       break;
8556     case BuiltinType::Char_U:
8557       EncType = "uc";
8558       break;
8559     case BuiltinType::UChar:
8560       EncType = "uc";
8561       break;
8562     case BuiltinType::SChar:
8563       EncType = "sc";
8564       break;
8565     case BuiltinType::UShort:
8566       EncType = "us";
8567       break;
8568     case BuiltinType::Short:
8569       EncType = "ss";
8570       break;
8571     case BuiltinType::UInt:
8572       EncType = "ui";
8573       break;
8574     case BuiltinType::Int:
8575       EncType = "si";
8576       break;
8577     case BuiltinType::ULong:
8578       EncType = "ul";
8579       break;
8580     case BuiltinType::Long:
8581       EncType = "sl";
8582       break;
8583     case BuiltinType::ULongLong:
8584       EncType = "ull";
8585       break;
8586     case BuiltinType::LongLong:
8587       EncType = "sll";
8588       break;
8589     case BuiltinType::Float:
8590       EncType = "ft";
8591       break;
8592     case BuiltinType::Double:
8593       EncType = "d";
8594       break;
8595     case BuiltinType::LongDouble:
8596       EncType = "ld";
8597       break;
8598     default:
8599       return false;
8600   }
8601   Enc += EncType;
8602   return true;
8603 }
8604 
8605 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
8606 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
8607                               const CodeGen::CodeGenModule &CGM,
8608                               TypeStringCache &TSC) {
8609   Enc += "p(";
8610   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
8611     return false;
8612   Enc += ')';
8613   return true;
8614 }
8615 
8616 /// Appends array encoding to Enc before calling appendType for the element.
8617 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
8618                             const ArrayType *AT,
8619                             const CodeGen::CodeGenModule &CGM,
8620                             TypeStringCache &TSC, StringRef NoSizeEnc) {
8621   if (AT->getSizeModifier() != ArrayType::Normal)
8622     return false;
8623   Enc += "a(";
8624   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
8625     CAT->getSize().toStringUnsigned(Enc);
8626   else
8627     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
8628   Enc += ':';
8629   // The Qualifiers should be attached to the type rather than the array.
8630   appendQualifier(Enc, QT);
8631   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
8632     return false;
8633   Enc += ')';
8634   return true;
8635 }
8636 
8637 /// Appends a function encoding to Enc, calling appendType for the return type
8638 /// and the arguments.
8639 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
8640                              const CodeGen::CodeGenModule &CGM,
8641                              TypeStringCache &TSC) {
8642   Enc += "f{";
8643   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
8644     return false;
8645   Enc += "}(";
8646   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
8647     // N.B. we are only interested in the adjusted param types.
8648     auto I = FPT->param_type_begin();
8649     auto E = FPT->param_type_end();
8650     if (I != E) {
8651       do {
8652         if (!appendType(Enc, *I, CGM, TSC))
8653           return false;
8654         ++I;
8655         if (I != E)
8656           Enc += ',';
8657       } while (I != E);
8658       if (FPT->isVariadic())
8659         Enc += ",va";
8660     } else {
8661       if (FPT->isVariadic())
8662         Enc += "va";
8663       else
8664         Enc += '0';
8665     }
8666   }
8667   Enc += ')';
8668   return true;
8669 }
8670 
8671 /// Handles the type's qualifier before dispatching a call to handle specific
8672 /// type encodings.
8673 static bool appendType(SmallStringEnc &Enc, QualType QType,
8674                        const CodeGen::CodeGenModule &CGM,
8675                        TypeStringCache &TSC) {
8676 
8677   QualType QT = QType.getCanonicalType();
8678 
8679   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
8680     // The Qualifiers should be attached to the type rather than the array.
8681     // Thus we don't call appendQualifier() here.
8682     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
8683 
8684   appendQualifier(Enc, QT);
8685 
8686   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
8687     return appendBuiltinType(Enc, BT);
8688 
8689   if (const PointerType *PT = QT->getAs<PointerType>())
8690     return appendPointerType(Enc, PT, CGM, TSC);
8691 
8692   if (const EnumType *ET = QT->getAs<EnumType>())
8693     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
8694 
8695   if (const RecordType *RT = QT->getAsStructureType())
8696     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8697 
8698   if (const RecordType *RT = QT->getAsUnionType())
8699     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8700 
8701   if (const FunctionType *FT = QT->getAs<FunctionType>())
8702     return appendFunctionType(Enc, FT, CGM, TSC);
8703 
8704   return false;
8705 }
8706 
8707 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8708                           CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
8709   if (!D)
8710     return false;
8711 
8712   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
8713     if (FD->getLanguageLinkage() != CLanguageLinkage)
8714       return false;
8715     return appendType(Enc, FD->getType(), CGM, TSC);
8716   }
8717 
8718   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
8719     if (VD->getLanguageLinkage() != CLanguageLinkage)
8720       return false;
8721     QualType QT = VD->getType().getCanonicalType();
8722     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
8723       // Global ArrayTypes are given a size of '*' if the size is unknown.
8724       // The Qualifiers should be attached to the type rather than the array.
8725       // Thus we don't call appendQualifier() here.
8726       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
8727     }
8728     return appendType(Enc, QT, CGM, TSC);
8729   }
8730   return false;
8731 }
8732 
8733 
8734 //===----------------------------------------------------------------------===//
8735 // Driver code
8736 //===----------------------------------------------------------------------===//
8737 
8738 bool CodeGenModule::supportsCOMDAT() const {
8739   return getTriple().supportsCOMDAT();
8740 }
8741 
8742 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
8743   if (TheTargetCodeGenInfo)
8744     return *TheTargetCodeGenInfo;
8745 
8746   // Helper to set the unique_ptr while still keeping the return value.
8747   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
8748     this->TheTargetCodeGenInfo.reset(P);
8749     return *P;
8750   };
8751 
8752   const llvm::Triple &Triple = getTarget().getTriple();
8753   switch (Triple.getArch()) {
8754   default:
8755     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
8756 
8757   case llvm::Triple::le32:
8758     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
8759   case llvm::Triple::mips:
8760   case llvm::Triple::mipsel:
8761     if (Triple.getOS() == llvm::Triple::NaCl)
8762       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
8763     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
8764 
8765   case llvm::Triple::mips64:
8766   case llvm::Triple::mips64el:
8767     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
8768 
8769   case llvm::Triple::avr:
8770     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
8771 
8772   case llvm::Triple::aarch64:
8773   case llvm::Triple::aarch64_be: {
8774     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
8775     if (getTarget().getABI() == "darwinpcs")
8776       Kind = AArch64ABIInfo::DarwinPCS;
8777     else if (Triple.isOSWindows())
8778       return SetCGInfo(
8779           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
8780 
8781     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
8782   }
8783 
8784   case llvm::Triple::wasm32:
8785   case llvm::Triple::wasm64:
8786     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
8787 
8788   case llvm::Triple::arm:
8789   case llvm::Triple::armeb:
8790   case llvm::Triple::thumb:
8791   case llvm::Triple::thumbeb: {
8792     if (Triple.getOS() == llvm::Triple::Win32) {
8793       return SetCGInfo(
8794           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
8795     }
8796 
8797     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
8798     StringRef ABIStr = getTarget().getABI();
8799     if (ABIStr == "apcs-gnu")
8800       Kind = ARMABIInfo::APCS;
8801     else if (ABIStr == "aapcs16")
8802       Kind = ARMABIInfo::AAPCS16_VFP;
8803     else if (CodeGenOpts.FloatABI == "hard" ||
8804              (CodeGenOpts.FloatABI != "soft" &&
8805               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
8806                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
8807                Triple.getEnvironment() == llvm::Triple::EABIHF)))
8808       Kind = ARMABIInfo::AAPCS_VFP;
8809 
8810     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
8811   }
8812 
8813   case llvm::Triple::ppc:
8814     return SetCGInfo(
8815         new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
8816   case llvm::Triple::ppc64:
8817     if (Triple.isOSBinFormatELF()) {
8818       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
8819       if (getTarget().getABI() == "elfv2")
8820         Kind = PPC64_SVR4_ABIInfo::ELFv2;
8821       bool HasQPX = getTarget().getABI() == "elfv1-qpx";
8822       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
8823 
8824       return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8825                                                         IsSoftFloat));
8826     } else
8827       return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
8828   case llvm::Triple::ppc64le: {
8829     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
8830     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
8831     if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
8832       Kind = PPC64_SVR4_ABIInfo::ELFv1;
8833     bool HasQPX = getTarget().getABI() == "elfv1-qpx";
8834     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
8835 
8836     return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8837                                                       IsSoftFloat));
8838   }
8839 
8840   case llvm::Triple::nvptx:
8841   case llvm::Triple::nvptx64:
8842     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
8843 
8844   case llvm::Triple::msp430:
8845     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
8846 
8847   case llvm::Triple::systemz: {
8848     bool HasVector = getTarget().getABI() == "vector";
8849     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
8850   }
8851 
8852   case llvm::Triple::tce:
8853   case llvm::Triple::tcele:
8854     return SetCGInfo(new TCETargetCodeGenInfo(Types));
8855 
8856   case llvm::Triple::x86: {
8857     bool IsDarwinVectorABI = Triple.isOSDarwin();
8858     bool RetSmallStructInRegABI =
8859         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
8860     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
8861 
8862     if (Triple.getOS() == llvm::Triple::Win32) {
8863       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
8864           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8865           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
8866     } else {
8867       return SetCGInfo(new X86_32TargetCodeGenInfo(
8868           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8869           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
8870           CodeGenOpts.FloatABI == "soft"));
8871     }
8872   }
8873 
8874   case llvm::Triple::x86_64: {
8875     StringRef ABI = getTarget().getABI();
8876     X86AVXABILevel AVXLevel =
8877         (ABI == "avx512"
8878              ? X86AVXABILevel::AVX512
8879              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
8880 
8881     switch (Triple.getOS()) {
8882     case llvm::Triple::Win32:
8883       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
8884     case llvm::Triple::PS4:
8885       return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
8886     default:
8887       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
8888     }
8889   }
8890   case llvm::Triple::hexagon:
8891     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
8892   case llvm::Triple::lanai:
8893     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
8894   case llvm::Triple::r600:
8895     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
8896   case llvm::Triple::amdgcn:
8897     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
8898   case llvm::Triple::sparc:
8899     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
8900   case llvm::Triple::sparcv9:
8901     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
8902   case llvm::Triple::xcore:
8903     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
8904   case llvm::Triple::spir:
8905   case llvm::Triple::spir64:
8906     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
8907   }
8908 }
8909