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