Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | c4a59eb | 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 | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 28 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 29 | llvm::Value *Array, |
| 30 | llvm::Value *Value, |
| 31 | unsigned FirstIndex, |
| 32 | unsigned LastIndex) { |
| 33 | // Alternatively, we could emit this as a loop in the source. |
| 34 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 35 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 36 | Builder.CreateStore(Value, Cell); |
| 37 | } |
| 38 | } |
| 39 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 40 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 41 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 42 | T->isMemberFunctionPointerType(); |
| 43 | } |
| 44 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 45 | ABIInfo::~ABIInfo() {} |
| 46 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 47 | static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) { |
| 48 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 49 | if (!RD) |
| 50 | return false; |
Mark Lacey | 2529660 | 2013-10-02 20:35:23 +0000 | [diff] [blame^] | 51 | return CGT.getCXXABI().isReturnTypeIndirect(RD); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) { |
| 56 | const RecordType *RT = T->getAs<RecordType>(); |
| 57 | if (!RT) |
| 58 | return false; |
| 59 | return isRecordReturnIndirect(RT, CGT); |
| 60 | } |
| 61 | |
| 62 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
| 63 | CodeGen::CodeGenTypes &CGT) { |
| 64 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 65 | if (!RD) |
| 66 | return CGCXXABI::RAA_Default; |
Mark Lacey | 2529660 | 2013-10-02 20:35:23 +0000 | [diff] [blame^] | 67 | return CGT.getCXXABI().getRecordArgABI(RD); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
| 71 | CodeGen::CodeGenTypes &CGT) { |
| 72 | const RecordType *RT = T->getAs<RecordType>(); |
| 73 | if (!RT) |
| 74 | return CGCXXABI::RAA_Default; |
| 75 | return getRecordArgABI(RT, CGT); |
| 76 | } |
| 77 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 78 | ASTContext &ABIInfo::getContext() const { |
| 79 | return CGT.getContext(); |
| 80 | } |
| 81 | |
| 82 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 83 | return CGT.getLLVMContext(); |
| 84 | } |
| 85 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 86 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 87 | return CGT.getDataLayout(); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 90 | const TargetInfo &ABIInfo::getTarget() const { |
| 91 | return CGT.getTarget(); |
| 92 | } |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 93 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 94 | void ABIArgInfo::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 95 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 96 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 97 | switch (TheKind) { |
| 98 | case Direct: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 99 | OS << "Direct Type="; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 100 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 101 | Ty->print(OS); |
| 102 | else |
| 103 | OS << "null"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 104 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 105 | case Extend: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 106 | OS << "Extend"; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 107 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 108 | case Ignore: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 109 | OS << "Ignore"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 111 | case Indirect: |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 112 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | e9b5d77 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 113 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 114 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 115 | break; |
| 116 | case Expand: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 117 | OS << "Expand"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 118 | break; |
| 119 | } |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 120 | OS << ")\n"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 123 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 124 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 125 | // If someone can figure out a general rule for this, that would be great. |
| 126 | // It's probably just doomed to be platform-dependent, though. |
| 127 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 128 | // Verified for: |
| 129 | // x86-64 FreeBSD, Linux, Darwin |
| 130 | // x86-32 FreeBSD, Linux, Darwin |
| 131 | // PowerPC Linux, Darwin |
| 132 | // ARM Darwin (*not* EABI) |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 133 | // AArch64 Linux |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 134 | return 32; |
| 135 | } |
| 136 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 137 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 138 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 139 | // The following conventions are known to require this to be false: |
| 140 | // x86_stdcall |
| 141 | // MIPS |
| 142 | // For everything else, we just prefer false unless we opt out. |
| 143 | return false; |
| 144 | } |
| 145 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 146 | void |
| 147 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 148 | llvm::SmallString<24> &Opt) const { |
| 149 | // This assumes the user is passing a library name like "rt" instead of a |
| 150 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 151 | // dynamic. |
| 152 | Opt = "-l"; |
| 153 | Opt += Lib; |
| 154 | } |
| 155 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 156 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 157 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 158 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 159 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 160 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 161 | bool AllowArrays) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 162 | if (FD->isUnnamedBitfield()) |
| 163 | return true; |
| 164 | |
| 165 | QualType FT = FD->getType(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 166 | |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 167 | // Constant arrays of empty records count as empty, strip them off. |
| 168 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 169 | if (AllowArrays) |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 170 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 171 | if (AT->getSize() == 0) |
| 172 | return true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 173 | FT = AT->getElementType(); |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 174 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 176 | const RecordType *RT = FT->getAs<RecordType>(); |
| 177 | if (!RT) |
| 178 | return false; |
| 179 | |
| 180 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 181 | // |
| 182 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 183 | // current ABI. |
| 184 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 185 | return false; |
| 186 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 187 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 190 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 191 | /// fields. Note that a structure with a flexible array member is not |
| 192 | /// considered empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 193 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 194 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 195 | if (!RT) |
| 196 | return 0; |
| 197 | const RecordDecl *RD = RT->getDecl(); |
| 198 | if (RD->hasFlexibleArrayMember()) |
| 199 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 200 | |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 201 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 202 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 203 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 204 | e = CXXRD->bases_end(); i != e; ++i) |
| 205 | if (!isEmptyRecord(Context, i->getType(), true)) |
| 206 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 207 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 208 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 209 | i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 210 | if (!isEmptyField(Context, *i, AllowArrays)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | return false; |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | /// isSingleElementStruct - Determine if a structure is a "single |
| 216 | /// element struct", i.e. it has exactly one non-empty field or |
| 217 | /// exactly one field which is itself a single element |
| 218 | /// struct. Structures with flexible array members are never |
| 219 | /// considered single element structs. |
| 220 | /// |
| 221 | /// \return The field declaration for the single non-empty field, if |
| 222 | /// it exists. |
| 223 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 224 | const RecordType *RT = T->getAsStructureType(); |
| 225 | if (!RT) |
| 226 | return 0; |
| 227 | |
| 228 | const RecordDecl *RD = RT->getDecl(); |
| 229 | if (RD->hasFlexibleArrayMember()) |
| 230 | return 0; |
| 231 | |
| 232 | const Type *Found = 0; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 234 | // If this is a C++ record, check the bases first. |
| 235 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 236 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 237 | e = CXXRD->bases_end(); i != e; ++i) { |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 238 | // Ignore empty records. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 239 | if (isEmptyRecord(Context, i->getType(), true)) |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 240 | continue; |
| 241 | |
| 242 | // If we already found an element then this isn't a single-element struct. |
| 243 | if (Found) |
| 244 | return 0; |
| 245 | |
| 246 | // If this is non-empty and not a single element struct, the composite |
| 247 | // cannot be a single element struct. |
| 248 | Found = isSingleElementStruct(i->getType(), Context); |
| 249 | if (!Found) |
| 250 | return 0; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Check for single element. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 255 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 256 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 257 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 258 | QualType FT = FD->getType(); |
| 259 | |
| 260 | // Ignore empty fields. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 261 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 262 | continue; |
| 263 | |
| 264 | // If we already found an element then this isn't a single-element |
| 265 | // struct. |
| 266 | if (Found) |
| 267 | return 0; |
| 268 | |
| 269 | // Treat single element arrays as the element. |
| 270 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 271 | if (AT->getSize().getZExtValue() != 1) |
| 272 | break; |
| 273 | FT = AT->getElementType(); |
| 274 | } |
| 275 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 276 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 277 | Found = FT.getTypePtr(); |
| 278 | } else { |
| 279 | Found = isSingleElementStruct(FT, Context); |
| 280 | if (!Found) |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 285 | // We don't consider a struct a single-element struct if it has |
| 286 | // padding beyond the element type. |
| 287 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| 288 | return 0; |
| 289 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 290 | return Found; |
| 291 | } |
| 292 | |
| 293 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | db748a3 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 294 | // Treat complex types as the element type. |
| 295 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 296 | Ty = CTy->getElementType(); |
| 297 | |
| 298 | // Check for a type which we know has a simple scalar argument-passing |
| 299 | // convention without any padding. (We're specifically looking for 32 |
| 300 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 301 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | db748a3 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 302 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 303 | return false; |
| 304 | |
| 305 | uint64_t Size = Context.getTypeSize(Ty); |
| 306 | return Size == 32 || Size == 64; |
| 307 | } |
| 308 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 309 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 310 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 311 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 312 | /// inhibiting optimizations. |
| 313 | /// |
| 314 | // FIXME: This predicate is missing many cases, currently it just follows |
| 315 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 316 | // should probably make this smarter, or better yet make the LLVM backend |
| 317 | // capable of handling it. |
| 318 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 319 | // We can only expand structure types. |
| 320 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 321 | if (!RT) |
| 322 | return false; |
| 323 | |
| 324 | // We can only expand (C) structures. |
| 325 | // |
| 326 | // FIXME: This needs to be generalized to handle classes as well. |
| 327 | const RecordDecl *RD = RT->getDecl(); |
| 328 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 329 | return false; |
| 330 | |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 331 | uint64_t Size = 0; |
| 332 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 333 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 334 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 335 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 336 | |
| 337 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 338 | return false; |
| 339 | |
| 340 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 341 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 342 | // counts as "basic" is more complicated than what we were doing previously. |
| 343 | if (FD->isBitField()) |
| 344 | return false; |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 345 | |
| 346 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 349 | // Make sure there are not any holes in the struct. |
| 350 | if (Size != Context.getTypeSize(Ty)) |
| 351 | return false; |
| 352 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | namespace { |
| 357 | /// DefaultABIInfo - The default implementation for ABI specific |
| 358 | /// details. This implementation provides information which results in |
| 359 | /// self-consistent and sensible LLVM IR generation, but does not |
| 360 | /// conform to any particular ABI. |
| 361 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 362 | public: |
| 363 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 364 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 365 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 366 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 367 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 368 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 369 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 370 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 371 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 372 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 376 | CodeGenFunction &CGF) const; |
| 377 | }; |
| 378 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 379 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 380 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 381 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 382 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 386 | CodeGenFunction &CGF) const { |
| 387 | return 0; |
| 388 | } |
| 389 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 390 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 9030693 | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 391 | if (isAggregateTypeForABI(Ty)) { |
| 392 | // Records with non trivial destructors/constructors should not be passed |
| 393 | // by value. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 394 | if (isRecordReturnIndirect(Ty, CGT)) |
Jan Wen Voung | 9030693 | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 395 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 396 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 397 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 9030693 | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 398 | } |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 399 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 400 | // Treat an enum type as its underlying type. |
| 401 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 402 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 403 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 404 | return (Ty->isPromotableIntegerType() ? |
| 405 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Bob Wilson | 0024f94 | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 408 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 409 | if (RetTy->isVoidType()) |
| 410 | return ABIArgInfo::getIgnore(); |
| 411 | |
| 412 | if (isAggregateTypeForABI(RetTy)) |
| 413 | return ABIArgInfo::getIndirect(0); |
| 414 | |
| 415 | // Treat an enum type as its underlying type. |
| 416 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 417 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 418 | |
| 419 | return (RetTy->isPromotableIntegerType() ? |
| 420 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 421 | } |
| 422 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 425 | // |
| 426 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 427 | // are always passed on the stack. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | |
| 430 | class PNaClABIInfo : public ABIInfo { |
| 431 | public: |
| 432 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 433 | |
| 434 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 435 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 436 | |
| 437 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 438 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 439 | CodeGenFunction &CGF) const; |
| 440 | }; |
| 441 | |
| 442 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 443 | public: |
| 444 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 445 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 446 | }; |
| 447 | |
| 448 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 449 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 450 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 451 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 452 | it != ie; ++it) |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 453 | it->info = classifyArgumentType(it->type); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 457 | CodeGenFunction &CGF) const { |
| 458 | return 0; |
| 459 | } |
| 460 | |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 461 | /// \brief Classify argument of given type \p Ty. |
| 462 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 463 | if (isAggregateTypeForABI(Ty)) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 464 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 465 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 466 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 467 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 468 | // Treat an enum type as its underlying type. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 469 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 470 | } else if (Ty->isFloatingType()) { |
| 471 | // Floating-point types don't go inreg. |
| 472 | return ABIArgInfo::getDirect(); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 473 | } |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 474 | |
| 475 | return (Ty->isPromotableIntegerType() ? |
| 476 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 480 | if (RetTy->isVoidType()) |
| 481 | return ABIArgInfo::getIgnore(); |
| 482 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 483 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 484 | if (isAggregateTypeForABI(RetTy)) |
| 485 | return ABIArgInfo::getIndirect(0); |
| 486 | |
| 487 | // Treat an enum type as its underlying type. |
| 488 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 489 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 490 | |
| 491 | return (RetTy->isPromotableIntegerType() ? |
| 492 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 493 | } |
| 494 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 495 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 496 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 497 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 498 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 499 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 500 | IRType->getScalarSizeInBits() != 64; |
| 501 | } |
| 502 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 503 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 504 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 505 | llvm::Type* Ty) { |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 506 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 507 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 508 | // Invalid MMX constraint |
| 509 | return 0; |
| 510 | } |
| 511 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 512 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // No operation needed |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 516 | return Ty; |
| 517 | } |
| 518 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
| 520 | // X86-32 ABI Implementation |
| 521 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 522 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 523 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 524 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 525 | enum Class { |
| 526 | Integer, |
| 527 | Float |
| 528 | }; |
| 529 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 530 | static const unsigned MinABIStackAlignInBytes = 4; |
| 531 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 532 | bool IsDarwinVectorABI; |
| 533 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 534 | bool IsWin32StructABI; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 535 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | |
| 537 | static bool isRegisterSize(unsigned Size) { |
| 538 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 539 | } |
| 540 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 541 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 542 | unsigned callingConvention); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 543 | |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 544 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 545 | /// such that the argument will be passed in memory. |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 546 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, |
| 547 | unsigned &FreeRegs) const; |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 548 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 549 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 550 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 551 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 552 | Class classify(QualType Ty) const; |
Rafael Espindola | b33a3c4 | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 553 | ABIArgInfo classifyReturnType(QualType RetTy, |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 554 | unsigned callingConvention) const; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 555 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs, |
| 556 | bool IsFastCall) const; |
| 557 | bool shouldUseInReg(QualType Ty, unsigned &FreeRegs, |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 558 | bool IsFastCall, bool &NeedsPadding) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 559 | |
Rafael Espindola | b33a3c4 | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 560 | public: |
| 561 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 562 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 563 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 564 | CodeGenFunction &CGF) const; |
| 565 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 566 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 567 | unsigned r) |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 568 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 569 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 570 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 571 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 572 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 573 | public: |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 574 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 575 | bool d, bool p, bool w, unsigned r) |
| 576 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 577 | |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 578 | static bool isStructReturnInRegABI( |
| 579 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 580 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 581 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 582 | CodeGen::CodeGenModule &CGM) const; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 583 | |
| 584 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 585 | // Darwin uses different dwarf register numbers for EH. |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 586 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 587 | return 4; |
| 588 | } |
| 589 | |
| 590 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 591 | llvm::Value *Address) const; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 592 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 593 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 594 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 595 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 596 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 597 | } |
| 598 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 602 | |
| 603 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 604 | /// passed in a register (for the Darwin ABI). |
| 605 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 606 | ASTContext &Context, |
| 607 | unsigned callingConvention) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 608 | uint64_t Size = Context.getTypeSize(Ty); |
| 609 | |
| 610 | // Type must be register sized. |
| 611 | if (!isRegisterSize(Size)) |
| 612 | return false; |
| 613 | |
| 614 | if (Ty->isVectorType()) { |
| 615 | // 64- and 128- bit vectors inside structures are not returned in |
| 616 | // registers. |
| 617 | if (Size == 64 || Size == 128) |
| 618 | return false; |
| 619 | |
| 620 | return true; |
| 621 | } |
| 622 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 623 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 624 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 625 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 626 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 627 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 628 | return true; |
| 629 | |
| 630 | // Arrays are treated like records. |
| 631 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 632 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
| 633 | callingConvention); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 634 | |
| 635 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 636 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 637 | if (!RT) return false; |
| 638 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 639 | // FIXME: Traverse bases here too. |
| 640 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 641 | // For thiscall conventions, structures will never be returned in |
| 642 | // a register. This is for compatibility with the MSVC ABI |
| 643 | if (callingConvention == llvm::CallingConv::X86_ThisCall && |
| 644 | RT->isStructureType()) { |
| 645 | return false; |
| 646 | } |
| 647 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 648 | // Structure types are passed in register if all fields would be |
| 649 | // passed in a register. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 650 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 651 | e = RT->getDecl()->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 652 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 653 | |
| 654 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 655 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 656 | continue; |
| 657 | |
| 658 | // Check fields recursively. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 659 | if (!shouldReturnTypeInRegister(FD->getType(), Context, |
| 660 | callingConvention)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 661 | return false; |
| 662 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 666 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 667 | unsigned callingConvention) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 668 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 669 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 670 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 671 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 672 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 673 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 674 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 675 | |
| 676 | // 128-bit vectors are a special case; they are returned in |
| 677 | // registers and we need to make sure to pick a type the LLVM |
| 678 | // backend will like. |
| 679 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 680 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 681 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 682 | |
| 683 | // Always return in register if it fits in a general purpose |
| 684 | // register, or if it is 64 bits and has a single element. |
| 685 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 686 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 687 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 688 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 689 | |
| 690 | return ABIArgInfo::getIndirect(0); |
| 691 | } |
| 692 | |
| 693 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 694 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 695 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 696 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 697 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 698 | if (isRecordReturnIndirect(RT, CGT)) |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 699 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 701 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 702 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 703 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 704 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 705 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 706 | // If specified, structs and unions are always indirect. |
| 707 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 708 | return ABIArgInfo::getIndirect(0); |
| 709 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 710 | // Small structures which are register sized are generally returned |
| 711 | // in a register. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 712 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), |
| 713 | callingConvention)) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 714 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 715 | |
| 716 | // As a special-case, if the struct is a "single-element" struct, and |
| 717 | // the field is of type "float" or "double", return it in a |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 718 | // floating-point register. (MSVC does not apply this special case.) |
| 719 | // We apply a similar transformation for pointer types to improve the |
| 720 | // quality of the generated IR. |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 721 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 722 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 723 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 724 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 725 | |
| 726 | // FIXME: We should be able to narrow this integer in cases with dead |
| 727 | // padding. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 728 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 732 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 733 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 734 | // Treat an enum type as its underlying type. |
| 735 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 736 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 737 | |
| 738 | return (RetTy->isPromotableIntegerType() ? |
| 739 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 742 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 743 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 744 | } |
| 745 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 746 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 747 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 748 | if (!RT) |
| 749 | return 0; |
| 750 | const RecordDecl *RD = RT->getDecl(); |
| 751 | |
| 752 | // If this is a C++ record, check the bases first. |
| 753 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 754 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 755 | e = CXXRD->bases_end(); i != e; ++i) |
| 756 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 757 | return false; |
| 758 | |
| 759 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 760 | i != e; ++i) { |
| 761 | QualType FT = i->getType(); |
| 762 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 763 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 764 | return true; |
| 765 | |
| 766 | if (isRecordWithSSEVectorType(Context, FT)) |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | return false; |
| 771 | } |
| 772 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 773 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 774 | unsigned Align) const { |
| 775 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 776 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 777 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 778 | return 0; // Use default alignment. |
| 779 | |
| 780 | // On non-Darwin, the stack type alignment is always 4. |
| 781 | if (!IsDarwinVectorABI) { |
| 782 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 783 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 784 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 785 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 786 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 787 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 788 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 789 | return 16; |
| 790 | |
| 791 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 794 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 795 | unsigned &FreeRegs) const { |
| 796 | if (!ByVal) { |
| 797 | if (FreeRegs) { |
| 798 | --FreeRegs; // Non byval indirects just use one pointer. |
| 799 | return ABIArgInfo::getIndirectInReg(0, false); |
| 800 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 801 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 802 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 803 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 804 | // Compute the byval alignment. |
| 805 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 806 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 807 | if (StackAlign == 0) |
Chris Lattner | de92d73 | 2011-05-22 23:35:00 +0000 | [diff] [blame] | 808 | return ABIArgInfo::getIndirect(4); |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 809 | |
| 810 | // If the stack alignment is less than the type alignment, realign the |
| 811 | // argument. |
| 812 | if (StackAlign < TypeAlign) |
| 813 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 814 | /*Realign=*/true); |
| 815 | |
| 816 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 819 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 820 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 821 | if (!T) |
| 822 | T = Ty.getTypePtr(); |
| 823 | |
| 824 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 825 | BuiltinType::Kind K = BT->getKind(); |
| 826 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 827 | return Float; |
| 828 | } |
| 829 | return Integer; |
| 830 | } |
| 831 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 832 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs, |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 833 | bool IsFastCall, bool &NeedsPadding) const { |
| 834 | NeedsPadding = false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 835 | Class C = classify(Ty); |
| 836 | if (C == Float) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 837 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 838 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 839 | unsigned Size = getContext().getTypeSize(Ty); |
| 840 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | 5f14fcb | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 841 | |
| 842 | if (SizeInRegs == 0) |
| 843 | return false; |
| 844 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 845 | if (SizeInRegs > FreeRegs) { |
| 846 | FreeRegs = 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 847 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 848 | } |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 849 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 850 | FreeRegs -= SizeInRegs; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 851 | |
| 852 | if (IsFastCall) { |
| 853 | if (Size > 32) |
| 854 | return false; |
| 855 | |
| 856 | if (Ty->isIntegralOrEnumerationType()) |
| 857 | return true; |
| 858 | |
| 859 | if (Ty->isPointerType()) |
| 860 | return true; |
| 861 | |
| 862 | if (Ty->isReferenceType()) |
| 863 | return true; |
| 864 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 865 | if (FreeRegs) |
| 866 | NeedsPadding = true; |
| 867 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 871 | return true; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 874 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 875 | unsigned &FreeRegs, |
| 876 | bool IsFastCall) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 877 | // FIXME: Set alignment on indirect arguments. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 878 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 879 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 880 | if (IsWin32StructABI) |
| 881 | return getIndirectResult(Ty, true, FreeRegs); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 882 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 883 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) |
| 884 | return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs); |
| 885 | |
| 886 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 887 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 888 | return getIndirectResult(Ty, true, FreeRegs); |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 889 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 890 | |
Eli Friedman | 5a4d352 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 891 | // Ignore empty structs/unions. |
Eli Friedman | 5a1ac89 | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 892 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 893 | return ABIArgInfo::getIgnore(); |
| 894 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 895 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 896 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 897 | bool NeedsPadding; |
| 898 | if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) { |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 899 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | b9bad79 | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 900 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 901 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 902 | return ABIArgInfo::getDirectInReg(Result); |
| 903 | } |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 904 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 906 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 907 | // of those arguments will match the struct. This is important because the |
| 908 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 909 | // optimizations. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 910 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 911 | canExpandIndirectArgument(Ty, getContext())) |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 912 | return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 913 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 914 | return getIndirectResult(Ty, true, FreeRegs); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 917 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 918 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 919 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 920 | if (IsDarwinVectorABI) { |
| 921 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 922 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 923 | (Size == 64 && VT->getNumElements() == 1)) |
| 924 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 925 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 926 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 927 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 928 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 929 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 930 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 931 | return ABIArgInfo::getDirect(); |
| 932 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 933 | |
| 934 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 935 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 936 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 937 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 938 | bool NeedsPadding; |
| 939 | bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 940 | |
| 941 | if (Ty->isPromotableIntegerType()) { |
| 942 | if (InReg) |
| 943 | return ABIArgInfo::getExtendInReg(); |
| 944 | return ABIArgInfo::getExtend(); |
| 945 | } |
| 946 | if (InReg) |
| 947 | return ABIArgInfo::getDirectInReg(); |
| 948 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 951 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 952 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), |
| 953 | FI.getCallingConvention()); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 954 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 955 | unsigned CC = FI.getCallingConvention(); |
| 956 | bool IsFastCall = CC == llvm::CallingConv::X86_FastCall; |
| 957 | unsigned FreeRegs; |
| 958 | if (IsFastCall) |
| 959 | FreeRegs = 2; |
| 960 | else if (FI.getHasRegParm()) |
| 961 | FreeRegs = FI.getRegParm(); |
| 962 | else |
| 963 | FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 964 | |
| 965 | // If the return value is indirect, then the hidden argument is consuming one |
| 966 | // integer register. |
| 967 | if (FI.getReturnInfo().isIndirect() && FreeRegs) { |
| 968 | --FreeRegs; |
| 969 | ABIArgInfo &Old = FI.getReturnInfo(); |
| 970 | Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(), |
| 971 | Old.getIndirectByVal(), |
| 972 | Old.getIndirectRealign()); |
| 973 | } |
| 974 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 975 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 976 | it != ie; ++it) |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 977 | it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall); |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 980 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 981 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 982 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 983 | |
| 984 | CGBuilderTy &Builder = CGF.Builder; |
| 985 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 986 | "ap"); |
| 987 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 988 | |
| 989 | // Compute if the address needs to be aligned |
| 990 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 991 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 992 | Align = std::max(Align, 4U); |
| 993 | if (Align > 4) { |
| 994 | // addr = (addr + align - 1) & -align; |
| 995 | llvm::Value *Offset = |
| 996 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 997 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 998 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 999 | CGF.Int32Ty); |
| 1000 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1001 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1002 | Addr->getType(), |
| 1003 | "ap.cur.aligned"); |
| 1004 | } |
| 1005 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1006 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1007 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1008 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1009 | |
| 1010 | uint64_t Offset = |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1011 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1012 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1013 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1014 | "ap.next"); |
| 1015 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1016 | |
| 1017 | return AddrTyped; |
| 1018 | } |
| 1019 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1020 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1021 | llvm::GlobalValue *GV, |
| 1022 | CodeGen::CodeGenModule &CGM) const { |
| 1023 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1024 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1025 | // Get the LLVM function. |
| 1026 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1027 | |
| 1028 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1029 | llvm::AttrBuilder B; |
Bill Wendling | e91e9ec | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1030 | B.addStackAlignmentAttr(16); |
Bill Wendling | 909b6de | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1031 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1032 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1033 | llvm::AttributeSet::FunctionIndex, |
| 1034 | B)); |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1039 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1040 | CodeGen::CodeGenFunction &CGF, |
| 1041 | llvm::Value *Address) const { |
| 1042 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1044 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1045 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1046 | // 0-7 are the eight integer registers; the order is different |
| 1047 | // on Darwin (for EH), but the range is the same. |
| 1048 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1049 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1050 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1051 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1052 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1053 | // These have size 16, which is sizeof(long double) on |
| 1054 | // platforms with 8-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1055 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1056 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1057 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1058 | } else { |
| 1059 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1060 | // reason. |
| 1061 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1062 | |
| 1063 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1064 | // These have size 12, which is sizeof(long double) on |
| 1065 | // platforms with 4-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1066 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1067 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1068 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1069 | |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1073 | //===----------------------------------------------------------------------===// |
| 1074 | // X86-64 ABI Implementation |
| 1075 | //===----------------------------------------------------------------------===// |
| 1076 | |
| 1077 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1078 | namespace { |
| 1079 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1080 | class X86_64ABIInfo : public ABIInfo { |
| 1081 | enum Class { |
| 1082 | Integer = 0, |
| 1083 | SSE, |
| 1084 | SSEUp, |
| 1085 | X87, |
| 1086 | X87Up, |
| 1087 | ComplexX87, |
| 1088 | NoClass, |
| 1089 | Memory |
| 1090 | }; |
| 1091 | |
| 1092 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1093 | /// |
| 1094 | /// Merge an accumulating classification \arg Accum with a field |
| 1095 | /// classification \arg Field. |
| 1096 | /// |
| 1097 | /// \param Accum - The accumulating classification. This should |
| 1098 | /// always be either NoClass or the result of a previous merge |
| 1099 | /// call. In addition, this should never be Memory (the caller |
| 1100 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1101 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1102 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1103 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1104 | /// |
| 1105 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1106 | /// final MEMORY or SSE classes when necessary. |
| 1107 | /// |
| 1108 | /// \param AggregateSize - The size of the current aggregate in |
| 1109 | /// the classification process. |
| 1110 | /// |
| 1111 | /// \param Lo - The classification for the parts of the type |
| 1112 | /// residing in the low word of the containing object. |
| 1113 | /// |
| 1114 | /// \param Hi - The classification for the parts of the type |
| 1115 | /// residing in the higher words of the containing object. |
| 1116 | /// |
| 1117 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1118 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1119 | /// classify - Determine the x86_64 register classes in which the |
| 1120 | /// given type T should be passed. |
| 1121 | /// |
| 1122 | /// \param Lo - The classification for the parts of the type |
| 1123 | /// residing in the low word of the containing object. |
| 1124 | /// |
| 1125 | /// \param Hi - The classification for the parts of the type |
| 1126 | /// residing in the high word of the containing object. |
| 1127 | /// |
| 1128 | /// \param OffsetBase - The bit offset of this type in the |
| 1129 | /// containing object. Some parameters are classified different |
| 1130 | /// depending on whether they straddle an eightbyte boundary. |
| 1131 | /// |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1132 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1133 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1134 | /// |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1135 | /// If a word is unused its result will be NoClass; if a type should |
| 1136 | /// be passed in Memory then at least the classification of \arg Lo |
| 1137 | /// will be Memory. |
| 1138 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1139 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1140 | /// |
| 1141 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1142 | /// also be ComplexX87. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1143 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1144 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1145 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1146 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1147 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1148 | unsigned IROffset, QualType SourceTy, |
| 1149 | unsigned SourceOffset) const; |
| 1150 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1151 | unsigned IROffset, QualType SourceTy, |
| 1152 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1153 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1154 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1155 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1156 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1157 | |
| 1158 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1159 | /// such that the argument will be passed in memory. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1160 | /// |
| 1161 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1162 | /// available. |
| 1163 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1165 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1166 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1167 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1168 | unsigned freeIntRegs, |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1169 | unsigned &neededInt, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1170 | unsigned &neededSSE, |
| 1171 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1172 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1173 | bool IsIllegalVectorType(QualType Ty) const; |
| 1174 | |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1175 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1176 | /// unfortunately in ways that were not always consistent with |
| 1177 | /// certain previous compilers. In particular, platforms which |
| 1178 | /// required strict binary compatibility with older versions of GCC |
| 1179 | /// may need to exempt themselves. |
| 1180 | bool honorsRevision0_98() const { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1181 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1184 | bool HasAVX; |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1185 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1186 | // 64-bit hardware. |
| 1187 | bool Has64BitPointers; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1188 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1189 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1190 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1191 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 90da80c | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1192 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1193 | } |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1194 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1195 | bool isPassedUsingAVXType(QualType type) const { |
| 1196 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1197 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1198 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1199 | /*isNamedArg*/true); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1200 | if (info.isDirect()) { |
| 1201 | llvm::Type *ty = info.getCoerceToType(); |
| 1202 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1203 | return (vectorTy->getBitWidth() > 128); |
| 1204 | } |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1208 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1209 | |
| 1210 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1211 | CodeGenFunction &CGF) const; |
| 1212 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1214 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1215 | class WinX86_64ABIInfo : public ABIInfo { |
| 1216 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1217 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1219 | public: |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1220 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1221 | |
| 1222 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1223 | |
| 1224 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1225 | CodeGenFunction &CGF) const; |
| 1226 | }; |
| 1227 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1228 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1229 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1230 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1231 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1232 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1233 | const X86_64ABIInfo &getABIInfo() const { |
| 1234 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1235 | } |
| 1236 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1237 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1238 | return 7; |
| 1239 | } |
| 1240 | |
| 1241 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1242 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1243 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1244 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1245 | // 0-15 are the 16 integer registers. |
| 1246 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1247 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1250 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1251 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1252 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1253 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1254 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1255 | } |
| 1256 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1257 | bool isNoProtoCallVariadic(const CallArgList &args, |
| 1258 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1259 | // The default CC on x86-64 sets %al to the number of SSA |
| 1260 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1261 | // function, so we override the default behavior. However, don't do |
Eli Friedman | 68805fe | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1262 | // that when AVX types are involved: the ABI explicitly states it is |
| 1263 | // undefined, and it doesn't work in practice because of how the ABI |
| 1264 | // defines varargs anyway. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1265 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1266 | bool HasAVXType = false; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1267 | for (CallArgList::const_iterator |
| 1268 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1269 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1270 | HasAVXType = true; |
| 1271 | break; |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1272 | } |
| 1273 | } |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1274 | |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1275 | if (!HasAVXType) |
| 1276 | return true; |
| 1277 | } |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1278 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1279 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1282 | }; |
| 1283 | |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1284 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1285 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1286 | // matches the behavior of MSVC. |
| 1287 | std::string ArgStr = Lib; |
| 1288 | if (Lib.size() <= 4 || |
| 1289 | Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) { |
| 1290 | ArgStr += ".lib"; |
| 1291 | } |
| 1292 | return ArgStr; |
| 1293 | } |
| 1294 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1295 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1296 | public: |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1297 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1298 | bool d, bool p, bool w, unsigned RegParms) |
| 1299 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1300 | |
| 1301 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1302 | llvm::SmallString<24> &Opt) const { |
| 1303 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1304 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1305 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1306 | |
| 1307 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1308 | llvm::StringRef Value, |
| 1309 | llvm::SmallString<32> &Opt) const { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1310 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1311 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1312 | }; |
| 1313 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1314 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1315 | public: |
| 1316 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1317 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1318 | |
| 1319 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1320 | return 7; |
| 1321 | } |
| 1322 | |
| 1323 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1324 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1325 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1327 | // 0-15 are the 16 integer registers. |
| 1328 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1329 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1330 | return false; |
| 1331 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1332 | |
| 1333 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1334 | llvm::SmallString<24> &Opt) const { |
| 1335 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1336 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1337 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1338 | |
| 1339 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1340 | llvm::StringRef Value, |
| 1341 | llvm::SmallString<32> &Opt) const { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1342 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1343 | } |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1344 | }; |
| 1345 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1348 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1349 | Class &Hi) const { |
| 1350 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1351 | // |
| 1352 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1353 | // memory. |
| 1354 | // |
| 1355 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1356 | // memory. |
| 1357 | // |
| 1358 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1359 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1360 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1361 | // ABI working for processors that don't support the __m256 type. |
| 1362 | // |
| 1363 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1364 | // |
| 1365 | // Some of these are enforced by the merging logic. Others can arise |
| 1366 | // only with unions; for example: |
| 1367 | // union { _Complex double; unsigned; } |
| 1368 | // |
| 1369 | // Note that clauses (b) and (c) were added in 0.98. |
| 1370 | // |
| 1371 | if (Hi == Memory) |
| 1372 | Lo = Memory; |
| 1373 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1374 | Lo = Memory; |
| 1375 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1376 | Lo = Memory; |
| 1377 | if (Hi == SSEUp && Lo != SSE) |
| 1378 | Hi = SSE; |
| 1379 | } |
| 1380 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1381 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1382 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1383 | // classified recursively so that always two fields are |
| 1384 | // considered. The resulting class is calculated according to |
| 1385 | // the classes of the fields in the eightbyte: |
| 1386 | // |
| 1387 | // (a) If both classes are equal, this is the resulting class. |
| 1388 | // |
| 1389 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1390 | // the other class. |
| 1391 | // |
| 1392 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1393 | // class. |
| 1394 | // |
| 1395 | // (d) If one of the classes is INTEGER, the result is the |
| 1396 | // INTEGER. |
| 1397 | // |
| 1398 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1399 | // MEMORY is used as class. |
| 1400 | // |
| 1401 | // (f) Otherwise class SSE is used. |
| 1402 | |
| 1403 | // Accum should never be memory (we should have returned) or |
| 1404 | // ComplexX87 (because this cannot be passed in a structure). |
| 1405 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1406 | "Invalid accumulated classification during merge."); |
| 1407 | if (Accum == Field || Field == NoClass) |
| 1408 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1409 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1410 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1411 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1412 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1413 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1414 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1415 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1416 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1417 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1418 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1421 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1422 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1423 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1424 | // Class pairs with appropriate constructor methods for the various |
| 1425 | // situations. |
| 1426 | |
| 1427 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1428 | // shouldn't be passed in registers for example, so there is no chance they |
| 1429 | // can straddle an eightbyte. Verify & simplify. |
| 1430 | |
| 1431 | Lo = Hi = NoClass; |
| 1432 | |
| 1433 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1434 | Current = Memory; |
| 1435 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1436 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1437 | BuiltinType::Kind k = BT->getKind(); |
| 1438 | |
| 1439 | if (k == BuiltinType::Void) { |
| 1440 | Current = NoClass; |
| 1441 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1442 | Lo = Integer; |
| 1443 | Hi = Integer; |
| 1444 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1445 | Current = Integer; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1446 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1447 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 57b1da1 | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1448 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1449 | Current = SSE; |
| 1450 | } else if (k == BuiltinType::LongDouble) { |
| 1451 | Lo = X87; |
| 1452 | Hi = X87Up; |
| 1453 | } |
| 1454 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1455 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1456 | return; |
| 1457 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1459 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1460 | // Classify the underlying integer type. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1461 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1462 | return; |
| 1463 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1465 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1466 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1467 | return; |
| 1468 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1469 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1470 | if (Ty->isMemberPointerType()) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1471 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1472 | Lo = Hi = Integer; |
| 1473 | else |
| 1474 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1478 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1479 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1480 | if (Size == 32) { |
| 1481 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1482 | // float> as integer. |
| 1483 | Current = Integer; |
| 1484 | |
| 1485 | // If this type crosses an eightbyte boundary, it should be |
| 1486 | // split. |
| 1487 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1488 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1489 | if (EB_Real != EB_Imag) |
| 1490 | Hi = Lo; |
| 1491 | } else if (Size == 64) { |
| 1492 | // gcc passes <1 x double> in memory. :( |
| 1493 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1494 | return; |
| 1495 | |
| 1496 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 473f8e7 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1497 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 0fefa41 | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1498 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1499 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1500 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1501 | Current = Integer; |
| 1502 | else |
| 1503 | Current = SSE; |
| 1504 | |
| 1505 | // If this type crosses an eightbyte boundary, it should be |
| 1506 | // split. |
| 1507 | if (OffsetBase && OffsetBase != 64) |
| 1508 | Hi = Lo; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1509 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1510 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1511 | // least significant one belongs to class SSE and all the others to class |
| 1512 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1513 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1514 | // This design isn't correct for 256-bits, but since there're no cases |
| 1515 | // where the upper parts would need to be inspected, avoid adding |
| 1516 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1517 | // |
| 1518 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1519 | // registers if they are "named", i.e. not part of the "..." of a |
| 1520 | // variadic function. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1521 | Lo = SSE; |
| 1522 | Hi = SSEUp; |
| 1523 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1524 | return; |
| 1525 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1527 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1528 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1530 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1531 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1532 | if (Size <= 64) |
| 1533 | Current = Integer; |
| 1534 | else if (Size <= 128) |
| 1535 | Lo = Hi = Integer; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1536 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1537 | Current = SSE; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1538 | else if (ET == getContext().DoubleTy || |
| 1539 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 57b1da1 | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1540 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1541 | Lo = Hi = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1542 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1543 | Current = ComplexX87; |
| 1544 | |
| 1545 | // If this complex type crosses an eightbyte boundary then it |
| 1546 | // should be split. |
| 1547 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1548 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1549 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1550 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1555 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1556 | // Arrays are treated like structures. |
| 1557 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1558 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1559 | |
| 1560 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1561 | // than four eightbytes, ..., it has class MEMORY. |
| 1562 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | |
| 1565 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1566 | // fields, it has class MEMORY. |
| 1567 | // |
| 1568 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1569 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1570 | return; |
| 1571 | |
| 1572 | // Otherwise implement simplified merge. We could be smarter about |
| 1573 | // this, but it isn't worth it and would be harder to verify. |
| 1574 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1575 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1576 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 089d892 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1577 | |
| 1578 | // The only case a 256-bit wide vector could be used is when the array |
| 1579 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1580 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1581 | if (Size > 128 && EltSize != 256) |
| 1582 | return; |
| 1583 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1584 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1585 | Class FieldLo, FieldHi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1586 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1587 | Lo = merge(Lo, FieldLo); |
| 1588 | Hi = merge(Hi, FieldHi); |
| 1589 | if (Lo == Memory || Hi == Memory) |
| 1590 | break; |
| 1591 | } |
| 1592 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1593 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1594 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1595 | return; |
| 1596 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1598 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1599 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1600 | |
| 1601 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1602 | // than four eightbytes, ..., it has class MEMORY. |
| 1603 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1604 | return; |
| 1605 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1606 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1607 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1608 | // reference. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1609 | if (getRecordArgABI(RT, CGT)) |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1610 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1611 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1612 | const RecordDecl *RD = RT->getDecl(); |
| 1613 | |
| 1614 | // Assume variable sized types are passed in memory. |
| 1615 | if (RD->hasFlexibleArrayMember()) |
| 1616 | return; |
| 1617 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1618 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Reset Lo class, this will be recomputed. |
| 1621 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1622 | |
| 1623 | // If this is a C++ record, classify the bases first. |
| 1624 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1625 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1626 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1627 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1628 | "Unexpected base class!"); |
| 1629 | const CXXRecordDecl *Base = |
| 1630 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1631 | |
| 1632 | // Classify this field. |
| 1633 | // |
| 1634 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1635 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1636 | // initialized to class NO_CLASS. |
| 1637 | Class FieldLo, FieldHi; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1638 | uint64_t Offset = |
| 1639 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1640 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1641 | Lo = merge(Lo, FieldLo); |
| 1642 | Hi = merge(Hi, FieldHi); |
| 1643 | if (Lo == Memory || Hi == Memory) |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1649 | unsigned idx = 0; |
Bruno Cardoso Lopes | 548e478 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1650 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1651 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1652 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1653 | bool BitField = i->isBitField(); |
| 1654 | |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1655 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1656 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1657 | // |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1658 | // The only case a 256-bit wide vector could be used is when the struct |
| 1659 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1660 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1661 | // |
| 1662 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1663 | Lo = Memory; |
| 1664 | return; |
| 1665 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1666 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1667 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1668 | Lo = Memory; |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | // Classify this field. |
| 1673 | // |
| 1674 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1675 | // exceeds a single eightbyte, each is classified |
| 1676 | // separately. Each eightbyte gets initialized to class |
| 1677 | // NO_CLASS. |
| 1678 | Class FieldLo, FieldHi; |
| 1679 | |
| 1680 | // Bit-fields require special handling, they do not force the |
| 1681 | // structure to be passed in memory even if unaligned, and |
| 1682 | // therefore they can straddle an eightbyte. |
| 1683 | if (BitField) { |
| 1684 | // Ignore padding bit-fields. |
| 1685 | if (i->isUnnamedBitfield()) |
| 1686 | continue; |
| 1687 | |
| 1688 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1689 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1690 | |
| 1691 | uint64_t EB_Lo = Offset / 64; |
| 1692 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1693 | FieldLo = FieldHi = NoClass; |
| 1694 | if (EB_Lo) { |
| 1695 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1696 | FieldLo = NoClass; |
| 1697 | FieldHi = Integer; |
| 1698 | } else { |
| 1699 | FieldLo = Integer; |
| 1700 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1701 | } |
| 1702 | } else |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1703 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1704 | Lo = merge(Lo, FieldLo); |
| 1705 | Hi = merge(Hi, FieldHi); |
| 1706 | if (Lo == Memory || Hi == Memory) |
| 1707 | break; |
| 1708 | } |
| 1709 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1710 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1714 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1715 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1716 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1717 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1718 | // Treat an enum type as its underlying type. |
| 1719 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1720 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1721 | |
| 1722 | return (Ty->isPromotableIntegerType() ? |
| 1723 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1724 | } |
| 1725 | |
| 1726 | return ABIArgInfo::getIndirect(0); |
| 1727 | } |
| 1728 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1729 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1730 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1731 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1732 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1733 | if (Size <= 64 || Size > LargestVector) |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1740 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1741 | unsigned freeIntRegs) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1742 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1743 | // place naturally. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1744 | // |
| 1745 | // This assumption is optimistic, as there could be free registers available |
| 1746 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1747 | // the argument in the free register. This does not seem to happen currently, |
| 1748 | // but this code would be much safer if we could mark the argument with |
| 1749 | // 'onstack'. See PR12193. |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1750 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1751 | // Treat an enum type as its underlying type. |
| 1752 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1753 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1754 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1755 | return (Ty->isPromotableIntegerType() ? |
| 1756 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1757 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1758 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1759 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 1760 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1762 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1763 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1764 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1767 | // is important for good codegen. |
| 1768 | // |
| 1769 | // We do this by coercing the value into a scalar type which the backend can |
| 1770 | // handle naturally (i.e., without using byval). |
| 1771 | // |
| 1772 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1773 | // free integer registers. Doing this when there are free integer registers |
| 1774 | // would require more care, as we would have to ensure that the coerced value |
| 1775 | // did not claim the unused register. That would require either reording the |
| 1776 | // arguments to the function (so that any subsequent inreg values came first), |
| 1777 | // or only doing this optimization when there were no following arguments that |
| 1778 | // might be inreg. |
| 1779 | // |
| 1780 | // We currently expect it to be rare (particularly in well written code) for |
| 1781 | // arguments to be passed on the stack when there are still free integer |
| 1782 | // registers available (this would typically imply large structs being passed |
| 1783 | // by value), so this seems like a fair tradeoff for now. |
| 1784 | // |
| 1785 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1786 | // attributes. See PR12193. |
| 1787 | if (freeIntRegs == 0) { |
| 1788 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1789 | |
| 1790 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1791 | // type, which will end up on the stack (with alignment 8). |
| 1792 | if (Align == 8 && Size <= 64) |
| 1793 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1794 | Size)); |
| 1795 | } |
| 1796 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1797 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1800 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1801 | /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1802 | /// vector register. |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1803 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1804 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1805 | |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1806 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1807 | // strip them off if present. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1808 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1809 | while (STy && STy->getNumElements() == 1) { |
| 1810 | IRType = STy->getElementType(0); |
| 1811 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1812 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1813 | |
Bruno Cardoso Lopes | 528a8c7 | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1814 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1815 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1816 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1817 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | ce27567 | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1818 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1819 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1820 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1821 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1822 | EltTy->isIntegerTy(128))) |
| 1823 | return VT; |
| 1824 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1826 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1827 | } |
| 1828 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1829 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1830 | /// is known to either be off the end of the specified type or being in |
| 1831 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1832 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1833 | /// classification that put one of the two halves in the INTEGER class. |
| 1834 | /// |
| 1835 | /// It is conservatively correct to return false. |
| 1836 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1837 | unsigned EndBit, ASTContext &Context) { |
| 1838 | // If the bytes being queried are off the end of the type, there is no user |
| 1839 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1840 | // types that don't contain interesting padding. |
| 1841 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1842 | if (TySize <= StartBit) |
| 1843 | return true; |
| 1844 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1845 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1846 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1847 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1848 | |
| 1849 | // Check each element to see if the element overlaps with the queried range. |
| 1850 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1851 | // If the element is after the span we care about, then we're done.. |
| 1852 | unsigned EltOffset = i*EltSize; |
| 1853 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1855 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1856 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1857 | EndBit-EltOffset, Context)) |
| 1858 | return false; |
| 1859 | } |
| 1860 | // If it overlaps no elements, then it is safe to process as padding. |
| 1861 | return true; |
| 1862 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1864 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1865 | const RecordDecl *RD = RT->getDecl(); |
| 1866 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1868 | // If this is a C++ record, check the bases first. |
| 1869 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1870 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1871 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1872 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1873 | "Unexpected base class!"); |
| 1874 | const CXXRecordDecl *Base = |
| 1875 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1877 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1878 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1879 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1881 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1882 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1883 | EndBit-BaseOffset, Context)) |
| 1884 | return false; |
| 1885 | } |
| 1886 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1888 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1889 | // this could be sped up a lot by being smarter about queried fields, |
| 1890 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1891 | // much. |
| 1892 | unsigned idx = 0; |
| 1893 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1894 | i != e; ++i, ++idx) { |
| 1895 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1897 | // If we found a field after the region we care about, then we're done. |
| 1898 | if (FieldOffset >= EndBit) break; |
| 1899 | |
| 1900 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1901 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1902 | Context)) |
| 1903 | return false; |
| 1904 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1906 | // If nothing in this record overlapped the area of interest, then we're |
| 1907 | // clean. |
| 1908 | return true; |
| 1909 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1910 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1911 | return false; |
| 1912 | } |
| 1913 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1914 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1915 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1916 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1917 | /// false. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1918 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1919 | const llvm::DataLayout &TD) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1920 | // Base case if we find a float. |
| 1921 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1922 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1924 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1925 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1926 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1927 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1928 | IROffset -= SL->getElementOffset(Elt); |
| 1929 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1930 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1932 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1933 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1934 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1935 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1936 | IROffset -= IROffset/EltSize*EltSize; |
| 1937 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1938 | } |
| 1939 | |
| 1940 | return false; |
| 1941 | } |
| 1942 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1943 | |
| 1944 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1945 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1946 | llvm::Type *X86_64ABIInfo:: |
| 1947 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1948 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1949 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1950 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1951 | // structs that contain 3 floats. |
| 1952 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1953 | SourceOffset*8+64, getContext())) |
| 1954 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1956 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1957 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1958 | // case. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1959 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 1960 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1961 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1963 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1964 | } |
| 1965 | |
| 1966 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1967 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1968 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1969 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1970 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1971 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1972 | /// etc). |
| 1973 | /// |
| 1974 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1975 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1976 | /// the 8-byte value references. PrefType may be null. |
| 1977 | /// |
| 1978 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1979 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1980 | /// |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1981 | llvm::Type *X86_64ABIInfo:: |
| 1982 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1983 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1984 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1985 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1986 | if (IROffset == 0) { |
| 1987 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1988 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 1989 | IRType->isIntegerTy(64)) |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1990 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1992 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1993 | // goodness in the source type is just tail padding. This is allowed to |
| 1994 | // kick in for struct {double,int} on the int, but not on |
| 1995 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1996 | // have to do this analysis on the source type because we can't depend on |
| 1997 | // unions being lowered a specific way etc. |
| 1998 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1999 | IRType->isIntegerTy(32) || |
| 2000 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2001 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2002 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2004 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2005 | SourceOffset*8+64, getContext())) |
| 2006 | return IRType; |
| 2007 | } |
| 2008 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2010 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2011 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2012 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2013 | if (IROffset < SL->getSizeInBytes()) { |
| 2014 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2015 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2017 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2018 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2019 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2020 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2022 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2023 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2024 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2025 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2026 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2027 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2028 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2029 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2030 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2031 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2032 | unsigned TySizeInBytes = |
| 2033 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2035 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2036 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2037 | // It is always safe to classify this as an integer type up to i64 that |
| 2038 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2039 | return llvm::IntegerType::get(getVMContext(), |
| 2040 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2043 | |
| 2044 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2045 | /// be used as elements of a two register pair to pass or return, return a |
| 2046 | /// first class aggregate to represent them. For example, if the low part of |
| 2047 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2048 | /// return {i32*, float}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2049 | static llvm::Type * |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2050 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2051 | const llvm::DataLayout &TD) { |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2052 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2053 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2054 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2055 | // the second element at offset 8. Check for this: |
| 2056 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2057 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2058 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2059 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2061 | // To handle this, we have to increase the size of the low part so that the |
| 2062 | // second element will start at an 8 byte offset. We can't increase the size |
| 2063 | // of the second element because it might make us access off the end of the |
| 2064 | // struct. |
| 2065 | if (HiStart != 8) { |
| 2066 | // There are only two sorts of types the ABI generation code can produce for |
| 2067 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2068 | // Promote these to a larger type. |
| 2069 | if (Lo->isFloatTy()) |
| 2070 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2071 | else { |
| 2072 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2073 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2074 | } |
| 2075 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2076 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2077 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2078 | |
| 2079 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2080 | // Verify that the second element is at an 8-byte offset. |
| 2081 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2082 | "Invalid x86-64 argument pair!"); |
| 2083 | return Result; |
| 2084 | } |
| 2085 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2086 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2087 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2088 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2089 | // classification algorithm. |
| 2090 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2091 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2092 | |
| 2093 | // Check some invariants. |
| 2094 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2095 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2096 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2097 | llvm::Type *ResType = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2098 | switch (Lo) { |
| 2099 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2100 | if (Hi == NoClass) |
| 2101 | return ABIArgInfo::getIgnore(); |
| 2102 | // If the low part is just padding, it takes no register, leave ResType |
| 2103 | // null. |
| 2104 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2105 | "Unknown missing lo part"); |
| 2106 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2107 | |
| 2108 | case SSEUp: |
| 2109 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2110 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2111 | |
| 2112 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2113 | // hidden argument. |
| 2114 | case Memory: |
| 2115 | return getIndirectReturnResult(RetTy); |
| 2116 | |
| 2117 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2118 | // available register of the sequence %rax, %rdx is used. |
| 2119 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2120 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2122 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2123 | // so that the parameter gets the right LLVM IR attributes. |
| 2124 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2125 | // Treat an enum type as its underlying type. |
| 2126 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2127 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2129 | if (RetTy->isIntegralOrEnumerationType() && |
| 2130 | RetTy->isPromotableIntegerType()) |
| 2131 | return ABIArgInfo::getExtend(); |
| 2132 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2133 | break; |
| 2134 | |
| 2135 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2136 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2137 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2138 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2139 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2140 | |
| 2141 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2142 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2143 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2144 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2145 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2146 | |
| 2147 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2148 | // part of the value is returned in %st0 and the imaginary part in |
| 2149 | // %st1. |
| 2150 | case ComplexX87: |
| 2151 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2152 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2153 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2154 | NULL); |
| 2155 | break; |
| 2156 | } |
| 2157 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2158 | llvm::Type *HighPart = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2159 | switch (Hi) { |
| 2160 | // Memory was handled previously and X87 should |
| 2161 | // never occur as a hi class. |
| 2162 | case Memory: |
| 2163 | case X87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2164 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2165 | |
| 2166 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2167 | case NoClass: |
| 2168 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2169 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2170 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2171 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2172 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2173 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2174 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2175 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2176 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2177 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2178 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2179 | break; |
| 2180 | |
| 2181 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2182 | // is passed in the next available eightbyte chunk if the last used |
| 2183 | // vector register. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2184 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2185 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2186 | case SSEUp: |
| 2187 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2188 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | |
| 2191 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2192 | // returned together with the previous X87 value in %st0. |
| 2193 | case X87Up: |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2194 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2195 | // anything. However, in some cases with unions it may not be |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2196 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2197 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2198 | if (Lo != X87) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2199 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2200 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2201 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2202 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2206 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2207 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2208 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2209 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2210 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2211 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2212 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2215 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2216 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2217 | bool isNamedArg) |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2218 | const |
| 2219 | { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2220 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2221 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2222 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2223 | // Check some invariants. |
| 2224 | // FIXME: Enforce these by construction. |
| 2225 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2226 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2227 | |
| 2228 | neededInt = 0; |
| 2229 | neededSSE = 0; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2230 | llvm::Type *ResType = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2231 | switch (Lo) { |
| 2232 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2233 | if (Hi == NoClass) |
| 2234 | return ABIArgInfo::getIgnore(); |
| 2235 | // If the low part is just padding, it takes no register, leave ResType |
| 2236 | // null. |
| 2237 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2238 | "Unknown missing lo part"); |
| 2239 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2240 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2241 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2242 | // on the stack. |
| 2243 | case Memory: |
| 2244 | |
| 2245 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2246 | // COMPLEX_X87, it is passed in memory. |
| 2247 | case X87: |
| 2248 | case ComplexX87: |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2249 | if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect) |
Eli Friedman | ded137f | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2250 | ++neededInt; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2251 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2252 | |
| 2253 | case SSEUp: |
| 2254 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2255 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2256 | |
| 2257 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2258 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2259 | // and %r9 is used. |
| 2260 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2261 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2262 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2263 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2264 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2265 | |
| 2266 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2267 | // so that the parameter gets the right LLVM IR attributes. |
| 2268 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2269 | // Treat an enum type as its underlying type. |
| 2270 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2271 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2272 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2273 | if (Ty->isIntegralOrEnumerationType() && |
| 2274 | Ty->isPromotableIntegerType()) |
| 2275 | return ABIArgInfo::getExtend(); |
| 2276 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2277 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2278 | break; |
| 2279 | |
| 2280 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2281 | // available SSE register is used, the registers are taken in the |
| 2282 | // order from %xmm0 to %xmm7. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2283 | case SSE: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2284 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 14508ff | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2285 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2286 | ++neededSSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2287 | break; |
| 2288 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2289 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2291 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2292 | switch (Hi) { |
| 2293 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2294 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2295 | // which is passed in memory. |
| 2296 | case Memory: |
| 2297 | case X87: |
| 2298 | case ComplexX87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2299 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2300 | |
| 2301 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2303 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2304 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2305 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2306 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2308 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2309 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | |
| 2312 | // X87Up generally doesn't occur here (long double is passed in |
| 2313 | // memory), except in situations involving unions. |
| 2314 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2315 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2316 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2317 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2318 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2319 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2320 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2321 | ++neededSSE; |
| 2322 | break; |
| 2323 | |
| 2324 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2325 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2326 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2327 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2328 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2329 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | } |
| 2332 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2333 | // If a high part was specified, merge it together with the low part. It is |
| 2334 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2335 | // first class struct aggregate with the high and low part: {low, high} |
| 2336 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2337 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2339 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2342 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2344 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2345 | |
| 2346 | // Keep track of the number of assigned registers. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2347 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2348 | |
| 2349 | // If the return value is indirect, then the hidden argument is consuming one |
| 2350 | // integer register. |
| 2351 | if (FI.getReturnInfo().isIndirect()) |
| 2352 | --freeIntRegs; |
| 2353 | |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2354 | bool isVariadic = FI.isVariadic(); |
| 2355 | unsigned numRequiredArgs = 0; |
| 2356 | if (isVariadic) |
| 2357 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2358 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2359 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2360 | // get assigned (in left-to-right order) for passing as follows... |
| 2361 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2362 | it != ie; ++it) { |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2363 | bool isNamedArg = true; |
| 2364 | if (isVariadic) |
Aaron Ballman | eba7d2f | 2013-06-12 15:03:45 +0000 | [diff] [blame] | 2365 | isNamedArg = (it - FI.arg_begin()) < |
| 2366 | static_cast<signed>(numRequiredArgs); |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2367 | |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2368 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2369 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2370 | neededSSE, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2371 | |
| 2372 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2373 | // eightbyte of an argument, the whole argument is passed on the |
| 2374 | // stack. If registers have already been assigned for some |
| 2375 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2376 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2377 | freeIntRegs -= neededInt; |
| 2378 | freeSSERegs -= neededSSE; |
| 2379 | } else { |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2380 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2386 | QualType Ty, |
| 2387 | CodeGenFunction &CGF) { |
| 2388 | llvm::Value *overflow_arg_area_p = |
| 2389 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2390 | llvm::Value *overflow_arg_area = |
| 2391 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2392 | |
| 2393 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2394 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2395 | // It isn't stated explicitly in the standard, but in practice we use |
| 2396 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2397 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2398 | if (Align > 8) { |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2399 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2400 | llvm::Value *Offset = |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2401 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2402 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2403 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2404 | CGF.Int64Ty); |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2405 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2406 | overflow_arg_area = |
| 2407 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2408 | overflow_arg_area->getType(), |
| 2409 | "overflow_arg_area.align"); |
| 2410 | } |
| 2411 | |
| 2412 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2413 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | llvm::Value *Res = |
| 2415 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2416 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2417 | |
| 2418 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2419 | // l->overflow_arg_area + sizeof(type). |
| 2420 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2421 | // an 8 byte boundary. |
| 2422 | |
| 2423 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2424 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2425 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2426 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2427 | "overflow_arg_area.next"); |
| 2428 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2429 | |
| 2430 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2431 | return Res; |
| 2432 | } |
| 2433 | |
| 2434 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2435 | CodeGenFunction &CGF) const { |
| 2436 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2437 | // struct { |
| 2438 | // i32 gp_offset; |
| 2439 | // i32 fp_offset; |
| 2440 | // i8* overflow_arg_area; |
| 2441 | // i8* reg_save_area; |
| 2442 | // }; |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2443 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2445 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2446 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2447 | /*isNamedArg*/false); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2448 | |
| 2449 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2450 | // in the registers. If not go to step 7. |
| 2451 | if (!neededInt && !neededSSE) |
| 2452 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2453 | |
| 2454 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2455 | // general purpose registers needed to pass type and num_fp to hold |
| 2456 | // the number of floating point registers needed. |
| 2457 | |
| 2458 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2459 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2460 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2461 | // |
| 2462 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2463 | // register save space). |
| 2464 | |
| 2465 | llvm::Value *InRegs = 0; |
| 2466 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2467 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2468 | if (neededInt) { |
| 2469 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2470 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2471 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2472 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | if (neededSSE) { |
| 2476 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2477 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2478 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2479 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2480 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2482 | } |
| 2483 | |
| 2484 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2485 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2486 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2487 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2488 | |
| 2489 | // Emit code to load the value if it was passed in registers. |
| 2490 | |
| 2491 | CGF.EmitBlock(InRegBlock); |
| 2492 | |
| 2493 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2494 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2495 | // copying to a temporary location in case the parameter is passed |
| 2496 | // in different register classes or requires an alignment greater |
| 2497 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2498 | // |
| 2499 | // FIXME: This really results in shameful code when we end up needing to |
| 2500 | // collect arguments from different places; often what should result in a |
| 2501 | // simple assembling of a structure from scattered addresses has many more |
| 2502 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2503 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2504 | llvm::Value *RegAddr = |
| 2505 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2506 | "reg_save_area"); |
| 2507 | if (neededInt && neededSSE) { |
| 2508 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2509 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2510 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2511 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2512 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2513 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2514 | llvm::Type *TyLo = ST->getElementType(0); |
| 2515 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2516 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2518 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2519 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2520 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2521 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2522 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2523 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2524 | llvm::Value *V = |
| 2525 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2526 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2527 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2528 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2529 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2530 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2531 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2532 | } else if (neededInt) { |
| 2533 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2534 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2535 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2536 | |
| 2537 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2538 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2539 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2540 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2541 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2542 | if (TyAlign > 8) { |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2543 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2544 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2545 | RegAddr = Tmp; |
| 2546 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2547 | } else if (neededSSE == 1) { |
| 2548 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2549 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2550 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2551 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2552 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2553 | // SSE registers are spaced 16 bytes apart in the register save |
| 2554 | // area, we need to collect the two eightbytes together. |
| 2555 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2556 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2557 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2558 | llvm::Type *DblPtrTy = |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2559 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2560 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2561 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2562 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2563 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2564 | DblPtrTy)); |
| 2565 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2566 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2567 | DblPtrTy)); |
| 2568 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2569 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2570 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2574 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2575 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2576 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2577 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2578 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2579 | gp_offset_p); |
| 2580 | } |
| 2581 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2582 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2583 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2584 | fp_offset_p); |
| 2585 | } |
| 2586 | CGF.EmitBranch(ContBlock); |
| 2587 | |
| 2588 | // Emit code to load the value if it was passed in memory. |
| 2589 | |
| 2590 | CGF.EmitBlock(InMemBlock); |
| 2591 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2592 | |
| 2593 | // Return the appropriate result. |
| 2594 | |
| 2595 | CGF.EmitBlock(ContBlock); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2596 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | "vaarg.addr"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2598 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2599 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2600 | return ResAddr; |
| 2601 | } |
| 2602 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2603 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (Ty->isVoidType()) |
| 2606 | return ABIArgInfo::getIgnore(); |
| 2607 | |
| 2608 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2609 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2610 | |
| 2611 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2612 | |
| 2613 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2614 | if (IsReturnType) { |
| 2615 | if (isRecordReturnIndirect(RT, CGT)) |
| 2616 | return ABIArgInfo::getIndirect(0, false); |
| 2617 | } else { |
| 2618 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) |
| 2619 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2620 | } |
| 2621 | |
| 2622 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2623 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2624 | |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2625 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2626 | if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32) |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2627 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2628 | Size)); |
| 2629 | |
| 2630 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2631 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2632 | if (Size <= 64 && |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2633 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2634 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2635 | Size)); |
| 2636 | |
| 2637 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2638 | } |
| 2639 | |
| 2640 | if (Ty->isPromotableIntegerType()) |
| 2641 | return ABIArgInfo::getExtend(); |
| 2642 | |
| 2643 | return ABIArgInfo::getDirect(); |
| 2644 | } |
| 2645 | |
| 2646 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2647 | |
| 2648 | QualType RetTy = FI.getReturnType(); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2649 | FI.getReturnInfo() = classify(RetTy, true); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2650 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2651 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2652 | it != ie; ++it) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2653 | it->info = classify(it->type, false); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2656 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2657 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2658 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2660 | CGBuilderTy &Builder = CGF.Builder; |
| 2661 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2662 | "ap"); |
| 2663 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2664 | llvm::Type *PTy = |
| 2665 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2666 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2667 | |
| 2668 | uint64_t Offset = |
| 2669 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2670 | llvm::Value *NextAddr = |
| 2671 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2672 | "ap.next"); |
| 2673 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2674 | |
| 2675 | return AddrTyped; |
| 2676 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2677 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2678 | namespace { |
| 2679 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2680 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2681 | public: |
| 2682 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2683 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
| 2684 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2685 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2686 | CodeGenFunction &CGF) const; |
| 2687 | private: |
| 2688 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2689 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2690 | }; |
| 2691 | |
| 2692 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2693 | public: |
| 2694 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2695 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2696 | }; |
| 2697 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2700 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2701 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2702 | PInfo.computeInfo(FI); |
| 2703 | else |
| 2704 | NInfo.computeInfo(FI); |
| 2705 | } |
| 2706 | |
| 2707 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2708 | CodeGenFunction &CGF) const { |
| 2709 | // Always use the native convention; calling pnacl-style varargs functions |
| 2710 | // is unuspported. |
| 2711 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2712 | } |
| 2713 | |
| 2714 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2715 | // PowerPC-32 |
| 2716 | |
| 2717 | namespace { |
| 2718 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2719 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2720 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2721 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2722 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2723 | // This is recovered from gcc output. |
| 2724 | return 1; // r1 is the dedicated stack pointer |
| 2725 | } |
| 2726 | |
| 2727 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2728 | llvm::Value *Address) const; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2729 | }; |
| 2730 | |
| 2731 | } |
| 2732 | |
| 2733 | bool |
| 2734 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2735 | llvm::Value *Address) const { |
| 2736 | // This is calculated from the LLVM and GCC tables and verified |
| 2737 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2738 | |
| 2739 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2741 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2742 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2743 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2744 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2745 | |
| 2746 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2747 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2748 | |
| 2749 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2750 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2751 | |
| 2752 | // 64-76 are various 4-byte special-purpose registers: |
| 2753 | // 64: mq |
| 2754 | // 65: lr |
| 2755 | // 66: ctr |
| 2756 | // 67: ap |
| 2757 | // 68-75 cr0-7 |
| 2758 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2759 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2760 | |
| 2761 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2762 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2763 | |
| 2764 | // 109: vrsave |
| 2765 | // 110: vscr |
| 2766 | // 111: spe_acc |
| 2767 | // 112: spefscr |
| 2768 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2769 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2770 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2771 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2774 | // PowerPC-64 |
| 2775 | |
| 2776 | namespace { |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2777 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2778 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2779 | |
| 2780 | public: |
| 2781 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2782 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2783 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2784 | |
| 2785 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2786 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2787 | |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2788 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2789 | // Example: For aggregate arguments that fit in a register, we could |
| 2790 | // use getDirectInReg (as is done below for structs containing a single |
| 2791 | // floating-point value) to avoid pushing them to memory on function |
| 2792 | // entry. This would require changing the logic in PPCISelLowering |
| 2793 | // when lowering the parameters in the caller and args in the callee. |
| 2794 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 2795 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2796 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2797 | it != ie; ++it) { |
| 2798 | // We rely on the default argument classification for the most part. |
| 2799 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | b199310 | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2800 | // or vector item must be passed in a register if one is available. |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2801 | const Type *T = isSingleElementStruct(it->type, getContext()); |
| 2802 | if (T) { |
| 2803 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Bill Schmidt | b199310 | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2804 | if (T->isVectorType() || (BT && BT->isFloatingPoint())) { |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2805 | QualType QT(T, 0); |
| 2806 | it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
| 2807 | continue; |
| 2808 | } |
| 2809 | } |
| 2810 | it->info = classifyArgumentType(it->type); |
| 2811 | } |
| 2812 | } |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2813 | |
| 2814 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, |
| 2815 | QualType Ty, |
| 2816 | CodeGenFunction &CGF) const; |
| 2817 | }; |
| 2818 | |
| 2819 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2820 | public: |
| 2821 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2822 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2823 | |
| 2824 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2825 | // This is recovered from gcc output. |
| 2826 | return 1; // r1 is the dedicated stack pointer |
| 2827 | } |
| 2828 | |
| 2829 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2830 | llvm::Value *Address) const; |
| 2831 | }; |
| 2832 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2833 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2834 | public: |
| 2835 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2836 | |
| 2837 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2838 | // This is recovered from gcc output. |
| 2839 | return 1; // r1 is the dedicated stack pointer |
| 2840 | } |
| 2841 | |
| 2842 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2843 | llvm::Value *Address) const; |
| 2844 | }; |
| 2845 | |
| 2846 | } |
| 2847 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2848 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2849 | // extended to 64 bits. |
| 2850 | bool |
| 2851 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2852 | // Treat an enum type as its underlying type. |
| 2853 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2854 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2855 | |
| 2856 | // Promotable integer types are required to be promoted by the ABI. |
| 2857 | if (Ty->isPromotableIntegerType()) |
| 2858 | return true; |
| 2859 | |
| 2860 | // In addition to the usual promotable integer types, we also need to |
| 2861 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2862 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2863 | switch (BT->getKind()) { |
| 2864 | case BuiltinType::Int: |
| 2865 | case BuiltinType::UInt: |
| 2866 | return true; |
| 2867 | default: |
| 2868 | break; |
| 2869 | } |
| 2870 | |
| 2871 | return false; |
| 2872 | } |
| 2873 | |
| 2874 | ABIArgInfo |
| 2875 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | c9715fc | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 2876 | if (Ty->isAnyComplexType()) |
| 2877 | return ABIArgInfo::getDirect(); |
| 2878 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2879 | if (isAggregateTypeForABI(Ty)) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2880 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 2881 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2882 | |
| 2883 | return ABIArgInfo::getIndirect(0); |
| 2884 | } |
| 2885 | |
| 2886 | return (isPromotableTypeForABI(Ty) ? |
| 2887 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2888 | } |
| 2889 | |
| 2890 | ABIArgInfo |
| 2891 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 2892 | if (RetTy->isVoidType()) |
| 2893 | return ABIArgInfo::getIgnore(); |
| 2894 | |
Bill Schmidt | 9e6111a | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 2895 | if (RetTy->isAnyComplexType()) |
| 2896 | return ABIArgInfo::getDirect(); |
| 2897 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2898 | if (isAggregateTypeForABI(RetTy)) |
| 2899 | return ABIArgInfo::getIndirect(0); |
| 2900 | |
| 2901 | return (isPromotableTypeForABI(RetTy) ? |
| 2902 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2903 | } |
| 2904 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2905 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 2906 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 2907 | QualType Ty, |
| 2908 | CodeGenFunction &CGF) const { |
| 2909 | llvm::Type *BP = CGF.Int8PtrTy; |
| 2910 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 2911 | |
| 2912 | CGBuilderTy &Builder = CGF.Builder; |
| 2913 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 2914 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2915 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2916 | // Update the va_list pointer. The pointer should be bumped by the |
| 2917 | // size of the object. We can trust getTypeSize() except for a complex |
| 2918 | // type whose base type is smaller than a doubleword. For these, the |
| 2919 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2920 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2921 | QualType BaseTy; |
| 2922 | unsigned CplxBaseSize = 0; |
| 2923 | |
| 2924 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 2925 | BaseTy = CTy->getElementType(); |
| 2926 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 2927 | if (CplxBaseSize < 8) |
| 2928 | SizeInBytes = 16; |
| 2929 | } |
| 2930 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2931 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 2932 | llvm::Value *NextAddr = |
| 2933 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 2934 | "ap.next"); |
| 2935 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2936 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2937 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 2938 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 2939 | // in separate doublewords. However, Clang expects us to produce a |
| 2940 | // pointer to a structure with the two parts packed tightly. So generate |
| 2941 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 2942 | // and store them to a temporary structure. |
| 2943 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 2944 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2945 | llvm::Value *ImagAddr = RealAddr; |
| 2946 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 2947 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 2948 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 2949 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 2950 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 2951 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 2952 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 2953 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 2954 | "vacplx"); |
| 2955 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 2956 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 2957 | Builder.CreateStore(Real, RealPtr, false); |
| 2958 | Builder.CreateStore(Imag, ImagPtr, false); |
| 2959 | return Ptr; |
| 2960 | } |
| 2961 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2962 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 2963 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 2964 | // correct offset. |
| 2965 | if (SizeInBytes < 8) { |
| 2966 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2967 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 2968 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 2969 | } |
| 2970 | |
| 2971 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2972 | return Builder.CreateBitCast(Addr, PTy); |
| 2973 | } |
| 2974 | |
| 2975 | static bool |
| 2976 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2977 | llvm::Value *Address) { |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2978 | // This is calculated from the LLVM and GCC tables and verified |
| 2979 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2980 | |
| 2981 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2982 | |
| 2983 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 2984 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2985 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2986 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2987 | |
| 2988 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 2989 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 2990 | |
| 2991 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 2992 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 2993 | |
| 2994 | // 64-76 are various 4-byte special-purpose registers: |
| 2995 | // 64: mq |
| 2996 | // 65: lr |
| 2997 | // 66: ctr |
| 2998 | // 67: ap |
| 2999 | // 68-75 cr0-7 |
| 3000 | // 76: xer |
| 3001 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3002 | |
| 3003 | // 77-108: v0-31, the 16-byte vector registers |
| 3004 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3005 | |
| 3006 | // 109: vrsave |
| 3007 | // 110: vscr |
| 3008 | // 111: spe_acc |
| 3009 | // 112: spefscr |
| 3010 | // 113: sfp |
| 3011 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3012 | |
| 3013 | return false; |
| 3014 | } |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3015 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3016 | bool |
| 3017 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3018 | CodeGen::CodeGenFunction &CGF, |
| 3019 | llvm::Value *Address) const { |
| 3020 | |
| 3021 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3022 | } |
| 3023 | |
| 3024 | bool |
| 3025 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3026 | llvm::Value *Address) const { |
| 3027 | |
| 3028 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3029 | } |
| 3030 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3031 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3032 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3033 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3034 | |
| 3035 | namespace { |
| 3036 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3037 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3038 | public: |
| 3039 | enum ABIKind { |
| 3040 | APCS = 0, |
| 3041 | AAPCS = 1, |
| 3042 | AAPCS_VFP |
| 3043 | }; |
| 3044 | |
| 3045 | private: |
| 3046 | ABIKind Kind; |
| 3047 | |
| 3048 | public: |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3049 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
| 3050 | setRuntimeCC(); |
| 3051 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3052 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3053 | bool isEABI() const { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 3054 | StringRef Env = getTarget().getTriple().getEnvironmentName(); |
Logan Chien | 94a7142 | 2012-09-02 09:30:11 +0000 | [diff] [blame] | 3055 | return (Env == "gnueabi" || Env == "eabi" || |
| 3056 | Env == "android" || Env == "androideabi"); |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3059 | ABIKind getABIKind() const { return Kind; } |
| 3060 | |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3061 | private: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3062 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3063 | ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs, |
| 3064 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3065 | bool &IsHA) const; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3066 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3067 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3068 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3069 | |
| 3070 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3071 | CodeGenFunction &CGF) const; |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3072 | |
| 3073 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 3074 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 3075 | void setRuntimeCC(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3076 | }; |
| 3077 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3078 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3079 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3080 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 3081 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3082 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3083 | const ARMABIInfo &getABIInfo() const { |
| 3084 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3085 | } |
| 3086 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3087 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3088 | return 13; |
| 3089 | } |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3090 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3091 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3092 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3093 | } |
| 3094 | |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3095 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3096 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3097 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3098 | |
| 3099 | // 0-15 are the 16 integer registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3100 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3101 | return false; |
| 3102 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3103 | |
| 3104 | unsigned getSizeOfUnwindException() const { |
| 3105 | if (getABIInfo().isEABI()) return 88; |
| 3106 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3107 | } |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3108 | |
| 3109 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 3110 | CodeGen::CodeGenModule &CGM) const { |
| 3111 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3112 | if (!FD) |
| 3113 | return; |
| 3114 | |
| 3115 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 3116 | if (!Attr) |
| 3117 | return; |
| 3118 | |
| 3119 | const char *Kind; |
| 3120 | switch (Attr->getInterrupt()) { |
| 3121 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 3122 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 3123 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 3124 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 3125 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 3126 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 3127 | } |
| 3128 | |
| 3129 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 3130 | |
| 3131 | Fn->addFnAttr("interrupt", Kind); |
| 3132 | |
| 3133 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 3134 | return; |
| 3135 | |
| 3136 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 3137 | // however this is not necessarily true on taking any interrupt. Instruct |
| 3138 | // the backend to perform a realignment as part of the function prologue. |
| 3139 | llvm::AttrBuilder B; |
| 3140 | B.addStackAlignmentAttr(8); |
| 3141 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 3142 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 3143 | llvm::AttributeSet::FunctionIndex, |
| 3144 | B)); |
| 3145 | } |
| 3146 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3147 | }; |
| 3148 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3151 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3152 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3153 | // VFP registers allocated so far. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3154 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3155 | // VFP registers of the appropriate type unallocated then the argument is |
| 3156 | // allocated to the lowest-numbered sequence of such registers. |
| 3157 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3158 | // unallocated are marked as unavailable. |
| 3159 | unsigned AllocatedVFP = 0; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3160 | int VFPRegs[16] = { 0 }; |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3161 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3162 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3163 | it != ie; ++it) { |
| 3164 | unsigned PreAllocation = AllocatedVFP; |
| 3165 | bool IsHA = false; |
| 3166 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3167 | // s0-s15 (d0-d7) for passing arguments. |
| 3168 | const unsigned NumVFPs = 16; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3169 | it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3170 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3171 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3172 | // padding of (NumVFPs - PreAllocation) floats. |
| 3173 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3174 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3175 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
| 3176 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3177 | } |
| 3178 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3179 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3180 | // Always honor user-specified calling convention. |
| 3181 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3182 | return; |
| 3183 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3184 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3185 | if (cc != llvm::CallingConv::C) |
| 3186 | FI.setEffectiveCallingConvention(cc); |
| 3187 | } |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3188 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3189 | /// Return the default calling convention that LLVM will use. |
| 3190 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3191 | // The default calling convention that LLVM will infer. |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 3192 | if (getTarget().getTriple().getEnvironmentName()=="gnueabihf") |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3193 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3194 | else if (isEABI()) |
| 3195 | return llvm::CallingConv::ARM_AAPCS; |
| 3196 | else |
| 3197 | return llvm::CallingConv::ARM_APCS; |
| 3198 | } |
| 3199 | |
| 3200 | /// Return the calling convention that our ABI would like us to use |
| 3201 | /// as the C calling convention. |
| 3202 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3203 | switch (getABIKind()) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3204 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3205 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3206 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3207 | } |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3208 | llvm_unreachable("bad ABI kind"); |
| 3209 | } |
| 3210 | |
| 3211 | void ARMABIInfo::setRuntimeCC() { |
| 3212 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3213 | |
| 3214 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3215 | // they'd just match what LLVM will infer from the triple. |
| 3216 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3217 | if (abiCC != getLLVMDefaultCC()) |
| 3218 | RuntimeCC = abiCC; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3221 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3222 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3223 | /// contained in the type is returned through it; this is used for the |
| 3224 | /// recursive calls that check aggregate component types. |
| 3225 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3226 | ASTContext &Context, |
| 3227 | uint64_t *HAMembers = 0) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3228 | uint64_t Members = 0; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3229 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3230 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3231 | return false; |
| 3232 | Members *= AT->getSize().getZExtValue(); |
| 3233 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3234 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3235 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3236 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3237 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3238 | Members = 0; |
| 3239 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3240 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3241 | const FieldDecl *FD = *i; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3242 | uint64_t FldMembers; |
| 3243 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3244 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3245 | |
| 3246 | Members = (RD->isUnion() ? |
| 3247 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3248 | } |
| 3249 | } else { |
| 3250 | Members = 1; |
| 3251 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3252 | Members = 2; |
| 3253 | Ty = CT->getElementType(); |
| 3254 | } |
| 3255 | |
| 3256 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 3257 | // double, or 64-bit or 128-bit vectors. |
| 3258 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3259 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | adfa45f | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 3260 | BT->getKind() != BuiltinType::Double && |
| 3261 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3262 | return false; |
| 3263 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3264 | unsigned VecSize = Context.getTypeSize(VT); |
| 3265 | if (VecSize != 64 && VecSize != 128) |
| 3266 | return false; |
| 3267 | } else { |
| 3268 | return false; |
| 3269 | } |
| 3270 | |
| 3271 | // The base type must be the same for all members. Vector types of the |
| 3272 | // same total size are treated as being equivalent here. |
| 3273 | const Type *TyPtr = Ty.getTypePtr(); |
| 3274 | if (!Base) |
| 3275 | Base = TyPtr; |
| 3276 | if (Base != TyPtr && |
| 3277 | (!Base->isVectorType() || !TyPtr->isVectorType() || |
| 3278 | Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) |
| 3279 | return false; |
| 3280 | } |
| 3281 | |
| 3282 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 3283 | if (HAMembers) |
| 3284 | *HAMembers = Members; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3285 | |
| 3286 | return (Members > 0 && Members <= 4); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3289 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 3290 | /// number of VFP registers (unit is S register) requested. |
| 3291 | static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP, |
| 3292 | unsigned Alignment, |
| 3293 | unsigned NumRequired) { |
| 3294 | // Early Exit. |
| 3295 | if (AllocatedVFP >= 16) |
| 3296 | return; |
| 3297 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3298 | // VFP registers of the appropriate type unallocated then the argument is |
| 3299 | // allocated to the lowest-numbered sequence of such registers. |
| 3300 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 3301 | bool FoundSlot = true; |
| 3302 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3303 | if (J >= 16 || VFPRegs[J]) { |
| 3304 | FoundSlot = false; |
| 3305 | break; |
| 3306 | } |
| 3307 | if (FoundSlot) { |
| 3308 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3309 | VFPRegs[J] = 1; |
| 3310 | AllocatedVFP += NumRequired; |
| 3311 | return; |
| 3312 | } |
| 3313 | } |
| 3314 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3315 | // unallocated are marked as unavailable. |
| 3316 | for (unsigned I = 0; I < 16; I++) |
| 3317 | VFPRegs[I] = 1; |
| 3318 | AllocatedVFP = 17; // We do not have enough VFP registers. |
| 3319 | } |
| 3320 | |
| 3321 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs, |
| 3322 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3323 | bool &IsHA) const { |
| 3324 | // We update number of allocated VFPs according to |
| 3325 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 3326 | // A single-precision floating-point type (including promoted |
| 3327 | // half-precision types); A double-precision floating-point type; |
| 3328 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 3329 | // with a Base Type of a single- or double-precision floating-point type, |
| 3330 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 3331 | // to four Elements. |
| 3332 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3333 | // Handle illegal vector types here. |
| 3334 | if (isIllegalVectorType(Ty)) { |
| 3335 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3336 | if (Size <= 32) { |
| 3337 | llvm::Type *ResType = |
| 3338 | llvm::Type::getInt32Ty(getVMContext()); |
| 3339 | return ABIArgInfo::getDirect(ResType); |
| 3340 | } |
| 3341 | if (Size == 64) { |
| 3342 | llvm::Type *ResType = llvm::VectorType::get( |
| 3343 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3344 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3345 | return ABIArgInfo::getDirect(ResType); |
| 3346 | } |
| 3347 | if (Size == 128) { |
| 3348 | llvm::Type *ResType = llvm::VectorType::get( |
| 3349 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3350 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3351 | return ABIArgInfo::getDirect(ResType); |
| 3352 | } |
| 3353 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3354 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3355 | // Update VFPRegs for legal vector types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3356 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3357 | uint64_t Size = getContext().getTypeSize(VT); |
| 3358 | // Size of a legal vector should be power of 2 and above 64. |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3359 | markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3360 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3361 | // Update VFPRegs for floating point types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3362 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3363 | if (BT->getKind() == BuiltinType::Half || |
| 3364 | BT->getKind() == BuiltinType::Float) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3365 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3366 | if (BT->getKind() == BuiltinType::Double || |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3367 | BT->getKind() == BuiltinType::LongDouble) |
| 3368 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3369 | } |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3370 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3371 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3372 | // Treat an enum type as its underlying type. |
| 3373 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3374 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3375 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3376 | return (Ty->isPromotableIntegerType() ? |
| 3377 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3378 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3379 | |
Tim Northover | f5c3a25 | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 3380 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 3381 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3382 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3383 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3384 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3385 | return ABIArgInfo::getIgnore(); |
| 3386 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3387 | if (getABIKind() == ARMABIInfo::AAPCS_VFP) { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3388 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 3389 | // into VFP registers. |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3390 | const Type *Base = 0; |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3391 | uint64_t Members = 0; |
| 3392 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3393 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3394 | // Base can be a floating-point or a vector. |
| 3395 | if (Base->isVectorType()) { |
| 3396 | // ElementSize is in number of floats. |
| 3397 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3398 | markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize, |
| 3399 | Members * ElementSize); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3400 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3401 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3402 | else { |
| 3403 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 3404 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3405 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3406 | } |
| 3407 | IsHA = true; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3408 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3409 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Manman Ren | 634b3d2 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 3412 | // Support byval for ARM. |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3413 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 3414 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 3415 | // than ABI alignment. |
Manman Ren | fd1ba91 | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 3416 | uint64_t ABIAlign = 4; |
| 3417 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 3418 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3419 | getABIKind() == ARMABIInfo::AAPCS) |
| 3420 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 885ad69 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 3421 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
| 3422 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true, |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3423 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Daniel Dunbar | 8aa87c7 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 3426 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3427 | llvm::Type* ElemTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3428 | unsigned SizeRegs; |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3429 | // FIXME: Try to match the types of the arguments more accurately where |
| 3430 | // we can. |
| 3431 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 3432 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 3433 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3434 | } else { |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3435 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3436 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 3437 | } |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3438 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3439 | llvm::Type *STy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3440 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3441 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3444 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3445 | llvm::LLVMContext &VMContext) { |
| 3446 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 3447 | // is called integer-like if its size is less than or equal to one word, and |
| 3448 | // the offset of each of its addressable sub-fields is zero. |
| 3449 | |
| 3450 | uint64_t Size = Context.getTypeSize(Ty); |
| 3451 | |
| 3452 | // Check that the type fits in a word. |
| 3453 | if (Size > 32) |
| 3454 | return false; |
| 3455 | |
| 3456 | // FIXME: Handle vector types! |
| 3457 | if (Ty->isVectorType()) |
| 3458 | return false; |
| 3459 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 3460 | // Float types are never treated as "integer like". |
| 3461 | if (Ty->isRealFloatingType()) |
| 3462 | return false; |
| 3463 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3464 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3465 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3466 | return true; |
| 3467 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 3468 | // Small complex integer types are "integer like". |
| 3469 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 3470 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3471 | |
| 3472 | // Single element and zero sized arrays should be allowed, by the definition |
| 3473 | // above, but they are not. |
| 3474 | |
| 3475 | // Otherwise, it must be a record type. |
| 3476 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3477 | if (!RT) return false; |
| 3478 | |
| 3479 | // Ignore records with flexible arrays. |
| 3480 | const RecordDecl *RD = RT->getDecl(); |
| 3481 | if (RD->hasFlexibleArrayMember()) |
| 3482 | return false; |
| 3483 | |
| 3484 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 3485 | // like". |
| 3486 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3487 | |
| 3488 | bool HadField = false; |
| 3489 | unsigned idx = 0; |
| 3490 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3491 | i != e; ++i, ++idx) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3492 | const FieldDecl *FD = *i; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3493 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3494 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 3495 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 3496 | // struct { int : 0; int x } |
| 3497 | // is non-integer like according to gcc. |
| 3498 | if (FD->isBitField()) { |
| 3499 | if (!RD->isUnion()) |
| 3500 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3501 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3502 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3503 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3504 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3505 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3508 | // Check if this field is at offset 0. |
| 3509 | if (Layout.getFieldOffset(idx) != 0) |
| 3510 | return false; |
| 3511 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3512 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3513 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3514 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3515 | // Only allow at most one field in a structure. This doesn't match the |
| 3516 | // wording above, but follows gcc in situations with a field following an |
| 3517 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3518 | if (!RD->isUnion()) { |
| 3519 | if (HadField) |
| 3520 | return false; |
| 3521 | |
| 3522 | HadField = true; |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | return true; |
| 3527 | } |
| 3528 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3529 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3530 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3531 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3532 | |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3533 | // Large vector types should be returned via memory. |
| 3534 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3535 | return ABIArgInfo::getIndirect(0); |
| 3536 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3537 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3538 | // Treat an enum type as its underlying type. |
| 3539 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3540 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3541 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3542 | return (RetTy->isPromotableIntegerType() ? |
| 3543 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3544 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3545 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3546 | // Structures with either a non-trivial destructor or a non-trivial |
| 3547 | // copy constructor are always indirect. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3548 | if (isRecordReturnIndirect(RetTy, CGT)) |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3549 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3550 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3551 | // Are we following APCS? |
| 3552 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3553 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3554 | return ABIArgInfo::getIgnore(); |
| 3555 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3556 | // Complex types are all returned as packed integers. |
| 3557 | // |
| 3558 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 3559 | // correctly. |
| 3560 | if (RetTy->isAnyComplexType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3561 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3562 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3563 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3564 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3565 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3566 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3567 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3568 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3569 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3570 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3571 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3572 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | // Otherwise return in memory. |
| 3576 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3577 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3578 | |
| 3579 | // Otherwise this is an AAPCS variant. |
| 3580 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3581 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3582 | return ABIArgInfo::getIgnore(); |
| 3583 | |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3584 | // Check for homogeneous aggregates with AAPCS-VFP. |
| 3585 | if (getABIKind() == AAPCS_VFP) { |
| 3586 | const Type *Base = 0; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3587 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 3588 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3589 | // Homogeneous Aggregates are returned directly. |
| 3590 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3591 | } |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3594 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 3595 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3596 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3597 | if (Size <= 32) { |
| 3598 | // Return in the smallest viable integer type. |
| 3599 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3600 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3601 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3602 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3603 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3606 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3609 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 3610 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3611 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3612 | // Check whether VT is legal. |
| 3613 | unsigned NumElements = VT->getNumElements(); |
| 3614 | uint64_t Size = getContext().getTypeSize(VT); |
| 3615 | // NumElements should be power of 2. |
| 3616 | if ((NumElements & (NumElements - 1)) != 0) |
| 3617 | return true; |
| 3618 | // Size should be greater than 32 bits. |
| 3619 | return Size <= 32; |
| 3620 | } |
| 3621 | return false; |
| 3622 | } |
| 3623 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3624 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3625 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3626 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3627 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3628 | |
| 3629 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3630 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3631 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3632 | |
Tim Northover | 373ac0a | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 3633 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3634 | // These are ignored for parameter passing purposes. |
| 3635 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3636 | return Builder.CreateBitCast(Addr, PTy); |
| 3637 | } |
| 3638 | |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3639 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3640 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3641 | bool IsIndirect = false; |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3642 | |
| 3643 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 3644 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
Manman Ren | 9337102 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3645 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3646 | getABIKind() == ARMABIInfo::AAPCS) |
| 3647 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 3648 | else |
| 3649 | TyAlign = 4; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3650 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 3651 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 3652 | IsIndirect = true; |
| 3653 | Size = 4; |
| 3654 | TyAlign = 4; |
| 3655 | } |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3656 | |
| 3657 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3658 | if (TyAlign > 4) { |
| 3659 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 3660 | "Alignment is not power of 2!"); |
| 3661 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3662 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 3663 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3664 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3665 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3666 | |
| 3667 | uint64_t Offset = |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3668 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3669 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3670 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3671 | "ap.next"); |
| 3672 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3673 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3674 | if (IsIndirect) |
| 3675 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 9337102 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3676 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3677 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 3678 | // may not be correctly aligned for the vector type. We create an aligned |
| 3679 | // temporary space and copy the content over from ap.cur to the temporary |
| 3680 | // space. This is necessary if the natural alignment of the type is greater |
| 3681 | // than the ABI alignment. |
| 3682 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 3683 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 3684 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 3685 | "var.align"); |
| 3686 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 3687 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 3688 | Builder.CreateMemCpy(Dst, Src, |
| 3689 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 3690 | TyAlign, false); |
| 3691 | Addr = AlignedTemp; //The content is in aligned location. |
| 3692 | } |
| 3693 | llvm::Type *PTy = |
| 3694 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3695 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3696 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3697 | return AddrTyped; |
| 3698 | } |
| 3699 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3700 | namespace { |
| 3701 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3702 | class NaClARMABIInfo : public ABIInfo { |
| 3703 | public: |
| 3704 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3705 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
| 3706 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3707 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3708 | CodeGenFunction &CGF) const; |
| 3709 | private: |
| 3710 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 3711 | ARMABIInfo NInfo; // Used for everything else. |
| 3712 | }; |
| 3713 | |
| 3714 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3715 | public: |
| 3716 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3717 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 3718 | }; |
| 3719 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3722 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3723 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 3724 | PInfo.computeInfo(FI); |
| 3725 | else |
| 3726 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 3727 | } |
| 3728 | |
| 3729 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3730 | CodeGenFunction &CGF) const { |
| 3731 | // Always use the native convention; calling pnacl-style varargs functions |
| 3732 | // is unsupported. |
| 3733 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 3734 | } |
| 3735 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3736 | //===----------------------------------------------------------------------===// |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3737 | // AArch64 ABI Implementation |
| 3738 | //===----------------------------------------------------------------------===// |
| 3739 | |
| 3740 | namespace { |
| 3741 | |
| 3742 | class AArch64ABIInfo : public ABIInfo { |
| 3743 | public: |
| 3744 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 3745 | |
| 3746 | private: |
| 3747 | // The AArch64 PCS is explicit about return types and argument types being |
| 3748 | // handled identically, so we don't need to draw a distinction between |
| 3749 | // Argument and Return classification. |
| 3750 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 3751 | int &FreeVFPRegs) const; |
| 3752 | |
| 3753 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 3754 | llvm::Type *DirectTy = 0) const; |
| 3755 | |
| 3756 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3757 | |
| 3758 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3759 | CodeGenFunction &CGF) const; |
| 3760 | }; |
| 3761 | |
| 3762 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3763 | public: |
| 3764 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3765 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 3766 | |
| 3767 | const AArch64ABIInfo &getABIInfo() const { |
| 3768 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3769 | } |
| 3770 | |
| 3771 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3772 | return 31; |
| 3773 | } |
| 3774 | |
| 3775 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3776 | llvm::Value *Address) const { |
| 3777 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 3778 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 3779 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 3780 | |
| 3781 | // 64-95 are v0-v31: 16 bytes each |
| 3782 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 3783 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 3784 | |
| 3785 | return false; |
| 3786 | } |
| 3787 | |
| 3788 | }; |
| 3789 | |
| 3790 | } |
| 3791 | |
| 3792 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3793 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3794 | |
| 3795 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 3796 | FreeIntRegs, FreeVFPRegs); |
| 3797 | |
| 3798 | FreeIntRegs = FreeVFPRegs = 8; |
| 3799 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3800 | it != ie; ++it) { |
| 3801 | it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs); |
| 3802 | |
| 3803 | } |
| 3804 | } |
| 3805 | |
| 3806 | ABIArgInfo |
| 3807 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 3808 | bool IsInt, llvm::Type *DirectTy) const { |
| 3809 | if (FreeRegs >= RegsNeeded) { |
| 3810 | FreeRegs -= RegsNeeded; |
| 3811 | return ABIArgInfo::getDirect(DirectTy); |
| 3812 | } |
| 3813 | |
| 3814 | llvm::Type *Padding = 0; |
| 3815 | |
| 3816 | // We need padding so that later arguments don't get filled in anyway. That |
| 3817 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 3818 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 3819 | // concerned. |
| 3820 | if (FreeRegs > 0) { |
| 3821 | if (IsInt) |
| 3822 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 3823 | else |
| 3824 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 3825 | |
| 3826 | // Either [N x i64] or [N x float]. |
| 3827 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 3828 | FreeRegs = 0; |
| 3829 | } |
| 3830 | |
| 3831 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 3832 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 3833 | Padding); |
| 3834 | } |
| 3835 | |
| 3836 | |
| 3837 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 3838 | int &FreeIntRegs, |
| 3839 | int &FreeVFPRegs) const { |
| 3840 | // Can only occurs for return, but harmless otherwise. |
| 3841 | if (Ty->isVoidType()) |
| 3842 | return ABIArgInfo::getIgnore(); |
| 3843 | |
| 3844 | // Large vector types should be returned via memory. There's no such concept |
| 3845 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 3846 | // classified they'd go into memory (see B.3). |
| 3847 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 3848 | if (FreeIntRegs > 0) |
| 3849 | --FreeIntRegs; |
| 3850 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3851 | } |
| 3852 | |
| 3853 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 3854 | // be passed directly. After this block we're guaranteed to be in a |
| 3855 | // complicated case. |
| 3856 | if (!isAggregateTypeForABI(Ty)) { |
| 3857 | // Treat an enum type as its underlying type. |
| 3858 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3859 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3860 | |
| 3861 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 3862 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 3863 | |
| 3864 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 3865 | "unexpectedly large scalar type"); |
| 3866 | |
| 3867 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3868 | |
| 3869 | // If the type may need padding registers to ensure "alignment", we must be |
| 3870 | // careful when this is accounted for. Increasing the effective size covers |
| 3871 | // all cases. |
| 3872 | if (getContext().getTypeAlign(Ty) == 128) |
| 3873 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 3874 | |
| 3875 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 3876 | } |
| 3877 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3878 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { |
| 3879 | if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3880 | --FreeIntRegs; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3881 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3885 | if (!getContext().getLangOpts().CPlusPlus) { |
| 3886 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 3887 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 3888 | // the object for parameter-passsing purposes. |
| 3889 | return ABIArgInfo::getIgnore(); |
| 3890 | } |
| 3891 | |
| 3892 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 3893 | // description of va_arg in the PCS require that an empty struct does |
| 3894 | // actually occupy space for parameter-passing. I'm hoping for a |
| 3895 | // clarification giving an explicit paragraph to point to in future. |
| 3896 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 3897 | llvm::Type::getInt8Ty(getVMContext())); |
| 3898 | } |
| 3899 | |
| 3900 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 3901 | const Type *Base = 0; |
| 3902 | uint64_t NumMembers = 0; |
| 3903 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 3904 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 3905 | // Homogeneous aggregates are passed and returned directly. |
| 3906 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 3907 | /*IsInt=*/ false); |
| 3908 | } |
| 3909 | |
| 3910 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3911 | if (Size <= 128) { |
| 3912 | // Small structs can use the same direct type whether they're in registers |
| 3913 | // or on the stack. |
| 3914 | llvm::Type *BaseTy; |
| 3915 | unsigned NumBases; |
| 3916 | int SizeInRegs = (Size + 63) / 64; |
| 3917 | |
| 3918 | if (getContext().getTypeAlign(Ty) == 128) { |
| 3919 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 3920 | NumBases = 1; |
| 3921 | |
| 3922 | // If the type may need padding registers to ensure "alignment", we must |
| 3923 | // be careful when this is accounted for. Increasing the effective size |
| 3924 | // covers all cases. |
| 3925 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 3926 | } else { |
| 3927 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3928 | NumBases = SizeInRegs; |
| 3929 | } |
| 3930 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 3931 | |
| 3932 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 3933 | /*IsInt=*/ true, DirectTy); |
| 3934 | } |
| 3935 | |
| 3936 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 3937 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 3938 | --FreeIntRegs; |
| 3939 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 3940 | } |
| 3941 | |
| 3942 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3943 | CodeGenFunction &CGF) const { |
| 3944 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3945 | // Standard, section B.4: |
| 3946 | // |
| 3947 | // struct { |
| 3948 | // void *__stack; |
| 3949 | // void *__gr_top; |
| 3950 | // void *__vr_top; |
| 3951 | // int __gr_offs; |
| 3952 | // int __vr_offs; |
| 3953 | // }; |
| 3954 | |
| 3955 | assert(!CGF.CGM.getDataLayout().isBigEndian() |
| 3956 | && "va_arg not implemented for big-endian AArch64"); |
| 3957 | |
| 3958 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3959 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 3960 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 3961 | |
| 3962 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3963 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3964 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3965 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3966 | |
| 3967 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 3968 | int reg_top_index; |
| 3969 | int RegSize; |
| 3970 | if (FreeIntRegs < 8) { |
| 3971 | assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs"); |
| 3972 | // 3 is the field number of __gr_offs |
| 3973 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3974 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3975 | reg_top_index = 1; // field number for __gr_top |
| 3976 | RegSize = 8 * (8 - FreeIntRegs); |
| 3977 | } else { |
| 3978 | assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs"); |
| 3979 | // 4 is the field number of __vr_offs. |
| 3980 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3981 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3982 | reg_top_index = 2; // field number for __vr_top |
| 3983 | RegSize = 16 * (8 - FreeVFPRegs); |
| 3984 | } |
| 3985 | |
| 3986 | //======================================= |
| 3987 | // Find out where argument was passed |
| 3988 | //======================================= |
| 3989 | |
| 3990 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3991 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3992 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3993 | // whatever they get). |
| 3994 | llvm::Value *UsingStack = 0; |
| 3995 | UsingStack = CGF.Builder.CreateICmpSGE(reg_offs, |
| 3996 | llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3997 | |
| 3998 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3999 | |
| 4000 | // Otherwise, at least some kind of argument could go in these registers, the |
| 4001 | // quesiton is whether this particular type is too big. |
| 4002 | CGF.EmitBlock(MaybeRegBlock); |
| 4003 | |
| 4004 | // Integer arguments may need to correct register alignment (for example a |
| 4005 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4006 | // align __gr_offs to calculate the potential address. |
| 4007 | if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 4008 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 4009 | |
| 4010 | reg_offs = CGF.Builder.CreateAdd(reg_offs, |
| 4011 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4012 | "align_regoffs"); |
| 4013 | reg_offs = CGF.Builder.CreateAnd(reg_offs, |
| 4014 | llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4015 | "aligned_regoffs"); |
| 4016 | } |
| 4017 | |
| 4018 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 4019 | llvm::Value *NewOffset = 0; |
| 4020 | NewOffset = CGF.Builder.CreateAdd(reg_offs, |
| 4021 | llvm::ConstantInt::get(CGF.Int32Ty, RegSize), |
| 4022 | "new_reg_offs"); |
| 4023 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4024 | |
| 4025 | // Now we're in a position to decide whether this argument really was in |
| 4026 | // registers or not. |
| 4027 | llvm::Value *InRegs = 0; |
| 4028 | InRegs = CGF.Builder.CreateICmpSLE(NewOffset, |
| 4029 | llvm::ConstantInt::get(CGF.Int32Ty, 0), |
| 4030 | "inreg"); |
| 4031 | |
| 4032 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4033 | |
| 4034 | //======================================= |
| 4035 | // Argument was in registers |
| 4036 | //======================================= |
| 4037 | |
| 4038 | // Now we emit the code for if the argument was originally passed in |
| 4039 | // registers. First start the appropriate block: |
| 4040 | CGF.EmitBlock(InRegBlock); |
| 4041 | |
| 4042 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 4043 | reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 4044 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4045 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 4046 | llvm::Value *RegAddr = 0; |
| 4047 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4048 | |
| 4049 | if (!AI.isDirect()) { |
| 4050 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4051 | // stored registers or on the stack will actually be a struct **. |
| 4052 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4053 | } |
| 4054 | |
| 4055 | const Type *Base = 0; |
| 4056 | uint64_t NumMembers; |
| 4057 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers) |
| 4058 | && NumMembers > 1) { |
| 4059 | // Homogeneous aggregates passed in registers will have their elements split |
| 4060 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4061 | // qN+1, ...). We reload and store into a temporary local variable |
| 4062 | // contiguously. |
| 4063 | assert(AI.isDirect() && "Homogeneous aggregates should be passed directly"); |
| 4064 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4065 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 4066 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 4067 | |
| 4068 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4069 | llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i); |
| 4070 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4071 | LoadAddr = CGF.Builder.CreateBitCast(LoadAddr, |
| 4072 | llvm::PointerType::getUnqual(BaseTy)); |
| 4073 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 4074 | |
| 4075 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4076 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4077 | } |
| 4078 | |
| 4079 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4080 | } else { |
| 4081 | // Otherwise the object is contiguous in memory |
| 4082 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4083 | } |
| 4084 | |
| 4085 | CGF.EmitBranch(ContBlock); |
| 4086 | |
| 4087 | //======================================= |
| 4088 | // Argument was on the stack |
| 4089 | //======================================= |
| 4090 | CGF.EmitBlock(OnStackBlock); |
| 4091 | |
| 4092 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 4093 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 4094 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4095 | |
| 4096 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4097 | // floating-point ones might be affected. |
| 4098 | if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 4099 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 4100 | |
| 4101 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4102 | |
| 4103 | OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, |
| 4104 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4105 | "align_stack"); |
| 4106 | OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr, |
| 4107 | llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4108 | "align_stack"); |
| 4109 | |
| 4110 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4111 | } |
| 4112 | |
| 4113 | uint64_t StackSize; |
| 4114 | if (AI.isDirect()) |
| 4115 | StackSize = getContext().getTypeSize(Ty) / 8; |
| 4116 | else |
| 4117 | StackSize = 8; |
| 4118 | |
| 4119 | // All stack slots are 8 bytes |
| 4120 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4121 | |
| 4122 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4123 | llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, |
| 4124 | "new_stack"); |
| 4125 | |
| 4126 | // Write the new value of __stack for the next call to va_arg |
| 4127 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4128 | |
| 4129 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4130 | |
| 4131 | CGF.EmitBranch(ContBlock); |
| 4132 | |
| 4133 | //======================================= |
| 4134 | // Tidy up |
| 4135 | //======================================= |
| 4136 | CGF.EmitBlock(ContBlock); |
| 4137 | |
| 4138 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4139 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4140 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4141 | |
| 4142 | if (AI.isDirect()) |
| 4143 | return ResAddr; |
| 4144 | |
| 4145 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4146 | } |
| 4147 | |
| 4148 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4149 | // NVPTX ABI Implementation |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4150 | //===----------------------------------------------------------------------===// |
| 4151 | |
| 4152 | namespace { |
| 4153 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4154 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4155 | public: |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4156 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4157 | |
| 4158 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4159 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4160 | |
| 4161 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4162 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4163 | CodeGenFunction &CFG) const; |
| 4164 | }; |
| 4165 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4166 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4167 | public: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4168 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4169 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4170 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4171 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4172 | CodeGen::CodeGenModule &M) const; |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4173 | private: |
| 4174 | static void addKernelMetadata(llvm::Function *F); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4175 | }; |
| 4176 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4177 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4178 | if (RetTy->isVoidType()) |
| 4179 | return ABIArgInfo::getIgnore(); |
| 4180 | if (isAggregateTypeForABI(RetTy)) |
| 4181 | return ABIArgInfo::getIndirect(0); |
| 4182 | return ABIArgInfo::getDirect(); |
| 4183 | } |
| 4184 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4185 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4186 | if (isAggregateTypeForABI(Ty)) |
| 4187 | return ABIArgInfo::getIndirect(0); |
| 4188 | |
| 4189 | return ABIArgInfo::getDirect(); |
| 4190 | } |
| 4191 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4192 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4193 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4194 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4195 | it != ie; ++it) |
| 4196 | it->info = classifyArgumentType(it->type); |
| 4197 | |
| 4198 | // Always honor user-specified calling convention. |
| 4199 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4200 | return; |
| 4201 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4202 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4203 | } |
| 4204 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4205 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4206 | CodeGenFunction &CFG) const { |
| 4207 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4210 | void NVPTXTargetCodeGenInfo:: |
| 4211 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4212 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4213 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4214 | if (!FD) return; |
| 4215 | |
| 4216 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4217 | |
| 4218 | // Perform special handling in OpenCL mode |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4219 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4220 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4221 | // By default, all functions are device functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4222 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4223 | // OpenCL __kernel functions get kernel metadata |
| 4224 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4225 | // And kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4226 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4227 | } |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4228 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4229 | |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4230 | // Perform special handling in CUDA mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4231 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4232 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4233 | // __global__ functions cannot be called from the device, we do not |
| 4234 | // need to set the noinline attribute. |
| 4235 | if (FD->getAttr<CUDAGlobalAttr>()) |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4236 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4240 | void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) { |
| 4241 | llvm::Module *M = F->getParent(); |
| 4242 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4243 | |
| 4244 | // Get "nvvm.annotations" metadata node |
| 4245 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4246 | |
| 4247 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4248 | llvm::SmallVector<llvm::Value *, 3> MDVals; |
| 4249 | MDVals.push_back(F); |
| 4250 | MDVals.push_back(llvm::MDString::get(Ctx, "kernel")); |
| 4251 | MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)); |
| 4252 | |
| 4253 | // Append metadata to nvvm.annotations |
| 4254 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4255 | } |
| 4256 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4260 | // SystemZ ABI Implementation |
| 4261 | //===----------------------------------------------------------------------===// |
| 4262 | |
| 4263 | namespace { |
| 4264 | |
| 4265 | class SystemZABIInfo : public ABIInfo { |
| 4266 | public: |
| 4267 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4268 | |
| 4269 | bool isPromotableIntegerType(QualType Ty) const; |
| 4270 | bool isCompoundType(QualType Ty) const; |
| 4271 | bool isFPArgumentType(QualType Ty) const; |
| 4272 | |
| 4273 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4274 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4275 | |
| 4276 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 4277 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4278 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4279 | it != ie; ++it) |
| 4280 | it->info = classifyArgumentType(it->type); |
| 4281 | } |
| 4282 | |
| 4283 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4284 | CodeGenFunction &CGF) const; |
| 4285 | }; |
| 4286 | |
| 4287 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4288 | public: |
| 4289 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4290 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4291 | }; |
| 4292 | |
| 4293 | } |
| 4294 | |
| 4295 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4296 | // Treat an enum type as its underlying type. |
| 4297 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4298 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4299 | |
| 4300 | // Promotable integer types are required to be promoted by the ABI. |
| 4301 | if (Ty->isPromotableIntegerType()) |
| 4302 | return true; |
| 4303 | |
| 4304 | // 32-bit values must also be promoted. |
| 4305 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4306 | switch (BT->getKind()) { |
| 4307 | case BuiltinType::Int: |
| 4308 | case BuiltinType::UInt: |
| 4309 | return true; |
| 4310 | default: |
| 4311 | return false; |
| 4312 | } |
| 4313 | return false; |
| 4314 | } |
| 4315 | |
| 4316 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 4317 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 4318 | } |
| 4319 | |
| 4320 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 4321 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4322 | switch (BT->getKind()) { |
| 4323 | case BuiltinType::Float: |
| 4324 | case BuiltinType::Double: |
| 4325 | return true; |
| 4326 | default: |
| 4327 | return false; |
| 4328 | } |
| 4329 | |
| 4330 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 4331 | const RecordDecl *RD = RT->getDecl(); |
| 4332 | bool Found = false; |
| 4333 | |
| 4334 | // If this is a C++ record, check the bases first. |
| 4335 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 4336 | for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(), |
| 4337 | E = CXXRD->bases_end(); I != E; ++I) { |
| 4338 | QualType Base = I->getType(); |
| 4339 | |
| 4340 | // Empty bases don't affect things either way. |
| 4341 | if (isEmptyRecord(getContext(), Base, true)) |
| 4342 | continue; |
| 4343 | |
| 4344 | if (Found) |
| 4345 | return false; |
| 4346 | Found = isFPArgumentType(Base); |
| 4347 | if (!Found) |
| 4348 | return false; |
| 4349 | } |
| 4350 | |
| 4351 | // Check the fields. |
| 4352 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 4353 | E = RD->field_end(); I != E; ++I) { |
| 4354 | const FieldDecl *FD = *I; |
| 4355 | |
| 4356 | // Empty bitfields don't affect things either way. |
| 4357 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 4358 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 4359 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4360 | return true; |
| 4361 | |
| 4362 | // Unlike isSingleElementStruct(), arrays do not count. |
| 4363 | // Nested isFPArgumentType structures still do though. |
| 4364 | if (Found) |
| 4365 | return false; |
| 4366 | Found = isFPArgumentType(FD->getType()); |
| 4367 | if (!Found) |
| 4368 | return false; |
| 4369 | } |
| 4370 | |
| 4371 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 4372 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 4373 | return Found; |
| 4374 | } |
| 4375 | |
| 4376 | return false; |
| 4377 | } |
| 4378 | |
| 4379 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4380 | CodeGenFunction &CGF) const { |
| 4381 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 4382 | // struct { |
| 4383 | // i64 __gpr; |
| 4384 | // i64 __fpr; |
| 4385 | // i8 *__overflow_arg_area; |
| 4386 | // i8 *__reg_save_area; |
| 4387 | // }; |
| 4388 | |
| 4389 | // Every argument occupies 8 bytes and is passed by preference in either |
| 4390 | // GPRs or FPRs. |
| 4391 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4392 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 4393 | bool InFPRs = isFPArgumentType(Ty); |
| 4394 | |
| 4395 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4396 | bool IsIndirect = AI.isIndirect(); |
| 4397 | unsigned UnpaddedBitSize; |
| 4398 | if (IsIndirect) { |
| 4399 | APTy = llvm::PointerType::getUnqual(APTy); |
| 4400 | UnpaddedBitSize = 64; |
| 4401 | } else |
| 4402 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 4403 | unsigned PaddedBitSize = 64; |
| 4404 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 4405 | |
| 4406 | unsigned PaddedSize = PaddedBitSize / 8; |
| 4407 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 4408 | |
| 4409 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 4410 | if (InFPRs) { |
| 4411 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 4412 | RegCountField = 1; // __fpr |
| 4413 | RegSaveIndex = 16; // save offset for f0 |
| 4414 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 4415 | } else { |
| 4416 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 4417 | RegCountField = 0; // __gpr |
| 4418 | RegSaveIndex = 2; // save offset for r2 |
| 4419 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 4420 | } |
| 4421 | |
| 4422 | llvm::Value *RegCountPtr = |
| 4423 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 4424 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 4425 | llvm::Type *IndexTy = RegCount->getType(); |
| 4426 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 4427 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
| 4428 | "fits_in_regs"); |
| 4429 | |
| 4430 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4431 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 4432 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4433 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 4434 | |
| 4435 | // Emit code to load the value if it was passed in registers. |
| 4436 | CGF.EmitBlock(InRegBlock); |
| 4437 | |
| 4438 | // Work out the address of an argument register. |
| 4439 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 4440 | llvm::Value *ScaledRegCount = |
| 4441 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 4442 | llvm::Value *RegBase = |
| 4443 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 4444 | llvm::Value *RegOffset = |
| 4445 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 4446 | llvm::Value *RegSaveAreaPtr = |
| 4447 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 4448 | llvm::Value *RegSaveArea = |
| 4449 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 4450 | llvm::Value *RawRegAddr = |
| 4451 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 4452 | llvm::Value *RegAddr = |
| 4453 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 4454 | |
| 4455 | // Update the register count |
| 4456 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 4457 | llvm::Value *NewRegCount = |
| 4458 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 4459 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 4460 | CGF.EmitBranch(ContBlock); |
| 4461 | |
| 4462 | // Emit code to load the value if it was passed in memory. |
| 4463 | CGF.EmitBlock(InMemBlock); |
| 4464 | |
| 4465 | // Work out the address of a stack argument. |
| 4466 | llvm::Value *OverflowArgAreaPtr = |
| 4467 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 4468 | llvm::Value *OverflowArgArea = |
| 4469 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 4470 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 4471 | llvm::Value *RawMemAddr = |
| 4472 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 4473 | llvm::Value *MemAddr = |
| 4474 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 4475 | |
| 4476 | // Update overflow_arg_area_ptr pointer |
| 4477 | llvm::Value *NewOverflowArgArea = |
| 4478 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 4479 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 4480 | CGF.EmitBranch(ContBlock); |
| 4481 | |
| 4482 | // Return the appropriate result. |
| 4483 | CGF.EmitBlock(ContBlock); |
| 4484 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 4485 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4486 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 4487 | |
| 4488 | if (IsIndirect) |
| 4489 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 4490 | |
| 4491 | return ResAddr; |
| 4492 | } |
| 4493 | |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 4494 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 4495 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 4496 | assert(Triple.getArch() == llvm::Triple::x86); |
| 4497 | |
| 4498 | switch (Opts.getStructReturnConvention()) { |
| 4499 | case CodeGenOptions::SRCK_Default: |
| 4500 | break; |
| 4501 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 4502 | return false; |
| 4503 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 4504 | return true; |
| 4505 | } |
| 4506 | |
| 4507 | if (Triple.isOSDarwin()) |
| 4508 | return true; |
| 4509 | |
| 4510 | switch (Triple.getOS()) { |
| 4511 | case llvm::Triple::Cygwin: |
| 4512 | case llvm::Triple::MinGW32: |
| 4513 | case llvm::Triple::AuroraUX: |
| 4514 | case llvm::Triple::DragonFly: |
| 4515 | case llvm::Triple::FreeBSD: |
| 4516 | case llvm::Triple::OpenBSD: |
| 4517 | case llvm::Triple::Bitrig: |
| 4518 | case llvm::Triple::Win32: |
| 4519 | return true; |
| 4520 | default: |
| 4521 | return false; |
| 4522 | } |
| 4523 | } |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4524 | |
| 4525 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 4526 | if (RetTy->isVoidType()) |
| 4527 | return ABIArgInfo::getIgnore(); |
| 4528 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 4529 | return ABIArgInfo::getIndirect(0); |
| 4530 | return (isPromotableIntegerType(RetTy) ? |
| 4531 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4532 | } |
| 4533 | |
| 4534 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 4535 | // Handle the generic C++ ABI. |
| 4536 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 4537 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 4538 | |
| 4539 | // Integers and enums are extended to full register width. |
| 4540 | if (isPromotableIntegerType(Ty)) |
| 4541 | return ABIArgInfo::getExtend(); |
| 4542 | |
| 4543 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 4544 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4545 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
| 4546 | return ABIArgInfo::getIndirect(0); |
| 4547 | |
| 4548 | // Handle small structures. |
| 4549 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4550 | // Structures with flexible arrays have variable length, so really |
| 4551 | // fail the size test above. |
| 4552 | const RecordDecl *RD = RT->getDecl(); |
| 4553 | if (RD->hasFlexibleArrayMember()) |
| 4554 | return ABIArgInfo::getIndirect(0); |
| 4555 | |
| 4556 | // The structure is passed as an unextended integer, a float, or a double. |
| 4557 | llvm::Type *PassTy; |
| 4558 | if (isFPArgumentType(Ty)) { |
| 4559 | assert(Size == 32 || Size == 64); |
| 4560 | if (Size == 32) |
| 4561 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 4562 | else |
| 4563 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 4564 | } else |
| 4565 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4566 | return ABIArgInfo::getDirect(PassTy); |
| 4567 | } |
| 4568 | |
| 4569 | // Non-structure compounds are passed indirectly. |
| 4570 | if (isCompoundType(Ty)) |
| 4571 | return ABIArgInfo::getIndirect(0); |
| 4572 | |
| 4573 | return ABIArgInfo::getDirect(0); |
| 4574 | } |
| 4575 | |
| 4576 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4577 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4578 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4579 | |
| 4580 | namespace { |
| 4581 | |
| 4582 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4583 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4584 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4585 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4586 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4587 | CodeGen::CodeGenModule &M) const; |
| 4588 | }; |
| 4589 | |
| 4590 | } |
| 4591 | |
| 4592 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4593 | llvm::GlobalValue *GV, |
| 4594 | CodeGen::CodeGenModule &M) const { |
| 4595 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4596 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 4597 | // Handle 'interrupt' attribute: |
| 4598 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4599 | |
| 4600 | // Step 1: Set ISR calling convention. |
| 4601 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 4602 | |
| 4603 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4604 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4605 | |
| 4606 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4607 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4608 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4609 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4610 | GV, &M.getModule()); |
| 4611 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4615 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4616 | // MIPS ABI Implementation. This works for both little-endian and |
| 4617 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4618 | //===----------------------------------------------------------------------===// |
| 4619 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4620 | namespace { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4621 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4622 | bool IsO32; |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4623 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 4624 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4625 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4626 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4627 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4628 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4629 | public: |
Akira Hatanaka | b551dd3 | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 4630 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4631 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
| 4632 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4633 | |
| 4634 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4635 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4636 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4637 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4638 | CodeGenFunction &CGF) const; |
| 4639 | }; |
| 4640 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4641 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4642 | unsigned SizeOfUnwindException; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4643 | public: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4644 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 4645 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
| 4646 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4647 | |
| 4648 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 4649 | return 29; |
| 4650 | } |
| 4651 | |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4652 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4653 | CodeGen::CodeGenModule &CGM) const { |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4654 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4655 | if (!FD) return; |
Rafael Espindola | d8e6d6d | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 4656 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4657 | if (FD->hasAttr<Mips16Attr>()) { |
| 4658 | Fn->addFnAttr("mips16"); |
| 4659 | } |
| 4660 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 4661 | Fn->addFnAttr("nomips16"); |
| 4662 | } |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4663 | } |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4664 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4665 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4666 | llvm::Value *Address) const; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4667 | |
| 4668 | unsigned getSizeOfUnwindException() const { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4669 | return SizeOfUnwindException; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4670 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4671 | }; |
| 4672 | } |
| 4673 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4674 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 4675 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4676 | llvm::IntegerType *IntTy = |
| 4677 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4678 | |
| 4679 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 4680 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 4681 | ArgList.push_back(IntTy); |
| 4682 | |
| 4683 | // If necessary, add one more integer type to ArgList. |
| 4684 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 4685 | |
| 4686 | if (R) |
| 4687 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4690 | // In N32/64, an aligned double precision floating point field is passed in |
| 4691 | // a register. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4692 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4693 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 4694 | |
| 4695 | if (IsO32) { |
| 4696 | CoerceToIntArgs(TySize, ArgList); |
| 4697 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4698 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4699 | |
Akira Hatanaka | 2afd23d | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 4700 | if (Ty->isComplexType()) |
| 4701 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 6d1080f | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 4702 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4703 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4704 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4705 | // Unions/vectors are passed in integer registers. |
| 4706 | if (!RT || !RT->isStructureOrClassType()) { |
| 4707 | CoerceToIntArgs(TySize, ArgList); |
| 4708 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4709 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4710 | |
| 4711 | const RecordDecl *RD = RT->getDecl(); |
| 4712 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4713 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4714 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4715 | uint64_t LastOffset = 0; |
| 4716 | unsigned idx = 0; |
| 4717 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 4718 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4719 | // Iterate over fields in the struct/class and check if there are any aligned |
| 4720 | // double fields. |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4721 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4722 | i != e; ++i, ++idx) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4723 | const QualType Ty = i->getType(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4724 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 4725 | |
| 4726 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 4727 | continue; |
| 4728 | |
| 4729 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 4730 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 4731 | continue; |
| 4732 | |
| 4733 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 4734 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 4735 | ArgList.push_back(I64); |
| 4736 | |
| 4737 | // Add double type. |
| 4738 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 4739 | LastOffset = Offset + 64; |
| 4740 | } |
| 4741 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4742 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 4743 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4744 | |
| 4745 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4746 | } |
| 4747 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4748 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4749 | assert((Offset % MinABIStackAlignInBytes) == 0); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4750 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4751 | if ((Align - 1) & Offset) |
| 4752 | return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
| 4753 | |
| 4754 | return 0; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4755 | } |
Akira Hatanaka | 9659d59 | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 4756 | |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4757 | ABIArgInfo |
| 4758 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4759 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4760 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4761 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4762 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4763 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 4764 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4765 | Offset = llvm::RoundUpToAlignment(Offset, Align); |
| 4766 | Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4767 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4768 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4769 | // Ignore empty aggregates. |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4770 | if (TySize == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4771 | return ABIArgInfo::getIgnore(); |
| 4772 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4773 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4774 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4775 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4776 | } |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 4777 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4778 | // If we have reached here, aggregates are passed directly by coercing to |
| 4779 | // another structure type. Padding is inserted if the offset of the |
| 4780 | // aggregate is unaligned. |
| 4781 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 4782 | getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | // Treat an enum type as its underlying type. |
| 4786 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4787 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4788 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4789 | if (Ty->isPromotableIntegerType()) |
| 4790 | return ABIArgInfo::getExtend(); |
| 4791 | |
Akira Hatanaka | 4055cfc | 2013-01-24 21:47:33 +0000 | [diff] [blame] | 4792 | return ABIArgInfo::getDirect(0, 0, |
| 4793 | IsO32 ? 0 : getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4796 | llvm::Type* |
| 4797 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4798 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4799 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4800 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4801 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4802 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4803 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 4804 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4805 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4806 | // N32/64 returns struct/classes in floating point registers if the |
| 4807 | // following conditions are met: |
| 4808 | // 1. The size of the struct/class is no larger than 128-bit. |
| 4809 | // 2. The struct/class has one or two fields all of which are floating |
| 4810 | // point types. |
| 4811 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 4812 | // |
| 4813 | // Any other composite results are returned in integer registers. |
| 4814 | // |
| 4815 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 4816 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 4817 | for (; b != e; ++b) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4818 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4819 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4820 | if (!BT || !BT->isFloatingPoint()) |
| 4821 | break; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4822 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4823 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | if (b == e) |
| 4827 | return llvm::StructType::get(getVMContext(), RTList, |
| 4828 | RD->hasAttr<PackedAttr>()); |
| 4829 | |
| 4830 | RTList.clear(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4831 | } |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4834 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4835 | return llvm::StructType::get(getVMContext(), RTList); |
| 4836 | } |
| 4837 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4838 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | a8536c0 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 4839 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4840 | |
| 4841 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4842 | return ABIArgInfo::getIgnore(); |
| 4843 | |
Akira Hatanaka | 8aeb147 | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 4844 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4845 | if (isRecordReturnIndirect(RetTy, CGT)) |
| 4846 | return ABIArgInfo::getIndirect(0); |
| 4847 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4848 | if (Size <= 128) { |
| 4849 | if (RetTy->isAnyComplexType()) |
| 4850 | return ABIArgInfo::getDirect(); |
| 4851 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4852 | // O32 returns integer vectors in registers. |
| 4853 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 4854 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4855 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4856 | if (!IsO32) |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4857 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4858 | } |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4859 | |
| 4860 | return ABIArgInfo::getIndirect(0); |
| 4861 | } |
| 4862 | |
| 4863 | // Treat an enum type as its underlying type. |
| 4864 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4865 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4866 | |
| 4867 | return (RetTy->isPromotableIntegerType() ? |
| 4868 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4869 | } |
| 4870 | |
| 4871 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4872 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 4873 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 4874 | |
| 4875 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4876 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4877 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4878 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4879 | it != ie; ++it) |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4880 | it->info = classifyArgumentType(it->type, Offset); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4884 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4885 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4886 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4887 | |
| 4888 | CGBuilderTy &Builder = CGF.Builder; |
| 4889 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4890 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4891 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4892 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4893 | llvm::Value *AddrTyped; |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 4894 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4895 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4896 | |
| 4897 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4898 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 4899 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 4900 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 4901 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4902 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 4903 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 4904 | } |
| 4905 | else |
| 4906 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4907 | |
| 4908 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4909 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4910 | uint64_t Offset = |
| 4911 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 4912 | llvm::Value *NextAddr = |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4913 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4914 | "ap.next"); |
| 4915 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4916 | |
| 4917 | return AddrTyped; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4920 | bool |
| 4921 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4922 | llvm::Value *Address) const { |
| 4923 | // This information comes from gcc's implementation, which seems to |
| 4924 | // as canonical as it gets. |
| 4925 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4926 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 4927 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4928 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4929 | |
| 4930 | // 0-31 are the general purpose registers, $0 - $31. |
| 4931 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 4932 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 4933 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4934 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4935 | |
| 4936 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 4937 | // They are one bit wide and ignored here. |
| 4938 | |
| 4939 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 4940 | // (coprocessor 1 is the FP unit) |
| 4941 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 4942 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 4943 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4944 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4945 | return false; |
| 4946 | } |
| 4947 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4948 | //===----------------------------------------------------------------------===// |
| 4949 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 4950 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 4951 | // handling. |
| 4952 | //===----------------------------------------------------------------------===// |
| 4953 | |
| 4954 | namespace { |
| 4955 | |
| 4956 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4957 | public: |
| 4958 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 4959 | : DefaultTargetCodeGenInfo(CGT) {} |
| 4960 | |
| 4961 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4962 | CodeGen::CodeGenModule &M) const; |
| 4963 | }; |
| 4964 | |
| 4965 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4966 | llvm::GlobalValue *GV, |
| 4967 | CodeGen::CodeGenModule &M) const { |
| 4968 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4969 | if (!FD) return; |
| 4970 | |
| 4971 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4972 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4973 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4974 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 4975 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4976 | F->addFnAttr(llvm::Attribute::NoInline); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4977 | |
| 4978 | if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 4979 | |
| 4980 | // Convert the reqd_work_group_size() attributes to metadata. |
| 4981 | llvm::LLVMContext &Context = F->getContext(); |
| 4982 | llvm::NamedMDNode *OpenCLMetadata = |
| 4983 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 4984 | |
| 4985 | SmallVector<llvm::Value*, 5> Operands; |
| 4986 | Operands.push_back(F); |
| 4987 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4988 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4989 | llvm::APInt(32, |
| 4990 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); |
| 4991 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4992 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4993 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4994 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4995 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4996 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); |
| 4997 | |
| 4998 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 4999 | // for implementing the work_group_size_hint attr later. Currently |
| 5000 | // always true as the hint is not yet implemented. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5001 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5002 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5003 | } |
| 5004 | } |
| 5005 | } |
| 5006 | } |
| 5007 | |
| 5008 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5009 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5010 | //===----------------------------------------------------------------------===// |
| 5011 | // Hexagon ABI Implementation |
| 5012 | //===----------------------------------------------------------------------===// |
| 5013 | |
| 5014 | namespace { |
| 5015 | |
| 5016 | class HexagonABIInfo : public ABIInfo { |
| 5017 | |
| 5018 | |
| 5019 | public: |
| 5020 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5021 | |
| 5022 | private: |
| 5023 | |
| 5024 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5025 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5026 | |
| 5027 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 5028 | |
| 5029 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5030 | CodeGenFunction &CGF) const; |
| 5031 | }; |
| 5032 | |
| 5033 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5034 | public: |
| 5035 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5036 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5037 | |
| 5038 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 5039 | return 29; |
| 5040 | } |
| 5041 | }; |
| 5042 | |
| 5043 | } |
| 5044 | |
| 5045 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5046 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 5047 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5048 | it != ie; ++it) |
| 5049 | it->info = classifyArgumentType(it->type); |
| 5050 | } |
| 5051 | |
| 5052 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5053 | if (!isAggregateTypeForABI(Ty)) { |
| 5054 | // Treat an enum type as its underlying type. |
| 5055 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5056 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5057 | |
| 5058 | return (Ty->isPromotableIntegerType() ? |
| 5059 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5060 | } |
| 5061 | |
| 5062 | // Ignore empty records. |
| 5063 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5064 | return ABIArgInfo::getIgnore(); |
| 5065 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5066 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 5067 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5068 | |
| 5069 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5070 | if (Size > 64) |
| 5071 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5072 | // Pass in the smallest viable integer type. |
| 5073 | else if (Size > 32) |
| 5074 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5075 | else if (Size > 16) |
| 5076 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5077 | else if (Size > 8) |
| 5078 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5079 | else |
| 5080 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5081 | } |
| 5082 | |
| 5083 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5084 | if (RetTy->isVoidType()) |
| 5085 | return ABIArgInfo::getIgnore(); |
| 5086 | |
| 5087 | // Large vector types should be returned via memory. |
| 5088 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5089 | return ABIArgInfo::getIndirect(0); |
| 5090 | |
| 5091 | if (!isAggregateTypeForABI(RetTy)) { |
| 5092 | // Treat an enum type as its underlying type. |
| 5093 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5094 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5095 | |
| 5096 | return (RetTy->isPromotableIntegerType() ? |
| 5097 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5098 | } |
| 5099 | |
| 5100 | // Structures with either a non-trivial destructor or a non-trivial |
| 5101 | // copy constructor are always indirect. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5102 | if (isRecordReturnIndirect(RetTy, CGT)) |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5103 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5104 | |
| 5105 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5106 | return ABIArgInfo::getIgnore(); |
| 5107 | |
| 5108 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5109 | // are returned indirectly. |
| 5110 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5111 | if (Size <= 64) { |
| 5112 | // Return in the smallest viable integer type. |
| 5113 | if (Size <= 8) |
| 5114 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5115 | if (Size <= 16) |
| 5116 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5117 | if (Size <= 32) |
| 5118 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5119 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5120 | } |
| 5121 | |
| 5122 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5123 | } |
| 5124 | |
| 5125 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5126 | CodeGenFunction &CGF) const { |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5127 | // FIXME: Need to handle alignment |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5128 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5129 | |
| 5130 | CGBuilderTy &Builder = CGF.Builder; |
| 5131 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5132 | "ap"); |
| 5133 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5134 | llvm::Type *PTy = |
| 5135 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5136 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5137 | |
| 5138 | uint64_t Offset = |
| 5139 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5140 | llvm::Value *NextAddr = |
| 5141 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5142 | "ap.next"); |
| 5143 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5144 | |
| 5145 | return AddrTyped; |
| 5146 | } |
| 5147 | |
| 5148 | |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5149 | //===----------------------------------------------------------------------===// |
| 5150 | // SPARC v9 ABI Implementation. |
| 5151 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5152 | // |
| 5153 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5154 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5155 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5156 | // |
| 5157 | // One case requires special care: |
| 5158 | // |
| 5159 | // struct mixed { |
| 5160 | // int i; |
| 5161 | // float f; |
| 5162 | // }; |
| 5163 | // |
| 5164 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5165 | // parameter array, but the int is passed in an integer register, and the float |
| 5166 | // is passed in a floating point register. This is represented as two arguments |
| 5167 | // with the LLVM IR inreg attribute: |
| 5168 | // |
| 5169 | // declare void f(i32 inreg %i, float inreg %f) |
| 5170 | // |
| 5171 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5172 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5173 | // bytes. |
| 5174 | // |
| 5175 | namespace { |
| 5176 | class SparcV9ABIInfo : public ABIInfo { |
| 5177 | public: |
| 5178 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5179 | |
| 5180 | private: |
| 5181 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
| 5182 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 5183 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5184 | CodeGenFunction &CGF) const; |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5185 | |
| 5186 | // Coercion type builder for structs passed in registers. The coercion type |
| 5187 | // serves two purposes: |
| 5188 | // |
| 5189 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5190 | // in registers. |
| 5191 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5192 | // code generator knows to pass them in floating point registers. |
| 5193 | // |
| 5194 | // We also compute the InReg flag which indicates that the struct contains |
| 5195 | // aligned 32-bit floats. |
| 5196 | // |
| 5197 | struct CoerceBuilder { |
| 5198 | llvm::LLVMContext &Context; |
| 5199 | const llvm::DataLayout &DL; |
| 5200 | SmallVector<llvm::Type*, 8> Elems; |
| 5201 | uint64_t Size; |
| 5202 | bool InReg; |
| 5203 | |
| 5204 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5205 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5206 | |
| 5207 | // Pad Elems with integers until Size is ToSize. |
| 5208 | void pad(uint64_t ToSize) { |
| 5209 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5210 | if (ToSize == Size) |
| 5211 | return; |
| 5212 | |
| 5213 | // Finish the current 64-bit word. |
| 5214 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5215 | if (Aligned > Size && Aligned <= ToSize) { |
| 5216 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5217 | Size = Aligned; |
| 5218 | } |
| 5219 | |
| 5220 | // Add whole 64-bit words. |
| 5221 | while (Size + 64 <= ToSize) { |
| 5222 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5223 | Size += 64; |
| 5224 | } |
| 5225 | |
| 5226 | // Final in-word padding. |
| 5227 | if (Size < ToSize) { |
| 5228 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5229 | Size = ToSize; |
| 5230 | } |
| 5231 | } |
| 5232 | |
| 5233 | // Add a floating point element at Offset. |
| 5234 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5235 | // Unaligned floats are treated as integers. |
| 5236 | if (Offset % Bits) |
| 5237 | return; |
| 5238 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5239 | if (Bits < 64) |
| 5240 | InReg = true; |
| 5241 | pad(Offset); |
| 5242 | Elems.push_back(Ty); |
| 5243 | Size = Offset + Bits; |
| 5244 | } |
| 5245 | |
| 5246 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5247 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5248 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5249 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5250 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5251 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5252 | switch (ElemTy->getTypeID()) { |
| 5253 | case llvm::Type::StructTyID: |
| 5254 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5255 | break; |
| 5256 | case llvm::Type::FloatTyID: |
| 5257 | addFloat(ElemOffset, ElemTy, 32); |
| 5258 | break; |
| 5259 | case llvm::Type::DoubleTyID: |
| 5260 | addFloat(ElemOffset, ElemTy, 64); |
| 5261 | break; |
| 5262 | case llvm::Type::FP128TyID: |
| 5263 | addFloat(ElemOffset, ElemTy, 128); |
| 5264 | break; |
| 5265 | case llvm::Type::PointerTyID: |
| 5266 | if (ElemOffset % 64 == 0) { |
| 5267 | pad(ElemOffset); |
| 5268 | Elems.push_back(ElemTy); |
| 5269 | Size += 64; |
| 5270 | } |
| 5271 | break; |
| 5272 | default: |
| 5273 | break; |
| 5274 | } |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | // Check if Ty is a usable substitute for the coercion type. |
| 5279 | bool isUsableType(llvm::StructType *Ty) const { |
| 5280 | if (Ty->getNumElements() != Elems.size()) |
| 5281 | return false; |
| 5282 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5283 | if (Elems[i] != Ty->getElementType(i)) |
| 5284 | return false; |
| 5285 | return true; |
| 5286 | } |
| 5287 | |
| 5288 | // Get the coercion type as a literal struct type. |
| 5289 | llvm::Type *getType() const { |
| 5290 | if (Elems.size() == 1) |
| 5291 | return Elems.front(); |
| 5292 | else |
| 5293 | return llvm::StructType::get(Context, Elems); |
| 5294 | } |
| 5295 | }; |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5296 | }; |
| 5297 | } // end anonymous namespace |
| 5298 | |
| 5299 | ABIArgInfo |
| 5300 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5301 | if (Ty->isVoidType()) |
| 5302 | return ABIArgInfo::getIgnore(); |
| 5303 | |
| 5304 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5305 | |
| 5306 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5307 | // pointer / sret pointer. |
| 5308 | if (Size > SizeLimit) |
| 5309 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5310 | |
| 5311 | // Treat an enum type as its underlying type. |
| 5312 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5313 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5314 | |
| 5315 | // Integer types smaller than a register are extended. |
| 5316 | if (Size < 64 && Ty->isIntegerType()) |
| 5317 | return ABIArgInfo::getExtend(); |
| 5318 | |
| 5319 | // Other non-aggregates go in registers. |
| 5320 | if (!isAggregateTypeForABI(Ty)) |
| 5321 | return ABIArgInfo::getDirect(); |
| 5322 | |
| 5323 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5324 | // Build a coercion type from the LLVM struct type. |
| 5325 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5326 | if (!StrTy) |
| 5327 | return ABIArgInfo::getDirect(); |
| 5328 | |
| 5329 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5330 | CB.addStruct(0, StrTy); |
| 5331 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5332 | |
| 5333 | // Try to use the original type for coercion. |
| 5334 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5335 | |
| 5336 | if (CB.InReg) |
| 5337 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5338 | else |
| 5339 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5343 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5344 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 5345 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5346 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5347 | AI.setCoerceToType(ArgTy); |
| 5348 | |
| 5349 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 5350 | CGBuilderTy &Builder = CGF.Builder; |
| 5351 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5352 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5353 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 5354 | llvm::Value *ArgAddr; |
| 5355 | unsigned Stride; |
| 5356 | |
| 5357 | switch (AI.getKind()) { |
| 5358 | case ABIArgInfo::Expand: |
| 5359 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5360 | |
| 5361 | case ABIArgInfo::Extend: |
| 5362 | Stride = 8; |
| 5363 | ArgAddr = Builder |
| 5364 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 5365 | "extend"); |
| 5366 | break; |
| 5367 | |
| 5368 | case ABIArgInfo::Direct: |
| 5369 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 5370 | ArgAddr = Addr; |
| 5371 | break; |
| 5372 | |
| 5373 | case ABIArgInfo::Indirect: |
| 5374 | Stride = 8; |
| 5375 | ArgAddr = Builder.CreateBitCast(Addr, |
| 5376 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 5377 | "indirect"); |
| 5378 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 5379 | break; |
| 5380 | |
| 5381 | case ABIArgInfo::Ignore: |
| 5382 | return llvm::UndefValue::get(ArgPtrTy); |
| 5383 | } |
| 5384 | |
| 5385 | // Update VAList. |
| 5386 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 5387 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 5388 | |
| 5389 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5393 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
| 5394 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5395 | it != ie; ++it) |
| 5396 | it->info = classifyType(it->type, 16 * 8); |
| 5397 | } |
| 5398 | |
| 5399 | namespace { |
| 5400 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5401 | public: |
| 5402 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5403 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
| 5404 | }; |
| 5405 | } // end anonymous namespace |
| 5406 | |
| 5407 | |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5408 | //===----------------------------------------------------------------------===// |
| 5409 | // Xcore ABI Implementation |
| 5410 | //===----------------------------------------------------------------------===// |
| 5411 | namespace { |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5412 | class XCoreABIInfo : public DefaultABIInfo { |
| 5413 | public: |
| 5414 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 5415 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5416 | CodeGenFunction &CGF) const; |
| 5417 | }; |
| 5418 | |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5419 | class XcoreTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5420 | public: |
| 5421 | XcoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5422 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5423 | }; |
| 5424 | } // end anonymous namespace |
| 5425 | |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5426 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5427 | CodeGenFunction &CGF) const { |
| 5428 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5429 | CGBuilderTy &Builder = CGF.Builder; |
| 5430 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5431 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5432 | AI.setCoerceToType(ArgTy); |
| 5433 | |
| 5434 | // handle the VAList |
| 5435 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 5436 | CGF.Int8PtrPtrTy); |
| 5437 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
| 5438 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, 4); |
| 5439 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 5440 | |
| 5441 | // handle the argument |
| 5442 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 5443 | switch (AI.getKind()) { |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5444 | case ABIArgInfo::Expand: |
| 5445 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5446 | case ABIArgInfo::Ignore: |
| 5447 | return llvm::UndefValue::get(ArgPtrTy); |
| 5448 | case ABIArgInfo::Extend: |
| 5449 | case ABIArgInfo::Direct: |
| 5450 | return Builder.CreatePointerCast(AP, ArgPtrTy); |
| 5451 | case ABIArgInfo::Indirect: |
| 5452 | llvm::Value *ArgAddr; |
| 5453 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 5454 | ArgAddr = Builder.CreateLoad(ArgAddr); |
| 5455 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 5456 | } |
Alexey Samsonov | acfea84 | 2013-08-19 13:07:12 +0000 | [diff] [blame] | 5457 | llvm_unreachable("Unknown ABI kind"); |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 5458 | } |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5459 | |
| 5460 | //===----------------------------------------------------------------------===// |
| 5461 | // Driver code |
| 5462 | //===----------------------------------------------------------------------===// |
| 5463 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5464 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5465 | if (TheTargetCodeGenInfo) |
| 5466 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5467 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5468 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 5469 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5470 | default: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5471 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5472 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 5473 | case llvm::Triple::le32: |
| 5474 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5475 | case llvm::Triple::mips: |
| 5476 | case llvm::Triple::mipsel: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5477 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5478 | |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 5479 | case llvm::Triple::mips64: |
| 5480 | case llvm::Triple::mips64el: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5481 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 5482 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5483 | case llvm::Triple::aarch64: |
| 5484 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 5485 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5486 | case llvm::Triple::arm: |
| 5487 | case llvm::Triple::thumb: |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5488 | { |
| 5489 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5490 | if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5491 | Kind = ARMABIInfo::APCS; |
David Tweed | b16abb1 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 5492 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5493 | (CodeGenOpts.FloatABI != "soft" && |
| 5494 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5495 | Kind = ARMABIInfo::AAPCS_VFP; |
| 5496 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5497 | switch (Triple.getOS()) { |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5498 | case llvm::Triple::NaCl: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5499 | return *(TheTargetCodeGenInfo = |
| 5500 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 5501 | default: |
| 5502 | return *(TheTargetCodeGenInfo = |
| 5503 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 5504 | } |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5505 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5506 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5507 | case llvm::Triple::ppc: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5508 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 5509 | case llvm::Triple::ppc64: |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 5510 | if (Triple.isOSBinFormatELF()) |
| 5511 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 5512 | else |
| 5513 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Bill Schmidt | ea7fb0c | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 5514 | case llvm::Triple::ppc64le: |
| 5515 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
| 5516 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5517 | |
Peter Collingbourne | edb66f3 | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 5518 | case llvm::Triple::nvptx: |
| 5519 | case llvm::Triple::nvptx64: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5520 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5521 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5522 | case llvm::Triple::msp430: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5523 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5524 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5525 | case llvm::Triple::systemz: |
| 5526 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 5527 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5528 | case llvm::Triple::tce: |
| 5529 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 5530 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5531 | case llvm::Triple::x86: { |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5532 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 5533 | bool IsSmallStructInRegABI = |
| 5534 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
| 5535 | bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32); |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 5536 | |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5537 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 5538 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 5539 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5540 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 5541 | IsWin32FloatStructABI, |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 5542 | CodeGenOpts.NumRegisterParameters)); |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5543 | } else { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5544 | return *(TheTargetCodeGenInfo = |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5545 | new X86_32TargetCodeGenInfo(Types, |
| 5546 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 5547 | IsWin32FloatStructABI, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 5548 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5549 | } |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5550 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5551 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5552 | case llvm::Triple::x86_64: { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5553 | bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5554 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5555 | switch (Triple.getOS()) { |
| 5556 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 0aa2057 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 5557 | case llvm::Triple::MinGW32: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5558 | case llvm::Triple::Cygwin: |
| 5559 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5560 | case llvm::Triple::NaCl: |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5561 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 5562 | HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5563 | default: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5564 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 5565 | HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5566 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5567 | } |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5568 | case llvm::Triple::hexagon: |
| 5569 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5570 | case llvm::Triple::sparcv9: |
| 5571 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 5572 | case llvm::Triple::xcore: |
| 5573 | return *(TheTargetCodeGenInfo = new XcoreTargetCodeGenInfo(Types)); |
| 5574 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5575 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5576 | } |