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" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/SwiftCallingConv.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 29 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 30 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 34 | // Helper for coercing an aggregate argument or return value into an integer |
| 35 | // array of the same size (including padding) and alignment. This alternate |
| 36 | // coercion happens only for the RenderScript ABI and can be removed after |
| 37 | // runtimes that rely on it are no longer supported. |
| 38 | // |
| 39 | // RenderScript assumes that the size of the argument / return value in the IR |
| 40 | // is the same as the size of the corresponding qualified type. This helper |
| 41 | // coerces the aggregate type into an array of the same size (including |
| 42 | // padding). This coercion is used in lieu of expansion of struct members or |
| 43 | // other canonical coercions that return a coerced-type of larger size. |
| 44 | // |
| 45 | // Ty - The argument / return value type |
| 46 | // Context - The associated ASTContext |
| 47 | // LLVMContext - The associated LLVMContext |
| 48 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 49 | ASTContext &Context, |
| 50 | llvm::LLVMContext &LLVMContext) { |
| 51 | // Alignment and Size are measured in bits. |
| 52 | const uint64_t Size = Context.getTypeSize(Ty); |
| 53 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 54 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 55 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 56 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 57 | } |
| 58 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 59 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 60 | llvm::Value *Array, |
| 61 | llvm::Value *Value, |
| 62 | unsigned FirstIndex, |
| 63 | unsigned LastIndex) { |
| 64 | // Alternatively, we could emit this as a loop in the source. |
| 65 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 66 | llvm::Value *Cell = |
| 67 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 68 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 72 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 73 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 74 | T->isMemberFunctionPointerType(); |
| 75 | } |
| 76 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 77 | ABIArgInfo |
| 78 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 79 | llvm::Type *Padding) const { |
| 80 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 81 | ByRef, Realign, Padding); |
| 82 | } |
| 83 | |
| 84 | ABIArgInfo |
| 85 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 86 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 87 | /*ByRef*/ false, Realign); |
| 88 | } |
| 89 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 90 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 91 | QualType Ty) const { |
| 92 | return Address::invalid(); |
| 93 | } |
| 94 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 95 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 96 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 97 | /// Does the given lowering require more than the given number of |
| 98 | /// registers when expanded? |
| 99 | /// |
| 100 | /// This is intended to be the basis of a reasonable basic implementation |
| 101 | /// of should{Pass,Return}IndirectlyForSwift. |
| 102 | /// |
| 103 | /// For most targets, a limit of four total registers is reasonable; this |
| 104 | /// limits the amount of code required in order to move around the value |
| 105 | /// in case it wasn't produced immediately prior to the call by the caller |
| 106 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 107 | /// immediately within the callee. But some targets may need to further |
| 108 | /// limit the register count due to an inability to support that many |
| 109 | /// return registers. |
| 110 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 111 | ArrayRef<llvm::Type*> scalarTypes, |
| 112 | unsigned maxAllRegisters) { |
| 113 | unsigned intCount = 0, fpCount = 0; |
| 114 | for (llvm::Type *type : scalarTypes) { |
| 115 | if (type->isPointerTy()) { |
| 116 | intCount++; |
| 117 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 118 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 119 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 120 | } else { |
| 121 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 122 | fpCount++; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return (intCount + fpCount > maxAllRegisters); |
| 127 | } |
| 128 | |
| 129 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 130 | llvm::Type *eltTy, |
| 131 | unsigned numElts) const { |
| 132 | // The default implementation of this assumes that the target guarantees |
| 133 | // 128-bit SIMD support but nothing more. |
| 134 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 135 | } |
| 136 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 137 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 138 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 139 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 140 | if (!RD) |
| 141 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 142 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 146 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 147 | const RecordType *RT = T->getAs<RecordType>(); |
| 148 | if (!RT) |
| 149 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 150 | return getRecordArgABI(RT, CXXABI); |
| 151 | } |
| 152 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 153 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 154 | /// should ensure that all elements of the union have the same "machine type". |
| 155 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 156 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 157 | const RecordDecl *UD = UT->getDecl(); |
| 158 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 159 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 160 | return UD->field_begin()->getType(); |
| 161 | } |
| 162 | } |
| 163 | return Ty; |
| 164 | } |
| 165 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 166 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 167 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 170 | ASTContext &ABIInfo::getContext() const { |
| 171 | return CGT.getContext(); |
| 172 | } |
| 173 | |
| 174 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 175 | return CGT.getLLVMContext(); |
| 176 | } |
| 177 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 178 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 179 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 182 | const TargetInfo &ABIInfo::getTarget() const { |
| 183 | return CGT.getTarget(); |
| 184 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 185 | |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 186 | bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); } |
| 187 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 188 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 193 | uint64_t Members) const { |
| 194 | return false; |
| 195 | } |
| 196 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 197 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 198 | return false; |
| 199 | } |
| 200 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 201 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 202 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 203 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 204 | switch (TheKind) { |
| 205 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 206 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 207 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 208 | Ty->print(OS); |
| 209 | else |
| 210 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 212 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 213 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 214 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 215 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 216 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 217 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 218 | case InAlloca: |
| 219 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 220 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 221 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 222 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 223 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 224 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 225 | break; |
| 226 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 227 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 229 | case CoerceAndExpand: |
| 230 | OS << "CoerceAndExpand Type="; |
| 231 | getCoerceAndExpandType()->print(OS); |
| 232 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 234 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 237 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 238 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 239 | llvm::Value *Ptr, |
| 240 | CharUnits Align) { |
| 241 | llvm::Value *PtrAsInt = Ptr; |
| 242 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 243 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 244 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 245 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 246 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 247 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 248 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 249 | Ptr->getType(), |
| 250 | Ptr->getName() + ".aligned"); |
| 251 | return PtrAsInt; |
| 252 | } |
| 253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 254 | /// Emit va_arg for a platform using the common void* representation, |
| 255 | /// where arguments are simply emitted in an array of slots on the stack. |
| 256 | /// |
| 257 | /// This version implements the core direct-value passing rules. |
| 258 | /// |
| 259 | /// \param SlotSize - The size and alignment of a stack slot. |
| 260 | /// Each argument will be allocated to a multiple of this number of |
| 261 | /// slots, and all the slots will be aligned to this value. |
| 262 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 263 | /// an argument type with an alignment greater than the slot size |
| 264 | /// will be emitted on a higher-alignment address, potentially |
| 265 | /// leaving one or more empty slots behind as padding. If this |
| 266 | /// is false, the returned address might be less-aligned than |
| 267 | /// DirectAlign. |
| 268 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 269 | Address VAListAddr, |
| 270 | llvm::Type *DirectTy, |
| 271 | CharUnits DirectSize, |
| 272 | CharUnits DirectAlign, |
| 273 | CharUnits SlotSize, |
| 274 | bool AllowHigherAlign) { |
| 275 | // Cast the element type to i8* if necessary. Some platforms define |
| 276 | // va_list as a struct containing an i8* instead of just an i8*. |
| 277 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 278 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 279 | |
| 280 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 281 | |
| 282 | // If the CC aligns values higher than the slot size, do so if needed. |
| 283 | Address Addr = Address::invalid(); |
| 284 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 285 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 286 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 287 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 288 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 292 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 293 | llvm::Value *NextPtr = |
| 294 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 295 | "argp.next"); |
| 296 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 297 | |
| 298 | // If the argument is smaller than a slot, and this is a big-endian |
| 299 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 300 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 301 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 302 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 303 | } |
| 304 | |
| 305 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 306 | return Addr; |
| 307 | } |
| 308 | |
| 309 | /// Emit va_arg for a platform using the common void* representation, |
| 310 | /// where arguments are simply emitted in an array of slots on the stack. |
| 311 | /// |
| 312 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 313 | /// \param ValueInfo - The size and alignment of this type, generally |
| 314 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 315 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 316 | /// Each argument will be allocated to a multiple of this number of |
| 317 | /// slots, and all the slots will be aligned to this value. |
| 318 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 319 | /// an argument type with an alignment greater than the slot size |
| 320 | /// will be emitted on a higher-alignment address, potentially |
| 321 | /// leaving one or more empty slots behind as padding. |
| 322 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 323 | QualType ValueTy, bool IsIndirect, |
| 324 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 325 | CharUnits SlotSizeAndAlign, |
| 326 | bool AllowHigherAlign) { |
| 327 | // The size and alignment of the value that was passed directly. |
| 328 | CharUnits DirectSize, DirectAlign; |
| 329 | if (IsIndirect) { |
| 330 | DirectSize = CGF.getPointerSize(); |
| 331 | DirectAlign = CGF.getPointerAlign(); |
| 332 | } else { |
| 333 | DirectSize = ValueInfo.first; |
| 334 | DirectAlign = ValueInfo.second; |
| 335 | } |
| 336 | |
| 337 | // Cast the address we've calculated to the right type. |
| 338 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 339 | if (IsIndirect) |
| 340 | DirectTy = DirectTy->getPointerTo(0); |
| 341 | |
| 342 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 343 | DirectSize, DirectAlign, |
| 344 | SlotSizeAndAlign, |
| 345 | AllowHigherAlign); |
| 346 | |
| 347 | if (IsIndirect) { |
| 348 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 349 | } |
| 350 | |
| 351 | return Addr; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 356 | Address Addr1, llvm::BasicBlock *Block1, |
| 357 | Address Addr2, llvm::BasicBlock *Block2, |
| 358 | const llvm::Twine &Name = "") { |
| 359 | assert(Addr1.getType() == Addr2.getType()); |
| 360 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 361 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 362 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 363 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 364 | return Address(PHI, Align); |
| 365 | } |
| 366 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 367 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 368 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 369 | // If someone can figure out a general rule for this, that would be great. |
| 370 | // It's probably just doomed to be platform-dependent, though. |
| 371 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 372 | // Verified for: |
| 373 | // x86-64 FreeBSD, Linux, Darwin |
| 374 | // x86-32 FreeBSD, Linux, Darwin |
| 375 | // PowerPC Linux, Darwin |
| 376 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 377 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 378 | return 32; |
| 379 | } |
| 380 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 381 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 382 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 383 | // The following conventions are known to require this to be false: |
| 384 | // x86_stdcall |
| 385 | // MIPS |
| 386 | // For everything else, we just prefer false unless we opt out. |
| 387 | return false; |
| 388 | } |
| 389 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 390 | void |
| 391 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 392 | llvm::SmallString<24> &Opt) const { |
| 393 | // This assumes the user is passing a library name like "rt" instead of a |
| 394 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 395 | // dynamic. |
| 396 | Opt = "-l"; |
| 397 | Opt += Lib; |
| 398 | } |
| 399 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 400 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 401 | return llvm::CallingConv::C; |
| 402 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 403 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 404 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 405 | llvm::PointerType *T, QualType QT) const { |
| 406 | return llvm::ConstantPointerNull::get(T); |
| 407 | } |
| 408 | |
| 409 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
| 410 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, QualType SrcTy, |
| 411 | QualType DestTy) const { |
| 412 | // Since target may map different address spaces in AST to the same address |
| 413 | // space, an address space conversion may end up as a bitcast. |
| 414 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, |
| 415 | CGF.ConvertType(DestTy)); |
| 416 | } |
| 417 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 418 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 419 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 420 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 421 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 422 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 423 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 424 | if (FD->isUnnamedBitfield()) |
| 425 | return true; |
| 426 | |
| 427 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 428 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 429 | // Constant arrays of empty records count as empty, strip them off. |
| 430 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 431 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 432 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 433 | if (AT->getSize() == 0) |
| 434 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 435 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 436 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 437 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 438 | const RecordType *RT = FT->getAs<RecordType>(); |
| 439 | if (!RT) |
| 440 | return false; |
| 441 | |
| 442 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 443 | // |
| 444 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 445 | // current ABI. |
| 446 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 447 | return false; |
| 448 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 449 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 452 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 453 | /// fields. Note that a structure with a flexible array member is not |
| 454 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 455 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 456 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 457 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 458 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 459 | const RecordDecl *RD = RT->getDecl(); |
| 460 | if (RD->hasFlexibleArrayMember()) |
| 461 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 462 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 463 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 464 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 465 | for (const auto &I : CXXRD->bases()) |
| 466 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 467 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 468 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 469 | for (const auto *I : RD->fields()) |
| 470 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 471 | return false; |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | /// isSingleElementStruct - Determine if a structure is a "single |
| 476 | /// element struct", i.e. it has exactly one non-empty field or |
| 477 | /// exactly one field which is itself a single element |
| 478 | /// struct. Structures with flexible array members are never |
| 479 | /// considered single element structs. |
| 480 | /// |
| 481 | /// \return The field declaration for the single non-empty field, if |
| 482 | /// it exists. |
| 483 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 484 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 485 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 486 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 487 | |
| 488 | const RecordDecl *RD = RT->getDecl(); |
| 489 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 490 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 491 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 492 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 493 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 494 | // If this is a C++ record, check the bases first. |
| 495 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 496 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 497 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 498 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 499 | continue; |
| 500 | |
| 501 | // If we already found an element then this isn't a single-element struct. |
| 502 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 503 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 504 | |
| 505 | // If this is non-empty and not a single element struct, the composite |
| 506 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 507 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 508 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 509 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 514 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 515 | QualType FT = FD->getType(); |
| 516 | |
| 517 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 518 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 519 | continue; |
| 520 | |
| 521 | // If we already found an element then this isn't a single-element |
| 522 | // struct. |
| 523 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 524 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 525 | |
| 526 | // Treat single element arrays as the element. |
| 527 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 528 | if (AT->getSize().getZExtValue() != 1) |
| 529 | break; |
| 530 | FT = AT->getElementType(); |
| 531 | } |
| 532 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 533 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 534 | Found = FT.getTypePtr(); |
| 535 | } else { |
| 536 | Found = isSingleElementStruct(FT, Context); |
| 537 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 538 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 542 | // We don't consider a struct a single-element struct if it has |
| 543 | // padding beyond the element type. |
| 544 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 545 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 546 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 547 | return Found; |
| 548 | } |
| 549 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 550 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 551 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 552 | const ABIArgInfo &AI) { |
| 553 | // This default implementation defers to the llvm backend's va_arg |
| 554 | // instruction. It can handle only passing arguments directly |
| 555 | // (typically only handled in the backend for primitive types), or |
| 556 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 557 | // flag has ABI impact in the callee, this implementation cannot |
| 558 | // work.) |
| 559 | |
| 560 | // Only a few cases are covered here at the moment -- those needed |
| 561 | // by the default abi. |
| 562 | llvm::Value *Val; |
| 563 | |
| 564 | if (AI.isIndirect()) { |
| 565 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 566 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 567 | assert( |
| 568 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 569 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 570 | |
| 571 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 572 | CharUnits TyAlignForABI = TyInfo.second; |
| 573 | |
| 574 | llvm::Type *BaseTy = |
| 575 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 576 | llvm::Value *Addr = |
| 577 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 578 | return Address(Addr, TyAlignForABI); |
| 579 | } else { |
| 580 | assert((AI.isDirect() || AI.isExtend()) && |
| 581 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 582 | |
| 583 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 584 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 585 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 586 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 587 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 588 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 589 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 590 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 591 | |
| 592 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 593 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 594 | CGF.Builder.CreateStore(Val, Temp); |
| 595 | return Temp; |
| 596 | } |
| 597 | } |
| 598 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 599 | /// DefaultABIInfo - The default implementation for ABI specific |
| 600 | /// details. This implementation provides information which results in |
| 601 | /// self-consistent and sensible LLVM IR generation, but does not |
| 602 | /// conform to any particular ABI. |
| 603 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 604 | public: |
| 605 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 607 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 608 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 609 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 610 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 611 | if (!getCXXABI().classifyReturnType(FI)) |
| 612 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 613 | for (auto &I : FI.arguments()) |
| 614 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 617 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 618 | QualType Ty) const override { |
| 619 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 620 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 621 | }; |
| 622 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 623 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 624 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 625 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 626 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 629 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 630 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 631 | |
| 632 | if (isAggregateTypeForABI(Ty)) { |
| 633 | // Records with non-trivial destructors/copy-constructors should not be |
| 634 | // passed by value. |
| 635 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 636 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 637 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 638 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 639 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 641 | // Treat an enum type as its underlying type. |
| 642 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 643 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 645 | return (Ty->isPromotableIntegerType() ? |
| 646 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 649 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 650 | if (RetTy->isVoidType()) |
| 651 | return ABIArgInfo::getIgnore(); |
| 652 | |
| 653 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 654 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 655 | |
| 656 | // Treat an enum type as its underlying type. |
| 657 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 658 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 659 | |
| 660 | return (RetTy->isPromotableIntegerType() ? |
| 661 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 662 | } |
| 663 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 665 | // WebAssembly ABI Implementation |
| 666 | // |
| 667 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | |
| 670 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 671 | public: |
| 672 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 673 | : DefaultABIInfo(CGT) {} |
| 674 | |
| 675 | private: |
| 676 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 677 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 678 | |
| 679 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 680 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 681 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 682 | void computeInfo(CGFunctionInfo &FI) const override { |
| 683 | if (!getCXXABI().classifyReturnType(FI)) |
| 684 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 685 | for (auto &Arg : FI.arguments()) |
| 686 | Arg.info = classifyArgumentType(Arg.type); |
| 687 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 688 | |
| 689 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 690 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 694 | public: |
| 695 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 696 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 697 | }; |
| 698 | |
| 699 | /// \brief Classify argument of given type \p Ty. |
| 700 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 701 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 702 | |
| 703 | if (isAggregateTypeForABI(Ty)) { |
| 704 | // Records with non-trivial destructors/copy-constructors should not be |
| 705 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 706 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 707 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 708 | // Ignore empty structs/unions. |
| 709 | if (isEmptyRecord(getContext(), Ty, true)) |
| 710 | return ABIArgInfo::getIgnore(); |
| 711 | // Lower single-element structs to just pass a regular value. TODO: We |
| 712 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 713 | // though watch out for things like bitfields. |
| 714 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 715 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | // Otherwise just do the default thing. |
| 719 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 720 | } |
| 721 | |
| 722 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 723 | if (isAggregateTypeForABI(RetTy)) { |
| 724 | // Records with non-trivial destructors/copy-constructors should not be |
| 725 | // returned by value. |
| 726 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 727 | // Ignore empty structs/unions. |
| 728 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 729 | return ABIArgInfo::getIgnore(); |
| 730 | // Lower single-element structs to just return a regular value. TODO: We |
| 731 | // could do reasonable-size multiple-element structs too, using |
| 732 | // ABIArgInfo::getDirect(). |
| 733 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 734 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // Otherwise just do the default thing. |
| 739 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 740 | } |
| 741 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 742 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 743 | QualType Ty) const { |
| 744 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 745 | getContext().getTypeInfoInChars(Ty), |
| 746 | CharUnits::fromQuantity(4), |
| 747 | /*AllowHigherAlign=*/ true); |
| 748 | } |
| 749 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 750 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 751 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 752 | // |
| 753 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 754 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 755 | //===----------------------------------------------------------------------===// |
| 756 | |
| 757 | class PNaClABIInfo : public ABIInfo { |
| 758 | public: |
| 759 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 760 | |
| 761 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 762 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 763 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 764 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 765 | Address EmitVAArg(CodeGenFunction &CGF, |
| 766 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 770 | public: |
| 771 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 772 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 773 | }; |
| 774 | |
| 775 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 776 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 777 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 778 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 779 | for (auto &I : FI.arguments()) |
| 780 | I.info = classifyArgumentType(I.type); |
| 781 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 782 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 783 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 784 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 785 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 786 | // function classification. Structs get passed directly for varargs |
| 787 | // functions, through a rewriting transform in |
| 788 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 789 | // this target to actually support a va_arg instructions with an |
| 790 | // aggregate type, unlike other targets. |
| 791 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 794 | /// \brief Classify argument of given type \p Ty. |
| 795 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 796 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 797 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 798 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 799 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 800 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 801 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 802 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 803 | } else if (Ty->isFloatingType()) { |
| 804 | // Floating-point types don't go inreg. |
| 805 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 806 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 807 | |
| 808 | return (Ty->isPromotableIntegerType() ? |
| 809 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 813 | if (RetTy->isVoidType()) |
| 814 | return ABIArgInfo::getIgnore(); |
| 815 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 816 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 817 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 818 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 819 | |
| 820 | // Treat an enum type as its underlying type. |
| 821 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 822 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 823 | |
| 824 | return (RetTy->isPromotableIntegerType() ? |
| 825 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 826 | } |
| 827 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 828 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 829 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 830 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 831 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 832 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 833 | IRType->getScalarSizeInBits() != 64; |
| 834 | } |
| 835 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 836 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 837 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 838 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 839 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 840 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 841 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 842 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 845 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 849 | return Ty; |
| 850 | } |
| 851 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 852 | /// Returns true if this type can be passed in SSE registers with the |
| 853 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 854 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 855 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 856 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 857 | return true; |
| 858 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 859 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 860 | // registers specially. |
| 861 | unsigned VecSize = Context.getTypeSize(VT); |
| 862 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 863 | return true; |
| 864 | } |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 869 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 870 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 871 | return NumMembers <= 4; |
| 872 | } |
| 873 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 874 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 875 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 876 | auto AI = ABIArgInfo::getDirect(T); |
| 877 | AI.setInReg(true); |
| 878 | AI.setCanBeFlattened(false); |
| 879 | return AI; |
| 880 | } |
| 881 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 882 | //===----------------------------------------------------------------------===// |
| 883 | // X86-32 ABI Implementation |
| 884 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 885 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 886 | /// \brief Similar to llvm::CCState, but for Clang. |
| 887 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 888 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 889 | |
| 890 | unsigned CC; |
| 891 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 892 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 893 | }; |
| 894 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 895 | enum { |
| 896 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 897 | VectorcallMaxParamNumAsReg = 6 |
| 898 | }; |
| 899 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 900 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 901 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 902 | enum Class { |
| 903 | Integer, |
| 904 | Float |
| 905 | }; |
| 906 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 907 | static const unsigned MinABIStackAlignInBytes = 4; |
| 908 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 909 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 910 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 911 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 912 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 913 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 914 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 915 | |
| 916 | static bool isRegisterSize(unsigned Size) { |
| 917 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 918 | } |
| 919 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 920 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 921 | // FIXME: Assumes vectorcall is in use. |
| 922 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 923 | } |
| 924 | |
| 925 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 926 | uint64_t NumMembers) const override { |
| 927 | // FIXME: Assumes vectorcall is in use. |
| 928 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 929 | } |
| 930 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 931 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 932 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 933 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 934 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 935 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 936 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 937 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 938 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 939 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 940 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 941 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 942 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 943 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 944 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 945 | ABIArgInfo reclassifyHvaArgType(QualType RetTy, CCState &State, |
| 946 | const ABIArgInfo& current) const; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 947 | /// \brief Updates the number of available free registers, returns |
| 948 | /// true if any registers were allocated. |
| 949 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 950 | |
| 951 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 952 | bool &NeedsPadding) const; |
| 953 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 954 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 955 | bool canExpandIndirectArgument(QualType Ty) const; |
| 956 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 957 | /// \brief Rewrite the function info so that all memory arguments use |
| 958 | /// inalloca. |
| 959 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 960 | |
| 961 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 962 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 963 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 964 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 965 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 966 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 967 | public: |
| 968 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 969 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 970 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 971 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 972 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 973 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 974 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 975 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 976 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 977 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 978 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 979 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 980 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 981 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 982 | |
| 983 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 984 | ArrayRef<llvm::Type*> scalars, |
| 985 | bool asReturnValue) const override { |
| 986 | // LLVM's x86-32 lowering currently only assigns up to three |
| 987 | // integer registers and three fp registers. Oddly, it'll use up to |
| 988 | // four vector registers for vectors, but those can overlap with the |
| 989 | // scalar registers. |
| 990 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 991 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 992 | |
| 993 | bool isSwiftErrorInRegister() const override { |
| 994 | // x86-32 lowering does not support passing swifterror in a register. |
| 995 | return false; |
| 996 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 997 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 998 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 999 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1000 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1001 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1002 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1003 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1004 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1005 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1006 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1007 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1008 | static bool isStructReturnInRegABI( |
| 1009 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1010 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1011 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1012 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1013 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1014 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1015 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1016 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1017 | return 4; |
| 1018 | } |
| 1019 | |
| 1020 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1021 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1022 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1023 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1024 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1025 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1026 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1027 | } |
| 1028 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1029 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1030 | std::string &Constraints, |
| 1031 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1032 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1033 | std::vector<LValue> &ResultRegDests, |
| 1034 | std::string &AsmString, |
| 1035 | unsigned NumOutputs) const override; |
| 1036 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1037 | llvm::Constant * |
| 1038 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1039 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1040 | (0x06 << 8) | // .+0x08 |
| 1041 | ('F' << 16) | |
| 1042 | ('T' << 24); |
| 1043 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1044 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1045 | |
| 1046 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1047 | return "movl\t%ebp, %ebp" |
| 1048 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1049 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1050 | }; |
| 1051 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1052 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1054 | /// Rewrite input constraint references after adding some output constraints. |
| 1055 | /// In the case where there is one output and one input and we add one output, |
| 1056 | /// we need to replace all operand references greater than or equal to 1: |
| 1057 | /// mov $0, $1 |
| 1058 | /// mov eax, $1 |
| 1059 | /// The result will be: |
| 1060 | /// mov $0, $2 |
| 1061 | /// mov eax, $2 |
| 1062 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1063 | unsigned NumNewOuts, |
| 1064 | std::string &AsmString) { |
| 1065 | std::string Buf; |
| 1066 | llvm::raw_string_ostream OS(Buf); |
| 1067 | size_t Pos = 0; |
| 1068 | while (Pos < AsmString.size()) { |
| 1069 | size_t DollarStart = AsmString.find('$', Pos); |
| 1070 | if (DollarStart == std::string::npos) |
| 1071 | DollarStart = AsmString.size(); |
| 1072 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1073 | if (DollarEnd == std::string::npos) |
| 1074 | DollarEnd = AsmString.size(); |
| 1075 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1076 | Pos = DollarEnd; |
| 1077 | size_t NumDollars = DollarEnd - DollarStart; |
| 1078 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1079 | // We have an operand reference. |
| 1080 | size_t DigitStart = Pos; |
| 1081 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1082 | if (DigitEnd == std::string::npos) |
| 1083 | DigitEnd = AsmString.size(); |
| 1084 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1085 | unsigned OperandIndex; |
| 1086 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1087 | if (OperandIndex >= FirstIn) |
| 1088 | OperandIndex += NumNewOuts; |
| 1089 | OS << OperandIndex; |
| 1090 | } else { |
| 1091 | OS << OperandStr; |
| 1092 | } |
| 1093 | Pos = DigitEnd; |
| 1094 | } |
| 1095 | } |
| 1096 | AsmString = std::move(OS.str()); |
| 1097 | } |
| 1098 | |
| 1099 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1100 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1101 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1102 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1103 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1104 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1105 | unsigned NumOutputs) const { |
| 1106 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1107 | |
| 1108 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1109 | // larger. |
| 1110 | if (!Constraints.empty()) |
| 1111 | Constraints += ','; |
| 1112 | if (RetWidth <= 32) { |
| 1113 | Constraints += "={eax}"; |
| 1114 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1115 | } else { |
| 1116 | // Use the 'A' constraint for EAX:EDX. |
| 1117 | Constraints += "=A"; |
| 1118 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1119 | } |
| 1120 | |
| 1121 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1122 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1123 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1124 | |
| 1125 | // Coerce the integer by bitcasting the return slot pointer. |
| 1126 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1127 | CoerceTy->getPointerTo())); |
| 1128 | ResultRegDests.push_back(ReturnSlot); |
| 1129 | |
| 1130 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1131 | } |
| 1132 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1133 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1134 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1135 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1136 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1137 | uint64_t Size = Context.getTypeSize(Ty); |
| 1138 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1139 | // For i386, type must be register sized. |
| 1140 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1141 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1142 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (Ty->isVectorType()) { |
| 1145 | // 64- and 128- bit vectors inside structures are not returned in |
| 1146 | // registers. |
| 1147 | if (Size == 64 || Size == 128) |
| 1148 | return false; |
| 1149 | |
| 1150 | return true; |
| 1151 | } |
| 1152 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1153 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1154 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1155 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1156 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1157 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | |
| 1160 | // Arrays are treated like records. |
| 1161 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1162 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1165 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1166 | if (!RT) return false; |
| 1167 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1168 | // FIXME: Traverse bases here too. |
| 1169 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1170 | // Structure types are passed in register if all fields would be |
| 1171 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1172 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1173 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1174 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1175 | continue; |
| 1176 | |
| 1177 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1178 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1179 | return false; |
| 1180 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1181 | return true; |
| 1182 | } |
| 1183 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1184 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1185 | // Treat complex types as the element type. |
| 1186 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1187 | Ty = CTy->getElementType(); |
| 1188 | |
| 1189 | // Check for a type which we know has a simple scalar argument-passing |
| 1190 | // convention without any padding. (We're specifically looking for 32 |
| 1191 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1192 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1193 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1194 | return false; |
| 1195 | |
| 1196 | uint64_t Size = Context.getTypeSize(Ty); |
| 1197 | return Size == 32 || Size == 64; |
| 1198 | } |
| 1199 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1200 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1201 | uint64_t &Size) { |
| 1202 | for (const auto *FD : RD->fields()) { |
| 1203 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1204 | // argument is smaller than 32-bits, expanding the struct will create |
| 1205 | // alignment padding. |
| 1206 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1207 | return false; |
| 1208 | |
| 1209 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1210 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1211 | // counts as "basic" is more complicated than what we were doing previously. |
| 1212 | if (FD->isBitField()) |
| 1213 | return false; |
| 1214 | |
| 1215 | Size += Context.getTypeSize(FD->getType()); |
| 1216 | } |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1221 | uint64_t &Size) { |
| 1222 | // Don't do this if there are any non-empty bases. |
| 1223 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1224 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1225 | Size)) |
| 1226 | return false; |
| 1227 | } |
| 1228 | if (!addFieldSizes(Context, RD, Size)) |
| 1229 | return false; |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1233 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1234 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1235 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1236 | /// optimizations. |
| 1237 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1238 | // We can only expand structure types. |
| 1239 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1240 | if (!RT) |
| 1241 | return false; |
| 1242 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1243 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1244 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1245 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1246 | // On non-Windows, we have to conservatively match our old bitcode |
| 1247 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1248 | if (!CXXRD->isCLike()) |
| 1249 | return false; |
| 1250 | } else { |
| 1251 | // Don't do this for dynamic classes. |
| 1252 | if (CXXRD->isDynamicClass()) |
| 1253 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1254 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1255 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1256 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1259 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | // We can do this if there was no alignment padding. |
| 1263 | return Size == getContext().getTypeSize(Ty); |
| 1264 | } |
| 1265 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1266 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1267 | // If the return value is indirect, then the hidden argument is consuming one |
| 1268 | // integer register. |
| 1269 | if (State.FreeRegs) { |
| 1270 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1271 | if (!IsMCUABI) |
| 1272 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1273 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1274 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1277 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1278 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1279 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1280 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1281 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1282 | const Type *Base = nullptr; |
| 1283 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1284 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1285 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1286 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1287 | // The LLVM struct type for such an aggregate should lower properly. |
| 1288 | return ABIArgInfo::getDirect(); |
| 1289 | } |
| 1290 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1291 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1292 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1293 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1294 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1295 | |
| 1296 | // 128-bit vectors are a special case; they are returned in |
| 1297 | // registers and we need to make sure to pick a type the LLVM |
| 1298 | // backend will like. |
| 1299 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1300 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1301 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Always return in register if it fits in a general purpose |
| 1304 | // register, or if it is 64 bits and has a single element. |
| 1305 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1306 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1307 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1308 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1309 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1310 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1314 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1315 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1316 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1317 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1318 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1319 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1320 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1321 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1322 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1323 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1324 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1325 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1326 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1327 | // Ignore empty structs/unions. |
| 1328 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1329 | return ABIArgInfo::getIgnore(); |
| 1330 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1331 | // Small structures which are register sized are generally returned |
| 1332 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1333 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1334 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1335 | |
| 1336 | // As a special-case, if the struct is a "single-element" struct, and |
| 1337 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1338 | // floating-point register. (MSVC does not apply this special case.) |
| 1339 | // We apply a similar transformation for pointer types to improve the |
| 1340 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1341 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1342 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1343 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1344 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1345 | |
| 1346 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1347 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1348 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1351 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1352 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1354 | // Treat an enum type as its underlying type. |
| 1355 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1356 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1357 | |
| 1358 | return (RetTy->isPromotableIntegerType() ? |
| 1359 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1362 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1363 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1364 | } |
| 1365 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1366 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1367 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1368 | if (!RT) |
| 1369 | return 0; |
| 1370 | const RecordDecl *RD = RT->getDecl(); |
| 1371 | |
| 1372 | // If this is a C++ record, check the bases first. |
| 1373 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1374 | for (const auto &I : CXXRD->bases()) |
| 1375 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1376 | return false; |
| 1377 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1378 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1379 | QualType FT = i->getType(); |
| 1380 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1381 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1382 | return true; |
| 1383 | |
| 1384 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1391 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1392 | unsigned Align) const { |
| 1393 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1394 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1395 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1396 | return 0; // Use default alignment. |
| 1397 | |
| 1398 | // On non-Darwin, the stack type alignment is always 4. |
| 1399 | if (!IsDarwinVectorABI) { |
| 1400 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1401 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1402 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1403 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1404 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1405 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1406 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1407 | return 16; |
| 1408 | |
| 1409 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1412 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1413 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1414 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1415 | if (State.FreeRegs) { |
| 1416 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1417 | if (!IsMCUABI) |
| 1418 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1419 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1420 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1421 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1422 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1423 | // Compute the byval alignment. |
| 1424 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1425 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1426 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1427 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1428 | |
| 1429 | // If the stack alignment is less than the type alignment, realign the |
| 1430 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1431 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1432 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1433 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1436 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1437 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1438 | if (!T) |
| 1439 | T = Ty.getTypePtr(); |
| 1440 | |
| 1441 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1442 | BuiltinType::Kind K = BT->getKind(); |
| 1443 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1444 | return Float; |
| 1445 | } |
| 1446 | return Integer; |
| 1447 | } |
| 1448 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1449 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1450 | if (!IsSoftFloatABI) { |
| 1451 | Class C = classify(Ty); |
| 1452 | if (C == Float) |
| 1453 | return false; |
| 1454 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1455 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1456 | unsigned Size = getContext().getTypeSize(Ty); |
| 1457 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (SizeInRegs == 0) |
| 1460 | return false; |
| 1461 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1462 | if (!IsMCUABI) { |
| 1463 | if (SizeInRegs > State.FreeRegs) { |
| 1464 | State.FreeRegs = 0; |
| 1465 | return false; |
| 1466 | } |
| 1467 | } else { |
| 1468 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1469 | // earlier parameters that are passed on the stack. Also, |
| 1470 | // it does not allow passing >8-byte structs in-register, |
| 1471 | // even if there are 3 free registers available. |
| 1472 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1473 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1474 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1475 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1476 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | } |
| 1479 | |
| 1480 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1481 | bool &InReg, |
| 1482 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1483 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1484 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1485 | // (HFAs) have already been dealt with at this point. |
| 1486 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1487 | return false; |
| 1488 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1489 | NeedsPadding = false; |
| 1490 | InReg = !IsMCUABI; |
| 1491 | |
| 1492 | if (!updateFreeRegs(Ty, State)) |
| 1493 | return false; |
| 1494 | |
| 1495 | if (IsMCUABI) |
| 1496 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1497 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1498 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1499 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1500 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1501 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1502 | NeedsPadding = true; |
| 1503 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1504 | return false; |
| 1505 | } |
| 1506 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1507 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1510 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1511 | if (!updateFreeRegs(Ty, State)) |
| 1512 | return false; |
| 1513 | |
| 1514 | if (IsMCUABI) |
| 1515 | return false; |
| 1516 | |
| 1517 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1518 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1519 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1520 | if (getContext().getTypeSize(Ty) > 32) |
| 1521 | return false; |
| 1522 | |
| 1523 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1524 | Ty->isReferenceType()); |
| 1525 | } |
| 1526 | |
| 1527 | return true; |
| 1528 | } |
| 1529 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1530 | ABIArgInfo |
| 1531 | X86_32ABIInfo::reclassifyHvaArgType(QualType Ty, CCState &State, |
| 1532 | const ABIArgInfo ¤t) const { |
| 1533 | // Assumes vectorCall calling convention. |
| 1534 | const Type *Base = nullptr; |
| 1535 | uint64_t NumElts = 0; |
| 1536 | |
| 1537 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 1538 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1539 | if (State.FreeSSERegs >= NumElts) { |
| 1540 | // HVA types get passed directly in registers if there is room. |
| 1541 | State.FreeSSERegs -= NumElts; |
| 1542 | return getDirectX86Hva(); |
| 1543 | } |
| 1544 | // If there's no room, the HVA gets passed as normal indirect |
| 1545 | // structure. |
| 1546 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1547 | } |
| 1548 | return current; |
| 1549 | } |
| 1550 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1551 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1552 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1553 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1554 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1555 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1556 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1557 | // Check with the C++ ABI first. |
| 1558 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1559 | if (RT) { |
| 1560 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1561 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1562 | return getIndirectResult(Ty, false, State); |
| 1563 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1564 | // The field index doesn't matter, we'll fix it up later. |
| 1565 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1570 | // to other targets, regcall uses some of the HVA rules. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1571 | const Type *Base = nullptr; |
| 1572 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1573 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1574 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1575 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1578 | if (State.FreeSSERegs >= NumElts) { |
| 1579 | State.FreeSSERegs -= NumElts; |
| 1580 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1581 | return ABIArgInfo::getDirect(); |
| 1582 | return ABIArgInfo::getExpand(); |
| 1583 | |
| 1584 | } |
| 1585 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1586 | } else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1587 | if (State.FreeSSERegs >= NumElts && (Ty->isBuiltinType() || Ty->isVectorType())) { |
| 1588 | // Actual floating-point types get registers first time through if |
| 1589 | // there is registers available |
| 1590 | State.FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1591 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1592 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 1593 | // HVA Types only get registers after everything else has been |
| 1594 | // set, so it gets set as indirect for now. |
| 1595 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty)); |
| 1596 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1597 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1601 | // Structures with flexible arrays are always indirect. |
| 1602 | // FIXME: This should not be byval! |
| 1603 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1604 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1605 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1606 | // Ignore empty structs/unions on non-Windows. |
| 1607 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1608 | return ABIArgInfo::getIgnore(); |
| 1609 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1610 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1611 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1612 | bool NeedsPadding = false; |
| 1613 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1614 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1615 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1616 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1617 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1618 | if (InReg) |
| 1619 | return ABIArgInfo::getDirectInReg(Result); |
| 1620 | else |
| 1621 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1622 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1623 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1624 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1625 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1626 | // of those arguments will match the struct. This is important because the |
| 1627 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1628 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1629 | // Don't do this for the MCU if there are still free integer registers |
| 1630 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1631 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1632 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1633 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1634 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1635 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1636 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1637 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1638 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1639 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1642 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1643 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1644 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1645 | if (IsDarwinVectorABI) { |
| 1646 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1647 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1648 | (Size == 64 && VT->getNumElements() == 1)) |
| 1649 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1650 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1651 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1652 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1653 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1654 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1656 | return ABIArgInfo::getDirect(); |
| 1657 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1658 | |
| 1659 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1660 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1661 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1662 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1663 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (Ty->isPromotableIntegerType()) { |
| 1666 | if (InReg) |
| 1667 | return ABIArgInfo::getExtendInReg(); |
| 1668 | return ABIArgInfo::getExtend(); |
| 1669 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1670 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1671 | if (InReg) |
| 1672 | return ABIArgInfo::getDirectInReg(); |
| 1673 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1676 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1677 | bool &UsedInAlloca) const { |
| 1678 | // Vectorcall only allows the first 6 parameters to be passed in registers, |
| 1679 | // and homogeneous vector aggregates are only put into registers as a second |
| 1680 | // priority. |
| 1681 | unsigned Count = 0; |
| 1682 | CCState ZeroState = State; |
| 1683 | ZeroState.FreeRegs = ZeroState.FreeSSERegs = 0; |
| 1684 | // HVAs must be done as a second priority for registers, so the deferred |
| 1685 | // items are dealt with by going through the pattern a second time. |
| 1686 | for (auto &I : FI.arguments()) { |
| 1687 | if (Count < VectorcallMaxParamNumAsReg) |
| 1688 | I.info = classifyArgumentType(I.type, State); |
| 1689 | else |
| 1690 | // Parameters after the 6th cannot be passed in registers, |
| 1691 | // so pretend there are no registers left for them. |
| 1692 | I.info = classifyArgumentType(I.type, ZeroState); |
| 1693 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1694 | ++Count; |
| 1695 | } |
| 1696 | Count = 0; |
| 1697 | // Go through the arguments a second time to get HVAs registers if there |
| 1698 | // are still some available. |
| 1699 | for (auto &I : FI.arguments()) { |
| 1700 | if (Count < VectorcallMaxParamNumAsReg) |
| 1701 | I.info = reclassifyHvaArgType(I.type, State, I.info); |
| 1702 | ++Count; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1706 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1707 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1708 | if (IsMCUABI) |
| 1709 | State.FreeRegs = 3; |
| 1710 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1711 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1712 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1713 | State.FreeRegs = 2; |
| 1714 | State.FreeSSERegs = 6; |
| 1715 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1716 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1717 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1718 | State.FreeRegs = 5; |
| 1719 | State.FreeSSERegs = 8; |
| 1720 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1721 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1722 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1723 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1724 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1725 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1726 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1727 | // return value was sret and put it in a register ourselves if appropriate. |
| 1728 | if (State.FreeRegs) { |
| 1729 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1730 | if (!IsMCUABI) |
| 1731 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1734 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1735 | // The chain argument effectively gives us another free register. |
| 1736 | if (FI.isChainCall()) |
| 1737 | ++State.FreeRegs; |
| 1738 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1739 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1740 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1741 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1742 | } else { |
| 1743 | // If not vectorcall, revert to normal behavior. |
| 1744 | for (auto &I : FI.arguments()) { |
| 1745 | I.info = classifyArgumentType(I.type, State); |
| 1746 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1747 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1751 | // all the memory arguments to use inalloca. |
| 1752 | if (UsedInAlloca) |
| 1753 | rewriteWithInAlloca(FI); |
| 1754 | } |
| 1755 | |
| 1756 | void |
| 1757 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1758 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1759 | QualType Type) const { |
| 1760 | // Arguments are always 4-byte-aligned. |
| 1761 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1762 | |
| 1763 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1764 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1765 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1766 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1767 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1768 | // Insert padding bytes to respect alignment. |
| 1769 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1770 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1771 | if (StackOffset != FieldEnd) { |
| 1772 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1773 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1774 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1775 | FrameFields.push_back(Ty); |
| 1776 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1779 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1780 | // Leave ignored and inreg arguments alone. |
| 1781 | switch (Info.getKind()) { |
| 1782 | case ABIArgInfo::InAlloca: |
| 1783 | return true; |
| 1784 | case ABIArgInfo::Indirect: |
| 1785 | assert(Info.getIndirectByVal()); |
| 1786 | return true; |
| 1787 | case ABIArgInfo::Ignore: |
| 1788 | return false; |
| 1789 | case ABIArgInfo::Direct: |
| 1790 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1791 | if (Info.getInReg()) |
| 1792 | return false; |
| 1793 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1794 | case ABIArgInfo::Expand: |
| 1795 | case ABIArgInfo::CoerceAndExpand: |
| 1796 | // These are aggregate types which are never passed in registers when |
| 1797 | // inalloca is involved. |
| 1798 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1799 | } |
| 1800 | llvm_unreachable("invalid enum"); |
| 1801 | } |
| 1802 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1803 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1804 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1805 | |
| 1806 | // Build a packed struct type for all of the arguments in memory. |
| 1807 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1808 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1809 | // The stack alignment is always 4. |
| 1810 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1811 | |
| 1812 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1813 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1814 | |
| 1815 | // Put 'this' into the struct before 'sret', if necessary. |
| 1816 | bool IsThisCall = |
| 1817 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1818 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1819 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1820 | isArgInAlloca(I->info)) { |
| 1821 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1822 | ++I; |
| 1823 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1824 | |
| 1825 | // Put the sret parameter into the inalloca struct if it's in memory. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1826 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1827 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1828 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1829 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1830 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1834 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1835 | ++I; |
| 1836 | |
| 1837 | // Put arguments passed in memory into the struct. |
| 1838 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1839 | if (isArgInAlloca(I->info)) |
| 1840 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1844 | /*isPacked=*/true), |
| 1845 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1848 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1849 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1850 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1851 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1852 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1853 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1854 | // |
| 1855 | // Just messing with TypeInfo like this works because we never pass |
| 1856 | // anything indirectly. |
| 1857 | TypeInfo.second = CharUnits::fromQuantity( |
| 1858 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1859 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1860 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1861 | TypeInfo, CharUnits::fromQuantity(4), |
| 1862 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1865 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1866 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1867 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1868 | |
| 1869 | switch (Opts.getStructReturnConvention()) { |
| 1870 | case CodeGenOptions::SRCK_Default: |
| 1871 | break; |
| 1872 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1873 | return false; |
| 1874 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1875 | return true; |
| 1876 | } |
| 1877 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1878 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1879 | return true; |
| 1880 | |
| 1881 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1882 | case llvm::Triple::DragonFly: |
| 1883 | case llvm::Triple::FreeBSD: |
| 1884 | case llvm::Triple::OpenBSD: |
| 1885 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1886 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1887 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1888 | default: |
| 1889 | return false; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1893 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1894 | llvm::GlobalValue *GV, |
| 1895 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1896 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1897 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1898 | // Get the LLVM function. |
| 1899 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1900 | |
| 1901 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1902 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1903 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1904 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1905 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1906 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1907 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1908 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1909 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1913 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1914 | CodeGen::CodeGenFunction &CGF, |
| 1915 | llvm::Value *Address) const { |
| 1916 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1918 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1919 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1920 | // 0-7 are the eight integer registers; the order is different |
| 1921 | // on Darwin (for EH), but the range is the same. |
| 1922 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1923 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1924 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1925 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1926 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1927 | // These have size 16, which is sizeof(long double) on |
| 1928 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1929 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1930 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1931 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1932 | } else { |
| 1933 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1934 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1935 | Builder.CreateAlignedStore( |
| 1936 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1937 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1938 | |
| 1939 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1940 | // These have size 12, which is sizeof(long double) on |
| 1941 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1942 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1943 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1944 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1945 | |
| 1946 | return false; |
| 1947 | } |
| 1948 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1949 | //===----------------------------------------------------------------------===// |
| 1950 | // X86-64 ABI Implementation |
| 1951 | //===----------------------------------------------------------------------===// |
| 1952 | |
| 1953 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1954 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1955 | /// The AVX ABI level for X86 targets. |
| 1956 | enum class X86AVXABILevel { |
| 1957 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1958 | AVX, |
| 1959 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1960 | }; |
| 1961 | |
| 1962 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1963 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1964 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1965 | case X86AVXABILevel::AVX512: |
| 1966 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1967 | case X86AVXABILevel::AVX: |
| 1968 | return 256; |
| 1969 | case X86AVXABILevel::None: |
| 1970 | return 128; |
| 1971 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1972 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1975 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1976 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1977 | enum Class { |
| 1978 | Integer = 0, |
| 1979 | SSE, |
| 1980 | SSEUp, |
| 1981 | X87, |
| 1982 | X87Up, |
| 1983 | ComplexX87, |
| 1984 | NoClass, |
| 1985 | Memory |
| 1986 | }; |
| 1987 | |
| 1988 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1989 | /// |
| 1990 | /// Merge an accumulating classification \arg Accum with a field |
| 1991 | /// classification \arg Field. |
| 1992 | /// |
| 1993 | /// \param Accum - The accumulating classification. This should |
| 1994 | /// always be either NoClass or the result of a previous merge |
| 1995 | /// call. In addition, this should never be Memory (the caller |
| 1996 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1997 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1998 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1999 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2000 | /// |
| 2001 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2002 | /// final MEMORY or SSE classes when necessary. |
| 2003 | /// |
| 2004 | /// \param AggregateSize - The size of the current aggregate in |
| 2005 | /// the classification process. |
| 2006 | /// |
| 2007 | /// \param Lo - The classification for the parts of the type |
| 2008 | /// residing in the low word of the containing object. |
| 2009 | /// |
| 2010 | /// \param Hi - The classification for the parts of the type |
| 2011 | /// residing in the higher words of the containing object. |
| 2012 | /// |
| 2013 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2014 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2015 | /// classify - Determine the x86_64 register classes in which the |
| 2016 | /// given type T should be passed. |
| 2017 | /// |
| 2018 | /// \param Lo - The classification for the parts of the type |
| 2019 | /// residing in the low word of the containing object. |
| 2020 | /// |
| 2021 | /// \param Hi - The classification for the parts of the type |
| 2022 | /// residing in the high word of the containing object. |
| 2023 | /// |
| 2024 | /// \param OffsetBase - The bit offset of this type in the |
| 2025 | /// containing object. Some parameters are classified different |
| 2026 | /// depending on whether they straddle an eightbyte boundary. |
| 2027 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2028 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2029 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2030 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2031 | /// If a word is unused its result will be NoClass; if a type should |
| 2032 | /// be passed in Memory then at least the classification of \arg Lo |
| 2033 | /// will be Memory. |
| 2034 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2035 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2036 | /// |
| 2037 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2038 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2039 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2040 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2041 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2042 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2043 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2044 | unsigned IROffset, QualType SourceTy, |
| 2045 | unsigned SourceOffset) const; |
| 2046 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2047 | unsigned IROffset, QualType SourceTy, |
| 2048 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2049 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2050 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2051 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2052 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2053 | |
| 2054 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2055 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2056 | /// |
| 2057 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2058 | /// available. |
| 2059 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2061 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2062 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2063 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2064 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2065 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2066 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2067 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2068 | unsigned &NeededSSE) const; |
| 2069 | |
| 2070 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2071 | unsigned &NeededSSE) const; |
| 2072 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2073 | bool IsIllegalVectorType(QualType Ty) const; |
| 2074 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2075 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2076 | /// unfortunately in ways that were not always consistent with |
| 2077 | /// certain previous compilers. In particular, platforms which |
| 2078 | /// required strict binary compatibility with older versions of GCC |
| 2079 | /// may need to exempt themselves. |
| 2080 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2081 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2084 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 2085 | // compilers require us to classify it as INTEGER. |
| 2086 | bool classifyIntegerMMXAsSSE() const { |
| 2087 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2088 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2089 | return false; |
| 2090 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2091 | return false; |
| 2092 | return true; |
| 2093 | } |
| 2094 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2095 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2096 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2097 | // 64-bit hardware. |
| 2098 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2099 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2100 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2101 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2102 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2103 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2104 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2105 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2106 | bool isPassedUsingAVXType(QualType type) const { |
| 2107 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2108 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2109 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2110 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2111 | if (info.isDirect()) { |
| 2112 | llvm::Type *ty = info.getCoerceToType(); |
| 2113 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2114 | return (vectorTy->getBitWidth() > 128); |
| 2115 | } |
| 2116 | return false; |
| 2117 | } |
| 2118 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2119 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2120 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2121 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2122 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2123 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2124 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2125 | |
| 2126 | bool has64BitPointers() const { |
| 2127 | return Has64BitPointers; |
| 2128 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2129 | |
| 2130 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2131 | ArrayRef<llvm::Type*> scalars, |
| 2132 | bool asReturnValue) const override { |
| 2133 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2134 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2135 | bool isSwiftErrorInRegister() const override { |
| 2136 | return true; |
| 2137 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2138 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2139 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2140 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2141 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2142 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2143 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2144 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2145 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2146 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2147 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2148 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2149 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2150 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2151 | |
| 2152 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2153 | // FIXME: Assumes vectorcall is in use. |
| 2154 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2155 | } |
| 2156 | |
| 2157 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2158 | uint64_t NumMembers) const override { |
| 2159 | // FIXME: Assumes vectorcall is in use. |
| 2160 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2161 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2162 | |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2163 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2164 | ArrayRef<llvm::Type *> scalars, |
| 2165 | bool asReturnValue) const override { |
| 2166 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2167 | } |
| 2168 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2169 | bool isSwiftErrorInRegister() const override { |
| 2170 | return true; |
| 2171 | } |
| 2172 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2173 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2174 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2175 | bool IsVectorCall, bool IsRegCall) const; |
| 2176 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2177 | const ABIArgInfo ¤t) const; |
| 2178 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2179 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2180 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2181 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2182 | }; |
| 2183 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2184 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2185 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2186 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2187 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2188 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2189 | const X86_64ABIInfo &getABIInfo() const { |
| 2190 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2191 | } |
| 2192 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2193 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2194 | return 7; |
| 2195 | } |
| 2196 | |
| 2197 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2198 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2199 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2200 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2201 | // 0-15 are the 16 integer registers. |
| 2202 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2203 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2204 | return false; |
| 2205 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2206 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2207 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2208 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2209 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2210 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2211 | } |
| 2212 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2213 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2214 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2215 | // The default CC on x86-64 sets %al to the number of SSA |
| 2216 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2217 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2218 | // that when AVX types are involved: the ABI explicitly states it is |
| 2219 | // undefined, and it doesn't work in practice because of how the ABI |
| 2220 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2221 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2222 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2223 | for (CallArgList::const_iterator |
| 2224 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2225 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2226 | HasAVXType = true; |
| 2227 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2230 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2231 | if (!HasAVXType) |
| 2232 | return true; |
| 2233 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2234 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2235 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2238 | llvm::Constant * |
| 2239 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2240 | unsigned Sig; |
| 2241 | if (getABIInfo().has64BitPointers()) |
| 2242 | Sig = (0xeb << 0) | // jmp rel8 |
| 2243 | (0x0a << 8) | // .+0x0c |
| 2244 | ('F' << 16) | |
| 2245 | ('T' << 24); |
| 2246 | else |
| 2247 | Sig = (0xeb << 0) | // jmp rel8 |
| 2248 | (0x06 << 8) | // .+0x08 |
| 2249 | ('F' << 16) | |
| 2250 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2251 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2252 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2253 | |
| 2254 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2255 | CodeGen::CodeGenModule &CGM) const override { |
| 2256 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2257 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2258 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2259 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2260 | } |
| 2261 | } |
| 2262 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2263 | }; |
| 2264 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2265 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2266 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2267 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2268 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2269 | |
| 2270 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2271 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2272 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2273 | // If the argument contains a space, enclose it in quotes. |
| 2274 | if (Lib.find(" ") != StringRef::npos) |
| 2275 | Opt += "\"" + Lib.str() + "\""; |
| 2276 | else |
| 2277 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2278 | } |
| 2279 | }; |
| 2280 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2281 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2282 | // If the argument does not end in .lib, automatically add the suffix. |
| 2283 | // If the argument contains a space, enclose it in quotes. |
| 2284 | // This matches the behavior of MSVC. |
| 2285 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2286 | std::string ArgStr = Quote ? "\"" : ""; |
| 2287 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2288 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2289 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2290 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2291 | return ArgStr; |
| 2292 | } |
| 2293 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2294 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2295 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2296 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2297 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2298 | unsigned NumRegisterParameters) |
| 2299 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2300 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2301 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2302 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2303 | CodeGen::CodeGenModule &CGM) const override; |
| 2304 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2305 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2306 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2307 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2308 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2309 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2310 | |
| 2311 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2312 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2313 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2314 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2315 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2316 | }; |
| 2317 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2318 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2319 | llvm::GlobalValue *GV, |
| 2320 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2321 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2322 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2323 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2324 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2325 | Fn->addFnAttr("stack-probe-size", |
| 2326 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2331 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2332 | llvm::GlobalValue *GV, |
| 2333 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2334 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2335 | |
| 2336 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2337 | } |
| 2338 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2339 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2340 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2341 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2342 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2343 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2344 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2345 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2346 | CodeGen::CodeGenModule &CGM) const override; |
| 2347 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2348 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2349 | return 7; |
| 2350 | } |
| 2351 | |
| 2352 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2353 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2354 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2355 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2356 | // 0-15 are the 16 integer registers. |
| 2357 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2358 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2359 | return false; |
| 2360 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2361 | |
| 2362 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2363 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2364 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2365 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2366 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2367 | |
| 2368 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2369 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2370 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2371 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2372 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2373 | }; |
| 2374 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2375 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2376 | llvm::GlobalValue *GV, |
| 2377 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2378 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2379 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2380 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2381 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2382 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2383 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2384 | } |
| 2385 | } |
| 2386 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2387 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2388 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2389 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2390 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2391 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2392 | Class &Hi) const { |
| 2393 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2394 | // |
| 2395 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2396 | // memory. |
| 2397 | // |
| 2398 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2399 | // memory. |
| 2400 | // |
| 2401 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2402 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2403 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2404 | // ABI working for processors that don't support the __m256 type. |
| 2405 | // |
| 2406 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2407 | // |
| 2408 | // Some of these are enforced by the merging logic. Others can arise |
| 2409 | // only with unions; for example: |
| 2410 | // union { _Complex double; unsigned; } |
| 2411 | // |
| 2412 | // Note that clauses (b) and (c) were added in 0.98. |
| 2413 | // |
| 2414 | if (Hi == Memory) |
| 2415 | Lo = Memory; |
| 2416 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2417 | Lo = Memory; |
| 2418 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2419 | Lo = Memory; |
| 2420 | if (Hi == SSEUp && Lo != SSE) |
| 2421 | Hi = SSE; |
| 2422 | } |
| 2423 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2424 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2425 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2426 | // classified recursively so that always two fields are |
| 2427 | // considered. The resulting class is calculated according to |
| 2428 | // the classes of the fields in the eightbyte: |
| 2429 | // |
| 2430 | // (a) If both classes are equal, this is the resulting class. |
| 2431 | // |
| 2432 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2433 | // the other class. |
| 2434 | // |
| 2435 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2436 | // class. |
| 2437 | // |
| 2438 | // (d) If one of the classes is INTEGER, the result is the |
| 2439 | // INTEGER. |
| 2440 | // |
| 2441 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2442 | // MEMORY is used as class. |
| 2443 | // |
| 2444 | // (f) Otherwise class SSE is used. |
| 2445 | |
| 2446 | // Accum should never be memory (we should have returned) or |
| 2447 | // ComplexX87 (because this cannot be passed in a structure). |
| 2448 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2449 | "Invalid accumulated classification during merge."); |
| 2450 | if (Accum == Field || Field == NoClass) |
| 2451 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2452 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2453 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2454 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2455 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2456 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2457 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2458 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2459 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2460 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2461 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2464 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2465 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2466 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2467 | // Class pairs with appropriate constructor methods for the various |
| 2468 | // situations. |
| 2469 | |
| 2470 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2471 | // shouldn't be passed in registers for example, so there is no chance they |
| 2472 | // can straddle an eightbyte. Verify & simplify. |
| 2473 | |
| 2474 | Lo = Hi = NoClass; |
| 2475 | |
| 2476 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2477 | Current = Memory; |
| 2478 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2479 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2480 | BuiltinType::Kind k = BT->getKind(); |
| 2481 | |
| 2482 | if (k == BuiltinType::Void) { |
| 2483 | Current = NoClass; |
| 2484 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2485 | Lo = Integer; |
| 2486 | Hi = Integer; |
| 2487 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2488 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2489 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2490 | Current = SSE; |
| 2491 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2492 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2493 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2494 | Lo = SSE; |
| 2495 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2496 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2497 | Lo = X87; |
| 2498 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2499 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2500 | Current = SSE; |
| 2501 | } else |
| 2502 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2503 | } |
| 2504 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2505 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2506 | return; |
| 2507 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2508 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2509 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2510 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2511 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2512 | return; |
| 2513 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2514 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2515 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2516 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2517 | return; |
| 2518 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2520 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2521 | if (Ty->isMemberFunctionPointerType()) { |
| 2522 | if (Has64BitPointers) { |
| 2523 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2524 | // Lo and Hi now. |
| 2525 | Lo = Hi = Integer; |
| 2526 | } else { |
| 2527 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2528 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2529 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2530 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2531 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2532 | Lo = Hi = Integer; |
| 2533 | } else { |
| 2534 | Current = Integer; |
| 2535 | } |
| 2536 | } |
| 2537 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2538 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2539 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2543 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2544 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2545 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2546 | // gcc passes the following as integer: |
| 2547 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2548 | // 2 bytes - <2 x char>, <1 x short> |
| 2549 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2550 | Current = Integer; |
| 2551 | |
| 2552 | // If this type crosses an eightbyte boundary, it should be |
| 2553 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2554 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2555 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2556 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2557 | Hi = Lo; |
| 2558 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2559 | QualType ElementType = VT->getElementType(); |
| 2560 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2561 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2562 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2563 | return; |
| 2564 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2565 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2566 | // pass them as integer. For platforms where clang is the de facto |
| 2567 | // platform compiler, we must continue to use integer. |
| 2568 | if (!classifyIntegerMMXAsSSE() && |
| 2569 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2570 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2571 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2572 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2573 | Current = Integer; |
| 2574 | else |
| 2575 | Current = SSE; |
| 2576 | |
| 2577 | // If this type crosses an eightbyte boundary, it should be |
| 2578 | // split. |
| 2579 | if (OffsetBase && OffsetBase != 64) |
| 2580 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2581 | } else if (Size == 128 || |
| 2582 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2583 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2584 | // least significant one belongs to class SSE and all the others to class |
| 2585 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2586 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2587 | // This design isn't correct for 256-bits, but since there're no cases |
| 2588 | // where the upper parts would need to be inspected, avoid adding |
| 2589 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2590 | // |
| 2591 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2592 | // registers if they are "named", i.e. not part of the "..." of a |
| 2593 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2594 | // |
| 2595 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2596 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | Lo = SSE; |
| 2598 | Hi = SSEUp; |
| 2599 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2600 | return; |
| 2601 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2602 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2603 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2604 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2606 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2607 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2608 | if (Size <= 64) |
| 2609 | Current = Integer; |
| 2610 | else if (Size <= 128) |
| 2611 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2612 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2613 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2614 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2615 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2616 | } else if (ET == getContext().LongDoubleTy) { |
| 2617 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2618 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2619 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2620 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2621 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2622 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2623 | Lo = Hi = SSE; |
| 2624 | else |
| 2625 | llvm_unreachable("unexpected long double representation!"); |
| 2626 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | |
| 2628 | // If this complex type crosses an eightbyte boundary then it |
| 2629 | // should be split. |
| 2630 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2631 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2632 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2633 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2634 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2638 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2639 | // Arrays are treated like structures. |
| 2640 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2641 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | |
| 2643 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2644 | // than eight eightbytes, ..., it has class MEMORY. |
| 2645 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | return; |
| 2647 | |
| 2648 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2649 | // fields, it has class MEMORY. |
| 2650 | // |
| 2651 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2652 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2653 | return; |
| 2654 | |
| 2655 | // Otherwise implement simplified merge. We could be smarter about |
| 2656 | // this, but it isn't worth it and would be harder to verify. |
| 2657 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2658 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2659 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2660 | |
| 2661 | // The only case a 256-bit wide vector could be used is when the array |
| 2662 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2663 | // to work for sizes wider than 128, early check and fallback to memory. |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2664 | // |
| 2665 | if (Size > 128 && |
| 2666 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2667 | return; |
| 2668 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2669 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2670 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2671 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2672 | Lo = merge(Lo, FieldLo); |
| 2673 | Hi = merge(Hi, FieldHi); |
| 2674 | if (Lo == Memory || Hi == Memory) |
| 2675 | break; |
| 2676 | } |
| 2677 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2678 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2679 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2683 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2684 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2685 | |
| 2686 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2687 | // than eight eightbytes, ..., it has class MEMORY. |
| 2688 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2691 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2692 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2693 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2694 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2695 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2696 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2697 | const RecordDecl *RD = RT->getDecl(); |
| 2698 | |
| 2699 | // Assume variable sized types are passed in memory. |
| 2700 | if (RD->hasFlexibleArrayMember()) |
| 2701 | return; |
| 2702 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2703 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | |
| 2705 | // Reset Lo class, this will be recomputed. |
| 2706 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2707 | |
| 2708 | // If this is a C++ record, classify the bases first. |
| 2709 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2710 | for (const auto &I : CXXRD->bases()) { |
| 2711 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2712 | "Unexpected base class!"); |
| 2713 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2714 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Classify this field. |
| 2717 | // |
| 2718 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2719 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2720 | // initialized to class NO_CLASS. |
| 2721 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2722 | uint64_t Offset = |
| 2723 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2724 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2725 | Lo = merge(Lo, FieldLo); |
| 2726 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2727 | if (Lo == Memory || Hi == Memory) { |
| 2728 | postMerge(Size, Lo, Hi); |
| 2729 | return; |
| 2730 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2735 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2736 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2737 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2739 | bool BitField = i->isBitField(); |
| 2740 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2741 | // Ignore padding bit-fields. |
| 2742 | if (BitField && i->isUnnamedBitfield()) |
| 2743 | continue; |
| 2744 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2745 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2746 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2747 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2748 | // The only case a 256-bit wide vector could be used is when the struct |
| 2749 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2750 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2751 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2752 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2753 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2754 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2755 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2756 | return; |
| 2757 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2758 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2759 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2760 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2761 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | } |
| 2764 | |
| 2765 | // Classify this field. |
| 2766 | // |
| 2767 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2768 | // exceeds a single eightbyte, each is classified |
| 2769 | // separately. Each eightbyte gets initialized to class |
| 2770 | // NO_CLASS. |
| 2771 | Class FieldLo, FieldHi; |
| 2772 | |
| 2773 | // Bit-fields require special handling, they do not force the |
| 2774 | // structure to be passed in memory even if unaligned, and |
| 2775 | // therefore they can straddle an eightbyte. |
| 2776 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2777 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2778 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2779 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2780 | |
| 2781 | uint64_t EB_Lo = Offset / 64; |
| 2782 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2783 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2784 | if (EB_Lo) { |
| 2785 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2786 | FieldLo = NoClass; |
| 2787 | FieldHi = Integer; |
| 2788 | } else { |
| 2789 | FieldLo = Integer; |
| 2790 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2791 | } |
| 2792 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2793 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2794 | Lo = merge(Lo, FieldLo); |
| 2795 | Hi = merge(Hi, FieldHi); |
| 2796 | if (Lo == Memory || Hi == Memory) |
| 2797 | break; |
| 2798 | } |
| 2799 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2800 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2801 | } |
| 2802 | } |
| 2803 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2804 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2805 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2806 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2807 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2808 | // Treat an enum type as its underlying type. |
| 2809 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2810 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2811 | |
| 2812 | return (Ty->isPromotableIntegerType() ? |
| 2813 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2814 | } |
| 2815 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2816 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2819 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2820 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2821 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2822 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2823 | if (Size <= 64 || Size > LargestVector) |
| 2824 | return true; |
| 2825 | } |
| 2826 | |
| 2827 | return false; |
| 2828 | } |
| 2829 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2830 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2831 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2832 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2833 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2834 | // |
| 2835 | // This assumption is optimistic, as there could be free registers available |
| 2836 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2837 | // the argument in the free register. This does not seem to happen currently, |
| 2838 | // but this code would be much safer if we could mark the argument with |
| 2839 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2840 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2841 | // Treat an enum type as its underlying type. |
| 2842 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2843 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2844 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2845 | return (Ty->isPromotableIntegerType() ? |
| 2846 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2847 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2848 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2849 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2850 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2851 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2852 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2853 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2854 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2857 | // is important for good codegen. |
| 2858 | // |
| 2859 | // We do this by coercing the value into a scalar type which the backend can |
| 2860 | // handle naturally (i.e., without using byval). |
| 2861 | // |
| 2862 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2863 | // free integer registers. Doing this when there are free integer registers |
| 2864 | // would require more care, as we would have to ensure that the coerced value |
| 2865 | // did not claim the unused register. That would require either reording the |
| 2866 | // arguments to the function (so that any subsequent inreg values came first), |
| 2867 | // or only doing this optimization when there were no following arguments that |
| 2868 | // might be inreg. |
| 2869 | // |
| 2870 | // We currently expect it to be rare (particularly in well written code) for |
| 2871 | // arguments to be passed on the stack when there are still free integer |
| 2872 | // registers available (this would typically imply large structs being passed |
| 2873 | // by value), so this seems like a fair tradeoff for now. |
| 2874 | // |
| 2875 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2876 | // attributes. See PR12193. |
| 2877 | if (freeIntRegs == 0) { |
| 2878 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2879 | |
| 2880 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2881 | // type, which will end up on the stack (with alignment 8). |
| 2882 | if (Align == 8 && Size <= 64) |
| 2883 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2884 | Size)); |
| 2885 | } |
| 2886 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2887 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2890 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2891 | /// register. Pick an LLVM IR type that will be passed as a vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2892 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2893 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2894 | // vectors; strip them off if present. |
| 2895 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2896 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2897 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2898 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2899 | if (isa<llvm::VectorType>(IRType) || |
| 2900 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2901 | return IRType; |
| 2902 | |
| 2903 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2904 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2905 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2906 | |
| 2907 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2908 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2909 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2912 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2913 | /// is known to either be off the end of the specified type or being in |
| 2914 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2915 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2916 | /// classification that put one of the two halves in the INTEGER class. |
| 2917 | /// |
| 2918 | /// It is conservatively correct to return false. |
| 2919 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2920 | unsigned EndBit, ASTContext &Context) { |
| 2921 | // If the bytes being queried are off the end of the type, there is no user |
| 2922 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2923 | // types that don't contain interesting padding. |
| 2924 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2925 | if (TySize <= StartBit) |
| 2926 | return true; |
| 2927 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2928 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2929 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2930 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2931 | |
| 2932 | // Check each element to see if the element overlaps with the queried range. |
| 2933 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2934 | // If the element is after the span we care about, then we're done.. |
| 2935 | unsigned EltOffset = i*EltSize; |
| 2936 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2938 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2939 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2940 | EndBit-EltOffset, Context)) |
| 2941 | return false; |
| 2942 | } |
| 2943 | // If it overlaps no elements, then it is safe to process as padding. |
| 2944 | return true; |
| 2945 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2946 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2947 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2948 | const RecordDecl *RD = RT->getDecl(); |
| 2949 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2950 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2951 | // If this is a C++ record, check the bases first. |
| 2952 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2953 | for (const auto &I : CXXRD->bases()) { |
| 2954 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2955 | "Unexpected base class!"); |
| 2956 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2957 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2958 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2959 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2960 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2961 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2963 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2964 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2965 | EndBit-BaseOffset, Context)) |
| 2966 | return false; |
| 2967 | } |
| 2968 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2970 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2971 | // this could be sped up a lot by being smarter about queried fields, |
| 2972 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2973 | // much. |
| 2974 | unsigned idx = 0; |
| 2975 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2976 | i != e; ++i, ++idx) { |
| 2977 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2978 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2979 | // If we found a field after the region we care about, then we're done. |
| 2980 | if (FieldOffset >= EndBit) break; |
| 2981 | |
| 2982 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2983 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2984 | Context)) |
| 2985 | return false; |
| 2986 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2988 | // If nothing in this record overlapped the area of interest, then we're |
| 2989 | // clean. |
| 2990 | return true; |
| 2991 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2992 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2993 | return false; |
| 2994 | } |
| 2995 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2996 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2997 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2998 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2999 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3000 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3001 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3002 | // Base case if we find a float. |
| 3003 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3004 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3005 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3006 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3007 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3008 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3009 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3010 | IROffset -= SL->getElementOffset(Elt); |
| 3011 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3012 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3013 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3014 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3015 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3016 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3017 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3018 | IROffset -= IROffset/EltSize*EltSize; |
| 3019 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3020 | } |
| 3021 | |
| 3022 | return false; |
| 3023 | } |
| 3024 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3025 | |
| 3026 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3027 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3028 | llvm::Type *X86_64ABIInfo:: |
| 3029 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3030 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3031 | // 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] | 3032 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3033 | // structs that contain 3 floats. |
| 3034 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3035 | SourceOffset*8+64, getContext())) |
| 3036 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3038 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3039 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3040 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3041 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3042 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3043 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3044 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3045 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3046 | } |
| 3047 | |
| 3048 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3049 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3050 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3051 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3052 | /// 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] | 3053 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3054 | /// etc). |
| 3055 | /// |
| 3056 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3057 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3058 | /// the 8-byte value references. PrefType may be null. |
| 3059 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3060 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3061 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3062 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3063 | llvm::Type *X86_64ABIInfo:: |
| 3064 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3065 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3066 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3067 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3068 | if (IROffset == 0) { |
| 3069 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3070 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3071 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3072 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3074 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3075 | // goodness in the source type is just tail padding. This is allowed to |
| 3076 | // kick in for struct {double,int} on the int, but not on |
| 3077 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3078 | // have to do this analysis on the source type because we can't depend on |
| 3079 | // unions being lowered a specific way etc. |
| 3080 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3081 | IRType->isIntegerTy(32) || |
| 3082 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3083 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3084 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3085 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3086 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3087 | SourceOffset*8+64, getContext())) |
| 3088 | return IRType; |
| 3089 | } |
| 3090 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3092 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3093 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3094 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3095 | if (IROffset < SL->getSizeInBytes()) { |
| 3096 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3097 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3098 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3099 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3100 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3101 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3102 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3103 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3104 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3105 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3106 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3107 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3108 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3109 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3110 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3112 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3113 | // 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] | 3114 | unsigned TySizeInBytes = |
| 3115 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3116 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3117 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3118 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3119 | // It is always safe to classify this as an integer type up to i64 that |
| 3120 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3121 | return llvm::IntegerType::get(getVMContext(), |
| 3122 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3125 | |
| 3126 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3127 | /// be used as elements of a two register pair to pass or return, return a |
| 3128 | /// first class aggregate to represent them. For example, if the low part of |
| 3129 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3130 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3131 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3132 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3133 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3134 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3135 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3136 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3137 | // the second element at offset 8. Check for this: |
| 3138 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3139 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3140 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3141 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3143 | // To handle this, we have to increase the size of the low part so that the |
| 3144 | // second element will start at an 8 byte offset. We can't increase the size |
| 3145 | // of the second element because it might make us access off the end of the |
| 3146 | // struct. |
| 3147 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3148 | // There are usually two sorts of types the ABI generation code can produce |
| 3149 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3150 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3151 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3152 | // Promote these to a larger type. |
| 3153 | if (Lo->isFloatTy()) |
| 3154 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3155 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3156 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3157 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3158 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3159 | } |
| 3160 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3161 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3162 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3164 | // Verify that the second element is at an 8-byte offset. |
| 3165 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3166 | "Invalid x86-64 argument pair!"); |
| 3167 | return Result; |
| 3168 | } |
| 3169 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3170 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3171 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3172 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3173 | // classification algorithm. |
| 3174 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3175 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3176 | |
| 3177 | // Check some invariants. |
| 3178 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3179 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3180 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3181 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3182 | switch (Lo) { |
| 3183 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3184 | if (Hi == NoClass) |
| 3185 | return ABIArgInfo::getIgnore(); |
| 3186 | // If the low part is just padding, it takes no register, leave ResType |
| 3187 | // null. |
| 3188 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3189 | "Unknown missing lo part"); |
| 3190 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3191 | |
| 3192 | case SSEUp: |
| 3193 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3194 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3195 | |
| 3196 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3197 | // hidden argument. |
| 3198 | case Memory: |
| 3199 | return getIndirectReturnResult(RetTy); |
| 3200 | |
| 3201 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3202 | // available register of the sequence %rax, %rdx is used. |
| 3203 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3204 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3206 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3207 | // so that the parameter gets the right LLVM IR attributes. |
| 3208 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3209 | // Treat an enum type as its underlying type. |
| 3210 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3211 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3212 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3213 | if (RetTy->isIntegralOrEnumerationType() && |
| 3214 | RetTy->isPromotableIntegerType()) |
| 3215 | return ABIArgInfo::getExtend(); |
| 3216 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3217 | break; |
| 3218 | |
| 3219 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3220 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3221 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3222 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3223 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3224 | |
| 3225 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3226 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3227 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3228 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3229 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3230 | |
| 3231 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3232 | // part of the value is returned in %st0 and the imaginary part in |
| 3233 | // %st1. |
| 3234 | case ComplexX87: |
| 3235 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3236 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3237 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3238 | break; |
| 3239 | } |
| 3240 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3241 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3242 | switch (Hi) { |
| 3243 | // Memory was handled previously and X87 should |
| 3244 | // never occur as a hi class. |
| 3245 | case Memory: |
| 3246 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3247 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3248 | |
| 3249 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3250 | case NoClass: |
| 3251 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3252 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3253 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3254 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3255 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3256 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3257 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3258 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3259 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3260 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3261 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3262 | break; |
| 3263 | |
| 3264 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3265 | // is passed in the next available eightbyte chunk if the last used |
| 3266 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3267 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3268 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3269 | case SSEUp: |
| 3270 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3271 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3272 | break; |
| 3273 | |
| 3274 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3275 | // returned together with the previous X87 value in %st0. |
| 3276 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3277 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3278 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3279 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3280 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3281 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3282 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3283 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3284 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3285 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3286 | break; |
| 3287 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3288 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3289 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3290 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3291 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3292 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3293 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3295 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3298 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3299 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3300 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3301 | const |
| 3302 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3303 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3304 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3305 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3306 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3307 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3308 | // Check some invariants. |
| 3309 | // FIXME: Enforce these by construction. |
| 3310 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3311 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3312 | |
| 3313 | neededInt = 0; |
| 3314 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3315 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3316 | switch (Lo) { |
| 3317 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3318 | if (Hi == NoClass) |
| 3319 | return ABIArgInfo::getIgnore(); |
| 3320 | // If the low part is just padding, it takes no register, leave ResType |
| 3321 | // null. |
| 3322 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3323 | "Unknown missing lo part"); |
| 3324 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3325 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3326 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3327 | // on the stack. |
| 3328 | case Memory: |
| 3329 | |
| 3330 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3331 | // COMPLEX_X87, it is passed in memory. |
| 3332 | case X87: |
| 3333 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3334 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3335 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3336 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3337 | |
| 3338 | case SSEUp: |
| 3339 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3340 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3341 | |
| 3342 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3343 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3344 | // and %r9 is used. |
| 3345 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3346 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3347 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3348 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3349 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3350 | |
| 3351 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3352 | // so that the parameter gets the right LLVM IR attributes. |
| 3353 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3354 | // Treat an enum type as its underlying type. |
| 3355 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3356 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3357 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3358 | if (Ty->isIntegralOrEnumerationType() && |
| 3359 | Ty->isPromotableIntegerType()) |
| 3360 | return ABIArgInfo::getExtend(); |
| 3361 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3362 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3363 | break; |
| 3364 | |
| 3365 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3366 | // available SSE register is used, the registers are taken in the |
| 3367 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3368 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3369 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3370 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3371 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3372 | break; |
| 3373 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3374 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3375 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3376 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3377 | switch (Hi) { |
| 3378 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3379 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3380 | // which is passed in memory. |
| 3381 | case Memory: |
| 3382 | case X87: |
| 3383 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3384 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3385 | |
| 3386 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3388 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3389 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3390 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3391 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3392 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3393 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3394 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3395 | break; |
| 3396 | |
| 3397 | // X87Up generally doesn't occur here (long double is passed in |
| 3398 | // memory), except in situations involving unions. |
| 3399 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3400 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3401 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3402 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3403 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3404 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3405 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3406 | ++neededSSE; |
| 3407 | break; |
| 3408 | |
| 3409 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3410 | // 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] | 3411 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3412 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3413 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3414 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3415 | break; |
| 3416 | } |
| 3417 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3418 | // If a high part was specified, merge it together with the low part. It is |
| 3419 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3420 | // first class struct aggregate with the high and low part: {low, high} |
| 3421 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3422 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3423 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3424 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3427 | ABIArgInfo |
| 3428 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3429 | unsigned &NeededSSE) const { |
| 3430 | auto RT = Ty->getAs<RecordType>(); |
| 3431 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3432 | |
| 3433 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3434 | return getIndirectReturnResult(Ty); |
| 3435 | |
| 3436 | // Sum up bases |
| 3437 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3438 | if (CXXRD->isDynamicClass()) { |
| 3439 | NeededInt = NeededSSE = 0; |
| 3440 | return getIndirectReturnResult(Ty); |
| 3441 | } |
| 3442 | |
| 3443 | for (const auto &I : CXXRD->bases()) |
| 3444 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3445 | .isIndirect()) { |
| 3446 | NeededInt = NeededSSE = 0; |
| 3447 | return getIndirectReturnResult(Ty); |
| 3448 | } |
| 3449 | } |
| 3450 | |
| 3451 | // Sum up members |
| 3452 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3453 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3454 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3455 | .isIndirect()) { |
| 3456 | NeededInt = NeededSSE = 0; |
| 3457 | return getIndirectReturnResult(Ty); |
| 3458 | } |
| 3459 | } else { |
| 3460 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3461 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3462 | LocalNeededSSE, true) |
| 3463 | .isIndirect()) { |
| 3464 | NeededInt = NeededSSE = 0; |
| 3465 | return getIndirectReturnResult(Ty); |
| 3466 | } |
| 3467 | NeededInt += LocalNeededInt; |
| 3468 | NeededSSE += LocalNeededSSE; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | return ABIArgInfo::getDirect(); |
| 3473 | } |
| 3474 | |
| 3475 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3476 | unsigned &NeededInt, |
| 3477 | unsigned &NeededSSE) const { |
| 3478 | |
| 3479 | NeededInt = 0; |
| 3480 | NeededSSE = 0; |
| 3481 | |
| 3482 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3483 | } |
| 3484 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3485 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3486 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3487 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3488 | |
| 3489 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3490 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3491 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3492 | unsigned NeededInt, NeededSSE; |
| 3493 | |
| 3494 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3495 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3496 | FI.getReturnInfo() = |
| 3497 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3498 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3499 | FreeIntRegs -= NeededInt; |
| 3500 | FreeSSERegs -= NeededSSE; |
| 3501 | } else { |
| 3502 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3503 | } |
| 3504 | } else if (!getCXXABI().classifyReturnType(FI)) |
| 3505 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3506 | |
| 3507 | // If the return value is indirect, then the hidden argument is consuming one |
| 3508 | // integer register. |
| 3509 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3510 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3511 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3512 | // The chain argument effectively gives us another free register. |
| 3513 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3514 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3515 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3516 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3517 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3518 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3519 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3520 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3521 | it != ie; ++it, ++ArgNo) { |
| 3522 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3523 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3524 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3525 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3526 | else |
| 3527 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3528 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3529 | |
| 3530 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3531 | // eightbyte of an argument, the whole argument is passed on the |
| 3532 | // stack. If registers have already been assigned for some |
| 3533 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3534 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3535 | FreeIntRegs -= NeededInt; |
| 3536 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3537 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3538 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3543 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3544 | Address VAListAddr, QualType Ty) { |
| 3545 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3546 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3547 | llvm::Value *overflow_arg_area = |
| 3548 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3549 | |
| 3550 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3551 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3552 | // It isn't stated explicitly in the standard, but in practice we use |
| 3553 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3554 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3555 | if (Align > CharUnits::fromQuantity(8)) { |
| 3556 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3557 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3561 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3562 | llvm::Value *Res = |
| 3563 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3564 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3565 | |
| 3566 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3567 | // l->overflow_arg_area + sizeof(type). |
| 3568 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3569 | // an 8 byte boundary. |
| 3570 | |
| 3571 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3572 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3573 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3574 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3575 | "overflow_arg_area.next"); |
| 3576 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3577 | |
| 3578 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3579 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3582 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3583 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3584 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3585 | // struct { |
| 3586 | // i32 gp_offset; |
| 3587 | // i32 fp_offset; |
| 3588 | // i8* overflow_arg_area; |
| 3589 | // i8* reg_save_area; |
| 3590 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3591 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3593 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3594 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3595 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3596 | |
| 3597 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3598 | // in the registers. If not go to step 7. |
| 3599 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3600 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3601 | |
| 3602 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3603 | // general purpose registers needed to pass type and num_fp to hold |
| 3604 | // the number of floating point registers needed. |
| 3605 | |
| 3606 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3607 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3608 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3609 | // |
| 3610 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3611 | // register save space). |
| 3612 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3613 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3614 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3615 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3616 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3617 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3618 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3619 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3620 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3621 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3622 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3626 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3627 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3628 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3629 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3630 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3631 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3632 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3633 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3634 | } |
| 3635 | |
| 3636 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3637 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3638 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3639 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3640 | |
| 3641 | // Emit code to load the value if it was passed in registers. |
| 3642 | |
| 3643 | CGF.EmitBlock(InRegBlock); |
| 3644 | |
| 3645 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3646 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3647 | // copying to a temporary location in case the parameter is passed |
| 3648 | // in different register classes or requires an alignment greater |
| 3649 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3650 | // |
| 3651 | // FIXME: This really results in shameful code when we end up needing to |
| 3652 | // collect arguments from different places; often what should result in a |
| 3653 | // simple assembling of a structure from scattered addresses has many more |
| 3654 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3655 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3656 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3657 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3658 | "reg_save_area"); |
| 3659 | |
| 3660 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3661 | if (neededInt && neededSSE) { |
| 3662 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3663 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3664 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3665 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3666 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3667 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3668 | llvm::Type *TyLo = ST->getElementType(0); |
| 3669 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3670 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3671 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3672 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3673 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3674 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3675 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3676 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3677 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3678 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3679 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3680 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3681 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3682 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3683 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3684 | CGF.Builder.CreateStore(V, |
| 3685 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3686 | |
| 3687 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3688 | V = CGF.Builder.CreateAlignedLoad( |
| 3689 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3690 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3691 | CharUnits Offset = CharUnits::fromQuantity( |
| 3692 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3693 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3694 | |
| 3695 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3696 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3697 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3698 | CharUnits::fromQuantity(8)); |
| 3699 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3700 | |
| 3701 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3702 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3703 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3704 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3705 | CharUnits TyAlign = SizeAlign.second; |
| 3706 | |
| 3707 | // Copy into a temporary if the type is more aligned than the |
| 3708 | // register save area. |
| 3709 | if (TyAlign.getQuantity() > 8) { |
| 3710 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3711 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3712 | RegAddr = Tmp; |
| 3713 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3714 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3715 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3716 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3717 | CharUnits::fromQuantity(16)); |
| 3718 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3719 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3720 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3721 | // SSE registers are spaced 16 bytes apart in the register save |
| 3722 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3723 | // The ABI isn't explicit about this, but it seems reasonable |
| 3724 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3725 | // naturally 16-byte aligned and the prologue is expected to store |
| 3726 | // all the SSE registers to the RSA. |
| 3727 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3728 | CharUnits::fromQuantity(16)); |
| 3729 | Address RegAddrHi = |
| 3730 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3731 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3732 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3733 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3734 | llvm::Value *V; |
| 3735 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3736 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3737 | V = CGF.Builder.CreateLoad( |
| 3738 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3739 | CGF.Builder.CreateStore(V, |
| 3740 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3741 | V = CGF.Builder.CreateLoad( |
| 3742 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3743 | CGF.Builder.CreateStore(V, |
| 3744 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3745 | |
| 3746 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3750 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3751 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3752 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3753 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3754 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3755 | gp_offset_p); |
| 3756 | } |
| 3757 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3758 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3759 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3760 | fp_offset_p); |
| 3761 | } |
| 3762 | CGF.EmitBranch(ContBlock); |
| 3763 | |
| 3764 | // Emit code to load the value if it was passed in memory. |
| 3765 | |
| 3766 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3767 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Return the appropriate result. |
| 3770 | |
| 3771 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3772 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3773 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3774 | return ResAddr; |
| 3775 | } |
| 3776 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3777 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3778 | QualType Ty) const { |
| 3779 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3780 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3781 | CharUnits::fromQuantity(8), |
| 3782 | /*allowHigherAlign*/ false); |
| 3783 | } |
| 3784 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3785 | ABIArgInfo |
| 3786 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3787 | const ABIArgInfo ¤t) const { |
| 3788 | // Assumes vectorCall calling convention. |
| 3789 | const Type *Base = nullptr; |
| 3790 | uint64_t NumElts = 0; |
| 3791 | |
| 3792 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3793 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3794 | FreeSSERegs -= NumElts; |
| 3795 | return getDirectX86Hva(); |
| 3796 | } |
| 3797 | return current; |
| 3798 | } |
| 3799 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3800 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3801 | bool IsReturnType, bool IsVectorCall, |
| 3802 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3803 | |
| 3804 | if (Ty->isVoidType()) |
| 3805 | return ABIArgInfo::getIgnore(); |
| 3806 | |
| 3807 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3808 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3809 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3810 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3811 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3812 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3813 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3814 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3815 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3816 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3817 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3818 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3822 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3823 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3824 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3825 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3826 | const Type *Base = nullptr; |
| 3827 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3828 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3829 | // other targets. |
| 3830 | if ((IsVectorCall || IsRegCall) && |
| 3831 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3832 | if (IsRegCall) { |
| 3833 | if (FreeSSERegs >= NumElts) { |
| 3834 | FreeSSERegs -= NumElts; |
| 3835 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3836 | return ABIArgInfo::getDirect(); |
| 3837 | return ABIArgInfo::getExpand(); |
| 3838 | } |
| 3839 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3840 | } else if (IsVectorCall) { |
| 3841 | if (FreeSSERegs >= NumElts && |
| 3842 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3843 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3844 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3845 | } else if (IsReturnType) { |
| 3846 | return ABIArgInfo::getExpand(); |
| 3847 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3848 | // HVAs are delayed and reclassified in the 2nd step. |
| 3849 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3850 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3851 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3854 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3855 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3856 | // directly. |
| 3857 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3858 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3859 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3862 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3863 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3864 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3865 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3866 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3867 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3868 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3869 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3872 | // Bool type is always extended to the ABI, other builtin types are not |
| 3873 | // extended. |
| 3874 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3875 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3876 | return ABIArgInfo::getExtend(); |
| 3877 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3878 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3879 | // passes them indirectly through memory. |
| 3880 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3881 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3882 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3883 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3884 | } |
| 3885 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3886 | return ABIArgInfo::getDirect(); |
| 3887 | } |
| 3888 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3889 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3890 | unsigned FreeSSERegs, |
| 3891 | bool IsVectorCall, |
| 3892 | bool IsRegCall) const { |
| 3893 | unsigned Count = 0; |
| 3894 | for (auto &I : FI.arguments()) { |
| 3895 | if (Count < VectorcallMaxParamNumAsReg) |
| 3896 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3897 | else { |
| 3898 | // Since these cannot be passed in registers, pretend no registers |
| 3899 | // are left. |
| 3900 | unsigned ZeroSSERegsAvail = 0; |
| 3901 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3902 | IsVectorCall, IsRegCall); |
| 3903 | } |
| 3904 | ++Count; |
| 3905 | } |
| 3906 | |
| 3907 | Count = 0; |
| 3908 | for (auto &I : FI.arguments()) { |
| 3909 | if (Count < VectorcallMaxParamNumAsReg) |
| 3910 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
| 3911 | ++Count; |
| 3912 | } |
| 3913 | } |
| 3914 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3915 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3916 | bool IsVectorCall = |
| 3917 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3918 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3919 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3920 | unsigned FreeSSERegs = 0; |
| 3921 | if (IsVectorCall) { |
| 3922 | // We can use up to 4 SSE return registers with vectorcall. |
| 3923 | FreeSSERegs = 4; |
| 3924 | } else if (IsRegCall) { |
| 3925 | // RegCall gives us 16 SSE registers. |
| 3926 | FreeSSERegs = 16; |
| 3927 | } |
| 3928 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3929 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3930 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3931 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3932 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3933 | if (IsVectorCall) { |
| 3934 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3935 | FreeSSERegs = 6; |
| 3936 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3937 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3938 | FreeSSERegs = 16; |
| 3939 | } |
| 3940 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3941 | if (IsVectorCall) { |
| 3942 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 3943 | } else { |
| 3944 | for (auto &I : FI.arguments()) |
| 3945 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3946 | } |
| 3947 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3950 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3951 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 3952 | |
| 3953 | bool IsIndirect = false; |
| 3954 | |
| 3955 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3956 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 3957 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 3958 | uint64_t Width = getContext().getTypeSize(Ty); |
| 3959 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 3960 | } |
| 3961 | |
| 3962 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3963 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3964 | CharUnits::fromQuantity(8), |
| 3965 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3966 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3967 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3968 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3969 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3970 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3971 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3972 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3973 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3974 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3975 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3976 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3977 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3978 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3979 | }; |
| 3980 | |
| 3981 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3982 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3983 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3984 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3985 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3986 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3987 | // This is recovered from gcc output. |
| 3988 | return 1; // r1 is the dedicated stack pointer |
| 3989 | } |
| 3990 | |
| 3991 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3992 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3993 | }; |
| 3994 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3995 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3996 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3997 | // TODO: this implementation is now likely redundant with |
| 3998 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3999 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4000 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4001 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4002 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4003 | // TODO: Implement this. For now ignore. |
| 4004 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4005 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4008 | // struct __va_list_tag { |
| 4009 | // unsigned char gpr; |
| 4010 | // unsigned char fpr; |
| 4011 | // unsigned short reserved; |
| 4012 | // void *overflow_arg_area; |
| 4013 | // void *reg_save_area; |
| 4014 | // }; |
| 4015 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4016 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4017 | bool isInt = |
| 4018 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4019 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4020 | |
| 4021 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4022 | // with the argument-lowering code. |
| 4023 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4024 | |
| 4025 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4026 | |
| 4027 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4028 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4029 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4030 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4031 | } else { |
| 4032 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4033 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4034 | |
| 4035 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4036 | |
| 4037 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4038 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4039 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4040 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4041 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4042 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4043 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4044 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4045 | |
| 4046 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4047 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4048 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4049 | |
| 4050 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4051 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4052 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4053 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4054 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4055 | // Case 1: consume registers. |
| 4056 | Address RegAddr = Address::invalid(); |
| 4057 | { |
| 4058 | CGF.EmitBlock(UsingRegs); |
| 4059 | |
| 4060 | Address RegSaveAreaPtr = |
| 4061 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4062 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4063 | CharUnits::fromQuantity(8)); |
| 4064 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4065 | |
| 4066 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4067 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4068 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4069 | CharUnits::fromQuantity(32)); |
| 4070 | } |
| 4071 | |
| 4072 | // Get the address of the saved value by scaling the number of |
| 4073 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4074 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4075 | llvm::Value *RegOffset = |
| 4076 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4077 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4078 | RegAddr.getPointer(), RegOffset), |
| 4079 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4080 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4081 | |
| 4082 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4083 | NumRegs = |
| 4084 | Builder.CreateAdd(NumRegs, |
| 4085 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4086 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4087 | |
| 4088 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4089 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4090 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4091 | // Case 2: consume space in the overflow area. |
| 4092 | Address MemAddr = Address::invalid(); |
| 4093 | { |
| 4094 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4095 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4096 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4097 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4098 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4099 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4100 | |
| 4101 | CharUnits Size; |
| 4102 | if (!isIndirect) { |
| 4103 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4104 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4105 | } else { |
| 4106 | Size = CGF.getPointerSize(); |
| 4107 | } |
| 4108 | |
| 4109 | Address OverflowAreaAddr = |
| 4110 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4111 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4112 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4113 | // Round up address of argument to alignment |
| 4114 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4115 | if (Align > OverflowAreaAlign) { |
| 4116 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4117 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4118 | Align); |
| 4119 | } |
| 4120 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4121 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4122 | |
| 4123 | // Increase the overflow area. |
| 4124 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4125 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4126 | CGF.EmitBranch(Cont); |
| 4127 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4128 | |
| 4129 | CGF.EmitBlock(Cont); |
| 4130 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4131 | // Merge the cases with a phi. |
| 4132 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4133 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4134 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4135 | // Load the pointer if the argument was passed indirectly. |
| 4136 | if (isIndirect) { |
| 4137 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4138 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | return Result; |
| 4142 | } |
| 4143 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4144 | bool |
| 4145 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4146 | llvm::Value *Address) const { |
| 4147 | // This is calculated from the LLVM and GCC tables and verified |
| 4148 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4149 | |
| 4150 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4151 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4152 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4153 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4154 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4155 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4156 | |
| 4157 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4158 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4159 | |
| 4160 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4161 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4162 | |
| 4163 | // 64-76 are various 4-byte special-purpose registers: |
| 4164 | // 64: mq |
| 4165 | // 65: lr |
| 4166 | // 66: ctr |
| 4167 | // 67: ap |
| 4168 | // 68-75 cr0-7 |
| 4169 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4170 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4171 | |
| 4172 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4173 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4174 | |
| 4175 | // 109: vrsave |
| 4176 | // 110: vscr |
| 4177 | // 111: spe_acc |
| 4178 | // 112: spefscr |
| 4179 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4180 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4181 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4182 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4185 | // PowerPC-64 |
| 4186 | |
| 4187 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4188 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4189 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4190 | public: |
| 4191 | enum ABIKind { |
| 4192 | ELFv1 = 0, |
| 4193 | ELFv2 |
| 4194 | }; |
| 4195 | |
| 4196 | private: |
| 4197 | static const unsigned GPRBits = 64; |
| 4198 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4199 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4200 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4201 | |
| 4202 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4203 | // will be passed in a QPX register. |
| 4204 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4205 | if (!HasQPX) |
| 4206 | return false; |
| 4207 | |
| 4208 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4209 | unsigned NumElements = VT->getNumElements(); |
| 4210 | if (NumElements == 1) |
| 4211 | return false; |
| 4212 | |
| 4213 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4214 | if (getContext().getTypeSize(Ty) <= 256) |
| 4215 | return true; |
| 4216 | } else if (VT->getElementType()-> |
| 4217 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4218 | if (getContext().getTypeSize(Ty) <= 128) |
| 4219 | return true; |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | return false; |
| 4224 | } |
| 4225 | |
| 4226 | bool IsQPXVectorTy(QualType Ty) const { |
| 4227 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4228 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4229 | |
| 4230 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4231 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4232 | bool SoftFloatABI) |
| 4233 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4234 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4235 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4236 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4237 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4238 | |
| 4239 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4240 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4241 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4242 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4243 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4244 | uint64_t Members) const override; |
| 4245 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4246 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4247 | // Example: For aggregate arguments that fit in a register, we could |
| 4248 | // use getDirectInReg (as is done below for structs containing a single |
| 4249 | // floating-point value) to avoid pushing them to memory on function |
| 4250 | // entry. This would require changing the logic in PPCISelLowering |
| 4251 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4252 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4253 | if (!getCXXABI().classifyReturnType(FI)) |
| 4254 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4255 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4256 | // We rely on the default argument classification for the most part. |
| 4257 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4258 | // or vector item must be passed in a register if one is available. |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4259 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4260 | if (T) { |
| 4261 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4262 | if (IsQPXVectorTy(T) || |
| 4263 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4264 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4265 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4266 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4267 | continue; |
| 4268 | } |
| 4269 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4270 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4271 | } |
| 4272 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4274 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4275 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4276 | }; |
| 4277 | |
| 4278 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4279 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4280 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4281 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4282 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4283 | bool SoftFloatABI) |
| 4284 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4285 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4286 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4287 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4288 | // This is recovered from gcc output. |
| 4289 | return 1; // r1 is the dedicated stack pointer |
| 4290 | } |
| 4291 | |
| 4292 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4293 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4294 | }; |
| 4295 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4296 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4297 | public: |
| 4298 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4299 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4300 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4301 | // This is recovered from gcc output. |
| 4302 | return 1; // r1 is the dedicated stack pointer |
| 4303 | } |
| 4304 | |
| 4305 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4306 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4307 | }; |
| 4308 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4309 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4310 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4311 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4312 | // extended to 64 bits. |
| 4313 | bool |
| 4314 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4315 | // Treat an enum type as its underlying type. |
| 4316 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4317 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4318 | |
| 4319 | // Promotable integer types are required to be promoted by the ABI. |
| 4320 | if (Ty->isPromotableIntegerType()) |
| 4321 | return true; |
| 4322 | |
| 4323 | // In addition to the usual promotable integer types, we also need to |
| 4324 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4325 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4326 | switch (BT->getKind()) { |
| 4327 | case BuiltinType::Int: |
| 4328 | case BuiltinType::UInt: |
| 4329 | return true; |
| 4330 | default: |
| 4331 | break; |
| 4332 | } |
| 4333 | |
| 4334 | return false; |
| 4335 | } |
| 4336 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4337 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4338 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4339 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4340 | // Complex types are passed just like their elements. |
| 4341 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4342 | Ty = CTy->getElementType(); |
| 4343 | |
| 4344 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4345 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4346 | if (IsQPXVectorTy(Ty)) { |
| 4347 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4348 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4349 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4350 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4351 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4352 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4353 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4354 | |
| 4355 | // For single-element float/vector structs, we consider the whole type |
| 4356 | // to have the same alignment requirements as its single element. |
| 4357 | const Type *AlignAsType = nullptr; |
| 4358 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4359 | if (EltType) { |
| 4360 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4361 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4362 | getContext().getTypeSize(EltType) == 128) || |
| 4363 | (BT && BT->isFloatingPoint())) |
| 4364 | AlignAsType = EltType; |
| 4365 | } |
| 4366 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4367 | // Likewise for ELFv2 homogeneous aggregates. |
| 4368 | const Type *Base = nullptr; |
| 4369 | uint64_t Members = 0; |
| 4370 | if (!AlignAsType && Kind == ELFv2 && |
| 4371 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4372 | AlignAsType = Base; |
| 4373 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4374 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4375 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4376 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4377 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4378 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4379 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4380 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4381 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4382 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4383 | |
| 4384 | // Otherwise, we only need alignment for any aggregate type that |
| 4385 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4386 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4387 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4388 | return CharUnits::fromQuantity(32); |
| 4389 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4390 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4391 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4392 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4395 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4396 | /// aggregate. Base is set to the base element type, and Members is set |
| 4397 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4398 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4399 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4400 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4401 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4402 | if (NElements == 0) |
| 4403 | return false; |
| 4404 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4405 | return false; |
| 4406 | Members *= NElements; |
| 4407 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4408 | const RecordDecl *RD = RT->getDecl(); |
| 4409 | if (RD->hasFlexibleArrayMember()) |
| 4410 | return false; |
| 4411 | |
| 4412 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4413 | |
| 4414 | // If this is a C++ record, check the bases first. |
| 4415 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4416 | for (const auto &I : CXXRD->bases()) { |
| 4417 | // Ignore empty records. |
| 4418 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4419 | continue; |
| 4420 | |
| 4421 | uint64_t FldMembers; |
| 4422 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4423 | return false; |
| 4424 | |
| 4425 | Members += FldMembers; |
| 4426 | } |
| 4427 | } |
| 4428 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4429 | for (const auto *FD : RD->fields()) { |
| 4430 | // Ignore (non-zero arrays of) empty records. |
| 4431 | QualType FT = FD->getType(); |
| 4432 | while (const ConstantArrayType *AT = |
| 4433 | getContext().getAsConstantArrayType(FT)) { |
| 4434 | if (AT->getSize().getZExtValue() == 0) |
| 4435 | return false; |
| 4436 | FT = AT->getElementType(); |
| 4437 | } |
| 4438 | if (isEmptyRecord(getContext(), FT, true)) |
| 4439 | continue; |
| 4440 | |
| 4441 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4442 | if (getContext().getLangOpts().CPlusPlus && |
| 4443 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4444 | continue; |
| 4445 | |
| 4446 | uint64_t FldMembers; |
| 4447 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4448 | return false; |
| 4449 | |
| 4450 | Members = (RD->isUnion() ? |
| 4451 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4452 | } |
| 4453 | |
| 4454 | if (!Base) |
| 4455 | return false; |
| 4456 | |
| 4457 | // Ensure there is no padding. |
| 4458 | if (getContext().getTypeSize(Base) * Members != |
| 4459 | getContext().getTypeSize(Ty)) |
| 4460 | return false; |
| 4461 | } else { |
| 4462 | Members = 1; |
| 4463 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4464 | Members = 2; |
| 4465 | Ty = CT->getElementType(); |
| 4466 | } |
| 4467 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4468 | // Most ABIs only support float, double, and some vector type widths. |
| 4469 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4470 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4471 | |
| 4472 | // The base type must be the same for all members. Types that |
| 4473 | // agree in both total size and mode (float vs. vector) are |
| 4474 | // treated as being equivalent here. |
| 4475 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4476 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4477 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4478 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4479 | // so make sure to widen it explicitly. |
| 4480 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4481 | QualType EltTy = VT->getElementType(); |
| 4482 | unsigned NumElements = |
| 4483 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4484 | Base = getContext() |
| 4485 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4486 | .getTypePtr(); |
| 4487 | } |
| 4488 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4489 | |
| 4490 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4491 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4492 | return false; |
| 4493 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4494 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4495 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4496 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4497 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4498 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4499 | // double, long double, or 128-bit vectors. |
| 4500 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4501 | if (BT->getKind() == BuiltinType::Float || |
| 4502 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4503 | BT->getKind() == BuiltinType::LongDouble) { |
| 4504 | if (IsSoftFloatABI) |
| 4505 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4506 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4507 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4508 | } |
| 4509 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4510 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4511 | return true; |
| 4512 | } |
| 4513 | return false; |
| 4514 | } |
| 4515 | |
| 4516 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4517 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4518 | // Vector types require one register, floating point types require one |
| 4519 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4520 | uint32_t NumRegs = |
| 4521 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4522 | |
| 4523 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4524 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4527 | ABIArgInfo |
| 4528 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4529 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4530 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4531 | if (Ty->isAnyComplexType()) |
| 4532 | return ABIArgInfo::getDirect(); |
| 4533 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4534 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4535 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4536 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4537 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4538 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4539 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4540 | else if (Size < 128) { |
| 4541 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4542 | return ABIArgInfo::getDirect(CoerceTy); |
| 4543 | } |
| 4544 | } |
| 4545 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4546 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4547 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4548 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4549 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4550 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4551 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4552 | |
| 4553 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4554 | const Type *Base = nullptr; |
| 4555 | uint64_t Members = 0; |
| 4556 | if (Kind == ELFv2 && |
| 4557 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4558 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4559 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4560 | return ABIArgInfo::getDirect(CoerceTy); |
| 4561 | } |
| 4562 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4563 | // If an aggregate may end up fully in registers, we do not |
| 4564 | // use the ByVal method, but pass the aggregate as array. |
| 4565 | // This is usually beneficial since we avoid forcing the |
| 4566 | // back-end to store the argument to memory. |
| 4567 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4568 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4569 | llvm::Type *CoerceTy; |
| 4570 | |
| 4571 | // Types up to 8 bytes are passed as integer type (which will be |
| 4572 | // properly aligned in the argument save area doubleword). |
| 4573 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4574 | CoerceTy = |
| 4575 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4576 | // Larger types are passed as arrays, with the base type selected |
| 4577 | // according to the required alignment in the save area. |
| 4578 | else { |
| 4579 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4580 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4581 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4582 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4583 | } |
| 4584 | |
| 4585 | return ABIArgInfo::getDirect(CoerceTy); |
| 4586 | } |
| 4587 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4588 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4589 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4590 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4591 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | return (isPromotableTypeForABI(Ty) ? |
| 4595 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4596 | } |
| 4597 | |
| 4598 | ABIArgInfo |
| 4599 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4600 | if (RetTy->isVoidType()) |
| 4601 | return ABIArgInfo::getIgnore(); |
| 4602 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4603 | if (RetTy->isAnyComplexType()) |
| 4604 | return ABIArgInfo::getDirect(); |
| 4605 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4606 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4607 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4608 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4609 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4610 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4611 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4612 | else if (Size < 128) { |
| 4613 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4614 | return ABIArgInfo::getDirect(CoerceTy); |
| 4615 | } |
| 4616 | } |
| 4617 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4618 | if (isAggregateTypeForABI(RetTy)) { |
| 4619 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4620 | const Type *Base = nullptr; |
| 4621 | uint64_t Members = 0; |
| 4622 | if (Kind == ELFv2 && |
| 4623 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4624 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4625 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4626 | return ABIArgInfo::getDirect(CoerceTy); |
| 4627 | } |
| 4628 | |
| 4629 | // ELFv2 small aggregates are returned in up to two registers. |
| 4630 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4631 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4632 | if (Bits == 0) |
| 4633 | return ABIArgInfo::getIgnore(); |
| 4634 | |
| 4635 | llvm::Type *CoerceTy; |
| 4636 | if (Bits > GPRBits) { |
| 4637 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4638 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4639 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4640 | CoerceTy = |
| 4641 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4642 | return ABIArgInfo::getDirect(CoerceTy); |
| 4643 | } |
| 4644 | |
| 4645 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4646 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4647 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4648 | |
| 4649 | return (isPromotableTypeForABI(RetTy) ? |
| 4650 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4651 | } |
| 4652 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4653 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4654 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4655 | QualType Ty) const { |
| 4656 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4657 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4658 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4659 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4660 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4661 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4662 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4663 | // in separate doublewords. However, Clang expects us to produce a |
| 4664 | // pointer to a structure with the two parts packed tightly. So generate |
| 4665 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4666 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4667 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4668 | CharUnits EltSize = TypeInfo.first / 2; |
| 4669 | if (EltSize < SlotSize) { |
| 4670 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4671 | SlotSize * 2, SlotSize, |
| 4672 | SlotSize, /*AllowHigher*/ true); |
| 4673 | |
| 4674 | Address RealAddr = Addr; |
| 4675 | Address ImagAddr = RealAddr; |
| 4676 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4677 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4678 | SlotSize - EltSize); |
| 4679 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4680 | 2 * SlotSize - EltSize); |
| 4681 | } else { |
| 4682 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4683 | } |
| 4684 | |
| 4685 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4686 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4687 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4688 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4689 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4690 | |
| 4691 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4692 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4693 | /*init*/ true); |
| 4694 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4695 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4698 | // Otherwise, just use the general rule. |
| 4699 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4700 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | static bool |
| 4704 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4705 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4706 | // This is calculated from the LLVM and GCC tables and verified |
| 4707 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4708 | |
| 4709 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4710 | |
| 4711 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4712 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4713 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4714 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4715 | |
| 4716 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4717 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4718 | |
| 4719 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4720 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4721 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4722 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4723 | // 64: mq |
| 4724 | // 65: lr |
| 4725 | // 66: ctr |
| 4726 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4727 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4728 | |
| 4729 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4730 | // 68-75 cr0-7 |
| 4731 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4732 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4733 | |
| 4734 | // 77-108: v0-31, the 16-byte vector registers |
| 4735 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4736 | |
| 4737 | // 109: vrsave |
| 4738 | // 110: vscr |
| 4739 | // 111: spe_acc |
| 4740 | // 112: spefscr |
| 4741 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4742 | // 114: tfhar |
| 4743 | // 115: tfiar |
| 4744 | // 116: texasr |
| 4745 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4746 | |
| 4747 | return false; |
| 4748 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4749 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4750 | bool |
| 4751 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4752 | CodeGen::CodeGenFunction &CGF, |
| 4753 | llvm::Value *Address) const { |
| 4754 | |
| 4755 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4756 | } |
| 4757 | |
| 4758 | bool |
| 4759 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4760 | llvm::Value *Address) const { |
| 4761 | |
| 4762 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4763 | } |
| 4764 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4765 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4766 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4767 | //===----------------------------------------------------------------------===// |
| 4768 | |
| 4769 | namespace { |
| 4770 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4771 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4772 | public: |
| 4773 | enum ABIKind { |
| 4774 | AAPCS = 0, |
| 4775 | DarwinPCS |
| 4776 | }; |
| 4777 | |
| 4778 | private: |
| 4779 | ABIKind Kind; |
| 4780 | |
| 4781 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4782 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4783 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4784 | |
| 4785 | private: |
| 4786 | ABIKind getABIKind() const { return Kind; } |
| 4787 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4788 | |
| 4789 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4790 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4791 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4792 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4793 | uint64_t Members) const override; |
| 4794 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4795 | bool isIllegalVectorType(QualType Ty) const; |
| 4796 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4797 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4798 | if (!getCXXABI().classifyReturnType(FI)) |
| 4799 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4800 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4801 | for (auto &it : FI.arguments()) |
| 4802 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4805 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4806 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4807 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4808 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4809 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4810 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4811 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4812 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4813 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4814 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4815 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4816 | |
| 4817 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4818 | ArrayRef<llvm::Type*> scalars, |
| 4819 | bool asReturnValue) const override { |
| 4820 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4821 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4822 | bool isSwiftErrorInRegister() const override { |
| 4823 | return true; |
| 4824 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4825 | }; |
| 4826 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4827 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4828 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4829 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4830 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4831 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4832 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Saleem Abdulrasool | 40db477 | 2017-02-11 23:03:13 +0000 | [diff] [blame] | 4833 | return "mov\tfp, fp\t\t# marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4836 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4837 | return 31; |
| 4838 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4839 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4840 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4841 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4842 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4843 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4844 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4845 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4846 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4847 | // Handle illegal vector types here. |
| 4848 | if (isIllegalVectorType(Ty)) { |
| 4849 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4850 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4851 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4852 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4853 | return ABIArgInfo::getDirect(ResType); |
| 4854 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4855 | if (Size <= 32) { |
| 4856 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4857 | return ABIArgInfo::getDirect(ResType); |
| 4858 | } |
| 4859 | if (Size == 64) { |
| 4860 | llvm::Type *ResType = |
| 4861 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4862 | return ABIArgInfo::getDirect(ResType); |
| 4863 | } |
| 4864 | if (Size == 128) { |
| 4865 | llvm::Type *ResType = |
| 4866 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4867 | return ABIArgInfo::getDirect(ResType); |
| 4868 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4869 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4870 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4871 | |
| 4872 | if (!isAggregateTypeForABI(Ty)) { |
| 4873 | // Treat an enum type as its underlying type. |
| 4874 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4875 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4876 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4877 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4878 | ? ABIArgInfo::getExtend() |
| 4879 | : ABIArgInfo::getDirect()); |
| 4880 | } |
| 4881 | |
| 4882 | // Structures with either a non-trivial destructor or a non-trivial |
| 4883 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4884 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4885 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4886 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4890 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4891 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4892 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 4893 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4894 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4895 | return ABIArgInfo::getIgnore(); |
| 4896 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4897 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 4898 | // 0. |
| 4899 | if (IsEmpty && Size == 0) |
| 4900 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4901 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4902 | } |
| 4903 | |
| 4904 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4905 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4906 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4907 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4908 | return ABIArgInfo::getDirect( |
| 4909 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4913 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4914 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4915 | // same size and alignment. |
| 4916 | if (getTarget().isRenderScriptTarget()) { |
| 4917 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4918 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4919 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 4920 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4921 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4922 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4923 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4924 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4925 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4926 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4927 | } |
| 4928 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4929 | } |
| 4930 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4931 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4934 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4935 | if (RetTy->isVoidType()) |
| 4936 | return ABIArgInfo::getIgnore(); |
| 4937 | |
| 4938 | // Large vector types should be returned via memory. |
| 4939 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4940 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4941 | |
| 4942 | if (!isAggregateTypeForABI(RetTy)) { |
| 4943 | // Treat an enum type as its underlying type. |
| 4944 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4945 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4946 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4947 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4948 | ? ABIArgInfo::getExtend() |
| 4949 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4952 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4953 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4954 | return ABIArgInfo::getIgnore(); |
| 4955 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4956 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4957 | uint64_t Members = 0; |
| 4958 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4960 | return ABIArgInfo::getDirect(); |
| 4961 | |
| 4962 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4963 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4964 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4965 | // same size and alignment. |
| 4966 | if (getTarget().isRenderScriptTarget()) { |
| 4967 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 4968 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4969 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 4970 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4971 | |
| 4972 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4973 | // For aggregates with 16-byte alignment, we use i128. |
| 4974 | if (Alignment < 128 && Size == 128) { |
| 4975 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4976 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4977 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4978 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4979 | } |
| 4980 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4981 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4984 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4985 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4986 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4987 | // Check whether VT is legal. |
| 4988 | unsigned NumElements = VT->getNumElements(); |
| 4989 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 4990 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 4991 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4992 | return true; |
| 4993 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4994 | } |
| 4995 | return false; |
| 4996 | } |
| 4997 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4998 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4999 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5000 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5001 | // but with the difference that any floating-point type is allowed, |
| 5002 | // including __fp16. |
| 5003 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5004 | if (BT->isFloatingPoint()) |
| 5005 | return true; |
| 5006 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5007 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5008 | if (VecSize == 64 || VecSize == 128) |
| 5009 | return true; |
| 5010 | } |
| 5011 | return false; |
| 5012 | } |
| 5013 | |
| 5014 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5015 | uint64_t Members) const { |
| 5016 | return Members <= 4; |
| 5017 | } |
| 5018 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5019 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5020 | QualType Ty, |
| 5021 | CodeGenFunction &CGF) const { |
| 5022 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5023 | bool IsIndirect = AI.isIndirect(); |
| 5024 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5025 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5026 | if (IsIndirect) |
| 5027 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5028 | else if (AI.getCoerceToType()) |
| 5029 | BaseTy = AI.getCoerceToType(); |
| 5030 | |
| 5031 | unsigned NumRegs = 1; |
| 5032 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5033 | BaseTy = ArrTy->getElementType(); |
| 5034 | NumRegs = ArrTy->getNumElements(); |
| 5035 | } |
| 5036 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5037 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5038 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5039 | // Standard, section B.4: |
| 5040 | // |
| 5041 | // struct { |
| 5042 | // void *__stack; |
| 5043 | // void *__gr_top; |
| 5044 | // void *__vr_top; |
| 5045 | // int __gr_offs; |
| 5046 | // int __vr_offs; |
| 5047 | // }; |
| 5048 | |
| 5049 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5050 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5051 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5052 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5053 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5054 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5055 | CharUnits TyAlign = TyInfo.second; |
| 5056 | |
| 5057 | Address reg_offs_p = Address::invalid(); |
| 5058 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5059 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5060 | CharUnits reg_top_offset; |
| 5061 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5062 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5063 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5064 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5065 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5066 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5067 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5068 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5069 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5070 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5071 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5072 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5073 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5074 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5075 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5076 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5077 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5078 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5079 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | //======================================= |
| 5083 | // Find out where argument was passed |
| 5084 | //======================================= |
| 5085 | |
| 5086 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5087 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5088 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5089 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5090 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5091 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5092 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5093 | |
| 5094 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5095 | |
| 5096 | // Otherwise, at least some kind of argument could go in these registers, the |
Bob Wilson | 3abf169 | 2014-04-21 01:23:36 +0000 | [diff] [blame] | 5097 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5098 | CGF.EmitBlock(MaybeRegBlock); |
| 5099 | |
| 5100 | // Integer arguments may need to correct register alignment (for example a |
| 5101 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5102 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5103 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5104 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5105 | |
| 5106 | reg_offs = CGF.Builder.CreateAdd( |
| 5107 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5108 | "align_regoffs"); |
| 5109 | reg_offs = CGF.Builder.CreateAnd( |
| 5110 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5111 | "aligned_regoffs"); |
| 5112 | } |
| 5113 | |
| 5114 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5115 | // The fact that this is done unconditionally reflects the fact that |
| 5116 | // allocating an argument to the stack also uses up all the remaining |
| 5117 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5118 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5119 | NewOffset = CGF.Builder.CreateAdd( |
| 5120 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5121 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5122 | |
| 5123 | // Now we're in a position to decide whether this argument really was in |
| 5124 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5125 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5126 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5127 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5128 | |
| 5129 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5130 | |
| 5131 | //======================================= |
| 5132 | // Argument was in registers |
| 5133 | //======================================= |
| 5134 | |
| 5135 | // Now we emit the code for if the argument was originally passed in |
| 5136 | // registers. First start the appropriate block: |
| 5137 | CGF.EmitBlock(InRegBlock); |
| 5138 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5139 | llvm::Value *reg_top = nullptr; |
| 5140 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5141 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5142 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5143 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5144 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5145 | Address RegAddr = Address::invalid(); |
| 5146 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5147 | |
| 5148 | if (IsIndirect) { |
| 5149 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5150 | // stored registers or on the stack will actually be a struct **. |
| 5151 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5152 | } |
| 5153 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5154 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5155 | uint64_t NumMembers = 0; |
| 5156 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5157 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5158 | // Homogeneous aggregates passed in registers will have their elements split |
| 5159 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5160 | // qN+1, ...). We reload and store into a temporary local variable |
| 5161 | // contiguously. |
| 5162 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5163 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5164 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5165 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5166 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5167 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5168 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5169 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5170 | int Offset = 0; |
| 5171 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5172 | BaseTyInfo.first.getQuantity() < 16) |
| 5173 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5174 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5175 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5176 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5177 | Address LoadAddr = |
| 5178 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5179 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5180 | |
| 5181 | Address StoreAddr = |
| 5182 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5183 | |
| 5184 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5185 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5186 | } |
| 5187 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5188 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5189 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5190 | // Otherwise the object is contiguous in memory. |
| 5191 | |
| 5192 | // It might be right-aligned in its slot. |
| 5193 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5194 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5195 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5196 | TyInfo.first < SlotSize) { |
| 5197 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5198 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5201 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | CGF.EmitBranch(ContBlock); |
| 5205 | |
| 5206 | //======================================= |
| 5207 | // Argument was on the stack |
| 5208 | //======================================= |
| 5209 | CGF.EmitBlock(OnStackBlock); |
| 5210 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5211 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5212 | CharUnits::Zero(), "stack_p"); |
| 5213 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5214 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5215 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5216 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5217 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5218 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5219 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5220 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5221 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5222 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5223 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5224 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5225 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5226 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5227 | "align_stack"); |
| 5228 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5229 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5230 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5231 | Address OnStackAddr(OnStackPtr, |
| 5232 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5233 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5234 | // All stack slots are multiples of 8 bytes. |
| 5235 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5236 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5237 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5238 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5239 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5240 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5241 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5242 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5243 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5244 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5245 | |
| 5246 | // Write the new value of __stack for the next call to va_arg |
| 5247 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5248 | |
| 5249 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5250 | TyInfo.first < StackSlotSize) { |
| 5251 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5252 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5253 | } |
| 5254 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5255 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5256 | |
| 5257 | CGF.EmitBranch(ContBlock); |
| 5258 | |
| 5259 | //======================================= |
| 5260 | // Tidy up |
| 5261 | //======================================= |
| 5262 | CGF.EmitBlock(ContBlock); |
| 5263 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5264 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5265 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5266 | |
| 5267 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5268 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5269 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5270 | |
| 5271 | return ResAddr; |
| 5272 | } |
| 5273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5274 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5275 | CodeGenFunction &CGF) const { |
| 5276 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5277 | // illegal vector types. Lower VAArg here for these cases and use |
| 5278 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5279 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5280 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5281 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5282 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5283 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5284 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5285 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5286 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5287 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5288 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5291 | // The size of the actual thing passed, which might end up just |
| 5292 | // being a pointer for indirect types. |
| 5293 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5294 | |
| 5295 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5296 | // aggregates should be passed indirectly. |
| 5297 | bool IsIndirect = false; |
| 5298 | if (TyInfo.first.getQuantity() > 16) { |
| 5299 | const Type *Base = nullptr; |
| 5300 | uint64_t Members = 0; |
| 5301 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5304 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5305 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5309 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5310 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5311 | |
| 5312 | namespace { |
| 5313 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5314 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5315 | public: |
| 5316 | enum ABIKind { |
| 5317 | APCS = 0, |
| 5318 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5319 | AAPCS_VFP = 2, |
| 5320 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5321 | }; |
| 5322 | |
| 5323 | private: |
| 5324 | ABIKind Kind; |
| 5325 | |
| 5326 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5327 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5328 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5329 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5330 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5331 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5332 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5333 | switch (getTarget().getTriple().getEnvironment()) { |
| 5334 | case llvm::Triple::Android: |
| 5335 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5336 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5337 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5338 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5339 | case llvm::Triple::MuslEABI: |
| 5340 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5341 | return true; |
| 5342 | default: |
| 5343 | return false; |
| 5344 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5347 | bool isEABIHF() const { |
| 5348 | switch (getTarget().getTriple().getEnvironment()) { |
| 5349 | case llvm::Triple::EABIHF: |
| 5350 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5351 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5352 | return true; |
| 5353 | default: |
| 5354 | return false; |
| 5355 | } |
| 5356 | } |
| 5357 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5358 | ABIKind getABIKind() const { return Kind; } |
| 5359 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5360 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5361 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5362 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5363 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5364 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5365 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5366 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5367 | uint64_t Members) const override; |
| 5368 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5369 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5371 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5372 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5373 | |
| 5374 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5375 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5376 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5377 | |
| 5378 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5379 | ArrayRef<llvm::Type*> scalars, |
| 5380 | bool asReturnValue) const override { |
| 5381 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5382 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5383 | bool isSwiftErrorInRegister() const override { |
| 5384 | return true; |
| 5385 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5386 | }; |
| 5387 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5388 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5389 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5390 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5391 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5392 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5393 | const ARMABIInfo &getABIInfo() const { |
| 5394 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5395 | } |
| 5396 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5397 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5398 | return 13; |
| 5399 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5400 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5401 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5402 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5403 | } |
| 5404 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5405 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5406 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5407 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5408 | |
| 5409 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5410 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5411 | return false; |
| 5412 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5413 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5414 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5415 | if (getABIInfo().isEABI()) return 88; |
| 5416 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5417 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5418 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5419 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5420 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5421 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5422 | if (!FD) |
| 5423 | return; |
| 5424 | |
| 5425 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5426 | if (!Attr) |
| 5427 | return; |
| 5428 | |
| 5429 | const char *Kind; |
| 5430 | switch (Attr->getInterrupt()) { |
| 5431 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5432 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5433 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5434 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5435 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5436 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5437 | } |
| 5438 | |
| 5439 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5440 | |
| 5441 | Fn->addFnAttr("interrupt", Kind); |
| 5442 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5443 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5444 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5445 | return; |
| 5446 | |
| 5447 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5448 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5449 | // the backend to perform a realignment as part of the function prologue. |
| 5450 | llvm::AttrBuilder B; |
| 5451 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5452 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5453 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5454 | }; |
| 5455 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5456 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5457 | public: |
| 5458 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5459 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5460 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5461 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5462 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5463 | |
| 5464 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5465 | llvm::SmallString<24> &Opt) const override { |
| 5466 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5467 | } |
| 5468 | |
| 5469 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5470 | llvm::SmallString<32> &Opt) const override { |
| 5471 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5472 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5473 | }; |
| 5474 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5475 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5476 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5477 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5478 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5479 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5480 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5481 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5482 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5483 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5484 | FI.getReturnInfo() = |
| 5485 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5486 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5487 | for (auto &I : FI.arguments()) |
| 5488 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5489 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5490 | // Always honor user-specified calling convention. |
| 5491 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5492 | return; |
| 5493 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5494 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5495 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5496 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5497 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5498 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5499 | /// Return the default calling convention that LLVM will use. |
| 5500 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5501 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5502 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5503 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5504 | else if (isEABI()) |
| 5505 | return llvm::CallingConv::ARM_AAPCS; |
| 5506 | else |
| 5507 | return llvm::CallingConv::ARM_APCS; |
| 5508 | } |
| 5509 | |
| 5510 | /// Return the calling convention that our ABI would like us to use |
| 5511 | /// as the C calling convention. |
| 5512 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5513 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5514 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5515 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5516 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5517 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5518 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5519 | llvm_unreachable("bad ABI kind"); |
| 5520 | } |
| 5521 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5522 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5523 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5524 | |
| 5525 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5526 | // they'd just match what LLVM will infer from the triple. |
| 5527 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5528 | if (abiCC != getLLVMDefaultCC()) |
| 5529 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5530 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5531 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5532 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5533 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5534 | switch (getABIKind()) { |
| 5535 | case APCS: |
| 5536 | case AAPCS16_VFP: |
| 5537 | if (abiCC != getLLVMDefaultCC()) |
| 5538 | BuiltinCC = abiCC; |
| 5539 | break; |
| 5540 | case AAPCS: |
| 5541 | case AAPCS_VFP: |
| 5542 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5543 | break; |
| 5544 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5547 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5548 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5549 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5550 | // A single-precision floating-point type (including promoted |
| 5551 | // half-precision types); A double-precision floating-point type; |
| 5552 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5553 | // with a Base Type of a single- or double-precision floating-point type, |
| 5554 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5555 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5556 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5557 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5558 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5559 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5560 | // Handle illegal vector types here. |
| 5561 | if (isIllegalVectorType(Ty)) { |
| 5562 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5563 | if (Size <= 32) { |
| 5564 | llvm::Type *ResType = |
| 5565 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5566 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5567 | } |
| 5568 | if (Size == 64) { |
| 5569 | llvm::Type *ResType = llvm::VectorType::get( |
| 5570 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5571 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5572 | } |
| 5573 | if (Size == 128) { |
| 5574 | llvm::Type *ResType = llvm::VectorType::get( |
| 5575 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5576 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5577 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5578 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5581 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5582 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5583 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5584 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5585 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5586 | llvm::Type::getFloatTy(getVMContext()) : |
| 5587 | llvm::Type::getInt32Ty(getVMContext()); |
| 5588 | return ABIArgInfo::getDirect(ResType); |
| 5589 | } |
| 5590 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5591 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5592 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5593 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5594 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5595 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5596 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5597 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5598 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5599 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5600 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5601 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5602 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5603 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5604 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5605 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5606 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5607 | return ABIArgInfo::getIgnore(); |
| 5608 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5609 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5610 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5611 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5612 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5613 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5614 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5615 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5616 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5617 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5618 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5619 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5620 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5621 | // this convention even for a variadic function: the backend will use GPRs |
| 5622 | // if needed. |
| 5623 | const Type *Base = nullptr; |
| 5624 | uint64_t Members = 0; |
| 5625 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5626 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5627 | llvm::Type *Ty = |
| 5628 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5629 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5634 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5635 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5636 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5637 | // and a pointer is passed. |
| 5638 | return ABIArgInfo::getIndirect( |
| 5639 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5642 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5643 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5644 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5645 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5646 | uint64_t ABIAlign = 4; |
| 5647 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5648 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5649 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5650 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5651 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5652 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5653 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5654 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5655 | /*ByVal=*/true, |
| 5656 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5659 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5660 | // same size and alignment. |
| 5661 | if (getTarget().isRenderScriptTarget()) { |
| 5662 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5663 | } |
| 5664 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5665 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5666 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5667 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5668 | // FIXME: Try to match the types of the arguments more accurately where |
| 5669 | // we can. |
| 5670 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5671 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5672 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5673 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5674 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5675 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5676 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5677 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5678 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5681 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5682 | llvm::LLVMContext &VMContext) { |
| 5683 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5684 | // is called integer-like if its size is less than or equal to one word, and |
| 5685 | // the offset of each of its addressable sub-fields is zero. |
| 5686 | |
| 5687 | uint64_t Size = Context.getTypeSize(Ty); |
| 5688 | |
| 5689 | // Check that the type fits in a word. |
| 5690 | if (Size > 32) |
| 5691 | return false; |
| 5692 | |
| 5693 | // FIXME: Handle vector types! |
| 5694 | if (Ty->isVectorType()) |
| 5695 | return false; |
| 5696 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5697 | // Float types are never treated as "integer like". |
| 5698 | if (Ty->isRealFloatingType()) |
| 5699 | return false; |
| 5700 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5701 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5702 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5703 | return true; |
| 5704 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5705 | // Small complex integer types are "integer like". |
| 5706 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5707 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5708 | |
| 5709 | // Single element and zero sized arrays should be allowed, by the definition |
| 5710 | // above, but they are not. |
| 5711 | |
| 5712 | // Otherwise, it must be a record type. |
| 5713 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5714 | if (!RT) return false; |
| 5715 | |
| 5716 | // Ignore records with flexible arrays. |
| 5717 | const RecordDecl *RD = RT->getDecl(); |
| 5718 | if (RD->hasFlexibleArrayMember()) |
| 5719 | return false; |
| 5720 | |
| 5721 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5722 | // like". |
| 5723 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5724 | |
| 5725 | bool HadField = false; |
| 5726 | unsigned idx = 0; |
| 5727 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5728 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5729 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5730 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5731 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5732 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5733 | // struct { int : 0; int x } |
| 5734 | // is non-integer like according to gcc. |
| 5735 | if (FD->isBitField()) { |
| 5736 | if (!RD->isUnion()) |
| 5737 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5738 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5739 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5740 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5741 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5742 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5745 | // Check if this field is at offset 0. |
| 5746 | if (Layout.getFieldOffset(idx) != 0) |
| 5747 | return false; |
| 5748 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5749 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5750 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5751 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5752 | // Only allow at most one field in a structure. This doesn't match the |
| 5753 | // wording above, but follows gcc in situations with a field following an |
| 5754 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5755 | if (!RD->isUnion()) { |
| 5756 | if (HadField) |
| 5757 | return false; |
| 5758 | |
| 5759 | HadField = true; |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | return true; |
| 5764 | } |
| 5765 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5766 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5767 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5768 | bool IsEffectivelyAAPCS_VFP = |
| 5769 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5770 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5771 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5772 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5773 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5774 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5775 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5776 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5777 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5778 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5779 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5780 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5781 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5782 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5783 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5784 | llvm::Type::getFloatTy(getVMContext()) : |
| 5785 | llvm::Type::getInt32Ty(getVMContext()); |
| 5786 | return ABIArgInfo::getDirect(ResType); |
| 5787 | } |
| 5788 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5789 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5790 | // Treat an enum type as its underlying type. |
| 5791 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5792 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5793 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5794 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5795 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5796 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5797 | |
| 5798 | // Are we following APCS? |
| 5799 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5800 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5801 | return ABIArgInfo::getIgnore(); |
| 5802 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5803 | // Complex types are all returned as packed integers. |
| 5804 | // |
| 5805 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5806 | // correctly. |
| 5807 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5808 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5809 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5810 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5811 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5812 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5813 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5814 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5815 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5816 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5817 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5818 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5819 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5823 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5824 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5825 | |
| 5826 | // Otherwise this is an AAPCS variant. |
| 5827 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5828 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5829 | return ABIArgInfo::getIgnore(); |
| 5830 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5831 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5832 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5833 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5834 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5835 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5836 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5837 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5838 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5839 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5842 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5843 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5844 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5845 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5846 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5847 | // same size and alignment. |
| 5848 | if (getTarget().isRenderScriptTarget()) { |
| 5849 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5850 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5851 | if (getDataLayout().isBigEndian()) |
| 5852 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5853 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5854 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5855 | // Return in the smallest viable integer type. |
| 5856 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5857 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5858 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5859 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5860 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5861 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5862 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5863 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5864 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5865 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5868 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5871 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5872 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5873 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5874 | if (isAndroid()) { |
| 5875 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5876 | // vector ABI. The primary differences were that 3-element vector types |
| 5877 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5878 | // accepts that legacy behavior for Android only. |
| 5879 | // Check whether VT is legal. |
| 5880 | unsigned NumElements = VT->getNumElements(); |
| 5881 | // NumElements should be power of 2 or equal to 3. |
| 5882 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5883 | return true; |
| 5884 | } else { |
| 5885 | // Check whether VT is legal. |
| 5886 | unsigned NumElements = VT->getNumElements(); |
| 5887 | uint64_t Size = getContext().getTypeSize(VT); |
| 5888 | // NumElements should be power of 2. |
| 5889 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5890 | return true; |
| 5891 | // Size should be greater than 32 bits. |
| 5892 | return Size <= 32; |
| 5893 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5894 | } |
| 5895 | return false; |
| 5896 | } |
| 5897 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5898 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5899 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5900 | // double, or 64-bit or 128-bit vectors. |
| 5901 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5902 | if (BT->getKind() == BuiltinType::Float || |
| 5903 | BT->getKind() == BuiltinType::Double || |
| 5904 | BT->getKind() == BuiltinType::LongDouble) |
| 5905 | return true; |
| 5906 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5907 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5908 | if (VecSize == 64 || VecSize == 128) |
| 5909 | return true; |
| 5910 | } |
| 5911 | return false; |
| 5912 | } |
| 5913 | |
| 5914 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5915 | uint64_t Members) const { |
| 5916 | return Members <= 4; |
| 5917 | } |
| 5918 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5919 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5920 | QualType Ty) const { |
| 5921 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5922 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5923 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5924 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5925 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5926 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5927 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5930 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5931 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5932 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5933 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5934 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5935 | const Type *Base = nullptr; |
| 5936 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5937 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5938 | IsIndirect = true; |
| 5939 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5940 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5941 | // allocated by the caller. |
| 5942 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5943 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5944 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5945 | IsIndirect = true; |
| 5946 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5947 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5948 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5949 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5950 | // Our callers should be prepared to handle an under-aligned address. |
| 5951 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5952 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5953 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5954 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5955 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5956 | // ARMv7k allows type alignment up to 16 bytes. |
| 5957 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5958 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5959 | } else { |
| 5960 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5961 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5962 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5963 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5964 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5965 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5968 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5969 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5970 | //===----------------------------------------------------------------------===// |
| 5971 | |
| 5972 | namespace { |
| 5973 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5974 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5975 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5976 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5977 | |
| 5978 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5979 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5980 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5981 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5982 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5983 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5984 | }; |
| 5985 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5986 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5987 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5988 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5989 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5990 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5991 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5992 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5993 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5994 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5995 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5996 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5997 | }; |
| 5998 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5999 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6000 | if (RetTy->isVoidType()) |
| 6001 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6002 | |
| 6003 | // note: this is different from default ABI |
| 6004 | if (!RetTy->isScalarType()) |
| 6005 | return ABIArgInfo::getDirect(); |
| 6006 | |
| 6007 | // Treat an enum type as its underlying type. |
| 6008 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6009 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6010 | |
| 6011 | return (RetTy->isPromotableIntegerType() ? |
| 6012 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6013 | } |
| 6014 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6015 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6016 | // Treat an enum type as its underlying type. |
| 6017 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6018 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6019 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6020 | // Return aggregates type as indirect by value |
| 6021 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6022 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6023 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6024 | return (Ty->isPromotableIntegerType() ? |
| 6025 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6028 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6029 | if (!getCXXABI().classifyReturnType(FI)) |
| 6030 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6031 | for (auto &I : FI.arguments()) |
| 6032 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6033 | |
| 6034 | // Always honor user-specified calling convention. |
| 6035 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6036 | return; |
| 6037 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6038 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6039 | } |
| 6040 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6041 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6042 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6043 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6046 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6047 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6048 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6049 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6050 | if (!FD) return; |
| 6051 | |
| 6052 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6053 | |
| 6054 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6055 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6056 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6057 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6058 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6059 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6060 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6061 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6062 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6063 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6064 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6065 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6066 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6067 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6068 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6069 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6070 | // __global__ functions cannot be called from the device, we do not |
| 6071 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6072 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6073 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6074 | addNVVMMetadata(F, "kernel", 1); |
| 6075 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6076 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6077 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6078 | llvm::APSInt MaxThreads(32); |
| 6079 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6080 | if (MaxThreads > 0) |
| 6081 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6082 | |
| 6083 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6084 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6085 | // we don't have to add a PTX directive. |
| 6086 | if (Attr->getMinBlocks()) { |
| 6087 | llvm::APSInt MinBlocks(32); |
| 6088 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6089 | if (MinBlocks > 0) |
| 6090 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6091 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6092 | } |
| 6093 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6094 | } |
| 6095 | } |
| 6096 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6097 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6098 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6099 | llvm::Module *M = F->getParent(); |
| 6100 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6101 | |
| 6102 | // Get "nvvm.annotations" metadata node |
| 6103 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6104 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6105 | llvm::Metadata *MDVals[] = { |
| 6106 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6107 | llvm::ConstantAsMetadata::get( |
| 6108 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6109 | // Append metadata to nvvm.annotations |
| 6110 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6111 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6112 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6113 | |
| 6114 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6115 | // SystemZ ABI Implementation |
| 6116 | //===----------------------------------------------------------------------===// |
| 6117 | |
| 6118 | namespace { |
| 6119 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6120 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6121 | bool HasVector; |
| 6122 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6123 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6124 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6125 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6126 | |
| 6127 | bool isPromotableIntegerType(QualType Ty) const; |
| 6128 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6129 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6130 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6131 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6132 | |
| 6133 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6134 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6135 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6136 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6137 | if (!getCXXABI().classifyReturnType(FI)) |
| 6138 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6139 | for (auto &I : FI.arguments()) |
| 6140 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6143 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6144 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6145 | |
| 6146 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 6147 | ArrayRef<llvm::Type*> scalars, |
| 6148 | bool asReturnValue) const override { |
| 6149 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6150 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6151 | bool isSwiftErrorInRegister() const override { |
| 6152 | return true; |
| 6153 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6154 | }; |
| 6155 | |
| 6156 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6157 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6158 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6159 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6160 | }; |
| 6161 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6162 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6163 | |
| 6164 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6165 | // Treat an enum type as its underlying type. |
| 6166 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6167 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6168 | |
| 6169 | // Promotable integer types are required to be promoted by the ABI. |
| 6170 | if (Ty->isPromotableIntegerType()) |
| 6171 | return true; |
| 6172 | |
| 6173 | // 32-bit values must also be promoted. |
| 6174 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6175 | switch (BT->getKind()) { |
| 6176 | case BuiltinType::Int: |
| 6177 | case BuiltinType::UInt: |
| 6178 | return true; |
| 6179 | default: |
| 6180 | return false; |
| 6181 | } |
| 6182 | return false; |
| 6183 | } |
| 6184 | |
| 6185 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6186 | return (Ty->isAnyComplexType() || |
| 6187 | Ty->isVectorType() || |
| 6188 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6189 | } |
| 6190 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6191 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6192 | return (HasVector && |
| 6193 | Ty->isVectorType() && |
| 6194 | getContext().getTypeSize(Ty) <= 128); |
| 6195 | } |
| 6196 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6197 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6198 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6199 | switch (BT->getKind()) { |
| 6200 | case BuiltinType::Float: |
| 6201 | case BuiltinType::Double: |
| 6202 | return true; |
| 6203 | default: |
| 6204 | return false; |
| 6205 | } |
| 6206 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6207 | return false; |
| 6208 | } |
| 6209 | |
| 6210 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6211 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6212 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6213 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6214 | |
| 6215 | // If this is a C++ record, check the bases first. |
| 6216 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6217 | for (const auto &I : CXXRD->bases()) { |
| 6218 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6219 | |
| 6220 | // Empty bases don't affect things either way. |
| 6221 | if (isEmptyRecord(getContext(), Base, true)) |
| 6222 | continue; |
| 6223 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6224 | if (!Found.isNull()) |
| 6225 | return Ty; |
| 6226 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6230 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6231 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6232 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6233 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6234 | if (getContext().getLangOpts().CPlusPlus && |
| 6235 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6236 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6237 | |
| 6238 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6239 | // Nested structures still do though. |
| 6240 | if (!Found.isNull()) |
| 6241 | return Ty; |
| 6242 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6246 | // An 8-byte aligned struct s { float f; } is passed as a double. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6247 | if (!Found.isNull()) |
| 6248 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6251 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6254 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6255 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6256 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6257 | // struct { |
| 6258 | // i64 __gpr; |
| 6259 | // i64 __fpr; |
| 6260 | // i8 *__overflow_arg_area; |
| 6261 | // i8 *__reg_save_area; |
| 6262 | // }; |
| 6263 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6264 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6265 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6266 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6267 | Ty = getContext().getCanonicalType(Ty); |
| 6268 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6269 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6270 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6271 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6272 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6273 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6274 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6275 | CharUnits UnpaddedSize; |
| 6276 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6277 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6278 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6279 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6280 | } else { |
| 6281 | if (AI.getCoerceToType()) |
| 6282 | ArgTy = AI.getCoerceToType(); |
| 6283 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6284 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6285 | UnpaddedSize = TyInfo.first; |
| 6286 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6287 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6288 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6289 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6290 | PaddedSize = CharUnits::fromQuantity(16); |
| 6291 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6292 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6293 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6294 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6295 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6296 | llvm::Value *PaddedSizeV = |
| 6297 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6298 | |
| 6299 | if (IsVector) { |
| 6300 | // Work out the address of a vector argument on the stack. |
| 6301 | // Vector arguments are always passed in the high bits of a |
| 6302 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6303 | Address OverflowArgAreaPtr = |
| 6304 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6305 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6306 | Address OverflowArgArea = |
| 6307 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6308 | TyInfo.second); |
| 6309 | Address MemAddr = |
| 6310 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6311 | |
| 6312 | // Update overflow_arg_area_ptr pointer |
| 6313 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6314 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6315 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6316 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6317 | |
| 6318 | return MemAddr; |
| 6319 | } |
| 6320 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6321 | assert(PaddedSize.getQuantity() == 8); |
| 6322 | |
| 6323 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6324 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6325 | if (InFPRs) { |
| 6326 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6327 | RegCountField = 1; // __fpr |
| 6328 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6329 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6330 | } else { |
| 6331 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6332 | RegCountField = 0; // __gpr |
| 6333 | RegSaveIndex = 2; // save offset for r2 |
| 6334 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6335 | } |
| 6336 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6337 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6338 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6339 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6340 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6341 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6342 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6343 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6344 | |
| 6345 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6346 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6347 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6348 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6349 | |
| 6350 | // Emit code to load the value if it was passed in registers. |
| 6351 | CGF.EmitBlock(InRegBlock); |
| 6352 | |
| 6353 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6354 | llvm::Value *ScaledRegCount = |
| 6355 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6356 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6357 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6358 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6359 | llvm::Value *RegOffset = |
| 6360 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6361 | Address RegSaveAreaPtr = |
| 6362 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6363 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6364 | llvm::Value *RegSaveArea = |
| 6365 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6366 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6367 | "raw_reg_addr"), |
| 6368 | PaddedSize); |
| 6369 | Address RegAddr = |
| 6370 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6371 | |
| 6372 | // Update the register count |
| 6373 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6374 | llvm::Value *NewRegCount = |
| 6375 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6376 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6377 | CGF.EmitBranch(ContBlock); |
| 6378 | |
| 6379 | // Emit code to load the value if it was passed in memory. |
| 6380 | CGF.EmitBlock(InMemBlock); |
| 6381 | |
| 6382 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6383 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6384 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6385 | Address OverflowArgArea = |
| 6386 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6387 | PaddedSize); |
| 6388 | Address RawMemAddr = |
| 6389 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6390 | Address MemAddr = |
| 6391 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6392 | |
| 6393 | // Update overflow_arg_area_ptr pointer |
| 6394 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6395 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6396 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6397 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6398 | CGF.EmitBranch(ContBlock); |
| 6399 | |
| 6400 | // Return the appropriate result. |
| 6401 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6402 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6403 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6404 | |
| 6405 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6406 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6407 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6408 | |
| 6409 | return ResAddr; |
| 6410 | } |
| 6411 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6412 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6413 | if (RetTy->isVoidType()) |
| 6414 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6415 | if (isVectorArgumentType(RetTy)) |
| 6416 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6417 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6418 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6419 | return (isPromotableIntegerType(RetTy) ? |
| 6420 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6421 | } |
| 6422 | |
| 6423 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6424 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6425 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6426 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6427 | |
| 6428 | // Integers and enums are extended to full register width. |
| 6429 | if (isPromotableIntegerType(Ty)) |
| 6430 | return ABIArgInfo::getExtend(); |
| 6431 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6432 | // Handle vector types and vector-like structure types. Note that |
| 6433 | // as opposed to float-like structure types, we do not allow any |
| 6434 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6435 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6436 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6437 | if (isVectorArgumentType(SingleElementTy) && |
| 6438 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6439 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6440 | |
| 6441 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6442 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6443 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6444 | |
| 6445 | // Handle small structures. |
| 6446 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6447 | // Structures with flexible arrays have variable length, so really |
| 6448 | // fail the size test above. |
| 6449 | const RecordDecl *RD = RT->getDecl(); |
| 6450 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6451 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6452 | |
| 6453 | // The structure is passed as an unextended integer, a float, or a double. |
| 6454 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6455 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6456 | assert(Size == 32 || Size == 64); |
| 6457 | if (Size == 32) |
| 6458 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6459 | else |
| 6460 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6461 | } else |
| 6462 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6463 | return ABIArgInfo::getDirect(PassTy); |
| 6464 | } |
| 6465 | |
| 6466 | // Non-structure compounds are passed indirectly. |
| 6467 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6468 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6469 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6470 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6474 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6475 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6476 | |
| 6477 | namespace { |
| 6478 | |
| 6479 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6480 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6481 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6482 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6483 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6484 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6485 | }; |
| 6486 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6487 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6488 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6489 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6490 | llvm::GlobalValue *GV, |
| 6491 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6492 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6493 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6494 | // Handle 'interrupt' attribute: |
| 6495 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6496 | |
| 6497 | // Step 1: Set ISR calling convention. |
| 6498 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6499 | |
| 6500 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6501 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6502 | |
| 6503 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6504 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6505 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6506 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6507 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6508 | } |
| 6509 | } |
| 6510 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6511 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6512 | // MIPS ABI Implementation. This works for both little-endian and |
| 6513 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6514 | //===----------------------------------------------------------------------===// |
| 6515 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6516 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6517 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6518 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6519 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6520 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6521 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6522 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6523 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6524 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6525 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6526 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6527 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6528 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6529 | |
| 6530 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6531 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6532 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6533 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6534 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6535 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6536 | }; |
| 6537 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6538 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6539 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6540 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6541 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6542 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6543 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6544 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6545 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6546 | return 29; |
| 6547 | } |
| 6548 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6549 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6550 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6551 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6552 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6553 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6554 | if (FD->hasAttr<Mips16Attr>()) { |
| 6555 | Fn->addFnAttr("mips16"); |
| 6556 | } |
| 6557 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6558 | Fn->addFnAttr("nomips16"); |
| 6559 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6560 | |
| 6561 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6562 | if (!Attr) |
| 6563 | return; |
| 6564 | |
| 6565 | const char *Kind; |
| 6566 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6567 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6568 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6569 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6570 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6571 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6572 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6573 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6574 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6575 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6576 | } |
| 6577 | |
| 6578 | Fn->addFnAttr("interrupt", Kind); |
| 6579 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6580 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6581 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6582 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6583 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6584 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6585 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6586 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6587 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6588 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6589 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6590 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6591 | void MipsABIInfo::CoerceToIntArgs( |
| 6592 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6593 | llvm::IntegerType *IntTy = |
| 6594 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6595 | |
| 6596 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6597 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6598 | ArgList.push_back(IntTy); |
| 6599 | |
| 6600 | // If necessary, add one more integer type to ArgList. |
| 6601 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6602 | |
| 6603 | if (R) |
| 6604 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6607 | // In N32/64, an aligned double precision floating point field is passed in |
| 6608 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6609 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6610 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6611 | |
| 6612 | if (IsO32) { |
| 6613 | CoerceToIntArgs(TySize, ArgList); |
| 6614 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6615 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6616 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6617 | if (Ty->isComplexType()) |
| 6618 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6619 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6620 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6621 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6622 | // Unions/vectors are passed in integer registers. |
| 6623 | if (!RT || !RT->isStructureOrClassType()) { |
| 6624 | CoerceToIntArgs(TySize, ArgList); |
| 6625 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6626 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6627 | |
| 6628 | const RecordDecl *RD = RT->getDecl(); |
| 6629 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6630 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6631 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6632 | uint64_t LastOffset = 0; |
| 6633 | unsigned idx = 0; |
| 6634 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6635 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6636 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6637 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6638 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6639 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6640 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6641 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6642 | |
| 6643 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6644 | continue; |
| 6645 | |
| 6646 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6647 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6648 | continue; |
| 6649 | |
| 6650 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6651 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6652 | ArgList.push_back(I64); |
| 6653 | |
| 6654 | // Add double type. |
| 6655 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6656 | LastOffset = Offset + 64; |
| 6657 | } |
| 6658 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6659 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6660 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6661 | |
| 6662 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6663 | } |
| 6664 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6665 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6666 | uint64_t Offset) const { |
| 6667 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6668 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6669 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6670 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6671 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6672 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6673 | ABIArgInfo |
| 6674 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6675 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6676 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6677 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6678 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6679 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6680 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6681 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6682 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6683 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6684 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6685 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6686 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6687 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6688 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6689 | return ABIArgInfo::getIgnore(); |
| 6690 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6691 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6692 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6693 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6694 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6695 | |
Petar Jovanovic | 6f4cdb8 | 2017-05-10 14:28:18 +0000 | [diff] [blame] | 6696 | // Use indirect if the aggregate cannot fit into registers for |
| 6697 | // passing arguments according to the ABI |
| 6698 | unsigned Threshold = IsO32 ? 16 : 64; |
| 6699 | |
| 6700 | if(getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(Threshold)) |
| 6701 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), true, |
| 6702 | getContext().getTypeAlign(Ty) / 8 > Align); |
| 6703 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6704 | // If we have reached here, aggregates are passed directly by coercing to |
| 6705 | // another structure type. Padding is inserted if the offset of the |
| 6706 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6707 | ABIArgInfo ArgInfo = |
| 6708 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6709 | getPaddingType(OrigOffset, CurrOffset)); |
| 6710 | ArgInfo.setInReg(true); |
| 6711 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | // Treat an enum type as its underlying type. |
| 6715 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6716 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6717 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6718 | // All integral types are promoted to the GPR width. |
| 6719 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6720 | return ABIArgInfo::getExtend(); |
| 6721 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6722 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6723 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6726 | llvm::Type* |
| 6727 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6728 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6729 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6730 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6731 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6732 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6733 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6734 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6735 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6736 | // N32/64 returns struct/classes in floating point registers if the |
| 6737 | // following conditions are met: |
| 6738 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6739 | // 2. The struct/class has one or two fields all of which are floating |
| 6740 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6741 | // 3. The offset of the first field is zero (this follows what gcc does). |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6742 | // |
| 6743 | // Any other composite results are returned in integer registers. |
| 6744 | // |
| 6745 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6746 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6747 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6748 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6749 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6750 | if (!BT || !BT->isFloatingPoint()) |
| 6751 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6752 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6753 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | if (b == e) |
| 6757 | return llvm::StructType::get(getVMContext(), RTList, |
| 6758 | RD->hasAttr<PackedAttr>()); |
| 6759 | |
| 6760 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6761 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6764 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6765 | return llvm::StructType::get(getVMContext(), RTList); |
| 6766 | } |
| 6767 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6768 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6769 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6770 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6771 | if (RetTy->isVoidType()) |
| 6772 | return ABIArgInfo::getIgnore(); |
| 6773 | |
| 6774 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6775 | // However, N32/N64 ignores zero sized return values. |
| 6776 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6777 | return ABIArgInfo::getIgnore(); |
| 6778 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6779 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6780 | if (Size <= 128) { |
| 6781 | if (RetTy->isAnyComplexType()) |
| 6782 | return ABIArgInfo::getDirect(); |
| 6783 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6784 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6785 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6786 | if (!IsO32 || |
| 6787 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6788 | ABIArgInfo ArgInfo = |
| 6789 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6790 | ArgInfo.setInReg(true); |
| 6791 | return ArgInfo; |
| 6792 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6793 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6794 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6795 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | // Treat an enum type as its underlying type. |
| 6799 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6800 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6801 | |
| 6802 | return (RetTy->isPromotableIntegerType() ? |
| 6803 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6804 | } |
| 6805 | |
| 6806 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6807 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6808 | if (!getCXXABI().classifyReturnType(FI)) |
| 6809 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6810 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6811 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6812 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6813 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6814 | for (auto &I : FI.arguments()) |
| 6815 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6818 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6819 | QualType OrigTy) const { |
| 6820 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6821 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6822 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6823 | // Pointers are also promoted in the same way but this only matters for N32. |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6824 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6825 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6826 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6827 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6828 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6829 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6830 | DidPromote = true; |
| 6831 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6832 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6833 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6834 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6835 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6836 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6837 | // The alignment of things in the argument area is never larger than |
| 6838 | // StackAlignInBytes. |
| 6839 | TyInfo.second = |
| 6840 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6841 | |
| 6842 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6843 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6844 | |
| 6845 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6846 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6847 | |
| 6848 | |
| 6849 | // If there was a promotion, "unpromote" into a temporary. |
| 6850 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6851 | if (DidPromote) { |
| 6852 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6853 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6854 | |
| 6855 | // Truncate down to the right width. |
| 6856 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6857 | : CGF.IntPtrTy); |
| 6858 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6859 | if (OrigTy->isPointerType()) |
| 6860 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6861 | |
| 6862 | CGF.Builder.CreateStore(V, Temp); |
| 6863 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6864 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6865 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6866 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6867 | } |
| 6868 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6869 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6870 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6871 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6872 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6873 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6874 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6875 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6876 | return false; |
| 6877 | } |
| 6878 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6879 | bool |
| 6880 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6881 | llvm::Value *Address) const { |
| 6882 | // This information comes from gcc's implementation, which seems to |
| 6883 | // as canonical as it gets. |
| 6884 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6885 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6886 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6887 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6888 | |
| 6889 | // 0-31 are the general purpose registers, $0 - $31. |
| 6890 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6891 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6892 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6893 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6894 | |
| 6895 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6896 | // They are one bit wide and ignored here. |
| 6897 | |
| 6898 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6899 | // (coprocessor 1 is the FP unit) |
| 6900 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6901 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6902 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6903 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6904 | return false; |
| 6905 | } |
| 6906 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6907 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6908 | // AVR ABI Implementation. |
| 6909 | //===----------------------------------------------------------------------===// |
| 6910 | |
| 6911 | namespace { |
| 6912 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6913 | public: |
| 6914 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6915 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 6916 | |
| 6917 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 6918 | CodeGen::CodeGenModule &CGM) const override { |
| 6919 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 6920 | if (!FD) return; |
| 6921 | auto *Fn = cast<llvm::Function>(GV); |
| 6922 | |
| 6923 | if (FD->getAttr<AVRInterruptAttr>()) |
| 6924 | Fn->addFnAttr("interrupt"); |
| 6925 | |
| 6926 | if (FD->getAttr<AVRSignalAttr>()) |
| 6927 | Fn->addFnAttr("signal"); |
| 6928 | } |
| 6929 | }; |
| 6930 | } |
| 6931 | |
| 6932 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6933 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6934 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6935 | // handling. |
| 6936 | //===----------------------------------------------------------------------===// |
| 6937 | |
| 6938 | namespace { |
| 6939 | |
| 6940 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6941 | public: |
| 6942 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6943 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6944 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6945 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6946 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6947 | }; |
| 6948 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6949 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6950 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6951 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6952 | if (!FD) return; |
| 6953 | |
| 6954 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6955 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6956 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6957 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6958 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6959 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6960 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6961 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6962 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6963 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6964 | llvm::NamedMDNode *OpenCLMetadata = |
| 6965 | M.getModule().getOrInsertNamedMetadata( |
| 6966 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6967 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6968 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6969 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6970 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6971 | Operands.push_back( |
| 6972 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6973 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6974 | Operands.push_back( |
| 6975 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6976 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6977 | Operands.push_back( |
| 6978 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6979 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6980 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6981 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6982 | // (false) for implementing the work_group_size_hint attr later. |
| 6983 | // Currently always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6984 | Operands.push_back( |
| 6985 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6986 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6987 | } |
| 6988 | } |
| 6989 | } |
| 6990 | } |
| 6991 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6992 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6993 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6994 | //===----------------------------------------------------------------------===// |
| 6995 | // Hexagon ABI Implementation |
| 6996 | //===----------------------------------------------------------------------===// |
| 6997 | |
| 6998 | namespace { |
| 6999 | |
| 7000 | class HexagonABIInfo : public ABIInfo { |
| 7001 | |
| 7002 | |
| 7003 | public: |
| 7004 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7005 | |
| 7006 | private: |
| 7007 | |
| 7008 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7009 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7010 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7011 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7012 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7013 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7014 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7015 | }; |
| 7016 | |
| 7017 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7018 | public: |
| 7019 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7020 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7021 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7022 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7023 | return 29; |
| 7024 | } |
| 7025 | }; |
| 7026 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7027 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7028 | |
| 7029 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7030 | if (!getCXXABI().classifyReturnType(FI)) |
| 7031 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7032 | for (auto &I : FI.arguments()) |
| 7033 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7037 | if (!isAggregateTypeForABI(Ty)) { |
| 7038 | // Treat an enum type as its underlying type. |
| 7039 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7040 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7041 | |
| 7042 | return (Ty->isPromotableIntegerType() ? |
| 7043 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7044 | } |
| 7045 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame^] | 7046 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7047 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7048 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7049 | // Ignore empty records. |
| 7050 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7051 | return ABIArgInfo::getIgnore(); |
| 7052 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7053 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7054 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7055 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7056 | // Pass in the smallest viable integer type. |
| 7057 | else if (Size > 32) |
| 7058 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7059 | else if (Size > 16) |
| 7060 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7061 | else if (Size > 8) |
| 7062 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7063 | else |
| 7064 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7065 | } |
| 7066 | |
| 7067 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7068 | if (RetTy->isVoidType()) |
| 7069 | return ABIArgInfo::getIgnore(); |
| 7070 | |
| 7071 | // Large vector types should be returned via memory. |
| 7072 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7073 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7074 | |
| 7075 | if (!isAggregateTypeForABI(RetTy)) { |
| 7076 | // Treat an enum type as its underlying type. |
| 7077 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7078 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7079 | |
| 7080 | return (RetTy->isPromotableIntegerType() ? |
| 7081 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7082 | } |
| 7083 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7084 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7085 | return ABIArgInfo::getIgnore(); |
| 7086 | |
| 7087 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7088 | // are returned indirectly. |
| 7089 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7090 | if (Size <= 64) { |
| 7091 | // Return in the smallest viable integer type. |
| 7092 | if (Size <= 8) |
| 7093 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7094 | if (Size <= 16) |
| 7095 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7096 | if (Size <= 32) |
| 7097 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7098 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7099 | } |
| 7100 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7101 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7104 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7105 | QualType Ty) const { |
| 7106 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7107 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7108 | getContext().getTypeInfoInChars(Ty), |
| 7109 | CharUnits::fromQuantity(4), |
| 7110 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7113 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7114 | // Lanai ABI Implementation |
| 7115 | //===----------------------------------------------------------------------===// |
| 7116 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7117 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7118 | class LanaiABIInfo : public DefaultABIInfo { |
| 7119 | public: |
| 7120 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7121 | |
| 7122 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7123 | |
| 7124 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7125 | CCState State(FI.getCallingConvention()); |
| 7126 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7127 | // regparm attribute set. |
| 7128 | if (FI.getHasRegParm()) { |
| 7129 | State.FreeRegs = FI.getRegParm(); |
| 7130 | } else { |
| 7131 | State.FreeRegs = 4; |
| 7132 | } |
| 7133 | |
| 7134 | if (!getCXXABI().classifyReturnType(FI)) |
| 7135 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7136 | for (auto &I : FI.arguments()) |
| 7137 | I.info = classifyArgumentType(I.type, State); |
| 7138 | } |
| 7139 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7140 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7141 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7142 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7143 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7144 | |
| 7145 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7146 | unsigned Size = getContext().getTypeSize(Ty); |
| 7147 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7148 | |
| 7149 | if (SizeInRegs == 0) |
| 7150 | return false; |
| 7151 | |
| 7152 | if (SizeInRegs > State.FreeRegs) { |
| 7153 | State.FreeRegs = 0; |
| 7154 | return false; |
| 7155 | } |
| 7156 | |
| 7157 | State.FreeRegs -= SizeInRegs; |
| 7158 | |
| 7159 | return true; |
| 7160 | } |
| 7161 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7162 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7163 | CCState &State) const { |
| 7164 | if (!ByVal) { |
| 7165 | if (State.FreeRegs) { |
| 7166 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7167 | return getNaturalAlignIndirectInReg(Ty); |
| 7168 | } |
| 7169 | return getNaturalAlignIndirect(Ty, false); |
| 7170 | } |
| 7171 | |
| 7172 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7173 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7174 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7175 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7176 | /*Realign=*/TypeAlign > |
| 7177 | MinABIStackAlignInBytes); |
| 7178 | } |
| 7179 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7180 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7181 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7182 | // Check with the C++ ABI first. |
| 7183 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7184 | if (RT) { |
| 7185 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7186 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7187 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7188 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7189 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7190 | } |
| 7191 | } |
| 7192 | |
| 7193 | if (isAggregateTypeForABI(Ty)) { |
| 7194 | // Structures with flexible arrays are always indirect. |
| 7195 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7196 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7197 | |
| 7198 | // Ignore empty structs/unions. |
| 7199 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7200 | return ABIArgInfo::getIgnore(); |
| 7201 | |
| 7202 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7203 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7204 | if (SizeInRegs <= State.FreeRegs) { |
| 7205 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7206 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7207 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7208 | State.FreeRegs -= SizeInRegs; |
| 7209 | return ABIArgInfo::getDirectInReg(Result); |
| 7210 | } else { |
| 7211 | State.FreeRegs = 0; |
| 7212 | } |
| 7213 | return getIndirectResult(Ty, true, State); |
| 7214 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7215 | |
| 7216 | // Treat an enum type as its underlying type. |
| 7217 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7218 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7219 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7220 | bool InReg = shouldUseInReg(Ty, State); |
| 7221 | if (Ty->isPromotableIntegerType()) { |
| 7222 | if (InReg) |
| 7223 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7224 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7225 | } |
| 7226 | if (InReg) |
| 7227 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7228 | return ABIArgInfo::getDirect(); |
| 7229 | } |
| 7230 | |
| 7231 | namespace { |
| 7232 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7233 | public: |
| 7234 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7235 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7236 | }; |
| 7237 | } |
| 7238 | |
| 7239 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7240 | // AMDGPU ABI Implementation |
| 7241 | //===----------------------------------------------------------------------===// |
| 7242 | |
| 7243 | namespace { |
| 7244 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7245 | class AMDGPUABIInfo final : public DefaultABIInfo { |
| 7246 | public: |
| 7247 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7248 | |
| 7249 | private: |
| 7250 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 7251 | |
| 7252 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7253 | }; |
| 7254 | |
| 7255 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7256 | if (!getCXXABI().classifyReturnType(FI)) |
| 7257 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7258 | |
| 7259 | unsigned CC = FI.getCallingConvention(); |
| 7260 | for (auto &Arg : FI.arguments()) |
| 7261 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) |
| 7262 | Arg.info = classifyArgumentType(Arg.type); |
| 7263 | else |
| 7264 | Arg.info = DefaultABIInfo::classifyArgumentType(Arg.type); |
| 7265 | } |
| 7266 | |
| 7267 | /// \brief Classify argument of given type \p Ty. |
| 7268 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty) const { |
| 7269 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7270 | if (!StrTy) { |
| 7271 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7272 | } |
| 7273 | |
| 7274 | // Coerce single element structs to its element. |
| 7275 | if (StrTy->getNumElements() == 1) { |
| 7276 | return ABIArgInfo::getDirect(); |
| 7277 | } |
| 7278 | |
| 7279 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7280 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7281 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7282 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7283 | } |
| 7284 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7285 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7286 | public: |
| 7287 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7288 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7289 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7290 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7291 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7292 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7293 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7294 | llvm::PointerType *T, QualType QT) const override; |
| 7295 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7296 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7297 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7298 | static void appendOpenCLVersionMD (CodeGen::CodeGenModule &CGM); |
| 7299 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7300 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7301 | const Decl *D, |
| 7302 | llvm::GlobalValue *GV, |
| 7303 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7304 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7305 | if (!FD) |
| 7306 | return; |
| 7307 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7308 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7309 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7310 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7311 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7312 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7313 | if (ReqdWGS || FlatWGS) { |
| 7314 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7315 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7316 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7317 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7318 | |
| 7319 | if (Min != 0) { |
| 7320 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7321 | |
| 7322 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7323 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7324 | } else |
| 7325 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7328 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7329 | unsigned Min = Attr->getMin(); |
| 7330 | unsigned Max = Attr->getMax(); |
| 7331 | |
| 7332 | if (Min != 0) { |
| 7333 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7334 | |
| 7335 | std::string AttrVal = llvm::utostr(Min); |
| 7336 | if (Max != 0) |
| 7337 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7338 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7339 | } else |
| 7340 | assert(Max == 0 && "Max must be zero"); |
| 7341 | } |
| 7342 | |
| 7343 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7344 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7345 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7346 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7347 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7348 | } |
| 7349 | |
| 7350 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7351 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7352 | |
| 7353 | if (NumVGPR != 0) |
| 7354 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7355 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7356 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7357 | appendOpenCLVersionMD(M); |
| 7358 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7359 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7360 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7361 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7362 | } |
| 7363 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7364 | // Currently LLVM assumes null pointers always have value 0, |
| 7365 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7366 | // emitting null pointers in private and local address spaces, a null |
| 7367 | // pointer in generic address space is emitted which is casted to a |
| 7368 | // pointer in local or private address space. |
| 7369 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7370 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7371 | QualType QT) const { |
| 7372 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7373 | return llvm::ConstantPointerNull::get(PT); |
| 7374 | |
| 7375 | auto &Ctx = CGM.getContext(); |
| 7376 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7377 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7378 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7379 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7380 | } |
| 7381 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7382 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7383 | // SPARC v8 ABI Implementation. |
| 7384 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7385 | // |
| 7386 | // Ensures that complex values are passed in registers. |
| 7387 | // |
| 7388 | namespace { |
| 7389 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7390 | public: |
| 7391 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7392 | |
| 7393 | private: |
| 7394 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7395 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7396 | }; |
| 7397 | } // end anonymous namespace |
| 7398 | |
| 7399 | |
| 7400 | ABIArgInfo |
| 7401 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7402 | if (Ty->isAnyComplexType()) { |
| 7403 | return ABIArgInfo::getDirect(); |
| 7404 | } |
| 7405 | else { |
| 7406 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7407 | } |
| 7408 | } |
| 7409 | |
| 7410 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7411 | |
| 7412 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7413 | for (auto &Arg : FI.arguments()) |
| 7414 | Arg.info = classifyArgumentType(Arg.type); |
| 7415 | } |
| 7416 | |
| 7417 | namespace { |
| 7418 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7419 | public: |
| 7420 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7421 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7422 | }; |
| 7423 | } // end anonymous namespace |
| 7424 | |
| 7425 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7426 | // SPARC v9 ABI Implementation. |
| 7427 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7428 | // |
| 7429 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7430 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7431 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7432 | // |
| 7433 | // One case requires special care: |
| 7434 | // |
| 7435 | // struct mixed { |
| 7436 | // int i; |
| 7437 | // float f; |
| 7438 | // }; |
| 7439 | // |
| 7440 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7441 | // parameter array, but the int is passed in an integer register, and the float |
| 7442 | // is passed in a floating point register. This is represented as two arguments |
| 7443 | // with the LLVM IR inreg attribute: |
| 7444 | // |
| 7445 | // declare void f(i32 inreg %i, float inreg %f) |
| 7446 | // |
| 7447 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7448 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7449 | // bytes. |
| 7450 | // |
| 7451 | namespace { |
| 7452 | class SparcV9ABIInfo : public ABIInfo { |
| 7453 | public: |
| 7454 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7455 | |
| 7456 | private: |
| 7457 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7458 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7459 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7460 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7461 | |
| 7462 | // Coercion type builder for structs passed in registers. The coercion type |
| 7463 | // serves two purposes: |
| 7464 | // |
| 7465 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7466 | // in registers. |
| 7467 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7468 | // code generator knows to pass them in floating point registers. |
| 7469 | // |
| 7470 | // We also compute the InReg flag which indicates that the struct contains |
| 7471 | // aligned 32-bit floats. |
| 7472 | // |
| 7473 | struct CoerceBuilder { |
| 7474 | llvm::LLVMContext &Context; |
| 7475 | const llvm::DataLayout &DL; |
| 7476 | SmallVector<llvm::Type*, 8> Elems; |
| 7477 | uint64_t Size; |
| 7478 | bool InReg; |
| 7479 | |
| 7480 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7481 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7482 | |
| 7483 | // Pad Elems with integers until Size is ToSize. |
| 7484 | void pad(uint64_t ToSize) { |
| 7485 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7486 | if (ToSize == Size) |
| 7487 | return; |
| 7488 | |
| 7489 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7490 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7491 | if (Aligned > Size && Aligned <= ToSize) { |
| 7492 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7493 | Size = Aligned; |
| 7494 | } |
| 7495 | |
| 7496 | // Add whole 64-bit words. |
| 7497 | while (Size + 64 <= ToSize) { |
| 7498 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7499 | Size += 64; |
| 7500 | } |
| 7501 | |
| 7502 | // Final in-word padding. |
| 7503 | if (Size < ToSize) { |
| 7504 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7505 | Size = ToSize; |
| 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | // Add a floating point element at Offset. |
| 7510 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7511 | // Unaligned floats are treated as integers. |
| 7512 | if (Offset % Bits) |
| 7513 | return; |
| 7514 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7515 | if (Bits < 64) |
| 7516 | InReg = true; |
| 7517 | pad(Offset); |
| 7518 | Elems.push_back(Ty); |
| 7519 | Size = Offset + Bits; |
| 7520 | } |
| 7521 | |
| 7522 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7523 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7524 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7525 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7526 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7527 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7528 | switch (ElemTy->getTypeID()) { |
| 7529 | case llvm::Type::StructTyID: |
| 7530 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7531 | break; |
| 7532 | case llvm::Type::FloatTyID: |
| 7533 | addFloat(ElemOffset, ElemTy, 32); |
| 7534 | break; |
| 7535 | case llvm::Type::DoubleTyID: |
| 7536 | addFloat(ElemOffset, ElemTy, 64); |
| 7537 | break; |
| 7538 | case llvm::Type::FP128TyID: |
| 7539 | addFloat(ElemOffset, ElemTy, 128); |
| 7540 | break; |
| 7541 | case llvm::Type::PointerTyID: |
| 7542 | if (ElemOffset % 64 == 0) { |
| 7543 | pad(ElemOffset); |
| 7544 | Elems.push_back(ElemTy); |
| 7545 | Size += 64; |
| 7546 | } |
| 7547 | break; |
| 7548 | default: |
| 7549 | break; |
| 7550 | } |
| 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | // Check if Ty is a usable substitute for the coercion type. |
| 7555 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7556 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7557 | } |
| 7558 | |
| 7559 | // Get the coercion type as a literal struct type. |
| 7560 | llvm::Type *getType() const { |
| 7561 | if (Elems.size() == 1) |
| 7562 | return Elems.front(); |
| 7563 | else |
| 7564 | return llvm::StructType::get(Context, Elems); |
| 7565 | } |
| 7566 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7567 | }; |
| 7568 | } // end anonymous namespace |
| 7569 | |
| 7570 | ABIArgInfo |
| 7571 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7572 | if (Ty->isVoidType()) |
| 7573 | return ABIArgInfo::getIgnore(); |
| 7574 | |
| 7575 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7576 | |
| 7577 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7578 | // pointer / sret pointer. |
| 7579 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7580 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7581 | |
| 7582 | // Treat an enum type as its underlying type. |
| 7583 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7584 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7585 | |
| 7586 | // Integer types smaller than a register are extended. |
| 7587 | if (Size < 64 && Ty->isIntegerType()) |
| 7588 | return ABIArgInfo::getExtend(); |
| 7589 | |
| 7590 | // Other non-aggregates go in registers. |
| 7591 | if (!isAggregateTypeForABI(Ty)) |
| 7592 | return ABIArgInfo::getDirect(); |
| 7593 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7594 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7595 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7596 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7597 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7598 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7599 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7600 | // Build a coercion type from the LLVM struct type. |
| 7601 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7602 | if (!StrTy) |
| 7603 | return ABIArgInfo::getDirect(); |
| 7604 | |
| 7605 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7606 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7607 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7608 | |
| 7609 | // Try to use the original type for coercion. |
| 7610 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7611 | |
| 7612 | if (CB.InReg) |
| 7613 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7614 | else |
| 7615 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7616 | } |
| 7617 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7618 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7619 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7620 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7621 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7622 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7623 | AI.setCoerceToType(ArgTy); |
| 7624 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7625 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7626 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7627 | CGBuilderTy &Builder = CGF.Builder; |
| 7628 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7629 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7630 | |
| 7631 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7632 | |
| 7633 | Address ArgAddr = Address::invalid(); |
| 7634 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7635 | switch (AI.getKind()) { |
| 7636 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7637 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7638 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7639 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7640 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7641 | case ABIArgInfo::Extend: { |
| 7642 | Stride = SlotSize; |
| 7643 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7644 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7645 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7646 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7647 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7648 | case ABIArgInfo::Direct: { |
| 7649 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7650 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7651 | ArgAddr = Addr; |
| 7652 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7653 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7654 | |
| 7655 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7656 | Stride = SlotSize; |
| 7657 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7658 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7659 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7660 | break; |
| 7661 | |
| 7662 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7663 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
| 7666 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7667 | llvm::Value *NextPtr = |
| 7668 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7669 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7670 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7671 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7672 | } |
| 7673 | |
| 7674 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7675 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7676 | for (auto &I : FI.arguments()) |
| 7677 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7678 | } |
| 7679 | |
| 7680 | namespace { |
| 7681 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7682 | public: |
| 7683 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7684 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7685 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7686 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7687 | return 14; |
| 7688 | } |
| 7689 | |
| 7690 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7691 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7692 | }; |
| 7693 | } // end anonymous namespace |
| 7694 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7695 | bool |
| 7696 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7697 | llvm::Value *Address) const { |
| 7698 | // This is calculated from the LLVM and GCC tables and verified |
| 7699 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7700 | |
| 7701 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7702 | |
| 7703 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7704 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7705 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7706 | |
| 7707 | // 0-31: the 8-byte general-purpose registers |
| 7708 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7709 | |
| 7710 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7711 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7712 | |
| 7713 | // Y = 64 |
| 7714 | // PSR = 65 |
| 7715 | // WIM = 66 |
| 7716 | // TBR = 67 |
| 7717 | // PC = 68 |
| 7718 | // NPC = 69 |
| 7719 | // FSR = 70 |
| 7720 | // CSR = 71 |
| 7721 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7722 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7723 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7724 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7725 | |
| 7726 | return false; |
| 7727 | } |
| 7728 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7729 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7730 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7731 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7732 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7733 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7734 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7735 | |
| 7736 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7737 | /// it by reference between functions that append to it. |
| 7738 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7739 | |
| 7740 | /// TypeStringCache caches the meta encodings of Types. |
| 7741 | /// |
| 7742 | /// The reason for caching TypeStrings is two fold: |
| 7743 | /// 1. To cache a type's encoding for later uses; |
| 7744 | /// 2. As a means to break recursive member type inclusion. |
| 7745 | /// |
| 7746 | /// A cache Entry can have a Status of: |
| 7747 | /// NonRecursive: The type encoding is not recursive; |
| 7748 | /// Recursive: The type encoding is recursive; |
| 7749 | /// Incomplete: An incomplete TypeString; |
| 7750 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7751 | /// Recursive type encoding. |
| 7752 | /// |
| 7753 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7754 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7755 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7756 | /// the type is encountered. |
| 7757 | /// |
| 7758 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7759 | /// possible. The type itself is recursive and it may contain other types which |
| 7760 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7761 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7762 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7763 | /// |
| 7764 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7765 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7766 | /// are placed into the cache during type expansion as a means to identify and |
| 7767 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7768 | /// the entry becomes IncompleteUsed. |
| 7769 | /// |
| 7770 | /// During the expansion of a RecordType's members: |
| 7771 | /// |
| 7772 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7773 | /// cached encoding is used; |
| 7774 | /// |
| 7775 | /// If the cache contains a Recursive encoding for the member type, the |
| 7776 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7777 | /// |
| 7778 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7779 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7780 | /// |
| 7781 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7782 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7783 | /// it is swapped back in; |
| 7784 | /// |
| 7785 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7786 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7787 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7788 | /// |
| 7789 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7790 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7791 | /// Else the member is part of a recursive type and thus the recursion has |
| 7792 | /// been exited too soon for the encoding to be correct for the member. |
| 7793 | /// |
| 7794 | class TypeStringCache { |
| 7795 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7796 | struct Entry { |
| 7797 | std::string Str; // The encoded TypeString for the type. |
| 7798 | enum Status State; // Information about the encoding in 'Str'. |
| 7799 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7800 | // during the expansion of RecordType's members. |
| 7801 | }; |
| 7802 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7803 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7804 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7805 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7806 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7807 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7808 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7809 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7810 | bool IsRecursive); |
| 7811 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7812 | }; |
| 7813 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7814 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7815 | /// FieldEncoding is a helper for this ordering process. |
| 7816 | class FieldEncoding { |
| 7817 | bool HasName; |
| 7818 | std::string Enc; |
| 7819 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7820 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 7821 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7822 | bool operator<(const FieldEncoding &rhs) const { |
| 7823 | if (HasName != rhs.HasName) return HasName; |
| 7824 | return Enc < rhs.Enc; |
| 7825 | } |
| 7826 | }; |
| 7827 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7828 | class XCoreABIInfo : public DefaultABIInfo { |
| 7829 | public: |
| 7830 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7831 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7832 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7833 | }; |
| 7834 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7835 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7836 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7837 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7838 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7839 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 7840 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7841 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7842 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7843 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7844 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7845 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 7846 | // TODO: this implementation is likely now redundant with the default |
| 7847 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7848 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7849 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7850 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7851 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7852 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7853 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 7854 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7855 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7856 | // Handle the argument. |
| 7857 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7858 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7859 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7860 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7861 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7862 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7863 | |
| 7864 | Address Val = Address::invalid(); |
| 7865 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7866 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7867 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7868 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7869 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7870 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7871 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7872 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 7873 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7874 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7875 | case ABIArgInfo::Extend: |
| 7876 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7877 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 7878 | ArgSize = CharUnits::fromQuantity( |
| 7879 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7880 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7881 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7882 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7883 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 7884 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 7885 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7886 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7887 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7888 | |
| 7889 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7890 | if (!ArgSize.isZero()) { |
| 7891 | llvm::Value *APN = |
| 7892 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7893 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7894 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7895 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7896 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7897 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7898 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7899 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7900 | /// into the cache as a means to identify and break recursion. |
| 7901 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7902 | /// be reinserted by removeIncomplete(). |
| 7903 | /// All other types of encoding should have been used rather than arriving here. |
| 7904 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7905 | std::string StubEnc) { |
| 7906 | if (!ID) |
| 7907 | return; |
| 7908 | Entry &E = Map[ID]; |
| 7909 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7910 | "Incorrectly use of addIncomplete"); |
| 7911 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7912 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7913 | E.Str.swap(StubEnc); |
| 7914 | E.State = Incomplete; |
| 7915 | ++IncompleteCount; |
| 7916 | } |
| 7917 | |
| 7918 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7919 | /// must be removed from the cache. |
| 7920 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7921 | /// Returns true if the RecordType was defined recursively. |
| 7922 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7923 | if (!ID) |
| 7924 | return false; |
| 7925 | auto I = Map.find(ID); |
| 7926 | assert(I != Map.end() && "Entry not present"); |
| 7927 | Entry &E = I->second; |
| 7928 | assert( (E.State == Incomplete || |
| 7929 | E.State == IncompleteUsed) && |
| 7930 | "Entry must be an incomplete type"); |
| 7931 | bool IsRecursive = false; |
| 7932 | if (E.State == IncompleteUsed) { |
| 7933 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7934 | IsRecursive = true; |
| 7935 | --IncompleteUsedCount; |
| 7936 | } |
| 7937 | if (E.Swapped.empty()) |
| 7938 | Map.erase(I); |
| 7939 | else { |
| 7940 | // Swap the Recursive back. |
| 7941 | E.Swapped.swap(E.Str); |
| 7942 | E.Swapped.clear(); |
| 7943 | E.State = Recursive; |
| 7944 | } |
| 7945 | --IncompleteCount; |
| 7946 | return IsRecursive; |
| 7947 | } |
| 7948 | |
| 7949 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7950 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7951 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7952 | bool IsRecursive) { |
| 7953 | if (!ID || IncompleteUsedCount) |
| 7954 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7955 | Entry &E = Map[ID]; |
| 7956 | if (IsRecursive && !E.Str.empty()) { |
| 7957 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7958 | "This is not the same Recursive entry"); |
| 7959 | // The parent container was not recursive after all, so we could have used |
| 7960 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7961 | // we started viz: IncompleteCount!=0. |
| 7962 | return; |
| 7963 | } |
| 7964 | assert(E.Str.empty() && "Entry already present"); |
| 7965 | E.Str = Str.str(); |
| 7966 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7967 | } |
| 7968 | |
| 7969 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7970 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7971 | /// encoding is Recursive, return an empty StringRef. |
| 7972 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7973 | if (!ID) |
| 7974 | return StringRef(); // We have no key. |
| 7975 | auto I = Map.find(ID); |
| 7976 | if (I == Map.end()) |
| 7977 | return StringRef(); // We have no encoding. |
| 7978 | Entry &E = I->second; |
| 7979 | if (E.State == Recursive && IncompleteCount) |
| 7980 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7981 | |
| 7982 | if (E.State == Incomplete) { |
| 7983 | // The incomplete type is being used to break out of recursion. |
| 7984 | E.State = IncompleteUsed; |
| 7985 | ++IncompleteUsedCount; |
| 7986 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 7987 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7988 | } |
| 7989 | |
| 7990 | /// The XCore ABI includes a type information section that communicates symbol |
| 7991 | /// type information to the linker. The linker uses this information to verify |
| 7992 | /// safety/correctness of things such as array bound and pointers et al. |
| 7993 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7994 | /// This type information (TypeString) is emitted into meta data for all global |
| 7995 | /// symbols: definitions, declarations, functions & variables. |
| 7996 | /// |
| 7997 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7998 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7999 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8000 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8001 | /// |
| 8002 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8003 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8004 | |
| 8005 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8006 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8007 | CodeGen::CodeGenModule &CGM) const { |
| 8008 | SmallStringEnc Enc; |
| 8009 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8010 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8011 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8012 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8013 | llvm::NamedMDNode *MD = |
| 8014 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8015 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8016 | } |
| 8017 | } |
| 8018 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8019 | //===----------------------------------------------------------------------===// |
| 8020 | // SPIR ABI Implementation |
| 8021 | //===----------------------------------------------------------------------===// |
| 8022 | |
| 8023 | namespace { |
| 8024 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8025 | public: |
| 8026 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8027 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 8028 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8029 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8030 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8031 | }; |
| 8032 | } // End anonymous namespace. |
| 8033 | |
| 8034 | /// Emit SPIR specific metadata: OpenCL and SPIR version. |
| 8035 | void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8036 | CodeGen::CodeGenModule &CGM) const { |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8037 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 8038 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 8039 | llvm::Module &M = CGM.getModule(); |
| 8040 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 8041 | // opencl.spir.version named metadata. |
| 8042 | llvm::Metadata *SPIRVerElts[] = { |
Alexey Bader | b319082 | 2016-12-07 08:38:24 +0000 | [diff] [blame] | 8043 | llvm::ConstantAsMetadata::get( |
| 8044 | llvm::ConstantInt::get(Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), |
| 8045 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 8046 | Int32Ty, (CGM.getLangOpts().OpenCLVersion / 100 > 1) ? 0 : 2))}; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8047 | llvm::NamedMDNode *SPIRVerMD = |
| 8048 | M.getOrInsertNamedMetadata("opencl.spir.version"); |
| 8049 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 8050 | appendOpenCLVersionMD(CGM); |
| 8051 | } |
| 8052 | |
Matt Arsenault | 8afb5cd | 2016-09-07 07:07:59 +0000 | [diff] [blame] | 8053 | static void appendOpenCLVersionMD(CodeGen::CodeGenModule &CGM) { |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 8054 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 8055 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 8056 | llvm::Module &M = CGM.getModule(); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8057 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 8058 | // opencl.ocl.version named metadata node. |
| 8059 | llvm::Metadata *OCLVerElts[] = { |
| 8060 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 8061 | Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), |
| 8062 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 8063 | Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))}; |
| 8064 | llvm::NamedMDNode *OCLVerMD = |
| 8065 | M.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 8066 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 8067 | } |
| 8068 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8069 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8070 | return llvm::CallingConv::SPIR_KERNEL; |
| 8071 | } |
| 8072 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8073 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8074 | const CodeGen::CodeGenModule &CGM, |
| 8075 | TypeStringCache &TSC); |
| 8076 | |
| 8077 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8078 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8079 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8080 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8081 | const RecordDecl *RD, |
| 8082 | const CodeGen::CodeGenModule &CGM, |
| 8083 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8084 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8085 | SmallStringEnc Enc; |
| 8086 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8087 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8088 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8089 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8090 | Enc += "b("; |
| 8091 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8092 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8093 | Enc += ':'; |
| 8094 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8095 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8096 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8097 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8098 | Enc += ')'; |
| 8099 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8100 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8101 | } |
| 8102 | return true; |
| 8103 | } |
| 8104 | |
| 8105 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8106 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8107 | /// Union types have their fields ordered according to the ABI. |
| 8108 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8109 | const CodeGen::CodeGenModule &CGM, |
| 8110 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8111 | // Append the cached TypeString if we have one. |
| 8112 | StringRef TypeString = TSC.lookupStr(ID); |
| 8113 | if (!TypeString.empty()) { |
| 8114 | Enc += TypeString; |
| 8115 | return true; |
| 8116 | } |
| 8117 | |
| 8118 | // Start to emit an incomplete TypeString. |
| 8119 | size_t Start = Enc.size(); |
| 8120 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8121 | Enc += '('; |
| 8122 | if (ID) |
| 8123 | Enc += ID->getName(); |
| 8124 | Enc += "){"; |
| 8125 | |
| 8126 | // We collect all encoded fields and order as necessary. |
| 8127 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8128 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8129 | if (RD && !RD->field_empty()) { |
| 8130 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8131 | // so that recursive calls to this RecordType will use it whilst building a |
| 8132 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8133 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8134 | std::string StubEnc(Enc.substr(Start).str()); |
| 8135 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8136 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8137 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8138 | (void) TSC.removeIncomplete(ID); |
| 8139 | return false; |
| 8140 | } |
| 8141 | IsRecursive = TSC.removeIncomplete(ID); |
| 8142 | // The ABI requires unions to be sorted but not structures. |
| 8143 | // See FieldEncoding::operator< for sort algorithm. |
| 8144 | if (RT->isUnionType()) |
| 8145 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8146 | // We can now complete the TypeString. |
| 8147 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8148 | for (unsigned I = 0; I != E; ++I) { |
| 8149 | if (I) |
| 8150 | Enc += ','; |
| 8151 | Enc += FE[I].str(); |
| 8152 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8153 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8154 | Enc += '}'; |
| 8155 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8156 | return true; |
| 8157 | } |
| 8158 | |
| 8159 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8160 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8161 | TypeStringCache &TSC, |
| 8162 | const IdentifierInfo *ID) { |
| 8163 | // Append the cached TypeString if we have one. |
| 8164 | StringRef TypeString = TSC.lookupStr(ID); |
| 8165 | if (!TypeString.empty()) { |
| 8166 | Enc += TypeString; |
| 8167 | return true; |
| 8168 | } |
| 8169 | |
| 8170 | size_t Start = Enc.size(); |
| 8171 | Enc += "e("; |
| 8172 | if (ID) |
| 8173 | Enc += ID->getName(); |
| 8174 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8175 | |
| 8176 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8177 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8178 | SmallVector<FieldEncoding, 16> FE; |
| 8179 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8180 | ++I) { |
| 8181 | SmallStringEnc EnumEnc; |
| 8182 | EnumEnc += "m("; |
| 8183 | EnumEnc += I->getName(); |
| 8184 | EnumEnc += "){"; |
| 8185 | I->getInitVal().toString(EnumEnc); |
| 8186 | EnumEnc += '}'; |
| 8187 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8188 | } |
| 8189 | std::sort(FE.begin(), FE.end()); |
| 8190 | unsigned E = FE.size(); |
| 8191 | for (unsigned I = 0; I != E; ++I) { |
| 8192 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8193 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8194 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8195 | } |
| 8196 | } |
| 8197 | Enc += '}'; |
| 8198 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8199 | return true; |
| 8200 | } |
| 8201 | |
| 8202 | /// Appends type's qualifier to Enc. |
| 8203 | /// This is done prior to appending the type's encoding. |
| 8204 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8205 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8206 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8207 | int Lookup = 0; |
| 8208 | if (QT.isConstQualified()) |
| 8209 | Lookup += 1<<0; |
| 8210 | if (QT.isRestrictQualified()) |
| 8211 | Lookup += 1<<1; |
| 8212 | if (QT.isVolatileQualified()) |
| 8213 | Lookup += 1<<2; |
| 8214 | Enc += Table[Lookup]; |
| 8215 | } |
| 8216 | |
| 8217 | /// Appends built-in types to Enc. |
| 8218 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8219 | const char *EncType; |
| 8220 | switch (BT->getKind()) { |
| 8221 | case BuiltinType::Void: |
| 8222 | EncType = "0"; |
| 8223 | break; |
| 8224 | case BuiltinType::Bool: |
| 8225 | EncType = "b"; |
| 8226 | break; |
| 8227 | case BuiltinType::Char_U: |
| 8228 | EncType = "uc"; |
| 8229 | break; |
| 8230 | case BuiltinType::UChar: |
| 8231 | EncType = "uc"; |
| 8232 | break; |
| 8233 | case BuiltinType::SChar: |
| 8234 | EncType = "sc"; |
| 8235 | break; |
| 8236 | case BuiltinType::UShort: |
| 8237 | EncType = "us"; |
| 8238 | break; |
| 8239 | case BuiltinType::Short: |
| 8240 | EncType = "ss"; |
| 8241 | break; |
| 8242 | case BuiltinType::UInt: |
| 8243 | EncType = "ui"; |
| 8244 | break; |
| 8245 | case BuiltinType::Int: |
| 8246 | EncType = "si"; |
| 8247 | break; |
| 8248 | case BuiltinType::ULong: |
| 8249 | EncType = "ul"; |
| 8250 | break; |
| 8251 | case BuiltinType::Long: |
| 8252 | EncType = "sl"; |
| 8253 | break; |
| 8254 | case BuiltinType::ULongLong: |
| 8255 | EncType = "ull"; |
| 8256 | break; |
| 8257 | case BuiltinType::LongLong: |
| 8258 | EncType = "sll"; |
| 8259 | break; |
| 8260 | case BuiltinType::Float: |
| 8261 | EncType = "ft"; |
| 8262 | break; |
| 8263 | case BuiltinType::Double: |
| 8264 | EncType = "d"; |
| 8265 | break; |
| 8266 | case BuiltinType::LongDouble: |
| 8267 | EncType = "ld"; |
| 8268 | break; |
| 8269 | default: |
| 8270 | return false; |
| 8271 | } |
| 8272 | Enc += EncType; |
| 8273 | return true; |
| 8274 | } |
| 8275 | |
| 8276 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8277 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8278 | const CodeGen::CodeGenModule &CGM, |
| 8279 | TypeStringCache &TSC) { |
| 8280 | Enc += "p("; |
| 8281 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8282 | return false; |
| 8283 | Enc += ')'; |
| 8284 | return true; |
| 8285 | } |
| 8286 | |
| 8287 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8288 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8289 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8290 | const CodeGen::CodeGenModule &CGM, |
| 8291 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8292 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8293 | return false; |
| 8294 | Enc += "a("; |
| 8295 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8296 | CAT->getSize().toStringUnsigned(Enc); |
| 8297 | else |
| 8298 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8299 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8300 | // The Qualifiers should be attached to the type rather than the array. |
| 8301 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8302 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8303 | return false; |
| 8304 | Enc += ')'; |
| 8305 | return true; |
| 8306 | } |
| 8307 | |
| 8308 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8309 | /// and the arguments. |
| 8310 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8311 | const CodeGen::CodeGenModule &CGM, |
| 8312 | TypeStringCache &TSC) { |
| 8313 | Enc += "f{"; |
| 8314 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8315 | return false; |
| 8316 | Enc += "}("; |
| 8317 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8318 | // N.B. we are only interested in the adjusted param types. |
| 8319 | auto I = FPT->param_type_begin(); |
| 8320 | auto E = FPT->param_type_end(); |
| 8321 | if (I != E) { |
| 8322 | do { |
| 8323 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8324 | return false; |
| 8325 | ++I; |
| 8326 | if (I != E) |
| 8327 | Enc += ','; |
| 8328 | } while (I != E); |
| 8329 | if (FPT->isVariadic()) |
| 8330 | Enc += ",va"; |
| 8331 | } else { |
| 8332 | if (FPT->isVariadic()) |
| 8333 | Enc += "va"; |
| 8334 | else |
| 8335 | Enc += '0'; |
| 8336 | } |
| 8337 | } |
| 8338 | Enc += ')'; |
| 8339 | return true; |
| 8340 | } |
| 8341 | |
| 8342 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8343 | /// type encodings. |
| 8344 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8345 | const CodeGen::CodeGenModule &CGM, |
| 8346 | TypeStringCache &TSC) { |
| 8347 | |
| 8348 | QualType QT = QType.getCanonicalType(); |
| 8349 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8350 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8351 | // The Qualifiers should be attached to the type rather than the array. |
| 8352 | // Thus we don't call appendQualifier() here. |
| 8353 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8354 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8355 | appendQualifier(Enc, QT); |
| 8356 | |
| 8357 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8358 | return appendBuiltinType(Enc, BT); |
| 8359 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8360 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8361 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8362 | |
| 8363 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8364 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8365 | |
| 8366 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8367 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8368 | |
| 8369 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8370 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8371 | |
| 8372 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8373 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8374 | |
| 8375 | return false; |
| 8376 | } |
| 8377 | |
| 8378 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8379 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8380 | if (!D) |
| 8381 | return false; |
| 8382 | |
| 8383 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8384 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8385 | return false; |
| 8386 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8387 | } |
| 8388 | |
| 8389 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8390 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8391 | return false; |
| 8392 | QualType QT = VD->getType().getCanonicalType(); |
| 8393 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8394 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8395 | // The Qualifiers should be attached to the type rather than the array. |
| 8396 | // Thus we don't call appendQualifier() here. |
| 8397 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8398 | } |
| 8399 | return appendType(Enc, QT, CGM, TSC); |
| 8400 | } |
| 8401 | return false; |
| 8402 | } |
| 8403 | |
| 8404 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8405 | //===----------------------------------------------------------------------===// |
| 8406 | // Driver code |
| 8407 | //===----------------------------------------------------------------------===// |
| 8408 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8409 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8410 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8413 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8414 | if (TheTargetCodeGenInfo) |
| 8415 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8416 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8417 | // Helper to set the unique_ptr while still keeping the return value. |
| 8418 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8419 | this->TheTargetCodeGenInfo.reset(P); |
| 8420 | return *P; |
| 8421 | }; |
| 8422 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8423 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8424 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8425 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8426 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8427 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8428 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8429 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8430 | case llvm::Triple::mips: |
| 8431 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8432 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8433 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8434 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8435 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8436 | case llvm::Triple::mips64: |
| 8437 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8438 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8439 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8440 | case llvm::Triple::avr: |
| 8441 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8442 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8443 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8444 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8445 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8446 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8447 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8448 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8449 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8450 | } |
| 8451 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8452 | case llvm::Triple::wasm32: |
| 8453 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8454 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8455 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8456 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8457 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8458 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8459 | case llvm::Triple::thumbeb: { |
| 8460 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8461 | return SetCGInfo( |
| 8462 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8463 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8464 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8465 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8466 | StringRef ABIStr = getTarget().getABI(); |
| 8467 | if (ABIStr == "apcs-gnu") |
| 8468 | Kind = ARMABIInfo::APCS; |
| 8469 | else if (ABIStr == "aapcs16") |
| 8470 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8471 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8472 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8473 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8474 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8475 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8476 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8477 | |
| 8478 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8479 | } |
| 8480 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8481 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8482 | return SetCGInfo( |
| 8483 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8484 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8485 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8486 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8487 | if (getTarget().getABI() == "elfv2") |
| 8488 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8489 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8490 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8491 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8492 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8493 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8494 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8495 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8496 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8497 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8498 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8499 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8500 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8501 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8502 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8503 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8504 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8505 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8506 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8507 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8508 | case llvm::Triple::nvptx: |
| 8509 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8510 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8511 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8512 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8513 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8514 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8515 | case llvm::Triple::systemz: { |
| 8516 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8517 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8518 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8519 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8520 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 8521 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8522 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8523 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8524 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8525 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8526 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8527 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8528 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8529 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8530 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8531 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8532 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8533 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8534 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8535 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8536 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8537 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8538 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8539 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8540 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8541 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8542 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8543 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8544 | X86AVXABILevel AVXLevel = |
| 8545 | (ABI == "avx512" |
| 8546 | ? X86AVXABILevel::AVX512 |
| 8547 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8548 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8549 | switch (Triple.getOS()) { |
| 8550 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8551 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8552 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8553 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8554 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8555 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8556 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8557 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8558 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8559 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8560 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8561 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8562 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8563 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8564 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8565 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8566 | case llvm::Triple::sparc: |
| 8567 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8568 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8569 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8570 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8571 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8572 | case llvm::Triple::spir: |
| 8573 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8574 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8575 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8576 | } |