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