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; |
| 51 | return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD); |
| 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; |
| 67 | return CGT.CGM.getCXXABI().getRecordArgABI(RD); |
| 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 | |
| 578 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 579 | CodeGen::CodeGenModule &CGM) const; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 580 | |
| 581 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 582 | // Darwin uses different dwarf register numbers for EH. |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 583 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 584 | return 4; |
| 585 | } |
| 586 | |
| 587 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 588 | llvm::Value *Address) const; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 589 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 590 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 591 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 592 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 593 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 594 | } |
| 595 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 596 | }; |
| 597 | |
| 598 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 599 | |
| 600 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 601 | /// passed in a register (for the Darwin ABI). |
| 602 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 603 | ASTContext &Context, |
| 604 | unsigned callingConvention) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 605 | uint64_t Size = Context.getTypeSize(Ty); |
| 606 | |
| 607 | // Type must be register sized. |
| 608 | if (!isRegisterSize(Size)) |
| 609 | return false; |
| 610 | |
| 611 | if (Ty->isVectorType()) { |
| 612 | // 64- and 128- bit vectors inside structures are not returned in |
| 613 | // registers. |
| 614 | if (Size == 64 || Size == 128) |
| 615 | return false; |
| 616 | |
| 617 | return true; |
| 618 | } |
| 619 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 620 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 621 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 622 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 623 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 624 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 625 | return true; |
| 626 | |
| 627 | // Arrays are treated like records. |
| 628 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 629 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
| 630 | callingConvention); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 631 | |
| 632 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 633 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 634 | if (!RT) return false; |
| 635 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 636 | // FIXME: Traverse bases here too. |
| 637 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 638 | // For thiscall conventions, structures will never be returned in |
| 639 | // a register. This is for compatibility with the MSVC ABI |
| 640 | if (callingConvention == llvm::CallingConv::X86_ThisCall && |
| 641 | RT->isStructureType()) { |
| 642 | return false; |
| 643 | } |
| 644 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 645 | // Structure types are passed in register if all fields would be |
| 646 | // passed in a register. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 647 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 648 | e = RT->getDecl()->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 649 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | |
| 651 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 652 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 653 | continue; |
| 654 | |
| 655 | // Check fields recursively. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 656 | if (!shouldReturnTypeInRegister(FD->getType(), Context, |
| 657 | callingConvention)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 658 | return false; |
| 659 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 660 | return true; |
| 661 | } |
| 662 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 663 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 664 | unsigned callingConvention) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 665 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 666 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 667 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 668 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 669 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 670 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 671 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 672 | |
| 673 | // 128-bit vectors are a special case; they are returned in |
| 674 | // registers and we need to make sure to pick a type the LLVM |
| 675 | // backend will like. |
| 676 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 677 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 678 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 679 | |
| 680 | // Always return in register if it fits in a general purpose |
| 681 | // register, or if it is 64 bits and has a single element. |
| 682 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 683 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 684 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 685 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 686 | |
| 687 | return ABIArgInfo::getIndirect(0); |
| 688 | } |
| 689 | |
| 690 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 691 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 692 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 693 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 694 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 695 | if (isRecordReturnIndirect(RT, CGT)) |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 696 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 698 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 699 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 700 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 701 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 702 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 703 | // If specified, structs and unions are always indirect. |
| 704 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 705 | return ABIArgInfo::getIndirect(0); |
| 706 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 707 | // Small structures which are register sized are generally returned |
| 708 | // in a register. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 709 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), |
| 710 | callingConvention)) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 711 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 712 | |
| 713 | // As a special-case, if the struct is a "single-element" struct, and |
| 714 | // the field is of type "float" or "double", return it in a |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 715 | // floating-point register. (MSVC does not apply this special case.) |
| 716 | // We apply a similar transformation for pointer types to improve the |
| 717 | // quality of the generated IR. |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 718 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 719 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 720 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 721 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 722 | |
| 723 | // FIXME: We should be able to narrow this integer in cases with dead |
| 724 | // padding. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 725 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 729 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 730 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 731 | // Treat an enum type as its underlying type. |
| 732 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 733 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 734 | |
| 735 | return (RetTy->isPromotableIntegerType() ? |
| 736 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 739 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 740 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 741 | } |
| 742 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 743 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 744 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 745 | if (!RT) |
| 746 | return 0; |
| 747 | const RecordDecl *RD = RT->getDecl(); |
| 748 | |
| 749 | // If this is a C++ record, check the bases first. |
| 750 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 751 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 752 | e = CXXRD->bases_end(); i != e; ++i) |
| 753 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 754 | return false; |
| 755 | |
| 756 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 757 | i != e; ++i) { |
| 758 | QualType FT = i->getType(); |
| 759 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 760 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 761 | return true; |
| 762 | |
| 763 | if (isRecordWithSSEVectorType(Context, FT)) |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | return false; |
| 768 | } |
| 769 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 770 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 771 | unsigned Align) const { |
| 772 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 773 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 774 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 775 | return 0; // Use default alignment. |
| 776 | |
| 777 | // On non-Darwin, the stack type alignment is always 4. |
| 778 | if (!IsDarwinVectorABI) { |
| 779 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 780 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 781 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 782 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 783 | // 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] | 784 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 785 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 786 | return 16; |
| 787 | |
| 788 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 791 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 792 | unsigned &FreeRegs) const { |
| 793 | if (!ByVal) { |
| 794 | if (FreeRegs) { |
| 795 | --FreeRegs; // Non byval indirects just use one pointer. |
| 796 | return ABIArgInfo::getIndirectInReg(0, false); |
| 797 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 798 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 799 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 800 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 801 | // Compute the byval alignment. |
| 802 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 803 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 804 | if (StackAlign == 0) |
Chris Lattner | de92d73 | 2011-05-22 23:35:00 +0000 | [diff] [blame] | 805 | return ABIArgInfo::getIndirect(4); |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 806 | |
| 807 | // If the stack alignment is less than the type alignment, realign the |
| 808 | // argument. |
| 809 | if (StackAlign < TypeAlign) |
| 810 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 811 | /*Realign=*/true); |
| 812 | |
| 813 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 816 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 817 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 818 | if (!T) |
| 819 | T = Ty.getTypePtr(); |
| 820 | |
| 821 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 822 | BuiltinType::Kind K = BT->getKind(); |
| 823 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 824 | return Float; |
| 825 | } |
| 826 | return Integer; |
| 827 | } |
| 828 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 829 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs, |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 830 | bool IsFastCall, bool &NeedsPadding) const { |
| 831 | NeedsPadding = false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 832 | Class C = classify(Ty); |
| 833 | if (C == Float) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 834 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 835 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 836 | unsigned Size = getContext().getTypeSize(Ty); |
| 837 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | 5f14fcb | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 838 | |
| 839 | if (SizeInRegs == 0) |
| 840 | return false; |
| 841 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 842 | if (SizeInRegs > FreeRegs) { |
| 843 | FreeRegs = 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 844 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 845 | } |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 846 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 847 | FreeRegs -= SizeInRegs; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 848 | |
| 849 | if (IsFastCall) { |
| 850 | if (Size > 32) |
| 851 | return false; |
| 852 | |
| 853 | if (Ty->isIntegralOrEnumerationType()) |
| 854 | return true; |
| 855 | |
| 856 | if (Ty->isPointerType()) |
| 857 | return true; |
| 858 | |
| 859 | if (Ty->isReferenceType()) |
| 860 | return true; |
| 861 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 862 | if (FreeRegs) |
| 863 | NeedsPadding = true; |
| 864 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 868 | return true; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 871 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 872 | unsigned &FreeRegs, |
| 873 | bool IsFastCall) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 874 | // FIXME: Set alignment on indirect arguments. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 875 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 876 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 877 | if (IsWin32StructABI) |
| 878 | return getIndirectResult(Ty, true, FreeRegs); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 879 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 880 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) |
| 881 | return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs); |
| 882 | |
| 883 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 884 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 885 | return getIndirectResult(Ty, true, FreeRegs); |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 886 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 887 | |
Eli Friedman | 5a4d352 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 888 | // Ignore empty structs/unions. |
Eli Friedman | 5a1ac89 | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 889 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 890 | return ABIArgInfo::getIgnore(); |
| 891 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 892 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 893 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 894 | bool NeedsPadding; |
| 895 | if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) { |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 896 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 897 | SmallVector<llvm::Type*, 3> Elements; |
| 898 | for (unsigned I = 0; I < SizeInRegs; ++I) |
| 899 | Elements.push_back(Int32); |
| 900 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 901 | return ABIArgInfo::getDirectInReg(Result); |
| 902 | } |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 903 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 904 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 905 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 906 | // of those arguments will match the struct. This is important because the |
| 907 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 908 | // optimizations. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 909 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 910 | canExpandIndirectArgument(Ty, getContext())) |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 911 | return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 912 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 913 | return getIndirectResult(Ty, true, FreeRegs); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 916 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 917 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 918 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 919 | if (IsDarwinVectorABI) { |
| 920 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 921 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 922 | (Size == 64 && VT->getNumElements() == 1)) |
| 923 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 924 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 925 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 926 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 927 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 928 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 929 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 930 | return ABIArgInfo::getDirect(); |
| 931 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 932 | |
| 933 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 934 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 935 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 936 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 937 | bool NeedsPadding; |
| 938 | bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 939 | |
| 940 | if (Ty->isPromotableIntegerType()) { |
| 941 | if (InReg) |
| 942 | return ABIArgInfo::getExtendInReg(); |
| 943 | return ABIArgInfo::getExtend(); |
| 944 | } |
| 945 | if (InReg) |
| 946 | return ABIArgInfo::getDirectInReg(); |
| 947 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 950 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 951 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), |
| 952 | FI.getCallingConvention()); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 953 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 954 | unsigned CC = FI.getCallingConvention(); |
| 955 | bool IsFastCall = CC == llvm::CallingConv::X86_FastCall; |
| 956 | unsigned FreeRegs; |
| 957 | if (IsFastCall) |
| 958 | FreeRegs = 2; |
| 959 | else if (FI.getHasRegParm()) |
| 960 | FreeRegs = FI.getRegParm(); |
| 961 | else |
| 962 | FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 963 | |
| 964 | // If the return value is indirect, then the hidden argument is consuming one |
| 965 | // integer register. |
| 966 | if (FI.getReturnInfo().isIndirect() && FreeRegs) { |
| 967 | --FreeRegs; |
| 968 | ABIArgInfo &Old = FI.getReturnInfo(); |
| 969 | Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(), |
| 970 | Old.getIndirectByVal(), |
| 971 | Old.getIndirectRealign()); |
| 972 | } |
| 973 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 974 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 975 | it != ie; ++it) |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 976 | it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall); |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 979 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 980 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 981 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 982 | |
| 983 | CGBuilderTy &Builder = CGF.Builder; |
| 984 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 985 | "ap"); |
| 986 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 987 | |
| 988 | // Compute if the address needs to be aligned |
| 989 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 990 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 991 | Align = std::max(Align, 4U); |
| 992 | if (Align > 4) { |
| 993 | // addr = (addr + align - 1) & -align; |
| 994 | llvm::Value *Offset = |
| 995 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 996 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 997 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 998 | CGF.Int32Ty); |
| 999 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1000 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1001 | Addr->getType(), |
| 1002 | "ap.cur.aligned"); |
| 1003 | } |
| 1004 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1005 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1006 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1007 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1008 | |
| 1009 | uint64_t Offset = |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1010 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1011 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1012 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1013 | "ap.next"); |
| 1014 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1015 | |
| 1016 | return AddrTyped; |
| 1017 | } |
| 1018 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1019 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1020 | llvm::GlobalValue *GV, |
| 1021 | CodeGen::CodeGenModule &CGM) const { |
| 1022 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1023 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1024 | // Get the LLVM function. |
| 1025 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1026 | |
| 1027 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1028 | llvm::AttrBuilder B; |
Bill Wendling | e91e9ec | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1029 | B.addStackAlignmentAttr(16); |
Bill Wendling | 909b6de | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1030 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1031 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1032 | llvm::AttributeSet::FunctionIndex, |
| 1033 | B)); |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1038 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1039 | CodeGen::CodeGenFunction &CGF, |
| 1040 | llvm::Value *Address) const { |
| 1041 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1042 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1043 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1044 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1045 | // 0-7 are the eight integer registers; the order is different |
| 1046 | // on Darwin (for EH), but the range is the same. |
| 1047 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1048 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1049 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1050 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1051 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1052 | // These have size 16, which is sizeof(long double) on |
| 1053 | // platforms with 8-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1054 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1055 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1056 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1057 | } else { |
| 1058 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1059 | // reason. |
| 1060 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1061 | |
| 1062 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1063 | // These have size 12, which is sizeof(long double) on |
| 1064 | // platforms with 4-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1065 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1066 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1067 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1068 | |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1072 | //===----------------------------------------------------------------------===// |
| 1073 | // X86-64 ABI Implementation |
| 1074 | //===----------------------------------------------------------------------===// |
| 1075 | |
| 1076 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1077 | namespace { |
| 1078 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1079 | class X86_64ABIInfo : public ABIInfo { |
| 1080 | enum Class { |
| 1081 | Integer = 0, |
| 1082 | SSE, |
| 1083 | SSEUp, |
| 1084 | X87, |
| 1085 | X87Up, |
| 1086 | ComplexX87, |
| 1087 | NoClass, |
| 1088 | Memory |
| 1089 | }; |
| 1090 | |
| 1091 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1092 | /// |
| 1093 | /// Merge an accumulating classification \arg Accum with a field |
| 1094 | /// classification \arg Field. |
| 1095 | /// |
| 1096 | /// \param Accum - The accumulating classification. This should |
| 1097 | /// always be either NoClass or the result of a previous merge |
| 1098 | /// call. In addition, this should never be Memory (the caller |
| 1099 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1100 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1102 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1103 | /// |
| 1104 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1105 | /// final MEMORY or SSE classes when necessary. |
| 1106 | /// |
| 1107 | /// \param AggregateSize - The size of the current aggregate in |
| 1108 | /// the classification process. |
| 1109 | /// |
| 1110 | /// \param Lo - The classification for the parts of the type |
| 1111 | /// residing in the low word of the containing object. |
| 1112 | /// |
| 1113 | /// \param Hi - The classification for the parts of the type |
| 1114 | /// residing in the higher words of the containing object. |
| 1115 | /// |
| 1116 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1117 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1118 | /// classify - Determine the x86_64 register classes in which the |
| 1119 | /// given type T should be passed. |
| 1120 | /// |
| 1121 | /// \param Lo - The classification for the parts of the type |
| 1122 | /// residing in the low word of the containing object. |
| 1123 | /// |
| 1124 | /// \param Hi - The classification for the parts of the type |
| 1125 | /// residing in the high word of the containing object. |
| 1126 | /// |
| 1127 | /// \param OffsetBase - The bit offset of this type in the |
| 1128 | /// containing object. Some parameters are classified different |
| 1129 | /// depending on whether they straddle an eightbyte boundary. |
| 1130 | /// |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1131 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1132 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1133 | /// |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1134 | /// If a word is unused its result will be NoClass; if a type should |
| 1135 | /// be passed in Memory then at least the classification of \arg Lo |
| 1136 | /// will be Memory. |
| 1137 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1138 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1139 | /// |
| 1140 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1141 | /// also be ComplexX87. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1142 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1143 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1144 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1145 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1146 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1147 | unsigned IROffset, QualType SourceTy, |
| 1148 | unsigned SourceOffset) const; |
| 1149 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1150 | unsigned IROffset, QualType SourceTy, |
| 1151 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1152 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1153 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1154 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1155 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1156 | |
| 1157 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1158 | /// such that the argument will be passed in memory. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1159 | /// |
| 1160 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1161 | /// available. |
| 1162 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1164 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1165 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1166 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1167 | unsigned freeIntRegs, |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1168 | unsigned &neededInt, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1169 | unsigned &neededSSE, |
| 1170 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1171 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1172 | bool IsIllegalVectorType(QualType Ty) const; |
| 1173 | |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1174 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1175 | /// unfortunately in ways that were not always consistent with |
| 1176 | /// certain previous compilers. In particular, platforms which |
| 1177 | /// required strict binary compatibility with older versions of GCC |
| 1178 | /// may need to exempt themselves. |
| 1179 | bool honorsRevision0_98() const { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1180 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1183 | bool HasAVX; |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1184 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1185 | // 64-bit hardware. |
| 1186 | bool Has64BitPointers; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1187 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1188 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1189 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1190 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 90da80c | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1191 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1192 | } |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1193 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1194 | bool isPassedUsingAVXType(QualType type) const { |
| 1195 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1196 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1197 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1198 | /*isNamedArg*/true); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1199 | if (info.isDirect()) { |
| 1200 | llvm::Type *ty = info.getCoerceToType(); |
| 1201 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1202 | return (vectorTy->getBitWidth() > 128); |
| 1203 | } |
| 1204 | return false; |
| 1205 | } |
| 1206 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1207 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1208 | |
| 1209 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1210 | CodeGenFunction &CGF) const; |
| 1211 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1213 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1214 | class WinX86_64ABIInfo : public ABIInfo { |
| 1215 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1216 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1218 | public: |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1219 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1220 | |
| 1221 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1222 | |
| 1223 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1224 | CodeGenFunction &CGF) const; |
| 1225 | }; |
| 1226 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1227 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1228 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1229 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1230 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1231 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1232 | const X86_64ABIInfo &getABIInfo() const { |
| 1233 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1234 | } |
| 1235 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1236 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1237 | return 7; |
| 1238 | } |
| 1239 | |
| 1240 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1241 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1242 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1243 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1244 | // 0-15 are the 16 integer registers. |
| 1245 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1246 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1249 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1250 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1251 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1252 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1253 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1254 | } |
| 1255 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1256 | bool isNoProtoCallVariadic(const CallArgList &args, |
| 1257 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1258 | // The default CC on x86-64 sets %al to the number of SSA |
| 1259 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1260 | // function, so we override the default behavior. However, don't do |
Eli Friedman | 68805fe | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1261 | // that when AVX types are involved: the ABI explicitly states it is |
| 1262 | // undefined, and it doesn't work in practice because of how the ABI |
| 1263 | // defines varargs anyway. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1264 | if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) { |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1265 | bool HasAVXType = false; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1266 | for (CallArgList::const_iterator |
| 1267 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1268 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1269 | HasAVXType = true; |
| 1270 | break; |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1271 | } |
| 1272 | } |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1273 | |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1274 | if (!HasAVXType) |
| 1275 | return true; |
| 1276 | } |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1277 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1278 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1281 | }; |
| 1282 | |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1283 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1284 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1285 | // matches the behavior of MSVC. |
| 1286 | std::string ArgStr = Lib; |
| 1287 | if (Lib.size() <= 4 || |
| 1288 | Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) { |
| 1289 | ArgStr += ".lib"; |
| 1290 | } |
| 1291 | return ArgStr; |
| 1292 | } |
| 1293 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1294 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1295 | public: |
| 1296 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned RegParms) |
| 1297 | : X86_32TargetCodeGenInfo(CGT, false, true, true, RegParms) {} |
| 1298 | |
| 1299 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1300 | llvm::SmallString<24> &Opt) const { |
| 1301 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1302 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1303 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1304 | |
| 1305 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1306 | llvm::StringRef Value, |
| 1307 | llvm::SmallString<32> &Opt) const { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1308 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1309 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1310 | }; |
| 1311 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1312 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1313 | public: |
| 1314 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1315 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1316 | |
| 1317 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1318 | return 7; |
| 1319 | } |
| 1320 | |
| 1321 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1322 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1323 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1325 | // 0-15 are the 16 integer registers. |
| 1326 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1327 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1328 | return false; |
| 1329 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1330 | |
| 1331 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1332 | llvm::SmallString<24> &Opt) const { |
| 1333 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1334 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1335 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1336 | |
| 1337 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1338 | llvm::StringRef Value, |
| 1339 | llvm::SmallString<32> &Opt) const { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1340 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1341 | } |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1342 | }; |
| 1343 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1346 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1347 | Class &Hi) const { |
| 1348 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1349 | // |
| 1350 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1351 | // memory. |
| 1352 | // |
| 1353 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1354 | // memory. |
| 1355 | // |
| 1356 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1357 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1358 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1359 | // ABI working for processors that don't support the __m256 type. |
| 1360 | // |
| 1361 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1362 | // |
| 1363 | // Some of these are enforced by the merging logic. Others can arise |
| 1364 | // only with unions; for example: |
| 1365 | // union { _Complex double; unsigned; } |
| 1366 | // |
| 1367 | // Note that clauses (b) and (c) were added in 0.98. |
| 1368 | // |
| 1369 | if (Hi == Memory) |
| 1370 | Lo = Memory; |
| 1371 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1372 | Lo = Memory; |
| 1373 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1374 | Lo = Memory; |
| 1375 | if (Hi == SSEUp && Lo != SSE) |
| 1376 | Hi = SSE; |
| 1377 | } |
| 1378 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1379 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1380 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1381 | // classified recursively so that always two fields are |
| 1382 | // considered. The resulting class is calculated according to |
| 1383 | // the classes of the fields in the eightbyte: |
| 1384 | // |
| 1385 | // (a) If both classes are equal, this is the resulting class. |
| 1386 | // |
| 1387 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1388 | // the other class. |
| 1389 | // |
| 1390 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1391 | // class. |
| 1392 | // |
| 1393 | // (d) If one of the classes is INTEGER, the result is the |
| 1394 | // INTEGER. |
| 1395 | // |
| 1396 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1397 | // MEMORY is used as class. |
| 1398 | // |
| 1399 | // (f) Otherwise class SSE is used. |
| 1400 | |
| 1401 | // Accum should never be memory (we should have returned) or |
| 1402 | // ComplexX87 (because this cannot be passed in a structure). |
| 1403 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1404 | "Invalid accumulated classification during merge."); |
| 1405 | if (Accum == Field || Field == NoClass) |
| 1406 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1407 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1408 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1409 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1410 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1411 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1412 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1413 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1414 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1415 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1416 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1419 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1420 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1421 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1422 | // Class pairs with appropriate constructor methods for the various |
| 1423 | // situations. |
| 1424 | |
| 1425 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1426 | // shouldn't be passed in registers for example, so there is no chance they |
| 1427 | // can straddle an eightbyte. Verify & simplify. |
| 1428 | |
| 1429 | Lo = Hi = NoClass; |
| 1430 | |
| 1431 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1432 | Current = Memory; |
| 1433 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1434 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1435 | BuiltinType::Kind k = BT->getKind(); |
| 1436 | |
| 1437 | if (k == BuiltinType::Void) { |
| 1438 | Current = NoClass; |
| 1439 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1440 | Lo = Integer; |
| 1441 | Hi = Integer; |
| 1442 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1443 | Current = Integer; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1444 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1445 | (k == BuiltinType::LongDouble && |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1446 | getTarget().getTriple().getOS() == llvm::Triple::NaCl)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1447 | Current = SSE; |
| 1448 | } else if (k == BuiltinType::LongDouble) { |
| 1449 | Lo = X87; |
| 1450 | Hi = X87Up; |
| 1451 | } |
| 1452 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1453 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1454 | return; |
| 1455 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1457 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1458 | // Classify the underlying integer type. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1459 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1463 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1464 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1465 | return; |
| 1466 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1468 | if (Ty->isMemberPointerType()) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1469 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1470 | Lo = Hi = Integer; |
| 1471 | else |
| 1472 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1473 | return; |
| 1474 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1476 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1477 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1478 | if (Size == 32) { |
| 1479 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1480 | // float> as integer. |
| 1481 | Current = Integer; |
| 1482 | |
| 1483 | // If this type crosses an eightbyte boundary, it should be |
| 1484 | // split. |
| 1485 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1486 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1487 | if (EB_Real != EB_Imag) |
| 1488 | Hi = Lo; |
| 1489 | } else if (Size == 64) { |
| 1490 | // gcc passes <1 x double> in memory. :( |
| 1491 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1492 | return; |
| 1493 | |
| 1494 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 473f8e7 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1495 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 0fefa41 | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1496 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1497 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1498 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1499 | Current = Integer; |
| 1500 | else |
| 1501 | Current = SSE; |
| 1502 | |
| 1503 | // If this type crosses an eightbyte boundary, it should be |
| 1504 | // split. |
| 1505 | if (OffsetBase && OffsetBase != 64) |
| 1506 | Hi = Lo; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1507 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1508 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1509 | // least significant one belongs to class SSE and all the others to class |
| 1510 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1511 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1512 | // This design isn't correct for 256-bits, but since there're no cases |
| 1513 | // where the upper parts would need to be inspected, avoid adding |
| 1514 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1515 | // |
| 1516 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1517 | // registers if they are "named", i.e. not part of the "..." of a |
| 1518 | // variadic function. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1519 | Lo = SSE; |
| 1520 | Hi = SSEUp; |
| 1521 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1524 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1525 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1526 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1528 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1529 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1530 | if (Size <= 64) |
| 1531 | Current = Integer; |
| 1532 | else if (Size <= 128) |
| 1533 | Lo = Hi = Integer; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1534 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1535 | Current = SSE; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1536 | else if (ET == getContext().DoubleTy || |
| 1537 | (ET == getContext().LongDoubleTy && |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1538 | getTarget().getTriple().getOS() == llvm::Triple::NaCl)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1539 | Lo = Hi = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1540 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1541 | Current = ComplexX87; |
| 1542 | |
| 1543 | // If this complex type crosses an eightbyte boundary then it |
| 1544 | // should be split. |
| 1545 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1546 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1547 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1548 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1550 | return; |
| 1551 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1553 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1554 | // Arrays are treated like structures. |
| 1555 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1556 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1557 | |
| 1558 | // 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] | 1559 | // than four eightbytes, ..., it has class MEMORY. |
| 1560 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | |
| 1563 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1564 | // fields, it has class MEMORY. |
| 1565 | // |
| 1566 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1567 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1568 | return; |
| 1569 | |
| 1570 | // Otherwise implement simplified merge. We could be smarter about |
| 1571 | // this, but it isn't worth it and would be harder to verify. |
| 1572 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1573 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1574 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 089d892 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1575 | |
| 1576 | // The only case a 256-bit wide vector could be used is when the array |
| 1577 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1578 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1579 | if (Size > 128 && EltSize != 256) |
| 1580 | return; |
| 1581 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1582 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1583 | Class FieldLo, FieldHi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1584 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1585 | Lo = merge(Lo, FieldLo); |
| 1586 | Hi = merge(Hi, FieldHi); |
| 1587 | if (Lo == Memory || Hi == Memory) |
| 1588 | break; |
| 1589 | } |
| 1590 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1591 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1592 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1596 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1597 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1598 | |
| 1599 | // 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] | 1600 | // than four eightbytes, ..., it has class MEMORY. |
| 1601 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1602 | return; |
| 1603 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1604 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1605 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1606 | // reference. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1607 | if (getRecordArgABI(RT, CGT)) |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1608 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1609 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1610 | const RecordDecl *RD = RT->getDecl(); |
| 1611 | |
| 1612 | // Assume variable sized types are passed in memory. |
| 1613 | if (RD->hasFlexibleArrayMember()) |
| 1614 | return; |
| 1615 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1616 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Reset Lo class, this will be recomputed. |
| 1619 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1620 | |
| 1621 | // If this is a C++ record, classify the bases first. |
| 1622 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1623 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1624 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1625 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1626 | "Unexpected base class!"); |
| 1627 | const CXXRecordDecl *Base = |
| 1628 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1629 | |
| 1630 | // Classify this field. |
| 1631 | // |
| 1632 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1633 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1634 | // initialized to class NO_CLASS. |
| 1635 | Class FieldLo, FieldHi; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1636 | uint64_t Offset = |
| 1637 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1638 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1639 | Lo = merge(Lo, FieldLo); |
| 1640 | Hi = merge(Hi, FieldHi); |
| 1641 | if (Lo == Memory || Hi == Memory) |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1647 | unsigned idx = 0; |
Bruno Cardoso Lopes | 548e478 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1648 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1649 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1650 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1651 | bool BitField = i->isBitField(); |
| 1652 | |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1653 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1654 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1655 | // |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1656 | // The only case a 256-bit wide vector could be used is when the struct |
| 1657 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1658 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1659 | // |
| 1660 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1661 | Lo = Memory; |
| 1662 | return; |
| 1663 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1664 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1665 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1666 | Lo = Memory; |
| 1667 | return; |
| 1668 | } |
| 1669 | |
| 1670 | // Classify this field. |
| 1671 | // |
| 1672 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1673 | // exceeds a single eightbyte, each is classified |
| 1674 | // separately. Each eightbyte gets initialized to class |
| 1675 | // NO_CLASS. |
| 1676 | Class FieldLo, FieldHi; |
| 1677 | |
| 1678 | // Bit-fields require special handling, they do not force the |
| 1679 | // structure to be passed in memory even if unaligned, and |
| 1680 | // therefore they can straddle an eightbyte. |
| 1681 | if (BitField) { |
| 1682 | // Ignore padding bit-fields. |
| 1683 | if (i->isUnnamedBitfield()) |
| 1684 | continue; |
| 1685 | |
| 1686 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1687 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1688 | |
| 1689 | uint64_t EB_Lo = Offset / 64; |
| 1690 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1691 | FieldLo = FieldHi = NoClass; |
| 1692 | if (EB_Lo) { |
| 1693 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1694 | FieldLo = NoClass; |
| 1695 | FieldHi = Integer; |
| 1696 | } else { |
| 1697 | FieldLo = Integer; |
| 1698 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1699 | } |
| 1700 | } else |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 1701 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1702 | Lo = merge(Lo, FieldLo); |
| 1703 | Hi = merge(Hi, FieldHi); |
| 1704 | if (Lo == Memory || Hi == Memory) |
| 1705 | break; |
| 1706 | } |
| 1707 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1708 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1712 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1713 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1714 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1715 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1716 | // Treat an enum type as its underlying type. |
| 1717 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1718 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1719 | |
| 1720 | return (Ty->isPromotableIntegerType() ? |
| 1721 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1722 | } |
| 1723 | |
| 1724 | return ABIArgInfo::getIndirect(0); |
| 1725 | } |
| 1726 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1727 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1728 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1729 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1730 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1731 | if (Size <= 64 || Size > LargestVector) |
| 1732 | return true; |
| 1733 | } |
| 1734 | |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1738 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1739 | unsigned freeIntRegs) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1740 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1741 | // place naturally. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1742 | // |
| 1743 | // This assumption is optimistic, as there could be free registers available |
| 1744 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1745 | // the argument in the free register. This does not seem to happen currently, |
| 1746 | // but this code would be much safer if we could mark the argument with |
| 1747 | // 'onstack'. See PR12193. |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1748 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1749 | // Treat an enum type as its underlying type. |
| 1750 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1751 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1752 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1753 | return (Ty->isPromotableIntegerType() ? |
| 1754 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1755 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1756 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1757 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 1758 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1760 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1761 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1762 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1763 | |
| 1764 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1765 | // is important for good codegen. |
| 1766 | // |
| 1767 | // We do this by coercing the value into a scalar type which the backend can |
| 1768 | // handle naturally (i.e., without using byval). |
| 1769 | // |
| 1770 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1771 | // free integer registers. Doing this when there are free integer registers |
| 1772 | // would require more care, as we would have to ensure that the coerced value |
| 1773 | // did not claim the unused register. That would require either reording the |
| 1774 | // arguments to the function (so that any subsequent inreg values came first), |
| 1775 | // or only doing this optimization when there were no following arguments that |
| 1776 | // might be inreg. |
| 1777 | // |
| 1778 | // We currently expect it to be rare (particularly in well written code) for |
| 1779 | // arguments to be passed on the stack when there are still free integer |
| 1780 | // registers available (this would typically imply large structs being passed |
| 1781 | // by value), so this seems like a fair tradeoff for now. |
| 1782 | // |
| 1783 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1784 | // attributes. See PR12193. |
| 1785 | if (freeIntRegs == 0) { |
| 1786 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1787 | |
| 1788 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1789 | // type, which will end up on the stack (with alignment 8). |
| 1790 | if (Align == 8 && Size <= 64) |
| 1791 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1792 | Size)); |
| 1793 | } |
| 1794 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1795 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1798 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1799 | /// 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] | 1800 | /// vector register. |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1801 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1802 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1804 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1805 | // strip them off if present. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1806 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1807 | while (STy && STy->getNumElements() == 1) { |
| 1808 | IRType = STy->getElementType(0); |
| 1809 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1810 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1811 | |
Bruno Cardoso Lopes | 528a8c7 | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1812 | // 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] | 1813 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1814 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1815 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | ce27567 | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1816 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1817 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1818 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1819 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1820 | EltTy->isIntegerTy(128))) |
| 1821 | return VT; |
| 1822 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1823 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1824 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1825 | } |
| 1826 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1827 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1828 | /// is known to either be off the end of the specified type or being in |
| 1829 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1830 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1831 | /// classification that put one of the two halves in the INTEGER class. |
| 1832 | /// |
| 1833 | /// It is conservatively correct to return false. |
| 1834 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1835 | unsigned EndBit, ASTContext &Context) { |
| 1836 | // If the bytes being queried are off the end of the type, there is no user |
| 1837 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1838 | // types that don't contain interesting padding. |
| 1839 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1840 | if (TySize <= StartBit) |
| 1841 | return true; |
| 1842 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1843 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1844 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1845 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1846 | |
| 1847 | // Check each element to see if the element overlaps with the queried range. |
| 1848 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1849 | // If the element is after the span we care about, then we're done.. |
| 1850 | unsigned EltOffset = i*EltSize; |
| 1851 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1853 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1854 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1855 | EndBit-EltOffset, Context)) |
| 1856 | return false; |
| 1857 | } |
| 1858 | // If it overlaps no elements, then it is safe to process as padding. |
| 1859 | return true; |
| 1860 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1862 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1863 | const RecordDecl *RD = RT->getDecl(); |
| 1864 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1866 | // If this is a C++ record, check the bases first. |
| 1867 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1868 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1869 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1870 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1871 | "Unexpected base class!"); |
| 1872 | const CXXRecordDecl *Base = |
| 1873 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1875 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1876 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1877 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1879 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1880 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1881 | EndBit-BaseOffset, Context)) |
| 1882 | return false; |
| 1883 | } |
| 1884 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1886 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1887 | // this could be sped up a lot by being smarter about queried fields, |
| 1888 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1889 | // much. |
| 1890 | unsigned idx = 0; |
| 1891 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1892 | i != e; ++i, ++idx) { |
| 1893 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1895 | // If we found a field after the region we care about, then we're done. |
| 1896 | if (FieldOffset >= EndBit) break; |
| 1897 | |
| 1898 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1899 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1900 | Context)) |
| 1901 | return false; |
| 1902 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1904 | // If nothing in this record overlapped the area of interest, then we're |
| 1905 | // clean. |
| 1906 | return true; |
| 1907 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1908 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1909 | return false; |
| 1910 | } |
| 1911 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1912 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1913 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1914 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1915 | /// false. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1916 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1917 | const llvm::DataLayout &TD) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1918 | // Base case if we find a float. |
| 1919 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1920 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1921 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1922 | // 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] | 1923 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1924 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1925 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1926 | IROffset -= SL->getElementOffset(Elt); |
| 1927 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1928 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1930 | // 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] | 1931 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1932 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1933 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1934 | IROffset -= IROffset/EltSize*EltSize; |
| 1935 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1936 | } |
| 1937 | |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1941 | |
| 1942 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1943 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1944 | llvm::Type *X86_64ABIInfo:: |
| 1945 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1946 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1947 | // 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] | 1948 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1949 | // structs that contain 3 floats. |
| 1950 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1951 | SourceOffset*8+64, getContext())) |
| 1952 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1953 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1954 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1955 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1956 | // case. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1957 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 1958 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1959 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1960 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1961 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1962 | } |
| 1963 | |
| 1964 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1965 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1966 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1967 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1968 | /// 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] | 1969 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1970 | /// etc). |
| 1971 | /// |
| 1972 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1973 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1974 | /// the 8-byte value references. PrefType may be null. |
| 1975 | /// |
| 1976 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1977 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1978 | /// |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1979 | llvm::Type *X86_64ABIInfo:: |
| 1980 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1981 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1982 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1983 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1984 | if (IROffset == 0) { |
| 1985 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1986 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 1987 | IRType->isIntegerTy(64)) |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1988 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1989 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1990 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1991 | // goodness in the source type is just tail padding. This is allowed to |
| 1992 | // kick in for struct {double,int} on the int, but not on |
| 1993 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1994 | // have to do this analysis on the source type because we can't depend on |
| 1995 | // unions being lowered a specific way etc. |
| 1996 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1997 | IRType->isIntegerTy(32) || |
| 1998 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 1999 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2000 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2001 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2002 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2003 | SourceOffset*8+64, getContext())) |
| 2004 | return IRType; |
| 2005 | } |
| 2006 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2008 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2009 | // 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] | 2010 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2011 | if (IROffset < SL->getSizeInBytes()) { |
| 2012 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2013 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2015 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2016 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2017 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2018 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2020 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2021 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2022 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2023 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2024 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2025 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2026 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2027 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2028 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2029 | // 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] | 2030 | unsigned TySizeInBytes = |
| 2031 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2033 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2035 | // It is always safe to classify this as an integer type up to i64 that |
| 2036 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2037 | return llvm::IntegerType::get(getVMContext(), |
| 2038 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2041 | |
| 2042 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2043 | /// be used as elements of a two register pair to pass or return, return a |
| 2044 | /// first class aggregate to represent them. For example, if the low part of |
| 2045 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2046 | /// return {i32*, float}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2047 | static llvm::Type * |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2048 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2049 | const llvm::DataLayout &TD) { |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2050 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2051 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2052 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2053 | // the second element at offset 8. Check for this: |
| 2054 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2055 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2056 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2057 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2059 | // To handle this, we have to increase the size of the low part so that the |
| 2060 | // second element will start at an 8 byte offset. We can't increase the size |
| 2061 | // of the second element because it might make us access off the end of the |
| 2062 | // struct. |
| 2063 | if (HiStart != 8) { |
| 2064 | // There are only two sorts of types the ABI generation code can produce for |
| 2065 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2066 | // Promote these to a larger type. |
| 2067 | if (Lo->isFloatTy()) |
| 2068 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2069 | else { |
| 2070 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2071 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2072 | } |
| 2073 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2075 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2076 | |
| 2077 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2078 | // Verify that the second element is at an 8-byte offset. |
| 2079 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2080 | "Invalid x86-64 argument pair!"); |
| 2081 | return Result; |
| 2082 | } |
| 2083 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2084 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2085 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2086 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2087 | // classification algorithm. |
| 2088 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2089 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Check some invariants. |
| 2092 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2093 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2094 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2095 | llvm::Type *ResType = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2096 | switch (Lo) { |
| 2097 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2098 | if (Hi == NoClass) |
| 2099 | return ABIArgInfo::getIgnore(); |
| 2100 | // If the low part is just padding, it takes no register, leave ResType |
| 2101 | // null. |
| 2102 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2103 | "Unknown missing lo part"); |
| 2104 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2105 | |
| 2106 | case SSEUp: |
| 2107 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2108 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2109 | |
| 2110 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2111 | // hidden argument. |
| 2112 | case Memory: |
| 2113 | return getIndirectReturnResult(RetTy); |
| 2114 | |
| 2115 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2116 | // available register of the sequence %rax, %rdx is used. |
| 2117 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2118 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2120 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2121 | // so that the parameter gets the right LLVM IR attributes. |
| 2122 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2123 | // Treat an enum type as its underlying type. |
| 2124 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2125 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2127 | if (RetTy->isIntegralOrEnumerationType() && |
| 2128 | RetTy->isPromotableIntegerType()) |
| 2129 | return ABIArgInfo::getExtend(); |
| 2130 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | |
| 2133 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2134 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2135 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2136 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2137 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2138 | |
| 2139 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2140 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2141 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2142 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2143 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2144 | |
| 2145 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2146 | // part of the value is returned in %st0 and the imaginary part in |
| 2147 | // %st1. |
| 2148 | case ComplexX87: |
| 2149 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2150 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2151 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2152 | NULL); |
| 2153 | break; |
| 2154 | } |
| 2155 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2156 | llvm::Type *HighPart = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2157 | switch (Hi) { |
| 2158 | // Memory was handled previously and X87 should |
| 2159 | // never occur as a hi class. |
| 2160 | case Memory: |
| 2161 | case X87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2162 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2163 | |
| 2164 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2165 | case NoClass: |
| 2166 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2168 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2169 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2170 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2171 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2172 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2173 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2174 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2175 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2176 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2177 | break; |
| 2178 | |
| 2179 | // 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] | 2180 | // is passed in the next available eightbyte chunk if the last used |
| 2181 | // vector register. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2182 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2183 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2184 | case SSEUp: |
| 2185 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2186 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2187 | break; |
| 2188 | |
| 2189 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2190 | // returned together with the previous X87 value in %st0. |
| 2191 | case X87Up: |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2192 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2193 | // anything. However, in some cases with unions it may not be |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2194 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2195 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2196 | if (Lo != X87) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2197 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2198 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2199 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2200 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2203 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2204 | // 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] | 2205 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2206 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2207 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2208 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2210 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2213 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2214 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2215 | bool isNamedArg) |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2216 | const |
| 2217 | { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2218 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2219 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2220 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2221 | // Check some invariants. |
| 2222 | // FIXME: Enforce these by construction. |
| 2223 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2224 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2225 | |
| 2226 | neededInt = 0; |
| 2227 | neededSSE = 0; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2228 | llvm::Type *ResType = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2229 | switch (Lo) { |
| 2230 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2231 | if (Hi == NoClass) |
| 2232 | return ABIArgInfo::getIgnore(); |
| 2233 | // If the low part is just padding, it takes no register, leave ResType |
| 2234 | // null. |
| 2235 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2236 | "Unknown missing lo part"); |
| 2237 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2238 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2239 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2240 | // on the stack. |
| 2241 | case Memory: |
| 2242 | |
| 2243 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2244 | // COMPLEX_X87, it is passed in memory. |
| 2245 | case X87: |
| 2246 | case ComplexX87: |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2247 | if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect) |
Eli Friedman | ded137f | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2248 | ++neededInt; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2249 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2250 | |
| 2251 | case SSEUp: |
| 2252 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2253 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2254 | |
| 2255 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2256 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2257 | // and %r9 is used. |
| 2258 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2259 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2260 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2261 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2262 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2263 | |
| 2264 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2265 | // so that the parameter gets the right LLVM IR attributes. |
| 2266 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2267 | // Treat an enum type as its underlying type. |
| 2268 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2269 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2270 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2271 | if (Ty->isIntegralOrEnumerationType() && |
| 2272 | Ty->isPromotableIntegerType()) |
| 2273 | return ABIArgInfo::getExtend(); |
| 2274 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2275 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | |
| 2278 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2279 | // available SSE register is used, the registers are taken in the |
| 2280 | // order from %xmm0 to %xmm7. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2281 | case SSE: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2282 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 14508ff | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2283 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2284 | ++neededSSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2285 | break; |
| 2286 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2287 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2288 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2289 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2290 | switch (Hi) { |
| 2291 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2292 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2293 | // which is passed in memory. |
| 2294 | case Memory: |
| 2295 | case X87: |
| 2296 | case ComplexX87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2297 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2298 | |
| 2299 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2301 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2302 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2303 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2304 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2305 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2306 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2307 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2308 | break; |
| 2309 | |
| 2310 | // X87Up generally doesn't occur here (long double is passed in |
| 2311 | // memory), except in situations involving unions. |
| 2312 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2313 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2314 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2316 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2317 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2318 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2319 | ++neededSSE; |
| 2320 | break; |
| 2321 | |
| 2322 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2323 | // 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] | 2324 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2325 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2326 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2327 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | } |
| 2330 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2331 | // If a high part was specified, merge it together with the low part. It is |
| 2332 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2333 | // first class struct aggregate with the high and low part: {low, high} |
| 2334 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2335 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2337 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2340 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2341 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2342 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2343 | |
| 2344 | // Keep track of the number of assigned registers. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2345 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2346 | |
| 2347 | // If the return value is indirect, then the hidden argument is consuming one |
| 2348 | // integer register. |
| 2349 | if (FI.getReturnInfo().isIndirect()) |
| 2350 | --freeIntRegs; |
| 2351 | |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2352 | bool isVariadic = FI.isVariadic(); |
| 2353 | unsigned numRequiredArgs = 0; |
| 2354 | if (isVariadic) |
| 2355 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2356 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2357 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2358 | // get assigned (in left-to-right order) for passing as follows... |
| 2359 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2360 | it != ie; ++it) { |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2361 | bool isNamedArg = true; |
| 2362 | if (isVariadic) |
| 2363 | isNamedArg = (it - FI.arg_begin()) < numRequiredArgs; |
| 2364 | |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2365 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2366 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2367 | neededSSE, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2368 | |
| 2369 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2370 | // eightbyte of an argument, the whole argument is passed on the |
| 2371 | // stack. If registers have already been assigned for some |
| 2372 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2373 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2374 | freeIntRegs -= neededInt; |
| 2375 | freeSSERegs -= neededSSE; |
| 2376 | } else { |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2377 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2383 | QualType Ty, |
| 2384 | CodeGenFunction &CGF) { |
| 2385 | llvm::Value *overflow_arg_area_p = |
| 2386 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2387 | llvm::Value *overflow_arg_area = |
| 2388 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2389 | |
| 2390 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2391 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2392 | // It isn't stated explicitly in the standard, but in practice we use |
| 2393 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2394 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2395 | if (Align > 8) { |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2396 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2397 | llvm::Value *Offset = |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2398 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2399 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2400 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2401 | CGF.Int64Ty); |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2402 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2403 | overflow_arg_area = |
| 2404 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2405 | overflow_arg_area->getType(), |
| 2406 | "overflow_arg_area.align"); |
| 2407 | } |
| 2408 | |
| 2409 | // 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] | 2410 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2411 | llvm::Value *Res = |
| 2412 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2413 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | |
| 2415 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2416 | // l->overflow_arg_area + sizeof(type). |
| 2417 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2418 | // an 8 byte boundary. |
| 2419 | |
| 2420 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2421 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2422 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2423 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2424 | "overflow_arg_area.next"); |
| 2425 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2426 | |
| 2427 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2428 | return Res; |
| 2429 | } |
| 2430 | |
| 2431 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2432 | CodeGenFunction &CGF) const { |
| 2433 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2434 | // struct { |
| 2435 | // i32 gp_offset; |
| 2436 | // i32 fp_offset; |
| 2437 | // i8* overflow_arg_area; |
| 2438 | // i8* reg_save_area; |
| 2439 | // }; |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2440 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2441 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2442 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame^] | 2443 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2444 | /*isNamedArg*/false); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2445 | |
| 2446 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2447 | // in the registers. If not go to step 7. |
| 2448 | if (!neededInt && !neededSSE) |
| 2449 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2450 | |
| 2451 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2452 | // general purpose registers needed to pass type and num_fp to hold |
| 2453 | // the number of floating point registers needed. |
| 2454 | |
| 2455 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2456 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2457 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2458 | // |
| 2459 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2460 | // register save space). |
| 2461 | |
| 2462 | llvm::Value *InRegs = 0; |
| 2463 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2464 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2465 | if (neededInt) { |
| 2466 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2467 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2468 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2469 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | if (neededSSE) { |
| 2473 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2474 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2475 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2476 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2477 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2478 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2479 | } |
| 2480 | |
| 2481 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2482 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2483 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2484 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2485 | |
| 2486 | // Emit code to load the value if it was passed in registers. |
| 2487 | |
| 2488 | CGF.EmitBlock(InRegBlock); |
| 2489 | |
| 2490 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2491 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2492 | // copying to a temporary location in case the parameter is passed |
| 2493 | // in different register classes or requires an alignment greater |
| 2494 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2495 | // |
| 2496 | // FIXME: This really results in shameful code when we end up needing to |
| 2497 | // collect arguments from different places; often what should result in a |
| 2498 | // simple assembling of a structure from scattered addresses has many more |
| 2499 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2500 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2501 | llvm::Value *RegAddr = |
| 2502 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2503 | "reg_save_area"); |
| 2504 | if (neededInt && neededSSE) { |
| 2505 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2506 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2507 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2508 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2509 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2510 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2511 | llvm::Type *TyLo = ST->getElementType(0); |
| 2512 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2513 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2515 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2516 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2518 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2519 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2520 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2521 | llvm::Value *V = |
| 2522 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2523 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2524 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2525 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2526 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2527 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2528 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2529 | } else if (neededInt) { |
| 2530 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2531 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2532 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2535 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2536 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2537 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2538 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2539 | if (TyAlign > 8) { |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2540 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2541 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2542 | RegAddr = Tmp; |
| 2543 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2544 | } else if (neededSSE == 1) { |
| 2545 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2546 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2547 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2548 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2549 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2550 | // SSE registers are spaced 16 bytes apart in the register save |
| 2551 | // area, we need to collect the two eightbytes together. |
| 2552 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2553 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2554 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2555 | llvm::Type *DblPtrTy = |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2556 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2557 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2558 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2559 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2560 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2561 | DblPtrTy)); |
| 2562 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2563 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2564 | DblPtrTy)); |
| 2565 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2566 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2567 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2571 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2572 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2573 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2574 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2575 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2576 | gp_offset_p); |
| 2577 | } |
| 2578 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2579 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2580 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2581 | fp_offset_p); |
| 2582 | } |
| 2583 | CGF.EmitBranch(ContBlock); |
| 2584 | |
| 2585 | // Emit code to load the value if it was passed in memory. |
| 2586 | |
| 2587 | CGF.EmitBlock(InMemBlock); |
| 2588 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2589 | |
| 2590 | // Return the appropriate result. |
| 2591 | |
| 2592 | CGF.EmitBlock(ContBlock); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2593 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2594 | "vaarg.addr"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2595 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2596 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | return ResAddr; |
| 2598 | } |
| 2599 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2600 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2601 | |
| 2602 | if (Ty->isVoidType()) |
| 2603 | return ABIArgInfo::getIgnore(); |
| 2604 | |
| 2605 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2606 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2607 | |
| 2608 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2609 | |
| 2610 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2611 | if (IsReturnType) { |
| 2612 | if (isRecordReturnIndirect(RT, CGT)) |
| 2613 | return ABIArgInfo::getIndirect(0, false); |
| 2614 | } else { |
| 2615 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) |
| 2616 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2617 | } |
| 2618 | |
| 2619 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2620 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2621 | |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2622 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2623 | if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32) |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2624 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2625 | Size)); |
| 2626 | |
| 2627 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2628 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2629 | if (Size <= 64 && |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2630 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2631 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2632 | Size)); |
| 2633 | |
| 2634 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2635 | } |
| 2636 | |
| 2637 | if (Ty->isPromotableIntegerType()) |
| 2638 | return ABIArgInfo::getExtend(); |
| 2639 | |
| 2640 | return ABIArgInfo::getDirect(); |
| 2641 | } |
| 2642 | |
| 2643 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2644 | |
| 2645 | QualType RetTy = FI.getReturnType(); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2646 | FI.getReturnInfo() = classify(RetTy, true); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2647 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2648 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2649 | it != ie; ++it) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2650 | it->info = classify(it->type, false); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2653 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2654 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2655 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2656 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2657 | CGBuilderTy &Builder = CGF.Builder; |
| 2658 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2659 | "ap"); |
| 2660 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2661 | llvm::Type *PTy = |
| 2662 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2663 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2664 | |
| 2665 | uint64_t Offset = |
| 2666 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2667 | llvm::Value *NextAddr = |
| 2668 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2669 | "ap.next"); |
| 2670 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2671 | |
| 2672 | return AddrTyped; |
| 2673 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2674 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2675 | namespace { |
| 2676 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2677 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2678 | public: |
| 2679 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2680 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
| 2681 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2682 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2683 | CodeGenFunction &CGF) const; |
| 2684 | private: |
| 2685 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2686 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2687 | }; |
| 2688 | |
| 2689 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2690 | public: |
| 2691 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2692 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2693 | }; |
| 2694 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2697 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2698 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2699 | PInfo.computeInfo(FI); |
| 2700 | else |
| 2701 | NInfo.computeInfo(FI); |
| 2702 | } |
| 2703 | |
| 2704 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2705 | CodeGenFunction &CGF) const { |
| 2706 | // Always use the native convention; calling pnacl-style varargs functions |
| 2707 | // is unuspported. |
| 2708 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2709 | } |
| 2710 | |
| 2711 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2712 | // PowerPC-32 |
| 2713 | |
| 2714 | namespace { |
| 2715 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2716 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2717 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2718 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2719 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2720 | // This is recovered from gcc output. |
| 2721 | return 1; // r1 is the dedicated stack pointer |
| 2722 | } |
| 2723 | |
| 2724 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2725 | llvm::Value *Address) const; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2726 | }; |
| 2727 | |
| 2728 | } |
| 2729 | |
| 2730 | bool |
| 2731 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2732 | llvm::Value *Address) const { |
| 2733 | // This is calculated from the LLVM and GCC tables and verified |
| 2734 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2735 | |
| 2736 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2737 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2738 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2739 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2740 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2741 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2742 | |
| 2743 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2744 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2745 | |
| 2746 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2747 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2748 | |
| 2749 | // 64-76 are various 4-byte special-purpose registers: |
| 2750 | // 64: mq |
| 2751 | // 65: lr |
| 2752 | // 66: ctr |
| 2753 | // 67: ap |
| 2754 | // 68-75 cr0-7 |
| 2755 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2756 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2757 | |
| 2758 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2759 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2760 | |
| 2761 | // 109: vrsave |
| 2762 | // 110: vscr |
| 2763 | // 111: spe_acc |
| 2764 | // 112: spefscr |
| 2765 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2766 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2767 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2768 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2771 | // PowerPC-64 |
| 2772 | |
| 2773 | namespace { |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2774 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2775 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2776 | |
| 2777 | public: |
| 2778 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2779 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2780 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2781 | |
| 2782 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2783 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2784 | |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2785 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2786 | // Example: For aggregate arguments that fit in a register, we could |
| 2787 | // use getDirectInReg (as is done below for structs containing a single |
| 2788 | // floating-point value) to avoid pushing them to memory on function |
| 2789 | // entry. This would require changing the logic in PPCISelLowering |
| 2790 | // when lowering the parameters in the caller and args in the callee. |
| 2791 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 2792 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2793 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2794 | it != ie; ++it) { |
| 2795 | // We rely on the default argument classification for the most part. |
| 2796 | // One exception: An aggregate containing a single floating-point |
| 2797 | // item must be passed in a register if one is available. |
| 2798 | const Type *T = isSingleElementStruct(it->type, getContext()); |
| 2799 | if (T) { |
| 2800 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
| 2801 | if (BT && BT->isFloatingPoint()) { |
| 2802 | QualType QT(T, 0); |
| 2803 | it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
| 2804 | continue; |
| 2805 | } |
| 2806 | } |
| 2807 | it->info = classifyArgumentType(it->type); |
| 2808 | } |
| 2809 | } |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2810 | |
| 2811 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, |
| 2812 | QualType Ty, |
| 2813 | CodeGenFunction &CGF) const; |
| 2814 | }; |
| 2815 | |
| 2816 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2817 | public: |
| 2818 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2819 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2820 | |
| 2821 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2822 | // This is recovered from gcc output. |
| 2823 | return 1; // r1 is the dedicated stack pointer |
| 2824 | } |
| 2825 | |
| 2826 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2827 | llvm::Value *Address) const; |
| 2828 | }; |
| 2829 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2830 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2831 | public: |
| 2832 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2833 | |
| 2834 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2835 | // This is recovered from gcc output. |
| 2836 | return 1; // r1 is the dedicated stack pointer |
| 2837 | } |
| 2838 | |
| 2839 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2840 | llvm::Value *Address) const; |
| 2841 | }; |
| 2842 | |
| 2843 | } |
| 2844 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2845 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2846 | // extended to 64 bits. |
| 2847 | bool |
| 2848 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2849 | // Treat an enum type as its underlying type. |
| 2850 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2851 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2852 | |
| 2853 | // Promotable integer types are required to be promoted by the ABI. |
| 2854 | if (Ty->isPromotableIntegerType()) |
| 2855 | return true; |
| 2856 | |
| 2857 | // In addition to the usual promotable integer types, we also need to |
| 2858 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2859 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2860 | switch (BT->getKind()) { |
| 2861 | case BuiltinType::Int: |
| 2862 | case BuiltinType::UInt: |
| 2863 | return true; |
| 2864 | default: |
| 2865 | break; |
| 2866 | } |
| 2867 | |
| 2868 | return false; |
| 2869 | } |
| 2870 | |
| 2871 | ABIArgInfo |
| 2872 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | c9715fc | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 2873 | if (Ty->isAnyComplexType()) |
| 2874 | return ABIArgInfo::getDirect(); |
| 2875 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2876 | if (isAggregateTypeForABI(Ty)) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2877 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 2878 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2879 | |
| 2880 | return ABIArgInfo::getIndirect(0); |
| 2881 | } |
| 2882 | |
| 2883 | return (isPromotableTypeForABI(Ty) ? |
| 2884 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2885 | } |
| 2886 | |
| 2887 | ABIArgInfo |
| 2888 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 2889 | if (RetTy->isVoidType()) |
| 2890 | return ABIArgInfo::getIgnore(); |
| 2891 | |
Bill Schmidt | 9e6111a | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 2892 | if (RetTy->isAnyComplexType()) |
| 2893 | return ABIArgInfo::getDirect(); |
| 2894 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2895 | if (isAggregateTypeForABI(RetTy)) |
| 2896 | return ABIArgInfo::getIndirect(0); |
| 2897 | |
| 2898 | return (isPromotableTypeForABI(RetTy) ? |
| 2899 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2900 | } |
| 2901 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2902 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 2903 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 2904 | QualType Ty, |
| 2905 | CodeGenFunction &CGF) const { |
| 2906 | llvm::Type *BP = CGF.Int8PtrTy; |
| 2907 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 2908 | |
| 2909 | CGBuilderTy &Builder = CGF.Builder; |
| 2910 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 2911 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2912 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2913 | // Update the va_list pointer. The pointer should be bumped by the |
| 2914 | // size of the object. We can trust getTypeSize() except for a complex |
| 2915 | // type whose base type is smaller than a doubleword. For these, the |
| 2916 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2917 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2918 | QualType BaseTy; |
| 2919 | unsigned CplxBaseSize = 0; |
| 2920 | |
| 2921 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 2922 | BaseTy = CTy->getElementType(); |
| 2923 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 2924 | if (CplxBaseSize < 8) |
| 2925 | SizeInBytes = 16; |
| 2926 | } |
| 2927 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2928 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 2929 | llvm::Value *NextAddr = |
| 2930 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 2931 | "ap.next"); |
| 2932 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2933 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2934 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 2935 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 2936 | // in separate doublewords. However, Clang expects us to produce a |
| 2937 | // pointer to a structure with the two parts packed tightly. So generate |
| 2938 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 2939 | // and store them to a temporary structure. |
| 2940 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 2941 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2942 | llvm::Value *ImagAddr = RealAddr; |
| 2943 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 2944 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 2945 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 2946 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 2947 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 2948 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 2949 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 2950 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 2951 | "vacplx"); |
| 2952 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 2953 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 2954 | Builder.CreateStore(Real, RealPtr, false); |
| 2955 | Builder.CreateStore(Imag, ImagPtr, false); |
| 2956 | return Ptr; |
| 2957 | } |
| 2958 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2959 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 2960 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 2961 | // correct offset. |
| 2962 | if (SizeInBytes < 8) { |
| 2963 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2964 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 2965 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 2966 | } |
| 2967 | |
| 2968 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2969 | return Builder.CreateBitCast(Addr, PTy); |
| 2970 | } |
| 2971 | |
| 2972 | static bool |
| 2973 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2974 | llvm::Value *Address) { |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2975 | // This is calculated from the LLVM and GCC tables and verified |
| 2976 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2977 | |
| 2978 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2979 | |
| 2980 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 2981 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2982 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2983 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2984 | |
| 2985 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 2986 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 2987 | |
| 2988 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 2989 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 2990 | |
| 2991 | // 64-76 are various 4-byte special-purpose registers: |
| 2992 | // 64: mq |
| 2993 | // 65: lr |
| 2994 | // 66: ctr |
| 2995 | // 67: ap |
| 2996 | // 68-75 cr0-7 |
| 2997 | // 76: xer |
| 2998 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 2999 | |
| 3000 | // 77-108: v0-31, the 16-byte vector registers |
| 3001 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3002 | |
| 3003 | // 109: vrsave |
| 3004 | // 110: vscr |
| 3005 | // 111: spe_acc |
| 3006 | // 112: spefscr |
| 3007 | // 113: sfp |
| 3008 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3009 | |
| 3010 | return false; |
| 3011 | } |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3012 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3013 | bool |
| 3014 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3015 | CodeGen::CodeGenFunction &CGF, |
| 3016 | llvm::Value *Address) const { |
| 3017 | |
| 3018 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3019 | } |
| 3020 | |
| 3021 | bool |
| 3022 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3023 | llvm::Value *Address) const { |
| 3024 | |
| 3025 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3026 | } |
| 3027 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3028 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3029 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3030 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3031 | |
| 3032 | namespace { |
| 3033 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3034 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3035 | public: |
| 3036 | enum ABIKind { |
| 3037 | APCS = 0, |
| 3038 | AAPCS = 1, |
| 3039 | AAPCS_VFP |
| 3040 | }; |
| 3041 | |
| 3042 | private: |
| 3043 | ABIKind Kind; |
| 3044 | |
| 3045 | public: |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3046 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
| 3047 | setRuntimeCC(); |
| 3048 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3049 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3050 | bool isEABI() const { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 3051 | StringRef Env = getTarget().getTriple().getEnvironmentName(); |
Logan Chien | 94a7142 | 2012-09-02 09:30:11 +0000 | [diff] [blame] | 3052 | return (Env == "gnueabi" || Env == "eabi" || |
| 3053 | Env == "android" || Env == "androideabi"); |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3056 | private: |
| 3057 | ABIKind getABIKind() const { return Kind; } |
| 3058 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3059 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3060 | ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs, |
| 3061 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3062 | bool &IsHA) const; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3063 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3065 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3066 | |
| 3067 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3068 | CodeGenFunction &CGF) const; |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3069 | |
| 3070 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 3071 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 3072 | void setRuntimeCC(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3073 | }; |
| 3074 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3075 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3076 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3077 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 3078 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3079 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3080 | const ARMABIInfo &getABIInfo() const { |
| 3081 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3082 | } |
| 3083 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3084 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3085 | return 13; |
| 3086 | } |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3088 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3089 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3090 | } |
| 3091 | |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3092 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3093 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3094 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3095 | |
| 3096 | // 0-15 are the 16 integer registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3097 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3098 | return false; |
| 3099 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3100 | |
| 3101 | unsigned getSizeOfUnwindException() const { |
| 3102 | if (getABIInfo().isEABI()) return 88; |
| 3103 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3104 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3105 | }; |
| 3106 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3109 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3110 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3111 | // VFP registers allocated so far. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3112 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3113 | // VFP registers of the appropriate type unallocated then the argument is |
| 3114 | // allocated to the lowest-numbered sequence of such registers. |
| 3115 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3116 | // unallocated are marked as unavailable. |
| 3117 | unsigned AllocatedVFP = 0; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3118 | int VFPRegs[16] = { 0 }; |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3119 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3120 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3121 | it != ie; ++it) { |
| 3122 | unsigned PreAllocation = AllocatedVFP; |
| 3123 | bool IsHA = false; |
| 3124 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3125 | // s0-s15 (d0-d7) for passing arguments. |
| 3126 | const unsigned NumVFPs = 16; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3127 | it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3128 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3129 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3130 | // padding of (NumVFPs - PreAllocation) floats. |
| 3131 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3132 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3133 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
| 3134 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3135 | } |
| 3136 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3137 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3138 | // Always honor user-specified calling convention. |
| 3139 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3140 | return; |
| 3141 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3142 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3143 | if (cc != llvm::CallingConv::C) |
| 3144 | FI.setEffectiveCallingConvention(cc); |
| 3145 | } |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3146 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3147 | /// Return the default calling convention that LLVM will use. |
| 3148 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3149 | // The default calling convention that LLVM will infer. |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 3150 | if (getTarget().getTriple().getEnvironmentName()=="gnueabihf") |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3151 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3152 | else if (isEABI()) |
| 3153 | return llvm::CallingConv::ARM_AAPCS; |
| 3154 | else |
| 3155 | return llvm::CallingConv::ARM_APCS; |
| 3156 | } |
| 3157 | |
| 3158 | /// Return the calling convention that our ABI would like us to use |
| 3159 | /// as the C calling convention. |
| 3160 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3161 | switch (getABIKind()) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3162 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3163 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3164 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3165 | } |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3166 | llvm_unreachable("bad ABI kind"); |
| 3167 | } |
| 3168 | |
| 3169 | void ARMABIInfo::setRuntimeCC() { |
| 3170 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3171 | |
| 3172 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3173 | // they'd just match what LLVM will infer from the triple. |
| 3174 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3175 | if (abiCC != getLLVMDefaultCC()) |
| 3176 | RuntimeCC = abiCC; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3179 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3180 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3181 | /// contained in the type is returned through it; this is used for the |
| 3182 | /// recursive calls that check aggregate component types. |
| 3183 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3184 | ASTContext &Context, |
| 3185 | uint64_t *HAMembers = 0) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3186 | uint64_t Members = 0; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3187 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3188 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3189 | return false; |
| 3190 | Members *= AT->getSize().getZExtValue(); |
| 3191 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3192 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3193 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3194 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3195 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3196 | Members = 0; |
| 3197 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3198 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3199 | const FieldDecl *FD = *i; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3200 | uint64_t FldMembers; |
| 3201 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3202 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3203 | |
| 3204 | Members = (RD->isUnion() ? |
| 3205 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } else { |
| 3208 | Members = 1; |
| 3209 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3210 | Members = 2; |
| 3211 | Ty = CT->getElementType(); |
| 3212 | } |
| 3213 | |
| 3214 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 3215 | // double, or 64-bit or 128-bit vectors. |
| 3216 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3217 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | adfa45f | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 3218 | BT->getKind() != BuiltinType::Double && |
| 3219 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3220 | return false; |
| 3221 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3222 | unsigned VecSize = Context.getTypeSize(VT); |
| 3223 | if (VecSize != 64 && VecSize != 128) |
| 3224 | return false; |
| 3225 | } else { |
| 3226 | return false; |
| 3227 | } |
| 3228 | |
| 3229 | // The base type must be the same for all members. Vector types of the |
| 3230 | // same total size are treated as being equivalent here. |
| 3231 | const Type *TyPtr = Ty.getTypePtr(); |
| 3232 | if (!Base) |
| 3233 | Base = TyPtr; |
| 3234 | if (Base != TyPtr && |
| 3235 | (!Base->isVectorType() || !TyPtr->isVectorType() || |
| 3236 | Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) |
| 3237 | return false; |
| 3238 | } |
| 3239 | |
| 3240 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 3241 | if (HAMembers) |
| 3242 | *HAMembers = Members; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3243 | |
| 3244 | return (Members > 0 && Members <= 4); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3247 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 3248 | /// number of VFP registers (unit is S register) requested. |
| 3249 | static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP, |
| 3250 | unsigned Alignment, |
| 3251 | unsigned NumRequired) { |
| 3252 | // Early Exit. |
| 3253 | if (AllocatedVFP >= 16) |
| 3254 | return; |
| 3255 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3256 | // VFP registers of the appropriate type unallocated then the argument is |
| 3257 | // allocated to the lowest-numbered sequence of such registers. |
| 3258 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 3259 | bool FoundSlot = true; |
| 3260 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3261 | if (J >= 16 || VFPRegs[J]) { |
| 3262 | FoundSlot = false; |
| 3263 | break; |
| 3264 | } |
| 3265 | if (FoundSlot) { |
| 3266 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3267 | VFPRegs[J] = 1; |
| 3268 | AllocatedVFP += NumRequired; |
| 3269 | return; |
| 3270 | } |
| 3271 | } |
| 3272 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3273 | // unallocated are marked as unavailable. |
| 3274 | for (unsigned I = 0; I < 16; I++) |
| 3275 | VFPRegs[I] = 1; |
| 3276 | AllocatedVFP = 17; // We do not have enough VFP registers. |
| 3277 | } |
| 3278 | |
| 3279 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs, |
| 3280 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3281 | bool &IsHA) const { |
| 3282 | // We update number of allocated VFPs according to |
| 3283 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 3284 | // A single-precision floating-point type (including promoted |
| 3285 | // half-precision types); A double-precision floating-point type; |
| 3286 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 3287 | // with a Base Type of a single- or double-precision floating-point type, |
| 3288 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 3289 | // to four Elements. |
| 3290 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3291 | // Handle illegal vector types here. |
| 3292 | if (isIllegalVectorType(Ty)) { |
| 3293 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3294 | if (Size <= 32) { |
| 3295 | llvm::Type *ResType = |
| 3296 | llvm::Type::getInt32Ty(getVMContext()); |
| 3297 | return ABIArgInfo::getDirect(ResType); |
| 3298 | } |
| 3299 | if (Size == 64) { |
| 3300 | llvm::Type *ResType = llvm::VectorType::get( |
| 3301 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3302 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3303 | return ABIArgInfo::getDirect(ResType); |
| 3304 | } |
| 3305 | if (Size == 128) { |
| 3306 | llvm::Type *ResType = llvm::VectorType::get( |
| 3307 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3308 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3309 | return ABIArgInfo::getDirect(ResType); |
| 3310 | } |
| 3311 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3312 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3313 | // Update VFPRegs for legal vector types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3314 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3315 | uint64_t Size = getContext().getTypeSize(VT); |
| 3316 | // 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] | 3317 | markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3318 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3319 | // Update VFPRegs for floating point types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3320 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3321 | if (BT->getKind() == BuiltinType::Half || |
| 3322 | BT->getKind() == BuiltinType::Float) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3323 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3324 | if (BT->getKind() == BuiltinType::Double || |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3325 | BT->getKind() == BuiltinType::LongDouble) |
| 3326 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3327 | } |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3328 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3329 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3330 | // Treat an enum type as its underlying type. |
| 3331 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3332 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3333 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3334 | return (Ty->isPromotableIntegerType() ? |
| 3335 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3336 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3337 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3338 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3339 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3340 | return ABIArgInfo::getIgnore(); |
| 3341 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3342 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 3343 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3344 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3345 | if (getABIKind() == ARMABIInfo::AAPCS_VFP) { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3346 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 3347 | // into VFP registers. |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3348 | const Type *Base = 0; |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3349 | uint64_t Members = 0; |
| 3350 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3351 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3352 | // Base can be a floating-point or a vector. |
| 3353 | if (Base->isVectorType()) { |
| 3354 | // ElementSize is in number of floats. |
| 3355 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3356 | markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize, |
| 3357 | Members * ElementSize); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3358 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3359 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3360 | else { |
| 3361 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 3362 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3363 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3364 | } |
| 3365 | IsHA = true; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3366 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3367 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Manman Ren | 634b3d2 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 3370 | // Support byval for ARM. |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3371 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 3372 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 3373 | // than ABI alignment. |
Manman Ren | fd1ba91 | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 3374 | uint64_t ABIAlign = 4; |
| 3375 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 3376 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3377 | getABIKind() == ARMABIInfo::AAPCS) |
| 3378 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 885ad69 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 3379 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
| 3380 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true, |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3381 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Daniel Dunbar | 8aa87c7 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 3384 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3385 | llvm::Type* ElemTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3386 | unsigned SizeRegs; |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3387 | // FIXME: Try to match the types of the arguments more accurately where |
| 3388 | // we can. |
| 3389 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 3390 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 3391 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3392 | } else { |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3393 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3394 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 3395 | } |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3396 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3397 | llvm::Type *STy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3398 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3399 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3402 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3403 | llvm::LLVMContext &VMContext) { |
| 3404 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 3405 | // is called integer-like if its size is less than or equal to one word, and |
| 3406 | // the offset of each of its addressable sub-fields is zero. |
| 3407 | |
| 3408 | uint64_t Size = Context.getTypeSize(Ty); |
| 3409 | |
| 3410 | // Check that the type fits in a word. |
| 3411 | if (Size > 32) |
| 3412 | return false; |
| 3413 | |
| 3414 | // FIXME: Handle vector types! |
| 3415 | if (Ty->isVectorType()) |
| 3416 | return false; |
| 3417 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 3418 | // Float types are never treated as "integer like". |
| 3419 | if (Ty->isRealFloatingType()) |
| 3420 | return false; |
| 3421 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3422 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3423 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3424 | return true; |
| 3425 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 3426 | // Small complex integer types are "integer like". |
| 3427 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 3428 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3429 | |
| 3430 | // Single element and zero sized arrays should be allowed, by the definition |
| 3431 | // above, but they are not. |
| 3432 | |
| 3433 | // Otherwise, it must be a record type. |
| 3434 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3435 | if (!RT) return false; |
| 3436 | |
| 3437 | // Ignore records with flexible arrays. |
| 3438 | const RecordDecl *RD = RT->getDecl(); |
| 3439 | if (RD->hasFlexibleArrayMember()) |
| 3440 | return false; |
| 3441 | |
| 3442 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 3443 | // like". |
| 3444 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3445 | |
| 3446 | bool HadField = false; |
| 3447 | unsigned idx = 0; |
| 3448 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3449 | i != e; ++i, ++idx) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3450 | const FieldDecl *FD = *i; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3451 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3452 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 3453 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 3454 | // struct { int : 0; int x } |
| 3455 | // is non-integer like according to gcc. |
| 3456 | if (FD->isBitField()) { |
| 3457 | if (!RD->isUnion()) |
| 3458 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3459 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3460 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3461 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3462 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3463 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3466 | // Check if this field is at offset 0. |
| 3467 | if (Layout.getFieldOffset(idx) != 0) |
| 3468 | return false; |
| 3469 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3470 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3471 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3472 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3473 | // Only allow at most one field in a structure. This doesn't match the |
| 3474 | // wording above, but follows gcc in situations with a field following an |
| 3475 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3476 | if (!RD->isUnion()) { |
| 3477 | if (HadField) |
| 3478 | return false; |
| 3479 | |
| 3480 | HadField = true; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | return true; |
| 3485 | } |
| 3486 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3487 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3488 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3489 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3490 | |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3491 | // Large vector types should be returned via memory. |
| 3492 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3493 | return ABIArgInfo::getIndirect(0); |
| 3494 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3495 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3496 | // Treat an enum type as its underlying type. |
| 3497 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3498 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3499 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3500 | return (RetTy->isPromotableIntegerType() ? |
| 3501 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3502 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3503 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3504 | // Structures with either a non-trivial destructor or a non-trivial |
| 3505 | // copy constructor are always indirect. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3506 | if (isRecordReturnIndirect(RetTy, CGT)) |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3507 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3508 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3509 | // Are we following APCS? |
| 3510 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3511 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3512 | return ABIArgInfo::getIgnore(); |
| 3513 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3514 | // Complex types are all returned as packed integers. |
| 3515 | // |
| 3516 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 3517 | // correctly. |
| 3518 | if (RetTy->isAnyComplexType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3519 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3520 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3521 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3522 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3523 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3524 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3525 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3526 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3527 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3528 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3529 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3530 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | // Otherwise return in memory. |
| 3534 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3535 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3536 | |
| 3537 | // Otherwise this is an AAPCS variant. |
| 3538 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3539 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3540 | return ABIArgInfo::getIgnore(); |
| 3541 | |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3542 | // Check for homogeneous aggregates with AAPCS-VFP. |
| 3543 | if (getABIKind() == AAPCS_VFP) { |
| 3544 | const Type *Base = 0; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3545 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 3546 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3547 | // Homogeneous Aggregates are returned directly. |
| 3548 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3549 | } |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3552 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 3553 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3554 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3555 | if (Size <= 32) { |
| 3556 | // Return in the smallest viable integer type. |
| 3557 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3558 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3559 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3560 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3561 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3564 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3567 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 3568 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3569 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3570 | // Check whether VT is legal. |
| 3571 | unsigned NumElements = VT->getNumElements(); |
| 3572 | uint64_t Size = getContext().getTypeSize(VT); |
| 3573 | // NumElements should be power of 2. |
| 3574 | if ((NumElements & (NumElements - 1)) != 0) |
| 3575 | return true; |
| 3576 | // Size should be greater than 32 bits. |
| 3577 | return Size <= 32; |
| 3578 | } |
| 3579 | return false; |
| 3580 | } |
| 3581 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3582 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3583 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3584 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3585 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3586 | |
| 3587 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3588 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3589 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3590 | |
| 3591 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3592 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3593 | bool IsIndirect = false; |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3594 | |
| 3595 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 3596 | // 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] | 3597 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3598 | getABIKind() == ARMABIInfo::AAPCS) |
| 3599 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 3600 | else |
| 3601 | TyAlign = 4; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3602 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 3603 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 3604 | IsIndirect = true; |
| 3605 | Size = 4; |
| 3606 | TyAlign = 4; |
| 3607 | } |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3608 | |
| 3609 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3610 | if (TyAlign > 4) { |
| 3611 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 3612 | "Alignment is not power of 2!"); |
| 3613 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3614 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 3615 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3616 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3617 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3618 | |
| 3619 | uint64_t Offset = |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3620 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3621 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3622 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3623 | "ap.next"); |
| 3624 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3625 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3626 | if (IsIndirect) |
| 3627 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 9337102 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3628 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3629 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 3630 | // may not be correctly aligned for the vector type. We create an aligned |
| 3631 | // temporary space and copy the content over from ap.cur to the temporary |
| 3632 | // space. This is necessary if the natural alignment of the type is greater |
| 3633 | // than the ABI alignment. |
| 3634 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 3635 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 3636 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 3637 | "var.align"); |
| 3638 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 3639 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 3640 | Builder.CreateMemCpy(Dst, Src, |
| 3641 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 3642 | TyAlign, false); |
| 3643 | Addr = AlignedTemp; //The content is in aligned location. |
| 3644 | } |
| 3645 | llvm::Type *PTy = |
| 3646 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3647 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3648 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3649 | return AddrTyped; |
| 3650 | } |
| 3651 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3652 | namespace { |
| 3653 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3654 | class NaClARMABIInfo : public ABIInfo { |
| 3655 | public: |
| 3656 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3657 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
| 3658 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3659 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3660 | CodeGenFunction &CGF) const; |
| 3661 | private: |
| 3662 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 3663 | ARMABIInfo NInfo; // Used for everything else. |
| 3664 | }; |
| 3665 | |
| 3666 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3667 | public: |
| 3668 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3669 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 3670 | }; |
| 3671 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3674 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3675 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 3676 | PInfo.computeInfo(FI); |
| 3677 | else |
| 3678 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 3679 | } |
| 3680 | |
| 3681 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3682 | CodeGenFunction &CGF) const { |
| 3683 | // Always use the native convention; calling pnacl-style varargs functions |
| 3684 | // is unsupported. |
| 3685 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 3686 | } |
| 3687 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3688 | //===----------------------------------------------------------------------===// |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3689 | // AArch64 ABI Implementation |
| 3690 | //===----------------------------------------------------------------------===// |
| 3691 | |
| 3692 | namespace { |
| 3693 | |
| 3694 | class AArch64ABIInfo : public ABIInfo { |
| 3695 | public: |
| 3696 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 3697 | |
| 3698 | private: |
| 3699 | // The AArch64 PCS is explicit about return types and argument types being |
| 3700 | // handled identically, so we don't need to draw a distinction between |
| 3701 | // Argument and Return classification. |
| 3702 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 3703 | int &FreeVFPRegs) const; |
| 3704 | |
| 3705 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 3706 | llvm::Type *DirectTy = 0) const; |
| 3707 | |
| 3708 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3709 | |
| 3710 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3711 | CodeGenFunction &CGF) const; |
| 3712 | }; |
| 3713 | |
| 3714 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3715 | public: |
| 3716 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3717 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 3718 | |
| 3719 | const AArch64ABIInfo &getABIInfo() const { |
| 3720 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3721 | } |
| 3722 | |
| 3723 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3724 | return 31; |
| 3725 | } |
| 3726 | |
| 3727 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3728 | llvm::Value *Address) const { |
| 3729 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 3730 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 3731 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 3732 | |
| 3733 | // 64-95 are v0-v31: 16 bytes each |
| 3734 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 3735 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 3736 | |
| 3737 | return false; |
| 3738 | } |
| 3739 | |
| 3740 | }; |
| 3741 | |
| 3742 | } |
| 3743 | |
| 3744 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3745 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3746 | |
| 3747 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 3748 | FreeIntRegs, FreeVFPRegs); |
| 3749 | |
| 3750 | FreeIntRegs = FreeVFPRegs = 8; |
| 3751 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3752 | it != ie; ++it) { |
| 3753 | it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs); |
| 3754 | |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | ABIArgInfo |
| 3759 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 3760 | bool IsInt, llvm::Type *DirectTy) const { |
| 3761 | if (FreeRegs >= RegsNeeded) { |
| 3762 | FreeRegs -= RegsNeeded; |
| 3763 | return ABIArgInfo::getDirect(DirectTy); |
| 3764 | } |
| 3765 | |
| 3766 | llvm::Type *Padding = 0; |
| 3767 | |
| 3768 | // We need padding so that later arguments don't get filled in anyway. That |
| 3769 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 3770 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 3771 | // concerned. |
| 3772 | if (FreeRegs > 0) { |
| 3773 | if (IsInt) |
| 3774 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 3775 | else |
| 3776 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 3777 | |
| 3778 | // Either [N x i64] or [N x float]. |
| 3779 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 3780 | FreeRegs = 0; |
| 3781 | } |
| 3782 | |
| 3783 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 3784 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 3785 | Padding); |
| 3786 | } |
| 3787 | |
| 3788 | |
| 3789 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 3790 | int &FreeIntRegs, |
| 3791 | int &FreeVFPRegs) const { |
| 3792 | // Can only occurs for return, but harmless otherwise. |
| 3793 | if (Ty->isVoidType()) |
| 3794 | return ABIArgInfo::getIgnore(); |
| 3795 | |
| 3796 | // Large vector types should be returned via memory. There's no such concept |
| 3797 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 3798 | // classified they'd go into memory (see B.3). |
| 3799 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 3800 | if (FreeIntRegs > 0) |
| 3801 | --FreeIntRegs; |
| 3802 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3803 | } |
| 3804 | |
| 3805 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 3806 | // be passed directly. After this block we're guaranteed to be in a |
| 3807 | // complicated case. |
| 3808 | if (!isAggregateTypeForABI(Ty)) { |
| 3809 | // Treat an enum type as its underlying type. |
| 3810 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3811 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3812 | |
| 3813 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 3814 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 3815 | |
| 3816 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 3817 | "unexpectedly large scalar type"); |
| 3818 | |
| 3819 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3820 | |
| 3821 | // If the type may need padding registers to ensure "alignment", we must be |
| 3822 | // careful when this is accounted for. Increasing the effective size covers |
| 3823 | // all cases. |
| 3824 | if (getContext().getTypeAlign(Ty) == 128) |
| 3825 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 3826 | |
| 3827 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 3828 | } |
| 3829 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3830 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { |
| 3831 | if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3832 | --FreeIntRegs; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3833 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3837 | if (!getContext().getLangOpts().CPlusPlus) { |
| 3838 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 3839 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 3840 | // the object for parameter-passsing purposes. |
| 3841 | return ABIArgInfo::getIgnore(); |
| 3842 | } |
| 3843 | |
| 3844 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 3845 | // description of va_arg in the PCS require that an empty struct does |
| 3846 | // actually occupy space for parameter-passing. I'm hoping for a |
| 3847 | // clarification giving an explicit paragraph to point to in future. |
| 3848 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 3849 | llvm::Type::getInt8Ty(getVMContext())); |
| 3850 | } |
| 3851 | |
| 3852 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 3853 | const Type *Base = 0; |
| 3854 | uint64_t NumMembers = 0; |
| 3855 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 3856 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 3857 | // Homogeneous aggregates are passed and returned directly. |
| 3858 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 3859 | /*IsInt=*/ false); |
| 3860 | } |
| 3861 | |
| 3862 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3863 | if (Size <= 128) { |
| 3864 | // Small structs can use the same direct type whether they're in registers |
| 3865 | // or on the stack. |
| 3866 | llvm::Type *BaseTy; |
| 3867 | unsigned NumBases; |
| 3868 | int SizeInRegs = (Size + 63) / 64; |
| 3869 | |
| 3870 | if (getContext().getTypeAlign(Ty) == 128) { |
| 3871 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 3872 | NumBases = 1; |
| 3873 | |
| 3874 | // If the type may need padding registers to ensure "alignment", we must |
| 3875 | // be careful when this is accounted for. Increasing the effective size |
| 3876 | // covers all cases. |
| 3877 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 3878 | } else { |
| 3879 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3880 | NumBases = SizeInRegs; |
| 3881 | } |
| 3882 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 3883 | |
| 3884 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 3885 | /*IsInt=*/ true, DirectTy); |
| 3886 | } |
| 3887 | |
| 3888 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 3889 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 3890 | --FreeIntRegs; |
| 3891 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 3892 | } |
| 3893 | |
| 3894 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3895 | CodeGenFunction &CGF) const { |
| 3896 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3897 | // Standard, section B.4: |
| 3898 | // |
| 3899 | // struct { |
| 3900 | // void *__stack; |
| 3901 | // void *__gr_top; |
| 3902 | // void *__vr_top; |
| 3903 | // int __gr_offs; |
| 3904 | // int __vr_offs; |
| 3905 | // }; |
| 3906 | |
| 3907 | assert(!CGF.CGM.getDataLayout().isBigEndian() |
| 3908 | && "va_arg not implemented for big-endian AArch64"); |
| 3909 | |
| 3910 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3911 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 3912 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 3913 | |
| 3914 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3915 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3916 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3917 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3918 | |
| 3919 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 3920 | int reg_top_index; |
| 3921 | int RegSize; |
| 3922 | if (FreeIntRegs < 8) { |
| 3923 | assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs"); |
| 3924 | // 3 is the field number of __gr_offs |
| 3925 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3926 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3927 | reg_top_index = 1; // field number for __gr_top |
| 3928 | RegSize = 8 * (8 - FreeIntRegs); |
| 3929 | } else { |
| 3930 | assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs"); |
| 3931 | // 4 is the field number of __vr_offs. |
| 3932 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3933 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3934 | reg_top_index = 2; // field number for __vr_top |
| 3935 | RegSize = 16 * (8 - FreeVFPRegs); |
| 3936 | } |
| 3937 | |
| 3938 | //======================================= |
| 3939 | // Find out where argument was passed |
| 3940 | //======================================= |
| 3941 | |
| 3942 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3943 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3944 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3945 | // whatever they get). |
| 3946 | llvm::Value *UsingStack = 0; |
| 3947 | UsingStack = CGF.Builder.CreateICmpSGE(reg_offs, |
| 3948 | llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3949 | |
| 3950 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3951 | |
| 3952 | // Otherwise, at least some kind of argument could go in these registers, the |
| 3953 | // quesiton is whether this particular type is too big. |
| 3954 | CGF.EmitBlock(MaybeRegBlock); |
| 3955 | |
| 3956 | // Integer arguments may need to correct register alignment (for example a |
| 3957 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3958 | // align __gr_offs to calculate the potential address. |
| 3959 | if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 3960 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 3961 | |
| 3962 | reg_offs = CGF.Builder.CreateAdd(reg_offs, |
| 3963 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3964 | "align_regoffs"); |
| 3965 | reg_offs = CGF.Builder.CreateAnd(reg_offs, |
| 3966 | llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3967 | "aligned_regoffs"); |
| 3968 | } |
| 3969 | |
| 3970 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 3971 | llvm::Value *NewOffset = 0; |
| 3972 | NewOffset = CGF.Builder.CreateAdd(reg_offs, |
| 3973 | llvm::ConstantInt::get(CGF.Int32Ty, RegSize), |
| 3974 | "new_reg_offs"); |
| 3975 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3976 | |
| 3977 | // Now we're in a position to decide whether this argument really was in |
| 3978 | // registers or not. |
| 3979 | llvm::Value *InRegs = 0; |
| 3980 | InRegs = CGF.Builder.CreateICmpSLE(NewOffset, |
| 3981 | llvm::ConstantInt::get(CGF.Int32Ty, 0), |
| 3982 | "inreg"); |
| 3983 | |
| 3984 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3985 | |
| 3986 | //======================================= |
| 3987 | // Argument was in registers |
| 3988 | //======================================= |
| 3989 | |
| 3990 | // Now we emit the code for if the argument was originally passed in |
| 3991 | // registers. First start the appropriate block: |
| 3992 | CGF.EmitBlock(InRegBlock); |
| 3993 | |
| 3994 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 3995 | reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3996 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3997 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 3998 | llvm::Value *RegAddr = 0; |
| 3999 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4000 | |
| 4001 | if (!AI.isDirect()) { |
| 4002 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4003 | // stored registers or on the stack will actually be a struct **. |
| 4004 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4005 | } |
| 4006 | |
| 4007 | const Type *Base = 0; |
| 4008 | uint64_t NumMembers; |
| 4009 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers) |
| 4010 | && NumMembers > 1) { |
| 4011 | // Homogeneous aggregates passed in registers will have their elements split |
| 4012 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4013 | // qN+1, ...). We reload and store into a temporary local variable |
| 4014 | // contiguously. |
| 4015 | assert(AI.isDirect() && "Homogeneous aggregates should be passed directly"); |
| 4016 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4017 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 4018 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 4019 | |
| 4020 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4021 | llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i); |
| 4022 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4023 | LoadAddr = CGF.Builder.CreateBitCast(LoadAddr, |
| 4024 | llvm::PointerType::getUnqual(BaseTy)); |
| 4025 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 4026 | |
| 4027 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4028 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4029 | } |
| 4030 | |
| 4031 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4032 | } else { |
| 4033 | // Otherwise the object is contiguous in memory |
| 4034 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4035 | } |
| 4036 | |
| 4037 | CGF.EmitBranch(ContBlock); |
| 4038 | |
| 4039 | //======================================= |
| 4040 | // Argument was on the stack |
| 4041 | //======================================= |
| 4042 | CGF.EmitBlock(OnStackBlock); |
| 4043 | |
| 4044 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 4045 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 4046 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4047 | |
| 4048 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4049 | // floating-point ones might be affected. |
| 4050 | if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 4051 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 4052 | |
| 4053 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4054 | |
| 4055 | OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, |
| 4056 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4057 | "align_stack"); |
| 4058 | OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr, |
| 4059 | llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4060 | "align_stack"); |
| 4061 | |
| 4062 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4063 | } |
| 4064 | |
| 4065 | uint64_t StackSize; |
| 4066 | if (AI.isDirect()) |
| 4067 | StackSize = getContext().getTypeSize(Ty) / 8; |
| 4068 | else |
| 4069 | StackSize = 8; |
| 4070 | |
| 4071 | // All stack slots are 8 bytes |
| 4072 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4073 | |
| 4074 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4075 | llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, |
| 4076 | "new_stack"); |
| 4077 | |
| 4078 | // Write the new value of __stack for the next call to va_arg |
| 4079 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4080 | |
| 4081 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4082 | |
| 4083 | CGF.EmitBranch(ContBlock); |
| 4084 | |
| 4085 | //======================================= |
| 4086 | // Tidy up |
| 4087 | //======================================= |
| 4088 | CGF.EmitBlock(ContBlock); |
| 4089 | |
| 4090 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4091 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4092 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4093 | |
| 4094 | if (AI.isDirect()) |
| 4095 | return ResAddr; |
| 4096 | |
| 4097 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4098 | } |
| 4099 | |
| 4100 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4101 | // NVPTX ABI Implementation |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4102 | //===----------------------------------------------------------------------===// |
| 4103 | |
| 4104 | namespace { |
| 4105 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4106 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4107 | public: |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4108 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4109 | |
| 4110 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4111 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4112 | |
| 4113 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4114 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4115 | CodeGenFunction &CFG) const; |
| 4116 | }; |
| 4117 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4118 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4119 | public: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4120 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4121 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4122 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4123 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4124 | CodeGen::CodeGenModule &M) const; |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4125 | private: |
| 4126 | static void addKernelMetadata(llvm::Function *F); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4127 | }; |
| 4128 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4129 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4130 | if (RetTy->isVoidType()) |
| 4131 | return ABIArgInfo::getIgnore(); |
| 4132 | if (isAggregateTypeForABI(RetTy)) |
| 4133 | return ABIArgInfo::getIndirect(0); |
| 4134 | return ABIArgInfo::getDirect(); |
| 4135 | } |
| 4136 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4137 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4138 | if (isAggregateTypeForABI(Ty)) |
| 4139 | return ABIArgInfo::getIndirect(0); |
| 4140 | |
| 4141 | return ABIArgInfo::getDirect(); |
| 4142 | } |
| 4143 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4144 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4145 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4146 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4147 | it != ie; ++it) |
| 4148 | it->info = classifyArgumentType(it->type); |
| 4149 | |
| 4150 | // Always honor user-specified calling convention. |
| 4151 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4152 | return; |
| 4153 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4154 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4155 | } |
| 4156 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4157 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4158 | CodeGenFunction &CFG) const { |
| 4159 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4162 | void NVPTXTargetCodeGenInfo:: |
| 4163 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4164 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4165 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4166 | if (!FD) return; |
| 4167 | |
| 4168 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4169 | |
| 4170 | // Perform special handling in OpenCL mode |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4171 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4172 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4173 | // By default, all functions are device functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4174 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4175 | // OpenCL __kernel functions get kernel metadata |
| 4176 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4177 | // And kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4178 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4179 | } |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4180 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4181 | |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4182 | // Perform special handling in CUDA mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4183 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4184 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4185 | // __global__ functions cannot be called from the device, we do not |
| 4186 | // need to set the noinline attribute. |
| 4187 | if (FD->getAttr<CUDAGlobalAttr>()) |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4188 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4192 | void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) { |
| 4193 | llvm::Module *M = F->getParent(); |
| 4194 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4195 | |
| 4196 | // Get "nvvm.annotations" metadata node |
| 4197 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4198 | |
| 4199 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4200 | llvm::SmallVector<llvm::Value *, 3> MDVals; |
| 4201 | MDVals.push_back(F); |
| 4202 | MDVals.push_back(llvm::MDString::get(Ctx, "kernel")); |
| 4203 | MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)); |
| 4204 | |
| 4205 | // Append metadata to nvvm.annotations |
| 4206 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4207 | } |
| 4208 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4212 | // SystemZ ABI Implementation |
| 4213 | //===----------------------------------------------------------------------===// |
| 4214 | |
| 4215 | namespace { |
| 4216 | |
| 4217 | class SystemZABIInfo : public ABIInfo { |
| 4218 | public: |
| 4219 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4220 | |
| 4221 | bool isPromotableIntegerType(QualType Ty) const; |
| 4222 | bool isCompoundType(QualType Ty) const; |
| 4223 | bool isFPArgumentType(QualType Ty) const; |
| 4224 | |
| 4225 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4226 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4227 | |
| 4228 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 4229 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4230 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4231 | it != ie; ++it) |
| 4232 | it->info = classifyArgumentType(it->type); |
| 4233 | } |
| 4234 | |
| 4235 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4236 | CodeGenFunction &CGF) const; |
| 4237 | }; |
| 4238 | |
| 4239 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4240 | public: |
| 4241 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4242 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4243 | }; |
| 4244 | |
| 4245 | } |
| 4246 | |
| 4247 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4248 | // Treat an enum type as its underlying type. |
| 4249 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4250 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4251 | |
| 4252 | // Promotable integer types are required to be promoted by the ABI. |
| 4253 | if (Ty->isPromotableIntegerType()) |
| 4254 | return true; |
| 4255 | |
| 4256 | // 32-bit values must also be promoted. |
| 4257 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4258 | switch (BT->getKind()) { |
| 4259 | case BuiltinType::Int: |
| 4260 | case BuiltinType::UInt: |
| 4261 | return true; |
| 4262 | default: |
| 4263 | return false; |
| 4264 | } |
| 4265 | return false; |
| 4266 | } |
| 4267 | |
| 4268 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 4269 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 4270 | } |
| 4271 | |
| 4272 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 4273 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4274 | switch (BT->getKind()) { |
| 4275 | case BuiltinType::Float: |
| 4276 | case BuiltinType::Double: |
| 4277 | return true; |
| 4278 | default: |
| 4279 | return false; |
| 4280 | } |
| 4281 | |
| 4282 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 4283 | const RecordDecl *RD = RT->getDecl(); |
| 4284 | bool Found = false; |
| 4285 | |
| 4286 | // If this is a C++ record, check the bases first. |
| 4287 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 4288 | for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(), |
| 4289 | E = CXXRD->bases_end(); I != E; ++I) { |
| 4290 | QualType Base = I->getType(); |
| 4291 | |
| 4292 | // Empty bases don't affect things either way. |
| 4293 | if (isEmptyRecord(getContext(), Base, true)) |
| 4294 | continue; |
| 4295 | |
| 4296 | if (Found) |
| 4297 | return false; |
| 4298 | Found = isFPArgumentType(Base); |
| 4299 | if (!Found) |
| 4300 | return false; |
| 4301 | } |
| 4302 | |
| 4303 | // Check the fields. |
| 4304 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 4305 | E = RD->field_end(); I != E; ++I) { |
| 4306 | const FieldDecl *FD = *I; |
| 4307 | |
| 4308 | // Empty bitfields don't affect things either way. |
| 4309 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 4310 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 4311 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4312 | return true; |
| 4313 | |
| 4314 | // Unlike isSingleElementStruct(), arrays do not count. |
| 4315 | // Nested isFPArgumentType structures still do though. |
| 4316 | if (Found) |
| 4317 | return false; |
| 4318 | Found = isFPArgumentType(FD->getType()); |
| 4319 | if (!Found) |
| 4320 | return false; |
| 4321 | } |
| 4322 | |
| 4323 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 4324 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 4325 | return Found; |
| 4326 | } |
| 4327 | |
| 4328 | return false; |
| 4329 | } |
| 4330 | |
| 4331 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4332 | CodeGenFunction &CGF) const { |
| 4333 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 4334 | // struct { |
| 4335 | // i64 __gpr; |
| 4336 | // i64 __fpr; |
| 4337 | // i8 *__overflow_arg_area; |
| 4338 | // i8 *__reg_save_area; |
| 4339 | // }; |
| 4340 | |
| 4341 | // Every argument occupies 8 bytes and is passed by preference in either |
| 4342 | // GPRs or FPRs. |
| 4343 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4344 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 4345 | bool InFPRs = isFPArgumentType(Ty); |
| 4346 | |
| 4347 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4348 | bool IsIndirect = AI.isIndirect(); |
| 4349 | unsigned UnpaddedBitSize; |
| 4350 | if (IsIndirect) { |
| 4351 | APTy = llvm::PointerType::getUnqual(APTy); |
| 4352 | UnpaddedBitSize = 64; |
| 4353 | } else |
| 4354 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 4355 | unsigned PaddedBitSize = 64; |
| 4356 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 4357 | |
| 4358 | unsigned PaddedSize = PaddedBitSize / 8; |
| 4359 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 4360 | |
| 4361 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 4362 | if (InFPRs) { |
| 4363 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 4364 | RegCountField = 1; // __fpr |
| 4365 | RegSaveIndex = 16; // save offset for f0 |
| 4366 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 4367 | } else { |
| 4368 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 4369 | RegCountField = 0; // __gpr |
| 4370 | RegSaveIndex = 2; // save offset for r2 |
| 4371 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 4372 | } |
| 4373 | |
| 4374 | llvm::Value *RegCountPtr = |
| 4375 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 4376 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 4377 | llvm::Type *IndexTy = RegCount->getType(); |
| 4378 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 4379 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
| 4380 | "fits_in_regs"); |
| 4381 | |
| 4382 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4383 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 4384 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4385 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 4386 | |
| 4387 | // Emit code to load the value if it was passed in registers. |
| 4388 | CGF.EmitBlock(InRegBlock); |
| 4389 | |
| 4390 | // Work out the address of an argument register. |
| 4391 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 4392 | llvm::Value *ScaledRegCount = |
| 4393 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 4394 | llvm::Value *RegBase = |
| 4395 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 4396 | llvm::Value *RegOffset = |
| 4397 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 4398 | llvm::Value *RegSaveAreaPtr = |
| 4399 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 4400 | llvm::Value *RegSaveArea = |
| 4401 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 4402 | llvm::Value *RawRegAddr = |
| 4403 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 4404 | llvm::Value *RegAddr = |
| 4405 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 4406 | |
| 4407 | // Update the register count |
| 4408 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 4409 | llvm::Value *NewRegCount = |
| 4410 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 4411 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 4412 | CGF.EmitBranch(ContBlock); |
| 4413 | |
| 4414 | // Emit code to load the value if it was passed in memory. |
| 4415 | CGF.EmitBlock(InMemBlock); |
| 4416 | |
| 4417 | // Work out the address of a stack argument. |
| 4418 | llvm::Value *OverflowArgAreaPtr = |
| 4419 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 4420 | llvm::Value *OverflowArgArea = |
| 4421 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 4422 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 4423 | llvm::Value *RawMemAddr = |
| 4424 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 4425 | llvm::Value *MemAddr = |
| 4426 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 4427 | |
| 4428 | // Update overflow_arg_area_ptr pointer |
| 4429 | llvm::Value *NewOverflowArgArea = |
| 4430 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 4431 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 4432 | CGF.EmitBranch(ContBlock); |
| 4433 | |
| 4434 | // Return the appropriate result. |
| 4435 | CGF.EmitBlock(ContBlock); |
| 4436 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 4437 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4438 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 4439 | |
| 4440 | if (IsIndirect) |
| 4441 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 4442 | |
| 4443 | return ResAddr; |
| 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 4448 | if (RetTy->isVoidType()) |
| 4449 | return ABIArgInfo::getIgnore(); |
| 4450 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 4451 | return ABIArgInfo::getIndirect(0); |
| 4452 | return (isPromotableIntegerType(RetTy) ? |
| 4453 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4454 | } |
| 4455 | |
| 4456 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 4457 | // Handle the generic C++ ABI. |
| 4458 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 4459 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 4460 | |
| 4461 | // Integers and enums are extended to full register width. |
| 4462 | if (isPromotableIntegerType(Ty)) |
| 4463 | return ABIArgInfo::getExtend(); |
| 4464 | |
| 4465 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 4466 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4467 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
| 4468 | return ABIArgInfo::getIndirect(0); |
| 4469 | |
| 4470 | // Handle small structures. |
| 4471 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4472 | // Structures with flexible arrays have variable length, so really |
| 4473 | // fail the size test above. |
| 4474 | const RecordDecl *RD = RT->getDecl(); |
| 4475 | if (RD->hasFlexibleArrayMember()) |
| 4476 | return ABIArgInfo::getIndirect(0); |
| 4477 | |
| 4478 | // The structure is passed as an unextended integer, a float, or a double. |
| 4479 | llvm::Type *PassTy; |
| 4480 | if (isFPArgumentType(Ty)) { |
| 4481 | assert(Size == 32 || Size == 64); |
| 4482 | if (Size == 32) |
| 4483 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 4484 | else |
| 4485 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 4486 | } else |
| 4487 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4488 | return ABIArgInfo::getDirect(PassTy); |
| 4489 | } |
| 4490 | |
| 4491 | // Non-structure compounds are passed indirectly. |
| 4492 | if (isCompoundType(Ty)) |
| 4493 | return ABIArgInfo::getIndirect(0); |
| 4494 | |
| 4495 | return ABIArgInfo::getDirect(0); |
| 4496 | } |
| 4497 | |
| 4498 | //===----------------------------------------------------------------------===// |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4499 | // MBlaze ABI Implementation |
| 4500 | //===----------------------------------------------------------------------===// |
| 4501 | |
| 4502 | namespace { |
| 4503 | |
| 4504 | class MBlazeABIInfo : public ABIInfo { |
| 4505 | public: |
| 4506 | MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4507 | |
| 4508 | bool isPromotableIntegerType(QualType Ty) const; |
| 4509 | |
| 4510 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4511 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 4512 | |
| 4513 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 4514 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4515 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4516 | it != ie; ++it) |
| 4517 | it->info = classifyArgumentType(it->type); |
| 4518 | } |
| 4519 | |
| 4520 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4521 | CodeGenFunction &CGF) const; |
| 4522 | }; |
| 4523 | |
| 4524 | class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4525 | public: |
| 4526 | MBlazeTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4527 | : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {} |
| 4528 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4529 | CodeGen::CodeGenModule &M) const; |
| 4530 | }; |
| 4531 | |
| 4532 | } |
| 4533 | |
| 4534 | bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4535 | // MBlaze ABI requires all 8 and 16 bit quantities to be extended. |
| 4536 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4537 | switch (BT->getKind()) { |
| 4538 | case BuiltinType::Bool: |
| 4539 | case BuiltinType::Char_S: |
| 4540 | case BuiltinType::Char_U: |
| 4541 | case BuiltinType::SChar: |
| 4542 | case BuiltinType::UChar: |
| 4543 | case BuiltinType::Short: |
| 4544 | case BuiltinType::UShort: |
| 4545 | return true; |
| 4546 | default: |
| 4547 | return false; |
| 4548 | } |
| 4549 | return false; |
| 4550 | } |
| 4551 | |
| 4552 | llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4553 | CodeGenFunction &CGF) const { |
| 4554 | // FIXME: Implement |
| 4555 | return 0; |
| 4556 | } |
| 4557 | |
| 4558 | |
| 4559 | ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const { |
| 4560 | if (RetTy->isVoidType()) |
| 4561 | return ABIArgInfo::getIgnore(); |
| 4562 | if (isAggregateTypeForABI(RetTy)) |
| 4563 | return ABIArgInfo::getIndirect(0); |
| 4564 | |
| 4565 | return (isPromotableIntegerType(RetTy) ? |
| 4566 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4567 | } |
| 4568 | |
| 4569 | ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const { |
| 4570 | if (isAggregateTypeForABI(Ty)) |
| 4571 | return ABIArgInfo::getIndirect(0); |
| 4572 | |
| 4573 | return (isPromotableIntegerType(Ty) ? |
| 4574 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4575 | } |
| 4576 | |
| 4577 | void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4578 | llvm::GlobalValue *GV, |
| 4579 | CodeGen::CodeGenModule &M) |
| 4580 | const { |
| 4581 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4582 | if (!FD) return; |
NAKAMURA Takumi | 125b4cb | 2011-02-17 08:50:50 +0000 | [diff] [blame] | 4583 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4584 | llvm::CallingConv::ID CC = llvm::CallingConv::C; |
| 4585 | if (FD->hasAttr<MBlazeInterruptHandlerAttr>()) |
| 4586 | CC = llvm::CallingConv::MBLAZE_INTR; |
| 4587 | else if (FD->hasAttr<MBlazeSaveVolatilesAttr>()) |
| 4588 | CC = llvm::CallingConv::MBLAZE_SVOL; |
| 4589 | |
| 4590 | if (CC != llvm::CallingConv::C) { |
| 4591 | // Handle 'interrupt_handler' attribute: |
| 4592 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4593 | |
| 4594 | // Step 1: Set ISR calling convention. |
| 4595 | F->setCallingConv(CC); |
| 4596 | |
| 4597 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4598 | F->addFnAttr(llvm::Attribute::NoInline); |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | // Step 3: Emit _interrupt_handler alias. |
| 4602 | if (CC == llvm::CallingConv::MBLAZE_INTR) |
| 4603 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
| 4604 | "_interrupt_handler", GV, &M.getModule()); |
| 4605 | } |
| 4606 | |
| 4607 | |
| 4608 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4609 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4610 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4611 | |
| 4612 | namespace { |
| 4613 | |
| 4614 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4615 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4616 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4617 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4618 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4619 | CodeGen::CodeGenModule &M) const; |
| 4620 | }; |
| 4621 | |
| 4622 | } |
| 4623 | |
| 4624 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4625 | llvm::GlobalValue *GV, |
| 4626 | CodeGen::CodeGenModule &M) const { |
| 4627 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4628 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 4629 | // Handle 'interrupt' attribute: |
| 4630 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4631 | |
| 4632 | // Step 1: Set ISR calling convention. |
| 4633 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 4634 | |
| 4635 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4636 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4637 | |
| 4638 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4639 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4640 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4641 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4642 | GV, &M.getModule()); |
| 4643 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4644 | } |
| 4645 | } |
| 4646 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4647 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4648 | // MIPS ABI Implementation. This works for both little-endian and |
| 4649 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4650 | //===----------------------------------------------------------------------===// |
| 4651 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4652 | namespace { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4653 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4654 | bool IsO32; |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4655 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 4656 | void CoerceToIntArgs(uint64_t TySize, |
| 4657 | SmallVector<llvm::Type*, 8> &ArgList) const; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4658 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4659 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4660 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4661 | public: |
Akira Hatanaka | b551dd3 | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 4662 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4663 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
| 4664 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4665 | |
| 4666 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4667 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4668 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4669 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4670 | CodeGenFunction &CGF) const; |
| 4671 | }; |
| 4672 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4673 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4674 | unsigned SizeOfUnwindException; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4675 | public: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4676 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 4677 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
| 4678 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4679 | |
| 4680 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 4681 | return 29; |
| 4682 | } |
| 4683 | |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4684 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4685 | CodeGen::CodeGenModule &CGM) const { |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4686 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4687 | if (!FD) return; |
Rafael Espindola | d8e6d6d | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 4688 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4689 | if (FD->hasAttr<Mips16Attr>()) { |
| 4690 | Fn->addFnAttr("mips16"); |
| 4691 | } |
| 4692 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 4693 | Fn->addFnAttr("nomips16"); |
| 4694 | } |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4695 | } |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4696 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4697 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4698 | llvm::Value *Address) const; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4699 | |
| 4700 | unsigned getSizeOfUnwindException() const { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4701 | return SizeOfUnwindException; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4702 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4703 | }; |
| 4704 | } |
| 4705 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4706 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
| 4707 | SmallVector<llvm::Type*, 8> &ArgList) const { |
| 4708 | llvm::IntegerType *IntTy = |
| 4709 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4710 | |
| 4711 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 4712 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 4713 | ArgList.push_back(IntTy); |
| 4714 | |
| 4715 | // If necessary, add one more integer type to ArgList. |
| 4716 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 4717 | |
| 4718 | if (R) |
| 4719 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4722 | // In N32/64, an aligned double precision floating point field is passed in |
| 4723 | // a register. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4724 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4725 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 4726 | |
| 4727 | if (IsO32) { |
| 4728 | CoerceToIntArgs(TySize, ArgList); |
| 4729 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4730 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4731 | |
Akira Hatanaka | 2afd23d | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 4732 | if (Ty->isComplexType()) |
| 4733 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 6d1080f | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 4734 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4735 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4736 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4737 | // Unions/vectors are passed in integer registers. |
| 4738 | if (!RT || !RT->isStructureOrClassType()) { |
| 4739 | CoerceToIntArgs(TySize, ArgList); |
| 4740 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4741 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4742 | |
| 4743 | const RecordDecl *RD = RT->getDecl(); |
| 4744 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4745 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4746 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4747 | uint64_t LastOffset = 0; |
| 4748 | unsigned idx = 0; |
| 4749 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 4750 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4751 | // Iterate over fields in the struct/class and check if there are any aligned |
| 4752 | // double fields. |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4753 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4754 | i != e; ++i, ++idx) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4755 | const QualType Ty = i->getType(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4756 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 4757 | |
| 4758 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 4759 | continue; |
| 4760 | |
| 4761 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 4762 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 4763 | continue; |
| 4764 | |
| 4765 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 4766 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 4767 | ArgList.push_back(I64); |
| 4768 | |
| 4769 | // Add double type. |
| 4770 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 4771 | LastOffset = Offset + 64; |
| 4772 | } |
| 4773 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4774 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 4775 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4776 | |
| 4777 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4778 | } |
| 4779 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4780 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4781 | assert((Offset % MinABIStackAlignInBytes) == 0); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4782 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4783 | if ((Align - 1) & Offset) |
| 4784 | return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
| 4785 | |
| 4786 | return 0; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4787 | } |
Akira Hatanaka | 9659d59 | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 4788 | |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4789 | ABIArgInfo |
| 4790 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4791 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4792 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4793 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4794 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4795 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 4796 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4797 | Offset = llvm::RoundUpToAlignment(Offset, Align); |
| 4798 | Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4799 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4800 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4801 | // Ignore empty aggregates. |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4802 | if (TySize == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4803 | return ABIArgInfo::getIgnore(); |
| 4804 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4805 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4806 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4807 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4808 | } |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 4809 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4810 | // If we have reached here, aggregates are passed directly by coercing to |
| 4811 | // another structure type. Padding is inserted if the offset of the |
| 4812 | // aggregate is unaligned. |
| 4813 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 4814 | getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | // Treat an enum type as its underlying type. |
| 4818 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4819 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4820 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4821 | if (Ty->isPromotableIntegerType()) |
| 4822 | return ABIArgInfo::getExtend(); |
| 4823 | |
Akira Hatanaka | 4055cfc | 2013-01-24 21:47:33 +0000 | [diff] [blame] | 4824 | return ABIArgInfo::getDirect(0, 0, |
| 4825 | IsO32 ? 0 : getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4828 | llvm::Type* |
| 4829 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4830 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4831 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4832 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4833 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4834 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4835 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 4836 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4837 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4838 | // N32/64 returns struct/classes in floating point registers if the |
| 4839 | // following conditions are met: |
| 4840 | // 1. The size of the struct/class is no larger than 128-bit. |
| 4841 | // 2. The struct/class has one or two fields all of which are floating |
| 4842 | // point types. |
| 4843 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 4844 | // |
| 4845 | // Any other composite results are returned in integer registers. |
| 4846 | // |
| 4847 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 4848 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 4849 | for (; b != e; ++b) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4850 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4851 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4852 | if (!BT || !BT->isFloatingPoint()) |
| 4853 | break; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4854 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4855 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | if (b == e) |
| 4859 | return llvm::StructType::get(getVMContext(), RTList, |
| 4860 | RD->hasAttr<PackedAttr>()); |
| 4861 | |
| 4862 | RTList.clear(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4863 | } |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4866 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4867 | return llvm::StructType::get(getVMContext(), RTList); |
| 4868 | } |
| 4869 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4870 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | a8536c0 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 4871 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4872 | |
| 4873 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4874 | return ABIArgInfo::getIgnore(); |
| 4875 | |
Akira Hatanaka | 8aeb147 | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 4876 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4877 | if (isRecordReturnIndirect(RetTy, CGT)) |
| 4878 | return ABIArgInfo::getIndirect(0); |
| 4879 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4880 | if (Size <= 128) { |
| 4881 | if (RetTy->isAnyComplexType()) |
| 4882 | return ABIArgInfo::getDirect(); |
| 4883 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4884 | // O32 returns integer vectors in registers. |
| 4885 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 4886 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4887 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4888 | if (!IsO32) |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4889 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4890 | } |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4891 | |
| 4892 | return ABIArgInfo::getIndirect(0); |
| 4893 | } |
| 4894 | |
| 4895 | // Treat an enum type as its underlying type. |
| 4896 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4897 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4898 | |
| 4899 | return (RetTy->isPromotableIntegerType() ? |
| 4900 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4901 | } |
| 4902 | |
| 4903 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4904 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 4905 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 4906 | |
| 4907 | // 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] | 4908 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4909 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4910 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4911 | it != ie; ++it) |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4912 | it->info = classifyArgumentType(it->type, Offset); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4916 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4917 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4918 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4919 | |
| 4920 | CGBuilderTy &Builder = CGF.Builder; |
| 4921 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4922 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4923 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4924 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4925 | llvm::Value *AddrTyped; |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 4926 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4927 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4928 | |
| 4929 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4930 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 4931 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 4932 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 4933 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4934 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 4935 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 4936 | } |
| 4937 | else |
| 4938 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4939 | |
| 4940 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4941 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4942 | uint64_t Offset = |
| 4943 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 4944 | llvm::Value *NextAddr = |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4945 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4946 | "ap.next"); |
| 4947 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4948 | |
| 4949 | return AddrTyped; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4952 | bool |
| 4953 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4954 | llvm::Value *Address) const { |
| 4955 | // This information comes from gcc's implementation, which seems to |
| 4956 | // as canonical as it gets. |
| 4957 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4958 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 4959 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4960 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4961 | |
| 4962 | // 0-31 are the general purpose registers, $0 - $31. |
| 4963 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 4964 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 4965 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4966 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4967 | |
| 4968 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 4969 | // They are one bit wide and ignored here. |
| 4970 | |
| 4971 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 4972 | // (coprocessor 1 is the FP unit) |
| 4973 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 4974 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 4975 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4976 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4977 | return false; |
| 4978 | } |
| 4979 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4980 | //===----------------------------------------------------------------------===// |
| 4981 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 4982 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 4983 | // handling. |
| 4984 | //===----------------------------------------------------------------------===// |
| 4985 | |
| 4986 | namespace { |
| 4987 | |
| 4988 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4989 | public: |
| 4990 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 4991 | : DefaultTargetCodeGenInfo(CGT) {} |
| 4992 | |
| 4993 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4994 | CodeGen::CodeGenModule &M) const; |
| 4995 | }; |
| 4996 | |
| 4997 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4998 | llvm::GlobalValue *GV, |
| 4999 | CodeGen::CodeGenModule &M) const { |
| 5000 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5001 | if (!FD) return; |
| 5002 | |
| 5003 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5004 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5005 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5006 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5007 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5008 | F->addFnAttr(llvm::Attribute::NoInline); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5009 | |
| 5010 | if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 5011 | |
| 5012 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5013 | llvm::LLVMContext &Context = F->getContext(); |
| 5014 | llvm::NamedMDNode *OpenCLMetadata = |
| 5015 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5016 | |
| 5017 | SmallVector<llvm::Value*, 5> Operands; |
| 5018 | Operands.push_back(F); |
| 5019 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5020 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 5021 | llvm::APInt(32, |
| 5022 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); |
| 5023 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 5024 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5025 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5026 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 5027 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5028 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); |
| 5029 | |
| 5030 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5031 | // for implementing the work_group_size_hint attr later. Currently |
| 5032 | // always true as the hint is not yet implemented. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5033 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5034 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5035 | } |
| 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5041 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5042 | //===----------------------------------------------------------------------===// |
| 5043 | // Hexagon ABI Implementation |
| 5044 | //===----------------------------------------------------------------------===// |
| 5045 | |
| 5046 | namespace { |
| 5047 | |
| 5048 | class HexagonABIInfo : public ABIInfo { |
| 5049 | |
| 5050 | |
| 5051 | public: |
| 5052 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5053 | |
| 5054 | private: |
| 5055 | |
| 5056 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5057 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5058 | |
| 5059 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 5060 | |
| 5061 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5062 | CodeGenFunction &CGF) const; |
| 5063 | }; |
| 5064 | |
| 5065 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5066 | public: |
| 5067 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5068 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5069 | |
| 5070 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 5071 | return 29; |
| 5072 | } |
| 5073 | }; |
| 5074 | |
| 5075 | } |
| 5076 | |
| 5077 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5078 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 5079 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5080 | it != ie; ++it) |
| 5081 | it->info = classifyArgumentType(it->type); |
| 5082 | } |
| 5083 | |
| 5084 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5085 | if (!isAggregateTypeForABI(Ty)) { |
| 5086 | // Treat an enum type as its underlying type. |
| 5087 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5088 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5089 | |
| 5090 | return (Ty->isPromotableIntegerType() ? |
| 5091 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5092 | } |
| 5093 | |
| 5094 | // Ignore empty records. |
| 5095 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5096 | return ABIArgInfo::getIgnore(); |
| 5097 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5098 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) |
| 5099 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5100 | |
| 5101 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5102 | if (Size > 64) |
| 5103 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5104 | // Pass in the smallest viable integer type. |
| 5105 | else if (Size > 32) |
| 5106 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5107 | else if (Size > 16) |
| 5108 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5109 | else if (Size > 8) |
| 5110 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5111 | else |
| 5112 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5113 | } |
| 5114 | |
| 5115 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5116 | if (RetTy->isVoidType()) |
| 5117 | return ABIArgInfo::getIgnore(); |
| 5118 | |
| 5119 | // Large vector types should be returned via memory. |
| 5120 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5121 | return ABIArgInfo::getIndirect(0); |
| 5122 | |
| 5123 | if (!isAggregateTypeForABI(RetTy)) { |
| 5124 | // Treat an enum type as its underlying type. |
| 5125 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5126 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5127 | |
| 5128 | return (RetTy->isPromotableIntegerType() ? |
| 5129 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5130 | } |
| 5131 | |
| 5132 | // Structures with either a non-trivial destructor or a non-trivial |
| 5133 | // copy constructor are always indirect. |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5134 | if (isRecordReturnIndirect(RetTy, CGT)) |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5135 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5136 | |
| 5137 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5138 | return ABIArgInfo::getIgnore(); |
| 5139 | |
| 5140 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5141 | // are returned indirectly. |
| 5142 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5143 | if (Size <= 64) { |
| 5144 | // Return in the smallest viable integer type. |
| 5145 | if (Size <= 8) |
| 5146 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5147 | if (Size <= 16) |
| 5148 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5149 | if (Size <= 32) |
| 5150 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5151 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5152 | } |
| 5153 | |
| 5154 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5155 | } |
| 5156 | |
| 5157 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5158 | CodeGenFunction &CGF) const { |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5159 | // FIXME: Need to handle alignment |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5160 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5161 | |
| 5162 | CGBuilderTy &Builder = CGF.Builder; |
| 5163 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5164 | "ap"); |
| 5165 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5166 | llvm::Type *PTy = |
| 5167 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5168 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5169 | |
| 5170 | uint64_t Offset = |
| 5171 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5172 | llvm::Value *NextAddr = |
| 5173 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5174 | "ap.next"); |
| 5175 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5176 | |
| 5177 | return AddrTyped; |
| 5178 | } |
| 5179 | |
| 5180 | |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5181 | //===----------------------------------------------------------------------===// |
| 5182 | // SPARC v9 ABI Implementation. |
| 5183 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5184 | // |
| 5185 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5186 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5187 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5188 | // |
| 5189 | // One case requires special care: |
| 5190 | // |
| 5191 | // struct mixed { |
| 5192 | // int i; |
| 5193 | // float f; |
| 5194 | // }; |
| 5195 | // |
| 5196 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5197 | // parameter array, but the int is passed in an integer register, and the float |
| 5198 | // is passed in a floating point register. This is represented as two arguments |
| 5199 | // with the LLVM IR inreg attribute: |
| 5200 | // |
| 5201 | // declare void f(i32 inreg %i, float inreg %f) |
| 5202 | // |
| 5203 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5204 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5205 | // bytes. |
| 5206 | // |
| 5207 | namespace { |
| 5208 | class SparcV9ABIInfo : public ABIInfo { |
| 5209 | public: |
| 5210 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5211 | |
| 5212 | private: |
| 5213 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
| 5214 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 5215 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5216 | CodeGenFunction &CGF) const; |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5217 | |
| 5218 | // Coercion type builder for structs passed in registers. The coercion type |
| 5219 | // serves two purposes: |
| 5220 | // |
| 5221 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5222 | // in registers. |
| 5223 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5224 | // code generator knows to pass them in floating point registers. |
| 5225 | // |
| 5226 | // We also compute the InReg flag which indicates that the struct contains |
| 5227 | // aligned 32-bit floats. |
| 5228 | // |
| 5229 | struct CoerceBuilder { |
| 5230 | llvm::LLVMContext &Context; |
| 5231 | const llvm::DataLayout &DL; |
| 5232 | SmallVector<llvm::Type*, 8> Elems; |
| 5233 | uint64_t Size; |
| 5234 | bool InReg; |
| 5235 | |
| 5236 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5237 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5238 | |
| 5239 | // Pad Elems with integers until Size is ToSize. |
| 5240 | void pad(uint64_t ToSize) { |
| 5241 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5242 | if (ToSize == Size) |
| 5243 | return; |
| 5244 | |
| 5245 | // Finish the current 64-bit word. |
| 5246 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5247 | if (Aligned > Size && Aligned <= ToSize) { |
| 5248 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5249 | Size = Aligned; |
| 5250 | } |
| 5251 | |
| 5252 | // Add whole 64-bit words. |
| 5253 | while (Size + 64 <= ToSize) { |
| 5254 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5255 | Size += 64; |
| 5256 | } |
| 5257 | |
| 5258 | // Final in-word padding. |
| 5259 | if (Size < ToSize) { |
| 5260 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5261 | Size = ToSize; |
| 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | // Add a floating point element at Offset. |
| 5266 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5267 | // Unaligned floats are treated as integers. |
| 5268 | if (Offset % Bits) |
| 5269 | return; |
| 5270 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5271 | if (Bits < 64) |
| 5272 | InReg = true; |
| 5273 | pad(Offset); |
| 5274 | Elems.push_back(Ty); |
| 5275 | Size = Offset + Bits; |
| 5276 | } |
| 5277 | |
| 5278 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5279 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5280 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5281 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5282 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5283 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5284 | switch (ElemTy->getTypeID()) { |
| 5285 | case llvm::Type::StructTyID: |
| 5286 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5287 | break; |
| 5288 | case llvm::Type::FloatTyID: |
| 5289 | addFloat(ElemOffset, ElemTy, 32); |
| 5290 | break; |
| 5291 | case llvm::Type::DoubleTyID: |
| 5292 | addFloat(ElemOffset, ElemTy, 64); |
| 5293 | break; |
| 5294 | case llvm::Type::FP128TyID: |
| 5295 | addFloat(ElemOffset, ElemTy, 128); |
| 5296 | break; |
| 5297 | case llvm::Type::PointerTyID: |
| 5298 | if (ElemOffset % 64 == 0) { |
| 5299 | pad(ElemOffset); |
| 5300 | Elems.push_back(ElemTy); |
| 5301 | Size += 64; |
| 5302 | } |
| 5303 | break; |
| 5304 | default: |
| 5305 | break; |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | |
| 5310 | // Check if Ty is a usable substitute for the coercion type. |
| 5311 | bool isUsableType(llvm::StructType *Ty) const { |
| 5312 | if (Ty->getNumElements() != Elems.size()) |
| 5313 | return false; |
| 5314 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5315 | if (Elems[i] != Ty->getElementType(i)) |
| 5316 | return false; |
| 5317 | return true; |
| 5318 | } |
| 5319 | |
| 5320 | // Get the coercion type as a literal struct type. |
| 5321 | llvm::Type *getType() const { |
| 5322 | if (Elems.size() == 1) |
| 5323 | return Elems.front(); |
| 5324 | else |
| 5325 | return llvm::StructType::get(Context, Elems); |
| 5326 | } |
| 5327 | }; |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5328 | }; |
| 5329 | } // end anonymous namespace |
| 5330 | |
| 5331 | ABIArgInfo |
| 5332 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5333 | if (Ty->isVoidType()) |
| 5334 | return ABIArgInfo::getIgnore(); |
| 5335 | |
| 5336 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5337 | |
| 5338 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5339 | // pointer / sret pointer. |
| 5340 | if (Size > SizeLimit) |
| 5341 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5342 | |
| 5343 | // Treat an enum type as its underlying type. |
| 5344 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5345 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5346 | |
| 5347 | // Integer types smaller than a register are extended. |
| 5348 | if (Size < 64 && Ty->isIntegerType()) |
| 5349 | return ABIArgInfo::getExtend(); |
| 5350 | |
| 5351 | // Other non-aggregates go in registers. |
| 5352 | if (!isAggregateTypeForABI(Ty)) |
| 5353 | return ABIArgInfo::getDirect(); |
| 5354 | |
| 5355 | // 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] | 5356 | // Build a coercion type from the LLVM struct type. |
| 5357 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5358 | if (!StrTy) |
| 5359 | return ABIArgInfo::getDirect(); |
| 5360 | |
| 5361 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5362 | CB.addStruct(0, StrTy); |
| 5363 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5364 | |
| 5365 | // Try to use the original type for coercion. |
| 5366 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5367 | |
| 5368 | if (CB.InReg) |
| 5369 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5370 | else |
| 5371 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5375 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 5376 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 5377 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 5378 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 5379 | AI.setCoerceToType(ArgTy); |
| 5380 | |
| 5381 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 5382 | CGBuilderTy &Builder = CGF.Builder; |
| 5383 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5384 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5385 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 5386 | llvm::Value *ArgAddr; |
| 5387 | unsigned Stride; |
| 5388 | |
| 5389 | switch (AI.getKind()) { |
| 5390 | case ABIArgInfo::Expand: |
| 5391 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 5392 | |
| 5393 | case ABIArgInfo::Extend: |
| 5394 | Stride = 8; |
| 5395 | ArgAddr = Builder |
| 5396 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 5397 | "extend"); |
| 5398 | break; |
| 5399 | |
| 5400 | case ABIArgInfo::Direct: |
| 5401 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 5402 | ArgAddr = Addr; |
| 5403 | break; |
| 5404 | |
| 5405 | case ABIArgInfo::Indirect: |
| 5406 | Stride = 8; |
| 5407 | ArgAddr = Builder.CreateBitCast(Addr, |
| 5408 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 5409 | "indirect"); |
| 5410 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 5411 | break; |
| 5412 | |
| 5413 | case ABIArgInfo::Ignore: |
| 5414 | return llvm::UndefValue::get(ArgPtrTy); |
| 5415 | } |
| 5416 | |
| 5417 | // Update VAList. |
| 5418 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 5419 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 5420 | |
| 5421 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5425 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
| 5426 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 5427 | it != ie; ++it) |
| 5428 | it->info = classifyType(it->type, 16 * 8); |
| 5429 | } |
| 5430 | |
| 5431 | namespace { |
| 5432 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5433 | public: |
| 5434 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5435 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
| 5436 | }; |
| 5437 | } // end anonymous namespace |
| 5438 | |
| 5439 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5440 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5441 | if (TheTargetCodeGenInfo) |
| 5442 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5443 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5444 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 5445 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5446 | default: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5447 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5448 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 5449 | case llvm::Triple::le32: |
| 5450 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5451 | case llvm::Triple::mips: |
| 5452 | case llvm::Triple::mipsel: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5453 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5454 | |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 5455 | case llvm::Triple::mips64: |
| 5456 | case llvm::Triple::mips64el: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5457 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 5458 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5459 | case llvm::Triple::aarch64: |
| 5460 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 5461 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5462 | case llvm::Triple::arm: |
| 5463 | case llvm::Triple::thumb: |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5464 | { |
| 5465 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5466 | if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5467 | Kind = ARMABIInfo::APCS; |
David Tweed | b16abb1 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 5468 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5469 | (CodeGenOpts.FloatABI != "soft" && |
| 5470 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5471 | Kind = ARMABIInfo::AAPCS_VFP; |
| 5472 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5473 | switch (Triple.getOS()) { |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5474 | case llvm::Triple::NaCl: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 5475 | return *(TheTargetCodeGenInfo = |
| 5476 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 5477 | default: |
| 5478 | return *(TheTargetCodeGenInfo = |
| 5479 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 5480 | } |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 5481 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5482 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5483 | case llvm::Triple::ppc: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5484 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 5485 | case llvm::Triple::ppc64: |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 5486 | if (Triple.isOSBinFormatELF()) |
| 5487 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 5488 | else |
| 5489 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 5490 | |
Peter Collingbourne | edb66f3 | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 5491 | case llvm::Triple::nvptx: |
| 5492 | case llvm::Triple::nvptx64: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5493 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5494 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 5495 | case llvm::Triple::mblaze: |
| 5496 | return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types)); |
| 5497 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5498 | case llvm::Triple::msp430: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5499 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5500 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5501 | case llvm::Triple::systemz: |
| 5502 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 5503 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5504 | case llvm::Triple::tce: |
| 5505 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 5506 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5507 | case llvm::Triple::x86: { |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 5508 | if (Triple.isOSDarwin()) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5509 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 5510 | new X86_32TargetCodeGenInfo(Types, true, true, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 5511 | CodeGenOpts.NumRegisterParameters)); |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 5512 | |
| 5513 | switch (Triple.getOS()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5514 | case llvm::Triple::Cygwin: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5515 | case llvm::Triple::MinGW32: |
Edward O'Callaghan | 727e268 | 2009-10-21 11:58:24 +0000 | [diff] [blame] | 5516 | case llvm::Triple::AuroraUX: |
| 5517 | case llvm::Triple::DragonFly: |
David Chisnall | 75c135a | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 5518 | case llvm::Triple::FreeBSD: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5519 | case llvm::Triple::OpenBSD: |
Eli Friedman | 42f74f2 | 2012-08-08 23:57:20 +0000 | [diff] [blame] | 5520 | case llvm::Triple::Bitrig: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5521 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 5522 | new X86_32TargetCodeGenInfo(Types, false, true, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 5523 | CodeGenOpts.NumRegisterParameters)); |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 5524 | |
| 5525 | case llvm::Triple::Win32: |
| 5526 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 5527 | new WinX86_32TargetCodeGenInfo(Types, |
| 5528 | CodeGenOpts.NumRegisterParameters)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5529 | |
| 5530 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5531 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 5532 | new X86_32TargetCodeGenInfo(Types, false, false, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 5533 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5534 | } |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 5535 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5536 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5537 | case llvm::Triple::x86_64: { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5538 | bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5539 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5540 | switch (Triple.getOS()) { |
| 5541 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 0aa2057 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 5542 | case llvm::Triple::MinGW32: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5543 | case llvm::Triple::Cygwin: |
| 5544 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 5545 | case llvm::Triple::NaCl: |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5546 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 5547 | HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5548 | default: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5549 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 5550 | HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 5551 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 5552 | } |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5553 | case llvm::Triple::hexagon: |
| 5554 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5555 | case llvm::Triple::sparcv9: |
| 5556 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 5557 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5558 | } |