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