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()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 371 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 372 | it != ie; ++it) |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 373 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 376 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 377 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 380 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 381 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 382 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 383 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 387 | CodeGenFunction &CGF) const { |
| 388 | return 0; |
| 389 | } |
| 390 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 391 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 392 | if (isAggregateTypeForABI(Ty)) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 393 | // Records with non-trivial destructors/constructors should not be passed |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 394 | // by value. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 395 | if (isRecordReturnIndirect(Ty, getCXXABI())) |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 396 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 397 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 398 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 399 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 401 | // Treat an enum type as its underlying type. |
| 402 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 403 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 405 | return (Ty->isPromotableIntegerType() ? |
| 406 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 409 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 410 | if (RetTy->isVoidType()) |
| 411 | return ABIArgInfo::getIgnore(); |
| 412 | |
| 413 | if (isAggregateTypeForABI(RetTy)) |
| 414 | return ABIArgInfo::getIndirect(0); |
| 415 | |
| 416 | // Treat an enum type as its underlying type. |
| 417 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 418 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 419 | |
| 420 | return (RetTy->isPromotableIntegerType() ? |
| 421 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 422 | } |
| 423 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 424 | //===----------------------------------------------------------------------===// |
| 425 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 426 | // |
| 427 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 428 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 429 | //===----------------------------------------------------------------------===// |
| 430 | |
| 431 | class PNaClABIInfo : public ABIInfo { |
| 432 | public: |
| 433 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 434 | |
| 435 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 436 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 437 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 438 | void computeInfo(CGFunctionInfo &FI) const override; |
| 439 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 440 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 444 | public: |
| 445 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 446 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 447 | }; |
| 448 | |
| 449 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 450 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 451 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 452 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 453 | it != ie; ++it) |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 454 | it->info = classifyArgumentType(it->type); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 458 | CodeGenFunction &CGF) const { |
| 459 | return 0; |
| 460 | } |
| 461 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 462 | /// \brief Classify argument of given type \p Ty. |
| 463 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 464 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 465 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 466 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 467 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 468 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 469 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 470 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 471 | } else if (Ty->isFloatingType()) { |
| 472 | // Floating-point types don't go inreg. |
| 473 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 474 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 475 | |
| 476 | return (Ty->isPromotableIntegerType() ? |
| 477 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 481 | if (RetTy->isVoidType()) |
| 482 | return ABIArgInfo::getIgnore(); |
| 483 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 484 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 485 | if (isAggregateTypeForABI(RetTy)) |
| 486 | return ABIArgInfo::getIndirect(0); |
| 487 | |
| 488 | // Treat an enum type as its underlying type. |
| 489 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 490 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 491 | |
| 492 | return (RetTy->isPromotableIntegerType() ? |
| 493 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 494 | } |
| 495 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 496 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 497 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 498 | // 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] | 499 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 500 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 501 | IRType->getScalarSizeInBits() != 64; |
| 502 | } |
| 503 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 504 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 505 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 506 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 507 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 508 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 509 | // Invalid MMX constraint |
| 510 | return 0; |
| 511 | } |
| 512 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 513 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 517 | return Ty; |
| 518 | } |
| 519 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 520 | //===----------------------------------------------------------------------===// |
| 521 | // X86-32 ABI Implementation |
| 522 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 523 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 524 | /// \brief Similar to llvm::CCState, but for Clang. |
| 525 | struct CCState { |
| 526 | CCState(unsigned CC) : CC(CC), FreeRegs(0) {} |
| 527 | |
| 528 | unsigned CC; |
| 529 | unsigned FreeRegs; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 530 | unsigned StackOffset; |
| 531 | bool UseInAlloca; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 534 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 535 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 536 | enum Class { |
| 537 | Integer, |
| 538 | Float |
| 539 | }; |
| 540 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 541 | static const unsigned MinABIStackAlignInBytes = 4; |
| 542 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 543 | bool IsDarwinVectorABI; |
| 544 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 545 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 546 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 547 | |
| 548 | static bool isRegisterSize(unsigned Size) { |
| 549 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 550 | } |
| 551 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 552 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 553 | bool IsInstanceMethod) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 554 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 555 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 556 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 557 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 558 | |
| 559 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 561 | /// \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] | 562 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 563 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 564 | Class classify(QualType Ty) const; |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 565 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State, |
| 566 | bool IsInstanceMethod) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 567 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 568 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 569 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 570 | /// \brief Rewrite the function info so that all memory arguments use |
| 571 | /// inalloca. |
| 572 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 573 | |
| 574 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 575 | unsigned &StackOffset, ABIArgInfo &Info, |
| 576 | QualType Type) const; |
| 577 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 578 | public: |
| 579 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 580 | void computeInfo(CGFunctionInfo &FI) const override; |
| 581 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 582 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 583 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 584 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 585 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 586 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 587 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 588 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 589 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 590 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 591 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 592 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 593 | bool d, bool p, bool w, unsigned r) |
| 594 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 595 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 596 | static bool isStructReturnInRegABI( |
| 597 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 598 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 599 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 600 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 601 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 602 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 603 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 604 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 605 | return 4; |
| 606 | } |
| 607 | |
| 608 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 609 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 610 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 611 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 612 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 613 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 614 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 615 | } |
| 616 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 617 | llvm::Constant * |
| 618 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 619 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 620 | (0x06 << 8) | // .+0x08 |
| 621 | ('F' << 16) | |
| 622 | ('T' << 24); |
| 623 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 624 | } |
| 625 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 629 | |
| 630 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 631 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 632 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 633 | bool IsInstanceMethod) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 634 | uint64_t Size = Context.getTypeSize(Ty); |
| 635 | |
| 636 | // Type must be register sized. |
| 637 | if (!isRegisterSize(Size)) |
| 638 | return false; |
| 639 | |
| 640 | if (Ty->isVectorType()) { |
| 641 | // 64- and 128- bit vectors inside structures are not returned in |
| 642 | // registers. |
| 643 | if (Size == 64 || Size == 128) |
| 644 | return false; |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 649 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 650 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 651 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 652 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 653 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 654 | return true; |
| 655 | |
| 656 | // Arrays are treated like records. |
| 657 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 658 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 659 | IsInstanceMethod); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 660 | |
| 661 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 662 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 663 | if (!RT) return false; |
| 664 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 665 | // FIXME: Traverse bases here too. |
| 666 | |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 667 | // For thiscall conventions, structures will never be returned in |
| 668 | // a register. This is for compatibility with the MSVC ABI |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 669 | if (IsWin32StructABI && IsInstanceMethod && RT->isStructureType()) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 670 | return false; |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 671 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 672 | // Structure types are passed in register if all fields would be |
| 673 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 674 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 675 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 676 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 677 | continue; |
| 678 | |
| 679 | // Check fields recursively. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 680 | if (!shouldReturnTypeInRegister(FD->getType(), Context, IsInstanceMethod)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 681 | return false; |
| 682 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 686 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 687 | // If the return value is indirect, then the hidden argument is consuming one |
| 688 | // integer register. |
| 689 | if (State.FreeRegs) { |
| 690 | --State.FreeRegs; |
| 691 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 692 | } |
| 693 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 694 | } |
| 695 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 696 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State, |
| 697 | bool IsInstanceMethod) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 698 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 699 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 701 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 702 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 703 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 704 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 705 | |
| 706 | // 128-bit vectors are a special case; they are returned in |
| 707 | // registers and we need to make sure to pick a type the LLVM |
| 708 | // backend will like. |
| 709 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 710 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 711 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 712 | |
| 713 | // Always return in register if it fits in a general purpose |
| 714 | // register, or if it is 64 bits and has a single element. |
| 715 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 716 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 717 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 718 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 719 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 720 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 724 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 725 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 726 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 727 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 728 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 729 | return getIndirectReturnResult(State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 731 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 732 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 733 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 734 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 735 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 736 | // If specified, structs and unions are always indirect. |
| 737 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 738 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 739 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 740 | // Small structures which are register sized are generally returned |
| 741 | // in a register. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 742 | if (shouldReturnTypeInRegister(RetTy, getContext(), IsInstanceMethod)) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 743 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 744 | |
| 745 | // As a special-case, if the struct is a "single-element" struct, and |
| 746 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 747 | // floating-point register. (MSVC does not apply this special case.) |
| 748 | // We apply a similar transformation for pointer types to improve the |
| 749 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 750 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 751 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 752 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 753 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 754 | |
| 755 | // FIXME: We should be able to narrow this integer in cases with dead |
| 756 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 757 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 760 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 761 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 762 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 763 | // Treat an enum type as its underlying type. |
| 764 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 765 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 766 | |
| 767 | return (RetTy->isPromotableIntegerType() ? |
| 768 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 771 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 772 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 773 | } |
| 774 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 775 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 776 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 777 | if (!RT) |
| 778 | return 0; |
| 779 | const RecordDecl *RD = RT->getDecl(); |
| 780 | |
| 781 | // If this is a C++ record, check the bases first. |
| 782 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 783 | for (const auto &I : CXXRD->bases()) |
| 784 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 785 | return false; |
| 786 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 787 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 788 | QualType FT = i->getType(); |
| 789 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 790 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 791 | return true; |
| 792 | |
| 793 | if (isRecordWithSSEVectorType(Context, FT)) |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | return false; |
| 798 | } |
| 799 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 800 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 801 | unsigned Align) const { |
| 802 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 803 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 804 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 805 | return 0; // Use default alignment. |
| 806 | |
| 807 | // On non-Darwin, the stack type alignment is always 4. |
| 808 | if (!IsDarwinVectorABI) { |
| 809 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 810 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 811 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 812 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 813 | // 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] | 814 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 815 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 816 | return 16; |
| 817 | |
| 818 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 821 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 822 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 823 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 824 | if (State.FreeRegs) { |
| 825 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 826 | return ABIArgInfo::getIndirectInReg(0, false); |
| 827 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 828 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 829 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 831 | // Compute the byval alignment. |
| 832 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 833 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 834 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 835 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 836 | |
| 837 | // If the stack alignment is less than the type alignment, realign the |
| 838 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 839 | bool Realign = TypeAlign > StackAlign; |
| 840 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 843 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 844 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 845 | if (!T) |
| 846 | T = Ty.getTypePtr(); |
| 847 | |
| 848 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 849 | BuiltinType::Kind K = BT->getKind(); |
| 850 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 851 | return Float; |
| 852 | } |
| 853 | return Integer; |
| 854 | } |
| 855 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 856 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 857 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 858 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 859 | Class C = classify(Ty); |
| 860 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 861 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 862 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 863 | unsigned Size = getContext().getTypeSize(Ty); |
| 864 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 865 | |
| 866 | if (SizeInRegs == 0) |
| 867 | return false; |
| 868 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 869 | if (SizeInRegs > State.FreeRegs) { |
| 870 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 871 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 872 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 873 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 874 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 875 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 876 | if (State.CC == llvm::CallingConv::X86_FastCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 877 | if (Size > 32) |
| 878 | return false; |
| 879 | |
| 880 | if (Ty->isIntegralOrEnumerationType()) |
| 881 | return true; |
| 882 | |
| 883 | if (Ty->isPointerType()) |
| 884 | return true; |
| 885 | |
| 886 | if (Ty->isReferenceType()) |
| 887 | return true; |
| 888 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 889 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 890 | NeedsPadding = true; |
| 891 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 892 | return false; |
| 893 | } |
| 894 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 895 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 898 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 899 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 900 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 901 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 902 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 903 | // Check with the C++ ABI first. |
| 904 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 905 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 906 | return getIndirectResult(Ty, false, State); |
| 907 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 908 | // The field index doesn't matter, we'll fix it up later. |
| 909 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 910 | } |
| 911 | |
| 912 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 913 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 914 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 915 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 916 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 917 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 918 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 919 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 920 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 921 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 922 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 923 | return ABIArgInfo::getIgnore(); |
| 924 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 925 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 926 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 927 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 928 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 929 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 930 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 931 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 932 | return ABIArgInfo::getDirectInReg(Result); |
| 933 | } |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 934 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 935 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 936 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 937 | // of those arguments will match the struct. This is important because the |
| 938 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 939 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 940 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 941 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 942 | return ABIArgInfo::getExpandWithPadding( |
| 943 | State.CC == llvm::CallingConv::X86_FastCall, PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 944 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 945 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 948 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 949 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 950 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 951 | if (IsDarwinVectorABI) { |
| 952 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 953 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 954 | (Size == 64 && VT->getNumElements() == 1)) |
| 955 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 956 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 957 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 958 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 959 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 960 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 961 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 962 | return ABIArgInfo::getDirect(); |
| 963 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 964 | |
| 965 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 966 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 967 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 968 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 969 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 970 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 971 | |
| 972 | if (Ty->isPromotableIntegerType()) { |
| 973 | if (InReg) |
| 974 | return ABIArgInfo::getExtendInReg(); |
| 975 | return ABIArgInfo::getExtend(); |
| 976 | } |
| 977 | if (InReg) |
| 978 | return ABIArgInfo::getDirectInReg(); |
| 979 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 982 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 983 | CCState State(FI.getCallingConvention()); |
| 984 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 985 | State.FreeRegs = 2; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 986 | else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 987 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 988 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 989 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 990 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 991 | FI.getReturnInfo() = |
| 992 | classifyReturnType(FI.getReturnType(), State, FI.isInstanceMethod()); |
| 993 | |
| 994 | // On win32, use the x86_cdeclmethodcc convention for cdecl methods that use |
| 995 | // sret. This convention swaps the order of the first two parameters behind |
| 996 | // the scenes to match MSVC. |
| 997 | if (IsWin32StructABI && FI.isInstanceMethod() && |
| 998 | FI.getCallingConvention() == llvm::CallingConv::C && |
| 999 | FI.getReturnInfo().isIndirect()) |
| 1000 | FI.setEffectiveCallingConvention(llvm::CallingConv::X86_CDeclMethod); |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1001 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1002 | bool UsedInAlloca = false; |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1003 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1004 | it != ie; ++it) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1005 | it->info = classifyArgumentType(it->type, State); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1006 | UsedInAlloca |= (it->info.getKind() == ABIArgInfo::InAlloca); |
| 1007 | } |
| 1008 | |
| 1009 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1010 | // all the memory arguments to use inalloca. |
| 1011 | if (UsedInAlloca) |
| 1012 | rewriteWithInAlloca(FI); |
| 1013 | } |
| 1014 | |
| 1015 | void |
| 1016 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1017 | unsigned &StackOffset, |
| 1018 | ABIArgInfo &Info, QualType Type) const { |
| 1019 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1020 | // byte aligned. |
| 1021 | unsigned Align = 4U; |
| 1022 | if (Info.getKind() == ABIArgInfo::Indirect && Info.getIndirectByVal()) |
| 1023 | Align = std::max(Align, Info.getIndirectAlign()); |
| 1024 | if (StackOffset & (Align - 1)) { |
| 1025 | unsigned OldOffset = StackOffset; |
| 1026 | StackOffset = llvm::RoundUpToAlignment(StackOffset, Align); |
| 1027 | unsigned NumBytes = StackOffset - OldOffset; |
| 1028 | assert(NumBytes); |
| 1029 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1030 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1031 | FrameFields.push_back(Ty); |
| 1032 | } |
| 1033 | |
| 1034 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1035 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1036 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1037 | } |
| 1038 | |
| 1039 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1040 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1041 | |
| 1042 | // Build a packed struct type for all of the arguments in memory. |
| 1043 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1044 | |
| 1045 | unsigned StackOffset = 0; |
| 1046 | |
| 1047 | // Put the sret parameter into the inalloca struct if it's in memory. |
| 1048 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1049 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1050 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1051 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1052 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1053 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // Skip the 'this' parameter in ecx. |
| 1057 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1058 | if (FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall) |
| 1059 | ++I; |
| 1060 | |
| 1061 | // Put arguments passed in memory into the struct. |
| 1062 | for (; I != E; ++I) { |
| 1063 | |
| 1064 | // Leave ignored and inreg arguments alone. |
| 1065 | switch (I->info.getKind()) { |
| 1066 | case ABIArgInfo::Indirect: |
| 1067 | assert(I->info.getIndirectByVal()); |
| 1068 | break; |
| 1069 | case ABIArgInfo::Ignore: |
| 1070 | continue; |
| 1071 | case ABIArgInfo::Direct: |
| 1072 | case ABIArgInfo::Extend: |
| 1073 | if (I->info.getInReg()) |
| 1074 | continue; |
| 1075 | break; |
| 1076 | default: |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1081 | } |
| 1082 | |
| 1083 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1084 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1087 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1088 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1089 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1090 | |
| 1091 | CGBuilderTy &Builder = CGF.Builder; |
| 1092 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1093 | "ap"); |
| 1094 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Compute if the address needs to be aligned |
| 1097 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1098 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1099 | Align = std::max(Align, 4U); |
| 1100 | if (Align > 4) { |
| 1101 | // addr = (addr + align - 1) & -align; |
| 1102 | llvm::Value *Offset = |
| 1103 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1104 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1105 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1106 | CGF.Int32Ty); |
| 1107 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1108 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1109 | Addr->getType(), |
| 1110 | "ap.cur.aligned"); |
| 1111 | } |
| 1112 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1113 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1114 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1115 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1116 | |
| 1117 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1118 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1119 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1120 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1121 | "ap.next"); |
| 1122 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1123 | |
| 1124 | return AddrTyped; |
| 1125 | } |
| 1126 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1127 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1128 | llvm::GlobalValue *GV, |
| 1129 | CodeGen::CodeGenModule &CGM) const { |
| 1130 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1131 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1132 | // Get the LLVM function. |
| 1133 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1134 | |
| 1135 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1136 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1137 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1138 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1139 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1140 | llvm::AttributeSet::FunctionIndex, |
| 1141 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1146 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1147 | CodeGen::CodeGenFunction &CGF, |
| 1148 | llvm::Value *Address) const { |
| 1149 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1151 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1152 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1153 | // 0-7 are the eight integer registers; the order is different |
| 1154 | // on Darwin (for EH), but the range is the same. |
| 1155 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1156 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1157 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1158 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1159 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1160 | // These have size 16, which is sizeof(long double) on |
| 1161 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1162 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1163 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1164 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1165 | } else { |
| 1166 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1167 | // reason. |
| 1168 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1169 | |
| 1170 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1171 | // These have size 12, which is sizeof(long double) on |
| 1172 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1173 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1174 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1175 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1176 | |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1180 | //===----------------------------------------------------------------------===// |
| 1181 | // X86-64 ABI Implementation |
| 1182 | //===----------------------------------------------------------------------===// |
| 1183 | |
| 1184 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1185 | namespace { |
| 1186 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1187 | class X86_64ABIInfo : public ABIInfo { |
| 1188 | enum Class { |
| 1189 | Integer = 0, |
| 1190 | SSE, |
| 1191 | SSEUp, |
| 1192 | X87, |
| 1193 | X87Up, |
| 1194 | ComplexX87, |
| 1195 | NoClass, |
| 1196 | Memory |
| 1197 | }; |
| 1198 | |
| 1199 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1200 | /// |
| 1201 | /// Merge an accumulating classification \arg Accum with a field |
| 1202 | /// classification \arg Field. |
| 1203 | /// |
| 1204 | /// \param Accum - The accumulating classification. This should |
| 1205 | /// always be either NoClass or the result of a previous merge |
| 1206 | /// call. In addition, this should never be Memory (the caller |
| 1207 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1208 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1209 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1210 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1211 | /// |
| 1212 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1213 | /// final MEMORY or SSE classes when necessary. |
| 1214 | /// |
| 1215 | /// \param AggregateSize - The size of the current aggregate in |
| 1216 | /// the classification process. |
| 1217 | /// |
| 1218 | /// \param Lo - The classification for the parts of the type |
| 1219 | /// residing in the low word of the containing object. |
| 1220 | /// |
| 1221 | /// \param Hi - The classification for the parts of the type |
| 1222 | /// residing in the higher words of the containing object. |
| 1223 | /// |
| 1224 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1225 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1226 | /// classify - Determine the x86_64 register classes in which the |
| 1227 | /// given type T should be passed. |
| 1228 | /// |
| 1229 | /// \param Lo - The classification for the parts of the type |
| 1230 | /// residing in the low word of the containing object. |
| 1231 | /// |
| 1232 | /// \param Hi - The classification for the parts of the type |
| 1233 | /// residing in the high word of the containing object. |
| 1234 | /// |
| 1235 | /// \param OffsetBase - The bit offset of this type in the |
| 1236 | /// containing object. Some parameters are classified different |
| 1237 | /// depending on whether they straddle an eightbyte boundary. |
| 1238 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1239 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1240 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1241 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1242 | /// If a word is unused its result will be NoClass; if a type should |
| 1243 | /// be passed in Memory then at least the classification of \arg Lo |
| 1244 | /// will be Memory. |
| 1245 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1246 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1247 | /// |
| 1248 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1249 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1250 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1251 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1252 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1253 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1254 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1255 | unsigned IROffset, QualType SourceTy, |
| 1256 | unsigned SourceOffset) const; |
| 1257 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1258 | unsigned IROffset, QualType SourceTy, |
| 1259 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1260 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1261 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1262 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1263 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1264 | |
| 1265 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1266 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1267 | /// |
| 1268 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1269 | /// available. |
| 1270 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1272 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1273 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1274 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1275 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1276 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1277 | unsigned &neededSSE, |
| 1278 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1279 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1280 | bool IsIllegalVectorType(QualType Ty) const; |
| 1281 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1282 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1283 | /// unfortunately in ways that were not always consistent with |
| 1284 | /// certain previous compilers. In particular, platforms which |
| 1285 | /// required strict binary compatibility with older versions of GCC |
| 1286 | /// may need to exempt themselves. |
| 1287 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1288 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1291 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1292 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1293 | // 64-bit hardware. |
| 1294 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1295 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1296 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1297 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1298 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1299 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1300 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1301 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1302 | bool isPassedUsingAVXType(QualType type) const { |
| 1303 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1304 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1305 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1306 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1307 | if (info.isDirect()) { |
| 1308 | llvm::Type *ty = info.getCoerceToType(); |
| 1309 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1310 | return (vectorTy->getBitWidth() > 128); |
| 1311 | } |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1315 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1316 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1317 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1318 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1319 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1321 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1322 | class WinX86_64ABIInfo : public ABIInfo { |
| 1323 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1324 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1326 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1327 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1328 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1329 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1330 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1331 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1332 | CodeGenFunction &CGF) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1333 | }; |
| 1334 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1335 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1336 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1337 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1338 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1339 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1340 | const X86_64ABIInfo &getABIInfo() const { |
| 1341 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1342 | } |
| 1343 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1344 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1345 | return 7; |
| 1346 | } |
| 1347 | |
| 1348 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1349 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1350 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1351 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1352 | // 0-15 are the 16 integer registers. |
| 1353 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1354 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1355 | return false; |
| 1356 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1357 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1358 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1359 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1360 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1361 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1362 | } |
| 1363 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1364 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1365 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1366 | // The default CC on x86-64 sets %al to the number of SSA |
| 1367 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1368 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1369 | // that when AVX types are involved: the ABI explicitly states it is |
| 1370 | // undefined, and it doesn't work in practice because of how the ABI |
| 1371 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1372 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1373 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1374 | for (CallArgList::const_iterator |
| 1375 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1376 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1377 | HasAVXType = true; |
| 1378 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1381 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1382 | if (!HasAVXType) |
| 1383 | return true; |
| 1384 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1385 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1386 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1389 | llvm::Constant * |
| 1390 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1391 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1392 | (0x0a << 8) | // .+0x0c |
| 1393 | ('F' << 16) | |
| 1394 | ('T' << 24); |
| 1395 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1396 | } |
| 1397 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1398 | }; |
| 1399 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1400 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1401 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1402 | // matches the behavior of MSVC. |
| 1403 | std::string ArgStr = Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1404 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1405 | ArgStr += ".lib"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1406 | return ArgStr; |
| 1407 | } |
| 1408 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1409 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1410 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1411 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1412 | bool d, bool p, bool w, unsigned RegParms) |
| 1413 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1414 | |
| 1415 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1416 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1417 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1418 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1419 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1420 | |
| 1421 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1422 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1423 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1424 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1425 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1426 | }; |
| 1427 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1428 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1429 | public: |
| 1430 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1431 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1432 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1433 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1434 | return 7; |
| 1435 | } |
| 1436 | |
| 1437 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1438 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1439 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1441 | // 0-15 are the 16 integer registers. |
| 1442 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1443 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1446 | |
| 1447 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1448 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1449 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1450 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1451 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1452 | |
| 1453 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1454 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1455 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1456 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1457 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1458 | }; |
| 1459 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1462 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1463 | Class &Hi) const { |
| 1464 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1465 | // |
| 1466 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1467 | // memory. |
| 1468 | // |
| 1469 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1470 | // memory. |
| 1471 | // |
| 1472 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1473 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1474 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1475 | // ABI working for processors that don't support the __m256 type. |
| 1476 | // |
| 1477 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1478 | // |
| 1479 | // Some of these are enforced by the merging logic. Others can arise |
| 1480 | // only with unions; for example: |
| 1481 | // union { _Complex double; unsigned; } |
| 1482 | // |
| 1483 | // Note that clauses (b) and (c) were added in 0.98. |
| 1484 | // |
| 1485 | if (Hi == Memory) |
| 1486 | Lo = Memory; |
| 1487 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1488 | Lo = Memory; |
| 1489 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1490 | Lo = Memory; |
| 1491 | if (Hi == SSEUp && Lo != SSE) |
| 1492 | Hi = SSE; |
| 1493 | } |
| 1494 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1495 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1496 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1497 | // classified recursively so that always two fields are |
| 1498 | // considered. The resulting class is calculated according to |
| 1499 | // the classes of the fields in the eightbyte: |
| 1500 | // |
| 1501 | // (a) If both classes are equal, this is the resulting class. |
| 1502 | // |
| 1503 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1504 | // the other class. |
| 1505 | // |
| 1506 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1507 | // class. |
| 1508 | // |
| 1509 | // (d) If one of the classes is INTEGER, the result is the |
| 1510 | // INTEGER. |
| 1511 | // |
| 1512 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1513 | // MEMORY is used as class. |
| 1514 | // |
| 1515 | // (f) Otherwise class SSE is used. |
| 1516 | |
| 1517 | // Accum should never be memory (we should have returned) or |
| 1518 | // ComplexX87 (because this cannot be passed in a structure). |
| 1519 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1520 | "Invalid accumulated classification during merge."); |
| 1521 | if (Accum == Field || Field == NoClass) |
| 1522 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1523 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1524 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1525 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1526 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1527 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1528 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1529 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1530 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1531 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1532 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1535 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1536 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1537 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1538 | // Class pairs with appropriate constructor methods for the various |
| 1539 | // situations. |
| 1540 | |
| 1541 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1542 | // shouldn't be passed in registers for example, so there is no chance they |
| 1543 | // can straddle an eightbyte. Verify & simplify. |
| 1544 | |
| 1545 | Lo = Hi = NoClass; |
| 1546 | |
| 1547 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1548 | Current = Memory; |
| 1549 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1550 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1551 | BuiltinType::Kind k = BT->getKind(); |
| 1552 | |
| 1553 | if (k == BuiltinType::Void) { |
| 1554 | Current = NoClass; |
| 1555 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1556 | Lo = Integer; |
| 1557 | Hi = Integer; |
| 1558 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1559 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1560 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1561 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1562 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1563 | Current = SSE; |
| 1564 | } else if (k == BuiltinType::LongDouble) { |
| 1565 | Lo = X87; |
| 1566 | Hi = X87Up; |
| 1567 | } |
| 1568 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1569 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1570 | return; |
| 1571 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1573 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1574 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1575 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
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->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1580 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1581 | return; |
| 1582 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1584 | if (Ty->isMemberPointerType()) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1585 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1586 | Lo = Hi = Integer; |
| 1587 | else |
| 1588 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1591 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1592 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1593 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1594 | if (Size == 32) { |
| 1595 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1596 | // float> as integer. |
| 1597 | Current = Integer; |
| 1598 | |
| 1599 | // If this type crosses an eightbyte boundary, it should be |
| 1600 | // split. |
| 1601 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1602 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1603 | if (EB_Real != EB_Imag) |
| 1604 | Hi = Lo; |
| 1605 | } else if (Size == 64) { |
| 1606 | // gcc passes <1 x double> in memory. :( |
| 1607 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1608 | return; |
| 1609 | |
| 1610 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1611 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1612 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1613 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1614 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1615 | Current = Integer; |
| 1616 | else |
| 1617 | Current = SSE; |
| 1618 | |
| 1619 | // If this type crosses an eightbyte boundary, it should be |
| 1620 | // split. |
| 1621 | if (OffsetBase && OffsetBase != 64) |
| 1622 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1623 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1624 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1625 | // least significant one belongs to class SSE and all the others to class |
| 1626 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1627 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1628 | // This design isn't correct for 256-bits, but since there're no cases |
| 1629 | // where the upper parts would need to be inspected, avoid adding |
| 1630 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1631 | // |
| 1632 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1633 | // registers if they are "named", i.e. not part of the "..." of a |
| 1634 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1635 | Lo = SSE; |
| 1636 | Hi = SSEUp; |
| 1637 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1638 | return; |
| 1639 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1641 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1642 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1644 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1645 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1646 | if (Size <= 64) |
| 1647 | Current = Integer; |
| 1648 | else if (Size <= 128) |
| 1649 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1650 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1651 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1652 | else if (ET == getContext().DoubleTy || |
| 1653 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1654 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1655 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1656 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1657 | Current = ComplexX87; |
| 1658 | |
| 1659 | // If this complex type crosses an eightbyte boundary then it |
| 1660 | // should be split. |
| 1661 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1662 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1663 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1664 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1666 | return; |
| 1667 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1669 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1670 | // Arrays are treated like structures. |
| 1671 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1672 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1673 | |
| 1674 | // 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] | 1675 | // than four eightbytes, ..., it has class MEMORY. |
| 1676 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1677 | return; |
| 1678 | |
| 1679 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1680 | // fields, it has class MEMORY. |
| 1681 | // |
| 1682 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1683 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1684 | return; |
| 1685 | |
| 1686 | // Otherwise implement simplified merge. We could be smarter about |
| 1687 | // this, but it isn't worth it and would be harder to verify. |
| 1688 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1689 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1690 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1691 | |
| 1692 | // The only case a 256-bit wide vector could be used is when the array |
| 1693 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1694 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1695 | if (Size > 128 && EltSize != 256) |
| 1696 | return; |
| 1697 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1698 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1699 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1700 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1701 | Lo = merge(Lo, FieldLo); |
| 1702 | Hi = merge(Hi, FieldHi); |
| 1703 | if (Lo == Memory || Hi == Memory) |
| 1704 | break; |
| 1705 | } |
| 1706 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1707 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1708 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1709 | return; |
| 1710 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1711 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1712 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1713 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1714 | |
| 1715 | // 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] | 1716 | // than four eightbytes, ..., it has class MEMORY. |
| 1717 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1718 | return; |
| 1719 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1720 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1721 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1722 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1723 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1724 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1725 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1726 | const RecordDecl *RD = RT->getDecl(); |
| 1727 | |
| 1728 | // Assume variable sized types are passed in memory. |
| 1729 | if (RD->hasFlexibleArrayMember()) |
| 1730 | return; |
| 1731 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1732 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1733 | |
| 1734 | // Reset Lo class, this will be recomputed. |
| 1735 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1736 | |
| 1737 | // If this is a C++ record, classify the bases first. |
| 1738 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1739 | for (const auto &I : CXXRD->bases()) { |
| 1740 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1741 | "Unexpected base class!"); |
| 1742 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1743 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Classify this field. |
| 1746 | // |
| 1747 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1748 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1749 | // initialized to class NO_CLASS. |
| 1750 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1751 | uint64_t Offset = |
| 1752 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1753 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1754 | Lo = merge(Lo, FieldLo); |
| 1755 | Hi = merge(Hi, FieldHi); |
| 1756 | if (Lo == Memory || Hi == Memory) |
| 1757 | break; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1762 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1763 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1764 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1765 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1766 | bool BitField = i->isBitField(); |
| 1767 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1768 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1769 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1770 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1771 | // The only case a 256-bit wide vector could be used is when the struct |
| 1772 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1773 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1774 | // |
| 1775 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1776 | Lo = Memory; |
| 1777 | return; |
| 1778 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1779 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1780 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1781 | Lo = Memory; |
| 1782 | return; |
| 1783 | } |
| 1784 | |
| 1785 | // Classify this field. |
| 1786 | // |
| 1787 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1788 | // exceeds a single eightbyte, each is classified |
| 1789 | // separately. Each eightbyte gets initialized to class |
| 1790 | // NO_CLASS. |
| 1791 | Class FieldLo, FieldHi; |
| 1792 | |
| 1793 | // Bit-fields require special handling, they do not force the |
| 1794 | // structure to be passed in memory even if unaligned, and |
| 1795 | // therefore they can straddle an eightbyte. |
| 1796 | if (BitField) { |
| 1797 | // Ignore padding bit-fields. |
| 1798 | if (i->isUnnamedBitfield()) |
| 1799 | continue; |
| 1800 | |
| 1801 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1802 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1803 | |
| 1804 | uint64_t EB_Lo = Offset / 64; |
| 1805 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 1806 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1807 | if (EB_Lo) { |
| 1808 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1809 | FieldLo = NoClass; |
| 1810 | FieldHi = Integer; |
| 1811 | } else { |
| 1812 | FieldLo = Integer; |
| 1813 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1814 | } |
| 1815 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1816 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1817 | Lo = merge(Lo, FieldLo); |
| 1818 | Hi = merge(Hi, FieldHi); |
| 1819 | if (Lo == Memory || Hi == Memory) |
| 1820 | break; |
| 1821 | } |
| 1822 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1823 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1827 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1828 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1829 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1830 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1831 | // Treat an enum type as its underlying type. |
| 1832 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1833 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1834 | |
| 1835 | return (Ty->isPromotableIntegerType() ? |
| 1836 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1837 | } |
| 1838 | |
| 1839 | return ABIArgInfo::getIndirect(0); |
| 1840 | } |
| 1841 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1842 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1843 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1844 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1845 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1846 | if (Size <= 64 || Size > LargestVector) |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
| 1850 | return false; |
| 1851 | } |
| 1852 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1853 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1854 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1855 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1856 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1857 | // |
| 1858 | // This assumption is optimistic, as there could be free registers available |
| 1859 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1860 | // the argument in the free register. This does not seem to happen currently, |
| 1861 | // but this code would be much safer if we could mark the argument with |
| 1862 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1863 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1864 | // Treat an enum type as its underlying type. |
| 1865 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1866 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1867 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1868 | return (Ty->isPromotableIntegerType() ? |
| 1869 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1870 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1871 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1872 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1873 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1875 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1876 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1877 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1878 | |
| 1879 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1880 | // is important for good codegen. |
| 1881 | // |
| 1882 | // We do this by coercing the value into a scalar type which the backend can |
| 1883 | // handle naturally (i.e., without using byval). |
| 1884 | // |
| 1885 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1886 | // free integer registers. Doing this when there are free integer registers |
| 1887 | // would require more care, as we would have to ensure that the coerced value |
| 1888 | // did not claim the unused register. That would require either reording the |
| 1889 | // arguments to the function (so that any subsequent inreg values came first), |
| 1890 | // or only doing this optimization when there were no following arguments that |
| 1891 | // might be inreg. |
| 1892 | // |
| 1893 | // We currently expect it to be rare (particularly in well written code) for |
| 1894 | // arguments to be passed on the stack when there are still free integer |
| 1895 | // registers available (this would typically imply large structs being passed |
| 1896 | // by value), so this seems like a fair tradeoff for now. |
| 1897 | // |
| 1898 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1899 | // attributes. See PR12193. |
| 1900 | if (freeIntRegs == 0) { |
| 1901 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1902 | |
| 1903 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1904 | // type, which will end up on the stack (with alignment 8). |
| 1905 | if (Align == 8 && Size <= 64) |
| 1906 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1907 | Size)); |
| 1908 | } |
| 1909 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1910 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1913 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1914 | /// 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] | 1915 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1916 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1917 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1919 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1920 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1921 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1922 | while (STy && STy->getNumElements() == 1) { |
| 1923 | IRType = STy->getElementType(0); |
| 1924 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1925 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1926 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1927 | // 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] | 1928 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1929 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1930 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | 71f1b2d | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1931 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1932 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1933 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1934 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1935 | EltTy->isIntegerTy(128))) |
| 1936 | return VT; |
| 1937 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1939 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1940 | } |
| 1941 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1942 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1943 | /// is known to either be off the end of the specified type or being in |
| 1944 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1945 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1946 | /// classification that put one of the two halves in the INTEGER class. |
| 1947 | /// |
| 1948 | /// It is conservatively correct to return false. |
| 1949 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1950 | unsigned EndBit, ASTContext &Context) { |
| 1951 | // If the bytes being queried are off the end of the type, there is no user |
| 1952 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1953 | // types that don't contain interesting padding. |
| 1954 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1955 | if (TySize <= StartBit) |
| 1956 | return true; |
| 1957 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1958 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1959 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1960 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1961 | |
| 1962 | // Check each element to see if the element overlaps with the queried range. |
| 1963 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1964 | // If the element is after the span we care about, then we're done.. |
| 1965 | unsigned EltOffset = i*EltSize; |
| 1966 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1967 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1968 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1969 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1970 | EndBit-EltOffset, Context)) |
| 1971 | return false; |
| 1972 | } |
| 1973 | // If it overlaps no elements, then it is safe to process as padding. |
| 1974 | return true; |
| 1975 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1976 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1977 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1978 | const RecordDecl *RD = RT->getDecl(); |
| 1979 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1981 | // If this is a C++ record, check the bases first. |
| 1982 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1983 | for (const auto &I : CXXRD->bases()) { |
| 1984 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1985 | "Unexpected base class!"); |
| 1986 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1987 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1988 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1989 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1990 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1991 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1993 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1994 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1995 | EndBit-BaseOffset, Context)) |
| 1996 | return false; |
| 1997 | } |
| 1998 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2000 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2001 | // this could be sped up a lot by being smarter about queried fields, |
| 2002 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2003 | // much. |
| 2004 | unsigned idx = 0; |
| 2005 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2006 | i != e; ++i, ++idx) { |
| 2007 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2009 | // If we found a field after the region we care about, then we're done. |
| 2010 | if (FieldOffset >= EndBit) break; |
| 2011 | |
| 2012 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2013 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2014 | Context)) |
| 2015 | return false; |
| 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 | // If nothing in this record overlapped the area of interest, then we're |
| 2019 | // clean. |
| 2020 | return true; |
| 2021 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2023 | return false; |
| 2024 | } |
| 2025 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2026 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2027 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2028 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2029 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2030 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2031 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2032 | // Base case if we find a float. |
| 2033 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2034 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2036 | // 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] | 2037 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2038 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2039 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2040 | IROffset -= SL->getElementOffset(Elt); |
| 2041 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2042 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2043 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2044 | // 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] | 2045 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2046 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2047 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2048 | IROffset -= IROffset/EltSize*EltSize; |
| 2049 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2050 | } |
| 2051 | |
| 2052 | return false; |
| 2053 | } |
| 2054 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2055 | |
| 2056 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2057 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2058 | llvm::Type *X86_64ABIInfo:: |
| 2059 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2060 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2061 | // 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] | 2062 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2063 | // structs that contain 3 floats. |
| 2064 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2065 | SourceOffset*8+64, getContext())) |
| 2066 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2067 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2068 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2069 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2070 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2071 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2072 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2073 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2075 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2076 | } |
| 2077 | |
| 2078 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2079 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2080 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2081 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2082 | /// 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] | 2083 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2084 | /// etc). |
| 2085 | /// |
| 2086 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2087 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2088 | /// the 8-byte value references. PrefType may be null. |
| 2089 | /// |
| 2090 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 2091 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2092 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2093 | llvm::Type *X86_64ABIInfo:: |
| 2094 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2095 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2096 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2097 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2098 | if (IROffset == 0) { |
| 2099 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2100 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2101 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2102 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2103 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2104 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2105 | // goodness in the source type is just tail padding. This is allowed to |
| 2106 | // kick in for struct {double,int} on the int, but not on |
| 2107 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2108 | // have to do this analysis on the source type because we can't depend on |
| 2109 | // unions being lowered a specific way etc. |
| 2110 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2111 | IRType->isIntegerTy(32) || |
| 2112 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2113 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2114 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2115 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2116 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2117 | SourceOffset*8+64, getContext())) |
| 2118 | return IRType; |
| 2119 | } |
| 2120 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2122 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2123 | // 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] | 2124 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2125 | if (IROffset < SL->getSizeInBytes()) { |
| 2126 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2127 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2129 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2130 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2131 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2132 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2134 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2135 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2136 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2137 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2138 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2139 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2140 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2141 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2142 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2143 | // 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] | 2144 | unsigned TySizeInBytes = |
| 2145 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2147 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2149 | // It is always safe to classify this as an integer type up to i64 that |
| 2150 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2151 | return llvm::IntegerType::get(getVMContext(), |
| 2152 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2155 | |
| 2156 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2157 | /// be used as elements of a two register pair to pass or return, return a |
| 2158 | /// first class aggregate to represent them. For example, if the low part of |
| 2159 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2160 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2161 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2162 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2163 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2164 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2165 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2166 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2167 | // the second element at offset 8. Check for this: |
| 2168 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2169 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2170 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2171 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2173 | // To handle this, we have to increase the size of the low part so that the |
| 2174 | // second element will start at an 8 byte offset. We can't increase the size |
| 2175 | // of the second element because it might make us access off the end of the |
| 2176 | // struct. |
| 2177 | if (HiStart != 8) { |
| 2178 | // There are only two sorts of types the ABI generation code can produce for |
| 2179 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2180 | // Promote these to a larger type. |
| 2181 | if (Lo->isFloatTy()) |
| 2182 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2183 | else { |
| 2184 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2185 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2186 | } |
| 2187 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2188 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2189 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2190 | |
| 2191 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2192 | // Verify that the second element is at an 8-byte offset. |
| 2193 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2194 | "Invalid x86-64 argument pair!"); |
| 2195 | return Result; |
| 2196 | } |
| 2197 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2198 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2199 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2200 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2201 | // classification algorithm. |
| 2202 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2203 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2204 | |
| 2205 | // Check some invariants. |
| 2206 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2207 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2208 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2209 | llvm::Type *ResType = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2210 | switch (Lo) { |
| 2211 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2212 | if (Hi == NoClass) |
| 2213 | return ABIArgInfo::getIgnore(); |
| 2214 | // If the low part is just padding, it takes no register, leave ResType |
| 2215 | // null. |
| 2216 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2217 | "Unknown missing lo part"); |
| 2218 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2219 | |
| 2220 | case SSEUp: |
| 2221 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2222 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2223 | |
| 2224 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2225 | // hidden argument. |
| 2226 | case Memory: |
| 2227 | return getIndirectReturnResult(RetTy); |
| 2228 | |
| 2229 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2230 | // available register of the sequence %rax, %rdx is used. |
| 2231 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2232 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2234 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2235 | // so that the parameter gets the right LLVM IR attributes. |
| 2236 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2237 | // Treat an enum type as its underlying type. |
| 2238 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2239 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2240 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2241 | if (RetTy->isIntegralOrEnumerationType() && |
| 2242 | RetTy->isPromotableIntegerType()) |
| 2243 | return ABIArgInfo::getExtend(); |
| 2244 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2245 | break; |
| 2246 | |
| 2247 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2248 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2249 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2250 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2251 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2252 | |
| 2253 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2254 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2255 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2256 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2257 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2258 | |
| 2259 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2260 | // part of the value is returned in %st0 and the imaginary part in |
| 2261 | // %st1. |
| 2262 | case ComplexX87: |
| 2263 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2264 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2265 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2266 | NULL); |
| 2267 | break; |
| 2268 | } |
| 2269 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2270 | llvm::Type *HighPart = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2271 | switch (Hi) { |
| 2272 | // Memory was handled previously and X87 should |
| 2273 | // never occur as a hi class. |
| 2274 | case Memory: |
| 2275 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2276 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2277 | |
| 2278 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2279 | case NoClass: |
| 2280 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2281 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2282 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2283 | HighPart = GetINTEGERTypeAtOffset(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; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2287 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2288 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2289 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2290 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2291 | break; |
| 2292 | |
| 2293 | // 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] | 2294 | // is passed in the next available eightbyte chunk if the last used |
| 2295 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2296 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2297 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2298 | case SSEUp: |
| 2299 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2300 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | |
| 2303 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2304 | // returned together with the previous X87 value in %st0. |
| 2305 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2306 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2307 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2308 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2309 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2310 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2311 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2312 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2313 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2314 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2315 | break; |
| 2316 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2317 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2318 | // 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] | 2319 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2320 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2321 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2322 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2323 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2324 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2327 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2328 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2329 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2330 | const |
| 2331 | { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2332 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2333 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2334 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2335 | // Check some invariants. |
| 2336 | // FIXME: Enforce these by construction. |
| 2337 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2338 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2339 | |
| 2340 | neededInt = 0; |
| 2341 | neededSSE = 0; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2342 | llvm::Type *ResType = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2343 | switch (Lo) { |
| 2344 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2345 | if (Hi == NoClass) |
| 2346 | return ABIArgInfo::getIgnore(); |
| 2347 | // If the low part is just padding, it takes no register, leave ResType |
| 2348 | // null. |
| 2349 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2350 | "Unknown missing lo part"); |
| 2351 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2352 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2353 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2354 | // on the stack. |
| 2355 | case Memory: |
| 2356 | |
| 2357 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2358 | // COMPLEX_X87, it is passed in memory. |
| 2359 | case X87: |
| 2360 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2361 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2362 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2363 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2364 | |
| 2365 | case SSEUp: |
| 2366 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2367 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2368 | |
| 2369 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2370 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2371 | // and %r9 is used. |
| 2372 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2373 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2375 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2376 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2377 | |
| 2378 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2379 | // so that the parameter gets the right LLVM IR attributes. |
| 2380 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2381 | // Treat an enum type as its underlying type. |
| 2382 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2383 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2384 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2385 | if (Ty->isIntegralOrEnumerationType() && |
| 2386 | Ty->isPromotableIntegerType()) |
| 2387 | return ABIArgInfo::getExtend(); |
| 2388 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2389 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2390 | break; |
| 2391 | |
| 2392 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2393 | // available SSE register is used, the registers are taken in the |
| 2394 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2395 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2396 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2397 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2398 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2399 | break; |
| 2400 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2401 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2403 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2404 | switch (Hi) { |
| 2405 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2406 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2407 | // which is passed in memory. |
| 2408 | case Memory: |
| 2409 | case X87: |
| 2410 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2411 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2412 | |
| 2413 | case NoClass: break; |
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 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2416 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2417 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2418 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2420 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2421 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | |
| 2424 | // X87Up generally doesn't occur here (long double is passed in |
| 2425 | // memory), except in situations involving unions. |
| 2426 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2427 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2428 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2429 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2430 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2431 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2432 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2433 | ++neededSSE; |
| 2434 | break; |
| 2435 | |
| 2436 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2437 | // 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] | 2438 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2439 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2440 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2441 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2442 | break; |
| 2443 | } |
| 2444 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2445 | // If a high part was specified, merge it together with the low part. It is |
| 2446 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2447 | // first class struct aggregate with the high and low part: {low, high} |
| 2448 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2449 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2451 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2454 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2456 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2457 | |
| 2458 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2459 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2460 | |
| 2461 | // If the return value is indirect, then the hidden argument is consuming one |
| 2462 | // integer register. |
| 2463 | if (FI.getReturnInfo().isIndirect()) |
| 2464 | --freeIntRegs; |
| 2465 | |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2466 | bool isVariadic = FI.isVariadic(); |
| 2467 | unsigned numRequiredArgs = 0; |
| 2468 | if (isVariadic) |
| 2469 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2470 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2471 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2472 | // get assigned (in left-to-right order) for passing as follows... |
| 2473 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2474 | it != ie; ++it) { |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2475 | bool isNamedArg = true; |
| 2476 | if (isVariadic) |
Aaron Ballman | 6a30264 | 2013-06-12 15:03:45 +0000 | [diff] [blame] | 2477 | isNamedArg = (it - FI.arg_begin()) < |
| 2478 | static_cast<signed>(numRequiredArgs); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2479 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2480 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2481 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2482 | neededSSE, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2483 | |
| 2484 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2485 | // eightbyte of an argument, the whole argument is passed on the |
| 2486 | // stack. If registers have already been assigned for some |
| 2487 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2488 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2489 | freeIntRegs -= neededInt; |
| 2490 | freeSSERegs -= neededSSE; |
| 2491 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2492 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2498 | QualType Ty, |
| 2499 | CodeGenFunction &CGF) { |
| 2500 | llvm::Value *overflow_arg_area_p = |
| 2501 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2502 | llvm::Value *overflow_arg_area = |
| 2503 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2504 | |
| 2505 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2506 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2507 | // It isn't stated explicitly in the standard, but in practice we use |
| 2508 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2509 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2510 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2511 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2512 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2513 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2515 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2516 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2517 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2518 | overflow_arg_area = |
| 2519 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2520 | overflow_arg_area->getType(), |
| 2521 | "overflow_arg_area.align"); |
| 2522 | } |
| 2523 | |
| 2524 | // 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] | 2525 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2526 | llvm::Value *Res = |
| 2527 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2528 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2529 | |
| 2530 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2531 | // l->overflow_arg_area + sizeof(type). |
| 2532 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2533 | // an 8 byte boundary. |
| 2534 | |
| 2535 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2536 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2537 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2538 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2539 | "overflow_arg_area.next"); |
| 2540 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2541 | |
| 2542 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2543 | return Res; |
| 2544 | } |
| 2545 | |
| 2546 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2547 | CodeGenFunction &CGF) const { |
| 2548 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2549 | // struct { |
| 2550 | // i32 gp_offset; |
| 2551 | // i32 fp_offset; |
| 2552 | // i8* overflow_arg_area; |
| 2553 | // i8* reg_save_area; |
| 2554 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2555 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2556 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2557 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2558 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2559 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2560 | |
| 2561 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2562 | // in the registers. If not go to step 7. |
| 2563 | if (!neededInt && !neededSSE) |
| 2564 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2565 | |
| 2566 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2567 | // general purpose registers needed to pass type and num_fp to hold |
| 2568 | // the number of floating point registers needed. |
| 2569 | |
| 2570 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2571 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2572 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2573 | // |
| 2574 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2575 | // register save space). |
| 2576 | |
| 2577 | llvm::Value *InRegs = 0; |
| 2578 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2579 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2580 | if (neededInt) { |
| 2581 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2582 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2583 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2584 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | if (neededSSE) { |
| 2588 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2589 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2590 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2591 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2592 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2593 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2594 | } |
| 2595 | |
| 2596 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2597 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2598 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2599 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2600 | |
| 2601 | // Emit code to load the value if it was passed in registers. |
| 2602 | |
| 2603 | CGF.EmitBlock(InRegBlock); |
| 2604 | |
| 2605 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2606 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2607 | // copying to a temporary location in case the parameter is passed |
| 2608 | // in different register classes or requires an alignment greater |
| 2609 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2610 | // |
| 2611 | // FIXME: This really results in shameful code when we end up needing to |
| 2612 | // collect arguments from different places; often what should result in a |
| 2613 | // simple assembling of a structure from scattered addresses has many more |
| 2614 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2615 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2616 | llvm::Value *RegAddr = |
| 2617 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2618 | "reg_save_area"); |
| 2619 | if (neededInt && neededSSE) { |
| 2620 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2621 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2622 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2623 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2624 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2625 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2626 | llvm::Type *TyLo = ST->getElementType(0); |
| 2627 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2628 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2629 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2630 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2631 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2632 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2633 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | 998f9d9 | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2634 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2635 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2636 | llvm::Value *V = |
| 2637 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2638 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2639 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2640 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2641 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2642 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2643 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2644 | } else if (neededInt) { |
| 2645 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2646 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2647 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2648 | |
| 2649 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2650 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2651 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2652 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2653 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2654 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2655 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2656 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2657 | RegAddr = Tmp; |
| 2658 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2659 | } else if (neededSSE == 1) { |
| 2660 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2661 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2662 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2663 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2664 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2665 | // SSE registers are spaced 16 bytes apart in the register save |
| 2666 | // area, we need to collect the two eightbytes together. |
| 2667 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2668 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2669 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2670 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2671 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2672 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2673 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2674 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2675 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2676 | DblPtrTy)); |
| 2677 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2678 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2679 | DblPtrTy)); |
| 2680 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2681 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2682 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2686 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2687 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2688 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2689 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2691 | gp_offset_p); |
| 2692 | } |
| 2693 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2694 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2695 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2696 | fp_offset_p); |
| 2697 | } |
| 2698 | CGF.EmitBranch(ContBlock); |
| 2699 | |
| 2700 | // Emit code to load the value if it was passed in memory. |
| 2701 | |
| 2702 | CGF.EmitBlock(InMemBlock); |
| 2703 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2704 | |
| 2705 | // Return the appropriate result. |
| 2706 | |
| 2707 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2708 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2709 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2710 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2711 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2712 | return ResAddr; |
| 2713 | } |
| 2714 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2715 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2716 | |
| 2717 | if (Ty->isVoidType()) |
| 2718 | return ABIArgInfo::getIgnore(); |
| 2719 | |
| 2720 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2721 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2722 | |
| 2723 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2724 | |
| 2725 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2726 | if (IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2727 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2728 | return ABIArgInfo::getIndirect(0, false); |
| 2729 | } else { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2730 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2731 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2732 | } |
| 2733 | |
| 2734 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2735 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2736 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2737 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2738 | if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2739 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2740 | Size)); |
| 2741 | |
| 2742 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2743 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2744 | if (Size <= 64 && |
NAKAMURA Takumi | e03c603 | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2745 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2746 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2747 | Size)); |
| 2748 | |
| 2749 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2750 | } |
| 2751 | |
| 2752 | if (Ty->isPromotableIntegerType()) |
| 2753 | return ABIArgInfo::getExtend(); |
| 2754 | |
| 2755 | return ABIArgInfo::getDirect(); |
| 2756 | } |
| 2757 | |
| 2758 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2759 | |
| 2760 | QualType RetTy = FI.getReturnType(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2761 | FI.getReturnInfo() = classify(RetTy, true); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2762 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2763 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2764 | it != ie; ++it) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2765 | it->info = classify(it->type, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2768 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2769 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2770 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2772 | CGBuilderTy &Builder = CGF.Builder; |
| 2773 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2774 | "ap"); |
| 2775 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2776 | llvm::Type *PTy = |
| 2777 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2778 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2779 | |
| 2780 | uint64_t Offset = |
| 2781 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2782 | llvm::Value *NextAddr = |
| 2783 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2784 | "ap.next"); |
| 2785 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2786 | |
| 2787 | return AddrTyped; |
| 2788 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2789 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2790 | namespace { |
| 2791 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2792 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2793 | public: |
| 2794 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2795 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2796 | void computeInfo(CGFunctionInfo &FI) const override; |
| 2797 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2798 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2799 | private: |
| 2800 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2801 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2802 | }; |
| 2803 | |
| 2804 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2805 | public: |
| 2806 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2807 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2808 | }; |
| 2809 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2812 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2813 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2814 | PInfo.computeInfo(FI); |
| 2815 | else |
| 2816 | NInfo.computeInfo(FI); |
| 2817 | } |
| 2818 | |
| 2819 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2820 | CodeGenFunction &CGF) const { |
| 2821 | // Always use the native convention; calling pnacl-style varargs functions |
| 2822 | // is unuspported. |
| 2823 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2824 | } |
| 2825 | |
| 2826 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2827 | // PowerPC-32 |
| 2828 | |
| 2829 | namespace { |
| 2830 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2831 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2832 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2833 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2834 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2835 | // This is recovered from gcc output. |
| 2836 | return 1; // r1 is the dedicated stack pointer |
| 2837 | } |
| 2838 | |
| 2839 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2840 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2841 | }; |
| 2842 | |
| 2843 | } |
| 2844 | |
| 2845 | bool |
| 2846 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2847 | llvm::Value *Address) const { |
| 2848 | // This is calculated from the LLVM and GCC tables and verified |
| 2849 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2850 | |
| 2851 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2852 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2853 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2854 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2855 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2856 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2857 | |
| 2858 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2859 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2860 | |
| 2861 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2862 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2863 | |
| 2864 | // 64-76 are various 4-byte special-purpose registers: |
| 2865 | // 64: mq |
| 2866 | // 65: lr |
| 2867 | // 66: ctr |
| 2868 | // 67: ap |
| 2869 | // 68-75 cr0-7 |
| 2870 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2871 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2872 | |
| 2873 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2874 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2875 | |
| 2876 | // 109: vrsave |
| 2877 | // 110: vscr |
| 2878 | // 111: spe_acc |
| 2879 | // 112: spefscr |
| 2880 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2881 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2882 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2883 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2886 | // PowerPC-64 |
| 2887 | |
| 2888 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2889 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2890 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2891 | |
| 2892 | public: |
| 2893 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2894 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2895 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2896 | |
| 2897 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2898 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2899 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2900 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2901 | // Example: For aggregate arguments that fit in a register, we could |
| 2902 | // use getDirectInReg (as is done below for structs containing a single |
| 2903 | // floating-point value) to avoid pushing them to memory on function |
| 2904 | // entry. This would require changing the logic in PPCISelLowering |
| 2905 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2906 | void computeInfo(CGFunctionInfo &FI) const override { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2907 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2908 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2909 | it != ie; ++it) { |
| 2910 | // We rely on the default argument classification for the most part. |
| 2911 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2912 | // or vector item must be passed in a register if one is available. |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2913 | const Type *T = isSingleElementStruct(it->type, getContext()); |
| 2914 | if (T) { |
| 2915 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2916 | if (T->isVectorType() || (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2917 | QualType QT(T, 0); |
| 2918 | it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
| 2919 | continue; |
| 2920 | } |
| 2921 | } |
| 2922 | it->info = classifyArgumentType(it->type); |
| 2923 | } |
| 2924 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2925 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2926 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2927 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2928 | }; |
| 2929 | |
| 2930 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2931 | public: |
| 2932 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2933 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2934 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2935 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2936 | // This is recovered from gcc output. |
| 2937 | return 1; // r1 is the dedicated stack pointer |
| 2938 | } |
| 2939 | |
| 2940 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2941 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2942 | }; |
| 2943 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2944 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2945 | public: |
| 2946 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2947 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2948 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2949 | // This is recovered from gcc output. |
| 2950 | return 1; // r1 is the dedicated stack pointer |
| 2951 | } |
| 2952 | |
| 2953 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2954 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2955 | }; |
| 2956 | |
| 2957 | } |
| 2958 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2959 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2960 | // extended to 64 bits. |
| 2961 | bool |
| 2962 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2963 | // Treat an enum type as its underlying type. |
| 2964 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2965 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2966 | |
| 2967 | // Promotable integer types are required to be promoted by the ABI. |
| 2968 | if (Ty->isPromotableIntegerType()) |
| 2969 | return true; |
| 2970 | |
| 2971 | // In addition to the usual promotable integer types, we also need to |
| 2972 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2973 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2974 | switch (BT->getKind()) { |
| 2975 | case BuiltinType::Int: |
| 2976 | case BuiltinType::UInt: |
| 2977 | return true; |
| 2978 | default: |
| 2979 | break; |
| 2980 | } |
| 2981 | |
| 2982 | return false; |
| 2983 | } |
| 2984 | |
| 2985 | ABIArgInfo |
| 2986 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 2987 | if (Ty->isAnyComplexType()) |
| 2988 | return ABIArgInfo::getDirect(); |
| 2989 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2990 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2991 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2992 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2993 | |
| 2994 | return ABIArgInfo::getIndirect(0); |
| 2995 | } |
| 2996 | |
| 2997 | return (isPromotableTypeForABI(Ty) ? |
| 2998 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2999 | } |
| 3000 | |
| 3001 | ABIArgInfo |
| 3002 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3003 | if (RetTy->isVoidType()) |
| 3004 | return ABIArgInfo::getIgnore(); |
| 3005 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3006 | if (RetTy->isAnyComplexType()) |
| 3007 | return ABIArgInfo::getDirect(); |
| 3008 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3009 | if (isAggregateTypeForABI(RetTy)) |
| 3010 | return ABIArgInfo::getIndirect(0); |
| 3011 | |
| 3012 | return (isPromotableTypeForABI(RetTy) ? |
| 3013 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3014 | } |
| 3015 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3016 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3017 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3018 | QualType Ty, |
| 3019 | CodeGenFunction &CGF) const { |
| 3020 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3021 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3022 | |
| 3023 | CGBuilderTy &Builder = CGF.Builder; |
| 3024 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3025 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3026 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3027 | // Update the va_list pointer. The pointer should be bumped by the |
| 3028 | // size of the object. We can trust getTypeSize() except for a complex |
| 3029 | // type whose base type is smaller than a doubleword. For these, the |
| 3030 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3031 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3032 | QualType BaseTy; |
| 3033 | unsigned CplxBaseSize = 0; |
| 3034 | |
| 3035 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3036 | BaseTy = CTy->getElementType(); |
| 3037 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3038 | if (CplxBaseSize < 8) |
| 3039 | SizeInBytes = 16; |
| 3040 | } |
| 3041 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3042 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3043 | llvm::Value *NextAddr = |
| 3044 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3045 | "ap.next"); |
| 3046 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3047 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3048 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3049 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3050 | // in separate doublewords. However, Clang expects us to produce a |
| 3051 | // pointer to a structure with the two parts packed tightly. So generate |
| 3052 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3053 | // and store them to a temporary structure. |
| 3054 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3055 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3056 | llvm::Value *ImagAddr = RealAddr; |
| 3057 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3058 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3059 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3060 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3061 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3062 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3063 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3064 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3065 | "vacplx"); |
| 3066 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3067 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3068 | Builder.CreateStore(Real, RealPtr, false); |
| 3069 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3070 | return Ptr; |
| 3071 | } |
| 3072 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3073 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3074 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3075 | // correct offset. |
| 3076 | if (SizeInBytes < 8) { |
| 3077 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3078 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3079 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3080 | } |
| 3081 | |
| 3082 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3083 | return Builder.CreateBitCast(Addr, PTy); |
| 3084 | } |
| 3085 | |
| 3086 | static bool |
| 3087 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3088 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3089 | // This is calculated from the LLVM and GCC tables and verified |
| 3090 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3091 | |
| 3092 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3093 | |
| 3094 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3095 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3096 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3097 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3098 | |
| 3099 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3100 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3101 | |
| 3102 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3103 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3104 | |
| 3105 | // 64-76 are various 4-byte special-purpose registers: |
| 3106 | // 64: mq |
| 3107 | // 65: lr |
| 3108 | // 66: ctr |
| 3109 | // 67: ap |
| 3110 | // 68-75 cr0-7 |
| 3111 | // 76: xer |
| 3112 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3113 | |
| 3114 | // 77-108: v0-31, the 16-byte vector registers |
| 3115 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3116 | |
| 3117 | // 109: vrsave |
| 3118 | // 110: vscr |
| 3119 | // 111: spe_acc |
| 3120 | // 112: spefscr |
| 3121 | // 113: sfp |
| 3122 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3123 | |
| 3124 | return false; |
| 3125 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3126 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3127 | bool |
| 3128 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3129 | CodeGen::CodeGenFunction &CGF, |
| 3130 | llvm::Value *Address) const { |
| 3131 | |
| 3132 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3133 | } |
| 3134 | |
| 3135 | bool |
| 3136 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3137 | llvm::Value *Address) const { |
| 3138 | |
| 3139 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3140 | } |
| 3141 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3142 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3143 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3144 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3145 | |
| 3146 | namespace { |
| 3147 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3148 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3149 | public: |
| 3150 | enum ABIKind { |
| 3151 | APCS = 0, |
| 3152 | AAPCS = 1, |
| 3153 | AAPCS_VFP |
| 3154 | }; |
| 3155 | |
| 3156 | private: |
| 3157 | ABIKind Kind; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3158 | mutable int VFPRegs[16]; |
| 3159 | const unsigned NumVFPs; |
| 3160 | const unsigned NumGPRs; |
| 3161 | mutable unsigned AllocatedGPRs; |
| 3162 | mutable unsigned AllocatedVFPs; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3163 | |
| 3164 | public: |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3165 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), |
| 3166 | NumVFPs(16), NumGPRs(4) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3167 | setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3168 | resetAllocatedRegs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3169 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3170 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3171 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3172 | switch (getTarget().getTriple().getEnvironment()) { |
| 3173 | case llvm::Triple::Android: |
| 3174 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3175 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3176 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 3177 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3178 | return true; |
| 3179 | default: |
| 3180 | return false; |
| 3181 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3184 | bool isEABIHF() const { |
| 3185 | switch (getTarget().getTriple().getEnvironment()) { |
| 3186 | case llvm::Triple::EABIHF: |
| 3187 | case llvm::Triple::GNUEABIHF: |
| 3188 | return true; |
| 3189 | default: |
| 3190 | return false; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3194 | ABIKind getABIKind() const { return Kind; } |
| 3195 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3196 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3197 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3198 | ABIArgInfo classifyArgumentType(QualType RetTy, bool &IsHA, bool isVariadic, |
| 3199 | bool &IsCPRC) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3200 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3201 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3202 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3203 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3204 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3205 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3206 | |
| 3207 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 3208 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 3209 | void setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3210 | |
| 3211 | void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; |
| 3212 | void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; |
| 3213 | void resetAllocatedRegs(void) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3214 | }; |
| 3215 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3216 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3217 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3218 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 3219 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3220 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3221 | const ARMABIInfo &getABIInfo() const { |
| 3222 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3223 | } |
| 3224 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3225 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3226 | return 13; |
| 3227 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3228 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3229 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3230 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3231 | } |
| 3232 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3233 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3234 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3235 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3236 | |
| 3237 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3238 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3239 | return false; |
| 3240 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3241 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3242 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3243 | if (getABIInfo().isEABI()) return 88; |
| 3244 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3245 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3246 | |
| 3247 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3248 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3249 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3250 | if (!FD) |
| 3251 | return; |
| 3252 | |
| 3253 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 3254 | if (!Attr) |
| 3255 | return; |
| 3256 | |
| 3257 | const char *Kind; |
| 3258 | switch (Attr->getInterrupt()) { |
| 3259 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 3260 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 3261 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 3262 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 3263 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 3264 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 3265 | } |
| 3266 | |
| 3267 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 3268 | |
| 3269 | Fn->addFnAttr("interrupt", Kind); |
| 3270 | |
| 3271 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 3272 | return; |
| 3273 | |
| 3274 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 3275 | // however this is not necessarily true on taking any interrupt. Instruct |
| 3276 | // the backend to perform a realignment as part of the function prologue. |
| 3277 | llvm::AttrBuilder B; |
| 3278 | B.addStackAlignmentAttr(8); |
| 3279 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 3280 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 3281 | llvm::AttributeSet::FunctionIndex, |
| 3282 | B)); |
| 3283 | } |
| 3284 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3285 | }; |
| 3286 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3289 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3290 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3291 | // VFP registers allocated so far. |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3292 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3293 | // VFP registers of the appropriate type unallocated then the argument is |
| 3294 | // allocated to the lowest-numbered sequence of such registers. |
| 3295 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3296 | // unallocated are marked as unavailable. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3297 | resetAllocatedRegs(); |
| 3298 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3299 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3300 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3301 | it != ie; ++it) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3302 | unsigned PreAllocationVFPs = AllocatedVFPs; |
| 3303 | unsigned PreAllocationGPRs = AllocatedGPRs; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3304 | bool IsHA = false; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3305 | bool IsCPRC = false; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3306 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3307 | // s0-s15 (d0-d7) for passing arguments. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3308 | it->info = classifyArgumentType(it->type, IsHA, FI.isVariadic(), IsCPRC); |
| 3309 | assert((IsCPRC || !IsHA) && "Homogeneous aggregates must be CPRCs"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3310 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3311 | // that are unallocated are marked as unavailable. To achieve this, we add |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3312 | // padding of (NumVFPs - PreAllocationVFP) floats. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3313 | // Note that IsHA will only be set when using the AAPCS-VFP calling convention, |
| 3314 | // and the callee is not variadic. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3315 | if (IsHA && AllocatedVFPs > NumVFPs && PreAllocationVFPs < NumVFPs) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3316 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3317 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocationVFPs); |
| 3318 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3319 | } |
| 3320 | |
| 3321 | // If we have allocated some arguments onto the stack (due to running |
| 3322 | // out of VFP registers), we cannot split an argument between GPRs and |
| 3323 | // the stack. If this situation occurs, we add padding to prevent the |
| 3324 | // GPRs from being used. In this situiation, the current argument could |
| 3325 | // only be allocated by rule C.8, so rule C.6 would mark these GPRs as |
| 3326 | // unusable anyway. |
| 3327 | const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; |
| 3328 | if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && StackUsed) { |
| 3329 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3330 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3331 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3332 | } |
| 3333 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3334 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3335 | // Always honor user-specified calling convention. |
| 3336 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3337 | return; |
| 3338 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3339 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3340 | if (cc != llvm::CallingConv::C) |
| 3341 | FI.setEffectiveCallingConvention(cc); |
| 3342 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3343 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3344 | /// Return the default calling convention that LLVM will use. |
| 3345 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3346 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3347 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3348 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3349 | else if (isEABI()) |
| 3350 | return llvm::CallingConv::ARM_AAPCS; |
| 3351 | else |
| 3352 | return llvm::CallingConv::ARM_APCS; |
| 3353 | } |
| 3354 | |
| 3355 | /// Return the calling convention that our ABI would like us to use |
| 3356 | /// as the C calling convention. |
| 3357 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3358 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3359 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3360 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3361 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3362 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3363 | llvm_unreachable("bad ABI kind"); |
| 3364 | } |
| 3365 | |
| 3366 | void ARMABIInfo::setRuntimeCC() { |
| 3367 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3368 | |
| 3369 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3370 | // they'd just match what LLVM will infer from the triple. |
| 3371 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3372 | if (abiCC != getLLVMDefaultCC()) |
| 3373 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3376 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3377 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3378 | /// contained in the type is returned through it; this is used for the |
| 3379 | /// recursive calls that check aggregate component types. |
| 3380 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3381 | ASTContext &Context, |
| 3382 | uint64_t *HAMembers = 0) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3383 | uint64_t Members = 0; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3384 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3385 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3386 | return false; |
| 3387 | Members *= AT->getSize().getZExtValue(); |
| 3388 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3389 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3390 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3391 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3392 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3393 | Members = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3394 | for (const auto *FD : RD->fields()) { |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3395 | uint64_t FldMembers; |
| 3396 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3397 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3398 | |
| 3399 | Members = (RD->isUnion() ? |
| 3400 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3401 | } |
| 3402 | } else { |
| 3403 | Members = 1; |
| 3404 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3405 | Members = 2; |
| 3406 | Ty = CT->getElementType(); |
| 3407 | } |
| 3408 | |
| 3409 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 3410 | // double, or 64-bit or 128-bit vectors. |
| 3411 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3412 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | eb752d4 | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 3413 | BT->getKind() != BuiltinType::Double && |
| 3414 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3415 | return false; |
| 3416 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3417 | unsigned VecSize = Context.getTypeSize(VT); |
| 3418 | if (VecSize != 64 && VecSize != 128) |
| 3419 | return false; |
| 3420 | } else { |
| 3421 | return false; |
| 3422 | } |
| 3423 | |
| 3424 | // The base type must be the same for all members. Vector types of the |
| 3425 | // same total size are treated as being equivalent here. |
| 3426 | const Type *TyPtr = Ty.getTypePtr(); |
| 3427 | if (!Base) |
| 3428 | Base = TyPtr; |
Oliver Stannard | 5e8558f | 2014-02-07 11:25:57 +0000 | [diff] [blame] | 3429 | |
| 3430 | if (Base != TyPtr) { |
| 3431 | // Homogeneous aggregates are defined as containing members with the |
| 3432 | // same machine type. There are two cases in which two members have |
| 3433 | // different TypePtrs but the same machine type: |
| 3434 | |
| 3435 | // 1) Vectors of the same length, regardless of the type and number |
| 3436 | // of their members. |
| 3437 | const bool SameLengthVectors = Base->isVectorType() && TyPtr->isVectorType() |
| 3438 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 3439 | |
| 3440 | // 2) In the 32-bit AAPCS, `double' and `long double' have the same |
| 3441 | // machine type. This is not the case for the 64-bit AAPCS. |
| 3442 | const bool SameSizeDoubles = |
| 3443 | ( ( Base->isSpecificBuiltinType(BuiltinType::Double) |
| 3444 | && TyPtr->isSpecificBuiltinType(BuiltinType::LongDouble)) |
| 3445 | || ( Base->isSpecificBuiltinType(BuiltinType::LongDouble) |
| 3446 | && TyPtr->isSpecificBuiltinType(BuiltinType::Double))) |
| 3447 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 3448 | |
| 3449 | if (!SameLengthVectors && !SameSizeDoubles) |
| 3450 | return false; |
| 3451 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 3455 | if (HAMembers) |
| 3456 | *HAMembers = Members; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3457 | |
| 3458 | return (Members > 0 && Members <= 4); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3461 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 3462 | /// number of VFP registers (unit is S register) requested. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3463 | void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, |
| 3464 | unsigned NumRequired) const { |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3465 | // Early Exit. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3466 | if (AllocatedVFPs >= 16) { |
| 3467 | // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on |
| 3468 | // the stack. |
| 3469 | AllocatedVFPs = 17; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3470 | return; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3471 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3472 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3473 | // VFP registers of the appropriate type unallocated then the argument is |
| 3474 | // allocated to the lowest-numbered sequence of such registers. |
| 3475 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 3476 | bool FoundSlot = true; |
| 3477 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3478 | if (J >= 16 || VFPRegs[J]) { |
| 3479 | FoundSlot = false; |
| 3480 | break; |
| 3481 | } |
| 3482 | if (FoundSlot) { |
| 3483 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3484 | VFPRegs[J] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3485 | AllocatedVFPs += NumRequired; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3486 | return; |
| 3487 | } |
| 3488 | } |
| 3489 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3490 | // unallocated are marked as unavailable. |
| 3491 | for (unsigned I = 0; I < 16; I++) |
| 3492 | VFPRegs[I] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3493 | AllocatedVFPs = 17; // We do not have enough VFP registers. |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3496 | /// Update AllocatedGPRs to record the number of general purpose registers |
| 3497 | /// which have been allocated. It is valid for AllocatedGPRs to go above 4, |
| 3498 | /// this represents arguments being stored on the stack. |
| 3499 | void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, |
| 3500 | unsigned NumRequired) const { |
| 3501 | assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); |
| 3502 | |
| 3503 | if (Alignment == 2 && AllocatedGPRs & 0x1) |
| 3504 | AllocatedGPRs += 1; |
| 3505 | |
| 3506 | AllocatedGPRs += NumRequired; |
| 3507 | } |
| 3508 | |
| 3509 | void ARMABIInfo::resetAllocatedRegs(void) const { |
| 3510 | AllocatedGPRs = 0; |
| 3511 | AllocatedVFPs = 0; |
| 3512 | for (unsigned i = 0; i < NumVFPs; ++i) |
| 3513 | VFPRegs[i] = 0; |
| 3514 | } |
| 3515 | |
| 3516 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool &IsHA, |
| 3517 | bool isVariadic, |
| 3518 | bool &IsCPRC) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3519 | // We update number of allocated VFPs according to |
| 3520 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 3521 | // A single-precision floating-point type (including promoted |
| 3522 | // half-precision types); A double-precision floating-point type; |
| 3523 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 3524 | // with a Base Type of a single- or double-precision floating-point type, |
| 3525 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 3526 | // to four Elements. |
| 3527 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3528 | // Handle illegal vector types here. |
| 3529 | if (isIllegalVectorType(Ty)) { |
| 3530 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3531 | if (Size <= 32) { |
| 3532 | llvm::Type *ResType = |
| 3533 | llvm::Type::getInt32Ty(getVMContext()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3534 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3535 | return ABIArgInfo::getDirect(ResType); |
| 3536 | } |
| 3537 | if (Size == 64) { |
| 3538 | llvm::Type *ResType = llvm::VectorType::get( |
| 3539 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3540 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ |
| 3541 | markAllocatedGPRs(2, 2); |
| 3542 | } else { |
| 3543 | markAllocatedVFPs(2, 2); |
| 3544 | IsCPRC = true; |
| 3545 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3546 | return ABIArgInfo::getDirect(ResType); |
| 3547 | } |
| 3548 | if (Size == 128) { |
| 3549 | llvm::Type *ResType = llvm::VectorType::get( |
| 3550 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3551 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { |
| 3552 | markAllocatedGPRs(2, 4); |
| 3553 | } else { |
| 3554 | markAllocatedVFPs(4, 4); |
| 3555 | IsCPRC = true; |
| 3556 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3557 | return ABIArgInfo::getDirect(ResType); |
| 3558 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3559 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3560 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3561 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3562 | // Update VFPRegs for legal vector types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3563 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 3564 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3565 | uint64_t Size = getContext().getTypeSize(VT); |
| 3566 | // Size of a legal vector should be power of 2 and above 64. |
| 3567 | markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); |
| 3568 | IsCPRC = true; |
| 3569 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3570 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3571 | // Update VFPRegs for floating point types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3572 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 3573 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3574 | if (BT->getKind() == BuiltinType::Half || |
| 3575 | BT->getKind() == BuiltinType::Float) { |
| 3576 | markAllocatedVFPs(1, 1); |
| 3577 | IsCPRC = true; |
| 3578 | } |
| 3579 | if (BT->getKind() == BuiltinType::Double || |
| 3580 | BT->getKind() == BuiltinType::LongDouble) { |
| 3581 | markAllocatedVFPs(2, 2); |
| 3582 | IsCPRC = true; |
| 3583 | } |
| 3584 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3585 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3586 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3587 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3588 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3589 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3590 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3591 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3592 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3593 | unsigned Size = getContext().getTypeSize(Ty); |
| 3594 | if (!IsCPRC) |
| 3595 | markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3596 | return (Ty->isPromotableIntegerType() ? |
| 3597 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3598 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3599 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3600 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 3601 | markAllocatedGPRs(1, 1); |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 3602 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3603 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 3604 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3605 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3606 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3607 | return ABIArgInfo::getIgnore(); |
| 3608 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3609 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3610 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 3611 | // into VFP registers. |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3612 | const Type *Base = 0; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3613 | uint64_t Members = 0; |
| 3614 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3615 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3616 | // Base can be a floating-point or a vector. |
| 3617 | if (Base->isVectorType()) { |
| 3618 | // ElementSize is in number of floats. |
| 3619 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3620 | markAllocatedVFPs(ElementSize, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3621 | Members * ElementSize); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3622 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3623 | markAllocatedVFPs(1, Members); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3624 | else { |
| 3625 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 3626 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3627 | markAllocatedVFPs(2, Members * 2); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3628 | } |
| 3629 | IsHA = true; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3630 | IsCPRC = true; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3631 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3632 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 3635 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3636 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 3637 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 3638 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 3639 | uint64_t ABIAlign = 4; |
| 3640 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 3641 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3642 | getABIKind() == ARMABIInfo::AAPCS) |
| 3643 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 3644 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3645 | // Update Allocated GPRs |
| 3646 | markAllocatedGPRs(1, 1); |
Oliver Stannard | 7c3c09e | 2014-03-12 14:02:50 +0000 | [diff] [blame] | 3647 | return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3648 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 3651 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3652 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3653 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3654 | // FIXME: Try to match the types of the arguments more accurately where |
| 3655 | // we can. |
| 3656 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 3657 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 3658 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3659 | markAllocatedGPRs(1, SizeRegs); |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3660 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3661 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3662 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3663 | markAllocatedGPRs(2, SizeRegs * 2); |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 3664 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3665 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3666 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3667 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3668 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3671 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3672 | llvm::LLVMContext &VMContext) { |
| 3673 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 3674 | // is called integer-like if its size is less than or equal to one word, and |
| 3675 | // the offset of each of its addressable sub-fields is zero. |
| 3676 | |
| 3677 | uint64_t Size = Context.getTypeSize(Ty); |
| 3678 | |
| 3679 | // Check that the type fits in a word. |
| 3680 | if (Size > 32) |
| 3681 | return false; |
| 3682 | |
| 3683 | // FIXME: Handle vector types! |
| 3684 | if (Ty->isVectorType()) |
| 3685 | return false; |
| 3686 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 3687 | // Float types are never treated as "integer like". |
| 3688 | if (Ty->isRealFloatingType()) |
| 3689 | return false; |
| 3690 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3691 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3692 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3693 | return true; |
| 3694 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 3695 | // Small complex integer types are "integer like". |
| 3696 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 3697 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3698 | |
| 3699 | // Single element and zero sized arrays should be allowed, by the definition |
| 3700 | // above, but they are not. |
| 3701 | |
| 3702 | // Otherwise, it must be a record type. |
| 3703 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3704 | if (!RT) return false; |
| 3705 | |
| 3706 | // Ignore records with flexible arrays. |
| 3707 | const RecordDecl *RD = RT->getDecl(); |
| 3708 | if (RD->hasFlexibleArrayMember()) |
| 3709 | return false; |
| 3710 | |
| 3711 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 3712 | // like". |
| 3713 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3714 | |
| 3715 | bool HadField = false; |
| 3716 | unsigned idx = 0; |
| 3717 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3718 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3719 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3720 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3721 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 3722 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 3723 | // struct { int : 0; int x } |
| 3724 | // is non-integer like according to gcc. |
| 3725 | if (FD->isBitField()) { |
| 3726 | if (!RD->isUnion()) |
| 3727 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3728 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3729 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3730 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3731 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3732 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3735 | // Check if this field is at offset 0. |
| 3736 | if (Layout.getFieldOffset(idx) != 0) |
| 3737 | return false; |
| 3738 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3739 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3740 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3741 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3742 | // Only allow at most one field in a structure. This doesn't match the |
| 3743 | // wording above, but follows gcc in situations with a field following an |
| 3744 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3745 | if (!RD->isUnion()) { |
| 3746 | if (HadField) |
| 3747 | return false; |
| 3748 | |
| 3749 | HadField = true; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | return true; |
| 3754 | } |
| 3755 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3756 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 3757 | bool isVariadic) const { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3758 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3759 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3760 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3761 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3762 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
| 3763 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3764 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3765 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3766 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3767 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3768 | // Treat an enum type as its underlying type. |
| 3769 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3770 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3771 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3772 | return (RetTy->isPromotableIntegerType() ? |
| 3773 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3774 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3775 | |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3776 | // Structures with either a non-trivial destructor or a non-trivial |
| 3777 | // copy constructor are always indirect. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3778 | if (isRecordReturnIndirect(RetTy, getCXXABI())) { |
| 3779 | markAllocatedGPRs(1, 1); |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3780 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3781 | } |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3782 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3783 | // Are we following APCS? |
| 3784 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3785 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3786 | return ABIArgInfo::getIgnore(); |
| 3787 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3788 | // Complex types are all returned as packed integers. |
| 3789 | // |
| 3790 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 3791 | // correctly. |
| 3792 | if (RetTy->isAnyComplexType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3793 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3794 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3795 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3796 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3797 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3798 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3799 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3800 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3801 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3802 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3803 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3804 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | // Otherwise return in memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3808 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3809 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3810 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3811 | |
| 3812 | // Otherwise this is an AAPCS variant. |
| 3813 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3814 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3815 | return ABIArgInfo::getIgnore(); |
| 3816 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3817 | // Check for homogeneous aggregates with AAPCS-VFP. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3818 | if (getABIKind() == AAPCS_VFP && !isVariadic) { |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3819 | const Type *Base = 0; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3820 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 3821 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3822 | // Homogeneous Aggregates are returned directly. |
| 3823 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3824 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3827 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 3828 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3829 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3830 | if (Size <= 32) { |
| 3831 | // Return in the smallest viable integer type. |
| 3832 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3833 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3834 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3835 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3836 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3839 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3840 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3843 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 3844 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3845 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3846 | // Check whether VT is legal. |
| 3847 | unsigned NumElements = VT->getNumElements(); |
| 3848 | uint64_t Size = getContext().getTypeSize(VT); |
| 3849 | // NumElements should be power of 2. |
| 3850 | if ((NumElements & (NumElements - 1)) != 0) |
| 3851 | return true; |
| 3852 | // Size should be greater than 32 bits. |
| 3853 | return Size <= 32; |
| 3854 | } |
| 3855 | return false; |
| 3856 | } |
| 3857 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3858 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3859 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3860 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3861 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3862 | |
| 3863 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3864 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3865 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3866 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 3867 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3868 | // These are ignored for parameter passing purposes. |
| 3869 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3870 | return Builder.CreateBitCast(Addr, PTy); |
| 3871 | } |
| 3872 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3873 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3874 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3875 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3876 | |
| 3877 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 3878 | // 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] | 3879 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3880 | getABIKind() == ARMABIInfo::AAPCS) |
| 3881 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 3882 | else |
| 3883 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3884 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 3885 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 3886 | IsIndirect = true; |
| 3887 | Size = 4; |
| 3888 | TyAlign = 4; |
| 3889 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3890 | |
| 3891 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3892 | if (TyAlign > 4) { |
| 3893 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 3894 | "Alignment is not power of 2!"); |
| 3895 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3896 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 3897 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3898 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3899 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3900 | |
| 3901 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3902 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3903 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3904 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3905 | "ap.next"); |
| 3906 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3907 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3908 | if (IsIndirect) |
| 3909 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3910 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3911 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 3912 | // may not be correctly aligned for the vector type. We create an aligned |
| 3913 | // temporary space and copy the content over from ap.cur to the temporary |
| 3914 | // space. This is necessary if the natural alignment of the type is greater |
| 3915 | // than the ABI alignment. |
| 3916 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 3917 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 3918 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 3919 | "var.align"); |
| 3920 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 3921 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 3922 | Builder.CreateMemCpy(Dst, Src, |
| 3923 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 3924 | TyAlign, false); |
| 3925 | Addr = AlignedTemp; //The content is in aligned location. |
| 3926 | } |
| 3927 | llvm::Type *PTy = |
| 3928 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3929 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3930 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3931 | return AddrTyped; |
| 3932 | } |
| 3933 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3934 | namespace { |
| 3935 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3936 | class NaClARMABIInfo : public ABIInfo { |
| 3937 | public: |
| 3938 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3939 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3940 | void computeInfo(CGFunctionInfo &FI) const override; |
| 3941 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3942 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3943 | private: |
| 3944 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 3945 | ARMABIInfo NInfo; // Used for everything else. |
| 3946 | }; |
| 3947 | |
| 3948 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3949 | public: |
| 3950 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3951 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 3952 | }; |
| 3953 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3956 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3957 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 3958 | PInfo.computeInfo(FI); |
| 3959 | else |
| 3960 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 3961 | } |
| 3962 | |
| 3963 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3964 | CodeGenFunction &CGF) const { |
| 3965 | // Always use the native convention; calling pnacl-style varargs functions |
| 3966 | // is unsupported. |
| 3967 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 3968 | } |
| 3969 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3970 | //===----------------------------------------------------------------------===// |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3971 | // AArch64 ABI Implementation |
| 3972 | //===----------------------------------------------------------------------===// |
| 3973 | |
| 3974 | namespace { |
| 3975 | |
| 3976 | class AArch64ABIInfo : public ABIInfo { |
| 3977 | public: |
| 3978 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 3979 | |
| 3980 | private: |
| 3981 | // The AArch64 PCS is explicit about return types and argument types being |
| 3982 | // handled identically, so we don't need to draw a distinction between |
| 3983 | // Argument and Return classification. |
| 3984 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 3985 | int &FreeVFPRegs) const; |
| 3986 | |
| 3987 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 3988 | llvm::Type *DirectTy = 0) const; |
| 3989 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3990 | void computeInfo(CGFunctionInfo &FI) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3991 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3992 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3993 | CodeGenFunction &CGF) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3994 | }; |
| 3995 | |
| 3996 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3997 | public: |
| 3998 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3999 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 4000 | |
| 4001 | const AArch64ABIInfo &getABIInfo() const { |
| 4002 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4003 | } |
| 4004 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4005 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4006 | return 31; |
| 4007 | } |
| 4008 | |
| 4009 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4010 | llvm::Value *Address) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4011 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 4012 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 4013 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 4014 | |
| 4015 | // 64-95 are v0-v31: 16 bytes each |
| 4016 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 4017 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 4018 | |
| 4019 | return false; |
| 4020 | } |
| 4021 | |
| 4022 | }; |
| 4023 | |
| 4024 | } |
| 4025 | |
| 4026 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4027 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4028 | |
| 4029 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 4030 | FreeIntRegs, FreeVFPRegs); |
| 4031 | |
| 4032 | FreeIntRegs = FreeVFPRegs = 8; |
| 4033 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4034 | it != ie; ++it) { |
| 4035 | it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs); |
| 4036 | |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | ABIArgInfo |
| 4041 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 4042 | bool IsInt, llvm::Type *DirectTy) const { |
| 4043 | if (FreeRegs >= RegsNeeded) { |
| 4044 | FreeRegs -= RegsNeeded; |
| 4045 | return ABIArgInfo::getDirect(DirectTy); |
| 4046 | } |
| 4047 | |
| 4048 | llvm::Type *Padding = 0; |
| 4049 | |
| 4050 | // We need padding so that later arguments don't get filled in anyway. That |
| 4051 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 4052 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 4053 | // concerned. |
| 4054 | if (FreeRegs > 0) { |
| 4055 | if (IsInt) |
| 4056 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 4057 | else |
| 4058 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 4059 | |
| 4060 | // Either [N x i64] or [N x float]. |
| 4061 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 4062 | FreeRegs = 0; |
| 4063 | } |
| 4064 | |
| 4065 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 4066 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 4067 | Padding); |
| 4068 | } |
| 4069 | |
| 4070 | |
| 4071 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 4072 | int &FreeIntRegs, |
| 4073 | int &FreeVFPRegs) const { |
| 4074 | // Can only occurs for return, but harmless otherwise. |
| 4075 | if (Ty->isVoidType()) |
| 4076 | return ABIArgInfo::getIgnore(); |
| 4077 | |
| 4078 | // Large vector types should be returned via memory. There's no such concept |
| 4079 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 4080 | // classified they'd go into memory (see B.3). |
| 4081 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 4082 | if (FreeIntRegs > 0) |
| 4083 | --FreeIntRegs; |
| 4084 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4085 | } |
| 4086 | |
| 4087 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 4088 | // be passed directly. After this block we're guaranteed to be in a |
| 4089 | // complicated case. |
| 4090 | if (!isAggregateTypeForABI(Ty)) { |
| 4091 | // Treat an enum type as its underlying type. |
| 4092 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4093 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4094 | |
| 4095 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 4096 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 4097 | |
| 4098 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 4099 | "unexpectedly large scalar type"); |
| 4100 | |
| 4101 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 4102 | |
| 4103 | // If the type may need padding registers to ensure "alignment", we must be |
| 4104 | // careful when this is accounted for. Increasing the effective size covers |
| 4105 | // all cases. |
| 4106 | if (getContext().getTypeAlign(Ty) == 128) |
| 4107 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 4108 | |
| 4109 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 4110 | } |
| 4111 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4112 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4113 | if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4114 | --FreeIntRegs; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4115 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4119 | if (!getContext().getLangOpts().CPlusPlus) { |
| 4120 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 4121 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 4122 | // the object for parameter-passsing purposes. |
| 4123 | return ABIArgInfo::getIgnore(); |
| 4124 | } |
| 4125 | |
| 4126 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 4127 | // description of va_arg in the PCS require that an empty struct does |
| 4128 | // actually occupy space for parameter-passing. I'm hoping for a |
| 4129 | // clarification giving an explicit paragraph to point to in future. |
| 4130 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 4131 | llvm::Type::getInt8Ty(getVMContext())); |
| 4132 | } |
| 4133 | |
| 4134 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 4135 | const Type *Base = 0; |
| 4136 | uint64_t NumMembers = 0; |
| 4137 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 4138 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 4139 | // Homogeneous aggregates are passed and returned directly. |
| 4140 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 4141 | /*IsInt=*/ false); |
| 4142 | } |
| 4143 | |
| 4144 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4145 | if (Size <= 128) { |
| 4146 | // Small structs can use the same direct type whether they're in registers |
| 4147 | // or on the stack. |
| 4148 | llvm::Type *BaseTy; |
| 4149 | unsigned NumBases; |
| 4150 | int SizeInRegs = (Size + 63) / 64; |
| 4151 | |
| 4152 | if (getContext().getTypeAlign(Ty) == 128) { |
| 4153 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 4154 | NumBases = 1; |
| 4155 | |
| 4156 | // If the type may need padding registers to ensure "alignment", we must |
| 4157 | // be careful when this is accounted for. Increasing the effective size |
| 4158 | // covers all cases. |
| 4159 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 4160 | } else { |
| 4161 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4162 | NumBases = SizeInRegs; |
| 4163 | } |
| 4164 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 4165 | |
| 4166 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 4167 | /*IsInt=*/ true, DirectTy); |
| 4168 | } |
| 4169 | |
| 4170 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 4171 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 4172 | --FreeIntRegs; |
| 4173 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 4174 | } |
| 4175 | |
| 4176 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4177 | CodeGenFunction &CGF) const { |
| 4178 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4179 | // Standard, section B.4: |
| 4180 | // |
| 4181 | // struct { |
| 4182 | // void *__stack; |
| 4183 | // void *__gr_top; |
| 4184 | // void *__vr_top; |
| 4185 | // int __gr_offs; |
| 4186 | // int __vr_offs; |
| 4187 | // }; |
| 4188 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4189 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4190 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4191 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 4192 | |
| 4193 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4194 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4195 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4196 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4197 | |
| 4198 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 4199 | int reg_top_index; |
| 4200 | int RegSize; |
| 4201 | if (FreeIntRegs < 8) { |
| 4202 | assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs"); |
| 4203 | // 3 is the field number of __gr_offs |
| 4204 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 4205 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4206 | reg_top_index = 1; // field number for __gr_top |
| 4207 | RegSize = 8 * (8 - FreeIntRegs); |
| 4208 | } else { |
| 4209 | assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs"); |
| 4210 | // 4 is the field number of __vr_offs. |
| 4211 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 4212 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4213 | reg_top_index = 2; // field number for __vr_top |
| 4214 | RegSize = 16 * (8 - FreeVFPRegs); |
| 4215 | } |
| 4216 | |
| 4217 | //======================================= |
| 4218 | // Find out where argument was passed |
| 4219 | //======================================= |
| 4220 | |
| 4221 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4222 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4223 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4224 | // whatever they get). |
| 4225 | llvm::Value *UsingStack = 0; |
| 4226 | UsingStack = CGF.Builder.CreateICmpSGE(reg_offs, |
| 4227 | llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4228 | |
| 4229 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4230 | |
| 4231 | // Otherwise, at least some kind of argument could go in these registers, the |
| 4232 | // quesiton is whether this particular type is too big. |
| 4233 | CGF.EmitBlock(MaybeRegBlock); |
| 4234 | |
| 4235 | // Integer arguments may need to correct register alignment (for example a |
| 4236 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4237 | // align __gr_offs to calculate the potential address. |
| 4238 | if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 4239 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 4240 | |
| 4241 | reg_offs = CGF.Builder.CreateAdd(reg_offs, |
| 4242 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4243 | "align_regoffs"); |
| 4244 | reg_offs = CGF.Builder.CreateAnd(reg_offs, |
| 4245 | llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4246 | "aligned_regoffs"); |
| 4247 | } |
| 4248 | |
| 4249 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 4250 | llvm::Value *NewOffset = 0; |
| 4251 | NewOffset = CGF.Builder.CreateAdd(reg_offs, |
| 4252 | llvm::ConstantInt::get(CGF.Int32Ty, RegSize), |
| 4253 | "new_reg_offs"); |
| 4254 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4255 | |
| 4256 | // Now we're in a position to decide whether this argument really was in |
| 4257 | // registers or not. |
| 4258 | llvm::Value *InRegs = 0; |
| 4259 | InRegs = CGF.Builder.CreateICmpSLE(NewOffset, |
| 4260 | llvm::ConstantInt::get(CGF.Int32Ty, 0), |
| 4261 | "inreg"); |
| 4262 | |
| 4263 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4264 | |
| 4265 | //======================================= |
| 4266 | // Argument was in registers |
| 4267 | //======================================= |
| 4268 | |
| 4269 | // Now we emit the code for if the argument was originally passed in |
| 4270 | // registers. First start the appropriate block: |
| 4271 | CGF.EmitBlock(InRegBlock); |
| 4272 | |
| 4273 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 4274 | reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 4275 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4276 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 4277 | llvm::Value *RegAddr = 0; |
| 4278 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4279 | |
| 4280 | if (!AI.isDirect()) { |
| 4281 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4282 | // stored registers or on the stack will actually be a struct **. |
| 4283 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4284 | } |
| 4285 | |
| 4286 | const Type *Base = 0; |
| 4287 | uint64_t NumMembers; |
| 4288 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers) |
| 4289 | && NumMembers > 1) { |
| 4290 | // Homogeneous aggregates passed in registers will have their elements split |
| 4291 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4292 | // qN+1, ...). We reload and store into a temporary local variable |
| 4293 | // contiguously. |
| 4294 | assert(AI.isDirect() && "Homogeneous aggregates should be passed directly"); |
| 4295 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4296 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 4297 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
Christian Pirker | f516422 | 2014-03-14 11:51:06 +0000 | [diff] [blame^] | 4298 | int Offset = 0; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4299 | |
Christian Pirker | f516422 | 2014-03-14 11:51:06 +0000 | [diff] [blame^] | 4300 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4301 | getContext().getTypeSize(Base) < 128) |
| 4302 | Offset = 16 - getContext().getTypeSize(Base)/8; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4303 | for (unsigned i = 0; i < NumMembers; ++i) { |
Christian Pirker | f516422 | 2014-03-14 11:51:06 +0000 | [diff] [blame^] | 4304 | llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, |
| 4305 | 16 * i + Offset); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4306 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4307 | LoadAddr = CGF.Builder.CreateBitCast(LoadAddr, |
| 4308 | llvm::PointerType::getUnqual(BaseTy)); |
| 4309 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 4310 | |
| 4311 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4312 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4313 | } |
| 4314 | |
| 4315 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4316 | } else { |
| 4317 | // Otherwise the object is contiguous in memory |
Christian Pirker | f516422 | 2014-03-14 11:51:06 +0000 | [diff] [blame^] | 4318 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
| 4319 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4320 | getContext().getTypeSize(Ty) < (BeAlign * 8)) { |
| 4321 | int Offset = BeAlign - getContext().getTypeSize(Ty)/8; |
| 4322 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4323 | |
| 4324 | BaseAddr = CGF.Builder.CreateAdd(BaseAddr, |
| 4325 | llvm::ConstantInt::get(CGF.Int64Ty, |
| 4326 | Offset), |
| 4327 | "align_be"); |
| 4328 | |
| 4329 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4330 | } |
| 4331 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4332 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4333 | } |
| 4334 | |
| 4335 | CGF.EmitBranch(ContBlock); |
| 4336 | |
| 4337 | //======================================= |
| 4338 | // Argument was on the stack |
| 4339 | //======================================= |
| 4340 | CGF.EmitBlock(OnStackBlock); |
| 4341 | |
| 4342 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 4343 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 4344 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4345 | |
| 4346 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4347 | // floating-point ones might be affected. |
| 4348 | if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 4349 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 4350 | |
| 4351 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4352 | |
| 4353 | OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, |
| 4354 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4355 | "align_stack"); |
| 4356 | OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr, |
| 4357 | llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4358 | "align_stack"); |
| 4359 | |
| 4360 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4361 | } |
| 4362 | |
| 4363 | uint64_t StackSize; |
| 4364 | if (AI.isDirect()) |
| 4365 | StackSize = getContext().getTypeSize(Ty) / 8; |
| 4366 | else |
| 4367 | StackSize = 8; |
| 4368 | |
| 4369 | // All stack slots are 8 bytes |
| 4370 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4371 | |
| 4372 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4373 | llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, |
| 4374 | "new_stack"); |
| 4375 | |
| 4376 | // Write the new value of __stack for the next call to va_arg |
| 4377 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4378 | |
Christian Pirker | f516422 | 2014-03-14 11:51:06 +0000 | [diff] [blame^] | 4379 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4380 | getContext().getTypeSize(Ty) < 64 ) { |
| 4381 | int Offset = 8 - getContext().getTypeSize(Ty)/8; |
| 4382 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4383 | |
| 4384 | OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, |
| 4385 | llvm::ConstantInt::get(CGF.Int64Ty, |
| 4386 | Offset), |
| 4387 | "align_be"); |
| 4388 | |
| 4389 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4390 | } |
| 4391 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4392 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4393 | |
| 4394 | CGF.EmitBranch(ContBlock); |
| 4395 | |
| 4396 | //======================================= |
| 4397 | // Tidy up |
| 4398 | //======================================= |
| 4399 | CGF.EmitBlock(ContBlock); |
| 4400 | |
| 4401 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4402 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4403 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4404 | |
| 4405 | if (AI.isDirect()) |
| 4406 | return ResAddr; |
| 4407 | |
| 4408 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4409 | } |
| 4410 | |
| 4411 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4412 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4413 | //===----------------------------------------------------------------------===// |
| 4414 | |
| 4415 | namespace { |
| 4416 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4417 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4418 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4419 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4420 | |
| 4421 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4422 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4423 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4424 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4425 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4426 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4427 | }; |
| 4428 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4429 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4430 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4431 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4432 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4433 | |
| 4434 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4435 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4436 | private: |
| 4437 | static void addKernelMetadata(llvm::Function *F); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4438 | }; |
| 4439 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4440 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4441 | if (RetTy->isVoidType()) |
| 4442 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4443 | |
| 4444 | // note: this is different from default ABI |
| 4445 | if (!RetTy->isScalarType()) |
| 4446 | return ABIArgInfo::getDirect(); |
| 4447 | |
| 4448 | // Treat an enum type as its underlying type. |
| 4449 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4450 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4451 | |
| 4452 | return (RetTy->isPromotableIntegerType() ? |
| 4453 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4456 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4457 | // Treat an enum type as its underlying type. |
| 4458 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4459 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4460 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4461 | return (Ty->isPromotableIntegerType() ? |
| 4462 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4465 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4466 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4467 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4468 | it != ie; ++it) |
| 4469 | it->info = classifyArgumentType(it->type); |
| 4470 | |
| 4471 | // Always honor user-specified calling convention. |
| 4472 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4473 | return; |
| 4474 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4475 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4476 | } |
| 4477 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4478 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4479 | CodeGenFunction &CFG) const { |
| 4480 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4483 | void NVPTXTargetCodeGenInfo:: |
| 4484 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4485 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4486 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4487 | if (!FD) return; |
| 4488 | |
| 4489 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4490 | |
| 4491 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4492 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4493 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4494 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4495 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4496 | // OpenCL __kernel functions get kernel metadata |
| 4497 | addKernelMetadata(F); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4498 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4499 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4500 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4501 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4502 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4503 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4504 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4505 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4506 | // __global__ functions cannot be called from the device, we do not |
| 4507 | // need to set the noinline attribute. |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 4508 | if (FD->hasAttr<CUDAGlobalAttr>()) |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4509 | addKernelMetadata(F); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4510 | } |
| 4511 | } |
| 4512 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4513 | void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) { |
| 4514 | llvm::Module *M = F->getParent(); |
| 4515 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4516 | |
| 4517 | // Get "nvvm.annotations" metadata node |
| 4518 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4519 | |
| 4520 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4521 | llvm::SmallVector<llvm::Value *, 3> MDVals; |
| 4522 | MDVals.push_back(F); |
| 4523 | MDVals.push_back(llvm::MDString::get(Ctx, "kernel")); |
| 4524 | MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)); |
| 4525 | |
| 4526 | // Append metadata to nvvm.annotations |
| 4527 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4528 | } |
| 4529 | |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4533 | // SystemZ ABI Implementation |
| 4534 | //===----------------------------------------------------------------------===// |
| 4535 | |
| 4536 | namespace { |
| 4537 | |
| 4538 | class SystemZABIInfo : public ABIInfo { |
| 4539 | public: |
| 4540 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4541 | |
| 4542 | bool isPromotableIntegerType(QualType Ty) const; |
| 4543 | bool isCompoundType(QualType Ty) const; |
| 4544 | bool isFPArgumentType(QualType Ty) const; |
| 4545 | |
| 4546 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4547 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4548 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4549 | void computeInfo(CGFunctionInfo &FI) const override { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4550 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4551 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4552 | it != ie; ++it) |
| 4553 | it->info = classifyArgumentType(it->type); |
| 4554 | } |
| 4555 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4556 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4557 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4558 | }; |
| 4559 | |
| 4560 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4561 | public: |
| 4562 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4563 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4564 | }; |
| 4565 | |
| 4566 | } |
| 4567 | |
| 4568 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4569 | // Treat an enum type as its underlying type. |
| 4570 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4571 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4572 | |
| 4573 | // Promotable integer types are required to be promoted by the ABI. |
| 4574 | if (Ty->isPromotableIntegerType()) |
| 4575 | return true; |
| 4576 | |
| 4577 | // 32-bit values must also be promoted. |
| 4578 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4579 | switch (BT->getKind()) { |
| 4580 | case BuiltinType::Int: |
| 4581 | case BuiltinType::UInt: |
| 4582 | return true; |
| 4583 | default: |
| 4584 | return false; |
| 4585 | } |
| 4586 | return false; |
| 4587 | } |
| 4588 | |
| 4589 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 4590 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 4591 | } |
| 4592 | |
| 4593 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 4594 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4595 | switch (BT->getKind()) { |
| 4596 | case BuiltinType::Float: |
| 4597 | case BuiltinType::Double: |
| 4598 | return true; |
| 4599 | default: |
| 4600 | return false; |
| 4601 | } |
| 4602 | |
| 4603 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 4604 | const RecordDecl *RD = RT->getDecl(); |
| 4605 | bool Found = false; |
| 4606 | |
| 4607 | // If this is a C++ record, check the bases first. |
| 4608 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4609 | for (const auto &I : CXXRD->bases()) { |
| 4610 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Empty bases don't affect things either way. |
| 4613 | if (isEmptyRecord(getContext(), Base, true)) |
| 4614 | continue; |
| 4615 | |
| 4616 | if (Found) |
| 4617 | return false; |
| 4618 | Found = isFPArgumentType(Base); |
| 4619 | if (!Found) |
| 4620 | return false; |
| 4621 | } |
| 4622 | |
| 4623 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4624 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4625 | // Empty bitfields don't affect things either way. |
| 4626 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 4627 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 4628 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4629 | return true; |
| 4630 | |
| 4631 | // Unlike isSingleElementStruct(), arrays do not count. |
| 4632 | // Nested isFPArgumentType structures still do though. |
| 4633 | if (Found) |
| 4634 | return false; |
| 4635 | Found = isFPArgumentType(FD->getType()); |
| 4636 | if (!Found) |
| 4637 | return false; |
| 4638 | } |
| 4639 | |
| 4640 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 4641 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 4642 | return Found; |
| 4643 | } |
| 4644 | |
| 4645 | return false; |
| 4646 | } |
| 4647 | |
| 4648 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4649 | CodeGenFunction &CGF) const { |
| 4650 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 4651 | // struct { |
| 4652 | // i64 __gpr; |
| 4653 | // i64 __fpr; |
| 4654 | // i8 *__overflow_arg_area; |
| 4655 | // i8 *__reg_save_area; |
| 4656 | // }; |
| 4657 | |
| 4658 | // Every argument occupies 8 bytes and is passed by preference in either |
| 4659 | // GPRs or FPRs. |
| 4660 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4661 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 4662 | bool InFPRs = isFPArgumentType(Ty); |
| 4663 | |
| 4664 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4665 | bool IsIndirect = AI.isIndirect(); |
| 4666 | unsigned UnpaddedBitSize; |
| 4667 | if (IsIndirect) { |
| 4668 | APTy = llvm::PointerType::getUnqual(APTy); |
| 4669 | UnpaddedBitSize = 64; |
| 4670 | } else |
| 4671 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 4672 | unsigned PaddedBitSize = 64; |
| 4673 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 4674 | |
| 4675 | unsigned PaddedSize = PaddedBitSize / 8; |
| 4676 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 4677 | |
| 4678 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 4679 | if (InFPRs) { |
| 4680 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 4681 | RegCountField = 1; // __fpr |
| 4682 | RegSaveIndex = 16; // save offset for f0 |
| 4683 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 4684 | } else { |
| 4685 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 4686 | RegCountField = 0; // __gpr |
| 4687 | RegSaveIndex = 2; // save offset for r2 |
| 4688 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 4689 | } |
| 4690 | |
| 4691 | llvm::Value *RegCountPtr = |
| 4692 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 4693 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 4694 | llvm::Type *IndexTy = RegCount->getType(); |
| 4695 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 4696 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4697 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4698 | |
| 4699 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4700 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 4701 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4702 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 4703 | |
| 4704 | // Emit code to load the value if it was passed in registers. |
| 4705 | CGF.EmitBlock(InRegBlock); |
| 4706 | |
| 4707 | // Work out the address of an argument register. |
| 4708 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 4709 | llvm::Value *ScaledRegCount = |
| 4710 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 4711 | llvm::Value *RegBase = |
| 4712 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 4713 | llvm::Value *RegOffset = |
| 4714 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 4715 | llvm::Value *RegSaveAreaPtr = |
| 4716 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 4717 | llvm::Value *RegSaveArea = |
| 4718 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 4719 | llvm::Value *RawRegAddr = |
| 4720 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 4721 | llvm::Value *RegAddr = |
| 4722 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 4723 | |
| 4724 | // Update the register count |
| 4725 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 4726 | llvm::Value *NewRegCount = |
| 4727 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 4728 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 4729 | CGF.EmitBranch(ContBlock); |
| 4730 | |
| 4731 | // Emit code to load the value if it was passed in memory. |
| 4732 | CGF.EmitBlock(InMemBlock); |
| 4733 | |
| 4734 | // Work out the address of a stack argument. |
| 4735 | llvm::Value *OverflowArgAreaPtr = |
| 4736 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 4737 | llvm::Value *OverflowArgArea = |
| 4738 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 4739 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 4740 | llvm::Value *RawMemAddr = |
| 4741 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 4742 | llvm::Value *MemAddr = |
| 4743 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 4744 | |
| 4745 | // Update overflow_arg_area_ptr pointer |
| 4746 | llvm::Value *NewOverflowArgArea = |
| 4747 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 4748 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 4749 | CGF.EmitBranch(ContBlock); |
| 4750 | |
| 4751 | // Return the appropriate result. |
| 4752 | CGF.EmitBlock(ContBlock); |
| 4753 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 4754 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4755 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 4756 | |
| 4757 | if (IsIndirect) |
| 4758 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 4759 | |
| 4760 | return ResAddr; |
| 4761 | } |
| 4762 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 4763 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 4764 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 4765 | assert(Triple.getArch() == llvm::Triple::x86); |
| 4766 | |
| 4767 | switch (Opts.getStructReturnConvention()) { |
| 4768 | case CodeGenOptions::SRCK_Default: |
| 4769 | break; |
| 4770 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 4771 | return false; |
| 4772 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 4773 | return true; |
| 4774 | } |
| 4775 | |
| 4776 | if (Triple.isOSDarwin()) |
| 4777 | return true; |
| 4778 | |
| 4779 | switch (Triple.getOS()) { |
| 4780 | case llvm::Triple::Cygwin: |
| 4781 | case llvm::Triple::MinGW32: |
| 4782 | case llvm::Triple::AuroraUX: |
| 4783 | case llvm::Triple::DragonFly: |
| 4784 | case llvm::Triple::FreeBSD: |
| 4785 | case llvm::Triple::OpenBSD: |
| 4786 | case llvm::Triple::Bitrig: |
| 4787 | case llvm::Triple::Win32: |
| 4788 | return true; |
| 4789 | default: |
| 4790 | return false; |
| 4791 | } |
| 4792 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4793 | |
| 4794 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 4795 | if (RetTy->isVoidType()) |
| 4796 | return ABIArgInfo::getIgnore(); |
| 4797 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 4798 | return ABIArgInfo::getIndirect(0); |
| 4799 | return (isPromotableIntegerType(RetTy) ? |
| 4800 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4801 | } |
| 4802 | |
| 4803 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 4804 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4805 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4806 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 4807 | |
| 4808 | // Integers and enums are extended to full register width. |
| 4809 | if (isPromotableIntegerType(Ty)) |
| 4810 | return ABIArgInfo::getExtend(); |
| 4811 | |
| 4812 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 4813 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4814 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 4815 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4816 | |
| 4817 | // Handle small structures. |
| 4818 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4819 | // Structures with flexible arrays have variable length, so really |
| 4820 | // fail the size test above. |
| 4821 | const RecordDecl *RD = RT->getDecl(); |
| 4822 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 4823 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4824 | |
| 4825 | // The structure is passed as an unextended integer, a float, or a double. |
| 4826 | llvm::Type *PassTy; |
| 4827 | if (isFPArgumentType(Ty)) { |
| 4828 | assert(Size == 32 || Size == 64); |
| 4829 | if (Size == 32) |
| 4830 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 4831 | else |
| 4832 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 4833 | } else |
| 4834 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4835 | return ABIArgInfo::getDirect(PassTy); |
| 4836 | } |
| 4837 | |
| 4838 | // Non-structure compounds are passed indirectly. |
| 4839 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 4840 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4841 | |
| 4842 | return ABIArgInfo::getDirect(0); |
| 4843 | } |
| 4844 | |
| 4845 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4846 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4847 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4848 | |
| 4849 | namespace { |
| 4850 | |
| 4851 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4852 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4853 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4854 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4855 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4856 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4857 | }; |
| 4858 | |
| 4859 | } |
| 4860 | |
| 4861 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4862 | llvm::GlobalValue *GV, |
| 4863 | CodeGen::CodeGenModule &M) const { |
| 4864 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4865 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 4866 | // Handle 'interrupt' attribute: |
| 4867 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4868 | |
| 4869 | // Step 1: Set ISR calling convention. |
| 4870 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 4871 | |
| 4872 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4873 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4876 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4877 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4878 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4879 | GV, &M.getModule()); |
| 4880 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4881 | } |
| 4882 | } |
| 4883 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4884 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4885 | // MIPS ABI Implementation. This works for both little-endian and |
| 4886 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4887 | //===----------------------------------------------------------------------===// |
| 4888 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4889 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4890 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4891 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4892 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 4893 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4894 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4895 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4896 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4897 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4898 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 4899 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4900 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 4901 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4902 | |
| 4903 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4904 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4905 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4906 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4907 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4908 | }; |
| 4909 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4910 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4911 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4912 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 4913 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 4914 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4915 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4916 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4917 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4918 | return 29; |
| 4919 | } |
| 4920 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4921 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4922 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4923 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4924 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 4925 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4926 | if (FD->hasAttr<Mips16Attr>()) { |
| 4927 | Fn->addFnAttr("mips16"); |
| 4928 | } |
| 4929 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 4930 | Fn->addFnAttr("nomips16"); |
| 4931 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4932 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4933 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4934 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4935 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4936 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4937 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4938 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4939 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4940 | }; |
| 4941 | } |
| 4942 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4943 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4944 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4945 | llvm::IntegerType *IntTy = |
| 4946 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4947 | |
| 4948 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 4949 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 4950 | ArgList.push_back(IntTy); |
| 4951 | |
| 4952 | // If necessary, add one more integer type to ArgList. |
| 4953 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 4954 | |
| 4955 | if (R) |
| 4956 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4959 | // In N32/64, an aligned double precision floating point field is passed in |
| 4960 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4961 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4962 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 4963 | |
| 4964 | if (IsO32) { |
| 4965 | CoerceToIntArgs(TySize, ArgList); |
| 4966 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4967 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4968 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 4969 | if (Ty->isComplexType()) |
| 4970 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 4971 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4972 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4973 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4974 | // Unions/vectors are passed in integer registers. |
| 4975 | if (!RT || !RT->isStructureOrClassType()) { |
| 4976 | CoerceToIntArgs(TySize, ArgList); |
| 4977 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4978 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4979 | |
| 4980 | const RecordDecl *RD = RT->getDecl(); |
| 4981 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4982 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4983 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4984 | uint64_t LastOffset = 0; |
| 4985 | unsigned idx = 0; |
| 4986 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 4987 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4988 | // Iterate over fields in the struct/class and check if there are any aligned |
| 4989 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4990 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4991 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4992 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4993 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 4994 | |
| 4995 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 4996 | continue; |
| 4997 | |
| 4998 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 4999 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5000 | continue; |
| 5001 | |
| 5002 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5003 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5004 | ArgList.push_back(I64); |
| 5005 | |
| 5006 | // Add double type. |
| 5007 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5008 | LastOffset = Offset + 64; |
| 5009 | } |
| 5010 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5011 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5012 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5013 | |
| 5014 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5015 | } |
| 5016 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5017 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5018 | uint64_t Offset) const { |
| 5019 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
| 5020 | return 0; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5021 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5022 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5023 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5024 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5025 | ABIArgInfo |
| 5026 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5027 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5028 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5029 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5030 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5031 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5032 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5033 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5034 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5035 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5036 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5037 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5038 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5039 | return ABIArgInfo::getIgnore(); |
| 5040 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5041 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5042 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5043 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5044 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5045 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5046 | // If we have reached here, aggregates are passed directly by coercing to |
| 5047 | // another structure type. Padding is inserted if the offset of the |
| 5048 | // aggregate is unaligned. |
| 5049 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5050 | getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | // Treat an enum type as its underlying type. |
| 5054 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5055 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5056 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5057 | if (Ty->isPromotableIntegerType()) |
| 5058 | return ABIArgInfo::getExtend(); |
| 5059 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5060 | return ABIArgInfo::getDirect( |
| 5061 | 0, 0, IsO32 ? 0 : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5064 | llvm::Type* |
| 5065 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5066 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5067 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5068 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5069 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5070 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5071 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5072 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5073 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5074 | // N32/64 returns struct/classes in floating point registers if the |
| 5075 | // following conditions are met: |
| 5076 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5077 | // 2. The struct/class has one or two fields all of which are floating |
| 5078 | // point types. |
| 5079 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5080 | // |
| 5081 | // Any other composite results are returned in integer registers. |
| 5082 | // |
| 5083 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5084 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5085 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5086 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5087 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5088 | if (!BT || !BT->isFloatingPoint()) |
| 5089 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5090 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5091 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | if (b == e) |
| 5095 | return llvm::StructType::get(getVMContext(), RTList, |
| 5096 | RD->hasAttr<PackedAttr>()); |
| 5097 | |
| 5098 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5099 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5102 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5103 | return llvm::StructType::get(getVMContext(), RTList); |
| 5104 | } |
| 5105 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5106 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5107 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5108 | |
| 5109 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5110 | return ABIArgInfo::getIgnore(); |
| 5111 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5112 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5113 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5114 | return ABIArgInfo::getIndirect(0); |
| 5115 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5116 | if (Size <= 128) { |
| 5117 | if (RetTy->isAnyComplexType()) |
| 5118 | return ABIArgInfo::getDirect(); |
| 5119 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5120 | // O32 returns integer vectors in registers. |
| 5121 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 5122 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5123 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5124 | if (!IsO32) |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5125 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5126 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5127 | |
| 5128 | return ABIArgInfo::getIndirect(0); |
| 5129 | } |
| 5130 | |
| 5131 | // Treat an enum type as its underlying type. |
| 5132 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5133 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5134 | |
| 5135 | return (RetTy->isPromotableIntegerType() ? |
| 5136 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5137 | } |
| 5138 | |
| 5139 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5140 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 5141 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 5142 | |
| 5143 | // 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] | 5144 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5145 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5146 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5147 | it != ie; ++it) |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5148 | it->info = classifyArgumentType(it->type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5152 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5153 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5154 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5155 | |
| 5156 | CGBuilderTy &Builder = CGF.Builder; |
| 5157 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5158 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5159 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5160 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5161 | llvm::Value *AddrTyped; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5162 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5163 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5164 | |
| 5165 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5166 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5167 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5168 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5169 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5170 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5171 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5172 | } |
| 5173 | else |
| 5174 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5175 | |
| 5176 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5177 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5178 | uint64_t Offset = |
| 5179 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 5180 | llvm::Value *NextAddr = |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5181 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5182 | "ap.next"); |
| 5183 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5184 | |
| 5185 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5188 | bool |
| 5189 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5190 | llvm::Value *Address) const { |
| 5191 | // This information comes from gcc's implementation, which seems to |
| 5192 | // as canonical as it gets. |
| 5193 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5194 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5195 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5196 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5197 | |
| 5198 | // 0-31 are the general purpose registers, $0 - $31. |
| 5199 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5200 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5201 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5202 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5203 | |
| 5204 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5205 | // They are one bit wide and ignored here. |
| 5206 | |
| 5207 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5208 | // (coprocessor 1 is the FP unit) |
| 5209 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5210 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5211 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5212 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5213 | return false; |
| 5214 | } |
| 5215 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5216 | //===----------------------------------------------------------------------===// |
| 5217 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5218 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5219 | // handling. |
| 5220 | //===----------------------------------------------------------------------===// |
| 5221 | |
| 5222 | namespace { |
| 5223 | |
| 5224 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5225 | public: |
| 5226 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5227 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5228 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5229 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5230 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5231 | }; |
| 5232 | |
| 5233 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5234 | llvm::GlobalValue *GV, |
| 5235 | CodeGen::CodeGenModule &M) const { |
| 5236 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5237 | if (!FD) return; |
| 5238 | |
| 5239 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5240 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5241 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5242 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5243 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5244 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5245 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5246 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5247 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5248 | llvm::LLVMContext &Context = F->getContext(); |
| 5249 | llvm::NamedMDNode *OpenCLMetadata = |
| 5250 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5251 | |
| 5252 | SmallVector<llvm::Value*, 5> Operands; |
| 5253 | Operands.push_back(F); |
| 5254 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5255 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5256 | llvm::APInt(32, Attr->getXDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5257 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5258 | llvm::APInt(32, Attr->getYDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5259 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5260 | llvm::APInt(32, Attr->getZDim()))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5261 | |
| 5262 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5263 | // for implementing the work_group_size_hint attr later. Currently |
| 5264 | // always true as the hint is not yet implemented. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5265 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5266 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5267 | } |
| 5268 | } |
| 5269 | } |
| 5270 | } |
| 5271 | |
| 5272 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5273 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5274 | //===----------------------------------------------------------------------===// |
| 5275 | // Hexagon ABI Implementation |
| 5276 | //===----------------------------------------------------------------------===// |
| 5277 | |
| 5278 | namespace { |
| 5279 | |
| 5280 | class HexagonABIInfo : public ABIInfo { |
| 5281 | |
| 5282 | |
| 5283 | public: |
| 5284 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5285 | |
| 5286 | private: |
| 5287 | |
| 5288 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5289 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5290 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5291 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5292 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5293 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5294 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5295 | }; |
| 5296 | |
| 5297 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5298 | public: |
| 5299 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5300 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5301 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5302 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5303 | return 29; |
| 5304 | } |
| 5305 | }; |
| 5306 | |
| 5307 | } |
| 5308 | |
| 5309 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5310 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 5311 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5312 | it != ie; ++it) |
| 5313 | it->info = classifyArgumentType(it->type); |
| 5314 | } |
| 5315 | |
| 5316 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5317 | if (!isAggregateTypeForABI(Ty)) { |
| 5318 | // Treat an enum type as its underlying type. |
| 5319 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5320 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5321 | |
| 5322 | return (Ty->isPromotableIntegerType() ? |
| 5323 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5324 | } |
| 5325 | |
| 5326 | // Ignore empty records. |
| 5327 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5328 | return ABIArgInfo::getIgnore(); |
| 5329 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5330 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5331 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5332 | |
| 5333 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5334 | if (Size > 64) |
| 5335 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5336 | // Pass in the smallest viable integer type. |
| 5337 | else if (Size > 32) |
| 5338 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5339 | else if (Size > 16) |
| 5340 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5341 | else if (Size > 8) |
| 5342 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5343 | else |
| 5344 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5345 | } |
| 5346 | |
| 5347 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5348 | if (RetTy->isVoidType()) |
| 5349 | return ABIArgInfo::getIgnore(); |
| 5350 | |
| 5351 | // Large vector types should be returned via memory. |
| 5352 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5353 | return ABIArgInfo::getIndirect(0); |
| 5354 | |
| 5355 | if (!isAggregateTypeForABI(RetTy)) { |
| 5356 | // Treat an enum type as its underlying type. |
| 5357 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5358 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5359 | |
| 5360 | return (RetTy->isPromotableIntegerType() ? |
| 5361 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5362 | } |
| 5363 | |
| 5364 | // Structures with either a non-trivial destructor or a non-trivial |
| 5365 | // copy constructor are always indirect. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5366 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5367 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5368 | |
| 5369 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5370 | return ABIArgInfo::getIgnore(); |
| 5371 | |
| 5372 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5373 | // are returned indirectly. |
| 5374 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5375 | if (Size <= 64) { |
| 5376 | // Return in the smallest viable integer type. |
| 5377 | if (Size <= 8) |
| 5378 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5379 | if (Size <= 16) |
| 5380 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5381 | if (Size <= 32) |
| 5382 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5383 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5384 | } |
| 5385 | |
| 5386 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5387 | } |
| 5388 | |
| 5389 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5390 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5391 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5392 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5393 | |
| 5394 | CGBuilderTy &Builder = CGF.Builder; |
| 5395 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5396 | "ap"); |
| 5397 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5398 | llvm::Type *PTy = |
| 5399 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5400 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5401 | |
| 5402 | uint64_t Offset = |
| 5403 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5404 | llvm::Value *NextAddr = |
| 5405 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5406 | "ap.next"); |
| 5407 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5408 | |
| 5409 | return AddrTyped; |
| 5410 | } |
| 5411 | |
| 5412 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5413 | //===----------------------------------------------------------------------===// |
| 5414 | // SPARC v9 ABI Implementation. |
| 5415 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5416 | // |
| 5417 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5418 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5419 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5420 | // |
| 5421 | // One case requires special care: |
| 5422 | // |
| 5423 | // struct mixed { |
| 5424 | // int i; |
| 5425 | // float f; |
| 5426 | // }; |
| 5427 | // |
| 5428 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5429 | // parameter array, but the int is passed in an integer register, and the float |
| 5430 | // is passed in a floating point register. This is represented as two arguments |
| 5431 | // with the LLVM IR inreg attribute: |
| 5432 | // |
| 5433 | // declare void f(i32 inreg %i, float inreg %f) |
| 5434 | // |
| 5435 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5436 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5437 | // bytes. |
| 5438 | // |
| 5439 | namespace { |
| 5440 | class SparcV9ABIInfo : public ABIInfo { |
| 5441 | public: |
| 5442 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5443 | |
| 5444 | private: |
| 5445 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5446 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5447 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5448 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5449 | |
| 5450 | // Coercion type builder for structs passed in registers. The coercion type |
| 5451 | // serves two purposes: |
| 5452 | // |
| 5453 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5454 | // in registers. |
| 5455 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5456 | // code generator knows to pass them in floating point registers. |
| 5457 | // |
| 5458 | // We also compute the InReg flag which indicates that the struct contains |
| 5459 | // aligned 32-bit floats. |
| 5460 | // |
| 5461 | struct CoerceBuilder { |
| 5462 | llvm::LLVMContext &Context; |
| 5463 | const llvm::DataLayout &DL; |
| 5464 | SmallVector<llvm::Type*, 8> Elems; |
| 5465 | uint64_t Size; |
| 5466 | bool InReg; |
| 5467 | |
| 5468 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5469 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5470 | |
| 5471 | // Pad Elems with integers until Size is ToSize. |
| 5472 | void pad(uint64_t ToSize) { |
| 5473 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5474 | if (ToSize == Size) |
| 5475 | return; |
| 5476 | |
| 5477 | // Finish the current 64-bit word. |
| 5478 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5479 | if (Aligned > Size && Aligned <= ToSize) { |
| 5480 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5481 | Size = Aligned; |
| 5482 | } |
| 5483 | |
| 5484 | // Add whole 64-bit words. |
| 5485 | while (Size + 64 <= ToSize) { |
| 5486 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5487 | Size += 64; |
| 5488 | } |
| 5489 | |
| 5490 | // Final in-word padding. |
| 5491 | if (Size < ToSize) { |
| 5492 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5493 | Size = ToSize; |
| 5494 | } |
| 5495 | } |
| 5496 | |
| 5497 | // Add a floating point element at Offset. |
| 5498 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5499 | // Unaligned floats are treated as integers. |
| 5500 | if (Offset % Bits) |
| 5501 | return; |
| 5502 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5503 | if (Bits < 64) |
| 5504 | InReg = true; |
| 5505 | pad(Offset); |
| 5506 | Elems.push_back(Ty); |
| 5507 | Size = Offset + Bits; |
| 5508 | } |
| 5509 | |
| 5510 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5511 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5512 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5513 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5514 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5515 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5516 | switch (ElemTy->getTypeID()) { |
| 5517 | case llvm::Type::StructTyID: |
| 5518 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5519 | break; |
| 5520 | case llvm::Type::FloatTyID: |
| 5521 | addFloat(ElemOffset, ElemTy, 32); |
| 5522 | break; |
| 5523 | case llvm::Type::DoubleTyID: |
| 5524 | addFloat(ElemOffset, ElemTy, 64); |
| 5525 | break; |
| 5526 | case llvm::Type::FP128TyID: |
| 5527 | addFloat(ElemOffset, ElemTy, 128); |
| 5528 | break; |
| 5529 | case llvm::Type::PointerTyID: |
| 5530 | if (ElemOffset % 64 == 0) { |
| 5531 | pad(ElemOffset); |
| 5532 | Elems.push_back(ElemTy); |
| 5533 | Size += 64; |
| 5534 | } |
| 5535 | break; |
| 5536 | default: |
| 5537 | break; |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | // Check if Ty is a usable substitute for the coercion type. |
| 5543 | bool isUsableType(llvm::StructType *Ty) const { |
| 5544 | if (Ty->getNumElements() != Elems.size()) |
| 5545 | return false; |
| 5546 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5547 | if (Elems[i] != Ty->getElementType(i)) |
| 5548 | return false; |
| 5549 | return true; |
| 5550 | } |
| 5551 | |
| 5552 | // Get the coercion type as a literal struct type. |
| 5553 | llvm::Type *getType() const { |
| 5554 | if (Elems.size() == 1) |
| 5555 | return Elems.front(); |
| 5556 | else |
| 5557 | return llvm::StructType::get(Context, Elems); |
| 5558 | } |
| 5559 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5560 | }; |
| 5561 | } // end anonymous namespace |
| 5562 | |
| 5563 | ABIArgInfo |
| 5564 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5565 | if (Ty->isVoidType()) |
| 5566 | return ABIArgInfo::getIgnore(); |
| 5567 | |
| 5568 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5569 | |
| 5570 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5571 | // pointer / sret pointer. |
| 5572 | if (Size > SizeLimit) |
| 5573 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5574 | |
| 5575 | // Treat an enum type as its underlying type. |
| 5576 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5577 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5578 | |
| 5579 | // Integer types smaller than a register are extended. |
| 5580 | if (Size < 64 && Ty->isIntegerType()) |
| 5581 | return ABIArgInfo::getExtend(); |
| 5582 | |
| 5583 | // Other non-aggregates go in registers. |
| 5584 | if (!isAggregateTypeForABI(Ty)) |
| 5585 | return ABIArgInfo::getDirect(); |
| 5586 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 5587 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 5588 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 5589 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 5590 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5591 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5592 | // 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] | 5593 | // Build a coercion type from the LLVM struct type. |
| 5594 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5595 | if (!StrTy) |
| 5596 | return ABIArgInfo::getDirect(); |
| 5597 | |
| 5598 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5599 | CB.addStruct(0, StrTy); |
| 5600 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5601 | |
| 5602 | // Try to use the original type for coercion. |
| 5603 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5604 | |
| 5605 | if (CB.InReg) |
| 5606 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5607 | else |
| 5608 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5612 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5613 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 5614 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5615 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5616 | AI.setCoerceToType(ArgTy); |
| 5617 | |
| 5618 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 5619 | CGBuilderTy &Builder = CGF.Builder; |
| 5620 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5621 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5622 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 5623 | llvm::Value *ArgAddr; |
| 5624 | unsigned Stride; |
| 5625 | |
| 5626 | switch (AI.getKind()) { |
| 5627 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 5628 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5629 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5630 | |
| 5631 | case ABIArgInfo::Extend: |
| 5632 | Stride = 8; |
| 5633 | ArgAddr = Builder |
| 5634 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 5635 | "extend"); |
| 5636 | break; |
| 5637 | |
| 5638 | case ABIArgInfo::Direct: |
| 5639 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 5640 | ArgAddr = Addr; |
| 5641 | break; |
| 5642 | |
| 5643 | case ABIArgInfo::Indirect: |
| 5644 | Stride = 8; |
| 5645 | ArgAddr = Builder.CreateBitCast(Addr, |
| 5646 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 5647 | "indirect"); |
| 5648 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 5649 | break; |
| 5650 | |
| 5651 | case ABIArgInfo::Ignore: |
| 5652 | return llvm::UndefValue::get(ArgPtrTy); |
| 5653 | } |
| 5654 | |
| 5655 | // Update VAList. |
| 5656 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 5657 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 5658 | |
| 5659 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5663 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
| 5664 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5665 | it != ie; ++it) |
| 5666 | it->info = classifyType(it->type, 16 * 8); |
| 5667 | } |
| 5668 | |
| 5669 | namespace { |
| 5670 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5671 | public: |
| 5672 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5673 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 5674 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5675 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 5676 | return 14; |
| 5677 | } |
| 5678 | |
| 5679 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5680 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5681 | }; |
| 5682 | } // end anonymous namespace |
| 5683 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 5684 | bool |
| 5685 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5686 | llvm::Value *Address) const { |
| 5687 | // This is calculated from the LLVM and GCC tables and verified |
| 5688 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 5689 | |
| 5690 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 5691 | |
| 5692 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 5693 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 5694 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 5695 | |
| 5696 | // 0-31: the 8-byte general-purpose registers |
| 5697 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 5698 | |
| 5699 | // 32-63: f0-31, the 4-byte floating-point registers |
| 5700 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 5701 | |
| 5702 | // Y = 64 |
| 5703 | // PSR = 65 |
| 5704 | // WIM = 66 |
| 5705 | // TBR = 67 |
| 5706 | // PC = 68 |
| 5707 | // NPC = 69 |
| 5708 | // FSR = 70 |
| 5709 | // CSR = 71 |
| 5710 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 5711 | |
| 5712 | // 72-87: d0-15, the 8-byte floating-point registers |
| 5713 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 5714 | |
| 5715 | return false; |
| 5716 | } |
| 5717 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5718 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5719 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 5720 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5721 | //===----------------------------------------------------------------------===// |
| 5722 | namespace { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5723 | class XCoreABIInfo : public DefaultABIInfo { |
| 5724 | public: |
| 5725 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5726 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5727 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5728 | }; |
| 5729 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 5730 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5731 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 5732 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5733 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5734 | }; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5735 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5736 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5737 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5738 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5739 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5740 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5741 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5742 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 5743 | CGF.Int8PtrPtrTy); |
| 5744 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5745 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5746 | // Handle the argument. |
| 5747 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5748 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5749 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5750 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5751 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5752 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 5753 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5754 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5755 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 5756 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5757 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5758 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5759 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 5760 | ArgSize = 0; |
| 5761 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5762 | case ABIArgInfo::Extend: |
| 5763 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5764 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 5765 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 5766 | if (ArgSize < 4) |
| 5767 | ArgSize = 4; |
| 5768 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5769 | case ABIArgInfo::Indirect: |
| 5770 | llvm::Value *ArgAddr; |
| 5771 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 5772 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5773 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 5774 | ArgSize = 4; |
| 5775 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5776 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 5777 | |
| 5778 | // Increment the VAList. |
| 5779 | if (ArgSize) { |
| 5780 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 5781 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 5782 | } |
| 5783 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5784 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5785 | |
| 5786 | //===----------------------------------------------------------------------===// |
| 5787 | // Driver code |
| 5788 | //===----------------------------------------------------------------------===// |
| 5789 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5790 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5791 | if (TheTargetCodeGenInfo) |
| 5792 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5793 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5794 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 5795 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5796 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5797 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5798 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 5799 | case llvm::Triple::le32: |
| 5800 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5801 | case llvm::Triple::mips: |
| 5802 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5803 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 5804 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 5805 | case llvm::Triple::mips64: |
| 5806 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5807 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 5808 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5809 | case llvm::Triple::aarch64: |
Christian Pirker | 9b019ae | 2014-02-25 13:51:00 +0000 | [diff] [blame] | 5810 | case llvm::Triple::aarch64_be: |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5811 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 5812 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5813 | case llvm::Triple::arm: |
| 5814 | case llvm::Triple::thumb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5815 | { |
| 5816 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5817 | if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5818 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 5819 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5820 | (CodeGenOpts.FloatABI != "soft" && |
| 5821 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5822 | Kind = ARMABIInfo::AAPCS_VFP; |
| 5823 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5824 | switch (Triple.getOS()) { |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5825 | case llvm::Triple::NaCl: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5826 | return *(TheTargetCodeGenInfo = |
| 5827 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 5828 | default: |
| 5829 | return *(TheTargetCodeGenInfo = |
| 5830 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 5831 | } |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5832 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5833 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5834 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5835 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 5836 | case llvm::Triple::ppc64: |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 5837 | if (Triple.isOSBinFormatELF()) |
| 5838 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 5839 | else |
| 5840 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 5841 | case llvm::Triple::ppc64le: |
| 5842 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
| 5843 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5844 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 5845 | case llvm::Triple::nvptx: |
| 5846 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5847 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5848 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5849 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5850 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5851 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5852 | case llvm::Triple::systemz: |
| 5853 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 5854 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5855 | case llvm::Triple::tce: |
| 5856 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 5857 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5858 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5859 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 5860 | bool IsSmallStructInRegABI = |
| 5861 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
| 5862 | bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 5863 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5864 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 5865 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 5866 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5867 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 5868 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 5869 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5870 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5871 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5872 | new X86_32TargetCodeGenInfo(Types, |
| 5873 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 5874 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 5875 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5876 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5877 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5878 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5879 | case llvm::Triple::x86_64: { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5880 | bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5881 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5882 | switch (Triple.getOS()) { |
| 5883 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 31ea2f1 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 5884 | case llvm::Triple::MinGW32: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5885 | case llvm::Triple::Cygwin: |
| 5886 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5887 | case llvm::Triple::NaCl: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5888 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 5889 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5890 | default: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5891 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 5892 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5893 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5894 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5895 | case llvm::Triple::hexagon: |
| 5896 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5897 | case llvm::Triple::sparcv9: |
| 5898 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5899 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 5900 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5901 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5902 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5903 | } |