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