TargetInfo.cpp revision 89735b9516b1a378c6d33620a6c3a0d5705f9d04
182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
3c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//                     The LLVM Compiler Infrastructure
4c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
5c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// This file is distributed under the University of Illinois Open Source
6c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// License. See LICENSE.TXT for details.
7c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
8c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
9c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
10c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// These classes wrap the information about a call or function
11c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// definition used to handle ABI compliancy.
12c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
13c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
14c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h"
16c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "ABIInfo.h"
17ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov#include "CGCXXABI.h"
18c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "CodeGenFunction.h"
1919cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2034c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel#include "clang/Frontend/CodeGenOptions.h"
212c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar#include "llvm/ADT/Triple.h"
223b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Type.h"
2428df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar#include "llvm/Support/raw_ostream.h"
25c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace clang;
26c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace CodeGen;
27c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
28aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallstatic void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
29aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Array,
30aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Value,
31aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned FirstIndex,
32aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned LastIndex) {
33aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Alternatively, we could emit this as a loop in the source.
34aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
35aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
36aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    Builder.CreateStore(Value, Cell);
37aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
38aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
39aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
40d608cdb7c044365cf4e8764ade1e11e99c176078John McCallstatic bool isAggregateTypeForABI(QualType T) {
419d232c884ea9872d6555df0fd7359699819bc1f1John McCall  return !CodeGenFunction::hasScalarEvaluationKind(T) ||
42d608cdb7c044365cf4e8764ade1e11e99c176078John McCall         T->isMemberFunctionPointerType();
43d608cdb7c044365cf4e8764ade1e11e99c176078John McCall}
44d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
45c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton KorobeynikovABIInfo::~ABIInfo() {}
46c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
47ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanovstatic bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) {
48ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
49ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (!RD)
50ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return false;
51ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD);
52ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov}
53ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
54ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
55ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanovstatic bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) {
56ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  const RecordType *RT = T->getAs<RecordType>();
57ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (!RT)
58ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return false;
59ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  return isRecordReturnIndirect(RT, CGT);
60ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov}
61ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
62ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanovstatic CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
63ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov                                              CodeGen::CodeGenTypes &CGT) {
64ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
65ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (!RD)
66ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return CGCXXABI::RAA_Default;
67ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  return CGT.CGM.getCXXABI().getRecordArgABI(RD);
68ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov}
69ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
70ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanovstatic CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
71ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov                                              CodeGen::CodeGenTypes &CGT) {
72ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  const RecordType *RT = T->getAs<RecordType>();
73ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (!RT)
74ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return CGCXXABI::RAA_Default;
75ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  return getRecordArgABI(RT, CGT);
76ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov}
77ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
78ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris LattnerASTContext &ABIInfo::getContext() const {
79ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getContext();
80ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
81ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
82ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerllvm::LLVMContext &ABIInfo::getVMContext() const {
83ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getLLVMContext();
84ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
85ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
8625a6a84cf5067b32c271e3ba078676dee838798dMicah Villmowconst llvm::DataLayout &ABIInfo::getDataLayout() const {
8725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  return CGT.getDataLayout();
88ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
89ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
9064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCallconst TargetInfo &ABIInfo::getTarget() const {
9164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  return CGT.getTarget();
9264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall}
93ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
94c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovvoid ABIArgInfo::dump() const {
955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  raw_ostream &OS = llvm::errs();
9628df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << "(ABIArgInfo Kind=";
97c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (TheKind) {
98c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Direct:
99800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    OS << "Direct Type=";
1002acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    if (llvm::Type *Ty = getCoerceToType())
101800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      Ty->print(OS);
102800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    else
103800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      OS << "null";
104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
105cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case Extend:
10628df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Extend";
107cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    break;
108c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Ignore:
10928df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Ignore";
110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Indirect:
112dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar    OS << "Indirect Align=" << getIndirectAlign()
113e9b5d77b7bfd3e8bba05df9914a6e8c336d68ff3Joerg Sonnenberger       << " ByVal=" << getIndirectByVal()
114cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar       << " Realign=" << getIndirectRealign();
115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Expand:
11728df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Expand";
118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
12028df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << ")\n";
121c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
122c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
12382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovTargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
12482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
12549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// If someone can figure out a general rule for this, that would be great.
12649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// It's probably just doomed to be platform-dependent, though.
12749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCallunsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
12849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  // Verified for:
12949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-64     FreeBSD, Linux, Darwin
13049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-32     FreeBSD, Linux, Darwin
13149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   PowerPC    Linux, Darwin
13249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   ARM        Darwin (*not* EABI)
133c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   AArch64    Linux
13449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  return 32;
13549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall}
13649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
137de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallbool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
138de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                     const FunctionNoProtoType *fnType) const {
13901f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // The following conventions are known to require this to be false:
14001f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   x86_stdcall
14101f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   MIPS
14201f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // For everything else, we just prefer false unless we opt out.
14301f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  return false;
14401f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall}
14501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
1463190ca922d3743137e15fe0c525c04b177b9983bReid Klecknervoid
1473190ca922d3743137e15fe0c525c04b177b9983bReid KlecknerTargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
1483190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner                                             llvm::SmallString<24> &Opt) const {
1493190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  // This assumes the user is passing a library name like "rt" instead of a
1503190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  // filename like "librt.a/so", and that they don't care whether it's static or
1513190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  // dynamic.
1523190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  Opt = "-l";
1533190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  Opt += Lib;
1543190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner}
1553190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner
15698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
157c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
158f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru/// isEmptyField - Return true iff a the field is "empty", that is it
159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// is an unnamed bit-field or an (array of) empty record(s).
16098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
16198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                         bool AllowArrays) {
162c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FD->isUnnamedBitfield())
163c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
164c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  QualType FT = FD->getType();
166c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1677e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of empty records count as empty, strip them off.
1687e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of zero length always count as empty.
16998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (AllowArrays)
1707e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
1717e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman      if (AT->getSize() == 0)
1727e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman        return true;
17398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      FT = AT->getElementType();
1747e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    }
17598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
1765ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  const RecordType *RT = FT->getAs<RecordType>();
1775ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (!RT)
1785ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1795ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
1805ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // C++ record fields are never empty, at least in the Itanium ABI.
1815ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  //
1825ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // FIXME: We should use a predicate for whether this behavior is true in the
1835ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // current ABI.
1845ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (isa<CXXRecordDecl>(RT->getDecl()))
1855ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1865ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
18798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return isEmptyRecord(Context, FT, AllowArrays);
188c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
189c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
190f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru/// isEmptyRecord - Return true iff a structure contains only empty
191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// fields. Note that a structure with a flexible array member is not
192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered empty.
19398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
1946217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = T->getAs<RecordType>();
195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
2005ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
201c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis  // If this is a C++ record, check the bases first.
2025ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
203c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
204c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis           e = CXXRD->bases_end(); i != e; ++i)
205c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis      if (!isEmptyRecord(Context, i->getType(), true))
206c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis        return false;
2075ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
20817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
20917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i)
210581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!isEmptyField(Context, *i, AllowArrays))
211c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
213c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
214c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
215c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isSingleElementStruct - Determine if a structure is a "single
216c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// element struct", i.e. it has exactly one non-empty field or
217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// exactly one field which is itself a single element
218c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// struct. Structures with flexible array members are never
219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered single element structs.
220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov///
221c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// \return The field declaration for the single non-empty field, if
222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// it exists.
223c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
224c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordType *RT = T->getAsStructureType();
225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
228c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
229c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
230c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
231c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
232c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const Type *Found = 0;
2338bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2349430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // If this is a C++ record, check the bases first.
2359430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2369430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
2379430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i) {
2389430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // Ignore empty records.
2395ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar      if (isEmptyRecord(Context, i->getType(), true))
2409430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        continue;
2419430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2429430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If we already found an element then this isn't a single-element struct.
2439430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (Found)
2449430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2459430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2469430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If this is non-empty and not a single element struct, the composite
2479430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // cannot be a single element struct.
2489430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      Found = isSingleElementStruct(i->getType(), Context);
2499430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (!Found)
2509430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2519430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    }
2529430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  }
2539430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2549430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // Check for single element.
25517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
25617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
257581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    QualType FT = FD->getType();
259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Ignore empty fields.
26198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
262c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
263c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
264c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If we already found an element then this isn't a single-element
265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // struct.
266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Found)
267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return 0;
268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Treat single element arrays as the element.
270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (AT->getSize().getZExtValue() != 1)
272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      FT = AT->getElementType();
274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
276d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (!isAggregateTypeForABI(FT)) {
277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = FT.getTypePtr();
278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = isSingleElementStruct(FT, Context);
280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (!Found)
281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return 0;
282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
285bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // We don't consider a struct a single-element struct if it has
286bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // padding beyond the element type.
287bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
288bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman    return 0;
289bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
290c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Found;
291c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
292c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
293c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
294db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman  // Treat complex types as the element type.
295db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman  if (const ComplexType *CTy = Ty->getAs<ComplexType>())
296db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman    Ty = CTy->getElementType();
297db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman
298db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman  // Check for a type which we know has a simple scalar argument-passing
299db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman  // convention without any padding.  (We're specifically looking for 32
300db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman  // and 64-bit integer and integer-equivalents, float, and double.)
301a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
302db748a380ab89b1c0b6e751e55291f57605cccceEli Friedman      !Ty->isEnumeralType() && !Ty->isBlockPointerType())
303c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
305c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
306c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Size == 32 || Size == 64;
307c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
30953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// canExpandIndirectArgument - Test whether an argument type which is to be
31053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// passed indirectly (on the stack) would have the equivalent layout if it was
31153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// expanded into separate arguments. If so, we prefer to do the latter to avoid
31253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// inhibiting optimizations.
31353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar///
31453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// FIXME: This predicate is missing many cases, currently it just follows
31553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
31653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// should probably make this smarter, or better yet make the LLVM backend
31753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// capable of handling it.
31853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbarstatic bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
31953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand structure types.
32053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
32153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RT)
32253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
32353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
32453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand (C) structures.
32553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  //
32653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // FIXME: This needs to be generalized to handle classes as well.
32753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
32853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
32953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
33053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
331506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  uint64_t Size = 0;
332506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
33317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
33417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
335581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
337c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (!is32Or64BitBasicType(FD->getType(), Context))
338c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
340c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
341c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // how to expand them yet, and the predicate for telling if a bitfield still
342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // counts as "basic" is more complicated than what we were doing previously.
343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (FD->isBitField())
344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
345506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
346506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    Size += Context.getTypeSize(FD->getType());
347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
349506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  // Make sure there are not any holes in the struct.
350506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  if (Size != Context.getTypeSize(Ty))
351506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    return false;
352506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
353c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
354c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
356c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
357c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// DefaultABIInfo - The default implementation for ABI specific
358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// details. This implementation provides information which results in
359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// self-consistent and sensible LLVM IR generation, but does not
360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// conform to any particular ABI.
361c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass DefaultABIInfo : public ABIInfo {
362ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerpublic:
363ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
3648bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
365a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
366a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
368ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const {
369a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
370c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
371c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         it != ie; ++it)
372a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      it->info = classifyArgumentType(it->type);
373c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
374c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
375c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
376c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
378c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
37982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
38082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
381ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
382ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
38382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
38482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
38582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovllvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
38682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                       CodeGenFunction &CGF) const {
38782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  return 0;
38882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
38982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
390a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
39190306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  if (isAggregateTypeForABI(Ty)) {
39290306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // Records with non trivial destructors/constructors should not be passed
39390306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // by value.
394ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (isRecordReturnIndirect(Ty, CGT))
39590306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
39690306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung
39782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
39890306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  }
399dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
400a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  // Treat an enum type as its underlying type.
401a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
402a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
403aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
404a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  return (Ty->isPromotableIntegerType() ?
405a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
40682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
40782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
4080024f940dd15987b8ffbe6e787dcf860a9ea1effBob WilsonABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
4090024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (RetTy->isVoidType())
4100024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIgnore();
4110024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
4120024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (isAggregateTypeForABI(RetTy))
4130024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIndirect(0);
4140024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
4150024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  // Treat an enum type as its underlying type.
4160024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4170024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    RetTy = EnumTy->getDecl()->getIntegerType();
4180024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
4190024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  return (RetTy->isPromotableIntegerType() ?
4200024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4210024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson}
4220024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
4239ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff//===----------------------------------------------------------------------===//
4249ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff// le32/PNaCl bitcode ABI Implementation
425c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky//
426c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky// This is a simplified version of the x86_32 ABI.  Arguments and return values
427c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky// are always passed on the stack.
4289ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff//===----------------------------------------------------------------------===//
4299ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4309ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffclass PNaClABIInfo : public ABIInfo {
4319ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff public:
4329ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
4339ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4349ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  ABIArgInfo classifyReturnType(QualType RetTy) const;
435c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky  ABIArgInfo classifyArgumentType(QualType RetTy) const;
4369ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4379ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  virtual void computeInfo(CGFunctionInfo &FI) const;
4389ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4399ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff                                 CodeGenFunction &CGF) const;
4409ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff};
4419ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4429ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffclass PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
4439ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff public:
4449ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
4459ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
4469ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff};
4479ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4489ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffvoid PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
4499ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4509ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4519ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4529ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff         it != ie; ++it)
453c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky      it->info = classifyArgumentType(it->type);
4549ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  }
4559ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4569ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffllvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4579ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff                                       CodeGenFunction &CGF) const {
4589ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  return 0;
4599ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4609ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
461c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky/// \brief Classify argument of given type \p Ty.
462c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli BenderskyABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
4639ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (isAggregateTypeForABI(Ty)) {
464ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
465ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4669ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIndirect(0);
467c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky  } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
468c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky    // Treat an enum type as its underlying type.
4699ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    Ty = EnumTy->getDecl()->getIntegerType();
470c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky  } else if (Ty->isFloatingType()) {
471c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky    // Floating-point types don't go inreg.
472c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky    return ABIArgInfo::getDirect();
473c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky  }
4749ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
475c0783dc18a78b55e9486b72fa0b193dbf1b65fbbEli Bendersky  return (Ty->isPromotableIntegerType() ?
4769ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4779ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4789ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4799ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek SchuffABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
4809ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (RetTy->isVoidType())
4819ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIgnore();
4829ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
483e45dfd15d9d821b0f2066bc0cad525eef2e307c3Eli Bendersky  // In the PNaCl ABI we always return records/structures on the stack.
4849ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (isAggregateTypeForABI(RetTy))
4859ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIndirect(0);
4869ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4879ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  // Treat an enum type as its underlying type.
4889ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4899ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    RetTy = EnumTy->getDecl()->getIntegerType();
4909ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4919ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  return (RetTy->isPromotableIntegerType() ?
4929ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4939ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4949ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4951f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier/// IsX86_MMXType - Return true if this is an MMX type.
4961f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosierbool IsX86_MMXType(llvm::Type *IRType) {
4971f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier  // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
498bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
499bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
500bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    IRType->getScalarSizeInBits() != 64;
501bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling}
502bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
503ef6de3da8572607f786303c07150daa6e140ab19Jay Foadstatic llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
5045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef Constraint,
505ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                          llvm::Type* Ty) {
5060507be662df482b5c67b7905ed7ca368cb5c6b69Bill Wendling  if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
5074b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
5084b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  return Ty;
5094b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne}
5104b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
511dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
512dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-32 ABI Implementation
513dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
5148bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
515c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_32ABIInfo - The X86-32 ABI information.
516c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_32ABIInfo : public ABIInfo {
517b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  enum Class {
518b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Integer,
519b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Float
520b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  };
521b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
522fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  static const unsigned MinABIStackAlignInBytes = 4;
523fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
5241e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsDarwinVectorABI;
5251e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsSmallStructInRegABI;
526ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  bool IsWin32StructABI;
527b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned DefaultNumRegisterParameters;
528c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
529c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  static bool isRegisterSize(unsigned Size) {
530c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
531c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
532c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5336c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
5346c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                          unsigned callingConvention);
535c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
536dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
537dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// such that the argument will be passed in memory.
5380b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
5390b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola                               unsigned &FreeRegs) const;
540dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
541fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  /// \brief Return the alignment to use for the given type on the stack.
542e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
543fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
544b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class classify(QualType Ty) const;
545b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola  ABIArgInfo classifyReturnType(QualType RetTy,
5466c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                unsigned callingConvention) const;
547b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
548b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola                                  bool IsFastCall) const;
549b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
550e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola                      bool IsFastCall, bool &NeedsPadding) const;
551c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
552b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindolapublic:
553b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola
554aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  virtual void computeInfo(CGFunctionInfo &FI) const;
555c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
556c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
557c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5581f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
559b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                unsigned r)
560c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
561ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
562c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
563c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
56482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
56582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
56655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
5671f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier      bool d, bool p, bool w, unsigned r)
5681f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
56974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
57074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
57174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                           CodeGen::CodeGenModule &CGM) const;
5726374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
5736374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
5746374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // Darwin uses different dwarf register numbers for EH.
57564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
5766374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 4;
5776374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
5786374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
5796374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5806374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const;
5814b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
582ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
5835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
584ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
5854b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
5864b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
5874b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
58882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
58982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
59082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
591c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
592c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// shouldReturnTypeInRegister - Determine if the given type should be
593c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// passed in a register (for the Darwin ABI).
594c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovbool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
5956c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               ASTContext &Context,
5966c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               unsigned callingConvention) {
597c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
598c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
599c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Type must be register sized.
600c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!isRegisterSize(Size))
601c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
602c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
603c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Ty->isVectorType()) {
604c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // 64- and 128- bit vectors inside structures are not returned in
605c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // registers.
606c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 64 || Size == 128)
607c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
608c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
609c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
610c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
611c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
6127711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // If this is a builtin, pointer, enum, complex type, member pointer, or
6137711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // member function pointer it is ok.
614a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
61555e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
6167711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar      Ty->isBlockPointerType() || Ty->isMemberPointerType())
617c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
618c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
619c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Arrays are treated like records.
620c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
6216c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return shouldReturnTypeInRegister(AT->getElementType(), Context,
6226c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                      callingConvention);
623c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
624c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Otherwise, it must be a record type.
6256217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = Ty->getAs<RecordType>();
626c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT) return false;
627c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
628a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson  // FIXME: Traverse bases here too.
629a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson
6306c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // For thiscall conventions, structures will never be returned in
6316c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // a register.  This is for compatibility with the MSVC ABI
6326c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  if (callingConvention == llvm::CallingConv::X86_ThisCall &&
6336c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman      RT->isStructureType()) {
6346c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return false;
6356c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  }
6366c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman
637c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Structure types are passed in register if all fields would be
638c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // passed in a register.
63917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
64017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         e = RT->getDecl()->field_end(); i != e; ++i) {
641581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
642c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
643c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Empty fields are ignored.
64498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
645c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
646c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
647c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Check fields recursively.
6486c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (!shouldReturnTypeInRegister(FD->getType(), Context,
6496c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                    callingConvention))
650c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
651c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
652c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
653c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
654c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
6556c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron BallmanABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
6566c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                            unsigned callingConvention) const {
657a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (RetTy->isVoidType())
658c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
6598bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
660a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
661c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // On Darwin, some vectors are returned in registers.
6621e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (IsDarwinVectorABI) {
663a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
664c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
665c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // 128-bit vectors are a special case; they are returned in
666c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // registers and we need to make sure to pick a type the LLVM
667c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // backend will like.
668c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size == 128)
669800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::VectorType::get(
670a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                  llvm::Type::getInt64Ty(getVMContext()), 2));
671c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
672c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Always return in register if it fits in a general purpose
673c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // register, or if it is 64 bits and has a single element.
674c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if ((Size == 8 || Size == 16 || Size == 32) ||
675c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          (Size == 64 && VT->getNumElements() == 1))
676800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
677a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                                            Size));
678c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
679c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
680c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
681c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
682c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getDirect();
683a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  }
6848bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
685d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(RetTy)) {
686a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
687ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (isRecordReturnIndirect(RT, CGT))
68840092972b591646b47037d2b46b695a4014df413Anders Carlsson        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
6898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
69040092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with flexible arrays are always indirect.
691c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
692c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return ABIArgInfo::getIndirect(0);
69340092972b591646b47037d2b46b695a4014df413Anders Carlsson    }
6948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
6951e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    // If specified, structs and unions are always indirect.
6961e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
697c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
698c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
699c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Small structures which are register sized are generally returned
700c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // in a register.
7016c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
7026c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                                  callingConvention)) {
703a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
704bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
705bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // As a special-case, if the struct is a "single-element" struct, and
706bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // the field is of type "float" or "double", return it in a
70755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // floating-point register. (MSVC does not apply this special case.)
70855fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // We apply a similar transformation for pointer types to improve the
70955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // quality of the generated IR.
710bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
711ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov        if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
71255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman            || SeltTy->hasPointerRepresentation())
713bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
714bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
715bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // FIXME: We should be able to narrow this integer in cases with dead
716bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // padding.
717800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
718c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
719c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
720c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
721c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
7228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
723a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  // Treat an enum type as its underlying type.
724a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
725a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    RetTy = EnumTy->getDecl()->getIntegerType();
726a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner
727a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (RetTy->isPromotableIntegerType() ?
728a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
729c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
730c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
731f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedmanstatic bool isSSEVectorType(ASTContext &Context, QualType Ty) {
732f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
733f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman}
734f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman
73593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbarstatic bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
73693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
73793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (!RT)
73893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 0;
73993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
74093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
74193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // If this is a C++ record, check the bases first.
74293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
74393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
74493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i)
74593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      if (!isRecordWithSSEVectorType(Context, i->getType()))
74693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar        return false;
74793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
74893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
74993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar       i != e; ++i) {
75093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    QualType FT = i->getType();
75193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
752f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman    if (isSSEVectorType(Context, FT))
75393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
75493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
75593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    if (isRecordWithSSEVectorType(Context, FT))
75693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
75793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  }
75893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
75993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return false;
76093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar}
76193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
762e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbarunsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
763e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                                 unsigned Align) const {
764e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Otherwise, if the alignment is less than or equal to the minimum ABI
765e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // alignment, just use the default; the backend will handle this.
766fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  if (Align <= MinABIStackAlignInBytes)
767e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return 0; // Use default alignment.
768e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
769e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // On non-Darwin, the stack type alignment is always 4.
770e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (!IsDarwinVectorABI) {
771e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    // Set explicit alignment, since we may need to realign the top.
772fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar    return MinABIStackAlignInBytes;
773e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  }
774fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
77593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
776f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
777f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman                      isRecordWithSSEVectorType(getContext(), Ty)))
77893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 16;
77993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
78093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return MinABIStackAlignInBytes;
781fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar}
782fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
7830b4cc950c54c8dd2de51587ef48446de670fa012Rafael EspindolaABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7840b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola                                            unsigned &FreeRegs) const {
7850b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  if (!ByVal) {
7860b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    if (FreeRegs) {
7870b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      --FreeRegs; // Non byval indirects just use one pointer.
7880b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      return ABIArgInfo::getIndirectInReg(0, false);
7890b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    }
79046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, false);
7910b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  }
79246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
793e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Compute the byval alignment.
794e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
795e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
796e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign == 0)
797de92d739ba0ef42a5a7dcfd6e170329549d0716bChris Lattner    return ABIArgInfo::getIndirect(4);
798e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
799e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // If the stack alignment is less than the type alignment, realign the
800e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // argument.
801e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign < TypeAlign)
802e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
803e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                   /*Realign=*/true);
804e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
805e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  return ABIArgInfo::getIndirect(StackAlign);
806dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar}
807dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
808b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaX86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
809b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  const Type *T = isSingleElementStruct(Ty, getContext());
810b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (!T)
811b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    T = Ty.getTypePtr();
812b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
813b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
814b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    BuiltinType::Kind K = BT->getKind();
815b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    if (K == BuiltinType::Float || K == BuiltinType::Double)
816b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      return Float;
817b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
818b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  return Integer;
819b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
820b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
821b6932692234eba2472ef85a38434496e9342fd38Rafael Espindolabool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
822e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola                                   bool IsFastCall, bool &NeedsPadding) const {
823e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola  NeedsPadding = false;
824b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class C = classify(Ty);
825b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (C == Float)
8260b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    return false;
827b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
828b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  unsigned Size = getContext().getTypeSize(Ty);
829b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  unsigned SizeInRegs = (Size + 31) / 32;
8305f14fcbd45870585a136ae735d29d0e085c0d7f8Rafael Espindola
8315f14fcbd45870585a136ae735d29d0e085c0d7f8Rafael Espindola  if (SizeInRegs == 0)
8325f14fcbd45870585a136ae735d29d0e085c0d7f8Rafael Espindola    return false;
8335f14fcbd45870585a136ae735d29d0e085c0d7f8Rafael Espindola
834b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (SizeInRegs > FreeRegs) {
835b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    FreeRegs = 0;
8360b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    return false;
837b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
838b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
8390b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  FreeRegs -= SizeInRegs;
840b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
841b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  if (IsFastCall) {
842b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    if (Size > 32)
843b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola      return false;
844b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
845b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    if (Ty->isIntegralOrEnumerationType())
846b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola      return true;
847b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
848b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    if (Ty->isPointerType())
849b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola      return true;
850b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
851b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    if (Ty->isReferenceType())
852b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola      return true;
853b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
854e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (FreeRegs)
855e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      NeedsPadding = true;
856e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
857b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    return false;
858b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  }
859b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola
8600b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  return true;
861b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
862b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
8630b4cc950c54c8dd2de51587ef48446de670fa012Rafael EspindolaABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
864b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola                                               unsigned &FreeRegs,
865b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola                                               bool IsFastCall) const {
866c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Set alignment on indirect arguments.
867d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(Ty)) {
868a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
869ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (IsWin32StructABI)
870ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov        return getIndirectResult(Ty, true, FreeRegs);
871dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
872ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
873ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov        return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
874ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
875ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      // Structures with flexible arrays are always indirect.
876c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
8770b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola        return getIndirectResult(Ty, true, FreeRegs);
878a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    }
879c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
8805a4d35247f55dae6dd0d5ad349ecadbbea0b4572Eli Friedman    // Ignore empty structs/unions.
8815a1ac89b244940a0337ea7ae7dc371e2a9bf7c50Eli Friedman    if (isEmptyRecord(getContext(), Ty, true))
882c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIgnore();
883c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
884e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    llvm::LLVMContext &LLVMContext = getVMContext();
885e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
886e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    bool NeedsPadding;
887e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
8880b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
8890b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      SmallVector<llvm::Type*, 3> Elements;
8900b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      for (unsigned I = 0; I < SizeInRegs; ++I)
8910b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola        Elements.push_back(Int32);
8920b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8930b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      return ABIArgInfo::getDirectInReg(Result);
8940b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    }
895e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
8960b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola
89753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // Expand small (<= 128-bit) record types when we know that the stack layout
89853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // of those arguments will match the struct. This is important because the
89953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // LLVM backend isn't smart enough to remove byval, which inhibits many
90053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // optimizations.
901a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (getContext().getTypeSize(Ty) <= 4*32 &&
902a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        canExpandIndirectArgument(Ty, getContext()))
903e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
904c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
9050b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    return getIndirectResult(Ty, true, FreeRegs);
9068bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  }
9078bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
908bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
9097b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // On Darwin, some vectors are passed in memory, we handle this by passing
9107b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // it as an i8/i16/i32/i64.
911bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    if (IsDarwinVectorABI) {
912bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      uint64_t Size = getContext().getTypeSize(Ty);
913bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      if ((Size == 8 || Size == 16 || Size == 32) ||
914bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner          (Size == 64 && VT->getNumElements() == 1))
915bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
916bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner                                                            Size));
917bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    }
918bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
9191f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier    if (IsX86_MMXType(CGT.ConvertType(Ty)))
9201f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
9219cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
922bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    return ABIArgInfo::getDirect();
923bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  }
9249cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
9259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
926a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
927a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
928aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
929e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola  bool NeedsPadding;
930e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola  bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
9310b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola
9320b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  if (Ty->isPromotableIntegerType()) {
9330b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    if (InReg)
9340b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      return ABIArgInfo::getExtendInReg();
9350b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    return ABIArgInfo::getExtend();
9360b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  }
9370b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  if (InReg)
9380b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola    return ABIArgInfo::getDirectInReg();
9390b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola  return ABIArgInfo::getDirect();
940c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
941c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
942aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindolavoid X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
943aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
944aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola                                          FI.getCallingConvention());
945b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
946b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  unsigned CC = FI.getCallingConvention();
947b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
948b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  unsigned FreeRegs;
949b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  if (IsFastCall)
950b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    FreeRegs = 2;
951b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  else if (FI.getHasRegParm())
952b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    FreeRegs = FI.getRegParm();
953b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola  else
954b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    FreeRegs = DefaultNumRegisterParameters;
955b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
956b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // If the return value is indirect, then the hidden argument is consuming one
957b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // integer register.
958b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (FI.getReturnInfo().isIndirect() && FreeRegs) {
959b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    --FreeRegs;
960b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    ABIArgInfo &Old = FI.getReturnInfo();
961b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
962b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectByVal(),
963b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectRealign());
964b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
965b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
966aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
967aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola       it != ie; ++it)
968b6932692234eba2472ef85a38434496e9342fd38Rafael Espindola    it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
969aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola}
970aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola
971c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
972c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
9738b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
974c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
975c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
976c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
977c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                       "ap");
978c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
9797b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
9807b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  // Compute if the address needs to be aligned
9817b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
9827b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = getTypeStackAlignInBytes(Ty, Align);
9837b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = std::max(Align, 4U);
9847b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  if (Align > 4) {
9857b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    // addr = (addr + align - 1) & -align;
9867b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Offset =
9877b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman      llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
9887b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateGEP(Addr, Offset);
9897b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
9907b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                                    CGF.Int32Ty);
9917b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
9927b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
9937b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      Addr->getType(),
9947b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      "ap.cur.aligned");
9957b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  }
9967b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
997c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
99896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
999c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1000c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1001c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
10027b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
1003c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
100477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
1005c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
1006c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1007c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1008c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
1009c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1010c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
101174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davisvoid X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
101274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                                  llvm::GlobalValue *GV,
101374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                            CodeGen::CodeGenModule &CGM) const {
101474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
101574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
101674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Get the LLVM function.
101774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      llvm::Function *Fn = cast<llvm::Function>(GV);
101874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
101974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Now add the 'alignstack' attribute with a value of 16.
10200d5833921cc728bc1d2e45fbaf7b3e11cddbf99dBill Wendling      llvm::AttrBuilder B;
1021e91e9ecf2f6ef18ed9d9642915e5e1abb63e150aBill Wendling      B.addStackAlignmentAttr(16);
1022909b6ded6be68a5740d98b478454fa55ea817ad6Bill Wendling      Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1023909b6ded6be68a5740d98b478454fa55ea817ad6Bill Wendling                      llvm::AttributeSet::get(CGM.getLLVMContext(),
1024909b6ded6be68a5740d98b478454fa55ea817ad6Bill Wendling                                              llvm::AttributeSet::FunctionIndex,
1025909b6ded6be68a5740d98b478454fa55ea817ad6Bill Wendling                                              B));
102674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    }
102774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  }
102874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis}
102974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
10306374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
10316374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               CodeGen::CodeGenFunction &CGF,
10326374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               llvm::Value *Address) const {
10336374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
10346374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10358b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
10368bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
10376374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 0-7 are the eight integer registers;  the order is different
10386374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  //   on Darwin (for EH), but the range is the same.
10396374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 8 is %eip.
1040aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 8);
10416374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
104264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
10436374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 12-16 are st(0..4).  Not sure why we stop at 4.
10446374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 16, which is sizeof(long double) on
10456374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 8-byte alignment for that type.
10468b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
1047aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
10488bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
10496374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  } else {
10506374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 9 is %eflags, which doesn't get a size on Darwin for some
10516374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // reason.
10526374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
10536374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10546374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 11-16 are st(0..5).  Not sure why we stop at 5.
10556374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 12, which is sizeof(long double) on
10566374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 4-byte alignment for that type.
10578b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1058aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1059aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
10606374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10616374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  return false;
10626374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall}
10636374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
1064dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
1065dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-64 ABI Implementation
1066dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
1067dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
1068dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
1069c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
1070c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_64ABIInfo - The X86_64 ABI information.
1071c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_64ABIInfo : public ABIInfo {
1072c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  enum Class {
1073c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Integer = 0,
1074c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSE,
1075c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSEUp,
1076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87,
1077c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87Up,
1078c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ComplexX87,
1079c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    NoClass,
1080c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Memory
1081c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  };
1082c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1083c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// merge - Implement the X86_64 ABI merging algorithm.
1084c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1085c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// Merge an accumulating classification \arg Accum with a field
1086c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classification \arg Field.
1087c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1088c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Accum - The accumulating classification. This should
1089c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// always be either NoClass or the result of a previous merge
1090c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// call. In addition, this should never be Memory (the caller
1091c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// should just return Memory for the aggregate).
10921090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  static Class merge(Class Accum, Class Field);
1093c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
10944943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// postMerge - Implement the X86_64 ABI post merging algorithm.
10954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10964943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
10974943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// final MEMORY or SSE classes when necessary.
10984943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10994943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param AggregateSize - The size of the current aggregate in
11004943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// the classification process.
11014943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
11024943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Lo - The classification for the parts of the type
11034943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the low word of the containing object.
11044943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
11054943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Hi - The classification for the parts of the type
11064943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the higher words of the containing object.
11074943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
11084943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
11094943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
1110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classify - Determine the x86_64 register classes in which the
1111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// given type T should be passed.
1112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Lo - The classification for the parts of the type
1114c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the low word of the containing object.
1115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Hi - The classification for the parts of the type
1117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the high word of the containing object.
1118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param OffsetBase - The bit offset of this type in the
1120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// containing object.  Some parameters are classified different
1121c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// depending on whether they straddle an eightbyte boundary.
1122c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1123c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If a word is unused its result will be NoClass; if a type should
1124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// be passed in Memory then at least the classification of \arg Lo
1125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// will be Memory.
1126c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1127f3477c13eeaf11b32a41f181398fb5deffd0dd73Sylvestre Ledru  /// The \arg Lo class will be NoClass iff the argument is ignored.
1128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1129c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1130c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// also be ComplexX87.
11319c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
1132c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
11334943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  llvm::Type *GetByteVectorType(QualType Ty) const;
11349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
11359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned IROffset, QualType SourceTy,
11369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned SourceOffset) const;
11379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
11389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned IROffset, QualType SourceTy,
11399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned SourceOffset) const;
11408bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1141c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
114246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// such that the argument will be returned in memory.
11439c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
114446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
114546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// such that the argument will be passed in memory.
1147edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ///
1148edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// \param freeIntRegs - The number of free integer registers remaining
1149edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// available.
1150edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1152a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
1153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1154bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  ABIArgInfo classifyArgumentType(QualType Ty,
1155edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                  unsigned freeIntRegs,
1156bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling                                  unsigned &neededInt,
115799aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling                                  unsigned &neededSSE) const;
1158c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1159ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool IsIllegalVectorType(QualType Ty) const;
1160ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
116167a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// The 0.98 ABI revision clarified a lot of ambiguities,
116267a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// unfortunately in ways that were not always consistent with
116367a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// certain previous compilers.  In particular, platforms which
116467a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// required strict binary compatibility with older versions of GCC
116567a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// may need to exempt themselves.
116667a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  bool honorsRevision0_98() const {
116764aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    return !getTarget().getTriple().isOSDarwin();
116867a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  }
116967a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall
1170ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool HasAVX;
1171babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff  // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1172babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff  // 64-bit hardware.
1173babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff  bool Has64BitPointers;
1174ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1175c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovpublic:
1176ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
1177babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff      ABIInfo(CGT), HasAVX(hasavx),
117890da80c869eebc5a73bf031af5bedb6f281214fbDerek Schuff      Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
1179babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff  }
11809c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
1181de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isPassedUsingAVXType(QualType type) const {
1182de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    unsigned neededInt, neededSSE;
1183edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // The freeIntRegs argument doesn't matter here.
1184edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
1185de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (info.isDirect()) {
1186de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      llvm::Type *ty = info.getCoerceToType();
1187de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1188de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        return (vectorTy->getBitWidth() > 128);
1189de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    }
1190de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return false;
1191de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1192de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1193ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
1194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
1197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
119882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1199f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1200a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumiclass WinX86_64ABIInfo : public ABIInfo {
1201a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1202ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
1203a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1204f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1205a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1206a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1207a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  virtual void computeInfo(CGFunctionInfo &FI) const;
1208f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1209f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1210f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                 CodeGenFunction &CGF) const;
1211f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1212f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
121382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
121482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
1215ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1216babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff      : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
12176374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
1218de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const X86_64ABIInfo &getABIInfo() const {
1219de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1220de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1221de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
12226374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
12236374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 7;
12246374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
12256374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
12266374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
12276374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const {
12288b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
12298bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1230aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 0-15 are the 16 integer registers.
1231aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 16 is %rip.
12328b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
12336374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return false;
12346374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
12354b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1236ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
12375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
1238ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
12394b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
12404b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
12414b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1242de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isNoProtoCallVariadic(const CallArgList &args,
1243de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             const FunctionNoProtoType *fnType) const {
124401f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // The default CC on x86-64 sets %al to the number of SSA
124501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // registers used, and GCC sets this when calling an unprototyped
12463ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    // function, so we override the default behavior.  However, don't do
124768805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // that when AVX types are involved: the ABI explicitly states it is
124868805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // undefined, and it doesn't work in practice because of how the ABI
124968805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // defines varargs anyway.
1250de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
12513ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      bool HasAVXType = false;
1252de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      for (CallArgList::const_iterator
1253de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall             it = args.begin(), ie = args.end(); it != ie; ++it) {
1254de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1255de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          HasAVXType = true;
1256de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          break;
12573ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        }
12583ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      }
1259de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
12603ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      if (!HasAVXType)
12613ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        return true;
12623ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    }
126301f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
1264de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
126501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  }
126601f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
126782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
126882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
126989735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballmanstatic std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
127089735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  // If the argument does not end in .lib, automatically add the suffix. This
127189735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  // matches the behavior of MSVC.
127289735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  std::string ArgStr = Lib;
127389735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  if (Lib.size() <= 4 ||
127489735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman      Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
127589735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman    ArgStr += ".lib";
127689735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  }
127789735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman  return ArgStr;
127889735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman}
127989735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman
12803190ca922d3743137e15fe0c525c04b177b9983bReid Klecknerclass WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
12813190ca922d3743137e15fe0c525c04b177b9983bReid Klecknerpublic:
12823190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms)
12833190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner    : X86_32TargetCodeGenInfo(CGT, false, true, true, RegParms) {}
12843190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner
12853190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  void getDependentLibraryOption(llvm::StringRef Lib,
12863190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner                                 llvm::SmallString<24> &Opt) const {
12873190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner    Opt = "/DEFAULTLIB:";
128889735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman    Opt += qualifyWindowsLibrary(Lib);
12893190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  }
12903190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner};
12913190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner
1292f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerclass WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1293f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1294f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1295f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1296f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1297f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1298f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return 7;
1299f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1300f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1301f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1302f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                               llvm::Value *Address) const {
13038b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
13049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1305f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 0-15 are the 16 integer registers.
1306f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 16 is %rip.
13078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1308f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return false;
1309f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
13103190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner
13113190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  void getDependentLibraryOption(llvm::StringRef Lib,
13123190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner                                 llvm::SmallString<24> &Opt) const {
13133190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner    Opt = "/DEFAULTLIB:";
131489735b9516b1a378c6d33620a6c3a0d5705f9d04Aaron Ballman    Opt += qualifyWindowsLibrary(Lib);
13153190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner  }
1316f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1317f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1318c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1319c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
13204943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesvoid X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
13214943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes                              Class &Hi) const {
13224943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
13234943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13244943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (a) If one of the classes is Memory, the whole argument is passed in
13254943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
13264943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13274943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (b) If X87UP is not preceded by X87, the whole argument is passed in
13284943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
13294943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13304943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (c) If the size of the aggregate exceeds two eightbytes and the first
13314943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
13324943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     argument is passed in memory. NOTE: This is necessary to keep the
13334943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     ABI working for processors that don't support the __m256 type.
13344943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13354943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
13364943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13374943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Some of these are enforced by the merging logic.  Others can arise
13384943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // only with unions; for example:
13394943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //   union { _Complex double; unsigned; }
13404943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13414943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Note that clauses (b) and (c) were added in 0.98.
13424943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
13434943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == Memory)
13444943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
13454943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
13464943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
13474943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
13484943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
13494943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == SSEUp && Lo != SSE)
13504943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Hi = SSE;
13514943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes}
13524943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
13531090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerX86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1354c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classified recursively so that always two fields are
1356c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // considered. The resulting class is calculated according to
1357c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the classes of the fields in the eightbyte:
1358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (a) If both classes are equal, this is the resulting class.
1360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1361c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (b) If one of the classes is NO_CLASS, the resulting class is
1362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the other class.
1363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1364c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (c) If one of the classes is MEMORY, the result is the MEMORY
1365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // class.
1366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (d) If one of the classes is INTEGER, the result is the
1368c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // INTEGER.
1369c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1370c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1371c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // MEMORY is used as class.
1372c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1373c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (f) Otherwise class SSE is used.
1374c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1375c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Accum should never be memory (we should have returned) or
1376c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // ComplexX87 (because this cannot be passed in a structure).
1377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Accum != Memory && Accum != ComplexX87) &&
1378c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         "Invalid accumulated classification during merge.");
1379c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Accum == Field || Field == NoClass)
1380c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Accum;
13811090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == Memory)
1382c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
13831090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == NoClass)
1384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Field;
13851090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == Integer || Field == Integer)
1386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Integer;
13871090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
13881090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      Accum == X87 || Accum == X87Up)
1389c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
13901090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  return SSE;
1391c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1392c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1393bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattnervoid X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1394c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                             Class &Lo, Class &Hi) const {
1395c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This code can be simplified by introducing a simple value class for
1396c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Class pairs with appropriate constructor methods for the various
1397c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // situations.
1398c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1399c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Some of the split computations are wrong; unaligned vectors
1400c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // shouldn't be passed in registers for example, so there is no chance they
1401c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // can straddle an eightbyte. Verify & simplify.
1402c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1403c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Lo = Hi = NoClass;
1404c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1405c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Class &Current = OffsetBase < 64 ? Lo : Hi;
1406c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Current = Memory;
1407c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1408183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1409c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    BuiltinType::Kind k = BT->getKind();
1410c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1411c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (k == BuiltinType::Void) {
1412c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = NoClass;
1413c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1414c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Integer;
1415c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Integer;
1416c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1417c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
14187da46f949f6ec63d7c7dcda5f49588261c669ffbDerek Schuff    } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
14197da46f949f6ec63d7c7dcda5f49588261c669ffbDerek Schuff               (k == BuiltinType::LongDouble &&
142064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
1421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::LongDouble) {
1423c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = X87;
1424c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = X87Up;
1425c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1426c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _Decimal32 and _Decimal64 are SSE.
1427c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
14281090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14291090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14308bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14311090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const EnumType *ET = Ty->getAs<EnumType>()) {
1432c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Classify the underlying integer type.
14339c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
14341090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14351090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14368bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14371090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->hasPointerRepresentation()) {
1438c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = Integer;
14391090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14401090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14418bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14421090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->isMemberPointerType()) {
1443babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff    if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
144467d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Lo = Hi = Integer;
144567d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    else
144667d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Current = Integer;
14471090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14481090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14498bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14501090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1451ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(VT);
1452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 32) {
1453c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1454c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // float> as integer.
1455c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1456c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1457c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1458c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1459c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Real = (OffsetBase) / 64;
1460c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1461c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (EB_Real != EB_Imag)
1462c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1463c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (Size == 64) {
1464c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x double> in memory. :(
1465c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1466c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1467c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1468c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x long long> as INTEGER.
1469473f8e723be93d84bd5fd15b094f4184802d4676Chris Lattner      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
14700fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
14710fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
14720fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1473c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1474c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else
1475c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = SSE;
1476c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1477c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1478c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1479c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (OffsetBase && OffsetBase != 64)
1480c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1481ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    } else if (Size == 128 || (HasAVX && Size == 256)) {
14824943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // Arguments of 256-bits are split into four eightbyte chunks. The
14834943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // least significant one belongs to class SSE and all the others to class
14844943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // SSEUP. The original Lo and Hi design considers that types can't be
14854943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
14864943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // This design isn't correct for 256-bits, but since there're no cases
14874943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // where the upper parts would need to be inspected, avoid adding
14884943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // complexity and just consider Hi to match the 64-256 part.
1489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = SSE;
1490c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = SSEUp;
1491c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
14921090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14931090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14951090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1496ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    QualType ET = getContext().getCanonicalType(CT->getElementType());
1497c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1498ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
14992ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (ET->isIntegralOrEnumerationType()) {
1500c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size <= 64)
1501c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1502c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else if (Size <= 128)
1503c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Hi = Integer;
1504ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    } else if (ET == getContext().FloatTy)
1505c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
15067da46f949f6ec63d7c7dcda5f49588261c669ffbDerek Schuff    else if (ET == getContext().DoubleTy ||
15077da46f949f6ec63d7c7dcda5f49588261c669ffbDerek Schuff             (ET == getContext().LongDoubleTy &&
150864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall              getTarget().getTriple().getOS() == llvm::Triple::NaCl))
1509c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Hi = SSE;
1510ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().LongDoubleTy)
1511c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = ComplexX87;
1512c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1513c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If this complex type crosses an eightbyte boundary then it
1514c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // should be split.
1515c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t EB_Real = (OffsetBase) / 64;
1516ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1517c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Hi == NoClass && EB_Real != EB_Imag)
1518c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Lo;
15198bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
15201090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
15211090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
15228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1523ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1524c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Arrays are treated like structures.
1525c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1526ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1527c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1528c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
15294943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
15304943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1531c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1532c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1534c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // fields, it has class MEMORY.
1535c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1536c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Only need to check alignment of array base.
1537ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
1538c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1539c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1540c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Otherwise implement simplified merge. We could be smarter about
1541c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // this, but it isn't worth it and would be harder to verify.
1542c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1543ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
1544c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t ArraySize = AT->getSize().getZExtValue();
1545089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1546089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // The only case a 256-bit wide vector could be used is when the array
1547089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1548089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // to work for sizes wider than 128, early check and fallback to memory.
1549089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    if (Size > 128 && EltSize != 256)
1550089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes      return;
1551089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1552c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1553c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
15549c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner      classify(AT->getElementType(), Offset, FieldLo, FieldHi);
1555c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1556c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1557c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1558c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1559c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1560c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15614943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1562c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
15631090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
15641090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
15658bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
15661090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1567ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1568c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1569c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
15704943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
15714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1572c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1573c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15740a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
15750a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // copy constructor or a non-trivial destructor, it is passed by invisible
15760a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // reference.
1577ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (getRecordArgABI(RT, CGT))
15780a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson      return;
1579ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1580c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    const RecordDecl *RD = RT->getDecl();
1581c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1582c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Assume variable sized types are passed in memory.
1583c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (RD->hasFlexibleArrayMember())
1584c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1585c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1586ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1587c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1588c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Reset Lo class, this will be recomputed.
1589c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1590ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1591ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // If this is a C++ record, classify the bases first.
1592ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1593ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1594ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar             e = CXXRD->bases_end(); i != e; ++i) {
1595ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1596ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar               "Unexpected base class!");
1597ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        const CXXRecordDecl *Base =
1598ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1599ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1600ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // Classify this field.
1601ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        //
1602ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1603ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // single eightbyte, each is classified separately. Each eightbyte gets
1604ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // initialized to class NO_CLASS.
1605ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Class FieldLo, FieldHi;
1606d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t Offset =
1607d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer          OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
16089c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1609ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Lo = merge(Lo, FieldLo);
1610ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Hi = merge(Hi, FieldHi);
1611ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        if (Lo == Memory || Hi == Memory)
1612ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          break;
1613ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      }
1614ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    }
1615ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1616ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // Classify the fields one at a time, merging the results.
1617c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    unsigned idx = 0;
1618548e478b8bd02b0295bc4efd0c282337f00646fdBruno Cardoso Lopes    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
161917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis           i != e; ++i, ++idx) {
1620c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1621c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      bool BitField = i->isBitField();
1622c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1623b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1624b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
1625c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1626b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // The only case a 256-bit wide vector could be used is when the struct
1627b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1628b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // to work for sizes wider than 128, early check and fallback to memory.
1629b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      //
1630b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1631b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        Lo = Memory;
1632b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        return;
1633b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      }
1634c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Note, skip this test for bit-fields, see below.
1635ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1636c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Memory;
1637c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1638c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      }
1639c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1640c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Classify this field.
1641c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1642c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1643c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // exceeds a single eightbyte, each is classified
1644c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // separately. Each eightbyte gets initialized to class
1645c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // NO_CLASS.
1646c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
1647c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1648c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Bit-fields require special handling, they do not force the
1649c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // structure to be passed in memory even if unaligned, and
1650c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // therefore they can straddle an eightbyte.
1651c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (BitField) {
1652c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        // Ignore padding bit-fields.
1653c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (i->isUnnamedBitfield())
1654c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          continue;
1655c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1656c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1657a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        uint64_t Size = i->getBitWidthValue(getContext());
1658c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1659c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Lo = Offset / 64;
1660c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1661c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        FieldLo = FieldHi = NoClass;
1662c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (EB_Lo) {
1663c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1664c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = NoClass;
1665c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = Integer;
1666c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        } else {
1667c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = Integer;
1668c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = EB_Hi ? Integer : NoClass;
1669c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        }
1670c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      } else
16719c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1672c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1673c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1674c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1675c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1676c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1677c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
16784943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1679c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1680c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1681c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
16829c254f0415bef9a0bafe5b5026ddb54b727597b1Chris LattnerABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
168346c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // If this is a scalar LLVM value then assume LLVM will pass it in the right
168446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // place naturally.
1685d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
168646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    // Treat an enum type as its underlying type.
168746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
168846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar      Ty = EnumTy->getDecl()->getIntegerType();
168946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
169046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return (Ty->isPromotableIntegerType() ?
169146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
169246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  }
169346c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
169446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  return ABIArgInfo::getIndirect(0);
169546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar}
169646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
1697ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedmanbool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1698ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1699ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    uint64_t Size = getContext().getTypeSize(VecTy);
1700ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    unsigned LargestVector = HasAVX ? 256 : 128;
1701ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    if (Size <= 64 || Size > LargestVector)
1702ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return true;
1703ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
1704ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1705ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  return false;
1706ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman}
1707ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1708edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1709edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                            unsigned freeIntRegs) const {
1710c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1711c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // place naturally.
1712edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1713edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // This assumption is optimistic, as there could be free registers available
1714edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // when we need to pass this argument in memory, and LLVM could try to pass
1715edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // the argument in the free register. This does not seem to happen currently,
1716edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // but this code would be much safer if we could mark the argument with
1717edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // 'onstack'. See PR12193.
1718ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
1719aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
1720aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1721aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
1722aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1723cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
1724cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1725aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1726c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1727ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1728ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
17290a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1730855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // Compute the byval alignment. We specify the alignment of the byval in all
1731855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // cases so that the mid-level optimizer knows the alignment of the byval.
1732855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1733edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1734edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // Attempt to avoid passing indirect results using byval when possible. This
1735edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // is important for good codegen.
1736edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1737edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We do this by coercing the value into a scalar type which the backend can
1738edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // handle naturally (i.e., without using byval).
1739edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1740edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // For simplicity, we currently only do this when we have exhausted all of the
1741edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // free integer registers. Doing this when there are free integer registers
1742edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // would require more care, as we would have to ensure that the coerced value
1743edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // did not claim the unused register. That would require either reording the
1744edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to the function (so that any subsequent inreg values came first),
1745edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // or only doing this optimization when there were no following arguments that
1746edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // might be inreg.
1747edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1748edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We currently expect it to be rare (particularly in well written code) for
1749edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to be passed on the stack when there are still free integer
1750edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // registers available (this would typically imply large structs being passed
1751edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // by value), so this seems like a fair tradeoff for now.
1752edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1753edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We can revisit this if the backend grows support for 'onstack' parameter
1754edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // attributes. See PR12193.
1755edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  if (freeIntRegs == 0) {
1756edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    uint64_t Size = getContext().getTypeSize(Ty);
1757edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1758edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // If this type fits in an eightbyte, coerce it into the matching integral
1759edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // type, which will end up on the stack (with alignment 8).
1760edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    if (Align == 8 && Size <= 64)
1761edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1762edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                                          Size));
1763edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  }
1764edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1765855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  return ABIArgInfo::getIndirect(Align);
1766c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1767c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
17684943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// GetByteVectorType - The ABI specifies that a value should be passed in an
17694943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
17700f408f5242522cbede304472e17931357c1b573dChris Lattner/// vector register.
17714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesllvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
17729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IRType = CGT.ConvertType(Ty);
17738bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
177415842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // Wrapper structs that just contain vectors are passed just like vectors,
177515842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // strip them off if present.
17769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
177715842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  while (STy && STy->getNumElements() == 1) {
177815842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    IRType = STy->getElementType(0);
177915842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    STy = dyn_cast<llvm::StructType>(IRType);
178015842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  }
17818bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1782528a8c7b4c39ae1c551760fd087a508a71ee9541Bruno Cardoso Lopes  // If the preferred type is a 16-byte vector, prefer to pass it.
17839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
17849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = VT->getElementType();
17854943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    unsigned BitWidth = VT->getBitWidth();
1786ce275675d33142c235d7027db16abe43da616ee4Tanya Lattner    if ((BitWidth >= 128 && BitWidth <= 256) &&
17870f408f5242522cbede304472e17931357c1b573dChris Lattner        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
17880f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
17890f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
17900f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(128)))
17910f408f5242522cbede304472e17931357c1b573dChris Lattner      return VT;
17920f408f5242522cbede304472e17931357c1b573dChris Lattner  }
17938bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17940f408f5242522cbede304472e17931357c1b573dChris Lattner  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
17950f408f5242522cbede304472e17931357c1b573dChris Lattner}
17960f408f5242522cbede304472e17931357c1b573dChris Lattner
1797e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1798e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// is known to either be off the end of the specified type or being in
1799e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// alignment padding.  The user type specified is known to be at most 128 bits
1800e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// in size, and have passed through X86_64ABIInfo::classify with a successful
1801e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// classification that put one of the two halves in the INTEGER class.
1802e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner///
1803e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// It is conservatively correct to return false.
1804e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattnerstatic bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1805e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                  unsigned EndBit, ASTContext &Context) {
1806e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If the bytes being queried are off the end of the type, there is no user
1807e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // data hiding here.  This handles analysis of builtins, vectors and other
1808e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // types that don't contain interesting padding.
1809e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1810e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (TySize <= StartBit)
1811e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1812e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1813021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1814021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1815021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1816021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner
1817021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // Check each element to see if the element overlaps with the queried range.
1818021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    for (unsigned i = 0; i != NumElts; ++i) {
1819021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      // If the element is after the span we care about, then we're done..
1820021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltOffset = i*EltSize;
1821021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (EltOffset >= EndBit) break;
18228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1823021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1824021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1825021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner                                 EndBit-EltOffset, Context))
1826021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner        return false;
1827021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    }
1828021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // If it overlaps no elements, then it is safe to process as padding.
1829021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    return true;
1830021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
18318bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1832e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1833e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const RecordDecl *RD = RT->getDecl();
1834e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
18358bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1836e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If this is a C++ record, check the bases first.
1837e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1838e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1839e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner           e = CXXRD->bases_end(); i != e; ++i) {
1840e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1841e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner               "Unexpected base class!");
1842e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        const CXXRecordDecl *Base =
1843e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
18448bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1845e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        // If the base is after the span we care about, ignore it.
1846d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
1847e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (BaseOffset >= EndBit) continue;
18488bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1849e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1850e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (!BitsContainNoUserData(i->getType(), BaseStart,
1851e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                   EndBit-BaseOffset, Context))
1852e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          return false;
1853e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      }
1854e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
18558bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1856e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Verify that no field has data that overlaps the region of interest.  Yes
1857e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // this could be sped up a lot by being smarter about queried fields,
1858e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // however we're only looking at structs up to 16 bytes, so we don't care
1859e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // much.
1860e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    unsigned idx = 0;
1861e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1862e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner         i != e; ++i, ++idx) {
1863e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
18648bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1865e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      // If we found a field after the region we care about, then we're done.
1866e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (FieldOffset >= EndBit) break;
1867e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1868e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1869e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1870e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                 Context))
1871e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return false;
1872e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
18738bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1874e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If nothing in this record overlapped the area of interest, then we're
1875e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // clean.
1876e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1877e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
18788bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1879e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  return false;
1880e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner}
1881e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
18820b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
18830b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float member at the specified offset.  For example, {int,{float}} has a
18840b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float at offset 4.  It is conservatively correct for this routine to return
18850b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// false.
18862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerstatic bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
188725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow                                  const llvm::DataLayout &TD) {
18880b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // Base case if we find a float.
18890b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (IROffset == 0 && IRType->isFloatTy())
18900b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return true;
18918bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18920b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is a struct, recurse into the field at the specified offset.
18932acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
18940b3620066bfbb33004bed1816c851a923b9301afChris Lattner    const llvm::StructLayout *SL = TD.getStructLayout(STy);
18950b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned Elt = SL->getElementContainingOffset(IROffset);
18960b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= SL->getElementOffset(Elt);
18970b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
18980b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
18998bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
19000b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is an array, recurse into the field at the specified offset.
19012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
19022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ATy->getElementType();
19030b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned EltSize = TD.getTypeAllocSize(EltTy);
19040b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= IROffset/EltSize*EltSize;
19050b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(EltTy, IROffset, TD);
19060b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
19070b3620066bfbb33004bed1816c851a923b9301afChris Lattner
19080b3620066bfbb33004bed1816c851a923b9301afChris Lattner  return false;
19090b3620066bfbb33004bed1816c851a923b9301afChris Lattner}
19100b3620066bfbb33004bed1816c851a923b9301afChris Lattner
1911f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1912f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1913f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// low 8 bytes of an XMM register, corresponding to the SSE class.
19149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
19159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1916f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                   QualType SourceTy, unsigned SourceOffset) const {
1917cba8d310163f84630fd140fbfa9b6fdad9d26587Chris Lattner  // The only three choices we have are either double, <2 x float>, or float. We
1918f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // pass as float if the last 4 bytes is just padding.  This happens for
1919f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // structs that contain 3 floats.
1920f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1921f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                            SourceOffset*8+64, getContext()))
1922f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner    return llvm::Type::getFloatTy(getVMContext());
19238bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
19240b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // We want to pass as <2 x float> if the LLVM IR type contains a float at
19250b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
19260b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // case.
192725a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
192825a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow      ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
192922fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
19308bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1931f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  return llvm::Type::getDoubleTy(getVMContext());
1932f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner}
1933f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1934f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
19350d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
19360d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// an 8-byte GPR.  This means that we either have a scalar or we are talking
19370d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// about the high or low part of an up-to-16-byte struct.  This routine picks
19380d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// the best LLVM IR type to represent this, which may be i64 or may be anything
1939519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1940519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// etc).
1941519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1942519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1943519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1944519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the 8-byte value references.  PrefType may be null.
1945519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1946519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// SourceTy is the source level type for the entire argument.  SourceOffset is
1947519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// an offset into this that we're processing (which is always either 0 or 8).
1948519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
19499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
19509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
19510d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                       QualType SourceTy, unsigned SourceOffset) const {
1952e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1953e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // returning an 8-byte unit starting with it.  See if we can safely use it.
1954e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (IROffset == 0) {
1955e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Pointers and int64's always fill the 8-byte unit.
1956babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff    if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1957babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff        IRType->isIntegerTy(64))
1958e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      return IRType;
1959e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1960e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1961e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // goodness in the source type is just tail padding.  This is allowed to
1962e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // kick in for struct {double,int} on the int, but not on
1963e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // struct{double,int,int} because we wouldn't return the second int.  We
1964e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // have to do this analysis on the source type because we can't depend on
1965e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // unions being lowered a specific way etc.
1966e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1967babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff        IRType->isIntegerTy(32) ||
1968babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff        (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1969babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff      unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1970babaf31d401310464db93627ef6b195a7ffb1029Derek Schuff          cast<llvm::IntegerType>(IRType)->getBitWidth();
19718bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1972e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1973e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                SourceOffset*8+64, getContext()))
1974e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return IRType;
1975e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
1976e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
1977519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
19782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1979519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    // If this is a struct, recurse into the field at the specified offset.
198025a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
1981519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    if (IROffset < SL->getSizeInBytes()) {
1982519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1983519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      IROffset -= SL->getElementOffset(FieldIdx);
19848bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
19850d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
19860d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                    SourceTy, SourceOffset);
19878bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    }
1988519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  }
19898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
19902acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
19919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = ATy->getElementType();
199225a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
1993021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltOffset = IROffset/EltSize*EltSize;
19940d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
19950d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                  SourceOffset);
1996021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
19978bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1998519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // Okay, we don't have any better idea of what to pass, so we pass this in an
1999519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // integer register that isn't too big to fit the rest of the struct.
20009e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  unsigned TySizeInBytes =
20019e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
2002519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
20039e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  assert(TySizeInBytes != SourceOffset && "Empty field?");
20048bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2005519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // It is always safe to classify this as an integer type up to i64 that
2006519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // isn't larger than the structure.
20079e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  return llvm::IntegerType::get(getVMContext(),
20089e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
2009519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner}
2010519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
201166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
201266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
201366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// be used as elements of a two register pair to pass or return, return a
201466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// first class aggregate to represent them.  For example, if the low part of
201566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// a by-value argument should be passed as i32* and the high part as float,
201666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// return {i32*, float}.
20179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *
2018ef6de3da8572607f786303c07150daa6e140ab19Jay FoadGetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
201925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow                           const llvm::DataLayout &TD) {
202066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // In order to correctly satisfy the ABI, we need to the high part to start
202166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // at offset 8.  If the high and low parts we inferred are both 4-byte types
202266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
202366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // the second element at offset 8.  Check for this:
202466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
202566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiAlign = TD.getABITypeAlignment(Hi);
202625a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
202766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
20289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
202966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // To handle this, we have to increase the size of the low part so that the
203066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // second element will start at an 8 byte offset.  We can't increase the size
203166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // of the second element because it might make us access off the end of the
203266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // struct.
203366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HiStart != 8) {
203466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // There are only two sorts of types the ABI generation code can produce for
203566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
203666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // Promote these to a larger type.
203766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    if (Lo->isFloatTy())
203866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getDoubleTy(Lo->getContext());
203966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    else {
204066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
204166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getInt64Ty(Lo->getContext());
204266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    }
204366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  }
20449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
20459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
20469cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
20479cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
204866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // Verify that the second element is at an 8-byte offset.
204966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
205066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner         "Invalid x86-64 argument pair!");
205166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  return Result;
205266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner}
205366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
20541090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerABIArgInfo X86_64ABIInfo::
2055a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerclassifyReturnType(QualType RetTy) const {
2056c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2057c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classification algorithm.
2058c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
20599c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(RetTy, 0, Lo, Hi);
2060c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2061c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
2062c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2063c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
20659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
2066c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
2067c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
2068117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
2069117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
2070117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
2071117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
2072117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2073117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
2074117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
2075c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2077c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2078b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
2079c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2080c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2081c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // hidden argument.
2082c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
20839c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    return getIndirectReturnResult(RetTy);
2084c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2085c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2086c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rax, %rdx is used.
2087c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
20889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
20898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2090eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
2091eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
2092eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2093eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
2094eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2095eb518b4b89e4134b21975530809697142f69b779Chris Lattner        RetTy = EnumTy->getDecl()->getIntegerType();
20968bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2097eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (RetTy->isIntegralOrEnumerationType() &&
2098eb518b4b89e4134b21975530809697142f69b779Chris Lattner          RetTy->isPromotableIntegerType())
2099eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
2100eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
2101519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    break;
2102c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2103c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register of the sequence %xmm0, %xmm1 is used.
2105c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSE:
21069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
21070b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2108c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2109c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned on the X87 stack in %st0 as 80-bit x87 number.
2111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2112ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
21130b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2114c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // part of the value is returned in %st0 and the imaginary part in
2117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // %st1.
2118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
21207650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
2121ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner                                    llvm::Type::getX86_FP80Ty(getVMContext()),
2122c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                    NULL);
2123c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
21269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
2127c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
2128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously and X87 should
2129c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // never occur as a hi class.
2130c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2131c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2132b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2133c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2134c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87: // Previously handled.
21350b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner  case NoClass:
21360b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2137c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
21383db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case Integer:
21399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
21403db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
21413db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
21433db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case SSE:
21449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
21453db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
21463db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2147c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2148c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
21504943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // is passed in the next available eightbyte chunk if the last used
21514943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // vector register.
2152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
2153fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // SSEUP should always be preceded by SSE, just widen.
2154c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2155c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Lo == SSE && "Unexpected SSEUp classification.");
21564943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(RetTy);
2157c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2158c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2160c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned together with the previous X87 value in %st0.
2161c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2162fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // If X87Up is preceded by X87, we don't need to do
2163c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // anything. However, in some cases with unions it may not be
2164fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // preceded by X87. In such situations we follow gcc and pass the
2165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // extra bits in an SSE reg.
2166603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    if (Lo != X87) {
21679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
21683db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
21693db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner        return ABIArgInfo::getDirect(HighPart, 8);
2170603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    }
2171c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2172c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
21739cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
21743db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2175645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2176645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
217766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HighPart)
217825a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2179c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2180eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
21819c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner}
21829c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
2183edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::classifyArgumentType(
2184edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2185edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  const
2186edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar{
2187c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
21889c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(Ty, 0, Lo, Hi);
21898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
2191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Enforce these by construction.
2192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededInt = 0;
2196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededSSE = 0;
21979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
2198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
2199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
2200117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
2201117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
2202117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
2203117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
2204117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2205117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
2206117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
22078bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2208c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2209c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // on the stack.
2210c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2211c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2213c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // COMPLEX_X87, it is passed in memory.
2214c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2215c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2216ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
2217ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman      ++neededInt;
2218edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    return getIndirectResult(Ty, freeIntRegs);
2219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2221c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2222b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
2223c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2224c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // and %r9 is used.
2227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
22289c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    ++neededInt;
22298bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
223049382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
22319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2232eb518b4b89e4134b21975530809697142f69b779Chris Lattner
2233eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
2234eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
2235eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2236eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
2237eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2238eb518b4b89e4134b21975530809697142f69b779Chris Lattner        Ty = EnumTy->getDecl()->getIntegerType();
22398bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2240eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (Ty->isIntegralOrEnumerationType() &&
2241eb518b4b89e4134b21975530809697142f69b779Chris Lattner          Ty->isPromotableIntegerType())
2242eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
2243eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
22448bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2248c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register is used, the registers are taken in the
2249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // order from %xmm0 to %xmm7.
2250bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  case SSE: {
22519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
225214508ff0bffee0fdfe5d336946c6db0e709099c8Eli Friedman    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
225399aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    ++neededSSE;
2254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2256bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  }
2257c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
22589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
2259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
2260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously, ComplexX87 and X87 should
2261fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // never occur as hi classes, and X87Up must be preceded by X87,
2262c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // which is passed in memory.
2263c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2264c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2266b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass: break;
22698bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2270645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case Integer:
2271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededInt;
227249382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
22739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2274117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2275645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2276645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // X87Up generally doesn't occur here (long double is passed in
2280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // memory), except in situations involving unions.
2281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2282645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case SSE:
22839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
22848bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2285645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2286645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2287117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2288c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededSSE;
2289c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2290c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2291c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2292c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte is passed in the upper half of the last used SSE
22938bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    // register.  This only happens when 128-bit vectors are passed.
2294c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2295ab5722e67794b3954c874a369086fc5f41ac46a5Chris Lattner    assert(Lo == SSE && "Unexpected SSEUp classification");
22964943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(Ty);
2297c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2298c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2299c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2300645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2301645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2302645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
2303645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  if (HighPart)
230425a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
23059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2306eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
2307c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2309ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
23108bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2311a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2312c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2313c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Keep track of the number of assigned registers.
231499aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned freeIntRegs = 6, freeSSERegs = 8;
2315c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If the return value is indirect, then the hidden argument is consuming one
2317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // integer register.
2318c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FI.getReturnInfo().isIndirect())
2319c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    --freeIntRegs;
2320c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2321c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // get assigned (in left-to-right order) for passing as follows...
2323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov       it != ie; ++it) {
232599aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    unsigned neededInt, neededSSE;
2326edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2327edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                    neededSSE);
2328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: If there are no registers available for any
2330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte of an argument, the whole argument is passed on the
2331c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // stack. If registers have already been assigned for some
2332c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbytes of such an argument, the assignments get reverted.
233399aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2334c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeIntRegs -= neededInt;
2335c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeSSERegs -= neededSSE;
2336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
2337edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      it->info = getIndirectResult(it->type, freeIntRegs);
2338c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
2339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2340c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2341c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        QualType Ty,
2344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        CodeGenFunction &CGF) {
2345c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area_p =
2346c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area =
2348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2349c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2350c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2351c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // byte boundary if alignment needed by type exceeds 8 byte boundary.
23528d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // It isn't stated explicitly in the standard, but in practice we use
23538d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // alignment greater than 16 where necessary.
2354c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Align > 8) {
23568d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
23570032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    llvm::Value *Offset =
23588d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman      llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
236177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                                    CGF.Int64Ty);
23628d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area =
2364c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 overflow_arg_area->getType(),
2366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 "overflow_arg_area.align");
2367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2368c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2369c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
23702acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2371c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Res =
2372c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateBitCast(overflow_arg_area,
237396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                              llvm::PointerType::getUnqual(LTy));
2374c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2375c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2376c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->overflow_arg_area + sizeof(type).
2377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2378c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an 8 byte boundary.
2379c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2380c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
23810032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Offset =
238277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
2383c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                            "overflow_arg_area.next");
2385c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2387c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2388c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Res;
2389c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2390c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2391c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2392c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
2393c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Assume that va_list type is correct; should be pointer to LLVM type:
2394c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // struct {
2395c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 gp_offset;
2396c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 fp_offset;
2397c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* overflow_arg_area;
2398c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* reg_save_area;
2399c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // };
240099aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned neededInt, neededSSE;
24018bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2402a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  Ty = CGF.getContext().getCanonicalType(Ty);
2403edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
2404c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2405c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2406c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in the registers. If not go to step 7.
2407c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!neededInt && !neededSSE)
2408c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2409c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2410c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2411c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // general purpose registers needed to pass type and num_fp to hold
2412c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the number of floating point registers needed.
2413c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2414c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2415c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2416c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset > 304 - num_fp * 16 go to step 7.
2417c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2418c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2419c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // register save space).
2420c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *InRegs = 0;
2422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2423c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2424c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
2425c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2426c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
24271090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
24281090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2429c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2430c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2431c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
2432c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2433c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2434c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FitsInFP =
24351090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
24361090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2437c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2438c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2439c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2440c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2441c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2442c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2443c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2444c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2445c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in registers.
2446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2447c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InRegBlock);
2448c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2450c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an offset of l->gp_offset and/or l->fp_offset. This may require
2451c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // copying to a temporary location in case the parameter is passed
2452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in different register classes or requires an alignment greater
2453c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // than 8 for general purpose registers and 16 for XMM registers.
2454c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2455c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This really results in shameful code when we end up needing to
2456c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // collect arguments from different places; often what should result in a
2457c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // simple assembling of a structure from scattered addresses has many more
2458c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // loads than necessary. Can we clean this up?
24592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2460c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *RegAddr =
2461c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2462c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                           "reg_save_area");
2463c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt && neededSSE) {
2464c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Cleanup.
2465800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
24662acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2467c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2468c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
24692acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyLo = ST->getElementType(0);
24702acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyHi = ST->getElementType(1);
2471a8b7a7d3eaa51dd200cba1e5541f2542d24d7a6eChris Lattner    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2472c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov           "Unexpected ABI info for mixed regs");
24732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
24742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2475c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2476c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2477f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2478f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
2479c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *V =
2480c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2481c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2482c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2483c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2484c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2485a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    RegAddr = CGF.Builder.CreateBitCast(Tmp,
248696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2487c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else if (neededInt) {
2488c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
249096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2491dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner  } else if (neededSSE == 1) {
2492dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2493dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2494dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2495c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else {
2496dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    assert(neededSSE == 2 && "Invalid number of needed registers!");
2497dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // SSE registers are spaced 16 bytes apart in the register save
2498dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // area, we need to collect the two eightbytes together.
2499dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
25001090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
25018b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Type *DoubleTy = CGF.DoubleTy;
25022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *DblPtrTy =
2503dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner      llvm::PointerType::getUnqual(DoubleTy);
25042acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = llvm::StructType::get(DoubleTy,
2505dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                       DoubleTy, NULL);
2506dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2507dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2508dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2509dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2510dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2511dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2512dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2513dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2514dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2515c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2516c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2517c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 5. Set:
2518c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->gp_offset = l->gp_offset + num_gp * 8
2519c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset = l->fp_offset + num_fp * 16.
2520c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
252177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
2522c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2523c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            gp_offset_p);
2524c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2525c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
252677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
2527c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2528c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            fp_offset_p);
2529c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2530c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBranch(ContBlock);
2531c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2532c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in memory.
2533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2534c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InMemBlock);
2535c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2536c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2537c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Return the appropriate result.
2538c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2539c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(ContBlock);
2540bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
2541c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                 "vaarg.addr");
2542c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(RegAddr, InRegBlock);
2543c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(MemAddr, InMemBlock);
2544c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return ResAddr;
2545c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2546c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2547ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur IskhodzhanovABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
2548a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2549a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isVoidType())
2550a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIgnore();
2551a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2552a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2553a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    Ty = EnumTy->getDecl()->getIntegerType();
2554a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2555a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  uint64_t Size = getContext().getTypeSize(Ty);
2556a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2557a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2558ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (IsReturnType) {
2559ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (isRecordReturnIndirect(RT, CGT))
2560ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov        return ABIArgInfo::getIndirect(0, false);
2561ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    } else {
2562ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2563ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov        return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2564ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    }
2565ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
2566ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (RT->getDecl()->hasFlexibleArrayMember())
2567a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2568a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
25696f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // FIXME: mingw-w64-gcc emits 128-bit struct as i128
257064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
25716f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
25726f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi                                                          Size));
25736f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi
25746f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
25756f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // not 1, 2, 4, or 8 bytes, must be passed by reference."
25766f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size <= 64 &&
2577ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        (Size & (Size - 1)) == 0)
2578a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2579a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi                                                          Size));
2580a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2581a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2582a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  }
2583a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2584a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isPromotableIntegerType())
2585a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getExtend();
2586a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2587a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  return ABIArgInfo::getDirect();
2588a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2589a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2590a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumivoid WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2591a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2592a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  QualType RetTy = FI.getReturnType();
2593ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  FI.getReturnInfo() = classify(RetTy, true);
2594a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2595a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2596a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi       it != ie; ++it)
2597ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    it->info = classify(it->type, false);
2598a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2599a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2600f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerllvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2601f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                      CodeGenFunction &CGF) const {
26028b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2603f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2604f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  CGBuilderTy &Builder = CGF.Builder;
2605f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2606f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                                       "ap");
2607f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2608f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Type *PTy =
2609f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2610f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2611f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2612f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  uint64_t Offset =
2613f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2614f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *NextAddr =
2615f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2616f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                      "ap.next");
2617f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2618dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2619f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  return AddrTyped;
2620f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner}
2621dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2622c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramernamespace {
2623c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer
2624263366f9241366f29ba65b703120f302490c39ffDerek Schuffclass NaClX86_64ABIInfo : public ABIInfo {
2625263366f9241366f29ba65b703120f302490c39ffDerek Schuff public:
2626263366f9241366f29ba65b703120f302490c39ffDerek Schuff  NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2627263366f9241366f29ba65b703120f302490c39ffDerek Schuff      : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2628263366f9241366f29ba65b703120f302490c39ffDerek Schuff  virtual void computeInfo(CGFunctionInfo &FI) const;
2629263366f9241366f29ba65b703120f302490c39ffDerek Schuff  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2630263366f9241366f29ba65b703120f302490c39ffDerek Schuff                                 CodeGenFunction &CGF) const;
2631263366f9241366f29ba65b703120f302490c39ffDerek Schuff private:
2632263366f9241366f29ba65b703120f302490c39ffDerek Schuff  PNaClABIInfo PInfo;  // Used for generating calls with pnaclcall callingconv.
2633263366f9241366f29ba65b703120f302490c39ffDerek Schuff  X86_64ABIInfo NInfo; // Used for everything else.
2634263366f9241366f29ba65b703120f302490c39ffDerek Schuff};
2635263366f9241366f29ba65b703120f302490c39ffDerek Schuff
2636263366f9241366f29ba65b703120f302490c39ffDerek Schuffclass NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo  {
2637263366f9241366f29ba65b703120f302490c39ffDerek Schuff public:
2638263366f9241366f29ba65b703120f302490c39ffDerek Schuff  NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2639263366f9241366f29ba65b703120f302490c39ffDerek Schuff      : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2640263366f9241366f29ba65b703120f302490c39ffDerek Schuff};
2641263366f9241366f29ba65b703120f302490c39ffDerek Schuff
2642c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer}
2643c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer
2644263366f9241366f29ba65b703120f302490c39ffDerek Schuffvoid NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2645263366f9241366f29ba65b703120f302490c39ffDerek Schuff  if (FI.getASTCallingConvention() == CC_PnaclCall)
2646263366f9241366f29ba65b703120f302490c39ffDerek Schuff    PInfo.computeInfo(FI);
2647263366f9241366f29ba65b703120f302490c39ffDerek Schuff  else
2648263366f9241366f29ba65b703120f302490c39ffDerek Schuff    NInfo.computeInfo(FI);
2649263366f9241366f29ba65b703120f302490c39ffDerek Schuff}
2650263366f9241366f29ba65b703120f302490c39ffDerek Schuff
2651263366f9241366f29ba65b703120f302490c39ffDerek Schuffllvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2652263366f9241366f29ba65b703120f302490c39ffDerek Schuff                                          CodeGenFunction &CGF) const {
2653263366f9241366f29ba65b703120f302490c39ffDerek Schuff  // Always use the native convention; calling pnacl-style varargs functions
2654263366f9241366f29ba65b703120f302490c39ffDerek Schuff  // is unuspported.
2655263366f9241366f29ba65b703120f302490c39ffDerek Schuff  return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2656263366f9241366f29ba65b703120f302490c39ffDerek Schuff}
2657263366f9241366f29ba65b703120f302490c39ffDerek Schuff
2658263366f9241366f29ba65b703120f302490c39ffDerek Schuff
2659ec853ba1087f606e9685cb1e800616565ba35093John McCall// PowerPC-32
2660ec853ba1087f606e9685cb1e800616565ba35093John McCall
2661ec853ba1087f606e9685cb1e800616565ba35093John McCallnamespace {
2662ec853ba1087f606e9685cb1e800616565ba35093John McCallclass PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2663ec853ba1087f606e9685cb1e800616565ba35093John McCallpublic:
2664ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
26658bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2666ec853ba1087f606e9685cb1e800616565ba35093John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2667ec853ba1087f606e9685cb1e800616565ba35093John McCall    // This is recovered from gcc output.
2668ec853ba1087f606e9685cb1e800616565ba35093John McCall    return 1; // r1 is the dedicated stack pointer
2669ec853ba1087f606e9685cb1e800616565ba35093John McCall  }
2670ec853ba1087f606e9685cb1e800616565ba35093John McCall
2671ec853ba1087f606e9685cb1e800616565ba35093John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
26728bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
2673ec853ba1087f606e9685cb1e800616565ba35093John McCall};
2674ec853ba1087f606e9685cb1e800616565ba35093John McCall
2675ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2676ec853ba1087f606e9685cb1e800616565ba35093John McCall
2677ec853ba1087f606e9685cb1e800616565ba35093John McCallbool
2678ec853ba1087f606e9685cb1e800616565ba35093John McCallPPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2679ec853ba1087f606e9685cb1e800616565ba35093John McCall                                                llvm::Value *Address) const {
2680ec853ba1087f606e9685cb1e800616565ba35093John McCall  // This is calculated from the LLVM and GCC tables and verified
2681ec853ba1087f606e9685cb1e800616565ba35093John McCall  // against gcc output.  AFAIK all ABIs use the same encoding.
2682ec853ba1087f606e9685cb1e800616565ba35093John McCall
2683ec853ba1087f606e9685cb1e800616565ba35093John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2684ec853ba1087f606e9685cb1e800616565ba35093John McCall
26858b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::IntegerType *i8 = CGF.Int8Ty;
2686ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2687ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2688ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2689ec853ba1087f606e9685cb1e800616565ba35093John McCall
2690ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 0-31: r0-31, the 4-byte general-purpose registers
2691aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 31);
2692ec853ba1087f606e9685cb1e800616565ba35093John McCall
2693ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 32-63: fp0-31, the 8-byte floating-point registers
2694aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2695ec853ba1087f606e9685cb1e800616565ba35093John McCall
2696ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64-76 are various 4-byte special-purpose registers:
2697ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64: mq
2698ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 65: lr
2699ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 66: ctr
2700ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 67: ap
2701ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 68-75 cr0-7
2702ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 76: xer
2703aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 64, 76);
2704ec853ba1087f606e9685cb1e800616565ba35093John McCall
2705ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 77-108: v0-31, the 16-byte vector registers
2706aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2707ec853ba1087f606e9685cb1e800616565ba35093John McCall
2708ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 109: vrsave
2709ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 110: vscr
2710ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 111: spe_acc
2711ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 112: spefscr
2712ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 113: sfp
2713aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 109, 113);
2714ec853ba1087f606e9685cb1e800616565ba35093John McCall
27158bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  return false;
2716ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2717ec853ba1087f606e9685cb1e800616565ba35093John McCall
27180fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky// PowerPC-64
27190fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
27200fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackynamespace {
27212fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
27222fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtclass PPC64_SVR4_ABIInfo : public DefaultABIInfo {
27232fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27242fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtpublic:
27252fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
27262fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
272771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  bool isPromotableTypeForABI(QualType Ty) const;
272871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
272971c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  ABIArgInfo classifyReturnType(QualType RetTy) const;
273071c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  ABIArgInfo classifyArgumentType(QualType Ty) const;
273171c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
2732b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // TODO: We can add more logic to computeInfo to improve performance.
2733b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // Example: For aggregate arguments that fit in a register, we could
2734b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // use getDirectInReg (as is done below for structs containing a single
2735b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // floating-point value) to avoid pushing them to memory on function
2736b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // entry.  This would require changing the logic in PPCISelLowering
2737b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  // when lowering the parameters in the caller and args in the callee.
2738b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  virtual void computeInfo(CGFunctionInfo &FI) const {
2739b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2740b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2741b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt         it != ie; ++it) {
2742b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      // We rely on the default argument classification for the most part.
2743b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      // One exception:  An aggregate containing a single floating-point
2744b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      // item must be passed in a register if one is available.
2745b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      const Type *T = isSingleElementStruct(it->type, getContext());
2746b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      if (T) {
2747b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt        const BuiltinType *BT = T->getAs<BuiltinType>();
2748b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt        if (BT && BT->isFloatingPoint()) {
2749b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt          QualType QT(T, 0);
2750b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt          it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2751b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt          continue;
2752b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt        }
2753b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      }
2754b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt      it->info = classifyArgumentType(it->type);
2755b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt    }
2756b1f5fe017a596e0c7749dee10c9d3ff1c0f2788cBill Schmidt  }
27572fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27582fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
27592fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                                 QualType Ty,
27602fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                                 CodeGenFunction &CGF) const;
27612fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt};
27622fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27632fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtclass PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
27642fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtpublic:
27652fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
27662fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
27672fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27682fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
27692fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    // This is recovered from gcc output.
27702fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    return 1; // r1 is the dedicated stack pointer
27712fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  }
27722fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27732fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
27742fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                               llvm::Value *Address) const;
27752fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt};
27762fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
27770fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackyclass PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
27780fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackypublic:
27790fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
27800fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
27810fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
27820fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    // This is recovered from gcc output.
27830fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    return 1; // r1 is the dedicated stack pointer
27840fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  }
27850fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
27860fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
27870fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky                               llvm::Value *Address) const;
27880fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky};
27890fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
27900fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
27910fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
279271c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand// Return true if the ABI requires Ty to be passed sign- or zero-
279371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand// extended to 64 bits.
279471c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigandbool
279571c0dcc129262e565069fbfc0b5239a05c94b86cUlrich WeigandPPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
279671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  // Treat an enum type as its underlying type.
279771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
279871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    Ty = EnumTy->getDecl()->getIntegerType();
279971c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
280071c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  // Promotable integer types are required to be promoted by the ABI.
280171c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (Ty->isPromotableIntegerType())
280271c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    return true;
280371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
280471c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  // In addition to the usual promotable integer types, we also need to
280571c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  // extend all 32-bit types, since the ABI requires promotion to 64 bits.
280671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
280771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    switch (BT->getKind()) {
280871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    case BuiltinType::Int:
280971c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    case BuiltinType::UInt:
281071c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand      return true;
281171c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    default:
281271c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand      break;
281371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    }
281471c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
281571c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  return false;
281671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand}
281771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
281871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich WeigandABIArgInfo
281971c0dcc129262e565069fbfc0b5239a05c94b86cUlrich WeigandPPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
2820c9715fc7c329c85e0b7aa0884c9209fa1fe5b819Bill Schmidt  if (Ty->isAnyComplexType())
2821c9715fc7c329c85e0b7aa0884c9209fa1fe5b819Bill Schmidt    return ABIArgInfo::getDirect();
2822c9715fc7c329c85e0b7aa0884c9209fa1fe5b819Bill Schmidt
282371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (isAggregateTypeForABI(Ty)) {
2824ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2825ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
282671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
282771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    return ABIArgInfo::getIndirect(0);
282871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  }
282971c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
283071c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  return (isPromotableTypeForABI(Ty) ?
283171c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
283271c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand}
283371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
283471c0dcc129262e565069fbfc0b5239a05c94b86cUlrich WeigandABIArgInfo
283571c0dcc129262e565069fbfc0b5239a05c94b86cUlrich WeigandPPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
283671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (RetTy->isVoidType())
283771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    return ABIArgInfo::getIgnore();
283871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
28399e6111ad10d29802d8eff99c941d262979e99084Bill Schmidt  if (RetTy->isAnyComplexType())
28409e6111ad10d29802d8eff99c941d262979e99084Bill Schmidt    return ABIArgInfo::getDirect();
28419e6111ad10d29802d8eff99c941d262979e99084Bill Schmidt
284271c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  if (isAggregateTypeForABI(RetTy))
284371c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand    return ABIArgInfo::getIndirect(0);
284471c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
284571c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand  return (isPromotableTypeForABI(RetTy) ?
284671c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
284771c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand}
284871c0dcc129262e565069fbfc0b5239a05c94b86cUlrich Weigand
28492fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
28502fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtllvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
28512fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                                           QualType Ty,
28522fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                                           CodeGenFunction &CGF) const {
28532fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Type *BP = CGF.Int8PtrTy;
28542fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Type *BPP = CGF.Int8PtrPtrTy;
28552fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
28562fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  CGBuilderTy &Builder = CGF.Builder;
28572fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
28582fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
28592fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
286019f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // Update the va_list pointer.  The pointer should be bumped by the
286119f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // size of the object.  We can trust getTypeSize() except for a complex
286219f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // type whose base type is smaller than a doubleword.  For these, the
286319f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // size of the object is 16 bytes; see below for further explanation.
28642fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
286519f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  QualType BaseTy;
286619f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  unsigned CplxBaseSize = 0;
286719f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt
286819f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
286919f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    BaseTy = CTy->getElementType();
287019f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
287119f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    if (CplxBaseSize < 8)
287219f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt      SizeInBytes = 16;
287319f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  }
287419f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt
28752fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
28762fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Value *NextAddr =
28772fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
28782fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                      "ap.next");
28792fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
28802fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
288119f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // If we have a complex type and the base type is smaller than 8 bytes,
288219f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // the ABI calls for the real and imaginary parts to be right-adjusted
288319f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // in separate doublewords.  However, Clang expects us to produce a
288419f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // pointer to a structure with the two parts packed tightly.  So generate
288519f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // loads of the real and imaginary parts relative to the va_list pointer,
288619f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  // and store them to a temporary structure.
288719f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  if (CplxBaseSize && CplxBaseSize < 8) {
288819f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
288919f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *ImagAddr = RealAddr;
289019f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
289119f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
289219f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
289319f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
289419f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
289519f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
289619f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
289719f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
289819f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt                                            "vacplx");
289919f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
290019f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
290119f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    Builder.CreateStore(Real, RealPtr, false);
290219f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    Builder.CreateStore(Imag, ImagPtr, false);
290319f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt    return Ptr;
290419f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt  }
290519f8e85d2e0ba2b97ad848b5f879ec96ebc1660eBill Schmidt
29062fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  // If the argument is smaller than 8 bytes, it is right-adjusted in
29072fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  // its doubleword slot.  Adjust the pointer to pick it up from the
29082fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  // correct offset.
29092fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  if (SizeInBytes < 8) {
29102fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
29112fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
29122fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
29132fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  }
29142fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
29152fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
29162fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  return Builder.CreateBitCast(Addr, PTy);
29172fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt}
29182fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
29192fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtstatic bool
29202fc107f5652a526d9c2972dc3b386e5d86769e44Bill SchmidtPPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
29212fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                              llvm::Value *Address) {
29220fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // This is calculated from the LLVM and GCC tables and verified
29230fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // against gcc output.  AFAIK all ABIs use the same encoding.
29240fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29250fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  CodeGen::CGBuilderTy &Builder = CGF.Builder;
29260fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29270fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::IntegerType *i8 = CGF.Int8Ty;
29280fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
29290fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
29300fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
29310fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29320fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 0-31: r0-31, the 8-byte general-purpose registers
29330fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
29340fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29350fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 32-63: fp0-31, the 8-byte floating-point registers
29360fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
29370fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29380fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64-76 are various 4-byte special-purpose registers:
29390fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64: mq
29400fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 65: lr
29410fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 66: ctr
29420fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 67: ap
29430fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 68-75 cr0-7
29440fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 76: xer
29450fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 64, 76);
29460fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29470fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 77-108: v0-31, the 16-byte vector registers
29480fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
29490fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29500fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 109: vrsave
29510fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 110: vscr
29520fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 111: spe_acc
29530fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 112: spefscr
29540fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 113: sfp
29550fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 109, 113);
29560fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
29570fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  return false;
29580fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
2959ec853ba1087f606e9685cb1e800616565ba35093John McCall
29602fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtbool
29612fc107f5652a526d9c2972dc3b386e5d86769e44Bill SchmidtPPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
29622fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  CodeGen::CodeGenFunction &CGF,
29632fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  llvm::Value *Address) const {
29642fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
29652fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
29662fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt}
29672fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
29682fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidtbool
29692fc107f5652a526d9c2972dc3b386e5d86769e44Bill SchmidtPPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
29702fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt                                                llvm::Value *Address) const {
29712fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
29722fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
29732fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt}
29742fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt
2975dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
297634d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar// ARM ABI Implementation
2977dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
297834d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
297934d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbarnamespace {
298034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2981c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass ARMABIInfo : public ABIInfo {
29825e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
29835e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  enum ABIKind {
29845e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    APCS = 0,
29855e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS = 1,
29865e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS_VFP
29875e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  };
29885e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
29895e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
29905e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind Kind;
29915e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
29925e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
2993bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
2994bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    setRuntimeCC();
2995bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  }
29965e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
299749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  bool isEABI() const {
299864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    StringRef Env = getTarget().getTriple().getEnvironmentName();
299994a7142f74bb4a9daa53c22087b19d4568073109Logan Chien    return (Env == "gnueabi" || Env == "eabi" ||
300094a7142f74bb4a9daa53c22087b19d4568073109Logan Chien            Env == "android" || Env == "androideabi");
300149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
300249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
30035e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
30045e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind getABIKind() const { return Kind; }
30055e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
3006a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
3007710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3008710c517431954cfffba519fc7814cfbd8412a9aaManman Ren                                  unsigned &AllocatedVFP,
3009b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren                                  bool &IsHA) const;
301097f81573636068fb9536436188caadf030584e58Manman Ren  bool isIllegalVectorType(QualType Ty) const;
3011c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3012ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
3013c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3014c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
3016bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
3017bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallingConv::ID getLLVMDefaultCC() const;
3018bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallingConv::ID getABIDefaultCC() const;
3019bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  void setRuntimeCC();
3020c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
3021c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
302282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass ARMTargetCodeGenInfo : public TargetCodeGenInfo {
302382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
3024ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3025ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
30266374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
302749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  const ARMABIInfo &getABIInfo() const {
302849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
302949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
303049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
30316374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
30326374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 13;
30336374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
303409345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
30355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef getARCRetainAutoreleasedReturnValueMarker() const {
3036f85e193739c953358c865005855253af4f68a497John McCall    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3037f85e193739c953358c865005855253af4f68a497John McCall  }
3038f85e193739c953358c865005855253af4f68a497John McCall
303909345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
304009345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky                               llvm::Value *Address) const {
30418b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
304209345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
304309345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    // 0-15 are the 16 integer registers.
30448b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
304509345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    return false;
304609345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  }
304749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
304849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
304949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    if (getABIInfo().isEABI()) return 88;
305049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return TargetCodeGenInfo::getSizeOfUnwindException();
305149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
305282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
305382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
305434d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar}
305534d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
3056ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3057b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // To correctly handle Homogeneous Aggregate, we need to keep track of the
3058710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // VFP registers allocated so far.
3059b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3060b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // VFP registers of the appropriate type unallocated then the argument is
3061b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // allocated to the lowest-numbered sequence of such registers.
3062b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3063b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // unallocated are marked as unavailable.
3064b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  unsigned AllocatedVFP = 0;
3065710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  int VFPRegs[16] = { 0 };
3066a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3067c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3068b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren       it != ie; ++it) {
3069b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    unsigned PreAllocation = AllocatedVFP;
3070b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    bool IsHA = false;
3071b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // 6.1.2.3 There is one VFP co-processor register class using registers
3072b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // s0-s15 (d0-d7) for passing arguments.
3073b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    const unsigned NumVFPs = 16;
3074710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
3075b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // If we do not have enough VFP registers for the HA, any VFP registers
3076b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // that are unallocated are marked as unavailable. To achieve this, we add
3077b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // padding of (NumVFPs - PreAllocation) floats.
3078b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3079b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      llvm::Type *PaddingTy = llvm::ArrayType::get(
3080b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren          llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3081b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3082b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    }
3083b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  }
30845e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
3085414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Always honor user-specified calling convention.
3086414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (FI.getCallingConvention() != llvm::CallingConv::C)
3087414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return;
3088414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
3089bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallingConv::ID cc = getRuntimeCC();
3090bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  if (cc != llvm::CallingConv::C)
3091bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    FI.setEffectiveCallingConvention(cc);
3092bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
3093bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
3094bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Return the default calling convention that LLVM will use.
3095bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3096bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  // The default calling convention that LLVM will infer.
309764aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
3098bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    return llvm::CallingConv::ARM_AAPCS_VFP;
3099b16abb1bd8ed94c7994836de24915703e6a4e81aDavid Tweed  else if (isEABI())
3100bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    return llvm::CallingConv::ARM_AAPCS;
31011ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola  else
3102bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    return llvm::CallingConv::ARM_APCS;
3103bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
310425117ab35c1a033846073183314c68ef07d1701aRafael Espindola
3105bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Return the calling convention that our ABI would like us to use
3106bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// as the C calling convention.
3107bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
31085e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  switch (getABIKind()) {
3109bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  case APCS: return llvm::CallingConv::ARM_APCS;
3110bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3111bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
31125e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  }
3113bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm_unreachable("bad ABI kind");
3114bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
3115bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
3116bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallvoid ARMABIInfo::setRuntimeCC() {
3117bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  assert(getRuntimeCC() == llvm::CallingConv::C);
3118bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
3119bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  // Don't muddy up the IR with a ton of explicit annotations if
3120bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  // they'd just match what LLVM will infer from the triple.
3121bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallingConv::ID abiCC = getABIDefaultCC();
3122bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  if (abiCC != getLLVMDefaultCC())
3123bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    RuntimeCC = abiCC;
3124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3126194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3127194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// aggregate.  If HAMembers is non-null, the number of base elements
3128194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// contained in the type is returned through it; this is used for the
3129194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// recursive calls that check aggregate component types.
3130194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilsonstatic bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3131194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   ASTContext &Context,
3132194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   uint64_t *HAMembers = 0) {
3133eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  uint64_t Members = 0;
3134194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3135194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3136194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
3137194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members *= AT->getSize().getZExtValue();
3138194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3139194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
3140eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->hasFlexibleArrayMember())
3141194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
3142eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
3143194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 0;
3144194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3145194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         i != e; ++i) {
3146581deb3da481053c4993c7600f97acf7768caac5David Blaikie      const FieldDecl *FD = *i;
3147194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      uint64_t FldMembers;
3148194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3149194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
3150eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
3151eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      Members = (RD->isUnion() ?
3152eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov                 std::max(Members, FldMembers) : Members + FldMembers);
3153194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
3154194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
3155194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 1;
3156194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3157194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Members = 2;
3158194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Ty = CT->getElementType();
3159194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
3160194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
3161194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3162194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // double, or 64-bit or 128-bit vectors.
3163194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3164194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (BT->getKind() != BuiltinType::Float &&
3165adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::Double &&
3166adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::LongDouble)
3167194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
3168194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3169194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      unsigned VecSize = Context.getTypeSize(VT);
3170194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (VecSize != 64 && VecSize != 128)
3171194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
3172194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else {
3173194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
3174194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
3175194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
3176194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // The base type must be the same for all members.  Vector types of the
3177194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // same total size are treated as being equivalent here.
3178194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *TyPtr = Ty.getTypePtr();
3179194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!Base)
3180194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Base = TyPtr;
3181194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (Base != TyPtr &&
3182194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        (!Base->isVectorType() || !TyPtr->isVectorType() ||
3183194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3184194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
3185194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
3186194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
3187194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  // Homogeneous Aggregates can have at most 4 members of the base type.
3188194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (HAMembers)
3189194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    *HAMembers = Members;
3190eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
3191eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  return (Members > 0 && Members <= 4);
3192194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson}
3193194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
3194710c517431954cfffba519fc7814cfbd8412a9aaManman Ren/// markAllocatedVFPs - update VFPRegs according to the alignment and
3195710c517431954cfffba519fc7814cfbd8412a9aaManman Ren/// number of VFP registers (unit is S register) requested.
3196710c517431954cfffba519fc7814cfbd8412a9aaManman Renstatic void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3197710c517431954cfffba519fc7814cfbd8412a9aaManman Ren                              unsigned Alignment,
3198710c517431954cfffba519fc7814cfbd8412a9aaManman Ren                              unsigned NumRequired) {
3199710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // Early Exit.
3200710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  if (AllocatedVFP >= 16)
3201710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    return;
3202710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3203710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // VFP registers of the appropriate type unallocated then the argument is
3204710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // allocated to the lowest-numbered sequence of such registers.
3205710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  for (unsigned I = 0; I < 16; I += Alignment) {
3206710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    bool FoundSlot = true;
3207710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3208710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      if (J >= 16 || VFPRegs[J]) {
3209710c517431954cfffba519fc7814cfbd8412a9aaManman Ren         FoundSlot = false;
3210710c517431954cfffba519fc7814cfbd8412a9aaManman Ren         break;
3211710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      }
3212710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    if (FoundSlot) {
3213710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3214710c517431954cfffba519fc7814cfbd8412a9aaManman Ren        VFPRegs[J] = 1;
3215710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      AllocatedVFP += NumRequired;
3216710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      return;
3217710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    }
3218710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  }
3219710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3220710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // unallocated are marked as unavailable.
3221710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  for (unsigned I = 0; I < 16; I++)
3222710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    VFPRegs[I] = 1;
3223710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  AllocatedVFP = 17; // We do not have enough VFP registers.
3224710c517431954cfffba519fc7814cfbd8412a9aaManman Ren}
3225710c517431954cfffba519fc7814cfbd8412a9aaManman Ren
3226710c517431954cfffba519fc7814cfbd8412a9aaManman RenABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3227710c517431954cfffba519fc7814cfbd8412a9aaManman Ren                                            unsigned &AllocatedVFP,
3228b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren                                            bool &IsHA) const {
3229b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // We update number of allocated VFPs according to
3230b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  // 6.1.2.1 The following argument types are VFP CPRCs:
3231b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   A single-precision floating-point type (including promoted
3232b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   half-precision types); A double-precision floating-point type;
3233b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3234b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   with a Base Type of a single- or double-precision floating-point type,
3235b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   64-bit containerized vectors or 128-bit containerized vectors with one
3236b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  //   to four Elements.
3237b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren
323897f81573636068fb9536436188caadf030584e58Manman Ren  // Handle illegal vector types here.
323997f81573636068fb9536436188caadf030584e58Manman Ren  if (isIllegalVectorType(Ty)) {
324097f81573636068fb9536436188caadf030584e58Manman Ren    uint64_t Size = getContext().getTypeSize(Ty);
324197f81573636068fb9536436188caadf030584e58Manman Ren    if (Size <= 32) {
324297f81573636068fb9536436188caadf030584e58Manman Ren      llvm::Type *ResType =
324397f81573636068fb9536436188caadf030584e58Manman Ren          llvm::Type::getInt32Ty(getVMContext());
324497f81573636068fb9536436188caadf030584e58Manman Ren      return ABIArgInfo::getDirect(ResType);
324597f81573636068fb9536436188caadf030584e58Manman Ren    }
324697f81573636068fb9536436188caadf030584e58Manman Ren    if (Size == 64) {
324797f81573636068fb9536436188caadf030584e58Manman Ren      llvm::Type *ResType = llvm::VectorType::get(
324897f81573636068fb9536436188caadf030584e58Manman Ren          llvm::Type::getInt32Ty(getVMContext()), 2);
3249710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
325097f81573636068fb9536436188caadf030584e58Manman Ren      return ABIArgInfo::getDirect(ResType);
325197f81573636068fb9536436188caadf030584e58Manman Ren    }
325297f81573636068fb9536436188caadf030584e58Manman Ren    if (Size == 128) {
325397f81573636068fb9536436188caadf030584e58Manman Ren      llvm::Type *ResType = llvm::VectorType::get(
325497f81573636068fb9536436188caadf030584e58Manman Ren          llvm::Type::getInt32Ty(getVMContext()), 4);
3255710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
325697f81573636068fb9536436188caadf030584e58Manman Ren      return ABIArgInfo::getDirect(ResType);
325797f81573636068fb9536436188caadf030584e58Manman Ren    }
325897f81573636068fb9536436188caadf030584e58Manman Ren    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
325997f81573636068fb9536436188caadf030584e58Manman Ren  }
3260710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // Update VFPRegs for legal vector types.
3261b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  if (const VectorType *VT = Ty->getAs<VectorType>()) {
3262b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    uint64_t Size = getContext().getTypeSize(VT);
3263b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // Size of a legal vector should be power of 2 and above 64.
3264710c517431954cfffba519fc7814cfbd8412a9aaManman Ren    markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
3265b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  }
3266710c517431954cfffba519fc7814cfbd8412a9aaManman Ren  // Update VFPRegs for floating point types.
3267b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3268b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    if (BT->getKind() == BuiltinType::Half ||
3269b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren        BT->getKind() == BuiltinType::Float)
3270710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
3271b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    if (BT->getKind() == BuiltinType::Double ||
3272710c517431954cfffba519fc7814cfbd8412a9aaManman Ren        BT->getKind() == BuiltinType::LongDouble)
3273710c517431954cfffba519fc7814cfbd8412a9aaManman Ren      markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
3274b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren  }
327597f81573636068fb9536436188caadf030584e58Manman Ren
3276d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
3277aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
3278aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3279aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
3280aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
3281cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
3282cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3283aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
328498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3285420255710694e958fa04bed1d80d96508949879eDaniel Dunbar  // Ignore empty records.
3286a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), Ty, true))
3287420255710694e958fa04bed1d80d96508949879eDaniel Dunbar    return ABIArgInfo::getIgnore();
3288420255710694e958fa04bed1d80d96508949879eDaniel Dunbar
3289ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3290ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
32910eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
3292194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
3293b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3294b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    // into VFP registers.
3295194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *Base = 0;
3296b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    uint64_t Members = 0;
3297b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren    if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
3298eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
3299b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      // Base can be a floating-point or a vector.
3300b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      if (Base->isVectorType()) {
3301b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren        // ElementSize is in number of floats.
3302b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren        unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
3303cb489dde66331865281e007b21f8f94da01f8d1eManman Ren        markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3304cb489dde66331865281e007b21f8f94da01f8d1eManman Ren                          Members * ElementSize);
3305b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
3306710c517431954cfffba519fc7814cfbd8412a9aaManman Ren        markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
3307b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      else {
3308b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren        assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3309b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren               Base->isSpecificBuiltinType(BuiltinType::LongDouble));
3310710c517431954cfffba519fc7814cfbd8412a9aaManman Ren        markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
3311b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      }
3312b3fa55f18d7b3759b3a6547719cf5b3418370a96Manman Ren      IsHA = true;
3313194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return ABIArgInfo::getExpand();
3314eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
3315194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
3316194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
3317634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren  // Support byval for ARM.
3318cb489dde66331865281e007b21f8f94da01f8d1eManman Ren  // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3319cb489dde66331865281e007b21f8f94da01f8d1eManman Ren  // most 8-byte. We realign the indirect argument if type alignment is bigger
3320cb489dde66331865281e007b21f8f94da01f8d1eManman Ren  // than ABI alignment.
3321fd1ba91e009ff7775744627f2855ffbfe713333eManman Ren  uint64_t ABIAlign = 4;
3322fd1ba91e009ff7775744627f2855ffbfe713333eManman Ren  uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3323fd1ba91e009ff7775744627f2855ffbfe713333eManman Ren  if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3324fd1ba91e009ff7775744627f2855ffbfe713333eManman Ren      getABIKind() == ARMABIInfo::AAPCS)
3325fd1ba91e009ff7775744627f2855ffbfe713333eManman Ren    ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3326885ad6928f8aca8e9f66eeece53e00364e14ea75Manman Ren  if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3327885ad6928f8aca8e9f66eeece53e00364e14ea75Manman Ren    return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
3328cb489dde66331865281e007b21f8f94da01f8d1eManman Ren           /*Realign=*/TyAlign > ABIAlign);
332979f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  }
333079f30981fcd25c6ff88807372a2744af02a7690eEli Friedman
33318aa87c71d9bfec14e135c683b0d7b9de999dbcb0Daniel Dunbar  // Otherwise, pass by coercing to a structure of the appropriate size.
33322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type* ElemTy;
3333c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  unsigned SizeRegs;
333479f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // FIXME: Try to match the types of the arguments more accurately where
333579f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // we can.
333679f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  if (getContext().getTypeAlign(Ty) <= 32) {
333753fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    ElemTy = llvm::Type::getInt32Ty(getVMContext());
333853fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
333978eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren  } else {
334078eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    ElemTy = llvm::Type::getInt64Ty(getVMContext());
334178eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
3342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
3343b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings
33449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *STy =
33457650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
3346b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings  return ABIArgInfo::getDirect(STy);
3347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3349a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattnerstatic bool isIntegerLikeType(QualType Ty, ASTContext &Context,
335098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                              llvm::LLVMContext &VMContext) {
335198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
335298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // is called integer-like if its size is less than or equal to one word, and
335398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // the offset of each of its addressable sub-fields is zero.
335498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
335598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
335698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
335798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that the type fits in a word.
335898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Size > 32)
335998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
336098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
336198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // FIXME: Handle vector types!
336298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Ty->isVectorType())
336398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
336498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3365b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  // Float types are never treated as "integer like".
3366b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  if (Ty->isRealFloatingType())
3367b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar    return false;
3368b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar
336998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // If this is a builtin or pointer type then it is ok.
3370183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
337198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return true;
337298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
33734581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  // Small complex integer types are "integer like".
33744581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  if (const ComplexType *CT = Ty->getAs<ComplexType>())
33754581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
337698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
337798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Single element and zero sized arrays should be allowed, by the definition
337898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // above, but they are not.
337998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
338098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise, it must be a record type.
338198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
338298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (!RT) return false;
338398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
338498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Ignore records with flexible arrays.
338598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
338698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RD->hasFlexibleArrayMember())
338798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
338898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
338998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that all sub-fields are at offset 0, and are themselves "integer
339098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // like".
339198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
339298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
339398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  bool HadField = false;
339498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  unsigned idx = 0;
339598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
339698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar       i != e; ++i, ++idx) {
3397581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
339898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3399679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Bit-fields are not addressable, we only need to verify they are "integer
3400679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // like". We still have to disallow a subsequent non-bitfield, for example:
3401679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    //   struct { int : 0; int x }
3402679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // is non-integer like according to gcc.
3403679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (FD->isBitField()) {
3404679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!RD->isUnion())
3405679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        HadField = true;
3406679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
3407679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3408679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        return false;
340998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3410679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      continue;
341198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
341298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3413679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Check if this field is at offset 0.
3414679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (Layout.getFieldOffset(idx) != 0)
3415679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      return false;
3416679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
341798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
341898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return false;
34198bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
3420679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Only allow at most one field in a structure. This doesn't match the
3421679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // wording above, but follows gcc in situations with a field following an
3422679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // empty structure.
342398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!RD->isUnion()) {
342498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (HadField)
342598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar        return false;
342698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
342798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      HadField = true;
342898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
342998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  }
343098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
343198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return true;
343298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar}
343398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3434a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
343598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RetTy->isVoidType())
3436c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
343798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3438f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  // Large vector types should be returned via memory.
3439f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3440f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar    return ABIArgInfo::getIndirect(0);
3441f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar
3442d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(RetTy)) {
3443aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
3444aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3445aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      RetTy = EnumTy->getDecl()->getIntegerType();
3446aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
3447cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (RetTy->isPromotableIntegerType() ?
3448cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3449aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
345098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
34510eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
34520eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
3453ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (isRecordReturnIndirect(RetTy, CGT))
34540eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
34550eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
345698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Are we following APCS?
345798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (getABIKind() == APCS) {
3458a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isEmptyRecord(getContext(), RetTy, false))
345998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return ABIArgInfo::getIgnore();
346098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
34614cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // Complex types are all returned as packed integers.
34624cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    //
34634cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // FIXME: Consider using 2 x vector types if the back end handles them
34644cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // correctly.
34654cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    if (RetTy->isAnyComplexType())
3466800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3467a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                              getContext().getTypeSize(RetTy)));
34684cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar
346998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Integer like structures are returned in r0.
3470a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
347198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      // Return in the smallest viable integer type.
3472a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
347398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 8)
3474800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
347598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 16)
3476800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3477800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
347898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
347998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
348098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Otherwise return in memory.
348198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return ABIArgInfo::getIndirect(0);
3482c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
348398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
348498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise this is an AAPCS variant.
348598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3486a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), RetTy, true))
348716a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    return ABIArgInfo::getIgnore();
348816a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
34893b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  // Check for homogeneous aggregates with AAPCS-VFP.
34903b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  if (getABIKind() == AAPCS_VFP) {
34913b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson    const Type *Base = 0;
3492eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3493eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
34943b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      // Homogeneous Aggregates are returned directly.
34953b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      return ABIArgInfo::getDirect();
3496eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
34973b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  }
34983b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson
349998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Aggregates <= 4 bytes are returned in r0; other aggregates
350098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // are returned indirectly.
3501a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  uint64_t Size = getContext().getTypeSize(RetTy);
350216a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  if (Size <= 32) {
350316a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    // Return in the smallest viable integer type.
350416a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 8)
3505800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
350616a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 16)
3507800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3508800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
350916a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  }
351016a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
351198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return ABIArgInfo::getIndirect(0);
3512c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3513c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
351497f81573636068fb9536436188caadf030584e58Manman Ren/// isIllegalVector - check whether Ty is an illegal vector type.
351597f81573636068fb9536436188caadf030584e58Manman Renbool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
351697f81573636068fb9536436188caadf030584e58Manman Ren  if (const VectorType *VT = Ty->getAs<VectorType>()) {
351797f81573636068fb9536436188caadf030584e58Manman Ren    // Check whether VT is legal.
351897f81573636068fb9536436188caadf030584e58Manman Ren    unsigned NumElements = VT->getNumElements();
351997f81573636068fb9536436188caadf030584e58Manman Ren    uint64_t Size = getContext().getTypeSize(VT);
352097f81573636068fb9536436188caadf030584e58Manman Ren    // NumElements should be power of 2.
352197f81573636068fb9536436188caadf030584e58Manman Ren    if ((NumElements & (NumElements - 1)) != 0)
352297f81573636068fb9536436188caadf030584e58Manman Ren      return true;
352397f81573636068fb9536436188caadf030584e58Manman Ren    // Size should be greater than 32 bits.
352497f81573636068fb9536436188caadf030584e58Manman Ren    return Size <= 32;
352597f81573636068fb9536436188caadf030584e58Manman Ren  }
352697f81573636068fb9536436188caadf030584e58Manman Ren  return false;
352797f81573636068fb9536436188caadf030584e58Manman Ren}
352897f81573636068fb9536436188caadf030584e58Manman Ren
3529c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
353077b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                   CodeGenFunction &CGF) const {
35318b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
35328b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3534c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
35358b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3536c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3537d105e73368e677e65af724947be85ec87a0fa45eManman Ren
3538d105e73368e677e65af724947be85ec87a0fa45eManman Ren  uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
3539e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
354097f81573636068fb9536436188caadf030584e58Manman Ren  bool IsIndirect = false;
3541d105e73368e677e65af724947be85ec87a0fa45eManman Ren
3542d105e73368e677e65af724947be85ec87a0fa45eManman Ren  // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3543d105e73368e677e65af724947be85ec87a0fa45eManman Ren  // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
3544933710242edc66da21e865948d4c8e3a6badf2dfManman Ren  if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3545933710242edc66da21e865948d4c8e3a6badf2dfManman Ren      getABIKind() == ARMABIInfo::AAPCS)
3546933710242edc66da21e865948d4c8e3a6badf2dfManman Ren    TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3547933710242edc66da21e865948d4c8e3a6badf2dfManman Ren  else
3548933710242edc66da21e865948d4c8e3a6badf2dfManman Ren    TyAlign = 4;
354997f81573636068fb9536436188caadf030584e58Manman Ren  // Use indirect if size of the illegal vector is bigger than 16 bytes.
355097f81573636068fb9536436188caadf030584e58Manman Ren  if (isIllegalVectorType(Ty) && Size > 16) {
355197f81573636068fb9536436188caadf030584e58Manman Ren    IsIndirect = true;
355297f81573636068fb9536436188caadf030584e58Manman Ren    Size = 4;
355397f81573636068fb9536436188caadf030584e58Manman Ren    TyAlign = 4;
355497f81573636068fb9536436188caadf030584e58Manman Ren  }
3555d105e73368e677e65af724947be85ec87a0fa45eManman Ren
3556d105e73368e677e65af724947be85ec87a0fa45eManman Ren  // Handle address alignment for ABI alignment > 4 bytes.
3557e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  if (TyAlign > 4) {
3558e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    assert((TyAlign & (TyAlign - 1)) == 0 &&
3559e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola           "Alignment is not power of 2!");
3560e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3561e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3562e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
3563d105e73368e677e65af724947be85ec87a0fa45eManman Ren    Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
3564e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  }
3565c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3566c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
3567d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::RoundUpToAlignment(Size, 4);
3568c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
356977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3570c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
3571c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3572c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
357397f81573636068fb9536436188caadf030584e58Manman Ren  if (IsIndirect)
357497f81573636068fb9536436188caadf030584e58Manman Ren    Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
3575933710242edc66da21e865948d4c8e3a6badf2dfManman Ren  else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
3576d105e73368e677e65af724947be85ec87a0fa45eManman Ren    // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3577d105e73368e677e65af724947be85ec87a0fa45eManman Ren    // may not be correctly aligned for the vector type. We create an aligned
3578d105e73368e677e65af724947be85ec87a0fa45eManman Ren    // temporary space and copy the content over from ap.cur to the temporary
3579d105e73368e677e65af724947be85ec87a0fa45eManman Ren    // space. This is necessary if the natural alignment of the type is greater
3580d105e73368e677e65af724947be85ec87a0fa45eManman Ren    // than the ABI alignment.
3581d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3582d105e73368e677e65af724947be85ec87a0fa45eManman Ren    CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3583d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3584d105e73368e677e65af724947be85ec87a0fa45eManman Ren                                                    "var.align");
3585d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3586d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3587d105e73368e677e65af724947be85ec87a0fa45eManman Ren    Builder.CreateMemCpy(Dst, Src,
3588d105e73368e677e65af724947be85ec87a0fa45eManman Ren        llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3589d105e73368e677e65af724947be85ec87a0fa45eManman Ren        TyAlign, false);
3590d105e73368e677e65af724947be85ec87a0fa45eManman Ren    Addr = AlignedTemp; //The content is in aligned location.
3591d105e73368e677e65af724947be85ec87a0fa45eManman Ren  }
3592d105e73368e677e65af724947be85ec87a0fa45eManman Ren  llvm::Type *PTy =
3593d105e73368e677e65af724947be85ec87a0fa45eManman Ren    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3594d105e73368e677e65af724947be85ec87a0fa45eManman Ren  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3595d105e73368e677e65af724947be85ec87a0fa45eManman Ren
3596c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
3597c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3598c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3599c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramernamespace {
3600c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer
3601263366f9241366f29ba65b703120f302490c39ffDerek Schuffclass NaClARMABIInfo : public ABIInfo {
3602263366f9241366f29ba65b703120f302490c39ffDerek Schuff public:
3603263366f9241366f29ba65b703120f302490c39ffDerek Schuff  NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3604263366f9241366f29ba65b703120f302490c39ffDerek Schuff      : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3605263366f9241366f29ba65b703120f302490c39ffDerek Schuff  virtual void computeInfo(CGFunctionInfo &FI) const;
3606263366f9241366f29ba65b703120f302490c39ffDerek Schuff  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3607263366f9241366f29ba65b703120f302490c39ffDerek Schuff                                 CodeGenFunction &CGF) const;
3608263366f9241366f29ba65b703120f302490c39ffDerek Schuff private:
3609263366f9241366f29ba65b703120f302490c39ffDerek Schuff  PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3610263366f9241366f29ba65b703120f302490c39ffDerek Schuff  ARMABIInfo NInfo; // Used for everything else.
3611263366f9241366f29ba65b703120f302490c39ffDerek Schuff};
3612263366f9241366f29ba65b703120f302490c39ffDerek Schuff
3613263366f9241366f29ba65b703120f302490c39ffDerek Schuffclass NaClARMTargetCodeGenInfo : public TargetCodeGenInfo  {
3614263366f9241366f29ba65b703120f302490c39ffDerek Schuff public:
3615263366f9241366f29ba65b703120f302490c39ffDerek Schuff  NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3616263366f9241366f29ba65b703120f302490c39ffDerek Schuff      : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3617263366f9241366f29ba65b703120f302490c39ffDerek Schuff};
3618263366f9241366f29ba65b703120f302490c39ffDerek Schuff
3619c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer}
3620c6f84cf73e0bc04faacd1a9b7845e014e7fac21eBenjamin Kramer
3621263366f9241366f29ba65b703120f302490c39ffDerek Schuffvoid NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3622263366f9241366f29ba65b703120f302490c39ffDerek Schuff  if (FI.getASTCallingConvention() == CC_PnaclCall)
3623263366f9241366f29ba65b703120f302490c39ffDerek Schuff    PInfo.computeInfo(FI);
3624263366f9241366f29ba65b703120f302490c39ffDerek Schuff  else
3625263366f9241366f29ba65b703120f302490c39ffDerek Schuff    static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3626263366f9241366f29ba65b703120f302490c39ffDerek Schuff}
3627263366f9241366f29ba65b703120f302490c39ffDerek Schuff
3628263366f9241366f29ba65b703120f302490c39ffDerek Schuffllvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3629263366f9241366f29ba65b703120f302490c39ffDerek Schuff                                       CodeGenFunction &CGF) const {
3630263366f9241366f29ba65b703120f302490c39ffDerek Schuff  // Always use the native convention; calling pnacl-style varargs functions
3631263366f9241366f29ba65b703120f302490c39ffDerek Schuff  // is unsupported.
3632263366f9241366f29ba65b703120f302490c39ffDerek Schuff  return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3633263366f9241366f29ba65b703120f302490c39ffDerek Schuff}
3634263366f9241366f29ba65b703120f302490c39ffDerek Schuff
3635dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3636c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover// AArch64 ABI Implementation
3637c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover//===----------------------------------------------------------------------===//
3638c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3639c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northovernamespace {
3640c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3641c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverclass AArch64ABIInfo : public ABIInfo {
3642c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverpublic:
3643c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3644c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3645c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverprivate:
3646c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // The AArch64 PCS is explicit about return types and argument types being
3647c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // handled identically, so we don't need to draw a distinction between
3648c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Argument and Return classification.
3649c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3650c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 int &FreeVFPRegs) const;
3651c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3652c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3653c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                        llvm::Type *DirectTy = 0) const;
3654c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3655c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  virtual void computeInfo(CGFunctionInfo &FI) const;
3656c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3657c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3658c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 CodeGenFunction &CGF) const;
3659c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover};
3660c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3661c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverclass AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3662c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverpublic:
3663c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3664c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3665c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3666c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const AArch64ABIInfo &getABIInfo() const {
3667c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3668c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3669c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3670c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3671c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return 31;
3672c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3673c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3674c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3675c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               llvm::Value *Address) const {
3676c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // 0-31 are x0-x30 and sp: 8 bytes each
3677c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3678c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3679c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3680c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // 64-95 are v0-v31: 16 bytes each
3681c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3682c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3683c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3684c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return false;
3685c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3686c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3687c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover};
3688c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3689c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
3690c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3691c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northovervoid AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3692c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  int FreeIntRegs = 8, FreeVFPRegs = 8;
3693c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3694c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3695c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                           FreeIntRegs, FreeVFPRegs);
3696c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3697c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FreeIntRegs = FreeVFPRegs = 8;
3698c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3699c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover       it != ie; ++it) {
3700c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3701c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3702c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3703c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
3704c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3705c264e16a42b3f6c36521857a29ea0949d9781c22Tim NorthoverABIArgInfo
3706c264e16a42b3f6c36521857a29ea0949d9781c22Tim NorthoverAArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3707c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                           bool IsInt, llvm::Type *DirectTy) const {
3708c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (FreeRegs >= RegsNeeded) {
3709c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    FreeRegs -= RegsNeeded;
3710c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return ABIArgInfo::getDirect(DirectTy);
3711c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3712c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3713c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Type *Padding = 0;
3714c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3715c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // We need padding so that later arguments don't get filled in anyway. That
3716c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // wouldn't happen if only ByVal arguments followed in the same category, but
3717c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // a large structure will simply seem to be a pointer as far as LLVM is
3718c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // concerned.
3719c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (FreeRegs > 0) {
3720c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (IsInt)
3721c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      Padding = llvm::Type::getInt64Ty(getVMContext());
3722c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    else
3723c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      Padding = llvm::Type::getFloatTy(getVMContext());
3724c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3725c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Either [N x i64] or [N x float].
3726c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    Padding = llvm::ArrayType::get(Padding, FreeRegs);
3727c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    FreeRegs = 0;
3728c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3729c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3730c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3731c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 /*IsByVal=*/ true, /*Realign=*/ false,
3732c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 Padding);
3733c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
3734c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3735c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3736c264e16a42b3f6c36521857a29ea0949d9781c22Tim NorthoverABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3737c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                               int &FreeIntRegs,
3738c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                               int &FreeVFPRegs) const {
3739c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Can only occurs for return, but harmless otherwise.
3740c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (Ty->isVoidType())
3741c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return ABIArgInfo::getIgnore();
3742c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3743c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Large vector types should be returned via memory. There's no such concept
3744c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3745c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // classified they'd go into memory (see B.3).
3746c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3747c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (FreeIntRegs > 0)
3748c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      --FreeIntRegs;
3749c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3750c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3751c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3752c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // All non-aggregate LLVM types have a concrete ABI representation so they can
3753c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // be passed directly. After this block we're guaranteed to be in a
3754c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // complicated case.
3755c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (!isAggregateTypeForABI(Ty)) {
3756c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Treat an enum type as its underlying type.
3757c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3758c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      Ty = EnumTy->getDecl()->getIntegerType();
3759c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3760c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (Ty->isFloatingType() || Ty->isVectorType())
3761c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3762c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3763c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    assert(getContext().getTypeSize(Ty) <= 128 &&
3764c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover           "unexpectedly large scalar type");
3765c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3766c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3767c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3768c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // If the type may need padding registers to ensure "alignment", we must be
3769c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // careful when this is accounted for. Increasing the effective size covers
3770c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // all cases.
3771c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (getContext().getTypeAlign(Ty) == 128)
3772c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      RegsNeeded += FreeIntRegs % 2 != 0;
3773c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3774c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3775c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3776c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3777ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3778ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
3779c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      --FreeIntRegs;
3780ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3781c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3782c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3783c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (isEmptyRecord(getContext(), Ty, true)) {
3784c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (!getContext().getLangOpts().CPlusPlus) {
3785c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // Empty structs outside C++ mode are a GNU extension, so no ABI can
3786c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // possibly tell us what to do. It turns out (I believe) that GCC ignores
3787c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // the object for parameter-passsing purposes.
3788c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      return ABIArgInfo::getIgnore();
3789c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    }
3790c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3791c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3792c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // description of va_arg in the PCS require that an empty struct does
3793c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // actually occupy space for parameter-passing. I'm hoping for a
3794c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // clarification giving an explicit paragraph to point to in future.
3795c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3796c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                      llvm::Type::getInt8Ty(getVMContext()));
3797c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3798c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3799c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Homogeneous vector aggregates get passed in registers or on the stack.
3800c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const Type *Base = 0;
3801c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  uint64_t NumMembers = 0;
3802c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3803c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    assert(Base && "Base class should be set for homogeneous aggregate");
3804c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Homogeneous aggregates are passed and returned directly.
3805c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3806c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                      /*IsInt=*/ false);
3807c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3808c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3809c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  uint64_t Size = getContext().getTypeSize(Ty);
3810c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (Size <= 128) {
3811c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Small structs can use the same direct type whether they're in registers
3812c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // or on the stack.
3813c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Type *BaseTy;
3814c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    unsigned NumBases;
3815c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    int SizeInRegs = (Size + 63) / 64;
3816c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3817c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    if (getContext().getTypeAlign(Ty) == 128) {
3818c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3819c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      NumBases = 1;
3820c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3821c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // If the type may need padding registers to ensure "alignment", we must
3822c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // be careful when this is accounted for. Increasing the effective size
3823c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      // covers all cases.
3824c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      SizeInRegs += FreeIntRegs % 2 != 0;
3825c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    } else {
3826c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      BaseTy = llvm::Type::getInt64Ty(getVMContext());
3827c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      NumBases = SizeInRegs;
3828c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    }
3829c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3830c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3831c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3832c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                      /*IsInt=*/ true, DirectTy);
3833c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3834c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3835c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3836c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3837c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  --FreeIntRegs;
3838c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3839c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
3840c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3841c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverllvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3842c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                       CodeGenFunction &CGF) const {
3843c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // The AArch64 va_list type and handling is specified in the Procedure Call
3844c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Standard, section B.4:
3845c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //
3846c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // struct {
3847c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   void *__stack;
3848c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   void *__gr_top;
3849c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   void *__vr_top;
3850c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   int __gr_offs;
3851c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //   int __vr_offs;
3852c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // };
3853c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3854c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  assert(!CGF.CGM.getDataLayout().isBigEndian()
3855c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover         && "va_arg not implemented for big-endian AArch64");
3856c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3857c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  int FreeIntRegs = 8, FreeVFPRegs = 8;
3858c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  Ty = CGF.getContext().getCanonicalType(Ty);
3859c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3860c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3861c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3862c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3863c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3864c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3865c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3866c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3867c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  int reg_top_index;
3868c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  int RegSize;
3869c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (FreeIntRegs < 8) {
3870c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3871c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // 3 is the field number of __gr_offs
3872c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3873c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3874c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_top_index = 1; // field number for __gr_top
3875c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    RegSize = 8 * (8 - FreeIntRegs);
3876c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  } else {
3877c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3878c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // 4 is the field number of __vr_offs.
3879c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3880c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3881c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_top_index = 2; // field number for __vr_top
3882c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    RegSize = 16 * (8 - FreeVFPRegs);
3883c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3884c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3885c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3886c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Find out where argument was passed
3887c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3888c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3889c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // If reg_offs >= 0 we're already using the stack for this type of
3890c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // argument. We don't want to keep updating reg_offs (in case it overflows,
3891c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3892c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // whatever they get).
3893c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *UsingStack = 0;
3894c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3895c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         llvm::ConstantInt::get(CGF.Int32Ty, 0));
3896c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3897c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3898c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3899c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Otherwise, at least some kind of argument could go in these registers, the
3900c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // quesiton is whether this particular type is too big.
3901c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBlock(MaybeRegBlock);
3902c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3903c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Integer arguments may need to correct register alignment (for example a
3904c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3905c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // align __gr_offs to calculate the potential address.
3906c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3907c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    int Align = getContext().getTypeAlign(Ty) / 8;
3908c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3909c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs = CGF.Builder.CreateAdd(reg_offs,
3910c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3911c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 "align_regoffs");
3912c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    reg_offs = CGF.Builder.CreateAnd(reg_offs,
3913c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3914c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    "aligned_regoffs");
3915c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3916c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3917c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3918c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *NewOffset = 0;
3919c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  NewOffset = CGF.Builder.CreateAdd(reg_offs,
3920c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3921c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    "new_reg_offs");
3922c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3923c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3924c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Now we're in a position to decide whether this argument really was in
3925c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // registers or not.
3926c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *InRegs = 0;
3927c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3928c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                     llvm::ConstantInt::get(CGF.Int32Ty, 0),
3929c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                     "inreg");
3930c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3931c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3932c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3933c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3934c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Argument was in registers
3935c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3936c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3937c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Now we emit the code for if the argument was originally passed in
3938c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // registers. First start the appropriate block:
3939c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBlock(InRegBlock);
3940c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3941c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *reg_top_p = 0, *reg_top = 0;
3942c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
3943c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
3944c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
3945c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *RegAddr = 0;
3946c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
3947c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3948c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (!AI.isDirect()) {
3949c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // If it's been passed indirectly (actually a struct), whatever we find from
3950c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // stored registers or on the stack will actually be a struct **.
3951c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    MemTy = llvm::PointerType::getUnqual(MemTy);
3952c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3953c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3954c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const Type *Base = 0;
3955c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  uint64_t NumMembers;
3956c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
3957c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      && NumMembers > 1) {
3958c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Homogeneous aggregates passed in registers will have their elements split
3959c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // and stored 16-bytes apart regardless of size (they're notionally in qN,
3960c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // qN+1, ...). We reload and store into a temporary local variable
3961c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // contiguously.
3962c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
3963c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
3964c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
3965c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
3966c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3967c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    for (unsigned i = 0; i < NumMembers; ++i) {
3968c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
3969c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
3970c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
3971c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                           llvm::PointerType::getUnqual(BaseTy));
3972c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
3973c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3974c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
3975c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover      CGF.Builder.CreateStore(Elem, StoreAddr);
3976c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    }
3977c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3978c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
3979c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  } else {
3980c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Otherwise the object is contiguous in memory
3981c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
3982c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
3983c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3984c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBranch(ContBlock);
3985c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3986c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3987c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Argument was on the stack
3988c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
3989c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBlock(OnStackBlock);
3990c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3991c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *stack_p = 0, *OnStackAddr = 0;
3992c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
3993c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
3994c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
3995c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Again, stack arguments may need realigmnent. In this case both integer and
3996c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // floating-point ones might be affected.
3997c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3998c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    int Align = getContext().getTypeAlign(Ty) / 8;
3999c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4000c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4001c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4002c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4003c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4004c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                 "align_stack");
4005c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4006c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4007c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                    "align_stack");
4008c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4009c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4010c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
4011c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4012c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  uint64_t StackSize;
4013c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (AI.isDirect())
4014c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    StackSize = getContext().getTypeSize(Ty) / 8;
4015c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  else
4016c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    StackSize = 8;
4017c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4018c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // All stack slots are 8 bytes
4019c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4020c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4021c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4022c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4023c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                                "new_stack");
4024c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4025c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Write the new value of __stack for the next call to va_arg
4026c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.Builder.CreateStore(NewStack, stack_p);
4027c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4028c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4029c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4030c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBranch(ContBlock);
4031c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4032c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
4033c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Tidy up
4034c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  //=======================================
4035c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  CGF.EmitBlock(ContBlock);
4036c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4037c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4038c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  ResAddr->addIncoming(RegAddr, InRegBlock);
4039c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4040c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4041c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (AI.isDirect())
4042c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return ResAddr;
4043c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4044c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4045c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
4046c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
4047c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover//===----------------------------------------------------------------------===//
40482c585b991596859f39860b6094247ba027a03530Justin Holewinski// NVPTX ABI Implementation
40490259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
40500259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40510259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskinamespace {
40520259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40532c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXABIInfo : public ABIInfo {
40540259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
4055dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
40560259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40570259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyReturnType(QualType RetTy) const;
40580259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyArgumentType(QualType Ty) const;
40590259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40600259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual void computeInfo(CGFunctionInfo &FI) const;
40610259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
40620259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski                                 CodeGenFunction &CFG) const;
40630259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
40640259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40652c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
40660259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
40672c585b991596859f39860b6094247ba027a03530Justin Holewinski  NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
40682c585b991596859f39860b6094247ba027a03530Justin Holewinski    : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
4069818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
40702f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
40712f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
4072dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinskiprivate:
4073dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  static void addKernelMetadata(llvm::Function *F);
40740259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
40750259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40762c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
40770259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (RetTy->isVoidType())
40780259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIgnore();
40790259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(RetTy))
40800259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
40810259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
40820259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
40830259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40842c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
40850259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(Ty))
40860259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
40870259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40880259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
40890259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
40900259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40912c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
40920259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
40930259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
40940259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski       it != ie; ++it)
40950259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    it->info = classifyArgumentType(it->type);
40960259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
40970259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Always honor user-specified calling convention.
40980259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (FI.getCallingConvention() != llvm::CallingConv::C)
40990259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return;
41000259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
4101bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  FI.setEffectiveCallingConvention(getRuntimeCC());
4102bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
4103bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
41042c585b991596859f39860b6094247ba027a03530Justin Holewinskillvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
41052c585b991596859f39860b6094247ba027a03530Justin Holewinski                                     CodeGenFunction &CFG) const {
41062c585b991596859f39860b6094247ba027a03530Justin Holewinski  llvm_unreachable("NVPTX does not support varargs");
41070259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
41080259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
41092c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXTargetCodeGenInfo::
41102c585b991596859f39860b6094247ba027a03530Justin HolewinskiSetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
41112c585b991596859f39860b6094247ba027a03530Justin Holewinski                    CodeGen::CodeGenModule &M) const{
4112818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4113818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  if (!FD) return;
4114818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
4115818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  llvm::Function *F = cast<llvm::Function>(GV);
4116818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
4117818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  // Perform special handling in OpenCL mode
41184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
4119dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski    // Use OpenCL function attributes to check for kernel functions
4120818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // By default, all functions are device functions
4121818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (FD->hasAttr<OpenCLKernelAttr>()) {
4122dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski      // OpenCL __kernel functions get kernel metadata
4123dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski      addKernelMetadata(F);
4124818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // And kernel functions are not subject to inlining
412572390b39c545426023ec104afe8706395d732badBill Wendling      F->addFnAttr(llvm::Attribute::NoInline);
4126818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    }
4127744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  }
4128818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
4129744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  // Perform special handling in CUDA mode.
41304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().CUDA) {
4131dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski    // CUDA __global__ functions get a kernel metadata entry.  Since
4132744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // __global__ functions cannot be called from the device, we do not
4133744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // need to set the noinline attribute.
4134744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    if (FD->getAttr<CUDAGlobalAttr>())
4135dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski      addKernelMetadata(F);
4136818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
4137818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski}
4138818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
4139dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinskivoid NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4140dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  llvm::Module *M = F->getParent();
4141dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  llvm::LLVMContext &Ctx = M->getContext();
4142dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski
4143dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  // Get "nvvm.annotations" metadata node
4144dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4145dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski
4146dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  // Create !{<func-ref>, metadata !"kernel", i32 1} node
4147dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  llvm::SmallVector<llvm::Value *, 3> MDVals;
4148dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  MDVals.push_back(F);
4149dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4150dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4151dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski
4152dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  // Append metadata to nvvm.annotations
4153dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski  MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4154dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski}
4155dca8f336e6da2b50eb965535d81d603e39294f9cJustin Holewinski
41560259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
41570259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
41580259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
4159b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand// SystemZ ABI Implementation
4160b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand//===----------------------------------------------------------------------===//
4161b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4162b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandnamespace {
4163b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4164b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandclass SystemZABIInfo : public ABIInfo {
4165b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandpublic:
4166b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4167b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4168b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  bool isPromotableIntegerType(QualType Ty) const;
4169b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  bool isCompoundType(QualType Ty) const;
4170b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  bool isFPArgumentType(QualType Ty) const;
4171b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4172b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  ABIArgInfo classifyReturnType(QualType RetTy) const;
4173b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4174b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4175b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  virtual void computeInfo(CGFunctionInfo &FI) const {
4176b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4177b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4178b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand         it != ie; ++it)
4179b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      it->info = classifyArgumentType(it->type);
4180b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  }
4181b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4182b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4183b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                 CodeGenFunction &CGF) const;
4184b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand};
4185b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4186b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandclass SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4187b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandpublic:
4188b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4189b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4190b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand};
4191b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4192b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4193b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4194b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandbool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4195b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Treat an enum type as its underlying type.
4196b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4197b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    Ty = EnumTy->getDecl()->getIntegerType();
4198b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4199b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Promotable integer types are required to be promoted by the ABI.
4200b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (Ty->isPromotableIntegerType())
4201b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return true;
4202b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4203b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // 32-bit values must also be promoted.
4204b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4205b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    switch (BT->getKind()) {
4206b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    case BuiltinType::Int:
4207b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    case BuiltinType::UInt:
4208b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      return true;
4209b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    default:
4210b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      return false;
4211b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    }
4212b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return false;
4213b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4214b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4215b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandbool SystemZABIInfo::isCompoundType(QualType Ty) const {
4216b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4217b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4218b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4219b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandbool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4220b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4221b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    switch (BT->getKind()) {
4222b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    case BuiltinType::Float:
4223b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    case BuiltinType::Double:
4224b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      return true;
4225b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    default:
4226b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      return false;
4227b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    }
4228b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4229b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (const RecordType *RT = Ty->getAsStructureType()) {
4230b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    const RecordDecl *RD = RT->getDecl();
4231b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    bool Found = false;
4232b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4233b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // If this is a C++ record, check the bases first.
4234b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4235b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4236b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand             E = CXXRD->bases_end(); I != E; ++I) {
4237b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        QualType Base = I->getType();
4238b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4239b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        // Empty bases don't affect things either way.
4240b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        if (isEmptyRecord(getContext(), Base, true))
4241b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand          continue;
4242b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4243b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        if (Found)
4244b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand          return false;
4245b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        Found = isFPArgumentType(Base);
4246b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        if (!Found)
4247b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand          return false;
4248b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      }
4249b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4250b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // Check the fields.
4251b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    for (RecordDecl::field_iterator I = RD->field_begin(),
4252b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand           E = RD->field_end(); I != E; ++I) {
4253b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      const FieldDecl *FD = *I;
4254b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4255b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      // Empty bitfields don't affect things either way.
4256b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      // Unlike isSingleElementStruct(), empty structure and array fields
4257b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      // do count.  So do anonymous bitfields that aren't zero-sized.
4258b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4259b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        return true;
4260b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4261b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      // Unlike isSingleElementStruct(), arrays do not count.
4262b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      // Nested isFPArgumentType structures still do though.
4263b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      if (Found)
4264b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        return false;
4265b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      Found = isFPArgumentType(FD->getType());
4266b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      if (!Found)
4267b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        return false;
4268b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    }
4269b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4270b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // Unlike isSingleElementStruct(), trailing padding is allowed.
4271b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // An 8-byte aligned struct s { float f; } is passed as a double.
4272b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return Found;
4273b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  }
4274b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4275b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return false;
4276b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4277b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4278b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandllvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4279b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                       CodeGenFunction &CGF) const {
4280b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Assume that va_list type is correct; should be pointer to LLVM type:
4281b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // struct {
4282b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   i64 __gpr;
4283b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   i64 __fpr;
4284b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   i8 *__overflow_arg_area;
4285b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   i8 *__reg_save_area;
4286b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // };
4287b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4288b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Every argument occupies 8 bytes and is passed by preference in either
4289b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // GPRs or FPRs.
4290b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  Ty = CGF.getContext().getCanonicalType(Ty);
4291b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  ABIArgInfo AI = classifyArgumentType(Ty);
4292b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  bool InFPRs = isFPArgumentType(Ty);
4293b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4294b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4295b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  bool IsIndirect = AI.isIndirect();
4296b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  unsigned UnpaddedBitSize;
4297b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (IsIndirect) {
4298b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    APTy = llvm::PointerType::getUnqual(APTy);
4299b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    UnpaddedBitSize = 64;
4300b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  } else
4301b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    UnpaddedBitSize = getContext().getTypeSize(Ty);
4302b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  unsigned PaddedBitSize = 64;
4303b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4304b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4305b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  unsigned PaddedSize = PaddedBitSize / 8;
4306b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4307b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4308b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4309b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (InFPRs) {
4310b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    MaxRegs = 4; // Maximum of 4 FPR arguments
4311b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegCountField = 1; // __fpr
4312b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegSaveIndex = 16; // save offset for f0
4313b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegPadding = 0; // floats are passed in the high bits of an FPR
4314b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  } else {
4315b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    MaxRegs = 5; // Maximum of 5 GPR arguments
4316b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegCountField = 0; // __gpr
4317b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegSaveIndex = 2; // save offset for r2
4318b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    RegPadding = Padding; // values are passed in the low bits of a GPR
4319b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  }
4320b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4321b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegCountPtr =
4322b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4323b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4324b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Type *IndexTy = RegCount->getType();
4325b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4326b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4327b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand						  "fits_in_regs");
4328b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4329b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4330b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4331b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4332b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4333b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4334b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Emit code to load the value if it was passed in registers.
4335b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.EmitBlock(InRegBlock);
4336b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4337b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Work out the address of an argument register.
4338b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4339b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *ScaledRegCount =
4340b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4341b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegBase =
4342b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4343b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegOffset =
4344b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4345b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegSaveAreaPtr =
4346b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4347b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegSaveArea =
4348b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4349b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RawRegAddr =
4350b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4351b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RegAddr =
4352b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4353b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4354b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Update the register count
4355b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4356b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *NewRegCount =
4357b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4358b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4359b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.EmitBranch(ContBlock);
4360b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4361b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Emit code to load the value if it was passed in memory.
4362b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.EmitBlock(InMemBlock);
4363b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4364b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Work out the address of a stack argument.
4365b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *OverflowArgAreaPtr =
4366b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4367b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *OverflowArgArea =
4368b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4369b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4370b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *RawMemAddr =
4371b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4372b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *MemAddr =
4373b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4374b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4375b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Update overflow_arg_area_ptr pointer
4376b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::Value *NewOverflowArgArea =
4377b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4378b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4379b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.EmitBranch(ContBlock);
4380b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4381b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Return the appropriate result.
4382b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  CGF.EmitBlock(ContBlock);
4383b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4384b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  ResAddr->addIncoming(RegAddr, InRegBlock);
4385b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  ResAddr->addIncoming(MemAddr, InMemBlock);
4386b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4387b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (IsIndirect)
4388b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4389b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4390b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return ResAddr;
4391b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4392b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4393b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4394b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich WeigandABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4395b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (RetTy->isVoidType())
4396b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getIgnore();
4397b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4398b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getIndirect(0);
4399b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return (isPromotableIntegerType(RetTy) ?
4400b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4401b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4402b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4403b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich WeigandABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4404b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Handle the generic C++ ABI.
4405b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4406b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4407b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4408b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Integers and enums are extended to full register width.
4409b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (isPromotableIntegerType(Ty))
4410b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getExtend();
4411b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4412b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4413b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  uint64_t Size = getContext().getTypeSize(Ty);
4414b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4415b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getIndirect(0);
4416b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4417b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Handle small structures.
4418b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (const RecordType *RT = Ty->getAs<RecordType>()) {
4419b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // Structures with flexible arrays have variable length, so really
4420b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // fail the size test above.
4421b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    const RecordDecl *RD = RT->getDecl();
4422b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    if (RD->hasFlexibleArrayMember())
4423b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      return ABIArgInfo::getIndirect(0);
4424b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4425b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    // The structure is passed as an unextended integer, a float, or a double.
4426b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    llvm::Type *PassTy;
4427b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    if (isFPArgumentType(Ty)) {
4428b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      assert(Size == 32 || Size == 64);
4429b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      if (Size == 32)
4430b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        PassTy = llvm::Type::getFloatTy(getVMContext());
4431b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      else
4432b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand        PassTy = llvm::Type::getDoubleTy(getVMContext());
4433b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    } else
4434b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand      PassTy = llvm::IntegerType::get(getVMContext(), Size);
4435b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getDirect(PassTy);
4436b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  }
4437b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4438b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Non-structure compounds are passed indirectly.
4439b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  if (isCompoundType(Ty))
4440b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return ABIArgInfo::getIndirect(0);
4441b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4442b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return ABIArgInfo::getDirect(0);
4443b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
4444b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
4445b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand//===----------------------------------------------------------------------===//
4446276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck// MBlaze ABI Implementation
4447276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
4448276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4449276fdf408050d205f3a7f34c1e788224a67d2098Wesley Pecknamespace {
4450276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4451276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeABIInfo : public ABIInfo {
4452276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
4453276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4454276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4455276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  bool isPromotableIntegerType(QualType Ty) const;
4456276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4457276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyReturnType(QualType RetTy) const;
4458276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyArgumentType(QualType RetTy) const;
4459276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4460276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual void computeInfo(CGFunctionInfo &FI) const {
4461276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4462276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4463276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck         it != ie; ++it)
4464276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      it->info = classifyArgumentType(it->type);
4465276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
4466276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4467276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4468276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                 CodeGenFunction &CGF) const;
4469276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
4470276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4471276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
4472276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
4473276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
4474276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
4475276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4476276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                           CodeGen::CodeGenModule &M) const;
4477276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
4478276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4479276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4480276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4481276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckbool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
4482276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
4483276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4484276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    switch (BT->getKind()) {
4485276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Bool:
4486276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_S:
4487276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_U:
4488276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::SChar:
4489276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UChar:
4490276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Short:
4491276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UShort:
4492276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return true;
4493276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    default:
4494276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return false;
4495276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    }
4496276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return false;
4497276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4498276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4499276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckllvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4500276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                      CodeGenFunction &CGF) const {
4501276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // FIXME: Implement
4502276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return 0;
4503276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4504276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4505276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4506276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
4507276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (RetTy->isVoidType())
4508276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIgnore();
4509276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(RetTy))
4510276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
4511276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4512276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(RetTy) ?
4513276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4514276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4515276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4516276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
4517276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(Ty))
4518276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
4519276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4520276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(Ty) ?
4521276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4522276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4523276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4524276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckvoid MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4525276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  llvm::GlobalValue *GV,
4526276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  CodeGen::CodeGenModule &M)
4527276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  const {
4528276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4529276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (!FD) return;
4530125b4cb35536e45201f8f2cb19ee620e3ad67c49NAKAMURA Takumi
4531276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  llvm::CallingConv::ID CC = llvm::CallingConv::C;
4532276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
4533276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_INTR;
4534276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
4535276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_SVOL;
4536276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4537276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC != llvm::CallingConv::C) {
4538276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Handle 'interrupt_handler' attribute:
4539276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      llvm::Function *F = cast<llvm::Function>(GV);
4540276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4541276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 1: Set ISR calling convention.
4542276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->setCallingConv(CC);
4543276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4544276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 2: Add attributes goodness.
454572390b39c545426023ec104afe8706395d732badBill Wendling      F->addFnAttr(llvm::Attribute::NoInline);
4546276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
4547276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4548276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // Step 3: Emit _interrupt_handler alias.
4549276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC == llvm::CallingConv::MBLAZE_INTR)
4550276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4551276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                          "_interrupt_handler", GV, &M.getModule());
4552276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
4553276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4554276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
4555276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
455682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov// MSP430 ABI Implementation
4557dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
455882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
455982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovnamespace {
456082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
456182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
456282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
4563ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4564ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
456582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
456682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                           CodeGen::CodeGenModule &M) const;
456782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
456882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
4569c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
4570c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
457182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovvoid MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
457282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                                  llvm::GlobalValue *GV,
457382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                             CodeGen::CodeGenModule &M) const {
457482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
457582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
457682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Handle 'interrupt' attribute:
457782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      llvm::Function *F = cast<llvm::Function>(GV);
457882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
457982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 1: Set ISR calling convention.
458082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
458182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
458282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 2: Add attributes goodness.
458372390b39c545426023ec104afe8706395d732badBill Wendling      F->addFnAttr(llvm::Attribute::NoInline);
458482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
458582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 3: Emit ISR vector alias.
4586f419a856b56354781141a2a37f6190918be548edAnton Korobeynikov      unsigned Num = attr->getNumber() / 2;
458782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4588f419a856b56354781141a2a37f6190918be548edAnton Korobeynikov                            "__isr_" + Twine(Num),
458982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                            GV, &M.getModule());
459082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    }
459182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  }
4592c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
4593c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
4594dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
4595aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// MIPS ABI Implementation.  This works for both little-endian and
4596aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// big-endian variants.
4597dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
4598dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
4599aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallnamespace {
4600619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakaclass MipsABIInfo : public ABIInfo {
4601c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  bool IsO32;
4602c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4603c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  void CoerceToIntArgs(uint64_t TySize,
4604c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                       SmallVector<llvm::Type*, 8> &ArgList) const;
460591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
4606c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
4607a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
4608619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakapublic:
4609b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
4610c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4611c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    StackAlignInBytes(IsO32 ? 8 : 16) {}
4612619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4613619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  ABIArgInfo classifyReturnType(QualType RetTy) const;
4614f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
4615619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual void computeInfo(CGFunctionInfo &FI) const;
4616619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4617619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                 CodeGenFunction &CGF) const;
4618619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka};
4619619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4620aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallclass MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
4621e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka  unsigned SizeOfUnwindException;
4622aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallpublic:
4623c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4624c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4625c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka      SizeOfUnwindException(IsO32 ? 24 : 32) {}
4626aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4627aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4628aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    return 29;
4629aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
4630aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
46317dfd18275259df609f8574a25302fc73a000aa64Reed Kotler  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
46327dfd18275259df609f8574a25302fc73a000aa64Reed Kotler                           CodeGen::CodeGenModule &CGM) const {
4633ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4634ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    if (!FD) return;
4635d8e6d6da90fd5a715c9e5cb676abbb8c3878c85aRafael Espindola    llvm::Function *Fn = cast<llvm::Function>(GV);
4636ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    if (FD->hasAttr<Mips16Attr>()) {
4637ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler      Fn->addFnAttr("mips16");
4638ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    }
4639ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    else if (FD->hasAttr<NoMips16Attr>()) {
4640ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler      Fn->addFnAttr("nomips16");
4641ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler    }
46427dfd18275259df609f8574a25302fc73a000aa64Reed Kotler  }
4643ad4b8b43e66ebc2838fb314358017079665f058fReed Kotler
4644aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
46458bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
464649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
464749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
4648e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka    return SizeOfUnwindException;
464949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
4650aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall};
4651aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
4652aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4653c359f2029d19016560a422551704ccc2419be0b1Akira Hatanakavoid MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4654c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                                  SmallVector<llvm::Type*, 8> &ArgList) const {
4655c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  llvm::IntegerType *IntTy =
4656c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
465791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
465891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
465991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
466091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(IntTy);
466191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
466291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // If necessary, add one more integer type to ArgList.
466391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  unsigned R = TySize % (MinABIStackAlignInBytes * 8);
466491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
466591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if (R)
466691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
466791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka}
466891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
4669d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// In N32/64, an aligned double precision floating point field is passed in
4670d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// a register.
467191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanakallvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
4672c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4673c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
4674c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (IsO32) {
4675c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
4676c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
4677c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
4678d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
46792afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka  if (Ty->isComplexType())
46802afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka    return CGT.ConvertType(Ty);
46816d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka
4682a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  const RecordType *RT = Ty->getAs<RecordType>();
4683d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4684c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  // Unions/vectors are passed in integer registers.
4685c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (!RT || !RT->isStructureOrClassType()) {
4686c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
4687c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
4688c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
4689d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4690d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const RecordDecl *RD = RT->getDecl();
4691d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
469291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
4693d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4694d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  uint64_t LastOffset = 0;
4695d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  unsigned idx = 0;
4696d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4697d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4698a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // Iterate over fields in the struct/class and check if there are any aligned
4699a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // double fields.
4700d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4701d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka       i != e; ++i, ++idx) {
4702262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    const QualType Ty = i->getType();
4703d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    const BuiltinType *BT = Ty->getAs<BuiltinType>();
4704d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4705d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (!BT || BT->getKind() != BuiltinType::Double)
4706d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
4707d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4708d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    uint64_t Offset = Layout.getFieldOffset(idx);
4709d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (Offset % 64) // Ignore doubles that are not aligned.
4710d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
4711d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4712d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add ((Offset - LastOffset) / 64) args of type i64.
4713d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4714d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      ArgList.push_back(I64);
4715d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4716d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add double type.
4717d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4718d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    LastOffset = Offset + 64;
4719d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  }
4720d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4721c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(TySize - LastOffset, IntArgList);
4722c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  ArgList.append(IntArgList.begin(), IntArgList.end());
4723d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4724d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  return llvm::StructType::get(getVMContext(), ArgList);
4725d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka}
4726d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
4727a33fd393d5255716e904fed021f87260095ed00aAkira Hatanakallvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
472891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert((Offset % MinABIStackAlignInBytes) == 0);
472991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
473091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if ((Align - 1) & Offset)
473191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4732a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
473391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  return 0;
4734a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka}
47359659d59ec368933050684af573b6d32ab5714332Akira Hatanaka
4736f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaABIArgInfo
4737f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaMipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
4738a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t OrigOffset = Offset;
473991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t TySize = getContext().getTypeSize(Ty);
4740a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
474191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
4742c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4743c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                   (uint64_t)StackAlignInBytes);
474491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset = llvm::RoundUpToAlignment(Offset, Align);
474591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
4746a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
4747c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
4748619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    // Ignore empty aggregates.
4749f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (TySize == 0)
4750619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka      return ABIArgInfo::getIgnore();
4751619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4752ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
475391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka      Offset = OrigOffset + MinABIStackAlignInBytes;
4754ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4755f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    }
4756511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka
475791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // If we have reached here, aggregates are passed directly by coercing to
475891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // another structure type. Padding is inserted if the offset of the
475991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // aggregate is unaligned.
476091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
476191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka                                 getPaddingType(Align, OrigOffset));
4762619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
4763619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4764619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
4765619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4766619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    Ty = EnumTy->getDecl()->getIntegerType();
4767619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4768a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  if (Ty->isPromotableIntegerType())
4769a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return ABIArgInfo::getExtend();
4770a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
47714055cfc46a5beb13d0daeace53ac3fe56a1f4ad1Akira Hatanaka  return ABIArgInfo::getDirect(0, 0,
47724055cfc46a5beb13d0daeace53ac3fe56a1f4ad1Akira Hatanaka                               IsO32 ? 0 : getPaddingType(Align, OrigOffset));
4773619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
4774619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4775c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanakallvm::Type*
4776c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira HatanakaMipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
4777da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  const RecordType *RT = RetTy->getAs<RecordType>();
4778c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> RTList;
4779c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4780da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  if (RT && RT->isStructureOrClassType()) {
4781c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    const RecordDecl *RD = RT->getDecl();
4782da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4783da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    unsigned FieldCnt = Layout.getFieldCount();
4784da54ff306270e179f64d046369419724356d30d7Akira Hatanaka
4785da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // N32/64 returns struct/classes in floating point registers if the
4786da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // following conditions are met:
4787da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 1. The size of the struct/class is no larger than 128-bit.
4788da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 2. The struct/class has one or two fields all of which are floating
4789da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //    point types.
4790da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 3. The offset of the first field is zero (this follows what gcc does).
4791da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
4792da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // Any other composite results are returned in integer registers.
4793da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
4794da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4795da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4796da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      for (; b != e; ++b) {
4797262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
4798c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4799da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        if (!BT || !BT->isFloatingPoint())
4800da54ff306270e179f64d046369419724356d30d7Akira Hatanaka          break;
4801c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4802262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        RTList.push_back(CGT.ConvertType(b->getType()));
4803da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      }
4804c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4805da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      if (b == e)
4806da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        return llvm::StructType::get(getVMContext(), RTList,
4807da54ff306270e179f64d046369419724356d30d7Akira Hatanaka                                     RD->hasAttr<PackedAttr>());
4808c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4809da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RTList.clear();
4810da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    }
4811c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  }
4812c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4813c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(Size, RTList);
4814c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  return llvm::StructType::get(getVMContext(), RTList);
4815c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka}
4816c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4817619e8875d29cc019c7360595f66b9f91b3439494Akira HatanakaABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
4818a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  uint64_t Size = getContext().getTypeSize(RetTy);
4819a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka
4820a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  if (RetTy->isVoidType() || Size == 0)
4821619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIgnore();
4822619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
48238aeb1471ef62a4befba00721925a3717914f21d8Akira Hatanaka  if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
4824ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (isRecordReturnIndirect(RetTy, CGT))
4825ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      return ABIArgInfo::getIndirect(0);
4826ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
4827c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    if (Size <= 128) {
4828c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka      if (RetTy->isAnyComplexType())
4829c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect();
4830c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
4831c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      // O32 returns integer vectors in registers.
4832c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4833c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4834c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
4835ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      if (!IsO32)
4836c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4837c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    }
4838619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4839619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIndirect(0);
4840619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
4841619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4842619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
4843619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4844619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    RetTy = EnumTy->getDecl()->getIntegerType();
4845619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4846619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  return (RetTy->isPromotableIntegerType() ?
4847619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4848619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
4849619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4850619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakavoid MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
4851cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  ABIArgInfo &RetInfo = FI.getReturnInfo();
4852cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  RetInfo = classifyReturnType(FI.getReturnType());
4853cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
4854cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  // Check if a pointer to an aggregate is passed as a hidden argument.
485591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
4856cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
4857619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4858619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka       it != ie; ++it)
4859f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    it->info = classifyArgumentType(it->type, Offset);
4860619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
4861619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4862619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakallvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4863619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                    CodeGenFunction &CGF) const {
48648b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
48658b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
4866c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
4867c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  CGBuilderTy &Builder = CGF.Builder;
4868c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4869c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
48708f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
4871c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4872c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AddrTyped;
487364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  unsigned PtrWidth = getTarget().getPointerWidth(0);
48748f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
4875c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
4876c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  if (TypeAlign > MinABIStackAlignInBytes) {
48778f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
48788f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
48798f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
48808f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
4881c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4882c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4883c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  }
4884c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  else
4885c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = Builder.CreateBitCast(Addr, PTy);
4886c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
4887c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
48888f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
4889c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  uint64_t Offset =
4890c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4891c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *NextAddr =
48928f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
4893c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka                      "ap.next");
4894c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4895c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
4896c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  return AddrTyped;
4897619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
4898619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
4899aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallbool
4900aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallMIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4901aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                                               llvm::Value *Address) const {
4902aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // This information comes from gcc's implementation, which seems to
4903aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // as canonical as it gets.
4904aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4905aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Everything on MIPS is 4 bytes.  Double-precision FP registers
4906aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // are aliased to pairs of single-precision FP registers.
49078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
4908aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4909aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 0-31 are the general purpose registers, $0 - $31.
4910aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 32-63 are the floating-point registers, $f0 - $f31.
4911aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 64 and 65 are the multiply/divide registers, $hi and $lo.
4912aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 66 is the (notional, I think) register for signal-handler return.
49138b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
4914aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4915aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4916aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // They are one bit wide and ignored here.
4917aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
4918aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4919aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // (coprocessor 1 is the FP unit)
4920aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4921aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4922aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 176-181 are the DSP accumulator registers.
49238b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
4924aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  return false;
4925aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
4926aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
49272f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
49282f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
49292f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// Currently subclassed only to implement custom OpenCL C function attribute
49302f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// handling.
49312f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
49322f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49332f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournenamespace {
49342f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49352f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourneclass TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
49362f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournepublic:
49372f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  TCETargetCodeGenInfo(CodeGenTypes &CGT)
49382f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    : DefaultTargetCodeGenInfo(CGT) {}
49392f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49402f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
49412f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
49422f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne};
49432f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49442f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournevoid TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
49452f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               llvm::GlobalValue *GV,
49462f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               CodeGen::CodeGenModule &M) const {
49472f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
49482f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  if (!FD) return;
49492f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49502f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  llvm::Function *F = cast<llvm::Function>(GV);
49512f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
49532f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    if (FD->hasAttr<OpenCLKernelAttr>()) {
49542f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      // OpenCL C Kernel functions are not subject to inlining
495572390b39c545426023ec104afe8706395d732badBill Wendling      F->addFnAttr(llvm::Attribute::NoInline);
49562f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49572f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
49582f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49592f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Convert the reqd_work_group_size() attributes to metadata.
49602f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::LLVMContext &Context = F->getContext();
49612f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
49622f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne            M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
49632f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49642f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        SmallVector<llvm::Value*, 5> Operands;
49652f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        Operands.push_back(F);
49662f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49678b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
49688b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
49698b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
49708b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
49718b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
49722f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
49738b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
49748b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
49752f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
49762f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49772f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Add a boolean constant operand for "required" (true) or "hint" (false)
49782f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // for implementing the work_group_size_hint attr later. Currently
49792f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // always true as the hint is not yet implemented.
49808b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::ConstantInt::getTrue(Context));
49812f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
49822f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      }
49832f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    }
49842f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  }
49852f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
49862f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
49872f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
4988aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
49899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
49909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum// Hexagon ABI Implementation
49919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
49929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
49939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumnamespace {
49949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
49959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonABIInfo : public ABIInfo {
49969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
49979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
49989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
49999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
50009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50019631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumprivate:
50029631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50039631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyReturnType(QualType RetTy) const;
50049631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyArgumentType(QualType RetTy) const;
50059631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual void computeInfo(CGFunctionInfo &FI) const;
50079631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
50099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                 CodeGenFunction &CGF) const;
50109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
50119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
50139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
50149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
50159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
50169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
50189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return 29;
50199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
50209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
50219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
50239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumvoid HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
50259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
50269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
50279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum       it != ie; ++it)
50289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    it->info = classifyArgumentType(it->type);
50299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
50309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
50329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(Ty)) {
50339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
50349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
50359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      Ty = EnumTy->getDecl()->getIntegerType();
50369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (Ty->isPromotableIntegerType() ?
50389631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
50399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
50409631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Ignore empty records.
50429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), Ty, true))
50439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
50449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
5045ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5046ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
50479631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50489631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(Ty);
50499631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size > 64)
50509631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
50519631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Pass in the smallest viable integer type.
50529631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 32)
50539631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
50549631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 16)
50559631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
50569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 8)
50579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
50589631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else
50599631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
50609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
50619631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50629631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
50639631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVoidType())
50649631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
50659631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50669631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Large vector types should be returned via memory.
50679631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
50689631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0);
50699631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50709631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(RetTy)) {
50719631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
50729631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
50739631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      RetTy = EnumTy->getDecl()->getIntegerType();
50749631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50759631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (RetTy->isPromotableIntegerType() ?
50769631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
50779631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
50789631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50799631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
50809631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
5081ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  if (isRecordReturnIndirect(RetTy, CGT))
50829631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
50839631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50849631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), RetTy, true))
50859631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
50869631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
50879631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Aggregates <= 8 bytes are returned in r0; other aggregates
50889631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // are returned indirectly.
50899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(RetTy);
50909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size <= 64) {
50919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Return in the smallest viable integer type.
50929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 8)
50939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
50949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 16)
50959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
50969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 32)
50979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
50989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
50999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
51009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51019631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
51029631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
51039631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51049631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumllvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
51058b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                                       CodeGenFunction &CGF) const {
51069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // FIXME: Need to handle alignment
51078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
51089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  CGBuilderTy &Builder = CGF.Builder;
51109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
51119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                                       "ap");
51129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
51139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Type *PTy =
51149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
51159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
51169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Offset =
51189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
51199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *NextAddr =
51209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
51219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                      "ap.next");
51229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
51239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return AddrTyped;
51259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
51269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
51279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
5128ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
512982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (TheTargetCodeGenInfo)
513082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return *TheTargetCodeGenInfo;
5131c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
513264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  const llvm::Triple &Triple = getTarget().getTriple();
51331752ee4849f4c37f5e03193e658be92650b0e65aDaniel Dunbar  switch (Triple.getArch()) {
51342c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar  default:
5135ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
51362c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
51379ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  case llvm::Triple::le32:
51389ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
5139aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mips:
5140aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mipsel:
5141c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
5142aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
51438c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64:
51448c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64el:
5145c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
51468c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka
5147c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case llvm::Triple::aarch64:
5148c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5149c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
515034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::arm:
515134d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::thumb:
515234c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    {
515334c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
515464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall      if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
515534c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::APCS;
5156b16abb1bd8ed94c7994836de24915703e6a4e81aDavid Tweed      else if (CodeGenOpts.FloatABI == "hard" ||
515764aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall               (CodeGenOpts.FloatABI != "soft" &&
515864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
515934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::AAPCS_VFP;
516034c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel
5161263366f9241366f29ba65b703120f302490c39ffDerek Schuff      switch (Triple.getOS()) {
5162441d9f7a36d3289eed0299823f05f70f810364eeEli Bendersky        case llvm::Triple::NaCl:
5163263366f9241366f29ba65b703120f302490c39ffDerek Schuff          return *(TheTargetCodeGenInfo =
5164263366f9241366f29ba65b703120f302490c39ffDerek Schuff                   new NaClARMTargetCodeGenInfo(Types, Kind));
5165263366f9241366f29ba65b703120f302490c39ffDerek Schuff        default:
5166263366f9241366f29ba65b703120f302490c39ffDerek Schuff          return *(TheTargetCodeGenInfo =
5167263366f9241366f29ba65b703120f302490c39ffDerek Schuff                   new ARMTargetCodeGenInfo(Types, Kind));
5168263366f9241366f29ba65b703120f302490c39ffDerek Schuff      }
516934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    }
517034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
5171ec853ba1087f606e9685cb1e800616565ba35093John McCall  case llvm::Triple::ppc:
5172ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
51730fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  case llvm::Triple::ppc64:
51742fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    if (Triple.isOSBinFormatELF())
51752fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt      return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
51762fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt    else
51772fc107f5652a526d9c2972dc3b386e5d86769e44Bill Schmidt      return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
5178ec853ba1087f606e9685cb1e800616565ba35093John McCall
5179edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx:
5180edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx64:
51812c585b991596859f39860b6094247ba027a03530Justin Holewinski    return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
51820259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
5183276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  case llvm::Triple::mblaze:
5184276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
5185276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
518682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  case llvm::Triple::msp430:
5187ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
518834d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
5189b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  case llvm::Triple::systemz:
5190b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5191b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
51922f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  case llvm::Triple::tce:
51932f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
51942f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
5195c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  case llvm::Triple::x86: {
5196db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    if (Triple.isOSDarwin())
519782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
51981f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier               new X86_32TargetCodeGenInfo(Types, true, true, false,
5199b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
5200db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar
5201db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    switch (Triple.getOS()) {
52022c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::Cygwin:
52032c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::MinGW32:
5204727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::AuroraUX:
5205727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::DragonFly:
520675c135a511c855d94bbfa7f00dd27a165f61e953David Chisnall    case llvm::Triple::FreeBSD:
52072c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::OpenBSD:
520842f74f21ece01dc8573d5377859d327fbb23b26cEli Friedman    case llvm::Triple::Bitrig:
520982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
52101f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier               new X86_32TargetCodeGenInfo(Types, false, true, false,
5211b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
521255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman
521355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    case llvm::Triple::Win32:
521455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      return *(TheTargetCodeGenInfo =
52153190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner               new WinX86_32TargetCodeGenInfo(Types,
52163190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner                                              CodeGenOpts.NumRegisterParameters));
52172c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
52182c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    default:
521982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
52201f1df1f48e4c804d80d996fa6e38dee9de633deaChad Rosier               new X86_32TargetCodeGenInfo(Types, false, false, false,
5221b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
5222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
5223c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  }
52242c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
5225ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  case llvm::Triple::x86_64: {
522664aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall    bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
5227ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
5228f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    switch (Triple.getOS()) {
5229f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Win32:
52300aa205765aec0aa5eed672f8e3cade543372edcdNAKAMURA Takumi    case llvm::Triple::MinGW32:
5231f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Cygwin:
5232f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner      return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
5233441d9f7a36d3289eed0299823f05f70f810364eeEli Bendersky    case llvm::Triple::NaCl:
523464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall      return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
523564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall                                                                      HasAVX));
5236f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    default:
5237ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5238ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman                                                                  HasAVX));
5239f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    }
5240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
52419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  case llvm::Triple::hexagon:
52429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
5243ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
5244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
5245