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