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