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