Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 20 | #include "clang/CodeGen/CGFunctionInfo.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 29 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 30 | llvm::Value *Array, |
| 31 | llvm::Value *Value, |
| 32 | unsigned FirstIndex, |
| 33 | unsigned LastIndex) { |
| 34 | // Alternatively, we could emit this as a loop in the source. |
| 35 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 36 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 37 | Builder.CreateStore(Value, Cell); |
| 38 | } |
| 39 | } |
| 40 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 41 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 42 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 43 | T->isMemberFunctionPointerType(); |
| 44 | } |
| 45 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 46 | ABIInfo::~ABIInfo() {} |
| 47 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 48 | static bool isRecordReturnIndirect(const RecordType *RT, |
| 49 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 50 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 51 | if (!RD) |
| 52 | return false; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 53 | return CXXABI.isReturnTypeIndirect(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 57 | static bool isRecordReturnIndirect(QualType T, CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 58 | const RecordType *RT = T->getAs<RecordType>(); |
| 59 | if (!RT) |
| 60 | return false; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 61 | return isRecordReturnIndirect(RT, CXXABI); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 65 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 66 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 67 | if (!RD) |
| 68 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 69 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 73 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 74 | const RecordType *RT = T->getAs<RecordType>(); |
| 75 | if (!RT) |
| 76 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 77 | return getRecordArgABI(RT, CXXABI); |
| 78 | } |
| 79 | |
| 80 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 81 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 84 | ASTContext &ABIInfo::getContext() const { |
| 85 | return CGT.getContext(); |
| 86 | } |
| 87 | |
| 88 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 89 | return CGT.getLLVMContext(); |
| 90 | } |
| 91 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 92 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 93 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 96 | const TargetInfo &ABIInfo::getTarget() const { |
| 97 | return CGT.getTarget(); |
| 98 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 99 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 100 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 101 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 102 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 103 | switch (TheKind) { |
| 104 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 105 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 106 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 107 | Ty->print(OS); |
| 108 | else |
| 109 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 111 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 112 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 113 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 114 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 115 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 116 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 117 | case InAlloca: |
| 118 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 119 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 120 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 121 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 122 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 123 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 124 | break; |
| 125 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 126 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 127 | break; |
| 128 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 129 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 132 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 133 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 134 | // If someone can figure out a general rule for this, that would be great. |
| 135 | // It's probably just doomed to be platform-dependent, though. |
| 136 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 137 | // Verified for: |
| 138 | // x86-64 FreeBSD, Linux, Darwin |
| 139 | // x86-32 FreeBSD, Linux, Darwin |
| 140 | // PowerPC Linux, Darwin |
| 141 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 142 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 143 | return 32; |
| 144 | } |
| 145 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 146 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 147 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 148 | // The following conventions are known to require this to be false: |
| 149 | // x86_stdcall |
| 150 | // MIPS |
| 151 | // For everything else, we just prefer false unless we opt out. |
| 152 | return false; |
| 153 | } |
| 154 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 155 | void |
| 156 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 157 | llvm::SmallString<24> &Opt) const { |
| 158 | // This assumes the user is passing a library name like "rt" instead of a |
| 159 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 160 | // dynamic. |
| 161 | Opt = "-l"; |
| 162 | Opt += Lib; |
| 163 | } |
| 164 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 165 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 166 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 167 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 168 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 169 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 170 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 171 | if (FD->isUnnamedBitfield()) |
| 172 | return true; |
| 173 | |
| 174 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 175 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 176 | // Constant arrays of empty records count as empty, strip them off. |
| 177 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 178 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 179 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 180 | if (AT->getSize() == 0) |
| 181 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 182 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 183 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 185 | const RecordType *RT = FT->getAs<RecordType>(); |
| 186 | if (!RT) |
| 187 | return false; |
| 188 | |
| 189 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 190 | // |
| 191 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 192 | // current ABI. |
| 193 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 194 | return false; |
| 195 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 196 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 199 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 200 | /// fields. Note that a structure with a flexible array member is not |
| 201 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 202 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 203 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 204 | if (!RT) |
| 205 | return 0; |
| 206 | const RecordDecl *RD = RT->getDecl(); |
| 207 | if (RD->hasFlexibleArrayMember()) |
| 208 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 209 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 210 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 211 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 212 | for (const auto &I : CXXRD->bases()) |
| 213 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 214 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 215 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 216 | for (const auto *I : RD->fields()) |
| 217 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 218 | return false; |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /// isSingleElementStruct - Determine if a structure is a "single |
| 223 | /// element struct", i.e. it has exactly one non-empty field or |
| 224 | /// exactly one field which is itself a single element |
| 225 | /// struct. Structures with flexible array members are never |
| 226 | /// considered single element structs. |
| 227 | /// |
| 228 | /// \return The field declaration for the single non-empty field, if |
| 229 | /// it exists. |
| 230 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 231 | const RecordType *RT = T->getAsStructureType(); |
| 232 | if (!RT) |
| 233 | return 0; |
| 234 | |
| 235 | const RecordDecl *RD = RT->getDecl(); |
| 236 | if (RD->hasFlexibleArrayMember()) |
| 237 | return 0; |
| 238 | |
| 239 | const Type *Found = 0; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 240 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 241 | // If this is a C++ record, check the bases first. |
| 242 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 243 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 244 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 245 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 246 | continue; |
| 247 | |
| 248 | // If we already found an element then this isn't a single-element struct. |
| 249 | if (Found) |
| 250 | return 0; |
| 251 | |
| 252 | // If this is non-empty and not a single element struct, the composite |
| 253 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 254 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 255 | if (!Found) |
| 256 | return 0; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 261 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 262 | QualType FT = FD->getType(); |
| 263 | |
| 264 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 265 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 266 | continue; |
| 267 | |
| 268 | // If we already found an element then this isn't a single-element |
| 269 | // struct. |
| 270 | if (Found) |
| 271 | return 0; |
| 272 | |
| 273 | // Treat single element arrays as the element. |
| 274 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 275 | if (AT->getSize().getZExtValue() != 1) |
| 276 | break; |
| 277 | FT = AT->getElementType(); |
| 278 | } |
| 279 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 280 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 281 | Found = FT.getTypePtr(); |
| 282 | } else { |
| 283 | Found = isSingleElementStruct(FT, Context); |
| 284 | if (!Found) |
| 285 | return 0; |
| 286 | } |
| 287 | } |
| 288 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 289 | // We don't consider a struct a single-element struct if it has |
| 290 | // padding beyond the element type. |
| 291 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| 292 | return 0; |
| 293 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 294 | return Found; |
| 295 | } |
| 296 | |
| 297 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 298 | // Treat complex types as the element type. |
| 299 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 300 | Ty = CTy->getElementType(); |
| 301 | |
| 302 | // Check for a type which we know has a simple scalar argument-passing |
| 303 | // convention without any padding. (We're specifically looking for 32 |
| 304 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 305 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 306 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 307 | return false; |
| 308 | |
| 309 | uint64_t Size = Context.getTypeSize(Ty); |
| 310 | return Size == 32 || Size == 64; |
| 311 | } |
| 312 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 313 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 314 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 315 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 316 | /// inhibiting optimizations. |
| 317 | /// |
| 318 | // FIXME: This predicate is missing many cases, currently it just follows |
| 319 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 320 | // should probably make this smarter, or better yet make the LLVM backend |
| 321 | // capable of handling it. |
| 322 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 323 | // We can only expand structure types. |
| 324 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 325 | if (!RT) |
| 326 | return false; |
| 327 | |
| 328 | // We can only expand (C) structures. |
| 329 | // |
| 330 | // FIXME: This needs to be generalized to handle classes as well. |
| 331 | const RecordDecl *RD = RT->getDecl(); |
| 332 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 333 | return false; |
| 334 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 335 | uint64_t Size = 0; |
| 336 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 337 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 338 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 339 | return false; |
| 340 | |
| 341 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 342 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 343 | // counts as "basic" is more complicated than what we were doing previously. |
| 344 | if (FD->isBitField()) |
| 345 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 346 | |
| 347 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 350 | // Make sure there are not any holes in the struct. |
| 351 | if (Size != Context.getTypeSize(Ty)) |
| 352 | return false; |
| 353 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | |
| 357 | namespace { |
| 358 | /// DefaultABIInfo - The default implementation for ABI specific |
| 359 | /// details. This implementation provides information which results in |
| 360 | /// self-consistent and sensible LLVM IR generation, but does not |
| 361 | /// conform to any particular ABI. |
| 362 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 363 | public: |
| 364 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 366 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 367 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 368 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 369 | void computeInfo(CGFunctionInfo &FI) const override { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 370 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 371 | for (auto &I : FI.arguments()) |
| 372 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 375 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 376 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 379 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 380 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 381 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 382 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 386 | CodeGenFunction &CGF) const { |
| 387 | return 0; |
| 388 | } |
| 389 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 390 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 391 | if (isAggregateTypeForABI(Ty)) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 392 | // Records with non-trivial destructors/constructors should not be passed |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 393 | // by value. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 394 | if (isRecordReturnIndirect(Ty, getCXXABI())) |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 395 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 396 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 397 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 398 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 400 | // Treat an enum type as its underlying type. |
| 401 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 402 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 404 | return (Ty->isPromotableIntegerType() ? |
| 405 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 408 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 409 | if (RetTy->isVoidType()) |
| 410 | return ABIArgInfo::getIgnore(); |
| 411 | |
| 412 | if (isAggregateTypeForABI(RetTy)) |
| 413 | return ABIArgInfo::getIndirect(0); |
| 414 | |
| 415 | // Treat an enum type as its underlying type. |
| 416 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 417 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 418 | |
| 419 | return (RetTy->isPromotableIntegerType() ? |
| 420 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 421 | } |
| 422 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 425 | // |
| 426 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 427 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | |
| 430 | class PNaClABIInfo : public ABIInfo { |
| 431 | public: |
| 432 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 433 | |
| 434 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 435 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 436 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 437 | void computeInfo(CGFunctionInfo &FI) const override; |
| 438 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 439 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 443 | public: |
| 444 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 445 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 446 | }; |
| 447 | |
| 448 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 449 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 450 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 451 | for (auto &I : FI.arguments()) |
| 452 | I.info = classifyArgumentType(I.type); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 456 | CodeGenFunction &CGF) const { |
| 457 | return 0; |
| 458 | } |
| 459 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 460 | /// \brief Classify argument of given type \p Ty. |
| 461 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 462 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 463 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 464 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 465 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 466 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 467 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 468 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 469 | } else if (Ty->isFloatingType()) { |
| 470 | // Floating-point types don't go inreg. |
| 471 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 472 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 473 | |
| 474 | return (Ty->isPromotableIntegerType() ? |
| 475 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 479 | if (RetTy->isVoidType()) |
| 480 | return ABIArgInfo::getIgnore(); |
| 481 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 482 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 483 | if (isAggregateTypeForABI(RetTy)) |
| 484 | return ABIArgInfo::getIndirect(0); |
| 485 | |
| 486 | // Treat an enum type as its underlying type. |
| 487 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 488 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 489 | |
| 490 | return (RetTy->isPromotableIntegerType() ? |
| 491 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 492 | } |
| 493 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 494 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 495 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 496 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 497 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 498 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 499 | IRType->getScalarSizeInBits() != 64; |
| 500 | } |
| 501 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 502 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 503 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 504 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 505 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 506 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 507 | // Invalid MMX constraint |
| 508 | return 0; |
| 509 | } |
| 510 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 511 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 515 | return Ty; |
| 516 | } |
| 517 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 518 | //===----------------------------------------------------------------------===// |
| 519 | // X86-32 ABI Implementation |
| 520 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 521 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 522 | /// \brief Similar to llvm::CCState, but for Clang. |
| 523 | struct CCState { |
| 524 | CCState(unsigned CC) : CC(CC), FreeRegs(0) {} |
| 525 | |
| 526 | unsigned CC; |
| 527 | unsigned FreeRegs; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 528 | unsigned StackOffset; |
| 529 | bool UseInAlloca; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 533 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 534 | enum Class { |
| 535 | Integer, |
| 536 | Float |
| 537 | }; |
| 538 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 539 | static const unsigned MinABIStackAlignInBytes = 4; |
| 540 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 541 | bool IsDarwinVectorABI; |
| 542 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 543 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 544 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 545 | |
| 546 | static bool isRegisterSize(unsigned Size) { |
| 547 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 548 | } |
| 549 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 550 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 551 | bool IsInstanceMethod) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 552 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 553 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 554 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 555 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 556 | |
| 557 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 559 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 560 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 561 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 562 | Class classify(QualType Ty) const; |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 563 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State, |
| 564 | bool IsInstanceMethod) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 565 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 566 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 567 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 568 | /// \brief Rewrite the function info so that all memory arguments use |
| 569 | /// inalloca. |
| 570 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 571 | |
| 572 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 573 | unsigned &StackOffset, ABIArgInfo &Info, |
| 574 | QualType Type) const; |
| 575 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 576 | public: |
| 577 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 578 | void computeInfo(CGFunctionInfo &FI) const override; |
| 579 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 580 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 581 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 582 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 583 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 584 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 585 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 586 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 587 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 588 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 589 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 590 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 591 | bool d, bool p, bool w, unsigned r) |
| 592 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 593 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 594 | static bool isStructReturnInRegABI( |
| 595 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 596 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 597 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 598 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 599 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 600 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 601 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 602 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 603 | return 4; |
| 604 | } |
| 605 | |
| 606 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 607 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 608 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 609 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 610 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 611 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 612 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 613 | } |
| 614 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 615 | llvm::Constant * |
| 616 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 617 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 618 | (0x06 << 8) | // .+0x08 |
| 619 | ('F' << 16) | |
| 620 | ('T' << 24); |
| 621 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 622 | } |
| 623 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 627 | |
| 628 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 629 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 630 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 631 | bool IsInstanceMethod) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 632 | uint64_t Size = Context.getTypeSize(Ty); |
| 633 | |
| 634 | // Type must be register sized. |
| 635 | if (!isRegisterSize(Size)) |
| 636 | return false; |
| 637 | |
| 638 | if (Ty->isVectorType()) { |
| 639 | // 64- and 128- bit vectors inside structures are not returned in |
| 640 | // registers. |
| 641 | if (Size == 64 || Size == 128) |
| 642 | return false; |
| 643 | |
| 644 | return true; |
| 645 | } |
| 646 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 647 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 648 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 649 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 650 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 651 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 652 | return true; |
| 653 | |
| 654 | // Arrays are treated like records. |
| 655 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 656 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 657 | IsInstanceMethod); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 658 | |
| 659 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 660 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 661 | if (!RT) return false; |
| 662 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 663 | // FIXME: Traverse bases here too. |
| 664 | |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 665 | // For thiscall conventions, structures will never be returned in |
| 666 | // a register. This is for compatibility with the MSVC ABI |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 667 | if (IsWin32StructABI && IsInstanceMethod && RT->isStructureType()) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 668 | return false; |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 669 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 670 | // Structure types are passed in register if all fields would be |
| 671 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 672 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 673 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 674 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 675 | continue; |
| 676 | |
| 677 | // Check fields recursively. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 678 | if (!shouldReturnTypeInRegister(FD->getType(), Context, IsInstanceMethod)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 679 | return false; |
| 680 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 681 | return true; |
| 682 | } |
| 683 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 684 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 685 | // If the return value is indirect, then the hidden argument is consuming one |
| 686 | // integer register. |
| 687 | if (State.FreeRegs) { |
| 688 | --State.FreeRegs; |
| 689 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 690 | } |
| 691 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 692 | } |
| 693 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 694 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State, |
| 695 | bool IsInstanceMethod) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 696 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 697 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 699 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 700 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 701 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 702 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 703 | |
| 704 | // 128-bit vectors are a special case; they are returned in |
| 705 | // registers and we need to make sure to pick a type the LLVM |
| 706 | // backend will like. |
| 707 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 708 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 709 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 710 | |
| 711 | // Always return in register if it fits in a general purpose |
| 712 | // register, or if it is 64 bits and has a single element. |
| 713 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 714 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 715 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 716 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 717 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 718 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 722 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 723 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 724 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 725 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 726 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 727 | return getIndirectReturnResult(State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 728 | |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 729 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 730 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 731 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 732 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 733 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 734 | // If specified, structs and unions are always indirect. |
| 735 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 736 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 737 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 738 | // Small structures which are register sized are generally returned |
| 739 | // in a register. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 740 | if (shouldReturnTypeInRegister(RetTy, getContext(), IsInstanceMethod)) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 741 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 742 | |
| 743 | // As a special-case, if the struct is a "single-element" struct, and |
| 744 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 745 | // floating-point register. (MSVC does not apply this special case.) |
| 746 | // We apply a similar transformation for pointer types to improve the |
| 747 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 748 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 749 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 750 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 751 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 752 | |
| 753 | // FIXME: We should be able to narrow this integer in cases with dead |
| 754 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 755 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 758 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 759 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 761 | // Treat an enum type as its underlying type. |
| 762 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 763 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 764 | |
| 765 | return (RetTy->isPromotableIntegerType() ? |
| 766 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 769 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 770 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 771 | } |
| 772 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 773 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 774 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 775 | if (!RT) |
| 776 | return 0; |
| 777 | const RecordDecl *RD = RT->getDecl(); |
| 778 | |
| 779 | // If this is a C++ record, check the bases first. |
| 780 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 781 | for (const auto &I : CXXRD->bases()) |
| 782 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 783 | return false; |
| 784 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 785 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 786 | QualType FT = i->getType(); |
| 787 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 788 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 789 | return true; |
| 790 | |
| 791 | if (isRecordWithSSEVectorType(Context, FT)) |
| 792 | return true; |
| 793 | } |
| 794 | |
| 795 | return false; |
| 796 | } |
| 797 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 798 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 799 | unsigned Align) const { |
| 800 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 801 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 802 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 803 | return 0; // Use default alignment. |
| 804 | |
| 805 | // On non-Darwin, the stack type alignment is always 4. |
| 806 | if (!IsDarwinVectorABI) { |
| 807 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 808 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 809 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 810 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 811 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 812 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 813 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 814 | return 16; |
| 815 | |
| 816 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 819 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 820 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 821 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 822 | if (State.FreeRegs) { |
| 823 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 824 | return ABIArgInfo::getIndirectInReg(0, false); |
| 825 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 826 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 827 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 828 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 829 | // Compute the byval alignment. |
| 830 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 831 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 832 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 833 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 834 | |
| 835 | // If the stack alignment is less than the type alignment, realign the |
| 836 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 837 | bool Realign = TypeAlign > StackAlign; |
| 838 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 841 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 842 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 843 | if (!T) |
| 844 | T = Ty.getTypePtr(); |
| 845 | |
| 846 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 847 | BuiltinType::Kind K = BT->getKind(); |
| 848 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 849 | return Float; |
| 850 | } |
| 851 | return Integer; |
| 852 | } |
| 853 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 854 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 855 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 856 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 857 | Class C = classify(Ty); |
| 858 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 859 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 860 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 861 | unsigned Size = getContext().getTypeSize(Ty); |
| 862 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 863 | |
| 864 | if (SizeInRegs == 0) |
| 865 | return false; |
| 866 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 867 | if (SizeInRegs > State.FreeRegs) { |
| 868 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 869 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 870 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 871 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 872 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 873 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 874 | if (State.CC == llvm::CallingConv::X86_FastCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 875 | if (Size > 32) |
| 876 | return false; |
| 877 | |
| 878 | if (Ty->isIntegralOrEnumerationType()) |
| 879 | return true; |
| 880 | |
| 881 | if (Ty->isPointerType()) |
| 882 | return true; |
| 883 | |
| 884 | if (Ty->isReferenceType()) |
| 885 | return true; |
| 886 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 887 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 888 | NeedsPadding = true; |
| 889 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 890 | return false; |
| 891 | } |
| 892 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 893 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 896 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 897 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 898 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 899 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 900 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 901 | // Check with the C++ ABI first. |
| 902 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 903 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 904 | return getIndirectResult(Ty, false, State); |
| 905 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 906 | // The field index doesn't matter, we'll fix it up later. |
| 907 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 908 | } |
| 909 | |
| 910 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 911 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 912 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 913 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 914 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 915 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 916 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 917 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 918 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 919 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 920 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 921 | return ABIArgInfo::getIgnore(); |
| 922 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 923 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 924 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 925 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 926 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 927 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 928 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 929 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 930 | return ABIArgInfo::getDirectInReg(Result); |
| 931 | } |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 932 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 933 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 934 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 935 | // of those arguments will match the struct. This is important because the |
| 936 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 937 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 938 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 939 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 940 | return ABIArgInfo::getExpandWithPadding( |
| 941 | State.CC == llvm::CallingConv::X86_FastCall, PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 942 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 943 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 946 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 947 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 948 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 949 | if (IsDarwinVectorABI) { |
| 950 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 951 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 952 | (Size == 64 && VT->getNumElements() == 1)) |
| 953 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 954 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 955 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 956 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 957 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 958 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 959 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 960 | return ABIArgInfo::getDirect(); |
| 961 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 962 | |
| 963 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 964 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 965 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 966 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 967 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 968 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 969 | |
| 970 | if (Ty->isPromotableIntegerType()) { |
| 971 | if (InReg) |
| 972 | return ABIArgInfo::getExtendInReg(); |
| 973 | return ABIArgInfo::getExtend(); |
| 974 | } |
| 975 | if (InReg) |
| 976 | return ABIArgInfo::getDirectInReg(); |
| 977 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 980 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 981 | CCState State(FI.getCallingConvention()); |
| 982 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 983 | State.FreeRegs = 2; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 984 | else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 985 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 986 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 987 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 988 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 989 | FI.getReturnInfo() = |
| 990 | classifyReturnType(FI.getReturnType(), State, FI.isInstanceMethod()); |
| 991 | |
| 992 | // On win32, use the x86_cdeclmethodcc convention for cdecl methods that use |
| 993 | // sret. This convention swaps the order of the first two parameters behind |
| 994 | // the scenes to match MSVC. |
| 995 | if (IsWin32StructABI && FI.isInstanceMethod() && |
| 996 | FI.getCallingConvention() == llvm::CallingConv::C && |
| 997 | FI.getReturnInfo().isIndirect()) |
| 998 | FI.setEffectiveCallingConvention(llvm::CallingConv::X86_CDeclMethod); |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 999 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1000 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1001 | for (auto &I : FI.arguments()) { |
| 1002 | I.info = classifyArgumentType(I.type, State); |
| 1003 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1007 | // all the memory arguments to use inalloca. |
| 1008 | if (UsedInAlloca) |
| 1009 | rewriteWithInAlloca(FI); |
| 1010 | } |
| 1011 | |
| 1012 | void |
| 1013 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1014 | unsigned &StackOffset, |
| 1015 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1016 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1017 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1018 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1019 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1020 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1021 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1022 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1023 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1024 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1025 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1026 | unsigned NumBytes = StackOffset - OldOffset; |
| 1027 | assert(NumBytes); |
| 1028 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1029 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1030 | FrameFields.push_back(Ty); |
| 1031 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1035 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1036 | |
| 1037 | // Build a packed struct type for all of the arguments in memory. |
| 1038 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1039 | |
| 1040 | unsigned StackOffset = 0; |
| 1041 | |
| 1042 | // Put the sret parameter into the inalloca struct if it's in memory. |
| 1043 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1044 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1045 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1046 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1047 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1048 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | // Skip the 'this' parameter in ecx. |
| 1052 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1053 | if (FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall) |
| 1054 | ++I; |
| 1055 | |
| 1056 | // Put arguments passed in memory into the struct. |
| 1057 | for (; I != E; ++I) { |
| 1058 | |
| 1059 | // Leave ignored and inreg arguments alone. |
| 1060 | switch (I->info.getKind()) { |
| 1061 | case ABIArgInfo::Indirect: |
| 1062 | assert(I->info.getIndirectByVal()); |
| 1063 | break; |
| 1064 | case ABIArgInfo::Ignore: |
| 1065 | continue; |
| 1066 | case ABIArgInfo::Direct: |
| 1067 | case ABIArgInfo::Extend: |
| 1068 | if (I->info.getInReg()) |
| 1069 | continue; |
| 1070 | break; |
| 1071 | default: |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1076 | } |
| 1077 | |
| 1078 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1079 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1082 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1083 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1084 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1085 | |
| 1086 | CGBuilderTy &Builder = CGF.Builder; |
| 1087 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1088 | "ap"); |
| 1089 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Compute if the address needs to be aligned |
| 1092 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1093 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1094 | Align = std::max(Align, 4U); |
| 1095 | if (Align > 4) { |
| 1096 | // addr = (addr + align - 1) & -align; |
| 1097 | llvm::Value *Offset = |
| 1098 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1099 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1100 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1101 | CGF.Int32Ty); |
| 1102 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1103 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1104 | Addr->getType(), |
| 1105 | "ap.cur.aligned"); |
| 1106 | } |
| 1107 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1108 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1109 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1110 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1111 | |
| 1112 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1113 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1114 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1115 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1116 | "ap.next"); |
| 1117 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1118 | |
| 1119 | return AddrTyped; |
| 1120 | } |
| 1121 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1122 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1123 | llvm::GlobalValue *GV, |
| 1124 | CodeGen::CodeGenModule &CGM) const { |
| 1125 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1126 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1127 | // Get the LLVM function. |
| 1128 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1129 | |
| 1130 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1131 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1132 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1133 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1134 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1135 | llvm::AttributeSet::FunctionIndex, |
| 1136 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1141 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1142 | CodeGen::CodeGenFunction &CGF, |
| 1143 | llvm::Value *Address) const { |
| 1144 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1146 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1147 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1148 | // 0-7 are the eight integer registers; the order is different |
| 1149 | // on Darwin (for EH), but the range is the same. |
| 1150 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1151 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1152 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1153 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1154 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1155 | // These have size 16, which is sizeof(long double) on |
| 1156 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1157 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1158 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1159 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1160 | } else { |
| 1161 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1162 | // reason. |
| 1163 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1164 | |
| 1165 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1166 | // These have size 12, which is sizeof(long double) on |
| 1167 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1168 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1169 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1170 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1171 | |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1175 | //===----------------------------------------------------------------------===// |
| 1176 | // X86-64 ABI Implementation |
| 1177 | //===----------------------------------------------------------------------===// |
| 1178 | |
| 1179 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1180 | namespace { |
| 1181 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1182 | class X86_64ABIInfo : public ABIInfo { |
| 1183 | enum Class { |
| 1184 | Integer = 0, |
| 1185 | SSE, |
| 1186 | SSEUp, |
| 1187 | X87, |
| 1188 | X87Up, |
| 1189 | ComplexX87, |
| 1190 | NoClass, |
| 1191 | Memory |
| 1192 | }; |
| 1193 | |
| 1194 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1195 | /// |
| 1196 | /// Merge an accumulating classification \arg Accum with a field |
| 1197 | /// classification \arg Field. |
| 1198 | /// |
| 1199 | /// \param Accum - The accumulating classification. This should |
| 1200 | /// always be either NoClass or the result of a previous merge |
| 1201 | /// call. In addition, this should never be Memory (the caller |
| 1202 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1203 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1204 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1205 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1206 | /// |
| 1207 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1208 | /// final MEMORY or SSE classes when necessary. |
| 1209 | /// |
| 1210 | /// \param AggregateSize - The size of the current aggregate in |
| 1211 | /// the classification process. |
| 1212 | /// |
| 1213 | /// \param Lo - The classification for the parts of the type |
| 1214 | /// residing in the low word of the containing object. |
| 1215 | /// |
| 1216 | /// \param Hi - The classification for the parts of the type |
| 1217 | /// residing in the higher words of the containing object. |
| 1218 | /// |
| 1219 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1220 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1221 | /// classify - Determine the x86_64 register classes in which the |
| 1222 | /// given type T should be passed. |
| 1223 | /// |
| 1224 | /// \param Lo - The classification for the parts of the type |
| 1225 | /// residing in the low word of the containing object. |
| 1226 | /// |
| 1227 | /// \param Hi - The classification for the parts of the type |
| 1228 | /// residing in the high word of the containing object. |
| 1229 | /// |
| 1230 | /// \param OffsetBase - The bit offset of this type in the |
| 1231 | /// containing object. Some parameters are classified different |
| 1232 | /// depending on whether they straddle an eightbyte boundary. |
| 1233 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1234 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1235 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1236 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1237 | /// If a word is unused its result will be NoClass; if a type should |
| 1238 | /// be passed in Memory then at least the classification of \arg Lo |
| 1239 | /// will be Memory. |
| 1240 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1241 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1242 | /// |
| 1243 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1244 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1245 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1246 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1247 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1248 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1249 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1250 | unsigned IROffset, QualType SourceTy, |
| 1251 | unsigned SourceOffset) const; |
| 1252 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1253 | unsigned IROffset, QualType SourceTy, |
| 1254 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1255 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1257 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1258 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1259 | |
| 1260 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1261 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1262 | /// |
| 1263 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1264 | /// available. |
| 1265 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1266 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1267 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1268 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1269 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1270 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1271 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1272 | unsigned &neededSSE, |
| 1273 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1274 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1275 | bool IsIllegalVectorType(QualType Ty) const; |
| 1276 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1277 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1278 | /// unfortunately in ways that were not always consistent with |
| 1279 | /// certain previous compilers. In particular, platforms which |
| 1280 | /// required strict binary compatibility with older versions of GCC |
| 1281 | /// may need to exempt themselves. |
| 1282 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1283 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1286 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1287 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1288 | // 64-bit hardware. |
| 1289 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1290 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1291 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1292 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1293 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1294 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1295 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1296 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1297 | bool isPassedUsingAVXType(QualType type) const { |
| 1298 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1299 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1300 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1301 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1302 | if (info.isDirect()) { |
| 1303 | llvm::Type *ty = info.getCoerceToType(); |
| 1304 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1305 | return (vectorTy->getBitWidth() > 128); |
| 1306 | } |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1310 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1311 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1312 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1313 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1314 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1316 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1317 | class WinX86_64ABIInfo : public ABIInfo { |
| 1318 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1319 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1321 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1322 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1323 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1324 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1325 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1326 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1327 | CodeGenFunction &CGF) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1328 | }; |
| 1329 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1330 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1331 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1332 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1333 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1334 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1335 | const X86_64ABIInfo &getABIInfo() const { |
| 1336 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1337 | } |
| 1338 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1339 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1340 | return 7; |
| 1341 | } |
| 1342 | |
| 1343 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1344 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1345 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1346 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1347 | // 0-15 are the 16 integer registers. |
| 1348 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1349 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1350 | return false; |
| 1351 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1352 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1353 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1354 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1355 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1356 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1357 | } |
| 1358 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1359 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1360 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1361 | // The default CC on x86-64 sets %al to the number of SSA |
| 1362 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1363 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1364 | // that when AVX types are involved: the ABI explicitly states it is |
| 1365 | // undefined, and it doesn't work in practice because of how the ABI |
| 1366 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1367 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1368 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1369 | for (CallArgList::const_iterator |
| 1370 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1371 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1372 | HasAVXType = true; |
| 1373 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1376 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1377 | if (!HasAVXType) |
| 1378 | return true; |
| 1379 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1380 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1381 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1384 | llvm::Constant * |
| 1385 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1386 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1387 | (0x0a << 8) | // .+0x0c |
| 1388 | ('F' << 16) | |
| 1389 | ('T' << 24); |
| 1390 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1391 | } |
| 1392 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1393 | }; |
| 1394 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1395 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1396 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1397 | // matches the behavior of MSVC. |
| 1398 | std::string ArgStr = Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1399 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1400 | ArgStr += ".lib"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1401 | return ArgStr; |
| 1402 | } |
| 1403 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1404 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1405 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1406 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1407 | bool d, bool p, bool w, unsigned RegParms) |
| 1408 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1409 | |
| 1410 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1411 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1412 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1413 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1414 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1415 | |
| 1416 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1417 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1418 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1419 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1420 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1421 | }; |
| 1422 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1423 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1424 | public: |
| 1425 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1426 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1427 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1428 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1429 | return 7; |
| 1430 | } |
| 1431 | |
| 1432 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1433 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1434 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1435 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1436 | // 0-15 are the 16 integer registers. |
| 1437 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1438 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1439 | return false; |
| 1440 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1441 | |
| 1442 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1443 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1444 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1445 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1446 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1447 | |
| 1448 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1449 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1450 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1451 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1452 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1453 | }; |
| 1454 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1457 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1458 | Class &Hi) const { |
| 1459 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1460 | // |
| 1461 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1462 | // memory. |
| 1463 | // |
| 1464 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1465 | // memory. |
| 1466 | // |
| 1467 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1468 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1469 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1470 | // ABI working for processors that don't support the __m256 type. |
| 1471 | // |
| 1472 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1473 | // |
| 1474 | // Some of these are enforced by the merging logic. Others can arise |
| 1475 | // only with unions; for example: |
| 1476 | // union { _Complex double; unsigned; } |
| 1477 | // |
| 1478 | // Note that clauses (b) and (c) were added in 0.98. |
| 1479 | // |
| 1480 | if (Hi == Memory) |
| 1481 | Lo = Memory; |
| 1482 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1483 | Lo = Memory; |
| 1484 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1485 | Lo = Memory; |
| 1486 | if (Hi == SSEUp && Lo != SSE) |
| 1487 | Hi = SSE; |
| 1488 | } |
| 1489 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1490 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1491 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1492 | // classified recursively so that always two fields are |
| 1493 | // considered. The resulting class is calculated according to |
| 1494 | // the classes of the fields in the eightbyte: |
| 1495 | // |
| 1496 | // (a) If both classes are equal, this is the resulting class. |
| 1497 | // |
| 1498 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1499 | // the other class. |
| 1500 | // |
| 1501 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1502 | // class. |
| 1503 | // |
| 1504 | // (d) If one of the classes is INTEGER, the result is the |
| 1505 | // INTEGER. |
| 1506 | // |
| 1507 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1508 | // MEMORY is used as class. |
| 1509 | // |
| 1510 | // (f) Otherwise class SSE is used. |
| 1511 | |
| 1512 | // Accum should never be memory (we should have returned) or |
| 1513 | // ComplexX87 (because this cannot be passed in a structure). |
| 1514 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1515 | "Invalid accumulated classification during merge."); |
| 1516 | if (Accum == Field || Field == NoClass) |
| 1517 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1518 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1519 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1520 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1521 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1522 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1523 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1524 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1525 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1526 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1527 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1530 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1531 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1532 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1533 | // Class pairs with appropriate constructor methods for the various |
| 1534 | // situations. |
| 1535 | |
| 1536 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1537 | // shouldn't be passed in registers for example, so there is no chance they |
| 1538 | // can straddle an eightbyte. Verify & simplify. |
| 1539 | |
| 1540 | Lo = Hi = NoClass; |
| 1541 | |
| 1542 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1543 | Current = Memory; |
| 1544 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1545 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1546 | BuiltinType::Kind k = BT->getKind(); |
| 1547 | |
| 1548 | if (k == BuiltinType::Void) { |
| 1549 | Current = NoClass; |
| 1550 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1551 | Lo = Integer; |
| 1552 | Hi = Integer; |
| 1553 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1554 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1555 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1556 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1557 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1558 | Current = SSE; |
| 1559 | } else if (k == BuiltinType::LongDouble) { |
| 1560 | Lo = X87; |
| 1561 | Hi = X87Up; |
| 1562 | } |
| 1563 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1564 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1568 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1569 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1570 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1574 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1575 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1578 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1579 | if (Ty->isMemberPointerType()) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1580 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1581 | Lo = Hi = Integer; |
| 1582 | else |
| 1583 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1584 | return; |
| 1585 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1587 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1588 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1589 | if (Size == 32) { |
| 1590 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1591 | // float> as integer. |
| 1592 | Current = Integer; |
| 1593 | |
| 1594 | // If this type crosses an eightbyte boundary, it should be |
| 1595 | // split. |
| 1596 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1597 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1598 | if (EB_Real != EB_Imag) |
| 1599 | Hi = Lo; |
| 1600 | } else if (Size == 64) { |
| 1601 | // gcc passes <1 x double> in memory. :( |
| 1602 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1603 | return; |
| 1604 | |
| 1605 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1606 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1607 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1608 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1609 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1610 | Current = Integer; |
| 1611 | else |
| 1612 | Current = SSE; |
| 1613 | |
| 1614 | // If this type crosses an eightbyte boundary, it should be |
| 1615 | // split. |
| 1616 | if (OffsetBase && OffsetBase != 64) |
| 1617 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1618 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1619 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1620 | // least significant one belongs to class SSE and all the others to class |
| 1621 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1622 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1623 | // This design isn't correct for 256-bits, but since there're no cases |
| 1624 | // where the upper parts would need to be inspected, avoid adding |
| 1625 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1626 | // |
| 1627 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1628 | // registers if they are "named", i.e. not part of the "..." of a |
| 1629 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1630 | Lo = SSE; |
| 1631 | Hi = SSEUp; |
| 1632 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1633 | return; |
| 1634 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1636 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1637 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1639 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1640 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1641 | if (Size <= 64) |
| 1642 | Current = Integer; |
| 1643 | else if (Size <= 128) |
| 1644 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1645 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1646 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1647 | else if (ET == getContext().DoubleTy || |
| 1648 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1649 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1650 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1651 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1652 | Current = ComplexX87; |
| 1653 | |
| 1654 | // If this complex type crosses an eightbyte boundary then it |
| 1655 | // should be split. |
| 1656 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1657 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1658 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1659 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1661 | return; |
| 1662 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1664 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1665 | // Arrays are treated like structures. |
| 1666 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1667 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1668 | |
| 1669 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1670 | // than four eightbytes, ..., it has class MEMORY. |
| 1671 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1672 | return; |
| 1673 | |
| 1674 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1675 | // fields, it has class MEMORY. |
| 1676 | // |
| 1677 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1678 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1679 | return; |
| 1680 | |
| 1681 | // Otherwise implement simplified merge. We could be smarter about |
| 1682 | // this, but it isn't worth it and would be harder to verify. |
| 1683 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1684 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1685 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1686 | |
| 1687 | // The only case a 256-bit wide vector could be used is when the array |
| 1688 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1689 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1690 | if (Size > 128 && EltSize != 256) |
| 1691 | return; |
| 1692 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1693 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1694 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1695 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1696 | Lo = merge(Lo, FieldLo); |
| 1697 | Hi = merge(Hi, FieldHi); |
| 1698 | if (Lo == Memory || Hi == Memory) |
| 1699 | break; |
| 1700 | } |
| 1701 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1702 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1703 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1704 | return; |
| 1705 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1707 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1708 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1709 | |
| 1710 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1711 | // than four eightbytes, ..., it has class MEMORY. |
| 1712 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1713 | return; |
| 1714 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1715 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1716 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1717 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1718 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1719 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1720 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1721 | const RecordDecl *RD = RT->getDecl(); |
| 1722 | |
| 1723 | // Assume variable sized types are passed in memory. |
| 1724 | if (RD->hasFlexibleArrayMember()) |
| 1725 | return; |
| 1726 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1727 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Reset Lo class, this will be recomputed. |
| 1730 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1731 | |
| 1732 | // If this is a C++ record, classify the bases first. |
| 1733 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1734 | for (const auto &I : CXXRD->bases()) { |
| 1735 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1736 | "Unexpected base class!"); |
| 1737 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1738 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Classify this field. |
| 1741 | // |
| 1742 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1743 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1744 | // initialized to class NO_CLASS. |
| 1745 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1746 | uint64_t Offset = |
| 1747 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1748 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1749 | Lo = merge(Lo, FieldLo); |
| 1750 | Hi = merge(Hi, FieldHi); |
| 1751 | if (Lo == Memory || Hi == Memory) |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1757 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1758 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1759 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1760 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1761 | bool BitField = i->isBitField(); |
| 1762 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1763 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1764 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1765 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1766 | // The only case a 256-bit wide vector could be used is when the struct |
| 1767 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1768 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1769 | // |
| 1770 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1771 | Lo = Memory; |
| 1772 | return; |
| 1773 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1774 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1775 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1776 | Lo = Memory; |
| 1777 | return; |
| 1778 | } |
| 1779 | |
| 1780 | // Classify this field. |
| 1781 | // |
| 1782 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1783 | // exceeds a single eightbyte, each is classified |
| 1784 | // separately. Each eightbyte gets initialized to class |
| 1785 | // NO_CLASS. |
| 1786 | Class FieldLo, FieldHi; |
| 1787 | |
| 1788 | // Bit-fields require special handling, they do not force the |
| 1789 | // structure to be passed in memory even if unaligned, and |
| 1790 | // therefore they can straddle an eightbyte. |
| 1791 | if (BitField) { |
| 1792 | // Ignore padding bit-fields. |
| 1793 | if (i->isUnnamedBitfield()) |
| 1794 | continue; |
| 1795 | |
| 1796 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1797 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1798 | |
| 1799 | uint64_t EB_Lo = Offset / 64; |
| 1800 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 1801 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1802 | if (EB_Lo) { |
| 1803 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1804 | FieldLo = NoClass; |
| 1805 | FieldHi = Integer; |
| 1806 | } else { |
| 1807 | FieldLo = Integer; |
| 1808 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1809 | } |
| 1810 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1811 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1812 | Lo = merge(Lo, FieldLo); |
| 1813 | Hi = merge(Hi, FieldHi); |
| 1814 | if (Lo == Memory || Hi == Memory) |
| 1815 | break; |
| 1816 | } |
| 1817 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1818 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1822 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1823 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1824 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1825 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1826 | // Treat an enum type as its underlying type. |
| 1827 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1828 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1829 | |
| 1830 | return (Ty->isPromotableIntegerType() ? |
| 1831 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1832 | } |
| 1833 | |
| 1834 | return ABIArgInfo::getIndirect(0); |
| 1835 | } |
| 1836 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1837 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1838 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1839 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1840 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1841 | if (Size <= 64 || Size > LargestVector) |
| 1842 | return true; |
| 1843 | } |
| 1844 | |
| 1845 | return false; |
| 1846 | } |
| 1847 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1848 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1849 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1850 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1851 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1852 | // |
| 1853 | // This assumption is optimistic, as there could be free registers available |
| 1854 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1855 | // the argument in the free register. This does not seem to happen currently, |
| 1856 | // but this code would be much safer if we could mark the argument with |
| 1857 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1858 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1859 | // Treat an enum type as its underlying type. |
| 1860 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1861 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1862 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1863 | return (Ty->isPromotableIntegerType() ? |
| 1864 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1865 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1866 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1867 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1868 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1870 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1871 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1872 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1875 | // is important for good codegen. |
| 1876 | // |
| 1877 | // We do this by coercing the value into a scalar type which the backend can |
| 1878 | // handle naturally (i.e., without using byval). |
| 1879 | // |
| 1880 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1881 | // free integer registers. Doing this when there are free integer registers |
| 1882 | // would require more care, as we would have to ensure that the coerced value |
| 1883 | // did not claim the unused register. That would require either reording the |
| 1884 | // arguments to the function (so that any subsequent inreg values came first), |
| 1885 | // or only doing this optimization when there were no following arguments that |
| 1886 | // might be inreg. |
| 1887 | // |
| 1888 | // We currently expect it to be rare (particularly in well written code) for |
| 1889 | // arguments to be passed on the stack when there are still free integer |
| 1890 | // registers available (this would typically imply large structs being passed |
| 1891 | // by value), so this seems like a fair tradeoff for now. |
| 1892 | // |
| 1893 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1894 | // attributes. See PR12193. |
| 1895 | if (freeIntRegs == 0) { |
| 1896 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1897 | |
| 1898 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1899 | // type, which will end up on the stack (with alignment 8). |
| 1900 | if (Align == 8 && Size <= 64) |
| 1901 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1902 | Size)); |
| 1903 | } |
| 1904 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1905 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1908 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1909 | /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1910 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1911 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1912 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1914 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1915 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1916 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1917 | while (STy && STy->getNumElements() == 1) { |
| 1918 | IRType = STy->getElementType(0); |
| 1919 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1920 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1921 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1922 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1923 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1924 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1925 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | 71f1b2d | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1926 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1927 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1928 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1929 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1930 | EltTy->isIntegerTy(128))) |
| 1931 | return VT; |
| 1932 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1933 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1934 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1935 | } |
| 1936 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1937 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1938 | /// is known to either be off the end of the specified type or being in |
| 1939 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1940 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1941 | /// classification that put one of the two halves in the INTEGER class. |
| 1942 | /// |
| 1943 | /// It is conservatively correct to return false. |
| 1944 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1945 | unsigned EndBit, ASTContext &Context) { |
| 1946 | // If the bytes being queried are off the end of the type, there is no user |
| 1947 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1948 | // types that don't contain interesting padding. |
| 1949 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1950 | if (TySize <= StartBit) |
| 1951 | return true; |
| 1952 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1953 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1954 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1955 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1956 | |
| 1957 | // Check each element to see if the element overlaps with the queried range. |
| 1958 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1959 | // If the element is after the span we care about, then we're done.. |
| 1960 | unsigned EltOffset = i*EltSize; |
| 1961 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1963 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1964 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1965 | EndBit-EltOffset, Context)) |
| 1966 | return false; |
| 1967 | } |
| 1968 | // If it overlaps no elements, then it is safe to process as padding. |
| 1969 | return true; |
| 1970 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1972 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1973 | const RecordDecl *RD = RT->getDecl(); |
| 1974 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1975 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1976 | // If this is a C++ record, check the bases first. |
| 1977 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1978 | for (const auto &I : CXXRD->bases()) { |
| 1979 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1980 | "Unexpected base class!"); |
| 1981 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1982 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1984 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1985 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1986 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1987 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1988 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1989 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1990 | EndBit-BaseOffset, Context)) |
| 1991 | return false; |
| 1992 | } |
| 1993 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1994 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1995 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1996 | // this could be sped up a lot by being smarter about queried fields, |
| 1997 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1998 | // much. |
| 1999 | unsigned idx = 0; |
| 2000 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2001 | i != e; ++i, ++idx) { |
| 2002 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2004 | // If we found a field after the region we care about, then we're done. |
| 2005 | if (FieldOffset >= EndBit) break; |
| 2006 | |
| 2007 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2008 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2009 | Context)) |
| 2010 | return false; |
| 2011 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2013 | // If nothing in this record overlapped the area of interest, then we're |
| 2014 | // clean. |
| 2015 | return true; |
| 2016 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2018 | return false; |
| 2019 | } |
| 2020 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2021 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2022 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2023 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2024 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2025 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2026 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2027 | // Base case if we find a float. |
| 2028 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2029 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2031 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2032 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2033 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2034 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2035 | IROffset -= SL->getElementOffset(Elt); |
| 2036 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2037 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2038 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2039 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2040 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2041 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2042 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2043 | IROffset -= IROffset/EltSize*EltSize; |
| 2044 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2045 | } |
| 2046 | |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2050 | |
| 2051 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2052 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2053 | llvm::Type *X86_64ABIInfo:: |
| 2054 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2055 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2056 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2057 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2058 | // structs that contain 3 floats. |
| 2059 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2060 | SourceOffset*8+64, getContext())) |
| 2061 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2063 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2064 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2065 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2066 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2067 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2068 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2070 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2071 | } |
| 2072 | |
| 2073 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2074 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2075 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2076 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2077 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2078 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2079 | /// etc). |
| 2080 | /// |
| 2081 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2082 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2083 | /// the 8-byte value references. PrefType may be null. |
| 2084 | /// |
| 2085 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 2086 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2087 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2088 | llvm::Type *X86_64ABIInfo:: |
| 2089 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2090 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2091 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2092 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2093 | if (IROffset == 0) { |
| 2094 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2095 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2096 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2097 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2098 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2099 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2100 | // goodness in the source type is just tail padding. This is allowed to |
| 2101 | // kick in for struct {double,int} on the int, but not on |
| 2102 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2103 | // have to do this analysis on the source type because we can't depend on |
| 2104 | // unions being lowered a specific way etc. |
| 2105 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2106 | IRType->isIntegerTy(32) || |
| 2107 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2108 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2109 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2111 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2112 | SourceOffset*8+64, getContext())) |
| 2113 | return IRType; |
| 2114 | } |
| 2115 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2116 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2117 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2118 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2119 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2120 | if (IROffset < SL->getSizeInBytes()) { |
| 2121 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2122 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2123 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2124 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2125 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2126 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2127 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2129 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2130 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2131 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2132 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2133 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2134 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2135 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2137 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2138 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2139 | unsigned TySizeInBytes = |
| 2140 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2141 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2142 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2144 | // It is always safe to classify this as an integer type up to i64 that |
| 2145 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2146 | return llvm::IntegerType::get(getVMContext(), |
| 2147 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2150 | |
| 2151 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2152 | /// be used as elements of a two register pair to pass or return, return a |
| 2153 | /// first class aggregate to represent them. For example, if the low part of |
| 2154 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2155 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2156 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2157 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2158 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2159 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2160 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2161 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2162 | // the second element at offset 8. Check for this: |
| 2163 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2164 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2165 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2166 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2168 | // To handle this, we have to increase the size of the low part so that the |
| 2169 | // second element will start at an 8 byte offset. We can't increase the size |
| 2170 | // of the second element because it might make us access off the end of the |
| 2171 | // struct. |
| 2172 | if (HiStart != 8) { |
| 2173 | // There are only two sorts of types the ABI generation code can produce for |
| 2174 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2175 | // Promote these to a larger type. |
| 2176 | if (Lo->isFloatTy()) |
| 2177 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2178 | else { |
| 2179 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2180 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2181 | } |
| 2182 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2183 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2184 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2185 | |
| 2186 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2187 | // Verify that the second element is at an 8-byte offset. |
| 2188 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2189 | "Invalid x86-64 argument pair!"); |
| 2190 | return Result; |
| 2191 | } |
| 2192 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2193 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2194 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2195 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2196 | // classification algorithm. |
| 2197 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2198 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2199 | |
| 2200 | // Check some invariants. |
| 2201 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2202 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2203 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2204 | llvm::Type *ResType = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2205 | switch (Lo) { |
| 2206 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2207 | if (Hi == NoClass) |
| 2208 | return ABIArgInfo::getIgnore(); |
| 2209 | // If the low part is just padding, it takes no register, leave ResType |
| 2210 | // null. |
| 2211 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2212 | "Unknown missing lo part"); |
| 2213 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2214 | |
| 2215 | case SSEUp: |
| 2216 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2217 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2218 | |
| 2219 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2220 | // hidden argument. |
| 2221 | case Memory: |
| 2222 | return getIndirectReturnResult(RetTy); |
| 2223 | |
| 2224 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2225 | // available register of the sequence %rax, %rdx is used. |
| 2226 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2227 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2229 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2230 | // so that the parameter gets the right LLVM IR attributes. |
| 2231 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2232 | // Treat an enum type as its underlying type. |
| 2233 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2234 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2235 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2236 | if (RetTy->isIntegralOrEnumerationType() && |
| 2237 | RetTy->isPromotableIntegerType()) |
| 2238 | return ABIArgInfo::getExtend(); |
| 2239 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | |
| 2242 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2243 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2244 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2245 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2246 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2247 | |
| 2248 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2249 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2250 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2251 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2252 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2253 | |
| 2254 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2255 | // part of the value is returned in %st0 and the imaginary part in |
| 2256 | // %st1. |
| 2257 | case ComplexX87: |
| 2258 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2259 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2260 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2261 | NULL); |
| 2262 | break; |
| 2263 | } |
| 2264 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2265 | llvm::Type *HighPart = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2266 | switch (Hi) { |
| 2267 | // Memory was handled previously and X87 should |
| 2268 | // never occur as a hi class. |
| 2269 | case Memory: |
| 2270 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2271 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2272 | |
| 2273 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2274 | case NoClass: |
| 2275 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2276 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2277 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2278 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2279 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2280 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2281 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2282 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2283 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2284 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2285 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2286 | break; |
| 2287 | |
| 2288 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2289 | // is passed in the next available eightbyte chunk if the last used |
| 2290 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2291 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2292 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2293 | case SSEUp: |
| 2294 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2295 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2296 | break; |
| 2297 | |
| 2298 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2299 | // returned together with the previous X87 value in %st0. |
| 2300 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2301 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2302 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2303 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2304 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2305 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2306 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2307 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2308 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2309 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2313 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2314 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2315 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2316 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2317 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2318 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2319 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2322 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2323 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2324 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2325 | const |
| 2326 | { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2327 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2328 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2329 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | // Check some invariants. |
| 2331 | // FIXME: Enforce these by construction. |
| 2332 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2333 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2334 | |
| 2335 | neededInt = 0; |
| 2336 | neededSSE = 0; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2337 | llvm::Type *ResType = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2338 | switch (Lo) { |
| 2339 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2340 | if (Hi == NoClass) |
| 2341 | return ABIArgInfo::getIgnore(); |
| 2342 | // If the low part is just padding, it takes no register, leave ResType |
| 2343 | // null. |
| 2344 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2345 | "Unknown missing lo part"); |
| 2346 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2347 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2348 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2349 | // on the stack. |
| 2350 | case Memory: |
| 2351 | |
| 2352 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2353 | // COMPLEX_X87, it is passed in memory. |
| 2354 | case X87: |
| 2355 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2356 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2357 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2358 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2359 | |
| 2360 | case SSEUp: |
| 2361 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2362 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2363 | |
| 2364 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2365 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2366 | // and %r9 is used. |
| 2367 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2368 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2369 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2370 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2371 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2372 | |
| 2373 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2374 | // so that the parameter gets the right LLVM IR attributes. |
| 2375 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2376 | // Treat an enum type as its underlying type. |
| 2377 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2378 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2379 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2380 | if (Ty->isIntegralOrEnumerationType() && |
| 2381 | Ty->isPromotableIntegerType()) |
| 2382 | return ABIArgInfo::getExtend(); |
| 2383 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2384 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | |
| 2387 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2388 | // available SSE register is used, the registers are taken in the |
| 2389 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2390 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2391 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2392 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2393 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2396 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2398 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2399 | switch (Hi) { |
| 2400 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2401 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2402 | // which is passed in memory. |
| 2403 | case Memory: |
| 2404 | case X87: |
| 2405 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2406 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2407 | |
| 2408 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2410 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2411 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2412 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2413 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2415 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2416 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2417 | break; |
| 2418 | |
| 2419 | // X87Up generally doesn't occur here (long double is passed in |
| 2420 | // memory), except in situations involving unions. |
| 2421 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2422 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2423 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2425 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2426 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2427 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2428 | ++neededSSE; |
| 2429 | break; |
| 2430 | |
| 2431 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2432 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2433 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2434 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2435 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2436 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2437 | break; |
| 2438 | } |
| 2439 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2440 | // If a high part was specified, merge it together with the low part. It is |
| 2441 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2442 | // first class struct aggregate with the high and low part: {low, high} |
| 2443 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2444 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2446 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2449 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2451 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2452 | |
| 2453 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2454 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2455 | |
| 2456 | // If the return value is indirect, then the hidden argument is consuming one |
| 2457 | // integer register. |
| 2458 | if (FI.getReturnInfo().isIndirect()) |
| 2459 | --freeIntRegs; |
| 2460 | |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2461 | bool isVariadic = FI.isVariadic(); |
| 2462 | unsigned numRequiredArgs = 0; |
| 2463 | if (isVariadic) |
| 2464 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2465 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2466 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2467 | // get assigned (in left-to-right order) for passing as follows... |
| 2468 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2469 | it != ie; ++it) { |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2470 | bool isNamedArg = true; |
| 2471 | if (isVariadic) |
Aaron Ballman | 6a30264 | 2013-06-12 15:03:45 +0000 | [diff] [blame] | 2472 | isNamedArg = (it - FI.arg_begin()) < |
| 2473 | static_cast<signed>(numRequiredArgs); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2474 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2475 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2476 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2477 | neededSSE, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2478 | |
| 2479 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2480 | // eightbyte of an argument, the whole argument is passed on the |
| 2481 | // stack. If registers have already been assigned for some |
| 2482 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2483 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2484 | freeIntRegs -= neededInt; |
| 2485 | freeSSERegs -= neededSSE; |
| 2486 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2487 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2488 | } |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2493 | QualType Ty, |
| 2494 | CodeGenFunction &CGF) { |
| 2495 | llvm::Value *overflow_arg_area_p = |
| 2496 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2497 | llvm::Value *overflow_arg_area = |
| 2498 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2499 | |
| 2500 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2501 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2502 | // It isn't stated explicitly in the standard, but in practice we use |
| 2503 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2504 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2505 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2506 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2507 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2508 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2509 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2510 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2511 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2512 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2513 | overflow_arg_area = |
| 2514 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2515 | overflow_arg_area->getType(), |
| 2516 | "overflow_arg_area.align"); |
| 2517 | } |
| 2518 | |
| 2519 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2520 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2521 | llvm::Value *Res = |
| 2522 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2523 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2524 | |
| 2525 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2526 | // l->overflow_arg_area + sizeof(type). |
| 2527 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2528 | // an 8 byte boundary. |
| 2529 | |
| 2530 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2531 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2532 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2533 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2534 | "overflow_arg_area.next"); |
| 2535 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2536 | |
| 2537 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2538 | return Res; |
| 2539 | } |
| 2540 | |
| 2541 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2542 | CodeGenFunction &CGF) const { |
| 2543 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2544 | // struct { |
| 2545 | // i32 gp_offset; |
| 2546 | // i32 fp_offset; |
| 2547 | // i8* overflow_arg_area; |
| 2548 | // i8* reg_save_area; |
| 2549 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2550 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2552 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2553 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2554 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2555 | |
| 2556 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2557 | // in the registers. If not go to step 7. |
| 2558 | if (!neededInt && !neededSSE) |
| 2559 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2560 | |
| 2561 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2562 | // general purpose registers needed to pass type and num_fp to hold |
| 2563 | // the number of floating point registers needed. |
| 2564 | |
| 2565 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2566 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2567 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2568 | // |
| 2569 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2570 | // register save space). |
| 2571 | |
| 2572 | llvm::Value *InRegs = 0; |
| 2573 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2574 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2575 | if (neededInt) { |
| 2576 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2577 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2578 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2579 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | if (neededSSE) { |
| 2583 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2584 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2585 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2586 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2587 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2588 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2589 | } |
| 2590 | |
| 2591 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2592 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2593 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2594 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2595 | |
| 2596 | // Emit code to load the value if it was passed in registers. |
| 2597 | |
| 2598 | CGF.EmitBlock(InRegBlock); |
| 2599 | |
| 2600 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2601 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2602 | // copying to a temporary location in case the parameter is passed |
| 2603 | // in different register classes or requires an alignment greater |
| 2604 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2605 | // |
| 2606 | // FIXME: This really results in shameful code when we end up needing to |
| 2607 | // collect arguments from different places; often what should result in a |
| 2608 | // simple assembling of a structure from scattered addresses has many more |
| 2609 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2610 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2611 | llvm::Value *RegAddr = |
| 2612 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2613 | "reg_save_area"); |
| 2614 | if (neededInt && neededSSE) { |
| 2615 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2616 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2617 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2618 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2619 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2620 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2621 | llvm::Type *TyLo = ST->getElementType(0); |
| 2622 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2623 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2624 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2625 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2626 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2628 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | 998f9d9 | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2629 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2630 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2631 | llvm::Value *V = |
| 2632 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2633 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2634 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2635 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2636 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2637 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2638 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2639 | } else if (neededInt) { |
| 2640 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2641 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2642 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2643 | |
| 2644 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2645 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2646 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2647 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2648 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2649 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2650 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2651 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2652 | RegAddr = Tmp; |
| 2653 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2654 | } else if (neededSSE == 1) { |
| 2655 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2656 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2657 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2658 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2659 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2660 | // SSE registers are spaced 16 bytes apart in the register save |
| 2661 | // area, we need to collect the two eightbytes together. |
| 2662 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2663 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2664 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2665 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2666 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2667 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2668 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2669 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2670 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2671 | DblPtrTy)); |
| 2672 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2673 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2674 | DblPtrTy)); |
| 2675 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2676 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2677 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2681 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2682 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2683 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2684 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2685 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2686 | gp_offset_p); |
| 2687 | } |
| 2688 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2689 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2691 | fp_offset_p); |
| 2692 | } |
| 2693 | CGF.EmitBranch(ContBlock); |
| 2694 | |
| 2695 | // Emit code to load the value if it was passed in memory. |
| 2696 | |
| 2697 | CGF.EmitBlock(InMemBlock); |
| 2698 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2699 | |
| 2700 | // Return the appropriate result. |
| 2701 | |
| 2702 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2703 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2705 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2706 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2707 | return ResAddr; |
| 2708 | } |
| 2709 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2710 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2711 | |
| 2712 | if (Ty->isVoidType()) |
| 2713 | return ABIArgInfo::getIgnore(); |
| 2714 | |
| 2715 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2716 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2717 | |
| 2718 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2719 | |
| 2720 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2721 | if (IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2722 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2723 | return ABIArgInfo::getIndirect(0, false); |
| 2724 | } else { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2725 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2726 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2727 | } |
| 2728 | |
| 2729 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2730 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2731 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2732 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 2733 | if (Size == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2734 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2735 | Size)); |
| 2736 | |
| 2737 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2738 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2739 | if (Size <= 64 && |
NAKAMURA Takumi | e03c603 | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2740 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2741 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2742 | Size)); |
| 2743 | |
| 2744 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2745 | } |
| 2746 | |
| 2747 | if (Ty->isPromotableIntegerType()) |
| 2748 | return ABIArgInfo::getExtend(); |
| 2749 | |
| 2750 | return ABIArgInfo::getDirect(); |
| 2751 | } |
| 2752 | |
| 2753 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2754 | |
| 2755 | QualType RetTy = FI.getReturnType(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2756 | FI.getReturnInfo() = classify(RetTy, true); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2757 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2758 | for (auto &I : FI.arguments()) |
| 2759 | I.info = classify(I.type, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2762 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2763 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2764 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2765 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2766 | CGBuilderTy &Builder = CGF.Builder; |
| 2767 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2768 | "ap"); |
| 2769 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2770 | llvm::Type *PTy = |
| 2771 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2772 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2773 | |
| 2774 | uint64_t Offset = |
| 2775 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2776 | llvm::Value *NextAddr = |
| 2777 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2778 | "ap.next"); |
| 2779 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2780 | |
| 2781 | return AddrTyped; |
| 2782 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2783 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2784 | namespace { |
| 2785 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2786 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2787 | public: |
| 2788 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2789 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2790 | void computeInfo(CGFunctionInfo &FI) const override; |
| 2791 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2792 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2793 | private: |
| 2794 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2795 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2796 | }; |
| 2797 | |
| 2798 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2799 | public: |
| 2800 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2801 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2802 | }; |
| 2803 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2806 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2807 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2808 | PInfo.computeInfo(FI); |
| 2809 | else |
| 2810 | NInfo.computeInfo(FI); |
| 2811 | } |
| 2812 | |
| 2813 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2814 | CodeGenFunction &CGF) const { |
| 2815 | // Always use the native convention; calling pnacl-style varargs functions |
| 2816 | // is unuspported. |
| 2817 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2818 | } |
| 2819 | |
| 2820 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2821 | // PowerPC-32 |
| 2822 | |
| 2823 | namespace { |
| 2824 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2825 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2826 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2827 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2828 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2829 | // This is recovered from gcc output. |
| 2830 | return 1; // r1 is the dedicated stack pointer |
| 2831 | } |
| 2832 | |
| 2833 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2834 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2835 | }; |
| 2836 | |
| 2837 | } |
| 2838 | |
| 2839 | bool |
| 2840 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2841 | llvm::Value *Address) const { |
| 2842 | // This is calculated from the LLVM and GCC tables and verified |
| 2843 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2844 | |
| 2845 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2847 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2848 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2849 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2850 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2851 | |
| 2852 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2853 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2854 | |
| 2855 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2856 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2857 | |
| 2858 | // 64-76 are various 4-byte special-purpose registers: |
| 2859 | // 64: mq |
| 2860 | // 65: lr |
| 2861 | // 66: ctr |
| 2862 | // 67: ap |
| 2863 | // 68-75 cr0-7 |
| 2864 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2865 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2866 | |
| 2867 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2868 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2869 | |
| 2870 | // 109: vrsave |
| 2871 | // 110: vscr |
| 2872 | // 111: spe_acc |
| 2873 | // 112: spefscr |
| 2874 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2875 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2876 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2877 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2880 | // PowerPC-64 |
| 2881 | |
| 2882 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2883 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2884 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2885 | |
| 2886 | public: |
| 2887 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2888 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2889 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2890 | |
| 2891 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2892 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2893 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2894 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2895 | // Example: For aggregate arguments that fit in a register, we could |
| 2896 | // use getDirectInReg (as is done below for structs containing a single |
| 2897 | // floating-point value) to avoid pushing them to memory on function |
| 2898 | // entry. This would require changing the logic in PPCISelLowering |
| 2899 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2900 | void computeInfo(CGFunctionInfo &FI) const override { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2901 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2902 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2903 | // We rely on the default argument classification for the most part. |
| 2904 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2905 | // or vector item must be passed in a register if one is available. |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2906 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2907 | if (T) { |
| 2908 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2909 | if (T->isVectorType() || (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2910 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2911 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2912 | continue; |
| 2913 | } |
| 2914 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2915 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2918 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2919 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2920 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2921 | }; |
| 2922 | |
| 2923 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2924 | public: |
| 2925 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2926 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2927 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2928 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2929 | // This is recovered from gcc output. |
| 2930 | return 1; // r1 is the dedicated stack pointer |
| 2931 | } |
| 2932 | |
| 2933 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2934 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2935 | }; |
| 2936 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2937 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2938 | public: |
| 2939 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2940 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2941 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2942 | // This is recovered from gcc output. |
| 2943 | return 1; // r1 is the dedicated stack pointer |
| 2944 | } |
| 2945 | |
| 2946 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2947 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2948 | }; |
| 2949 | |
| 2950 | } |
| 2951 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2952 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2953 | // extended to 64 bits. |
| 2954 | bool |
| 2955 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2956 | // Treat an enum type as its underlying type. |
| 2957 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2958 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2959 | |
| 2960 | // Promotable integer types are required to be promoted by the ABI. |
| 2961 | if (Ty->isPromotableIntegerType()) |
| 2962 | return true; |
| 2963 | |
| 2964 | // In addition to the usual promotable integer types, we also need to |
| 2965 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2966 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2967 | switch (BT->getKind()) { |
| 2968 | case BuiltinType::Int: |
| 2969 | case BuiltinType::UInt: |
| 2970 | return true; |
| 2971 | default: |
| 2972 | break; |
| 2973 | } |
| 2974 | |
| 2975 | return false; |
| 2976 | } |
| 2977 | |
| 2978 | ABIArgInfo |
| 2979 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 2980 | if (Ty->isAnyComplexType()) |
| 2981 | return ABIArgInfo::getDirect(); |
| 2982 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2983 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2984 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2985 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2986 | |
| 2987 | return ABIArgInfo::getIndirect(0); |
| 2988 | } |
| 2989 | |
| 2990 | return (isPromotableTypeForABI(Ty) ? |
| 2991 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2992 | } |
| 2993 | |
| 2994 | ABIArgInfo |
| 2995 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 2996 | if (RetTy->isVoidType()) |
| 2997 | return ABIArgInfo::getIgnore(); |
| 2998 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 2999 | if (RetTy->isAnyComplexType()) |
| 3000 | return ABIArgInfo::getDirect(); |
| 3001 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3002 | if (isAggregateTypeForABI(RetTy)) |
| 3003 | return ABIArgInfo::getIndirect(0); |
| 3004 | |
| 3005 | return (isPromotableTypeForABI(RetTy) ? |
| 3006 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3007 | } |
| 3008 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3009 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3010 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3011 | QualType Ty, |
| 3012 | CodeGenFunction &CGF) const { |
| 3013 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3014 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3015 | |
| 3016 | CGBuilderTy &Builder = CGF.Builder; |
| 3017 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3018 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3019 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3020 | // Update the va_list pointer. The pointer should be bumped by the |
| 3021 | // size of the object. We can trust getTypeSize() except for a complex |
| 3022 | // type whose base type is smaller than a doubleword. For these, the |
| 3023 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3024 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3025 | QualType BaseTy; |
| 3026 | unsigned CplxBaseSize = 0; |
| 3027 | |
| 3028 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3029 | BaseTy = CTy->getElementType(); |
| 3030 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3031 | if (CplxBaseSize < 8) |
| 3032 | SizeInBytes = 16; |
| 3033 | } |
| 3034 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3035 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3036 | llvm::Value *NextAddr = |
| 3037 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3038 | "ap.next"); |
| 3039 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3040 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3041 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3042 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3043 | // in separate doublewords. However, Clang expects us to produce a |
| 3044 | // pointer to a structure with the two parts packed tightly. So generate |
| 3045 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3046 | // and store them to a temporary structure. |
| 3047 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3048 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3049 | llvm::Value *ImagAddr = RealAddr; |
| 3050 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3051 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3052 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3053 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3054 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3055 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3056 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3057 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3058 | "vacplx"); |
| 3059 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3060 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3061 | Builder.CreateStore(Real, RealPtr, false); |
| 3062 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3063 | return Ptr; |
| 3064 | } |
| 3065 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3066 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3067 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3068 | // correct offset. |
| 3069 | if (SizeInBytes < 8) { |
| 3070 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3071 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3072 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3073 | } |
| 3074 | |
| 3075 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3076 | return Builder.CreateBitCast(Addr, PTy); |
| 3077 | } |
| 3078 | |
| 3079 | static bool |
| 3080 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3081 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3082 | // This is calculated from the LLVM and GCC tables and verified |
| 3083 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3084 | |
| 3085 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3086 | |
| 3087 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3088 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3089 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3090 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3091 | |
| 3092 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3093 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3094 | |
| 3095 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3096 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3097 | |
| 3098 | // 64-76 are various 4-byte special-purpose registers: |
| 3099 | // 64: mq |
| 3100 | // 65: lr |
| 3101 | // 66: ctr |
| 3102 | // 67: ap |
| 3103 | // 68-75 cr0-7 |
| 3104 | // 76: xer |
| 3105 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3106 | |
| 3107 | // 77-108: v0-31, the 16-byte vector registers |
| 3108 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3109 | |
| 3110 | // 109: vrsave |
| 3111 | // 110: vscr |
| 3112 | // 111: spe_acc |
| 3113 | // 112: spefscr |
| 3114 | // 113: sfp |
| 3115 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3116 | |
| 3117 | return false; |
| 3118 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3119 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3120 | bool |
| 3121 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3122 | CodeGen::CodeGenFunction &CGF, |
| 3123 | llvm::Value *Address) const { |
| 3124 | |
| 3125 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3126 | } |
| 3127 | |
| 3128 | bool |
| 3129 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3130 | llvm::Value *Address) const { |
| 3131 | |
| 3132 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3133 | } |
| 3134 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3135 | //===----------------------------------------------------------------------===// |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3136 | // ARM64 ABI Implementation |
| 3137 | //===----------------------------------------------------------------------===// |
| 3138 | |
| 3139 | namespace { |
| 3140 | |
| 3141 | class ARM64ABIInfo : public ABIInfo { |
| 3142 | public: |
| 3143 | enum ABIKind { |
| 3144 | AAPCS = 0, |
| 3145 | DarwinPCS |
| 3146 | }; |
| 3147 | |
| 3148 | private: |
| 3149 | ABIKind Kind; |
| 3150 | |
| 3151 | public: |
| 3152 | ARM64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
| 3153 | |
| 3154 | private: |
| 3155 | ABIKind getABIKind() const { return Kind; } |
| 3156 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3157 | |
| 3158 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3159 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &AllocatedVFP, |
| 3160 | bool &IsHA, unsigned &AllocatedGPR, |
| 3161 | bool &IsSmallAggr) const; |
| 3162 | bool isIllegalVectorType(QualType Ty) const; |
| 3163 | |
| 3164 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 3165 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
| 3166 | // number of SIMD and Floating-point registers allocated so far. |
| 3167 | // If the argument is an HFA or an HVA and there are sufficient unallocated |
| 3168 | // SIMD and Floating-point registers, then the argument is allocated to SIMD |
| 3169 | // and Floating-point Registers (with one register per member of the HFA or |
| 3170 | // HVA). Otherwise, the NSRN is set to 8. |
| 3171 | unsigned AllocatedVFP = 0; |
| 3172 | // To correctly handle small aggregates, we need to keep track of the number |
| 3173 | // of GPRs allocated so far. If the small aggregate can't all fit into |
| 3174 | // registers, it will be on stack. We don't allow the aggregate to be |
| 3175 | // partially in registers. |
| 3176 | unsigned AllocatedGPR = 0; |
| 3177 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3178 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3179 | it != ie; ++it) { |
| 3180 | unsigned PreAllocation = AllocatedVFP, PreGPR = AllocatedGPR; |
| 3181 | bool IsHA = false, IsSmallAggr = false; |
| 3182 | const unsigned NumVFPs = 8; |
| 3183 | const unsigned NumGPRs = 8; |
| 3184 | it->info = classifyArgumentType(it->type, AllocatedVFP, IsHA, |
| 3185 | AllocatedGPR, IsSmallAggr); |
| 3186 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3187 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3188 | // padding of (NumVFPs - PreAllocation) floats. |
| 3189 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3190 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3191 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
| 3192 | if (isDarwinPCS()) |
| 3193 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3194 | else { |
| 3195 | // Under AAPCS the 64-bit stack slot alignment means we can't pass HAs |
| 3196 | // as sequences of floats since they'll get "holes" inserted as |
| 3197 | // padding by the back end. |
| 3198 | uint32_t NumStackSlots = getContext().getTypeSize(it->type); |
| 3199 | NumStackSlots = llvm::RoundUpToAlignment(NumStackSlots, 64) / 64; |
| 3200 | |
| 3201 | llvm::Type *CoerceTy = llvm::ArrayType::get( |
| 3202 | llvm::Type::getDoubleTy(getVMContext()), NumStackSlots); |
| 3203 | it->info = ABIArgInfo::getDirect(CoerceTy, 0, PaddingTy); |
| 3204 | } |
| 3205 | } |
| 3206 | // If we do not have enough GPRs for the small aggregate, any GPR regs |
| 3207 | // that are unallocated are marked as unavailable. |
| 3208 | if (IsSmallAggr && AllocatedGPR > NumGPRs && PreGPR < NumGPRs) { |
| 3209 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3210 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreGPR); |
| 3211 | it->info = |
| 3212 | ABIArgInfo::getDirect(it->info.getCoerceToType(), 0, PaddingTy); |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3218 | CodeGenFunction &CGF) const; |
| 3219 | |
| 3220 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3221 | CodeGenFunction &CGF) const; |
| 3222 | |
| 3223 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3224 | CodeGenFunction &CGF) const { |
| 3225 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3226 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3227 | } |
| 3228 | }; |
| 3229 | |
| 3230 | class ARM64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3231 | public: |
| 3232 | ARM64TargetCodeGenInfo(CodeGenTypes &CGT, ARM64ABIInfo::ABIKind Kind) |
| 3233 | : TargetCodeGenInfo(new ARM64ABIInfo(CGT, Kind)) {} |
| 3234 | |
| 3235 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3236 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3237 | } |
| 3238 | |
| 3239 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3240 | |
| 3241 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3242 | }; |
| 3243 | } |
| 3244 | |
| 3245 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3246 | ASTContext &Context, |
| 3247 | uint64_t *HAMembers = 0); |
| 3248 | |
| 3249 | ABIArgInfo ARM64ABIInfo::classifyArgumentType(QualType Ty, |
| 3250 | unsigned &AllocatedVFP, |
| 3251 | bool &IsHA, |
| 3252 | unsigned &AllocatedGPR, |
| 3253 | bool &IsSmallAggr) const { |
| 3254 | // Handle illegal vector types here. |
| 3255 | if (isIllegalVectorType(Ty)) { |
| 3256 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3257 | if (Size <= 32) { |
| 3258 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
| 3259 | AllocatedGPR++; |
| 3260 | return ABIArgInfo::getDirect(ResType); |
| 3261 | } |
| 3262 | if (Size == 64) { |
| 3263 | llvm::Type *ResType = |
| 3264 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
| 3265 | AllocatedVFP++; |
| 3266 | return ABIArgInfo::getDirect(ResType); |
| 3267 | } |
| 3268 | if (Size == 128) { |
| 3269 | llvm::Type *ResType = |
| 3270 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
| 3271 | AllocatedVFP++; |
| 3272 | return ABIArgInfo::getDirect(ResType); |
| 3273 | } |
| 3274 | AllocatedGPR++; |
| 3275 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3276 | } |
| 3277 | if (Ty->isVectorType()) |
| 3278 | // Size of a legal vector should be either 64 or 128. |
| 3279 | AllocatedVFP++; |
| 3280 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3281 | if (BT->getKind() == BuiltinType::Half || |
| 3282 | BT->getKind() == BuiltinType::Float || |
| 3283 | BT->getKind() == BuiltinType::Double || |
| 3284 | BT->getKind() == BuiltinType::LongDouble) |
| 3285 | AllocatedVFP++; |
| 3286 | } |
| 3287 | |
| 3288 | if (!isAggregateTypeForABI(Ty)) { |
| 3289 | // Treat an enum type as its underlying type. |
| 3290 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3291 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3292 | |
| 3293 | if (!Ty->isFloatingType() && !Ty->isVectorType()) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3294 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3295 | if (!isDarwinPCS() && Alignment > 64) |
| 3296 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3297 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3298 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3299 | AllocatedGPR += RegsNeeded; |
| 3300 | } |
| 3301 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3302 | ? ABIArgInfo::getExtend() |
| 3303 | : ABIArgInfo::getDirect()); |
| 3304 | } |
| 3305 | |
| 3306 | // Structures with either a non-trivial destructor or a non-trivial |
| 3307 | // copy constructor are always indirect. |
| 3308 | if (isRecordReturnIndirect(Ty, getCXXABI())) { |
| 3309 | AllocatedGPR++; |
| 3310 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3311 | } |
| 3312 | |
| 3313 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3314 | // elsewhere for GNU compatibility. |
| 3315 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3316 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3317 | return ABIArgInfo::getIgnore(); |
| 3318 | |
| 3319 | ++AllocatedGPR; |
| 3320 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 3321 | } |
| 3322 | |
| 3323 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
| 3324 | const Type *Base = 0; |
| 3325 | uint64_t Members = 0; |
| 3326 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
| 3327 | AllocatedVFP += Members; |
| 3328 | IsHA = true; |
| 3329 | return ABIArgInfo::getExpand(); |
| 3330 | } |
| 3331 | |
| 3332 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 3333 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3334 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3335 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3336 | if (!isDarwinPCS() && Alignment > 64) |
| 3337 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3338 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3339 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3340 | AllocatedGPR += Size / 64; |
| 3341 | IsSmallAggr = true; |
| 3342 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 3343 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3344 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3345 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3346 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3347 | } |
| 3348 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3349 | } |
| 3350 | |
| 3351 | AllocatedGPR++; |
| 3352 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3353 | } |
| 3354 | |
| 3355 | ABIArgInfo ARM64ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3356 | if (RetTy->isVoidType()) |
| 3357 | return ABIArgInfo::getIgnore(); |
| 3358 | |
| 3359 | // Large vector types should be returned via memory. |
| 3360 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3361 | return ABIArgInfo::getIndirect(0); |
| 3362 | |
| 3363 | if (!isAggregateTypeForABI(RetTy)) { |
| 3364 | // Treat an enum type as its underlying type. |
| 3365 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3366 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3367 | |
| 3368 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 3369 | : ABIArgInfo::getDirect()); |
| 3370 | } |
| 3371 | |
| 3372 | // Structures with either a non-trivial destructor or a non-trivial |
| 3373 | // copy constructor are always indirect. |
| 3374 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
| 3375 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3376 | |
| 3377 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 3378 | return ABIArgInfo::getIgnore(); |
| 3379 | |
| 3380 | const Type *Base = 0; |
| 3381 | if (isHomogeneousAggregate(RetTy, Base, getContext())) |
| 3382 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 3383 | return ABIArgInfo::getDirect(); |
| 3384 | |
| 3385 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 3386 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3387 | if (Size <= 128) { |
| 3388 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3389 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3390 | } |
| 3391 | |
| 3392 | return ABIArgInfo::getIndirect(0); |
| 3393 | } |
| 3394 | |
| 3395 | /// isIllegalVectorType - check whether the vector type is legal for ARM64. |
| 3396 | bool ARM64ABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3397 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3398 | // Check whether VT is legal. |
| 3399 | unsigned NumElements = VT->getNumElements(); |
| 3400 | uint64_t Size = getContext().getTypeSize(VT); |
| 3401 | // NumElements should be power of 2 between 1 and 16. |
| 3402 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 3403 | return true; |
| 3404 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 3405 | } |
| 3406 | return false; |
| 3407 | } |
| 3408 | |
| 3409 | static llvm::Value *EmitAArch64VAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3410 | int AllocatedGPR, int AllocatedVFP, |
| 3411 | bool IsIndirect, CodeGenFunction &CGF) { |
| 3412 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3413 | // Standard, section B.4: |
| 3414 | // |
| 3415 | // struct { |
| 3416 | // void *__stack; |
| 3417 | // void *__gr_top; |
| 3418 | // void *__vr_top; |
| 3419 | // int __gr_offs; |
| 3420 | // int __vr_offs; |
| 3421 | // }; |
| 3422 | |
| 3423 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3424 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3425 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3426 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3427 | auto &Ctx = CGF.getContext(); |
| 3428 | |
| 3429 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 3430 | int reg_top_index; |
| 3431 | int RegSize; |
| 3432 | if (AllocatedGPR) { |
| 3433 | assert(!AllocatedVFP && "Arguments never split between int & VFP regs"); |
| 3434 | // 3 is the field number of __gr_offs |
| 3435 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3436 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3437 | reg_top_index = 1; // field number for __gr_top |
| 3438 | RegSize = 8 * AllocatedGPR; |
| 3439 | } else { |
| 3440 | assert(!AllocatedGPR && "Argument must go in VFP or int regs"); |
| 3441 | // 4 is the field number of __vr_offs. |
| 3442 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3443 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3444 | reg_top_index = 2; // field number for __vr_top |
| 3445 | RegSize = 16 * AllocatedVFP; |
| 3446 | } |
| 3447 | |
| 3448 | //======================================= |
| 3449 | // Find out where argument was passed |
| 3450 | //======================================= |
| 3451 | |
| 3452 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3453 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3454 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3455 | // whatever they get). |
| 3456 | llvm::Value *UsingStack = 0; |
| 3457 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 3458 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3459 | |
| 3460 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3461 | |
| 3462 | // Otherwise, at least some kind of argument could go in these registers, the |
| 3463 | // quesiton is whether this particular type is too big. |
| 3464 | CGF.EmitBlock(MaybeRegBlock); |
| 3465 | |
| 3466 | // Integer arguments may need to correct register alignment (for example a |
| 3467 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3468 | // align __gr_offs to calculate the potential address. |
| 3469 | if (AllocatedGPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3470 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3471 | |
| 3472 | reg_offs = CGF.Builder.CreateAdd( |
| 3473 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3474 | "align_regoffs"); |
| 3475 | reg_offs = CGF.Builder.CreateAnd( |
| 3476 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3477 | "aligned_regoffs"); |
| 3478 | } |
| 3479 | |
| 3480 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 3481 | llvm::Value *NewOffset = 0; |
| 3482 | NewOffset = CGF.Builder.CreateAdd( |
| 3483 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 3484 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3485 | |
| 3486 | // Now we're in a position to decide whether this argument really was in |
| 3487 | // registers or not. |
| 3488 | llvm::Value *InRegs = 0; |
| 3489 | InRegs = CGF.Builder.CreateICmpSLE( |
| 3490 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 3491 | |
| 3492 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3493 | |
| 3494 | //======================================= |
| 3495 | // Argument was in registers |
| 3496 | //======================================= |
| 3497 | |
| 3498 | // Now we emit the code for if the argument was originally passed in |
| 3499 | // registers. First start the appropriate block: |
| 3500 | CGF.EmitBlock(InRegBlock); |
| 3501 | |
| 3502 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 3503 | reg_top_p = |
| 3504 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3505 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3506 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 3507 | llvm::Value *RegAddr = 0; |
| 3508 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 3509 | |
| 3510 | if (IsIndirect) { |
| 3511 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 3512 | // stored registers or on the stack will actually be a struct **. |
| 3513 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 3514 | } |
| 3515 | |
| 3516 | const Type *Base = 0; |
| 3517 | uint64_t NumMembers; |
| 3518 | if (isHomogeneousAggregate(Ty, Base, Ctx, &NumMembers) && NumMembers > 1) { |
| 3519 | // Homogeneous aggregates passed in registers will have their elements split |
| 3520 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 3521 | // qN+1, ...). We reload and store into a temporary local variable |
| 3522 | // contiguously. |
| 3523 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 3524 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 3525 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 3526 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 3527 | int Offset = 0; |
| 3528 | |
| 3529 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 3530 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 3531 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 3532 | llvm::Value *BaseOffset = |
| 3533 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 3534 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 3535 | LoadAddr = CGF.Builder.CreateBitCast( |
| 3536 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 3537 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 3538 | |
| 3539 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 3540 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 3541 | } |
| 3542 | |
| 3543 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 3544 | } else { |
| 3545 | // Otherwise the object is contiguous in memory |
| 3546 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
| 3547 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 3548 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 3549 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 3550 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 3551 | |
| 3552 | BaseAddr = CGF.Builder.CreateAdd( |
| 3553 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3554 | |
| 3555 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 3556 | } |
| 3557 | |
| 3558 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 3559 | } |
| 3560 | |
| 3561 | CGF.EmitBranch(ContBlock); |
| 3562 | |
| 3563 | //======================================= |
| 3564 | // Argument was on the stack |
| 3565 | //======================================= |
| 3566 | CGF.EmitBlock(OnStackBlock); |
| 3567 | |
| 3568 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 3569 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 3570 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 3571 | |
| 3572 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 3573 | // floating-point ones might be affected. |
| 3574 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3575 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3576 | |
| 3577 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3578 | |
| 3579 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3580 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 3581 | "align_stack"); |
| 3582 | OnStackAddr = CGF.Builder.CreateAnd( |
| 3583 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 3584 | "align_stack"); |
| 3585 | |
| 3586 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3587 | } |
| 3588 | |
| 3589 | uint64_t StackSize; |
| 3590 | if (IsIndirect) |
| 3591 | StackSize = 8; |
| 3592 | else |
| 3593 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 3594 | |
| 3595 | // All stack slots are 8 bytes |
| 3596 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 3597 | |
| 3598 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 3599 | llvm::Value *NewStack = |
| 3600 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 3601 | |
| 3602 | // Write the new value of __stack for the next call to va_arg |
| 3603 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 3604 | |
| 3605 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 3606 | Ctx.getTypeSize(Ty) < 64) { |
| 3607 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 3608 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3609 | |
| 3610 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3611 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3612 | |
| 3613 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3614 | } |
| 3615 | |
| 3616 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 3617 | |
| 3618 | CGF.EmitBranch(ContBlock); |
| 3619 | |
| 3620 | //======================================= |
| 3621 | // Tidy up |
| 3622 | //======================================= |
| 3623 | CGF.EmitBlock(ContBlock); |
| 3624 | |
| 3625 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 3626 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3627 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 3628 | |
| 3629 | if (IsIndirect) |
| 3630 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 3631 | |
| 3632 | return ResAddr; |
| 3633 | } |
| 3634 | |
| 3635 | llvm::Value *ARM64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3636 | CodeGenFunction &CGF) const { |
| 3637 | |
| 3638 | unsigned AllocatedGPR = 0, AllocatedVFP = 0; |
| 3639 | bool IsHA = false, IsSmallAggr = false; |
| 3640 | ABIArgInfo AI = |
| 3641 | classifyArgumentType(Ty, AllocatedVFP, IsHA, AllocatedGPR, IsSmallAggr); |
| 3642 | |
| 3643 | return EmitAArch64VAArg(VAListAddr, Ty, AllocatedGPR, AllocatedVFP, |
| 3644 | AI.isIndirect(), CGF); |
| 3645 | } |
| 3646 | |
| 3647 | llvm::Value *ARM64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3648 | CodeGenFunction &CGF) const { |
| 3649 | // We do not support va_arg for aggregates or illegal vector types. |
| 3650 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 3651 | // other cases. |
| 3652 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
| 3653 | return 0; |
| 3654 | |
| 3655 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 3656 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 3657 | |
| 3658 | const Type *Base = 0; |
| 3659 | bool isHA = isHomogeneousAggregate(Ty, Base, getContext()); |
| 3660 | |
| 3661 | bool isIndirect = false; |
| 3662 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 3663 | // be passed indirectly. |
| 3664 | if (Size > 16 && !isHA) { |
| 3665 | isIndirect = true; |
| 3666 | Size = 8; |
| 3667 | Align = 8; |
| 3668 | } |
| 3669 | |
| 3670 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3671 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 3672 | |
| 3673 | CGBuilderTy &Builder = CGF.Builder; |
| 3674 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3675 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3676 | |
| 3677 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3678 | // These are ignored for parameter passing purposes. |
| 3679 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3680 | return Builder.CreateBitCast(Addr, PTy); |
| 3681 | } |
| 3682 | |
| 3683 | const uint64_t MinABIAlign = 8; |
| 3684 | if (Align > MinABIAlign) { |
| 3685 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 3686 | Addr = Builder.CreateGEP(Addr, Offset); |
| 3687 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3688 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 3689 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 3690 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 3691 | } |
| 3692 | |
| 3693 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 3694 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 3695 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 3696 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3697 | |
| 3698 | if (isIndirect) |
| 3699 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 3700 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3701 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3702 | |
| 3703 | return AddrTyped; |
| 3704 | } |
| 3705 | |
| 3706 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3707 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3708 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3709 | |
| 3710 | namespace { |
| 3711 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3712 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3713 | public: |
| 3714 | enum ABIKind { |
| 3715 | APCS = 0, |
| 3716 | AAPCS = 1, |
| 3717 | AAPCS_VFP |
| 3718 | }; |
| 3719 | |
| 3720 | private: |
| 3721 | ABIKind Kind; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3722 | mutable int VFPRegs[16]; |
| 3723 | const unsigned NumVFPs; |
| 3724 | const unsigned NumGPRs; |
| 3725 | mutable unsigned AllocatedGPRs; |
| 3726 | mutable unsigned AllocatedVFPs; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3727 | |
| 3728 | public: |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3729 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), |
| 3730 | NumVFPs(16), NumGPRs(4) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3731 | setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3732 | resetAllocatedRegs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3733 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3734 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3735 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3736 | switch (getTarget().getTriple().getEnvironment()) { |
| 3737 | case llvm::Triple::Android: |
| 3738 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3739 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3740 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 3741 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3742 | return true; |
| 3743 | default: |
| 3744 | return false; |
| 3745 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3748 | bool isEABIHF() const { |
| 3749 | switch (getTarget().getTriple().getEnvironment()) { |
| 3750 | case llvm::Triple::EABIHF: |
| 3751 | case llvm::Triple::GNUEABIHF: |
| 3752 | return true; |
| 3753 | default: |
| 3754 | return false; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3758 | ABIKind getABIKind() const { return Kind; } |
| 3759 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3760 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3761 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3762 | ABIArgInfo classifyArgumentType(QualType RetTy, bool &IsHA, bool isVariadic, |
| 3763 | bool &IsCPRC) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3764 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3765 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3766 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3767 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3768 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3769 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3770 | |
| 3771 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 3772 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 3773 | void setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3774 | |
| 3775 | void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; |
| 3776 | void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; |
| 3777 | void resetAllocatedRegs(void) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3778 | }; |
| 3779 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3780 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3781 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3782 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 3783 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3784 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3785 | const ARMABIInfo &getABIInfo() const { |
| 3786 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3787 | } |
| 3788 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3789 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3790 | return 13; |
| 3791 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3792 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3793 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3794 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3795 | } |
| 3796 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3797 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3798 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3799 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3800 | |
| 3801 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3802 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3803 | return false; |
| 3804 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3805 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3806 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3807 | if (getABIInfo().isEABI()) return 88; |
| 3808 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3809 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3810 | |
| 3811 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3812 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3813 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3814 | if (!FD) |
| 3815 | return; |
| 3816 | |
| 3817 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 3818 | if (!Attr) |
| 3819 | return; |
| 3820 | |
| 3821 | const char *Kind; |
| 3822 | switch (Attr->getInterrupt()) { |
| 3823 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 3824 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 3825 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 3826 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 3827 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 3828 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 3829 | } |
| 3830 | |
| 3831 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 3832 | |
| 3833 | Fn->addFnAttr("interrupt", Kind); |
| 3834 | |
| 3835 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 3836 | return; |
| 3837 | |
| 3838 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 3839 | // however this is not necessarily true on taking any interrupt. Instruct |
| 3840 | // the backend to perform a realignment as part of the function prologue. |
| 3841 | llvm::AttrBuilder B; |
| 3842 | B.addStackAlignmentAttr(8); |
| 3843 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 3844 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 3845 | llvm::AttributeSet::FunctionIndex, |
| 3846 | B)); |
| 3847 | } |
| 3848 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3849 | }; |
| 3850 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3853 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3854 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3855 | // VFP registers allocated so far. |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3856 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3857 | // VFP registers of the appropriate type unallocated then the argument is |
| 3858 | // allocated to the lowest-numbered sequence of such registers. |
| 3859 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3860 | // unallocated are marked as unavailable. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3861 | resetAllocatedRegs(); |
| 3862 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3863 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3864 | for (auto &I : FI.arguments()) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3865 | unsigned PreAllocationVFPs = AllocatedVFPs; |
| 3866 | unsigned PreAllocationGPRs = AllocatedGPRs; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3867 | bool IsHA = false; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3868 | bool IsCPRC = false; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3869 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3870 | // s0-s15 (d0-d7) for passing arguments. |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3871 | I.info = classifyArgumentType(I.type, IsHA, FI.isVariadic(), IsCPRC); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3872 | assert((IsCPRC || !IsHA) && "Homogeneous aggregates must be CPRCs"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3873 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3874 | // that are unallocated are marked as unavailable. To achieve this, we add |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3875 | // padding of (NumVFPs - PreAllocationVFP) floats. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3876 | // Note that IsHA will only be set when using the AAPCS-VFP calling convention, |
| 3877 | // and the callee is not variadic. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3878 | if (IsHA && AllocatedVFPs > NumVFPs && PreAllocationVFPs < NumVFPs) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3879 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3880 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocationVFPs); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3881 | I.info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | // If we have allocated some arguments onto the stack (due to running |
| 3885 | // out of VFP registers), we cannot split an argument between GPRs and |
| 3886 | // the stack. If this situation occurs, we add padding to prevent the |
| 3887 | // GPRs from being used. In this situiation, the current argument could |
| 3888 | // only be allocated by rule C.8, so rule C.6 would mark these GPRs as |
| 3889 | // unusable anyway. |
| 3890 | const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; |
| 3891 | if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && StackUsed) { |
| 3892 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3893 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3894 | I.info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3895 | } |
| 3896 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3897 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3898 | // Always honor user-specified calling convention. |
| 3899 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3900 | return; |
| 3901 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3902 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3903 | if (cc != llvm::CallingConv::C) |
| 3904 | FI.setEffectiveCallingConvention(cc); |
| 3905 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3906 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3907 | /// Return the default calling convention that LLVM will use. |
| 3908 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3909 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3910 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3911 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3912 | else if (isEABI()) |
| 3913 | return llvm::CallingConv::ARM_AAPCS; |
| 3914 | else |
| 3915 | return llvm::CallingConv::ARM_APCS; |
| 3916 | } |
| 3917 | |
| 3918 | /// Return the calling convention that our ABI would like us to use |
| 3919 | /// as the C calling convention. |
| 3920 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3921 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3922 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3923 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3924 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3925 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3926 | llvm_unreachable("bad ABI kind"); |
| 3927 | } |
| 3928 | |
| 3929 | void ARMABIInfo::setRuntimeCC() { |
| 3930 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3931 | |
| 3932 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3933 | // they'd just match what LLVM will infer from the triple. |
| 3934 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3935 | if (abiCC != getLLVMDefaultCC()) |
| 3936 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3939 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3940 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3941 | /// contained in the type is returned through it; this is used for the |
| 3942 | /// recursive calls that check aggregate component types. |
| 3943 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3944 | ASTContext &Context, uint64_t *HAMembers) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3945 | uint64_t Members = 0; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3946 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3947 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3948 | return false; |
| 3949 | Members *= AT->getSize().getZExtValue(); |
| 3950 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3951 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3952 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3953 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3954 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3955 | Members = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3956 | for (const auto *FD : RD->fields()) { |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3957 | uint64_t FldMembers; |
| 3958 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3959 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3960 | |
| 3961 | Members = (RD->isUnion() ? |
| 3962 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3963 | } |
| 3964 | } else { |
| 3965 | Members = 1; |
| 3966 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3967 | Members = 2; |
| 3968 | Ty = CT->getElementType(); |
| 3969 | } |
| 3970 | |
| 3971 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 3972 | // double, or 64-bit or 128-bit vectors. |
| 3973 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3974 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | eb752d4 | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 3975 | BT->getKind() != BuiltinType::Double && |
| 3976 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3977 | return false; |
| 3978 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3979 | unsigned VecSize = Context.getTypeSize(VT); |
| 3980 | if (VecSize != 64 && VecSize != 128) |
| 3981 | return false; |
| 3982 | } else { |
| 3983 | return false; |
| 3984 | } |
| 3985 | |
| 3986 | // The base type must be the same for all members. Vector types of the |
| 3987 | // same total size are treated as being equivalent here. |
| 3988 | const Type *TyPtr = Ty.getTypePtr(); |
| 3989 | if (!Base) |
| 3990 | Base = TyPtr; |
Oliver Stannard | 5e8558f | 2014-02-07 11:25:57 +0000 | [diff] [blame] | 3991 | |
| 3992 | if (Base != TyPtr) { |
| 3993 | // Homogeneous aggregates are defined as containing members with the |
| 3994 | // same machine type. There are two cases in which two members have |
| 3995 | // different TypePtrs but the same machine type: |
| 3996 | |
| 3997 | // 1) Vectors of the same length, regardless of the type and number |
| 3998 | // of their members. |
| 3999 | const bool SameLengthVectors = Base->isVectorType() && TyPtr->isVectorType() |
| 4000 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4001 | |
| 4002 | // 2) In the 32-bit AAPCS, `double' and `long double' have the same |
| 4003 | // machine type. This is not the case for the 64-bit AAPCS. |
| 4004 | const bool SameSizeDoubles = |
| 4005 | ( ( Base->isSpecificBuiltinType(BuiltinType::Double) |
| 4006 | && TyPtr->isSpecificBuiltinType(BuiltinType::LongDouble)) |
| 4007 | || ( Base->isSpecificBuiltinType(BuiltinType::LongDouble) |
| 4008 | && TyPtr->isSpecificBuiltinType(BuiltinType::Double))) |
| 4009 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4010 | |
| 4011 | if (!SameLengthVectors && !SameSizeDoubles) |
| 4012 | return false; |
| 4013 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 4017 | if (HAMembers) |
| 4018 | *HAMembers = Members; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4019 | |
| 4020 | return (Members > 0 && Members <= 4); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4023 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 4024 | /// number of VFP registers (unit is S register) requested. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4025 | void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, |
| 4026 | unsigned NumRequired) const { |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4027 | // Early Exit. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4028 | if (AllocatedVFPs >= 16) { |
| 4029 | // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on |
| 4030 | // the stack. |
| 4031 | AllocatedVFPs = 17; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4032 | return; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4033 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4034 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4035 | // VFP registers of the appropriate type unallocated then the argument is |
| 4036 | // allocated to the lowest-numbered sequence of such registers. |
| 4037 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 4038 | bool FoundSlot = true; |
| 4039 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4040 | if (J >= 16 || VFPRegs[J]) { |
| 4041 | FoundSlot = false; |
| 4042 | break; |
| 4043 | } |
| 4044 | if (FoundSlot) { |
| 4045 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4046 | VFPRegs[J] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4047 | AllocatedVFPs += NumRequired; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4048 | return; |
| 4049 | } |
| 4050 | } |
| 4051 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4052 | // unallocated are marked as unavailable. |
| 4053 | for (unsigned I = 0; I < 16; I++) |
| 4054 | VFPRegs[I] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4055 | AllocatedVFPs = 17; // We do not have enough VFP registers. |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4058 | /// Update AllocatedGPRs to record the number of general purpose registers |
| 4059 | /// which have been allocated. It is valid for AllocatedGPRs to go above 4, |
| 4060 | /// this represents arguments being stored on the stack. |
| 4061 | void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, |
| 4062 | unsigned NumRequired) const { |
| 4063 | assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); |
| 4064 | |
| 4065 | if (Alignment == 2 && AllocatedGPRs & 0x1) |
| 4066 | AllocatedGPRs += 1; |
| 4067 | |
| 4068 | AllocatedGPRs += NumRequired; |
| 4069 | } |
| 4070 | |
| 4071 | void ARMABIInfo::resetAllocatedRegs(void) const { |
| 4072 | AllocatedGPRs = 0; |
| 4073 | AllocatedVFPs = 0; |
| 4074 | for (unsigned i = 0; i < NumVFPs; ++i) |
| 4075 | VFPRegs[i] = 0; |
| 4076 | } |
| 4077 | |
| 4078 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool &IsHA, |
| 4079 | bool isVariadic, |
| 4080 | bool &IsCPRC) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4081 | // We update number of allocated VFPs according to |
| 4082 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4083 | // A single-precision floating-point type (including promoted |
| 4084 | // half-precision types); A double-precision floating-point type; |
| 4085 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4086 | // with a Base Type of a single- or double-precision floating-point type, |
| 4087 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4088 | // to four Elements. |
| 4089 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4090 | // Handle illegal vector types here. |
| 4091 | if (isIllegalVectorType(Ty)) { |
| 4092 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4093 | if (Size <= 32) { |
| 4094 | llvm::Type *ResType = |
| 4095 | llvm::Type::getInt32Ty(getVMContext()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4096 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4097 | return ABIArgInfo::getDirect(ResType); |
| 4098 | } |
| 4099 | if (Size == 64) { |
| 4100 | llvm::Type *ResType = llvm::VectorType::get( |
| 4101 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4102 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ |
| 4103 | markAllocatedGPRs(2, 2); |
| 4104 | } else { |
| 4105 | markAllocatedVFPs(2, 2); |
| 4106 | IsCPRC = true; |
| 4107 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4108 | return ABIArgInfo::getDirect(ResType); |
| 4109 | } |
| 4110 | if (Size == 128) { |
| 4111 | llvm::Type *ResType = llvm::VectorType::get( |
| 4112 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4113 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { |
| 4114 | markAllocatedGPRs(2, 4); |
| 4115 | } else { |
| 4116 | markAllocatedVFPs(4, 4); |
| 4117 | IsCPRC = true; |
| 4118 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4119 | return ABIArgInfo::getDirect(ResType); |
| 4120 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4121 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4122 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4123 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4124 | // Update VFPRegs for legal vector types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4125 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4126 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4127 | uint64_t Size = getContext().getTypeSize(VT); |
| 4128 | // Size of a legal vector should be power of 2 and above 64. |
| 4129 | markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); |
| 4130 | IsCPRC = true; |
| 4131 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4132 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4133 | // Update VFPRegs for floating point types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4134 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4135 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4136 | if (BT->getKind() == BuiltinType::Half || |
| 4137 | BT->getKind() == BuiltinType::Float) { |
| 4138 | markAllocatedVFPs(1, 1); |
| 4139 | IsCPRC = true; |
| 4140 | } |
| 4141 | if (BT->getKind() == BuiltinType::Double || |
| 4142 | BT->getKind() == BuiltinType::LongDouble) { |
| 4143 | markAllocatedVFPs(2, 2); |
| 4144 | IsCPRC = true; |
| 4145 | } |
| 4146 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4147 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4148 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4149 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4150 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4151 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4152 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4153 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4154 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4155 | unsigned Size = getContext().getTypeSize(Ty); |
| 4156 | if (!IsCPRC) |
| 4157 | markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4158 | return (Ty->isPromotableIntegerType() ? |
| 4159 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4160 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4161 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4162 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 4163 | markAllocatedGPRs(1, 1); |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4164 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4165 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4166 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4167 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4168 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4169 | return ABIArgInfo::getIgnore(); |
| 4170 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4171 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4172 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4173 | // into VFP registers. |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4174 | const Type *Base = 0; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4175 | uint64_t Members = 0; |
| 4176 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4177 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4178 | // Base can be a floating-point or a vector. |
| 4179 | if (Base->isVectorType()) { |
| 4180 | // ElementSize is in number of floats. |
| 4181 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4182 | markAllocatedVFPs(ElementSize, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4183 | Members * ElementSize); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4184 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4185 | markAllocatedVFPs(1, Members); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4186 | else { |
| 4187 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 4188 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4189 | markAllocatedVFPs(2, Members * 2); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4190 | } |
| 4191 | IsHA = true; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4192 | IsCPRC = true; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4193 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4194 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4197 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4198 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4199 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4200 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4201 | uint64_t ABIAlign = 4; |
| 4202 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4203 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4204 | getABIKind() == ARMABIInfo::AAPCS) |
| 4205 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4206 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4207 | // Update Allocated GPRs |
| 4208 | markAllocatedGPRs(1, 1); |
Oliver Stannard | 7c3c09e | 2014-03-12 14:02:50 +0000 | [diff] [blame] | 4209 | return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4210 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4213 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4214 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4215 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4216 | // FIXME: Try to match the types of the arguments more accurately where |
| 4217 | // we can. |
| 4218 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4219 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4220 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4221 | markAllocatedGPRs(1, SizeRegs); |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4222 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4223 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4224 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4225 | markAllocatedGPRs(2, SizeRegs * 2); |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4226 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4227 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4228 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 4229 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4230 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4233 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4234 | llvm::LLVMContext &VMContext) { |
| 4235 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4236 | // is called integer-like if its size is less than or equal to one word, and |
| 4237 | // the offset of each of its addressable sub-fields is zero. |
| 4238 | |
| 4239 | uint64_t Size = Context.getTypeSize(Ty); |
| 4240 | |
| 4241 | // Check that the type fits in a word. |
| 4242 | if (Size > 32) |
| 4243 | return false; |
| 4244 | |
| 4245 | // FIXME: Handle vector types! |
| 4246 | if (Ty->isVectorType()) |
| 4247 | return false; |
| 4248 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4249 | // Float types are never treated as "integer like". |
| 4250 | if (Ty->isRealFloatingType()) |
| 4251 | return false; |
| 4252 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4253 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4254 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4255 | return true; |
| 4256 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4257 | // Small complex integer types are "integer like". |
| 4258 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4259 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4260 | |
| 4261 | // Single element and zero sized arrays should be allowed, by the definition |
| 4262 | // above, but they are not. |
| 4263 | |
| 4264 | // Otherwise, it must be a record type. |
| 4265 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4266 | if (!RT) return false; |
| 4267 | |
| 4268 | // Ignore records with flexible arrays. |
| 4269 | const RecordDecl *RD = RT->getDecl(); |
| 4270 | if (RD->hasFlexibleArrayMember()) |
| 4271 | return false; |
| 4272 | |
| 4273 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4274 | // like". |
| 4275 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4276 | |
| 4277 | bool HadField = false; |
| 4278 | unsigned idx = 0; |
| 4279 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4280 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4281 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4282 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4283 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4284 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4285 | // struct { int : 0; int x } |
| 4286 | // is non-integer like according to gcc. |
| 4287 | if (FD->isBitField()) { |
| 4288 | if (!RD->isUnion()) |
| 4289 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4290 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4291 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4292 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4293 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4294 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4297 | // Check if this field is at offset 0. |
| 4298 | if (Layout.getFieldOffset(idx) != 0) |
| 4299 | return false; |
| 4300 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4301 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4302 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4303 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4304 | // Only allow at most one field in a structure. This doesn't match the |
| 4305 | // wording above, but follows gcc in situations with a field following an |
| 4306 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4307 | if (!RD->isUnion()) { |
| 4308 | if (HadField) |
| 4309 | return false; |
| 4310 | |
| 4311 | HadField = true; |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | return true; |
| 4316 | } |
| 4317 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4318 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4319 | bool isVariadic) const { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4320 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4321 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4322 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4323 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4324 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
| 4325 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4326 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4327 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4328 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4329 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4330 | // Treat an enum type as its underlying type. |
| 4331 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4332 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4333 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4334 | return (RetTy->isPromotableIntegerType() ? |
| 4335 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4336 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4337 | |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4338 | // Structures with either a non-trivial destructor or a non-trivial |
| 4339 | // copy constructor are always indirect. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4340 | if (isRecordReturnIndirect(RetTy, getCXXABI())) { |
| 4341 | markAllocatedGPRs(1, 1); |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4342 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4343 | } |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4344 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4345 | // Are we following APCS? |
| 4346 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4347 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4348 | return ABIArgInfo::getIgnore(); |
| 4349 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4350 | // Complex types are all returned as packed integers. |
| 4351 | // |
| 4352 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4353 | // correctly. |
| 4354 | if (RetTy->isAnyComplexType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4355 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4356 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4357 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4358 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4359 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4360 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4361 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4362 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4363 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4364 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4365 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4366 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | // Otherwise return in memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4370 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4371 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4372 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4373 | |
| 4374 | // Otherwise this is an AAPCS variant. |
| 4375 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4376 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4377 | return ABIArgInfo::getIgnore(); |
| 4378 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4379 | // Check for homogeneous aggregates with AAPCS-VFP. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4380 | if (getABIKind() == AAPCS_VFP && !isVariadic) { |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4381 | const Type *Base = 0; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4382 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 4383 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4384 | // Homogeneous Aggregates are returned directly. |
| 4385 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4386 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4389 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4390 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4391 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4392 | if (Size <= 32) { |
| 4393 | // Return in the smallest viable integer type. |
| 4394 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4395 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4396 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4397 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4398 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4401 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4402 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4405 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4406 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4407 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4408 | // Check whether VT is legal. |
| 4409 | unsigned NumElements = VT->getNumElements(); |
| 4410 | uint64_t Size = getContext().getTypeSize(VT); |
| 4411 | // NumElements should be power of 2. |
| 4412 | if ((NumElements & (NumElements - 1)) != 0) |
| 4413 | return true; |
| 4414 | // Size should be greater than 32 bits. |
| 4415 | return Size <= 32; |
| 4416 | } |
| 4417 | return false; |
| 4418 | } |
| 4419 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4420 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4421 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4422 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4423 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4424 | |
| 4425 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4426 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4427 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4428 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4429 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4430 | // These are ignored for parameter passing purposes. |
| 4431 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4432 | return Builder.CreateBitCast(Addr, PTy); |
| 4433 | } |
| 4434 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4435 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4436 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4437 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4438 | |
| 4439 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4440 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4441 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4442 | getABIKind() == ARMABIInfo::AAPCS) |
| 4443 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4444 | else |
| 4445 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4446 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4447 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4448 | IsIndirect = true; |
| 4449 | Size = 4; |
| 4450 | TyAlign = 4; |
| 4451 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4452 | |
| 4453 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4454 | if (TyAlign > 4) { |
| 4455 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4456 | "Alignment is not power of 2!"); |
| 4457 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4458 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4459 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4460 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4461 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4462 | |
| 4463 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4464 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4465 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4466 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4467 | "ap.next"); |
| 4468 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4469 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4470 | if (IsIndirect) |
| 4471 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4472 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4473 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4474 | // may not be correctly aligned for the vector type. We create an aligned |
| 4475 | // temporary space and copy the content over from ap.cur to the temporary |
| 4476 | // space. This is necessary if the natural alignment of the type is greater |
| 4477 | // than the ABI alignment. |
| 4478 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4479 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4480 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4481 | "var.align"); |
| 4482 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4483 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4484 | Builder.CreateMemCpy(Dst, Src, |
| 4485 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4486 | TyAlign, false); |
| 4487 | Addr = AlignedTemp; //The content is in aligned location. |
| 4488 | } |
| 4489 | llvm::Type *PTy = |
| 4490 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4491 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4492 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4493 | return AddrTyped; |
| 4494 | } |
| 4495 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4496 | namespace { |
| 4497 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4498 | class NaClARMABIInfo : public ABIInfo { |
| 4499 | public: |
| 4500 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4501 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4502 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4503 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4504 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4505 | private: |
| 4506 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 4507 | ARMABIInfo NInfo; // Used for everything else. |
| 4508 | }; |
| 4509 | |
| 4510 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4511 | public: |
| 4512 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4513 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 4514 | }; |
| 4515 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4518 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4519 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 4520 | PInfo.computeInfo(FI); |
| 4521 | else |
| 4522 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 4523 | } |
| 4524 | |
| 4525 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4526 | CodeGenFunction &CGF) const { |
| 4527 | // Always use the native convention; calling pnacl-style varargs functions |
| 4528 | // is unsupported. |
| 4529 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 4530 | } |
| 4531 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4532 | //===----------------------------------------------------------------------===// |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4533 | // AArch64 ABI Implementation |
| 4534 | //===----------------------------------------------------------------------===// |
| 4535 | |
| 4536 | namespace { |
| 4537 | |
| 4538 | class AArch64ABIInfo : public ABIInfo { |
| 4539 | public: |
| 4540 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4541 | |
| 4542 | private: |
| 4543 | // The AArch64 PCS is explicit about return types and argument types being |
| 4544 | // handled identically, so we don't need to draw a distinction between |
| 4545 | // Argument and Return classification. |
| 4546 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 4547 | int &FreeVFPRegs) const; |
| 4548 | |
| 4549 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 4550 | llvm::Type *DirectTy = 0) const; |
| 4551 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4552 | void computeInfo(CGFunctionInfo &FI) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4553 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4554 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4555 | CodeGenFunction &CGF) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4556 | }; |
| 4557 | |
| 4558 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4559 | public: |
| 4560 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4561 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 4562 | |
| 4563 | const AArch64ABIInfo &getABIInfo() const { |
| 4564 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4565 | } |
| 4566 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4567 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4568 | return 31; |
| 4569 | } |
| 4570 | |
| 4571 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4572 | llvm::Value *Address) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4573 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 4574 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 4575 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 4576 | |
| 4577 | // 64-95 are v0-v31: 16 bytes each |
| 4578 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 4579 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 4580 | |
| 4581 | return false; |
| 4582 | } |
| 4583 | |
| 4584 | }; |
| 4585 | |
| 4586 | } |
| 4587 | |
| 4588 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4589 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4590 | |
| 4591 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 4592 | FreeIntRegs, FreeVFPRegs); |
| 4593 | |
| 4594 | FreeIntRegs = FreeVFPRegs = 8; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4595 | for (auto &I : FI.arguments()) { |
| 4596 | I.info = classifyGenericType(I.type, FreeIntRegs, FreeVFPRegs); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4597 | |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | ABIArgInfo |
| 4602 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 4603 | bool IsInt, llvm::Type *DirectTy) const { |
| 4604 | if (FreeRegs >= RegsNeeded) { |
| 4605 | FreeRegs -= RegsNeeded; |
| 4606 | return ABIArgInfo::getDirect(DirectTy); |
| 4607 | } |
| 4608 | |
| 4609 | llvm::Type *Padding = 0; |
| 4610 | |
| 4611 | // We need padding so that later arguments don't get filled in anyway. That |
| 4612 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 4613 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 4614 | // concerned. |
| 4615 | if (FreeRegs > 0) { |
| 4616 | if (IsInt) |
| 4617 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 4618 | else |
| 4619 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 4620 | |
| 4621 | // Either [N x i64] or [N x float]. |
| 4622 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 4623 | FreeRegs = 0; |
| 4624 | } |
| 4625 | |
| 4626 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 4627 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 4628 | Padding); |
| 4629 | } |
| 4630 | |
| 4631 | |
| 4632 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 4633 | int &FreeIntRegs, |
| 4634 | int &FreeVFPRegs) const { |
| 4635 | // Can only occurs for return, but harmless otherwise. |
| 4636 | if (Ty->isVoidType()) |
| 4637 | return ABIArgInfo::getIgnore(); |
| 4638 | |
| 4639 | // Large vector types should be returned via memory. There's no such concept |
| 4640 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 4641 | // classified they'd go into memory (see B.3). |
| 4642 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 4643 | if (FreeIntRegs > 0) |
| 4644 | --FreeIntRegs; |
| 4645 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4646 | } |
| 4647 | |
| 4648 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 4649 | // be passed directly. After this block we're guaranteed to be in a |
| 4650 | // complicated case. |
| 4651 | if (!isAggregateTypeForABI(Ty)) { |
| 4652 | // Treat an enum type as its underlying type. |
| 4653 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4654 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4655 | |
| 4656 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 4657 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 4658 | |
| 4659 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 4660 | "unexpectedly large scalar type"); |
| 4661 | |
| 4662 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 4663 | |
| 4664 | // If the type may need padding registers to ensure "alignment", we must be |
| 4665 | // careful when this is accounted for. Increasing the effective size covers |
| 4666 | // all cases. |
| 4667 | if (getContext().getTypeAlign(Ty) == 128) |
| 4668 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 4669 | |
| 4670 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 4671 | } |
| 4672 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4673 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4674 | if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4675 | --FreeIntRegs; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4676 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4680 | if (!getContext().getLangOpts().CPlusPlus) { |
| 4681 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 4682 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 4683 | // the object for parameter-passsing purposes. |
| 4684 | return ABIArgInfo::getIgnore(); |
| 4685 | } |
| 4686 | |
| 4687 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 4688 | // description of va_arg in the PCS require that an empty struct does |
| 4689 | // actually occupy space for parameter-passing. I'm hoping for a |
| 4690 | // clarification giving an explicit paragraph to point to in future. |
| 4691 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 4692 | llvm::Type::getInt8Ty(getVMContext())); |
| 4693 | } |
| 4694 | |
| 4695 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 4696 | const Type *Base = 0; |
| 4697 | uint64_t NumMembers = 0; |
| 4698 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 4699 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 4700 | // Homogeneous aggregates are passed and returned directly. |
| 4701 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 4702 | /*IsInt=*/ false); |
| 4703 | } |
| 4704 | |
| 4705 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4706 | if (Size <= 128) { |
| 4707 | // Small structs can use the same direct type whether they're in registers |
| 4708 | // or on the stack. |
| 4709 | llvm::Type *BaseTy; |
| 4710 | unsigned NumBases; |
| 4711 | int SizeInRegs = (Size + 63) / 64; |
| 4712 | |
| 4713 | if (getContext().getTypeAlign(Ty) == 128) { |
| 4714 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 4715 | NumBases = 1; |
| 4716 | |
| 4717 | // If the type may need padding registers to ensure "alignment", we must |
| 4718 | // be careful when this is accounted for. Increasing the effective size |
| 4719 | // covers all cases. |
| 4720 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 4721 | } else { |
| 4722 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4723 | NumBases = SizeInRegs; |
| 4724 | } |
| 4725 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 4726 | |
| 4727 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 4728 | /*IsInt=*/ true, DirectTy); |
| 4729 | } |
| 4730 | |
| 4731 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 4732 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 4733 | --FreeIntRegs; |
| 4734 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 4735 | } |
| 4736 | |
| 4737 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4738 | CodeGenFunction &CGF) const { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4739 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4740 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4741 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 4742 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4743 | return EmitAArch64VAArg(VAListAddr, Ty, 8 - FreeIntRegs, 8 - FreeVFPRegs, |
| 4744 | AI.isIndirect(), CGF); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4748 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4749 | //===----------------------------------------------------------------------===// |
| 4750 | |
| 4751 | namespace { |
| 4752 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4753 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4754 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4755 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4756 | |
| 4757 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4758 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4759 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4760 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4761 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4762 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4763 | }; |
| 4764 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4765 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4766 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4767 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4768 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4769 | |
| 4770 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4771 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4772 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4773 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 4774 | // resulting MDNode to the nvvm.annotations MDNode. |
| 4775 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4776 | }; |
| 4777 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4778 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4779 | if (RetTy->isVoidType()) |
| 4780 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4781 | |
| 4782 | // note: this is different from default ABI |
| 4783 | if (!RetTy->isScalarType()) |
| 4784 | return ABIArgInfo::getDirect(); |
| 4785 | |
| 4786 | // Treat an enum type as its underlying type. |
| 4787 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4788 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4789 | |
| 4790 | return (RetTy->isPromotableIntegerType() ? |
| 4791 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4794 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4795 | // Treat an enum type as its underlying type. |
| 4796 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4797 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4798 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4799 | return (Ty->isPromotableIntegerType() ? |
| 4800 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4803 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4804 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4805 | for (auto &I : FI.arguments()) |
| 4806 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4807 | |
| 4808 | // Always honor user-specified calling convention. |
| 4809 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4810 | return; |
| 4811 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4812 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4813 | } |
| 4814 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4815 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4816 | CodeGenFunction &CFG) const { |
| 4817 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4820 | void NVPTXTargetCodeGenInfo:: |
| 4821 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4822 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4823 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4824 | if (!FD) return; |
| 4825 | |
| 4826 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4827 | |
| 4828 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4829 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4830 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4831 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4832 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4833 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4834 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4835 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4836 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4837 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4838 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4839 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4840 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4841 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4842 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4843 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4844 | // __global__ functions cannot be called from the device, we do not |
| 4845 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4846 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 4847 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4848 | addNVVMMetadata(F, "kernel", 1); |
| 4849 | } |
| 4850 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 4851 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 4852 | addNVVMMetadata(F, "maxntidx", |
| 4853 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 4854 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 4855 | // zero value from getMinBlocks either means it was not specified in |
| 4856 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 4857 | // don't have to add a PTX directive. |
| 4858 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 4859 | if (MinCTASM > 0) { |
| 4860 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 4861 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 4862 | } |
| 4863 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4864 | } |
| 4865 | } |
| 4866 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4867 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 4868 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4869 | llvm::Module *M = F->getParent(); |
| 4870 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4871 | |
| 4872 | // Get "nvvm.annotations" metadata node |
| 4873 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4874 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4875 | llvm::SmallVector<llvm::Value *, 3> MDVals; |
| 4876 | MDVals.push_back(F); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4877 | MDVals.push_back(llvm::MDString::get(Ctx, Name)); |
| 4878 | MDVals.push_back( |
| 4879 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand)); |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4880 | // Append metadata to nvvm.annotations |
| 4881 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4882 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4886 | // SystemZ ABI Implementation |
| 4887 | //===----------------------------------------------------------------------===// |
| 4888 | |
| 4889 | namespace { |
| 4890 | |
| 4891 | class SystemZABIInfo : public ABIInfo { |
| 4892 | public: |
| 4893 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4894 | |
| 4895 | bool isPromotableIntegerType(QualType Ty) const; |
| 4896 | bool isCompoundType(QualType Ty) const; |
| 4897 | bool isFPArgumentType(QualType Ty) const; |
| 4898 | |
| 4899 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4900 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4901 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4902 | void computeInfo(CGFunctionInfo &FI) const override { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4903 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4904 | for (auto &I : FI.arguments()) |
| 4905 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4908 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4909 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4910 | }; |
| 4911 | |
| 4912 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4913 | public: |
| 4914 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4915 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4916 | }; |
| 4917 | |
| 4918 | } |
| 4919 | |
| 4920 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4921 | // Treat an enum type as its underlying type. |
| 4922 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4923 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4924 | |
| 4925 | // Promotable integer types are required to be promoted by the ABI. |
| 4926 | if (Ty->isPromotableIntegerType()) |
| 4927 | return true; |
| 4928 | |
| 4929 | // 32-bit values must also be promoted. |
| 4930 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4931 | switch (BT->getKind()) { |
| 4932 | case BuiltinType::Int: |
| 4933 | case BuiltinType::UInt: |
| 4934 | return true; |
| 4935 | default: |
| 4936 | return false; |
| 4937 | } |
| 4938 | return false; |
| 4939 | } |
| 4940 | |
| 4941 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 4942 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 4943 | } |
| 4944 | |
| 4945 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 4946 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4947 | switch (BT->getKind()) { |
| 4948 | case BuiltinType::Float: |
| 4949 | case BuiltinType::Double: |
| 4950 | return true; |
| 4951 | default: |
| 4952 | return false; |
| 4953 | } |
| 4954 | |
| 4955 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 4956 | const RecordDecl *RD = RT->getDecl(); |
| 4957 | bool Found = false; |
| 4958 | |
| 4959 | // If this is a C++ record, check the bases first. |
| 4960 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4961 | for (const auto &I : CXXRD->bases()) { |
| 4962 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4963 | |
| 4964 | // Empty bases don't affect things either way. |
| 4965 | if (isEmptyRecord(getContext(), Base, true)) |
| 4966 | continue; |
| 4967 | |
| 4968 | if (Found) |
| 4969 | return false; |
| 4970 | Found = isFPArgumentType(Base); |
| 4971 | if (!Found) |
| 4972 | return false; |
| 4973 | } |
| 4974 | |
| 4975 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4976 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4977 | // Empty bitfields don't affect things either way. |
| 4978 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 4979 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 4980 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4981 | return true; |
| 4982 | |
| 4983 | // Unlike isSingleElementStruct(), arrays do not count. |
| 4984 | // Nested isFPArgumentType structures still do though. |
| 4985 | if (Found) |
| 4986 | return false; |
| 4987 | Found = isFPArgumentType(FD->getType()); |
| 4988 | if (!Found) |
| 4989 | return false; |
| 4990 | } |
| 4991 | |
| 4992 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 4993 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 4994 | return Found; |
| 4995 | } |
| 4996 | |
| 4997 | return false; |
| 4998 | } |
| 4999 | |
| 5000 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5001 | CodeGenFunction &CGF) const { |
| 5002 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5003 | // struct { |
| 5004 | // i64 __gpr; |
| 5005 | // i64 __fpr; |
| 5006 | // i8 *__overflow_arg_area; |
| 5007 | // i8 *__reg_save_area; |
| 5008 | // }; |
| 5009 | |
| 5010 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5011 | // GPRs or FPRs. |
| 5012 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 5013 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5014 | bool InFPRs = isFPArgumentType(Ty); |
| 5015 | |
| 5016 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 5017 | bool IsIndirect = AI.isIndirect(); |
| 5018 | unsigned UnpaddedBitSize; |
| 5019 | if (IsIndirect) { |
| 5020 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5021 | UnpaddedBitSize = 64; |
| 5022 | } else |
| 5023 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 5024 | unsigned PaddedBitSize = 64; |
| 5025 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5026 | |
| 5027 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5028 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5029 | |
| 5030 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5031 | if (InFPRs) { |
| 5032 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5033 | RegCountField = 1; // __fpr |
| 5034 | RegSaveIndex = 16; // save offset for f0 |
| 5035 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5036 | } else { |
| 5037 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5038 | RegCountField = 0; // __gpr |
| 5039 | RegSaveIndex = 2; // save offset for r2 |
| 5040 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5041 | } |
| 5042 | |
| 5043 | llvm::Value *RegCountPtr = |
| 5044 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5045 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5046 | llvm::Type *IndexTy = RegCount->getType(); |
| 5047 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5048 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5049 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5050 | |
| 5051 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5052 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5053 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5054 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5055 | |
| 5056 | // Emit code to load the value if it was passed in registers. |
| 5057 | CGF.EmitBlock(InRegBlock); |
| 5058 | |
| 5059 | // Work out the address of an argument register. |
| 5060 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5061 | llvm::Value *ScaledRegCount = |
| 5062 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5063 | llvm::Value *RegBase = |
| 5064 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5065 | llvm::Value *RegOffset = |
| 5066 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5067 | llvm::Value *RegSaveAreaPtr = |
| 5068 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5069 | llvm::Value *RegSaveArea = |
| 5070 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5071 | llvm::Value *RawRegAddr = |
| 5072 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5073 | llvm::Value *RegAddr = |
| 5074 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5075 | |
| 5076 | // Update the register count |
| 5077 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5078 | llvm::Value *NewRegCount = |
| 5079 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5080 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5081 | CGF.EmitBranch(ContBlock); |
| 5082 | |
| 5083 | // Emit code to load the value if it was passed in memory. |
| 5084 | CGF.EmitBlock(InMemBlock); |
| 5085 | |
| 5086 | // Work out the address of a stack argument. |
| 5087 | llvm::Value *OverflowArgAreaPtr = |
| 5088 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5089 | llvm::Value *OverflowArgArea = |
| 5090 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5091 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5092 | llvm::Value *RawMemAddr = |
| 5093 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5094 | llvm::Value *MemAddr = |
| 5095 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5096 | |
| 5097 | // Update overflow_arg_area_ptr pointer |
| 5098 | llvm::Value *NewOverflowArgArea = |
| 5099 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5100 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5101 | CGF.EmitBranch(ContBlock); |
| 5102 | |
| 5103 | // Return the appropriate result. |
| 5104 | CGF.EmitBlock(ContBlock); |
| 5105 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5106 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5107 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5108 | |
| 5109 | if (IsIndirect) |
| 5110 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5111 | |
| 5112 | return ResAddr; |
| 5113 | } |
| 5114 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5115 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 5116 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 5117 | assert(Triple.getArch() == llvm::Triple::x86); |
| 5118 | |
| 5119 | switch (Opts.getStructReturnConvention()) { |
| 5120 | case CodeGenOptions::SRCK_Default: |
| 5121 | break; |
| 5122 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 5123 | return false; |
| 5124 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 5125 | return true; |
| 5126 | } |
| 5127 | |
| 5128 | if (Triple.isOSDarwin()) |
| 5129 | return true; |
| 5130 | |
| 5131 | switch (Triple.getOS()) { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5132 | case llvm::Triple::AuroraUX: |
| 5133 | case llvm::Triple::DragonFly: |
| 5134 | case llvm::Triple::FreeBSD: |
| 5135 | case llvm::Triple::OpenBSD: |
| 5136 | case llvm::Triple::Bitrig: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5137 | return true; |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 5138 | case llvm::Triple::Win32: |
| 5139 | switch (Triple.getEnvironment()) { |
| 5140 | case llvm::Triple::UnknownEnvironment: |
| 5141 | case llvm::Triple::Cygnus: |
| 5142 | case llvm::Triple::GNU: |
| 5143 | case llvm::Triple::MSVC: |
| 5144 | return true; |
| 5145 | default: |
| 5146 | return false; |
| 5147 | } |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5148 | default: |
| 5149 | return false; |
| 5150 | } |
| 5151 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5152 | |
| 5153 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5154 | if (RetTy->isVoidType()) |
| 5155 | return ABIArgInfo::getIgnore(); |
| 5156 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5157 | return ABIArgInfo::getIndirect(0); |
| 5158 | return (isPromotableIntegerType(RetTy) ? |
| 5159 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5160 | } |
| 5161 | |
| 5162 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5163 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5164 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5165 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5166 | |
| 5167 | // Integers and enums are extended to full register width. |
| 5168 | if (isPromotableIntegerType(Ty)) |
| 5169 | return ABIArgInfo::getExtend(); |
| 5170 | |
| 5171 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5172 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5173 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5174 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5175 | |
| 5176 | // Handle small structures. |
| 5177 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5178 | // Structures with flexible arrays have variable length, so really |
| 5179 | // fail the size test above. |
| 5180 | const RecordDecl *RD = RT->getDecl(); |
| 5181 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5182 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5183 | |
| 5184 | // The structure is passed as an unextended integer, a float, or a double. |
| 5185 | llvm::Type *PassTy; |
| 5186 | if (isFPArgumentType(Ty)) { |
| 5187 | assert(Size == 32 || Size == 64); |
| 5188 | if (Size == 32) |
| 5189 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5190 | else |
| 5191 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5192 | } else |
| 5193 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5194 | return ABIArgInfo::getDirect(PassTy); |
| 5195 | } |
| 5196 | |
| 5197 | // Non-structure compounds are passed indirectly. |
| 5198 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5199 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5200 | |
| 5201 | return ABIArgInfo::getDirect(0); |
| 5202 | } |
| 5203 | |
| 5204 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5205 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5206 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5207 | |
| 5208 | namespace { |
| 5209 | |
| 5210 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5211 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5212 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5213 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5214 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5215 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5216 | }; |
| 5217 | |
| 5218 | } |
| 5219 | |
| 5220 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5221 | llvm::GlobalValue *GV, |
| 5222 | CodeGen::CodeGenModule &M) const { |
| 5223 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5224 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5225 | // Handle 'interrupt' attribute: |
| 5226 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5227 | |
| 5228 | // Step 1: Set ISR calling convention. |
| 5229 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5230 | |
| 5231 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5232 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5233 | |
| 5234 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5235 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5236 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5237 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5238 | GV, &M.getModule()); |
| 5239 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5240 | } |
| 5241 | } |
| 5242 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5243 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5244 | // MIPS ABI Implementation. This works for both little-endian and |
| 5245 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5246 | //===----------------------------------------------------------------------===// |
| 5247 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5248 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5249 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5250 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5251 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5252 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5253 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5254 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5255 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5256 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5257 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5258 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5259 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5260 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5261 | |
| 5262 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5263 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5264 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5265 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5266 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5267 | }; |
| 5268 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5269 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5270 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5271 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5272 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5273 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5274 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5275 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5276 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5277 | return 29; |
| 5278 | } |
| 5279 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5280 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5281 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5282 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5283 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5284 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5285 | if (FD->hasAttr<Mips16Attr>()) { |
| 5286 | Fn->addFnAttr("mips16"); |
| 5287 | } |
| 5288 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5289 | Fn->addFnAttr("nomips16"); |
| 5290 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5291 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5292 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5293 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5294 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5295 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5296 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5297 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5298 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5299 | }; |
| 5300 | } |
| 5301 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5302 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5303 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5304 | llvm::IntegerType *IntTy = |
| 5305 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5306 | |
| 5307 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5308 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5309 | ArgList.push_back(IntTy); |
| 5310 | |
| 5311 | // If necessary, add one more integer type to ArgList. |
| 5312 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5313 | |
| 5314 | if (R) |
| 5315 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5318 | // In N32/64, an aligned double precision floating point field is passed in |
| 5319 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5320 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5321 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5322 | |
| 5323 | if (IsO32) { |
| 5324 | CoerceToIntArgs(TySize, ArgList); |
| 5325 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5326 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5327 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5328 | if (Ty->isComplexType()) |
| 5329 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5330 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5331 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5332 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5333 | // Unions/vectors are passed in integer registers. |
| 5334 | if (!RT || !RT->isStructureOrClassType()) { |
| 5335 | CoerceToIntArgs(TySize, ArgList); |
| 5336 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5337 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5338 | |
| 5339 | const RecordDecl *RD = RT->getDecl(); |
| 5340 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5341 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5342 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5343 | uint64_t LastOffset = 0; |
| 5344 | unsigned idx = 0; |
| 5345 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5346 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5347 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5348 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5349 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5350 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5351 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5352 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5353 | |
| 5354 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5355 | continue; |
| 5356 | |
| 5357 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5358 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5359 | continue; |
| 5360 | |
| 5361 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5362 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5363 | ArgList.push_back(I64); |
| 5364 | |
| 5365 | // Add double type. |
| 5366 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5367 | LastOffset = Offset + 64; |
| 5368 | } |
| 5369 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5370 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5371 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5372 | |
| 5373 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5374 | } |
| 5375 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5376 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5377 | uint64_t Offset) const { |
| 5378 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
| 5379 | return 0; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5380 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5381 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5382 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5383 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5384 | ABIArgInfo |
| 5385 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5386 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5387 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5388 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5389 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5390 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5391 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5392 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5393 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5394 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5395 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5396 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5397 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5398 | return ABIArgInfo::getIgnore(); |
| 5399 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5400 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5401 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5402 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5403 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5404 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5405 | // If we have reached here, aggregates are passed directly by coercing to |
| 5406 | // another structure type. Padding is inserted if the offset of the |
| 5407 | // aggregate is unaligned. |
| 5408 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5409 | getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | // Treat an enum type as its underlying type. |
| 5413 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5414 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5415 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5416 | if (Ty->isPromotableIntegerType()) |
| 5417 | return ABIArgInfo::getExtend(); |
| 5418 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5419 | return ABIArgInfo::getDirect( |
| 5420 | 0, 0, IsO32 ? 0 : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5423 | llvm::Type* |
| 5424 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5425 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5426 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5427 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5428 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5429 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5430 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5431 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5432 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5433 | // N32/64 returns struct/classes in floating point registers if the |
| 5434 | // following conditions are met: |
| 5435 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5436 | // 2. The struct/class has one or two fields all of which are floating |
| 5437 | // point types. |
| 5438 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5439 | // |
| 5440 | // Any other composite results are returned in integer registers. |
| 5441 | // |
| 5442 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5443 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5444 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5445 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5446 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5447 | if (!BT || !BT->isFloatingPoint()) |
| 5448 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5449 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5450 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | if (b == e) |
| 5454 | return llvm::StructType::get(getVMContext(), RTList, |
| 5455 | RD->hasAttr<PackedAttr>()); |
| 5456 | |
| 5457 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5458 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5461 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5462 | return llvm::StructType::get(getVMContext(), RTList); |
| 5463 | } |
| 5464 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5465 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5466 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5467 | |
| 5468 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5469 | return ABIArgInfo::getIgnore(); |
| 5470 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5471 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5472 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5473 | return ABIArgInfo::getIndirect(0); |
| 5474 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5475 | if (Size <= 128) { |
| 5476 | if (RetTy->isAnyComplexType()) |
| 5477 | return ABIArgInfo::getDirect(); |
| 5478 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5479 | // O32 returns integer vectors in registers. |
| 5480 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 5481 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5482 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5483 | if (!IsO32) |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5484 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5485 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5486 | |
| 5487 | return ABIArgInfo::getIndirect(0); |
| 5488 | } |
| 5489 | |
| 5490 | // Treat an enum type as its underlying type. |
| 5491 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5492 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5493 | |
| 5494 | return (RetTy->isPromotableIntegerType() ? |
| 5495 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5496 | } |
| 5497 | |
| 5498 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5499 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 5500 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 5501 | |
| 5502 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5503 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5504 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5505 | for (auto &I : FI.arguments()) |
| 5506 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5510 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5511 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5512 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5513 | |
| 5514 | CGBuilderTy &Builder = CGF.Builder; |
| 5515 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5516 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5517 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5518 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5519 | llvm::Value *AddrTyped; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5520 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5521 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5522 | |
| 5523 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5524 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5525 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5526 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5527 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5528 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5529 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5530 | } |
| 5531 | else |
| 5532 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5533 | |
| 5534 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5535 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5536 | uint64_t Offset = |
| 5537 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 5538 | llvm::Value *NextAddr = |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5539 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5540 | "ap.next"); |
| 5541 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5542 | |
| 5543 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5546 | bool |
| 5547 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5548 | llvm::Value *Address) const { |
| 5549 | // This information comes from gcc's implementation, which seems to |
| 5550 | // as canonical as it gets. |
| 5551 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5552 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5553 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5554 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5555 | |
| 5556 | // 0-31 are the general purpose registers, $0 - $31. |
| 5557 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5558 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5559 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5560 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5561 | |
| 5562 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5563 | // They are one bit wide and ignored here. |
| 5564 | |
| 5565 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5566 | // (coprocessor 1 is the FP unit) |
| 5567 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5568 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5569 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5570 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5571 | return false; |
| 5572 | } |
| 5573 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5574 | //===----------------------------------------------------------------------===// |
| 5575 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5576 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5577 | // handling. |
| 5578 | //===----------------------------------------------------------------------===// |
| 5579 | |
| 5580 | namespace { |
| 5581 | |
| 5582 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5583 | public: |
| 5584 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5585 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5586 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5587 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5588 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5589 | }; |
| 5590 | |
| 5591 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5592 | llvm::GlobalValue *GV, |
| 5593 | CodeGen::CodeGenModule &M) const { |
| 5594 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5595 | if (!FD) return; |
| 5596 | |
| 5597 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5598 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5599 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5600 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5601 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5602 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5603 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5604 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5605 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5606 | llvm::LLVMContext &Context = F->getContext(); |
| 5607 | llvm::NamedMDNode *OpenCLMetadata = |
| 5608 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5609 | |
| 5610 | SmallVector<llvm::Value*, 5> Operands; |
| 5611 | Operands.push_back(F); |
| 5612 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5613 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5614 | llvm::APInt(32, Attr->getXDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5615 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5616 | llvm::APInt(32, Attr->getYDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5617 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5618 | llvm::APInt(32, Attr->getZDim()))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5619 | |
| 5620 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5621 | // for implementing the work_group_size_hint attr later. Currently |
| 5622 | // always true as the hint is not yet implemented. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5623 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5624 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5625 | } |
| 5626 | } |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5631 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5632 | //===----------------------------------------------------------------------===// |
| 5633 | // Hexagon ABI Implementation |
| 5634 | //===----------------------------------------------------------------------===// |
| 5635 | |
| 5636 | namespace { |
| 5637 | |
| 5638 | class HexagonABIInfo : public ABIInfo { |
| 5639 | |
| 5640 | |
| 5641 | public: |
| 5642 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5643 | |
| 5644 | private: |
| 5645 | |
| 5646 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5647 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5648 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5649 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5650 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5651 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5652 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5653 | }; |
| 5654 | |
| 5655 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5656 | public: |
| 5657 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5658 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5659 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5660 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5661 | return 29; |
| 5662 | } |
| 5663 | }; |
| 5664 | |
| 5665 | } |
| 5666 | |
| 5667 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5668 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5669 | for (auto &I : FI.arguments()) |
| 5670 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
| 5673 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5674 | if (!isAggregateTypeForABI(Ty)) { |
| 5675 | // Treat an enum type as its underlying type. |
| 5676 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5677 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5678 | |
| 5679 | return (Ty->isPromotableIntegerType() ? |
| 5680 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5681 | } |
| 5682 | |
| 5683 | // Ignore empty records. |
| 5684 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5685 | return ABIArgInfo::getIgnore(); |
| 5686 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5687 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5688 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5689 | |
| 5690 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5691 | if (Size > 64) |
| 5692 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5693 | // Pass in the smallest viable integer type. |
| 5694 | else if (Size > 32) |
| 5695 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5696 | else if (Size > 16) |
| 5697 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5698 | else if (Size > 8) |
| 5699 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5700 | else |
| 5701 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5702 | } |
| 5703 | |
| 5704 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5705 | if (RetTy->isVoidType()) |
| 5706 | return ABIArgInfo::getIgnore(); |
| 5707 | |
| 5708 | // Large vector types should be returned via memory. |
| 5709 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5710 | return ABIArgInfo::getIndirect(0); |
| 5711 | |
| 5712 | if (!isAggregateTypeForABI(RetTy)) { |
| 5713 | // Treat an enum type as its underlying type. |
| 5714 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5715 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5716 | |
| 5717 | return (RetTy->isPromotableIntegerType() ? |
| 5718 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5719 | } |
| 5720 | |
| 5721 | // Structures with either a non-trivial destructor or a non-trivial |
| 5722 | // copy constructor are always indirect. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5723 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5724 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5725 | |
| 5726 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5727 | return ABIArgInfo::getIgnore(); |
| 5728 | |
| 5729 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5730 | // are returned indirectly. |
| 5731 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5732 | if (Size <= 64) { |
| 5733 | // Return in the smallest viable integer type. |
| 5734 | if (Size <= 8) |
| 5735 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5736 | if (Size <= 16) |
| 5737 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5738 | if (Size <= 32) |
| 5739 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5740 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5741 | } |
| 5742 | |
| 5743 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5744 | } |
| 5745 | |
| 5746 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5747 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5748 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5749 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5750 | |
| 5751 | CGBuilderTy &Builder = CGF.Builder; |
| 5752 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5753 | "ap"); |
| 5754 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5755 | llvm::Type *PTy = |
| 5756 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5757 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5758 | |
| 5759 | uint64_t Offset = |
| 5760 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5761 | llvm::Value *NextAddr = |
| 5762 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5763 | "ap.next"); |
| 5764 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5765 | |
| 5766 | return AddrTyped; |
| 5767 | } |
| 5768 | |
| 5769 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5770 | //===----------------------------------------------------------------------===// |
| 5771 | // SPARC v9 ABI Implementation. |
| 5772 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5773 | // |
| 5774 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5775 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5776 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5777 | // |
| 5778 | // One case requires special care: |
| 5779 | // |
| 5780 | // struct mixed { |
| 5781 | // int i; |
| 5782 | // float f; |
| 5783 | // }; |
| 5784 | // |
| 5785 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5786 | // parameter array, but the int is passed in an integer register, and the float |
| 5787 | // is passed in a floating point register. This is represented as two arguments |
| 5788 | // with the LLVM IR inreg attribute: |
| 5789 | // |
| 5790 | // declare void f(i32 inreg %i, float inreg %f) |
| 5791 | // |
| 5792 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5793 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5794 | // bytes. |
| 5795 | // |
| 5796 | namespace { |
| 5797 | class SparcV9ABIInfo : public ABIInfo { |
| 5798 | public: |
| 5799 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5800 | |
| 5801 | private: |
| 5802 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5803 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5804 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5805 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5806 | |
| 5807 | // Coercion type builder for structs passed in registers. The coercion type |
| 5808 | // serves two purposes: |
| 5809 | // |
| 5810 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5811 | // in registers. |
| 5812 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5813 | // code generator knows to pass them in floating point registers. |
| 5814 | // |
| 5815 | // We also compute the InReg flag which indicates that the struct contains |
| 5816 | // aligned 32-bit floats. |
| 5817 | // |
| 5818 | struct CoerceBuilder { |
| 5819 | llvm::LLVMContext &Context; |
| 5820 | const llvm::DataLayout &DL; |
| 5821 | SmallVector<llvm::Type*, 8> Elems; |
| 5822 | uint64_t Size; |
| 5823 | bool InReg; |
| 5824 | |
| 5825 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5826 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5827 | |
| 5828 | // Pad Elems with integers until Size is ToSize. |
| 5829 | void pad(uint64_t ToSize) { |
| 5830 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5831 | if (ToSize == Size) |
| 5832 | return; |
| 5833 | |
| 5834 | // Finish the current 64-bit word. |
| 5835 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5836 | if (Aligned > Size && Aligned <= ToSize) { |
| 5837 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5838 | Size = Aligned; |
| 5839 | } |
| 5840 | |
| 5841 | // Add whole 64-bit words. |
| 5842 | while (Size + 64 <= ToSize) { |
| 5843 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5844 | Size += 64; |
| 5845 | } |
| 5846 | |
| 5847 | // Final in-word padding. |
| 5848 | if (Size < ToSize) { |
| 5849 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5850 | Size = ToSize; |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | // Add a floating point element at Offset. |
| 5855 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5856 | // Unaligned floats are treated as integers. |
| 5857 | if (Offset % Bits) |
| 5858 | return; |
| 5859 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5860 | if (Bits < 64) |
| 5861 | InReg = true; |
| 5862 | pad(Offset); |
| 5863 | Elems.push_back(Ty); |
| 5864 | Size = Offset + Bits; |
| 5865 | } |
| 5866 | |
| 5867 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5868 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5869 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5870 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5871 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5872 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5873 | switch (ElemTy->getTypeID()) { |
| 5874 | case llvm::Type::StructTyID: |
| 5875 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5876 | break; |
| 5877 | case llvm::Type::FloatTyID: |
| 5878 | addFloat(ElemOffset, ElemTy, 32); |
| 5879 | break; |
| 5880 | case llvm::Type::DoubleTyID: |
| 5881 | addFloat(ElemOffset, ElemTy, 64); |
| 5882 | break; |
| 5883 | case llvm::Type::FP128TyID: |
| 5884 | addFloat(ElemOffset, ElemTy, 128); |
| 5885 | break; |
| 5886 | case llvm::Type::PointerTyID: |
| 5887 | if (ElemOffset % 64 == 0) { |
| 5888 | pad(ElemOffset); |
| 5889 | Elems.push_back(ElemTy); |
| 5890 | Size += 64; |
| 5891 | } |
| 5892 | break; |
| 5893 | default: |
| 5894 | break; |
| 5895 | } |
| 5896 | } |
| 5897 | } |
| 5898 | |
| 5899 | // Check if Ty is a usable substitute for the coercion type. |
| 5900 | bool isUsableType(llvm::StructType *Ty) const { |
| 5901 | if (Ty->getNumElements() != Elems.size()) |
| 5902 | return false; |
| 5903 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5904 | if (Elems[i] != Ty->getElementType(i)) |
| 5905 | return false; |
| 5906 | return true; |
| 5907 | } |
| 5908 | |
| 5909 | // Get the coercion type as a literal struct type. |
| 5910 | llvm::Type *getType() const { |
| 5911 | if (Elems.size() == 1) |
| 5912 | return Elems.front(); |
| 5913 | else |
| 5914 | return llvm::StructType::get(Context, Elems); |
| 5915 | } |
| 5916 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5917 | }; |
| 5918 | } // end anonymous namespace |
| 5919 | |
| 5920 | ABIArgInfo |
| 5921 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5922 | if (Ty->isVoidType()) |
| 5923 | return ABIArgInfo::getIgnore(); |
| 5924 | |
| 5925 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5926 | |
| 5927 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5928 | // pointer / sret pointer. |
| 5929 | if (Size > SizeLimit) |
| 5930 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5931 | |
| 5932 | // Treat an enum type as its underlying type. |
| 5933 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5934 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5935 | |
| 5936 | // Integer types smaller than a register are extended. |
| 5937 | if (Size < 64 && Ty->isIntegerType()) |
| 5938 | return ABIArgInfo::getExtend(); |
| 5939 | |
| 5940 | // Other non-aggregates go in registers. |
| 5941 | if (!isAggregateTypeForABI(Ty)) |
| 5942 | return ABIArgInfo::getDirect(); |
| 5943 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 5944 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 5945 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 5946 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 5947 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5948 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5949 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5950 | // Build a coercion type from the LLVM struct type. |
| 5951 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5952 | if (!StrTy) |
| 5953 | return ABIArgInfo::getDirect(); |
| 5954 | |
| 5955 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5956 | CB.addStruct(0, StrTy); |
| 5957 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5958 | |
| 5959 | // Try to use the original type for coercion. |
| 5960 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5961 | |
| 5962 | if (CB.InReg) |
| 5963 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5964 | else |
| 5965 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
| 5968 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5969 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5970 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 5971 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5972 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5973 | AI.setCoerceToType(ArgTy); |
| 5974 | |
| 5975 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 5976 | CGBuilderTy &Builder = CGF.Builder; |
| 5977 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5978 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5979 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 5980 | llvm::Value *ArgAddr; |
| 5981 | unsigned Stride; |
| 5982 | |
| 5983 | switch (AI.getKind()) { |
| 5984 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 5985 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5986 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5987 | |
| 5988 | case ABIArgInfo::Extend: |
| 5989 | Stride = 8; |
| 5990 | ArgAddr = Builder |
| 5991 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 5992 | "extend"); |
| 5993 | break; |
| 5994 | |
| 5995 | case ABIArgInfo::Direct: |
| 5996 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 5997 | ArgAddr = Addr; |
| 5998 | break; |
| 5999 | |
| 6000 | case ABIArgInfo::Indirect: |
| 6001 | Stride = 8; |
| 6002 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6003 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6004 | "indirect"); |
| 6005 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6006 | break; |
| 6007 | |
| 6008 | case ABIArgInfo::Ignore: |
| 6009 | return llvm::UndefValue::get(ArgPtrTy); |
| 6010 | } |
| 6011 | |
| 6012 | // Update VAList. |
| 6013 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6014 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6015 | |
| 6016 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6017 | } |
| 6018 | |
| 6019 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6020 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6021 | for (auto &I : FI.arguments()) |
| 6022 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | namespace { |
| 6026 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6027 | public: |
| 6028 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6029 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6030 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6031 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6032 | return 14; |
| 6033 | } |
| 6034 | |
| 6035 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6036 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6037 | }; |
| 6038 | } // end anonymous namespace |
| 6039 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6040 | bool |
| 6041 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6042 | llvm::Value *Address) const { |
| 6043 | // This is calculated from the LLVM and GCC tables and verified |
| 6044 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6045 | |
| 6046 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6047 | |
| 6048 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6049 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6050 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6051 | |
| 6052 | // 0-31: the 8-byte general-purpose registers |
| 6053 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6054 | |
| 6055 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6056 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6057 | |
| 6058 | // Y = 64 |
| 6059 | // PSR = 65 |
| 6060 | // WIM = 66 |
| 6061 | // TBR = 67 |
| 6062 | // PC = 68 |
| 6063 | // NPC = 69 |
| 6064 | // FSR = 70 |
| 6065 | // CSR = 71 |
| 6066 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6067 | |
| 6068 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6069 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6070 | |
| 6071 | return false; |
| 6072 | } |
| 6073 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6074 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6075 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6076 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6077 | //===----------------------------------------------------------------------===// |
| 6078 | namespace { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6079 | class XCoreABIInfo : public DefaultABIInfo { |
| 6080 | public: |
| 6081 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6082 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6083 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6084 | }; |
| 6085 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6086 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6087 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6088 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6089 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6090 | }; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6091 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6092 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6093 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6094 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6095 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6096 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6097 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6098 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6099 | CGF.Int8PtrPtrTy); |
| 6100 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6101 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6102 | // Handle the argument. |
| 6103 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6104 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6105 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6106 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6107 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6108 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6109 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6110 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6111 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6112 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6113 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6114 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6115 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6116 | ArgSize = 0; |
| 6117 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6118 | case ABIArgInfo::Extend: |
| 6119 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6120 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6121 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6122 | if (ArgSize < 4) |
| 6123 | ArgSize = 4; |
| 6124 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6125 | case ABIArgInfo::Indirect: |
| 6126 | llvm::Value *ArgAddr; |
| 6127 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6128 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6129 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6130 | ArgSize = 4; |
| 6131 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6132 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6133 | |
| 6134 | // Increment the VAList. |
| 6135 | if (ArgSize) { |
| 6136 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6137 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6138 | } |
| 6139 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6140 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6141 | |
| 6142 | //===----------------------------------------------------------------------===// |
| 6143 | // Driver code |
| 6144 | //===----------------------------------------------------------------------===// |
| 6145 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6146 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6147 | if (TheTargetCodeGenInfo) |
| 6148 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6149 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6150 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6151 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6152 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6153 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6154 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6155 | case llvm::Triple::le32: |
| 6156 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6157 | case llvm::Triple::mips: |
| 6158 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6159 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6160 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6161 | case llvm::Triple::mips64: |
| 6162 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6163 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 6164 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6165 | case llvm::Triple::arm64: { |
| 6166 | ARM64ABIInfo::ABIKind Kind = ARM64ABIInfo::AAPCS; |
| 6167 | if (strcmp(getTarget().getABI(), "darwinpcs") == 0) |
| 6168 | Kind = ARM64ABIInfo::DarwinPCS; |
| 6169 | |
| 6170 | return *(TheTargetCodeGenInfo = new ARM64TargetCodeGenInfo(Types, Kind)); |
| 6171 | } |
| 6172 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6173 | case llvm::Triple::aarch64: |
Christian Pirker | 9b019ae | 2014-02-25 13:51:00 +0000 | [diff] [blame] | 6174 | case llvm::Triple::aarch64_be: |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6175 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 6176 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6177 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6178 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6179 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6180 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6181 | { |
| 6182 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6183 | if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6184 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 6185 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6186 | (CodeGenOpts.FloatABI != "soft" && |
| 6187 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6188 | Kind = ARMABIInfo::AAPCS_VFP; |
| 6189 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6190 | switch (Triple.getOS()) { |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6191 | case llvm::Triple::NaCl: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6192 | return *(TheTargetCodeGenInfo = |
| 6193 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 6194 | default: |
| 6195 | return *(TheTargetCodeGenInfo = |
| 6196 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 6197 | } |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6198 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6199 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6200 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6201 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 6202 | case llvm::Triple::ppc64: |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 6203 | if (Triple.isOSBinFormatELF()) |
| 6204 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 6205 | else |
| 6206 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 6207 | case llvm::Triple::ppc64le: |
| 6208 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
| 6209 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6210 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 6211 | case llvm::Triple::nvptx: |
| 6212 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6213 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6214 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6215 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6216 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6217 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6218 | case llvm::Triple::systemz: |
| 6219 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 6220 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6221 | case llvm::Triple::tce: |
| 6222 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 6223 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6224 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6225 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 6226 | bool IsSmallStructInRegABI = |
| 6227 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 6228 | bool IsWin32FloatStructABI = Triple.isWindowsMSVCEnvironment(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 6229 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6230 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 6231 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6232 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6233 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6234 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6235 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6236 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6237 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6238 | new X86_32TargetCodeGenInfo(Types, |
| 6239 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6240 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 6241 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6242 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6243 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6244 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6245 | case llvm::Triple::x86_64: { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6246 | bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6247 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6248 | switch (Triple.getOS()) { |
| 6249 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 31ea2f1 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 6250 | case llvm::Triple::MinGW32: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6251 | case llvm::Triple::Cygwin: |
| 6252 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6253 | case llvm::Triple::NaCl: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6254 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 6255 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6256 | default: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6257 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 6258 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6259 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6260 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6261 | case llvm::Triple::hexagon: |
| 6262 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6263 | case llvm::Triple::sparcv9: |
| 6264 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6265 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6266 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6267 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6268 | } |