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" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.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 | |
| 131 | // If this is a C++ record, check the bases first. |
| 132 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 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; |
| 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 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 334 | //===----------------------------------------------------------------------===// |
| 335 | // X86-32 ABI Implementation |
| 336 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 337 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 338 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 339 | class X86_32ABIInfo : public ABIInfo { |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 340 | static const unsigned MinABIStackAlignInBytes = 4; |
| 341 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 342 | bool IsDarwinVectorABI; |
| 343 | bool IsSmallStructInRegABI; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 344 | |
| 345 | static bool isRegisterSize(unsigned Size) { |
| 346 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 347 | } |
| 348 | |
| 349 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context); |
| 350 | |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 351 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 352 | /// such that the argument will be passed in memory. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 353 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const; |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 355 | /// \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] | 356 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 357 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 358 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 359 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 360 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 361 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 362 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 363 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 364 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 365 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 366 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 367 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 371 | CodeGenFunction &CGF) const; |
| 372 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 373 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p) |
| 374 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {} |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 375 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 376 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 377 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 378 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 379 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p) |
| 380 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {} |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 381 | |
| 382 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 383 | CodeGen::CodeGenModule &CGM) const; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 384 | |
| 385 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 386 | // Darwin uses different dwarf register numbers for EH. |
| 387 | if (CGM.isTargetDarwin()) return 5; |
| 388 | |
| 389 | return 4; |
| 390 | } |
| 391 | |
| 392 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 393 | llvm::Value *Address) const; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 397 | |
| 398 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 399 | /// passed in a register (for the Darwin ABI). |
| 400 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 401 | ASTContext &Context) { |
| 402 | uint64_t Size = Context.getTypeSize(Ty); |
| 403 | |
| 404 | // Type must be register sized. |
| 405 | if (!isRegisterSize(Size)) |
| 406 | return false; |
| 407 | |
| 408 | if (Ty->isVectorType()) { |
| 409 | // 64- and 128- bit vectors inside structures are not returned in |
| 410 | // registers. |
| 411 | if (Size == 64 || Size == 128) |
| 412 | return false; |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 417 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 418 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 419 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 420 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 421 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 422 | return true; |
| 423 | |
| 424 | // Arrays are treated like records. |
| 425 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
| 426 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
| 427 | |
| 428 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 429 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 430 | if (!RT) return false; |
| 431 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 432 | // FIXME: Traverse bases here too. |
| 433 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 434 | // Structure types are passed in register if all fields would be |
| 435 | // passed in a register. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 436 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 437 | e = RT->getDecl()->field_end(); i != e; ++i) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 438 | const FieldDecl *FD = *i; |
| 439 | |
| 440 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 441 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 442 | continue; |
| 443 | |
| 444 | // Check fields recursively. |
| 445 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | return true; |
| 450 | } |
| 451 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 452 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const { |
| 453 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 454 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 455 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 456 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 457 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 458 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 459 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 460 | |
| 461 | // 128-bit vectors are a special case; they are returned in |
| 462 | // registers and we need to make sure to pick a type the LLVM |
| 463 | // backend will like. |
| 464 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 465 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 466 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 467 | |
| 468 | // Always return in register if it fits in a general purpose |
| 469 | // register, or if it is 64 bits and has a single element. |
| 470 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 471 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 472 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 473 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 474 | |
| 475 | return ABIArgInfo::getIndirect(0); |
| 476 | } |
| 477 | |
| 478 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 479 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 480 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 481 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 482 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 483 | // Structures with either a non-trivial destructor or a non-trivial |
| 484 | // copy constructor are always indirect. |
| 485 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 486 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 487 | |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 488 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 489 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 490 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 491 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 492 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 493 | // If specified, structs and unions are always indirect. |
| 494 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 495 | return ABIArgInfo::getIndirect(0); |
| 496 | |
| 497 | // Classify "single element" structs as their element type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 498 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 499 | if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 500 | if (BT->isIntegerType()) { |
| 501 | // We need to use the size of the structure, padding |
| 502 | // bit-fields can adjust that to be larger than the single |
| 503 | // element type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 504 | uint64_t Size = getContext().getTypeSize(RetTy); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 505 | return ABIArgInfo::getDirect( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 506 | llvm::IntegerType::get(getVMContext(), (unsigned)Size)); |
| 507 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 508 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 509 | if (BT->getKind() == BuiltinType::Float) { |
| 510 | assert(getContext().getTypeSize(RetTy) == |
| 511 | getContext().getTypeSize(SeltTy) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | "Unexpect single element structure size!"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 513 | return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext())); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 514 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 515 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 516 | if (BT->getKind() == BuiltinType::Double) { |
| 517 | assert(getContext().getTypeSize(RetTy) == |
| 518 | getContext().getTypeSize(SeltTy) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 519 | "Unexpect single element structure size!"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 520 | return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext())); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 521 | } |
| 522 | } else if (SeltTy->isPointerType()) { |
| 523 | // FIXME: It would be really nice if this could come out as the proper |
| 524 | // pointer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 525 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext()); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 526 | return ABIArgInfo::getDirect(PtrTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 527 | } else if (SeltTy->isVectorType()) { |
| 528 | // 64- and 128-bit vectors are never returned in a |
| 529 | // register when inside a structure. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 530 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 531 | if (Size == 64 || Size == 128) |
| 532 | return ABIArgInfo::getIndirect(0); |
| 533 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 534 | return classifyReturnType(QualType(SeltTy, 0)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | // Small structures which are register sized are generally returned |
| 539 | // in a register. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 540 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) { |
| 541 | uint64_t Size = getContext().getTypeSize(RetTy); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 542 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 546 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 547 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 548 | // Treat an enum type as its underlying type. |
| 549 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 550 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 551 | |
| 552 | return (RetTy->isPromotableIntegerType() ? |
| 553 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 556 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 557 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 558 | if (!RT) |
| 559 | return 0; |
| 560 | const RecordDecl *RD = RT->getDecl(); |
| 561 | |
| 562 | // If this is a C++ record, check the bases first. |
| 563 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 564 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 565 | e = CXXRD->bases_end(); i != e; ++i) |
| 566 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 567 | return false; |
| 568 | |
| 569 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 570 | i != e; ++i) { |
| 571 | QualType FT = i->getType(); |
| 572 | |
| 573 | if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128) |
| 574 | return true; |
| 575 | |
| 576 | if (isRecordWithSSEVectorType(Context, FT)) |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | return false; |
| 581 | } |
| 582 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 583 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 584 | unsigned Align) const { |
| 585 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 586 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 587 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 588 | return 0; // Use default alignment. |
| 589 | |
| 590 | // On non-Darwin, the stack type alignment is always 4. |
| 591 | if (!IsDarwinVectorABI) { |
| 592 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 593 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 594 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 595 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 596 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
| 597 | if (isRecordWithSSEVectorType(getContext(), Ty)) |
| 598 | return 16; |
| 599 | |
| 600 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 603 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 604 | if (!ByVal) |
| 605 | return ABIArgInfo::getIndirect(0, false); |
| 606 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 607 | // Compute the byval alignment. |
| 608 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 609 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 610 | if (StackAlign == 0) |
| 611 | return ABIArgInfo::getIndirect(0); |
| 612 | |
| 613 | // If the stack alignment is less than the type alignment, realign the |
| 614 | // argument. |
| 615 | if (StackAlign < TypeAlign) |
| 616 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 617 | /*Realign=*/true); |
| 618 | |
| 619 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 622 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 623 | // FIXME: Set alignment on indirect arguments. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 624 | if (isAggregateTypeForABI(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 625 | // Structures with flexible arrays are always indirect. |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 626 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 627 | // Structures with either a non-trivial destructor or a non-trivial |
| 628 | // copy constructor are always indirect. |
| 629 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 630 | return getIndirectResult(Ty, /*ByVal=*/false); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 631 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 632 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 633 | return getIndirectResult(Ty); |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 634 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 635 | |
| 636 | // Ignore empty structs. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 637 | if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 638 | return ABIArgInfo::getIgnore(); |
| 639 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 640 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 641 | // of those arguments will match the struct. This is important because the |
| 642 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 643 | // optimizations. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 644 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 645 | canExpandIndirectArgument(Ty, getContext())) |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 646 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 647 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 648 | return getIndirectResult(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 651 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 652 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 653 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 654 | if (IsDarwinVectorABI) { |
| 655 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 656 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 657 | (Size == 64 && VT->getNumElements() == 1)) |
| 658 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 659 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | return ABIArgInfo::getDirect(); |
| 663 | } |
| 664 | |
| 665 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 666 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 667 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 668 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 669 | return (Ty->isPromotableIntegerType() ? |
| 670 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 674 | CodeGenFunction &CGF) const { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 675 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 676 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 677 | |
| 678 | CGBuilderTy &Builder = CGF.Builder; |
| 679 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 680 | "ap"); |
| 681 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 682 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 683 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 684 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 685 | |
| 686 | uint64_t Offset = |
| 687 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 688 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 689 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 690 | "ap.next"); |
| 691 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 692 | |
| 693 | return AddrTyped; |
| 694 | } |
| 695 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 696 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 697 | llvm::GlobalValue *GV, |
| 698 | CodeGen::CodeGenModule &CGM) const { |
| 699 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 700 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 701 | // Get the LLVM function. |
| 702 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 703 | |
| 704 | // Now add the 'alignstack' attribute with a value of 16. |
| 705 | Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16)); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 710 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 711 | CodeGen::CodeGenFunction &CGF, |
| 712 | llvm::Value *Address) const { |
| 713 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 714 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 715 | |
| 716 | const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
| 717 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 718 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 719 | // 0-7 are the eight integer registers; the order is different |
| 720 | // on Darwin (for EH), but the range is the same. |
| 721 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 722 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 723 | |
| 724 | if (CGF.CGM.isTargetDarwin()) { |
| 725 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 726 | // These have size 16, which is sizeof(long double) on |
| 727 | // platforms with 8-byte alignment for that type. |
| 728 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 729 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 730 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 731 | } else { |
| 732 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 733 | // reason. |
| 734 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 735 | |
| 736 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 737 | // These have size 12, which is sizeof(long double) on |
| 738 | // platforms with 4-byte alignment for that type. |
| 739 | llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 740 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 741 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 742 | |
| 743 | return false; |
| 744 | } |
| 745 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------===// |
| 747 | // X86-64 ABI Implementation |
| 748 | //===----------------------------------------------------------------------===// |
| 749 | |
| 750 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 751 | namespace { |
| 752 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 753 | class X86_64ABIInfo : public ABIInfo { |
| 754 | enum Class { |
| 755 | Integer = 0, |
| 756 | SSE, |
| 757 | SSEUp, |
| 758 | X87, |
| 759 | X87Up, |
| 760 | ComplexX87, |
| 761 | NoClass, |
| 762 | Memory |
| 763 | }; |
| 764 | |
| 765 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 766 | /// |
| 767 | /// Merge an accumulating classification \arg Accum with a field |
| 768 | /// classification \arg Field. |
| 769 | /// |
| 770 | /// \param Accum - The accumulating classification. This should |
| 771 | /// always be either NoClass or the result of a previous merge |
| 772 | /// call. In addition, this should never be Memory (the caller |
| 773 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 774 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 775 | |
| 776 | /// classify - Determine the x86_64 register classes in which the |
| 777 | /// given type T should be passed. |
| 778 | /// |
| 779 | /// \param Lo - The classification for the parts of the type |
| 780 | /// residing in the low word of the containing object. |
| 781 | /// |
| 782 | /// \param Hi - The classification for the parts of the type |
| 783 | /// residing in the high word of the containing object. |
| 784 | /// |
| 785 | /// \param OffsetBase - The bit offset of this type in the |
| 786 | /// containing object. Some parameters are classified different |
| 787 | /// depending on whether they straddle an eightbyte boundary. |
| 788 | /// |
| 789 | /// If a word is unused its result will be NoClass; if a type should |
| 790 | /// be passed in Memory then at least the classification of \arg Lo |
| 791 | /// will be Memory. |
| 792 | /// |
| 793 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 794 | /// |
| 795 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 796 | /// also be ComplexX87. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 797 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 798 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 799 | const llvm::Type *Get16ByteVectorType(QualType Ty) const; |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 800 | const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 801 | unsigned IROffset, QualType SourceTy, |
| 802 | unsigned SourceOffset) const; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 803 | const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType, |
| 804 | unsigned IROffset, QualType SourceTy, |
| 805 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 806 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 807 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 808 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 809 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 810 | |
| 811 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 812 | /// such that the argument will be passed in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 813 | ABIArgInfo getIndirectResult(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 814 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 815 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 5868ca2 | 2010-07-29 04:41:05 +0000 | [diff] [blame] | 817 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt, |
| 818 | unsigned &neededSSE) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 819 | |
| 820 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 821 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 822 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 823 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 824 | |
| 825 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 826 | CodeGenFunction &CGF) const; |
| 827 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 828 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 829 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
| 830 | class WinX86_64ABIInfo : public X86_64ABIInfo { |
| 831 | public: |
| 832 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {} |
| 833 | |
| 834 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 835 | CodeGenFunction &CGF) const; |
| 836 | }; |
| 837 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 838 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 839 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 840 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 841 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 842 | |
| 843 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 844 | return 7; |
| 845 | } |
| 846 | |
| 847 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 848 | llvm::Value *Address) const { |
| 849 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 850 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 851 | |
| 852 | const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
| 853 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 854 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 855 | // 0-15 are the 16 integer registers. |
| 856 | // 16 is %rip. |
| 857 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 858 | |
| 859 | return false; |
| 860 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 861 | }; |
| 862 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 863 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 864 | public: |
| 865 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 866 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 867 | |
| 868 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 869 | return 7; |
| 870 | } |
| 871 | |
| 872 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 873 | llvm::Value *Address) const { |
| 874 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 875 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 876 | |
| 877 | const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
| 878 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 879 | |
| 880 | // 0-15 are the 16 integer registers. |
| 881 | // 16 is %rip. |
| 882 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
| 883 | |
| 884 | return false; |
| 885 | } |
| 886 | }; |
| 887 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 890 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 891 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 892 | // classified recursively so that always two fields are |
| 893 | // considered. The resulting class is calculated according to |
| 894 | // the classes of the fields in the eightbyte: |
| 895 | // |
| 896 | // (a) If both classes are equal, this is the resulting class. |
| 897 | // |
| 898 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 899 | // the other class. |
| 900 | // |
| 901 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 902 | // class. |
| 903 | // |
| 904 | // (d) If one of the classes is INTEGER, the result is the |
| 905 | // INTEGER. |
| 906 | // |
| 907 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 908 | // MEMORY is used as class. |
| 909 | // |
| 910 | // (f) Otherwise class SSE is used. |
| 911 | |
| 912 | // Accum should never be memory (we should have returned) or |
| 913 | // ComplexX87 (because this cannot be passed in a structure). |
| 914 | assert((Accum != Memory && Accum != ComplexX87) && |
| 915 | "Invalid accumulated classification during merge."); |
| 916 | if (Accum == Field || Field == NoClass) |
| 917 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 918 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 919 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 920 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 921 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 922 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 923 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 924 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 925 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 926 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 927 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 930 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 931 | Class &Lo, Class &Hi) const { |
| 932 | // FIXME: This code can be simplified by introducing a simple value class for |
| 933 | // Class pairs with appropriate constructor methods for the various |
| 934 | // situations. |
| 935 | |
| 936 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 937 | // shouldn't be passed in registers for example, so there is no chance they |
| 938 | // can straddle an eightbyte. Verify & simplify. |
| 939 | |
| 940 | Lo = Hi = NoClass; |
| 941 | |
| 942 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 943 | Current = Memory; |
| 944 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 945 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 946 | BuiltinType::Kind k = BT->getKind(); |
| 947 | |
| 948 | if (k == BuiltinType::Void) { |
| 949 | Current = NoClass; |
| 950 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 951 | Lo = Integer; |
| 952 | Hi = Integer; |
| 953 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 954 | Current = Integer; |
| 955 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
| 956 | Current = SSE; |
| 957 | } else if (k == BuiltinType::LongDouble) { |
| 958 | Lo = X87; |
| 959 | Hi = X87Up; |
| 960 | } |
| 961 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 962 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 963 | return; |
| 964 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 966 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 967 | // Classify the underlying integer type. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 968 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 969 | return; |
| 970 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 972 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 973 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 976 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 977 | if (Ty->isMemberPointerType()) { |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 978 | if (Ty->isMemberFunctionPointerType()) |
| 979 | Lo = Hi = Integer; |
| 980 | else |
| 981 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 985 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 986 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 987 | if (Size == 32) { |
| 988 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 989 | // float> as integer. |
| 990 | Current = Integer; |
| 991 | |
| 992 | // If this type crosses an eightbyte boundary, it should be |
| 993 | // split. |
| 994 | uint64_t EB_Real = (OffsetBase) / 64; |
| 995 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 996 | if (EB_Real != EB_Imag) |
| 997 | Hi = Lo; |
| 998 | } else if (Size == 64) { |
| 999 | // gcc passes <1 x double> in memory. :( |
| 1000 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1001 | return; |
| 1002 | |
| 1003 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 473f8e7 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1004 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 0fefa41 | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1005 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1006 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1007 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1008 | Current = Integer; |
| 1009 | else |
| 1010 | Current = SSE; |
| 1011 | |
| 1012 | // If this type crosses an eightbyte boundary, it should be |
| 1013 | // split. |
| 1014 | if (OffsetBase && OffsetBase != 64) |
| 1015 | Hi = Lo; |
| 1016 | } else if (Size == 128) { |
| 1017 | Lo = SSE; |
| 1018 | Hi = SSEUp; |
| 1019 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1020 | return; |
| 1021 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1023 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1024 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1026 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1027 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1028 | if (Size <= 64) |
| 1029 | Current = Integer; |
| 1030 | else if (Size <= 128) |
| 1031 | Lo = Hi = Integer; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1032 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1033 | Current = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1034 | else if (ET == getContext().DoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1035 | Lo = Hi = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1036 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1037 | Current = ComplexX87; |
| 1038 | |
| 1039 | // If this complex type crosses an eightbyte boundary then it |
| 1040 | // should be split. |
| 1041 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1042 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1043 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1044 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1049 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1050 | // Arrays are treated like structures. |
| 1051 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1052 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | |
| 1054 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 1055 | // than two eightbytes, ..., it has class MEMORY. |
| 1056 | if (Size > 128) |
| 1057 | return; |
| 1058 | |
| 1059 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1060 | // fields, it has class MEMORY. |
| 1061 | // |
| 1062 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1063 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | return; |
| 1065 | |
| 1066 | // Otherwise implement simplified merge. We could be smarter about |
| 1067 | // this, but it isn't worth it and would be harder to verify. |
| 1068 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1069 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1070 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 1071 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1072 | Class FieldLo, FieldHi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1073 | classify(AT->getElementType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1074 | Lo = merge(Lo, FieldLo); |
| 1075 | Hi = merge(Hi, FieldHi); |
| 1076 | if (Lo == Memory || Hi == Memory) |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 1081 | if (Hi == Memory) |
| 1082 | Lo = Memory; |
| 1083 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1087 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1088 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1089 | |
| 1090 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 1091 | // than two eightbytes, ..., it has class MEMORY. |
| 1092 | if (Size > 128) |
| 1093 | return; |
| 1094 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1095 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1096 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1097 | // reference. |
| 1098 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 1099 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1100 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | const RecordDecl *RD = RT->getDecl(); |
| 1102 | |
| 1103 | // Assume variable sized types are passed in memory. |
| 1104 | if (RD->hasFlexibleArrayMember()) |
| 1105 | return; |
| 1106 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1107 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1108 | |
| 1109 | // Reset Lo class, this will be recomputed. |
| 1110 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1111 | |
| 1112 | // If this is a C++ record, classify the bases first. |
| 1113 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1114 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1115 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1116 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1117 | "Unexpected base class!"); |
| 1118 | const CXXRecordDecl *Base = |
| 1119 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1120 | |
| 1121 | // Classify this field. |
| 1122 | // |
| 1123 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1124 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1125 | // initialized to class NO_CLASS. |
| 1126 | Class FieldLo, FieldHi; |
| 1127 | uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1128 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1129 | Lo = merge(Lo, FieldLo); |
| 1130 | Hi = merge(Hi, FieldHi); |
| 1131 | if (Lo == Memory || Hi == Memory) |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1137 | unsigned idx = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1138 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1139 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1140 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1141 | bool BitField = i->isBitField(); |
| 1142 | |
| 1143 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1144 | // fields, it has class MEMORY. |
| 1145 | // |
| 1146 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1147 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1148 | Lo = Memory; |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | // Classify this field. |
| 1153 | // |
| 1154 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1155 | // exceeds a single eightbyte, each is classified |
| 1156 | // separately. Each eightbyte gets initialized to class |
| 1157 | // NO_CLASS. |
| 1158 | Class FieldLo, FieldHi; |
| 1159 | |
| 1160 | // Bit-fields require special handling, they do not force the |
| 1161 | // structure to be passed in memory even if unaligned, and |
| 1162 | // therefore they can straddle an eightbyte. |
| 1163 | if (BitField) { |
| 1164 | // Ignore padding bit-fields. |
| 1165 | if (i->isUnnamedBitfield()) |
| 1166 | continue; |
| 1167 | |
| 1168 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1169 | uint64_t Size = |
| 1170 | i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1171 | |
| 1172 | uint64_t EB_Lo = Offset / 64; |
| 1173 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1174 | FieldLo = FieldHi = NoClass; |
| 1175 | if (EB_Lo) { |
| 1176 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1177 | FieldLo = NoClass; |
| 1178 | FieldHi = Integer; |
| 1179 | } else { |
| 1180 | FieldLo = Integer; |
| 1181 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1182 | } |
| 1183 | } else |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1184 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1185 | Lo = merge(Lo, FieldLo); |
| 1186 | Hi = merge(Hi, FieldHi); |
| 1187 | if (Lo == Memory || Hi == Memory) |
| 1188 | break; |
| 1189 | } |
| 1190 | |
| 1191 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1192 | // |
| 1193 | // (a) If one of the classes is MEMORY, the whole argument is |
| 1194 | // passed in memory. |
| 1195 | // |
| 1196 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 1197 | |
| 1198 | // The first of these conditions is guaranteed by how we implement |
| 1199 | // the merge (just bail). |
| 1200 | // |
| 1201 | // The second condition occurs in the case of unions; for example |
| 1202 | // union { _Complex double; unsigned; }. |
| 1203 | if (Hi == Memory) |
| 1204 | Lo = Memory; |
| 1205 | if (Hi == SSEUp && Lo != SSE) |
| 1206 | Hi = SSE; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1210 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1211 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1212 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1213 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1214 | // Treat an enum type as its underlying type. |
| 1215 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1216 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1217 | |
| 1218 | return (Ty->isPromotableIntegerType() ? |
| 1219 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1220 | } |
| 1221 | |
| 1222 | return ABIArgInfo::getIndirect(0); |
| 1223 | } |
| 1224 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1225 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1226 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1227 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1228 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1229 | // Treat an enum type as its underlying type. |
| 1230 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1231 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1232 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1233 | return (Ty->isPromotableIntegerType() ? |
| 1234 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1235 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1236 | |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1237 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1238 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1239 | |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1240 | // Compute the byval alignment. We trust the back-end to honor the |
| 1241 | // minimum ABI alignment for byval, to make cleaner IR. |
| 1242 | const unsigned MinABIAlign = 8; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1243 | unsigned Align = getContext().getTypeAlign(Ty) / 8; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1244 | if (Align > MinABIAlign) |
| 1245 | return ABIArgInfo::getIndirect(Align); |
| 1246 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1249 | /// Get16ByteVectorType - The ABI specifies that a value should be passed in an |
| 1250 | /// full vector XMM register. Pick an LLVM IR type that will be passed as a |
| 1251 | /// vector register. |
| 1252 | const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const { |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1253 | const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1255 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1256 | // strip them off if present. |
| 1257 | const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
| 1258 | while (STy && STy->getNumElements() == 1) { |
| 1259 | IRType = STy->getElementType(0); |
| 1260 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1261 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1263 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1264 | if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1265 | const llvm::Type *EltTy = VT->getElementType(); |
| 1266 | if (VT->getBitWidth() == 128 && |
| 1267 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1268 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1269 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1270 | EltTy->isIntegerTy(128))) |
| 1271 | return VT; |
| 1272 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1274 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1275 | } |
| 1276 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1277 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1278 | /// is known to either be off the end of the specified type or being in |
| 1279 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1280 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1281 | /// classification that put one of the two halves in the INTEGER class. |
| 1282 | /// |
| 1283 | /// It is conservatively correct to return false. |
| 1284 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1285 | unsigned EndBit, ASTContext &Context) { |
| 1286 | // If the bytes being queried are off the end of the type, there is no user |
| 1287 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1288 | // types that don't contain interesting padding. |
| 1289 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1290 | if (TySize <= StartBit) |
| 1291 | return true; |
| 1292 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1293 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1294 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1295 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1296 | |
| 1297 | // Check each element to see if the element overlaps with the queried range. |
| 1298 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1299 | // If the element is after the span we care about, then we're done.. |
| 1300 | unsigned EltOffset = i*EltSize; |
| 1301 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1303 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1304 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1305 | EndBit-EltOffset, Context)) |
| 1306 | return false; |
| 1307 | } |
| 1308 | // If it overlaps no elements, then it is safe to process as padding. |
| 1309 | return true; |
| 1310 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1312 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1313 | const RecordDecl *RD = RT->getDecl(); |
| 1314 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1316 | // If this is a C++ record, check the bases first. |
| 1317 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1318 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1319 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1320 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1321 | "Unexpected base class!"); |
| 1322 | const CXXRecordDecl *Base = |
| 1323 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1325 | // If the base is after the span we care about, ignore it. |
| 1326 | unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base); |
| 1327 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1328 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1329 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1330 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1331 | EndBit-BaseOffset, Context)) |
| 1332 | return false; |
| 1333 | } |
| 1334 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1336 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1337 | // this could be sped up a lot by being smarter about queried fields, |
| 1338 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1339 | // much. |
| 1340 | unsigned idx = 0; |
| 1341 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1342 | i != e; ++i, ++idx) { |
| 1343 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1345 | // If we found a field after the region we care about, then we're done. |
| 1346 | if (FieldOffset >= EndBit) break; |
| 1347 | |
| 1348 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1349 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1350 | Context)) |
| 1351 | return false; |
| 1352 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1354 | // If nothing in this record overlapped the area of interest, then we're |
| 1355 | // clean. |
| 1356 | return true; |
| 1357 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1359 | return false; |
| 1360 | } |
| 1361 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1362 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1363 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1364 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1365 | /// false. |
| 1366 | static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset, |
| 1367 | const llvm::TargetData &TD) { |
| 1368 | // Base case if we find a float. |
| 1369 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1370 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1372 | // If this is a struct, recurse into the field at the specified offset. |
| 1373 | if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
| 1374 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1375 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1376 | IROffset -= SL->getElementOffset(Elt); |
| 1377 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1378 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1380 | // If this is an array, recurse into the field at the specified offset. |
| 1381 | if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1382 | const llvm::Type *EltTy = ATy->getElementType(); |
| 1383 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1384 | IROffset -= IROffset/EltSize*EltSize; |
| 1385 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1386 | } |
| 1387 | |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1391 | |
| 1392 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1393 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
| 1394 | const llvm::Type *X86_64ABIInfo:: |
| 1395 | GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset, |
| 1396 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1397 | // 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] | 1398 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1399 | // structs that contain 3 floats. |
| 1400 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1401 | SourceOffset*8+64, getContext())) |
| 1402 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1404 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1405 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1406 | // case. |
| 1407 | if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) && |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1408 | ContainsFloatAtOffset(IRType, IROffset+4, getTargetData())) |
| 1409 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1411 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1412 | } |
| 1413 | |
| 1414 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1415 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1416 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1417 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1418 | /// 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] | 1419 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1420 | /// etc). |
| 1421 | /// |
| 1422 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1423 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1424 | /// the 8-byte value references. PrefType may be null. |
| 1425 | /// |
| 1426 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1427 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1428 | /// |
Chris Lattner | 44f0fd2 | 2010-07-29 02:20:19 +0000 | [diff] [blame] | 1429 | const llvm::Type *X86_64ABIInfo:: |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1430 | GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset, |
| 1431 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1432 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1433 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1434 | if (IROffset == 0) { |
| 1435 | // Pointers and int64's always fill the 8-byte unit. |
| 1436 | if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64)) |
| 1437 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1439 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1440 | // goodness in the source type is just tail padding. This is allowed to |
| 1441 | // kick in for struct {double,int} on the int, but not on |
| 1442 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1443 | // have to do this analysis on the source type because we can't depend on |
| 1444 | // unions being lowered a specific way etc. |
| 1445 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
| 1446 | IRType->isIntegerTy(32)) { |
| 1447 | unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1449 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 1450 | SourceOffset*8+64, getContext())) |
| 1451 | return IRType; |
| 1452 | } |
| 1453 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1454 | |
Chris Lattner | fe12d1e | 2010-07-29 04:51:12 +0000 | [diff] [blame] | 1455 | if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1456 | // 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] | 1457 | const llvm::StructLayout *SL = getTargetData().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1458 | if (IROffset < SL->getSizeInBytes()) { |
| 1459 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 1460 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1461 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1462 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 1463 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1464 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1465 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1467 | if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1468 | const llvm::Type *EltTy = ATy->getElementType(); |
| 1469 | unsigned EltSize = getTargetData().getTypeAllocSize(EltTy); |
| 1470 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1471 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 1472 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1473 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1475 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 1476 | // 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] | 1477 | unsigned TySizeInBytes = |
| 1478 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1480 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1482 | // It is always safe to classify this as an integer type up to i64 that |
| 1483 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1484 | return llvm::IntegerType::get(getVMContext(), |
| 1485 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1488 | |
| 1489 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 1490 | /// be used as elements of a two register pair to pass or return, return a |
| 1491 | /// first class aggregate to represent them. For example, if the low part of |
| 1492 | /// a by-value argument should be passed as i32* and the high part as float, |
| 1493 | /// return {i32*, float}. |
| 1494 | static const llvm::Type * |
| 1495 | GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi, |
| 1496 | const llvm::TargetData &TD) { |
| 1497 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 1498 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 1499 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 1500 | // the second element at offset 8. Check for this: |
| 1501 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 1502 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
| 1503 | unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign); |
| 1504 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
| 1505 | |
| 1506 | // To handle this, we have to increase the size of the low part so that the |
| 1507 | // second element will start at an 8 byte offset. We can't increase the size |
| 1508 | // of the second element because it might make us access off the end of the |
| 1509 | // struct. |
| 1510 | if (HiStart != 8) { |
| 1511 | // There are only two sorts of types the ABI generation code can produce for |
| 1512 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 1513 | // Promote these to a larger type. |
| 1514 | if (Lo->isFloatTy()) |
| 1515 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 1516 | else { |
| 1517 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 1518 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | const llvm::StructType *Result = |
| 1523 | llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL); |
| 1524 | |
| 1525 | |
| 1526 | // Verify that the second element is at an 8-byte offset. |
| 1527 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 1528 | "Invalid x86-64 argument pair!"); |
| 1529 | return Result; |
| 1530 | } |
| 1531 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1532 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1533 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1534 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 1535 | // classification algorithm. |
| 1536 | X86_64ABIInfo::Class Lo, Hi; |
| 1537 | classify(RetTy, 0, Lo, Hi); |
| 1538 | |
| 1539 | // Check some invariants. |
| 1540 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1541 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1542 | |
| 1543 | const llvm::Type *ResType = 0; |
| 1544 | switch (Lo) { |
| 1545 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1546 | if (Hi == NoClass) |
| 1547 | return ABIArgInfo::getIgnore(); |
| 1548 | // If the low part is just padding, it takes no register, leave ResType |
| 1549 | // null. |
| 1550 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1551 | "Unknown missing lo part"); |
| 1552 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1553 | |
| 1554 | case SSEUp: |
| 1555 | case X87Up: |
| 1556 | assert(0 && "Invalid classification for lo word."); |
| 1557 | |
| 1558 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 1559 | // hidden argument. |
| 1560 | case Memory: |
| 1561 | return getIndirectReturnResult(RetTy); |
| 1562 | |
| 1563 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 1564 | // available register of the sequence %rax, %rdx is used. |
| 1565 | case Integer: |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1566 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, |
| 1567 | RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1569 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1570 | // so that the parameter gets the right LLVM IR attributes. |
| 1571 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1572 | // Treat an enum type as its underlying type. |
| 1573 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1574 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1576 | if (RetTy->isIntegralOrEnumerationType() && |
| 1577 | RetTy->isPromotableIntegerType()) |
| 1578 | return ABIArgInfo::getExtend(); |
| 1579 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | |
| 1582 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 1583 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 1584 | case SSE: |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1585 | ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1586 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1587 | |
| 1588 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 1589 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 1590 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1591 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1592 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1593 | |
| 1594 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 1595 | // part of the value is returned in %st0 and the imaginary part in |
| 1596 | // %st1. |
| 1597 | case ComplexX87: |
| 1598 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1599 | ResType = llvm::StructType::get(getVMContext(), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1600 | llvm::Type::getX86_FP80Ty(getVMContext()), |
| 1601 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1602 | NULL); |
| 1603 | break; |
| 1604 | } |
| 1605 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1606 | const llvm::Type *HighPart = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1607 | switch (Hi) { |
| 1608 | // Memory was handled previously and X87 should |
| 1609 | // never occur as a hi class. |
| 1610 | case Memory: |
| 1611 | case X87: |
| 1612 | assert(0 && "Invalid classification for hi word."); |
| 1613 | |
| 1614 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1615 | case NoClass: |
| 1616 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1618 | case Integer: |
| 1619 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), |
| 1620 | 8, RetTy, 8); |
| 1621 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1622 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1623 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1624 | case SSE: |
| 1625 | HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8); |
| 1626 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1627 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | |
| 1630 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 1631 | // is passed in the upper half of the last used SSE register. |
| 1632 | // |
| 1633 | // SSEUP should always be preceeded by SSE, just widen. |
| 1634 | case SSEUp: |
| 1635 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1636 | ResType = Get16ByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | |
| 1639 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 1640 | // returned together with the previous X87 value in %st0. |
| 1641 | case X87Up: |
| 1642 | // If X87Up is preceeded by X87, we don't need to do |
| 1643 | // anything. However, in some cases with unions it may not be |
| 1644 | // preceeded by X87. In such situations we follow gcc and pass the |
| 1645 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1646 | if (Lo != X87) { |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1647 | HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), |
| 1648 | 8, RetTy, 8); |
| 1649 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1650 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1651 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | } |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1654 | |
| 1655 | // 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] | 1656 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1657 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1658 | if (HighPart) |
| 1659 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1661 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1664 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt, |
Chris Lattner | 5868ca2 | 2010-07-29 04:41:05 +0000 | [diff] [blame] | 1665 | unsigned &neededSSE) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1666 | X86_64ABIInfo::Class Lo, Hi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1667 | classify(Ty, 0, Lo, Hi); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1668 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1669 | // Check some invariants. |
| 1670 | // FIXME: Enforce these by construction. |
| 1671 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1672 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1673 | |
| 1674 | neededInt = 0; |
| 1675 | neededSSE = 0; |
| 1676 | const llvm::Type *ResType = 0; |
| 1677 | switch (Lo) { |
| 1678 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1679 | if (Hi == NoClass) |
| 1680 | return ABIArgInfo::getIgnore(); |
| 1681 | // If the low part is just padding, it takes no register, leave ResType |
| 1682 | // null. |
| 1683 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1684 | "Unknown missing lo part"); |
| 1685 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1686 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1687 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 1688 | // on the stack. |
| 1689 | case Memory: |
| 1690 | |
| 1691 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 1692 | // COMPLEX_X87, it is passed in memory. |
| 1693 | case X87: |
| 1694 | case ComplexX87: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1695 | return getIndirectResult(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1696 | |
| 1697 | case SSEUp: |
| 1698 | case X87Up: |
| 1699 | assert(0 && "Invalid classification for lo word."); |
| 1700 | |
| 1701 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 1702 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 1703 | // and %r9 is used. |
| 1704 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1705 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1707 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1708 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1709 | |
| 1710 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1711 | // so that the parameter gets the right LLVM IR attributes. |
| 1712 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1713 | // Treat an enum type as its underlying type. |
| 1714 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1715 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1717 | if (Ty->isIntegralOrEnumerationType() && |
| 1718 | Ty->isPromotableIntegerType()) |
| 1719 | return ABIArgInfo::getExtend(); |
| 1720 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1721 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1722 | break; |
| 1723 | |
| 1724 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 1725 | // available SSE register is used, the registers are taken in the |
| 1726 | // order from %xmm0 to %xmm7. |
| 1727 | case SSE: |
| 1728 | ++neededSSE; |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1729 | ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1730 | break; |
| 1731 | } |
| 1732 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1733 | const llvm::Type *HighPart = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1734 | switch (Hi) { |
| 1735 | // Memory was handled previously, ComplexX87 and X87 should |
| 1736 | // never occur as hi classes, and X87Up must be preceed by X87, |
| 1737 | // which is passed in memory. |
| 1738 | case Memory: |
| 1739 | case X87: |
| 1740 | case ComplexX87: |
| 1741 | assert(0 && "Invalid classification for hi word."); |
| 1742 | break; |
| 1743 | |
| 1744 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1746 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1747 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1748 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1749 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1751 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1752 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | |
| 1755 | // X87Up generally doesn't occur here (long double is passed in |
| 1756 | // memory), except in situations involving unions. |
| 1757 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1758 | case SSE: |
| 1759 | HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1760 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1761 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1762 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1763 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1764 | ++neededSSE; |
| 1765 | break; |
| 1766 | |
| 1767 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 1768 | // 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] | 1769 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1770 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 1771 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1772 | ResType = Get16ByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1773 | break; |
| 1774 | } |
| 1775 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1776 | // If a high part was specified, merge it together with the low part. It is |
| 1777 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1778 | // first class struct aggregate with the high and low part: {low, high} |
| 1779 | if (HighPart) |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1780 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1781 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1782 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1785 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1786 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1787 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1788 | |
| 1789 | // Keep track of the number of assigned registers. |
| 1790 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
| 1791 | |
| 1792 | // If the return value is indirect, then the hidden argument is consuming one |
| 1793 | // integer register. |
| 1794 | if (FI.getReturnInfo().isIndirect()) |
| 1795 | --freeIntRegs; |
| 1796 | |
| 1797 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 1798 | // get assigned (in left-to-right order) for passing as follows... |
| 1799 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1800 | it != ie; ++it) { |
| 1801 | unsigned neededInt, neededSSE; |
Chris Lattner | 5868ca2 | 2010-07-29 04:41:05 +0000 | [diff] [blame] | 1802 | it->info = classifyArgumentType(it->type, neededInt, neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1803 | |
| 1804 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 1805 | // eightbyte of an argument, the whole argument is passed on the |
| 1806 | // stack. If registers have already been assigned for some |
| 1807 | // eightbytes of such an argument, the assignments get reverted. |
| 1808 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
| 1809 | freeIntRegs -= neededInt; |
| 1810 | freeSSERegs -= neededSSE; |
| 1811 | } else { |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1812 | it->info = getIndirectResult(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 1818 | QualType Ty, |
| 1819 | CodeGenFunction &CGF) { |
| 1820 | llvm::Value *overflow_arg_area_p = |
| 1821 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 1822 | llvm::Value *overflow_arg_area = |
| 1823 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 1824 | |
| 1825 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 1826 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
| 1827 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 1828 | if (Align > 8) { |
| 1829 | // Note that we follow the ABI & gcc here, even though the type |
| 1830 | // could in theory have an alignment greater than 16. This case |
| 1831 | // shouldn't ever matter in practice. |
| 1832 | |
| 1833 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1834 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1835 | llvm::ConstantInt::get(CGF.Int32Ty, 15); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1836 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 1837 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1838 | CGF.Int64Ty); |
| 1839 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1840 | overflow_arg_area = |
| 1841 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1842 | overflow_arg_area->getType(), |
| 1843 | "overflow_arg_area.align"); |
| 1844 | } |
| 1845 | |
| 1846 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
| 1847 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 1848 | llvm::Value *Res = |
| 1849 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1850 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1851 | |
| 1852 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 1853 | // l->overflow_arg_area + sizeof(type). |
| 1854 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 1855 | // an 8 byte boundary. |
| 1856 | |
| 1857 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1858 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1859 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1860 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 1861 | "overflow_arg_area.next"); |
| 1862 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 1863 | |
| 1864 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 1865 | return Res; |
| 1866 | } |
| 1867 | |
| 1868 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1869 | CodeGenFunction &CGF) const { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1870 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1872 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 1873 | // struct { |
| 1874 | // i32 gp_offset; |
| 1875 | // i32 fp_offset; |
| 1876 | // i8* overflow_arg_area; |
| 1877 | // i8* reg_save_area; |
| 1878 | // }; |
| 1879 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 1881 | Ty = CGF.getContext().getCanonicalType(Ty); |
Chris Lattner | 5868ca2 | 2010-07-29 04:41:05 +0000 | [diff] [blame] | 1882 | ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1883 | |
| 1884 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 1885 | // in the registers. If not go to step 7. |
| 1886 | if (!neededInt && !neededSSE) |
| 1887 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 1888 | |
| 1889 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 1890 | // general purpose registers needed to pass type and num_fp to hold |
| 1891 | // the number of floating point registers needed. |
| 1892 | |
| 1893 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 1894 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 1895 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 1896 | // |
| 1897 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 1898 | // register save space). |
| 1899 | |
| 1900 | llvm::Value *InRegs = 0; |
| 1901 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 1902 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 1903 | if (neededInt) { |
| 1904 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 1905 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1906 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 1907 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | if (neededSSE) { |
| 1911 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 1912 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 1913 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1914 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 1915 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1916 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 1917 | } |
| 1918 | |
| 1919 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 1920 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 1921 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 1922 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 1923 | |
| 1924 | // Emit code to load the value if it was passed in registers. |
| 1925 | |
| 1926 | CGF.EmitBlock(InRegBlock); |
| 1927 | |
| 1928 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 1929 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 1930 | // copying to a temporary location in case the parameter is passed |
| 1931 | // in different register classes or requires an alignment greater |
| 1932 | // than 8 for general purpose registers and 16 for XMM registers. |
| 1933 | // |
| 1934 | // FIXME: This really results in shameful code when we end up needing to |
| 1935 | // collect arguments from different places; often what should result in a |
| 1936 | // simple assembling of a structure from scattered addresses has many more |
| 1937 | // loads than necessary. Can we clean this up? |
| 1938 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 1939 | llvm::Value *RegAddr = |
| 1940 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 1941 | "reg_save_area"); |
| 1942 | if (neededInt && neededSSE) { |
| 1943 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1944 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1945 | const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
| 1946 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 1947 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
| 1948 | const llvm::Type *TyLo = ST->getElementType(0); |
| 1949 | const llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 1950 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1951 | "Unexpected ABI info for mixed regs"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1952 | const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 1953 | const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1954 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1955 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 1956 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 1957 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1958 | llvm::Value *V = |
| 1959 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 1960 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1961 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 1962 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1963 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1964 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1965 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1966 | } else if (neededInt) { |
| 1967 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1968 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1969 | llvm::PointerType::getUnqual(LTy)); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1970 | } else if (neededSSE == 1) { |
| 1971 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1972 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 1973 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1974 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1975 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 1976 | // SSE registers are spaced 16 bytes apart in the register save |
| 1977 | // area, we need to collect the two eightbytes together. |
| 1978 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1979 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1980 | const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext); |
| 1981 | const llvm::Type *DblPtrTy = |
| 1982 | llvm::PointerType::getUnqual(DoubleTy); |
| 1983 | const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy, |
| 1984 | DoubleTy, NULL); |
| 1985 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 1986 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 1987 | DblPtrTy)); |
| 1988 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1989 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 1990 | DblPtrTy)); |
| 1991 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1992 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 1993 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 1997 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 1998 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 1999 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2000 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2001 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2002 | gp_offset_p); |
| 2003 | } |
| 2004 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2005 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2006 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2007 | fp_offset_p); |
| 2008 | } |
| 2009 | CGF.EmitBranch(ContBlock); |
| 2010 | |
| 2011 | // Emit code to load the value if it was passed in memory. |
| 2012 | |
| 2013 | CGF.EmitBlock(InMemBlock); |
| 2014 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2015 | |
| 2016 | // Return the appropriate result. |
| 2017 | |
| 2018 | CGF.EmitBlock(ContBlock); |
| 2019 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), |
| 2020 | "vaarg.addr"); |
| 2021 | ResAddr->reserveOperandSpace(2); |
| 2022 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2023 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2024 | return ResAddr; |
| 2025 | } |
| 2026 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2027 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2028 | CodeGenFunction &CGF) const { |
| 2029 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 2030 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2032 | CGBuilderTy &Builder = CGF.Builder; |
| 2033 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2034 | "ap"); |
| 2035 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2036 | llvm::Type *PTy = |
| 2037 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2038 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2039 | |
| 2040 | uint64_t Offset = |
| 2041 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2042 | llvm::Value *NextAddr = |
| 2043 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2044 | "ap.next"); |
| 2045 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2046 | |
| 2047 | return AddrTyped; |
| 2048 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2049 | |
| 2050 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2051 | // PIC16 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2052 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2053 | |
| 2054 | namespace { |
| 2055 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2056 | class PIC16ABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2057 | public: |
| 2058 | PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2060 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2061 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2062 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2064 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2065 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2066 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2067 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2068 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2072 | CodeGenFunction &CGF) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2073 | }; |
| 2074 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2075 | class PIC16TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2076 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2077 | PIC16TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2078 | : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2079 | }; |
| 2080 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2083 | ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2084 | if (RetTy->isVoidType()) { |
| 2085 | return ABIArgInfo::getIgnore(); |
| 2086 | } else { |
| 2087 | return ABIArgInfo::getDirect(); |
| 2088 | } |
| 2089 | } |
| 2090 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2091 | ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | return ABIArgInfo::getDirect(); |
| 2093 | } |
| 2094 | |
| 2095 | llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2096 | CodeGenFunction &CGF) const { |
Chris Lattner | 52d9ae3 | 2010-04-06 17:29:22 +0000 | [diff] [blame] | 2097 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Sanjiv Gupta | a446ecd | 2010-02-17 02:25:52 +0000 | [diff] [blame] | 2098 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 2099 | |
| 2100 | CGBuilderTy &Builder = CGF.Builder; |
| 2101 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2102 | "ap"); |
| 2103 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2104 | llvm::Type *PTy = |
| 2105 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2106 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2107 | |
| 2108 | uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8; |
| 2109 | |
| 2110 | llvm::Value *NextAddr = |
| 2111 | Builder.CreateGEP(Addr, llvm::ConstantInt::get( |
| 2112 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset), |
| 2113 | "ap.next"); |
| 2114 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2115 | |
| 2116 | return AddrTyped; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Sanjiv Gupta | a446ecd | 2010-02-17 02:25:52 +0000 | [diff] [blame] | 2119 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2120 | // PowerPC-32 |
| 2121 | |
| 2122 | namespace { |
| 2123 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2124 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2125 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2126 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2127 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2128 | // This is recovered from gcc output. |
| 2129 | return 1; // r1 is the dedicated stack pointer |
| 2130 | } |
| 2131 | |
| 2132 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2133 | llvm::Value *Address) const; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2134 | }; |
| 2135 | |
| 2136 | } |
| 2137 | |
| 2138 | bool |
| 2139 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2140 | llvm::Value *Address) const { |
| 2141 | // This is calculated from the LLVM and GCC tables and verified |
| 2142 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2143 | |
| 2144 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2145 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2146 | |
| 2147 | const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
| 2148 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2149 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2150 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2151 | |
| 2152 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2153 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2154 | |
| 2155 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2156 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2157 | |
| 2158 | // 64-76 are various 4-byte special-purpose registers: |
| 2159 | // 64: mq |
| 2160 | // 65: lr |
| 2161 | // 66: ctr |
| 2162 | // 67: ap |
| 2163 | // 68-75 cr0-7 |
| 2164 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2165 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2166 | |
| 2167 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2168 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2169 | |
| 2170 | // 109: vrsave |
| 2171 | // 110: vscr |
| 2172 | // 111: spe_acc |
| 2173 | // 112: spefscr |
| 2174 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2175 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2176 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2177 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2181 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2182 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2183 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2184 | |
| 2185 | namespace { |
| 2186 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2187 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2188 | public: |
| 2189 | enum ABIKind { |
| 2190 | APCS = 0, |
| 2191 | AAPCS = 1, |
| 2192 | AAPCS_VFP |
| 2193 | }; |
| 2194 | |
| 2195 | private: |
| 2196 | ABIKind Kind; |
| 2197 | |
| 2198 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2199 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {} |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2200 | |
| 2201 | private: |
| 2202 | ABIKind getABIKind() const { return Kind; } |
| 2203 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2204 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2205 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2207 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2208 | |
| 2209 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2210 | CodeGenFunction &CGF) const; |
| 2211 | }; |
| 2212 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2213 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2214 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2215 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 2216 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2217 | |
| 2218 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2219 | return 13; |
| 2220 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2221 | }; |
| 2222 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2225 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2226 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2227 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2228 | it != ie; ++it) |
| 2229 | it->info = classifyArgumentType(it->type); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2230 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2231 | const llvm::Triple &Triple(getContext().Target.getTriple()); |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2232 | llvm::CallingConv::ID DefaultCC; |
Rafael Espindola | 1ed1a59 | 2010-06-16 19:01:17 +0000 | [diff] [blame] | 2233 | if (Triple.getEnvironmentName() == "gnueabi" || |
| 2234 | Triple.getEnvironmentName() == "eabi") |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2235 | DefaultCC = llvm::CallingConv::ARM_AAPCS; |
Rafael Espindola | 1ed1a59 | 2010-06-16 19:01:17 +0000 | [diff] [blame] | 2236 | else |
| 2237 | DefaultCC = llvm::CallingConv::ARM_APCS; |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2238 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2239 | switch (getABIKind()) { |
| 2240 | case APCS: |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2241 | if (DefaultCC != llvm::CallingConv::ARM_APCS) |
| 2242 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2243 | break; |
| 2244 | |
| 2245 | case AAPCS: |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2246 | if (DefaultCC != llvm::CallingConv::ARM_AAPCS) |
| 2247 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2248 | break; |
| 2249 | |
| 2250 | case AAPCS_VFP: |
| 2251 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP); |
| 2252 | break; |
| 2253 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2256 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2257 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2258 | // Treat an enum type as its underlying type. |
| 2259 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2260 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2261 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2262 | return (Ty->isPromotableIntegerType() ? |
| 2263 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2264 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2265 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2266 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2267 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2268 | return ABIArgInfo::getIgnore(); |
| 2269 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2270 | // Structures with either a non-trivial destructor or a non-trivial |
| 2271 | // copy constructor are always indirect. |
| 2272 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 2273 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2274 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2275 | // FIXME: This is kind of nasty... but there isn't much choice because the ARM |
| 2276 | // backend doesn't support byval. |
| 2277 | // FIXME: This doesn't handle alignment > 64 bits. |
| 2278 | const llvm::Type* ElemTy; |
| 2279 | unsigned SizeRegs; |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2280 | if (getContext().getTypeAlign(Ty) > 32) { |
| 2281 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 2282 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2283 | } else { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2284 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 2285 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2286 | } |
| 2287 | std::vector<const llvm::Type*> LLVMFields; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2288 | LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2289 | const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields, |
| 2290 | true); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2291 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2294 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2295 | llvm::LLVMContext &VMContext) { |
| 2296 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 2297 | // is called integer-like if its size is less than or equal to one word, and |
| 2298 | // the offset of each of its addressable sub-fields is zero. |
| 2299 | |
| 2300 | uint64_t Size = Context.getTypeSize(Ty); |
| 2301 | |
| 2302 | // Check that the type fits in a word. |
| 2303 | if (Size > 32) |
| 2304 | return false; |
| 2305 | |
| 2306 | // FIXME: Handle vector types! |
| 2307 | if (Ty->isVectorType()) |
| 2308 | return false; |
| 2309 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 2310 | // Float types are never treated as "integer like". |
| 2311 | if (Ty->isRealFloatingType()) |
| 2312 | return false; |
| 2313 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2314 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2315 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2316 | return true; |
| 2317 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 2318 | // Small complex integer types are "integer like". |
| 2319 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 2320 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2321 | |
| 2322 | // Single element and zero sized arrays should be allowed, by the definition |
| 2323 | // above, but they are not. |
| 2324 | |
| 2325 | // Otherwise, it must be a record type. |
| 2326 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2327 | if (!RT) return false; |
| 2328 | |
| 2329 | // Ignore records with flexible arrays. |
| 2330 | const RecordDecl *RD = RT->getDecl(); |
| 2331 | if (RD->hasFlexibleArrayMember()) |
| 2332 | return false; |
| 2333 | |
| 2334 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 2335 | // like". |
| 2336 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2337 | |
| 2338 | bool HadField = false; |
| 2339 | unsigned idx = 0; |
| 2340 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2341 | i != e; ++i, ++idx) { |
| 2342 | const FieldDecl *FD = *i; |
| 2343 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2344 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 2345 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 2346 | // struct { int : 0; int x } |
| 2347 | // is non-integer like according to gcc. |
| 2348 | if (FD->isBitField()) { |
| 2349 | if (!RD->isUnion()) |
| 2350 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2351 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2352 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2353 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2354 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2355 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2358 | // Check if this field is at offset 0. |
| 2359 | if (Layout.getFieldOffset(idx) != 0) |
| 2360 | return false; |
| 2361 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2362 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2363 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2364 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2365 | // Only allow at most one field in a structure. This doesn't match the |
| 2366 | // wording above, but follows gcc in situations with a field following an |
| 2367 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2368 | if (!RD->isUnion()) { |
| 2369 | if (HadField) |
| 2370 | return false; |
| 2371 | |
| 2372 | HadField = true; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2379 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2380 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2381 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2382 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2383 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2384 | // Treat an enum type as its underlying type. |
| 2385 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2386 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 2387 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2388 | return (RetTy->isPromotableIntegerType() ? |
| 2389 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2390 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2391 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2392 | // Structures with either a non-trivial destructor or a non-trivial |
| 2393 | // copy constructor are always indirect. |
| 2394 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
| 2395 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2396 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2397 | // Are we following APCS? |
| 2398 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2399 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2400 | return ABIArgInfo::getIgnore(); |
| 2401 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2402 | // Complex types are all returned as packed integers. |
| 2403 | // |
| 2404 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 2405 | // correctly. |
| 2406 | if (RetTy->isAnyComplexType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2407 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2408 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2409 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2410 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2411 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2412 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2413 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2414 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2415 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2416 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2417 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2418 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | // Otherwise return in memory. |
| 2422 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2423 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2424 | |
| 2425 | // Otherwise this is an AAPCS variant. |
| 2426 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2427 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2428 | return ABIArgInfo::getIgnore(); |
| 2429 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2430 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 2431 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2432 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2433 | if (Size <= 32) { |
| 2434 | // Return in the smallest viable integer type. |
| 2435 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2436 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2437 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2438 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2439 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2442 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2446 | CodeGenFunction &CGF) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2447 | // FIXME: Need to handle alignment |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2448 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2449 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2450 | |
| 2451 | CGBuilderTy &Builder = CGF.Builder; |
| 2452 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2453 | "ap"); |
| 2454 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2455 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2456 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2457 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2458 | |
| 2459 | uint64_t Offset = |
| 2460 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 2461 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2462 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2463 | "ap.next"); |
| 2464 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2465 | |
| 2466 | return AddrTyped; |
| 2467 | } |
| 2468 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2469 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 2470 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2471 | return ABIArgInfo::getIgnore(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2472 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2473 | if (isAggregateTypeForABI(RetTy)) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2474 | return ABIArgInfo::getIndirect(0); |
| 2475 | |
| 2476 | // Treat an enum type as its underlying type. |
| 2477 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2478 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 2479 | |
| 2480 | return (RetTy->isPromotableIntegerType() ? |
| 2481 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2484 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2485 | // SystemZ ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2486 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2487 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2488 | namespace { |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2489 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2490 | class SystemZABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2491 | public: |
| 2492 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2493 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2494 | bool isPromotableIntegerType(QualType Ty) const; |
| 2495 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2496 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2497 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2499 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2500 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2501 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2502 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2503 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2507 | CodeGenFunction &CGF) const; |
| 2508 | }; |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2509 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2510 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2511 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2512 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2513 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2514 | }; |
| 2515 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 2519 | // 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] | 2520 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2521 | switch (BT->getKind()) { |
| 2522 | case BuiltinType::Bool: |
| 2523 | case BuiltinType::Char_S: |
| 2524 | case BuiltinType::Char_U: |
| 2525 | case BuiltinType::SChar: |
| 2526 | case BuiltinType::UChar: |
| 2527 | case BuiltinType::Short: |
| 2528 | case BuiltinType::UShort: |
| 2529 | case BuiltinType::Int: |
| 2530 | case BuiltinType::UInt: |
| 2531 | return true; |
| 2532 | default: |
| 2533 | return false; |
| 2534 | } |
| 2535 | return false; |
| 2536 | } |
| 2537 | |
| 2538 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2539 | CodeGenFunction &CGF) const { |
| 2540 | // FIXME: Implement |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
| 2544 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2545 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 2546 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2547 | return ABIArgInfo::getIgnore(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2548 | if (isAggregateTypeForABI(RetTy)) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2549 | return ABIArgInfo::getIndirect(0); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2550 | |
| 2551 | return (isPromotableIntegerType(RetTy) ? |
| 2552 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2555 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2556 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2557 | return ABIArgInfo::getIndirect(0); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2558 | |
| 2559 | return (isPromotableIntegerType(Ty) ? |
| 2560 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2563 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2564 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2565 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2566 | |
| 2567 | namespace { |
| 2568 | |
| 2569 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2570 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2571 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2572 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2573 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2574 | CodeGen::CodeGenModule &M) const; |
| 2575 | }; |
| 2576 | |
| 2577 | } |
| 2578 | |
| 2579 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2580 | llvm::GlobalValue *GV, |
| 2581 | CodeGen::CodeGenModule &M) const { |
| 2582 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2583 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 2584 | // Handle 'interrupt' attribute: |
| 2585 | llvm::Function *F = cast<llvm::Function>(GV); |
| 2586 | |
| 2587 | // Step 1: Set ISR calling convention. |
| 2588 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 2589 | |
| 2590 | // Step 2: Add attributes goodness. |
| 2591 | F->addFnAttr(llvm::Attribute::NoInline); |
| 2592 | |
| 2593 | // Step 3: Emit ISR vector alias. |
| 2594 | unsigned Num = attr->getNumber() + 0xffe0; |
| 2595 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
| 2596 | "vector_" + |
| 2597 | llvm::LowercaseString(llvm::utohexstr(Num)), |
| 2598 | GV, &M.getModule()); |
| 2599 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2603 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2604 | // MIPS ABI Implementation. This works for both little-endian and |
| 2605 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2606 | //===----------------------------------------------------------------------===// |
| 2607 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2608 | namespace { |
| 2609 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2610 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2611 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2612 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2613 | |
| 2614 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 2615 | return 29; |
| 2616 | } |
| 2617 | |
| 2618 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2619 | llvm::Value *Address) const; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2620 | }; |
| 2621 | } |
| 2622 | |
| 2623 | bool |
| 2624 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2625 | llvm::Value *Address) const { |
| 2626 | // This information comes from gcc's implementation, which seems to |
| 2627 | // as canonical as it gets. |
| 2628 | |
| 2629 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2630 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2631 | |
| 2632 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 2633 | // are aliased to pairs of single-precision FP registers. |
| 2634 | const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
| 2635 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2636 | |
| 2637 | // 0-31 are the general purpose registers, $0 - $31. |
| 2638 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 2639 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 2640 | // 66 is the (notional, I think) register for signal-handler return. |
| 2641 | AssignToArrayRange(Builder, Address, Four8, 0, 65); |
| 2642 | |
| 2643 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 2644 | // They are one bit wide and ignored here. |
| 2645 | |
| 2646 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 2647 | // (coprocessor 1 is the FP unit) |
| 2648 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 2649 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 2650 | // 176-181 are the DSP accumulator registers. |
| 2651 | AssignToArrayRange(Builder, Address, Four8, 80, 181); |
| 2652 | |
| 2653 | return false; |
| 2654 | } |
| 2655 | |
| 2656 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2657 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2658 | if (TheTargetCodeGenInfo) |
| 2659 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2660 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2661 | // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't |
| 2662 | // free it. |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2664 | const llvm::Triple &Triple = getContext().Target.getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 2665 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2666 | default: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2667 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2668 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2669 | case llvm::Triple::mips: |
| 2670 | case llvm::Triple::mipsel: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2671 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2672 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2673 | case llvm::Triple::arm: |
| 2674 | case llvm::Triple::thumb: |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2675 | // FIXME: We want to know the float calling convention as well. |
Daniel Dunbar | 018ba5a | 2009-09-14 00:35:03 +0000 | [diff] [blame] | 2676 | if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2677 | return *(TheTargetCodeGenInfo = |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2678 | new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS)); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2679 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2680 | return *(TheTargetCodeGenInfo = |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2681 | new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2682 | |
| 2683 | case llvm::Triple::pic16: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2684 | return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2685 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2686 | case llvm::Triple::ppc: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2687 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2688 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2689 | case llvm::Triple::systemz: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2690 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2691 | |
| 2692 | case llvm::Triple::msp430: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2693 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2694 | |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 2695 | case llvm::Triple::x86: |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 2696 | switch (Triple.getOS()) { |
Edward O'Callaghan | 7ee68bd | 2009-10-20 17:22:50 +0000 | [diff] [blame] | 2697 | case llvm::Triple::Darwin: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2698 | return *(TheTargetCodeGenInfo = |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2699 | new X86_32TargetCodeGenInfo(Types, true, true)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2700 | case llvm::Triple::Cygwin: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2701 | case llvm::Triple::MinGW32: |
Edward O'Callaghan | 727e268 | 2009-10-21 11:58:24 +0000 | [diff] [blame] | 2702 | case llvm::Triple::AuroraUX: |
| 2703 | case llvm::Triple::DragonFly: |
David Chisnall | 75c135a | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 2704 | case llvm::Triple::FreeBSD: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2705 | case llvm::Triple::OpenBSD: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2706 | return *(TheTargetCodeGenInfo = |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2707 | new X86_32TargetCodeGenInfo(Types, false, true)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2708 | |
| 2709 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2710 | return *(TheTargetCodeGenInfo = |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2711 | new X86_32TargetCodeGenInfo(Types, false, false)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2712 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2714 | case llvm::Triple::x86_64: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2715 | switch (Triple.getOS()) { |
| 2716 | case llvm::Triple::Win32: |
| 2717 | case llvm::Triple::MinGW64: |
| 2718 | case llvm::Triple::Cygwin: |
| 2719 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
| 2720 | default: |
| 2721 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types)); |
| 2722 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 2723 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2724 | } |