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