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