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