TargetInfo.cpp revision eb518b4b89e4134b21975530809697142f69b779
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 "CodeGenFunction.h"
18#include "clang/AST/RecordLayout.h"
19#include "llvm/Type.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25using namespace CodeGen;
26
27static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28                               llvm::Value *Array,
29                               llvm::Value *Value,
30                               unsigned FirstIndex,
31                               unsigned LastIndex) {
32  // Alternatively, we could emit this as a loop in the source.
33  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34    llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35    Builder.CreateStore(Value, Cell);
36  }
37}
38
39ABIInfo::~ABIInfo() {}
40
41ASTContext &ABIInfo::getContext() const {
42  return CGT.getContext();
43}
44
45llvm::LLVMContext &ABIInfo::getVMContext() const {
46  return CGT.getLLVMContext();
47}
48
49const llvm::TargetData &ABIInfo::getTargetData() const {
50  return CGT.getTargetData();
51}
52
53
54void ABIArgInfo::dump() const {
55  llvm::raw_ostream &OS = llvm::errs();
56  OS << "(ABIArgInfo Kind=";
57  switch (TheKind) {
58  case Direct:
59    OS << "Direct Type=";
60    if (const llvm::Type *Ty = getCoerceToType())
61      Ty->print(OS);
62    else
63      OS << "null";
64    break;
65  case Extend:
66    OS << "Extend";
67    break;
68  case Ignore:
69    OS << "Ignore";
70    break;
71  case Indirect:
72    OS << "Indirect Align=" << getIndirectAlign()
73       << " Byal=" << getIndirectByVal();
74    break;
75  case Expand:
76    OS << "Expand";
77    break;
78  }
79  OS << ")\n";
80}
81
82TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
83
84static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
85
86/// isEmptyField - Return true iff a the field is "empty", that is it
87/// is an unnamed bit-field or an (array of) empty record(s).
88static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
89                         bool AllowArrays) {
90  if (FD->isUnnamedBitfield())
91    return true;
92
93  QualType FT = FD->getType();
94
95    // Constant arrays of empty records count as empty, strip them off.
96  if (AllowArrays)
97    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
98      FT = AT->getElementType();
99
100  const RecordType *RT = FT->getAs<RecordType>();
101  if (!RT)
102    return false;
103
104  // C++ record fields are never empty, at least in the Itanium ABI.
105  //
106  // FIXME: We should use a predicate for whether this behavior is true in the
107  // current ABI.
108  if (isa<CXXRecordDecl>(RT->getDecl()))
109    return false;
110
111  return isEmptyRecord(Context, FT, AllowArrays);
112}
113
114/// isEmptyRecord - Return true iff a structure contains only empty
115/// fields. Note that a structure with a flexible array member is not
116/// considered empty.
117static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
118  const RecordType *RT = T->getAs<RecordType>();
119  if (!RT)
120    return 0;
121  const RecordDecl *RD = RT->getDecl();
122  if (RD->hasFlexibleArrayMember())
123    return false;
124
125  // If this is a C++ record, check the bases first.
126  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
127    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
128           e = CXXRD->bases_end(); i != e; ++i)
129      if (!isEmptyRecord(Context, i->getType(), true))
130        return false;
131
132  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133         i != e; ++i)
134    if (!isEmptyField(Context, *i, AllowArrays))
135      return false;
136  return true;
137}
138
139/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
140/// a non-trivial destructor or a non-trivial copy constructor.
141static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
142  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
143  if (!RD)
144    return false;
145
146  return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
147}
148
149/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
150/// a record type with either a non-trivial destructor or a non-trivial copy
151/// constructor.
152static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
153  const RecordType *RT = T->getAs<RecordType>();
154  if (!RT)
155    return false;
156
157  return hasNonTrivialDestructorOrCopyConstructor(RT);
158}
159
160/// isSingleElementStruct - Determine if a structure is a "single
161/// element struct", i.e. it has exactly one non-empty field or
162/// exactly one field which is itself a single element
163/// struct. Structures with flexible array members are never
164/// considered single element structs.
165///
166/// \return The field declaration for the single non-empty field, if
167/// it exists.
168static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
169  const RecordType *RT = T->getAsStructureType();
170  if (!RT)
171    return 0;
172
173  const RecordDecl *RD = RT->getDecl();
174  if (RD->hasFlexibleArrayMember())
175    return 0;
176
177  const Type *Found = 0;
178
179  // If this is a C++ record, check the bases first.
180  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
181    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
182           e = CXXRD->bases_end(); i != e; ++i) {
183      // Ignore empty records.
184      if (isEmptyRecord(Context, i->getType(), true))
185        continue;
186
187      // If we already found an element then this isn't a single-element struct.
188      if (Found)
189        return 0;
190
191      // If this is non-empty and not a single element struct, the composite
192      // cannot be a single element struct.
193      Found = isSingleElementStruct(i->getType(), Context);
194      if (!Found)
195        return 0;
196    }
197  }
198
199  // Check for single element.
200  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
201         i != e; ++i) {
202    const FieldDecl *FD = *i;
203    QualType FT = FD->getType();
204
205    // Ignore empty fields.
206    if (isEmptyField(Context, FD, true))
207      continue;
208
209    // If we already found an element then this isn't a single-element
210    // struct.
211    if (Found)
212      return 0;
213
214    // Treat single element arrays as the element.
215    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
216      if (AT->getSize().getZExtValue() != 1)
217        break;
218      FT = AT->getElementType();
219    }
220
221    if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
222      Found = FT.getTypePtr();
223    } else {
224      Found = isSingleElementStruct(FT, Context);
225      if (!Found)
226        return 0;
227    }
228  }
229
230  return Found;
231}
232
233static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
234  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
235      !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
236      !Ty->isBlockPointerType())
237    return false;
238
239  uint64_t Size = Context.getTypeSize(Ty);
240  return Size == 32 || Size == 64;
241}
242
243/// canExpandIndirectArgument - Test whether an argument type which is to be
244/// passed indirectly (on the stack) would have the equivalent layout if it was
245/// expanded into separate arguments. If so, we prefer to do the latter to avoid
246/// inhibiting optimizations.
247///
248// FIXME: This predicate is missing many cases, currently it just follows
249// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
250// should probably make this smarter, or better yet make the LLVM backend
251// capable of handling it.
252static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
253  // We can only expand structure types.
254  const RecordType *RT = Ty->getAs<RecordType>();
255  if (!RT)
256    return false;
257
258  // We can only expand (C) structures.
259  //
260  // FIXME: This needs to be generalized to handle classes as well.
261  const RecordDecl *RD = RT->getDecl();
262  if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
263    return false;
264
265  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
266         i != e; ++i) {
267    const FieldDecl *FD = *i;
268
269    if (!is32Or64BitBasicType(FD->getType(), Context))
270      return false;
271
272    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
273    // how to expand them yet, and the predicate for telling if a bitfield still
274    // counts as "basic" is more complicated than what we were doing previously.
275    if (FD->isBitField())
276      return false;
277  }
278
279  return true;
280}
281
282namespace {
283/// DefaultABIInfo - The default implementation for ABI specific
284/// details. This implementation provides information which results in
285/// self-consistent and sensible LLVM IR generation, but does not
286/// conform to any particular ABI.
287class DefaultABIInfo : public ABIInfo {
288public:
289  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
290
291  ABIArgInfo classifyReturnType(QualType RetTy) const;
292  ABIArgInfo classifyArgumentType(QualType RetTy) const;
293
294  virtual void computeInfo(CGFunctionInfo &FI) const {
295    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
296    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
297         it != ie; ++it)
298      it->info = classifyArgumentType(it->type);
299  }
300
301  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
302                                 CodeGenFunction &CGF) const;
303};
304
305class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
306public:
307  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
308    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
309};
310
311llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
312                                       CodeGenFunction &CGF) const {
313  return 0;
314}
315
316ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
317  if (CodeGenFunction::hasAggregateLLVMType(Ty))
318    return ABIArgInfo::getIndirect(0);
319
320  // Treat an enum type as its underlying type.
321  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
322    Ty = EnumTy->getDecl()->getIntegerType();
323
324  return (Ty->isPromotableIntegerType() ?
325          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
326}
327
328//===----------------------------------------------------------------------===//
329// X86-32 ABI Implementation
330//===----------------------------------------------------------------------===//
331
332/// X86_32ABIInfo - The X86-32 ABI information.
333class X86_32ABIInfo : public ABIInfo {
334  bool IsDarwinVectorABI;
335  bool IsSmallStructInRegABI;
336
337  static bool isRegisterSize(unsigned Size) {
338    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
339  }
340
341  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
342
343  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
344  /// such that the argument will be passed in memory.
345  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
346
347public:
348
349  ABIArgInfo classifyReturnType(QualType RetTy) const;
350  ABIArgInfo classifyArgumentType(QualType RetTy) const;
351
352  virtual void computeInfo(CGFunctionInfo &FI) const {
353    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
354    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
355         it != ie; ++it)
356      it->info = classifyArgumentType(it->type);
357  }
358
359  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
360                                 CodeGenFunction &CGF) const;
361
362  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
363    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
364};
365
366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
368  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
369    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
370
371  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372                           CodeGen::CodeGenModule &CGM) const;
373
374  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375    // Darwin uses different dwarf register numbers for EH.
376    if (CGM.isTargetDarwin()) return 5;
377
378    return 4;
379  }
380
381  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382                               llvm::Value *Address) const;
383};
384
385}
386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390                                               ASTContext &Context) {
391  uint64_t Size = Context.getTypeSize(Ty);
392
393  // Type must be register sized.
394  if (!isRegisterSize(Size))
395    return false;
396
397  if (Ty->isVectorType()) {
398    // 64- and 128- bit vectors inside structures are not returned in
399    // registers.
400    if (Size == 64 || Size == 128)
401      return false;
402
403    return true;
404  }
405
406  // If this is a builtin, pointer, enum, complex type, member pointer, or
407  // member function pointer it is ok.
408  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
409      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
410      Ty->isBlockPointerType() || Ty->isMemberPointerType())
411    return true;
412
413  // Arrays are treated like records.
414  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415    return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417  // Otherwise, it must be a record type.
418  const RecordType *RT = Ty->getAs<RecordType>();
419  if (!RT) return false;
420
421  // FIXME: Traverse bases here too.
422
423  // Structure types are passed in register if all fields would be
424  // passed in a register.
425  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426         e = RT->getDecl()->field_end(); i != e; ++i) {
427    const FieldDecl *FD = *i;
428
429    // Empty fields are ignored.
430    if (isEmptyField(Context, FD, true))
431      continue;
432
433    // Check fields recursively.
434    if (!shouldReturnTypeInRegister(FD->getType(), Context))
435      return false;
436  }
437
438  return true;
439}
440
441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
442  if (RetTy->isVoidType())
443    return ABIArgInfo::getIgnore();
444
445  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
446    // On Darwin, some vectors are returned in registers.
447    if (IsDarwinVectorABI) {
448      uint64_t Size = getContext().getTypeSize(RetTy);
449
450      // 128-bit vectors are a special case; they are returned in
451      // registers and we need to make sure to pick a type the LLVM
452      // backend will like.
453      if (Size == 128)
454        return ABIArgInfo::getDirect(llvm::VectorType::get(
455                  llvm::Type::getInt64Ty(getVMContext()), 2));
456
457      // Always return in register if it fits in a general purpose
458      // register, or if it is 64 bits and has a single element.
459      if ((Size == 8 || Size == 16 || Size == 32) ||
460          (Size == 64 && VT->getNumElements() == 1))
461        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
462                                                            Size));
463
464      return ABIArgInfo::getIndirect(0);
465    }
466
467    return ABIArgInfo::getDirect();
468  }
469
470  if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
471    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
472      // Structures with either a non-trivial destructor or a non-trivial
473      // copy constructor are always indirect.
474      if (hasNonTrivialDestructorOrCopyConstructor(RT))
475        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
476
477      // Structures with flexible arrays are always indirect.
478      if (RT->getDecl()->hasFlexibleArrayMember())
479        return ABIArgInfo::getIndirect(0);
480    }
481
482    // If specified, structs and unions are always indirect.
483    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
484      return ABIArgInfo::getIndirect(0);
485
486    // Classify "single element" structs as their element type.
487    if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
488      if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
489        if (BT->isIntegerType()) {
490          // We need to use the size of the structure, padding
491          // bit-fields can adjust that to be larger than the single
492          // element type.
493          uint64_t Size = getContext().getTypeSize(RetTy);
494          return ABIArgInfo::getDirect(
495            llvm::IntegerType::get(getVMContext(), (unsigned)Size));
496        }
497
498        if (BT->getKind() == BuiltinType::Float) {
499          assert(getContext().getTypeSize(RetTy) ==
500                 getContext().getTypeSize(SeltTy) &&
501                 "Unexpect single element structure size!");
502          return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
503        }
504
505        if (BT->getKind() == BuiltinType::Double) {
506          assert(getContext().getTypeSize(RetTy) ==
507                 getContext().getTypeSize(SeltTy) &&
508                 "Unexpect single element structure size!");
509          return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
510        }
511      } else if (SeltTy->isPointerType()) {
512        // FIXME: It would be really nice if this could come out as the proper
513        // pointer type.
514        const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
515        return ABIArgInfo::getDirect(PtrTy);
516      } else if (SeltTy->isVectorType()) {
517        // 64- and 128-bit vectors are never returned in a
518        // register when inside a structure.
519        uint64_t Size = getContext().getTypeSize(RetTy);
520        if (Size == 64 || Size == 128)
521          return ABIArgInfo::getIndirect(0);
522
523        return classifyReturnType(QualType(SeltTy, 0));
524      }
525    }
526
527    // Small structures which are register sized are generally returned
528    // in a register.
529    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
530      uint64_t Size = getContext().getTypeSize(RetTy);
531      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
532    }
533
534    return ABIArgInfo::getIndirect(0);
535  }
536
537  // Treat an enum type as its underlying type.
538  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
539    RetTy = EnumTy->getDecl()->getIntegerType();
540
541  return (RetTy->isPromotableIntegerType() ?
542          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
543}
544
545ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
546  if (!ByVal)
547    return ABIArgInfo::getIndirect(0, false);
548
549  // Compute the byval alignment. We trust the back-end to honor the
550  // minimum ABI alignment for byval, to make cleaner IR.
551  const unsigned MinABIAlign = 4;
552  unsigned Align = getContext().getTypeAlign(Ty) / 8;
553  if (Align > MinABIAlign)
554    return ABIArgInfo::getIndirect(Align);
555  return ABIArgInfo::getIndirect(0);
556}
557
558ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
559  // FIXME: Set alignment on indirect arguments.
560  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
561    // Structures with flexible arrays are always indirect.
562    if (const RecordType *RT = Ty->getAs<RecordType>()) {
563      // Structures with either a non-trivial destructor or a non-trivial
564      // copy constructor are always indirect.
565      if (hasNonTrivialDestructorOrCopyConstructor(RT))
566        return getIndirectResult(Ty, /*ByVal=*/false);
567
568      if (RT->getDecl()->hasFlexibleArrayMember())
569        return getIndirectResult(Ty);
570    }
571
572    // Ignore empty structs.
573    if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
574      return ABIArgInfo::getIgnore();
575
576    // Expand small (<= 128-bit) record types when we know that the stack layout
577    // of those arguments will match the struct. This is important because the
578    // LLVM backend isn't smart enough to remove byval, which inhibits many
579    // optimizations.
580    if (getContext().getTypeSize(Ty) <= 4*32 &&
581        canExpandIndirectArgument(Ty, getContext()))
582      return ABIArgInfo::getExpand();
583
584    return getIndirectResult(Ty);
585  }
586
587  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
588    Ty = EnumTy->getDecl()->getIntegerType();
589
590  return (Ty->isPromotableIntegerType() ?
591          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
592}
593
594llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
595                                      CodeGenFunction &CGF) const {
596  const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
597  const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
598
599  CGBuilderTy &Builder = CGF.Builder;
600  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
601                                                       "ap");
602  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
603  llvm::Type *PTy =
604    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
605  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
606
607  uint64_t Offset =
608    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
609  llvm::Value *NextAddr =
610    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
611                      "ap.next");
612  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
613
614  return AddrTyped;
615}
616
617void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
618                                                  llvm::GlobalValue *GV,
619                                            CodeGen::CodeGenModule &CGM) const {
620  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
621    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
622      // Get the LLVM function.
623      llvm::Function *Fn = cast<llvm::Function>(GV);
624
625      // Now add the 'alignstack' attribute with a value of 16.
626      Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
627    }
628  }
629}
630
631bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
632                                               CodeGen::CodeGenFunction &CGF,
633                                               llvm::Value *Address) const {
634  CodeGen::CGBuilderTy &Builder = CGF.Builder;
635  llvm::LLVMContext &Context = CGF.getLLVMContext();
636
637  const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
638  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
639
640  // 0-7 are the eight integer registers;  the order is different
641  //   on Darwin (for EH), but the range is the same.
642  // 8 is %eip.
643  AssignToArrayRange(Builder, Address, Four8, 0, 8);
644
645  if (CGF.CGM.isTargetDarwin()) {
646    // 12-16 are st(0..4).  Not sure why we stop at 4.
647    // These have size 16, which is sizeof(long double) on
648    // platforms with 8-byte alignment for that type.
649    llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
650    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
651
652  } else {
653    // 9 is %eflags, which doesn't get a size on Darwin for some
654    // reason.
655    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
656
657    // 11-16 are st(0..5).  Not sure why we stop at 5.
658    // These have size 12, which is sizeof(long double) on
659    // platforms with 4-byte alignment for that type.
660    llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
661    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
662  }
663
664  return false;
665}
666
667//===----------------------------------------------------------------------===//
668// X86-64 ABI Implementation
669//===----------------------------------------------------------------------===//
670
671
672namespace {
673/// X86_64ABIInfo - The X86_64 ABI information.
674class X86_64ABIInfo : public ABIInfo {
675  enum Class {
676    Integer = 0,
677    SSE,
678    SSEUp,
679    X87,
680    X87Up,
681    ComplexX87,
682    NoClass,
683    Memory
684  };
685
686  /// merge - Implement the X86_64 ABI merging algorithm.
687  ///
688  /// Merge an accumulating classification \arg Accum with a field
689  /// classification \arg Field.
690  ///
691  /// \param Accum - The accumulating classification. This should
692  /// always be either NoClass or the result of a previous merge
693  /// call. In addition, this should never be Memory (the caller
694  /// should just return Memory for the aggregate).
695  static Class merge(Class Accum, Class Field);
696
697  /// classify - Determine the x86_64 register classes in which the
698  /// given type T should be passed.
699  ///
700  /// \param Lo - The classification for the parts of the type
701  /// residing in the low word of the containing object.
702  ///
703  /// \param Hi - The classification for the parts of the type
704  /// residing in the high word of the containing object.
705  ///
706  /// \param OffsetBase - The bit offset of this type in the
707  /// containing object.  Some parameters are classified different
708  /// depending on whether they straddle an eightbyte boundary.
709  ///
710  /// If a word is unused its result will be NoClass; if a type should
711  /// be passed in Memory then at least the classification of \arg Lo
712  /// will be Memory.
713  ///
714  /// The \arg Lo class will be NoClass iff the argument is ignored.
715  ///
716  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
717  /// also be ComplexX87.
718  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
719
720  const llvm::Type *Get16ByteVectorType(QualType Ty) const;
721  const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
722                                       unsigned IROffset, QualType SourceTy,
723                                       unsigned SourceOffset) const;
724  const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
725                                           unsigned IROffset, QualType SourceTy,
726                                           unsigned SourceOffset) const;
727
728  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
729  /// such that the argument will be returned in memory.
730  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
731
732  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
733  /// such that the argument will be passed in memory.
734  ABIArgInfo getIndirectResult(QualType Ty) const;
735
736  ABIArgInfo classifyReturnType(QualType RetTy) const;
737
738  ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
739                                  unsigned &neededSSE) const;
740
741public:
742  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
743
744  virtual void computeInfo(CGFunctionInfo &FI) const;
745
746  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
747                                 CodeGenFunction &CGF) const;
748};
749
750class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
751public:
752  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
753    : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
754
755  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
756    return 7;
757  }
758
759  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
760                               llvm::Value *Address) const {
761    CodeGen::CGBuilderTy &Builder = CGF.Builder;
762    llvm::LLVMContext &Context = CGF.getLLVMContext();
763
764    const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
765    llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
766
767    // 0-15 are the 16 integer registers.
768    // 16 is %rip.
769    AssignToArrayRange(Builder, Address, Eight8, 0, 16);
770
771    return false;
772  }
773};
774
775}
776
777X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
778  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
779  // classified recursively so that always two fields are
780  // considered. The resulting class is calculated according to
781  // the classes of the fields in the eightbyte:
782  //
783  // (a) If both classes are equal, this is the resulting class.
784  //
785  // (b) If one of the classes is NO_CLASS, the resulting class is
786  // the other class.
787  //
788  // (c) If one of the classes is MEMORY, the result is the MEMORY
789  // class.
790  //
791  // (d) If one of the classes is INTEGER, the result is the
792  // INTEGER.
793  //
794  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
795  // MEMORY is used as class.
796  //
797  // (f) Otherwise class SSE is used.
798
799  // Accum should never be memory (we should have returned) or
800  // ComplexX87 (because this cannot be passed in a structure).
801  assert((Accum != Memory && Accum != ComplexX87) &&
802         "Invalid accumulated classification during merge.");
803  if (Accum == Field || Field == NoClass)
804    return Accum;
805  if (Field == Memory)
806    return Memory;
807  if (Accum == NoClass)
808    return Field;
809  if (Accum == Integer || Field == Integer)
810    return Integer;
811  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
812      Accum == X87 || Accum == X87Up)
813    return Memory;
814  return SSE;
815}
816
817void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
818                             Class &Lo, Class &Hi) const {
819  // FIXME: This code can be simplified by introducing a simple value class for
820  // Class pairs with appropriate constructor methods for the various
821  // situations.
822
823  // FIXME: Some of the split computations are wrong; unaligned vectors
824  // shouldn't be passed in registers for example, so there is no chance they
825  // can straddle an eightbyte. Verify & simplify.
826
827  Lo = Hi = NoClass;
828
829  Class &Current = OffsetBase < 64 ? Lo : Hi;
830  Current = Memory;
831
832  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
833    BuiltinType::Kind k = BT->getKind();
834
835    if (k == BuiltinType::Void) {
836      Current = NoClass;
837    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
838      Lo = Integer;
839      Hi = Integer;
840    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
841      Current = Integer;
842    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
843      Current = SSE;
844    } else if (k == BuiltinType::LongDouble) {
845      Lo = X87;
846      Hi = X87Up;
847    }
848    // FIXME: _Decimal32 and _Decimal64 are SSE.
849    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
850    return;
851  }
852
853  if (const EnumType *ET = Ty->getAs<EnumType>()) {
854    // Classify the underlying integer type.
855    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
856    return;
857  }
858
859  if (Ty->hasPointerRepresentation()) {
860    Current = Integer;
861    return;
862  }
863
864  if (Ty->isMemberPointerType()) {
865    if (Ty->isMemberFunctionPointerType())
866      Lo = Hi = Integer;
867    else
868      Current = Integer;
869    return;
870  }
871
872  if (const VectorType *VT = Ty->getAs<VectorType>()) {
873    uint64_t Size = getContext().getTypeSize(VT);
874    if (Size == 32) {
875      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
876      // float> as integer.
877      Current = Integer;
878
879      // If this type crosses an eightbyte boundary, it should be
880      // split.
881      uint64_t EB_Real = (OffsetBase) / 64;
882      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
883      if (EB_Real != EB_Imag)
884        Hi = Lo;
885    } else if (Size == 64) {
886      // gcc passes <1 x double> in memory. :(
887      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
888        return;
889
890      // gcc passes <1 x long long> as INTEGER.
891      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
892        Current = Integer;
893      else
894        Current = SSE;
895
896      // If this type crosses an eightbyte boundary, it should be
897      // split.
898      if (OffsetBase && OffsetBase != 64)
899        Hi = Lo;
900    } else if (Size == 128) {
901      Lo = SSE;
902      Hi = SSEUp;
903    }
904    return;
905  }
906
907  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
908    QualType ET = getContext().getCanonicalType(CT->getElementType());
909
910    uint64_t Size = getContext().getTypeSize(Ty);
911    if (ET->isIntegralOrEnumerationType()) {
912      if (Size <= 64)
913        Current = Integer;
914      else if (Size <= 128)
915        Lo = Hi = Integer;
916    } else if (ET == getContext().FloatTy)
917      Current = SSE;
918    else if (ET == getContext().DoubleTy)
919      Lo = Hi = SSE;
920    else if (ET == getContext().LongDoubleTy)
921      Current = ComplexX87;
922
923    // If this complex type crosses an eightbyte boundary then it
924    // should be split.
925    uint64_t EB_Real = (OffsetBase) / 64;
926    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
927    if (Hi == NoClass && EB_Real != EB_Imag)
928      Hi = Lo;
929
930    return;
931  }
932
933  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
934    // Arrays are treated like structures.
935
936    uint64_t Size = getContext().getTypeSize(Ty);
937
938    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
939    // than two eightbytes, ..., it has class MEMORY.
940    if (Size > 128)
941      return;
942
943    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
944    // fields, it has class MEMORY.
945    //
946    // Only need to check alignment of array base.
947    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
948      return;
949
950    // Otherwise implement simplified merge. We could be smarter about
951    // this, but it isn't worth it and would be harder to verify.
952    Current = NoClass;
953    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
954    uint64_t ArraySize = AT->getSize().getZExtValue();
955    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
956      Class FieldLo, FieldHi;
957      classify(AT->getElementType(), Offset, FieldLo, FieldHi);
958      Lo = merge(Lo, FieldLo);
959      Hi = merge(Hi, FieldHi);
960      if (Lo == Memory || Hi == Memory)
961        break;
962    }
963
964    // Do post merger cleanup (see below). Only case we worry about is Memory.
965    if (Hi == Memory)
966      Lo = Memory;
967    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
968    return;
969  }
970
971  if (const RecordType *RT = Ty->getAs<RecordType>()) {
972    uint64_t Size = getContext().getTypeSize(Ty);
973
974    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
975    // than two eightbytes, ..., it has class MEMORY.
976    if (Size > 128)
977      return;
978
979    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
980    // copy constructor or a non-trivial destructor, it is passed by invisible
981    // reference.
982    if (hasNonTrivialDestructorOrCopyConstructor(RT))
983      return;
984
985    const RecordDecl *RD = RT->getDecl();
986
987    // Assume variable sized types are passed in memory.
988    if (RD->hasFlexibleArrayMember())
989      return;
990
991    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
992
993    // Reset Lo class, this will be recomputed.
994    Current = NoClass;
995
996    // If this is a C++ record, classify the bases first.
997    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
998      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
999             e = CXXRD->bases_end(); i != e; ++i) {
1000        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1001               "Unexpected base class!");
1002        const CXXRecordDecl *Base =
1003          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1004
1005        // Classify this field.
1006        //
1007        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1008        // single eightbyte, each is classified separately. Each eightbyte gets
1009        // initialized to class NO_CLASS.
1010        Class FieldLo, FieldHi;
1011        uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
1012        classify(i->getType(), Offset, FieldLo, FieldHi);
1013        Lo = merge(Lo, FieldLo);
1014        Hi = merge(Hi, FieldHi);
1015        if (Lo == Memory || Hi == Memory)
1016          break;
1017      }
1018
1019      // If this record has no fields, no bases, no vtable, but isn't empty,
1020      // classify as INTEGER.
1021      if (CXXRD->isEmpty() && Size)
1022        Current = Integer;
1023    }
1024
1025    // Classify the fields one at a time, merging the results.
1026    unsigned idx = 0;
1027    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1028           i != e; ++i, ++idx) {
1029      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1030      bool BitField = i->isBitField();
1031
1032      // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1033      // fields, it has class MEMORY.
1034      //
1035      // Note, skip this test for bit-fields, see below.
1036      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1037        Lo = Memory;
1038        return;
1039      }
1040
1041      // Classify this field.
1042      //
1043      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1044      // exceeds a single eightbyte, each is classified
1045      // separately. Each eightbyte gets initialized to class
1046      // NO_CLASS.
1047      Class FieldLo, FieldHi;
1048
1049      // Bit-fields require special handling, they do not force the
1050      // structure to be passed in memory even if unaligned, and
1051      // therefore they can straddle an eightbyte.
1052      if (BitField) {
1053        // Ignore padding bit-fields.
1054        if (i->isUnnamedBitfield())
1055          continue;
1056
1057        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1058        uint64_t Size =
1059          i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
1060
1061        uint64_t EB_Lo = Offset / 64;
1062        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1063        FieldLo = FieldHi = NoClass;
1064        if (EB_Lo) {
1065          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1066          FieldLo = NoClass;
1067          FieldHi = Integer;
1068        } else {
1069          FieldLo = Integer;
1070          FieldHi = EB_Hi ? Integer : NoClass;
1071        }
1072      } else
1073        classify(i->getType(), Offset, FieldLo, FieldHi);
1074      Lo = merge(Lo, FieldLo);
1075      Hi = merge(Hi, FieldHi);
1076      if (Lo == Memory || Hi == Memory)
1077        break;
1078    }
1079
1080    // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1081    //
1082    // (a) If one of the classes is MEMORY, the whole argument is
1083    // passed in memory.
1084    //
1085    // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1086
1087    // The first of these conditions is guaranteed by how we implement
1088    // the merge (just bail).
1089    //
1090    // The second condition occurs in the case of unions; for example
1091    // union { _Complex double; unsigned; }.
1092    if (Hi == Memory)
1093      Lo = Memory;
1094    if (Hi == SSEUp && Lo != SSE)
1095      Hi = SSE;
1096  }
1097}
1098
1099ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
1100  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1101  // place naturally.
1102  if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1103    // Treat an enum type as its underlying type.
1104    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1105      Ty = EnumTy->getDecl()->getIntegerType();
1106
1107    return (Ty->isPromotableIntegerType() ?
1108            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1109  }
1110
1111  return ABIArgInfo::getIndirect(0);
1112}
1113
1114ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
1115  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1116  // place naturally.
1117  if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1118    // Treat an enum type as its underlying type.
1119    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1120      Ty = EnumTy->getDecl()->getIntegerType();
1121
1122    return (Ty->isPromotableIntegerType() ?
1123            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1124  }
1125
1126  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1127    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1128
1129  // Compute the byval alignment. We trust the back-end to honor the
1130  // minimum ABI alignment for byval, to make cleaner IR.
1131  const unsigned MinABIAlign = 8;
1132  unsigned Align = getContext().getTypeAlign(Ty) / 8;
1133  if (Align > MinABIAlign)
1134    return ABIArgInfo::getIndirect(Align);
1135  return ABIArgInfo::getIndirect(0);
1136}
1137
1138/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1139/// full vector XMM register.  Pick an LLVM IR type that will be passed as a
1140/// vector register.
1141const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
1142  const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
1143
1144  // Wrapper structs that just contain vectors are passed just like vectors,
1145  // strip them off if present.
1146  const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1147  while (STy && STy->getNumElements() == 1) {
1148    IRType = STy->getElementType(0);
1149    STy = dyn_cast<llvm::StructType>(IRType);
1150  }
1151
1152  // If the preferred type is a 16-byte vector, prefer to pass it.
1153  if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1154    const llvm::Type *EltTy = VT->getElementType();
1155    if (VT->getBitWidth() == 128 &&
1156        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1157         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1158         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1159         EltTy->isIntegerTy(128)))
1160      return VT;
1161  }
1162
1163  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1164}
1165
1166/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1167/// is known to either be off the end of the specified type or being in
1168/// alignment padding.  The user type specified is known to be at most 128 bits
1169/// in size, and have passed through X86_64ABIInfo::classify with a successful
1170/// classification that put one of the two halves in the INTEGER class.
1171///
1172/// It is conservatively correct to return false.
1173static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1174                                  unsigned EndBit, ASTContext &Context) {
1175  // If the bytes being queried are off the end of the type, there is no user
1176  // data hiding here.  This handles analysis of builtins, vectors and other
1177  // types that don't contain interesting padding.
1178  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1179  if (TySize <= StartBit)
1180    return true;
1181
1182  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1183    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1184    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1185
1186    // Check each element to see if the element overlaps with the queried range.
1187    for (unsigned i = 0; i != NumElts; ++i) {
1188      // If the element is after the span we care about, then we're done..
1189      unsigned EltOffset = i*EltSize;
1190      if (EltOffset >= EndBit) break;
1191
1192      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1193      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1194                                 EndBit-EltOffset, Context))
1195        return false;
1196    }
1197    // If it overlaps no elements, then it is safe to process as padding.
1198    return true;
1199  }
1200
1201  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1202    const RecordDecl *RD = RT->getDecl();
1203    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1204
1205    // If this is a C++ record, check the bases first.
1206    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1207      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1208           e = CXXRD->bases_end(); i != e; ++i) {
1209        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1210               "Unexpected base class!");
1211        const CXXRecordDecl *Base =
1212          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1213
1214        // If the base is after the span we care about, ignore it.
1215        unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1216        if (BaseOffset >= EndBit) continue;
1217
1218        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1219        if (!BitsContainNoUserData(i->getType(), BaseStart,
1220                                   EndBit-BaseOffset, Context))
1221          return false;
1222      }
1223    }
1224
1225    // Verify that no field has data that overlaps the region of interest.  Yes
1226    // this could be sped up a lot by being smarter about queried fields,
1227    // however we're only looking at structs up to 16 bytes, so we don't care
1228    // much.
1229    unsigned idx = 0;
1230    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1231         i != e; ++i, ++idx) {
1232      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
1233
1234      // If we found a field after the region we care about, then we're done.
1235      if (FieldOffset >= EndBit) break;
1236
1237      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1238      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1239                                 Context))
1240        return false;
1241    }
1242
1243    // If nothing in this record overlapped the area of interest, then we're
1244    // clean.
1245    return true;
1246  }
1247
1248  return false;
1249}
1250
1251/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1252/// float member at the specified offset.  For example, {int,{float}} has a
1253/// float at offset 4.  It is conservatively correct for this routine to return
1254/// false.
1255static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1256                                  const llvm::TargetData &TD) {
1257  // Base case if we find a float.
1258  if (IROffset == 0 && IRType->isFloatTy())
1259    return true;
1260
1261  // If this is a struct, recurse into the field at the specified offset.
1262  if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1263    const llvm::StructLayout *SL = TD.getStructLayout(STy);
1264    unsigned Elt = SL->getElementContainingOffset(IROffset);
1265    IROffset -= SL->getElementOffset(Elt);
1266    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1267  }
1268
1269  // If this is an array, recurse into the field at the specified offset.
1270  if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1271    const llvm::Type *EltTy = ATy->getElementType();
1272    unsigned EltSize = TD.getTypeAllocSize(EltTy);
1273    IROffset -= IROffset/EltSize*EltSize;
1274    return ContainsFloatAtOffset(EltTy, IROffset, TD);
1275  }
1276
1277  return false;
1278}
1279
1280
1281/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1282/// low 8 bytes of an XMM register, corresponding to the SSE class.
1283const llvm::Type *X86_64ABIInfo::
1284GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1285                   QualType SourceTy, unsigned SourceOffset) const {
1286  // The only three choices we have are either double, <2 x float>, or float. We
1287  // pass as float if the last 4 bytes is just padding.  This happens for
1288  // structs that contain 3 floats.
1289  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1290                            SourceOffset*8+64, getContext()))
1291    return llvm::Type::getFloatTy(getVMContext());
1292
1293  // We want to pass as <2 x float> if the LLVM IR type contains a float at
1294  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
1295  // case.
1296  if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
1297      ContainsFloatAtOffset(IRType, IROffset+4, getTargetData())) {
1298    // FIXME: <2 x float> doesn't pass as one XMM register yet.  Don't enable
1299    // this code until it does.
1300    //return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
1301
1302  }
1303
1304  return llvm::Type::getDoubleTy(getVMContext());
1305}
1306
1307
1308/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1309/// an 8-byte GPR.  This means that we either have a scalar or we are talking
1310/// about the high or low part of an up-to-16-byte struct.  This routine picks
1311/// the best LLVM IR type to represent this, which may be i64 or may be anything
1312/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1313/// etc).
1314///
1315/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1316/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1317/// the 8-byte value references.  PrefType may be null.
1318///
1319/// SourceTy is the source level type for the entire argument.  SourceOffset is
1320/// an offset into this that we're processing (which is always either 0 or 8).
1321///
1322const llvm::Type *X86_64ABIInfo::
1323GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1324                       QualType SourceTy, unsigned SourceOffset) const {
1325  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1326  // returning an 8-byte unit starting with it.  See if we can safely use it.
1327  if (IROffset == 0) {
1328    // Pointers and int64's always fill the 8-byte unit.
1329    if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1330      return IRType;
1331
1332    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1333    // goodness in the source type is just tail padding.  This is allowed to
1334    // kick in for struct {double,int} on the int, but not on
1335    // struct{double,int,int} because we wouldn't return the second int.  We
1336    // have to do this analysis on the source type because we can't depend on
1337    // unions being lowered a specific way etc.
1338    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1339        IRType->isIntegerTy(32)) {
1340      unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
1341
1342      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1343                                SourceOffset*8+64, getContext()))
1344        return IRType;
1345    }
1346  }
1347
1348  if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1349    // If this is a struct, recurse into the field at the specified offset.
1350    const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
1351    if (IROffset < SL->getSizeInBytes()) {
1352      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1353      IROffset -= SL->getElementOffset(FieldIdx);
1354
1355      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1356                                    SourceTy, SourceOffset);
1357    }
1358  }
1359
1360  if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1361    const llvm::Type *EltTy = ATy->getElementType();
1362    unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1363    unsigned EltOffset = IROffset/EltSize*EltSize;
1364    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1365                                  SourceOffset);
1366  }
1367
1368  // Okay, we don't have any better idea of what to pass, so we pass this in an
1369  // integer register that isn't too big to fit the rest of the struct.
1370  unsigned TySizeInBytes =
1371    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
1372
1373  assert(TySizeInBytes != SourceOffset && "Empty field?");
1374
1375  // It is always safe to classify this as an integer type up to i64 that
1376  // isn't larger than the structure.
1377  return llvm::IntegerType::get(getVMContext(),
1378                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
1379}
1380
1381ABIArgInfo X86_64ABIInfo::
1382classifyReturnType(QualType RetTy) const {
1383  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1384  // classification algorithm.
1385  X86_64ABIInfo::Class Lo, Hi;
1386  classify(RetTy, 0, Lo, Hi);
1387
1388  // Check some invariants.
1389  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1390  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1391  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1392
1393  const llvm::Type *ResType = 0;
1394  switch (Lo) {
1395  case NoClass:
1396    return ABIArgInfo::getIgnore();
1397
1398  case SSEUp:
1399  case X87Up:
1400    assert(0 && "Invalid classification for lo word.");
1401
1402    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1403    // hidden argument.
1404  case Memory:
1405    return getIndirectReturnResult(RetTy);
1406
1407    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1408    // available register of the sequence %rax, %rdx is used.
1409  case Integer:
1410    ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1411                                     RetTy, 0);
1412
1413    // If we have a sign or zero extended integer, make sure to return Extend
1414    // so that the parameter gets the right LLVM IR attributes.
1415    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1416      // Treat an enum type as its underlying type.
1417      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1418        RetTy = EnumTy->getDecl()->getIntegerType();
1419
1420      if (RetTy->isIntegralOrEnumerationType() &&
1421          RetTy->isPromotableIntegerType())
1422        return ABIArgInfo::getExtend();
1423    }
1424    break;
1425
1426    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1427    // available SSE register of the sequence %xmm0, %xmm1 is used.
1428  case SSE:
1429    ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
1430    break;
1431
1432    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1433    // returned on the X87 stack in %st0 as 80-bit x87 number.
1434  case X87:
1435    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
1436    break;
1437
1438    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1439    // part of the value is returned in %st0 and the imaginary part in
1440    // %st1.
1441  case ComplexX87:
1442    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
1443    ResType = llvm::StructType::get(getVMContext(),
1444                                    llvm::Type::getX86_FP80Ty(getVMContext()),
1445                                    llvm::Type::getX86_FP80Ty(getVMContext()),
1446                                    NULL);
1447    break;
1448  }
1449
1450  switch (Hi) {
1451    // Memory was handled previously and X87 should
1452    // never occur as a hi class.
1453  case Memory:
1454  case X87:
1455    assert(0 && "Invalid classification for hi word.");
1456
1457  case ComplexX87: // Previously handled.
1458  case NoClass:
1459    break;
1460
1461  case Integer: {
1462    const llvm::Type *HiType =
1463      GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1464    ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1465    break;
1466  }
1467  case SSE: {
1468    const llvm::Type *HiType =
1469      GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1470    ResType = llvm::StructType::get(getVMContext(), ResType, HiType,NULL);
1471    break;
1472  }
1473
1474    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1475    // is passed in the upper half of the last used SSE register.
1476    //
1477    // SSEUP should always be preceeded by SSE, just widen.
1478  case SSEUp:
1479    assert(Lo == SSE && "Unexpected SSEUp classification.");
1480    ResType = Get16ByteVectorType(RetTy);
1481    break;
1482
1483    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1484    // returned together with the previous X87 value in %st0.
1485  case X87Up:
1486    // If X87Up is preceeded by X87, we don't need to do
1487    // anything. However, in some cases with unions it may not be
1488    // preceeded by X87. In such situations we follow gcc and pass the
1489    // extra bits in an SSE reg.
1490    if (Lo != X87) {
1491      const llvm::Type *HiType =
1492        GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1493      ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1494    }
1495    break;
1496  }
1497
1498  return ABIArgInfo::getDirect(ResType);
1499}
1500
1501ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
1502                                               unsigned &neededSSE) const {
1503  X86_64ABIInfo::Class Lo, Hi;
1504  classify(Ty, 0, Lo, Hi);
1505
1506  // Check some invariants.
1507  // FIXME: Enforce these by construction.
1508  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1509  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1510  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1511
1512  neededInt = 0;
1513  neededSSE = 0;
1514  const llvm::Type *ResType = 0;
1515  switch (Lo) {
1516  case NoClass:
1517    return ABIArgInfo::getIgnore();
1518
1519    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1520    // on the stack.
1521  case Memory:
1522
1523    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1524    // COMPLEX_X87, it is passed in memory.
1525  case X87:
1526  case ComplexX87:
1527    return getIndirectResult(Ty);
1528
1529  case SSEUp:
1530  case X87Up:
1531    assert(0 && "Invalid classification for lo word.");
1532
1533    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1534    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1535    // and %r9 is used.
1536  case Integer:
1537    ++neededInt;
1538
1539    // Pick an 8-byte type based on the preferred type.
1540    ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
1541
1542    // If we have a sign or zero extended integer, make sure to return Extend
1543    // so that the parameter gets the right LLVM IR attributes.
1544    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1545      // Treat an enum type as its underlying type.
1546      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1547        Ty = EnumTy->getDecl()->getIntegerType();
1548
1549      if (Ty->isIntegralOrEnumerationType() &&
1550          Ty->isPromotableIntegerType())
1551        return ABIArgInfo::getExtend();
1552    }
1553
1554    break;
1555
1556    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1557    // available SSE register is used, the registers are taken in the
1558    // order from %xmm0 to %xmm7.
1559  case SSE:
1560    ++neededSSE;
1561    ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
1562    break;
1563  }
1564
1565  switch (Hi) {
1566    // Memory was handled previously, ComplexX87 and X87 should
1567    // never occur as hi classes, and X87Up must be preceed by X87,
1568    // which is passed in memory.
1569  case Memory:
1570  case X87:
1571  case ComplexX87:
1572    assert(0 && "Invalid classification for hi word.");
1573    break;
1574
1575  case NoClass: break;
1576
1577  case Integer: {
1578    ++neededInt;
1579    // Pick an 8-byte type based on the preferred type.
1580    const llvm::Type *HiType =
1581      GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
1582    ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1583    break;
1584  }
1585
1586    // X87Up generally doesn't occur here (long double is passed in
1587    // memory), except in situations involving unions.
1588  case X87Up:
1589  case SSE: {
1590    const llvm::Type *HiType =
1591      GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
1592    ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1593    ++neededSSE;
1594    break;
1595  }
1596
1597    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1598    // eightbyte is passed in the upper half of the last used SSE
1599    // register.  This only happens when 128-bit vectors are passed.
1600  case SSEUp:
1601    assert(Lo == SSE && "Unexpected SSEUp classification");
1602    ResType = Get16ByteVectorType(Ty);
1603    break;
1604  }
1605
1606  return ABIArgInfo::getDirect(ResType);
1607}
1608
1609void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1610
1611  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
1612
1613  // Keep track of the number of assigned registers.
1614  unsigned freeIntRegs = 6, freeSSERegs = 8;
1615
1616  // If the return value is indirect, then the hidden argument is consuming one
1617  // integer register.
1618  if (FI.getReturnInfo().isIndirect())
1619    --freeIntRegs;
1620
1621  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1622  // get assigned (in left-to-right order) for passing as follows...
1623  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1624       it != ie; ++it) {
1625    unsigned neededInt, neededSSE;
1626    it->info = classifyArgumentType(it->type, neededInt, neededSSE);
1627
1628    // AMD64-ABI 3.2.3p3: If there are no registers available for any
1629    // eightbyte of an argument, the whole argument is passed on the
1630    // stack. If registers have already been assigned for some
1631    // eightbytes of such an argument, the assignments get reverted.
1632    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1633      freeIntRegs -= neededInt;
1634      freeSSERegs -= neededSSE;
1635    } else {
1636      it->info = getIndirectResult(it->type);
1637    }
1638  }
1639}
1640
1641static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1642                                        QualType Ty,
1643                                        CodeGenFunction &CGF) {
1644  llvm::Value *overflow_arg_area_p =
1645    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1646  llvm::Value *overflow_arg_area =
1647    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1648
1649  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1650  // byte boundary if alignment needed by type exceeds 8 byte boundary.
1651  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1652  if (Align > 8) {
1653    // Note that we follow the ABI & gcc here, even though the type
1654    // could in theory have an alignment greater than 16. This case
1655    // shouldn't ever matter in practice.
1656
1657    // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1658    llvm::Value *Offset =
1659      llvm::ConstantInt::get(CGF.Int32Ty, 15);
1660    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1661    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1662                                                    CGF.Int64Ty);
1663    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
1664    overflow_arg_area =
1665      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1666                                 overflow_arg_area->getType(),
1667                                 "overflow_arg_area.align");
1668  }
1669
1670  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1671  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1672  llvm::Value *Res =
1673    CGF.Builder.CreateBitCast(overflow_arg_area,
1674                              llvm::PointerType::getUnqual(LTy));
1675
1676  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1677  // l->overflow_arg_area + sizeof(type).
1678  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1679  // an 8 byte boundary.
1680
1681  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1682  llvm::Value *Offset =
1683      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
1684  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1685                                            "overflow_arg_area.next");
1686  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1687
1688  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1689  return Res;
1690}
1691
1692llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1693                                      CodeGenFunction &CGF) const {
1694  llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1695
1696  // Assume that va_list type is correct; should be pointer to LLVM type:
1697  // struct {
1698  //   i32 gp_offset;
1699  //   i32 fp_offset;
1700  //   i8* overflow_arg_area;
1701  //   i8* reg_save_area;
1702  // };
1703  unsigned neededInt, neededSSE;
1704
1705  Ty = CGF.getContext().getCanonicalType(Ty);
1706  ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
1707
1708  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1709  // in the registers. If not go to step 7.
1710  if (!neededInt && !neededSSE)
1711    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1712
1713  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1714  // general purpose registers needed to pass type and num_fp to hold
1715  // the number of floating point registers needed.
1716
1717  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1718  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1719  // l->fp_offset > 304 - num_fp * 16 go to step 7.
1720  //
1721  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1722  // register save space).
1723
1724  llvm::Value *InRegs = 0;
1725  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1726  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1727  if (neededInt) {
1728    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1729    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1730    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1731    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
1732  }
1733
1734  if (neededSSE) {
1735    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1736    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1737    llvm::Value *FitsInFP =
1738      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1739    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
1740    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1741  }
1742
1743  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1744  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1745  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1746  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1747
1748  // Emit code to load the value if it was passed in registers.
1749
1750  CGF.EmitBlock(InRegBlock);
1751
1752  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1753  // an offset of l->gp_offset and/or l->fp_offset. This may require
1754  // copying to a temporary location in case the parameter is passed
1755  // in different register classes or requires an alignment greater
1756  // than 8 for general purpose registers and 16 for XMM registers.
1757  //
1758  // FIXME: This really results in shameful code when we end up needing to
1759  // collect arguments from different places; often what should result in a
1760  // simple assembling of a structure from scattered addresses has many more
1761  // loads than necessary. Can we clean this up?
1762  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1763  llvm::Value *RegAddr =
1764    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1765                           "reg_save_area");
1766  if (neededInt && neededSSE) {
1767    // FIXME: Cleanup.
1768    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
1769    const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1770    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1771    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1772    const llvm::Type *TyLo = ST->getElementType(0);
1773    const llvm::Type *TyHi = ST->getElementType(1);
1774    assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
1775           "Unexpected ABI info for mixed regs");
1776    const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1777    const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1778    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1779    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1780    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1781    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
1782    llvm::Value *V =
1783      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1784    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1785    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1786    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1787
1788    RegAddr = CGF.Builder.CreateBitCast(Tmp,
1789                                        llvm::PointerType::getUnqual(LTy));
1790  } else if (neededInt) {
1791    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1792    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1793                                        llvm::PointerType::getUnqual(LTy));
1794  } else if (neededSSE == 1) {
1795    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1796    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1797                                        llvm::PointerType::getUnqual(LTy));
1798  } else {
1799    assert(neededSSE == 2 && "Invalid number of needed registers!");
1800    // SSE registers are spaced 16 bytes apart in the register save
1801    // area, we need to collect the two eightbytes together.
1802    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1803    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
1804    const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1805    const llvm::Type *DblPtrTy =
1806      llvm::PointerType::getUnqual(DoubleTy);
1807    const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1808                                                       DoubleTy, NULL);
1809    llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1810    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1811                                                         DblPtrTy));
1812    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1813    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1814                                                         DblPtrTy));
1815    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1816    RegAddr = CGF.Builder.CreateBitCast(Tmp,
1817                                        llvm::PointerType::getUnqual(LTy));
1818  }
1819
1820  // AMD64-ABI 3.5.7p5: Step 5. Set:
1821  // l->gp_offset = l->gp_offset + num_gp * 8
1822  // l->fp_offset = l->fp_offset + num_fp * 16.
1823  if (neededInt) {
1824    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
1825    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1826                            gp_offset_p);
1827  }
1828  if (neededSSE) {
1829    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
1830    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1831                            fp_offset_p);
1832  }
1833  CGF.EmitBranch(ContBlock);
1834
1835  // Emit code to load the value if it was passed in memory.
1836
1837  CGF.EmitBlock(InMemBlock);
1838  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1839
1840  // Return the appropriate result.
1841
1842  CGF.EmitBlock(ContBlock);
1843  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1844                                                 "vaarg.addr");
1845  ResAddr->reserveOperandSpace(2);
1846  ResAddr->addIncoming(RegAddr, InRegBlock);
1847  ResAddr->addIncoming(MemAddr, InMemBlock);
1848  return ResAddr;
1849}
1850
1851
1852
1853//===----------------------------------------------------------------------===//
1854// PIC16 ABI Implementation
1855//===----------------------------------------------------------------------===//
1856
1857namespace {
1858
1859class PIC16ABIInfo : public ABIInfo {
1860public:
1861  PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1862
1863  ABIArgInfo classifyReturnType(QualType RetTy) const;
1864
1865  ABIArgInfo classifyArgumentType(QualType RetTy) const;
1866
1867  virtual void computeInfo(CGFunctionInfo &FI) const {
1868    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
1869    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1870         it != ie; ++it)
1871      it->info = classifyArgumentType(it->type);
1872  }
1873
1874  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1875                                 CodeGenFunction &CGF) const;
1876};
1877
1878class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1879public:
1880  PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1881    : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
1882};
1883
1884}
1885
1886ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
1887  if (RetTy->isVoidType()) {
1888    return ABIArgInfo::getIgnore();
1889  } else {
1890    return ABIArgInfo::getDirect();
1891  }
1892}
1893
1894ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
1895  return ABIArgInfo::getDirect();
1896}
1897
1898llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1899                                     CodeGenFunction &CGF) const {
1900  const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
1901  const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1902
1903  CGBuilderTy &Builder = CGF.Builder;
1904  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1905                                                       "ap");
1906  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1907  llvm::Type *PTy =
1908    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1909  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1910
1911  uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1912
1913  llvm::Value *NextAddr =
1914    Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1915                          llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1916                      "ap.next");
1917  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1918
1919  return AddrTyped;
1920}
1921
1922
1923// PowerPC-32
1924
1925namespace {
1926class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1927public:
1928  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1929
1930  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1931    // This is recovered from gcc output.
1932    return 1; // r1 is the dedicated stack pointer
1933  }
1934
1935  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1936                               llvm::Value *Address) const;
1937};
1938
1939}
1940
1941bool
1942PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1943                                                llvm::Value *Address) const {
1944  // This is calculated from the LLVM and GCC tables and verified
1945  // against gcc output.  AFAIK all ABIs use the same encoding.
1946
1947  CodeGen::CGBuilderTy &Builder = CGF.Builder;
1948  llvm::LLVMContext &Context = CGF.getLLVMContext();
1949
1950  const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1951  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1952  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1953  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1954
1955  // 0-31: r0-31, the 4-byte general-purpose registers
1956  AssignToArrayRange(Builder, Address, Four8, 0, 31);
1957
1958  // 32-63: fp0-31, the 8-byte floating-point registers
1959  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
1960
1961  // 64-76 are various 4-byte special-purpose registers:
1962  // 64: mq
1963  // 65: lr
1964  // 66: ctr
1965  // 67: ap
1966  // 68-75 cr0-7
1967  // 76: xer
1968  AssignToArrayRange(Builder, Address, Four8, 64, 76);
1969
1970  // 77-108: v0-31, the 16-byte vector registers
1971  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
1972
1973  // 109: vrsave
1974  // 110: vscr
1975  // 111: spe_acc
1976  // 112: spefscr
1977  // 113: sfp
1978  AssignToArrayRange(Builder, Address, Four8, 109, 113);
1979
1980  return false;
1981}
1982
1983
1984//===----------------------------------------------------------------------===//
1985// ARM ABI Implementation
1986//===----------------------------------------------------------------------===//
1987
1988namespace {
1989
1990class ARMABIInfo : public ABIInfo {
1991public:
1992  enum ABIKind {
1993    APCS = 0,
1994    AAPCS = 1,
1995    AAPCS_VFP
1996  };
1997
1998private:
1999  ABIKind Kind;
2000
2001public:
2002  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
2003
2004private:
2005  ABIKind getABIKind() const { return Kind; }
2006
2007  ABIArgInfo classifyReturnType(QualType RetTy) const;
2008  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2009
2010  virtual void computeInfo(CGFunctionInfo &FI) const;
2011
2012  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2013                                 CodeGenFunction &CGF) const;
2014};
2015
2016class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2017public:
2018  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2019    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
2020
2021  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2022    return 13;
2023  }
2024};
2025
2026}
2027
2028void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
2029  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2030  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2031       it != ie; ++it)
2032    it->info = classifyArgumentType(it->type);
2033
2034  const llvm::Triple &Triple(getContext().Target.getTriple());
2035  llvm::CallingConv::ID DefaultCC;
2036  if (Triple.getEnvironmentName() == "gnueabi" ||
2037      Triple.getEnvironmentName() == "eabi")
2038    DefaultCC = llvm::CallingConv::ARM_AAPCS;
2039  else
2040    DefaultCC = llvm::CallingConv::ARM_APCS;
2041
2042  switch (getABIKind()) {
2043  case APCS:
2044    if (DefaultCC != llvm::CallingConv::ARM_APCS)
2045      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
2046    break;
2047
2048  case AAPCS:
2049    if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2050      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
2051    break;
2052
2053  case AAPCS_VFP:
2054    FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2055    break;
2056  }
2057}
2058
2059ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
2060  if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
2061    // Treat an enum type as its underlying type.
2062    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2063      Ty = EnumTy->getDecl()->getIntegerType();
2064
2065    return (Ty->isPromotableIntegerType() ?
2066            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2067  }
2068
2069  // Ignore empty records.
2070  if (isEmptyRecord(getContext(), Ty, true))
2071    return ABIArgInfo::getIgnore();
2072
2073  // Structures with either a non-trivial destructor or a non-trivial
2074  // copy constructor are always indirect.
2075  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2076    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2077
2078  // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2079  // backend doesn't support byval.
2080  // FIXME: This doesn't handle alignment > 64 bits.
2081  const llvm::Type* ElemTy;
2082  unsigned SizeRegs;
2083  if (getContext().getTypeAlign(Ty) > 32) {
2084    ElemTy = llvm::Type::getInt64Ty(getVMContext());
2085    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
2086  } else {
2087    ElemTy = llvm::Type::getInt32Ty(getVMContext());
2088    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
2089  }
2090  std::vector<const llvm::Type*> LLVMFields;
2091  LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
2092  const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2093                                                true);
2094  return ABIArgInfo::getDirect(STy);
2095}
2096
2097static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
2098                              llvm::LLVMContext &VMContext) {
2099  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2100  // is called integer-like if its size is less than or equal to one word, and
2101  // the offset of each of its addressable sub-fields is zero.
2102
2103  uint64_t Size = Context.getTypeSize(Ty);
2104
2105  // Check that the type fits in a word.
2106  if (Size > 32)
2107    return false;
2108
2109  // FIXME: Handle vector types!
2110  if (Ty->isVectorType())
2111    return false;
2112
2113  // Float types are never treated as "integer like".
2114  if (Ty->isRealFloatingType())
2115    return false;
2116
2117  // If this is a builtin or pointer type then it is ok.
2118  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
2119    return true;
2120
2121  // Small complex integer types are "integer like".
2122  if (const ComplexType *CT = Ty->getAs<ComplexType>())
2123    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
2124
2125  // Single element and zero sized arrays should be allowed, by the definition
2126  // above, but they are not.
2127
2128  // Otherwise, it must be a record type.
2129  const RecordType *RT = Ty->getAs<RecordType>();
2130  if (!RT) return false;
2131
2132  // Ignore records with flexible arrays.
2133  const RecordDecl *RD = RT->getDecl();
2134  if (RD->hasFlexibleArrayMember())
2135    return false;
2136
2137  // Check that all sub-fields are at offset 0, and are themselves "integer
2138  // like".
2139  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2140
2141  bool HadField = false;
2142  unsigned idx = 0;
2143  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2144       i != e; ++i, ++idx) {
2145    const FieldDecl *FD = *i;
2146
2147    // Bit-fields are not addressable, we only need to verify they are "integer
2148    // like". We still have to disallow a subsequent non-bitfield, for example:
2149    //   struct { int : 0; int x }
2150    // is non-integer like according to gcc.
2151    if (FD->isBitField()) {
2152      if (!RD->isUnion())
2153        HadField = true;
2154
2155      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2156        return false;
2157
2158      continue;
2159    }
2160
2161    // Check if this field is at offset 0.
2162    if (Layout.getFieldOffset(idx) != 0)
2163      return false;
2164
2165    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2166      return false;
2167
2168    // Only allow at most one field in a structure. This doesn't match the
2169    // wording above, but follows gcc in situations with a field following an
2170    // empty structure.
2171    if (!RD->isUnion()) {
2172      if (HadField)
2173        return false;
2174
2175      HadField = true;
2176    }
2177  }
2178
2179  return true;
2180}
2181
2182ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
2183  if (RetTy->isVoidType())
2184    return ABIArgInfo::getIgnore();
2185
2186  if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2187    // Treat an enum type as its underlying type.
2188    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2189      RetTy = EnumTy->getDecl()->getIntegerType();
2190
2191    return (RetTy->isPromotableIntegerType() ?
2192            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2193  }
2194
2195  // Structures with either a non-trivial destructor or a non-trivial
2196  // copy constructor are always indirect.
2197  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2198    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2199
2200  // Are we following APCS?
2201  if (getABIKind() == APCS) {
2202    if (isEmptyRecord(getContext(), RetTy, false))
2203      return ABIArgInfo::getIgnore();
2204
2205    // Complex types are all returned as packed integers.
2206    //
2207    // FIXME: Consider using 2 x vector types if the back end handles them
2208    // correctly.
2209    if (RetTy->isAnyComplexType())
2210      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2211                                              getContext().getTypeSize(RetTy)));
2212
2213    // Integer like structures are returned in r0.
2214    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
2215      // Return in the smallest viable integer type.
2216      uint64_t Size = getContext().getTypeSize(RetTy);
2217      if (Size <= 8)
2218        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
2219      if (Size <= 16)
2220        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2221      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
2222    }
2223
2224    // Otherwise return in memory.
2225    return ABIArgInfo::getIndirect(0);
2226  }
2227
2228  // Otherwise this is an AAPCS variant.
2229
2230  if (isEmptyRecord(getContext(), RetTy, true))
2231    return ABIArgInfo::getIgnore();
2232
2233  // Aggregates <= 4 bytes are returned in r0; other aggregates
2234  // are returned indirectly.
2235  uint64_t Size = getContext().getTypeSize(RetTy);
2236  if (Size <= 32) {
2237    // Return in the smallest viable integer type.
2238    if (Size <= 8)
2239      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
2240    if (Size <= 16)
2241      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2242    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
2243  }
2244
2245  return ABIArgInfo::getIndirect(0);
2246}
2247
2248llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2249                                   CodeGenFunction &CGF) const {
2250  // FIXME: Need to handle alignment
2251  const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2252  const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
2253
2254  CGBuilderTy &Builder = CGF.Builder;
2255  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2256                                                       "ap");
2257  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2258  llvm::Type *PTy =
2259    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2260  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2261
2262  uint64_t Offset =
2263    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2264  llvm::Value *NextAddr =
2265    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2266                      "ap.next");
2267  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2268
2269  return AddrTyped;
2270}
2271
2272ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2273  if (RetTy->isVoidType())
2274    return ABIArgInfo::getIgnore();
2275
2276  if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2277    return ABIArgInfo::getIndirect(0);
2278
2279  // Treat an enum type as its underlying type.
2280  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2281    RetTy = EnumTy->getDecl()->getIntegerType();
2282
2283  return (RetTy->isPromotableIntegerType() ?
2284          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2285}
2286
2287//===----------------------------------------------------------------------===//
2288// SystemZ ABI Implementation
2289//===----------------------------------------------------------------------===//
2290
2291namespace {
2292
2293class SystemZABIInfo : public ABIInfo {
2294public:
2295  SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2296
2297  bool isPromotableIntegerType(QualType Ty) const;
2298
2299  ABIArgInfo classifyReturnType(QualType RetTy) const;
2300  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2301
2302  virtual void computeInfo(CGFunctionInfo &FI) const {
2303    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2304    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2305         it != ie; ++it)
2306      it->info = classifyArgumentType(it->type);
2307  }
2308
2309  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2310                                 CodeGenFunction &CGF) const;
2311};
2312
2313class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2314public:
2315  SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2316    : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
2317};
2318
2319}
2320
2321bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2322  // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
2323  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2324    switch (BT->getKind()) {
2325    case BuiltinType::Bool:
2326    case BuiltinType::Char_S:
2327    case BuiltinType::Char_U:
2328    case BuiltinType::SChar:
2329    case BuiltinType::UChar:
2330    case BuiltinType::Short:
2331    case BuiltinType::UShort:
2332    case BuiltinType::Int:
2333    case BuiltinType::UInt:
2334      return true;
2335    default:
2336      return false;
2337    }
2338  return false;
2339}
2340
2341llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2342                                       CodeGenFunction &CGF) const {
2343  // FIXME: Implement
2344  return 0;
2345}
2346
2347
2348ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2349  if (RetTy->isVoidType())
2350    return ABIArgInfo::getIgnore();
2351  if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2352    return ABIArgInfo::getIndirect(0);
2353
2354  return (isPromotableIntegerType(RetTy) ?
2355          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2356}
2357
2358ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2359  if (CodeGenFunction::hasAggregateLLVMType(Ty))
2360    return ABIArgInfo::getIndirect(0);
2361
2362  return (isPromotableIntegerType(Ty) ?
2363          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2364}
2365
2366//===----------------------------------------------------------------------===//
2367// MSP430 ABI Implementation
2368//===----------------------------------------------------------------------===//
2369
2370namespace {
2371
2372class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2373public:
2374  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2375    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
2376  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2377                           CodeGen::CodeGenModule &M) const;
2378};
2379
2380}
2381
2382void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2383                                                  llvm::GlobalValue *GV,
2384                                             CodeGen::CodeGenModule &M) const {
2385  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2386    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2387      // Handle 'interrupt' attribute:
2388      llvm::Function *F = cast<llvm::Function>(GV);
2389
2390      // Step 1: Set ISR calling convention.
2391      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2392
2393      // Step 2: Add attributes goodness.
2394      F->addFnAttr(llvm::Attribute::NoInline);
2395
2396      // Step 3: Emit ISR vector alias.
2397      unsigned Num = attr->getNumber() + 0xffe0;
2398      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2399                            "vector_" +
2400                            llvm::LowercaseString(llvm::utohexstr(Num)),
2401                            GV, &M.getModule());
2402    }
2403  }
2404}
2405
2406//===----------------------------------------------------------------------===//
2407// MIPS ABI Implementation.  This works for both little-endian and
2408// big-endian variants.
2409//===----------------------------------------------------------------------===//
2410
2411namespace {
2412class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2413public:
2414  MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2415    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
2416
2417  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2418    return 29;
2419  }
2420
2421  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2422                               llvm::Value *Address) const;
2423};
2424}
2425
2426bool
2427MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2428                                               llvm::Value *Address) const {
2429  // This information comes from gcc's implementation, which seems to
2430  // as canonical as it gets.
2431
2432  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2433  llvm::LLVMContext &Context = CGF.getLLVMContext();
2434
2435  // Everything on MIPS is 4 bytes.  Double-precision FP registers
2436  // are aliased to pairs of single-precision FP registers.
2437  const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2438  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2439
2440  // 0-31 are the general purpose registers, $0 - $31.
2441  // 32-63 are the floating-point registers, $f0 - $f31.
2442  // 64 and 65 are the multiply/divide registers, $hi and $lo.
2443  // 66 is the (notional, I think) register for signal-handler return.
2444  AssignToArrayRange(Builder, Address, Four8, 0, 65);
2445
2446  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2447  // They are one bit wide and ignored here.
2448
2449  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2450  // (coprocessor 1 is the FP unit)
2451  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2452  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2453  // 176-181 are the DSP accumulator registers.
2454  AssignToArrayRange(Builder, Address, Four8, 80, 181);
2455
2456  return false;
2457}
2458
2459
2460const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
2461  if (TheTargetCodeGenInfo)
2462    return *TheTargetCodeGenInfo;
2463
2464  // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2465  // free it.
2466
2467  const llvm::Triple &Triple = getContext().Target.getTriple();
2468  switch (Triple.getArch()) {
2469  default:
2470    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
2471
2472  case llvm::Triple::mips:
2473  case llvm::Triple::mipsel:
2474    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
2475
2476  case llvm::Triple::arm:
2477  case llvm::Triple::thumb:
2478    // FIXME: We want to know the float calling convention as well.
2479    if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
2480      return *(TheTargetCodeGenInfo =
2481               new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
2482
2483    return *(TheTargetCodeGenInfo =
2484             new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
2485
2486  case llvm::Triple::pic16:
2487    return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
2488
2489  case llvm::Triple::ppc:
2490    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
2491
2492  case llvm::Triple::systemz:
2493    return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
2494
2495  case llvm::Triple::msp430:
2496    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
2497
2498  case llvm::Triple::x86:
2499    switch (Triple.getOS()) {
2500    case llvm::Triple::Darwin:
2501      return *(TheTargetCodeGenInfo =
2502               new X86_32TargetCodeGenInfo(Types, true, true));
2503    case llvm::Triple::Cygwin:
2504    case llvm::Triple::MinGW32:
2505    case llvm::Triple::MinGW64:
2506    case llvm::Triple::AuroraUX:
2507    case llvm::Triple::DragonFly:
2508    case llvm::Triple::FreeBSD:
2509    case llvm::Triple::OpenBSD:
2510      return *(TheTargetCodeGenInfo =
2511               new X86_32TargetCodeGenInfo(Types, false, true));
2512
2513    default:
2514      return *(TheTargetCodeGenInfo =
2515               new X86_32TargetCodeGenInfo(Types, false, false));
2516    }
2517
2518  case llvm::Triple::x86_64:
2519    return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2520  }
2521}
2522