Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
| 17 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CodeGenOptions.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 27 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 28 | llvm::Value *Array, |
| 29 | llvm::Value *Value, |
| 30 | unsigned FirstIndex, |
| 31 | unsigned LastIndex) { |
| 32 | // Alternatively, we could emit this as a loop in the source. |
| 33 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 34 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 35 | Builder.CreateStore(Value, Cell); |
| 36 | } |
| 37 | } |
| 38 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 39 | static bool isAggregateTypeForABI(QualType T) { |
| 40 | return CodeGenFunction::hasAggregateLLVMType(T) || |
| 41 | T->isMemberFunctionPointerType(); |
| 42 | } |
| 43 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 44 | ABIInfo::~ABIInfo() {} |
| 45 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 46 | ASTContext &ABIInfo::getContext() const { |
| 47 | return CGT.getContext(); |
| 48 | } |
| 49 | |
| 50 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 51 | return CGT.getLLVMContext(); |
| 52 | } |
| 53 | |
| 54 | const llvm::TargetData &ABIInfo::getTargetData() const { |
| 55 | return CGT.getTargetData(); |
| 56 | } |
| 57 | |
| 58 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 59 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 61 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 62 | switch (TheKind) { |
| 63 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 64 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 65 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 66 | Ty->print(OS); |
| 67 | else |
| 68 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 69 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 70 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 71 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 72 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 73 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 74 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 75 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 76 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 77 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 78 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 79 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 80 | break; |
| 81 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 82 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 83 | break; |
| 84 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 85 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 88 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 89 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 90 | // If someone can figure out a general rule for this, that would be great. |
| 91 | // It's probably just doomed to be platform-dependent, though. |
| 92 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 93 | // Verified for: |
| 94 | // x86-64 FreeBSD, Linux, Darwin |
| 95 | // x86-32 FreeBSD, Linux, Darwin |
| 96 | // PowerPC Linux, Darwin |
| 97 | // ARM Darwin (*not* EABI) |
| 98 | return 32; |
| 99 | } |
| 100 | |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 101 | bool TargetCodeGenInfo::isNoProtoCallVariadic(CallingConv CC) const { |
| 102 | // The following conventions are known to require this to be false: |
| 103 | // x86_stdcall |
| 104 | // MIPS |
| 105 | // For everything else, we just prefer false unless we opt out. |
| 106 | return false; |
| 107 | } |
| 108 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 109 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | |
| 111 | /// isEmptyField - Return true iff a the field is "empty", that is it |
| 112 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 113 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 114 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 115 | if (FD->isUnnamedBitfield()) |
| 116 | return true; |
| 117 | |
| 118 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 119 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 120 | // Constant arrays of empty records count as empty, strip them off. |
| 121 | if (AllowArrays) |
| 122 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) |
| 123 | FT = AT->getElementType(); |
| 124 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 125 | const RecordType *RT = FT->getAs<RecordType>(); |
| 126 | if (!RT) |
| 127 | return false; |
| 128 | |
| 129 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 130 | // |
| 131 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 132 | // current ABI. |
| 133 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 134 | return false; |
| 135 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 136 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /// isEmptyRecord - Return true iff a structure contains only empty |
| 140 | /// fields. Note that a structure with a flexible array member is not |
| 141 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 142 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 143 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 144 | if (!RT) |
| 145 | return 0; |
| 146 | const RecordDecl *RD = RT->getDecl(); |
| 147 | if (RD->hasFlexibleArrayMember()) |
| 148 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 149 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 150 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 151 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 152 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 153 | e = CXXRD->bases_end(); i != e; ++i) |
| 154 | if (!isEmptyRecord(Context, i->getType(), true)) |
| 155 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 156 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 157 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 158 | i != e; ++i) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 159 | if (!isEmptyField(Context, *i, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 160 | return false; |
| 161 | return true; |
| 162 | } |
| 163 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 164 | /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either |
| 165 | /// a non-trivial destructor or a non-trivial copy constructor. |
| 166 | static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) { |
| 167 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 168 | if (!RD) |
| 169 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 170 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 171 | return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor(); |
| 172 | } |
| 173 | |
| 174 | /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is |
| 175 | /// a record type with either a non-trivial destructor or a non-trivial copy |
| 176 | /// constructor. |
| 177 | static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) { |
| 178 | const RecordType *RT = T->getAs<RecordType>(); |
| 179 | if (!RT) |
| 180 | return false; |
| 181 | |
| 182 | return hasNonTrivialDestructorOrCopyConstructor(RT); |
| 183 | } |
| 184 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 185 | /// isSingleElementStruct - Determine if a structure is a "single |
| 186 | /// element struct", i.e. it has exactly one non-empty field or |
| 187 | /// exactly one field which is itself a single element |
| 188 | /// struct. Structures with flexible array members are never |
| 189 | /// considered single element structs. |
| 190 | /// |
| 191 | /// \return The field declaration for the single non-empty field, if |
| 192 | /// it exists. |
| 193 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 194 | const RecordType *RT = T->getAsStructureType(); |
| 195 | if (!RT) |
| 196 | return 0; |
| 197 | |
| 198 | const RecordDecl *RD = RT->getDecl(); |
| 199 | if (RD->hasFlexibleArrayMember()) |
| 200 | return 0; |
| 201 | |
| 202 | const Type *Found = 0; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 204 | // If this is a C++ record, check the bases first. |
| 205 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 206 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 207 | e = CXXRD->bases_end(); i != e; ++i) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 208 | // Ignore empty records. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 209 | if (isEmptyRecord(Context, i->getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
| 212 | // If we already found an element then this isn't a single-element struct. |
| 213 | if (Found) |
| 214 | return 0; |
| 215 | |
| 216 | // If this is non-empty and not a single element struct, the composite |
| 217 | // cannot be a single element struct. |
| 218 | Found = isSingleElementStruct(i->getType(), Context); |
| 219 | if (!Found) |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Check for single element. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 225 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 226 | i != e; ++i) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 227 | const FieldDecl *FD = *i; |
| 228 | QualType FT = FD->getType(); |
| 229 | |
| 230 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 231 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 232 | continue; |
| 233 | |
| 234 | // If we already found an element then this isn't a single-element |
| 235 | // struct. |
| 236 | if (Found) |
| 237 | return 0; |
| 238 | |
| 239 | // Treat single element arrays as the element. |
| 240 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 241 | if (AT->getSize().getZExtValue() != 1) |
| 242 | break; |
| 243 | FT = AT->getElementType(); |
| 244 | } |
| 245 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 246 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 247 | Found = FT.getTypePtr(); |
| 248 | } else { |
| 249 | Found = isSingleElementStruct(FT, Context); |
| 250 | if (!Found) |
| 251 | return 0; |
| 252 | } |
| 253 | } |
| 254 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 255 | // We don't consider a struct a single-element struct if it has |
| 256 | // padding beyond the element type. |
| 257 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| 258 | return 0; |
| 259 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 260 | return Found; |
| 261 | } |
| 262 | |
| 263 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 264 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 265 | !Ty->isAnyComplexType() && !Ty->isEnumeralType() && |
| 266 | !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 267 | return false; |
| 268 | |
| 269 | uint64_t Size = Context.getTypeSize(Ty); |
| 270 | return Size == 32 || Size == 64; |
| 271 | } |
| 272 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 273 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 274 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 275 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 276 | /// inhibiting optimizations. |
| 277 | /// |
| 278 | // FIXME: This predicate is missing many cases, currently it just follows |
| 279 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 280 | // should probably make this smarter, or better yet make the LLVM backend |
| 281 | // capable of handling it. |
| 282 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 283 | // We can only expand structure types. |
| 284 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 285 | if (!RT) |
| 286 | return false; |
| 287 | |
| 288 | // We can only expand (C) structures. |
| 289 | // |
| 290 | // FIXME: This needs to be generalized to handle classes as well. |
| 291 | const RecordDecl *RD = RT->getDecl(); |
| 292 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 293 | return false; |
| 294 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame^] | 295 | uint64_t Size = 0; |
| 296 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 297 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 298 | i != e; ++i) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 299 | const FieldDecl *FD = *i; |
| 300 | |
| 301 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 302 | return false; |
| 303 | |
| 304 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 305 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 306 | // counts as "basic" is more complicated than what we were doing previously. |
| 307 | if (FD->isBitField()) |
| 308 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame^] | 309 | |
| 310 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame^] | 313 | // Make sure there are not any holes in the struct. |
| 314 | if (Size != Context.getTypeSize(Ty)) |
| 315 | return false; |
| 316 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | namespace { |
| 321 | /// DefaultABIInfo - The default implementation for ABI specific |
| 322 | /// details. This implementation provides information which results in |
| 323 | /// self-consistent and sensible LLVM IR generation, but does not |
| 324 | /// conform to any particular ABI. |
| 325 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 326 | public: |
| 327 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 329 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 330 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 332 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 333 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 334 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 335 | it != ie; ++it) |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 336 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 340 | CodeGenFunction &CGF) const; |
| 341 | }; |
| 342 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 343 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 344 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 345 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 346 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 350 | CodeGenFunction &CGF) const { |
| 351 | return 0; |
| 352 | } |
| 353 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 354 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 355 | if (isAggregateTypeForABI(Ty)) { |
| 356 | // Records with non trivial destructors/constructors should not be passed |
| 357 | // by value. |
| 358 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 359 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 360 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 361 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 362 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 364 | // Treat an enum type as its underlying type. |
| 365 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 366 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 368 | return (Ty->isPromotableIntegerType() ? |
| 369 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 372 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 373 | if (RetTy->isVoidType()) |
| 374 | return ABIArgInfo::getIgnore(); |
| 375 | |
| 376 | if (isAggregateTypeForABI(RetTy)) |
| 377 | return ABIArgInfo::getIndirect(0); |
| 378 | |
| 379 | // Treat an enum type as its underlying type. |
| 380 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 381 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 382 | |
| 383 | return (RetTy->isPromotableIntegerType() ? |
| 384 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 385 | } |
| 386 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 387 | /// UseX86_MMXType - Return true if this is an MMX type that should use the special |
| 388 | /// x86_mmx type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 389 | bool UseX86_MMXType(llvm::Type *IRType) { |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 390 | // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the |
| 391 | // special x86_mmx type. |
| 392 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 393 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 394 | IRType->getScalarSizeInBits() != 64; |
| 395 | } |
| 396 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 397 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 398 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 399 | llvm::Type* Ty) { |
Bill Wendling | ec9d263 | 2011-03-07 22:47:14 +0000 | [diff] [blame] | 400 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 401 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
| 402 | return Ty; |
| 403 | } |
| 404 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
| 406 | // X86-32 ABI Implementation |
| 407 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 408 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 409 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 410 | class X86_32ABIInfo : public ABIInfo { |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 411 | static const unsigned MinABIStackAlignInBytes = 4; |
| 412 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 413 | bool IsDarwinVectorABI; |
| 414 | bool IsSmallStructInRegABI; |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 415 | bool IsMMXDisabled; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 416 | |
| 417 | static bool isRegisterSize(unsigned Size) { |
| 418 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 419 | } |
| 420 | |
| 421 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context); |
| 422 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 423 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 424 | /// such that the argument will be passed in memory. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 425 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 426 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 427 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 428 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 429 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 430 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 432 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 433 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 435 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 436 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 437 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 438 | it != ie; ++it) |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 439 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 443 | CodeGenFunction &CGF) const; |
| 444 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 445 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m) |
| 446 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
| 447 | IsMMXDisabled(m) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 448 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 449 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 450 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 451 | public: |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 452 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m) |
| 453 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 454 | |
| 455 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 456 | CodeGen::CodeGenModule &CGM) const; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 457 | |
| 458 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 459 | // Darwin uses different dwarf register numbers for EH. |
| 460 | if (CGM.isTargetDarwin()) return 5; |
| 461 | |
| 462 | return 4; |
| 463 | } |
| 464 | |
| 465 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 466 | llvm::Value *Address) const; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 467 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 468 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 469 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 470 | llvm::Type* Ty) const { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 471 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 472 | } |
| 473 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 477 | |
| 478 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 479 | /// passed in a register (for the Darwin ABI). |
| 480 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 481 | ASTContext &Context) { |
| 482 | uint64_t Size = Context.getTypeSize(Ty); |
| 483 | |
| 484 | // Type must be register sized. |
| 485 | if (!isRegisterSize(Size)) |
| 486 | return false; |
| 487 | |
| 488 | if (Ty->isVectorType()) { |
| 489 | // 64- and 128- bit vectors inside structures are not returned in |
| 490 | // registers. |
| 491 | if (Size == 64 || Size == 128) |
| 492 | return false; |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 497 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 498 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 499 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 500 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 501 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 502 | return true; |
| 503 | |
| 504 | // Arrays are treated like records. |
| 505 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
| 506 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
| 507 | |
| 508 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 509 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 510 | if (!RT) return false; |
| 511 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 512 | // FIXME: Traverse bases here too. |
| 513 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 514 | // Structure types are passed in register if all fields would be |
| 515 | // passed in a register. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 516 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 517 | e = RT->getDecl()->field_end(); i != e; ++i) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 518 | const FieldDecl *FD = *i; |
| 519 | |
| 520 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 521 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 522 | continue; |
| 523 | |
| 524 | // Check fields recursively. |
| 525 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 532 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const { |
| 533 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 534 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 536 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 537 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 538 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 539 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 540 | |
| 541 | // 128-bit vectors are a special case; they are returned in |
| 542 | // registers and we need to make sure to pick a type the LLVM |
| 543 | // backend will like. |
| 544 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 545 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 546 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 547 | |
| 548 | // Always return in register if it fits in a general purpose |
| 549 | // register, or if it is 64 bits and has a single element. |
| 550 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 551 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 552 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 553 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 554 | |
| 555 | return ABIArgInfo::getIndirect(0); |
| 556 | } |
| 557 | |
| 558 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 559 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 560 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 561 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 562 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 563 | // Structures with either a non-trivial destructor or a non-trivial |
| 564 | // copy constructor are always indirect. |
| 565 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 566 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 567 | |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 568 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 569 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 570 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 571 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 572 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 573 | // If specified, structs and unions are always indirect. |
| 574 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 575 | return ABIArgInfo::getIndirect(0); |
| 576 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 577 | // Small structures which are register sized are generally returned |
| 578 | // in a register. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 579 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) { |
| 580 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 581 | |
| 582 | // As a special-case, if the struct is a "single-element" struct, and |
| 583 | // the field is of type "float" or "double", return it in a |
| 584 | // floating-point register. We apply a similar transformation for |
| 585 | // pointer types to improve the quality of the generated IR. |
| 586 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 587 | if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation()) |
| 588 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 589 | |
| 590 | // FIXME: We should be able to narrow this integer in cases with dead |
| 591 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 592 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 596 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 598 | // Treat an enum type as its underlying type. |
| 599 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 600 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 601 | |
| 602 | return (RetTy->isPromotableIntegerType() ? |
| 603 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 606 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 607 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 608 | if (!RT) |
| 609 | return 0; |
| 610 | const RecordDecl *RD = RT->getDecl(); |
| 611 | |
| 612 | // If this is a C++ record, check the bases first. |
| 613 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 614 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 615 | e = CXXRD->bases_end(); i != e; ++i) |
| 616 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 617 | return false; |
| 618 | |
| 619 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 620 | i != e; ++i) { |
| 621 | QualType FT = i->getType(); |
| 622 | |
| 623 | if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128) |
| 624 | return true; |
| 625 | |
| 626 | if (isRecordWithSSEVectorType(Context, FT)) |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 633 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 634 | unsigned Align) const { |
| 635 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 636 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 637 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 638 | return 0; // Use default alignment. |
| 639 | |
| 640 | // On non-Darwin, the stack type alignment is always 4. |
| 641 | if (!IsDarwinVectorABI) { |
| 642 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 643 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 644 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 645 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 646 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
| 647 | if (isRecordWithSSEVectorType(getContext(), Ty)) |
| 648 | return 16; |
| 649 | |
| 650 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 653 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 654 | if (!ByVal) |
| 655 | return ABIArgInfo::getIndirect(0, false); |
| 656 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 657 | // Compute the byval alignment. |
| 658 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 659 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 660 | if (StackAlign == 0) |
Chris Lattner | e76b95a | 2011-05-22 23:35:00 +0000 | [diff] [blame] | 661 | return ABIArgInfo::getIndirect(4); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 662 | |
| 663 | // If the stack alignment is less than the type alignment, realign the |
| 664 | // argument. |
| 665 | if (StackAlign < TypeAlign) |
| 666 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 667 | /*Realign=*/true); |
| 668 | |
| 669 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 672 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 673 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 674 | if (isAggregateTypeForABI(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 675 | // Structures with flexible arrays are always indirect. |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 676 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 677 | // Structures with either a non-trivial destructor or a non-trivial |
| 678 | // copy constructor are always indirect. |
| 679 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 680 | return getIndirectResult(Ty, /*ByVal=*/false); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 681 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 682 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 683 | return getIndirectResult(Ty); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 684 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 685 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 686 | // Ignore empty structs/unions. |
| 687 | if (Ty->isRecordType() && getContext().getTypeSize(Ty) == 0) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 688 | return ABIArgInfo::getIgnore(); |
| 689 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 690 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 691 | // of those arguments will match the struct. This is important because the |
| 692 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 693 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 694 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 695 | canExpandIndirectArgument(Ty, getContext())) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 696 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 698 | return getIndirectResult(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 701 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 702 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 703 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 704 | if (IsDarwinVectorABI) { |
| 705 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 706 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 707 | (Size == 64 && VT->getNumElements() == 1)) |
| 708 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 709 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 710 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 711 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 712 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 713 | if (UseX86_MMXType(IRType)) { |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 714 | if (IsMMXDisabled) |
| 715 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 716 | 64)); |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 717 | ABIArgInfo AAI = ABIArgInfo::getDirect(IRType); |
| 718 | AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext())); |
| 719 | return AAI; |
| 720 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 721 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 722 | return ABIArgInfo::getDirect(); |
| 723 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 724 | |
| 725 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 726 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 727 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 729 | return (Ty->isPromotableIntegerType() ? |
| 730 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 734 | CodeGenFunction &CGF) const { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 735 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 736 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 737 | |
| 738 | CGBuilderTy &Builder = CGF.Builder; |
| 739 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 740 | "ap"); |
| 741 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 742 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 743 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 744 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 745 | |
| 746 | uint64_t Offset = |
| 747 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 748 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 749 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 750 | "ap.next"); |
| 751 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 752 | |
| 753 | return AddrTyped; |
| 754 | } |
| 755 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 756 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 757 | llvm::GlobalValue *GV, |
| 758 | CodeGen::CodeGenModule &CGM) const { |
| 759 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 760 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 761 | // Get the LLVM function. |
| 762 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 763 | |
| 764 | // Now add the 'alignstack' attribute with a value of 16. |
| 765 | Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16)); |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 770 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 771 | CodeGen::CodeGenFunction &CGF, |
| 772 | llvm::Value *Address) const { |
| 773 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 774 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 775 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 776 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 777 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 778 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 779 | // 0-7 are the eight integer registers; the order is different |
| 780 | // on Darwin (for EH), but the range is the same. |
| 781 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 782 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 783 | |
| 784 | if (CGF.CGM.isTargetDarwin()) { |
| 785 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 786 | // These have size 16, which is sizeof(long double) on |
| 787 | // platforms with 8-byte alignment for that type. |
| 788 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 789 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 790 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 791 | } else { |
| 792 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 793 | // reason. |
| 794 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 795 | |
| 796 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 797 | // These have size 12, which is sizeof(long double) on |
| 798 | // platforms with 4-byte alignment for that type. |
| 799 | llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 800 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 801 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 802 | |
| 803 | return false; |
| 804 | } |
| 805 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 806 | //===----------------------------------------------------------------------===// |
| 807 | // X86-64 ABI Implementation |
| 808 | //===----------------------------------------------------------------------===// |
| 809 | |
| 810 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 811 | namespace { |
| 812 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 813 | class X86_64ABIInfo : public ABIInfo { |
| 814 | enum Class { |
| 815 | Integer = 0, |
| 816 | SSE, |
| 817 | SSEUp, |
| 818 | X87, |
| 819 | X87Up, |
| 820 | ComplexX87, |
| 821 | NoClass, |
| 822 | Memory |
| 823 | }; |
| 824 | |
| 825 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 826 | /// |
| 827 | /// Merge an accumulating classification \arg Accum with a field |
| 828 | /// classification \arg Field. |
| 829 | /// |
| 830 | /// \param Accum - The accumulating classification. This should |
| 831 | /// always be either NoClass or the result of a previous merge |
| 832 | /// call. In addition, this should never be Memory (the caller |
| 833 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 834 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 835 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 836 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 837 | /// |
| 838 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 839 | /// final MEMORY or SSE classes when necessary. |
| 840 | /// |
| 841 | /// \param AggregateSize - The size of the current aggregate in |
| 842 | /// the classification process. |
| 843 | /// |
| 844 | /// \param Lo - The classification for the parts of the type |
| 845 | /// residing in the low word of the containing object. |
| 846 | /// |
| 847 | /// \param Hi - The classification for the parts of the type |
| 848 | /// residing in the higher words of the containing object. |
| 849 | /// |
| 850 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 851 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 852 | /// classify - Determine the x86_64 register classes in which the |
| 853 | /// given type T should be passed. |
| 854 | /// |
| 855 | /// \param Lo - The classification for the parts of the type |
| 856 | /// residing in the low word of the containing object. |
| 857 | /// |
| 858 | /// \param Hi - The classification for the parts of the type |
| 859 | /// residing in the high word of the containing object. |
| 860 | /// |
| 861 | /// \param OffsetBase - The bit offset of this type in the |
| 862 | /// containing object. Some parameters are classified different |
| 863 | /// depending on whether they straddle an eightbyte boundary. |
| 864 | /// |
| 865 | /// If a word is unused its result will be NoClass; if a type should |
| 866 | /// be passed in Memory then at least the classification of \arg Lo |
| 867 | /// will be Memory. |
| 868 | /// |
| 869 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 870 | /// |
| 871 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 872 | /// also be ComplexX87. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 873 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 874 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 875 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 876 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 877 | unsigned IROffset, QualType SourceTy, |
| 878 | unsigned SourceOffset) const; |
| 879 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 880 | unsigned IROffset, QualType SourceTy, |
| 881 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 882 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 883 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 884 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 885 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 886 | |
| 887 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 888 | /// such that the argument will be passed in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 889 | ABIArgInfo getIndirectResult(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 891 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 892 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 893 | ABIArgInfo classifyArgumentType(QualType Ty, |
| 894 | unsigned &neededInt, |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 895 | unsigned &neededSSE) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 896 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 897 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 898 | /// unfortunately in ways that were not always consistent with |
| 899 | /// certain previous compilers. In particular, platforms which |
| 900 | /// required strict binary compatibility with older versions of GCC |
| 901 | /// may need to exempt themselves. |
| 902 | bool honorsRevision0_98() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 903 | return !getContext().getTargetInfo().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 906 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 907 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 908 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 909 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 910 | |
| 911 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 912 | CodeGenFunction &CGF) const; |
| 913 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 914 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 915 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 916 | class WinX86_64ABIInfo : public ABIInfo { |
| 917 | |
| 918 | ABIArgInfo classify(QualType Ty) const; |
| 919 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 920 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 921 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 922 | |
| 923 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 924 | |
| 925 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 926 | CodeGenFunction &CGF) const; |
| 927 | }; |
| 928 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 929 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 930 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 931 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 932 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 933 | |
| 934 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 935 | return 7; |
| 936 | } |
| 937 | |
| 938 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 939 | llvm::Value *Address) const { |
| 940 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 941 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 942 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 943 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 944 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 945 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 946 | // 0-15 are the 16 integer registers. |
| 947 | // 16 is %rip. |
| 948 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 949 | |
| 950 | return false; |
| 951 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 952 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 953 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 954 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 955 | llvm::Type* Ty) const { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 956 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 957 | } |
| 958 | |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 959 | bool isNoProtoCallVariadic(CallingConv CC) const { |
| 960 | // The default CC on x86-64 sets %al to the number of SSA |
| 961 | // registers used, and GCC sets this when calling an unprototyped |
| 962 | // function, so we override the default behavior. |
| 963 | if (CC == CC_Default || CC == CC_C) return true; |
| 964 | |
| 965 | return TargetCodeGenInfo::isNoProtoCallVariadic(CC); |
| 966 | } |
| 967 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 968 | }; |
| 969 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 970 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 971 | public: |
| 972 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 973 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 974 | |
| 975 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 976 | return 7; |
| 977 | } |
| 978 | |
| 979 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 980 | llvm::Value *Address) const { |
| 981 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 982 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 983 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 984 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 985 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 987 | // 0-15 are the 16 integer registers. |
| 988 | // 16 is %rip. |
| 989 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
| 990 | |
| 991 | return false; |
| 992 | } |
| 993 | }; |
| 994 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 997 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 998 | Class &Hi) const { |
| 999 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1000 | // |
| 1001 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1002 | // memory. |
| 1003 | // |
| 1004 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1005 | // memory. |
| 1006 | // |
| 1007 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1008 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1009 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1010 | // ABI working for processors that don't support the __m256 type. |
| 1011 | // |
| 1012 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1013 | // |
| 1014 | // Some of these are enforced by the merging logic. Others can arise |
| 1015 | // only with unions; for example: |
| 1016 | // union { _Complex double; unsigned; } |
| 1017 | // |
| 1018 | // Note that clauses (b) and (c) were added in 0.98. |
| 1019 | // |
| 1020 | if (Hi == Memory) |
| 1021 | Lo = Memory; |
| 1022 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1023 | Lo = Memory; |
| 1024 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1025 | Lo = Memory; |
| 1026 | if (Hi == SSEUp && Lo != SSE) |
| 1027 | Hi = SSE; |
| 1028 | } |
| 1029 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1030 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1031 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1032 | // classified recursively so that always two fields are |
| 1033 | // considered. The resulting class is calculated according to |
| 1034 | // the classes of the fields in the eightbyte: |
| 1035 | // |
| 1036 | // (a) If both classes are equal, this is the resulting class. |
| 1037 | // |
| 1038 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1039 | // the other class. |
| 1040 | // |
| 1041 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1042 | // class. |
| 1043 | // |
| 1044 | // (d) If one of the classes is INTEGER, the result is the |
| 1045 | // INTEGER. |
| 1046 | // |
| 1047 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1048 | // MEMORY is used as class. |
| 1049 | // |
| 1050 | // (f) Otherwise class SSE is used. |
| 1051 | |
| 1052 | // Accum should never be memory (we should have returned) or |
| 1053 | // ComplexX87 (because this cannot be passed in a structure). |
| 1054 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1055 | "Invalid accumulated classification during merge."); |
| 1056 | if (Accum == Field || Field == NoClass) |
| 1057 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1058 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1059 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1060 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1061 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1062 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1063 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1064 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1065 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1066 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1067 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1070 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1071 | Class &Lo, Class &Hi) const { |
| 1072 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1073 | // Class pairs with appropriate constructor methods for the various |
| 1074 | // situations. |
| 1075 | |
| 1076 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1077 | // shouldn't be passed in registers for example, so there is no chance they |
| 1078 | // can straddle an eightbyte. Verify & simplify. |
| 1079 | |
| 1080 | Lo = Hi = NoClass; |
| 1081 | |
| 1082 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1083 | Current = Memory; |
| 1084 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1085 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1086 | BuiltinType::Kind k = BT->getKind(); |
| 1087 | |
| 1088 | if (k == BuiltinType::Void) { |
| 1089 | Current = NoClass; |
| 1090 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1091 | Lo = Integer; |
| 1092 | Hi = Integer; |
| 1093 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1094 | Current = Integer; |
| 1095 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
| 1096 | Current = SSE; |
| 1097 | } else if (k == BuiltinType::LongDouble) { |
| 1098 | Lo = X87; |
| 1099 | Hi = X87Up; |
| 1100 | } |
| 1101 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1102 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1106 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1107 | // Classify the underlying integer type. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1108 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1109 | return; |
| 1110 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1112 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1113 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1114 | return; |
| 1115 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1117 | if (Ty->isMemberPointerType()) { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1118 | if (Ty->isMemberFunctionPointerType()) |
| 1119 | Lo = Hi = Integer; |
| 1120 | else |
| 1121 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1122 | return; |
| 1123 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1125 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1126 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1127 | if (Size == 32) { |
| 1128 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1129 | // float> as integer. |
| 1130 | Current = Integer; |
| 1131 | |
| 1132 | // If this type crosses an eightbyte boundary, it should be |
| 1133 | // split. |
| 1134 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1135 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1136 | if (EB_Real != EB_Imag) |
| 1137 | Hi = Lo; |
| 1138 | } else if (Size == 64) { |
| 1139 | // gcc passes <1 x double> in memory. :( |
| 1140 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1141 | return; |
| 1142 | |
| 1143 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1144 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1145 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1146 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1147 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1148 | Current = Integer; |
| 1149 | else |
| 1150 | Current = SSE; |
| 1151 | |
| 1152 | // If this type crosses an eightbyte boundary, it should be |
| 1153 | // split. |
| 1154 | if (OffsetBase && OffsetBase != 64) |
| 1155 | Hi = Lo; |
Bruno Cardoso Lopes | 37b7fd0 | 2011-07-12 02:47:38 +0000 | [diff] [blame] | 1156 | } else if (Size == 128 || Size == 256) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1157 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1158 | // least significant one belongs to class SSE and all the others to class |
| 1159 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1160 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1161 | // This design isn't correct for 256-bits, but since there're no cases |
| 1162 | // where the upper parts would need to be inspected, avoid adding |
| 1163 | // complexity and just consider Hi to match the 64-256 part. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1164 | Lo = SSE; |
| 1165 | Hi = SSEUp; |
| 1166 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1170 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1171 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1173 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1174 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1175 | if (Size <= 64) |
| 1176 | Current = Integer; |
| 1177 | else if (Size <= 128) |
| 1178 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1179 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1180 | Current = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1181 | else if (ET == getContext().DoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1182 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1183 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1184 | Current = ComplexX87; |
| 1185 | |
| 1186 | // If this complex type crosses an eightbyte boundary then it |
| 1187 | // should be split. |
| 1188 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1189 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1190 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1191 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1193 | return; |
| 1194 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1196 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1197 | // Arrays are treated like structures. |
| 1198 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1199 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1200 | |
| 1201 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1202 | // than four eightbytes, ..., it has class MEMORY. |
| 1203 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1204 | return; |
| 1205 | |
| 1206 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1207 | // fields, it has class MEMORY. |
| 1208 | // |
| 1209 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1210 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1211 | return; |
| 1212 | |
| 1213 | // Otherwise implement simplified merge. We could be smarter about |
| 1214 | // this, but it isn't worth it and would be harder to verify. |
| 1215 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1216 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1217 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1218 | |
| 1219 | // The only case a 256-bit wide vector could be used is when the array |
| 1220 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1221 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1222 | if (Size > 128 && EltSize != 256) |
| 1223 | return; |
| 1224 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1225 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1226 | Class FieldLo, FieldHi; |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1227 | classify(AT->getElementType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1228 | Lo = merge(Lo, FieldLo); |
| 1229 | Hi = merge(Hi, FieldHi); |
| 1230 | if (Lo == Memory || Hi == Memory) |
| 1231 | break; |
| 1232 | } |
| 1233 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1234 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1235 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1236 | return; |
| 1237 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1239 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1240 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1241 | |
| 1242 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1243 | // than four eightbytes, ..., it has class MEMORY. |
| 1244 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1247 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1248 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1249 | // reference. |
| 1250 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 1251 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1252 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | const RecordDecl *RD = RT->getDecl(); |
| 1254 | |
| 1255 | // Assume variable sized types are passed in memory. |
| 1256 | if (RD->hasFlexibleArrayMember()) |
| 1257 | return; |
| 1258 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1259 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Reset Lo class, this will be recomputed. |
| 1262 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1263 | |
| 1264 | // If this is a C++ record, classify the bases first. |
| 1265 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1266 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1267 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1268 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1269 | "Unexpected base class!"); |
| 1270 | const CXXRecordDecl *Base = |
| 1271 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1272 | |
| 1273 | // Classify this field. |
| 1274 | // |
| 1275 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1276 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1277 | // initialized to class NO_CLASS. |
| 1278 | Class FieldLo, FieldHi; |
Anders Carlsson | fd88a61 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1279 | uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1280 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1281 | Lo = merge(Lo, FieldLo); |
| 1282 | Hi = merge(Hi, FieldHi); |
| 1283 | if (Lo == Memory || Hi == Memory) |
| 1284 | break; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1289 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1290 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1291 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1292 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1293 | bool BitField = i->isBitField(); |
| 1294 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1295 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1296 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1297 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1298 | // The only case a 256-bit wide vector could be used is when the struct |
| 1299 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1300 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1301 | // |
| 1302 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1303 | Lo = Memory; |
| 1304 | return; |
| 1305 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1306 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1307 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1308 | Lo = Memory; |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | // Classify this field. |
| 1313 | // |
| 1314 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1315 | // exceeds a single eightbyte, each is classified |
| 1316 | // separately. Each eightbyte gets initialized to class |
| 1317 | // NO_CLASS. |
| 1318 | Class FieldLo, FieldHi; |
| 1319 | |
| 1320 | // Bit-fields require special handling, they do not force the |
| 1321 | // structure to be passed in memory even if unaligned, and |
| 1322 | // therefore they can straddle an eightbyte. |
| 1323 | if (BitField) { |
| 1324 | // Ignore padding bit-fields. |
| 1325 | if (i->isUnnamedBitfield()) |
| 1326 | continue; |
| 1327 | |
| 1328 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1329 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1330 | |
| 1331 | uint64_t EB_Lo = Offset / 64; |
| 1332 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1333 | FieldLo = FieldHi = NoClass; |
| 1334 | if (EB_Lo) { |
| 1335 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1336 | FieldLo = NoClass; |
| 1337 | FieldHi = Integer; |
| 1338 | } else { |
| 1339 | FieldLo = Integer; |
| 1340 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1341 | } |
| 1342 | } else |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1343 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1344 | Lo = merge(Lo, FieldLo); |
| 1345 | Hi = merge(Hi, FieldHi); |
| 1346 | if (Lo == Memory || Hi == Memory) |
| 1347 | break; |
| 1348 | } |
| 1349 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1350 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1354 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1355 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1356 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1357 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1358 | // Treat an enum type as its underlying type. |
| 1359 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1360 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1361 | |
| 1362 | return (Ty->isPromotableIntegerType() ? |
| 1363 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1364 | } |
| 1365 | |
| 1366 | return ABIArgInfo::getIndirect(0); |
| 1367 | } |
| 1368 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1369 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1370 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1371 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1372 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1373 | // Treat an enum type as its underlying type. |
| 1374 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1375 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1376 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1377 | return (Ty->isPromotableIntegerType() ? |
| 1378 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1379 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1380 | |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1381 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1382 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1383 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1384 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1385 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1386 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
| 1387 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1390 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1391 | /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1392 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1393 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1394 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1396 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1397 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1398 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1399 | while (STy && STy->getNumElements() == 1) { |
| 1400 | IRType = STy->getElementType(0); |
| 1401 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1402 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1403 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1404 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1405 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1406 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1407 | unsigned BitWidth = VT->getBitWidth(); |
| 1408 | if ((BitWidth == 128 || BitWidth == 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1409 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1410 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1411 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1412 | EltTy->isIntegerTy(128))) |
| 1413 | return VT; |
| 1414 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1416 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1417 | } |
| 1418 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1419 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1420 | /// is known to either be off the end of the specified type or being in |
| 1421 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1422 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1423 | /// classification that put one of the two halves in the INTEGER class. |
| 1424 | /// |
| 1425 | /// It is conservatively correct to return false. |
| 1426 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1427 | unsigned EndBit, ASTContext &Context) { |
| 1428 | // If the bytes being queried are off the end of the type, there is no user |
| 1429 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1430 | // types that don't contain interesting padding. |
| 1431 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1432 | if (TySize <= StartBit) |
| 1433 | return true; |
| 1434 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1435 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1436 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1437 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1438 | |
| 1439 | // Check each element to see if the element overlaps with the queried range. |
| 1440 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1441 | // If the element is after the span we care about, then we're done.. |
| 1442 | unsigned EltOffset = i*EltSize; |
| 1443 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1445 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1446 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1447 | EndBit-EltOffset, Context)) |
| 1448 | return false; |
| 1449 | } |
| 1450 | // If it overlaps no elements, then it is safe to process as padding. |
| 1451 | return true; |
| 1452 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1454 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1455 | const RecordDecl *RD = RT->getDecl(); |
| 1456 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1458 | // If this is a C++ record, check the bases first. |
| 1459 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1460 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1461 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1462 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1463 | "Unexpected base class!"); |
| 1464 | const CXXRecordDecl *Base = |
| 1465 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1467 | // If the base is after the span we care about, ignore it. |
Anders Carlsson | fd88a61 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1468 | unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1469 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1471 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1472 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1473 | EndBit-BaseOffset, Context)) |
| 1474 | return false; |
| 1475 | } |
| 1476 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1478 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1479 | // this could be sped up a lot by being smarter about queried fields, |
| 1480 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1481 | // much. |
| 1482 | unsigned idx = 0; |
| 1483 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1484 | i != e; ++i, ++idx) { |
| 1485 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1487 | // If we found a field after the region we care about, then we're done. |
| 1488 | if (FieldOffset >= EndBit) break; |
| 1489 | |
| 1490 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1491 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1492 | Context)) |
| 1493 | return false; |
| 1494 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1496 | // If nothing in this record overlapped the area of interest, then we're |
| 1497 | // clean. |
| 1498 | return true; |
| 1499 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1501 | return false; |
| 1502 | } |
| 1503 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1504 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1505 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1506 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1507 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1508 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1509 | const llvm::TargetData &TD) { |
| 1510 | // Base case if we find a float. |
| 1511 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1512 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1514 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1515 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1516 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1517 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1518 | IROffset -= SL->getElementOffset(Elt); |
| 1519 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1520 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1522 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1523 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1524 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1525 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1526 | IROffset -= IROffset/EltSize*EltSize; |
| 1527 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1528 | } |
| 1529 | |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1533 | |
| 1534 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1535 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1536 | llvm::Type *X86_64ABIInfo:: |
| 1537 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1538 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1539 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1540 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1541 | // structs that contain 3 floats. |
| 1542 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1543 | SourceOffset*8+64, getContext())) |
| 1544 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1546 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1547 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1548 | // case. |
| 1549 | if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) && |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1550 | ContainsFloatAtOffset(IRType, IROffset+4, getTargetData())) |
| 1551 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1553 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1554 | } |
| 1555 | |
| 1556 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1557 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1558 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1559 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1560 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1561 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1562 | /// etc). |
| 1563 | /// |
| 1564 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1565 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1566 | /// the 8-byte value references. PrefType may be null. |
| 1567 | /// |
| 1568 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1569 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1570 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1571 | llvm::Type *X86_64ABIInfo:: |
| 1572 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1573 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1574 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1575 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1576 | if (IROffset == 0) { |
| 1577 | // Pointers and int64's always fill the 8-byte unit. |
| 1578 | if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64)) |
| 1579 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1581 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1582 | // goodness in the source type is just tail padding. This is allowed to |
| 1583 | // kick in for struct {double,int} on the int, but not on |
| 1584 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1585 | // have to do this analysis on the source type because we can't depend on |
| 1586 | // unions being lowered a specific way etc. |
| 1587 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
| 1588 | IRType->isIntegerTy(32)) { |
| 1589 | unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1590 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1591 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 1592 | SourceOffset*8+64, getContext())) |
| 1593 | return IRType; |
| 1594 | } |
| 1595 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1597 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1598 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | c11301c | 2010-07-29 02:20:19 +0000 | [diff] [blame] | 1599 | const llvm::StructLayout *SL = getTargetData().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1600 | if (IROffset < SL->getSizeInBytes()) { |
| 1601 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 1602 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1603 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1604 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 1605 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1606 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1607 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1608 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1609 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1610 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1611 | unsigned EltSize = getTargetData().getTypeAllocSize(EltTy); |
| 1612 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1613 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 1614 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1615 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1617 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 1618 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1619 | unsigned TySizeInBytes = |
| 1620 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1621 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1622 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1624 | // It is always safe to classify this as an integer type up to i64 that |
| 1625 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1626 | return llvm::IntegerType::get(getVMContext(), |
| 1627 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1630 | |
| 1631 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 1632 | /// be used as elements of a two register pair to pass or return, return a |
| 1633 | /// first class aggregate to represent them. For example, if the low part of |
| 1634 | /// a by-value argument should be passed as i32* and the high part as float, |
| 1635 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1636 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1637 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1638 | const llvm::TargetData &TD) { |
| 1639 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 1640 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 1641 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 1642 | // the second element at offset 8. Check for this: |
| 1643 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 1644 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
| 1645 | unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign); |
| 1646 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1648 | // To handle this, we have to increase the size of the low part so that the |
| 1649 | // second element will start at an 8 byte offset. We can't increase the size |
| 1650 | // of the second element because it might make us access off the end of the |
| 1651 | // struct. |
| 1652 | if (HiStart != 8) { |
| 1653 | // There are only two sorts of types the ABI generation code can produce for |
| 1654 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 1655 | // Promote these to a larger type. |
| 1656 | if (Lo->isFloatTy()) |
| 1657 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 1658 | else { |
| 1659 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 1660 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 1661 | } |
| 1662 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1664 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1665 | |
| 1666 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1667 | // Verify that the second element is at an 8-byte offset. |
| 1668 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 1669 | "Invalid x86-64 argument pair!"); |
| 1670 | return Result; |
| 1671 | } |
| 1672 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1673 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1674 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1675 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 1676 | // classification algorithm. |
| 1677 | X86_64ABIInfo::Class Lo, Hi; |
| 1678 | classify(RetTy, 0, Lo, Hi); |
| 1679 | |
| 1680 | // Check some invariants. |
| 1681 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1682 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1683 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1684 | llvm::Type *ResType = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1685 | switch (Lo) { |
| 1686 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1687 | if (Hi == NoClass) |
| 1688 | return ABIArgInfo::getIgnore(); |
| 1689 | // If the low part is just padding, it takes no register, leave ResType |
| 1690 | // null. |
| 1691 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1692 | "Unknown missing lo part"); |
| 1693 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1694 | |
| 1695 | case SSEUp: |
| 1696 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1697 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1698 | |
| 1699 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 1700 | // hidden argument. |
| 1701 | case Memory: |
| 1702 | return getIndirectReturnResult(RetTy); |
| 1703 | |
| 1704 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 1705 | // available register of the sequence %rax, %rdx is used. |
| 1706 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1707 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1708 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1709 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1710 | // so that the parameter gets the right LLVM IR attributes. |
| 1711 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1712 | // Treat an enum type as its underlying type. |
| 1713 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1714 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1716 | if (RetTy->isIntegralOrEnumerationType() && |
| 1717 | RetTy->isPromotableIntegerType()) |
| 1718 | return ABIArgInfo::getExtend(); |
| 1719 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | |
| 1722 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 1723 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 1724 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1725 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1726 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1727 | |
| 1728 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 1729 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 1730 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1731 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1732 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1733 | |
| 1734 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 1735 | // part of the value is returned in %st0 and the imaginary part in |
| 1736 | // %st1. |
| 1737 | case ComplexX87: |
| 1738 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1739 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1740 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1741 | NULL); |
| 1742 | break; |
| 1743 | } |
| 1744 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1745 | llvm::Type *HighPart = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1746 | switch (Hi) { |
| 1747 | // Memory was handled previously and X87 should |
| 1748 | // never occur as a hi class. |
| 1749 | case Memory: |
| 1750 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1751 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1752 | |
| 1753 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1754 | case NoClass: |
| 1755 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1757 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1758 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1759 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1760 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1761 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1762 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1763 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1764 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1765 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | |
| 1768 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1769 | // is passed in the next available eightbyte chunk if the last used |
| 1770 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1771 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1772 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1773 | case SSEUp: |
| 1774 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1775 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1776 | break; |
| 1777 | |
| 1778 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 1779 | // returned together with the previous X87 value in %st0. |
| 1780 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1781 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1782 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1783 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1784 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1785 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1786 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1787 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1788 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1789 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1790 | break; |
| 1791 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1793 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1794 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1795 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1796 | if (HighPart) |
| 1797 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1799 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1802 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt, |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1803 | unsigned &neededSSE) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1804 | X86_64ABIInfo::Class Lo, Hi; |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1805 | classify(Ty, 0, Lo, Hi); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1806 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1807 | // Check some invariants. |
| 1808 | // FIXME: Enforce these by construction. |
| 1809 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1810 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1811 | |
| 1812 | neededInt = 0; |
| 1813 | neededSSE = 0; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1814 | llvm::Type *ResType = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1815 | switch (Lo) { |
| 1816 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1817 | if (Hi == NoClass) |
| 1818 | return ABIArgInfo::getIgnore(); |
| 1819 | // If the low part is just padding, it takes no register, leave ResType |
| 1820 | // null. |
| 1821 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1822 | "Unknown missing lo part"); |
| 1823 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1824 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1825 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 1826 | // on the stack. |
| 1827 | case Memory: |
| 1828 | |
| 1829 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 1830 | // COMPLEX_X87, it is passed in memory. |
| 1831 | case X87: |
| 1832 | case ComplexX87: |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 1833 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1834 | ++neededInt; |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1835 | return getIndirectResult(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1836 | |
| 1837 | case SSEUp: |
| 1838 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1839 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1840 | |
| 1841 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 1842 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 1843 | // and %r9 is used. |
| 1844 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1845 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1846 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1847 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1848 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1849 | |
| 1850 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1851 | // so that the parameter gets the right LLVM IR attributes. |
| 1852 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1853 | // Treat an enum type as its underlying type. |
| 1854 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1855 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1856 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1857 | if (Ty->isIntegralOrEnumerationType() && |
| 1858 | Ty->isPromotableIntegerType()) |
| 1859 | return ABIArgInfo::getExtend(); |
| 1860 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1861 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1862 | break; |
| 1863 | |
| 1864 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 1865 | // available SSE register is used, the registers are taken in the |
| 1866 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1867 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1868 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 1869 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1870 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1873 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1875 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1876 | switch (Hi) { |
| 1877 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1878 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1879 | // which is passed in memory. |
| 1880 | case Memory: |
| 1881 | case X87: |
| 1882 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1883 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1884 | |
| 1885 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1887 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1888 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1889 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1890 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1892 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1893 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1894 | break; |
| 1895 | |
| 1896 | // X87Up generally doesn't occur here (long double is passed in |
| 1897 | // memory), except in situations involving unions. |
| 1898 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1899 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1900 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1902 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1903 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1904 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1905 | ++neededSSE; |
| 1906 | break; |
| 1907 | |
| 1908 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 1909 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1910 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1911 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 1912 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1913 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | } |
| 1916 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1917 | // If a high part was specified, merge it together with the low part. It is |
| 1918 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1919 | // first class struct aggregate with the high and low part: {low, high} |
| 1920 | if (HighPart) |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1921 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1922 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1923 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1926 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1928 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1929 | |
| 1930 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1931 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1932 | |
| 1933 | // If the return value is indirect, then the hidden argument is consuming one |
| 1934 | // integer register. |
| 1935 | if (FI.getReturnInfo().isIndirect()) |
| 1936 | --freeIntRegs; |
| 1937 | |
| 1938 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 1939 | // get assigned (in left-to-right order) for passing as follows... |
| 1940 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1941 | it != ie; ++it) { |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1942 | unsigned neededInt, neededSSE; |
| 1943 | it->info = classifyArgumentType(it->type, neededInt, neededSSE); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1944 | |
| 1945 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 1946 | // eightbyte of an argument, the whole argument is passed on the |
| 1947 | // stack. If registers have already been assigned for some |
| 1948 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1949 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1950 | freeIntRegs -= neededInt; |
| 1951 | freeSSERegs -= neededSSE; |
| 1952 | } else { |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1953 | it->info = getIndirectResult(it->type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 1959 | QualType Ty, |
| 1960 | CodeGenFunction &CGF) { |
| 1961 | llvm::Value *overflow_arg_area_p = |
| 1962 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 1963 | llvm::Value *overflow_arg_area = |
| 1964 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 1965 | |
| 1966 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 1967 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
| 1968 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 1969 | if (Align > 8) { |
| 1970 | // Note that we follow the ABI & gcc here, even though the type |
| 1971 | // could in theory have an alignment greater than 16. This case |
| 1972 | // shouldn't ever matter in practice. |
| 1973 | |
| 1974 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1975 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1976 | llvm::ConstantInt::get(CGF.Int32Ty, 15); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1977 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 1978 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1979 | CGF.Int64Ty); |
| 1980 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1981 | overflow_arg_area = |
| 1982 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1983 | overflow_arg_area->getType(), |
| 1984 | "overflow_arg_area.align"); |
| 1985 | } |
| 1986 | |
| 1987 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1988 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1989 | llvm::Value *Res = |
| 1990 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1991 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | |
| 1993 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 1994 | // l->overflow_arg_area + sizeof(type). |
| 1995 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 1996 | // an 8 byte boundary. |
| 1997 | |
| 1998 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1999 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2000 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2001 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2002 | "overflow_arg_area.next"); |
| 2003 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2004 | |
| 2005 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2006 | return Res; |
| 2007 | } |
| 2008 | |
| 2009 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2010 | CodeGenFunction &CGF) const { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2011 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2013 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2014 | // struct { |
| 2015 | // i32 gp_offset; |
| 2016 | // i32 fp_offset; |
| 2017 | // i8* overflow_arg_area; |
| 2018 | // i8* reg_save_area; |
| 2019 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2020 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2022 | Ty = CGF.getContext().getCanonicalType(Ty); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2023 | ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2024 | |
| 2025 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2026 | // in the registers. If not go to step 7. |
| 2027 | if (!neededInt && !neededSSE) |
| 2028 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2029 | |
| 2030 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2031 | // general purpose registers needed to pass type and num_fp to hold |
| 2032 | // the number of floating point registers needed. |
| 2033 | |
| 2034 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2035 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2036 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2037 | // |
| 2038 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2039 | // register save space). |
| 2040 | |
| 2041 | llvm::Value *InRegs = 0; |
| 2042 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2043 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2044 | if (neededInt) { |
| 2045 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2046 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2047 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2048 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | if (neededSSE) { |
| 2052 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2053 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2054 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2055 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2056 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2058 | } |
| 2059 | |
| 2060 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2061 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2062 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2063 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2064 | |
| 2065 | // Emit code to load the value if it was passed in registers. |
| 2066 | |
| 2067 | CGF.EmitBlock(InRegBlock); |
| 2068 | |
| 2069 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2070 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2071 | // copying to a temporary location in case the parameter is passed |
| 2072 | // in different register classes or requires an alignment greater |
| 2073 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2074 | // |
| 2075 | // FIXME: This really results in shameful code when we end up needing to |
| 2076 | // collect arguments from different places; often what should result in a |
| 2077 | // simple assembling of a structure from scattered addresses has many more |
| 2078 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2079 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2080 | llvm::Value *RegAddr = |
| 2081 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2082 | "reg_save_area"); |
| 2083 | if (neededInt && neededSSE) { |
| 2084 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2085 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2086 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2087 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 2088 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2089 | llvm::Type *TyLo = ST->getElementType(0); |
| 2090 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2091 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2093 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2094 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2095 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2096 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | 998f9d9 | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2097 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2098 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2099 | llvm::Value *V = |
| 2100 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2101 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2102 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2103 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2104 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2105 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2106 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2107 | } else if (neededInt) { |
| 2108 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2109 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2110 | llvm::PointerType::getUnqual(LTy)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2111 | } else if (neededSSE == 1) { |
| 2112 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2113 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2114 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2115 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2116 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2117 | // SSE registers are spaced 16 bytes apart in the register save |
| 2118 | // area, we need to collect the two eightbytes together. |
| 2119 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2120 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2121 | llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2122 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2123 | llvm::PointerType::getUnqual(DoubleTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2124 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2125 | DoubleTy, NULL); |
| 2126 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 2127 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2128 | DblPtrTy)); |
| 2129 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2130 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2131 | DblPtrTy)); |
| 2132 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2133 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2134 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2138 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2139 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2140 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2141 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2142 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2143 | gp_offset_p); |
| 2144 | } |
| 2145 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2146 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2147 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2148 | fp_offset_p); |
| 2149 | } |
| 2150 | CGF.EmitBranch(ContBlock); |
| 2151 | |
| 2152 | // Emit code to load the value if it was passed in memory. |
| 2153 | |
| 2154 | CGF.EmitBlock(InMemBlock); |
| 2155 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2156 | |
| 2157 | // Return the appropriate result. |
| 2158 | |
| 2159 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2160 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2161 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2162 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2163 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2164 | return ResAddr; |
| 2165 | } |
| 2166 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2167 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const { |
| 2168 | |
| 2169 | if (Ty->isVoidType()) |
| 2170 | return ABIArgInfo::getIgnore(); |
| 2171 | |
| 2172 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2173 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2174 | |
| 2175 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2176 | |
| 2177 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
NAKAMURA Takumi | e03c603 | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2178 | if (hasNonTrivialDestructorOrCopyConstructor(RT) || |
| 2179 | RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2180 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2181 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2182 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
| 2183 | if (Size == 128 && |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2184 | getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2185 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2186 | Size)); |
| 2187 | |
| 2188 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2189 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2190 | if (Size <= 64 && |
NAKAMURA Takumi | e03c603 | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2191 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2192 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2193 | Size)); |
| 2194 | |
| 2195 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2196 | } |
| 2197 | |
| 2198 | if (Ty->isPromotableIntegerType()) |
| 2199 | return ABIArgInfo::getExtend(); |
| 2200 | |
| 2201 | return ABIArgInfo::getDirect(); |
| 2202 | } |
| 2203 | |
| 2204 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2205 | |
| 2206 | QualType RetTy = FI.getReturnType(); |
| 2207 | FI.getReturnInfo() = classify(RetTy); |
| 2208 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2209 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2210 | it != ie; ++it) |
| 2211 | it->info = classify(it->type); |
| 2212 | } |
| 2213 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2214 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2215 | CodeGenFunction &CGF) const { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2216 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 2217 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2218 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2219 | CGBuilderTy &Builder = CGF.Builder; |
| 2220 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2221 | "ap"); |
| 2222 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2223 | llvm::Type *PTy = |
| 2224 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2225 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2226 | |
| 2227 | uint64_t Offset = |
| 2228 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2229 | llvm::Value *NextAddr = |
| 2230 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2231 | "ap.next"); |
| 2232 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2233 | |
| 2234 | return AddrTyped; |
| 2235 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2236 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2237 | // PowerPC-32 |
| 2238 | |
| 2239 | namespace { |
| 2240 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2241 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2242 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2243 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2244 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2245 | // This is recovered from gcc output. |
| 2246 | return 1; // r1 is the dedicated stack pointer |
| 2247 | } |
| 2248 | |
| 2249 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2250 | llvm::Value *Address) const; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2251 | }; |
| 2252 | |
| 2253 | } |
| 2254 | |
| 2255 | bool |
| 2256 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2257 | llvm::Value *Address) const { |
| 2258 | // This is calculated from the LLVM and GCC tables and verified |
| 2259 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2260 | |
| 2261 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2262 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2263 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2264 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2265 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2266 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2267 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2268 | |
| 2269 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2270 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2271 | |
| 2272 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2273 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2274 | |
| 2275 | // 64-76 are various 4-byte special-purpose registers: |
| 2276 | // 64: mq |
| 2277 | // 65: lr |
| 2278 | // 66: ctr |
| 2279 | // 67: ap |
| 2280 | // 68-75 cr0-7 |
| 2281 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2282 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2283 | |
| 2284 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2285 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2286 | |
| 2287 | // 109: vrsave |
| 2288 | // 110: vscr |
| 2289 | // 111: spe_acc |
| 2290 | // 112: spefscr |
| 2291 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2292 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2293 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2294 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2298 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2299 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2300 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2301 | |
| 2302 | namespace { |
| 2303 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2304 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2305 | public: |
| 2306 | enum ABIKind { |
| 2307 | APCS = 0, |
| 2308 | AAPCS = 1, |
| 2309 | AAPCS_VFP |
| 2310 | }; |
| 2311 | |
| 2312 | private: |
| 2313 | ABIKind Kind; |
| 2314 | |
| 2315 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2316 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {} |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2317 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2318 | bool isEABI() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2319 | StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName(); |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2320 | return (Env == "gnueabi" || Env == "eabi"); |
| 2321 | } |
| 2322 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2323 | private: |
| 2324 | ABIKind getABIKind() const { return Kind; } |
| 2325 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2326 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2327 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2329 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | |
| 2331 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2332 | CodeGenFunction &CGF) const; |
| 2333 | }; |
| 2334 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2335 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2336 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2337 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 2338 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2339 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2340 | const ARMABIInfo &getABIInfo() const { |
| 2341 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2342 | } |
| 2343 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2344 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2345 | return 13; |
| 2346 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2348 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2349 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 2350 | } |
| 2351 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2352 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2353 | llvm::Value *Address) const { |
| 2354 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2355 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2356 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2357 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2358 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2359 | |
| 2360 | // 0-15 are the 16 integer registers. |
| 2361 | AssignToArrayRange(Builder, Address, Four8, 0, 15); |
| 2362 | |
| 2363 | return false; |
| 2364 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2365 | |
| 2366 | unsigned getSizeOfUnwindException() const { |
| 2367 | if (getABIInfo().isEABI()) return 88; |
| 2368 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 2369 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2370 | }; |
| 2371 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2374 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2375 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2376 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2377 | it != ie; ++it) |
| 2378 | it->info = classifyArgumentType(it->type); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2379 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2380 | // Always honor user-specified calling convention. |
| 2381 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 2382 | return; |
| 2383 | |
| 2384 | // Calling convention as default by an ABI. |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2385 | llvm::CallingConv::ID DefaultCC; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2386 | if (isEABI()) |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2387 | DefaultCC = llvm::CallingConv::ARM_AAPCS; |
Rafael Espindola | 23a8a06 | 2010-06-16 19:01:17 +0000 | [diff] [blame] | 2388 | else |
| 2389 | DefaultCC = llvm::CallingConv::ARM_APCS; |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2390 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2391 | // If user did not ask for specific calling convention explicitly (e.g. via |
| 2392 | // pcs attribute), set effective calling convention if it's different than ABI |
| 2393 | // default. |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2394 | switch (getABIKind()) { |
| 2395 | case APCS: |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2396 | if (DefaultCC != llvm::CallingConv::ARM_APCS) |
| 2397 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2398 | break; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2399 | case AAPCS: |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2400 | if (DefaultCC != llvm::CallingConv::ARM_AAPCS) |
| 2401 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2402 | break; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2403 | case AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2404 | if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP) |
| 2405 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2410 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 2411 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 2412 | /// contained in the type is returned through it; this is used for the |
| 2413 | /// recursive calls that check aggregate component types. |
| 2414 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 2415 | ASTContext &Context, |
| 2416 | uint64_t *HAMembers = 0) { |
| 2417 | uint64_t Members; |
| 2418 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2419 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 2420 | return false; |
| 2421 | Members *= AT->getSize().getZExtValue(); |
| 2422 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2423 | const RecordDecl *RD = RT->getDecl(); |
| 2424 | if (RD->isUnion() || RD->hasFlexibleArrayMember()) |
| 2425 | return false; |
| 2426 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 2427 | if (!CXXRD->isAggregate()) |
| 2428 | return false; |
| 2429 | } |
| 2430 | Members = 0; |
| 2431 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2432 | i != e; ++i) { |
| 2433 | const FieldDecl *FD = *i; |
| 2434 | uint64_t FldMembers; |
| 2435 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 2436 | return false; |
| 2437 | Members += FldMembers; |
| 2438 | } |
| 2439 | } else { |
| 2440 | Members = 1; |
| 2441 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 2442 | Members = 2; |
| 2443 | Ty = CT->getElementType(); |
| 2444 | } |
| 2445 | |
| 2446 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 2447 | // double, or 64-bit or 128-bit vectors. |
| 2448 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 2449 | if (BT->getKind() != BuiltinType::Float && |
| 2450 | BT->getKind() != BuiltinType::Double) |
| 2451 | return false; |
| 2452 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 2453 | unsigned VecSize = Context.getTypeSize(VT); |
| 2454 | if (VecSize != 64 && VecSize != 128) |
| 2455 | return false; |
| 2456 | } else { |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | // The base type must be the same for all members. Vector types of the |
| 2461 | // same total size are treated as being equivalent here. |
| 2462 | const Type *TyPtr = Ty.getTypePtr(); |
| 2463 | if (!Base) |
| 2464 | Base = TyPtr; |
| 2465 | if (Base != TyPtr && |
| 2466 | (!Base->isVectorType() || !TyPtr->isVectorType() || |
| 2467 | Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) |
| 2468 | return false; |
| 2469 | } |
| 2470 | |
| 2471 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 2472 | if (HAMembers) |
| 2473 | *HAMembers = Members; |
| 2474 | return (Members <= 4); |
| 2475 | } |
| 2476 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2477 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2478 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2479 | // Treat an enum type as its underlying type. |
| 2480 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2481 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2482 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2483 | return (Ty->isPromotableIntegerType() ? |
| 2484 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2485 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2486 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2487 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2488 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2489 | return ABIArgInfo::getIgnore(); |
| 2490 | |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2491 | // Structures with either a non-trivial destructor or a non-trivial |
| 2492 | // copy constructor are always indirect. |
| 2493 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 2494 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2495 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2496 | if (getABIKind() == ARMABIInfo::AAPCS_VFP) { |
| 2497 | // Homogeneous Aggregates need to be expanded. |
| 2498 | const Type *Base = 0; |
| 2499 | if (isHomogeneousAggregate(Ty, Base, getContext())) |
| 2500 | return ABIArgInfo::getExpand(); |
| 2501 | } |
| 2502 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 2503 | // Otherwise, pass by coercing to a structure of the appropriate size. |
| 2504 | // |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2505 | // FIXME: This is kind of nasty... but there isn't much choice because the ARM |
| 2506 | // backend doesn't support byval. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2507 | // FIXME: This doesn't handle alignment > 64 bits. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2508 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2509 | unsigned SizeRegs; |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2510 | if (getContext().getTypeAlign(Ty) > 32) { |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 2511 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 2512 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2513 | } else { |
| 2514 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 2515 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 2516 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 2517 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2518 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2519 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 2520 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2523 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2524 | llvm::LLVMContext &VMContext) { |
| 2525 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 2526 | // is called integer-like if its size is less than or equal to one word, and |
| 2527 | // the offset of each of its addressable sub-fields is zero. |
| 2528 | |
| 2529 | uint64_t Size = Context.getTypeSize(Ty); |
| 2530 | |
| 2531 | // Check that the type fits in a word. |
| 2532 | if (Size > 32) |
| 2533 | return false; |
| 2534 | |
| 2535 | // FIXME: Handle vector types! |
| 2536 | if (Ty->isVectorType()) |
| 2537 | return false; |
| 2538 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 2539 | // Float types are never treated as "integer like". |
| 2540 | if (Ty->isRealFloatingType()) |
| 2541 | return false; |
| 2542 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2543 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2544 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2545 | return true; |
| 2546 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 2547 | // Small complex integer types are "integer like". |
| 2548 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 2549 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2550 | |
| 2551 | // Single element and zero sized arrays should be allowed, by the definition |
| 2552 | // above, but they are not. |
| 2553 | |
| 2554 | // Otherwise, it must be a record type. |
| 2555 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2556 | if (!RT) return false; |
| 2557 | |
| 2558 | // Ignore records with flexible arrays. |
| 2559 | const RecordDecl *RD = RT->getDecl(); |
| 2560 | if (RD->hasFlexibleArrayMember()) |
| 2561 | return false; |
| 2562 | |
| 2563 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 2564 | // like". |
| 2565 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2566 | |
| 2567 | bool HadField = false; |
| 2568 | unsigned idx = 0; |
| 2569 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2570 | i != e; ++i, ++idx) { |
| 2571 | const FieldDecl *FD = *i; |
| 2572 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2573 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 2574 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 2575 | // struct { int : 0; int x } |
| 2576 | // is non-integer like according to gcc. |
| 2577 | if (FD->isBitField()) { |
| 2578 | if (!RD->isUnion()) |
| 2579 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2580 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2581 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2582 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2583 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2584 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2587 | // Check if this field is at offset 0. |
| 2588 | if (Layout.getFieldOffset(idx) != 0) |
| 2589 | return false; |
| 2590 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2591 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2592 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2593 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2594 | // Only allow at most one field in a structure. This doesn't match the |
| 2595 | // wording above, but follows gcc in situations with a field following an |
| 2596 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2597 | if (!RD->isUnion()) { |
| 2598 | if (HadField) |
| 2599 | return false; |
| 2600 | |
| 2601 | HadField = true; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | return true; |
| 2606 | } |
| 2607 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2608 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2609 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2610 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2611 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 2612 | // Large vector types should be returned via memory. |
| 2613 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 2614 | return ABIArgInfo::getIndirect(0); |
| 2615 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2616 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2617 | // Treat an enum type as its underlying type. |
| 2618 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2619 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 2620 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2621 | return (RetTy->isPromotableIntegerType() ? |
| 2622 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2623 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2624 | |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2625 | // Structures with either a non-trivial destructor or a non-trivial |
| 2626 | // copy constructor are always indirect. |
| 2627 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
| 2628 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2629 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2630 | // Are we following APCS? |
| 2631 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2632 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2633 | return ABIArgInfo::getIgnore(); |
| 2634 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2635 | // Complex types are all returned as packed integers. |
| 2636 | // |
| 2637 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 2638 | // correctly. |
| 2639 | if (RetTy->isAnyComplexType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2640 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2641 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2642 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2643 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2644 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2645 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2646 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2647 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2648 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2649 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2650 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2651 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | // Otherwise return in memory. |
| 2655 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2656 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2657 | |
| 2658 | // Otherwise this is an AAPCS variant. |
| 2659 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2660 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2661 | return ABIArgInfo::getIgnore(); |
| 2662 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 2663 | // Check for homogeneous aggregates with AAPCS-VFP. |
| 2664 | if (getABIKind() == AAPCS_VFP) { |
| 2665 | const Type *Base = 0; |
| 2666 | if (isHomogeneousAggregate(RetTy, Base, getContext())) |
| 2667 | // Homogeneous Aggregates are returned directly. |
| 2668 | return ABIArgInfo::getDirect(); |
| 2669 | } |
| 2670 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2671 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 2672 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2673 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2674 | if (Size <= 32) { |
| 2675 | // Return in the smallest viable integer type. |
| 2676 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2677 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2678 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2679 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2680 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2683 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2687 | CodeGenFunction &CGF) const { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2688 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 2689 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | |
| 2691 | CGBuilderTy &Builder = CGF.Builder; |
| 2692 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2693 | "ap"); |
| 2694 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 2695 | // Handle address alignment for type alignment > 32 bits |
| 2696 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2697 | if (TyAlign > 4) { |
| 2698 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 2699 | "Alignment is not power of 2!"); |
| 2700 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 2701 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 2702 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
| 2703 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 2704 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2705 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2706 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2707 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2708 | |
| 2709 | uint64_t Offset = |
| 2710 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 2711 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2712 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | "ap.next"); |
| 2714 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2715 | |
| 2716 | return AddrTyped; |
| 2717 | } |
| 2718 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2719 | //===----------------------------------------------------------------------===// |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2720 | // PTX ABI Implementation |
| 2721 | //===----------------------------------------------------------------------===// |
| 2722 | |
| 2723 | namespace { |
| 2724 | |
| 2725 | class PTXABIInfo : public ABIInfo { |
| 2726 | public: |
| 2727 | PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2728 | |
| 2729 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2730 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2731 | |
| 2732 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2733 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2734 | CodeGenFunction &CFG) const; |
| 2735 | }; |
| 2736 | |
| 2737 | class PTXTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2738 | public: |
| 2739 | PTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2740 | : TargetCodeGenInfo(new PTXABIInfo(CGT)) {} |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2741 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 2742 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2743 | CodeGen::CodeGenModule &M) const; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2744 | }; |
| 2745 | |
| 2746 | ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const { |
| 2747 | if (RetTy->isVoidType()) |
| 2748 | return ABIArgInfo::getIgnore(); |
| 2749 | if (isAggregateTypeForABI(RetTy)) |
| 2750 | return ABIArgInfo::getIndirect(0); |
| 2751 | return ABIArgInfo::getDirect(); |
| 2752 | } |
| 2753 | |
| 2754 | ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const { |
| 2755 | if (isAggregateTypeForABI(Ty)) |
| 2756 | return ABIArgInfo::getIndirect(0); |
| 2757 | |
| 2758 | return ABIArgInfo::getDirect(); |
| 2759 | } |
| 2760 | |
| 2761 | void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2762 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2763 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2764 | it != ie; ++it) |
| 2765 | it->info = classifyArgumentType(it->type); |
| 2766 | |
| 2767 | // Always honor user-specified calling convention. |
| 2768 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 2769 | return; |
| 2770 | |
| 2771 | // Calling convention as default by an ABI. |
| 2772 | llvm::CallingConv::ID DefaultCC; |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2773 | const LangOptions &LangOpts = getContext().getLangOptions(); |
| 2774 | if (LangOpts.OpenCL || LangOpts.CUDA) { |
| 2775 | // If we are in OpenCL or CUDA mode, then default to device functions |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2776 | DefaultCC = llvm::CallingConv::PTX_Device; |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2777 | } else { |
| 2778 | // If we are in standard C/C++ mode, use the triple to decide on the default |
| 2779 | StringRef Env = |
| 2780 | getContext().getTargetInfo().getTriple().getEnvironmentName(); |
| 2781 | if (Env == "device") |
| 2782 | DefaultCC = llvm::CallingConv::PTX_Device; |
| 2783 | else |
| 2784 | DefaultCC = llvm::CallingConv::PTX_Kernel; |
| 2785 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2786 | FI.setEffectiveCallingConvention(DefaultCC); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2787 | |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2791 | CodeGenFunction &CFG) const { |
| 2792 | llvm_unreachable("PTX does not support varargs"); |
| 2793 | return 0; |
| 2794 | } |
| 2795 | |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2796 | void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2797 | llvm::GlobalValue *GV, |
| 2798 | CodeGen::CodeGenModule &M) const{ |
| 2799 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 2800 | if (!FD) return; |
| 2801 | |
| 2802 | llvm::Function *F = cast<llvm::Function>(GV); |
| 2803 | |
| 2804 | // Perform special handling in OpenCL mode |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2805 | if (M.getLangOptions().OpenCL) { |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2806 | // Use OpenCL function attributes to set proper calling conventions |
| 2807 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2808 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 2809 | // OpenCL __kernel functions get a kernel calling convention |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2810 | F->setCallingConv(llvm::CallingConv::PTX_Kernel); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2811 | // And kernel functions are not subject to inlining |
| 2812 | F->addFnAttr(llvm::Attribute::NoInline); |
| 2813 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2814 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2815 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2816 | // Perform special handling in CUDA mode. |
| 2817 | if (M.getLangOptions().CUDA) { |
| 2818 | // CUDA __global__ functions get a kernel calling convention. Since |
| 2819 | // __global__ functions cannot be called from the device, we do not |
| 2820 | // need to set the noinline attribute. |
| 2821 | if (FD->getAttr<CUDAGlobalAttr>()) |
| 2822 | F->setCallingConv(llvm::CallingConv::PTX_Kernel); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | //===----------------------------------------------------------------------===// |
Wesley Peck | 36a1f68 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 2829 | // MBlaze ABI Implementation |
| 2830 | //===----------------------------------------------------------------------===// |
| 2831 | |
| 2832 | namespace { |
| 2833 | |
| 2834 | class MBlazeABIInfo : public ABIInfo { |
| 2835 | public: |
| 2836 | MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2837 | |
| 2838 | bool isPromotableIntegerType(QualType Ty) const; |
| 2839 | |
| 2840 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2841 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 2842 | |
| 2843 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 2844 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2845 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2846 | it != ie; ++it) |
| 2847 | it->info = classifyArgumentType(it->type); |
| 2848 | } |
| 2849 | |
| 2850 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2851 | CodeGenFunction &CGF) const; |
| 2852 | }; |
| 2853 | |
| 2854 | class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2855 | public: |
| 2856 | MBlazeTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2857 | : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {} |
| 2858 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2859 | CodeGen::CodeGenModule &M) const; |
| 2860 | }; |
| 2861 | |
| 2862 | } |
| 2863 | |
| 2864 | bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 2865 | // MBlaze ABI requires all 8 and 16 bit quantities to be extended. |
| 2866 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2867 | switch (BT->getKind()) { |
| 2868 | case BuiltinType::Bool: |
| 2869 | case BuiltinType::Char_S: |
| 2870 | case BuiltinType::Char_U: |
| 2871 | case BuiltinType::SChar: |
| 2872 | case BuiltinType::UChar: |
| 2873 | case BuiltinType::Short: |
| 2874 | case BuiltinType::UShort: |
| 2875 | return true; |
| 2876 | default: |
| 2877 | return false; |
| 2878 | } |
| 2879 | return false; |
| 2880 | } |
| 2881 | |
| 2882 | llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2883 | CodeGenFunction &CGF) const { |
| 2884 | // FIXME: Implement |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
| 2888 | |
| 2889 | ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const { |
| 2890 | if (RetTy->isVoidType()) |
| 2891 | return ABIArgInfo::getIgnore(); |
| 2892 | if (isAggregateTypeForABI(RetTy)) |
| 2893 | return ABIArgInfo::getIndirect(0); |
| 2894 | |
| 2895 | return (isPromotableIntegerType(RetTy) ? |
| 2896 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2897 | } |
| 2898 | |
| 2899 | ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const { |
| 2900 | if (isAggregateTypeForABI(Ty)) |
| 2901 | return ABIArgInfo::getIndirect(0); |
| 2902 | |
| 2903 | return (isPromotableIntegerType(Ty) ? |
| 2904 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2905 | } |
| 2906 | |
| 2907 | void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2908 | llvm::GlobalValue *GV, |
| 2909 | CodeGen::CodeGenModule &M) |
| 2910 | const { |
| 2911 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 2912 | if (!FD) return; |
NAKAMURA Takumi | 029d74b | 2011-02-17 08:50:50 +0000 | [diff] [blame] | 2913 | |
Wesley Peck | 36a1f68 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 2914 | llvm::CallingConv::ID CC = llvm::CallingConv::C; |
| 2915 | if (FD->hasAttr<MBlazeInterruptHandlerAttr>()) |
| 2916 | CC = llvm::CallingConv::MBLAZE_INTR; |
| 2917 | else if (FD->hasAttr<MBlazeSaveVolatilesAttr>()) |
| 2918 | CC = llvm::CallingConv::MBLAZE_SVOL; |
| 2919 | |
| 2920 | if (CC != llvm::CallingConv::C) { |
| 2921 | // Handle 'interrupt_handler' attribute: |
| 2922 | llvm::Function *F = cast<llvm::Function>(GV); |
| 2923 | |
| 2924 | // Step 1: Set ISR calling convention. |
| 2925 | F->setCallingConv(CC); |
| 2926 | |
| 2927 | // Step 2: Add attributes goodness. |
| 2928 | F->addFnAttr(llvm::Attribute::NoInline); |
| 2929 | } |
| 2930 | |
| 2931 | // Step 3: Emit _interrupt_handler alias. |
| 2932 | if (CC == llvm::CallingConv::MBLAZE_INTR) |
| 2933 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
| 2934 | "_interrupt_handler", GV, &M.getModule()); |
| 2935 | } |
| 2936 | |
| 2937 | |
| 2938 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2939 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2940 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2941 | |
| 2942 | namespace { |
| 2943 | |
| 2944 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2945 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2946 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2947 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2948 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2949 | CodeGen::CodeGenModule &M) const; |
| 2950 | }; |
| 2951 | |
| 2952 | } |
| 2953 | |
| 2954 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2955 | llvm::GlobalValue *GV, |
| 2956 | CodeGen::CodeGenModule &M) const { |
| 2957 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2958 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 2959 | // Handle 'interrupt' attribute: |
| 2960 | llvm::Function *F = cast<llvm::Function>(GV); |
| 2961 | |
| 2962 | // Step 1: Set ISR calling convention. |
| 2963 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 2964 | |
| 2965 | // Step 2: Add attributes goodness. |
| 2966 | F->addFnAttr(llvm::Attribute::NoInline); |
| 2967 | |
| 2968 | // Step 3: Emit ISR vector alias. |
| 2969 | unsigned Num = attr->getNumber() + 0xffe0; |
| 2970 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2971 | "vector_" + Twine::utohexstr(Num), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2972 | GV, &M.getModule()); |
| 2973 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2977 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2978 | // MIPS ABI Implementation. This works for both little-endian and |
| 2979 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2980 | //===----------------------------------------------------------------------===// |
| 2981 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2982 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 2983 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 2984 | bool IsO32; |
Akira Hatanaka | 756ce7f | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 2985 | unsigned MinABIStackAlignInBytes; |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 2986 | llvm::Type* HandleStructTy(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 2987 | public: |
Akira Hatanaka | 756ce7f | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 2988 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
| 2989 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 2990 | |
| 2991 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2992 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 2993 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2994 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2995 | CodeGenFunction &CGF) const; |
| 2996 | }; |
| 2997 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2998 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 2999 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3000 | public: |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 3001 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 3002 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
| 3003 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3004 | |
| 3005 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 3006 | return 29; |
| 3007 | } |
| 3008 | |
| 3009 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3010 | llvm::Value *Address) const; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3011 | |
| 3012 | unsigned getSizeOfUnwindException() const { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 3013 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3014 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3015 | }; |
| 3016 | } |
| 3017 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 3018 | // In N32/64, an aligned double precision floating point field is passed in |
| 3019 | // a register. |
| 3020 | llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const { |
| 3021 | if (IsO32) |
| 3022 | return 0; |
| 3023 | |
| 3024 | const RecordType *RT = Ty->getAsStructureType(); |
| 3025 | |
| 3026 | if (!RT) |
| 3027 | return 0; |
| 3028 | |
| 3029 | const RecordDecl *RD = RT->getDecl(); |
| 3030 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 3031 | uint64_t StructSize = getContext().getTypeSize(Ty); |
| 3032 | assert(!(StructSize % 8) && "Size of structure must be multiple of 8."); |
| 3033 | |
| 3034 | SmallVector<llvm::Type*, 8> ArgList; |
| 3035 | uint64_t LastOffset = 0; |
| 3036 | unsigned idx = 0; |
| 3037 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 3038 | |
| 3039 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3040 | i != e; ++i, ++idx) { |
| 3041 | const QualType Ty = (*i)->getType(); |
| 3042 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3043 | |
| 3044 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 3045 | continue; |
| 3046 | |
| 3047 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 3048 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 3049 | continue; |
| 3050 | |
| 3051 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 3052 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 3053 | ArgList.push_back(I64); |
| 3054 | |
| 3055 | // Add double type. |
| 3056 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 3057 | LastOffset = Offset + 64; |
| 3058 | } |
| 3059 | |
| 3060 | // This structure doesn't have an aligned double field. |
| 3061 | if (!LastOffset) |
| 3062 | return 0; |
| 3063 | |
| 3064 | // Add ((StructSize - LastOffset) / 64) args of type i64. |
| 3065 | for (unsigned N = (StructSize - LastOffset) / 64; N; --N) |
| 3066 | ArgList.push_back(I64); |
| 3067 | |
Akira Hatanaka | f3879ee | 2011-11-03 23:31:00 +0000 | [diff] [blame] | 3068 | // If the size of the remainder is not zero, add one more integer type to |
| 3069 | // ArgList. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 3070 | unsigned R = (StructSize - LastOffset) % 64; |
Akira Hatanaka | f3879ee | 2011-11-03 23:31:00 +0000 | [diff] [blame] | 3071 | if (R) |
| 3072 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 3073 | |
| 3074 | return llvm::StructType::get(getVMContext(), ArgList); |
| 3075 | } |
| 3076 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3077 | ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const { |
| 3078 | if (isAggregateTypeForABI(Ty)) { |
| 3079 | // Ignore empty aggregates. |
| 3080 | if (getContext().getTypeSize(Ty) == 0) |
| 3081 | return ABIArgInfo::getIgnore(); |
| 3082 | |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 3083 | // Records with non trivial destructors/constructors should not be passed |
| 3084 | // by value. |
| 3085 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 3086 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3087 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 3088 | llvm::Type *ResType; |
| 3089 | if ((ResType = HandleStructTy(Ty))) |
| 3090 | return ABIArgInfo::getDirect(ResType); |
| 3091 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3092 | return ABIArgInfo::getIndirect(0); |
| 3093 | } |
| 3094 | |
| 3095 | // Treat an enum type as its underlying type. |
| 3096 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3097 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3098 | |
| 3099 | return (Ty->isPromotableIntegerType() ? |
| 3100 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3101 | } |
| 3102 | |
| 3103 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
| 3104 | if (RetTy->isVoidType()) |
| 3105 | return ABIArgInfo::getIgnore(); |
| 3106 | |
| 3107 | if (isAggregateTypeForABI(RetTy)) { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 3108 | if ((IsO32 && RetTy->isAnyComplexType()) || |
| 3109 | (!IsO32 && (getContext().getTypeSize(RetTy) <= 128))) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3110 | return ABIArgInfo::getDirect(); |
| 3111 | |
| 3112 | return ABIArgInfo::getIndirect(0); |
| 3113 | } |
| 3114 | |
| 3115 | // Treat an enum type as its underlying type. |
| 3116 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3117 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3118 | |
| 3119 | return (RetTy->isPromotableIntegerType() ? |
| 3120 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3121 | } |
| 3122 | |
| 3123 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3124 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3125 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3126 | it != ie; ++it) |
| 3127 | it->info = classifyArgumentType(it->type); |
| 3128 | } |
| 3129 | |
| 3130 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3131 | CodeGenFunction &CGF) const { |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 3132 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3133 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 3134 | |
| 3135 | CGBuilderTy &Builder = CGF.Builder; |
| 3136 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3137 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3138 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 3139 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3140 | llvm::Value *AddrTyped; |
| 3141 | |
| 3142 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 3143 | llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3144 | llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1); |
| 3145 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign); |
| 3146 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc); |
| 3147 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 3148 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 3149 | } |
| 3150 | else |
| 3151 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3152 | |
| 3153 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | ae31c7a | 2011-08-12 02:30:14 +0000 | [diff] [blame] | 3154 | TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 3155 | uint64_t Offset = |
| 3156 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 3157 | llvm::Value *NextAddr = |
| 3158 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3159 | "ap.next"); |
| 3160 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3161 | |
| 3162 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3165 | bool |
| 3166 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3167 | llvm::Value *Address) const { |
| 3168 | // This information comes from gcc's implementation, which seems to |
| 3169 | // as canonical as it gets. |
| 3170 | |
| 3171 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3172 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 3173 | |
| 3174 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 3175 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3176 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3177 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3178 | |
| 3179 | // 0-31 are the general purpose registers, $0 - $31. |
| 3180 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 3181 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 3182 | // 66 is the (notional, I think) register for signal-handler return. |
| 3183 | AssignToArrayRange(Builder, Address, Four8, 0, 65); |
| 3184 | |
| 3185 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 3186 | // They are one bit wide and ignored here. |
| 3187 | |
| 3188 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 3189 | // (coprocessor 1 is the FP unit) |
| 3190 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 3191 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 3192 | // 176-181 are the DSP accumulator registers. |
| 3193 | AssignToArrayRange(Builder, Address, Four8, 80, 181); |
| 3194 | |
| 3195 | return false; |
| 3196 | } |
| 3197 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 3198 | //===----------------------------------------------------------------------===// |
| 3199 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 3200 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 3201 | // handling. |
| 3202 | //===----------------------------------------------------------------------===// |
| 3203 | |
| 3204 | namespace { |
| 3205 | |
| 3206 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3207 | public: |
| 3208 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 3209 | : DefaultTargetCodeGenInfo(CGT) {} |
| 3210 | |
| 3211 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 3212 | CodeGen::CodeGenModule &M) const; |
| 3213 | }; |
| 3214 | |
| 3215 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 3216 | llvm::GlobalValue *GV, |
| 3217 | CodeGen::CodeGenModule &M) const { |
| 3218 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3219 | if (!FD) return; |
| 3220 | |
| 3221 | llvm::Function *F = cast<llvm::Function>(GV); |
| 3222 | |
| 3223 | if (M.getLangOptions().OpenCL) { |
| 3224 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 3225 | // OpenCL C Kernel functions are not subject to inlining |
| 3226 | F->addFnAttr(llvm::Attribute::NoInline); |
| 3227 | |
| 3228 | if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 3229 | |
| 3230 | // Convert the reqd_work_group_size() attributes to metadata. |
| 3231 | llvm::LLVMContext &Context = F->getContext(); |
| 3232 | llvm::NamedMDNode *OpenCLMetadata = |
| 3233 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 3234 | |
| 3235 | SmallVector<llvm::Value*, 5> Operands; |
| 3236 | Operands.push_back(F); |
| 3237 | |
| 3238 | Operands.push_back(llvm::Constant::getIntegerValue( |
| 3239 | llvm::Type::getInt32Ty(Context), |
| 3240 | llvm::APInt( |
| 3241 | 32, |
| 3242 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); |
| 3243 | Operands.push_back(llvm::Constant::getIntegerValue( |
| 3244 | llvm::Type::getInt32Ty(Context), |
| 3245 | llvm::APInt( |
| 3246 | 32, |
| 3247 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); |
| 3248 | Operands.push_back(llvm::Constant::getIntegerValue( |
| 3249 | llvm::Type::getInt32Ty(Context), |
| 3250 | llvm::APInt( |
| 3251 | 32, |
| 3252 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); |
| 3253 | |
| 3254 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 3255 | // for implementing the work_group_size_hint attr later. Currently |
| 3256 | // always true as the hint is not yet implemented. |
| 3257 | Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context))); |
| 3258 | |
| 3259 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3266 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3267 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3268 | if (TheTargetCodeGenInfo) |
| 3269 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3270 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3271 | const llvm::Triple &Triple = getContext().getTargetInfo().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 3272 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3273 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3274 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3275 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3276 | case llvm::Triple::mips: |
| 3277 | case llvm::Triple::mipsel: |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 3278 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3279 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 3280 | case llvm::Triple::mips64: |
| 3281 | case llvm::Triple::mips64el: |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 3282 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 3283 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3284 | case llvm::Triple::arm: |
| 3285 | case llvm::Triple::thumb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 3286 | { |
| 3287 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3289 | if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 3290 | Kind = ARMABIInfo::APCS; |
| 3291 | else if (CodeGenOpts.FloatABI == "hard") |
| 3292 | Kind = ARMABIInfo::AAPCS_VFP; |
| 3293 | |
| 3294 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
| 3295 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3296 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3297 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3298 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3299 | |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 3300 | case llvm::Triple::ptx32: |
| 3301 | case llvm::Triple::ptx64: |
| 3302 | return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types)); |
| 3303 | |
Wesley Peck | 36a1f68 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 3304 | case llvm::Triple::mblaze: |
| 3305 | return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types)); |
| 3306 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3307 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3308 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3309 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 3310 | case llvm::Triple::tce: |
| 3311 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 3312 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3313 | case llvm::Triple::x86: { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3314 | bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0; |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3315 | |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 3316 | if (Triple.isOSDarwin()) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3317 | return *(TheTargetCodeGenInfo = |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3318 | new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX)); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 3319 | |
| 3320 | switch (Triple.getOS()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3321 | case llvm::Triple::Cygwin: |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3322 | case llvm::Triple::MinGW32: |
Edward O'Callaghan | 437ec1e | 2009-10-21 11:58:24 +0000 | [diff] [blame] | 3323 | case llvm::Triple::AuroraUX: |
| 3324 | case llvm::Triple::DragonFly: |
David Chisnall | 2c5bef2 | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 3325 | case llvm::Triple::FreeBSD: |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3326 | case llvm::Triple::OpenBSD: |
Benjamin Kramer | 24f1d3e | 2011-02-02 18:59:27 +0000 | [diff] [blame] | 3327 | case llvm::Triple::NetBSD: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3328 | return *(TheTargetCodeGenInfo = |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3329 | new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3330 | |
| 3331 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3332 | return *(TheTargetCodeGenInfo = |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3333 | new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3334 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3335 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3336 | |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3337 | case llvm::Triple::x86_64: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3338 | switch (Triple.getOS()) { |
| 3339 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 31ea2f1 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 3340 | case llvm::Triple::MinGW32: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3341 | case llvm::Triple::Cygwin: |
| 3342 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
| 3343 | default: |
| 3344 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types)); |
| 3345 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3346 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3347 | } |