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