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